Load & StoreInst no longer derive from MemAccessInst, so we don't have
[oota-llvm.git] / lib / Transforms / Scalar / GCSE.cpp
1 //===-- 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 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Transforms/Scalar.h"
11 #include "llvm/InstrTypes.h"
12 #include "llvm/iMemory.h"
13 #include "llvm/Analysis/Dominators.h"
14 #include "llvm/Analysis/AliasAnalysis.h"
15 #include "llvm/Support/InstVisitor.h"
16 #include "llvm/Support/InstIterator.h"
17 #include "llvm/Support/CFG.h"
18 #include "Support/StatisticReporter.h"
19 #include <algorithm>
20 using std::set;
21 using std::map;
22
23
24 static Statistic<> NumInstRemoved("gcse\t\t- Number of instructions removed");
25 static Statistic<> NumLoadRemoved("gcse\t\t- Number of loads removed");
26
27 namespace {
28   class GCSE : public FunctionPass, public InstVisitor<GCSE, bool> {
29     set<Instruction*>       WorkList;
30     DominatorSet           *DomSetInfo;
31     ImmediateDominators    *ImmDominator;
32     AliasAnalysis          *AA;
33
34   public:
35     virtual bool runOnFunction(Function &F);
36
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     //
41     bool visitBinaryOperator(Instruction &I);
42     bool visitGetElementPtrInst(GetElementPtrInst &I);
43     bool visitCastInst(CastInst &I);
44     bool visitShiftInst(ShiftInst &I) {
45       return visitBinaryOperator((Instruction&)I);
46     }
47     bool visitLoadInst(LoadInst &LI);
48     bool visitInstruction(Instruction &) { return false; }
49
50   private:
51     void ReplaceInstWithInst(Instruction *First, BasicBlock::iterator SI);
52     bool CommonSubExpressionFound(Instruction *I, Instruction *Other);
53
54     // 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
63     // of BB (until DestBB) contain an instruction that might invalidate Ptr.
64     //
65     bool CheckForInvalidatingInst(BasicBlock *BB, BasicBlock *DestBB,
66                                   Value *Ptr, set<BasicBlock*> &VisitedSet);
67
68     // This transformation requires dominator and immediate dominator info
69     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
70       AU.preservesCFG();
71       AU.addRequired<DominatorSet>();
72       AU.addRequired<ImmediateDominators>();
73       AU.addRequired<AliasAnalysis>();
74     }
75   };
76
77   RegisterOpt<GCSE> X("gcse", "Global Common Subexpression Elimination");
78 }
79
80 // createGCSEPass - The public interface to this file...
81 Pass *createGCSEPass() { return new GCSE(); }
82
83
84 // GCSE::runOnFunction - This is the main transformation entry point for a
85 // function.
86 //
87 bool GCSE::runOnFunction(Function &F) {
88   bool Changed = false;
89
90   // Get pointers to the analysis results that we will be using...
91   DomSetInfo = &getAnalysis<DominatorSet>();
92   ImmDominator = &getAnalysis<ImmediateDominators>();
93   AA = &getAnalysis<AliasAnalysis>();
94
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()) {
106     Instruction &I = **WorkList.begin(); // Get an instruction from the worklist
107     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   }
114
115   // 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 //
123 void GCSE::ReplaceInstWithInst(Instruction *First, BasicBlock::iterator SI) {
124   Instruction &Second = *SI;
125   
126   //cerr << "DEL " << (void*)Second << Second;
127
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
132   for (Value::use_iterator UI = Second.use_begin(), UE = Second.use_end();
133        UI != UE; ++UI)
134     WorkList.insert(cast<Instruction>(*UI));
135     
136   // Make all users of 'Second' now use 'First'
137   Second.replaceAllUsesWith(First);
138
139   // Erase the second instruction from the program
140   Second.getParent()->getInstList().erase(SI);
141 }
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 //
147 bool GCSE::CommonSubExpressionFound(Instruction *I, Instruction *Other) {
148   assert(I != Other);
149
150   WorkList.erase(I);
151   WorkList.erase(Other); // Other may not actually be on the worklist anymore...
152
153   ++NumInstRemoved;   // Keep track of number of instructions eliminated
154
155   // 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();
163     while (&*BI != I && &*BI != Other) {
164       ++BI;
165       assert(BI != BB1->end() && "Instructions not found in parent BB!");
166     }
167
168     // Keep track of which instructions occurred first & second
169     Instruction *First = BI;
170     Instruction *Second = I != First ? I : Other; // Get iterator to second inst
171     BI = Second;
172
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?
180     ReplaceInstWithInst(I, Other);
181   } else if (DomSetInfo->dominates(BB2, BB1)) {    // Other dom I?
182     ReplaceInstWithInst(Other, I);
183   } else {
184     // 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     //
210     return false;
211
212 #if 0
213     // 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);
232     SharedDom->getInstList().insert(--SharedDom->end(), I);
233
234     // Eliminate 'Other' now.
235     ReplaceInstWithInst(I, Other);
236 #endif
237   }
238   return true;
239 }
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
248 bool GCSE::visitCastInst(CastInst &CI) {
249   Instruction &I = (Instruction&)CI;
250   Value *Op = I.getOperand(0);
251   Function *F = I.getParent()->getParent();
252   
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))
256       // Check to see if this new cast is not I, but has the same operand...
257       if (Other != &I && Other->getOpcode() == I.getOpcode() &&
258           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...
264           Other->getType() == I.getType()) {
265         
266         // These instructions are identical.  Handle the situation.
267         if (CommonSubExpressionFound(&I, Other))
268           return true;   // One instruction eliminated!
269       }
270   
271   return false;
272 }
273
274 // isIdenticalBinaryInst - Return true if the two binary instructions are
275 // identical.
276 //
277 static inline bool isIdenticalBinaryInst(const Instruction &I1,
278                                          const Instruction *I2) {
279   // Is it embeded in the same function?  (This could be false if LHS
280   // is a constant or global!)
281   if (I1.getOpcode() != I2->getOpcode() ||
282       I1.getParent()->getParent() != I2->getParent()->getParent())
283     return false;
284   
285   // They are identical if both operands are the same!
286   if (I1.getOperand(0) == I2->getOperand(0) &&
287       I1.getOperand(1) == I2->getOperand(1))
288     return true;
289   
290   // If the instruction is commutative and associative, the instruction can
291   // match if the operands are swapped!
292   //
293   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))
300     return true;
301
302   return false;
303 }
304
305 bool GCSE::visitBinaryOperator(Instruction &I) {
306   Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
307   Function *F = I.getParent()->getParent();
308   
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...
313       if (Other != &I && isIdenticalBinaryInst(I, Other)) {        
314         // These instructions are identical.  Handle the situation.
315         if (CommonSubExpressionFound(&I, Other))
316           return true;   // One instruction eliminated!
317       }
318   
319   return false;
320 }
321
322 // IdenticalComplexInst - Return true if the two instructions are the same, by
323 // using a brute force comparison.
324 //
325 static 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
337 bool GCSE::visitGetElementPtrInst(GetElementPtrInst &I) {
338   Value *Op = I.getOperand(0);
339   Function *F = I.getParent()->getParent();
340   
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))
344       // Check to see if this new getelementptr is not I, but same operand...
345       if (Other != &I && IdenticalComplexInst(&I, Other)) {
346         // These instructions are identical.  Handle the situation.
347         if (CommonSubExpressionFound(&I, Other))
348           return true;   // One instruction eliminated!
349       }
350   
351   return false;
352 }
353
354 bool GCSE::visitLoadInst(LoadInst &LI) {
355   Value *Op = LI.getOperand(0);
356   Function *F = LI.getParent()->getParent();
357   
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...
362       if (Other != &LI && IdenticalComplexInst(&LI, Other) &&
363           TryToRemoveALoad(&LI, Other))
364         return true;   // An instruction was eliminated!
365   
366   return false;
367 }
368
369 // 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 //
374 bool 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();
384   Value *LoadAddress = L1->getOperand(0);
385
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     //
393     if (AA->canInstructionRangeModify(*L1, *L2, LoadAddress))
394       return false;   // Cannot eliminate load
395
396     ++NumLoadRemoved;
397     if (CommonSubExpressionFound(L1, L2))
398       return true;
399   } else {
400     // Make sure that there are no store instructions between L1 and the end of
401     // it's basic block...
402     //
403     if (AA->canInstructionRangeModify(*L1, *BB1->getTerminator(), LoadAddress))
404       return false;   // Cannot eliminate load
405
406     // Make sure that there are no store instructions between the start of BB2
407     // and the second load instruction...
408     //
409     if (AA->canInstructionRangeModify(BB2->front(), *L2, LoadAddress))
410       return false;   // Cannot eliminate load
411
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)
418       if (CheckForInvalidatingInst(*PI, BB1, LoadAddress, VisitedSet))
419         return false;
420     
421     ++NumLoadRemoved;
422     return CommonSubExpressionFound(L1, L2);
423   }
424   return false;
425 }
426
427 // CheckForInvalidatingInst - Return true if BB or any of the predecessors of BB
428 // (until DestBB) contain an instruction that might invalidate Ptr.
429 //
430 bool GCSE::CheckForInvalidatingInst(BasicBlock *BB, BasicBlock *DestBB,
431                                     Value *Ptr, set<BasicBlock*> &VisitedSet) {
432   // Found the termination point!
433   if (BB == DestBB || VisitedSet.count(BB)) return false;
434
435   // Avoid infinite recursion!
436   VisitedSet.insert(BB);
437
438   // Can this basic block modify Ptr?
439   if (AA->canBasicBlockModify(*BB, Ptr))
440     return true;
441
442   // Check all of our predecessor blocks...
443   for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI)
444     if (CheckForInvalidatingInst(*PI, DestBB, Ptr, VisitedSet))
445       return true;
446
447   // None of our predecessor blocks contain a store, and we don't either!
448   return false;
449 }