[llvm-commits] [llvm] r123541 - in /llvm/trunk: lib/Transforms/IPO/ConstantMerge.cpp test/Transforms/ConstantMerge/2011-01-15-EitherOrder.ll

Nick Lewycky nicholas at mxc.ca
Sat Jan 15 10:14:21 PST 2011


Author: nicholas
Date: Sat Jan 15 12:14:21 2011
New Revision: 123541

URL: http://llvm.org/viewvc/llvm-project?rev=123541&view=rev
Log:
Make constmerge a two-pass algorithm so that it won't miss merging
opporuntities. Fixes PR8978.

Added:
    llvm/trunk/test/Transforms/ConstantMerge/2011-01-15-EitherOrder.ll
Modified:
    llvm/trunk/lib/Transforms/IPO/ConstantMerge.cpp

Modified: llvm/trunk/lib/Transforms/IPO/ConstantMerge.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/ConstantMerge.cpp?rev=123541&r1=123540&r2=123541&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/IPO/ConstantMerge.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/ConstantMerge.cpp Sat Jan 15 12:14:21 2011
@@ -101,15 +101,18 @@
         continue;
       }
       
-      // Only process constants with initializers in the default addres space.
+      // Only process constants with initializers in the default address space.
       if (!GV->isConstant() ||!GV->hasDefinitiveInitializer() ||
-          GV->getType()->getAddressSpace() != 0 || !GV->getSection().empty() ||
+          GV->getType()->getAddressSpace() != 0 || GV->hasSection() ||
           // Don't touch values marked with attribute(used).
           UsedGlobals.count(GV))
         continue;
       
+      // Start by filling slots with only the globals we aren't allowed to
+      // delete because they're externally visible.
+      if (GV->hasLocalLinkage())
+        continue;
       
-      
       Constant *Init = GV->getInitializer();
 
       // Check to see if the initializer is already known.
@@ -117,7 +120,32 @@
 
       if (Slot == 0) {    // Nope, add it to the map.
         Slot = GV;
-      } else if (GV->hasLocalLinkage()) {    // Yup, this is a duplicate!
+      }
+    }
+
+    for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
+         GVI != E; ) {
+      GlobalVariable *GV = GVI++;
+
+      // Only process constants with initializers in the default address space.
+      if (!GV->isConstant() ||!GV->hasDefinitiveInitializer() ||
+          GV->getType()->getAddressSpace() != 0 || GV->hasSection() ||
+          // Don't touch values marked with attribute(used).
+          UsedGlobals.count(GV))
+        continue;
+
+      // Only look at the remaining globals now.
+      if (!GV->hasLocalLinkage())
+        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 {            // Yup, this is a duplicate!
         // Make all uses of the duplicate constant use the canonical version.
         Replacements.push_back(std::make_pair(GV, Slot));
       }
@@ -135,6 +163,8 @@
       Replacements[i].first->replaceAllUsesWith(Replacements[i].second);
 
       // Delete the global value from the module.
+      assert(Replacements[i].first->hasLocalLinkage() &&
+             "Refusing to delete an externally visible global variable.");
       Replacements[i].first->eraseFromParent();
     }
 

Added: llvm/trunk/test/Transforms/ConstantMerge/2011-01-15-EitherOrder.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/ConstantMerge/2011-01-15-EitherOrder.ll?rev=123541&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/ConstantMerge/2011-01-15-EitherOrder.ll (added)
+++ llvm/trunk/test/Transforms/ConstantMerge/2011-01-15-EitherOrder.ll Sat Jan 15 12:14:21 2011
@@ -0,0 +1,18 @@
+; RUN: opt -constmerge %s -S -o - | FileCheck %s
+; PR8978
+
+declare i32 @zed(%struct.foobar*, %struct.foobar*)
+
+%struct.foobar = type { i32 }
+; CHECK: bar.d
+ at bar.d =  constant %struct.foobar zeroinitializer, align 4
+; CHECK-NOT: foo.d
+ at foo.d = internal constant %struct.foobar zeroinitializer, align 4
+define i32 @main() nounwind ssp {
+entry:
+; CHECK: bar.d
+  %call2 = tail call i32 @zed(%struct.foobar* @foo.d, %struct.foobar* @bar.d)
+nounwind
+  ret i32 0
+}
+





More information about the llvm-commits mailing list