blob: fcbf8b38c9498f5f71fbb54573ada032a40353b9 [file] [log] [blame]
Chris Lattner4fd56002002-05-08 22:19:27 +00001//===- Reassociate.cpp - Reassociate binary expressions -------------------===//
2//
3// This pass reassociates commutative expressions in an order that is designed
4// to promote better constant propogation, GCSE, LICM, PRE...
5//
6// For example: 4 + (x + 5) -> x + (4 + 5)
7//
8// Note that this pass works best if left shifts have been promoted to explicit
9// multiplies before this pass executes.
10//
11// In the implementation of this algorithm, constants are assigned rank = 0,
12// function arguments are rank = 1, and other values are assigned ranks
13// corresponding to the reverse post order traversal of current function
14// (starting at 2), which effectively gives values in deep loops higher rank
15// than values not in loops.
16//
17//===----------------------------------------------------------------------===//
18
19#include "llvm/Transforms/Scalar.h"
20#include "llvm/Function.h"
21#include "llvm/BasicBlock.h"
22#include "llvm/iOperators.h"
23#include "llvm/Type.h"
24#include "llvm/Pass.h"
25#include "llvm/Constant.h"
26#include "llvm/Support/CFG.h"
27#include "Support/PostOrderIterator.h"
Chris Lattner3dec1f22002-05-10 15:38:35 +000028#include "Support/StatisticReporter.h"
29
Chris Lattnera36e6c82002-05-16 04:37:07 +000030static Statistic<> NumLinear ("reassociate\t- Number of insts linearized");
Chris Lattner3dec1f22002-05-10 15:38:35 +000031static Statistic<> NumChanged("reassociate\t- Number of insts reassociated");
32static Statistic<> NumSwapped("reassociate\t- Number of insts with operands swapped");
Chris Lattner4fd56002002-05-08 22:19:27 +000033
34namespace {
35 class Reassociate : public FunctionPass {
36 map<BasicBlock*, unsigned> RankMap;
37 public:
38 const char *getPassName() const {
39 return "Expression Reassociation";
40 }
41
42 bool runOnFunction(Function *F);
43
44 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
45 AU.preservesCFG();
46 }
47 private:
48 void BuildRankMap(Function *F);
49 unsigned getRank(Value *V);
50 bool ReassociateExpr(BinaryOperator *I);
51 bool ReassociateBB(BasicBlock *BB);
52 };
53}
54
55Pass *createReassociatePass() { return new Reassociate(); }
56
57void Reassociate::BuildRankMap(Function *F) {
58 unsigned i = 1;
59 ReversePostOrderTraversal<Function*> RPOT(F);
60 for (ReversePostOrderTraversal<Function*>::rpo_iterator I = RPOT.begin(),
61 E = RPOT.end(); I != E; ++I)
62 RankMap[*I] = ++i;
63}
64
65unsigned Reassociate::getRank(Value *V) {
66 if (isa<Argument>(V)) return 1; // Function argument...
67 if (Instruction *I = dyn_cast<Instruction>(V)) {
68 // If this is an expression, return the MAX(rank(LHS), rank(RHS)) so that we
69 // can reassociate expressions for code motion! Since we do not recurse for
70 // PHI nodes, we cannot have infinite recursion here, because there cannot
71 // be loops in the value graph (except for PHI nodes).
72 //
73 if (I->getOpcode() == Instruction::PHINode ||
74 I->getOpcode() == Instruction::Alloca ||
75 I->getOpcode() == Instruction::Malloc || isa<TerminatorInst>(I) ||
76 I->hasSideEffects())
77 return RankMap[I->getParent()];
78
Chris Lattnera36e6c82002-05-16 04:37:07 +000079 unsigned Rank = 0, MaxRank = RankMap[I->getParent()];
80 for (unsigned i = 0, e = I->getNumOperands();
81 i != e && Rank != MaxRank; ++i)
Chris Lattner4fd56002002-05-08 22:19:27 +000082 Rank = std::max(Rank, getRank(I->getOperand(i)));
83
84 return Rank;
85 }
86
87 // Otherwise it's a global or constant, rank 0.
88 return 0;
89}
90
91
92// isCommutativeOperator - Return true if the specified instruction is
93// commutative and associative. If the instruction is not commutative and
94// associative, we can not reorder its operands!
95//
96static inline BinaryOperator *isCommutativeOperator(Instruction *I) {
97 // Floating point operations do not commute!
98 if (I->getType()->isFloatingPoint()) return 0;
99
100 if (I->getOpcode() == Instruction::Add ||
101 I->getOpcode() == Instruction::Mul ||
102 I->getOpcode() == Instruction::And ||
103 I->getOpcode() == Instruction::Or ||
104 I->getOpcode() == Instruction::Xor)
105 return cast<BinaryOperator>(I);
106 return 0;
107}
108
109
110bool Reassociate::ReassociateExpr(BinaryOperator *I) {
111 Value *LHS = I->getOperand(0);
112 Value *RHS = I->getOperand(1);
113 unsigned LHSRank = getRank(LHS);
114 unsigned RHSRank = getRank(RHS);
115
116 bool Changed = false;
117
118 // Make sure the LHS of the operand always has the greater rank...
119 if (LHSRank < RHSRank) {
120 I->swapOperands();
121 std::swap(LHS, RHS);
122 std::swap(LHSRank, RHSRank);
123 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +0000124 ++NumSwapped;
Chris Lattnerf016ea42002-05-22 17:17:27 +0000125 DEBUG(std::cerr << "Transposed: " << I << " Result BB: " << I->getParent());
Chris Lattner4fd56002002-05-08 22:19:27 +0000126 }
127
128 // If the LHS is the same operator as the current one is, and if we are the
129 // only expression using it...
130 //
131 if (BinaryOperator *LHSI = dyn_cast<BinaryOperator>(LHS))
132 if (LHSI->getOpcode() == I->getOpcode() && LHSI->use_size() == 1) {
133 // If the rank of our current RHS is less than the rank of the LHS's LHS,
134 // then we reassociate the two instructions...
135 if (RHSRank < getRank(LHSI->getOperand(0))) {
136 unsigned TakeOp = 0;
137 if (BinaryOperator *IOp = dyn_cast<BinaryOperator>(LHSI->getOperand(0)))
138 if (IOp->getOpcode() == LHSI->getOpcode())
139 TakeOp = 1; // Hoist out non-tree portion
140
141 // Convert ((a + 12) + 10) into (a + (12 + 10))
142 I->setOperand(0, LHSI->getOperand(TakeOp));
143 LHSI->setOperand(TakeOp, RHS);
144 I->setOperand(1, LHSI);
145
Chris Lattner3dec1f22002-05-10 15:38:35 +0000146 ++NumChanged;
Chris Lattnerf016ea42002-05-22 17:17:27 +0000147 DEBUG(std::cerr << "Reassociated: " << I << " Result BB: "
148 << I->getParent());
Chris Lattner4fd56002002-05-08 22:19:27 +0000149
150 // Since we modified the RHS instruction, make sure that we recheck it.
151 ReassociateExpr(LHSI);
152 return true;
153 }
154 }
155
156 return Changed;
157}
158
159
Chris Lattnera36e6c82002-05-16 04:37:07 +0000160// NegateValue - Insert instructions before the instruction pointed to by BI,
161// that computes the negative version of the value specified. The negative
162// version of the value is returned, and BI is left pointing at the instruction
163// that should be processed next by the reassociation pass.
164//
165static Value *NegateValue(Value *V, BasicBlock *BB, BasicBlock::iterator &BI) {
166 // We are trying to expose opportunity for reassociation. One of the things
167 // that we want to do to achieve this is to push a negation as deep into an
168 // expression chain as possible, to expose the add instructions. In practice,
169 // this means that we turn this:
170 // X = -(A+12+C+D) into X = -A + -12 + -C + -D = -12 + -A + -C + -D
171 // so that later, a: Y = 12+X could get reassociated with the -12 to eliminate
172 // the constants. We assume that instcombine will clean up the mess later if
173 // we introduce tons of unneccesary negation instructions...
174 //
175 if (Instruction *I = dyn_cast<Instruction>(V))
176 if (I->getOpcode() == Instruction::Add && I->use_size() == 1) {
177 Value *RHS = NegateValue(I->getOperand(1), BB, BI);
178 Value *LHS = NegateValue(I->getOperand(0), BB, BI);
179
180 // We must actually insert a new add instruction here, because the neg
181 // instructions do not dominate the old add instruction in general. By
182 // adding it now, we are assured that the neg instructions we just
183 // inserted dominate the instruction we are about to insert after them.
184 //
185 BasicBlock::iterator NBI = BI;
186
187 // Scan through the inserted instructions, looking for RHS, which must be
188 // after LHS in the instruction list.
189 while (*NBI != RHS) ++NBI;
190
191 Instruction *Add =
192 BinaryOperator::create(Instruction::Add, LHS, RHS, I->getName()+".neg");
193 BB->getInstList().insert(NBI+1, Add); // Add to the basic block...
194 return Add;
195 }
196
197 // Insert a 'neg' instruction that subtracts the value from zero to get the
198 // negation.
199 //
200 Instruction *Neg =
201 BinaryOperator::create(Instruction::Sub,
202 Constant::getNullValue(V->getType()), V,
203 V->getName()+".neg");
204 BI = BB->getInstList().insert(BI, Neg); // Add to the basic block...
205 return Neg;
206}
207
208
Chris Lattner4fd56002002-05-08 22:19:27 +0000209bool Reassociate::ReassociateBB(BasicBlock *BB) {
210 bool Changed = false;
211 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end(); ++BI) {
212 Instruction *Inst = *BI;
213
214 // If this instruction is a commutative binary operator, and the ranks of
215 // the two operands are sorted incorrectly, fix it now.
216 //
217 if (BinaryOperator *I = isCommutativeOperator(Inst)) {
Chris Lattnera36e6c82002-05-16 04:37:07 +0000218 if (!I->use_empty()) {
219 // Make sure that we don't have a tree-shaped computation. If we do,
220 // linearize it. Convert (A+B)+(C+D) into ((A+B)+C)+D
221 //
222 Instruction *LHSI = dyn_cast<Instruction>(I->getOperand(0));
223 Instruction *RHSI = dyn_cast<Instruction>(I->getOperand(1));
224 if (LHSI && (int)LHSI->getOpcode() == I->getOpcode() &&
225 RHSI && (int)RHSI->getOpcode() == I->getOpcode() &&
226 RHSI->use_size() == 1) {
227 // Insert a new temporary instruction... (A+B)+C
228 BinaryOperator *Tmp = BinaryOperator::create(I->getOpcode(), LHSI,
229 RHSI->getOperand(0),
230 RHSI->getName()+".ra");
231 BI = BB->getInstList().insert(BI, Tmp); // Add to the basic block...
232 I->setOperand(0, Tmp);
233 I->setOperand(1, RHSI->getOperand(1));
234
235 // Process the temporary instruction for reassociation now.
236 I = Tmp;
237 ++NumLinear;
238 Changed = true;
Chris Lattnerf016ea42002-05-22 17:17:27 +0000239 DEBUG(std::cerr << "Linearized: " << I << " Result BB: " << BB);
Chris Lattnera36e6c82002-05-16 04:37:07 +0000240 }
241
242 // Make sure that this expression is correctly reassociated with respect
243 // to it's used values...
244 //
245 Changed |= ReassociateExpr(I);
246 }
Chris Lattner4fd56002002-05-08 22:19:27 +0000247
248 } else if (Inst->getOpcode() == Instruction::Sub &&
249 Inst->getOperand(0) != Constant::getNullValue(Inst->getType())) {
250 // Convert a subtract into an add and a neg instruction... so that sub
251 // instructions can be commuted with other add instructions...
252 //
253 Instruction *New = BinaryOperator::create(Instruction::Add,
Chris Lattnera36e6c82002-05-16 04:37:07 +0000254 Inst->getOperand(0),
255 Inst->getOperand(1),
Chris Lattner0aa7cd62002-05-09 20:11:23 +0000256 Inst->getName());
Chris Lattnera36e6c82002-05-16 04:37:07 +0000257 Value *NegatedValue = Inst->getOperand(1);
258
Chris Lattner4fd56002002-05-08 22:19:27 +0000259 // Everyone now refers to the add instruction...
260 Inst->replaceAllUsesWith(New);
Chris Lattner4fd56002002-05-08 22:19:27 +0000261
Chris Lattnera36e6c82002-05-16 04:37:07 +0000262 // Put the new add in the place of the subtract... deleting the subtract
263 delete BB->getInstList().replaceWith(BI, New);
264
265 // Calculate the negative value of Operand 1 of the sub instruction...
266 // and set it as the RHS of the add instruction we just made...
267 New->setOperand(1, NegateValue(NegatedValue, BB, BI));
268 --BI;
Chris Lattner4fd56002002-05-08 22:19:27 +0000269 Changed = true;
Chris Lattnerf016ea42002-05-22 17:17:27 +0000270 DEBUG(std::cerr << "Negated: " << New << " Result BB: " << BB);
Chris Lattner4fd56002002-05-08 22:19:27 +0000271 }
272 }
273
274 return Changed;
275}
276
277
278bool Reassociate::runOnFunction(Function *F) {
279 // Recalculate the rank map for F
280 BuildRankMap(F);
281
282 bool Changed = false;
283 for (Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
284 Changed |= ReassociateBB(*FI);
285
286 // We are done with the rank map...
287 RankMap.clear();
288 return Changed;
289}