blob: 190bd4b919c66583a2790072c94d8240509459d6 [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 Lattner59b6b8e2002-01-21 23:17:48 +000024#include "llvm/Transforms/Scalar/ConstantProp.h"
25#include "llvm/Transforms/Scalar/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"
Chris Lattner7061dc52001-12-03 18:02:31 +000030#include "llvm/iPHINode.h"
Chris Lattner00950542001-06-06 20:29:01 +000031#include "llvm/iOther.h"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000032#include "llvm/ConstantVals.h"
Chris Lattner00950542001-06-06 20:29:01 +000033
34inline static bool
Chris Lattnerfaffb052001-11-26 18:57:12 +000035ConstantFoldUnaryInst(BasicBlock *BB, BasicBlock::iterator &II,
Chris Lattnere9bb2df2001-12-03 22:26:30 +000036 UnaryOperator *Op, Constant *D) {
Chris Lattner59b6b8e2002-01-21 23:17:48 +000037 Constant *ReplaceWith = 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...
Chris Lattnerfaffb052001-11-26 18:57:12 +000045 Op->getParent()->getInstList().remove(II);
Chris Lattner00950542001-06-06 20:29:01 +000046
47 // The new constant inherits the old name of the operator...
Chris Lattner9b644cc2001-09-07 16:41:30 +000048 if (Op->hasName())
Chris Lattnerfaffb052001-11-26 18:57:12 +000049 ReplaceWith->setName(Op->getName(), BB->getParent()->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
Chris Lattnerfaffb052001-11-26 18:57:12 +000057ConstantFoldCast(BasicBlock *BB, BasicBlock::iterator &II,
Chris Lattnere9bb2df2001-12-03 22:26:30 +000058 CastInst *CI, Constant *D) {
Chris Lattner59b6b8e2002-01-21 23:17:48 +000059 Constant *ReplaceWith = ConstantFoldCastInstruction(D, CI->getType());
Chris Lattner37aabf22001-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 Lattnerfaffb052001-11-26 18:57:12 +000067 CI->getParent()->getInstList().remove(II);
Chris Lattner37aabf22001-10-31 05:07:57 +000068
69 // The new constant inherits the old name of the cast...
70 if (CI->hasName())
Chris Lattnerfaffb052001-11-26 18:57:12 +000071 ReplaceWith->setName(CI->getName(), BB->getParent()->getSymbolTableSure());
Chris Lattner37aabf22001-10-31 05:07:57 +000072
73 // Delete the cast now...
74 delete CI;
75 return true;
76}
77
78inline static bool
Chris Lattnerfaffb052001-11-26 18:57:12 +000079ConstantFoldBinaryInst(BasicBlock *BB, BasicBlock::iterator &II,
Chris Lattner00950542001-06-06 20:29:01 +000080 BinaryOperator *Op,
Chris Lattnere9bb2df2001-12-03 22:26:30 +000081 Constant *D1, Constant *D2) {
Chris Lattner59b6b8e2002-01-21 23:17:48 +000082 Constant *ReplaceWith = ConstantFoldBinaryInstruction(Op->getOpcode(), D1,D2);
Chris Lattner00950542001-06-06 20:29:01 +000083 if (!ReplaceWith) return false; // Nothing new to change...
84
Chris Lattner00950542001-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 Lattnerfaffb052001-11-26 18:57:12 +000089 Op->getParent()->getInstList().remove(II);
Chris Lattner00950542001-06-06 20:29:01 +000090
91 // The new constant inherits the old name of the operator...
Chris Lattner9b644cc2001-09-07 16:41:30 +000092 if (Op->hasName())
Chris Lattnerfaffb052001-11-26 18:57:12 +000093 ReplaceWith->setName(Op->getName(), BB->getParent()->getSymbolTableSure());
Chris Lattner00950542001-06-06 20:29:01 +000094
95 // Delete the operator now...
96 delete Op;
97 return true;
98}
99
Chris Lattner2b058802001-06-29 23:56:58 +0000100// ConstantFoldTerminator - If a terminator instruction is predicated on a
101// constant value, convert it into an unconditional branch to the constant
102// destination.
103//
Chris Lattner59b6b8e2002-01-21 23:17:48 +0000104bool ConstantFoldTerminator(TerminatorInst *T) {
Chris Lattner00950542001-06-06 20:29:01 +0000105 // Branch - See if we are conditional jumping on constant
Chris Lattnerb00c5822001-10-02 03:41:24 +0000106 if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
Chris Lattner2b058802001-06-29 23:56:58 +0000107 if (BI->isUnconditional()) return false; // Can't optimize uncond branch
Chris Lattner9636a912001-10-01 16:18:37 +0000108 BasicBlock *Dest1 = cast<BasicBlock>(BI->getOperand(0));
109 BasicBlock *Dest2 = cast<BasicBlock>(BI->getOperand(1));
Chris Lattner2b058802001-06-29 23:56:58 +0000110
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000111 if (ConstantBool *Cond = dyn_cast<ConstantBool>(BI->getCondition())) {
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000112 // Are we branching on constant?
Chris Lattner00950542001-06-06 20:29:01 +0000113 // YES. Change to unconditional branch...
Chris Lattner2b058802001-06-29 23:56:58 +0000114 BasicBlock *Destination = Cond->getValue() ? Dest1 : Dest2;
115 BasicBlock *OldDest = Cond->getValue() ? Dest2 : Dest1;
Chris Lattnerbca26a42001-06-29 05:23:10 +0000116
Chris Lattner2b058802001-06-29 23:56:58 +0000117 //cerr << "Method: " << T->getParent()->getParent()
118 // << "\nRemoving branch from " << T->getParent()
119 // << "\n\nTo: " << OldDest << endl;
Chris Lattnerbca26a42001-06-29 05:23:10 +0000120
121 // Let the basic block know that we are letting go of it. Based on this,
122 // it will adjust it's PHI nodes.
Chris Lattner2b058802001-06-29 23:56:58 +0000123 assert(BI->getParent() && "Terminator not inserted in block!");
124 OldDest->removePredecessor(BI->getParent());
Chris Lattner00950542001-06-06 20:29:01 +0000125
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000126 // Set the unconditional destination, and change the insn to be an
127 // unconditional branch.
128 BI->setUnconditionalDest(Destination);
Chris Lattner00950542001-06-06 20:29:01 +0000129 return true;
Chris Lattner9b644cc2001-09-07 16:41:30 +0000130 }
131#if 0
132 // FIXME: TODO: This doesn't work if the destination has PHI nodes with
133 // different incoming values on each branch!
134 //
135 else if (Dest2 == Dest1) { // Conditional branch to same location?
Chris Lattner2b058802001-06-29 23:56:58 +0000136 // This branch matches something like this:
137 // br bool %cond, label %Dest, label %Dest
138 // and changes it into: br label %Dest
139
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());
143
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000144 // Change a conditional branch to unconditional.
145 BI->setUnconditionalDest(Dest1);
Chris Lattner2b058802001-06-29 23:56:58 +0000146 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000147 }
Chris Lattner9b644cc2001-09-07 16:41:30 +0000148#endif
Chris Lattner00950542001-06-06 20:29:01 +0000149 }
150 return false;
151}
152
153// ConstantFoldInstruction - If an instruction references constants, try to fold
154// them together...
155//
Chris Lattner59b6b8e2002-01-21 23:17:48 +0000156bool ConstantPropogation::doConstantPropogation(BasicBlock *BB,
157 BasicBlock::iterator &II) {
Chris Lattner00950542001-06-06 20:29:01 +0000158 Instruction *Inst = *II;
Chris Lattner697954c2002-01-20 22:54:45 +0000159 if (isa<BinaryOperator>(Inst)) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000160 Constant *D1 = dyn_cast<Constant>(Inst->getOperand(0));
161 Constant *D2 = dyn_cast<Constant>(Inst->getOperand(1));
Chris Lattner531450d2001-06-27 23:35:26 +0000162
163 if (D1 && D2)
Chris Lattnerfaffb052001-11-26 18:57:12 +0000164 return ConstantFoldBinaryInst(BB, II, cast<BinaryOperator>(Inst), D1, D2);
Chris Lattner00950542001-06-06 20:29:01 +0000165
Chris Lattner37aabf22001-10-31 05:07:57 +0000166 } else if (CastInst *CI = dyn_cast<CastInst>(Inst)) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000167 Constant *D = dyn_cast<Constant>(CI->getOperand(0));
Chris Lattnerfaffb052001-11-26 18:57:12 +0000168 if (D) return ConstantFoldCast(BB, II, CI, D);
Chris Lattner37aabf22001-10-31 05:07:57 +0000169
Chris Lattnerb00c5822001-10-02 03:41:24 +0000170 } else if (UnaryOperator *UInst = dyn_cast<UnaryOperator>(Inst)) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000171 Constant *D = dyn_cast<Constant>(UInst->getOperand(0));
Chris Lattnerfaffb052001-11-26 18:57:12 +0000172 if (D) return ConstantFoldUnaryInst(BB, II, UInst, D);
Chris Lattnerb00c5822001-10-02 03:41:24 +0000173 } else if (TerminatorInst *TInst = dyn_cast<TerminatorInst>(Inst)) {
Chris Lattner59b6b8e2002-01-21 23:17:48 +0000174 return ConstantFoldTerminator(TInst);
Chris Lattner00950542001-06-06 20:29:01 +0000175
Chris Lattnerb00c5822001-10-02 03:41:24 +0000176 } 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.
Chris Lattner9102aee2001-12-13 00:45:40 +0000179 assert(PN->getNumOperands() && "PHI Node must have at least one operand!");
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000180 if (PN->getNumOperands() == 1) { // If the PHI Node has exactly 1 operand
Chris Lattner00950542001-06-06 20:29:01 +0000181 Value *V = PN->getOperand(0);
182 PN->replaceAllUsesWith(V); // Replace all uses of this PHI
183 // Unlink from basic block
Chris Lattnerfaffb052001-11-26 18:57:12 +0000184 PN->getParent()->getInstList().remove(II);
Chris Lattner9b644cc2001-09-07 16:41:30 +0000185 if (PN->hasName()) // Inherit PHINode name
Chris Lattnerfaffb052001-11-26 18:57:12 +0000186 V->setName(PN->getName(), BB->getParent()->getSymbolTableSure());
Chris Lattner00950542001-06-06 20:29:01 +0000187 delete PN; // Finally, delete the node...
188 return true;
189 }
190 }
191 return false;
192}
193
194// DoConstPropPass - Propogate constants and do constant folding on instructions
195// this returns true if something was changed, false if nothing was changed.
196//
197static bool DoConstPropPass(Method *M) {
198 bool SomethingChanged = false;
199
Chris Lattnerfaffb052001-11-26 18:57:12 +0000200 for (Method::iterator BBI = M->begin(); BBI != M->end(); ++BBI) {
201 BasicBlock *BB = *BBI;
202 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); )
Chris Lattner59b6b8e2002-01-21 23:17:48 +0000203 if (ConstantPropogation::doConstantPropogation(BB, I))
Chris Lattnerfaffb052001-11-26 18:57:12 +0000204 SomethingChanged = true;
205 else
206 ++I;
Chris Lattner00950542001-06-06 20:29:01 +0000207 }
Chris Lattner00950542001-06-06 20:29:01 +0000208 return SomethingChanged;
209}
210
211
Chris Lattnerfaffb052001-11-26 18:57:12 +0000212// returns whether or not the underlying method was modified
Chris Lattner00950542001-06-06 20:29:01 +0000213//
Chris Lattner59b6b8e2002-01-21 23:17:48 +0000214bool ConstantPropogation::doConstantPropogation(Method *M) {
Chris Lattner00950542001-06-06 20:29:01 +0000215 bool Modified = false;
216
217 // Fold constants until we make no progress...
218 while (DoConstPropPass(M)) Modified = true;
219
Chris Lattner00950542001-06-06 20:29:01 +0000220 return Modified;
221}