summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Transforms/IPO/ConstantMerge.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2006-03-07 17:56:59 +0000
committerChris Lattner <sabre@nondot.org>2006-03-07 17:56:59 +0000
commit7b87fd53f9ba1800bab45f6a61146cc34a9603fd (patch)
treee2da22c9d58402e9a15fc6e08d3eae1609fdd1b7 /llvm/lib/Transforms/IPO/ConstantMerge.cpp
parentf118a41fb0b74cfef04e1d0bbc61b906972d24c1 (diff)
downloadbcm5719-llvm-7b87fd53f9ba1800bab45f6a61146cc34a9603fd.tar.gz
bcm5719-llvm-7b87fd53f9ba1800bab45f6a61146cc34a9603fd.zip
Fix ConstantMerge/2006-03-07-DontMergeDiffSections.ll, a problem Jim
hypotheticalized about, where we would incorrectly merge two globals in different sections. llvm-svn: 26597
Diffstat (limited to 'llvm/lib/Transforms/IPO/ConstantMerge.cpp')
-rw-r--r--llvm/lib/Transforms/IPO/ConstantMerge.cpp25
1 files changed, 14 insertions, 11 deletions
diff --git a/llvm/lib/Transforms/IPO/ConstantMerge.cpp b/llvm/lib/Transforms/IPO/ConstantMerge.cpp
index b6026f27364..220d0d8722c 100644
--- a/llvm/lib/Transforms/IPO/ConstantMerge.cpp
+++ b/llvm/lib/Transforms/IPO/ConstantMerge.cpp
@@ -39,7 +39,9 @@ namespace {
ModulePass *llvm::createConstantMergePass() { return new ConstantMerge(); }
bool ConstantMerge::runOnModule(Module &M) {
- std::map<Constant*, GlobalVariable*> CMap;
+ // 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;
// Replacements - This vector contains a list of replacements to perform.
std::vector<std::pair<GlobalVariable*, GlobalVariable*> > Replacements;
@@ -56,23 +58,24 @@ 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)
- // Only process constants with initializers
+ for (Module::global_iterator GV = M.global_begin(), E = M.global_end();
+ GV != E; ++GV)
+ // Only process constants with initializers.
if (GV->isConstant() && GV->hasInitializer()) {
Constant *Init = GV->getInitializer();
- // Check to see if the initializer is already known...
- std::map<Constant*, GlobalVariable*>::iterator I = CMap.find(Init);
+ // Check to see if the initializer is already known.
+ GlobalVariable *&Slot = CMap[std::make_pair(Init, GV->getSection())];
- if (I == CMap.end()) { // Nope, add it to the map
- CMap.insert(I, std::make_pair(Init, GV));
+ if (Slot == 0) { // Nope, add it to the map.
+ Slot = GV;
} 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, I->second));
- } else if (I->second->hasInternalLinkage()) {
+ 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(I->second, GV));
- I->second = GV;
+ Replacements.push_back(std::make_pair(Slot, GV));
+ Slot = GV;
}
}
OpenPOWER on IntegriCloud