cleanup as per Duncan's review
[oota-llvm.git] / lib / Transforms / IPO / ConstantMerge.cpp
index 291653376ed0d081c0dce35ccc1f7bb67a4a4c67..86cfcc4624aab765a1e426da4eed782313fd7417 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 "llvm/Module.h"
 #include "llvm/Pass.h"
 #include "llvm/ADT/Statistic.h"
+#include "llvm/Support/Compiler.h"
+#include <map>
 using namespace llvm;
 
 STATISTIC(NumMerged, "Number of global constants merged");
 
 namespace {
-  struct ConstantMerge : public ModulePass {
+  struct VISIBILITY_HIDDEN ConstantMerge : public ModulePass {
+    static char ID; // Pass identification, replacement for typeid
+    ConstantMerge() : ModulePass((intptr_t)&ID) {}
+
     // run - For this pass, process all of the globals in the module,
     // eliminating duplicate constants.
     //
     bool runOnModule(Module &M);
   };
-
-  RegisterPass<ConstantMerge>X("constmerge","Merge Duplicate Global Constants");
 }
 
+char ConstantMerge::ID = 0;
+static RegisterPass<ConstantMerge>
+X("constmerge", "Merge Duplicate Global Constants");
+
 ModulePass *llvm::createConstantMergePass() { return new ConstantMerge(); }
 
 bool ConstantMerge::runOnModule(Module &M) {
@@ -59,8 +66,17 @@ bool ConstantMerge::runOnModule(Module &M) {
     // because doing so may cause initializers of other globals to be rewritten,
     // invalidating the Constant* pointers in CMap.
     //
-    for (Module::global_iterator GV = M.global_begin(), E = M.global_end();
-         GV != E; ++GV)
+    for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
+         GVI != E; ) {
+      GlobalVariable *GV = GVI++;
+      
+      // If this GV is dead, remove it.
+      GV->removeDeadConstantUsers();
+      if (GV->use_empty() && GV->hasInternalLinkage()) {
+        GV->eraseFromParent();
+        continue;
+      }
+      
       // Only process constants with initializers.
       if (GV->isConstant() && GV->hasInitializer()) {
         Constant *Init = GV->getInitializer();
@@ -73,12 +89,9 @@ bool ConstantMerge::runOnModule(Module &M) {
         } else if (GV->hasInternalLinkage()) {    // Yup, this is a duplicate!
           // Make all uses of the duplicate constant use the canonical version.
           Replacements.push_back(std::make_pair(GV, Slot));
-        } else if (GV->hasInternalLinkage()) {
-          // Make all uses of the duplicate constant use the canonical version.
-          Replacements.push_back(std::make_pair(Slot, GV));
-          Slot = GV;
         }
       }
+    }
 
     if (Replacements.empty())
       return MadeChange;