blob: 9e519e7cf7710244cb62d416012fc08f941d82da [file] [log] [blame]
Chris Lattner2f7c9632001-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 Lattneree965ab2002-01-21 23:17:48 +000024#include "llvm/Transforms/Scalar/ConstantProp.h"
Chris Lattner65b529f2002-04-08 20:18:09 +000025#include "llvm/ConstantHandling.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000026#include "llvm/Module.h"
Chris Lattner62b7fd12002-04-07 20:49:59 +000027#include "llvm/Function.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000028#include "llvm/BasicBlock.h"
29#include "llvm/iTerminators.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000030#include "llvm/iPHINode.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000031#include "llvm/iOther.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000032#include "llvm/Pass.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000033
34inline static bool
Chris Lattner1f868802001-11-26 18:57:12 +000035ConstantFoldUnaryInst(BasicBlock *BB, BasicBlock::iterator &II,
Chris Lattner3462ae32001-12-03 22:26:30 +000036 UnaryOperator *Op, Constant *D) {
Chris Lattneree965ab2002-01-21 23:17:48 +000037 Constant *ReplaceWith = ConstantFoldUnaryInstruction(Op->getOpcode(), D);
Chris Lattner2f7c9632001-06-06 20:29:01 +000038
39 if (!ReplaceWith) return false; // Nothing new to change...
40
Chris Lattner2f7c9632001-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...
Chris Lattner1f868802001-11-26 18:57:12 +000045 Op->getParent()->getInstList().remove(II);
Chris Lattner2f7c9632001-06-06 20:29:01 +000046
47 // The new constant inherits the old name of the operator...
Chris Lattner030772d2001-09-07 16:41:30 +000048 if (Op->hasName())
Chris Lattner1f868802001-11-26 18:57:12 +000049 ReplaceWith->setName(Op->getName(), BB->getParent()->getSymbolTableSure());
Chris Lattner2f7c9632001-06-06 20:29:01 +000050
51 // Delete the operator now...
52 delete Op;
53 return true;
54}
55
56inline static bool
Chris Lattner1f868802001-11-26 18:57:12 +000057ConstantFoldCast(BasicBlock *BB, BasicBlock::iterator &II,
Chris Lattner3462ae32001-12-03 22:26:30 +000058 CastInst *CI, Constant *D) {
Chris Lattneree965ab2002-01-21 23:17:48 +000059 Constant *ReplaceWith = ConstantFoldCastInstruction(D, CI->getType());
Chris Lattner9db8b762001-10-31 05:07:57 +000060
61 if (!ReplaceWith) return false; // Nothing new to change...
62
63 // Replaces all of the uses of a variable with uses of the constant.
64 CI->replaceAllUsesWith(ReplaceWith);
65
66 // Remove the cast from the list of definitions...
Chris Lattner1f868802001-11-26 18:57:12 +000067 CI->getParent()->getInstList().remove(II);
Chris Lattner9db8b762001-10-31 05:07:57 +000068
69 // The new constant inherits the old name of the cast...
70 if (CI->hasName())
Chris Lattner1f868802001-11-26 18:57:12 +000071 ReplaceWith->setName(CI->getName(), BB->getParent()->getSymbolTableSure());
Chris Lattner9db8b762001-10-31 05:07:57 +000072
73 // Delete the cast now...
74 delete CI;
75 return true;
76}
77
78inline static bool
Chris Lattner1f868802001-11-26 18:57:12 +000079ConstantFoldBinaryInst(BasicBlock *BB, BasicBlock::iterator &II,
Chris Lattner2f7c9632001-06-06 20:29:01 +000080 BinaryOperator *Op,
Chris Lattner3462ae32001-12-03 22:26:30 +000081 Constant *D1, Constant *D2) {
Chris Lattneree965ab2002-01-21 23:17:48 +000082 Constant *ReplaceWith = ConstantFoldBinaryInstruction(Op->getOpcode(), D1,D2);
Chris Lattner2f7c9632001-06-06 20:29:01 +000083 if (!ReplaceWith) return false; // Nothing new to change...
84
Chris Lattner2f7c9632001-06-06 20:29:01 +000085 // Replaces all of the uses of a variable with uses of the constant.
86 Op->replaceAllUsesWith(ReplaceWith);
87
88 // Remove the operator from the list of definitions...
Chris Lattner1f868802001-11-26 18:57:12 +000089 Op->getParent()->getInstList().remove(II);
Chris Lattner2f7c9632001-06-06 20:29:01 +000090
91 // The new constant inherits the old name of the operator...
Chris Lattner030772d2001-09-07 16:41:30 +000092 if (Op->hasName())
Chris Lattner1f868802001-11-26 18:57:12 +000093 ReplaceWith->setName(Op->getName(), BB->getParent()->getSymbolTableSure());
Chris Lattner2f7c9632001-06-06 20:29:01 +000094
95 // Delete the operator now...
96 delete Op;
97 return true;
98}
99
Chris Lattner940daed2002-05-06 03:01:37 +0000100inline static bool
101ConstantFoldShiftInst(BasicBlock *BB, BasicBlock::iterator &II,
102 ShiftInst *Op,
103 Constant *D1, Constant *D2) {
104 Constant *ReplaceWith = ConstantFoldShiftInstruction(Op->getOpcode(), D1,D2);
105 if (!ReplaceWith) return false; // Nothing new to change...
106
107 // Replaces all of the uses of a variable with uses of the constant.
108 Op->replaceAllUsesWith(ReplaceWith);
109
110 // Remove the operator from the list of definitions...
111 Op->getParent()->getInstList().remove(II);
112
113 // The new constant inherits the old name of the operator...
114 if (Op->hasName())
115 ReplaceWith->setName(Op->getName(), BB->getParent()->getSymbolTableSure());
116
117 // Delete the operator now...
118 delete Op;
119 return true;
120}
121
Chris Lattner7ce8b172001-06-29 23:56:58 +0000122// ConstantFoldTerminator - If a terminator instruction is predicated on a
123// constant value, convert it into an unconditional branch to the constant
124// destination.
125//
Chris Lattner477fe082002-03-11 22:11:07 +0000126bool ConstantFoldTerminator(BasicBlock *BB, BasicBlock::iterator &II,
127 TerminatorInst *T) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000128 // Branch - See if we are conditional jumping on constant
Chris Lattnerda558102001-10-02 03:41:24 +0000129 if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
Chris Lattner7ce8b172001-06-29 23:56:58 +0000130 if (BI->isUnconditional()) return false; // Can't optimize uncond branch
Chris Lattner4b717c02001-10-01 16:18:37 +0000131 BasicBlock *Dest1 = cast<BasicBlock>(BI->getOperand(0));
132 BasicBlock *Dest2 = cast<BasicBlock>(BI->getOperand(1));
Chris Lattner7ce8b172001-06-29 23:56:58 +0000133
Chris Lattner3462ae32001-12-03 22:26:30 +0000134 if (ConstantBool *Cond = dyn_cast<ConstantBool>(BI->getCondition())) {
Chris Lattner38569342001-10-01 20:11:19 +0000135 // Are we branching on constant?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000136 // YES. Change to unconditional branch...
Chris Lattner7ce8b172001-06-29 23:56:58 +0000137 BasicBlock *Destination = Cond->getValue() ? Dest1 : Dest2;
138 BasicBlock *OldDest = Cond->getValue() ? Dest2 : Dest1;
Chris Lattnere4abb602001-06-29 05:23:10 +0000139
Chris Lattner62b7fd12002-04-07 20:49:59 +0000140 //cerr << "Function: " << T->getParent()->getParent()
Chris Lattner7ce8b172001-06-29 23:56:58 +0000141 // << "\nRemoving branch from " << T->getParent()
142 // << "\n\nTo: " << OldDest << endl;
Chris Lattnere4abb602001-06-29 05:23:10 +0000143
144 // Let the basic block know that we are letting go of it. Based on this,
145 // it will adjust it's PHI nodes.
Chris Lattner7ce8b172001-06-29 23:56:58 +0000146 assert(BI->getParent() && "Terminator not inserted in block!");
147 OldDest->removePredecessor(BI->getParent());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000148
Chris Lattnera073acb2001-07-07 08:36:50 +0000149 // Set the unconditional destination, and change the insn to be an
150 // unconditional branch.
151 BI->setUnconditionalDest(Destination);
Chris Lattner477fe082002-03-11 22:11:07 +0000152 II = BB->end()-1; // Update instruction iterator!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000153 return true;
Chris Lattner030772d2001-09-07 16:41:30 +0000154 }
155#if 0
156 // FIXME: TODO: This doesn't work if the destination has PHI nodes with
157 // different incoming values on each branch!
158 //
159 else if (Dest2 == Dest1) { // Conditional branch to same location?
Chris Lattner7ce8b172001-06-29 23:56:58 +0000160 // This branch matches something like this:
161 // br bool %cond, label %Dest, label %Dest
162 // and changes it into: br label %Dest
163
164 // Let the basic block know that we are letting go of one copy of it.
165 assert(BI->getParent() && "Terminator not inserted in block!");
166 Dest1->removePredecessor(BI->getParent());
167
Chris Lattnera073acb2001-07-07 08:36:50 +0000168 // Change a conditional branch to unconditional.
169 BI->setUnconditionalDest(Dest1);
Chris Lattner7ce8b172001-06-29 23:56:58 +0000170 return true;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000171 }
Chris Lattner030772d2001-09-07 16:41:30 +0000172#endif
Chris Lattner2f7c9632001-06-06 20:29:01 +0000173 }
174 return false;
175}
176
177// ConstantFoldInstruction - If an instruction references constants, try to fold
178// them together...
179//
Chris Lattner04805fa2002-02-26 21:46:54 +0000180bool doConstantPropogation(BasicBlock *BB, BasicBlock::iterator &II) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000181 Instruction *Inst = *II;
Chris Lattner940daed2002-05-06 03:01:37 +0000182 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Inst)) {
183 Constant *D1 = dyn_cast<Constant>(BO->getOperand(0));
184 Constant *D2 = dyn_cast<Constant>(BO->getOperand(1));
Chris Lattnerf222bf32001-06-27 23:35:26 +0000185
186 if (D1 && D2)
Chris Lattner940daed2002-05-06 03:01:37 +0000187 return ConstantFoldBinaryInst(BB, II, BO, D1, D2);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000188
Chris Lattner9db8b762001-10-31 05:07:57 +0000189 } else if (CastInst *CI = dyn_cast<CastInst>(Inst)) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000190 Constant *D = dyn_cast<Constant>(CI->getOperand(0));
Chris Lattner1f868802001-11-26 18:57:12 +0000191 if (D) return ConstantFoldCast(BB, II, CI, D);
Chris Lattner9db8b762001-10-31 05:07:57 +0000192
Chris Lattnerda558102001-10-02 03:41:24 +0000193 } else if (UnaryOperator *UInst = dyn_cast<UnaryOperator>(Inst)) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000194 Constant *D = dyn_cast<Constant>(UInst->getOperand(0));
Chris Lattner1f868802001-11-26 18:57:12 +0000195 if (D) return ConstantFoldUnaryInst(BB, II, UInst, D);
Chris Lattnerda558102001-10-02 03:41:24 +0000196 } else if (TerminatorInst *TInst = dyn_cast<TerminatorInst>(Inst)) {
Chris Lattner477fe082002-03-11 22:11:07 +0000197 return ConstantFoldTerminator(BB, II, TInst);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000198
Chris Lattnerda558102001-10-02 03:41:24 +0000199 } else if (PHINode *PN = dyn_cast<PHINode>(Inst)) {
200 // If it's a PHI node and only has one operand
201 // Then replace it directly with that operand.
Chris Lattnerf5c6f65e2001-12-13 00:45:40 +0000202 assert(PN->getNumOperands() && "PHI Node must have at least one operand!");
Chris Lattnera073acb2001-07-07 08:36:50 +0000203 if (PN->getNumOperands() == 1) { // If the PHI Node has exactly 1 operand
Chris Lattner2f7c9632001-06-06 20:29:01 +0000204 Value *V = PN->getOperand(0);
205 PN->replaceAllUsesWith(V); // Replace all uses of this PHI
206 // Unlink from basic block
Chris Lattner1f868802001-11-26 18:57:12 +0000207 PN->getParent()->getInstList().remove(II);
Chris Lattner030772d2001-09-07 16:41:30 +0000208 if (PN->hasName()) // Inherit PHINode name
Chris Lattner1f868802001-11-26 18:57:12 +0000209 V->setName(PN->getName(), BB->getParent()->getSymbolTableSure());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000210 delete PN; // Finally, delete the node...
211 return true;
212 }
Chris Lattner940daed2002-05-06 03:01:37 +0000213 } else if (ShiftInst *SI = dyn_cast<ShiftInst>(Inst)) {
214 Constant *D1 = dyn_cast<Constant>(SI->getOperand(0));
215 Constant *D2 = dyn_cast<Constant>(SI->getOperand(1));
216
217 if (D1 && D2)
218 return ConstantFoldShiftInst(BB, II, SI, D1, D2);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000219 }
Chris Lattner940daed2002-05-06 03:01:37 +0000220
Chris Lattner2f7c9632001-06-06 20:29:01 +0000221 return false;
222}
223
224// DoConstPropPass - Propogate constants and do constant folding on instructions
225// this returns true if something was changed, false if nothing was changed.
226//
Chris Lattner62b7fd12002-04-07 20:49:59 +0000227static bool DoConstPropPass(Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000228 bool SomethingChanged = false;
229
Chris Lattnerf8e4dc32002-04-08 22:03:00 +0000230 for (Function::iterator BBI = F->begin(); BBI != F->end(); ++BBI) {
Chris Lattner1f868802001-11-26 18:57:12 +0000231 BasicBlock *BB = *BBI;
232 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); )
Chris Lattner04805fa2002-02-26 21:46:54 +0000233 if (doConstantPropogation(BB, I))
Chris Lattner1f868802001-11-26 18:57:12 +0000234 SomethingChanged = true;
235 else
236 ++I;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000237 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000238 return SomethingChanged;
239}
240
Chris Lattner04805fa2002-02-26 21:46:54 +0000241namespace {
Chris Lattnerc8e66542002-04-27 06:56:12 +0000242 struct ConstantPropogation : public FunctionPass {
Chris Lattner37104aa2002-04-29 14:57:45 +0000243 const char *getPassName() const { return "Simple Constant Propogation"; }
244
Chris Lattnerc8e66542002-04-27 06:56:12 +0000245 inline bool runOnFunction(Function *F) {
Chris Lattner04805fa2002-02-26 21:46:54 +0000246 bool Modified = false;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000247
Chris Lattner04805fa2002-02-26 21:46:54 +0000248 // Fold constants until we make no progress...
Chris Lattner62b7fd12002-04-07 20:49:59 +0000249 while (DoConstPropPass(F)) Modified = true;
Chris Lattner04805fa2002-02-26 21:46:54 +0000250
251 return Modified;
252 }
Chris Lattnerf12cc842002-04-28 21:27:06 +0000253
254 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
255 // FIXME: This pass does not preserve the CFG because it folds terminator
256 // instructions!
257 //AU.preservesCFG();
258 }
Chris Lattner04805fa2002-02-26 21:46:54 +0000259 };
Chris Lattner2f7c9632001-06-06 20:29:01 +0000260}
Chris Lattner04805fa2002-02-26 21:46:54 +0000261
262Pass *createConstantPropogationPass() {
263 return new ConstantPropogation();
264}
265