Fix ConstantMerge/2006-03-07-DontMergeDiffSections.ll, a problem Jim

hypotheticalized about, where we would incorrectly merge two globals in
different sections.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@26597 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2006-03-07 17:56:59 +00:00
parent 1159e8d8f1
commit e1c173bc77

View File

@ -39,7 +39,9 @@ namespace {
ModulePass *llvm::createConstantMergePass() { return new ConstantMerge(); } ModulePass *llvm::createConstantMergePass() { return new ConstantMerge(); }
bool ConstantMerge::runOnModule(Module &M) { 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. // Replacements - This vector contains a list of replacements to perform.
std::vector<std::pair<GlobalVariable*, GlobalVariable*> > Replacements; 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, // because doing so may cause initializers of other globals to be rewritten,
// invalidating the Constant* pointers in CMap. // 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 GV = M.global_begin(), E = M.global_end();
// Only process constants with initializers GV != E; ++GV)
// Only process constants with initializers.
if (GV->isConstant() && GV->hasInitializer()) { if (GV->isConstant() && GV->hasInitializer()) {
Constant *Init = GV->getInitializer(); Constant *Init = GV->getInitializer();
// Check to see if the initializer is already known... // Check to see if the initializer is already known.
std::map<Constant*, GlobalVariable*>::iterator I = CMap.find(Init); GlobalVariable *&Slot = CMap[std::make_pair(Init, GV->getSection())];
if (I == CMap.end()) { // Nope, add it to the map if (Slot == 0) { // Nope, add it to the map.
CMap.insert(I, std::make_pair(Init, GV)); Slot = GV;
} else if (GV->hasInternalLinkage()) { // Yup, this is a duplicate! } else if (GV->hasInternalLinkage()) { // Yup, this is a duplicate!
// Make all uses of the duplicate constant use the canonical version. // Make all uses of the duplicate constant use the canonical version.
Replacements.push_back(std::make_pair(GV, I->second)); Replacements.push_back(std::make_pair(GV, Slot));
} else if (I->second->hasInternalLinkage()) { } else if (GV->hasInternalLinkage()) {
// Make all uses of the duplicate constant use the canonical version. // Make all uses of the duplicate constant use the canonical version.
Replacements.push_back(std::make_pair(I->second, GV)); Replacements.push_back(std::make_pair(Slot, GV));
I->second = GV; Slot = GV;
} }
} }