12754fe60SDimitry Andric //===- BasicAliasAnalysis.cpp - Stateless Alias Analysis Impl -------------===//
2f22ef01cSRoman Divacky //
3f22ef01cSRoman Divacky // The LLVM Compiler Infrastructure
4f22ef01cSRoman Divacky //
5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source
6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details.
7f22ef01cSRoman Divacky //
8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
9f22ef01cSRoman Divacky //
102754fe60SDimitry Andric // This file defines the primary stateless implementation of the
112754fe60SDimitry Andric // Alias Analysis interface that implements identities (two different
122754fe60SDimitry Andric // globals cannot alias, etc), but does no stateful analysis.
13f22ef01cSRoman Divacky //
14f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
15f22ef01cSRoman Divacky
167d523365SDimitry Andric #include "llvm/Analysis/BasicAliasAnalysis.h"
172cab237bSDimitry Andric #include "llvm/ADT/APInt.h"
182cab237bSDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
19f22ef01cSRoman Divacky #include "llvm/ADT/SmallVector.h"
207d523365SDimitry Andric #include "llvm/ADT/Statistic.h"
21139f7f9bSDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
225517e702SDimitry Andric #include "llvm/Analysis/AssumptionCache.h"
2385d60e68SDimitry Andric #include "llvm/Analysis/CFG.h"
2491bc56edSDimitry Andric #include "llvm/Analysis/CaptureTracking.h"
25139f7f9bSDimitry Andric #include "llvm/Analysis/InstructionSimplify.h"
2685d60e68SDimitry Andric #include "llvm/Analysis/LoopInfo.h"
27139f7f9bSDimitry Andric #include "llvm/Analysis/MemoryBuiltins.h"
282cab237bSDimitry Andric #include "llvm/Analysis/MemoryLocation.h"
292cab237bSDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h"
30139f7f9bSDimitry Andric #include "llvm/Analysis/ValueTracking.h"
314ba319b5SDimitry Andric #include "llvm/Analysis/PhiValues.h"
322cab237bSDimitry Andric #include "llvm/IR/Argument.h"
332cab237bSDimitry Andric #include "llvm/IR/Attributes.h"
342cab237bSDimitry Andric #include "llvm/IR/Constant.h"
35139f7f9bSDimitry Andric #include "llvm/IR/Constants.h"
36139f7f9bSDimitry Andric #include "llvm/IR/DataLayout.h"
37139f7f9bSDimitry Andric #include "llvm/IR/DerivedTypes.h"
3891bc56edSDimitry Andric #include "llvm/IR/Dominators.h"
392cab237bSDimitry Andric #include "llvm/IR/Function.h"
402cab237bSDimitry Andric #include "llvm/IR/GetElementPtrTypeIterator.h"
41139f7f9bSDimitry Andric #include "llvm/IR/GlobalAlias.h"
42139f7f9bSDimitry Andric #include "llvm/IR/GlobalVariable.h"
432cab237bSDimitry Andric #include "llvm/IR/InstrTypes.h"
442cab237bSDimitry Andric #include "llvm/IR/Instruction.h"
45139f7f9bSDimitry Andric #include "llvm/IR/Instructions.h"
46139f7f9bSDimitry Andric #include "llvm/IR/IntrinsicInst.h"
472cab237bSDimitry Andric #include "llvm/IR/Intrinsics.h"
482cab237bSDimitry Andric #include "llvm/IR/Metadata.h"
49139f7f9bSDimitry Andric #include "llvm/IR/Operator.h"
502cab237bSDimitry Andric #include "llvm/IR/Type.h"
512cab237bSDimitry Andric #include "llvm/IR/User.h"
522cab237bSDimitry Andric #include "llvm/IR/Value.h"
53139f7f9bSDimitry Andric #include "llvm/Pass.h"
542cab237bSDimitry Andric #include "llvm/Support/Casting.h"
552cab237bSDimitry Andric #include "llvm/Support/CommandLine.h"
562cab237bSDimitry Andric #include "llvm/Support/Compiler.h"
575517e702SDimitry Andric #include "llvm/Support/KnownBits.h"
582cab237bSDimitry Andric #include <cassert>
592cab237bSDimitry Andric #include <cstdint>
602cab237bSDimitry Andric #include <cstdlib>
612cab237bSDimitry Andric #include <utility>
623ca95b02SDimitry Andric
633ca95b02SDimitry Andric #define DEBUG_TYPE "basicaa"
643ca95b02SDimitry Andric
65f22ef01cSRoman Divacky using namespace llvm;
66f22ef01cSRoman Divacky
677d523365SDimitry Andric /// Enable analysis of recursive PHI nodes.
687d523365SDimitry Andric static cl::opt<bool> EnableRecPhiAnalysis("basicaa-recphi", cl::Hidden,
697d523365SDimitry Andric cl::init(false));
70*b5893f02SDimitry Andric
71*b5893f02SDimitry Andric /// By default, even on 32-bit architectures we use 64-bit integers for
72*b5893f02SDimitry Andric /// calculations. This will allow us to more-aggressively decompose indexing
73*b5893f02SDimitry Andric /// expressions calculated using i64 values (e.g., long long in C) which is
74*b5893f02SDimitry Andric /// common enough to worry about.
75*b5893f02SDimitry Andric static cl::opt<bool> ForceAtLeast64Bits("basicaa-force-at-least-64b",
76*b5893f02SDimitry Andric cl::Hidden, cl::init(true));
77*b5893f02SDimitry Andric static cl::opt<bool> DoubleCalcBits("basicaa-double-calc-bits",
78*b5893f02SDimitry Andric cl::Hidden, cl::init(false));
79*b5893f02SDimitry Andric
807d523365SDimitry Andric /// SearchLimitReached / SearchTimes shows how often the limit of
817d523365SDimitry Andric /// to decompose GEPs is reached. It will affect the precision
827d523365SDimitry Andric /// of basic alias analysis.
837d523365SDimitry Andric STATISTIC(SearchLimitReached, "Number of times the limit to "
847d523365SDimitry Andric "decompose GEPs is reached");
857d523365SDimitry Andric STATISTIC(SearchTimes, "Number of times a GEP is decomposed");
867d523365SDimitry Andric
8785d60e68SDimitry Andric /// Cutoff after which to stop analysing a set of phi nodes potentially involved
883ca95b02SDimitry Andric /// in a cycle. Because we are analysing 'through' phi nodes, we need to be
8985d60e68SDimitry Andric /// careful with value equivalence. We use reachability to make sure a value
9085d60e68SDimitry Andric /// cannot be involved in a cycle.
9185d60e68SDimitry Andric const unsigned MaxNumPhiBBsValueReachabilityCheck = 20;
9285d60e68SDimitry Andric
9391bc56edSDimitry Andric // The max limit of the search depth in DecomposeGEPExpression() and
9491bc56edSDimitry Andric // GetUnderlyingObject(), both functions need to use the same search
9591bc56edSDimitry Andric // depth otherwise the algorithm in aliasGEP will assert.
9691bc56edSDimitry Andric static const unsigned MaxLookupSearchDepth = 6;
9791bc56edSDimitry Andric
invalidate(Function & Fn,const PreservedAnalyses & PA,FunctionAnalysisManager::Invalidator & Inv)984ba319b5SDimitry Andric bool BasicAAResult::invalidate(Function &Fn, const PreservedAnalyses &PA,
99d88c1a5aSDimitry Andric FunctionAnalysisManager::Invalidator &Inv) {
100d88c1a5aSDimitry Andric // We don't care if this analysis itself is preserved, it has no state. But
101d88c1a5aSDimitry Andric // we need to check that the analyses it depends on have been. Note that we
102d88c1a5aSDimitry Andric // may be created without handles to some analyses and in that case don't
103d88c1a5aSDimitry Andric // depend on them.
1044ba319b5SDimitry Andric if (Inv.invalidate<AssumptionAnalysis>(Fn, PA) ||
1054ba319b5SDimitry Andric (DT && Inv.invalidate<DominatorTreeAnalysis>(Fn, PA)) ||
1064ba319b5SDimitry Andric (LI && Inv.invalidate<LoopAnalysis>(Fn, PA)) ||
1074ba319b5SDimitry Andric (PV && Inv.invalidate<PhiValuesAnalysis>(Fn, PA)))
108d88c1a5aSDimitry Andric return true;
109d88c1a5aSDimitry Andric
110d88c1a5aSDimitry Andric // Otherwise this analysis result remains valid.
111d88c1a5aSDimitry Andric return false;
112d88c1a5aSDimitry Andric }
113d88c1a5aSDimitry Andric
114f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
115f22ef01cSRoman Divacky // Useful predicates
116f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
117f22ef01cSRoman Divacky
1187d523365SDimitry Andric /// Returns true if the pointer is to a function-local object that never
1197d523365SDimitry Andric /// escapes from the function.
isNonEscapingLocalObject(const Value * V)120f22ef01cSRoman Divacky static bool isNonEscapingLocalObject(const Value *V) {
121f22ef01cSRoman Divacky // If this is a local allocation, check to see if it escapes.
122f22ef01cSRoman Divacky if (isa<AllocaInst>(V) || isNoAliasCall(V))
123f22ef01cSRoman Divacky // Set StoreCaptures to True so that we can assume in our callers that the
124f22ef01cSRoman Divacky // pointer is not the result of a load instruction. Currently
125f22ef01cSRoman Divacky // PointerMayBeCaptured doesn't have any special analysis for the
126f22ef01cSRoman Divacky // StoreCaptures=false case; if it did, our callers could be refined to be
127f22ef01cSRoman Divacky // more precise.
128f22ef01cSRoman Divacky return !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true);
129f22ef01cSRoman Divacky
130f22ef01cSRoman Divacky // If this is an argument that corresponds to a byval or noalias argument,
131f22ef01cSRoman Divacky // then it has not escaped before entering the function. Check if it escapes
132f22ef01cSRoman Divacky // inside the function.
133f22ef01cSRoman Divacky if (const Argument *A = dyn_cast<Argument>(V))
1343861d79fSDimitry Andric if (A->hasByValAttr() || A->hasNoAliasAttr())
1353ca95b02SDimitry Andric // Note even if the argument is marked nocapture, we still need to check
1363861d79fSDimitry Andric // for copies made inside the function. The nocapture attribute only
1373861d79fSDimitry Andric // specifies that there are no copies made that outlive the function.
138f22ef01cSRoman Divacky return !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true);
1393861d79fSDimitry Andric
140f22ef01cSRoman Divacky return false;
141f22ef01cSRoman Divacky }
142f22ef01cSRoman Divacky
1437d523365SDimitry Andric /// Returns true if the pointer is one which would have been considered an
1447d523365SDimitry Andric /// escape by isNonEscapingLocalObject.
isEscapeSource(const Value * V)145ffd1746dSEd Schouten static bool isEscapeSource(const Value *V) {
146*b5893f02SDimitry Andric if (isa<CallBase>(V))
1474ba319b5SDimitry Andric return true;
1484ba319b5SDimitry Andric
1494ba319b5SDimitry Andric if (isa<Argument>(V))
150ffd1746dSEd Schouten return true;
151ffd1746dSEd Schouten
152ffd1746dSEd Schouten // The load case works because isNonEscapingLocalObject considers all
153ffd1746dSEd Schouten // stores to be escapes (it passes true for the StoreCaptures argument
154ffd1746dSEd Schouten // to PointerMayBeCaptured).
155ffd1746dSEd Schouten if (isa<LoadInst>(V))
156ffd1746dSEd Schouten return true;
157ffd1746dSEd Schouten
158ffd1746dSEd Schouten return false;
159ffd1746dSEd Schouten }
160f22ef01cSRoman Divacky
1613ca95b02SDimitry Andric /// Returns the size of the object specified by V or UnknownSize if unknown.
getObjectSize(const Value * V,const DataLayout & DL,const TargetLibraryInfo & TLI,bool NullIsValidLoc,bool RoundToAlign=false)16291bc56edSDimitry Andric static uint64_t getObjectSize(const Value *V, const DataLayout &DL,
1633861d79fSDimitry Andric const TargetLibraryInfo &TLI,
1644ba319b5SDimitry Andric bool NullIsValidLoc,
165dff0c46cSDimitry Andric bool RoundToAlign = false) {
1667ae0e2c9SDimitry Andric uint64_t Size;
1677a7e6055SDimitry Andric ObjectSizeOpts Opts;
1687a7e6055SDimitry Andric Opts.RoundToAlign = RoundToAlign;
1694ba319b5SDimitry Andric Opts.NullIsUnknownSize = NullIsValidLoc;
1707a7e6055SDimitry Andric if (getObjectSize(V, Size, DL, &TLI, Opts))
171dff0c46cSDimitry Andric return Size;
1728f0fd8f6SDimitry Andric return MemoryLocation::UnknownSize;
173f22ef01cSRoman Divacky }
174f22ef01cSRoman Divacky
1757d523365SDimitry Andric /// Returns true if we can prove that the object specified by V is smaller than
1767d523365SDimitry Andric /// Size.
isObjectSmallerThan(const Value * V,uint64_t Size,const DataLayout & DL,const TargetLibraryInfo & TLI,bool NullIsValidLoc)1772754fe60SDimitry Andric static bool isObjectSmallerThan(const Value *V, uint64_t Size,
17891bc56edSDimitry Andric const DataLayout &DL,
1794ba319b5SDimitry Andric const TargetLibraryInfo &TLI,
1804ba319b5SDimitry Andric bool NullIsValidLoc) {
181284c1978SDimitry Andric // Note that the meanings of the "object" are slightly different in the
182284c1978SDimitry Andric // following contexts:
183284c1978SDimitry Andric // c1: llvm::getObjectSize()
184284c1978SDimitry Andric // c2: llvm.objectsize() intrinsic
185284c1978SDimitry Andric // c3: isObjectSmallerThan()
186284c1978SDimitry Andric // c1 and c2 share the same meaning; however, the meaning of "object" in c3
187284c1978SDimitry Andric // refers to the "entire object".
188284c1978SDimitry Andric //
189284c1978SDimitry Andric // Consider this example:
190284c1978SDimitry Andric // char *p = (char*)malloc(100)
191284c1978SDimitry Andric // char *q = p+80;
192284c1978SDimitry Andric //
193284c1978SDimitry Andric // In the context of c1 and c2, the "object" pointed by q refers to the
194284c1978SDimitry Andric // stretch of memory of q[0:19]. So, getObjectSize(q) should return 20.
195284c1978SDimitry Andric //
196284c1978SDimitry Andric // However, in the context of c3, the "object" refers to the chunk of memory
197284c1978SDimitry Andric // being allocated. So, the "object" has 100 bytes, and q points to the middle
198284c1978SDimitry Andric // the "object". In case q is passed to isObjectSmallerThan() as the 1st
199284c1978SDimitry Andric // parameter, before the llvm::getObjectSize() is called to get the size of
200284c1978SDimitry Andric // entire object, we should:
201284c1978SDimitry Andric // - either rewind the pointer q to the base-address of the object in
202284c1978SDimitry Andric // question (in this case rewind to p), or
203284c1978SDimitry Andric // - just give up. It is up to caller to make sure the pointer is pointing
204284c1978SDimitry Andric // to the base address the object.
205284c1978SDimitry Andric //
206284c1978SDimitry Andric // We go for 2nd option for simplicity.
207284c1978SDimitry Andric if (!isIdentifiedObject(V))
208284c1978SDimitry Andric return false;
209284c1978SDimitry Andric
210dff0c46cSDimitry Andric // This function needs to use the aligned object size because we allow
211dff0c46cSDimitry Andric // reads a bit past the end given sufficient alignment.
2124ba319b5SDimitry Andric uint64_t ObjectSize = getObjectSize(V, DL, TLI, NullIsValidLoc,
2134ba319b5SDimitry Andric /*RoundToAlign*/ true);
214dff0c46cSDimitry Andric
2158f0fd8f6SDimitry Andric return ObjectSize != MemoryLocation::UnknownSize && ObjectSize < Size;
216f22ef01cSRoman Divacky }
217f22ef01cSRoman Divacky
2187d523365SDimitry Andric /// Returns true if we can prove that the object specified by V has size Size.
isObjectSize(const Value * V,uint64_t Size,const DataLayout & DL,const TargetLibraryInfo & TLI,bool NullIsValidLoc)2197d523365SDimitry Andric static bool isObjectSize(const Value *V, uint64_t Size, const DataLayout &DL,
2204ba319b5SDimitry Andric const TargetLibraryInfo &TLI, bool NullIsValidLoc) {
2214ba319b5SDimitry Andric uint64_t ObjectSize = getObjectSize(V, DL, TLI, NullIsValidLoc);
2228f0fd8f6SDimitry Andric return ObjectSize != MemoryLocation::UnknownSize && ObjectSize == Size;
223f22ef01cSRoman Divacky }
224f22ef01cSRoman Divacky
225f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
226e580952dSDimitry Andric // GetElementPtr Instruction Decomposition and Analysis
227e580952dSDimitry Andric //===----------------------------------------------------------------------===//
228e580952dSDimitry Andric
2297d523365SDimitry Andric /// Analyzes the specified value as a linear expression: "A*V + B", where A and
2307d523365SDimitry Andric /// B are constant integers.
2317d523365SDimitry Andric ///
2327d523365SDimitry Andric /// Returns the scale and offset values as APInts and return V as a Value*, and
2337d523365SDimitry Andric /// return whether we looked through any sign or zero extends. The incoming
2343ca95b02SDimitry Andric /// Value is known to have IntegerType, and it may already be sign or zero
2357d523365SDimitry Andric /// extended.
236e580952dSDimitry Andric ///
237e580952dSDimitry Andric /// Note that this looks through extends, so the high bits may not be
238e580952dSDimitry Andric /// represented in the result.
GetLinearExpression(const Value * V,APInt & Scale,APInt & Offset,unsigned & ZExtBits,unsigned & SExtBits,const DataLayout & DL,unsigned Depth,AssumptionCache * AC,DominatorTree * DT,bool & NSW,bool & NUW)2397d523365SDimitry Andric /*static*/ const Value *BasicAAResult::GetLinearExpression(
2407d523365SDimitry Andric const Value *V, APInt &Scale, APInt &Offset, unsigned &ZExtBits,
2417d523365SDimitry Andric unsigned &SExtBits, const DataLayout &DL, unsigned Depth,
2427d523365SDimitry Andric AssumptionCache *AC, DominatorTree *DT, bool &NSW, bool &NUW) {
243e580952dSDimitry Andric assert(V->getType()->isIntegerTy() && "Not an integer value");
244e580952dSDimitry Andric
245e580952dSDimitry Andric // Limit our recursion depth.
246e580952dSDimitry Andric if (Depth == 6) {
247e580952dSDimitry Andric Scale = 1;
248e580952dSDimitry Andric Offset = 0;
249e580952dSDimitry Andric return V;
250e580952dSDimitry Andric }
251e580952dSDimitry Andric
2527d523365SDimitry Andric if (const ConstantInt *Const = dyn_cast<ConstantInt>(V)) {
2533ca95b02SDimitry Andric // If it's a constant, just convert it to an offset and remove the variable.
2543ca95b02SDimitry Andric // If we've been called recursively, the Offset bit width will be greater
2557d523365SDimitry Andric // than the constant's (the Offset's always as wide as the outermost call),
2567d523365SDimitry Andric // so we'll zext here and process any extension in the isa<SExtInst> &
2577d523365SDimitry Andric // isa<ZExtInst> cases below.
2587d523365SDimitry Andric Offset += Const->getValue().zextOrSelf(Offset.getBitWidth());
2597d523365SDimitry Andric assert(Scale == 0 && "Constant values don't have a scale");
2607d523365SDimitry Andric return V;
2617d523365SDimitry Andric }
2627d523365SDimitry Andric
2637d523365SDimitry Andric if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(V)) {
264e580952dSDimitry Andric if (ConstantInt *RHSC = dyn_cast<ConstantInt>(BOp->getOperand(1))) {
2653ca95b02SDimitry Andric // If we've been called recursively, then Offset and Scale will be wider
2663ca95b02SDimitry Andric // than the BOp operands. We'll always zext it here as we'll process sign
2677d523365SDimitry Andric // extensions below (see the isa<SExtInst> / isa<ZExtInst> cases).
2687d523365SDimitry Andric APInt RHS = RHSC->getValue().zextOrSelf(Offset.getBitWidth());
2697d523365SDimitry Andric
270e580952dSDimitry Andric switch (BOp->getOpcode()) {
2717d523365SDimitry Andric default:
2727d523365SDimitry Andric // We don't understand this instruction, so we can't decompose it any
2737d523365SDimitry Andric // further.
2747d523365SDimitry Andric Scale = 1;
2757d523365SDimitry Andric Offset = 0;
2767d523365SDimitry Andric return V;
277e580952dSDimitry Andric case Instruction::Or:
278e580952dSDimitry Andric // X|C == X+C if all the bits in C are unset in X. Otherwise we can't
279e580952dSDimitry Andric // analyze it.
280ff0cc061SDimitry Andric if (!MaskedValueIsZero(BOp->getOperand(0), RHSC->getValue(), DL, 0, AC,
2817d523365SDimitry Andric BOp, DT)) {
2827d523365SDimitry Andric Scale = 1;
2837d523365SDimitry Andric Offset = 0;
284e580952dSDimitry Andric return V;
285e580952dSDimitry Andric }
286d88c1a5aSDimitry Andric LLVM_FALLTHROUGH;
2877d523365SDimitry Andric case Instruction::Add:
2887d523365SDimitry Andric V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, ZExtBits,
2897d523365SDimitry Andric SExtBits, DL, Depth + 1, AC, DT, NSW, NUW);
2907d523365SDimitry Andric Offset += RHS;
2917d523365SDimitry Andric break;
2927d523365SDimitry Andric case Instruction::Sub:
2937d523365SDimitry Andric V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, ZExtBits,
2947d523365SDimitry Andric SExtBits, DL, Depth + 1, AC, DT, NSW, NUW);
2957d523365SDimitry Andric Offset -= RHS;
2967d523365SDimitry Andric break;
2977d523365SDimitry Andric case Instruction::Mul:
2987d523365SDimitry Andric V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, ZExtBits,
2997d523365SDimitry Andric SExtBits, DL, Depth + 1, AC, DT, NSW, NUW);
3007d523365SDimitry Andric Offset *= RHS;
3017d523365SDimitry Andric Scale *= RHS;
3027d523365SDimitry Andric break;
3037d523365SDimitry Andric case Instruction::Shl:
3047d523365SDimitry Andric V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, ZExtBits,
3057d523365SDimitry Andric SExtBits, DL, Depth + 1, AC, DT, NSW, NUW);
3064ba319b5SDimitry Andric
3074ba319b5SDimitry Andric // We're trying to linearize an expression of the kind:
3084ba319b5SDimitry Andric // shl i8 -128, 36
3094ba319b5SDimitry Andric // where the shift count exceeds the bitwidth of the type.
3104ba319b5SDimitry Andric // We can't decompose this further (the expression would return
3114ba319b5SDimitry Andric // a poison value).
3124ba319b5SDimitry Andric if (Offset.getBitWidth() < RHS.getLimitedValue() ||
3134ba319b5SDimitry Andric Scale.getBitWidth() < RHS.getLimitedValue()) {
3144ba319b5SDimitry Andric Scale = 1;
3154ba319b5SDimitry Andric Offset = 0;
3164ba319b5SDimitry Andric return V;
3174ba319b5SDimitry Andric }
3184ba319b5SDimitry Andric
3197d523365SDimitry Andric Offset <<= RHS.getLimitedValue();
3207d523365SDimitry Andric Scale <<= RHS.getLimitedValue();
3217d523365SDimitry Andric // the semantics of nsw and nuw for left shifts don't match those of
3227d523365SDimitry Andric // multiplications, so we won't propagate them.
3237d523365SDimitry Andric NSW = NUW = false;
3247d523365SDimitry Andric return V;
3257d523365SDimitry Andric }
3267d523365SDimitry Andric
3277d523365SDimitry Andric if (isa<OverflowingBinaryOperator>(BOp)) {
3287d523365SDimitry Andric NUW &= BOp->hasNoUnsignedWrap();
3297d523365SDimitry Andric NSW &= BOp->hasNoSignedWrap();
3307d523365SDimitry Andric }
3317d523365SDimitry Andric return V;
332e580952dSDimitry Andric }
333e580952dSDimitry Andric }
334e580952dSDimitry Andric
335e580952dSDimitry Andric // Since GEP indices are sign extended anyway, we don't care about the high
336e580952dSDimitry Andric // bits of a sign or zero extended value - just scales and offsets. The
337e580952dSDimitry Andric // extensions have to be consistent though.
3387d523365SDimitry Andric if (isa<SExtInst>(V) || isa<ZExtInst>(V)) {
339e580952dSDimitry Andric Value *CastOp = cast<CastInst>(V)->getOperand(0);
3407d523365SDimitry Andric unsigned NewWidth = V->getType()->getPrimitiveSizeInBits();
341e580952dSDimitry Andric unsigned SmallWidth = CastOp->getType()->getPrimitiveSizeInBits();
3427d523365SDimitry Andric unsigned OldZExtBits = ZExtBits, OldSExtBits = SExtBits;
3437d523365SDimitry Andric const Value *Result =
3447d523365SDimitry Andric GetLinearExpression(CastOp, Scale, Offset, ZExtBits, SExtBits, DL,
3457d523365SDimitry Andric Depth + 1, AC, DT, NSW, NUW);
346e580952dSDimitry Andric
347d88c1a5aSDimitry Andric // zext(zext(%x)) == zext(%x), and similarly for sext; we'll handle this
3487d523365SDimitry Andric // by just incrementing the number of bits we've extended by.
3497d523365SDimitry Andric unsigned ExtendedBy = NewWidth - SmallWidth;
3507d523365SDimitry Andric
3517d523365SDimitry Andric if (isa<SExtInst>(V) && ZExtBits == 0) {
3527d523365SDimitry Andric // sext(sext(%x, a), b) == sext(%x, a + b)
3537d523365SDimitry Andric
3547d523365SDimitry Andric if (NSW) {
3557d523365SDimitry Andric // We haven't sign-wrapped, so it's valid to decompose sext(%x + c)
3567d523365SDimitry Andric // into sext(%x) + sext(c). We'll sext the Offset ourselves:
3577d523365SDimitry Andric unsigned OldWidth = Offset.getBitWidth();
3587d523365SDimitry Andric Offset = Offset.trunc(SmallWidth).sext(NewWidth).zextOrSelf(OldWidth);
3597d523365SDimitry Andric } else {
3607d523365SDimitry Andric // We may have signed-wrapped, so don't decompose sext(%x + c) into
3617d523365SDimitry Andric // sext(%x) + sext(c)
3627d523365SDimitry Andric Scale = 1;
3637d523365SDimitry Andric Offset = 0;
3647d523365SDimitry Andric Result = CastOp;
3657d523365SDimitry Andric ZExtBits = OldZExtBits;
3667d523365SDimitry Andric SExtBits = OldSExtBits;
3677d523365SDimitry Andric }
3687d523365SDimitry Andric SExtBits += ExtendedBy;
3697d523365SDimitry Andric } else {
3707d523365SDimitry Andric // sext(zext(%x, a), b) = zext(zext(%x, a), b) = zext(%x, a + b)
3717d523365SDimitry Andric
3727d523365SDimitry Andric if (!NUW) {
3737d523365SDimitry Andric // We may have unsigned-wrapped, so don't decompose zext(%x + c) into
3747d523365SDimitry Andric // zext(%x) + zext(c)
3757d523365SDimitry Andric Scale = 1;
3767d523365SDimitry Andric Offset = 0;
3777d523365SDimitry Andric Result = CastOp;
3787d523365SDimitry Andric ZExtBits = OldZExtBits;
3797d523365SDimitry Andric SExtBits = OldSExtBits;
3807d523365SDimitry Andric }
3817d523365SDimitry Andric ZExtBits += ExtendedBy;
3827d523365SDimitry Andric }
383e580952dSDimitry Andric
384e580952dSDimitry Andric return Result;
385e580952dSDimitry Andric }
386e580952dSDimitry Andric
387e580952dSDimitry Andric Scale = 1;
388e580952dSDimitry Andric Offset = 0;
389e580952dSDimitry Andric return V;
390e580952dSDimitry Andric }
391e580952dSDimitry Andric
3923ca95b02SDimitry Andric /// To ensure a pointer offset fits in an integer of size PointerSize
393*b5893f02SDimitry Andric /// (in bits) when that size is smaller than the maximum pointer size. This is
394*b5893f02SDimitry Andric /// an issue, for example, in particular for 32b pointers with negative indices
395*b5893f02SDimitry Andric /// that rely on two's complement wrap-arounds for precise alias information
396*b5893f02SDimitry Andric /// where the maximum pointer size is 64b.
adjustToPointerSize(APInt Offset,unsigned PointerSize)397*b5893f02SDimitry Andric static APInt adjustToPointerSize(APInt Offset, unsigned PointerSize) {
398*b5893f02SDimitry Andric assert(PointerSize <= Offset.getBitWidth() && "Invalid PointerSize!");
399*b5893f02SDimitry Andric unsigned ShiftBits = Offset.getBitWidth() - PointerSize;
400*b5893f02SDimitry Andric return (Offset << ShiftBits).ashr(ShiftBits);
401*b5893f02SDimitry Andric }
402*b5893f02SDimitry Andric
getMaxPointerSize(const DataLayout & DL)403*b5893f02SDimitry Andric static unsigned getMaxPointerSize(const DataLayout &DL) {
404*b5893f02SDimitry Andric unsigned MaxPointerSize = DL.getMaxPointerSizeInBits();
405*b5893f02SDimitry Andric if (MaxPointerSize < 64 && ForceAtLeast64Bits) MaxPointerSize = 64;
406*b5893f02SDimitry Andric if (DoubleCalcBits) MaxPointerSize *= 2;
407*b5893f02SDimitry Andric
408*b5893f02SDimitry Andric return MaxPointerSize;
4093ca95b02SDimitry Andric }
4103ca95b02SDimitry Andric
4117d523365SDimitry Andric /// If V is a symbolic pointer expression, decompose it into a base pointer
4127d523365SDimitry Andric /// with a constant offset and a number of scaled symbolic offsets.
413e580952dSDimitry Andric ///
4147d523365SDimitry Andric /// The scaled symbolic offsets (represented by pairs of a Value* and a scale
4157d523365SDimitry Andric /// in the VarIndices vector) are Value*'s that are known to be scaled by the
4167d523365SDimitry Andric /// specified amount, but which may have other unrepresented high bits. As
4177d523365SDimitry Andric /// such, the gep cannot necessarily be reconstructed from its decomposed form.
418e580952dSDimitry Andric ///
4193861d79fSDimitry Andric /// When DataLayout is around, this function is capable of analyzing everything
42091bc56edSDimitry Andric /// that GetUnderlyingObject can look through. To be able to do that
42191bc56edSDimitry Andric /// GetUnderlyingObject and DecomposeGEPExpression must use the same search
4227d523365SDimitry Andric /// depth (MaxLookupSearchDepth). When DataLayout not is around, it just looks
4237d523365SDimitry Andric /// through pointer casts.
DecomposeGEPExpression(const Value * V,DecomposedGEP & Decomposed,const DataLayout & DL,AssumptionCache * AC,DominatorTree * DT)4243ca95b02SDimitry Andric bool BasicAAResult::DecomposeGEPExpression(const Value *V,
4253ca95b02SDimitry Andric DecomposedGEP &Decomposed, const DataLayout &DL, AssumptionCache *AC,
4263ca95b02SDimitry Andric DominatorTree *DT) {
427e580952dSDimitry Andric // Limit recursion depth to limit compile time in crazy cases.
42891bc56edSDimitry Andric unsigned MaxLookup = MaxLookupSearchDepth;
4297d523365SDimitry Andric SearchTimes++;
430e580952dSDimitry Andric
431*b5893f02SDimitry Andric unsigned MaxPointerSize = getMaxPointerSize(DL);
4323ca95b02SDimitry Andric Decomposed.VarIndices.clear();
433e580952dSDimitry Andric do {
434e580952dSDimitry Andric // See if this is a bitcast or GEP.
435e580952dSDimitry Andric const Operator *Op = dyn_cast<Operator>(V);
43691bc56edSDimitry Andric if (!Op) {
437e580952dSDimitry Andric // The only non-operator case we can handle are GlobalAliases.
438e580952dSDimitry Andric if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
4393ca95b02SDimitry Andric if (!GA->isInterposable()) {
440e580952dSDimitry Andric V = GA->getAliasee();
441e580952dSDimitry Andric continue;
442e580952dSDimitry Andric }
443e580952dSDimitry Andric }
4443ca95b02SDimitry Andric Decomposed.Base = V;
4453ca95b02SDimitry Andric return false;
446e580952dSDimitry Andric }
447e580952dSDimitry Andric
44891bc56edSDimitry Andric if (Op->getOpcode() == Instruction::BitCast ||
44991bc56edSDimitry Andric Op->getOpcode() == Instruction::AddrSpaceCast) {
450e580952dSDimitry Andric V = Op->getOperand(0);
451e580952dSDimitry Andric continue;
452e580952dSDimitry Andric }
453e580952dSDimitry Andric
454bd5abe19SDimitry Andric const GEPOperator *GEPOp = dyn_cast<GEPOperator>(Op);
45591bc56edSDimitry Andric if (!GEPOp) {
456*b5893f02SDimitry Andric if (const auto *Call = dyn_cast<CallBase>(V)) {
4574ba319b5SDimitry Andric // CaptureTracking can know about special capturing properties of some
4584ba319b5SDimitry Andric // intrinsics like launder.invariant.group, that can't be expressed with
4594ba319b5SDimitry Andric // the attributes, but have properties like returning aliasing pointer.
4604ba319b5SDimitry Andric // Because some analysis may assume that nocaptured pointer is not
4614ba319b5SDimitry Andric // returned from some special intrinsic (because function would have to
4624ba319b5SDimitry Andric // be marked with returns attribute), it is crucial to use this function
4634ba319b5SDimitry Andric // because it should be in sync with CaptureTracking. Not using it may
4644ba319b5SDimitry Andric // cause weird miscompilations where 2 aliasing pointers are assumed to
4654ba319b5SDimitry Andric // noalias.
466*b5893f02SDimitry Andric if (auto *RP = getArgumentAliasingToReturnedPointer(Call)) {
4674ba319b5SDimitry Andric V = RP;
4683ca95b02SDimitry Andric continue;
4693ca95b02SDimitry Andric }
4704ba319b5SDimitry Andric }
4713ca95b02SDimitry Andric
472bd5abe19SDimitry Andric // If it's not a GEP, hand it off to SimplifyInstruction to see if it
473bd5abe19SDimitry Andric // can come up with something. This matches what GetUnderlyingObject does.
4742754fe60SDimitry Andric if (const Instruction *I = dyn_cast<Instruction>(V))
47539d628a0SDimitry Andric // TODO: Get a DominatorTree and AssumptionCache and use them here
47639d628a0SDimitry Andric // (these are both now available in this function, but this should be
47739d628a0SDimitry Andric // updated when GetUnderlyingObject is updated). TLI should be
47839d628a0SDimitry Andric // provided also.
4792754fe60SDimitry Andric if (const Value *Simplified =
48091bc56edSDimitry Andric SimplifyInstruction(const_cast<Instruction *>(I), DL)) {
4812754fe60SDimitry Andric V = Simplified;
4822754fe60SDimitry Andric continue;
4832754fe60SDimitry Andric }
4842754fe60SDimitry Andric
4853ca95b02SDimitry Andric Decomposed.Base = V;
4863ca95b02SDimitry Andric return false;
487bd5abe19SDimitry Andric }
488e580952dSDimitry Andric
489e580952dSDimitry Andric // Don't attempt to analyze GEPs over unsized objects.
4903ca95b02SDimitry Andric if (!GEPOp->getSourceElementType()->isSized()) {
4913ca95b02SDimitry Andric Decomposed.Base = V;
4923ca95b02SDimitry Andric return false;
4933ca95b02SDimitry Andric }
494e580952dSDimitry Andric
495f785676fSDimitry Andric unsigned AS = GEPOp->getPointerAddressSpace();
496e580952dSDimitry Andric // Walk the indices of the GEP, accumulating them into BaseOff/VarIndices.
497e580952dSDimitry Andric gep_type_iterator GTI = gep_type_begin(GEPOp);
4983ca95b02SDimitry Andric unsigned PointerSize = DL.getPointerSizeInBits(AS);
499d88c1a5aSDimitry Andric // Assume all GEP operands are constants until proven otherwise.
500d88c1a5aSDimitry Andric bool GepHasConstantOffset = true;
5017d523365SDimitry Andric for (User::const_op_iterator I = GEPOp->op_begin() + 1, E = GEPOp->op_end();
502d88c1a5aSDimitry Andric I != E; ++I, ++GTI) {
5037d523365SDimitry Andric const Value *Index = *I;
504e580952dSDimitry Andric // Compute the (potentially symbolic) offset in bytes for this index.
505d88c1a5aSDimitry Andric if (StructType *STy = GTI.getStructTypeOrNull()) {
506e580952dSDimitry Andric // For a struct, add the member offset.
507e580952dSDimitry Andric unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue();
5087d523365SDimitry Andric if (FieldNo == 0)
5097d523365SDimitry Andric continue;
510e580952dSDimitry Andric
5113ca95b02SDimitry Andric Decomposed.StructOffset +=
5123ca95b02SDimitry Andric DL.getStructLayout(STy)->getElementOffset(FieldNo);
513e580952dSDimitry Andric continue;
514e580952dSDimitry Andric }
515e580952dSDimitry Andric
516e580952dSDimitry Andric // For an array/pointer, add the element offset, explicitly scaled.
5177d523365SDimitry Andric if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Index)) {
5187d523365SDimitry Andric if (CIdx->isZero())
5197d523365SDimitry Andric continue;
5203ca95b02SDimitry Andric Decomposed.OtherOffset +=
521*b5893f02SDimitry Andric (DL.getTypeAllocSize(GTI.getIndexedType()) *
522*b5893f02SDimitry Andric CIdx->getValue().sextOrSelf(MaxPointerSize))
523*b5893f02SDimitry Andric .sextOrTrunc(MaxPointerSize);
524e580952dSDimitry Andric continue;
525e580952dSDimitry Andric }
526e580952dSDimitry Andric
527d88c1a5aSDimitry Andric GepHasConstantOffset = false;
528d88c1a5aSDimitry Andric
529*b5893f02SDimitry Andric APInt Scale(MaxPointerSize, DL.getTypeAllocSize(GTI.getIndexedType()));
5307d523365SDimitry Andric unsigned ZExtBits = 0, SExtBits = 0;
531e580952dSDimitry Andric
532e580952dSDimitry Andric // If the integer type is smaller than the pointer size, it is implicitly
533e580952dSDimitry Andric // sign extended to pointer size.
534f785676fSDimitry Andric unsigned Width = Index->getType()->getIntegerBitWidth();
5357d523365SDimitry Andric if (PointerSize > Width)
5367d523365SDimitry Andric SExtBits += PointerSize - Width;
537e580952dSDimitry Andric
538e580952dSDimitry Andric // Use GetLinearExpression to decompose the index into a C1*V+C2 form.
539e580952dSDimitry Andric APInt IndexScale(Width, 0), IndexOffset(Width, 0);
5407d523365SDimitry Andric bool NSW = true, NUW = true;
541*b5893f02SDimitry Andric const Value *OrigIndex = Index;
5427d523365SDimitry Andric Index = GetLinearExpression(Index, IndexScale, IndexOffset, ZExtBits,
5437d523365SDimitry Andric SExtBits, DL, 0, AC, DT, NSW, NUW);
544e580952dSDimitry Andric
545e580952dSDimitry Andric // The GEP index scale ("Scale") scales C1*V+C2, yielding (C1*V+C2)*Scale.
546e580952dSDimitry Andric // This gives us an aggregate computation of (C1*Scale)*V + C2*Scale.
547*b5893f02SDimitry Andric
548*b5893f02SDimitry Andric // It can be the case that, even through C1*V+C2 does not overflow for
549*b5893f02SDimitry Andric // relevant values of V, (C2*Scale) can overflow. In that case, we cannot
550*b5893f02SDimitry Andric // decompose the expression in this way.
551*b5893f02SDimitry Andric //
552*b5893f02SDimitry Andric // FIXME: C1*Scale and the other operations in the decomposed
553*b5893f02SDimitry Andric // (C1*Scale)*V+C2*Scale can also overflow. We should check for this
554*b5893f02SDimitry Andric // possibility.
555*b5893f02SDimitry Andric APInt WideScaledOffset = IndexOffset.sextOrTrunc(MaxPointerSize*2) *
556*b5893f02SDimitry Andric Scale.sext(MaxPointerSize*2);
557*b5893f02SDimitry Andric if (WideScaledOffset.getMinSignedBits() > MaxPointerSize) {
558*b5893f02SDimitry Andric Index = OrigIndex;
559*b5893f02SDimitry Andric IndexScale = 1;
560*b5893f02SDimitry Andric IndexOffset = 0;
561*b5893f02SDimitry Andric
562*b5893f02SDimitry Andric ZExtBits = SExtBits = 0;
563*b5893f02SDimitry Andric if (PointerSize > Width)
564*b5893f02SDimitry Andric SExtBits += PointerSize - Width;
565*b5893f02SDimitry Andric } else {
566*b5893f02SDimitry Andric Decomposed.OtherOffset += IndexOffset.sextOrTrunc(MaxPointerSize) * Scale;
567*b5893f02SDimitry Andric Scale *= IndexScale.sextOrTrunc(MaxPointerSize);
568*b5893f02SDimitry Andric }
569e580952dSDimitry Andric
5703b0f4066SDimitry Andric // If we already had an occurrence of this index variable, merge this
571e580952dSDimitry Andric // scale into it. For example, we want to handle:
572e580952dSDimitry Andric // A[x][x] -> x*16 + x*4 -> x*20
573e580952dSDimitry Andric // This also ensures that 'x' only appears in the index list once.
5743ca95b02SDimitry Andric for (unsigned i = 0, e = Decomposed.VarIndices.size(); i != e; ++i) {
5753ca95b02SDimitry Andric if (Decomposed.VarIndices[i].V == Index &&
5763ca95b02SDimitry Andric Decomposed.VarIndices[i].ZExtBits == ZExtBits &&
5773ca95b02SDimitry Andric Decomposed.VarIndices[i].SExtBits == SExtBits) {
5783ca95b02SDimitry Andric Scale += Decomposed.VarIndices[i].Scale;
5793ca95b02SDimitry Andric Decomposed.VarIndices.erase(Decomposed.VarIndices.begin() + i);
580e580952dSDimitry Andric break;
581e580952dSDimitry Andric }
582e580952dSDimitry Andric }
583e580952dSDimitry Andric
584e580952dSDimitry Andric // Make sure that we have a scale that makes sense for this target's
585e580952dSDimitry Andric // pointer size.
5863ca95b02SDimitry Andric Scale = adjustToPointerSize(Scale, PointerSize);
587e580952dSDimitry Andric
588*b5893f02SDimitry Andric if (!!Scale) {
589*b5893f02SDimitry Andric VariableGEPIndex Entry = {Index, ZExtBits, SExtBits, Scale};
5903ca95b02SDimitry Andric Decomposed.VarIndices.push_back(Entry);
591e580952dSDimitry Andric }
592e580952dSDimitry Andric }
593e580952dSDimitry Andric
5943ca95b02SDimitry Andric // Take care of wrap-arounds
595d88c1a5aSDimitry Andric if (GepHasConstantOffset) {
5963ca95b02SDimitry Andric Decomposed.StructOffset =
5973ca95b02SDimitry Andric adjustToPointerSize(Decomposed.StructOffset, PointerSize);
5983ca95b02SDimitry Andric Decomposed.OtherOffset =
5993ca95b02SDimitry Andric adjustToPointerSize(Decomposed.OtherOffset, PointerSize);
600d88c1a5aSDimitry Andric }
6013ca95b02SDimitry Andric
602e580952dSDimitry Andric // Analyze the base pointer next.
603e580952dSDimitry Andric V = GEPOp->getOperand(0);
604e580952dSDimitry Andric } while (--MaxLookup);
605e580952dSDimitry Andric
606e580952dSDimitry Andric // If the chain of expressions is too deep, just return early.
6073ca95b02SDimitry Andric Decomposed.Base = V;
6087d523365SDimitry Andric SearchLimitReached++;
6093ca95b02SDimitry Andric return true;
610e580952dSDimitry Andric }
611e580952dSDimitry Andric
6127d523365SDimitry Andric /// Returns whether the given pointer value points to memory that is local to
6137d523365SDimitry Andric /// the function, with global constants being considered local to all
6147d523365SDimitry Andric /// functions.
pointsToConstantMemory(const MemoryLocation & Loc,bool OrLocal)6157d523365SDimitry Andric bool BasicAAResult::pointsToConstantMemory(const MemoryLocation &Loc,
6168f0fd8f6SDimitry Andric bool OrLocal) {
6172754fe60SDimitry Andric assert(Visited.empty() && "Visited must be cleared after use!");
618f22ef01cSRoman Divacky
6192754fe60SDimitry Andric unsigned MaxLookup = 8;
6202754fe60SDimitry Andric SmallVector<const Value *, 16> Worklist;
6212754fe60SDimitry Andric Worklist.push_back(Loc.Ptr);
6222754fe60SDimitry Andric do {
6237d523365SDimitry Andric const Value *V = GetUnderlyingObject(Worklist.pop_back_val(), DL);
62439d628a0SDimitry Andric if (!Visited.insert(V).second) {
6252754fe60SDimitry Andric Visited.clear();
6267d523365SDimitry Andric return AAResultBase::pointsToConstantMemory(Loc, OrLocal);
6272754fe60SDimitry Andric }
6282754fe60SDimitry Andric
6292754fe60SDimitry Andric // An alloca instruction defines local memory.
6302754fe60SDimitry Andric if (OrLocal && isa<AllocaInst>(V))
6312754fe60SDimitry Andric continue;
6322754fe60SDimitry Andric
6332754fe60SDimitry Andric // A global constant counts as local memory for our purposes.
6342754fe60SDimitry Andric if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
635f22ef01cSRoman Divacky // Note: this doesn't require GV to be "ODR" because it isn't legal for a
6362754fe60SDimitry Andric // global to be marked constant in some modules and non-constant in
6372754fe60SDimitry Andric // others. GV may even be a declaration, not a definition.
6382754fe60SDimitry Andric if (!GV->isConstant()) {
6392754fe60SDimitry Andric Visited.clear();
6407d523365SDimitry Andric return AAResultBase::pointsToConstantMemory(Loc, OrLocal);
6412754fe60SDimitry Andric }
6422754fe60SDimitry Andric continue;
6432754fe60SDimitry Andric }
644e580952dSDimitry Andric
6452754fe60SDimitry Andric // If both select values point to local memory, then so does the select.
6462754fe60SDimitry Andric if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
6472754fe60SDimitry Andric Worklist.push_back(SI->getTrueValue());
6482754fe60SDimitry Andric Worklist.push_back(SI->getFalseValue());
6492754fe60SDimitry Andric continue;
6502754fe60SDimitry Andric }
6512754fe60SDimitry Andric
6522754fe60SDimitry Andric // If all values incoming to a phi node point to local memory, then so does
6532754fe60SDimitry Andric // the phi.
6542754fe60SDimitry Andric if (const PHINode *PN = dyn_cast<PHINode>(V)) {
6552754fe60SDimitry Andric // Don't bother inspecting phi nodes with many operands.
6562754fe60SDimitry Andric if (PN->getNumIncomingValues() > MaxLookup) {
6572754fe60SDimitry Andric Visited.clear();
6587d523365SDimitry Andric return AAResultBase::pointsToConstantMemory(Loc, OrLocal);
6592754fe60SDimitry Andric }
660ff0cc061SDimitry Andric for (Value *IncValue : PN->incoming_values())
661ff0cc061SDimitry Andric Worklist.push_back(IncValue);
6622754fe60SDimitry Andric continue;
6632754fe60SDimitry Andric }
6642754fe60SDimitry Andric
6652754fe60SDimitry Andric // Otherwise be conservative.
6662754fe60SDimitry Andric Visited.clear();
6677d523365SDimitry Andric return AAResultBase::pointsToConstantMemory(Loc, OrLocal);
6682754fe60SDimitry Andric } while (!Worklist.empty() && --MaxLookup);
6692754fe60SDimitry Andric
6702754fe60SDimitry Andric Visited.clear();
6712754fe60SDimitry Andric return Worklist.empty();
672f22ef01cSRoman Divacky }
673f22ef01cSRoman Divacky
6747d523365SDimitry Andric /// Returns the behavior when calling the given call site.
getModRefBehavior(const CallBase * Call)675*b5893f02SDimitry Andric FunctionModRefBehavior BasicAAResult::getModRefBehavior(const CallBase *Call) {
676*b5893f02SDimitry Andric if (Call->doesNotAccessMemory())
677e580952dSDimitry Andric // Can't do better than this.
6787d523365SDimitry Andric return FMRB_DoesNotAccessMemory;
679e580952dSDimitry Andric
6807d523365SDimitry Andric FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior;
681e580952dSDimitry Andric
682e580952dSDimitry Andric // If the callsite knows it only reads memory, don't return worse
683e580952dSDimitry Andric // than that.
684*b5893f02SDimitry Andric if (Call->onlyReadsMemory())
6857d523365SDimitry Andric Min = FMRB_OnlyReadsMemory;
686*b5893f02SDimitry Andric else if (Call->doesNotReadMemory())
6873ca95b02SDimitry Andric Min = FMRB_DoesNotReadMemory;
688e580952dSDimitry Andric
689*b5893f02SDimitry Andric if (Call->onlyAccessesArgMemory())
6907d523365SDimitry Andric Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesArgumentPointees);
691*b5893f02SDimitry Andric else if (Call->onlyAccessesInaccessibleMemory())
6922cab237bSDimitry Andric Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesInaccessibleMem);
693*b5893f02SDimitry Andric else if (Call->onlyAccessesInaccessibleMemOrArgMem())
6942cab237bSDimitry Andric Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesInaccessibleOrArgMem);
695875ed548SDimitry Andric
696*b5893f02SDimitry Andric // If the call has operand bundles then aliasing attributes from the function
697*b5893f02SDimitry Andric // it calls do not directly apply to the call. This can be made more precise
698*b5893f02SDimitry Andric // in the future.
699*b5893f02SDimitry Andric if (!Call->hasOperandBundles())
700*b5893f02SDimitry Andric if (const Function *F = Call->getCalledFunction())
7013ca95b02SDimitry Andric Min =
7023ca95b02SDimitry Andric FunctionModRefBehavior(Min & getBestAAResults().getModRefBehavior(F));
7033ca95b02SDimitry Andric
7043ca95b02SDimitry Andric return Min;
705e580952dSDimitry Andric }
706e580952dSDimitry Andric
7077d523365SDimitry Andric /// Returns the behavior when calling the given function. For use when the call
7087d523365SDimitry Andric /// site is not known.
getModRefBehavior(const Function * F)7097d523365SDimitry Andric FunctionModRefBehavior BasicAAResult::getModRefBehavior(const Function *F) {
7102754fe60SDimitry Andric // If the function declares it doesn't access memory, we can't do better.
711e580952dSDimitry Andric if (F->doesNotAccessMemory())
7127d523365SDimitry Andric return FMRB_DoesNotAccessMemory;
713e580952dSDimitry Andric
7147d523365SDimitry Andric FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior;
7152754fe60SDimitry Andric
7162754fe60SDimitry Andric // If the function declares it only reads memory, go with that.
7172754fe60SDimitry Andric if (F->onlyReadsMemory())
7187d523365SDimitry Andric Min = FMRB_OnlyReadsMemory;
7193ca95b02SDimitry Andric else if (F->doesNotReadMemory())
7203ca95b02SDimitry Andric Min = FMRB_DoesNotReadMemory;
7212754fe60SDimitry Andric
722875ed548SDimitry Andric if (F->onlyAccessesArgMemory())
7237d523365SDimitry Andric Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesArgumentPointees);
724d88c1a5aSDimitry Andric else if (F->onlyAccessesInaccessibleMemory())
725d88c1a5aSDimitry Andric Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesInaccessibleMem);
726d88c1a5aSDimitry Andric else if (F->onlyAccessesInaccessibleMemOrArgMem())
727d88c1a5aSDimitry Andric Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesInaccessibleOrArgMem);
728875ed548SDimitry Andric
7293ca95b02SDimitry Andric return Min;
730e580952dSDimitry Andric }
731f22ef01cSRoman Divacky
7323ca95b02SDimitry Andric /// Returns true if this is a writeonly (i.e Mod only) parameter.
isWriteOnlyParam(const CallBase * Call,unsigned ArgIdx,const TargetLibraryInfo & TLI)733*b5893f02SDimitry Andric static bool isWriteOnlyParam(const CallBase *Call, unsigned ArgIdx,
734444ed5c5SDimitry Andric const TargetLibraryInfo &TLI) {
735*b5893f02SDimitry Andric if (Call->paramHasAttr(ArgIdx, Attribute::WriteOnly))
736444ed5c5SDimitry Andric return true;
73791bc56edSDimitry Andric
73891bc56edSDimitry Andric // We can bound the aliasing properties of memset_pattern16 just as we can
73991bc56edSDimitry Andric // for memcpy/memset. This is particularly important because the
74091bc56edSDimitry Andric // LoopIdiomRecognizer likes to turn loops into calls to memset_pattern16
7413ca95b02SDimitry Andric // whenever possible.
7423ca95b02SDimitry Andric // FIXME Consider handling this in InferFunctionAttr.cpp together with other
7433ca95b02SDimitry Andric // attributes.
7447a7e6055SDimitry Andric LibFunc F;
745*b5893f02SDimitry Andric if (Call->getCalledFunction() &&
746*b5893f02SDimitry Andric TLI.getLibFunc(*Call->getCalledFunction(), F) &&
7477a7e6055SDimitry Andric F == LibFunc_memset_pattern16 && TLI.has(F))
7484d0b32cdSDimitry Andric if (ArgIdx == 0)
749444ed5c5SDimitry Andric return true;
750444ed5c5SDimitry Andric
751444ed5c5SDimitry Andric // TODO: memset_pattern4, memset_pattern8
752444ed5c5SDimitry Andric // TODO: _chk variants
753444ed5c5SDimitry Andric // TODO: strcmp, strcpy
754444ed5c5SDimitry Andric
755444ed5c5SDimitry Andric return false;
756444ed5c5SDimitry Andric }
757444ed5c5SDimitry Andric
getArgModRefInfo(const CallBase * Call,unsigned ArgIdx)758*b5893f02SDimitry Andric ModRefInfo BasicAAResult::getArgModRefInfo(const CallBase *Call,
759444ed5c5SDimitry Andric unsigned ArgIdx) {
7603ca95b02SDimitry Andric // Checking for known builtin intrinsics and target library functions.
761*b5893f02SDimitry Andric if (isWriteOnlyParam(Call, ArgIdx, TLI))
7622cab237bSDimitry Andric return ModRefInfo::Mod;
76391bc56edSDimitry Andric
764*b5893f02SDimitry Andric if (Call->paramHasAttr(ArgIdx, Attribute::ReadOnly))
7652cab237bSDimitry Andric return ModRefInfo::Ref;
7667d523365SDimitry Andric
767*b5893f02SDimitry Andric if (Call->paramHasAttr(ArgIdx, Attribute::ReadNone))
7682cab237bSDimitry Andric return ModRefInfo::NoModRef;
7697d523365SDimitry Andric
770*b5893f02SDimitry Andric return AAResultBase::getArgModRefInfo(Call, ArgIdx);
77191bc56edSDimitry Andric }
77291bc56edSDimitry Andric
isIntrinsicCall(const CallBase * Call,Intrinsic::ID IID)773*b5893f02SDimitry Andric static bool isIntrinsicCall(const CallBase *Call, Intrinsic::ID IID) {
774*b5893f02SDimitry Andric const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Call);
7753ca95b02SDimitry Andric return II && II->getIntrinsicID() == IID;
77639d628a0SDimitry Andric }
77739d628a0SDimitry Andric
7787d523365SDimitry Andric #ifndef NDEBUG
getParent(const Value * V)7797d523365SDimitry Andric static const Function *getParent(const Value *V) {
780d8866befSDimitry Andric if (const Instruction *inst = dyn_cast<Instruction>(V)) {
781d8866befSDimitry Andric if (!inst->getParent())
782d8866befSDimitry Andric return nullptr;
7837d523365SDimitry Andric return inst->getParent()->getParent();
784d8866befSDimitry Andric }
7857d523365SDimitry Andric
7867d523365SDimitry Andric if (const Argument *arg = dyn_cast<Argument>(V))
7877d523365SDimitry Andric return arg->getParent();
7887d523365SDimitry Andric
7897d523365SDimitry Andric return nullptr;
790ff0cc061SDimitry Andric }
791ff0cc061SDimitry Andric
notDifferentParent(const Value * O1,const Value * O2)7927d523365SDimitry Andric static bool notDifferentParent(const Value *O1, const Value *O2) {
7937d523365SDimitry Andric
7947d523365SDimitry Andric const Function *F1 = getParent(O1);
7957d523365SDimitry Andric const Function *F2 = getParent(O2);
7967d523365SDimitry Andric
7977d523365SDimitry Andric return !F1 || !F2 || F1 == F2;
7987d523365SDimitry Andric }
7997d523365SDimitry Andric #endif
8007d523365SDimitry Andric
alias(const MemoryLocation & LocA,const MemoryLocation & LocB)8017d523365SDimitry Andric AliasResult BasicAAResult::alias(const MemoryLocation &LocA,
8027d523365SDimitry Andric const MemoryLocation &LocB) {
8037d523365SDimitry Andric assert(notDifferentParent(LocA.Ptr, LocB.Ptr) &&
8047d523365SDimitry Andric "BasicAliasAnalysis doesn't support interprocedural queries.");
8057d523365SDimitry Andric
8067d523365SDimitry Andric // If we have a directly cached entry for these locations, we have recursed
8077d523365SDimitry Andric // through this once, so just return the cached results. Notably, when this
8087d523365SDimitry Andric // happens, we don't clear the cache.
8097d523365SDimitry Andric auto CacheIt = AliasCache.find(LocPair(LocA, LocB));
8107d523365SDimitry Andric if (CacheIt != AliasCache.end())
8117d523365SDimitry Andric return CacheIt->second;
8127d523365SDimitry Andric
8137d523365SDimitry Andric AliasResult Alias = aliasCheck(LocA.Ptr, LocA.Size, LocA.AATags, LocB.Ptr,
8147d523365SDimitry Andric LocB.Size, LocB.AATags);
8157d523365SDimitry Andric // AliasCache rarely has more than 1 or 2 elements, always use
8167d523365SDimitry Andric // shrink_and_clear so it quickly returns to the inline capacity of the
8177d523365SDimitry Andric // SmallDenseMap if it ever grows larger.
8187d523365SDimitry Andric // FIXME: This should really be shrink_to_inline_capacity_and_clear().
8197d523365SDimitry Andric AliasCache.shrink_and_clear();
8207d523365SDimitry Andric VisitedPhiBBs.clear();
8217d523365SDimitry Andric return Alias;
8227d523365SDimitry Andric }
8237d523365SDimitry Andric
8247d523365SDimitry Andric /// Checks to see if the specified callsite can clobber the specified memory
8257d523365SDimitry Andric /// object.
8267d523365SDimitry Andric ///
8277d523365SDimitry Andric /// Since we only look at local properties of this function, we really can't
8287d523365SDimitry Andric /// say much about this query. We do, however, use simple "address taken"
8297d523365SDimitry Andric /// analysis on local objects.
getModRefInfo(const CallBase * Call,const MemoryLocation & Loc)830*b5893f02SDimitry Andric ModRefInfo BasicAAResult::getModRefInfo(const CallBase *Call,
8318f0fd8f6SDimitry Andric const MemoryLocation &Loc) {
832*b5893f02SDimitry Andric assert(notDifferentParent(Call, Loc.Ptr) &&
833ffd1746dSEd Schouten "AliasAnalysis query involving multiple functions!");
834ffd1746dSEd Schouten
8357d523365SDimitry Andric const Value *Object = GetUnderlyingObject(Loc.Ptr, DL);
836f22ef01cSRoman Divacky
8374ba319b5SDimitry Andric // Calls marked 'tail' cannot read or write allocas from the current frame
8384ba319b5SDimitry Andric // because the current frame might be destroyed by the time they run. However,
8394ba319b5SDimitry Andric // a tail call may use an alloca with byval. Calling with byval copies the
8404ba319b5SDimitry Andric // contents of the alloca into argument registers or stack slots, so there is
8414ba319b5SDimitry Andric // no lifetime issue.
842f22ef01cSRoman Divacky if (isa<AllocaInst>(Object))
843*b5893f02SDimitry Andric if (const CallInst *CI = dyn_cast<CallInst>(Call))
8444ba319b5SDimitry Andric if (CI->isTailCall() &&
8454ba319b5SDimitry Andric !CI->getAttributes().hasAttrSomewhere(Attribute::ByVal))
8462cab237bSDimitry Andric return ModRefInfo::NoModRef;
847f22ef01cSRoman Divacky
848*b5893f02SDimitry Andric // Stack restore is able to modify unescaped dynamic allocas. Assume it may
849*b5893f02SDimitry Andric // modify them even though the alloca is not escaped.
850*b5893f02SDimitry Andric if (auto *AI = dyn_cast<AllocaInst>(Object))
851*b5893f02SDimitry Andric if (!AI->isStaticAlloca() && isIntrinsicCall(Call, Intrinsic::stackrestore))
852*b5893f02SDimitry Andric return ModRefInfo::Mod;
853*b5893f02SDimitry Andric
854f22ef01cSRoman Divacky // If the pointer is to a locally allocated object that does not escape,
855f22ef01cSRoman Divacky // then the call can not mod/ref the pointer unless the call takes the pointer
856f22ef01cSRoman Divacky // as an argument, and itself doesn't capture it.
857*b5893f02SDimitry Andric if (!isa<Constant>(Object) && Call != Object &&
858f22ef01cSRoman Divacky isNonEscapingLocalObject(Object)) {
8597a7e6055SDimitry Andric
8607a7e6055SDimitry Andric // Optimistically assume that call doesn't touch Object and check this
8617a7e6055SDimitry Andric // assumption in the following loop.
8622cab237bSDimitry Andric ModRefInfo Result = ModRefInfo::NoModRef;
863da09e106SDimitry Andric bool IsMustAlias = true;
8647a7e6055SDimitry Andric
8653ca95b02SDimitry Andric unsigned OperandNo = 0;
866*b5893f02SDimitry Andric for (auto CI = Call->data_operands_begin(), CE = Call->data_operands_end();
8673ca95b02SDimitry Andric CI != CE; ++CI, ++OperandNo) {
868bd5abe19SDimitry Andric // Only look at the no-capture or byval pointer arguments. If this
869bd5abe19SDimitry Andric // pointer were passed to arguments that were neither of these, then it
870bd5abe19SDimitry Andric // couldn't be no-capture.
871f22ef01cSRoman Divacky if (!(*CI)->getType()->isPointerTy() ||
872*b5893f02SDimitry Andric (!Call->doesNotCapture(OperandNo) &&
873*b5893f02SDimitry Andric OperandNo < Call->getNumArgOperands() &&
874*b5893f02SDimitry Andric !Call->isByValArgument(OperandNo)))
875f22ef01cSRoman Divacky continue;
876f22ef01cSRoman Divacky
8777a7e6055SDimitry Andric // Call doesn't access memory through this operand, so we don't care
8787a7e6055SDimitry Andric // if it aliases with Object.
879*b5893f02SDimitry Andric if (Call->doesNotAccessMemory(OperandNo))
8807a7e6055SDimitry Andric continue;
8817a7e6055SDimitry Andric
882f22ef01cSRoman Divacky // If this is a no-capture pointer argument, see if we can tell that it
8837a7e6055SDimitry Andric // is impossible to alias the pointer we're checking.
8847d523365SDimitry Andric AliasResult AR =
8857d523365SDimitry Andric getBestAAResults().alias(MemoryLocation(*CI), MemoryLocation(Object));
886da09e106SDimitry Andric if (AR != MustAlias)
887da09e106SDimitry Andric IsMustAlias = false;
8887a7e6055SDimitry Andric // Operand doesnt alias 'Object', continue looking for other aliases
8897a7e6055SDimitry Andric if (AR == NoAlias)
8907a7e6055SDimitry Andric continue;
8917a7e6055SDimitry Andric // Operand aliases 'Object', but call doesn't modify it. Strengthen
8927a7e6055SDimitry Andric // initial assumption and keep looking in case if there are more aliases.
893*b5893f02SDimitry Andric if (Call->onlyReadsMemory(OperandNo)) {
8942cab237bSDimitry Andric Result = setRef(Result);
8957a7e6055SDimitry Andric continue;
8967a7e6055SDimitry Andric }
8977a7e6055SDimitry Andric // Operand aliases 'Object' but call only writes into it.
898*b5893f02SDimitry Andric if (Call->doesNotReadMemory(OperandNo)) {
8992cab237bSDimitry Andric Result = setMod(Result);
9007a7e6055SDimitry Andric continue;
9017a7e6055SDimitry Andric }
9027a7e6055SDimitry Andric // This operand aliases 'Object' and call reads and writes into it.
903da09e106SDimitry Andric // Setting ModRef will not yield an early return below, MustAlias is not
904da09e106SDimitry Andric // used further.
9052cab237bSDimitry Andric Result = ModRefInfo::ModRef;
906f22ef01cSRoman Divacky break;
907f22ef01cSRoman Divacky }
908f22ef01cSRoman Divacky
909da09e106SDimitry Andric // No operand aliases, reset Must bit. Add below if at least one aliases
910da09e106SDimitry Andric // and all aliases found are MustAlias.
911da09e106SDimitry Andric if (isNoModRef(Result))
912da09e106SDimitry Andric IsMustAlias = false;
913da09e106SDimitry Andric
9147a7e6055SDimitry Andric // Early return if we improved mod ref information
9154ba319b5SDimitry Andric if (!isModAndRefSet(Result)) {
9164ba319b5SDimitry Andric if (isNoModRef(Result))
9174ba319b5SDimitry Andric return ModRefInfo::NoModRef;
918da09e106SDimitry Andric return IsMustAlias ? setMust(Result) : clearMust(Result);
919f22ef01cSRoman Divacky }
9204ba319b5SDimitry Andric }
921f22ef01cSRoman Divacky
922*b5893f02SDimitry Andric // If the call is to malloc or calloc, we can assume that it doesn't
9233ca95b02SDimitry Andric // modify any IR visible value. This is only valid because we assume these
9243ca95b02SDimitry Andric // routines do not read values visible in the IR. TODO: Consider special
9253ca95b02SDimitry Andric // casing realloc and strdup routines which access only their arguments as
9263ca95b02SDimitry Andric // well. Or alternatively, replace all of this with inaccessiblememonly once
9273ca95b02SDimitry Andric // that's implemented fully.
928*b5893f02SDimitry Andric if (isMallocOrCallocLikeFn(Call, &TLI)) {
9293ca95b02SDimitry Andric // Be conservative if the accessed pointer may alias the allocation -
9303ca95b02SDimitry Andric // fallback to the generic handling below.
931*b5893f02SDimitry Andric if (getBestAAResults().alias(MemoryLocation(Call), Loc) == NoAlias)
9322cab237bSDimitry Andric return ModRefInfo::NoModRef;
9333ca95b02SDimitry Andric }
9343ca95b02SDimitry Andric
935d88c1a5aSDimitry Andric // The semantics of memcpy intrinsics forbid overlap between their respective
936d88c1a5aSDimitry Andric // operands, i.e., source and destination of any given memcpy must no-alias.
937d88c1a5aSDimitry Andric // If Loc must-aliases either one of these two locations, then it necessarily
938d88c1a5aSDimitry Andric // no-aliases the other.
939*b5893f02SDimitry Andric if (auto *Inst = dyn_cast<AnyMemCpyInst>(Call)) {
940d88c1a5aSDimitry Andric AliasResult SrcAA, DestAA;
941d88c1a5aSDimitry Andric
942d88c1a5aSDimitry Andric if ((SrcAA = getBestAAResults().alias(MemoryLocation::getForSource(Inst),
943d88c1a5aSDimitry Andric Loc)) == MustAlias)
944d88c1a5aSDimitry Andric // Loc is exactly the memcpy source thus disjoint from memcpy dest.
9452cab237bSDimitry Andric return ModRefInfo::Ref;
946d88c1a5aSDimitry Andric if ((DestAA = getBestAAResults().alias(MemoryLocation::getForDest(Inst),
947d88c1a5aSDimitry Andric Loc)) == MustAlias)
948d88c1a5aSDimitry Andric // The converse case.
9492cab237bSDimitry Andric return ModRefInfo::Mod;
950d88c1a5aSDimitry Andric
951d88c1a5aSDimitry Andric // It's also possible for Loc to alias both src and dest, or neither.
9522cab237bSDimitry Andric ModRefInfo rv = ModRefInfo::NoModRef;
953d88c1a5aSDimitry Andric if (SrcAA != NoAlias)
9542cab237bSDimitry Andric rv = setRef(rv);
955d88c1a5aSDimitry Andric if (DestAA != NoAlias)
9562cab237bSDimitry Andric rv = setMod(rv);
957d88c1a5aSDimitry Andric return rv;
958d88c1a5aSDimitry Andric }
959d88c1a5aSDimitry Andric
96039d628a0SDimitry Andric // While the assume intrinsic is marked as arbitrarily writing so that
96139d628a0SDimitry Andric // proper control dependencies will be maintained, it never aliases any
96239d628a0SDimitry Andric // particular memory location.
963*b5893f02SDimitry Andric if (isIntrinsicCall(Call, Intrinsic::assume))
9642cab237bSDimitry Andric return ModRefInfo::NoModRef;
96539d628a0SDimitry Andric
9663ca95b02SDimitry Andric // Like assumes, guard intrinsics are also marked as arbitrarily writing so
9673ca95b02SDimitry Andric // that proper control dependencies are maintained but they never mods any
9683ca95b02SDimitry Andric // particular memory location.
9693ca95b02SDimitry Andric //
9703ca95b02SDimitry Andric // *Unlike* assumes, guard intrinsics are modeled as reading memory since the
9713ca95b02SDimitry Andric // heap state at the point the guard is issued needs to be consistent in case
9723ca95b02SDimitry Andric // the guard invokes the "deopt" continuation.
973*b5893f02SDimitry Andric if (isIntrinsicCall(Call, Intrinsic::experimental_guard))
9742cab237bSDimitry Andric return ModRefInfo::Ref;
9753ca95b02SDimitry Andric
976d88c1a5aSDimitry Andric // Like assumes, invariant.start intrinsics were also marked as arbitrarily
977d88c1a5aSDimitry Andric // writing so that proper control dependencies are maintained but they never
978d88c1a5aSDimitry Andric // mod any particular memory location visible to the IR.
979d88c1a5aSDimitry Andric // *Unlike* assumes (which are now modeled as NoModRef), invariant.start
980d88c1a5aSDimitry Andric // intrinsic is now modeled as reading memory. This prevents hoisting the
981d88c1a5aSDimitry Andric // invariant.start intrinsic over stores. Consider:
982d88c1a5aSDimitry Andric // *ptr = 40;
983d88c1a5aSDimitry Andric // *ptr = 50;
984d88c1a5aSDimitry Andric // invariant_start(ptr)
985d88c1a5aSDimitry Andric // int val = *ptr;
986d88c1a5aSDimitry Andric // print(val);
987d88c1a5aSDimitry Andric //
988d88c1a5aSDimitry Andric // This cannot be transformed to:
989d88c1a5aSDimitry Andric //
990d88c1a5aSDimitry Andric // *ptr = 40;
991d88c1a5aSDimitry Andric // invariant_start(ptr)
992d88c1a5aSDimitry Andric // *ptr = 50;
993d88c1a5aSDimitry Andric // int val = *ptr;
994d88c1a5aSDimitry Andric // print(val);
995d88c1a5aSDimitry Andric //
996d88c1a5aSDimitry Andric // The transformation will cause the second store to be ignored (based on
997d88c1a5aSDimitry Andric // rules of invariant.start) and print 40, while the first program always
998d88c1a5aSDimitry Andric // prints 50.
999*b5893f02SDimitry Andric if (isIntrinsicCall(Call, Intrinsic::invariant_start))
10002cab237bSDimitry Andric return ModRefInfo::Ref;
1001d88c1a5aSDimitry Andric
10027d523365SDimitry Andric // The AAResultBase base class has some smarts, lets use them.
1003*b5893f02SDimitry Andric return AAResultBase::getModRefInfo(Call, Loc);
10043861d79fSDimitry Andric }
10053861d79fSDimitry Andric
getModRefInfo(const CallBase * Call1,const CallBase * Call2)1006*b5893f02SDimitry Andric ModRefInfo BasicAAResult::getModRefInfo(const CallBase *Call1,
1007*b5893f02SDimitry Andric const CallBase *Call2) {
100839d628a0SDimitry Andric // While the assume intrinsic is marked as arbitrarily writing so that
100939d628a0SDimitry Andric // proper control dependencies will be maintained, it never aliases any
101039d628a0SDimitry Andric // particular memory location.
1011*b5893f02SDimitry Andric if (isIntrinsicCall(Call1, Intrinsic::assume) ||
1012*b5893f02SDimitry Andric isIntrinsicCall(Call2, Intrinsic::assume))
10132cab237bSDimitry Andric return ModRefInfo::NoModRef;
101439d628a0SDimitry Andric
10153ca95b02SDimitry Andric // Like assumes, guard intrinsics are also marked as arbitrarily writing so
10163ca95b02SDimitry Andric // that proper control dependencies are maintained but they never mod any
10173ca95b02SDimitry Andric // particular memory location.
10183ca95b02SDimitry Andric //
10193ca95b02SDimitry Andric // *Unlike* assumes, guard intrinsics are modeled as reading memory since the
10203ca95b02SDimitry Andric // heap state at the point the guard is issued needs to be consistent in case
10213ca95b02SDimitry Andric // the guard invokes the "deopt" continuation.
10223ca95b02SDimitry Andric
10233ca95b02SDimitry Andric // NB! This function is *not* commutative, so we specical case two
10243ca95b02SDimitry Andric // possibilities for guard intrinsics.
10253ca95b02SDimitry Andric
1026*b5893f02SDimitry Andric if (isIntrinsicCall(Call1, Intrinsic::experimental_guard))
1027*b5893f02SDimitry Andric return isModSet(createModRefInfo(getModRefBehavior(Call2)))
10282cab237bSDimitry Andric ? ModRefInfo::Ref
10292cab237bSDimitry Andric : ModRefInfo::NoModRef;
10303ca95b02SDimitry Andric
1031*b5893f02SDimitry Andric if (isIntrinsicCall(Call2, Intrinsic::experimental_guard))
1032*b5893f02SDimitry Andric return isModSet(createModRefInfo(getModRefBehavior(Call1)))
10332cab237bSDimitry Andric ? ModRefInfo::Mod
10342cab237bSDimitry Andric : ModRefInfo::NoModRef;
10353ca95b02SDimitry Andric
10367d523365SDimitry Andric // The AAResultBase base class has some smarts, lets use them.
1037*b5893f02SDimitry Andric return AAResultBase::getModRefInfo(Call1, Call2);
103839d628a0SDimitry Andric }
103939d628a0SDimitry Andric
10407d523365SDimitry Andric /// Provide ad-hoc rules to disambiguate accesses through two GEP operators,
10417d523365SDimitry Andric /// both having the exact same pointer operand.
aliasSameBasePointerGEPs(const GEPOperator * GEP1,LocationSize MaybeV1Size,const GEPOperator * GEP2,LocationSize MaybeV2Size,const DataLayout & DL)10423dac3a9bSDimitry Andric static AliasResult aliasSameBasePointerGEPs(const GEPOperator *GEP1,
1043*b5893f02SDimitry Andric LocationSize MaybeV1Size,
10443dac3a9bSDimitry Andric const GEPOperator *GEP2,
1045*b5893f02SDimitry Andric LocationSize MaybeV2Size,
1046ff0cc061SDimitry Andric const DataLayout &DL) {
10474ba319b5SDimitry Andric assert(GEP1->getPointerOperand()->stripPointerCastsAndInvariantGroups() ==
10484ba319b5SDimitry Andric GEP2->getPointerOperand()->stripPointerCastsAndInvariantGroups() &&
10496bc11b14SDimitry Andric GEP1->getPointerOperandType() == GEP2->getPointerOperandType() &&
1050ff0cc061SDimitry Andric "Expected GEPs with the same pointer operand");
1051ff0cc061SDimitry Andric
1052ff0cc061SDimitry Andric // Try to determine whether GEP1 and GEP2 index through arrays, into structs,
1053ff0cc061SDimitry Andric // such that the struct field accesses provably cannot alias.
1054ff0cc061SDimitry Andric // We also need at least two indices (the pointer, and the struct field).
1055ff0cc061SDimitry Andric if (GEP1->getNumIndices() != GEP2->getNumIndices() ||
1056ff0cc061SDimitry Andric GEP1->getNumIndices() < 2)
10573dac3a9bSDimitry Andric return MayAlias;
1058ff0cc061SDimitry Andric
1059ff0cc061SDimitry Andric // If we don't know the size of the accesses through both GEPs, we can't
1060ff0cc061SDimitry Andric // determine whether the struct fields accessed can't alias.
1061*b5893f02SDimitry Andric if (MaybeV1Size == LocationSize::unknown() ||
1062*b5893f02SDimitry Andric MaybeV2Size == LocationSize::unknown())
10633dac3a9bSDimitry Andric return MayAlias;
1064ff0cc061SDimitry Andric
1065*b5893f02SDimitry Andric const uint64_t V1Size = MaybeV1Size.getValue();
1066*b5893f02SDimitry Andric const uint64_t V2Size = MaybeV2Size.getValue();
1067*b5893f02SDimitry Andric
1068ff0cc061SDimitry Andric ConstantInt *C1 =
1069ff0cc061SDimitry Andric dyn_cast<ConstantInt>(GEP1->getOperand(GEP1->getNumOperands() - 1));
1070ff0cc061SDimitry Andric ConstantInt *C2 =
1071ff0cc061SDimitry Andric dyn_cast<ConstantInt>(GEP2->getOperand(GEP2->getNumOperands() - 1));
1072ff0cc061SDimitry Andric
10737d523365SDimitry Andric // If the last (struct) indices are constants and are equal, the other indices
10747d523365SDimitry Andric // might be also be dynamically equal, so the GEPs can alias.
1075*b5893f02SDimitry Andric if (C1 && C2) {
1076*b5893f02SDimitry Andric unsigned BitWidth = std::max(C1->getBitWidth(), C2->getBitWidth());
1077*b5893f02SDimitry Andric if (C1->getValue().sextOrSelf(BitWidth) ==
1078*b5893f02SDimitry Andric C2->getValue().sextOrSelf(BitWidth))
10793dac3a9bSDimitry Andric return MayAlias;
1080*b5893f02SDimitry Andric }
1081ff0cc061SDimitry Andric
1082ff0cc061SDimitry Andric // Find the last-indexed type of the GEP, i.e., the type you'd get if
1083ff0cc061SDimitry Andric // you stripped the last index.
1084ff0cc061SDimitry Andric // On the way, look at each indexed type. If there's something other
1085ff0cc061SDimitry Andric // than an array, different indices can lead to different final types.
1086ff0cc061SDimitry Andric SmallVector<Value *, 8> IntermediateIndices;
1087ff0cc061SDimitry Andric
1088ff0cc061SDimitry Andric // Insert the first index; we don't need to check the type indexed
1089ff0cc061SDimitry Andric // through it as it only drops the pointer indirection.
1090ff0cc061SDimitry Andric assert(GEP1->getNumIndices() > 1 && "Not enough GEP indices to examine");
1091ff0cc061SDimitry Andric IntermediateIndices.push_back(GEP1->getOperand(1));
1092ff0cc061SDimitry Andric
1093ff0cc061SDimitry Andric // Insert all the remaining indices but the last one.
1094ff0cc061SDimitry Andric // Also, check that they all index through arrays.
1095ff0cc061SDimitry Andric for (unsigned i = 1, e = GEP1->getNumIndices() - 1; i != e; ++i) {
1096ff0cc061SDimitry Andric if (!isa<ArrayType>(GetElementPtrInst::getIndexedType(
1097ff0cc061SDimitry Andric GEP1->getSourceElementType(), IntermediateIndices)))
10983dac3a9bSDimitry Andric return MayAlias;
1099ff0cc061SDimitry Andric IntermediateIndices.push_back(GEP1->getOperand(i + 1));
1100ff0cc061SDimitry Andric }
1101ff0cc061SDimitry Andric
11027d523365SDimitry Andric auto *Ty = GetElementPtrInst::getIndexedType(
11037d523365SDimitry Andric GEP1->getSourceElementType(), IntermediateIndices);
11047d523365SDimitry Andric StructType *LastIndexedStruct = dyn_cast<StructType>(Ty);
1105ff0cc061SDimitry Andric
11067d523365SDimitry Andric if (isa<SequentialType>(Ty)) {
11077d523365SDimitry Andric // We know that:
11087d523365SDimitry Andric // - both GEPs begin indexing from the exact same pointer;
11097d523365SDimitry Andric // - the last indices in both GEPs are constants, indexing into a sequential
11107d523365SDimitry Andric // type (array or pointer);
11117d523365SDimitry Andric // - both GEPs only index through arrays prior to that.
11127d523365SDimitry Andric //
11137d523365SDimitry Andric // Because array indices greater than the number of elements are valid in
11147d523365SDimitry Andric // GEPs, unless we know the intermediate indices are identical between
11157d523365SDimitry Andric // GEP1 and GEP2 we cannot guarantee that the last indexed arrays don't
11167d523365SDimitry Andric // partially overlap. We also need to check that the loaded size matches
11177d523365SDimitry Andric // the element size, otherwise we could still have overlap.
11187d523365SDimitry Andric const uint64_t ElementSize =
11197d523365SDimitry Andric DL.getTypeStoreSize(cast<SequentialType>(Ty)->getElementType());
11207d523365SDimitry Andric if (V1Size != ElementSize || V2Size != ElementSize)
11213dac3a9bSDimitry Andric return MayAlias;
1122ff0cc061SDimitry Andric
11237d523365SDimitry Andric for (unsigned i = 0, e = GEP1->getNumIndices() - 1; i != e; ++i)
11247d523365SDimitry Andric if (GEP1->getOperand(i + 1) != GEP2->getOperand(i + 1))
11257d523365SDimitry Andric return MayAlias;
11267d523365SDimitry Andric
11277d523365SDimitry Andric // Now we know that the array/pointer that GEP1 indexes into and that
11287d523365SDimitry Andric // that GEP2 indexes into must either precisely overlap or be disjoint.
11297d523365SDimitry Andric // Because they cannot partially overlap and because fields in an array
11307d523365SDimitry Andric // cannot overlap, if we can prove the final indices are different between
11317d523365SDimitry Andric // GEP1 and GEP2, we can conclude GEP1 and GEP2 don't alias.
11327d523365SDimitry Andric
11337d523365SDimitry Andric // If the last indices are constants, we've already checked they don't
11347d523365SDimitry Andric // equal each other so we can exit early.
11357d523365SDimitry Andric if (C1 && C2)
11367d523365SDimitry Andric return NoAlias;
113724d58133SDimitry Andric {
113824d58133SDimitry Andric Value *GEP1LastIdx = GEP1->getOperand(GEP1->getNumOperands() - 1);
113924d58133SDimitry Andric Value *GEP2LastIdx = GEP2->getOperand(GEP2->getNumOperands() - 1);
114024d58133SDimitry Andric if (isa<PHINode>(GEP1LastIdx) || isa<PHINode>(GEP2LastIdx)) {
114124d58133SDimitry Andric // If one of the indices is a PHI node, be safe and only use
114224d58133SDimitry Andric // computeKnownBits so we don't make any assumptions about the
114324d58133SDimitry Andric // relationships between the two indices. This is important if we're
114424d58133SDimitry Andric // asking about values from different loop iterations. See PR32314.
114524d58133SDimitry Andric // TODO: We may be able to change the check so we only do this when
114624d58133SDimitry Andric // we definitely looked through a PHINode.
1147edd7eaddSDimitry Andric if (GEP1LastIdx != GEP2LastIdx &&
1148edd7eaddSDimitry Andric GEP1LastIdx->getType() == GEP2LastIdx->getType()) {
114924d58133SDimitry Andric KnownBits Known1 = computeKnownBits(GEP1LastIdx, DL);
115024d58133SDimitry Andric KnownBits Known2 = computeKnownBits(GEP2LastIdx, DL);
115124d58133SDimitry Andric if (Known1.Zero.intersects(Known2.One) ||
115224d58133SDimitry Andric Known1.One.intersects(Known2.Zero))
11537d523365SDimitry Andric return NoAlias;
1154edd7eaddSDimitry Andric }
115524d58133SDimitry Andric } else if (isKnownNonEqual(GEP1LastIdx, GEP2LastIdx, DL))
115624d58133SDimitry Andric return NoAlias;
115724d58133SDimitry Andric }
11587d523365SDimitry Andric return MayAlias;
11597d523365SDimitry Andric } else if (!LastIndexedStruct || !C1 || !C2) {
11607d523365SDimitry Andric return MayAlias;
11617d523365SDimitry Andric }
11627d523365SDimitry Andric
1163*b5893f02SDimitry Andric if (C1->getValue().getActiveBits() > 64 ||
1164*b5893f02SDimitry Andric C2->getValue().getActiveBits() > 64)
1165*b5893f02SDimitry Andric return MayAlias;
1166*b5893f02SDimitry Andric
1167ff0cc061SDimitry Andric // We know that:
1168ff0cc061SDimitry Andric // - both GEPs begin indexing from the exact same pointer;
1169ff0cc061SDimitry Andric // - the last indices in both GEPs are constants, indexing into a struct;
1170ff0cc061SDimitry Andric // - said indices are different, hence, the pointed-to fields are different;
1171ff0cc061SDimitry Andric // - both GEPs only index through arrays prior to that.
1172ff0cc061SDimitry Andric //
1173ff0cc061SDimitry Andric // This lets us determine that the struct that GEP1 indexes into and the
1174ff0cc061SDimitry Andric // struct that GEP2 indexes into must either precisely overlap or be
1175ff0cc061SDimitry Andric // completely disjoint. Because they cannot partially overlap, indexing into
1176ff0cc061SDimitry Andric // different non-overlapping fields of the struct will never alias.
1177ff0cc061SDimitry Andric
1178ff0cc061SDimitry Andric // Therefore, the only remaining thing needed to show that both GEPs can't
1179ff0cc061SDimitry Andric // alias is that the fields are not overlapping.
1180ff0cc061SDimitry Andric const StructLayout *SL = DL.getStructLayout(LastIndexedStruct);
1181ff0cc061SDimitry Andric const uint64_t StructSize = SL->getSizeInBytes();
1182ff0cc061SDimitry Andric const uint64_t V1Off = SL->getElementOffset(C1->getZExtValue());
1183ff0cc061SDimitry Andric const uint64_t V2Off = SL->getElementOffset(C2->getZExtValue());
1184ff0cc061SDimitry Andric
1185ff0cc061SDimitry Andric auto EltsDontOverlap = [StructSize](uint64_t V1Off, uint64_t V1Size,
1186ff0cc061SDimitry Andric uint64_t V2Off, uint64_t V2Size) {
1187ff0cc061SDimitry Andric return V1Off < V2Off && V1Off + V1Size <= V2Off &&
1188ff0cc061SDimitry Andric ((V2Off + V2Size <= StructSize) ||
1189ff0cc061SDimitry Andric (V2Off + V2Size - StructSize <= V1Off));
1190ff0cc061SDimitry Andric };
1191ff0cc061SDimitry Andric
1192ff0cc061SDimitry Andric if (EltsDontOverlap(V1Off, V1Size, V2Off, V2Size) ||
1193ff0cc061SDimitry Andric EltsDontOverlap(V2Off, V2Size, V1Off, V1Size))
11943dac3a9bSDimitry Andric return NoAlias;
1195ff0cc061SDimitry Andric
11963dac3a9bSDimitry Andric return MayAlias;
1197ff0cc061SDimitry Andric }
1198ff0cc061SDimitry Andric
11993ca95b02SDimitry Andric // If a we have (a) a GEP and (b) a pointer based on an alloca, and the
12003ca95b02SDimitry Andric // beginning of the object the GEP points would have a negative offset with
12013ca95b02SDimitry Andric // repsect to the alloca, that means the GEP can not alias pointer (b).
12023ca95b02SDimitry Andric // Note that the pointer based on the alloca may not be a GEP. For
12033ca95b02SDimitry Andric // example, it may be the alloca itself.
12043ca95b02SDimitry Andric // The same applies if (b) is based on a GlobalVariable. Note that just being
12053ca95b02SDimitry Andric // based on isIdentifiedObject() is not enough - we need an identified object
12063ca95b02SDimitry Andric // that does not permit access to negative offsets. For example, a negative
12073ca95b02SDimitry Andric // offset from a noalias argument or call can be inbounds w.r.t the actual
12083ca95b02SDimitry Andric // underlying object.
12093ca95b02SDimitry Andric //
12103ca95b02SDimitry Andric // For example, consider:
12113ca95b02SDimitry Andric //
12123ca95b02SDimitry Andric // struct { int f0, int f1, ...} foo;
12133ca95b02SDimitry Andric // foo alloca;
12143ca95b02SDimitry Andric // foo* random = bar(alloca);
12153ca95b02SDimitry Andric // int *f0 = &alloca.f0
12163ca95b02SDimitry Andric // int *f1 = &random->f1;
12173ca95b02SDimitry Andric //
12183ca95b02SDimitry Andric // Which is lowered, approximately, to:
12193ca95b02SDimitry Andric //
12203ca95b02SDimitry Andric // %alloca = alloca %struct.foo
12213ca95b02SDimitry Andric // %random = call %struct.foo* @random(%struct.foo* %alloca)
12223ca95b02SDimitry Andric // %f0 = getelementptr inbounds %struct, %struct.foo* %alloca, i32 0, i32 0
12233ca95b02SDimitry Andric // %f1 = getelementptr inbounds %struct, %struct.foo* %random, i32 0, i32 1
12243ca95b02SDimitry Andric //
12253ca95b02SDimitry Andric // Assume %f1 and %f0 alias. Then %f1 would point into the object allocated
12263ca95b02SDimitry Andric // by %alloca. Since the %f1 GEP is inbounds, that means %random must also
12273ca95b02SDimitry Andric // point into the same object. But since %f0 points to the beginning of %alloca,
12283ca95b02SDimitry Andric // the highest %f1 can be is (%alloca + 3). This means %random can not be higher
12293ca95b02SDimitry Andric // than (%alloca - 1), and so is not inbounds, a contradiction.
isGEPBaseAtNegativeOffset(const GEPOperator * GEPOp,const DecomposedGEP & DecompGEP,const DecomposedGEP & DecompObject,LocationSize MaybeObjectAccessSize)12303ca95b02SDimitry Andric bool BasicAAResult::isGEPBaseAtNegativeOffset(const GEPOperator *GEPOp,
12313ca95b02SDimitry Andric const DecomposedGEP &DecompGEP, const DecomposedGEP &DecompObject,
1232*b5893f02SDimitry Andric LocationSize MaybeObjectAccessSize) {
12333ca95b02SDimitry Andric // If the object access size is unknown, or the GEP isn't inbounds, bail.
1234*b5893f02SDimitry Andric if (MaybeObjectAccessSize == LocationSize::unknown() || !GEPOp->isInBounds())
12353ca95b02SDimitry Andric return false;
12363ca95b02SDimitry Andric
1237*b5893f02SDimitry Andric const uint64_t ObjectAccessSize = MaybeObjectAccessSize.getValue();
1238*b5893f02SDimitry Andric
12393ca95b02SDimitry Andric // We need the object to be an alloca or a globalvariable, and want to know
12403ca95b02SDimitry Andric // the offset of the pointer from the object precisely, so no variable
12413ca95b02SDimitry Andric // indices are allowed.
12423ca95b02SDimitry Andric if (!(isa<AllocaInst>(DecompObject.Base) ||
12433ca95b02SDimitry Andric isa<GlobalVariable>(DecompObject.Base)) ||
12443ca95b02SDimitry Andric !DecompObject.VarIndices.empty())
12453ca95b02SDimitry Andric return false;
12463ca95b02SDimitry Andric
1247*b5893f02SDimitry Andric APInt ObjectBaseOffset = DecompObject.StructOffset +
12483ca95b02SDimitry Andric DecompObject.OtherOffset;
12493ca95b02SDimitry Andric
12503ca95b02SDimitry Andric // If the GEP has no variable indices, we know the precise offset
12514ba319b5SDimitry Andric // from the base, then use it. If the GEP has variable indices,
12524ba319b5SDimitry Andric // we can't get exact GEP offset to identify pointer alias. So return
12534ba319b5SDimitry Andric // false in that case.
12544ba319b5SDimitry Andric if (!DecompGEP.VarIndices.empty())
12554ba319b5SDimitry Andric return false;
1256*b5893f02SDimitry Andric
1257*b5893f02SDimitry Andric APInt GEPBaseOffset = DecompGEP.StructOffset;
12583ca95b02SDimitry Andric GEPBaseOffset += DecompGEP.OtherOffset;
12593ca95b02SDimitry Andric
1260*b5893f02SDimitry Andric return GEPBaseOffset.sge(ObjectBaseOffset + (int64_t)ObjectAccessSize);
12613ca95b02SDimitry Andric }
12623ca95b02SDimitry Andric
12637d523365SDimitry Andric /// Provides a bunch of ad-hoc rules to disambiguate a GEP instruction against
12647d523365SDimitry Andric /// another pointer.
1265f22ef01cSRoman Divacky ///
12667d523365SDimitry Andric /// We know that V1 is a GEP, but we don't know anything about V2.
12677d523365SDimitry Andric /// UnderlyingV1 is GetUnderlyingObject(GEP1, DL), UnderlyingV2 is the same for
12687d523365SDimitry Andric /// V2.
12694ba319b5SDimitry Andric AliasResult
aliasGEP(const GEPOperator * GEP1,LocationSize V1Size,const AAMDNodes & V1AAInfo,const Value * V2,LocationSize V2Size,const AAMDNodes & V2AAInfo,const Value * UnderlyingV1,const Value * UnderlyingV2)12704ba319b5SDimitry Andric BasicAAResult::aliasGEP(const GEPOperator *GEP1, LocationSize V1Size,
12717d523365SDimitry Andric const AAMDNodes &V1AAInfo, const Value *V2,
12724ba319b5SDimitry Andric LocationSize V2Size, const AAMDNodes &V2AAInfo,
12734ba319b5SDimitry Andric const Value *UnderlyingV1, const Value *UnderlyingV2) {
12743ca95b02SDimitry Andric DecomposedGEP DecompGEP1, DecompGEP2;
1275*b5893f02SDimitry Andric unsigned MaxPointerSize = getMaxPointerSize(DL);
1276*b5893f02SDimitry Andric DecompGEP1.StructOffset = DecompGEP1.OtherOffset = APInt(MaxPointerSize, 0);
1277*b5893f02SDimitry Andric DecompGEP2.StructOffset = DecompGEP2.OtherOffset = APInt(MaxPointerSize, 0);
1278*b5893f02SDimitry Andric
12793ca95b02SDimitry Andric bool GEP1MaxLookupReached =
12803ca95b02SDimitry Andric DecomposeGEPExpression(GEP1, DecompGEP1, DL, &AC, DT);
12813ca95b02SDimitry Andric bool GEP2MaxLookupReached =
12823ca95b02SDimitry Andric DecomposeGEPExpression(V2, DecompGEP2, DL, &AC, DT);
1283f22ef01cSRoman Divacky
1284*b5893f02SDimitry Andric APInt GEP1BaseOffset = DecompGEP1.StructOffset + DecompGEP1.OtherOffset;
1285*b5893f02SDimitry Andric APInt GEP2BaseOffset = DecompGEP2.StructOffset + DecompGEP2.OtherOffset;
12863ca95b02SDimitry Andric
12873ca95b02SDimitry Andric assert(DecompGEP1.Base == UnderlyingV1 && DecompGEP2.Base == UnderlyingV2 &&
12883ca95b02SDimitry Andric "DecomposeGEPExpression returned a result different from "
12893ca95b02SDimitry Andric "GetUnderlyingObject");
12903ca95b02SDimitry Andric
12913ca95b02SDimitry Andric // If the GEP's offset relative to its base is such that the base would
12923ca95b02SDimitry Andric // fall below the start of the object underlying V2, then the GEP and V2
12933ca95b02SDimitry Andric // cannot alias.
12943ca95b02SDimitry Andric if (!GEP1MaxLookupReached && !GEP2MaxLookupReached &&
12953ca95b02SDimitry Andric isGEPBaseAtNegativeOffset(GEP1, DecompGEP1, DecompGEP2, V2Size))
12963ca95b02SDimitry Andric return NoAlias;
12973861d79fSDimitry Andric // If we have two gep instructions with must-alias or not-alias'ing base
12983861d79fSDimitry Andric // pointers, figure out if the indexes to the GEP tell us anything about the
12993861d79fSDimitry Andric // derived pointer.
1300f22ef01cSRoman Divacky if (const GEPOperator *GEP2 = dyn_cast<GEPOperator>(V2)) {
13013ca95b02SDimitry Andric // Check for the GEP base being at a negative offset, this time in the other
13023ca95b02SDimitry Andric // direction.
13033ca95b02SDimitry Andric if (!GEP1MaxLookupReached && !GEP2MaxLookupReached &&
13043ca95b02SDimitry Andric isGEPBaseAtNegativeOffset(GEP2, DecompGEP2, DecompGEP1, V1Size))
13053ca95b02SDimitry Andric return NoAlias;
1306139f7f9bSDimitry Andric // Do the base pointers alias?
13078f0fd8f6SDimitry Andric AliasResult BaseAlias =
1308*b5893f02SDimitry Andric aliasCheck(UnderlyingV1, LocationSize::unknown(), AAMDNodes(),
1309*b5893f02SDimitry Andric UnderlyingV2, LocationSize::unknown(), AAMDNodes());
1310139f7f9bSDimitry Andric
13113861d79fSDimitry Andric // Check for geps of non-aliasing underlying pointers where the offsets are
13123861d79fSDimitry Andric // identical.
1313139f7f9bSDimitry Andric if ((BaseAlias == MayAlias) && V1Size == V2Size) {
13143861d79fSDimitry Andric // Do the base pointers alias assuming type and size.
13157d523365SDimitry Andric AliasResult PreciseBaseAlias = aliasCheck(UnderlyingV1, V1Size, V1AAInfo,
13167d523365SDimitry Andric UnderlyingV2, V2Size, V2AAInfo);
13173861d79fSDimitry Andric if (PreciseBaseAlias == NoAlias) {
13183861d79fSDimitry Andric // See if the computed offset from the common pointer tells us about the
13193861d79fSDimitry Andric // relation of the resulting pointer.
132091bc56edSDimitry Andric // If the max search depth is reached the result is undefined
132191bc56edSDimitry Andric if (GEP2MaxLookupReached || GEP1MaxLookupReached)
132291bc56edSDimitry Andric return MayAlias;
132391bc56edSDimitry Andric
13243861d79fSDimitry Andric // Same offsets.
13253861d79fSDimitry Andric if (GEP1BaseOffset == GEP2BaseOffset &&
13263ca95b02SDimitry Andric DecompGEP1.VarIndices == DecompGEP2.VarIndices)
13273861d79fSDimitry Andric return NoAlias;
13283861d79fSDimitry Andric }
13293861d79fSDimitry Andric }
13303861d79fSDimitry Andric
1331f22ef01cSRoman Divacky // If we get a No or May, then return it immediately, no amount of analysis
1332f22ef01cSRoman Divacky // will improve this situation.
13332cab237bSDimitry Andric if (BaseAlias != MustAlias) {
13342cab237bSDimitry Andric assert(BaseAlias == NoAlias || BaseAlias == MayAlias);
13357d523365SDimitry Andric return BaseAlias;
13362cab237bSDimitry Andric }
1337f22ef01cSRoman Divacky
1338f22ef01cSRoman Divacky // Otherwise, we have a MustAlias. Since the base pointers alias each other
1339f22ef01cSRoman Divacky // exactly, see if the computed offset from the common pointer tells us
1340f22ef01cSRoman Divacky // about the relation of the resulting pointer.
1341ff0cc061SDimitry Andric // If we know the two GEPs are based off of the exact same pointer (and not
1342ff0cc061SDimitry Andric // just the same underlying object), see if that tells us anything about
1343ff0cc061SDimitry Andric // the resulting pointers.
13444ba319b5SDimitry Andric if (GEP1->getPointerOperand()->stripPointerCastsAndInvariantGroups() ==
13454ba319b5SDimitry Andric GEP2->getPointerOperand()->stripPointerCastsAndInvariantGroups() &&
13466bc11b14SDimitry Andric GEP1->getPointerOperandType() == GEP2->getPointerOperandType()) {
13477d523365SDimitry Andric AliasResult R = aliasSameBasePointerGEPs(GEP1, V1Size, GEP2, V2Size, DL);
1348ff0cc061SDimitry Andric // If we couldn't find anything interesting, don't abandon just yet.
1349ff0cc061SDimitry Andric if (R != MayAlias)
1350ff0cc061SDimitry Andric return R;
1351ff0cc061SDimitry Andric }
1352ff0cc061SDimitry Andric
13533ca95b02SDimitry Andric // If the max search depth is reached, the result is undefined
135491bc56edSDimitry Andric if (GEP2MaxLookupReached || GEP1MaxLookupReached)
135591bc56edSDimitry Andric return MayAlias;
1356f22ef01cSRoman Divacky
1357f22ef01cSRoman Divacky // Subtract the GEP2 pointer from the GEP1 pointer to find out their
1358f22ef01cSRoman Divacky // symbolic difference.
1359f22ef01cSRoman Divacky GEP1BaseOffset -= GEP2BaseOffset;
13603ca95b02SDimitry Andric GetIndexDifference(DecompGEP1.VarIndices, DecompGEP2.VarIndices);
1361f22ef01cSRoman Divacky
1362f22ef01cSRoman Divacky } else {
1363f22ef01cSRoman Divacky // Check to see if these two pointers are related by the getelementptr
1364f22ef01cSRoman Divacky // instruction. If one pointer is a GEP with a non-zero index of the other
1365f22ef01cSRoman Divacky // pointer, we know they cannot alias.
1366f22ef01cSRoman Divacky
1367f22ef01cSRoman Divacky // If both accesses are unknown size, we can't do anything useful here.
1368*b5893f02SDimitry Andric if (V1Size == LocationSize::unknown() && V2Size == LocationSize::unknown())
1369f22ef01cSRoman Divacky return MayAlias;
1370f22ef01cSRoman Divacky
1371*b5893f02SDimitry Andric AliasResult R =
1372*b5893f02SDimitry Andric aliasCheck(UnderlyingV1, LocationSize::unknown(), AAMDNodes(), V2,
1373*b5893f02SDimitry Andric LocationSize::unknown(), V2AAInfo, nullptr, UnderlyingV2);
13742cab237bSDimitry Andric if (R != MustAlias) {
1375f22ef01cSRoman Divacky // If V2 may alias GEP base pointer, conservatively returns MayAlias.
1376f22ef01cSRoman Divacky // If V2 is known not to alias GEP base pointer, then the two values
137798221d2eSDimitry Andric // cannot alias per GEP semantics: "Any memory access must be done through
137898221d2eSDimitry Andric // a pointer value associated with an address range of the memory access,
137998221d2eSDimitry Andric // otherwise the behavior is undefined.".
13802cab237bSDimitry Andric assert(R == NoAlias || R == MayAlias);
1381f22ef01cSRoman Divacky return R;
13822cab237bSDimitry Andric }
1383f22ef01cSRoman Divacky
138491bc56edSDimitry Andric // If the max search depth is reached the result is undefined
138591bc56edSDimitry Andric if (GEP1MaxLookupReached)
138691bc56edSDimitry Andric return MayAlias;
1387f22ef01cSRoman Divacky }
1388f22ef01cSRoman Divacky
1389f22ef01cSRoman Divacky // In the two GEP Case, if there is no difference in the offsets of the
1390f22ef01cSRoman Divacky // computed pointers, the resultant pointers are a must alias. This
13913ca95b02SDimitry Andric // happens when we have two lexically identical GEP's (for example).
1392f22ef01cSRoman Divacky //
1393f22ef01cSRoman Divacky // In the other case, if we have getelementptr <ptr>, 0, 0, 0, 0, ... and V2
1394f22ef01cSRoman Divacky // must aliases the GEP, the end result is a must alias also.
13953ca95b02SDimitry Andric if (GEP1BaseOffset == 0 && DecompGEP1.VarIndices.empty())
1396f22ef01cSRoman Divacky return MustAlias;
1397f22ef01cSRoman Divacky
13986122f3e6SDimitry Andric // If there is a constant difference between the pointers, but the difference
13996122f3e6SDimitry Andric // is less than the size of the associated memory object, then we know
14006122f3e6SDimitry Andric // that the objects are partially overlapping. If the difference is
14016122f3e6SDimitry Andric // greater, we know they do not overlap.
14023ca95b02SDimitry Andric if (GEP1BaseOffset != 0 && DecompGEP1.VarIndices.empty()) {
1403*b5893f02SDimitry Andric if (GEP1BaseOffset.sge(0)) {
1404*b5893f02SDimitry Andric if (V2Size != LocationSize::unknown()) {
1405*b5893f02SDimitry Andric if (GEP1BaseOffset.ult(V2Size.getValue()))
14062754fe60SDimitry Andric return PartialAlias;
14076122f3e6SDimitry Andric return NoAlias;
14086122f3e6SDimitry Andric }
14096122f3e6SDimitry Andric } else {
141085d60e68SDimitry Andric // We have the situation where:
141185d60e68SDimitry Andric // + +
141285d60e68SDimitry Andric // | BaseOffset |
141385d60e68SDimitry Andric // ---------------->|
141485d60e68SDimitry Andric // |-->V1Size |-------> V2Size
141585d60e68SDimitry Andric // GEP1 V2
141685d60e68SDimitry Andric // We need to know that V2Size is not unknown, otherwise we might have
141785d60e68SDimitry Andric // stripped a gep with negative index ('gep <ptr>, -1, ...).
1418*b5893f02SDimitry Andric if (V1Size != LocationSize::unknown() &&
1419*b5893f02SDimitry Andric V2Size != LocationSize::unknown()) {
1420*b5893f02SDimitry Andric if ((-GEP1BaseOffset).ult(V1Size.getValue()))
14216122f3e6SDimitry Andric return PartialAlias;
14226122f3e6SDimitry Andric return NoAlias;
14236122f3e6SDimitry Andric }
14246122f3e6SDimitry Andric }
14252754fe60SDimitry Andric }
14262754fe60SDimitry Andric
14273ca95b02SDimitry Andric if (!DecompGEP1.VarIndices.empty()) {
1428*b5893f02SDimitry Andric APInt Modulo(MaxPointerSize, 0);
14297d523365SDimitry Andric bool AllPositive = true;
14303ca95b02SDimitry Andric for (unsigned i = 0, e = DecompGEP1.VarIndices.size(); i != e; ++i) {
14317d523365SDimitry Andric
14327d523365SDimitry Andric // Try to distinguish something like &A[i][1] against &A[42][0].
14337d523365SDimitry Andric // Grab the least significant bit set in any of the scales. We
14347d523365SDimitry Andric // don't need std::abs here (even if the scale's negative) as we'll
14357d523365SDimitry Andric // be ^'ing Modulo with itself later.
1436*b5893f02SDimitry Andric Modulo |= DecompGEP1.VarIndices[i].Scale;
14377d523365SDimitry Andric
14387d523365SDimitry Andric if (AllPositive) {
14397d523365SDimitry Andric // If the Value could change between cycles, then any reasoning about
14407d523365SDimitry Andric // the Value this cycle may not hold in the next cycle. We'll just
14417d523365SDimitry Andric // give up if we can't determine conditions that hold for every cycle:
14423ca95b02SDimitry Andric const Value *V = DecompGEP1.VarIndices[i].V;
14437d523365SDimitry Andric
14445517e702SDimitry Andric KnownBits Known = computeKnownBits(V, DL, 0, &AC, nullptr, DT);
14455517e702SDimitry Andric bool SignKnownZero = Known.isNonNegative();
14465517e702SDimitry Andric bool SignKnownOne = Known.isNegative();
14477d523365SDimitry Andric
14487d523365SDimitry Andric // Zero-extension widens the variable, and so forces the sign
14497d523365SDimitry Andric // bit to zero.
14503ca95b02SDimitry Andric bool IsZExt = DecompGEP1.VarIndices[i].ZExtBits > 0 || isa<ZExtInst>(V);
14517d523365SDimitry Andric SignKnownZero |= IsZExt;
14527d523365SDimitry Andric SignKnownOne &= !IsZExt;
14537d523365SDimitry Andric
14547d523365SDimitry Andric // If the variable begins with a zero then we know it's
14557d523365SDimitry Andric // positive, regardless of whether the value is signed or
14567d523365SDimitry Andric // unsigned.
1457*b5893f02SDimitry Andric APInt Scale = DecompGEP1.VarIndices[i].Scale;
14587d523365SDimitry Andric AllPositive =
1459*b5893f02SDimitry Andric (SignKnownZero && Scale.sge(0)) || (SignKnownOne && Scale.slt(0));
14607d523365SDimitry Andric }
14617d523365SDimitry Andric }
14627d523365SDimitry Andric
14636122f3e6SDimitry Andric Modulo = Modulo ^ (Modulo & (Modulo - 1));
1464f22ef01cSRoman Divacky
14656122f3e6SDimitry Andric // We can compute the difference between the two addresses
14666122f3e6SDimitry Andric // mod Modulo. Check whether that difference guarantees that the
14676122f3e6SDimitry Andric // two locations do not alias.
1468*b5893f02SDimitry Andric APInt ModOffset = GEP1BaseOffset & (Modulo - 1);
1469*b5893f02SDimitry Andric if (V1Size != LocationSize::unknown() &&
1470*b5893f02SDimitry Andric V2Size != LocationSize::unknown() && ModOffset.uge(V2Size.getValue()) &&
1471*b5893f02SDimitry Andric (Modulo - ModOffset).uge(V1Size.getValue()))
1472f22ef01cSRoman Divacky return NoAlias;
14737d523365SDimitry Andric
14747d523365SDimitry Andric // If we know all the variables are positive, then GEP1 >= GEP1BasePtr.
14757d523365SDimitry Andric // If GEP1BasePtr > V2 (GEP1BaseOffset > 0) then we know the pointers
14767d523365SDimitry Andric // don't alias if V2Size can fit in the gap between V2 and GEP1BasePtr.
1477*b5893f02SDimitry Andric if (AllPositive && GEP1BaseOffset.sgt(0) &&
1478*b5893f02SDimitry Andric V2Size != LocationSize::unknown() &&
1479*b5893f02SDimitry Andric GEP1BaseOffset.uge(V2Size.getValue()))
14807d523365SDimitry Andric return NoAlias;
14817d523365SDimitry Andric
14823ca95b02SDimitry Andric if (constantOffsetHeuristic(DecompGEP1.VarIndices, V1Size, V2Size,
14837d523365SDimitry Andric GEP1BaseOffset, &AC, DT))
14847d523365SDimitry Andric return NoAlias;
1485f22ef01cSRoman Divacky }
1486f22ef01cSRoman Divacky
1487bd5abe19SDimitry Andric // Statically, we can see that the base objects are the same, but the
1488bd5abe19SDimitry Andric // pointers have dynamic offsets which we can't resolve. And none of our
1489bd5abe19SDimitry Andric // little tricks above worked.
1490edd7eaddSDimitry Andric return MayAlias;
1491bd5abe19SDimitry Andric }
1492bd5abe19SDimitry Andric
MergeAliasResults(AliasResult A,AliasResult B)14933dac3a9bSDimitry Andric static AliasResult MergeAliasResults(AliasResult A, AliasResult B) {
1494bd5abe19SDimitry Andric // If the results agree, take it.
1495bd5abe19SDimitry Andric if (A == B)
1496bd5abe19SDimitry Andric return A;
1497bd5abe19SDimitry Andric // A mix of PartialAlias and MustAlias is PartialAlias.
14983dac3a9bSDimitry Andric if ((A == PartialAlias && B == MustAlias) ||
14993dac3a9bSDimitry Andric (B == PartialAlias && A == MustAlias))
15003dac3a9bSDimitry Andric return PartialAlias;
1501bd5abe19SDimitry Andric // Otherwise, we don't know anything.
15023dac3a9bSDimitry Andric return MayAlias;
1503f22ef01cSRoman Divacky }
1504f22ef01cSRoman Divacky
15057d523365SDimitry Andric /// Provides a bunch of ad-hoc rules to disambiguate a Select instruction
15067d523365SDimitry Andric /// against another.
aliasSelect(const SelectInst * SI,LocationSize SISize,const AAMDNodes & SIAAInfo,const Value * V2,LocationSize V2Size,const AAMDNodes & V2AAInfo,const Value * UnderV2)15074ba319b5SDimitry Andric AliasResult BasicAAResult::aliasSelect(const SelectInst *SI,
15084ba319b5SDimitry Andric LocationSize SISize,
150939d628a0SDimitry Andric const AAMDNodes &SIAAInfo,
15104ba319b5SDimitry Andric const Value *V2, LocationSize V2Size,
1511d88c1a5aSDimitry Andric const AAMDNodes &V2AAInfo,
1512d88c1a5aSDimitry Andric const Value *UnderV2) {
1513f22ef01cSRoman Divacky // If the values are Selects with the same condition, we can do a more precise
1514f22ef01cSRoman Divacky // check: just check for aliases between the values on corresponding arms.
1515f22ef01cSRoman Divacky if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2))
1516f22ef01cSRoman Divacky if (SI->getCondition() == SI2->getCondition()) {
15177d523365SDimitry Andric AliasResult Alias = aliasCheck(SI->getTrueValue(), SISize, SIAAInfo,
151839d628a0SDimitry Andric SI2->getTrueValue(), V2Size, V2AAInfo);
1519f22ef01cSRoman Divacky if (Alias == MayAlias)
1520f22ef01cSRoman Divacky return MayAlias;
1521f22ef01cSRoman Divacky AliasResult ThisAlias =
152239d628a0SDimitry Andric aliasCheck(SI->getFalseValue(), SISize, SIAAInfo,
152339d628a0SDimitry Andric SI2->getFalseValue(), V2Size, V2AAInfo);
1524bd5abe19SDimitry Andric return MergeAliasResults(ThisAlias, Alias);
1525f22ef01cSRoman Divacky }
1526f22ef01cSRoman Divacky
1527f22ef01cSRoman Divacky // If both arms of the Select node NoAlias or MustAlias V2, then returns
1528f22ef01cSRoman Divacky // NoAlias / MustAlias. Otherwise, returns MayAlias.
1529f22ef01cSRoman Divacky AliasResult Alias =
1530d88c1a5aSDimitry Andric aliasCheck(V2, V2Size, V2AAInfo, SI->getTrueValue(),
1531d88c1a5aSDimitry Andric SISize, SIAAInfo, UnderV2);
1532f22ef01cSRoman Divacky if (Alias == MayAlias)
1533f22ef01cSRoman Divacky return MayAlias;
1534ffd1746dSEd Schouten
1535f22ef01cSRoman Divacky AliasResult ThisAlias =
1536d88c1a5aSDimitry Andric aliasCheck(V2, V2Size, V2AAInfo, SI->getFalseValue(), SISize, SIAAInfo,
1537d88c1a5aSDimitry Andric UnderV2);
1538bd5abe19SDimitry Andric return MergeAliasResults(ThisAlias, Alias);
1539f22ef01cSRoman Divacky }
1540f22ef01cSRoman Divacky
15417d523365SDimitry Andric /// Provide a bunch of ad-hoc rules to disambiguate a PHI instruction against
15427d523365SDimitry Andric /// another.
aliasPHI(const PHINode * PN,LocationSize PNSize,const AAMDNodes & PNAAInfo,const Value * V2,LocationSize V2Size,const AAMDNodes & V2AAInfo,const Value * UnderV2)15434ba319b5SDimitry Andric AliasResult BasicAAResult::aliasPHI(const PHINode *PN, LocationSize PNSize,
15447d523365SDimitry Andric const AAMDNodes &PNAAInfo, const Value *V2,
15454ba319b5SDimitry Andric LocationSize V2Size,
15464ba319b5SDimitry Andric const AAMDNodes &V2AAInfo,
1547d88c1a5aSDimitry Andric const Value *UnderV2) {
154885d60e68SDimitry Andric // Track phi nodes we have visited. We use this information when we determine
154985d60e68SDimitry Andric // value equivalence.
155085d60e68SDimitry Andric VisitedPhiBBs.insert(PN->getParent());
155185d60e68SDimitry Andric
1552f22ef01cSRoman Divacky // If the values are PHIs in the same block, we can do a more precise
1553f22ef01cSRoman Divacky // as well as efficient check: just check for aliases between the values
1554f22ef01cSRoman Divacky // on corresponding edges.
1555f22ef01cSRoman Divacky if (const PHINode *PN2 = dyn_cast<PHINode>(V2))
1556f22ef01cSRoman Divacky if (PN2->getParent() == PN->getParent()) {
15578f0fd8f6SDimitry Andric LocPair Locs(MemoryLocation(PN, PNSize, PNAAInfo),
15588f0fd8f6SDimitry Andric MemoryLocation(V2, V2Size, V2AAInfo));
15593861d79fSDimitry Andric if (PN > V2)
15603861d79fSDimitry Andric std::swap(Locs.first, Locs.second);
1561139f7f9bSDimitry Andric // Analyse the PHIs' inputs under the assumption that the PHIs are
1562139f7f9bSDimitry Andric // NoAlias.
1563139f7f9bSDimitry Andric // If the PHIs are May/MustAlias there must be (recursively) an input
1564139f7f9bSDimitry Andric // operand from outside the PHIs' cycle that is MayAlias/MustAlias or
1565139f7f9bSDimitry Andric // there must be an operation on the PHIs within the PHIs' value cycle
1566139f7f9bSDimitry Andric // that causes a MayAlias.
15673861d79fSDimitry Andric // Pretend the phis do not alias.
1568139f7f9bSDimitry Andric AliasResult Alias = NoAlias;
15693861d79fSDimitry Andric assert(AliasCache.count(Locs) &&
15703861d79fSDimitry Andric "There must exist an entry for the phi node");
1571139f7f9bSDimitry Andric AliasResult OrigAliasResult = AliasCache[Locs];
15723861d79fSDimitry Andric AliasCache[Locs] = NoAlias;
15733861d79fSDimitry Andric
1574139f7f9bSDimitry Andric for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1575f22ef01cSRoman Divacky AliasResult ThisAlias =
157639d628a0SDimitry Andric aliasCheck(PN->getIncomingValue(i), PNSize, PNAAInfo,
1577f22ef01cSRoman Divacky PN2->getIncomingValueForBlock(PN->getIncomingBlock(i)),
157839d628a0SDimitry Andric V2Size, V2AAInfo);
1579bd5abe19SDimitry Andric Alias = MergeAliasResults(ThisAlias, Alias);
1580bd5abe19SDimitry Andric if (Alias == MayAlias)
1581bd5abe19SDimitry Andric break;
1582f22ef01cSRoman Divacky }
15833861d79fSDimitry Andric
15843861d79fSDimitry Andric // Reset if speculation failed.
1585139f7f9bSDimitry Andric if (Alias != NoAlias)
15863861d79fSDimitry Andric AliasCache[Locs] = OrigAliasResult;
15873861d79fSDimitry Andric
1588f22ef01cSRoman Divacky return Alias;
1589f22ef01cSRoman Divacky }
1590f22ef01cSRoman Divacky
1591f22ef01cSRoman Divacky SmallVector<Value *, 4> V1Srcs;
15927d523365SDimitry Andric bool isRecursive = false;
15934ba319b5SDimitry Andric if (PV) {
15944ba319b5SDimitry Andric // If we have PhiValues then use it to get the underlying phi values.
15954ba319b5SDimitry Andric const PhiValues::ValueSet &PhiValueSet = PV->getValuesForPhi(PN);
15964ba319b5SDimitry Andric // If we have more phi values than the search depth then return MayAlias
15974ba319b5SDimitry Andric // conservatively to avoid compile time explosion. The worst possible case
15984ba319b5SDimitry Andric // is if both sides are PHI nodes. In which case, this is O(m x n) time
15994ba319b5SDimitry Andric // where 'm' and 'n' are the number of PHI sources.
16004ba319b5SDimitry Andric if (PhiValueSet.size() > MaxLookupSearchDepth)
16014ba319b5SDimitry Andric return MayAlias;
16024ba319b5SDimitry Andric // Add the values to V1Srcs
16034ba319b5SDimitry Andric for (Value *PV1 : PhiValueSet) {
16044ba319b5SDimitry Andric if (EnableRecPhiAnalysis) {
16054ba319b5SDimitry Andric if (GEPOperator *PV1GEP = dyn_cast<GEPOperator>(PV1)) {
16064ba319b5SDimitry Andric // Check whether the incoming value is a GEP that advances the pointer
16074ba319b5SDimitry Andric // result of this PHI node (e.g. in a loop). If this is the case, we
16084ba319b5SDimitry Andric // would recurse and always get a MayAlias. Handle this case specially
16094ba319b5SDimitry Andric // below.
16104ba319b5SDimitry Andric if (PV1GEP->getPointerOperand() == PN && PV1GEP->getNumIndices() == 1 &&
16114ba319b5SDimitry Andric isa<ConstantInt>(PV1GEP->idx_begin())) {
16124ba319b5SDimitry Andric isRecursive = true;
16134ba319b5SDimitry Andric continue;
16144ba319b5SDimitry Andric }
16154ba319b5SDimitry Andric }
16164ba319b5SDimitry Andric }
16174ba319b5SDimitry Andric V1Srcs.push_back(PV1);
16184ba319b5SDimitry Andric }
16194ba319b5SDimitry Andric } else {
16204ba319b5SDimitry Andric // If we don't have PhiInfo then just look at the operands of the phi itself
16214ba319b5SDimitry Andric // FIXME: Remove this once we can guarantee that we have PhiInfo always
16224ba319b5SDimitry Andric SmallPtrSet<Value *, 4> UniqueSrc;
1623ff0cc061SDimitry Andric for (Value *PV1 : PN->incoming_values()) {
1624f22ef01cSRoman Divacky if (isa<PHINode>(PV1))
1625f22ef01cSRoman Divacky // If any of the source itself is a PHI, return MayAlias conservatively
1626f22ef01cSRoman Divacky // to avoid compile time explosion. The worst possible case is if both
1627f22ef01cSRoman Divacky // sides are PHI nodes. In which case, this is O(m x n) time where 'm'
1628f22ef01cSRoman Divacky // and 'n' are the number of PHI sources.
1629f22ef01cSRoman Divacky return MayAlias;
16307d523365SDimitry Andric
16317d523365SDimitry Andric if (EnableRecPhiAnalysis)
16327d523365SDimitry Andric if (GEPOperator *PV1GEP = dyn_cast<GEPOperator>(PV1)) {
16337d523365SDimitry Andric // Check whether the incoming value is a GEP that advances the pointer
16347d523365SDimitry Andric // result of this PHI node (e.g. in a loop). If this is the case, we
16357d523365SDimitry Andric // would recurse and always get a MayAlias. Handle this case specially
16367d523365SDimitry Andric // below.
16377d523365SDimitry Andric if (PV1GEP->getPointerOperand() == PN && PV1GEP->getNumIndices() == 1 &&
16387d523365SDimitry Andric isa<ConstantInt>(PV1GEP->idx_begin())) {
16397d523365SDimitry Andric isRecursive = true;
16407d523365SDimitry Andric continue;
16417d523365SDimitry Andric }
16427d523365SDimitry Andric }
16437d523365SDimitry Andric
164439d628a0SDimitry Andric if (UniqueSrc.insert(PV1).second)
1645f22ef01cSRoman Divacky V1Srcs.push_back(PV1);
1646f22ef01cSRoman Divacky }
16474ba319b5SDimitry Andric }
16484ba319b5SDimitry Andric
16494ba319b5SDimitry Andric // If V1Srcs is empty then that means that the phi has no underlying non-phi
16504ba319b5SDimitry Andric // value. This should only be possible in blocks unreachable from the entry
16514ba319b5SDimitry Andric // block, but return MayAlias just in case.
16524ba319b5SDimitry Andric if (V1Srcs.empty())
16534ba319b5SDimitry Andric return MayAlias;
1654f22ef01cSRoman Divacky
16557d523365SDimitry Andric // If this PHI node is recursive, set the size of the accessed memory to
16567d523365SDimitry Andric // unknown to represent all the possible values the GEP could advance the
16577d523365SDimitry Andric // pointer to.
16587d523365SDimitry Andric if (isRecursive)
1659*b5893f02SDimitry Andric PNSize = LocationSize::unknown();
16607d523365SDimitry Andric
16617d523365SDimitry Andric AliasResult Alias =
1662d88c1a5aSDimitry Andric aliasCheck(V2, V2Size, V2AAInfo, V1Srcs[0],
1663d88c1a5aSDimitry Andric PNSize, PNAAInfo, UnderV2);
16647d523365SDimitry Andric
1665f22ef01cSRoman Divacky // Early exit if the check of the first PHI source against V2 is MayAlias.
1666f22ef01cSRoman Divacky // Other results are not possible.
1667f22ef01cSRoman Divacky if (Alias == MayAlias)
1668f22ef01cSRoman Divacky return MayAlias;
1669f22ef01cSRoman Divacky
1670f22ef01cSRoman Divacky // If all sources of the PHI node NoAlias or MustAlias V2, then returns
1671f22ef01cSRoman Divacky // NoAlias / MustAlias. Otherwise, returns MayAlias.
1672f22ef01cSRoman Divacky for (unsigned i = 1, e = V1Srcs.size(); i != e; ++i) {
1673f22ef01cSRoman Divacky Value *V = V1Srcs[i];
1674f22ef01cSRoman Divacky
16757d523365SDimitry Andric AliasResult ThisAlias =
1676d88c1a5aSDimitry Andric aliasCheck(V2, V2Size, V2AAInfo, V, PNSize, PNAAInfo, UnderV2);
1677bd5abe19SDimitry Andric Alias = MergeAliasResults(ThisAlias, Alias);
1678bd5abe19SDimitry Andric if (Alias == MayAlias)
1679bd5abe19SDimitry Andric break;
1680f22ef01cSRoman Divacky }
1681f22ef01cSRoman Divacky
1682f22ef01cSRoman Divacky return Alias;
1683f22ef01cSRoman Divacky }
1684f22ef01cSRoman Divacky
16857d523365SDimitry Andric /// Provides a bunch of ad-hoc rules to disambiguate in common cases, such as
16867d523365SDimitry Andric /// array references.
aliasCheck(const Value * V1,LocationSize V1Size,AAMDNodes V1AAInfo,const Value * V2,LocationSize V2Size,AAMDNodes V2AAInfo,const Value * O1,const Value * O2)16874ba319b5SDimitry Andric AliasResult BasicAAResult::aliasCheck(const Value *V1, LocationSize V1Size,
16883dac3a9bSDimitry Andric AAMDNodes V1AAInfo, const Value *V2,
16894ba319b5SDimitry Andric LocationSize V2Size, AAMDNodes V2AAInfo,
1690d88c1a5aSDimitry Andric const Value *O1, const Value *O2) {
1691f22ef01cSRoman Divacky // If either of the memory references is empty, it doesn't matter what the
1692f22ef01cSRoman Divacky // pointer values are.
1693*b5893f02SDimitry Andric if (V1Size.isZero() || V2Size.isZero())
1694f22ef01cSRoman Divacky return NoAlias;
1695f22ef01cSRoman Divacky
1696f22ef01cSRoman Divacky // Strip off any casts if they exist.
16974ba319b5SDimitry Andric V1 = V1->stripPointerCastsAndInvariantGroups();
16984ba319b5SDimitry Andric V2 = V2->stripPointerCastsAndInvariantGroups();
1699f22ef01cSRoman Divacky
1700ff0cc061SDimitry Andric // If V1 or V2 is undef, the result is NoAlias because we can always pick a
1701ff0cc061SDimitry Andric // value for undef that aliases nothing in the program.
1702ff0cc061SDimitry Andric if (isa<UndefValue>(V1) || isa<UndefValue>(V2))
1703ff0cc061SDimitry Andric return NoAlias;
1704ff0cc061SDimitry Andric
1705f22ef01cSRoman Divacky // Are we checking for alias of the same value?
17063ca95b02SDimitry Andric // Because we look 'through' phi nodes, we could look at "Value" pointers from
170785d60e68SDimitry Andric // different iterations. We must therefore make sure that this is not the
170885d60e68SDimitry Andric // case. The function isValueEqualInPotentialCycles ensures that this cannot
170985d60e68SDimitry Andric // happen by looking at the visited phi nodes and making sure they cannot
171085d60e68SDimitry Andric // reach the value.
171185d60e68SDimitry Andric if (isValueEqualInPotentialCycles(V1, V2))
171285d60e68SDimitry Andric return MustAlias;
1713f22ef01cSRoman Divacky
1714f22ef01cSRoman Divacky if (!V1->getType()->isPointerTy() || !V2->getType()->isPointerTy())
1715f22ef01cSRoman Divacky return NoAlias; // Scalars cannot alias each other
1716f22ef01cSRoman Divacky
1717f22ef01cSRoman Divacky // Figure out what objects these things are pointing to if we can.
1718d88c1a5aSDimitry Andric if (O1 == nullptr)
1719d88c1a5aSDimitry Andric O1 = GetUnderlyingObject(V1, DL, MaxLookupSearchDepth);
1720d88c1a5aSDimitry Andric
1721d88c1a5aSDimitry Andric if (O2 == nullptr)
1722d88c1a5aSDimitry Andric O2 = GetUnderlyingObject(V2, DL, MaxLookupSearchDepth);
1723f22ef01cSRoman Divacky
1724f22ef01cSRoman Divacky // Null values in the default address space don't point to any object, so they
1725f22ef01cSRoman Divacky // don't alias any other pointer.
1726f22ef01cSRoman Divacky if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O1))
17274ba319b5SDimitry Andric if (!NullPointerIsDefined(&F, CPN->getType()->getAddressSpace()))
1728f22ef01cSRoman Divacky return NoAlias;
1729f22ef01cSRoman Divacky if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O2))
17304ba319b5SDimitry Andric if (!NullPointerIsDefined(&F, CPN->getType()->getAddressSpace()))
1731f22ef01cSRoman Divacky return NoAlias;
1732f22ef01cSRoman Divacky
1733f22ef01cSRoman Divacky if (O1 != O2) {
17343ca95b02SDimitry Andric // If V1/V2 point to two different objects, we know that we have no alias.
1735f22ef01cSRoman Divacky if (isIdentifiedObject(O1) && isIdentifiedObject(O2))
1736f22ef01cSRoman Divacky return NoAlias;
1737f22ef01cSRoman Divacky
1738f22ef01cSRoman Divacky // Constant pointers can't alias with non-const isIdentifiedObject objects.
1739f22ef01cSRoman Divacky if ((isa<Constant>(O1) && isIdentifiedObject(O2) && !isa<Constant>(O2)) ||
1740f22ef01cSRoman Divacky (isa<Constant>(O2) && isIdentifiedObject(O1) && !isa<Constant>(O1)))
1741f22ef01cSRoman Divacky return NoAlias;
1742f22ef01cSRoman Divacky
1743f785676fSDimitry Andric // Function arguments can't alias with things that are known to be
1744f785676fSDimitry Andric // unambigously identified at the function level.
1745f785676fSDimitry Andric if ((isa<Argument>(O1) && isIdentifiedFunctionLocal(O2)) ||
1746f785676fSDimitry Andric (isa<Argument>(O2) && isIdentifiedFunctionLocal(O1)))
1747f22ef01cSRoman Divacky return NoAlias;
1748f22ef01cSRoman Divacky
1749ffd1746dSEd Schouten // If one pointer is the result of a call/invoke or load and the other is a
1750ffd1746dSEd Schouten // non-escaping local object within the same function, then we know the
1751ffd1746dSEd Schouten // object couldn't escape to a point where the call could return it.
1752ffd1746dSEd Schouten //
1753ffd1746dSEd Schouten // Note that if the pointers are in different functions, there are a
1754ffd1746dSEd Schouten // variety of complications. A call with a nocapture argument may still
1755ffd1746dSEd Schouten // temporary store the nocapture argument's value in a temporary memory
1756ffd1746dSEd Schouten // location if that memory location doesn't escape. Or it may pass a
1757ffd1746dSEd Schouten // nocapture value to other functions as long as they don't capture it.
1758ffd1746dSEd Schouten if (isEscapeSource(O1) && isNonEscapingLocalObject(O2))
1759ffd1746dSEd Schouten return NoAlias;
1760ffd1746dSEd Schouten if (isEscapeSource(O2) && isNonEscapingLocalObject(O1))
1761f22ef01cSRoman Divacky return NoAlias;
1762f22ef01cSRoman Divacky }
1763f22ef01cSRoman Divacky
1764f22ef01cSRoman Divacky // If the size of one access is larger than the entire object on the other
1765f22ef01cSRoman Divacky // side, then we know such behavior is undefined and can assume no alias.
17664ba319b5SDimitry Andric bool NullIsValidLocation = NullPointerIsDefined(&F);
1767*b5893f02SDimitry Andric if ((V1Size.isPrecise() && isObjectSmallerThan(O2, V1Size.getValue(), DL, TLI,
1768*b5893f02SDimitry Andric NullIsValidLocation)) ||
1769*b5893f02SDimitry Andric (V2Size.isPrecise() && isObjectSmallerThan(O1, V2Size.getValue(), DL, TLI,
1770*b5893f02SDimitry Andric NullIsValidLocation)))
1771f22ef01cSRoman Divacky return NoAlias;
1772f22ef01cSRoman Divacky
1773bd5abe19SDimitry Andric // Check the cache before climbing up use-def chains. This also terminates
1774bd5abe19SDimitry Andric // otherwise infinitely recursive queries.
17758f0fd8f6SDimitry Andric LocPair Locs(MemoryLocation(V1, V1Size, V1AAInfo),
17768f0fd8f6SDimitry Andric MemoryLocation(V2, V2Size, V2AAInfo));
1777bd5abe19SDimitry Andric if (V1 > V2)
1778bd5abe19SDimitry Andric std::swap(Locs.first, Locs.second);
1779bd5abe19SDimitry Andric std::pair<AliasCacheTy::iterator, bool> Pair =
1780bd5abe19SDimitry Andric AliasCache.insert(std::make_pair(Locs, MayAlias));
1781bd5abe19SDimitry Andric if (!Pair.second)
1782bd5abe19SDimitry Andric return Pair.first->second;
1783bd5abe19SDimitry Andric
1784f22ef01cSRoman Divacky // FIXME: This isn't aggressively handling alias(GEP, PHI) for example: if the
1785f22ef01cSRoman Divacky // GEP can't simplify, we don't even look at the PHI cases.
1786f22ef01cSRoman Divacky if (!isa<GEPOperator>(V1) && isa<GEPOperator>(V2)) {
1787f22ef01cSRoman Divacky std::swap(V1, V2);
1788f22ef01cSRoman Divacky std::swap(V1Size, V2Size);
1789f22ef01cSRoman Divacky std::swap(O1, O2);
179039d628a0SDimitry Andric std::swap(V1AAInfo, V2AAInfo);
1791f22ef01cSRoman Divacky }
17922754fe60SDimitry Andric if (const GEPOperator *GV1 = dyn_cast<GEPOperator>(V1)) {
17937d523365SDimitry Andric AliasResult Result =
17947d523365SDimitry Andric aliasGEP(GV1, V1Size, V1AAInfo, V2, V2Size, V2AAInfo, O1, O2);
17957d523365SDimitry Andric if (Result != MayAlias)
17967d523365SDimitry Andric return AliasCache[Locs] = Result;
17972754fe60SDimitry Andric }
1798f22ef01cSRoman Divacky
1799f22ef01cSRoman Divacky if (isa<PHINode>(V2) && !isa<PHINode>(V1)) {
1800f22ef01cSRoman Divacky std::swap(V1, V2);
1801d88c1a5aSDimitry Andric std::swap(O1, O2);
1802f22ef01cSRoman Divacky std::swap(V1Size, V2Size);
180339d628a0SDimitry Andric std::swap(V1AAInfo, V2AAInfo);
1804f22ef01cSRoman Divacky }
18052754fe60SDimitry Andric if (const PHINode *PN = dyn_cast<PHINode>(V1)) {
1806d88c1a5aSDimitry Andric AliasResult Result = aliasPHI(PN, V1Size, V1AAInfo,
1807d88c1a5aSDimitry Andric V2, V2Size, V2AAInfo, O2);
18087d523365SDimitry Andric if (Result != MayAlias)
18097d523365SDimitry Andric return AliasCache[Locs] = Result;
18102754fe60SDimitry Andric }
1811f22ef01cSRoman Divacky
1812f22ef01cSRoman Divacky if (isa<SelectInst>(V2) && !isa<SelectInst>(V1)) {
1813f22ef01cSRoman Divacky std::swap(V1, V2);
1814d88c1a5aSDimitry Andric std::swap(O1, O2);
1815f22ef01cSRoman Divacky std::swap(V1Size, V2Size);
181639d628a0SDimitry Andric std::swap(V1AAInfo, V2AAInfo);
1817f22ef01cSRoman Divacky }
18182754fe60SDimitry Andric if (const SelectInst *S1 = dyn_cast<SelectInst>(V1)) {
18197d523365SDimitry Andric AliasResult Result =
1820d88c1a5aSDimitry Andric aliasSelect(S1, V1Size, V1AAInfo, V2, V2Size, V2AAInfo, O2);
18217d523365SDimitry Andric if (Result != MayAlias)
18227d523365SDimitry Andric return AliasCache[Locs] = Result;
1823f22ef01cSRoman Divacky }
1824f22ef01cSRoman Divacky
18252754fe60SDimitry Andric // If both pointers are pointing into the same object and one of them
18263ca95b02SDimitry Andric // accesses the entire object, then the accesses must overlap in some way.
18277d523365SDimitry Andric if (O1 == O2)
1828*b5893f02SDimitry Andric if (V1Size.isPrecise() && V2Size.isPrecise() &&
1829*b5893f02SDimitry Andric (isObjectSize(O1, V1Size.getValue(), DL, TLI, NullIsValidLocation) ||
1830*b5893f02SDimitry Andric isObjectSize(O2, V2Size.getValue(), DL, TLI, NullIsValidLocation)))
1831bd5abe19SDimitry Andric return AliasCache[Locs] = PartialAlias;
18322754fe60SDimitry Andric
18337d523365SDimitry Andric // Recurse back into the best AA results we have, potentially with refined
18347d523365SDimitry Andric // memory locations. We have already ensured that BasicAA has a MayAlias
18357d523365SDimitry Andric // cache result for these, so any recursion back into BasicAA won't loop.
18367d523365SDimitry Andric AliasResult Result = getBestAAResults().alias(Locs.first, Locs.second);
1837bd5abe19SDimitry Andric return AliasCache[Locs] = Result;
18382754fe60SDimitry Andric }
183985d60e68SDimitry Andric
18407d523365SDimitry Andric /// Check whether two Values can be considered equivalent.
18417d523365SDimitry Andric ///
18427d523365SDimitry Andric /// In addition to pointer equivalence of \p V1 and \p V2 this checks whether
18437d523365SDimitry Andric /// they can not be part of a cycle in the value graph by looking at all
18447d523365SDimitry Andric /// visited phi nodes an making sure that the phis cannot reach the value. We
18457d523365SDimitry Andric /// have to do this because we are looking through phi nodes (That is we say
18467d523365SDimitry Andric /// noalias(V, phi(VA, VB)) if noalias(V, VA) and noalias(V, VB).
isValueEqualInPotentialCycles(const Value * V,const Value * V2)18477d523365SDimitry Andric bool BasicAAResult::isValueEqualInPotentialCycles(const Value *V,
184885d60e68SDimitry Andric const Value *V2) {
184985d60e68SDimitry Andric if (V != V2)
185085d60e68SDimitry Andric return false;
185185d60e68SDimitry Andric
185285d60e68SDimitry Andric const Instruction *Inst = dyn_cast<Instruction>(V);
185385d60e68SDimitry Andric if (!Inst)
185485d60e68SDimitry Andric return true;
185585d60e68SDimitry Andric
1856ff0cc061SDimitry Andric if (VisitedPhiBBs.empty())
1857ff0cc061SDimitry Andric return true;
1858ff0cc061SDimitry Andric
185985d60e68SDimitry Andric if (VisitedPhiBBs.size() > MaxNumPhiBBsValueReachabilityCheck)
186085d60e68SDimitry Andric return false;
186185d60e68SDimitry Andric
186285d60e68SDimitry Andric // Make sure that the visited phis cannot reach the Value. This ensures that
186385d60e68SDimitry Andric // the Values cannot come from different iterations of a potential cycle the
186485d60e68SDimitry Andric // phi nodes could be involved in.
186539d628a0SDimitry Andric for (auto *P : VisitedPhiBBs)
18667d523365SDimitry Andric if (isPotentiallyReachable(&P->front(), Inst, DT, LI))
186785d60e68SDimitry Andric return false;
186885d60e68SDimitry Andric
186985d60e68SDimitry Andric return true;
187085d60e68SDimitry Andric }
187185d60e68SDimitry Andric
18727d523365SDimitry Andric /// Computes the symbolic difference between two de-composed GEPs.
18737d523365SDimitry Andric ///
18747d523365SDimitry Andric /// Dest and Src are the variable indices from two decomposed GetElementPtr
18757d523365SDimitry Andric /// instructions GEP1 and GEP2 which have common base pointers.
GetIndexDifference(SmallVectorImpl<VariableGEPIndex> & Dest,const SmallVectorImpl<VariableGEPIndex> & Src)18767d523365SDimitry Andric void BasicAAResult::GetIndexDifference(
187785d60e68SDimitry Andric SmallVectorImpl<VariableGEPIndex> &Dest,
187885d60e68SDimitry Andric const SmallVectorImpl<VariableGEPIndex> &Src) {
187985d60e68SDimitry Andric if (Src.empty())
188085d60e68SDimitry Andric return;
188185d60e68SDimitry Andric
188285d60e68SDimitry Andric for (unsigned i = 0, e = Src.size(); i != e; ++i) {
188385d60e68SDimitry Andric const Value *V = Src[i].V;
18847d523365SDimitry Andric unsigned ZExtBits = Src[i].ZExtBits, SExtBits = Src[i].SExtBits;
1885*b5893f02SDimitry Andric APInt Scale = Src[i].Scale;
188685d60e68SDimitry Andric
188785d60e68SDimitry Andric // Find V in Dest. This is N^2, but pointer indices almost never have more
188885d60e68SDimitry Andric // than a few variable indexes.
188985d60e68SDimitry Andric for (unsigned j = 0, e = Dest.size(); j != e; ++j) {
189085d60e68SDimitry Andric if (!isValueEqualInPotentialCycles(Dest[j].V, V) ||
18917d523365SDimitry Andric Dest[j].ZExtBits != ZExtBits || Dest[j].SExtBits != SExtBits)
189285d60e68SDimitry Andric continue;
189385d60e68SDimitry Andric
189485d60e68SDimitry Andric // If we found it, subtract off Scale V's from the entry in Dest. If it
189585d60e68SDimitry Andric // goes to zero, remove the entry.
189685d60e68SDimitry Andric if (Dest[j].Scale != Scale)
189785d60e68SDimitry Andric Dest[j].Scale -= Scale;
189885d60e68SDimitry Andric else
189985d60e68SDimitry Andric Dest.erase(Dest.begin() + j);
190085d60e68SDimitry Andric Scale = 0;
190185d60e68SDimitry Andric break;
190285d60e68SDimitry Andric }
190385d60e68SDimitry Andric
190485d60e68SDimitry Andric // If we didn't consume this entry, add it to the end of the Dest list.
1905*b5893f02SDimitry Andric if (!!Scale) {
19067d523365SDimitry Andric VariableGEPIndex Entry = {V, ZExtBits, SExtBits, -Scale};
190785d60e68SDimitry Andric Dest.push_back(Entry);
190885d60e68SDimitry Andric }
190985d60e68SDimitry Andric }
191085d60e68SDimitry Andric }
19117d523365SDimitry Andric
constantOffsetHeuristic(const SmallVectorImpl<VariableGEPIndex> & VarIndices,LocationSize MaybeV1Size,LocationSize MaybeV2Size,APInt BaseOffset,AssumptionCache * AC,DominatorTree * DT)19127d523365SDimitry Andric bool BasicAAResult::constantOffsetHeuristic(
1913*b5893f02SDimitry Andric const SmallVectorImpl<VariableGEPIndex> &VarIndices,
1914*b5893f02SDimitry Andric LocationSize MaybeV1Size, LocationSize MaybeV2Size, APInt BaseOffset,
1915*b5893f02SDimitry Andric AssumptionCache *AC, DominatorTree *DT) {
1916*b5893f02SDimitry Andric if (VarIndices.size() != 2 || MaybeV1Size == LocationSize::unknown() ||
1917*b5893f02SDimitry Andric MaybeV2Size == LocationSize::unknown())
19187d523365SDimitry Andric return false;
19197d523365SDimitry Andric
1920*b5893f02SDimitry Andric const uint64_t V1Size = MaybeV1Size.getValue();
1921*b5893f02SDimitry Andric const uint64_t V2Size = MaybeV2Size.getValue();
1922*b5893f02SDimitry Andric
19237d523365SDimitry Andric const VariableGEPIndex &Var0 = VarIndices[0], &Var1 = VarIndices[1];
19247d523365SDimitry Andric
19257d523365SDimitry Andric if (Var0.ZExtBits != Var1.ZExtBits || Var0.SExtBits != Var1.SExtBits ||
19267d523365SDimitry Andric Var0.Scale != -Var1.Scale)
19277d523365SDimitry Andric return false;
19287d523365SDimitry Andric
19297d523365SDimitry Andric unsigned Width = Var1.V->getType()->getIntegerBitWidth();
19307d523365SDimitry Andric
19317d523365SDimitry Andric // We'll strip off the Extensions of Var0 and Var1 and do another round
19327d523365SDimitry Andric // of GetLinearExpression decomposition. In the example above, if Var0
19337d523365SDimitry Andric // is zext(%x + 1) we should get V1 == %x and V1Offset == 1.
19347d523365SDimitry Andric
19357d523365SDimitry Andric APInt V0Scale(Width, 0), V0Offset(Width, 0), V1Scale(Width, 0),
19367d523365SDimitry Andric V1Offset(Width, 0);
19377d523365SDimitry Andric bool NSW = true, NUW = true;
19387d523365SDimitry Andric unsigned V0ZExtBits = 0, V0SExtBits = 0, V1ZExtBits = 0, V1SExtBits = 0;
19397d523365SDimitry Andric const Value *V0 = GetLinearExpression(Var0.V, V0Scale, V0Offset, V0ZExtBits,
19407d523365SDimitry Andric V0SExtBits, DL, 0, AC, DT, NSW, NUW);
19413ca95b02SDimitry Andric NSW = true;
19423ca95b02SDimitry Andric NUW = true;
19437d523365SDimitry Andric const Value *V1 = GetLinearExpression(Var1.V, V1Scale, V1Offset, V1ZExtBits,
19447d523365SDimitry Andric V1SExtBits, DL, 0, AC, DT, NSW, NUW);
19457d523365SDimitry Andric
19467d523365SDimitry Andric if (V0Scale != V1Scale || V0ZExtBits != V1ZExtBits ||
19477d523365SDimitry Andric V0SExtBits != V1SExtBits || !isValueEqualInPotentialCycles(V0, V1))
19487d523365SDimitry Andric return false;
19497d523365SDimitry Andric
19507d523365SDimitry Andric // We have a hit - Var0 and Var1 only differ by a constant offset!
19517d523365SDimitry Andric
19527d523365SDimitry Andric // If we've been sext'ed then zext'd the maximum difference between Var0 and
19537d523365SDimitry Andric // Var1 is possible to calculate, but we're just interested in the absolute
19547d523365SDimitry Andric // minimum difference between the two. The minimum distance may occur due to
19557d523365SDimitry Andric // wrapping; consider "add i3 %i, 5": if %i == 7 then 7 + 5 mod 8 == 4, and so
19567d523365SDimitry Andric // the minimum distance between %i and %i + 5 is 3.
19577d523365SDimitry Andric APInt MinDiff = V0Offset - V1Offset, Wrapped = -MinDiff;
19587d523365SDimitry Andric MinDiff = APIntOps::umin(MinDiff, Wrapped);
1959*b5893f02SDimitry Andric APInt MinDiffBytes =
1960*b5893f02SDimitry Andric MinDiff.zextOrTrunc(Var0.Scale.getBitWidth()) * Var0.Scale.abs();
19617d523365SDimitry Andric
19627d523365SDimitry Andric // We can't definitely say whether GEP1 is before or after V2 due to wrapping
19637d523365SDimitry Andric // arithmetic (i.e. for some values of GEP1 and V2 GEP1 < V2, and for other
19647d523365SDimitry Andric // values GEP1 > V2). We'll therefore only declare NoAlias if both V1Size and
19657d523365SDimitry Andric // V2Size can fit in the MinDiffBytes gap.
1966*b5893f02SDimitry Andric return MinDiffBytes.uge(V1Size + BaseOffset.abs()) &&
1967*b5893f02SDimitry Andric MinDiffBytes.uge(V2Size + BaseOffset.abs());
19687d523365SDimitry Andric }
19697d523365SDimitry Andric
19707d523365SDimitry Andric //===----------------------------------------------------------------------===//
19717d523365SDimitry Andric // BasicAliasAnalysis Pass
19727d523365SDimitry Andric //===----------------------------------------------------------------------===//
19737d523365SDimitry Andric
1974d88c1a5aSDimitry Andric AnalysisKey BasicAA::Key;
19757d523365SDimitry Andric
run(Function & F,FunctionAnalysisManager & AM)1976d88c1a5aSDimitry Andric BasicAAResult BasicAA::run(Function &F, FunctionAnalysisManager &AM) {
19777d523365SDimitry Andric return BasicAAResult(F.getParent()->getDataLayout(),
19784ba319b5SDimitry Andric F,
19793ca95b02SDimitry Andric AM.getResult<TargetLibraryAnalysis>(F),
19803ca95b02SDimitry Andric AM.getResult<AssumptionAnalysis>(F),
19813ca95b02SDimitry Andric &AM.getResult<DominatorTreeAnalysis>(F),
19824ba319b5SDimitry Andric AM.getCachedResult<LoopAnalysis>(F),
19834ba319b5SDimitry Andric AM.getCachedResult<PhiValuesAnalysis>(F));
19847d523365SDimitry Andric }
19857d523365SDimitry Andric
BasicAAWrapperPass()19867d523365SDimitry Andric BasicAAWrapperPass::BasicAAWrapperPass() : FunctionPass(ID) {
19877d523365SDimitry Andric initializeBasicAAWrapperPassPass(*PassRegistry::getPassRegistry());
19887d523365SDimitry Andric }
19897d523365SDimitry Andric
19907d523365SDimitry Andric char BasicAAWrapperPass::ID = 0;
19912cab237bSDimitry Andric
anchor()19927d523365SDimitry Andric void BasicAAWrapperPass::anchor() {}
19937d523365SDimitry Andric
19947d523365SDimitry Andric INITIALIZE_PASS_BEGIN(BasicAAWrapperPass, "basicaa",
19954ba319b5SDimitry Andric "Basic Alias Analysis (stateless AA impl)", false, true)
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)19967d523365SDimitry Andric INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
19973ca95b02SDimitry Andric INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
19987d523365SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
19997d523365SDimitry Andric INITIALIZE_PASS_END(BasicAAWrapperPass, "basicaa",
20004ba319b5SDimitry Andric "Basic Alias Analysis (stateless AA impl)", false, true)
20017d523365SDimitry Andric
20027d523365SDimitry Andric FunctionPass *llvm::createBasicAAWrapperPass() {
20037d523365SDimitry Andric return new BasicAAWrapperPass();
20047d523365SDimitry Andric }
20057d523365SDimitry Andric
runOnFunction(Function & F)20067d523365SDimitry Andric bool BasicAAWrapperPass::runOnFunction(Function &F) {
20077d523365SDimitry Andric auto &ACT = getAnalysis<AssumptionCacheTracker>();
20087d523365SDimitry Andric auto &TLIWP = getAnalysis<TargetLibraryInfoWrapperPass>();
20093ca95b02SDimitry Andric auto &DTWP = getAnalysis<DominatorTreeWrapperPass>();
20107d523365SDimitry Andric auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>();
20114ba319b5SDimitry Andric auto *PVWP = getAnalysisIfAvailable<PhiValuesWrapperPass>();
20127d523365SDimitry Andric
20134ba319b5SDimitry Andric Result.reset(new BasicAAResult(F.getParent()->getDataLayout(), F, TLIWP.getTLI(),
20143ca95b02SDimitry Andric ACT.getAssumptionCache(F), &DTWP.getDomTree(),
20154ba319b5SDimitry Andric LIWP ? &LIWP->getLoopInfo() : nullptr,
20164ba319b5SDimitry Andric PVWP ? &PVWP->getResult() : nullptr));
20177d523365SDimitry Andric
20187d523365SDimitry Andric return false;
20197d523365SDimitry Andric }
20207d523365SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const20217d523365SDimitry Andric void BasicAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
20227d523365SDimitry Andric AU.setPreservesAll();
20237d523365SDimitry Andric AU.addRequired<AssumptionCacheTracker>();
20243ca95b02SDimitry Andric AU.addRequired<DominatorTreeWrapperPass>();
20257d523365SDimitry Andric AU.addRequired<TargetLibraryInfoWrapperPass>();
20264ba319b5SDimitry Andric AU.addUsedIfAvailable<PhiValuesWrapperPass>();
20277d523365SDimitry Andric }
20287d523365SDimitry Andric
createLegacyPMBasicAAResult(Pass & P,Function & F)20297d523365SDimitry Andric BasicAAResult llvm::createLegacyPMBasicAAResult(Pass &P, Function &F) {
20307d523365SDimitry Andric return BasicAAResult(
20317d523365SDimitry Andric F.getParent()->getDataLayout(),
20324ba319b5SDimitry Andric F,
20337d523365SDimitry Andric P.getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(),
20347d523365SDimitry Andric P.getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F));
20357d523365SDimitry Andric }
2036