add an assertion to make it clear that PHI nodes are not allowed.
[oota-llvm.git] / lib / Transforms / IPO / GlobalDCE.cpp
index ac23760bf20183def828d0abcf9a6d2199e42b13..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.
 //
 //===----------------------------------------------------------------------===//
 //
 //
 //===----------------------------------------------------------------------===//
 
+#define DEBUG_TYPE "globaldce"
 #include "llvm/Transforms/IPO.h"
 #include "llvm/Constants.h"
 #include "llvm/Module.h"
 #include "llvm/Pass.h"
 #include "llvm/ADT/Statistic.h"
+#include "llvm/Support/Compiler.h"
 #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 {
-  Statistic NumFunctions("globaldce","Number of functions removed");
-  Statistic NumVariables("globaldce","Number of global variables removed");
+  struct VISIBILITY_HIDDEN GlobalDCE : public ModulePass {
+    static char ID; // Pass identification, replacement for typeid
+    GlobalDCE() : ModulePass(&ID) {}
 
-  struct GlobalDCE : public ModulePass {
     // run - Do the GlobalDCE pass on the specified module, optionally updating
     // the specified callgraph to reflect the changes.
     //
@@ -36,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);
@@ -44,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) {
@@ -55,20 +63,28 @@ 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()) &&
-        !I->isExternal())
+    if ((!I->hasLocalLinkage() && !I->hasLinkOnceLinkage()) &&
+        !I->isDeclaration())
       GlobalIsNeeded(I);
   }
 
-  for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) {
+  for (Module::global_iterator I = M.global_begin(), E = M.global_end();
+       I != E; ++I) {
     Changed |= RemoveUnusedGlobalValue(*I);
     // Externally visible & appending globals are needed, if they have an
     // initializer.
-    if ((!I->hasInternalLinkage() && !I->hasLinkOnceLinkage()) &&
-        !I->isExternal())
+    if ((!I->hasLocalLinkage() && !I->hasLinkOnceLinkage()) &&
+        !I->isDeclaration())
       GlobalIsNeeded(I);
   }
 
+  for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
+       I != E; ++I) {
+    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
   // through the program, deleting those which are not alive.
@@ -82,18 +98,26 @@ 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)
     if (!AliveGlobals.count(I)) {
       DeadFunctions.push_back(I);         // Keep track of dead globals
-      if (!I->isExternal())
+      if (!I->isDeclaration())
         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]);
@@ -112,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);
@@ -133,6 +167,9 @@ void GlobalDCE::GlobalIsNeeded(GlobalValue *G) {
     // referenced by the initializer to the alive set.
     if (GV->hasInitializer())
       MarkUsedGlobalsAsNeeded(GV->getInitializer());
+  } 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