For PR1043:
[oota-llvm.git] / lib / Transforms / IPO / Internalize.cpp
index d583d8886842c1ba0a421e3a23ed0b1cd2382872..bf99235fb8b00b1057777894ca60aa6388ac91ef 100644 (file)
@@ -13,6 +13,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#define DEBUG_TYPE "internalize"
 #include "llvm/Transforms/IPO.h"
 #include "llvm/Pass.h"
 #include "llvm/Module.h"
 #include <set>
 using namespace llvm;
 
+STATISTIC(NumFunctions, "Number of functions internalized");
+STATISTIC(NumGlobals  , "Number of global vars internalized");
+
 namespace {
-  Statistic<> NumFunctions("internalize", "Number of functions internalized");
-  Statistic<> NumGlobals  ("internalize", "Number of global vars internalized");
 
   // APIFile - A file which contains a list of symbols that should not be marked
   // external.
@@ -44,10 +46,11 @@ namespace {
     bool DontInternalize;
   public:
     InternalizePass(bool InternalizeEverything = true);
+    InternalizePass(const std::vector <const char *>& exportList);
     void LoadFile(const char *Filename);
     virtual bool runOnModule(Module &M);
   };
-  RegisterOpt<InternalizePass> X("internalize", "Internalize Global Symbols");
+  RegisterPass<InternalizePass> X("internalize", "Internalize Global Symbols");
 } // end anonymous namespace
 
 InternalizePass::InternalizePass(bool InternalizeEverything) 
@@ -61,12 +64,19 @@ InternalizePass::InternalizePass(bool InternalizeEverything)
     DontInternalize = true;
 }
 
+InternalizePass::InternalizePass(const std::vector<const char *>&exportList) 
+  : DontInternalize(false){
+  for(std::vector<const char *>::const_iterator itr = exportList.begin();
+       itr != exportList.end(); itr++) {
+    ExternalNames.insert(*itr);
+  }
+}
+
 void InternalizePass::LoadFile(const char *Filename) {
   // Load the APIFile...
   std::ifstream In(Filename);
   if (!In.good()) {
-    std::cerr << "WARNING: Internalize couldn't load file '" << Filename
-    << "'!\n";
+    cerr << "WARNING: Internalize couldn't load file '" << Filename << "'!\n";
     return;   // Do not internalize anything...
   }
   while (In) {
@@ -103,7 +113,7 @@ bool InternalizePass::runOnModule(Module &M) {
       I->setLinkage(GlobalValue::InternalLinkage);
       Changed = true;
       ++NumFunctions;
-      DEBUG(std::cerr << "Internalizing func " << I->getName() << "\n");
+      DOUT << "Internalizing func " << I->getName() << "\n";
     }
   
   // Never internalize the llvm.used symbol.  It is used to implement
@@ -111,9 +121,10 @@ bool InternalizePass::runOnModule(Module &M) {
   ExternalNames.insert("llvm.used");
   
   // Never internalize anchors used by the debugger, else the debugger won't
-  // find them.
-  ExternalNames.insert("llvm.dbg.translation_units");
-  ExternalNames.insert("llvm.dbg.globals");
+  // find them.  (see MachineDebugInfo.)
+  ExternalNames.insert("llvm.dbg.compile_units");
+  ExternalNames.insert("llvm.dbg.global_variables");
+  ExternalNames.insert("llvm.dbg.subprograms");
       
   // Mark all global variables with initializers as internal as well.
   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
@@ -126,18 +137,21 @@ bool InternalizePass::runOnModule(Module &M) {
       //
       if (I->hasAppendingLinkage() && (I->getName() == "llvm.global_ctors" ||
                                        I->getName() == "llvm.global_dtors")) {
-        I->setConstant(true);
-        
         // If the global ctors/dtors list has no uses, do not internalize it, as
         // there is no __main in this program, so the asmprinter should handle
         // it.
         if (I->use_empty()) continue;
+        // Otherwise, also mark the list constant, as we know that it will not
+        // be mutated any longer, and the makes simple IPO xforms automatically
+        // better.
+        I->setConstant(true);
       }
       
       I->setLinkage(GlobalValue::InternalLinkage);
       Changed = true;
       ++NumGlobals;
-      DEBUG(std::cerr << "Internalized gvar " << I->getName() << "\n");
+      DOUT << "Internalized gvar " << I->getName() << "\n";
     }
       
   return Changed;
@@ -146,3 +160,7 @@ bool InternalizePass::runOnModule(Module &M) {
 ModulePass *llvm::createInternalizePass(bool InternalizeEverything) {
   return new InternalizePass(InternalizeEverything);
 }
+
+ModulePass *llvm::createInternalizePass(const std::vector <const char *> &el) {
+  return new InternalizePass(el);
+}