blob: 61c026a139204906c2b06cc26e7197a63d00931c [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
Chris Lattner7e02b7e2001-06-30 04:36:40 +000024#include "llvm/Optimizations/ConstantProp.h"
25#include "llvm/Optimizations/ConstantHandling.h"
Chris Lattner00950542001-06-06 20:29:01 +000026#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"
Chris Lattner00950542001-06-06 20:29:01 +000032
33inline static bool
34ConstantFoldUnaryInst(Method *M, Method::inst_iterator &DI,
35 UnaryOperator *Op, ConstPoolVal *D) {
Chris Lattner531450d2001-06-27 23:35:26 +000036 ConstPoolVal *ReplaceWith =
Chris Lattnera41f50d2001-07-07 19:24:15 +000037 opt::ConstantFoldUnaryInstruction(Op->getOpcode(), D);
Chris Lattner00950542001-06-06 20:29:01 +000038
39 if (!ReplaceWith) return false; // Nothing new to change...
40
Chris Lattner00950542001-06-06 20:29:01 +000041 // Replaces all of the uses of a variable with uses of the constant.
42 Op->replaceAllUsesWith(ReplaceWith);
43
44 // Remove the operator from the list of definitions...
45 Op->getParent()->getInstList().remove(DI.getInstructionIterator());
46
47 // The new constant inherits the old name of the operator...
Chris Lattner9b644cc2001-09-07 16:41:30 +000048 if (Op->hasName())
49 ReplaceWith->setName(Op->getName(), M->getSymbolTableSure());
Chris Lattner00950542001-06-06 20:29:01 +000050
51 // Delete the operator now...
52 delete Op;
53 return true;
54}
55
56inline static bool
57ConstantFoldBinaryInst(Method *M, Method::inst_iterator &DI,
58 BinaryOperator *Op,
59 ConstPoolVal *D1, ConstPoolVal *D2) {
Chris Lattner531450d2001-06-27 23:35:26 +000060 ConstPoolVal *ReplaceWith =
Chris Lattnera41f50d2001-07-07 19:24:15 +000061 opt::ConstantFoldBinaryInstruction(Op->getOpcode(), D1, D2);
Chris Lattner00950542001-06-06 20:29:01 +000062 if (!ReplaceWith) return false; // Nothing new to change...
63
Chris Lattner00950542001-06-06 20:29:01 +000064 // Replaces all of the uses of a variable with uses of the constant.
65 Op->replaceAllUsesWith(ReplaceWith);
66
67 // Remove the operator from the list of definitions...
68 Op->getParent()->getInstList().remove(DI.getInstructionIterator());
69
70 // The new constant inherits the old name of the operator...
Chris Lattner9b644cc2001-09-07 16:41:30 +000071 if (Op->hasName())
72 ReplaceWith->setName(Op->getName(), M->getSymbolTableSure());
Chris Lattner00950542001-06-06 20:29:01 +000073
74 // Delete the operator now...
75 delete Op;
76 return true;
77}
78
Chris Lattner2b058802001-06-29 23:56:58 +000079// ConstantFoldTerminator - If a terminator instruction is predicated on a
80// constant value, convert it into an unconditional branch to the constant
81// destination.
82//
Chris Lattner7e02b7e2001-06-30 04:36:40 +000083bool opt::ConstantFoldTerminator(TerminatorInst *T) {
Chris Lattner00950542001-06-06 20:29:01 +000084 // Branch - See if we are conditional jumping on constant
Chris Lattnerb00c5822001-10-02 03:41:24 +000085 if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
Chris Lattner2b058802001-06-29 23:56:58 +000086 if (BI->isUnconditional()) return false; // Can't optimize uncond branch
Chris Lattner9636a912001-10-01 16:18:37 +000087 BasicBlock *Dest1 = cast<BasicBlock>(BI->getOperand(0));
88 BasicBlock *Dest2 = cast<BasicBlock>(BI->getOperand(1));
Chris Lattner2b058802001-06-29 23:56:58 +000089
Chris Lattner1d87bcf2001-10-01 20:11:19 +000090 if (ConstPoolBool *Cond = dyn_cast<ConstPoolBool>(BI->getCondition())) {
91 // Are we branching on constant?
Chris Lattner00950542001-06-06 20:29:01 +000092 // YES. Change to unconditional branch...
Chris Lattner2b058802001-06-29 23:56:58 +000093 BasicBlock *Destination = Cond->getValue() ? Dest1 : Dest2;
94 BasicBlock *OldDest = Cond->getValue() ? Dest2 : Dest1;
Chris Lattnerbca26a42001-06-29 05:23:10 +000095
Chris Lattner2b058802001-06-29 23:56:58 +000096 //cerr << "Method: " << T->getParent()->getParent()
97 // << "\nRemoving branch from " << T->getParent()
98 // << "\n\nTo: " << OldDest << endl;
Chris Lattnerbca26a42001-06-29 05:23:10 +000099
100 // Let the basic block know that we are letting go of it. Based on this,
101 // it will adjust it's PHI nodes.
Chris Lattner2b058802001-06-29 23:56:58 +0000102 assert(BI->getParent() && "Terminator not inserted in block!");
103 OldDest->removePredecessor(BI->getParent());
Chris Lattner00950542001-06-06 20:29:01 +0000104
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000105 // Set the unconditional destination, and change the insn to be an
106 // unconditional branch.
107 BI->setUnconditionalDest(Destination);
Chris Lattner00950542001-06-06 20:29:01 +0000108 return true;
Chris Lattner9b644cc2001-09-07 16:41:30 +0000109 }
110#if 0
111 // FIXME: TODO: This doesn't work if the destination has PHI nodes with
112 // different incoming values on each branch!
113 //
114 else if (Dest2 == Dest1) { // Conditional branch to same location?
Chris Lattner2b058802001-06-29 23:56:58 +0000115 // This branch matches something like this:
116 // br bool %cond, label %Dest, label %Dest
117 // and changes it into: br label %Dest
118
119 // Let the basic block know that we are letting go of one copy of it.
120 assert(BI->getParent() && "Terminator not inserted in block!");
121 Dest1->removePredecessor(BI->getParent());
122
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000123 // Change a conditional branch to unconditional.
124 BI->setUnconditionalDest(Dest1);
Chris Lattner2b058802001-06-29 23:56:58 +0000125 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000126 }
Chris Lattner9b644cc2001-09-07 16:41:30 +0000127#endif
Chris Lattner00950542001-06-06 20:29:01 +0000128 }
129 return false;
130}
131
132// ConstantFoldInstruction - If an instruction references constants, try to fold
133// them together...
134//
135inline static bool
136ConstantFoldInstruction(Method *M, Method::inst_iterator &II) {
137 Instruction *Inst = *II;
Chris Lattnerb00c5822001-10-02 03:41:24 +0000138 if (BinaryOperator *BInst = dyn_cast<BinaryOperator>(Inst)) {
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000139 ConstPoolVal *D1 = dyn_cast<ConstPoolVal>(Inst->getOperand(0));
140 ConstPoolVal *D2 = dyn_cast<ConstPoolVal>(Inst->getOperand(1));
Chris Lattner531450d2001-06-27 23:35:26 +0000141
142 if (D1 && D2)
Chris Lattnerb00c5822001-10-02 03:41:24 +0000143 return ConstantFoldBinaryInst(M, II, cast<BinaryOperator>(Inst), D1, D2);
Chris Lattner00950542001-06-06 20:29:01 +0000144
Chris Lattnerb00c5822001-10-02 03:41:24 +0000145 } else if (UnaryOperator *UInst = dyn_cast<UnaryOperator>(Inst)) {
146 ConstPoolVal *D = dyn_cast<ConstPoolVal>(UInst->getOperand(0));
147 if (D) return ConstantFoldUnaryInst(M, II, UInst, D);
148 } else if (TerminatorInst *TInst = dyn_cast<TerminatorInst>(Inst)) {
149 return opt::ConstantFoldTerminator(TInst);
Chris Lattner00950542001-06-06 20:29:01 +0000150
Chris Lattnerb00c5822001-10-02 03:41:24 +0000151 } else if (PHINode *PN = dyn_cast<PHINode>(Inst)) {
152 // If it's a PHI node and only has one operand
153 // Then replace it directly with that operand.
Chris Lattner00950542001-06-06 20:29:01 +0000154 assert(PN->getOperand(0) && "PHI Node must have at least one operand!");
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000155 if (PN->getNumOperands() == 1) { // If the PHI Node has exactly 1 operand
Chris Lattner00950542001-06-06 20:29:01 +0000156 Value *V = PN->getOperand(0);
157 PN->replaceAllUsesWith(V); // Replace all uses of this PHI
158 // Unlink from basic block
159 PN->getParent()->getInstList().remove(II.getInstructionIterator());
Chris Lattner9b644cc2001-09-07 16:41:30 +0000160 if (PN->hasName()) // Inherit PHINode name
161 V->setName(PN->getName(), M->getSymbolTableSure());
Chris Lattner00950542001-06-06 20:29:01 +0000162 delete PN; // Finally, delete the node...
163 return true;
164 }
165 }
166 return false;
167}
168
169// DoConstPropPass - Propogate constants and do constant folding on instructions
170// this returns true if something was changed, false if nothing was changed.
171//
172static bool DoConstPropPass(Method *M) {
173 bool SomethingChanged = false;
174
175#if 1
176 Method::inst_iterator It = M->inst_begin();
177 while (It != M->inst_end())
178 if (ConstantFoldInstruction(M, It)) {
179 SomethingChanged = true; // If returned true, iter is already incremented
180
181 // Incrementing the iterator in an unchecked manner could mess up the
182 // internals of 'It'. To make sure everything is happy, tell it we might
183 // have broken it.
184 It.resyncInstructionIterator();
185 } else {
186 ++It;
187 }
188#else
Chris Lattner531450d2001-06-27 23:35:26 +0000189 for (Method::iterator BBIt = M->begin(); BBIt != M->end(); ++BBIt) {
Chris Lattner00950542001-06-06 20:29:01 +0000190 BasicBlock *BB = *BBIt;
191
Chris Lattner531450d2001-06-27 23:35:26 +0000192 reduce_apply_bool(BB->begin(), BB->end(),
193 bind1st(ConstantFoldInstruction, M));
Chris Lattner00950542001-06-06 20:29:01 +0000194 }
195#endif
196 return SomethingChanged;
197}
198
199
200// returns true on failure, false on success...
201//
Chris Lattner7e02b7e2001-06-30 04:36:40 +0000202bool opt::DoConstantPropogation(Method *M) {
Chris Lattner00950542001-06-06 20:29:01 +0000203 bool Modified = false;
204
205 // Fold constants until we make no progress...
206 while (DoConstPropPass(M)) Modified = true;
207
Chris Lattner00950542001-06-06 20:29:01 +0000208 return Modified;
209}