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"
23 // mergeDuplicateConstants - Workhorse for the pass. This eliminates duplicate
24 // constants, starting at global ConstantNo, and adds vars to the map if they
25 // are new and unique.
28 bool mergeDuplicateConstants(Module *M, unsigned &ConstantNo,
29 std::map<Constant*, GlobalVariable*> &CMap) {
30 Module::GlobalListType &GList = M->getGlobalList();
31 if (GList.size() <= ConstantNo) return false; // No new constants
32 bool MadeChanges = false;
34 for (; ConstantNo < GList.size(); ++ConstantNo) {
35 GlobalVariable *GV = GList[ConstantNo];
36 if (GV->isConstant()) { // Only process constants
37 assert(GV->hasInitializer() && "Globals constants must have inits!");
38 Constant *Init = GV->getInitializer();
40 // Check to see if the initializer is already known...
41 std::map<Constant*, GlobalVariable*>::iterator I = CMap.find(Init);
43 if (I == CMap.end()) { // Nope, add it to the map
44 CMap.insert(std::make_pair(Init, GV));
45 } else { // Yup, this is a duplicate!
46 // Make all uses of the duplicate constant use the cannonical version...
47 GV->replaceAllUsesWith(I->second);
49 // Remove and delete the global value from the module...
50 delete GList.remove(GList.begin()+ConstantNo);
52 --ConstantNo; // Don't skip the next constant.
61 // FIXME: ConstantMerge should not be a FunctionPass!!!
62 class ConstantMerge : public FunctionPass {
64 std::map<Constant*, GlobalVariable*> Constants;
65 unsigned LastConstantSeen;
67 inline ConstantMerge() : LastConstantSeen(0) {}
69 // doInitialization - For this pass, process all of the globals in the
70 // module, eliminating duplicate constants.
72 bool doInitialization(Module *M) {
73 return ::mergeDuplicateConstants(M, LastConstantSeen, Constants);
76 bool runOnFunction(Function *) { return false; }
78 // doFinalization - Clean up internal state for this module
80 bool doFinalization(Module *M) {
87 struct DynamicConstantMerge : public ConstantMerge {
88 // runOnFunction - Check to see if any globals have been added to the
89 // global list for the module. If so, eliminate them.
91 bool runOnFunction(Function *F) {
92 return ::mergeDuplicateConstants(F->getParent(), LastConstantSeen,
98 Pass *createConstantMergePass() { return new ConstantMerge(); }
99 Pass *createDynamicConstantMergePass() { return new DynamicConstantMerge(); }