Changes For Bug 352
[oota-llvm.git] / lib / Analysis / LoadValueNumbering.cpp
1 //===- LoadValueNumbering.cpp - Load Value #'ing Implementation -*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements a value numbering pass that value numbers load and call
11 // instructions.  To do this, it finds lexically identical load instructions,
12 // and uses alias analysis to determine which loads are guaranteed to produce
13 // the same value.  To value number call instructions, it looks for calls to
14 // functions that do not write to memory which do not have intervening
15 // instructions that clobber the memory that is read from.
16 //
17 // This pass builds off of another value numbering pass to implement value
18 // numbering for non-load and non-call instructions.  It uses Alias Analysis so
19 // that it can disambiguate the load instructions.  The more powerful these base
20 // analyses are, the more powerful the resultant value numbering will be.
21 //
22 //===----------------------------------------------------------------------===//
23
24 #include "llvm/Analysis/LoadValueNumbering.h"
25 #include "llvm/Constant.h"
26 #include "llvm/Function.h"
27 #include "llvm/Instructions.h"
28 #include "llvm/Pass.h"
29 #include "llvm/Type.h"
30 #include "llvm/Analysis/ValueNumbering.h"
31 #include "llvm/Analysis/AliasAnalysis.h"
32 #include "llvm/Analysis/Dominators.h"
33 #include "llvm/Support/CFG.h"
34 #include "llvm/Target/TargetData.h"
35 #include <set>
36 using namespace llvm;
37
38 namespace {
39   // FIXME: This should not be a FunctionPass.
40   struct LoadVN : public FunctionPass, public ValueNumbering {
41     
42     /// Pass Implementation stuff.  This doesn't do any analysis.
43     ///
44     bool runOnFunction(Function &) { return false; }
45     
46     /// getAnalysisUsage - Does not modify anything.  It uses Value Numbering
47     /// and Alias Analysis.
48     ///
49     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
50     
51     /// getEqualNumberNodes - Return nodes with the same value number as the
52     /// specified Value.  This fills in the argument vector with any equal
53     /// values.
54     ///
55     virtual void getEqualNumberNodes(Value *V1,
56                                      std::vector<Value*> &RetVals) const;
57
58     /// deleteValue - This method should be called whenever an LLVM Value is
59     /// deleted from the program, for example when an instruction is found to be
60     /// redundant and is eliminated.
61     ///
62     virtual void deleteValue(Value *V) {
63       getAnalysis<AliasAnalysis>().deleteValue(V);
64     }
65     
66     /// copyValue - This method should be used whenever a preexisting value in
67     /// the program is copied or cloned, introducing a new value.  Note that
68     /// analysis implementations should tolerate clients that use this method to
69     /// introduce the same value multiple times: if the analysis already knows
70     /// about a value, it should ignore the request.
71     ///
72     virtual void copyValue(Value *From, Value *To) {
73       getAnalysis<AliasAnalysis>().copyValue(From, To);
74     }
75
76     /// getCallEqualNumberNodes - Given a call instruction, find other calls
77     /// that have the same value number.
78     void getCallEqualNumberNodes(CallInst *CI,
79                                  std::vector<Value*> &RetVals) const;
80   };
81
82   // Register this pass...
83   RegisterOpt<LoadVN> X("load-vn", "Load Value Numbering");
84
85   // Declare that we implement the ValueNumbering interface
86   RegisterAnalysisGroup<ValueNumbering, LoadVN> Y;
87 }
88
89 FunctionPass *llvm::createLoadValueNumberingPass() { return new LoadVN(); }
90
91
92 /// getAnalysisUsage - Does not modify anything.  It uses Value Numbering and
93 /// Alias Analysis.
94 ///
95 void LoadVN::getAnalysisUsage(AnalysisUsage &AU) const {
96   AU.setPreservesAll();
97   AU.addRequired<AliasAnalysis>();
98   AU.addRequired<ValueNumbering>();
99   AU.addRequired<DominatorSet>();
100   AU.addRequired<TargetData>();
101 }
102
103 static bool isPathTransparentTo(BasicBlock *CurBlock, BasicBlock *Dom,
104                                 Value *Ptr, unsigned Size, AliasAnalysis &AA,
105                                 std::set<BasicBlock*> &Visited,
106                                 std::map<BasicBlock*, bool> &TransparentBlocks){
107   // If we have already checked out this path, or if we reached our destination,
108   // stop searching, returning success.
109   if (CurBlock == Dom || !Visited.insert(CurBlock).second)
110     return true;
111   
112   // Check whether this block is known transparent or not.
113   std::map<BasicBlock*, bool>::iterator TBI =
114     TransparentBlocks.lower_bound(CurBlock);
115
116   if (TBI == TransparentBlocks.end() || TBI->first != CurBlock) {
117     // If this basic block can modify the memory location, then the path is not
118     // transparent!
119     if (AA.canBasicBlockModify(*CurBlock, Ptr, Size)) {
120       TransparentBlocks.insert(TBI, std::make_pair(CurBlock, false));
121       return false;
122     }
123     TransparentBlocks.insert(TBI, std::make_pair(CurBlock, true));
124   } else if (!TBI->second)
125     // This block is known non-transparent, so that path can't be either.
126     return false;
127   
128   // The current block is known to be transparent.  The entire path is
129   // transparent if all of the predecessors paths to the parent is also
130   // transparent to the memory location.
131   for (pred_iterator PI = pred_begin(CurBlock), E = pred_end(CurBlock);
132        PI != E; ++PI)
133     if (!isPathTransparentTo(*PI, Dom, Ptr, Size, AA, Visited,
134                              TransparentBlocks))
135       return false;
136   return true;
137 }
138
139 /// getCallEqualNumberNodes - Given a call instruction, find other calls that
140 /// have the same value number.
141 void LoadVN::getCallEqualNumberNodes(CallInst *CI,
142                                      std::vector<Value*> &RetVals) const {
143   Function *CF = CI->getCalledFunction();
144   if (CF == 0) return;  // Indirect call.
145   AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
146   if (!AA.onlyReadsMemory(CF)) return;  // Nothing we can do.
147
148   // Scan all of the arguments of the function, looking for one that is not
149   // global.  In particular, we would prefer to have an argument or instruction
150   // operand to chase the def-use chains of.
151   Value *Op = CF;
152   for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i)
153     if (isa<Argument>(CI->getOperand(i)) ||
154         isa<Instruction>(CI->getOperand(i))) {
155       Op = CI->getOperand(i);
156       break;
157     }
158
159   // Identify all lexically identical calls in this function.
160   std::vector<CallInst*> IdenticalCalls;
161
162   Function *CIFunc = CI->getParent()->getParent();
163   for (Value::use_iterator UI = Op->use_begin(), E = Op->use_end(); UI != E;
164        ++UI)
165     if (CallInst *C = dyn_cast<CallInst>(*UI))
166       if (C->getNumOperands() == CI->getNumOperands() &&
167           C->getOperand(0) == CI->getOperand(0) &&
168           C->getParent()->getParent() == CIFunc && C != CI) {
169         bool AllOperandsEqual = true;
170         for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i)
171           if (C->getOperand(i) != CI->getOperand(i)) {
172             AllOperandsEqual = false;
173             break;
174           }
175
176         if (AllOperandsEqual)
177           IdenticalCalls.push_back(C);
178       }
179   
180   if (IdenticalCalls.empty()) return;
181
182   // Eliminate duplicates, which could occur if we chose a value that is passed
183   // into a call site multiple times.
184   std::sort(IdenticalCalls.begin(), IdenticalCalls.end());
185   IdenticalCalls.erase(std::unique(IdenticalCalls.begin(),IdenticalCalls.end()),
186                        IdenticalCalls.end());
187
188   // If the call reads memory, we must make sure that there are no stores
189   // between the calls in question.
190   //
191   // FIXME: This should use mod/ref information.  What we really care about it
192   // whether an intervening instruction could modify memory that is read, not
193   // ANY memory.
194   //
195   if (!AA.doesNotAccessMemory(CF)) {
196     DominatorSet &DomSetInfo = getAnalysis<DominatorSet>();
197     BasicBlock *CIBB = CI->getParent();
198     for (unsigned i = 0; i != IdenticalCalls.size(); ++i) {
199       CallInst *C = IdenticalCalls[i];
200       bool CantEqual = false;
201
202       if (DomSetInfo.dominates(CIBB, C->getParent())) {
203         // FIXME: we currently only handle the case where both calls are in the
204         // same basic block.
205         if (CIBB != C->getParent()) {
206           CantEqual = true;
207         } else {
208           Instruction *First = CI, *Second = C;
209           if (!DomSetInfo.dominates(CI, C))
210             std::swap(First, Second);
211           
212           // Scan the instructions between the calls, checking for stores or
213           // calls to dangerous functions.
214           BasicBlock::iterator I = First;
215           for (++First; I != BasicBlock::iterator(Second); ++I) {
216             if (isa<StoreInst>(I)) {
217               // FIXME: We could use mod/ref information to make this much
218               // better!
219               CantEqual = true;
220               break;
221             } else if (CallInst *CI = dyn_cast<CallInst>(I)) {
222               if (CI->getCalledFunction() == 0 ||
223                   !AA.onlyReadsMemory(CI->getCalledFunction())) {
224                 CantEqual = true;
225                 break;
226               }
227             } else if (I->mayWriteToMemory()) {
228               CantEqual = true;
229               break;
230             }
231           }
232         }
233
234       } else if (DomSetInfo.dominates(C->getParent(), CIBB)) {
235         // FIXME: We could implement this, but we don't for now.
236         CantEqual = true;
237       } else {
238         // FIXME: if one doesn't dominate the other, we can't tell yet.
239         CantEqual = true;
240       }
241
242
243       if (CantEqual) {
244         // This call does not produce the same value as the one in the query.
245         std::swap(IdenticalCalls[i--], IdenticalCalls.back());
246         IdenticalCalls.pop_back();
247       }
248     }
249   }
250
251   // Any calls that are identical and not destroyed will produce equal values!
252   for (unsigned i = 0, e = IdenticalCalls.size(); i != e; ++i)
253     RetVals.push_back(IdenticalCalls[i]);
254 }
255
256 // getEqualNumberNodes - Return nodes with the same value number as the
257 // specified Value.  This fills in the argument vector with any equal values.
258 //
259 void LoadVN::getEqualNumberNodes(Value *V,
260                                  std::vector<Value*> &RetVals) const {
261   // If the alias analysis has any must alias information to share with us, we
262   // can definitely use it.
263   if (isa<PointerType>(V->getType()))
264     getAnalysis<AliasAnalysis>().getMustAliases(V, RetVals);
265
266   if (!isa<LoadInst>(V)) {
267     if (CallInst *CI = dyn_cast<CallInst>(V))
268       getCallEqualNumberNodes(CI, RetVals);
269
270     // Not a load instruction?  Just chain to the base value numbering
271     // implementation to satisfy the request...
272     assert(&getAnalysis<ValueNumbering>() != (ValueNumbering*)this &&
273            "getAnalysis() returned this!");
274
275     return getAnalysis<ValueNumbering>().getEqualNumberNodes(V, RetVals);
276   }
277
278   // Volatile loads cannot be replaced with the value of other loads.
279   LoadInst *LI = cast<LoadInst>(V);
280   if (LI->isVolatile())
281     return getAnalysis<ValueNumbering>().getEqualNumberNodes(V, RetVals);
282   
283   // If we have a load instruction, find all of the load and store instructions
284   // that use the same source operand.  We implement this recursively, because
285   // there could be a load of a load of a load that are all identical.  We are
286   // guaranteed that this cannot be an infinite recursion because load
287   // instructions would have to pass through a PHI node in order for there to be
288   // a cycle.  The PHI node would be handled by the else case here, breaking the
289   // infinite recursion.
290   //
291   std::vector<Value*> PointerSources;
292   getEqualNumberNodes(LI->getOperand(0), PointerSources);
293   PointerSources.push_back(LI->getOperand(0));
294   
295   BasicBlock *LoadBB = LI->getParent();
296   Function *F = LoadBB->getParent();
297   
298   // Now that we know the set of equivalent source pointers for the load
299   // instruction, look to see if there are any load or store candidates that are
300   // identical.
301   //
302   std::map<BasicBlock*, std::vector<LoadInst*> >  CandidateLoads;
303   std::map<BasicBlock*, std::vector<StoreInst*> > CandidateStores;
304   std::set<AllocationInst*> Allocations;
305   
306   while (!PointerSources.empty()) {
307     Value *Source = PointerSources.back();
308     PointerSources.pop_back();                // Get a source pointer...
309
310     if (AllocationInst *AI = dyn_cast<AllocationInst>(Source))
311       Allocations.insert(AI);
312     
313     for (Value::use_iterator UI = Source->use_begin(), UE = Source->use_end();
314          UI != UE; ++UI)
315       if (LoadInst *Cand = dyn_cast<LoadInst>(*UI)) {// Is a load of source?
316         if (Cand->getParent()->getParent() == F &&   // In the same function?
317             Cand != LI && !Cand->isVolatile())       // Not LI itself?
318           CandidateLoads[Cand->getParent()].push_back(Cand);     // Got one...
319       } else if (StoreInst *Cand = dyn_cast<StoreInst>(*UI)) {
320         if (Cand->getParent()->getParent() == F && !Cand->isVolatile() &&
321             Cand->getOperand(1) == Source)  // It's a store THROUGH the ptr...
322           CandidateStores[Cand->getParent()].push_back(Cand);
323       }
324   }
325   
326   // Get alias analysis & dominators.
327   AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
328   DominatorSet &DomSetInfo = getAnalysis<DominatorSet>();
329   Value *LoadPtr = LI->getOperand(0);
330   // Find out how many bytes of memory are loaded by the load instruction...
331   unsigned LoadSize = getAnalysis<TargetData>().getTypeSize(LI->getType());
332
333   // Find all of the candidate loads and stores that are in the same block as
334   // the defining instruction.
335   std::set<Instruction*> Instrs;
336   Instrs.insert(CandidateLoads[LoadBB].begin(), CandidateLoads[LoadBB].end());
337   CandidateLoads.erase(LoadBB);
338   Instrs.insert(CandidateStores[LoadBB].begin(), CandidateStores[LoadBB].end());
339   CandidateStores.erase(LoadBB);
340
341   // Figure out if the load is invalidated from the entry of the block it is in
342   // until the actual instruction.  This scans the block backwards from LI.  If
343   // we see any candidate load or store instructions, then we know that the
344   // candidates have the same value # as LI.
345   bool LoadInvalidatedInBBBefore = false;
346   for (BasicBlock::iterator I = LI; I != LoadBB->begin(); ) {
347     --I;
348     // If this instruction is a candidate load before LI, we know there are no
349     // invalidating instructions between it and LI, so they have the same value
350     // number.
351     if (isa<LoadInst>(I) && Instrs.count(I)) {
352       RetVals.push_back(I);
353       Instrs.erase(I);
354     } else if (AllocationInst *AI = dyn_cast<AllocationInst>(I)) {
355       // If we run into an allocation of the value being loaded, then the
356       // contenxt are not initialized.  We can return any value, so we will
357       // return a zero.
358       if (Allocations.count(AI)) {
359         LoadInvalidatedInBBBefore = true;
360         RetVals.push_back(Constant::getNullValue(LI->getType()));
361         break;
362       }
363     }
364
365     if (AA.getModRefInfo(I, LoadPtr, LoadSize) & AliasAnalysis::Mod) {
366       // If the invalidating instruction is a store, and its in our candidate
367       // set, then we can do store-load forwarding: the load has the same value
368       // # as the stored value.
369       if (isa<StoreInst>(I) && Instrs.count(I)) {
370         Instrs.erase(I);
371         RetVals.push_back(I->getOperand(0));
372       }
373
374       LoadInvalidatedInBBBefore = true;
375       break;
376     }
377   }
378
379   // Figure out if the load is invalidated between the load and the exit of the
380   // block it is defined in.  While we are scanning the current basic block, if
381   // we see any candidate loads, then we know they have the same value # as LI.
382   //
383   bool LoadInvalidatedInBBAfter = false;
384   for (BasicBlock::iterator I = LI->getNext(); I != LoadBB->end(); ++I) {
385     // If this instruction is a load, then this instruction returns the same
386     // value as LI.
387     if (isa<LoadInst>(I) && Instrs.count(I)) {
388       RetVals.push_back(I);
389       Instrs.erase(I);
390     }
391
392     if (AA.getModRefInfo(I, LoadPtr, LoadSize) & AliasAnalysis::Mod) {
393       LoadInvalidatedInBBAfter = true;
394       break;
395     }
396   }
397
398   // If there is anything left in the Instrs set, it could not possibly equal
399   // LI.
400   Instrs.clear();
401
402   // TransparentBlocks - For each basic block the load/store is alive across,
403   // figure out if the pointer is invalidated or not.  If it is invalidated, the
404   // boolean is set to false, if it's not it is set to true.  If we don't know
405   // yet, the entry is not in the map.
406   std::map<BasicBlock*, bool> TransparentBlocks;
407
408   // Loop over all of the basic blocks that also load the value.  If the value
409   // is live across the CFG from the source to destination blocks, and if the
410   // value is not invalidated in either the source or destination blocks, add it
411   // to the equivalence sets.
412   for (std::map<BasicBlock*, std::vector<LoadInst*> >::iterator
413          I = CandidateLoads.begin(), E = CandidateLoads.end(); I != E; ++I) {
414     bool CantEqual = false;
415
416     // Right now we only can handle cases where one load dominates the other.
417     // FIXME: generalize this!
418     BasicBlock *BB1 = I->first, *BB2 = LoadBB;
419     if (DomSetInfo.dominates(BB1, BB2)) {
420       // The other load dominates LI.  If the loaded value is killed entering
421       // the LoadBB block, we know the load is not live.
422       if (LoadInvalidatedInBBBefore)
423         CantEqual = true;
424     } else if (DomSetInfo.dominates(BB2, BB1)) {
425       std::swap(BB1, BB2);          // Canonicalize
426       // LI dominates the other load.  If the loaded value is killed exiting
427       // the LoadBB block, we know the load is not live.
428       if (LoadInvalidatedInBBAfter)
429         CantEqual = true;
430     } else {
431       // None of these loads can VN the same.
432       CantEqual = true;
433     }
434
435     if (!CantEqual) {
436       // Ok, at this point, we know that BB1 dominates BB2, and that there is
437       // nothing in the LI block that kills the loaded value.  Check to see if
438       // the value is live across the CFG.
439       std::set<BasicBlock*> Visited;
440       for (pred_iterator PI = pred_begin(BB2), E = pred_end(BB2); PI!=E; ++PI)
441         if (!isPathTransparentTo(*PI, BB1, LoadPtr, LoadSize, AA,
442                                  Visited, TransparentBlocks)) {
443           // None of these loads can VN the same.
444           CantEqual = true;
445           break;
446         }
447     }
448
449     // If the loads can equal so far, scan the basic block that contains the
450     // loads under consideration to see if they are invalidated in the block.
451     // For any loads that are not invalidated, add them to the equivalence
452     // set!
453     if (!CantEqual) {
454       Instrs.insert(I->second.begin(), I->second.end());
455       if (BB1 == LoadBB) {
456         // If LI dominates the block in question, check to see if any of the
457         // loads in this block are invalidated before they are reached.
458         for (BasicBlock::iterator BBI = I->first->begin(); ; ++BBI) {
459           if (isa<LoadInst>(BBI) && Instrs.count(BBI)) {
460             // The load is in the set!
461             RetVals.push_back(BBI);
462             Instrs.erase(BBI);
463             if (Instrs.empty()) break;
464           } else if (AA.getModRefInfo(BBI, LoadPtr, LoadSize)
465                              & AliasAnalysis::Mod) {
466             // If there is a modifying instruction, nothing below it will value
467             // # the same.
468             break;
469           }
470         }
471       } else {
472         // If the block dominates LI, make sure that the loads in the block are
473         // not invalidated before the block ends.
474         BasicBlock::iterator BBI = I->first->end();
475         while (1) {
476           --BBI;
477           if (isa<LoadInst>(BBI) && Instrs.count(BBI)) {
478             // The load is in the set!
479             RetVals.push_back(BBI);
480             Instrs.erase(BBI);
481             if (Instrs.empty()) break;
482           } else if (AA.getModRefInfo(BBI, LoadPtr, LoadSize)
483                              & AliasAnalysis::Mod) {
484             // If there is a modifying instruction, nothing above it will value
485             // # the same.
486             break;
487           }
488         }
489       }
490
491       Instrs.clear();
492     }
493   }
494
495   // Handle candidate stores.  If the loaded location is clobbered on entrance
496   // to the LoadBB, no store outside of the LoadBB can value number equal, so
497   // quick exit.
498   if (LoadInvalidatedInBBBefore)
499     return;
500
501   for (std::map<BasicBlock*, std::vector<StoreInst*> >::iterator
502          I = CandidateStores.begin(), E = CandidateStores.end(); I != E; ++I)
503     if (DomSetInfo.dominates(I->first, LoadBB)) {
504       // Check to see if the path from the store to the load is transparent
505       // w.r.t. the memory location.
506       bool CantEqual = false;
507       std::set<BasicBlock*> Visited;
508       for (pred_iterator PI = pred_begin(LoadBB), E = pred_end(LoadBB);
509            PI != E; ++PI)
510         if (!isPathTransparentTo(*PI, I->first, LoadPtr, LoadSize, AA,
511                                  Visited, TransparentBlocks)) {
512           // None of these stores can VN the same.
513           CantEqual = true;
514           break;
515         }
516       Visited.clear();
517       if (!CantEqual) {
518         // Okay, the path from the store block to the load block is clear, and
519         // we know that there are no invalidating instructions from the start
520         // of the load block to the load itself.  Now we just scan the store
521         // block.
522
523         BasicBlock::iterator BBI = I->first->end();
524         while (1) {
525           assert(BBI != I->first->begin() &&
526                  "There is a store in this block of the pointer, but the store"
527                  " doesn't mod the address being stored to??  Must be a bug in"
528                  " the alias analysis implementation!");
529           --BBI;
530           if (AA.getModRefInfo(BBI, LoadPtr, LoadSize) & AliasAnalysis::Mod) {
531             // If the invalidating instruction is one of the candidates,
532             // then it provides the value the load loads.
533             if (StoreInst *SI = dyn_cast<StoreInst>(BBI))
534               if (std::find(I->second.begin(), I->second.end(), SI) !=
535                   I->second.end())
536                 RetVals.push_back(SI->getOperand(0));
537             break;
538           }
539         }
540       }
541     }
542 }