blob: fc4e3bf0e3eaa61c97da373823847458ab9875bb [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 Lattnerbd0ef772002-02-26 21:46:54 +000032#include "llvm/Pass.h"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000033#include "llvm/ConstantVals.h"
Chris Lattner00950542001-06-06 20:29:01 +000034
35inline static bool
Chris Lattnerfaffb052001-11-26 18:57:12 +000036ConstantFoldUnaryInst(BasicBlock *BB, BasicBlock::iterator &II,
Chris Lattnere9bb2df2001-12-03 22:26:30 +000037 UnaryOperator *Op, Constant *D) {
Chris Lattner59b6b8e2002-01-21 23:17:48 +000038 Constant *ReplaceWith = ConstantFoldUnaryInstruction(Op->getOpcode(), D);
Chris Lattner00950542001-06-06 20:29:01 +000039
40 if (!ReplaceWith) return false; // Nothing new to change...
41
Chris Lattner00950542001-06-06 20:29:01 +000042 // Replaces all of the uses of a variable with uses of the constant.
43 Op->replaceAllUsesWith(ReplaceWith);
44
45 // Remove the operator from the list of definitions...
Chris Lattnerfaffb052001-11-26 18:57:12 +000046 Op->getParent()->getInstList().remove(II);
Chris Lattner00950542001-06-06 20:29:01 +000047
48 // The new constant inherits the old name of the operator...
Chris Lattner9b644cc2001-09-07 16:41:30 +000049 if (Op->hasName())
Chris Lattnerfaffb052001-11-26 18:57:12 +000050 ReplaceWith->setName(Op->getName(), BB->getParent()->getSymbolTableSure());
Chris Lattner00950542001-06-06 20:29:01 +000051
52 // Delete the operator now...
53 delete Op;
54 return true;
55}
56
57inline static bool
Chris Lattnerfaffb052001-11-26 18:57:12 +000058ConstantFoldCast(BasicBlock *BB, BasicBlock::iterator &II,
Chris Lattnere9bb2df2001-12-03 22:26:30 +000059 CastInst *CI, Constant *D) {
Chris Lattner59b6b8e2002-01-21 23:17:48 +000060 Constant *ReplaceWith = ConstantFoldCastInstruction(D, CI->getType());
Chris Lattner37aabf22001-10-31 05:07:57 +000061
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,
Chris Lattnere9bb2df2001-12-03 22:26:30 +000082 Constant *D1, Constant *D2) {
Chris Lattner59b6b8e2002-01-21 23:17:48 +000083 Constant *ReplaceWith = ConstantFoldBinaryInstruction(Op->getOpcode(), D1,D2);
Chris Lattner00950542001-06-06 20:29:01 +000084 if (!ReplaceWith) return false; // Nothing new to change...
85
Chris Lattner00950542001-06-06 20:29:01 +000086 // Replaces all of the uses of a variable with uses of the constant.
87 Op->replaceAllUsesWith(ReplaceWith);
88
89 // Remove the operator from the list of definitions...
Chris Lattnerfaffb052001-11-26 18:57:12 +000090 Op->getParent()->getInstList().remove(II);
Chris Lattner00950542001-06-06 20:29:01 +000091
92 // The new constant inherits the old name of the operator...
Chris Lattner9b644cc2001-09-07 16:41:30 +000093 if (Op->hasName())
Chris Lattnerfaffb052001-11-26 18:57:12 +000094 ReplaceWith->setName(Op->getName(), BB->getParent()->getSymbolTableSure());
Chris Lattner00950542001-06-06 20:29:01 +000095
96 // Delete the operator now...
97 delete Op;
98 return true;
99}
100
Chris Lattner2b058802001-06-29 23:56:58 +0000101// ConstantFoldTerminator - If a terminator instruction is predicated on a
102// constant value, convert it into an unconditional branch to the constant
103// destination.
104//
Chris Lattner59b6b8e2002-01-21 23:17:48 +0000105bool ConstantFoldTerminator(TerminatorInst *T) {
Chris Lattner00950542001-06-06 20:29:01 +0000106 // Branch - See if we are conditional jumping on constant
Chris Lattnerb00c5822001-10-02 03:41:24 +0000107 if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
Chris Lattner2b058802001-06-29 23:56:58 +0000108 if (BI->isUnconditional()) return false; // Can't optimize uncond branch
Chris Lattner9636a912001-10-01 16:18:37 +0000109 BasicBlock *Dest1 = cast<BasicBlock>(BI->getOperand(0));
110 BasicBlock *Dest2 = cast<BasicBlock>(BI->getOperand(1));
Chris Lattner2b058802001-06-29 23:56:58 +0000111
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000112 if (ConstantBool *Cond = dyn_cast<ConstantBool>(BI->getCondition())) {
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000113 // Are we branching on constant?
Chris Lattner00950542001-06-06 20:29:01 +0000114 // YES. Change to unconditional branch...
Chris Lattner2b058802001-06-29 23:56:58 +0000115 BasicBlock *Destination = Cond->getValue() ? Dest1 : Dest2;
116 BasicBlock *OldDest = Cond->getValue() ? Dest2 : Dest1;
Chris Lattnerbca26a42001-06-29 05:23:10 +0000117
Chris Lattner2b058802001-06-29 23:56:58 +0000118 //cerr << "Method: " << T->getParent()->getParent()
119 // << "\nRemoving branch from " << T->getParent()
120 // << "\n\nTo: " << OldDest << endl;
Chris Lattnerbca26a42001-06-29 05:23:10 +0000121
122 // Let the basic block know that we are letting go of it. Based on this,
123 // it will adjust it's PHI nodes.
Chris Lattner2b058802001-06-29 23:56:58 +0000124 assert(BI->getParent() && "Terminator not inserted in block!");
125 OldDest->removePredecessor(BI->getParent());
Chris Lattner00950542001-06-06 20:29:01 +0000126
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000127 // Set the unconditional destination, and change the insn to be an
128 // unconditional branch.
129 BI->setUnconditionalDest(Destination);
Chris Lattner00950542001-06-06 20:29:01 +0000130 return true;
Chris Lattner9b644cc2001-09-07 16:41:30 +0000131 }
132#if 0
133 // FIXME: TODO: This doesn't work if the destination has PHI nodes with
134 // different incoming values on each branch!
135 //
136 else if (Dest2 == Dest1) { // Conditional branch to same location?
Chris Lattner2b058802001-06-29 23:56:58 +0000137 // This branch matches something like this:
138 // br bool %cond, label %Dest, label %Dest
139 // and changes it into: br label %Dest
140
141 // Let the basic block know that we are letting go of one copy of it.
142 assert(BI->getParent() && "Terminator not inserted in block!");
143 Dest1->removePredecessor(BI->getParent());
144
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000145 // Change a conditional branch to unconditional.
146 BI->setUnconditionalDest(Dest1);
Chris Lattner2b058802001-06-29 23:56:58 +0000147 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000148 }
Chris Lattner9b644cc2001-09-07 16:41:30 +0000149#endif
Chris Lattner00950542001-06-06 20:29:01 +0000150 }
151 return false;
152}
153
154// ConstantFoldInstruction - If an instruction references constants, try to fold
155// them together...
156//
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000157bool doConstantPropogation(BasicBlock *BB, 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 Lattnerbd0ef772002-02-26 21:46:54 +0000203 if (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
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000211namespace {
212 struct ConstantPropogation : public MethodPass {
213 inline bool runOnMethod(Method *M) {
214 bool Modified = false;
Chris Lattner00950542001-06-06 20:29:01 +0000215
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000216 // Fold constants until we make no progress...
217 while (DoConstPropPass(M)) Modified = true;
218
219 return Modified;
220 }
221 };
Chris Lattner00950542001-06-06 20:29:01 +0000222}
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000223
224Pass *createConstantPropogationPass() {
225 return new ConstantPropogation();
226}
227