diff options
| author | JF Bastien <jfbastien@apple.com> | 2018-08-10 22:10:20 +0000 |
|---|---|---|
| committer | JF Bastien <jfbastien@apple.com> | 2018-08-10 22:10:20 +0000 |
| commit | b99f131ffd780170f0e012fca55d23dd0cc8b60a (patch) | |
| tree | 3224a4cb34692f04b993b96da4b2c7590a145f7c | |
| parent | 6b84a48953e3cea98a44c74d9c98c1ee413b0272 (diff) | |
| download | bcm5719-llvm-b99f131ffd780170f0e012fca55d23dd0cc8b60a.tar.gz bcm5719-llvm-b99f131ffd780170f0e012fca55d23dd0cc8b60a.zip | |
Revert "[NFC] More ConstantMerge refactoring"
Sanitizers seem unhappy.
llvm-svn: 339480
| -rw-r--r-- | llvm/lib/Transforms/IPO/ConstantMerge.cpp | 47 |
1 files changed, 22 insertions, 25 deletions
diff --git a/llvm/lib/Transforms/IPO/ConstantMerge.cpp b/llvm/lib/Transforms/IPO/ConstantMerge.cpp index 16a77c0efcb..5ddf2e26e0c 100644 --- a/llvm/lib/Transforms/IPO/ConstantMerge.cpp +++ b/llvm/lib/Transforms/IPO/ConstantMerge.cpp @@ -40,7 +40,7 @@ using namespace llvm; #define DEBUG_TYPE "constmerge" -STATISTIC(NumIdenticalMerged, "Number of identical global constants merged"); +STATISTIC(NumMerged, "Number of global constants merged"); /// Find values that are marked as llvm.used. static void FindUsedValues(GlobalVariable *LLVMUsed, @@ -131,28 +131,25 @@ static bool mergeConstants(Module &M) { // Map unique constants to globals. DenseMap<Constant *, GlobalVariable *> CMap; - SmallVector<std::pair<GlobalVariable *, GlobalVariable *>, 32> - SameContentReplacements; + // Replacements - This vector contains a list of replacements to perform. + SmallVector<std::pair<GlobalVariable*, GlobalVariable*>, 32> Replacements; - size_t ChangesMade = 0; - size_t OldChangesMade = 0; + bool MadeChange = false; // Iterate constant merging while we are still making progress. Merging two // constants together may allow us to merge other constants together if the // second level constants have initializers which point to the globals that // were just merged. while (true) { - // Find the canonical constants others will be merged with. + // First: Find the canonical constants others will be merged with. for (Module::global_iterator GVI = M.global_begin(), E = M.global_end(); GVI != E; ) { GlobalVariable *GV = &*GVI++; - Constant *Init = GV->getInitializer(); // If this GV is dead, remove it. GV->removeDeadConstantUsers(); if (GV->use_empty() && GV->hasLocalLinkage()) { GV->eraseFromParent(); - ++ChangesMade; continue; } @@ -174,6 +171,8 @@ static bool mergeConstants(Module &M) { if (hasMetadataOtherThanDebugLoc(GV)) continue; + Constant *Init = GV->getInitializer(); + // Check to see if the initializer is already known. GlobalVariable *&Slot = CMap[Init]; @@ -188,14 +187,13 @@ static bool mergeConstants(Module &M) { } } - // Identify all globals that can be merged together, filling in the - // SameContentReplacements vector. We cannot do the replacement in this pass + // Second: identify all globals that can be merged together, filling in + // the Replacements vector. We cannot do the replacement in this pass // because doing so may cause initializers of other globals to be rewritten, // invalidating the Constant* pointers in CMap. for (Module::global_iterator GVI = M.global_begin(), E = M.global_end(); GVI != E; ) { GlobalVariable *GV = &*GVI++; - Constant *Init = GV->getInitializer(); // Only process constants with initializers in the default address space. if (!GV->isConstant() || !GV->hasDefinitiveInitializer() || @@ -208,6 +206,8 @@ static bool mergeConstants(Module &M) { if (!GV->hasLocalLinkage()) continue; + Constant *Init = GV->getInitializer(); + // Check to see if the initializer is already known. auto Found = CMap.find(Init); if (Found == CMap.end()) @@ -223,29 +223,26 @@ static bool mergeConstants(Module &M) { // Make all uses of the duplicate constant use the canonical version. LLVM_DEBUG(dbgs() << "Will replace: @" << GV->getName() << " -> @" << Slot->getName() << "\n"); - SameContentReplacements.push_back(std::make_pair(GV, Slot)); + Replacements.push_back(std::make_pair(GV, Slot)); } + if (Replacements.empty()) + return MadeChange; + CMap.clear(); + // Now that we have figured out which replacements must be made, do them all // now. This avoid invalidating the pointers in CMap, which are unneeded // now. - for (unsigned i = 0, e = SameContentReplacements.size(); i != e; ++i) { - GlobalVariable *Old = SameContentReplacements[i].first; - GlobalVariable *New = SameContentReplacements[i].second; + for (unsigned i = 0, e = Replacements.size(); i != e; ++i) { + GlobalVariable *Old = Replacements[i].first; + GlobalVariable *New = Replacements[i].second; replace(M, Old, New); - ++ChangesMade; - ++NumIdenticalMerged; + MadeChange = true; } - if (ChangesMade == OldChangesMade) - break; - OldChangesMade = ChangesMade; - - SameContentReplacements.clear(); - CMap.clear(); + NumMerged += Replacements.size(); + Replacements.clear(); } - - return ChangesMade; } PreservedAnalyses ConstantMergePass::run(Module &M, ModuleAnalysisManager &) { |

