1 //===- BasicAliasAnalysis.cpp - Stateless Alias Analysis Impl -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the primary stateless implementation of the
11 // Alias Analysis interface that implements identities (two different
12 // globals cannot alias, etc), but does no stateful analysis.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Analysis/Passes.h"
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/Analysis/AliasAnalysis.h"
20 #include "llvm/Analysis/CFG.h"
21 #include "llvm/Analysis/CaptureTracking.h"
22 #include "llvm/Analysis/InstructionSimplify.h"
23 #include "llvm/Analysis/LoopInfo.h"
24 #include "llvm/Analysis/MemoryBuiltins.h"
25 #include "llvm/Analysis/ValueTracking.h"
26 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/DataLayout.h"
28 #include "llvm/IR/DerivedTypes.h"
29 #include "llvm/IR/Dominators.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/GlobalAlias.h"
32 #include "llvm/IR/GlobalVariable.h"
33 #include "llvm/IR/Instructions.h"
34 #include "llvm/IR/IntrinsicInst.h"
35 #include "llvm/IR/LLVMContext.h"
36 #include "llvm/IR/Operator.h"
37 #include "llvm/Pass.h"
38 #include "llvm/Support/ErrorHandling.h"
39 #include "llvm/Support/GetElementPtrTypeIterator.h"
40 #include "llvm/Target/TargetLibraryInfo.h"
44 /// Cutoff after which to stop analysing a set of phi nodes potentially involved
45 /// in a cycle. Because we are analysing 'through' phi nodes we need to be
46 /// careful with value equivalence. We use reachability to make sure a value
47 /// cannot be involved in a cycle.
48 const unsigned MaxNumPhiBBsValueReachabilityCheck = 20;
50 //===----------------------------------------------------------------------===//
52 //===----------------------------------------------------------------------===//
54 /// isNonEscapingLocalObject - Return true if the pointer is to a function-local
55 /// object that never escapes from the function.
56 static bool isNonEscapingLocalObject(const Value *V) {
57 // If this is a local allocation, check to see if it escapes.
58 if (isa<AllocaInst>(V) || isNoAliasCall(V))
59 // Set StoreCaptures to True so that we can assume in our callers that the
60 // pointer is not the result of a load instruction. Currently
61 // PointerMayBeCaptured doesn't have any special analysis for the
62 // StoreCaptures=false case; if it did, our callers could be refined to be
64 return !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true);
66 // If this is an argument that corresponds to a byval or noalias argument,
67 // then it has not escaped before entering the function. Check if it escapes
68 // inside the function.
69 if (const Argument *A = dyn_cast<Argument>(V))
70 if (A->hasByValAttr() || A->hasNoAliasAttr())
71 // Note even if the argument is marked nocapture we still need to check
72 // for copies made inside the function. The nocapture attribute only
73 // specifies that there are no copies made that outlive the function.
74 return !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true);
79 /// isEscapeSource - Return true if the pointer is one which would have
80 /// been considered an escape by isNonEscapingLocalObject.
81 static bool isEscapeSource(const Value *V) {
82 if (isa<CallInst>(V) || isa<InvokeInst>(V) || isa<Argument>(V))
85 // The load case works because isNonEscapingLocalObject considers all
86 // stores to be escapes (it passes true for the StoreCaptures argument
87 // to PointerMayBeCaptured).
94 /// getObjectSize - Return the size of the object specified by V, or
95 /// UnknownSize if unknown.
96 static uint64_t getObjectSize(const Value *V, const DataLayout &TD,
97 const TargetLibraryInfo &TLI,
98 bool RoundToAlign = false) {
100 if (getObjectSize(V, Size, &TD, &TLI, RoundToAlign))
102 return AliasAnalysis::UnknownSize;
105 /// isObjectSmallerThan - Return true if we can prove that the object specified
106 /// by V is smaller than Size.
107 static bool isObjectSmallerThan(const Value *V, uint64_t Size,
108 const DataLayout &TD,
109 const TargetLibraryInfo &TLI) {
110 // Note that the meanings of the "object" are slightly different in the
111 // following contexts:
112 // c1: llvm::getObjectSize()
113 // c2: llvm.objectsize() intrinsic
114 // c3: isObjectSmallerThan()
115 // c1 and c2 share the same meaning; however, the meaning of "object" in c3
116 // refers to the "entire object".
118 // Consider this example:
119 // char *p = (char*)malloc(100)
122 // In the context of c1 and c2, the "object" pointed by q refers to the
123 // stretch of memory of q[0:19]. So, getObjectSize(q) should return 20.
125 // However, in the context of c3, the "object" refers to the chunk of memory
126 // being allocated. So, the "object" has 100 bytes, and q points to the middle
127 // the "object". In case q is passed to isObjectSmallerThan() as the 1st
128 // parameter, before the llvm::getObjectSize() is called to get the size of
129 // entire object, we should:
130 // - either rewind the pointer q to the base-address of the object in
131 // question (in this case rewind to p), or
132 // - just give up. It is up to caller to make sure the pointer is pointing
133 // to the base address the object.
135 // We go for 2nd option for simplicity.
136 if (!isIdentifiedObject(V))
139 // This function needs to use the aligned object size because we allow
140 // reads a bit past the end given sufficient alignment.
141 uint64_t ObjectSize = getObjectSize(V, TD, TLI, /*RoundToAlign*/true);
143 return ObjectSize != AliasAnalysis::UnknownSize && ObjectSize < Size;
146 /// isObjectSize - Return true if we can prove that the object specified
147 /// by V has size Size.
148 static bool isObjectSize(const Value *V, uint64_t Size,
149 const DataLayout &TD, const TargetLibraryInfo &TLI) {
150 uint64_t ObjectSize = getObjectSize(V, TD, TLI);
151 return ObjectSize != AliasAnalysis::UnknownSize && ObjectSize == Size;
154 /// isIdentifiedFunctionLocal - Return true if V is umabigously identified
155 /// at the function-level. Different IdentifiedFunctionLocals can't alias.
156 /// Further, an IdentifiedFunctionLocal can not alias with any function
157 /// arguments other than itself, which is not neccessarily true for
158 /// IdentifiedObjects.
159 static bool isIdentifiedFunctionLocal(const Value *V)
161 return isa<AllocaInst>(V) || isNoAliasCall(V) || isNoAliasArgument(V);
165 //===----------------------------------------------------------------------===//
166 // GetElementPtr Instruction Decomposition and Analysis
167 //===----------------------------------------------------------------------===//
176 struct VariableGEPIndex {
178 ExtensionKind Extension;
181 bool operator==(const VariableGEPIndex &Other) const {
182 return V == Other.V && Extension == Other.Extension &&
183 Scale == Other.Scale;
186 bool operator!=(const VariableGEPIndex &Other) const {
187 return !operator==(Other);
193 /// GetLinearExpression - Analyze the specified value as a linear expression:
194 /// "A*V + B", where A and B are constant integers. Return the scale and offset
195 /// values as APInts and return V as a Value*, and return whether we looked
196 /// through any sign or zero extends. The incoming Value is known to have
197 /// IntegerType and it may already be sign or zero extended.
199 /// Note that this looks through extends, so the high bits may not be
200 /// represented in the result.
201 static Value *GetLinearExpression(Value *V, APInt &Scale, APInt &Offset,
202 ExtensionKind &Extension,
203 const DataLayout &TD, unsigned Depth) {
204 assert(V->getType()->isIntegerTy() && "Not an integer value");
206 // Limit our recursion depth.
213 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(V)) {
214 if (ConstantInt *RHSC = dyn_cast<ConstantInt>(BOp->getOperand(1))) {
215 switch (BOp->getOpcode()) {
217 case Instruction::Or:
218 // X|C == X+C if all the bits in C are unset in X. Otherwise we can't
220 if (!MaskedValueIsZero(BOp->getOperand(0), RHSC->getValue(), &TD))
223 case Instruction::Add:
224 V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, Extension,
226 Offset += RHSC->getValue();
228 case Instruction::Mul:
229 V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, Extension,
231 Offset *= RHSC->getValue();
232 Scale *= RHSC->getValue();
234 case Instruction::Shl:
235 V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, Extension,
237 Offset <<= RHSC->getValue().getLimitedValue();
238 Scale <<= RHSC->getValue().getLimitedValue();
244 // Since GEP indices are sign extended anyway, we don't care about the high
245 // bits of a sign or zero extended value - just scales and offsets. The
246 // extensions have to be consistent though.
247 if ((isa<SExtInst>(V) && Extension != EK_ZeroExt) ||
248 (isa<ZExtInst>(V) && Extension != EK_SignExt)) {
249 Value *CastOp = cast<CastInst>(V)->getOperand(0);
250 unsigned OldWidth = Scale.getBitWidth();
251 unsigned SmallWidth = CastOp->getType()->getPrimitiveSizeInBits();
252 Scale = Scale.trunc(SmallWidth);
253 Offset = Offset.trunc(SmallWidth);
254 Extension = isa<SExtInst>(V) ? EK_SignExt : EK_ZeroExt;
256 Value *Result = GetLinearExpression(CastOp, Scale, Offset, Extension,
258 Scale = Scale.zext(OldWidth);
259 Offset = Offset.zext(OldWidth);
269 /// DecomposeGEPExpression - If V is a symbolic pointer expression, decompose it
270 /// into a base pointer with a constant offset and a number of scaled symbolic
273 /// The scaled symbolic offsets (represented by pairs of a Value* and a scale in
274 /// the VarIndices vector) are Value*'s that are known to be scaled by the
275 /// specified amount, but which may have other unrepresented high bits. As such,
276 /// the gep cannot necessarily be reconstructed from its decomposed form.
278 /// When DataLayout is around, this function is capable of analyzing everything
279 /// that GetUnderlyingObject can look through. When not, it just looks
280 /// through pointer casts.
283 DecomposeGEPExpression(const Value *V, int64_t &BaseOffs,
284 SmallVectorImpl<VariableGEPIndex> &VarIndices,
285 const DataLayout *TD) {
286 // Limit recursion depth to limit compile time in crazy cases.
287 unsigned MaxLookup = 6;
291 // See if this is a bitcast or GEP.
292 const Operator *Op = dyn_cast<Operator>(V);
294 // The only non-operator case we can handle are GlobalAliases.
295 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
296 if (!GA->mayBeOverridden()) {
297 V = GA->getAliasee();
304 if (Op->getOpcode() == Instruction::BitCast) {
305 V = Op->getOperand(0);
309 const GEPOperator *GEPOp = dyn_cast<GEPOperator>(Op);
311 // If it's not a GEP, hand it off to SimplifyInstruction to see if it
312 // can come up with something. This matches what GetUnderlyingObject does.
313 if (const Instruction *I = dyn_cast<Instruction>(V))
314 // TODO: Get a DominatorTree and use it here.
315 if (const Value *Simplified =
316 SimplifyInstruction(const_cast<Instruction *>(I), TD)) {
324 // Don't attempt to analyze GEPs over unsized objects.
325 if (!GEPOp->getOperand(0)->getType()->getPointerElementType()->isSized())
328 // If we are lacking DataLayout information, we can't compute the offets of
329 // elements computed by GEPs. However, we can handle bitcast equivalent
332 if (!GEPOp->hasAllZeroIndices())
334 V = GEPOp->getOperand(0);
338 unsigned AS = GEPOp->getPointerAddressSpace();
339 // Walk the indices of the GEP, accumulating them into BaseOff/VarIndices.
340 gep_type_iterator GTI = gep_type_begin(GEPOp);
341 for (User::const_op_iterator I = GEPOp->op_begin()+1,
342 E = GEPOp->op_end(); I != E; ++I) {
344 // Compute the (potentially symbolic) offset in bytes for this index.
345 if (StructType *STy = dyn_cast<StructType>(*GTI++)) {
346 // For a struct, add the member offset.
347 unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue();
348 if (FieldNo == 0) continue;
350 BaseOffs += TD->getStructLayout(STy)->getElementOffset(FieldNo);
354 // For an array/pointer, add the element offset, explicitly scaled.
355 if (ConstantInt *CIdx = dyn_cast<ConstantInt>(Index)) {
356 if (CIdx->isZero()) continue;
357 BaseOffs += TD->getTypeAllocSize(*GTI)*CIdx->getSExtValue();
361 uint64_t Scale = TD->getTypeAllocSize(*GTI);
362 ExtensionKind Extension = EK_NotExtended;
364 // If the integer type is smaller than the pointer size, it is implicitly
365 // sign extended to pointer size.
366 unsigned Width = Index->getType()->getIntegerBitWidth();
367 if (TD->getPointerSizeInBits(AS) > Width)
368 Extension = EK_SignExt;
370 // Use GetLinearExpression to decompose the index into a C1*V+C2 form.
371 APInt IndexScale(Width, 0), IndexOffset(Width, 0);
372 Index = GetLinearExpression(Index, IndexScale, IndexOffset, Extension,
375 // The GEP index scale ("Scale") scales C1*V+C2, yielding (C1*V+C2)*Scale.
376 // This gives us an aggregate computation of (C1*Scale)*V + C2*Scale.
377 BaseOffs += IndexOffset.getSExtValue()*Scale;
378 Scale *= IndexScale.getSExtValue();
380 // If we already had an occurrence of this index variable, merge this
381 // scale into it. For example, we want to handle:
382 // A[x][x] -> x*16 + x*4 -> x*20
383 // This also ensures that 'x' only appears in the index list once.
384 for (unsigned i = 0, e = VarIndices.size(); i != e; ++i) {
385 if (VarIndices[i].V == Index &&
386 VarIndices[i].Extension == Extension) {
387 Scale += VarIndices[i].Scale;
388 VarIndices.erase(VarIndices.begin()+i);
393 // Make sure that we have a scale that makes sense for this target's
395 if (unsigned ShiftBits = 64 - TD->getPointerSizeInBits(AS)) {
397 Scale = (int64_t)Scale >> ShiftBits;
401 VariableGEPIndex Entry = {Index, Extension,
402 static_cast<int64_t>(Scale)};
403 VarIndices.push_back(Entry);
407 // Analyze the base pointer next.
408 V = GEPOp->getOperand(0);
409 } while (--MaxLookup);
411 // If the chain of expressions is too deep, just return early.
415 //===----------------------------------------------------------------------===//
416 // BasicAliasAnalysis Pass
417 //===----------------------------------------------------------------------===//
420 static const Function *getParent(const Value *V) {
421 if (const Instruction *inst = dyn_cast<Instruction>(V))
422 return inst->getParent()->getParent();
424 if (const Argument *arg = dyn_cast<Argument>(V))
425 return arg->getParent();
430 static bool notDifferentParent(const Value *O1, const Value *O2) {
432 const Function *F1 = getParent(O1);
433 const Function *F2 = getParent(O2);
435 return !F1 || !F2 || F1 == F2;
440 /// BasicAliasAnalysis - This is the primary alias analysis implementation.
441 struct BasicAliasAnalysis : public ImmutablePass, public AliasAnalysis {
442 static char ID; // Class identification, replacement for typeinfo
443 BasicAliasAnalysis() : ImmutablePass(ID) {
444 initializeBasicAliasAnalysisPass(*PassRegistry::getPassRegistry());
447 virtual void initializePass() {
448 InitializeAliasAnalysis(this);
451 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
452 AU.addRequired<AliasAnalysis>();
453 AU.addRequired<TargetLibraryInfo>();
456 virtual AliasResult alias(const Location &LocA,
457 const Location &LocB) {
458 assert(AliasCache.empty() && "AliasCache must be cleared after use!");
459 assert(notDifferentParent(LocA.Ptr, LocB.Ptr) &&
460 "BasicAliasAnalysis doesn't support interprocedural queries.");
461 AliasResult Alias = aliasCheck(LocA.Ptr, LocA.Size, LocA.TBAATag,
462 LocB.Ptr, LocB.Size, LocB.TBAATag);
463 // AliasCache rarely has more than 1 or 2 elements, always use
464 // shrink_and_clear so it quickly returns to the inline capacity of the
465 // SmallDenseMap if it ever grows larger.
466 // FIXME: This should really be shrink_to_inline_capacity_and_clear().
467 AliasCache.shrink_and_clear();
468 VisitedPhiBBs.clear();
472 virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
473 const Location &Loc);
475 virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
476 ImmutableCallSite CS2) {
477 // The AliasAnalysis base class has some smarts, lets use them.
478 return AliasAnalysis::getModRefInfo(CS1, CS2);
481 /// pointsToConstantMemory - Chase pointers until we find a (constant
483 virtual bool pointsToConstantMemory(const Location &Loc, bool OrLocal);
485 /// getModRefBehavior - Return the behavior when calling the given
487 virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS);
489 /// getModRefBehavior - Return the behavior when calling the given function.
490 /// For use when the call site is not known.
491 virtual ModRefBehavior getModRefBehavior(const Function *F);
493 /// getAdjustedAnalysisPointer - This method is used when a pass implements
494 /// an analysis interface through multiple inheritance. If needed, it
495 /// should override this to adjust the this pointer as needed for the
496 /// specified pass info.
497 virtual void *getAdjustedAnalysisPointer(const void *ID) {
498 if (ID == &AliasAnalysis::ID)
499 return (AliasAnalysis*)this;
504 // AliasCache - Track alias queries to guard against recursion.
505 typedef std::pair<Location, Location> LocPair;
506 typedef SmallDenseMap<LocPair, AliasResult, 8> AliasCacheTy;
507 AliasCacheTy AliasCache;
509 /// \brief Track phi nodes we have visited. When interpret "Value" pointer
510 /// equality as value equality we need to make sure that the "Value" is not
511 /// part of a cycle. Otherwise, two uses could come from different
512 /// "iterations" of a cycle and see different values for the same "Value"
514 /// The following example shows the problem:
515 /// %p = phi(%alloca1, %addr2)
517 /// %addr1 = gep, %alloca2, 0, %l
518 /// %addr2 = gep %alloca2, 0, (%l + 1)
519 /// alias(%p, %addr1) -> MayAlias !
521 SmallPtrSet<const BasicBlock*, 8> VisitedPhiBBs;
523 // Visited - Track instructions visited by pointsToConstantMemory.
524 SmallPtrSet<const Value*, 16> Visited;
526 /// \brief Check whether two Values can be considered equivalent.
528 /// In addition to pointer equivalence of \p V1 and \p V2 this checks
529 /// whether they can not be part of a cycle in the value graph by looking at
530 /// all visited phi nodes an making sure that the phis cannot reach the
531 /// value. We have to do this because we are looking through phi nodes (That
532 /// is we say noalias(V, phi(VA, VB)) if noalias(V, VA) and noalias(V, VB).
533 bool isValueEqualInPotentialCycles(const Value *V1, const Value *V2);
535 /// \brief Dest and Src are the variable indices from two decomposed
536 /// GetElementPtr instructions GEP1 and GEP2 which have common base
537 /// pointers. Subtract the GEP2 indices from GEP1 to find the symbolic
538 /// difference between the two pointers.
539 void GetIndexDifference(SmallVectorImpl<VariableGEPIndex> &Dest,
540 const SmallVectorImpl<VariableGEPIndex> &Src);
542 // aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP
543 // instruction against another.
544 AliasResult aliasGEP(const GEPOperator *V1, uint64_t V1Size,
545 const MDNode *V1TBAAInfo,
546 const Value *V2, uint64_t V2Size,
547 const MDNode *V2TBAAInfo,
548 const Value *UnderlyingV1, const Value *UnderlyingV2);
550 // aliasPHI - Provide a bunch of ad-hoc rules to disambiguate a PHI
551 // instruction against another.
552 AliasResult aliasPHI(const PHINode *PN, uint64_t PNSize,
553 const MDNode *PNTBAAInfo,
554 const Value *V2, uint64_t V2Size,
555 const MDNode *V2TBAAInfo);
557 /// aliasSelect - Disambiguate a Select instruction against another value.
558 AliasResult aliasSelect(const SelectInst *SI, uint64_t SISize,
559 const MDNode *SITBAAInfo,
560 const Value *V2, uint64_t V2Size,
561 const MDNode *V2TBAAInfo);
563 AliasResult aliasCheck(const Value *V1, uint64_t V1Size,
564 const MDNode *V1TBAATag,
565 const Value *V2, uint64_t V2Size,
566 const MDNode *V2TBAATag);
568 } // End of anonymous namespace
570 // Register this pass...
571 char BasicAliasAnalysis::ID = 0;
572 INITIALIZE_AG_PASS_BEGIN(BasicAliasAnalysis, AliasAnalysis, "basicaa",
573 "Basic Alias Analysis (stateless AA impl)",
575 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
576 INITIALIZE_AG_PASS_END(BasicAliasAnalysis, AliasAnalysis, "basicaa",
577 "Basic Alias Analysis (stateless AA impl)",
581 ImmutablePass *llvm::createBasicAliasAnalysisPass() {
582 return new BasicAliasAnalysis();
585 /// pointsToConstantMemory - Returns whether the given pointer value
586 /// points to memory that is local to the function, with global constants being
587 /// considered local to all functions.
589 BasicAliasAnalysis::pointsToConstantMemory(const Location &Loc, bool OrLocal) {
590 assert(Visited.empty() && "Visited must be cleared after use!");
592 unsigned MaxLookup = 8;
593 SmallVector<const Value *, 16> Worklist;
594 Worklist.push_back(Loc.Ptr);
596 const Value *V = GetUnderlyingObject(Worklist.pop_back_val(), TD);
597 if (!Visited.insert(V)) {
599 return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
602 // An alloca instruction defines local memory.
603 if (OrLocal && isa<AllocaInst>(V))
606 // A global constant counts as local memory for our purposes.
607 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
608 // Note: this doesn't require GV to be "ODR" because it isn't legal for a
609 // global to be marked constant in some modules and non-constant in
610 // others. GV may even be a declaration, not a definition.
611 if (!GV->isConstant()) {
613 return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
618 // If both select values point to local memory, then so does the select.
619 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
620 Worklist.push_back(SI->getTrueValue());
621 Worklist.push_back(SI->getFalseValue());
625 // If all values incoming to a phi node point to local memory, then so does
627 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
628 // Don't bother inspecting phi nodes with many operands.
629 if (PN->getNumIncomingValues() > MaxLookup) {
631 return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
633 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
634 Worklist.push_back(PN->getIncomingValue(i));
638 // Otherwise be conservative.
640 return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
642 } while (!Worklist.empty() && --MaxLookup);
645 return Worklist.empty();
648 /// getModRefBehavior - Return the behavior when calling the given call site.
649 AliasAnalysis::ModRefBehavior
650 BasicAliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
651 if (CS.doesNotAccessMemory())
652 // Can't do better than this.
653 return DoesNotAccessMemory;
655 ModRefBehavior Min = UnknownModRefBehavior;
657 // If the callsite knows it only reads memory, don't return worse
659 if (CS.onlyReadsMemory())
660 Min = OnlyReadsMemory;
662 // The AliasAnalysis base class has some smarts, lets use them.
663 return ModRefBehavior(AliasAnalysis::getModRefBehavior(CS) & Min);
666 /// getModRefBehavior - Return the behavior when calling the given function.
667 /// For use when the call site is not known.
668 AliasAnalysis::ModRefBehavior
669 BasicAliasAnalysis::getModRefBehavior(const Function *F) {
670 // If the function declares it doesn't access memory, we can't do better.
671 if (F->doesNotAccessMemory())
672 return DoesNotAccessMemory;
674 // For intrinsics, we can check the table.
675 if (unsigned iid = F->getIntrinsicID()) {
676 #define GET_INTRINSIC_MODREF_BEHAVIOR
677 #include "llvm/IR/Intrinsics.gen"
678 #undef GET_INTRINSIC_MODREF_BEHAVIOR
681 ModRefBehavior Min = UnknownModRefBehavior;
683 // If the function declares it only reads memory, go with that.
684 if (F->onlyReadsMemory())
685 Min = OnlyReadsMemory;
687 // Otherwise be conservative.
688 return ModRefBehavior(AliasAnalysis::getModRefBehavior(F) & Min);
691 /// getModRefInfo - Check to see if the specified callsite can clobber the
692 /// specified memory object. Since we only look at local properties of this
693 /// function, we really can't say much about this query. We do, however, use
694 /// simple "address taken" analysis on local objects.
695 AliasAnalysis::ModRefResult
696 BasicAliasAnalysis::getModRefInfo(ImmutableCallSite CS,
697 const Location &Loc) {
698 assert(notDifferentParent(CS.getInstruction(), Loc.Ptr) &&
699 "AliasAnalysis query involving multiple functions!");
701 const Value *Object = GetUnderlyingObject(Loc.Ptr, TD);
703 // If this is a tail call and Loc.Ptr points to a stack location, we know that
704 // the tail call cannot access or modify the local stack.
705 // We cannot exclude byval arguments here; these belong to the caller of
706 // the current function not to the current function, and a tail callee
707 // may reference them.
708 if (isa<AllocaInst>(Object))
709 if (const CallInst *CI = dyn_cast<CallInst>(CS.getInstruction()))
710 if (CI->isTailCall())
713 // If the pointer is to a locally allocated object that does not escape,
714 // then the call can not mod/ref the pointer unless the call takes the pointer
715 // as an argument, and itself doesn't capture it.
716 if (!isa<Constant>(Object) && CS.getInstruction() != Object &&
717 isNonEscapingLocalObject(Object)) {
718 bool PassedAsArg = false;
720 for (ImmutableCallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
721 CI != CE; ++CI, ++ArgNo) {
722 // Only look at the no-capture or byval pointer arguments. If this
723 // pointer were passed to arguments that were neither of these, then it
724 // couldn't be no-capture.
725 if (!(*CI)->getType()->isPointerTy() ||
726 (!CS.doesNotCapture(ArgNo) && !CS.isByValArgument(ArgNo)))
729 // If this is a no-capture pointer argument, see if we can tell that it
730 // is impossible to alias the pointer we're checking. If not, we have to
731 // assume that the call could touch the pointer, even though it doesn't
733 if (!isNoAlias(Location(*CI), Location(Object))) {
743 const TargetLibraryInfo &TLI = getAnalysis<TargetLibraryInfo>();
744 ModRefResult Min = ModRef;
746 // Finally, handle specific knowledge of intrinsics.
747 const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction());
749 switch (II->getIntrinsicID()) {
751 case Intrinsic::memcpy:
752 case Intrinsic::memmove: {
753 uint64_t Len = UnknownSize;
754 if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getArgOperand(2)))
755 Len = LenCI->getZExtValue();
756 Value *Dest = II->getArgOperand(0);
757 Value *Src = II->getArgOperand(1);
758 // If it can't overlap the source dest, then it doesn't modref the loc.
759 if (isNoAlias(Location(Dest, Len), Loc)) {
760 if (isNoAlias(Location(Src, Len), Loc))
762 // If it can't overlap the dest, then worst case it reads the loc.
764 } else if (isNoAlias(Location(Src, Len), Loc)) {
765 // If it can't overlap the source, then worst case it mutates the loc.
770 case Intrinsic::memset:
771 // Since memset is 'accesses arguments' only, the AliasAnalysis base class
772 // will handle it for the variable length case.
773 if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getArgOperand(2))) {
774 uint64_t Len = LenCI->getZExtValue();
775 Value *Dest = II->getArgOperand(0);
776 if (isNoAlias(Location(Dest, Len), Loc))
779 // We know that memset doesn't load anything.
782 case Intrinsic::lifetime_start:
783 case Intrinsic::lifetime_end:
784 case Intrinsic::invariant_start: {
786 cast<ConstantInt>(II->getArgOperand(0))->getZExtValue();
787 if (isNoAlias(Location(II->getArgOperand(1),
789 II->getMetadata(LLVMContext::MD_tbaa)),
794 case Intrinsic::invariant_end: {
796 cast<ConstantInt>(II->getArgOperand(1))->getZExtValue();
797 if (isNoAlias(Location(II->getArgOperand(2),
799 II->getMetadata(LLVMContext::MD_tbaa)),
804 case Intrinsic::arm_neon_vld1: {
805 // LLVM's vld1 and vst1 intrinsics currently only support a single
808 TD ? TD->getTypeStoreSize(II->getType()) : UnknownSize;
809 if (isNoAlias(Location(II->getArgOperand(0), Size,
810 II->getMetadata(LLVMContext::MD_tbaa)),
815 case Intrinsic::arm_neon_vst1: {
817 TD ? TD->getTypeStoreSize(II->getArgOperand(1)->getType()) : UnknownSize;
818 if (isNoAlias(Location(II->getArgOperand(0), Size,
819 II->getMetadata(LLVMContext::MD_tbaa)),
826 // We can bound the aliasing properties of memset_pattern16 just as we can
827 // for memcpy/memset. This is particularly important because the
828 // LoopIdiomRecognizer likes to turn loops into calls to memset_pattern16
829 // whenever possible.
830 else if (TLI.has(LibFunc::memset_pattern16) &&
831 CS.getCalledFunction() &&
832 CS.getCalledFunction()->getName() == "memset_pattern16") {
833 const Function *MS = CS.getCalledFunction();
834 FunctionType *MemsetType = MS->getFunctionType();
835 if (!MemsetType->isVarArg() && MemsetType->getNumParams() == 3 &&
836 isa<PointerType>(MemsetType->getParamType(0)) &&
837 isa<PointerType>(MemsetType->getParamType(1)) &&
838 isa<IntegerType>(MemsetType->getParamType(2))) {
839 uint64_t Len = UnknownSize;
840 if (const ConstantInt *LenCI = dyn_cast<ConstantInt>(CS.getArgument(2)))
841 Len = LenCI->getZExtValue();
842 const Value *Dest = CS.getArgument(0);
843 const Value *Src = CS.getArgument(1);
844 // If it can't overlap the source dest, then it doesn't modref the loc.
845 if (isNoAlias(Location(Dest, Len), Loc)) {
846 // Always reads 16 bytes of the source.
847 if (isNoAlias(Location(Src, 16), Loc))
849 // If it can't overlap the dest, then worst case it reads the loc.
851 // Always reads 16 bytes of the source.
852 } else if (isNoAlias(Location(Src, 16), Loc)) {
853 // If it can't overlap the source, then worst case it mutates the loc.
859 // The AliasAnalysis base class has some smarts, lets use them.
860 return ModRefResult(AliasAnalysis::getModRefInfo(CS, Loc) & Min);
863 static bool areVarIndicesEqual(SmallVectorImpl<VariableGEPIndex> &Indices1,
864 SmallVectorImpl<VariableGEPIndex> &Indices2) {
865 unsigned Size1 = Indices1.size();
866 unsigned Size2 = Indices2.size();
871 for (unsigned I = 0; I != Size1; ++I)
872 if (Indices1[I] != Indices2[I])
878 /// aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP instruction
879 /// against another pointer. We know that V1 is a GEP, but we don't know
880 /// anything about V2. UnderlyingV1 is GetUnderlyingObject(GEP1, TD),
881 /// UnderlyingV2 is the same for V2.
883 AliasAnalysis::AliasResult
884 BasicAliasAnalysis::aliasGEP(const GEPOperator *GEP1, uint64_t V1Size,
885 const MDNode *V1TBAAInfo,
886 const Value *V2, uint64_t V2Size,
887 const MDNode *V2TBAAInfo,
888 const Value *UnderlyingV1,
889 const Value *UnderlyingV2) {
890 int64_t GEP1BaseOffset;
891 SmallVector<VariableGEPIndex, 4> GEP1VariableIndices;
893 // If we have two gep instructions with must-alias or not-alias'ing base
894 // pointers, figure out if the indexes to the GEP tell us anything about the
896 if (const GEPOperator *GEP2 = dyn_cast<GEPOperator>(V2)) {
897 // Do the base pointers alias?
898 AliasResult BaseAlias = aliasCheck(UnderlyingV1, UnknownSize, 0,
899 UnderlyingV2, UnknownSize, 0);
901 // Check for geps of non-aliasing underlying pointers where the offsets are
903 if ((BaseAlias == MayAlias) && V1Size == V2Size) {
904 // Do the base pointers alias assuming type and size.
905 AliasResult PreciseBaseAlias = aliasCheck(UnderlyingV1, V1Size,
906 V1TBAAInfo, UnderlyingV2,
908 if (PreciseBaseAlias == NoAlias) {
909 // See if the computed offset from the common pointer tells us about the
910 // relation of the resulting pointer.
911 int64_t GEP2BaseOffset;
912 SmallVector<VariableGEPIndex, 4> GEP2VariableIndices;
913 const Value *GEP2BasePtr =
914 DecomposeGEPExpression(GEP2, GEP2BaseOffset, GEP2VariableIndices, TD);
915 const Value *GEP1BasePtr =
916 DecomposeGEPExpression(GEP1, GEP1BaseOffset, GEP1VariableIndices, TD);
917 // DecomposeGEPExpression and GetUnderlyingObject should return the
918 // same result except when DecomposeGEPExpression has no DataLayout.
919 if (GEP1BasePtr != UnderlyingV1 || GEP2BasePtr != UnderlyingV2) {
921 "DecomposeGEPExpression and GetUnderlyingObject disagree!");
925 if (GEP1BaseOffset == GEP2BaseOffset &&
926 areVarIndicesEqual(GEP1VariableIndices, GEP2VariableIndices))
928 GEP1VariableIndices.clear();
932 // If we get a No or May, then return it immediately, no amount of analysis
933 // will improve this situation.
934 if (BaseAlias != MustAlias) return BaseAlias;
936 // Otherwise, we have a MustAlias. Since the base pointers alias each other
937 // exactly, see if the computed offset from the common pointer tells us
938 // about the relation of the resulting pointer.
939 const Value *GEP1BasePtr =
940 DecomposeGEPExpression(GEP1, GEP1BaseOffset, GEP1VariableIndices, TD);
942 int64_t GEP2BaseOffset;
943 SmallVector<VariableGEPIndex, 4> GEP2VariableIndices;
944 const Value *GEP2BasePtr =
945 DecomposeGEPExpression(GEP2, GEP2BaseOffset, GEP2VariableIndices, TD);
947 // DecomposeGEPExpression and GetUnderlyingObject should return the
948 // same result except when DecomposeGEPExpression has no DataLayout.
949 if (GEP1BasePtr != UnderlyingV1 || GEP2BasePtr != UnderlyingV2) {
951 "DecomposeGEPExpression and GetUnderlyingObject disagree!");
955 // Subtract the GEP2 pointer from the GEP1 pointer to find out their
956 // symbolic difference.
957 GEP1BaseOffset -= GEP2BaseOffset;
958 GetIndexDifference(GEP1VariableIndices, GEP2VariableIndices);
961 // Check to see if these two pointers are related by the getelementptr
962 // instruction. If one pointer is a GEP with a non-zero index of the other
963 // pointer, we know they cannot alias.
965 // If both accesses are unknown size, we can't do anything useful here.
966 if (V1Size == UnknownSize && V2Size == UnknownSize)
969 AliasResult R = aliasCheck(UnderlyingV1, UnknownSize, 0,
970 V2, V2Size, V2TBAAInfo);
972 // If V2 may alias GEP base pointer, conservatively returns MayAlias.
973 // If V2 is known not to alias GEP base pointer, then the two values
974 // cannot alias per GEP semantics: "A pointer value formed from a
975 // getelementptr instruction is associated with the addresses associated
976 // with the first operand of the getelementptr".
979 const Value *GEP1BasePtr =
980 DecomposeGEPExpression(GEP1, GEP1BaseOffset, GEP1VariableIndices, TD);
982 // DecomposeGEPExpression and GetUnderlyingObject should return the
983 // same result except when DecomposeGEPExpression has no DataLayout.
984 if (GEP1BasePtr != UnderlyingV1) {
986 "DecomposeGEPExpression and GetUnderlyingObject disagree!");
991 // In the two GEP Case, if there is no difference in the offsets of the
992 // computed pointers, the resultant pointers are a must alias. This
993 // hapens when we have two lexically identical GEP's (for example).
995 // In the other case, if we have getelementptr <ptr>, 0, 0, 0, 0, ... and V2
996 // must aliases the GEP, the end result is a must alias also.
997 if (GEP1BaseOffset == 0 && GEP1VariableIndices.empty())
1000 // If there is a constant difference between the pointers, but the difference
1001 // is less than the size of the associated memory object, then we know
1002 // that the objects are partially overlapping. If the difference is
1003 // greater, we know they do not overlap.
1004 if (GEP1BaseOffset != 0 && GEP1VariableIndices.empty()) {
1005 if (GEP1BaseOffset >= 0) {
1006 if (V2Size != UnknownSize) {
1007 if ((uint64_t)GEP1BaseOffset < V2Size)
1008 return PartialAlias;
1012 // We have the situation where:
1015 // ---------------->|
1016 // |-->V1Size |-------> V2Size
1018 // We need to know that V2Size is not unknown, otherwise we might have
1019 // stripped a gep with negative index ('gep <ptr>, -1, ...).
1020 if (V1Size != UnknownSize && V2Size != UnknownSize) {
1021 if (-(uint64_t)GEP1BaseOffset < V1Size)
1022 return PartialAlias;
1028 // Try to distinguish something like &A[i][1] against &A[42][0].
1029 // Grab the least significant bit set in any of the scales.
1030 if (!GEP1VariableIndices.empty()) {
1031 uint64_t Modulo = 0;
1032 for (unsigned i = 0, e = GEP1VariableIndices.size(); i != e; ++i)
1033 Modulo |= (uint64_t)GEP1VariableIndices[i].Scale;
1034 Modulo = Modulo ^ (Modulo & (Modulo - 1));
1036 // We can compute the difference between the two addresses
1037 // mod Modulo. Check whether that difference guarantees that the
1038 // two locations do not alias.
1039 uint64_t ModOffset = (uint64_t)GEP1BaseOffset & (Modulo - 1);
1040 if (V1Size != UnknownSize && V2Size != UnknownSize &&
1041 ModOffset >= V2Size && V1Size <= Modulo - ModOffset)
1045 // Statically, we can see that the base objects are the same, but the
1046 // pointers have dynamic offsets which we can't resolve. And none of our
1047 // little tricks above worked.
1049 // TODO: Returning PartialAlias instead of MayAlias is a mild hack; the
1050 // practical effect of this is protecting TBAA in the case of dynamic
1051 // indices into arrays of unions or malloc'd memory.
1052 return PartialAlias;
1055 static AliasAnalysis::AliasResult
1056 MergeAliasResults(AliasAnalysis::AliasResult A, AliasAnalysis::AliasResult B) {
1057 // If the results agree, take it.
1060 // A mix of PartialAlias and MustAlias is PartialAlias.
1061 if ((A == AliasAnalysis::PartialAlias && B == AliasAnalysis::MustAlias) ||
1062 (B == AliasAnalysis::PartialAlias && A == AliasAnalysis::MustAlias))
1063 return AliasAnalysis::PartialAlias;
1064 // Otherwise, we don't know anything.
1065 return AliasAnalysis::MayAlias;
1068 /// aliasSelect - Provide a bunch of ad-hoc rules to disambiguate a Select
1069 /// instruction against another.
1070 AliasAnalysis::AliasResult
1071 BasicAliasAnalysis::aliasSelect(const SelectInst *SI, uint64_t SISize,
1072 const MDNode *SITBAAInfo,
1073 const Value *V2, uint64_t V2Size,
1074 const MDNode *V2TBAAInfo) {
1075 // If the values are Selects with the same condition, we can do a more precise
1076 // check: just check for aliases between the values on corresponding arms.
1077 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2))
1078 if (SI->getCondition() == SI2->getCondition()) {
1080 aliasCheck(SI->getTrueValue(), SISize, SITBAAInfo,
1081 SI2->getTrueValue(), V2Size, V2TBAAInfo);
1082 if (Alias == MayAlias)
1084 AliasResult ThisAlias =
1085 aliasCheck(SI->getFalseValue(), SISize, SITBAAInfo,
1086 SI2->getFalseValue(), V2Size, V2TBAAInfo);
1087 return MergeAliasResults(ThisAlias, Alias);
1090 // If both arms of the Select node NoAlias or MustAlias V2, then returns
1091 // NoAlias / MustAlias. Otherwise, returns MayAlias.
1093 aliasCheck(V2, V2Size, V2TBAAInfo, SI->getTrueValue(), SISize, SITBAAInfo);
1094 if (Alias == MayAlias)
1097 AliasResult ThisAlias =
1098 aliasCheck(V2, V2Size, V2TBAAInfo, SI->getFalseValue(), SISize, SITBAAInfo);
1099 return MergeAliasResults(ThisAlias, Alias);
1102 // aliasPHI - Provide a bunch of ad-hoc rules to disambiguate a PHI instruction
1104 AliasAnalysis::AliasResult
1105 BasicAliasAnalysis::aliasPHI(const PHINode *PN, uint64_t PNSize,
1106 const MDNode *PNTBAAInfo,
1107 const Value *V2, uint64_t V2Size,
1108 const MDNode *V2TBAAInfo) {
1109 // Track phi nodes we have visited. We use this information when we determine
1110 // value equivalence.
1111 VisitedPhiBBs.insert(PN->getParent());
1113 // If the values are PHIs in the same block, we can do a more precise
1114 // as well as efficient check: just check for aliases between the values
1115 // on corresponding edges.
1116 if (const PHINode *PN2 = dyn_cast<PHINode>(V2))
1117 if (PN2->getParent() == PN->getParent()) {
1118 LocPair Locs(Location(PN, PNSize, PNTBAAInfo),
1119 Location(V2, V2Size, V2TBAAInfo));
1121 std::swap(Locs.first, Locs.second);
1122 // Analyse the PHIs' inputs under the assumption that the PHIs are
1124 // If the PHIs are May/MustAlias there must be (recursively) an input
1125 // operand from outside the PHIs' cycle that is MayAlias/MustAlias or
1126 // there must be an operation on the PHIs within the PHIs' value cycle
1127 // that causes a MayAlias.
1128 // Pretend the phis do not alias.
1129 AliasResult Alias = NoAlias;
1130 assert(AliasCache.count(Locs) &&
1131 "There must exist an entry for the phi node");
1132 AliasResult OrigAliasResult = AliasCache[Locs];
1133 AliasCache[Locs] = NoAlias;
1135 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1136 AliasResult ThisAlias =
1137 aliasCheck(PN->getIncomingValue(i), PNSize, PNTBAAInfo,
1138 PN2->getIncomingValueForBlock(PN->getIncomingBlock(i)),
1139 V2Size, V2TBAAInfo);
1140 Alias = MergeAliasResults(ThisAlias, Alias);
1141 if (Alias == MayAlias)
1145 // Reset if speculation failed.
1146 if (Alias != NoAlias)
1147 AliasCache[Locs] = OrigAliasResult;
1152 SmallPtrSet<Value*, 4> UniqueSrc;
1153 SmallVector<Value*, 4> V1Srcs;
1154 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1155 Value *PV1 = PN->getIncomingValue(i);
1156 if (isa<PHINode>(PV1))
1157 // If any of the source itself is a PHI, return MayAlias conservatively
1158 // to avoid compile time explosion. The worst possible case is if both
1159 // sides are PHI nodes. In which case, this is O(m x n) time where 'm'
1160 // and 'n' are the number of PHI sources.
1162 if (UniqueSrc.insert(PV1))
1163 V1Srcs.push_back(PV1);
1166 AliasResult Alias = aliasCheck(V2, V2Size, V2TBAAInfo,
1167 V1Srcs[0], PNSize, PNTBAAInfo);
1168 // Early exit if the check of the first PHI source against V2 is MayAlias.
1169 // Other results are not possible.
1170 if (Alias == MayAlias)
1173 // If all sources of the PHI node NoAlias or MustAlias V2, then returns
1174 // NoAlias / MustAlias. Otherwise, returns MayAlias.
1175 for (unsigned i = 1, e = V1Srcs.size(); i != e; ++i) {
1176 Value *V = V1Srcs[i];
1178 AliasResult ThisAlias = aliasCheck(V2, V2Size, V2TBAAInfo,
1179 V, PNSize, PNTBAAInfo);
1180 Alias = MergeAliasResults(ThisAlias, Alias);
1181 if (Alias == MayAlias)
1188 // aliasCheck - Provide a bunch of ad-hoc rules to disambiguate in common cases,
1189 // such as array references.
1191 AliasAnalysis::AliasResult
1192 BasicAliasAnalysis::aliasCheck(const Value *V1, uint64_t V1Size,
1193 const MDNode *V1TBAAInfo,
1194 const Value *V2, uint64_t V2Size,
1195 const MDNode *V2TBAAInfo) {
1196 // If either of the memory references is empty, it doesn't matter what the
1197 // pointer values are.
1198 if (V1Size == 0 || V2Size == 0)
1201 // Strip off any casts if they exist.
1202 V1 = V1->stripPointerCasts();
1203 V2 = V2->stripPointerCasts();
1205 // Are we checking for alias of the same value?
1206 // Because we look 'through' phi nodes we could look at "Value" pointers from
1207 // different iterations. We must therefore make sure that this is not the
1208 // case. The function isValueEqualInPotentialCycles ensures that this cannot
1209 // happen by looking at the visited phi nodes and making sure they cannot
1211 if (isValueEqualInPotentialCycles(V1, V2))
1214 if (!V1->getType()->isPointerTy() || !V2->getType()->isPointerTy())
1215 return NoAlias; // Scalars cannot alias each other
1217 // Figure out what objects these things are pointing to if we can.
1218 const Value *O1 = GetUnderlyingObject(V1, TD);
1219 const Value *O2 = GetUnderlyingObject(V2, TD);
1221 // Null values in the default address space don't point to any object, so they
1222 // don't alias any other pointer.
1223 if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O1))
1224 if (CPN->getType()->getAddressSpace() == 0)
1226 if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O2))
1227 if (CPN->getType()->getAddressSpace() == 0)
1231 // If V1/V2 point to two different objects we know that we have no alias.
1232 if (isIdentifiedObject(O1) && isIdentifiedObject(O2))
1235 // Constant pointers can't alias with non-const isIdentifiedObject objects.
1236 if ((isa<Constant>(O1) && isIdentifiedObject(O2) && !isa<Constant>(O2)) ||
1237 (isa<Constant>(O2) && isIdentifiedObject(O1) && !isa<Constant>(O1)))
1240 // Function arguments can't alias with things that are known to be
1241 // unambigously identified at the function level.
1242 if ((isa<Argument>(O1) && isIdentifiedFunctionLocal(O2)) ||
1243 (isa<Argument>(O2) && isIdentifiedFunctionLocal(O1)))
1246 // Most objects can't alias null.
1247 if ((isa<ConstantPointerNull>(O2) && isKnownNonNull(O1)) ||
1248 (isa<ConstantPointerNull>(O1) && isKnownNonNull(O2)))
1251 // If one pointer is the result of a call/invoke or load and the other is a
1252 // non-escaping local object within the same function, then we know the
1253 // object couldn't escape to a point where the call could return it.
1255 // Note that if the pointers are in different functions, there are a
1256 // variety of complications. A call with a nocapture argument may still
1257 // temporary store the nocapture argument's value in a temporary memory
1258 // location if that memory location doesn't escape. Or it may pass a
1259 // nocapture value to other functions as long as they don't capture it.
1260 if (isEscapeSource(O1) && isNonEscapingLocalObject(O2))
1262 if (isEscapeSource(O2) && isNonEscapingLocalObject(O1))
1266 // If the size of one access is larger than the entire object on the other
1267 // side, then we know such behavior is undefined and can assume no alias.
1269 if ((V1Size != UnknownSize && isObjectSmallerThan(O2, V1Size, *TD, *TLI)) ||
1270 (V2Size != UnknownSize && isObjectSmallerThan(O1, V2Size, *TD, *TLI)))
1273 // Check the cache before climbing up use-def chains. This also terminates
1274 // otherwise infinitely recursive queries.
1275 LocPair Locs(Location(V1, V1Size, V1TBAAInfo),
1276 Location(V2, V2Size, V2TBAAInfo));
1278 std::swap(Locs.first, Locs.second);
1279 std::pair<AliasCacheTy::iterator, bool> Pair =
1280 AliasCache.insert(std::make_pair(Locs, MayAlias));
1282 return Pair.first->second;
1284 // FIXME: This isn't aggressively handling alias(GEP, PHI) for example: if the
1285 // GEP can't simplify, we don't even look at the PHI cases.
1286 if (!isa<GEPOperator>(V1) && isa<GEPOperator>(V2)) {
1288 std::swap(V1Size, V2Size);
1290 std::swap(V1TBAAInfo, V2TBAAInfo);
1292 if (const GEPOperator *GV1 = dyn_cast<GEPOperator>(V1)) {
1293 AliasResult Result = aliasGEP(GV1, V1Size, V1TBAAInfo, V2, V2Size, V2TBAAInfo, O1, O2);
1294 if (Result != MayAlias) return AliasCache[Locs] = Result;
1297 if (isa<PHINode>(V2) && !isa<PHINode>(V1)) {
1299 std::swap(V1Size, V2Size);
1300 std::swap(V1TBAAInfo, V2TBAAInfo);
1302 if (const PHINode *PN = dyn_cast<PHINode>(V1)) {
1303 AliasResult Result = aliasPHI(PN, V1Size, V1TBAAInfo,
1304 V2, V2Size, V2TBAAInfo);
1305 if (Result != MayAlias) return AliasCache[Locs] = Result;
1308 if (isa<SelectInst>(V2) && !isa<SelectInst>(V1)) {
1310 std::swap(V1Size, V2Size);
1311 std::swap(V1TBAAInfo, V2TBAAInfo);
1313 if (const SelectInst *S1 = dyn_cast<SelectInst>(V1)) {
1314 AliasResult Result = aliasSelect(S1, V1Size, V1TBAAInfo,
1315 V2, V2Size, V2TBAAInfo);
1316 if (Result != MayAlias) return AliasCache[Locs] = Result;
1319 // If both pointers are pointing into the same object and one of them
1320 // accesses is accessing the entire object, then the accesses must
1321 // overlap in some way.
1323 if ((V1Size != UnknownSize && isObjectSize(O1, V1Size, *TD, *TLI)) ||
1324 (V2Size != UnknownSize && isObjectSize(O2, V2Size, *TD, *TLI)))
1325 return AliasCache[Locs] = PartialAlias;
1327 AliasResult Result =
1328 AliasAnalysis::alias(Location(V1, V1Size, V1TBAAInfo),
1329 Location(V2, V2Size, V2TBAAInfo));
1330 return AliasCache[Locs] = Result;
1333 bool BasicAliasAnalysis::isValueEqualInPotentialCycles(const Value *V,
1338 const Instruction *Inst = dyn_cast<Instruction>(V);
1342 if (VisitedPhiBBs.size() > MaxNumPhiBBsValueReachabilityCheck)
1345 // Use dominance or loop info if available.
1346 DominatorTreeWrapperPass *DTWP =
1347 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
1348 DominatorTree *DT = DTWP ? &DTWP->getDomTree() : 0;
1349 LoopInfo *LI = getAnalysisIfAvailable<LoopInfo>();
1351 // Make sure that the visited phis cannot reach the Value. This ensures that
1352 // the Values cannot come from different iterations of a potential cycle the
1353 // phi nodes could be involved in.
1354 for (SmallPtrSet<const BasicBlock *, 8>::iterator PI = VisitedPhiBBs.begin(),
1355 PE = VisitedPhiBBs.end();
1357 if (isPotentiallyReachable((*PI)->begin(), Inst, DT, LI))
1363 /// GetIndexDifference - Dest and Src are the variable indices from two
1364 /// decomposed GetElementPtr instructions GEP1 and GEP2 which have common base
1365 /// pointers. Subtract the GEP2 indices from GEP1 to find the symbolic
1366 /// difference between the two pointers.
1367 void BasicAliasAnalysis::GetIndexDifference(
1368 SmallVectorImpl<VariableGEPIndex> &Dest,
1369 const SmallVectorImpl<VariableGEPIndex> &Src) {
1373 for (unsigned i = 0, e = Src.size(); i != e; ++i) {
1374 const Value *V = Src[i].V;
1375 ExtensionKind Extension = Src[i].Extension;
1376 int64_t Scale = Src[i].Scale;
1378 // Find V in Dest. This is N^2, but pointer indices almost never have more
1379 // than a few variable indexes.
1380 for (unsigned j = 0, e = Dest.size(); j != e; ++j) {
1381 if (!isValueEqualInPotentialCycles(Dest[j].V, V) ||
1382 Dest[j].Extension != Extension)
1385 // If we found it, subtract off Scale V's from the entry in Dest. If it
1386 // goes to zero, remove the entry.
1387 if (Dest[j].Scale != Scale)
1388 Dest[j].Scale -= Scale;
1390 Dest.erase(Dest.begin() + j);
1395 // If we didn't consume this entry, add it to the end of the Dest list.
1397 VariableGEPIndex Entry = { V, Extension, -Scale };
1398 Dest.push_back(Entry);