blob: b4583e009631b597c131a0df2a2e0e274982ed09 [file] [log] [blame]
Chris Lattner1467f642002-04-28 00:47:11 +00001//===-- GCSE.cpp - SSA based Global Common Subexpr Elimination ------------===//
2//
3// This pass is designed to be a very quick global transformation that
4// eliminates global common subexpressions from a function. It does this by
5// examining the SSA value graph of the function, instead of doing slow, dense,
6// bit-vector computations.
7//
Chris Lattner1467f642002-04-28 00:47:11 +00008//===----------------------------------------------------------------------===//
9
Chris Lattnerb4cfa7f2002-05-07 20:03:00 +000010#include "llvm/Transforms/Scalar.h"
Chris Lattner1467f642002-04-28 00:47:11 +000011#include "llvm/InstrTypes.h"
12#include "llvm/iMemory.h"
13#include "llvm/Analysis/Dominators.h"
Chris Lattner879cb972002-08-22 18:24:48 +000014#include "llvm/Analysis/AliasAnalysis.h"
Chris Lattner1467f642002-04-28 00:47:11 +000015#include "llvm/Support/InstVisitor.h"
16#include "llvm/Support/InstIterator.h"
Chris Lattnerd38ddb12002-05-14 05:02:40 +000017#include "llvm/Support/CFG.h"
Chris Lattner0b18c1d2002-05-10 15:38:35 +000018#include "Support/StatisticReporter.h"
Chris Lattner1467f642002-04-28 00:47:11 +000019#include <algorithm>
Anand Shukla2bc64192002-06-25 21:07:58 +000020using std::set;
21using std::map;
22
Chris Lattner1467f642002-04-28 00:47:11 +000023
Chris Lattner0b18c1d2002-05-10 15:38:35 +000024static Statistic<> NumInstRemoved("gcse\t\t- Number of instructions removed");
Chris Lattnerd38ddb12002-05-14 05:02:40 +000025static Statistic<> NumLoadRemoved("gcse\t\t- Number of loads removed");
Chris Lattner0b18c1d2002-05-10 15:38:35 +000026
Chris Lattner1467f642002-04-28 00:47:11 +000027namespace {
28 class GCSE : public FunctionPass, public InstVisitor<GCSE, bool> {
Chris Lattnerd38ddb12002-05-14 05:02:40 +000029 set<Instruction*> WorkList;
30 DominatorSet *DomSetInfo;
31 ImmediateDominators *ImmDominator;
Chris Lattner879cb972002-08-22 18:24:48 +000032 AliasAnalysis *AA;
Chris Lattnerd38ddb12002-05-14 05:02:40 +000033
Chris Lattner1467f642002-04-28 00:47:11 +000034 public:
Chris Lattner113f4f42002-06-25 16:13:24 +000035 virtual bool runOnFunction(Function &F);
Chris Lattner1467f642002-04-28 00:47:11 +000036
37 // Visitation methods, these are invoked depending on the type of
38 // instruction being checked. They should return true if a common
39 // subexpression was folded.
40 //
Chris Lattner113f4f42002-06-25 16:13:24 +000041 bool visitBinaryOperator(Instruction &I);
42 bool visitGetElementPtrInst(GetElementPtrInst &I);
Chris Lattnerb193ff82002-08-14 18:18:02 +000043 bool visitCastInst(CastInst &I);
Chris Lattner113f4f42002-06-25 16:13:24 +000044 bool visitShiftInst(ShiftInst &I) {
45 return visitBinaryOperator((Instruction&)I);
Chris Lattner1467f642002-04-28 00:47:11 +000046 }
Chris Lattner113f4f42002-06-25 16:13:24 +000047 bool visitLoadInst(LoadInst &LI);
48 bool visitInstruction(Instruction &) { return false; }
Chris Lattner1467f642002-04-28 00:47:11 +000049
50 private:
51 void ReplaceInstWithInst(Instruction *First, BasicBlock::iterator SI);
Chris Lattner879cb972002-08-22 18:24:48 +000052 bool CommonSubExpressionFound(Instruction *I, Instruction *Other);
Chris Lattner1467f642002-04-28 00:47:11 +000053
Chris Lattnerd38ddb12002-05-14 05:02:40 +000054 // TryToRemoveALoad - Try to remove one of L1 or L2. The problem with
55 // removing loads is that intervening stores might make otherwise identical
56 // load's yield different values. To ensure that this is not the case, we
57 // check that there are no intervening stores or calls between the
58 // instructions.
59 //
60 bool TryToRemoveALoad(LoadInst *L1, LoadInst *L2);
61
62 // CheckForInvalidatingInst - Return true if BB or any of the predecessors
Chris Lattner879cb972002-08-22 18:24:48 +000063 // of BB (until DestBB) contain an instruction that might invalidate Ptr.
Chris Lattnerd38ddb12002-05-14 05:02:40 +000064 //
65 bool CheckForInvalidatingInst(BasicBlock *BB, BasicBlock *DestBB,
Chris Lattner879cb972002-08-22 18:24:48 +000066 Value *Ptr, set<BasicBlock*> &VisitedSet);
Chris Lattnerd38ddb12002-05-14 05:02:40 +000067
Chris Lattner1467f642002-04-28 00:47:11 +000068 // This transformation requires dominator and immediate dominator info
69 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerf12cc842002-04-28 21:27:06 +000070 AU.preservesCFG();
Chris Lattnerf0ed55d2002-08-08 19:01:30 +000071 AU.addRequired<DominatorSet>();
72 AU.addRequired<ImmediateDominators>();
Chris Lattner879cb972002-08-22 18:24:48 +000073 AU.addRequired<AliasAnalysis>();
Chris Lattner1467f642002-04-28 00:47:11 +000074 }
75 };
Chris Lattnerb28b6802002-07-23 18:06:35 +000076
Chris Lattnerc8b70922002-07-26 21:12:46 +000077 RegisterOpt<GCSE> X("gcse", "Global Common Subexpression Elimination");
Chris Lattner1467f642002-04-28 00:47:11 +000078}
79
80// createGCSEPass - The public interface to this file...
81Pass *createGCSEPass() { return new GCSE(); }
82
83
84// GCSE::runOnFunction - This is the main transformation entry point for a
85// function.
86//
Chris Lattner113f4f42002-06-25 16:13:24 +000087bool GCSE::runOnFunction(Function &F) {
Chris Lattner1467f642002-04-28 00:47:11 +000088 bool Changed = false;
89
Chris Lattner879cb972002-08-22 18:24:48 +000090 // Get pointers to the analysis results that we will be using...
Chris Lattner1467f642002-04-28 00:47:11 +000091 DomSetInfo = &getAnalysis<DominatorSet>();
92 ImmDominator = &getAnalysis<ImmediateDominators>();
Chris Lattner879cb972002-08-22 18:24:48 +000093 AA = &getAnalysis<AliasAnalysis>();
Chris Lattner1467f642002-04-28 00:47:11 +000094
95 // Step #1: Add all instructions in the function to the worklist for
96 // processing. All of the instructions are considered to be our
97 // subexpressions to eliminate if possible.
98 //
99 WorkList.insert(inst_begin(F), inst_end(F));
100
101 // Step #2: WorkList processing. Iterate through all of the instructions,
102 // checking to see if there are any additionally defined subexpressions in the
103 // program. If so, eliminate them!
104 //
105 while (!WorkList.empty()) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000106 Instruction &I = **WorkList.begin(); // Get an instruction from the worklist
Chris Lattner1467f642002-04-28 00:47:11 +0000107 WorkList.erase(WorkList.begin());
108
109 // Visit the instruction, dispatching to the correct visit function based on
110 // the instruction type. This does the checking.
111 //
112 Changed |= visit(I);
113 }
Chris Lattnerd38ddb12002-05-14 05:02:40 +0000114
Chris Lattner1467f642002-04-28 00:47:11 +0000115 // When the worklist is empty, return whether or not we changed anything...
116 return Changed;
117}
118
119
120// ReplaceInstWithInst - Destroy the instruction pointed to by SI, making all
121// uses of the instruction use First now instead.
122//
123void GCSE::ReplaceInstWithInst(Instruction *First, BasicBlock::iterator SI) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000124 Instruction &Second = *SI;
Chris Lattner2dfc6672002-04-29 16:20:25 +0000125
126 //cerr << "DEL " << (void*)Second << Second;
Chris Lattner1467f642002-04-28 00:47:11 +0000127
128 // Add the first instruction back to the worklist
129 WorkList.insert(First);
130
131 // Add all uses of the second instruction to the worklist
Chris Lattner113f4f42002-06-25 16:13:24 +0000132 for (Value::use_iterator UI = Second.use_begin(), UE = Second.use_end();
Chris Lattner1467f642002-04-28 00:47:11 +0000133 UI != UE; ++UI)
134 WorkList.insert(cast<Instruction>(*UI));
135
136 // Make all users of 'Second' now use 'First'
Chris Lattner113f4f42002-06-25 16:13:24 +0000137 Second.replaceAllUsesWith(First);
Chris Lattner1467f642002-04-28 00:47:11 +0000138
139 // Erase the second instruction from the program
Chris Lattner113f4f42002-06-25 16:13:24 +0000140 Second.getParent()->getInstList().erase(SI);
Chris Lattner1467f642002-04-28 00:47:11 +0000141}
142
143// CommonSubExpressionFound - The two instruction I & Other have been found to
144// be common subexpressions. This function is responsible for eliminating one
145// of them, and for fixing the worklist to be correct.
146//
Chris Lattner879cb972002-08-22 18:24:48 +0000147bool GCSE::CommonSubExpressionFound(Instruction *I, Instruction *Other) {
Chris Lattnerd38ddb12002-05-14 05:02:40 +0000148 assert(I != Other);
Chris Lattner2dfc6672002-04-29 16:20:25 +0000149
Chris Lattnerd38ddb12002-05-14 05:02:40 +0000150 WorkList.erase(I);
Chris Lattner2dfc6672002-04-29 16:20:25 +0000151 WorkList.erase(Other); // Other may not actually be on the worklist anymore...
Chris Lattner1467f642002-04-28 00:47:11 +0000152
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000153 ++NumInstRemoved; // Keep track of number of instructions eliminated
154
Chris Lattner1467f642002-04-28 00:47:11 +0000155 // Handle the easy case, where both instructions are in the same basic block
156 BasicBlock *BB1 = I->getParent(), *BB2 = Other->getParent();
157 if (BB1 == BB2) {
158 // Eliminate the second occuring instruction. Add all uses of the second
159 // instruction to the worklist.
160 //
161 // Scan the basic block looking for the "first" instruction
162 BasicBlock::iterator BI = BB1->begin();
Chris Lattner113f4f42002-06-25 16:13:24 +0000163 while (&*BI != I && &*BI != Other) {
Chris Lattner1467f642002-04-28 00:47:11 +0000164 ++BI;
165 assert(BI != BB1->end() && "Instructions not found in parent BB!");
166 }
167
168 // Keep track of which instructions occurred first & second
Chris Lattner113f4f42002-06-25 16:13:24 +0000169 Instruction *First = BI;
Chris Lattner1467f642002-04-28 00:47:11 +0000170 Instruction *Second = I != First ? I : Other; // Get iterator to second inst
Chris Lattner113f4f42002-06-25 16:13:24 +0000171 BI = Second;
Chris Lattner1467f642002-04-28 00:47:11 +0000172
173 // Destroy Second, using First instead.
174 ReplaceInstWithInst(First, BI);
175
176 // Otherwise, the two instructions are in different basic blocks. If one
177 // dominates the other instruction, we can simply use it
178 //
179 } else if (DomSetInfo->dominates(BB1, BB2)) { // I dom Other?
Chris Lattner113f4f42002-06-25 16:13:24 +0000180 ReplaceInstWithInst(I, Other);
Chris Lattner1467f642002-04-28 00:47:11 +0000181 } else if (DomSetInfo->dominates(BB2, BB1)) { // Other dom I?
Chris Lattner113f4f42002-06-25 16:13:24 +0000182 ReplaceInstWithInst(Other, I);
Chris Lattner1467f642002-04-28 00:47:11 +0000183 } else {
Chris Lattnerf56bd892002-08-02 18:06:01 +0000184 // This code is disabled because it has several problems:
185 // One, the actual assumption is wrong, as shown by this code:
186 // int "test"(int %X, int %Y) {
187 // %Z = add int %X, %Y
188 // ret int %Z
189 // Unreachable:
190 // %Q = add int %X, %Y
191 // ret int %Q
192 // }
193 //
194 // Here there are no shared dominators. Additionally, this had the habit of
195 // moving computations where they were not always computed. For example, in
196 // a cast like this:
197 // if (c) {
198 // if (d) ...
199 // else ... X+Y ...
200 // } else {
201 // ... X+Y ...
202 // }
203 //
204 // In thiscase, the expression would be hoisted to outside the 'if' stmt,
205 // causing the expression to be evaluated, even for the if (d) path, which
206 // could cause problems, if, for example, it caused a divide by zero. In
207 // general the problem this case is trying to solve is better addressed with
208 // PRE than GCSE.
209 //
Chris Lattner879cb972002-08-22 18:24:48 +0000210 return false;
Chris Lattnerf56bd892002-08-02 18:06:01 +0000211
212#if 0
Chris Lattner1467f642002-04-28 00:47:11 +0000213 // Handle the most general case now. In this case, neither I dom Other nor
214 // Other dom I. Because we are in SSA form, we are guaranteed that the
215 // operands of the two instructions both dominate the uses, so we _know_
216 // that there must exist a block that dominates both instructions (if the
217 // operands of the instructions are globals or constants, worst case we
218 // would get the entry node of the function). Search for this block now.
219 //
220
221 // Search up the immediate dominator chain of BB1 for the shared dominator
222 BasicBlock *SharedDom = (*ImmDominator)[BB1];
223 while (!DomSetInfo->dominates(SharedDom, BB2))
224 SharedDom = (*ImmDominator)[SharedDom];
225
226 // At this point, shared dom must dominate BOTH BB1 and BB2...
227 assert(SharedDom && DomSetInfo->dominates(SharedDom, BB1) &&
228 DomSetInfo->dominates(SharedDom, BB2) && "Dominators broken!");
229
230 // Rip 'I' out of BB1, and move it to the end of SharedDom.
231 BB1->getInstList().remove(I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000232 SharedDom->getInstList().insert(--SharedDom->end(), I);
Chris Lattner1467f642002-04-28 00:47:11 +0000233
234 // Eliminate 'Other' now.
Chris Lattner113f4f42002-06-25 16:13:24 +0000235 ReplaceInstWithInst(I, Other);
Chris Lattnerf56bd892002-08-02 18:06:01 +0000236#endif
Chris Lattner1467f642002-04-28 00:47:11 +0000237 }
Chris Lattner879cb972002-08-22 18:24:48 +0000238 return true;
Chris Lattner1467f642002-04-28 00:47:11 +0000239}
240
241//===----------------------------------------------------------------------===//
242//
243// Visitation methods, these are invoked depending on the type of instruction
244// being checked. They should return true if a common subexpression was folded.
245//
246//===----------------------------------------------------------------------===//
247
Chris Lattnerb80b69c2002-08-14 18:22:19 +0000248bool GCSE::visitCastInst(CastInst &CI) {
249 Instruction &I = (Instruction&)CI;
Chris Lattner113f4f42002-06-25 16:13:24 +0000250 Value *Op = I.getOperand(0);
251 Function *F = I.getParent()->getParent();
Chris Lattner1467f642002-04-28 00:47:11 +0000252
253 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
254 UI != UE; ++UI)
255 if (Instruction *Other = dyn_cast<Instruction>(*UI))
Chris Lattnerb193ff82002-08-14 18:18:02 +0000256 // Check to see if this new cast is not I, but has the same operand...
Chris Lattner113f4f42002-06-25 16:13:24 +0000257 if (Other != &I && Other->getOpcode() == I.getOpcode() &&
Chris Lattner1467f642002-04-28 00:47:11 +0000258 Other->getOperand(0) == Op && // Is the operand the same?
259 // Is it embeded in the same function? (This could be false if LHS
260 // is a constant or global!)
261 Other->getParent()->getParent() == F &&
262
263 // Check that the types are the same, since this code handles casts...
Chris Lattner113f4f42002-06-25 16:13:24 +0000264 Other->getType() == I.getType()) {
Chris Lattner1467f642002-04-28 00:47:11 +0000265
266 // These instructions are identical. Handle the situation.
Chris Lattner879cb972002-08-22 18:24:48 +0000267 if (CommonSubExpressionFound(&I, Other))
268 return true; // One instruction eliminated!
Chris Lattner1467f642002-04-28 00:47:11 +0000269 }
270
271 return false;
272}
273
Chris Lattnercd9837d2002-05-14 19:57:25 +0000274// isIdenticalBinaryInst - Return true if the two binary instructions are
275// identical.
276//
Chris Lattner113f4f42002-06-25 16:13:24 +0000277static inline bool isIdenticalBinaryInst(const Instruction &I1,
Chris Lattnercd9837d2002-05-14 19:57:25 +0000278 const Instruction *I2) {
279 // Is it embeded in the same function? (This could be false if LHS
280 // is a constant or global!)
Chris Lattner113f4f42002-06-25 16:13:24 +0000281 if (I1.getOpcode() != I2->getOpcode() ||
282 I1.getParent()->getParent() != I2->getParent()->getParent())
Chris Lattnercd9837d2002-05-14 19:57:25 +0000283 return false;
284
285 // They are identical if both operands are the same!
Chris Lattner113f4f42002-06-25 16:13:24 +0000286 if (I1.getOperand(0) == I2->getOperand(0) &&
287 I1.getOperand(1) == I2->getOperand(1))
Chris Lattnercd9837d2002-05-14 19:57:25 +0000288 return true;
289
290 // If the instruction is commutative and associative, the instruction can
291 // match if the operands are swapped!
292 //
Chris Lattner113f4f42002-06-25 16:13:24 +0000293 if ((I1.getOperand(0) == I2->getOperand(1) &&
294 I1.getOperand(1) == I2->getOperand(0)) &&
295 (I1.getOpcode() == Instruction::Add ||
296 I1.getOpcode() == Instruction::Mul ||
297 I1.getOpcode() == Instruction::And ||
298 I1.getOpcode() == Instruction::Or ||
299 I1.getOpcode() == Instruction::Xor))
Chris Lattnercd9837d2002-05-14 19:57:25 +0000300 return true;
301
302 return false;
303}
304
Chris Lattner113f4f42002-06-25 16:13:24 +0000305bool GCSE::visitBinaryOperator(Instruction &I) {
306 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
307 Function *F = I.getParent()->getParent();
Chris Lattner1467f642002-04-28 00:47:11 +0000308
309 for (Value::use_iterator UI = LHS->use_begin(), UE = LHS->use_end();
310 UI != UE; ++UI)
311 if (Instruction *Other = dyn_cast<Instruction>(*UI))
312 // Check to see if this new binary operator is not I, but same operand...
Chris Lattner113f4f42002-06-25 16:13:24 +0000313 if (Other != &I && isIdenticalBinaryInst(I, Other)) {
Chris Lattner1467f642002-04-28 00:47:11 +0000314 // These instructions are identical. Handle the situation.
Chris Lattner879cb972002-08-22 18:24:48 +0000315 if (CommonSubExpressionFound(&I, Other))
316 return true; // One instruction eliminated!
Chris Lattner1467f642002-04-28 00:47:11 +0000317 }
318
319 return false;
320}
321
Chris Lattnerd38ddb12002-05-14 05:02:40 +0000322// IdenticalComplexInst - Return true if the two instructions are the same, by
323// using a brute force comparison.
324//
325static bool IdenticalComplexInst(const Instruction *I1, const Instruction *I2) {
326 assert(I1->getOpcode() == I2->getOpcode());
327 // Equal if they are in the same function...
328 return I1->getParent()->getParent() == I2->getParent()->getParent() &&
329 // And return the same type...
330 I1->getType() == I2->getType() &&
331 // And have the same number of operands...
332 I1->getNumOperands() == I2->getNumOperands() &&
333 // And all of the operands are equal.
334 std::equal(I1->op_begin(), I1->op_end(), I2->op_begin());
335}
336
Chris Lattner113f4f42002-06-25 16:13:24 +0000337bool GCSE::visitGetElementPtrInst(GetElementPtrInst &I) {
338 Value *Op = I.getOperand(0);
339 Function *F = I.getParent()->getParent();
Chris Lattner1467f642002-04-28 00:47:11 +0000340
341 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
342 UI != UE; ++UI)
343 if (GetElementPtrInst *Other = dyn_cast<GetElementPtrInst>(*UI))
Chris Lattnerd38ddb12002-05-14 05:02:40 +0000344 // Check to see if this new getelementptr is not I, but same operand...
Chris Lattner113f4f42002-06-25 16:13:24 +0000345 if (Other != &I && IdenticalComplexInst(&I, Other)) {
Chris Lattnerd38ddb12002-05-14 05:02:40 +0000346 // These instructions are identical. Handle the situation.
Chris Lattner879cb972002-08-22 18:24:48 +0000347 if (CommonSubExpressionFound(&I, Other))
348 return true; // One instruction eliminated!
Chris Lattner1467f642002-04-28 00:47:11 +0000349 }
350
351 return false;
352}
Chris Lattnerd38ddb12002-05-14 05:02:40 +0000353
Chris Lattner113f4f42002-06-25 16:13:24 +0000354bool GCSE::visitLoadInst(LoadInst &LI) {
355 Value *Op = LI.getOperand(0);
356 Function *F = LI.getParent()->getParent();
Chris Lattnerd38ddb12002-05-14 05:02:40 +0000357
358 for (Value::use_iterator UI = Op->use_begin(), UE = Op->use_end();
359 UI != UE; ++UI)
360 if (LoadInst *Other = dyn_cast<LoadInst>(*UI))
361 // Check to see if this new load is not LI, but has the same operands...
Chris Lattner113f4f42002-06-25 16:13:24 +0000362 if (Other != &LI && IdenticalComplexInst(&LI, Other) &&
363 TryToRemoveALoad(&LI, Other))
Chris Lattnerd38ddb12002-05-14 05:02:40 +0000364 return true; // An instruction was eliminated!
365
366 return false;
367}
368
Chris Lattnerd38ddb12002-05-14 05:02:40 +0000369// TryToRemoveALoad - Try to remove one of L1 or L2. The problem with removing
370// loads is that intervening stores might make otherwise identical load's yield
371// different values. To ensure that this is not the case, we check that there
372// are no intervening stores or calls between the instructions.
373//
374bool GCSE::TryToRemoveALoad(LoadInst *L1, LoadInst *L2) {
375 // Figure out which load dominates the other one. If neither dominates the
376 // other we cannot eliminate one...
377 //
378 if (DomSetInfo->dominates(L2, L1))
379 std::swap(L1, L2); // Make L1 dominate L2
380 else if (!DomSetInfo->dominates(L1, L2))
381 return false; // Neither instruction dominates the other one...
382
383 BasicBlock *BB1 = L1->getParent(), *BB2 = L2->getParent();
Chris Lattner879cb972002-08-22 18:24:48 +0000384 Value *LoadAddress = L1->getOperand(0);
Chris Lattnerd38ddb12002-05-14 05:02:40 +0000385
386 // L1 now dominates L2. Check to see if the intervening instructions between
387 // the two loads include a store or call...
388 //
389 if (BB1 == BB2) { // In same basic block?
390 // In this degenerate case, no checking of global basic blocks has to occur
391 // just check the instructions BETWEEN L1 & L2...
392 //
Chris Lattner879cb972002-08-22 18:24:48 +0000393 if (AA->canInstructionRangeModify(*L1, *L2, LoadAddress))
394 return false; // Cannot eliminate load
Chris Lattnerd38ddb12002-05-14 05:02:40 +0000395
396 ++NumLoadRemoved;
Chris Lattner879cb972002-08-22 18:24:48 +0000397 if (CommonSubExpressionFound(L1, L2))
398 return true;
Chris Lattnerd38ddb12002-05-14 05:02:40 +0000399 } else {
400 // Make sure that there are no store instructions between L1 and the end of
401 // it's basic block...
402 //
Chris Lattner879cb972002-08-22 18:24:48 +0000403 if (AA->canInstructionRangeModify(*L1, *BB1->getTerminator(), LoadAddress))
404 return false; // Cannot eliminate load
Chris Lattnerd38ddb12002-05-14 05:02:40 +0000405
406 // Make sure that there are no store instructions between the start of BB2
407 // and the second load instruction...
408 //
Chris Lattner879cb972002-08-22 18:24:48 +0000409 if (AA->canInstructionRangeModify(BB2->front(), *L2, LoadAddress))
410 return false; // Cannot eliminate load
Chris Lattnerd38ddb12002-05-14 05:02:40 +0000411
412 // Do a depth first traversal of the inverse CFG starting at L2's block,
413 // looking for L1's block. The inverse CFG is made up of the predecessor
414 // nodes of a block... so all of the edges in the graph are "backward".
415 //
416 set<BasicBlock*> VisitedSet;
417 for (pred_iterator PI = pred_begin(BB2), PE = pred_end(BB2); PI != PE; ++PI)
Chris Lattner879cb972002-08-22 18:24:48 +0000418 if (CheckForInvalidatingInst(*PI, BB1, LoadAddress, VisitedSet))
Chris Lattnerd38ddb12002-05-14 05:02:40 +0000419 return false;
420
421 ++NumLoadRemoved;
Chris Lattner879cb972002-08-22 18:24:48 +0000422 return CommonSubExpressionFound(L1, L2);
Chris Lattnerd38ddb12002-05-14 05:02:40 +0000423 }
424 return false;
425}
426
427// CheckForInvalidatingInst - Return true if BB or any of the predecessors of BB
Chris Lattner879cb972002-08-22 18:24:48 +0000428// (until DestBB) contain an instruction that might invalidate Ptr.
Chris Lattnerd38ddb12002-05-14 05:02:40 +0000429//
430bool GCSE::CheckForInvalidatingInst(BasicBlock *BB, BasicBlock *DestBB,
Chris Lattner879cb972002-08-22 18:24:48 +0000431 Value *Ptr, set<BasicBlock*> &VisitedSet) {
Chris Lattnerd38ddb12002-05-14 05:02:40 +0000432 // Found the termination point!
433 if (BB == DestBB || VisitedSet.count(BB)) return false;
434
435 // Avoid infinite recursion!
436 VisitedSet.insert(BB);
437
Chris Lattner879cb972002-08-22 18:24:48 +0000438 // Can this basic block modify Ptr?
439 if (AA->canBasicBlockModify(*BB, Ptr))
440 return true;
Chris Lattnerd38ddb12002-05-14 05:02:40 +0000441
442 // Check all of our predecessor blocks...
443 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI)
Chris Lattner879cb972002-08-22 18:24:48 +0000444 if (CheckForInvalidatingInst(*PI, DestBB, Ptr, VisitedSet))
Chris Lattnerd38ddb12002-05-14 05:02:40 +0000445 return true;
446
447 // None of our predecessor blocks contain a store, and we don't either!
448 return false;
449}