blob: eef5a039f035b1421f73ce2ce223fa8f7aabc0a7 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===- ConstantProp.cpp - Code to perform Constant Propogation ------------===//
2//
3// This file implements constant propogation and merging:
4//
5// Specifically, this:
6// * Folds multiple identical constants in the constant pool together
7// Note that if one is named and the other is not, that the result gets the
8// original name.
9// * Converts instructions like "add int %1, %2" into a direct def of %3 in
10// the constant pool
11// * Converts conditional branches on a constant boolean value into direct
12// branches.
13// * Converts phi nodes with one incoming def to the incoming def directly
14// . Converts switch statements with one entry into a test & conditional
15// branch
16// . Converts switches on constant values into an unconditional branch.
17//
18// Notice that:
19// * This pass has a habit of making definitions be dead. It is a good idea
20// to to run a DCE pass sometime after running this pass.
21//
22//===----------------------------------------------------------------------===//
23
24#include "llvm/Module.h"
25#include "llvm/Method.h"
26#include "llvm/BasicBlock.h"
27#include "llvm/iTerminators.h"
28#include "llvm/iOther.h"
29#include "llvm/ConstPoolVals.h"
30#include "llvm/ConstantPool.h"
31#include "llvm/Opt/AllOpts.h"
32#include "llvm/Opt/ConstantHandling.h"
33
34// Merge identical constant values in the constant pool.
35//
36// TODO: We can do better than this simplistic N^2 algorithm...
37//
38static bool MergeConstantPoolReferences(ConstantPool &CP) {
39 bool Modified = false;
40 for (ConstantPool::plane_iterator PI = CP.begin(); PI != CP.end(); ++PI) {
41 for (ConstantPool::PlaneType::iterator I = (*PI)->begin();
42 I != (*PI)->end(); I++) {
43 ConstPoolVal *C = *I;
44
45 ConstantPool::PlaneType::iterator J = I;
46 for (++J; J != (*PI)->end(); J++) {
47 if (C->equals(*J)) {
48 Modified = true;
49 // Okay we know that *I == *J. So now we need to make all uses of *I
50 // point to *J.
51 //
52 C->replaceAllUsesWith(*J);
53
54 (*PI)->remove(I); // Remove C from constant pool...
55
56 if (C->hasName() && !(*J)->hasName()) // The merged constant inherits
57 (*J)->setName(C->getName()); // the old name...
58
59 delete C; // Delete the constant itself.
60 break; // Break out of inner for loop
61 }
62 }
63 }
64 }
65 return Modified;
66}
67
68inline static bool
69ConstantFoldUnaryInst(Method *M, Method::inst_iterator &DI,
70 UnaryOperator *Op, ConstPoolVal *D) {
71 ConstPoolVal *ReplaceWith = 0;
72
73 switch (Op->getInstType()) {
74 case Instruction::Not: ReplaceWith = !*D; break;
75 case Instruction::Neg: ReplaceWith = -*D; break;
76 }
77
78 if (!ReplaceWith) return false; // Nothing new to change...
79
80
81 // Add the new value to the constant pool...
82 M->getConstantPool().insert(ReplaceWith);
83
84 // Replaces all of the uses of a variable with uses of the constant.
85 Op->replaceAllUsesWith(ReplaceWith);
86
87 // Remove the operator from the list of definitions...
88 Op->getParent()->getInstList().remove(DI.getInstructionIterator());
89
90 // The new constant inherits the old name of the operator...
91 if (Op->hasName()) ReplaceWith->setName(Op->getName());
92
93 // Delete the operator now...
94 delete Op;
95 return true;
96}
97
98inline static bool
99ConstantFoldBinaryInst(Method *M, Method::inst_iterator &DI,
100 BinaryOperator *Op,
101 ConstPoolVal *D1, ConstPoolVal *D2) {
102 ConstPoolVal *ReplaceWith = 0;
103
104 switch (Op->getInstType()) {
105 case Instruction::Add: ReplaceWith = *D1 + *D2; break;
106 case Instruction::Sub: ReplaceWith = *D1 - *D2; break;
107
108 case Instruction::SetEQ: ReplaceWith = *D1 == *D2; break;
109 case Instruction::SetNE: ReplaceWith = *D1 != *D2; break;
110 case Instruction::SetLE: ReplaceWith = *D1 <= *D2; break;
111 case Instruction::SetGE: ReplaceWith = *D1 >= *D2; break;
112 case Instruction::SetLT: ReplaceWith = *D1 < *D2; break;
113 case Instruction::SetGT: ReplaceWith = *D1 > *D2; break;
114 }
115
116 if (!ReplaceWith) return false; // Nothing new to change...
117
118 // Add the new value to the constant pool...
119 M->getConstantPool().insert(ReplaceWith);
120
121 // Replaces all of the uses of a variable with uses of the constant.
122 Op->replaceAllUsesWith(ReplaceWith);
123
124 // Remove the operator from the list of definitions...
125 Op->getParent()->getInstList().remove(DI.getInstructionIterator());
126
127 // The new constant inherits the old name of the operator...
128 if (Op->hasName()) ReplaceWith->setName(Op->getName());
129
130 // Delete the operator now...
131 delete Op;
132 return true;
133}
134
135inline static bool ConstantFoldTerminator(TerminatorInst *T) {
136 // Branch - See if we are conditional jumping on constant
137 if (T->getInstType() == Instruction::Br) {
138 BranchInst *BI = (BranchInst*)T;
139 if (!BI->isUnconditional() && // Are we branching on constant?
140 BI->getOperand(2)->getValueType() == Value::ConstantVal) {
141 // YES. Change to unconditional branch...
142 ConstPoolBool *Cond = (ConstPoolBool*)BI->getOperand(2);
143 Value *Destination = BI->getOperand(Cond->getValue() ? 0 : 1);
144
145 BI->setOperand(0, Destination); // Set the unconditional destination
146 BI->setOperand(1, 0); // Clear the conditional destination
147 BI->setOperand(2, 0); // Clear the condition...
148 return true;
149 }
150 }
151 return false;
152}
153
154// ConstantFoldInstruction - If an instruction references constants, try to fold
155// them together...
156//
157inline static bool
158ConstantFoldInstruction(Method *M, Method::inst_iterator &II) {
159 Instruction *Inst = *II;
160 if (Inst->isBinaryOp()) {
161 Value *D1, *D2;
162 if (((D1 = Inst->getOperand(0))->getValueType() == Value::ConstantVal) &
163 ((D2 = Inst->getOperand(1))->getValueType() == Value::ConstantVal))
164 return ConstantFoldBinaryInst(M, II, (BinaryOperator*)Inst,
165 (ConstPoolVal*)D1, (ConstPoolVal*)D2);
166
167 } else if (Inst->isUnaryOp()) {
168 Value *D;
169 if ((D = Inst->getOperand(0))->getValueType() == Value::ConstantVal)
170 return ConstantFoldUnaryInst(M, II, (UnaryOperator*)Inst,
171 (ConstPoolVal*)D);
172 } else if (Inst->isTerminator()) {
173 return ConstantFoldTerminator((TerminatorInst*)Inst);
174
175 } else if (Inst->getInstType() == Instruction::PHINode) {
176 PHINode *PN = (PHINode*)Inst; // If it's a PHI node and only has one operand
177 // Then replace it directly with that operand.
178 assert(PN->getOperand(0) && "PHI Node must have at least one operand!");
179 if (PN->getOperand(1) == 0) { // If the PHI Node has exactly 1 operand
180 Value *V = PN->getOperand(0);
181 PN->replaceAllUsesWith(V); // Replace all uses of this PHI
182 // Unlink from basic block
183 PN->getParent()->getInstList().remove(II.getInstructionIterator());
184 if (PN->hasName()) V->setName(PN->getName()); // Inherit PHINode name
185 delete PN; // Finally, delete the node...
186 return true;
187 }
188 }
189 return false;
190}
191
192// DoConstPropPass - Propogate constants and do constant folding on instructions
193// this returns true if something was changed, false if nothing was changed.
194//
195static bool DoConstPropPass(Method *M) {
196 bool SomethingChanged = false;
197
198#if 1
199 Method::inst_iterator It = M->inst_begin();
200 while (It != M->inst_end())
201 if (ConstantFoldInstruction(M, It)) {
202 SomethingChanged = true; // If returned true, iter is already incremented
203
204 // Incrementing the iterator in an unchecked manner could mess up the
205 // internals of 'It'. To make sure everything is happy, tell it we might
206 // have broken it.
207 It.resyncInstructionIterator();
208 } else {
209 ++It;
210 }
211#else
212 Method::BasicBlocksType::iterator BBIt = M->getBasicBlocks().begin();
213 for (; BBIt != M->getBasicBlocks().end(); BBIt++) {
214 BasicBlock *BB = *BBIt;
215
216 BasicBlock::InstListType::iterator DI = BB->getInstList().begin();
217 for (; DI != BB->getInstList().end(); DI++)
218 SomethingChanged |= ConstantFoldInstruction(M, DI);
219 }
220#endif
221 return SomethingChanged;
222}
223
224
225// returns true on failure, false on success...
226//
227bool DoConstantPropogation(Method *M) {
228 bool Modified = false;
229
230 // Fold constants until we make no progress...
231 while (DoConstPropPass(M)) Modified = true;
232
233 // Merge identical constants last: this is important because we may have just
234 // introduced constants that already exist!
235 //
236 Modified |= MergeConstantPoolReferences(M->getConstantPool());
237
238 return Modified;
239}