Give 'hasPath' a longer but clearer name 'isPotentiallyReachable'. Also expand
[oota-llvm.git] / lib / Analysis / AliasAnalysis.cpp
1 //===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the generic AliasAnalysis interface which is used as the
11 // common interface used by all clients and implementations of alias analysis.
12 //
13 // This file also implements the default version of the AliasAnalysis interface
14 // that is to be used when no other implementation is specified.  This does some
15 // simple tests that detect obvious cases: two different global pointers cannot
16 // alias, a global cannot alias a malloc, two different mallocs cannot alias,
17 // etc.
18 //
19 // This alias analysis implementation really isn't very good for anything, but
20 // it is very fast, and makes a nice clean default implementation.  Because it
21 // handles lots of little corner cases, other, more complex, alias analysis
22 // implementations may choose to rely on this pass to resolve these simple and
23 // easy cases.
24 //
25 //===----------------------------------------------------------------------===//
26
27 #include "llvm/Analysis/AliasAnalysis.h"
28 #include "llvm/Analysis/CaptureTracking.h"
29 #include "llvm/Analysis/Dominators.h"
30 #include "llvm/Analysis/ValueTracking.h"
31 #include "llvm/IR/BasicBlock.h"
32 #include "llvm/IR/DataLayout.h"
33 #include "llvm/IR/Function.h"
34 #include "llvm/IR/Instructions.h"
35 #include "llvm/IR/IntrinsicInst.h"
36 #include "llvm/IR/LLVMContext.h"
37 #include "llvm/IR/Type.h"
38 #include "llvm/Pass.h"
39 #include "llvm/Target/TargetLibraryInfo.h"
40 using namespace llvm;
41
42 // Register the AliasAnalysis interface, providing a nice name to refer to.
43 INITIALIZE_ANALYSIS_GROUP(AliasAnalysis, "Alias Analysis", NoAA)
44 char AliasAnalysis::ID = 0;
45
46 //===----------------------------------------------------------------------===//
47 // Default chaining methods
48 //===----------------------------------------------------------------------===//
49
50 AliasAnalysis::AliasResult
51 AliasAnalysis::alias(const Location &LocA, const Location &LocB) {
52   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
53   return AA->alias(LocA, LocB);
54 }
55
56 bool AliasAnalysis::pointsToConstantMemory(const Location &Loc,
57                                            bool OrLocal) {
58   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
59   return AA->pointsToConstantMemory(Loc, OrLocal);
60 }
61
62 void AliasAnalysis::deleteValue(Value *V) {
63   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
64   AA->deleteValue(V);
65 }
66
67 void AliasAnalysis::copyValue(Value *From, Value *To) {
68   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
69   AA->copyValue(From, To);
70 }
71
72 void AliasAnalysis::addEscapingUse(Use &U) {
73   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
74   AA->addEscapingUse(U);
75 }
76
77
78 AliasAnalysis::ModRefResult
79 AliasAnalysis::getModRefInfo(ImmutableCallSite CS,
80                              const Location &Loc) {
81   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
82
83   ModRefBehavior MRB = getModRefBehavior(CS);
84   if (MRB == DoesNotAccessMemory)
85     return NoModRef;
86
87   ModRefResult Mask = ModRef;
88   if (onlyReadsMemory(MRB))
89     Mask = Ref;
90
91   if (onlyAccessesArgPointees(MRB)) {
92     bool doesAlias = false;
93     if (doesAccessArgPointees(MRB)) {
94       MDNode *CSTag = CS.getInstruction()->getMetadata(LLVMContext::MD_tbaa);
95       for (ImmutableCallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
96            AI != AE; ++AI) {
97         const Value *Arg = *AI;
98         if (!Arg->getType()->isPointerTy())
99           continue;
100         Location CSLoc(Arg, UnknownSize, CSTag);
101         if (!isNoAlias(CSLoc, Loc)) {
102           doesAlias = true;
103           break;
104         }
105       }
106     }
107     if (!doesAlias)
108       return NoModRef;
109   }
110
111   // If Loc is a constant memory location, the call definitely could not
112   // modify the memory location.
113   if ((Mask & Mod) && pointsToConstantMemory(Loc))
114     Mask = ModRefResult(Mask & ~Mod);
115
116   // If this is the end of the chain, don't forward.
117   if (!AA) return Mask;
118
119   // Otherwise, fall back to the next AA in the chain. But we can merge
120   // in any mask we've managed to compute.
121   return ModRefResult(AA->getModRefInfo(CS, Loc) & Mask);
122 }
123
124 AliasAnalysis::ModRefResult
125 AliasAnalysis::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) {
126   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
127
128   // If CS1 or CS2 are readnone, they don't interact.
129   ModRefBehavior CS1B = getModRefBehavior(CS1);
130   if (CS1B == DoesNotAccessMemory) return NoModRef;
131
132   ModRefBehavior CS2B = getModRefBehavior(CS2);
133   if (CS2B == DoesNotAccessMemory) return NoModRef;
134
135   // If they both only read from memory, there is no dependence.
136   if (onlyReadsMemory(CS1B) && onlyReadsMemory(CS2B))
137     return NoModRef;
138
139   AliasAnalysis::ModRefResult Mask = ModRef;
140
141   // If CS1 only reads memory, the only dependence on CS2 can be
142   // from CS1 reading memory written by CS2.
143   if (onlyReadsMemory(CS1B))
144     Mask = ModRefResult(Mask & Ref);
145
146   // If CS2 only access memory through arguments, accumulate the mod/ref
147   // information from CS1's references to the memory referenced by
148   // CS2's arguments.
149   if (onlyAccessesArgPointees(CS2B)) {
150     AliasAnalysis::ModRefResult R = NoModRef;
151     if (doesAccessArgPointees(CS2B)) {
152       MDNode *CS2Tag = CS2.getInstruction()->getMetadata(LLVMContext::MD_tbaa);
153       for (ImmutableCallSite::arg_iterator
154            I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) {
155         const Value *Arg = *I;
156         if (!Arg->getType()->isPointerTy())
157           continue;
158         Location CS2Loc(Arg, UnknownSize, CS2Tag);
159         R = ModRefResult((R | getModRefInfo(CS1, CS2Loc)) & Mask);
160         if (R == Mask)
161           break;
162       }
163     }
164     return R;
165   }
166
167   // If CS1 only accesses memory through arguments, check if CS2 references
168   // any of the memory referenced by CS1's arguments. If not, return NoModRef.
169   if (onlyAccessesArgPointees(CS1B)) {
170     AliasAnalysis::ModRefResult R = NoModRef;
171     if (doesAccessArgPointees(CS1B)) {
172       MDNode *CS1Tag = CS1.getInstruction()->getMetadata(LLVMContext::MD_tbaa);
173       for (ImmutableCallSite::arg_iterator
174            I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I) {
175         const Value *Arg = *I;
176         if (!Arg->getType()->isPointerTy())
177           continue;
178         Location CS1Loc(Arg, UnknownSize, CS1Tag);
179         if (getModRefInfo(CS2, CS1Loc) != NoModRef) {
180           R = Mask;
181           break;
182         }
183       }
184     }
185     if (R == NoModRef)
186       return R;
187   }
188
189   // If this is the end of the chain, don't forward.
190   if (!AA) return Mask;
191
192   // Otherwise, fall back to the next AA in the chain. But we can merge
193   // in any mask we've managed to compute.
194   return ModRefResult(AA->getModRefInfo(CS1, CS2) & Mask);
195 }
196
197 AliasAnalysis::ModRefBehavior
198 AliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
199   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
200
201   ModRefBehavior Min = UnknownModRefBehavior;
202
203   // Call back into the alias analysis with the other form of getModRefBehavior
204   // to see if it can give a better response.
205   if (const Function *F = CS.getCalledFunction())
206     Min = getModRefBehavior(F);
207
208   // If this is the end of the chain, don't forward.
209   if (!AA) return Min;
210
211   // Otherwise, fall back to the next AA in the chain. But we can merge
212   // in any result we've managed to compute.
213   return ModRefBehavior(AA->getModRefBehavior(CS) & Min);
214 }
215
216 AliasAnalysis::ModRefBehavior
217 AliasAnalysis::getModRefBehavior(const Function *F) {
218   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
219   return AA->getModRefBehavior(F);
220 }
221
222 //===----------------------------------------------------------------------===//
223 // AliasAnalysis non-virtual helper method implementation
224 //===----------------------------------------------------------------------===//
225
226 AliasAnalysis::Location AliasAnalysis::getLocation(const LoadInst *LI) {
227   return Location(LI->getPointerOperand(),
228                   getTypeStoreSize(LI->getType()),
229                   LI->getMetadata(LLVMContext::MD_tbaa));
230 }
231
232 AliasAnalysis::Location AliasAnalysis::getLocation(const StoreInst *SI) {
233   return Location(SI->getPointerOperand(),
234                   getTypeStoreSize(SI->getValueOperand()->getType()),
235                   SI->getMetadata(LLVMContext::MD_tbaa));
236 }
237
238 AliasAnalysis::Location AliasAnalysis::getLocation(const VAArgInst *VI) {
239   return Location(VI->getPointerOperand(),
240                   UnknownSize,
241                   VI->getMetadata(LLVMContext::MD_tbaa));
242 }
243
244 AliasAnalysis::Location
245 AliasAnalysis::getLocation(const AtomicCmpXchgInst *CXI) {
246   return Location(CXI->getPointerOperand(),
247                   getTypeStoreSize(CXI->getCompareOperand()->getType()),
248                   CXI->getMetadata(LLVMContext::MD_tbaa));
249 }
250
251 AliasAnalysis::Location
252 AliasAnalysis::getLocation(const AtomicRMWInst *RMWI) {
253   return Location(RMWI->getPointerOperand(),
254                   getTypeStoreSize(RMWI->getValOperand()->getType()),
255                   RMWI->getMetadata(LLVMContext::MD_tbaa));
256 }
257
258 AliasAnalysis::Location 
259 AliasAnalysis::getLocationForSource(const MemTransferInst *MTI) {
260   uint64_t Size = UnknownSize;
261   if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
262     Size = C->getValue().getZExtValue();
263
264   // memcpy/memmove can have TBAA tags. For memcpy, they apply
265   // to both the source and the destination.
266   MDNode *TBAATag = MTI->getMetadata(LLVMContext::MD_tbaa);
267
268   return Location(MTI->getRawSource(), Size, TBAATag);
269 }
270
271 AliasAnalysis::Location 
272 AliasAnalysis::getLocationForDest(const MemIntrinsic *MTI) {
273   uint64_t Size = UnknownSize;
274   if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
275     Size = C->getValue().getZExtValue();
276
277   // memcpy/memmove can have TBAA tags. For memcpy, they apply
278   // to both the source and the destination.
279   MDNode *TBAATag = MTI->getMetadata(LLVMContext::MD_tbaa);
280   
281   return Location(MTI->getRawDest(), Size, TBAATag);
282 }
283
284
285
286 AliasAnalysis::ModRefResult
287 AliasAnalysis::getModRefInfo(const LoadInst *L, const Location &Loc) {
288   // Be conservative in the face of volatile/atomic.
289   if (!L->isUnordered())
290     return ModRef;
291
292   // If the load address doesn't alias the given address, it doesn't read
293   // or write the specified memory.
294   if (!alias(getLocation(L), Loc))
295     return NoModRef;
296
297   // Otherwise, a load just reads.
298   return Ref;
299 }
300
301 AliasAnalysis::ModRefResult
302 AliasAnalysis::getModRefInfo(const StoreInst *S, const Location &Loc) {
303   // Be conservative in the face of volatile/atomic.
304   if (!S->isUnordered())
305     return ModRef;
306
307   // If the store address cannot alias the pointer in question, then the
308   // specified memory cannot be modified by the store.
309   if (!alias(getLocation(S), Loc))
310     return NoModRef;
311
312   // If the pointer is a pointer to constant memory, then it could not have been
313   // modified by this store.
314   if (pointsToConstantMemory(Loc))
315     return NoModRef;
316
317   // Otherwise, a store just writes.
318   return Mod;
319 }
320
321 AliasAnalysis::ModRefResult
322 AliasAnalysis::getModRefInfo(const VAArgInst *V, const Location &Loc) {
323   // If the va_arg address cannot alias the pointer in question, then the
324   // specified memory cannot be accessed by the va_arg.
325   if (!alias(getLocation(V), Loc))
326     return NoModRef;
327
328   // If the pointer is a pointer to constant memory, then it could not have been
329   // modified by this va_arg.
330   if (pointsToConstantMemory(Loc))
331     return NoModRef;
332
333   // Otherwise, a va_arg reads and writes.
334   return ModRef;
335 }
336
337 AliasAnalysis::ModRefResult
338 AliasAnalysis::getModRefInfo(const AtomicCmpXchgInst *CX, const Location &Loc) {
339   // Acquire/Release cmpxchg has properties that matter for arbitrary addresses.
340   if (CX->getOrdering() > Monotonic)
341     return ModRef;
342
343   // If the cmpxchg address does not alias the location, it does not access it.
344   if (!alias(getLocation(CX), Loc))
345     return NoModRef;
346
347   return ModRef;
348 }
349
350 AliasAnalysis::ModRefResult
351 AliasAnalysis::getModRefInfo(const AtomicRMWInst *RMW, const Location &Loc) {
352   // Acquire/Release atomicrmw has properties that matter for arbitrary addresses.
353   if (RMW->getOrdering() > Monotonic)
354     return ModRef;
355
356   // If the atomicrmw address does not alias the location, it does not access it.
357   if (!alias(getLocation(RMW), Loc))
358     return NoModRef;
359
360   return ModRef;
361 }
362
363 namespace {
364   /// Determine whether there is a path from From to To within a single
365   /// function. Returns false only if we can prove that once 'From' has been
366   /// executed then 'To' can not be executed. Conservatively returns true.
367   static bool isPotentiallyReachable(const BasicBlock *From,
368                                      const BasicBlock *To) {
369     const unsigned MaxCheck = 5;
370     const BasicBlock *Current = From;
371     for (unsigned I = 0; I < MaxCheck; I++) {
372       unsigned NumSuccs = Current->getTerminator()->getNumSuccessors();
373       if (NumSuccs > 1)
374         return true;
375       if (NumSuccs == 0)
376         return false;
377       Current = Current->getTerminator()->getSuccessor(0);
378       if (Current == To)
379         return true;
380     }
381     return true;
382   }
383
384   /// Only find pointer captures which happen before the given instruction. Uses
385   /// the dominator tree to determine whether one instruction is before another.
386   /// Only support the case where the Value is defined in the same basic block
387   /// as the given instruction and the use.
388   struct CapturesBefore : public CaptureTracker {
389     CapturesBefore(const Instruction *I, DominatorTree *DT)
390       : BeforeHere(I), DT(DT), Captured(false) {}
391
392     void tooManyUses() { Captured = true; }
393
394     bool shouldExplore(Use *U) {
395       Instruction *I = cast<Instruction>(U->getUser());
396       BasicBlock *BB = I->getParent();
397       // We explore this usage only if the usage can reach "BeforeHere".
398       // If use is not reachable from entry, there is no need to explore.
399       if (BeforeHere != I && !DT->isReachableFromEntry(BB))
400         return false;
401       // If the value is defined in the same basic block as use and BeforeHere,
402       // there is no need to explore the use if BeforeHere dominates use.
403       // Check whether there is a path from I to BeforeHere.
404       if (BeforeHere != I && DT->dominates(BeforeHere, I) &&
405           !isPotentiallyReachable(BB, BeforeHere->getParent()))
406         return false;
407       return true;
408     }
409
410     bool captured(Use *U) {
411       Instruction *I = cast<Instruction>(U->getUser());
412       BasicBlock *BB = I->getParent();
413       // Same logic as in shouldExplore.
414       if (BeforeHere != I && !DT->isReachableFromEntry(BB))
415         return false;
416       if (BeforeHere != I && DT->dominates(BeforeHere, I) &&
417           !isPotentiallyReachable(BB, BeforeHere->getParent()))
418         return false;
419       Captured = true;
420       return true;
421     }
422
423     const Instruction *BeforeHere;
424     DominatorTree *DT;
425
426     bool Captured;
427   };
428 }
429
430 // FIXME: this is really just shoring-up a deficiency in alias analysis.
431 // BasicAA isn't willing to spend linear time determining whether an alloca
432 // was captured before or after this particular call, while we are. However,
433 // with a smarter AA in place, this test is just wasting compile time.
434 AliasAnalysis::ModRefResult
435 AliasAnalysis::callCapturesBefore(const Instruction *I,
436                                   const AliasAnalysis::Location &MemLoc,
437                                   DominatorTree *DT) {
438   if (!DT || !TD) return AliasAnalysis::ModRef;
439
440   const Value *Object = GetUnderlyingObject(MemLoc.Ptr, TD);
441   if (!isIdentifiedObject(Object) || isa<GlobalValue>(Object) ||
442       isa<Constant>(Object))
443     return AliasAnalysis::ModRef;
444
445   ImmutableCallSite CS(I);
446   if (!CS.getInstruction() || CS.getInstruction() == Object)
447     return AliasAnalysis::ModRef;
448
449   CapturesBefore CB(I, DT);
450   llvm::PointerMayBeCaptured(Object, &CB);
451   if (CB.Captured)
452     return AliasAnalysis::ModRef;
453
454   unsigned ArgNo = 0;
455   AliasAnalysis::ModRefResult R = AliasAnalysis::NoModRef;
456   for (ImmutableCallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
457        CI != CE; ++CI, ++ArgNo) {
458     // Only look at the no-capture or byval pointer arguments.  If this
459     // pointer were passed to arguments that were neither of these, then it
460     // couldn't be no-capture.
461     if (!(*CI)->getType()->isPointerTy() ||
462         (!CS.doesNotCapture(ArgNo) && !CS.isByValArgument(ArgNo)))
463       continue;
464
465     // If this is a no-capture pointer argument, see if we can tell that it
466     // is impossible to alias the pointer we're checking.  If not, we have to
467     // assume that the call could touch the pointer, even though it doesn't
468     // escape.
469     if (isNoAlias(AliasAnalysis::Location(*CI),
470                   AliasAnalysis::Location(Object)))
471       continue;
472     if (CS.doesNotAccessMemory(ArgNo))
473       continue;
474     if (CS.onlyReadsMemory(ArgNo)) {
475       R = AliasAnalysis::Ref;
476       continue;
477     }
478     return AliasAnalysis::ModRef;
479   }
480   return R;
481 }
482
483 // AliasAnalysis destructor: DO NOT move this to the header file for
484 // AliasAnalysis or else clients of the AliasAnalysis class may not depend on
485 // the AliasAnalysis.o file in the current .a file, causing alias analysis
486 // support to not be included in the tool correctly!
487 //
488 AliasAnalysis::~AliasAnalysis() {}
489
490 /// InitializeAliasAnalysis - Subclasses must call this method to initialize the
491 /// AliasAnalysis interface before any other methods are called.
492 ///
493 void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
494   TD = P->getAnalysisIfAvailable<DataLayout>();
495   TLI = P->getAnalysisIfAvailable<TargetLibraryInfo>();
496   AA = &P->getAnalysis<AliasAnalysis>();
497 }
498
499 // getAnalysisUsage - All alias analysis implementations should invoke this
500 // directly (using AliasAnalysis::getAnalysisUsage(AU)).
501 void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
502   AU.addRequired<AliasAnalysis>();         // All AA's chain
503 }
504
505 /// getTypeStoreSize - Return the DataLayout store size for the given type,
506 /// if known, or a conservative value otherwise.
507 ///
508 uint64_t AliasAnalysis::getTypeStoreSize(Type *Ty) {
509   return TD ? TD->getTypeStoreSize(Ty) : UnknownSize;
510 }
511
512 /// canBasicBlockModify - Return true if it is possible for execution of the
513 /// specified basic block to modify the value pointed to by Ptr.
514 ///
515 bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
516                                         const Location &Loc) {
517   return canInstructionRangeModify(BB.front(), BB.back(), Loc);
518 }
519
520 /// canInstructionRangeModify - Return true if it is possible for the execution
521 /// of the specified instructions to modify the value pointed to by Ptr.  The
522 /// instructions to consider are all of the instructions in the range of [I1,I2]
523 /// INCLUSIVE.  I1 and I2 must be in the same basic block.
524 ///
525 bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
526                                               const Instruction &I2,
527                                               const Location &Loc) {
528   assert(I1.getParent() == I2.getParent() &&
529          "Instructions not in same basic block!");
530   BasicBlock::const_iterator I = &I1;
531   BasicBlock::const_iterator E = &I2;
532   ++E;  // Convert from inclusive to exclusive range.
533
534   for (; I != E; ++I) // Check every instruction in range
535     if (getModRefInfo(I, Loc) & Mod)
536       return true;
537   return false;
538 }
539
540 /// isNoAliasCall - Return true if this pointer is returned by a noalias
541 /// function.
542 bool llvm::isNoAliasCall(const Value *V) {
543   if (isa<CallInst>(V) || isa<InvokeInst>(V))
544     return ImmutableCallSite(cast<Instruction>(V))
545       .paramHasAttr(0, Attribute::NoAlias);
546   return false;
547 }
548
549 /// isNoAliasArgument - Return true if this is an argument with the noalias
550 /// attribute.
551 bool llvm::isNoAliasArgument(const Value *V)
552 {
553   if (const Argument *A = dyn_cast<Argument>(V))
554     return A->hasNoAliasAttr();
555   return false;
556 }
557
558 /// isIdentifiedObject - Return true if this pointer refers to a distinct and
559 /// identifiable object.  This returns true for:
560 ///    Global Variables and Functions (but not Global Aliases)
561 ///    Allocas and Mallocs
562 ///    ByVal and NoAlias Arguments
563 ///    NoAlias returns
564 ///
565 bool llvm::isIdentifiedObject(const Value *V) {
566   if (isa<AllocaInst>(V))
567     return true;
568   if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
569     return true;
570   if (isNoAliasCall(V))
571     return true;
572   if (const Argument *A = dyn_cast<Argument>(V))
573     return A->hasNoAliasAttr() || A->hasByValAttr();
574   return false;
575 }