1 //===- ConstantMerge.cpp - Merge duplicate global constants -----------------=//
3 // This file defines the interface to a pass that merges duplicate global
4 // constants together into a single constant that is shared. This is useful
5 // because some passes (ie TraceValues) insert a lot of string constants into
6 // the program, regardless of whether or not they duplicate an existing string.
8 // Algorithm: ConstantMerge is designed to build up a map of available constants
9 // and elminate duplicates when it is initialized.
11 // The DynamicConstantMerge method is a superset of the ConstantMerge algorithm
12 // that checks for each function to see if constants have been added to the
13 // constant pool since it was last run... if so, it processes them.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Transforms/ConstantMerge.h"
18 #include "llvm/GlobalVariable.h"
19 #include "llvm/Module.h"
20 #include "llvm/Function.h"
21 #include "llvm/Pass.h"
22 #include "Support/StatisticReporter.h"
24 static Statistic<> NumMerged("constmerge\t\t- Number of global constants merged");
26 // mergeDuplicateConstants - Workhorse for the pass. This eliminates duplicate
27 // constants, starting at global ConstantNo, and adds vars to the map if they
28 // are new and unique.
31 bool mergeDuplicateConstants(Module *M, unsigned &ConstantNo,
32 std::map<Constant*, GlobalVariable*> &CMap) {
33 Module::GlobalListType &GList = M->getGlobalList();
34 if (GList.size() <= ConstantNo) return false; // No new constants
35 bool MadeChanges = false;
37 for (; ConstantNo < GList.size(); ++ConstantNo) {
38 GlobalVariable *GV = GList[ConstantNo];
39 if (GV->isConstant()) { // Only process constants
40 assert(GV->hasInitializer() && "Globals constants must have inits!");
41 Constant *Init = GV->getInitializer();
43 // Check to see if the initializer is already known...
44 std::map<Constant*, GlobalVariable*>::iterator I = CMap.find(Init);
46 if (I == CMap.end()) { // Nope, add it to the map
47 CMap.insert(std::make_pair(Init, GV));
48 } else { // Yup, this is a duplicate!
49 // Make all uses of the duplicate constant use the cannonical version...
50 GV->replaceAllUsesWith(I->second);
52 // Remove and delete the global value from the module...
53 delete GList.remove(GList.begin()+ConstantNo);
55 --ConstantNo; // Don't skip the next constant.
65 // FIXME: ConstantMerge should not be a FunctionPass!!!
66 class ConstantMerge : public FunctionPass {
68 std::map<Constant*, GlobalVariable*> Constants;
69 unsigned LastConstantSeen;
71 inline ConstantMerge() : LastConstantSeen(0) {}
73 const char *getPassName() const {return "Merge Duplicate Global Constants";}
75 // doInitialization - For this pass, process all of the globals in the
76 // module, eliminating duplicate constants.
78 bool doInitialization(Module *M) {
79 return ::mergeDuplicateConstants(M, LastConstantSeen, Constants);
82 bool runOnFunction(Function *) { return false; }
84 // doFinalization - Clean up internal state for this module
86 bool doFinalization(Module *M) {
92 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
97 struct DynamicConstantMerge : public ConstantMerge {
98 const char *getPassName() const { return "Dynamic Constant Merge"; }
100 // runOnFunction - Check to see if any globals have been added to the
101 // global list for the module. If so, eliminate them.
103 bool runOnFunction(Function *F) {
104 return ::mergeDuplicateConstants(F->getParent(), LastConstantSeen,
110 Pass *createConstantMergePass() { return new ConstantMerge(); }
111 Pass *createDynamicConstantMergePass() { return new DynamicConstantMerge(); }