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 ConstantFoldBinaryInst(Method *M, Method::inst_iterator &DI,
59 ConstPoolVal *D1, ConstPoolVal *D2) {
60 ConstPoolVal *ReplaceWith =
61 opt::ConstantFoldBinaryInstruction(Op->getOpcode(), D1, D2);
62 if (!ReplaceWith) return false; // Nothing new to change...
64 // Replaces all of the uses of a variable with uses of the constant.
65 Op->replaceAllUsesWith(ReplaceWith);
67 // Remove the operator from the list of definitions...
68 Op->getParent()->getInstList().remove(DI.getInstructionIterator());
70 // The new constant inherits the old name of the operator...
72 ReplaceWith->setName(Op->getName(), M->getSymbolTableSure());
74 // Delete the operator now...
79 // ConstantFoldTerminator - If a terminator instruction is predicated on a
80 // constant value, convert it into an unconditional branch to the constant
83 bool opt::ConstantFoldTerminator(TerminatorInst *T) {
84 // Branch - See if we are conditional jumping on constant
85 if (T->getOpcode() == Instruction::Br) {
86 BranchInst *BI = (BranchInst*)T;
87 if (BI->isUnconditional()) return false; // Can't optimize uncond branch
88 BasicBlock *Dest1 = BI->getOperand(0)->castBasicBlockAsserting();
89 BasicBlock *Dest2 = BI->getOperand(1)->castBasicBlockAsserting();
91 if (BI->getCondition()->isConstant()) { // Are we branching on constant?
92 // YES. Change to unconditional branch...
93 ConstPoolBool *Cond = (ConstPoolBool*)BI->getCondition();
94 BasicBlock *Destination = Cond->getValue() ? Dest1 : Dest2;
95 BasicBlock *OldDest = Cond->getValue() ? Dest2 : Dest1;
97 //cerr << "Method: " << T->getParent()->getParent()
98 // << "\nRemoving branch from " << T->getParent()
99 // << "\n\nTo: " << OldDest << endl;
101 // Let the basic block know that we are letting go of it. Based on this,
102 // it will adjust it's PHI nodes.
103 assert(BI->getParent() && "Terminator not inserted in block!");
104 OldDest->removePredecessor(BI->getParent());
106 // Set the unconditional destination, and change the insn to be an
107 // unconditional branch.
108 BI->setUnconditionalDest(Destination);
112 // FIXME: TODO: This doesn't work if the destination has PHI nodes with
113 // different incoming values on each branch!
115 else if (Dest2 == Dest1) { // Conditional branch to same location?
116 // This branch matches something like this:
117 // br bool %cond, label %Dest, label %Dest
118 // and changes it into: br label %Dest
120 // Let the basic block know that we are letting go of one copy of it.
121 assert(BI->getParent() && "Terminator not inserted in block!");
122 Dest1->removePredecessor(BI->getParent());
124 // Change a conditional branch to unconditional.
125 BI->setUnconditionalDest(Dest1);
133 // ConstantFoldInstruction - If an instruction references constants, try to fold
137 ConstantFoldInstruction(Method *M, Method::inst_iterator &II) {
138 Instruction *Inst = *II;
139 if (Inst->isBinaryOp()) {
140 ConstPoolVal *D1 = Inst->getOperand(0)->castConstant();
141 ConstPoolVal *D2 = Inst->getOperand(1)->castConstant();
144 return ConstantFoldBinaryInst(M, II, (BinaryOperator*)Inst, D1, D2);
146 } else if (Inst->isUnaryOp()) {
147 ConstPoolVal *D = Inst->getOperand(0)->castConstant();
148 if (D) return ConstantFoldUnaryInst(M, II, (UnaryOperator*)Inst, D);
149 } else if (Inst->isTerminator()) {
150 return opt::ConstantFoldTerminator((TerminatorInst*)Inst);
152 } else if (Inst->isPHINode()) {
153 PHINode *PN = (PHINode*)Inst; // If it's a PHI node and only has one operand
154 // Then replace it directly with that operand.
155 assert(PN->getOperand(0) && "PHI Node must have at least one operand!");
156 if (PN->getNumOperands() == 1) { // If the PHI Node has exactly 1 operand
157 Value *V = PN->getOperand(0);
158 PN->replaceAllUsesWith(V); // Replace all uses of this PHI
159 // Unlink from basic block
160 PN->getParent()->getInstList().remove(II.getInstructionIterator());
161 if (PN->hasName()) // Inherit PHINode name
162 V->setName(PN->getName(), M->getSymbolTableSure());
163 delete PN; // Finally, delete the node...
170 // DoConstPropPass - Propogate constants and do constant folding on instructions
171 // this returns true if something was changed, false if nothing was changed.
173 static bool DoConstPropPass(Method *M) {
174 bool SomethingChanged = false;
177 Method::inst_iterator It = M->inst_begin();
178 while (It != M->inst_end())
179 if (ConstantFoldInstruction(M, It)) {
180 SomethingChanged = true; // If returned true, iter is already incremented
182 // Incrementing the iterator in an unchecked manner could mess up the
183 // internals of 'It'. To make sure everything is happy, tell it we might
185 It.resyncInstructionIterator();
190 for (Method::iterator BBIt = M->begin(); BBIt != M->end(); ++BBIt) {
191 BasicBlock *BB = *BBIt;
193 reduce_apply_bool(BB->begin(), BB->end(),
194 bind1st(ConstantFoldInstruction, M));
197 return SomethingChanged;
201 // returns true on failure, false on success...
203 bool opt::DoConstantPropogation(Method *M) {
204 bool Modified = false;
206 // Fold constants until we make no progress...
207 while (DoConstPropPass(M)) Modified = true;