1 //===- ConstantProp.cpp - Code to perform Constant Propogation ------------===//
3 // This file implements constant propogation and merging:
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
9 // * Converts instructions like "add int %1, %2" into a direct def of %3 in
11 // * Converts conditional branches on a constant boolean value into direct
13 // * Converts phi nodes with one incoming def to the incoming def directly
14 // . Converts switch statements with one entry into a test & conditional
16 // . Converts switches on constant values into an unconditional branch.
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.
22 //===----------------------------------------------------------------------===//
24 #include "llvm/Optimizations/ConstantProp.h"
25 #include "llvm/Optimizations/ConstantHandling.h"
26 #include "llvm/Module.h"
27 #include "llvm/Method.h"
28 #include "llvm/BasicBlock.h"
29 #include "llvm/iTerminators.h"
30 #include "llvm/iOther.h"
31 #include "llvm/ConstPoolVals.h"
34 ConstantFoldUnaryInst(Method *M, Method::inst_iterator &DI,
35 UnaryOperator *Op, ConstPoolVal *D) {
36 ConstPoolVal *ReplaceWith =
37 opt::ConstantFoldUnaryInstruction(Op->getOpcode(), D);
39 if (!ReplaceWith) return false; // Nothing new to change...
41 // Replaces all of the uses of a variable with uses of the constant.
42 Op->replaceAllUsesWith(ReplaceWith);
44 // Remove the operator from the list of definitions...
45 Op->getParent()->getInstList().remove(DI.getInstructionIterator());
47 // The new constant inherits the old name of the operator...
49 ReplaceWith->setName(Op->getName(), M->getSymbolTableSure());
51 // Delete the operator now...
57 ConstantFoldCast(Method *M, Method::inst_iterator &DI,
58 CastInst *CI, ConstPoolVal *D) {
59 ConstPoolVal *ReplaceWith =
60 opt::ConstantFoldCastInstruction(D, CI->getType());
62 if (!ReplaceWith) return false; // Nothing new to change...
64 // Replaces all of the uses of a variable with uses of the constant.
65 CI->replaceAllUsesWith(ReplaceWith);
67 // Remove the cast from the list of definitions...
68 CI->getParent()->getInstList().remove(DI.getInstructionIterator());
70 // The new constant inherits the old name of the cast...
72 ReplaceWith->setName(CI->getName(), M->getSymbolTableSure());
74 // Delete the cast now...
80 ConstantFoldBinaryInst(Method *M, Method::inst_iterator &DI,
82 ConstPoolVal *D1, ConstPoolVal *D2) {
83 ConstPoolVal *ReplaceWith =
84 opt::ConstantFoldBinaryInstruction(Op->getOpcode(), D1, D2);
85 if (!ReplaceWith) return false; // Nothing new to change...
87 // Replaces all of the uses of a variable with uses of the constant.
88 Op->replaceAllUsesWith(ReplaceWith);
90 // Remove the operator from the list of definitions...
91 Op->getParent()->getInstList().remove(DI.getInstructionIterator());
93 // The new constant inherits the old name of the operator...
95 ReplaceWith->setName(Op->getName(), M->getSymbolTableSure());
97 // Delete the operator now...
102 // ConstantFoldTerminator - If a terminator instruction is predicated on a
103 // constant value, convert it into an unconditional branch to the constant
106 bool opt::ConstantFoldTerminator(TerminatorInst *T) {
107 // Branch - See if we are conditional jumping on constant
108 if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
109 if (BI->isUnconditional()) return false; // Can't optimize uncond branch
110 BasicBlock *Dest1 = cast<BasicBlock>(BI->getOperand(0));
111 BasicBlock *Dest2 = cast<BasicBlock>(BI->getOperand(1));
113 if (ConstPoolBool *Cond = dyn_cast<ConstPoolBool>(BI->getCondition())) {
114 // Are we branching on constant?
115 // YES. Change to unconditional branch...
116 BasicBlock *Destination = Cond->getValue() ? Dest1 : Dest2;
117 BasicBlock *OldDest = Cond->getValue() ? Dest2 : Dest1;
119 //cerr << "Method: " << T->getParent()->getParent()
120 // << "\nRemoving branch from " << T->getParent()
121 // << "\n\nTo: " << OldDest << endl;
123 // Let the basic block know that we are letting go of it. Based on this,
124 // it will adjust it's PHI nodes.
125 assert(BI->getParent() && "Terminator not inserted in block!");
126 OldDest->removePredecessor(BI->getParent());
128 // Set the unconditional destination, and change the insn to be an
129 // unconditional branch.
130 BI->setUnconditionalDest(Destination);
134 // FIXME: TODO: This doesn't work if the destination has PHI nodes with
135 // different incoming values on each branch!
137 else if (Dest2 == Dest1) { // Conditional branch to same location?
138 // This branch matches something like this:
139 // br bool %cond, label %Dest, label %Dest
140 // and changes it into: br label %Dest
142 // Let the basic block know that we are letting go of one copy of it.
143 assert(BI->getParent() && "Terminator not inserted in block!");
144 Dest1->removePredecessor(BI->getParent());
146 // Change a conditional branch to unconditional.
147 BI->setUnconditionalDest(Dest1);
155 // ConstantFoldInstruction - If an instruction references constants, try to fold
159 ConstantFoldInstruction(Method *M, Method::inst_iterator &II) {
160 Instruction *Inst = *II;
161 if (BinaryOperator *BInst = dyn_cast<BinaryOperator>(Inst)) {
162 ConstPoolVal *D1 = dyn_cast<ConstPoolVal>(Inst->getOperand(0));
163 ConstPoolVal *D2 = dyn_cast<ConstPoolVal>(Inst->getOperand(1));
166 return ConstantFoldBinaryInst(M, II, cast<BinaryOperator>(Inst), D1, D2);
168 } else if (CastInst *CI = dyn_cast<CastInst>(Inst)) {
169 ConstPoolVal *D = dyn_cast<ConstPoolVal>(CI->getOperand(0));
170 if (D) return ConstantFoldCast(M, II, CI, D);
172 } else if (UnaryOperator *UInst = dyn_cast<UnaryOperator>(Inst)) {
173 ConstPoolVal *D = dyn_cast<ConstPoolVal>(UInst->getOperand(0));
174 if (D) return ConstantFoldUnaryInst(M, II, UInst, D);
175 } else if (TerminatorInst *TInst = dyn_cast<TerminatorInst>(Inst)) {
176 return opt::ConstantFoldTerminator(TInst);
178 } else if (PHINode *PN = dyn_cast<PHINode>(Inst)) {
179 // If it's a PHI node and only has one operand
180 // Then replace it directly with that operand.
181 assert(PN->getOperand(0) && "PHI Node must have at least one operand!");
182 if (PN->getNumOperands() == 1) { // If the PHI Node has exactly 1 operand
183 Value *V = PN->getOperand(0);
184 PN->replaceAllUsesWith(V); // Replace all uses of this PHI
185 // Unlink from basic block
186 PN->getParent()->getInstList().remove(II.getInstructionIterator());
187 if (PN->hasName()) // Inherit PHINode name
188 V->setName(PN->getName(), M->getSymbolTableSure());
189 delete PN; // Finally, delete the node...
196 // DoConstPropPass - Propogate constants and do constant folding on instructions
197 // this returns true if something was changed, false if nothing was changed.
199 static bool DoConstPropPass(Method *M) {
200 bool SomethingChanged = false;
203 Method::inst_iterator It = M->inst_begin();
204 while (It != M->inst_end())
205 if (ConstantFoldInstruction(M, It)) {
206 SomethingChanged = true; // If returned true, iter is already incremented
208 // Incrementing the iterator in an unchecked manner could mess up the
209 // internals of 'It'. To make sure everything is happy, tell it we might
211 It.resyncInstructionIterator();
216 for (Method::iterator BBIt = M->begin(); BBIt != M->end(); ++BBIt) {
217 BasicBlock *BB = *BBIt;
219 reduce_apply_bool(BB->begin(), BB->end(),
220 bind1st(ConstantFoldInstruction, M));
223 return SomethingChanged;
227 // returns true on failure, false on success...
229 bool opt::ConstantPropogation::doConstantPropogation(Method *M) {
230 bool Modified = false;
232 // Fold constants until we make no progress...
233 while (DoConstPropPass(M)) Modified = true;