From 9234d03373b7e0e590836f8fe1a5cd560d2620ea Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 25 Jun 2002 15:55:29 +0000 Subject: [PATCH] Remove DynamicConstantMerge pass, because it did not fit in with the Pass system correctly. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2772 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/IPO/ConstantMerge.cpp | 91 +++++++++------------------- 1 file changed, 27 insertions(+), 64 deletions(-) diff --git a/lib/Transforms/IPO/ConstantMerge.cpp b/lib/Transforms/IPO/ConstantMerge.cpp index 11191f535ab..9a67dbdf198 100644 --- a/lib/Transforms/IPO/ConstantMerge.cpp +++ b/lib/Transforms/IPO/ConstantMerge.cpp @@ -15,27 +15,39 @@ //===----------------------------------------------------------------------===// #include "llvm/Transforms/ConstantMerge.h" -#include "llvm/GlobalVariable.h" #include "llvm/Module.h" -#include "llvm/Function.h" #include "llvm/Pass.h" #include "Support/StatisticReporter.h" static Statistic<> NumMerged("constmerge\t\t- Number of global constants merged"); -// mergeDuplicateConstants - Workhorse for the pass. This eliminates duplicate +namespace { + struct ConstantMerge : public Pass { + const char *getPassName() const {return "Merge Duplicate Global Constants";} + + // run - For this pass, process all of the globals in the module, + // eliminating duplicate constants. + // + bool run(Module &M); + + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.preservesCFG(); + } + }; +} + +Pass *createConstantMergePass() { return new ConstantMerge(); } + + +// ConstantMerge::run - Workhorse for the pass. This eliminates duplicate // constants, starting at global ConstantNo, and adds vars to the map if they // are new and unique. // -static inline -bool mergeDuplicateConstants(Module *M, unsigned &ConstantNo, - std::map &CMap) { - Module::GlobalListType &GList = M->getGlobalList(); - if (GList.size() <= ConstantNo) return false; // No new constants +bool ConstantMerge::run(Module &M) { + std::map CMap; bool MadeChanges = false; - for (; ConstantNo < GList.size(); ++ConstantNo) { - GlobalVariable *GV = GList[ConstantNo]; + for (Module::giterator GV = M.gbegin(), E = M.gend(); GV != E; ++GV) if (GV->isConstant()) { // Only process constants assert(GV->hasInitializer() && "Globals constants must have inits!"); Constant *Init = GV->getInitializer(); @@ -44,68 +56,19 @@ bool mergeDuplicateConstants(Module *M, unsigned &ConstantNo, std::map::iterator I = CMap.find(Init); if (I == CMap.end()) { // Nope, add it to the map - CMap.insert(std::make_pair(Init, GV)); + CMap.insert(I, std::make_pair(Init, GV)); } else { // Yup, this is a duplicate! // Make all uses of the duplicate constant use the cannonical version... GV->replaceAllUsesWith(I->second); - // Remove and delete the global value from the module... - delete GList.remove(GList.begin()+ConstantNo); + // Delete the global value from the module... and back up iterator to + // not skip the next global... + GV = --M.getGlobalList().erase(GV); - --ConstantNo; // Don't skip the next constant. ++NumMerged; MadeChanges = true; } } - } + return MadeChanges; } - -namespace { - // FIXME: ConstantMerge should not be a FunctionPass!!! - class ConstantMerge : public FunctionPass { - protected: - std::map Constants; - unsigned LastConstantSeen; - public: - inline ConstantMerge() : LastConstantSeen(0) {} - - const char *getPassName() const {return "Merge Duplicate Global Constants";} - - // doInitialization - For this pass, process all of the globals in the - // module, eliminating duplicate constants. - // - bool doInitialization(Module *M) { - return ::mergeDuplicateConstants(M, LastConstantSeen, Constants); - } - - bool runOnFunction(Function *) { return false; } - - // doFinalization - Clean up internal state for this module - // - bool doFinalization(Module *M) { - LastConstantSeen = 0; - Constants.clear(); - return false; - } - - virtual void getAnalysisUsage(AnalysisUsage &AU) const { - AU.setPreservesAll(); - } - }; - - struct DynamicConstantMerge : public ConstantMerge { - const char *getPassName() const { return "Dynamic Constant Merge"; } - - // runOnFunction - Check to see if any globals have been added to the - // global list for the module. If so, eliminate them. - // - bool runOnFunction(Function *F) { - return ::mergeDuplicateConstants(F->getParent(), LastConstantSeen, - Constants); - } - }; -} - -Pass *createConstantMergePass() { return new ConstantMerge(); } -Pass *createDynamicConstantMergePass() { return new DynamicConstantMerge(); }