view gen/passes/StripExternals.cpp @ 1650:40bd4a0d4870

Update to work with LLVM 2.7. Removed use of dyn_cast, llvm no compiles without exceptions and rtti by default. We do need exceptions for the libconfig stuff, but rtti isn't necessary (anymore). Debug info needs to be rewritten, as in LLVM 2.7 the format has completely changed. To have something to look at while rewriting, the old code has been wrapped inside #ifndef DISABLE_DEBUG_INFO , this means that you have to define this to compile at the moment. Updated tango 0.99.9 patch to include updated EH runtime code, which is needed for LLVM 2.7 as well.
author Tomas Lindquist Olsen
date Wed, 19 May 2010 12:42:32 +0200
parents ed0feda76820
children
line wrap: on
line source

//===-- StripExternals.cpp - Strip available_externally symbols -----------===//
//
//                             The LLVM D Compiler
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This transform stips the bodies of available_externally functions and
// initializers of available_externally globals, turning them into external
// declarations.
// This is useful to allow Global DCE (-globaldce) to clean up references to
// globals only used by available_externally functions and initializers.
//
//===----------------------------------------------------------------------===//

#define DEBUG_TYPE "strip-externals"

#include "Passes.h"

#include "llvm/Module.h"
#include "llvm/Pass.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;

STATISTIC(NumFunctions, "Number of function bodies removed");
STATISTIC(NumVariables, "Number of global initializers removed");

namespace {
  struct VISIBILITY_HIDDEN StripExternals : public ModulePass {
    static char ID; // Pass identification, replacement for typeid
    StripExternals() : ModulePass(&ID) {}

    // run - Do the StripExternals pass on the specified module.
    //
    bool runOnModule(Module &M);
  };
}

char StripExternals::ID = 0;
static RegisterPass<StripExternals>
X("strip-externals", "Strip available_externally bodies and initializers");

ModulePass *createStripExternalsPass() { return new StripExternals(); }

bool StripExternals::runOnModule(Module &M) {
  bool Changed = false;

  for (Module::iterator I = M.begin(); I != M.end(); ) {
    if (I->hasAvailableExternallyLinkage()) {
      assert(!I->isDeclaration()&&"Declarations can't be available_externally");
      Changed = true;
      ++NumFunctions;
      if (I->use_empty()) {
        DEBUG(errs() << "Deleting function: " << *I);
        Module::iterator todelete = I;
        ++I;
        todelete->eraseFromParent();
        continue;
      } else {
        I->deleteBody();
        DEBUG(errs() << "Deleted function body: " << *I);
      }
    }
    ++I;
  }

  for (Module::global_iterator I = M.global_begin();
       I != M.global_end(); ) {
    if (I->hasAvailableExternallyLinkage()) {
      assert(!I->isDeclaration()&&"Declarations can't be available_externally");
      Changed = true;
      ++NumVariables;
      if (I->use_empty()) {
        DEBUG(errs() << "Deleting global: " << *I);
        Module::global_iterator todelete = I;
        ++I;
        todelete->eraseFromParent();
        continue;
      } else {
        I->setInitializer(0);
        I->setLinkage(GlobalValue::ExternalLinkage);
        DEBUG(errs() << "Deleted initializer: " << *I);
      }
    }
    ++I;
  }

  return Changed;
}