blob: 15be76343e13300082b732d30a94a1a1f6636b25 [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
Chris Lattnerfaffb052001-11-26 18:57:12 +000034ConstantFoldUnaryInst(BasicBlock *BB, BasicBlock::iterator &II,
Chris Lattner00950542001-06-06 20:29:01 +000035 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...
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 Lattner37aabf22001-10-31 05:07:57 +000058 CastInst *CI, ConstPoolVal *D) {
59 ConstPoolVal *ReplaceWith =
60 opt::ConstantFoldCastInstruction(D, CI->getType());
61
62 if (!ReplaceWith) return false; // Nothing new to change...
63
64 // Replaces all of the uses of a variable with uses of the constant.
65 CI->replaceAllUsesWith(ReplaceWith);
66
67 // Remove the cast from the list of definitions...
Chris Lattnerfaffb052001-11-26 18:57:12 +000068 CI->getParent()->getInstList().remove(II);
Chris Lattner37aabf22001-10-31 05:07:57 +000069
70 // The new constant inherits the old name of the cast...
71 if (CI->hasName())
Chris Lattnerfaffb052001-11-26 18:57:12 +000072 ReplaceWith->setName(CI->getName(), BB->getParent()->getSymbolTableSure());
Chris Lattner37aabf22001-10-31 05:07:57 +000073
74 // Delete the cast now...
75 delete CI;
76 return true;
77}
78
79inline static bool
Chris Lattnerfaffb052001-11-26 18:57:12 +000080ConstantFoldBinaryInst(BasicBlock *BB, BasicBlock::iterator &II,
Chris Lattner00950542001-06-06 20:29:01 +000081 BinaryOperator *Op,
82 ConstPoolVal *D1, ConstPoolVal *D2) {
Chris Lattner531450d2001-06-27 23:35:26 +000083 ConstPoolVal *ReplaceWith =
Chris Lattnera41f50d2001-07-07 19:24:15 +000084 opt::ConstantFoldBinaryInstruction(Op->getOpcode(), D1, D2);
Chris Lattner00950542001-06-06 20:29:01 +000085 if (!ReplaceWith) return false; // Nothing new to change...
86
Chris Lattner00950542001-06-06 20:29:01 +000087 // Replaces all of the uses of a variable with uses of the constant.
88 Op->replaceAllUsesWith(ReplaceWith);
89
90 // Remove the operator from the list of definitions...
Chris Lattnerfaffb052001-11-26 18:57:12 +000091 Op->getParent()->getInstList().remove(II);
Chris Lattner00950542001-06-06 20:29:01 +000092
93 // The new constant inherits the old name of the operator...
Chris Lattner9b644cc2001-09-07 16:41:30 +000094 if (Op->hasName())
Chris Lattnerfaffb052001-11-26 18:57:12 +000095 ReplaceWith->setName(Op->getName(), BB->getParent()->getSymbolTableSure());
Chris Lattner00950542001-06-06 20:29:01 +000096
97 // Delete the operator now...
98 delete Op;
99 return true;
100}
101
Chris Lattner2b058802001-06-29 23:56:58 +0000102// ConstantFoldTerminator - If a terminator instruction is predicated on a
103// constant value, convert it into an unconditional branch to the constant
104// destination.
105//
Chris Lattner7e02b7e2001-06-30 04:36:40 +0000106bool opt::ConstantFoldTerminator(TerminatorInst *T) {
Chris Lattner00950542001-06-06 20:29:01 +0000107 // Branch - See if we are conditional jumping on constant
Chris Lattnerb00c5822001-10-02 03:41:24 +0000108 if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
Chris Lattner2b058802001-06-29 23:56:58 +0000109 if (BI->isUnconditional()) return false; // Can't optimize uncond branch
Chris Lattner9636a912001-10-01 16:18:37 +0000110 BasicBlock *Dest1 = cast<BasicBlock>(BI->getOperand(0));
111 BasicBlock *Dest2 = cast<BasicBlock>(BI->getOperand(1));
Chris Lattner2b058802001-06-29 23:56:58 +0000112
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000113 if (ConstPoolBool *Cond = dyn_cast<ConstPoolBool>(BI->getCondition())) {
114 // Are we branching on constant?
Chris Lattner00950542001-06-06 20:29:01 +0000115 // YES. Change to unconditional branch...
Chris Lattner2b058802001-06-29 23:56:58 +0000116 BasicBlock *Destination = Cond->getValue() ? Dest1 : Dest2;
117 BasicBlock *OldDest = Cond->getValue() ? Dest2 : Dest1;
Chris Lattnerbca26a42001-06-29 05:23:10 +0000118
Chris Lattner2b058802001-06-29 23:56:58 +0000119 //cerr << "Method: " << T->getParent()->getParent()
120 // << "\nRemoving branch from " << T->getParent()
121 // << "\n\nTo: " << OldDest << endl;
Chris Lattnerbca26a42001-06-29 05:23:10 +0000122
123 // Let the basic block know that we are letting go of it. Based on this,
124 // it will adjust it's PHI nodes.
Chris Lattner2b058802001-06-29 23:56:58 +0000125 assert(BI->getParent() && "Terminator not inserted in block!");
126 OldDest->removePredecessor(BI->getParent());
Chris Lattner00950542001-06-06 20:29:01 +0000127
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000128 // Set the unconditional destination, and change the insn to be an
129 // unconditional branch.
130 BI->setUnconditionalDest(Destination);
Chris Lattner00950542001-06-06 20:29:01 +0000131 return true;
Chris Lattner9b644cc2001-09-07 16:41:30 +0000132 }
133#if 0
134 // FIXME: TODO: This doesn't work if the destination has PHI nodes with
135 // different incoming values on each branch!
136 //
137 else if (Dest2 == Dest1) { // Conditional branch to same location?
Chris Lattner2b058802001-06-29 23:56:58 +0000138 // This branch matches something like this:
139 // br bool %cond, label %Dest, label %Dest
140 // and changes it into: br label %Dest
141
142 // Let the basic block know that we are letting go of one copy of it.
143 assert(BI->getParent() && "Terminator not inserted in block!");
144 Dest1->removePredecessor(BI->getParent());
145
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000146 // Change a conditional branch to unconditional.
147 BI->setUnconditionalDest(Dest1);
Chris Lattner2b058802001-06-29 23:56:58 +0000148 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000149 }
Chris Lattner9b644cc2001-09-07 16:41:30 +0000150#endif
Chris Lattner00950542001-06-06 20:29:01 +0000151 }
152 return false;
153}
154
155// ConstantFoldInstruction - If an instruction references constants, try to fold
156// them together...
157//
Chris Lattnerfaffb052001-11-26 18:57:12 +0000158bool opt::ConstantPropogation::doConstantPropogation(BasicBlock *BB,
159 BasicBlock::iterator &II) {
Chris Lattner00950542001-06-06 20:29:01 +0000160 Instruction *Inst = *II;
Chris Lattnerb00c5822001-10-02 03:41:24 +0000161 if (BinaryOperator *BInst = dyn_cast<BinaryOperator>(Inst)) {
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000162 ConstPoolVal *D1 = dyn_cast<ConstPoolVal>(Inst->getOperand(0));
163 ConstPoolVal *D2 = dyn_cast<ConstPoolVal>(Inst->getOperand(1));
Chris Lattner531450d2001-06-27 23:35:26 +0000164
165 if (D1 && D2)
Chris Lattnerfaffb052001-11-26 18:57:12 +0000166 return ConstantFoldBinaryInst(BB, II, cast<BinaryOperator>(Inst), D1, D2);
Chris Lattner00950542001-06-06 20:29:01 +0000167
Chris Lattner37aabf22001-10-31 05:07:57 +0000168 } else if (CastInst *CI = dyn_cast<CastInst>(Inst)) {
169 ConstPoolVal *D = dyn_cast<ConstPoolVal>(CI->getOperand(0));
Chris Lattnerfaffb052001-11-26 18:57:12 +0000170 if (D) return ConstantFoldCast(BB, II, CI, D);
Chris Lattner37aabf22001-10-31 05:07:57 +0000171
Chris Lattnerb00c5822001-10-02 03:41:24 +0000172 } else if (UnaryOperator *UInst = dyn_cast<UnaryOperator>(Inst)) {
173 ConstPoolVal *D = dyn_cast<ConstPoolVal>(UInst->getOperand(0));
Chris Lattnerfaffb052001-11-26 18:57:12 +0000174 if (D) return ConstantFoldUnaryInst(BB, II, UInst, D);
Chris Lattnerb00c5822001-10-02 03:41:24 +0000175 } else if (TerminatorInst *TInst = dyn_cast<TerminatorInst>(Inst)) {
176 return opt::ConstantFoldTerminator(TInst);
Chris Lattner00950542001-06-06 20:29:01 +0000177
Chris Lattnerb00c5822001-10-02 03:41:24 +0000178 } else if (PHINode *PN = dyn_cast<PHINode>(Inst)) {
179 // If it's a PHI node and only has one operand
180 // Then replace it directly with that operand.
Chris Lattner00950542001-06-06 20:29:01 +0000181 assert(PN->getOperand(0) && "PHI Node must have at least one operand!");
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000182 if (PN->getNumOperands() == 1) { // If the PHI Node has exactly 1 operand
Chris Lattner00950542001-06-06 20:29:01 +0000183 Value *V = PN->getOperand(0);
184 PN->replaceAllUsesWith(V); // Replace all uses of this PHI
185 // Unlink from basic block
Chris Lattnerfaffb052001-11-26 18:57:12 +0000186 PN->getParent()->getInstList().remove(II);
Chris Lattner9b644cc2001-09-07 16:41:30 +0000187 if (PN->hasName()) // Inherit PHINode name
Chris Lattnerfaffb052001-11-26 18:57:12 +0000188 V->setName(PN->getName(), BB->getParent()->getSymbolTableSure());
Chris Lattner00950542001-06-06 20:29:01 +0000189 delete PN; // Finally, delete the node...
190 return true;
191 }
192 }
193 return false;
194}
195
196// DoConstPropPass - Propogate constants and do constant folding on instructions
197// this returns true if something was changed, false if nothing was changed.
198//
199static bool DoConstPropPass(Method *M) {
200 bool SomethingChanged = false;
201
Chris Lattnerfaffb052001-11-26 18:57:12 +0000202 for (Method::iterator BBI = M->begin(); BBI != M->end(); ++BBI) {
203 BasicBlock *BB = *BBI;
204 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); )
205 if (opt::ConstantPropogation::doConstantPropogation(BB, I))
206 SomethingChanged = true;
207 else
208 ++I;
Chris Lattner00950542001-06-06 20:29:01 +0000209 }
Chris Lattner00950542001-06-06 20:29:01 +0000210 return SomethingChanged;
211}
212
213
Chris Lattnerfaffb052001-11-26 18:57:12 +0000214// returns whether or not the underlying method was modified
Chris Lattner00950542001-06-06 20:29:01 +0000215//
Chris Lattner5680ee62001-10-18 01:32:34 +0000216bool opt::ConstantPropogation::doConstantPropogation(Method *M) {
Chris Lattner00950542001-06-06 20:29:01 +0000217 bool Modified = false;
218
219 // Fold constants until we make no progress...
220 while (DoConstPropPass(M)) Modified = true;
221
Chris Lattner00950542001-06-06 20:29:01 +0000222 return Modified;
223}