blob: 28c6519478f5a0f23726cbb8f45641c5dba0d03d [file] [log] [blame]
Chris Lattner475becb2001-10-18 20:05:37 +00001//===- ConstantMerge.cpp - Merge duplicate global constants -----------------=//
2//
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
Chris Lattnere4314ed2002-09-23 23:00:46 +00006// the program, regardless of whether or not an existing string is available.
Chris Lattner475becb2001-10-18 20:05:37 +00007//
8// Algorithm: ConstantMerge is designed to build up a map of available constants
Chris Lattnere4314ed2002-09-23 23:00:46 +00009// and eliminate duplicates when it is initialized.
Chris Lattner475becb2001-10-18 20:05:37 +000010//
11//===----------------------------------------------------------------------===//
12
Chris Lattner9cfea852002-07-23 19:57:40 +000013#include "llvm/Transforms/IPO.h"
Chris Lattner793c6b82002-01-31 00:45:11 +000014#include "llvm/Module.h"
Chris Lattnere4314ed2002-09-23 23:00:46 +000015#include "llvm/Constants.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000016#include "llvm/Pass.h"
Chris Lattner3dec1f22002-05-10 15:38:35 +000017#include "Support/StatisticReporter.h"
18
Chris Lattner9234d032002-06-25 15:55:29 +000019namespace {
20 struct ConstantMerge : public Pass {
Chris Lattner9234d032002-06-25 15:55:29 +000021 // run - For this pass, process all of the globals in the module,
22 // eliminating duplicate constants.
23 //
24 bool run(Module &M);
25
Chris Lattnere4314ed2002-09-23 23:00:46 +000026 private:
27 void replaceUsesOfWith(GlobalVariable *Old, GlobalVariable *New);
28 void replaceConstantWith(Constant *Old, Constant *New);
Chris Lattner9234d032002-06-25 15:55:29 +000029 };
Chris Lattneraf41a122002-07-23 18:06:30 +000030
Chris Lattner1e435162002-07-26 21:12:44 +000031 Statistic<> NumMerged("constmerge\t\t- Number of global constants merged");
32 RegisterOpt<ConstantMerge> X("constmerge","Merge Duplicate Global Constants");
Chris Lattner9234d032002-06-25 15:55:29 +000033}
34
35Pass *createConstantMergePass() { return new ConstantMerge(); }
36
37
Chris Lattner9234d032002-06-25 15:55:29 +000038bool ConstantMerge::run(Module &M) {
39 std::map<Constant*, GlobalVariable*> CMap;
Chris Lattner475becb2001-10-18 20:05:37 +000040 bool MadeChanges = false;
41
Chris Lattner9234d032002-06-25 15:55:29 +000042 for (Module::giterator GV = M.gbegin(), E = M.gend(); GV != E; ++GV)
Chris Lattner475becb2001-10-18 20:05:37 +000043 if (GV->isConstant()) { // Only process constants
44 assert(GV->hasInitializer() && "Globals constants must have inits!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +000045 Constant *Init = GV->getInitializer();
Chris Lattner475becb2001-10-18 20:05:37 +000046
47 // Check to see if the initializer is already known...
Chris Lattner697954c2002-01-20 22:54:45 +000048 std::map<Constant*, GlobalVariable*>::iterator I = CMap.find(Init);
Chris Lattner475becb2001-10-18 20:05:37 +000049
50 if (I == CMap.end()) { // Nope, add it to the map
Chris Lattner9234d032002-06-25 15:55:29 +000051 CMap.insert(I, std::make_pair(Init, GV));
Chris Lattner475becb2001-10-18 20:05:37 +000052 } else { // Yup, this is a duplicate!
53 // Make all uses of the duplicate constant use the cannonical version...
Chris Lattnere4314ed2002-09-23 23:00:46 +000054 replaceUsesOfWith(GV, I->second);
Chris Lattner475becb2001-10-18 20:05:37 +000055
Chris Lattner9234d032002-06-25 15:55:29 +000056 // Delete the global value from the module... and back up iterator to
57 // not skip the next global...
58 GV = --M.getGlobalList().erase(GV);
Chris Lattner475becb2001-10-18 20:05:37 +000059
Chris Lattner3dec1f22002-05-10 15:38:35 +000060 ++NumMerged;
Chris Lattner475becb2001-10-18 20:05:37 +000061 MadeChanges = true;
62 }
63 }
Chris Lattner9234d032002-06-25 15:55:29 +000064
Chris Lattner475becb2001-10-18 20:05:37 +000065 return MadeChanges;
66}
Chris Lattnere4314ed2002-09-23 23:00:46 +000067
68/// replaceUsesOfWith - Replace all uses of Old with New. For instructions,
69/// this is a really simple matter of replacing the reference to Old with a
70/// reference to New. For constants references, however, we must carefully
71/// build replacement constants to substitute in.
72///
73void ConstantMerge::replaceUsesOfWith(GlobalVariable *Old, GlobalVariable *New){
74 while (!Old->use_empty()) {
75 User *U = Old->use_back();
76 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(U))
77 replaceConstantWith(CPR, ConstantPointerRef::get(New));
78 else
79 U->replaceUsesOfWith(Old, New);
80 }
81}
82
83/// replaceWith - Ok, so we have a constant 'Old' and we want to replace it with
84/// 'New'. To do this, we have to recursively go through the uses of Old,
85/// replacing them with new things. The problem is that if a constant uses Old,
86/// then we need to replace the uses of the constant with uses of the equivalent
87/// constant that uses New instead.
88///
89void ConstantMerge::replaceConstantWith(Constant *Old, Constant *New) {
90 while (!Old->use_empty()) {
91 User *U = Old->use_back();
92
93 if (Constant *C = dyn_cast<Constant>(U)) {
94 Constant *Replacement = 0;
95
96 // Depending on the type of constant, build a suitable replacement...
97 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
98 if (CE->getOpcode() == Instruction::GetElementPtr) {
99 std::vector<Constant*> Indices;
100 Constant *Pointer = cast<Constant>(CE->getOperand(0));
101 Indices.reserve(CE->getNumOperands()-1);
102 if (Pointer == Old) Pointer = New;
103
104 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) {
105 Constant *Val = cast<Constant>(CE->getOperand(i));
106 if (Val == Old) Val = New;
107 Indices.push_back(Val);
108 }
109 Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
110 } else if (CE->getOpcode() == Instruction::Cast) {
111 assert(CE->getOperand(0) == Old && "Cast only has one use!");
112 Replacement = ConstantExpr::getCast(New, CE->getType());
113 } else if (CE->getNumOperands() == 2) {
114 Constant *C1 = cast<Constant>(CE->getOperand(0));
115 Constant *C2 = cast<Constant>(CE->getOperand(1));
116 if (C1 == Old) C1 = New;
117 if (C2 == Old) C2 = New;
118 Replacement = ConstantExpr::get(CE->getOpcode(), C1, C2);
119 } else {
120 assert(0 && "Unknown ConstantExpr type!");
121 }
122
123
124 } else if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
125 std::vector<Constant*> Values;
126 Values.reserve(CA->getValues().size());
127 for (unsigned i = 0, e = CA->getValues().size(); i != e; ++i) {
128 Constant *Val = cast<Constant>(CA->getValues()[i]);
129 if (Val == Old) Val = New;
130 Values.push_back(Val);
131 }
132
133 Replacement = ConstantArray::get(CA->getType(), Values);
134 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
135 std::vector<Constant*> Values;
136 Values.reserve(CS->getValues().size());
137
138 for (unsigned i = 0, e = CS->getValues().size(); i != e; ++i) {
139 Constant *Val = cast<Constant>(CS->getValues()[i]);
140 if (Val == Old) Val = New;
141 Values.push_back(Val);
142 }
143
144 Replacement = ConstantStruct::get(CS->getType(), Values);
145 } else {
146 assert(0 && "Unexpected/unknown constant type!");
147 }
148
149 // Now that we have a suitable replacement, recursively eliminate C.
150 replaceConstantWith(C, Replacement);
151
152 } else {
153 // If it is not a constant, we can simply replace uses of Old with New.
154 U->replaceUsesOfWith(Old, New);
155 }
156
157 }
158
159 // No-one refers to this old dead constant now, destroy it!
160 Old->destroyConstant();
161}