[llvm-commits] [llvm] r95995 - in /llvm/trunk: lib/Transforms/IPO/ConstantMerge.cpp test/Transforms/ConstantMerge/dont-merge.ll

Chris Lattner sabre at nondot.org
Fri Feb 12 10:17:23 PST 2010


Author: lattner
Date: Fri Feb 12 12:17:23 2010
New Revision: 95995

URL: http://llvm.org/viewvc/llvm-project?rev=95995&view=rev
Log:
1. modernize the constantmerge pass, using densemap/smallvector.
2. don't bother trying to merge globals in non-default sections,
   doing so is quite dubious at best anyway.
3. fix a bug reported by Arnaud de Grandmaison where we'd try to
   merge two globals in different address spaces.


Modified:
    llvm/trunk/lib/Transforms/IPO/ConstantMerge.cpp
    llvm/trunk/test/Transforms/ConstantMerge/dont-merge.ll

Modified: llvm/trunk/lib/Transforms/IPO/ConstantMerge.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/ConstantMerge.cpp?rev=95995&r1=95994&r2=95995&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/IPO/ConstantMerge.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/ConstantMerge.cpp Fri Feb 12 12:17:23 2010
@@ -19,10 +19,11 @@
 
 #define DEBUG_TYPE "constmerge"
 #include "llvm/Transforms/IPO.h"
+#include "llvm/DerivedTypes.h"
 #include "llvm/Module.h"
 #include "llvm/Pass.h"
+#include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/Statistic.h"
-#include <map>
 using namespace llvm;
 
 STATISTIC(NumMerged, "Number of global constants merged");
@@ -48,10 +49,10 @@
 bool ConstantMerge::runOnModule(Module &M) {
   // Map unique constant/section pairs to globals.  We don't want to merge
   // globals in different sections.
-  std::map<std::pair<Constant*, std::string>, GlobalVariable*> CMap;
+  DenseMap<Constant*, GlobalVariable*> CMap;
 
   // Replacements - This vector contains a list of replacements to perform.
-  std::vector<std::pair<GlobalVariable*, GlobalVariable*> > Replacements;
+  SmallVector<std::pair<GlobalVariable*, GlobalVariable*>, 32> Replacements;
 
   bool MadeChange = false;
 
@@ -76,19 +77,21 @@
         continue;
       }
       
-      // Only process constants with initializers.
-      if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
-        Constant *Init = GV->getInitializer();
-
-        // Check to see if the initializer is already known.
-        GlobalVariable *&Slot = CMap[std::make_pair(Init, GV->getSection())];
-
-        if (Slot == 0) {    // Nope, add it to the map.
-          Slot = GV;
-        } else if (GV->hasLocalLinkage()) {    // Yup, this is a duplicate!
-          // Make all uses of the duplicate constant use the canonical version.
-          Replacements.push_back(std::make_pair(GV, Slot));
-        }
+      // Only process constants with initializers in the default addres space.
+      if (!GV->isConstant() ||!GV->hasDefinitiveInitializer() ||
+          GV->getType()->getAddressSpace() != 0 || !GV->getSection().empty())
+        continue;
+      
+      Constant *Init = GV->getInitializer();
+
+      // Check to see if the initializer is already known.
+      GlobalVariable *&Slot = CMap[Init];
+
+      if (Slot == 0) {    // Nope, add it to the map.
+        Slot = GV;
+      } else if (GV->hasLocalLinkage()) {    // Yup, this is a duplicate!
+        // Make all uses of the duplicate constant use the canonical version.
+        Replacements.push_back(std::make_pair(GV, Slot));
       }
     }
 
@@ -100,11 +103,11 @@
     // now.  This avoid invalidating the pointers in CMap, which are unneeded
     // now.
     for (unsigned i = 0, e = Replacements.size(); i != e; ++i) {
-      // Eliminate any uses of the dead global...
+      // Eliminate any uses of the dead global.
       Replacements[i].first->replaceAllUsesWith(Replacements[i].second);
 
-      // Delete the global value from the module...
-      M.getGlobalList().erase(Replacements[i].first);
+      // Delete the global value from the module.
+      Replacements[i].first->eraseFromParent();
     }
 
     NumMerged += Replacements.size();

Modified: llvm/trunk/test/Transforms/ConstantMerge/dont-merge.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/ConstantMerge/dont-merge.ll?rev=95995&r1=95994&r2=95995&view=diff

==============================================================================
--- llvm/trunk/test/Transforms/ConstantMerge/dont-merge.ll (original)
+++ llvm/trunk/test/Transforms/ConstantMerge/dont-merge.ll Fri Feb 12 12:17:23 2010
@@ -1,16 +1,30 @@
-; RUN: opt < %s -constmerge -S | grep foo
-; RUN: opt < %s -constmerge -S | grep bar
+; RUN: opt < %s -constmerge -S | FileCheck %s
 
-; Don't merge constants in different sections.
+; Don't merge constants with specified sections.
 
- at G1 = internal constant i32 1, section "foo"            ; <i32*> [#uses=1]
- at G2 = internal constant i32 1, section "bar"            ; <i32*> [#uses=1]
- at G3 = internal constant i32 1, section "bar"            ; <i32*> [#uses=1]
-
-define void @test(i32** %P1, i32** %P2, i32** %P3) {
-        store i32* @G1, i32** %P1
-        store i32* @G2, i32** %P2
-        store i32* @G3, i32** %P3
+ at T1G1 = internal constant i32 1, section "foo"
+ at T1G2 = internal constant i32 1, section "bar"
+ at T1G3 = internal constant i32 1, section "bar"
+
+; CHECK: @T1G1
+; CHECK: @T1G2
+; CHECK: @T1G3
+
+define void @test1(i32** %P1, i32** %P2, i32** %P3) {
+        store i32* @T1G1, i32** %P1
+        store i32* @T1G2, i32** %P2
+        store i32* @T1G3, i32** %P3
         ret void
 }
 
+ at T2a = internal constant i32 224
+ at T2b = internal addrspace(30) constant i32 224
+
+; CHECK: @T2a
+; CHECK: @T2b
+
+define void @test2(i32** %P1, i32 addrspace(30)** %P2) {
+        store i32* @T2a, i32** %P1
+        store i32 addrspace(30)*  @T2b, i32 addrspace(30)** %P2
+        ret void
+}





More information about the llvm-commits mailing list