blob: 77a959910fcc5c2856e969d24b6ceef565c0eb8a [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"
Chris Lattner968ddc92002-04-08 20:18:09 +000025#include "llvm/ConstantHandling.h"
Chris Lattner00950542001-06-06 20:29:01 +000026#include "llvm/Module.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000027#include "llvm/Function.h"
Chris Lattner00950542001-06-06 20:29:01 +000028#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 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 Lattner0fce76a2002-03-11 22:11:07 +0000104bool ConstantFoldTerminator(BasicBlock *BB, BasicBlock::iterator &II,
105 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 Lattner2fbfdcf2002-04-07 20:49:59 +0000118 //cerr << "Function: " << T->getParent()->getParent()
Chris Lattner2b058802001-06-29 23:56:58 +0000119 // << "\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 Lattner0fce76a2002-03-11 22:11:07 +0000130 II = BB->end()-1; // Update instruction iterator!
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 Lattnerbd0ef772002-02-26 21:46:54 +0000158bool doConstantPropogation(BasicBlock *BB, BasicBlock::iterator &II) {
Chris Lattner00950542001-06-06 20:29:01 +0000159 Instruction *Inst = *II;
Chris Lattner697954c2002-01-20 22:54:45 +0000160 if (isa<BinaryOperator>(Inst)) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000161 Constant *D1 = dyn_cast<Constant>(Inst->getOperand(0));
162 Constant *D2 = dyn_cast<Constant>(Inst->getOperand(1));
Chris Lattner531450d2001-06-27 23:35:26 +0000163
164 if (D1 && D2)
Chris Lattnerfaffb052001-11-26 18:57:12 +0000165 return ConstantFoldBinaryInst(BB, II, cast<BinaryOperator>(Inst), D1, D2);
Chris Lattner00950542001-06-06 20:29:01 +0000166
Chris Lattner37aabf22001-10-31 05:07:57 +0000167 } else if (CastInst *CI = dyn_cast<CastInst>(Inst)) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000168 Constant *D = dyn_cast<Constant>(CI->getOperand(0));
Chris Lattnerfaffb052001-11-26 18:57:12 +0000169 if (D) return ConstantFoldCast(BB, II, CI, D);
Chris Lattner37aabf22001-10-31 05:07:57 +0000170
Chris Lattnerb00c5822001-10-02 03:41:24 +0000171 } else if (UnaryOperator *UInst = dyn_cast<UnaryOperator>(Inst)) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000172 Constant *D = dyn_cast<Constant>(UInst->getOperand(0));
Chris Lattnerfaffb052001-11-26 18:57:12 +0000173 if (D) return ConstantFoldUnaryInst(BB, II, UInst, D);
Chris Lattnerb00c5822001-10-02 03:41:24 +0000174 } else if (TerminatorInst *TInst = dyn_cast<TerminatorInst>(Inst)) {
Chris Lattner0fce76a2002-03-11 22:11:07 +0000175 return ConstantFoldTerminator(BB, II, TInst);
Chris Lattner00950542001-06-06 20:29:01 +0000176
Chris Lattnerb00c5822001-10-02 03:41:24 +0000177 } else if (PHINode *PN = dyn_cast<PHINode>(Inst)) {
178 // If it's a PHI node and only has one operand
179 // Then replace it directly with that operand.
Chris Lattner9102aee2001-12-13 00:45:40 +0000180 assert(PN->getNumOperands() && "PHI Node must have at least one operand!");
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000181 if (PN->getNumOperands() == 1) { // If the PHI Node has exactly 1 operand
Chris Lattner00950542001-06-06 20:29:01 +0000182 Value *V = PN->getOperand(0);
183 PN->replaceAllUsesWith(V); // Replace all uses of this PHI
184 // Unlink from basic block
Chris Lattnerfaffb052001-11-26 18:57:12 +0000185 PN->getParent()->getInstList().remove(II);
Chris Lattner9b644cc2001-09-07 16:41:30 +0000186 if (PN->hasName()) // Inherit PHINode name
Chris Lattnerfaffb052001-11-26 18:57:12 +0000187 V->setName(PN->getName(), BB->getParent()->getSymbolTableSure());
Chris Lattner00950542001-06-06 20:29:01 +0000188 delete PN; // Finally, delete the node...
189 return true;
190 }
191 }
192 return false;
193}
194
195// DoConstPropPass - Propogate constants and do constant folding on instructions
196// this returns true if something was changed, false if nothing was changed.
197//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000198static bool DoConstPropPass(Function *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000199 bool SomethingChanged = false;
200
Chris Lattner237e6d12002-04-08 22:03:00 +0000201 for (Function::iterator BBI = F->begin(); BBI != F->end(); ++BBI) {
Chris Lattnerfaffb052001-11-26 18:57:12 +0000202 BasicBlock *BB = *BBI;
203 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); )
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000204 if (doConstantPropogation(BB, I))
Chris Lattnerfaffb052001-11-26 18:57:12 +0000205 SomethingChanged = true;
206 else
207 ++I;
Chris Lattner00950542001-06-06 20:29:01 +0000208 }
Chris Lattner00950542001-06-06 20:29:01 +0000209 return SomethingChanged;
210}
211
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000212namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +0000213 struct ConstantPropogation : public FunctionPass {
Chris Lattner96c466b2002-04-29 14:57:45 +0000214 const char *getPassName() const { return "Simple Constant Propogation"; }
215
Chris Lattnerf57b8452002-04-27 06:56:12 +0000216 inline bool runOnFunction(Function *F) {
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000217 bool Modified = false;
Chris Lattner00950542001-06-06 20:29:01 +0000218
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000219 // Fold constants until we make no progress...
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000220 while (DoConstPropPass(F)) Modified = true;
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000221
222 return Modified;
223 }
Chris Lattner97e52e42002-04-28 21:27:06 +0000224
225 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
226 // FIXME: This pass does not preserve the CFG because it folds terminator
227 // instructions!
228 //AU.preservesCFG();
229 }
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000230 };
Chris Lattner00950542001-06-06 20:29:01 +0000231}
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000232
233Pass *createConstantPropogationPass() {
234 return new ConstantPropogation();
235}
236