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/Transforms/Scalar/ConstantProp.h"
25 #include "llvm/Transforms/Scalar/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/iPHINode.h"
31 #include "llvm/iOther.h"
32 #include "llvm/ConstantVals.h"
35 ConstantFoldUnaryInst(BasicBlock *BB, BasicBlock::iterator &II,
36 UnaryOperator *Op, Constant *D) {
37 Constant *ReplaceWith = 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(II);
47 // The new constant inherits the old name of the operator...
49 ReplaceWith->setName(Op->getName(), BB->getParent()->getSymbolTableSure());
51 // Delete the operator now...
57 ConstantFoldCast(BasicBlock *BB, BasicBlock::iterator &II,
58 CastInst *CI, Constant *D) {
59 Constant *ReplaceWith = ConstantFoldCastInstruction(D, CI->getType());
61 if (!ReplaceWith) return false; // Nothing new to change...
63 // Replaces all of the uses of a variable with uses of the constant.
64 CI->replaceAllUsesWith(ReplaceWith);
66 // Remove the cast from the list of definitions...
67 CI->getParent()->getInstList().remove(II);
69 // The new constant inherits the old name of the cast...
71 ReplaceWith->setName(CI->getName(), BB->getParent()->getSymbolTableSure());
73 // Delete the cast now...
79 ConstantFoldBinaryInst(BasicBlock *BB, BasicBlock::iterator &II,
81 Constant *D1, Constant *D2) {
82 Constant *ReplaceWith = ConstantFoldBinaryInstruction(Op->getOpcode(), D1,D2);
83 if (!ReplaceWith) return false; // Nothing new to change...
85 // Replaces all of the uses of a variable with uses of the constant.
86 Op->replaceAllUsesWith(ReplaceWith);
88 // Remove the operator from the list of definitions...
89 Op->getParent()->getInstList().remove(II);
91 // The new constant inherits the old name of the operator...
93 ReplaceWith->setName(Op->getName(), BB->getParent()->getSymbolTableSure());
95 // Delete the operator now...
100 // ConstantFoldTerminator - If a terminator instruction is predicated on a
101 // constant value, convert it into an unconditional branch to the constant
104 bool ConstantFoldTerminator(TerminatorInst *T) {
105 // Branch - See if we are conditional jumping on constant
106 if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
107 if (BI->isUnconditional()) return false; // Can't optimize uncond branch
108 BasicBlock *Dest1 = cast<BasicBlock>(BI->getOperand(0));
109 BasicBlock *Dest2 = cast<BasicBlock>(BI->getOperand(1));
111 if (ConstantBool *Cond = dyn_cast<ConstantBool>(BI->getCondition())) {
112 // Are we branching on constant?
113 // YES. Change to unconditional branch...
114 BasicBlock *Destination = Cond->getValue() ? Dest1 : Dest2;
115 BasicBlock *OldDest = Cond->getValue() ? Dest2 : Dest1;
117 //cerr << "Method: " << T->getParent()->getParent()
118 // << "\nRemoving branch from " << T->getParent()
119 // << "\n\nTo: " << OldDest << endl;
121 // Let the basic block know that we are letting go of it. Based on this,
122 // it will adjust it's PHI nodes.
123 assert(BI->getParent() && "Terminator not inserted in block!");
124 OldDest->removePredecessor(BI->getParent());
126 // Set the unconditional destination, and change the insn to be an
127 // unconditional branch.
128 BI->setUnconditionalDest(Destination);
132 // FIXME: TODO: This doesn't work if the destination has PHI nodes with
133 // different incoming values on each branch!
135 else if (Dest2 == Dest1) { // Conditional branch to same location?
136 // This branch matches something like this:
137 // br bool %cond, label %Dest, label %Dest
138 // and changes it into: br label %Dest
140 // Let the basic block know that we are letting go of one copy of it.
141 assert(BI->getParent() && "Terminator not inserted in block!");
142 Dest1->removePredecessor(BI->getParent());
144 // Change a conditional branch to unconditional.
145 BI->setUnconditionalDest(Dest1);
153 // ConstantFoldInstruction - If an instruction references constants, try to fold
156 bool ConstantPropogation::doConstantPropogation(BasicBlock *BB,
157 BasicBlock::iterator &II) {
158 Instruction *Inst = *II;
159 if (isa<BinaryOperator>(Inst)) {
160 Constant *D1 = dyn_cast<Constant>(Inst->getOperand(0));
161 Constant *D2 = dyn_cast<Constant>(Inst->getOperand(1));
164 return ConstantFoldBinaryInst(BB, II, cast<BinaryOperator>(Inst), D1, D2);
166 } else if (CastInst *CI = dyn_cast<CastInst>(Inst)) {
167 Constant *D = dyn_cast<Constant>(CI->getOperand(0));
168 if (D) return ConstantFoldCast(BB, II, CI, D);
170 } else if (UnaryOperator *UInst = dyn_cast<UnaryOperator>(Inst)) {
171 Constant *D = dyn_cast<Constant>(UInst->getOperand(0));
172 if (D) return ConstantFoldUnaryInst(BB, II, UInst, D);
173 } else if (TerminatorInst *TInst = dyn_cast<TerminatorInst>(Inst)) {
174 return ConstantFoldTerminator(TInst);
176 } else if (PHINode *PN = dyn_cast<PHINode>(Inst)) {
177 // If it's a PHI node and only has one operand
178 // Then replace it directly with that operand.
179 assert(PN->getNumOperands() && "PHI Node must have at least one operand!");
180 if (PN->getNumOperands() == 1) { // If the PHI Node has exactly 1 operand
181 Value *V = PN->getOperand(0);
182 PN->replaceAllUsesWith(V); // Replace all uses of this PHI
183 // Unlink from basic block
184 PN->getParent()->getInstList().remove(II);
185 if (PN->hasName()) // Inherit PHINode name
186 V->setName(PN->getName(), BB->getParent()->getSymbolTableSure());
187 delete PN; // Finally, delete the node...
194 // DoConstPropPass - Propogate constants and do constant folding on instructions
195 // this returns true if something was changed, false if nothing was changed.
197 static bool DoConstPropPass(Method *M) {
198 bool SomethingChanged = false;
200 for (Method::iterator BBI = M->begin(); BBI != M->end(); ++BBI) {
201 BasicBlock *BB = *BBI;
202 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); )
203 if (ConstantPropogation::doConstantPropogation(BB, I))
204 SomethingChanged = true;
208 return SomethingChanged;
212 // returns whether or not the underlying method was modified
214 bool ConstantPropogation::doConstantPropogation(Method *M) {
215 bool Modified = false;
217 // Fold constants until we make no progress...
218 while (DoConstPropPass(M)) Modified = true;