aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/IPO/ConstantMerge.cpp
diff options
context:
space:
mode:
authorNick Lewycky <nicholas@mxc.ca>2011-01-15 18:14:21 +0000
committerNick Lewycky <nicholas@mxc.ca>2011-01-15 18:14:21 +0000
commit0296a481f94b6f58f73b541ad4ca8605d97eeb3e (patch)
treee4903968cdf6e10915ed2a304a9b552e6ddfd5df /llvm/lib/Transforms/IPO/ConstantMerge.cpp
parent25ac830e72f66f95b2872382903bb8e6499d0db1 (diff)
downloadllvm-0296a481f94b6f58f73b541ad4ca8605d97eeb3e.zip
llvm-0296a481f94b6f58f73b541ad4ca8605d97eeb3e.tar.gz
llvm-0296a481f94b6f58f73b541ad4ca8605d97eeb3e.tar.bz2
Make constmerge a two-pass algorithm so that it won't miss merging
opporuntities. Fixes PR8978. llvm-svn: 123541
Diffstat (limited to 'llvm/lib/Transforms/IPO/ConstantMerge.cpp')
-rw-r--r--llvm/lib/Transforms/IPO/ConstantMerge.cpp38
1 files changed, 34 insertions, 4 deletions
diff --git a/llvm/lib/Transforms/IPO/ConstantMerge.cpp b/llvm/lib/Transforms/IPO/ConstantMerge.cpp
index ea01ffe..8efad05 100644
--- a/llvm/lib/Transforms/IPO/ConstantMerge.cpp
+++ b/llvm/lib/Transforms/IPO/ConstantMerge.cpp
@@ -101,15 +101,18 @@ bool ConstantMerge::runOnModule(Module &M) {
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 @@ bool ConstantMerge::runOnModule(Module &M) {
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 @@ bool ConstantMerge::runOnModule(Module &M) {
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();
}