add an assertion to make it clear that PHI nodes are not allowed.
[oota-llvm.git] / lib / Transforms / IPO / GlobalDCE.cpp
index 90150689cc87e107cada352fa453706c352bbe74..c28d5e3e57a06fe0eac2dcd7f76e035e43022fa1 100644 (file)
@@ -2,8 +2,8 @@
 //
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by the LLVM research group and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 //
 #include <set>
 using namespace llvm;
 
+STATISTIC(NumAliases  , "Number of global aliases removed");
 STATISTIC(NumFunctions, "Number of functions removed");
 STATISTIC(NumVariables, "Number of global variables removed");
 
 namespace {
   struct VISIBILITY_HIDDEN GlobalDCE : public ModulePass {
+    static char ID; // Pass identification, replacement for typeid
+    GlobalDCE() : ModulePass(&ID) {}
+
     // run - Do the GlobalDCE pass on the specified module, optionally updating
     // the specified callgraph to reflect the changes.
     //
@@ -38,7 +42,7 @@ namespace {
   private:
     std::set<GlobalValue*> AliveGlobals;
 
-    /// MarkGlobalIsNeeded - the specific global value as needed, and
+    /// GlobalIsNeeded - mark the specific global value as needed, and
     /// recursively mark anything that it uses as also needed.
     void GlobalIsNeeded(GlobalValue *GV);
     void MarkUsedGlobalsAsNeeded(Constant *C);
@@ -46,9 +50,11 @@ namespace {
     bool SafeToDestroyConstant(Constant* C);
     bool RemoveUnusedGlobalValue(GlobalValue &GV);
   };
-  RegisterPass<GlobalDCE> X("globaldce", "Dead Global Elimination");
 }
 
+char GlobalDCE::ID = 0;
+static RegisterPass<GlobalDCE> X("globaldce", "Dead Global Elimination");
+
 ModulePass *llvm::createGlobalDCEPass() { return new GlobalDCE(); }
 
 bool GlobalDCE::runOnModule(Module &M) {
@@ -57,7 +63,7 @@ bool GlobalDCE::runOnModule(Module &M) {
   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
     Changed |= RemoveUnusedGlobalValue(*I);
     // Functions with external linkage are needed if they have a body
-    if ((!I->hasInternalLinkage() && !I->hasLinkOnceLinkage()) &&
+    if ((!I->hasLocalLinkage() && !I->hasLinkOnceLinkage()) &&
         !I->isDeclaration())
       GlobalIsNeeded(I);
   }
@@ -67,16 +73,17 @@ bool GlobalDCE::runOnModule(Module &M) {
     Changed |= RemoveUnusedGlobalValue(*I);
     // Externally visible & appending globals are needed, if they have an
     // initializer.
-    if ((!I->hasInternalLinkage() && !I->hasLinkOnceLinkage()) &&
+    if ((!I->hasLocalLinkage() && !I->hasLinkOnceLinkage()) &&
         !I->isDeclaration())
       GlobalIsNeeded(I);
   }
 
-
   for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
        I != E; ++I) {
-    // Aliases are always needed even if they are not used.
-    MarkUsedGlobalsAsNeeded(cast<Constant>(I->getAliasee()));
+    Changed |= RemoveUnusedGlobalValue(*I);
+    // Externally visible aliases are needed.
+    if (!I->hasLocalLinkage() && !I->hasLinkOnceLinkage())
+      GlobalIsNeeded(I);
   }
 
   // Now that all globals which are needed are in the AliveGlobals set, we loop
@@ -91,7 +98,6 @@ bool GlobalDCE::runOnModule(Module &M) {
       I->setInitializer(0);
     }
 
-
   // The second pass drops the bodies of functions which are dead...
   std::vector<Function*> DeadFunctions;
   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
@@ -101,8 +107,17 @@ bool GlobalDCE::runOnModule(Module &M) {
         I->deleteBody();
     }
 
+  // The third pass drops targets of aliases which are dead...
+  std::vector<GlobalAlias*> DeadAliases;
+  for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); I != E;
+       ++I)
+    if (!AliveGlobals.count(I)) {
+      DeadAliases.push_back(I);
+      I->setAliasee(0);
+    }
+
   if (!DeadFunctions.empty()) {
-    // Now that all interreferences have been dropped, delete the actual objects
+    // Now that all interferences have been dropped, delete the actual objects
     // themselves.
     for (unsigned i = 0, e = DeadFunctions.size(); i != e; ++i) {
       RemoveUnusedGlobalValue(*DeadFunctions[i]);
@@ -121,18 +136,28 @@ bool GlobalDCE::runOnModule(Module &M) {
     Changed = true;
   }
 
+  // Now delete any dead aliases.
+  if (!DeadAliases.empty()) {
+    for (unsigned i = 0, e = DeadAliases.size(); i != e; ++i) {
+      RemoveUnusedGlobalValue(*DeadAliases[i]);
+      M.getAliasList().erase(DeadAliases[i]);
+    }
+    NumAliases += DeadAliases.size();
+    Changed = true;
+  }
+
   // Make sure that all memory is released
   AliveGlobals.clear();
   return Changed;
 }
 
-/// MarkGlobalIsNeeded - the specific global value as needed, and
+/// GlobalIsNeeded - the specific global value as needed, and
 /// recursively mark anything that it uses as also needed.
 void GlobalDCE::GlobalIsNeeded(GlobalValue *G) {
-  std::set<GlobalValue*>::iterator I = AliveGlobals.lower_bound(G);
+  std::set<GlobalValue*>::iterator I = AliveGlobals.find(G);
 
   // If the global is already in the set, no need to reprocess it.
-  if (I != AliveGlobals.end() && *I == G) return;
+  if (I != AliveGlobals.end()) return;
 
   // Otherwise insert it now, so we do not infinitely recurse
   AliveGlobals.insert(I, G);
@@ -142,7 +167,10 @@ void GlobalDCE::GlobalIsNeeded(GlobalValue *G) {
     // referenced by the initializer to the alive set.
     if (GV->hasInitializer())
       MarkUsedGlobalsAsNeeded(GV->getInitializer());
-  } else if (!isa<GlobalAlias>(G)) {
+  } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(G)) {
+    // The target of a global alias is needed.
+    MarkUsedGlobalsAsNeeded(GA->getAliasee());
+  } else {
     // Otherwise this must be a function object.  We have to scan the body of
     // the function looking for constants and global values which are used as
     // operands.  Any operands of these types must be processed to ensure that