X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FTransforms%2FIPO%2FConstantMerge.cpp;h=a73fca2b3c5269e9c6831ead3c826374b431c95a;hb=a145d4188cdb4243a8018a4537e3dc4ba25c72fa;hp=70e3437bb9e0d162878dc559c13b7fcfee9b5584;hpb=e9bb2df410f7a22decad9a883f7139d5857c1520;p=oota-llvm.git diff --git a/lib/Transforms/IPO/ConstantMerge.cpp b/lib/Transforms/IPO/ConstantMerge.cpp index 70e3437bb9e..a73fca2b3c5 100644 --- a/lib/Transforms/IPO/ConstantMerge.cpp +++ b/lib/Transforms/IPO/ConstantMerge.cpp @@ -3,78 +3,60 @@ // This file defines the interface to a pass that merges duplicate global // constants together into a single constant that is shared. This is useful // because some passes (ie TraceValues) insert a lot of string constants into -// the program, regardless of whether or not they duplicate an existing string. +// the program, regardless of whether or not an existing string is available. // // Algorithm: ConstantMerge is designed to build up a map of available constants -// and elminate duplicates when it is initialized. -// -// The DynamicConstantMerge method is a superset of the ConstantMerge algorithm -// that checks for each method to see if constants have been added to the -// constant pool since it was last run... if so, it processes them. +// and eliminate duplicates when it is initialized. // //===----------------------------------------------------------------------===// -#include "llvm/Transforms/ConstantMerge.h" -#include "llvm/GlobalVariable.h" +#include "llvm/Transforms/IPO.h" +#include "llvm/Module.h" +#include "llvm/Pass.h" +#include "Support/Statistic.h" -// mergeDuplicateConstants - 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, - map &CMap) { - Module::GlobalListType &GList = M->getGlobalList(); - if (GList.size() <= ConstantNo) return false; // No new constants +namespace { + Statistic<> NumMerged("constmerge", "Number of global constants merged"); + + struct ConstantMerge : public Pass { + // run - For this pass, process all of the globals in the module, + // eliminating duplicate constants. + // + bool run(Module &M); + }; + + RegisterOpt X("constmerge","Merge Duplicate Global Constants"); +} + +Pass *createConstantMergePass() { return new ConstantMerge(); } + + +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(); // Check to see if the initializer is already known... - map::iterator I = CMap.find(Init); + std::map::iterator I = CMap.find(Init); if (I == CMap.end()) { // Nope, add it to the map - CMap.insert(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; -} - -// mergeDuplicateConstants - Static accessor for clients that don't want to -// deal with passes. -// -bool ConstantMerge::mergeDuplicateConstants(Module *M) { - map Constants; - unsigned LastConstantSeen = 0; - return ::mergeDuplicateConstants(M, LastConstantSeen, Constants); -} - - -// doPassInitialization - For this pass, process all of the globals in the -// module, eliminating duplicate constants. -// -bool ConstantMerge::doPassInitialization(Module *M) { - return ::mergeDuplicateConstants(M, LastConstantSeen, Constants); -} - -// doPerMethodWork - Check to see if any globals have been added to the -// global list for the module. If so, eliminate them. -// -bool DynamicConstantMerge::doPerMethodWork(Method *M) { - return ::mergeDuplicateConstants(M->getParent(), LastConstantSeen, Constants); + return MadeChanges; }