BasicAliasAnalysis.cpp 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630
  1. //===- BasicAliasAnalysis.cpp - Stateless Alias Analysis Impl -------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the primary stateless implementation of the
  11. // Alias Analysis interface that implements identities (two different
  12. // globals cannot alias, etc), but does no stateful analysis.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/Analysis/BasicAliasAnalysis.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/ADT/Statistic.h"
  18. #include "llvm/Analysis/AliasAnalysis.h"
  19. #include "llvm/Analysis/CFG.h"
  20. #include "llvm/Analysis/CaptureTracking.h"
  21. #include "llvm/Analysis/InstructionSimplify.h"
  22. #include "llvm/Analysis/LoopInfo.h"
  23. #include "llvm/Analysis/MemoryBuiltins.h"
  24. #include "llvm/Analysis/ValueTracking.h"
  25. #include "llvm/Analysis/AssumptionCache.h"
  26. #include "llvm/IR/Constants.h"
  27. #include "llvm/IR/DataLayout.h"
  28. #include "llvm/IR/DerivedTypes.h"
  29. #include "llvm/IR/Dominators.h"
  30. #include "llvm/IR/GlobalAlias.h"
  31. #include "llvm/IR/GlobalVariable.h"
  32. #include "llvm/IR/Instructions.h"
  33. #include "llvm/IR/IntrinsicInst.h"
  34. #include "llvm/IR/LLVMContext.h"
  35. #include "llvm/IR/Operator.h"
  36. #include "llvm/Pass.h"
  37. #include "llvm/Support/ErrorHandling.h"
  38. #include <algorithm>
  39. using namespace llvm;
  40. /// Enable analysis of recursive PHI nodes.
  41. static cl::opt<bool> EnableRecPhiAnalysis("basicaa-recphi", cl::Hidden,
  42. cl::init(false));
  43. /// SearchLimitReached / SearchTimes shows how often the limit of
  44. /// to decompose GEPs is reached. It will affect the precision
  45. /// of basic alias analysis.
  46. #define DEBUG_TYPE "basicaa"
  47. STATISTIC(SearchLimitReached, "Number of times the limit to "
  48. "decompose GEPs is reached");
  49. STATISTIC(SearchTimes, "Number of times a GEP is decomposed");
  50. /// Cutoff after which to stop analysing a set of phi nodes potentially involved
  51. /// in a cycle. Because we are analysing 'through' phi nodes, we need to be
  52. /// careful with value equivalence. We use reachability to make sure a value
  53. /// cannot be involved in a cycle.
  54. const unsigned MaxNumPhiBBsValueReachabilityCheck = 20;
  55. // The max limit of the search depth in DecomposeGEPExpression() and
  56. // GetUnderlyingObject(), both functions need to use the same search
  57. // depth otherwise the algorithm in aliasGEP will assert.
  58. static const unsigned MaxLookupSearchDepth = 6;
  59. //===----------------------------------------------------------------------===//
  60. // Useful predicates
  61. //===----------------------------------------------------------------------===//
  62. /// Returns true if the pointer is to a function-local object that never
  63. /// escapes from the function.
  64. static bool isNonEscapingLocalObject(const Value *V) {
  65. // If this is a local allocation, check to see if it escapes.
  66. if (isa<AllocaInst>(V) || isNoAliasCall(V))
  67. // Set StoreCaptures to True so that we can assume in our callers that the
  68. // pointer is not the result of a load instruction. Currently
  69. // PointerMayBeCaptured doesn't have any special analysis for the
  70. // StoreCaptures=false case; if it did, our callers could be refined to be
  71. // more precise.
  72. return !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true);
  73. // If this is an argument that corresponds to a byval or noalias argument,
  74. // then it has not escaped before entering the function. Check if it escapes
  75. // inside the function.
  76. if (const Argument *A = dyn_cast<Argument>(V))
  77. if (A->hasByValAttr() || A->hasNoAliasAttr())
  78. // Note even if the argument is marked nocapture, we still need to check
  79. // for copies made inside the function. The nocapture attribute only
  80. // specifies that there are no copies made that outlive the function.
  81. return !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true);
  82. return false;
  83. }
  84. /// Returns true if the pointer is one which would have been considered an
  85. /// escape by isNonEscapingLocalObject.
  86. static bool isEscapeSource(const Value *V) {
  87. if (isa<CallInst>(V) || isa<InvokeInst>(V) || isa<Argument>(V))
  88. return true;
  89. // The load case works because isNonEscapingLocalObject considers all
  90. // stores to be escapes (it passes true for the StoreCaptures argument
  91. // to PointerMayBeCaptured).
  92. if (isa<LoadInst>(V))
  93. return true;
  94. return false;
  95. }
  96. /// Returns the size of the object specified by V or UnknownSize if unknown.
  97. static uint64_t getObjectSize(const Value *V, const DataLayout &DL,
  98. const TargetLibraryInfo &TLI,
  99. bool RoundToAlign = false) {
  100. uint64_t Size;
  101. if (getObjectSize(V, Size, DL, &TLI, RoundToAlign))
  102. return Size;
  103. return MemoryLocation::UnknownSize;
  104. }
  105. /// Returns true if we can prove that the object specified by V is smaller than
  106. /// Size.
  107. static bool isObjectSmallerThan(const Value *V, uint64_t Size,
  108. const DataLayout &DL,
  109. const TargetLibraryInfo &TLI) {
  110. // Note that the meanings of the "object" are slightly different in the
  111. // following contexts:
  112. // c1: llvm::getObjectSize()
  113. // c2: llvm.objectsize() intrinsic
  114. // c3: isObjectSmallerThan()
  115. // c1 and c2 share the same meaning; however, the meaning of "object" in c3
  116. // refers to the "entire object".
  117. //
  118. // Consider this example:
  119. // char *p = (char*)malloc(100)
  120. // char *q = p+80;
  121. //
  122. // In the context of c1 and c2, the "object" pointed by q refers to the
  123. // stretch of memory of q[0:19]. So, getObjectSize(q) should return 20.
  124. //
  125. // However, in the context of c3, the "object" refers to the chunk of memory
  126. // being allocated. So, the "object" has 100 bytes, and q points to the middle
  127. // the "object". In case q is passed to isObjectSmallerThan() as the 1st
  128. // parameter, before the llvm::getObjectSize() is called to get the size of
  129. // entire object, we should:
  130. // - either rewind the pointer q to the base-address of the object in
  131. // question (in this case rewind to p), or
  132. // - just give up. It is up to caller to make sure the pointer is pointing
  133. // to the base address the object.
  134. //
  135. // We go for 2nd option for simplicity.
  136. if (!isIdentifiedObject(V))
  137. return false;
  138. // This function needs to use the aligned object size because we allow
  139. // reads a bit past the end given sufficient alignment.
  140. uint64_t ObjectSize = getObjectSize(V, DL, TLI, /*RoundToAlign*/ true);
  141. return ObjectSize != MemoryLocation::UnknownSize && ObjectSize < Size;
  142. }
  143. /// Returns true if we can prove that the object specified by V has size Size.
  144. static bool isObjectSize(const Value *V, uint64_t Size, const DataLayout &DL,
  145. const TargetLibraryInfo &TLI) {
  146. uint64_t ObjectSize = getObjectSize(V, DL, TLI);
  147. return ObjectSize != MemoryLocation::UnknownSize && ObjectSize == Size;
  148. }
  149. //===----------------------------------------------------------------------===//
  150. // GetElementPtr Instruction Decomposition and Analysis
  151. //===----------------------------------------------------------------------===//
  152. /// Analyzes the specified value as a linear expression: "A*V + B", where A and
  153. /// B are constant integers.
  154. ///
  155. /// Returns the scale and offset values as APInts and return V as a Value*, and
  156. /// return whether we looked through any sign or zero extends. The incoming
  157. /// Value is known to have IntegerType, and it may already be sign or zero
  158. /// extended.
  159. ///
  160. /// Note that this looks through extends, so the high bits may not be
  161. /// represented in the result.
  162. /*static*/ const Value *BasicAAResult::GetLinearExpression(
  163. const Value *V, APInt &Scale, APInt &Offset, unsigned &ZExtBits,
  164. unsigned &SExtBits, const DataLayout &DL, unsigned Depth,
  165. AssumptionCache *AC, DominatorTree *DT, bool &NSW, bool &NUW) {
  166. assert(V->getType()->isIntegerTy() && "Not an integer value");
  167. // Limit our recursion depth.
  168. if (Depth == 6) {
  169. Scale = 1;
  170. Offset = 0;
  171. return V;
  172. }
  173. if (const ConstantInt *Const = dyn_cast<ConstantInt>(V)) {
  174. // If it's a constant, just convert it to an offset and remove the variable.
  175. // If we've been called recursively, the Offset bit width will be greater
  176. // than the constant's (the Offset's always as wide as the outermost call),
  177. // so we'll zext here and process any extension in the isa<SExtInst> &
  178. // isa<ZExtInst> cases below.
  179. Offset += Const->getValue().zextOrSelf(Offset.getBitWidth());
  180. assert(Scale == 0 && "Constant values don't have a scale");
  181. return V;
  182. }
  183. if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(V)) {
  184. if (ConstantInt *RHSC = dyn_cast<ConstantInt>(BOp->getOperand(1))) {
  185. // If we've been called recursively, then Offset and Scale will be wider
  186. // than the BOp operands. We'll always zext it here as we'll process sign
  187. // extensions below (see the isa<SExtInst> / isa<ZExtInst> cases).
  188. APInt RHS = RHSC->getValue().zextOrSelf(Offset.getBitWidth());
  189. switch (BOp->getOpcode()) {
  190. default:
  191. // We don't understand this instruction, so we can't decompose it any
  192. // further.
  193. Scale = 1;
  194. Offset = 0;
  195. return V;
  196. case Instruction::Or:
  197. // X|C == X+C if all the bits in C are unset in X. Otherwise we can't
  198. // analyze it.
  199. if (!MaskedValueIsZero(BOp->getOperand(0), RHSC->getValue(), DL, 0, AC,
  200. BOp, DT)) {
  201. Scale = 1;
  202. Offset = 0;
  203. return V;
  204. }
  205. // FALL THROUGH.
  206. case Instruction::Add:
  207. V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, ZExtBits,
  208. SExtBits, DL, Depth + 1, AC, DT, NSW, NUW);
  209. Offset += RHS;
  210. break;
  211. case Instruction::Sub:
  212. V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, ZExtBits,
  213. SExtBits, DL, Depth + 1, AC, DT, NSW, NUW);
  214. Offset -= RHS;
  215. break;
  216. case Instruction::Mul:
  217. V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, ZExtBits,
  218. SExtBits, DL, Depth + 1, AC, DT, NSW, NUW);
  219. Offset *= RHS;
  220. Scale *= RHS;
  221. break;
  222. case Instruction::Shl:
  223. V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, ZExtBits,
  224. SExtBits, DL, Depth + 1, AC, DT, NSW, NUW);
  225. Offset <<= RHS.getLimitedValue();
  226. Scale <<= RHS.getLimitedValue();
  227. // the semantics of nsw and nuw for left shifts don't match those of
  228. // multiplications, so we won't propagate them.
  229. NSW = NUW = false;
  230. return V;
  231. }
  232. if (isa<OverflowingBinaryOperator>(BOp)) {
  233. NUW &= BOp->hasNoUnsignedWrap();
  234. NSW &= BOp->hasNoSignedWrap();
  235. }
  236. return V;
  237. }
  238. }
  239. // Since GEP indices are sign extended anyway, we don't care about the high
  240. // bits of a sign or zero extended value - just scales and offsets. The
  241. // extensions have to be consistent though.
  242. if (isa<SExtInst>(V) || isa<ZExtInst>(V)) {
  243. Value *CastOp = cast<CastInst>(V)->getOperand(0);
  244. unsigned NewWidth = V->getType()->getPrimitiveSizeInBits();
  245. unsigned SmallWidth = CastOp->getType()->getPrimitiveSizeInBits();
  246. unsigned OldZExtBits = ZExtBits, OldSExtBits = SExtBits;
  247. const Value *Result =
  248. GetLinearExpression(CastOp, Scale, Offset, ZExtBits, SExtBits, DL,
  249. Depth + 1, AC, DT, NSW, NUW);
  250. // zext(zext(%x)) == zext(%x), and similiarly for sext; we'll handle this
  251. // by just incrementing the number of bits we've extended by.
  252. unsigned ExtendedBy = NewWidth - SmallWidth;
  253. if (isa<SExtInst>(V) && ZExtBits == 0) {
  254. // sext(sext(%x, a), b) == sext(%x, a + b)
  255. if (NSW) {
  256. // We haven't sign-wrapped, so it's valid to decompose sext(%x + c)
  257. // into sext(%x) + sext(c). We'll sext the Offset ourselves:
  258. unsigned OldWidth = Offset.getBitWidth();
  259. Offset = Offset.trunc(SmallWidth).sext(NewWidth).zextOrSelf(OldWidth);
  260. } else {
  261. // We may have signed-wrapped, so don't decompose sext(%x + c) into
  262. // sext(%x) + sext(c)
  263. Scale = 1;
  264. Offset = 0;
  265. Result = CastOp;
  266. ZExtBits = OldZExtBits;
  267. SExtBits = OldSExtBits;
  268. }
  269. SExtBits += ExtendedBy;
  270. } else {
  271. // sext(zext(%x, a), b) = zext(zext(%x, a), b) = zext(%x, a + b)
  272. if (!NUW) {
  273. // We may have unsigned-wrapped, so don't decompose zext(%x + c) into
  274. // zext(%x) + zext(c)
  275. Scale = 1;
  276. Offset = 0;
  277. Result = CastOp;
  278. ZExtBits = OldZExtBits;
  279. SExtBits = OldSExtBits;
  280. }
  281. ZExtBits += ExtendedBy;
  282. }
  283. return Result;
  284. }
  285. Scale = 1;
  286. Offset = 0;
  287. return V;
  288. }
  289. /// If V is a symbolic pointer expression, decompose it into a base pointer
  290. /// with a constant offset and a number of scaled symbolic offsets.
  291. ///
  292. /// The scaled symbolic offsets (represented by pairs of a Value* and a scale
  293. /// in the VarIndices vector) are Value*'s that are known to be scaled by the
  294. /// specified amount, but which may have other unrepresented high bits. As
  295. /// such, the gep cannot necessarily be reconstructed from its decomposed form.
  296. ///
  297. /// When DataLayout is around, this function is capable of analyzing everything
  298. /// that GetUnderlyingObject can look through. To be able to do that
  299. /// GetUnderlyingObject and DecomposeGEPExpression must use the same search
  300. /// depth (MaxLookupSearchDepth). When DataLayout not is around, it just looks
  301. /// through pointer casts.
  302. /*static*/ const Value *BasicAAResult::DecomposeGEPExpression(
  303. const Value *V, int64_t &BaseOffs,
  304. SmallVectorImpl<VariableGEPIndex> &VarIndices, bool &MaxLookupReached,
  305. const DataLayout &DL, AssumptionCache *AC, DominatorTree *DT) {
  306. // Limit recursion depth to limit compile time in crazy cases.
  307. unsigned MaxLookup = MaxLookupSearchDepth;
  308. MaxLookupReached = false;
  309. SearchTimes++;
  310. BaseOffs = 0;
  311. do {
  312. // See if this is a bitcast or GEP.
  313. const Operator *Op = dyn_cast<Operator>(V);
  314. if (!Op) {
  315. // The only non-operator case we can handle are GlobalAliases.
  316. if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
  317. if (!GA->mayBeOverridden()) {
  318. V = GA->getAliasee();
  319. continue;
  320. }
  321. }
  322. return V;
  323. }
  324. if (Op->getOpcode() == Instruction::BitCast ||
  325. Op->getOpcode() == Instruction::AddrSpaceCast) {
  326. V = Op->getOperand(0);
  327. continue;
  328. }
  329. const GEPOperator *GEPOp = dyn_cast<GEPOperator>(Op);
  330. if (!GEPOp) {
  331. // If it's not a GEP, hand it off to SimplifyInstruction to see if it
  332. // can come up with something. This matches what GetUnderlyingObject does.
  333. if (const Instruction *I = dyn_cast<Instruction>(V))
  334. // TODO: Get a DominatorTree and AssumptionCache and use them here
  335. // (these are both now available in this function, but this should be
  336. // updated when GetUnderlyingObject is updated). TLI should be
  337. // provided also.
  338. if (const Value *Simplified =
  339. SimplifyInstruction(const_cast<Instruction *>(I), DL)) {
  340. V = Simplified;
  341. continue;
  342. }
  343. return V;
  344. }
  345. // Don't attempt to analyze GEPs over unsized objects.
  346. if (!GEPOp->getSourceElementType()->isSized())
  347. return V;
  348. unsigned AS = GEPOp->getPointerAddressSpace();
  349. // Walk the indices of the GEP, accumulating them into BaseOff/VarIndices.
  350. gep_type_iterator GTI = gep_type_begin(GEPOp);
  351. for (User::const_op_iterator I = GEPOp->op_begin() + 1, E = GEPOp->op_end();
  352. I != E; ++I) {
  353. const Value *Index = *I;
  354. // Compute the (potentially symbolic) offset in bytes for this index.
  355. if (StructType *STy = dyn_cast<StructType>(*GTI++)) {
  356. // For a struct, add the member offset.
  357. unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue();
  358. if (FieldNo == 0)
  359. continue;
  360. BaseOffs += DL.getStructLayout(STy)->getElementOffset(FieldNo);
  361. continue;
  362. }
  363. // For an array/pointer, add the element offset, explicitly scaled.
  364. if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Index)) {
  365. if (CIdx->isZero())
  366. continue;
  367. BaseOffs += DL.getTypeAllocSize(*GTI) * CIdx->getSExtValue();
  368. continue;
  369. }
  370. uint64_t Scale = DL.getTypeAllocSize(*GTI);
  371. unsigned ZExtBits = 0, SExtBits = 0;
  372. // If the integer type is smaller than the pointer size, it is implicitly
  373. // sign extended to pointer size.
  374. unsigned Width = Index->getType()->getIntegerBitWidth();
  375. unsigned PointerSize = DL.getPointerSizeInBits(AS);
  376. if (PointerSize > Width)
  377. SExtBits += PointerSize - Width;
  378. // Use GetLinearExpression to decompose the index into a C1*V+C2 form.
  379. APInt IndexScale(Width, 0), IndexOffset(Width, 0);
  380. bool NSW = true, NUW = true;
  381. Index = GetLinearExpression(Index, IndexScale, IndexOffset, ZExtBits,
  382. SExtBits, DL, 0, AC, DT, NSW, NUW);
  383. // The GEP index scale ("Scale") scales C1*V+C2, yielding (C1*V+C2)*Scale.
  384. // This gives us an aggregate computation of (C1*Scale)*V + C2*Scale.
  385. BaseOffs += IndexOffset.getSExtValue() * Scale;
  386. Scale *= IndexScale.getSExtValue();
  387. // If we already had an occurrence of this index variable, merge this
  388. // scale into it. For example, we want to handle:
  389. // A[x][x] -> x*16 + x*4 -> x*20
  390. // This also ensures that 'x' only appears in the index list once.
  391. for (unsigned i = 0, e = VarIndices.size(); i != e; ++i) {
  392. if (VarIndices[i].V == Index && VarIndices[i].ZExtBits == ZExtBits &&
  393. VarIndices[i].SExtBits == SExtBits) {
  394. Scale += VarIndices[i].Scale;
  395. VarIndices.erase(VarIndices.begin() + i);
  396. break;
  397. }
  398. }
  399. // Make sure that we have a scale that makes sense for this target's
  400. // pointer size.
  401. if (unsigned ShiftBits = 64 - PointerSize) {
  402. Scale <<= ShiftBits;
  403. Scale = (int64_t)Scale >> ShiftBits;
  404. }
  405. if (Scale) {
  406. VariableGEPIndex Entry = {Index, ZExtBits, SExtBits,
  407. static_cast<int64_t>(Scale)};
  408. VarIndices.push_back(Entry);
  409. }
  410. }
  411. // Analyze the base pointer next.
  412. V = GEPOp->getOperand(0);
  413. } while (--MaxLookup);
  414. // If the chain of expressions is too deep, just return early.
  415. MaxLookupReached = true;
  416. SearchLimitReached++;
  417. return V;
  418. }
  419. /// Returns whether the given pointer value points to memory that is local to
  420. /// the function, with global constants being considered local to all
  421. /// functions.
  422. bool BasicAAResult::pointsToConstantMemory(const MemoryLocation &Loc,
  423. bool OrLocal) {
  424. assert(Visited.empty() && "Visited must be cleared after use!");
  425. unsigned MaxLookup = 8;
  426. SmallVector<const Value *, 16> Worklist;
  427. Worklist.push_back(Loc.Ptr);
  428. do {
  429. const Value *V = GetUnderlyingObject(Worklist.pop_back_val(), DL);
  430. if (!Visited.insert(V).second) {
  431. Visited.clear();
  432. return AAResultBase::pointsToConstantMemory(Loc, OrLocal);
  433. }
  434. // An alloca instruction defines local memory.
  435. if (OrLocal && isa<AllocaInst>(V))
  436. continue;
  437. // A global constant counts as local memory for our purposes.
  438. if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
  439. // Note: this doesn't require GV to be "ODR" because it isn't legal for a
  440. // global to be marked constant in some modules and non-constant in
  441. // others. GV may even be a declaration, not a definition.
  442. if (!GV->isConstant()) {
  443. Visited.clear();
  444. return AAResultBase::pointsToConstantMemory(Loc, OrLocal);
  445. }
  446. continue;
  447. }
  448. // If both select values point to local memory, then so does the select.
  449. if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
  450. Worklist.push_back(SI->getTrueValue());
  451. Worklist.push_back(SI->getFalseValue());
  452. continue;
  453. }
  454. // If all values incoming to a phi node point to local memory, then so does
  455. // the phi.
  456. if (const PHINode *PN = dyn_cast<PHINode>(V)) {
  457. // Don't bother inspecting phi nodes with many operands.
  458. if (PN->getNumIncomingValues() > MaxLookup) {
  459. Visited.clear();
  460. return AAResultBase::pointsToConstantMemory(Loc, OrLocal);
  461. }
  462. for (Value *IncValue : PN->incoming_values())
  463. Worklist.push_back(IncValue);
  464. continue;
  465. }
  466. // Otherwise be conservative.
  467. Visited.clear();
  468. return AAResultBase::pointsToConstantMemory(Loc, OrLocal);
  469. } while (!Worklist.empty() && --MaxLookup);
  470. Visited.clear();
  471. return Worklist.empty();
  472. }
  473. // FIXME: This code is duplicated with MemoryLocation and should be hoisted to
  474. // some common utility location.
  475. static bool isMemsetPattern16(const Function *MS,
  476. const TargetLibraryInfo &TLI) {
  477. if (TLI.has(LibFunc::memset_pattern16) &&
  478. MS->getName() == "memset_pattern16") {
  479. FunctionType *MemsetType = MS->getFunctionType();
  480. if (!MemsetType->isVarArg() && MemsetType->getNumParams() == 3 &&
  481. isa<PointerType>(MemsetType->getParamType(0)) &&
  482. isa<PointerType>(MemsetType->getParamType(1)) &&
  483. isa<IntegerType>(MemsetType->getParamType(2)))
  484. return true;
  485. }
  486. return false;
  487. }
  488. /// Returns the behavior when calling the given call site.
  489. FunctionModRefBehavior BasicAAResult::getModRefBehavior(ImmutableCallSite CS) {
  490. if (CS.doesNotAccessMemory())
  491. // Can't do better than this.
  492. return FMRB_DoesNotAccessMemory;
  493. FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior;
  494. // If the callsite knows it only reads memory, don't return worse
  495. // than that.
  496. if (CS.onlyReadsMemory())
  497. Min = FMRB_OnlyReadsMemory;
  498. if (CS.onlyAccessesArgMemory())
  499. Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesArgumentPointees);
  500. // The AAResultBase base class has some smarts, lets use them.
  501. return FunctionModRefBehavior(AAResultBase::getModRefBehavior(CS) & Min);
  502. }
  503. /// Returns the behavior when calling the given function. For use when the call
  504. /// site is not known.
  505. FunctionModRefBehavior BasicAAResult::getModRefBehavior(const Function *F) {
  506. // If the function declares it doesn't access memory, we can't do better.
  507. if (F->doesNotAccessMemory())
  508. return FMRB_DoesNotAccessMemory;
  509. FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior;
  510. // If the function declares it only reads memory, go with that.
  511. if (F->onlyReadsMemory())
  512. Min = FMRB_OnlyReadsMemory;
  513. if (F->onlyAccessesArgMemory())
  514. Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesArgumentPointees);
  515. // Otherwise be conservative.
  516. return FunctionModRefBehavior(AAResultBase::getModRefBehavior(F) & Min);
  517. }
  518. /// Returns true if this is a writeonly (i.e Mod only) parameter. Currently,
  519. /// we don't have a writeonly attribute, so this only knows about builtin
  520. /// intrinsics and target library functions. We could consider adding a
  521. /// writeonly attribute in the future and moving all of these facts to either
  522. /// Intrinsics.td or InferFunctionAttr.cpp
  523. static bool isWriteOnlyParam(ImmutableCallSite CS, unsigned ArgIdx,
  524. const TargetLibraryInfo &TLI) {
  525. if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction()))
  526. switch (II->getIntrinsicID()) {
  527. default:
  528. break;
  529. case Intrinsic::memset:
  530. case Intrinsic::memcpy:
  531. case Intrinsic::memmove:
  532. // We don't currently have a writeonly attribute. All other properties
  533. // of these intrinsics are nicely described via attributes in
  534. // Intrinsics.td and handled generically.
  535. if (ArgIdx == 0)
  536. return true;
  537. }
  538. // We can bound the aliasing properties of memset_pattern16 just as we can
  539. // for memcpy/memset. This is particularly important because the
  540. // LoopIdiomRecognizer likes to turn loops into calls to memset_pattern16
  541. // whenever possible. Note that all but the missing writeonly attribute are
  542. // handled via InferFunctionAttr.
  543. if (CS.getCalledFunction() && isMemsetPattern16(CS.getCalledFunction(), TLI))
  544. if (ArgIdx == 0)
  545. return true;
  546. // TODO: memset_pattern4, memset_pattern8
  547. // TODO: _chk variants
  548. // TODO: strcmp, strcpy
  549. return false;
  550. }
  551. ModRefInfo BasicAAResult::getArgModRefInfo(ImmutableCallSite CS,
  552. unsigned ArgIdx) {
  553. // Emulate the missing writeonly attribute by checking for known builtin
  554. // intrinsics and target library functions.
  555. if (isWriteOnlyParam(CS, ArgIdx, TLI))
  556. return MRI_Mod;
  557. if (CS.paramHasAttr(ArgIdx + 1, Attribute::ReadOnly))
  558. return MRI_Ref;
  559. if (CS.paramHasAttr(ArgIdx + 1, Attribute::ReadNone))
  560. return MRI_NoModRef;
  561. return AAResultBase::getArgModRefInfo(CS, ArgIdx);
  562. }
  563. static bool isAssumeIntrinsic(ImmutableCallSite CS) {
  564. const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction());
  565. return II && II->getIntrinsicID() == Intrinsic::assume;
  566. }
  567. #ifndef NDEBUG
  568. static const Function *getParent(const Value *V) {
  569. if (const Instruction *inst = dyn_cast<Instruction>(V))
  570. return inst->getParent()->getParent();
  571. if (const Argument *arg = dyn_cast<Argument>(V))
  572. return arg->getParent();
  573. return nullptr;
  574. }
  575. static bool notDifferentParent(const Value *O1, const Value *O2) {
  576. const Function *F1 = getParent(O1);
  577. const Function *F2 = getParent(O2);
  578. return !F1 || !F2 || F1 == F2;
  579. }
  580. #endif
  581. AliasResult BasicAAResult::alias(const MemoryLocation &LocA,
  582. const MemoryLocation &LocB) {
  583. assert(notDifferentParent(LocA.Ptr, LocB.Ptr) &&
  584. "BasicAliasAnalysis doesn't support interprocedural queries.");
  585. // If we have a directly cached entry for these locations, we have recursed
  586. // through this once, so just return the cached results. Notably, when this
  587. // happens, we don't clear the cache.
  588. auto CacheIt = AliasCache.find(LocPair(LocA, LocB));
  589. if (CacheIt != AliasCache.end())
  590. return CacheIt->second;
  591. AliasResult Alias = aliasCheck(LocA.Ptr, LocA.Size, LocA.AATags, LocB.Ptr,
  592. LocB.Size, LocB.AATags);
  593. // AliasCache rarely has more than 1 or 2 elements, always use
  594. // shrink_and_clear so it quickly returns to the inline capacity of the
  595. // SmallDenseMap if it ever grows larger.
  596. // FIXME: This should really be shrink_to_inline_capacity_and_clear().
  597. AliasCache.shrink_and_clear();
  598. VisitedPhiBBs.clear();
  599. return Alias;
  600. }
  601. /// Checks to see if the specified callsite can clobber the specified memory
  602. /// object.
  603. ///
  604. /// Since we only look at local properties of this function, we really can't
  605. /// say much about this query. We do, however, use simple "address taken"
  606. /// analysis on local objects.
  607. ModRefInfo BasicAAResult::getModRefInfo(ImmutableCallSite CS,
  608. const MemoryLocation &Loc) {
  609. assert(notDifferentParent(CS.getInstruction(), Loc.Ptr) &&
  610. "AliasAnalysis query involving multiple functions!");
  611. const Value *Object = GetUnderlyingObject(Loc.Ptr, DL);
  612. // If this is a tail call and Loc.Ptr points to a stack location, we know that
  613. // the tail call cannot access or modify the local stack.
  614. // We cannot exclude byval arguments here; these belong to the caller of
  615. // the current function not to the current function, and a tail callee
  616. // may reference them.
  617. if (isa<AllocaInst>(Object))
  618. if (const CallInst *CI = dyn_cast<CallInst>(CS.getInstruction()))
  619. if (CI->isTailCall())
  620. return MRI_NoModRef;
  621. // If the pointer is to a locally allocated object that does not escape,
  622. // then the call can not mod/ref the pointer unless the call takes the pointer
  623. // as an argument, and itself doesn't capture it.
  624. if (!isa<Constant>(Object) && CS.getInstruction() != Object &&
  625. isNonEscapingLocalObject(Object)) {
  626. bool PassedAsArg = false;
  627. unsigned OperandNo = 0;
  628. for (auto CI = CS.data_operands_begin(), CE = CS.data_operands_end();
  629. CI != CE; ++CI, ++OperandNo) {
  630. // Only look at the no-capture or byval pointer arguments. If this
  631. // pointer were passed to arguments that were neither of these, then it
  632. // couldn't be no-capture.
  633. if (!(*CI)->getType()->isPointerTy() ||
  634. (!CS.doesNotCapture(OperandNo) && !CS.isByValArgument(OperandNo)))
  635. continue;
  636. // If this is a no-capture pointer argument, see if we can tell that it
  637. // is impossible to alias the pointer we're checking. If not, we have to
  638. // assume that the call could touch the pointer, even though it doesn't
  639. // escape.
  640. AliasResult AR =
  641. getBestAAResults().alias(MemoryLocation(*CI), MemoryLocation(Object));
  642. if (AR) {
  643. PassedAsArg = true;
  644. break;
  645. }
  646. }
  647. if (!PassedAsArg)
  648. return MRI_NoModRef;
  649. }
  650. // While the assume intrinsic is marked as arbitrarily writing so that
  651. // proper control dependencies will be maintained, it never aliases any
  652. // particular memory location.
  653. if (isAssumeIntrinsic(CS))
  654. return MRI_NoModRef;
  655. // The AAResultBase base class has some smarts, lets use them.
  656. return AAResultBase::getModRefInfo(CS, Loc);
  657. }
  658. ModRefInfo BasicAAResult::getModRefInfo(ImmutableCallSite CS1,
  659. ImmutableCallSite CS2) {
  660. // While the assume intrinsic is marked as arbitrarily writing so that
  661. // proper control dependencies will be maintained, it never aliases any
  662. // particular memory location.
  663. if (isAssumeIntrinsic(CS1) || isAssumeIntrinsic(CS2))
  664. return MRI_NoModRef;
  665. // The AAResultBase base class has some smarts, lets use them.
  666. return AAResultBase::getModRefInfo(CS1, CS2);
  667. }
  668. /// Provide ad-hoc rules to disambiguate accesses through two GEP operators,
  669. /// both having the exact same pointer operand.
  670. static AliasResult aliasSameBasePointerGEPs(const GEPOperator *GEP1,
  671. uint64_t V1Size,
  672. const GEPOperator *GEP2,
  673. uint64_t V2Size,
  674. const DataLayout &DL) {
  675. assert(GEP1->getPointerOperand() == GEP2->getPointerOperand() &&
  676. "Expected GEPs with the same pointer operand");
  677. // Try to determine whether GEP1 and GEP2 index through arrays, into structs,
  678. // such that the struct field accesses provably cannot alias.
  679. // We also need at least two indices (the pointer, and the struct field).
  680. if (GEP1->getNumIndices() != GEP2->getNumIndices() ||
  681. GEP1->getNumIndices() < 2)
  682. return MayAlias;
  683. // If we don't know the size of the accesses through both GEPs, we can't
  684. // determine whether the struct fields accessed can't alias.
  685. if (V1Size == MemoryLocation::UnknownSize ||
  686. V2Size == MemoryLocation::UnknownSize)
  687. return MayAlias;
  688. ConstantInt *C1 =
  689. dyn_cast<ConstantInt>(GEP1->getOperand(GEP1->getNumOperands() - 1));
  690. ConstantInt *C2 =
  691. dyn_cast<ConstantInt>(GEP2->getOperand(GEP2->getNumOperands() - 1));
  692. // If the last (struct) indices are constants and are equal, the other indices
  693. // might be also be dynamically equal, so the GEPs can alias.
  694. if (C1 && C2 && C1 == C2)
  695. return MayAlias;
  696. // Find the last-indexed type of the GEP, i.e., the type you'd get if
  697. // you stripped the last index.
  698. // On the way, look at each indexed type. If there's something other
  699. // than an array, different indices can lead to different final types.
  700. SmallVector<Value *, 8> IntermediateIndices;
  701. // Insert the first index; we don't need to check the type indexed
  702. // through it as it only drops the pointer indirection.
  703. assert(GEP1->getNumIndices() > 1 && "Not enough GEP indices to examine");
  704. IntermediateIndices.push_back(GEP1->getOperand(1));
  705. // Insert all the remaining indices but the last one.
  706. // Also, check that they all index through arrays.
  707. for (unsigned i = 1, e = GEP1->getNumIndices() - 1; i != e; ++i) {
  708. if (!isa<ArrayType>(GetElementPtrInst::getIndexedType(
  709. GEP1->getSourceElementType(), IntermediateIndices)))
  710. return MayAlias;
  711. IntermediateIndices.push_back(GEP1->getOperand(i + 1));
  712. }
  713. auto *Ty = GetElementPtrInst::getIndexedType(
  714. GEP1->getSourceElementType(), IntermediateIndices);
  715. StructType *LastIndexedStruct = dyn_cast<StructType>(Ty);
  716. if (isa<SequentialType>(Ty)) {
  717. // We know that:
  718. // - both GEPs begin indexing from the exact same pointer;
  719. // - the last indices in both GEPs are constants, indexing into a sequential
  720. // type (array or pointer);
  721. // - both GEPs only index through arrays prior to that.
  722. //
  723. // Because array indices greater than the number of elements are valid in
  724. // GEPs, unless we know the intermediate indices are identical between
  725. // GEP1 and GEP2 we cannot guarantee that the last indexed arrays don't
  726. // partially overlap. We also need to check that the loaded size matches
  727. // the element size, otherwise we could still have overlap.
  728. const uint64_t ElementSize =
  729. DL.getTypeStoreSize(cast<SequentialType>(Ty)->getElementType());
  730. if (V1Size != ElementSize || V2Size != ElementSize)
  731. return MayAlias;
  732. for (unsigned i = 0, e = GEP1->getNumIndices() - 1; i != e; ++i)
  733. if (GEP1->getOperand(i + 1) != GEP2->getOperand(i + 1))
  734. return MayAlias;
  735. // Now we know that the array/pointer that GEP1 indexes into and that
  736. // that GEP2 indexes into must either precisely overlap or be disjoint.
  737. // Because they cannot partially overlap and because fields in an array
  738. // cannot overlap, if we can prove the final indices are different between
  739. // GEP1 and GEP2, we can conclude GEP1 and GEP2 don't alias.
  740. // If the last indices are constants, we've already checked they don't
  741. // equal each other so we can exit early.
  742. if (C1 && C2)
  743. return NoAlias;
  744. if (isKnownNonEqual(GEP1->getOperand(GEP1->getNumOperands() - 1),
  745. GEP2->getOperand(GEP2->getNumOperands() - 1),
  746. DL))
  747. return NoAlias;
  748. return MayAlias;
  749. } else if (!LastIndexedStruct || !C1 || !C2) {
  750. return MayAlias;
  751. }
  752. // We know that:
  753. // - both GEPs begin indexing from the exact same pointer;
  754. // - the last indices in both GEPs are constants, indexing into a struct;
  755. // - said indices are different, hence, the pointed-to fields are different;
  756. // - both GEPs only index through arrays prior to that.
  757. //
  758. // This lets us determine that the struct that GEP1 indexes into and the
  759. // struct that GEP2 indexes into must either precisely overlap or be
  760. // completely disjoint. Because they cannot partially overlap, indexing into
  761. // different non-overlapping fields of the struct will never alias.
  762. // Therefore, the only remaining thing needed to show that both GEPs can't
  763. // alias is that the fields are not overlapping.
  764. const StructLayout *SL = DL.getStructLayout(LastIndexedStruct);
  765. const uint64_t StructSize = SL->getSizeInBytes();
  766. const uint64_t V1Off = SL->getElementOffset(C1->getZExtValue());
  767. const uint64_t V2Off = SL->getElementOffset(C2->getZExtValue());
  768. auto EltsDontOverlap = [StructSize](uint64_t V1Off, uint64_t V1Size,
  769. uint64_t V2Off, uint64_t V2Size) {
  770. return V1Off < V2Off && V1Off + V1Size <= V2Off &&
  771. ((V2Off + V2Size <= StructSize) ||
  772. (V2Off + V2Size - StructSize <= V1Off));
  773. };
  774. if (EltsDontOverlap(V1Off, V1Size, V2Off, V2Size) ||
  775. EltsDontOverlap(V2Off, V2Size, V1Off, V1Size))
  776. return NoAlias;
  777. return MayAlias;
  778. }
  779. /// Provides a bunch of ad-hoc rules to disambiguate a GEP instruction against
  780. /// another pointer.
  781. ///
  782. /// We know that V1 is a GEP, but we don't know anything about V2.
  783. /// UnderlyingV1 is GetUnderlyingObject(GEP1, DL), UnderlyingV2 is the same for
  784. /// V2.
  785. AliasResult BasicAAResult::aliasGEP(const GEPOperator *GEP1, uint64_t V1Size,
  786. const AAMDNodes &V1AAInfo, const Value *V2,
  787. uint64_t V2Size, const AAMDNodes &V2AAInfo,
  788. const Value *UnderlyingV1,
  789. const Value *UnderlyingV2) {
  790. int64_t GEP1BaseOffset;
  791. bool GEP1MaxLookupReached;
  792. SmallVector<VariableGEPIndex, 4> GEP1VariableIndices;
  793. // If we have two gep instructions with must-alias or not-alias'ing base
  794. // pointers, figure out if the indexes to the GEP tell us anything about the
  795. // derived pointer.
  796. if (const GEPOperator *GEP2 = dyn_cast<GEPOperator>(V2)) {
  797. // Do the base pointers alias?
  798. AliasResult BaseAlias =
  799. aliasCheck(UnderlyingV1, MemoryLocation::UnknownSize, AAMDNodes(),
  800. UnderlyingV2, MemoryLocation::UnknownSize, AAMDNodes());
  801. // Check for geps of non-aliasing underlying pointers where the offsets are
  802. // identical.
  803. if ((BaseAlias == MayAlias) && V1Size == V2Size) {
  804. // Do the base pointers alias assuming type and size.
  805. AliasResult PreciseBaseAlias = aliasCheck(UnderlyingV1, V1Size, V1AAInfo,
  806. UnderlyingV2, V2Size, V2AAInfo);
  807. if (PreciseBaseAlias == NoAlias) {
  808. // See if the computed offset from the common pointer tells us about the
  809. // relation of the resulting pointer.
  810. int64_t GEP2BaseOffset;
  811. bool GEP2MaxLookupReached;
  812. SmallVector<VariableGEPIndex, 4> GEP2VariableIndices;
  813. const Value *GEP2BasePtr =
  814. DecomposeGEPExpression(GEP2, GEP2BaseOffset, GEP2VariableIndices,
  815. GEP2MaxLookupReached, DL, &AC, DT);
  816. const Value *GEP1BasePtr =
  817. DecomposeGEPExpression(GEP1, GEP1BaseOffset, GEP1VariableIndices,
  818. GEP1MaxLookupReached, DL, &AC, DT);
  819. // DecomposeGEPExpression and GetUnderlyingObject should return the
  820. // same result except when DecomposeGEPExpression has no DataLayout.
  821. // FIXME: They always have a DataLayout, so this should become an
  822. // assert.
  823. if (GEP1BasePtr != UnderlyingV1 || GEP2BasePtr != UnderlyingV2) {
  824. return MayAlias;
  825. }
  826. // If the max search depth is reached the result is undefined
  827. if (GEP2MaxLookupReached || GEP1MaxLookupReached)
  828. return MayAlias;
  829. // Same offsets.
  830. if (GEP1BaseOffset == GEP2BaseOffset &&
  831. GEP1VariableIndices == GEP2VariableIndices)
  832. return NoAlias;
  833. GEP1VariableIndices.clear();
  834. }
  835. }
  836. // If we get a No or May, then return it immediately, no amount of analysis
  837. // will improve this situation.
  838. if (BaseAlias != MustAlias)
  839. return BaseAlias;
  840. // Otherwise, we have a MustAlias. Since the base pointers alias each other
  841. // exactly, see if the computed offset from the common pointer tells us
  842. // about the relation of the resulting pointer.
  843. const Value *GEP1BasePtr =
  844. DecomposeGEPExpression(GEP1, GEP1BaseOffset, GEP1VariableIndices,
  845. GEP1MaxLookupReached, DL, &AC, DT);
  846. int64_t GEP2BaseOffset;
  847. bool GEP2MaxLookupReached;
  848. SmallVector<VariableGEPIndex, 4> GEP2VariableIndices;
  849. const Value *GEP2BasePtr =
  850. DecomposeGEPExpression(GEP2, GEP2BaseOffset, GEP2VariableIndices,
  851. GEP2MaxLookupReached, DL, &AC, DT);
  852. // DecomposeGEPExpression and GetUnderlyingObject should return the
  853. // same result except when DecomposeGEPExpression has no DataLayout.
  854. // FIXME: They always have a DataLayout, so this should become an assert.
  855. if (GEP1BasePtr != UnderlyingV1 || GEP2BasePtr != UnderlyingV2) {
  856. return MayAlias;
  857. }
  858. // If we know the two GEPs are based off of the exact same pointer (and not
  859. // just the same underlying object), see if that tells us anything about
  860. // the resulting pointers.
  861. if (GEP1->getPointerOperand() == GEP2->getPointerOperand()) {
  862. AliasResult R = aliasSameBasePointerGEPs(GEP1, V1Size, GEP2, V2Size, DL);
  863. // If we couldn't find anything interesting, don't abandon just yet.
  864. if (R != MayAlias)
  865. return R;
  866. }
  867. // If the max search depth is reached, the result is undefined
  868. if (GEP2MaxLookupReached || GEP1MaxLookupReached)
  869. return MayAlias;
  870. // Subtract the GEP2 pointer from the GEP1 pointer to find out their
  871. // symbolic difference.
  872. GEP1BaseOffset -= GEP2BaseOffset;
  873. GetIndexDifference(GEP1VariableIndices, GEP2VariableIndices);
  874. } else {
  875. // Check to see if these two pointers are related by the getelementptr
  876. // instruction. If one pointer is a GEP with a non-zero index of the other
  877. // pointer, we know they cannot alias.
  878. // If both accesses are unknown size, we can't do anything useful here.
  879. if (V1Size == MemoryLocation::UnknownSize &&
  880. V2Size == MemoryLocation::UnknownSize)
  881. return MayAlias;
  882. AliasResult R = aliasCheck(UnderlyingV1, MemoryLocation::UnknownSize,
  883. AAMDNodes(), V2, V2Size, V2AAInfo);
  884. if (R != MustAlias)
  885. // If V2 may alias GEP base pointer, conservatively returns MayAlias.
  886. // If V2 is known not to alias GEP base pointer, then the two values
  887. // cannot alias per GEP semantics: "A pointer value formed from a
  888. // getelementptr instruction is associated with the addresses associated
  889. // with the first operand of the getelementptr".
  890. return R;
  891. const Value *GEP1BasePtr =
  892. DecomposeGEPExpression(GEP1, GEP1BaseOffset, GEP1VariableIndices,
  893. GEP1MaxLookupReached, DL, &AC, DT);
  894. // DecomposeGEPExpression and GetUnderlyingObject should return the
  895. // same result except when DecomposeGEPExpression has no DataLayout.
  896. // FIXME: They always have a DataLayout, so this should become an assert.
  897. if (GEP1BasePtr != UnderlyingV1) {
  898. return MayAlias;
  899. }
  900. // If the max search depth is reached the result is undefined
  901. if (GEP1MaxLookupReached)
  902. return MayAlias;
  903. }
  904. // In the two GEP Case, if there is no difference in the offsets of the
  905. // computed pointers, the resultant pointers are a must alias. This
  906. // happens when we have two lexically identical GEP's (for example).
  907. //
  908. // In the other case, if we have getelementptr <ptr>, 0, 0, 0, 0, ... and V2
  909. // must aliases the GEP, the end result is a must alias also.
  910. if (GEP1BaseOffset == 0 && GEP1VariableIndices.empty())
  911. return MustAlias;
  912. // If there is a constant difference between the pointers, but the difference
  913. // is less than the size of the associated memory object, then we know
  914. // that the objects are partially overlapping. If the difference is
  915. // greater, we know they do not overlap.
  916. if (GEP1BaseOffset != 0 && GEP1VariableIndices.empty()) {
  917. if (GEP1BaseOffset >= 0) {
  918. if (V2Size != MemoryLocation::UnknownSize) {
  919. if ((uint64_t)GEP1BaseOffset < V2Size)
  920. return PartialAlias;
  921. return NoAlias;
  922. }
  923. } else {
  924. // We have the situation where:
  925. // + +
  926. // | BaseOffset |
  927. // ---------------->|
  928. // |-->V1Size |-------> V2Size
  929. // GEP1 V2
  930. // We need to know that V2Size is not unknown, otherwise we might have
  931. // stripped a gep with negative index ('gep <ptr>, -1, ...).
  932. if (V1Size != MemoryLocation::UnknownSize &&
  933. V2Size != MemoryLocation::UnknownSize) {
  934. if (-(uint64_t)GEP1BaseOffset < V1Size)
  935. return PartialAlias;
  936. return NoAlias;
  937. }
  938. }
  939. }
  940. if (!GEP1VariableIndices.empty()) {
  941. uint64_t Modulo = 0;
  942. bool AllPositive = true;
  943. for (unsigned i = 0, e = GEP1VariableIndices.size(); i != e; ++i) {
  944. // Try to distinguish something like &A[i][1] against &A[42][0].
  945. // Grab the least significant bit set in any of the scales. We
  946. // don't need std::abs here (even if the scale's negative) as we'll
  947. // be ^'ing Modulo with itself later.
  948. Modulo |= (uint64_t)GEP1VariableIndices[i].Scale;
  949. if (AllPositive) {
  950. // If the Value could change between cycles, then any reasoning about
  951. // the Value this cycle may not hold in the next cycle. We'll just
  952. // give up if we can't determine conditions that hold for every cycle:
  953. const Value *V = GEP1VariableIndices[i].V;
  954. bool SignKnownZero, SignKnownOne;
  955. ComputeSignBit(const_cast<Value *>(V), SignKnownZero, SignKnownOne, DL,
  956. 0, &AC, nullptr, DT);
  957. // Zero-extension widens the variable, and so forces the sign
  958. // bit to zero.
  959. bool IsZExt = GEP1VariableIndices[i].ZExtBits > 0 || isa<ZExtInst>(V);
  960. SignKnownZero |= IsZExt;
  961. SignKnownOne &= !IsZExt;
  962. // If the variable begins with a zero then we know it's
  963. // positive, regardless of whether the value is signed or
  964. // unsigned.
  965. int64_t Scale = GEP1VariableIndices[i].Scale;
  966. AllPositive =
  967. (SignKnownZero && Scale >= 0) || (SignKnownOne && Scale < 0);
  968. }
  969. }
  970. Modulo = Modulo ^ (Modulo & (Modulo - 1));
  971. // We can compute the difference between the two addresses
  972. // mod Modulo. Check whether that difference guarantees that the
  973. // two locations do not alias.
  974. uint64_t ModOffset = (uint64_t)GEP1BaseOffset & (Modulo - 1);
  975. if (V1Size != MemoryLocation::UnknownSize &&
  976. V2Size != MemoryLocation::UnknownSize && ModOffset >= V2Size &&
  977. V1Size <= Modulo - ModOffset)
  978. return NoAlias;
  979. // If we know all the variables are positive, then GEP1 >= GEP1BasePtr.
  980. // If GEP1BasePtr > V2 (GEP1BaseOffset > 0) then we know the pointers
  981. // don't alias if V2Size can fit in the gap between V2 and GEP1BasePtr.
  982. if (AllPositive && GEP1BaseOffset > 0 && V2Size <= (uint64_t)GEP1BaseOffset)
  983. return NoAlias;
  984. if (constantOffsetHeuristic(GEP1VariableIndices, V1Size, V2Size,
  985. GEP1BaseOffset, &AC, DT))
  986. return NoAlias;
  987. }
  988. // Statically, we can see that the base objects are the same, but the
  989. // pointers have dynamic offsets which we can't resolve. And none of our
  990. // little tricks above worked.
  991. //
  992. // TODO: Returning PartialAlias instead of MayAlias is a mild hack; the
  993. // practical effect of this is protecting TBAA in the case of dynamic
  994. // indices into arrays of unions or malloc'd memory.
  995. return PartialAlias;
  996. }
  997. static AliasResult MergeAliasResults(AliasResult A, AliasResult B) {
  998. // If the results agree, take it.
  999. if (A == B)
  1000. return A;
  1001. // A mix of PartialAlias and MustAlias is PartialAlias.
  1002. if ((A == PartialAlias && B == MustAlias) ||
  1003. (B == PartialAlias && A == MustAlias))
  1004. return PartialAlias;
  1005. // Otherwise, we don't know anything.
  1006. return MayAlias;
  1007. }
  1008. /// Provides a bunch of ad-hoc rules to disambiguate a Select instruction
  1009. /// against another.
  1010. AliasResult BasicAAResult::aliasSelect(const SelectInst *SI, uint64_t SISize,
  1011. const AAMDNodes &SIAAInfo,
  1012. const Value *V2, uint64_t V2Size,
  1013. const AAMDNodes &V2AAInfo) {
  1014. // If the values are Selects with the same condition, we can do a more precise
  1015. // check: just check for aliases between the values on corresponding arms.
  1016. if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2))
  1017. if (SI->getCondition() == SI2->getCondition()) {
  1018. AliasResult Alias = aliasCheck(SI->getTrueValue(), SISize, SIAAInfo,
  1019. SI2->getTrueValue(), V2Size, V2AAInfo);
  1020. if (Alias == MayAlias)
  1021. return MayAlias;
  1022. AliasResult ThisAlias =
  1023. aliasCheck(SI->getFalseValue(), SISize, SIAAInfo,
  1024. SI2->getFalseValue(), V2Size, V2AAInfo);
  1025. return MergeAliasResults(ThisAlias, Alias);
  1026. }
  1027. // If both arms of the Select node NoAlias or MustAlias V2, then returns
  1028. // NoAlias / MustAlias. Otherwise, returns MayAlias.
  1029. AliasResult Alias =
  1030. aliasCheck(V2, V2Size, V2AAInfo, SI->getTrueValue(), SISize, SIAAInfo);
  1031. if (Alias == MayAlias)
  1032. return MayAlias;
  1033. AliasResult ThisAlias =
  1034. aliasCheck(V2, V2Size, V2AAInfo, SI->getFalseValue(), SISize, SIAAInfo);
  1035. return MergeAliasResults(ThisAlias, Alias);
  1036. }
  1037. /// Provide a bunch of ad-hoc rules to disambiguate a PHI instruction against
  1038. /// another.
  1039. AliasResult BasicAAResult::aliasPHI(const PHINode *PN, uint64_t PNSize,
  1040. const AAMDNodes &PNAAInfo, const Value *V2,
  1041. uint64_t V2Size,
  1042. const AAMDNodes &V2AAInfo) {
  1043. // Track phi nodes we have visited. We use this information when we determine
  1044. // value equivalence.
  1045. VisitedPhiBBs.insert(PN->getParent());
  1046. // If the values are PHIs in the same block, we can do a more precise
  1047. // as well as efficient check: just check for aliases between the values
  1048. // on corresponding edges.
  1049. if (const PHINode *PN2 = dyn_cast<PHINode>(V2))
  1050. if (PN2->getParent() == PN->getParent()) {
  1051. LocPair Locs(MemoryLocation(PN, PNSize, PNAAInfo),
  1052. MemoryLocation(V2, V2Size, V2AAInfo));
  1053. if (PN > V2)
  1054. std::swap(Locs.first, Locs.second);
  1055. // Analyse the PHIs' inputs under the assumption that the PHIs are
  1056. // NoAlias.
  1057. // If the PHIs are May/MustAlias there must be (recursively) an input
  1058. // operand from outside the PHIs' cycle that is MayAlias/MustAlias or
  1059. // there must be an operation on the PHIs within the PHIs' value cycle
  1060. // that causes a MayAlias.
  1061. // Pretend the phis do not alias.
  1062. AliasResult Alias = NoAlias;
  1063. assert(AliasCache.count(Locs) &&
  1064. "There must exist an entry for the phi node");
  1065. AliasResult OrigAliasResult = AliasCache[Locs];
  1066. AliasCache[Locs] = NoAlias;
  1067. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
  1068. AliasResult ThisAlias =
  1069. aliasCheck(PN->getIncomingValue(i), PNSize, PNAAInfo,
  1070. PN2->getIncomingValueForBlock(PN->getIncomingBlock(i)),
  1071. V2Size, V2AAInfo);
  1072. Alias = MergeAliasResults(ThisAlias, Alias);
  1073. if (Alias == MayAlias)
  1074. break;
  1075. }
  1076. // Reset if speculation failed.
  1077. if (Alias != NoAlias)
  1078. AliasCache[Locs] = OrigAliasResult;
  1079. return Alias;
  1080. }
  1081. SmallPtrSet<Value *, 4> UniqueSrc;
  1082. SmallVector<Value *, 4> V1Srcs;
  1083. bool isRecursive = false;
  1084. for (Value *PV1 : PN->incoming_values()) {
  1085. if (isa<PHINode>(PV1))
  1086. // If any of the source itself is a PHI, return MayAlias conservatively
  1087. // to avoid compile time explosion. The worst possible case is if both
  1088. // sides are PHI nodes. In which case, this is O(m x n) time where 'm'
  1089. // and 'n' are the number of PHI sources.
  1090. return MayAlias;
  1091. if (EnableRecPhiAnalysis)
  1092. if (GEPOperator *PV1GEP = dyn_cast<GEPOperator>(PV1)) {
  1093. // Check whether the incoming value is a GEP that advances the pointer
  1094. // result of this PHI node (e.g. in a loop). If this is the case, we
  1095. // would recurse and always get a MayAlias. Handle this case specially
  1096. // below.
  1097. if (PV1GEP->getPointerOperand() == PN && PV1GEP->getNumIndices() == 1 &&
  1098. isa<ConstantInt>(PV1GEP->idx_begin())) {
  1099. isRecursive = true;
  1100. continue;
  1101. }
  1102. }
  1103. if (UniqueSrc.insert(PV1).second)
  1104. V1Srcs.push_back(PV1);
  1105. }
  1106. // If this PHI node is recursive, set the size of the accessed memory to
  1107. // unknown to represent all the possible values the GEP could advance the
  1108. // pointer to.
  1109. if (isRecursive)
  1110. PNSize = MemoryLocation::UnknownSize;
  1111. AliasResult Alias =
  1112. aliasCheck(V2, V2Size, V2AAInfo, V1Srcs[0], PNSize, PNAAInfo);
  1113. // Early exit if the check of the first PHI source against V2 is MayAlias.
  1114. // Other results are not possible.
  1115. if (Alias == MayAlias)
  1116. return MayAlias;
  1117. // If all sources of the PHI node NoAlias or MustAlias V2, then returns
  1118. // NoAlias / MustAlias. Otherwise, returns MayAlias.
  1119. for (unsigned i = 1, e = V1Srcs.size(); i != e; ++i) {
  1120. Value *V = V1Srcs[i];
  1121. AliasResult ThisAlias =
  1122. aliasCheck(V2, V2Size, V2AAInfo, V, PNSize, PNAAInfo);
  1123. Alias = MergeAliasResults(ThisAlias, Alias);
  1124. if (Alias == MayAlias)
  1125. break;
  1126. }
  1127. return Alias;
  1128. }
  1129. /// Provides a bunch of ad-hoc rules to disambiguate in common cases, such as
  1130. /// array references.
  1131. AliasResult BasicAAResult::aliasCheck(const Value *V1, uint64_t V1Size,
  1132. AAMDNodes V1AAInfo, const Value *V2,
  1133. uint64_t V2Size, AAMDNodes V2AAInfo) {
  1134. // If either of the memory references is empty, it doesn't matter what the
  1135. // pointer values are.
  1136. if (V1Size == 0 || V2Size == 0)
  1137. return NoAlias;
  1138. // Strip off any casts if they exist.
  1139. V1 = V1->stripPointerCasts();
  1140. V2 = V2->stripPointerCasts();
  1141. // If V1 or V2 is undef, the result is NoAlias because we can always pick a
  1142. // value for undef that aliases nothing in the program.
  1143. if (isa<UndefValue>(V1) || isa<UndefValue>(V2))
  1144. return NoAlias;
  1145. // Are we checking for alias of the same value?
  1146. // Because we look 'through' phi nodes, we could look at "Value" pointers from
  1147. // different iterations. We must therefore make sure that this is not the
  1148. // case. The function isValueEqualInPotentialCycles ensures that this cannot
  1149. // happen by looking at the visited phi nodes and making sure they cannot
  1150. // reach the value.
  1151. if (isValueEqualInPotentialCycles(V1, V2))
  1152. return MustAlias;
  1153. if (!V1->getType()->isPointerTy() || !V2->getType()->isPointerTy())
  1154. return NoAlias; // Scalars cannot alias each other
  1155. // Figure out what objects these things are pointing to if we can.
  1156. const Value *O1 = GetUnderlyingObject(V1, DL, MaxLookupSearchDepth);
  1157. const Value *O2 = GetUnderlyingObject(V2, DL, MaxLookupSearchDepth);
  1158. // Null values in the default address space don't point to any object, so they
  1159. // don't alias any other pointer.
  1160. if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O1))
  1161. if (CPN->getType()->getAddressSpace() == 0)
  1162. return NoAlias;
  1163. if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O2))
  1164. if (CPN->getType()->getAddressSpace() == 0)
  1165. return NoAlias;
  1166. if (O1 != O2) {
  1167. // If V1/V2 point to two different objects, we know that we have no alias.
  1168. if (isIdentifiedObject(O1) && isIdentifiedObject(O2))
  1169. return NoAlias;
  1170. // Constant pointers can't alias with non-const isIdentifiedObject objects.
  1171. if ((isa<Constant>(O1) && isIdentifiedObject(O2) && !isa<Constant>(O2)) ||
  1172. (isa<Constant>(O2) && isIdentifiedObject(O1) && !isa<Constant>(O1)))
  1173. return NoAlias;
  1174. // Function arguments can't alias with things that are known to be
  1175. // unambigously identified at the function level.
  1176. if ((isa<Argument>(O1) && isIdentifiedFunctionLocal(O2)) ||
  1177. (isa<Argument>(O2) && isIdentifiedFunctionLocal(O1)))
  1178. return NoAlias;
  1179. // Most objects can't alias null.
  1180. if ((isa<ConstantPointerNull>(O2) && isKnownNonNull(O1)) ||
  1181. (isa<ConstantPointerNull>(O1) && isKnownNonNull(O2)))
  1182. return NoAlias;
  1183. // If one pointer is the result of a call/invoke or load and the other is a
  1184. // non-escaping local object within the same function, then we know the
  1185. // object couldn't escape to a point where the call could return it.
  1186. //
  1187. // Note that if the pointers are in different functions, there are a
  1188. // variety of complications. A call with a nocapture argument may still
  1189. // temporary store the nocapture argument's value in a temporary memory
  1190. // location if that memory location doesn't escape. Or it may pass a
  1191. // nocapture value to other functions as long as they don't capture it.
  1192. if (isEscapeSource(O1) && isNonEscapingLocalObject(O2))
  1193. return NoAlias;
  1194. if (isEscapeSource(O2) && isNonEscapingLocalObject(O1))
  1195. return NoAlias;
  1196. }
  1197. // If the size of one access is larger than the entire object on the other
  1198. // side, then we know such behavior is undefined and can assume no alias.
  1199. if ((V1Size != MemoryLocation::UnknownSize &&
  1200. isObjectSmallerThan(O2, V1Size, DL, TLI)) ||
  1201. (V2Size != MemoryLocation::UnknownSize &&
  1202. isObjectSmallerThan(O1, V2Size, DL, TLI)))
  1203. return NoAlias;
  1204. // Check the cache before climbing up use-def chains. This also terminates
  1205. // otherwise infinitely recursive queries.
  1206. LocPair Locs(MemoryLocation(V1, V1Size, V1AAInfo),
  1207. MemoryLocation(V2, V2Size, V2AAInfo));
  1208. if (V1 > V2)
  1209. std::swap(Locs.first, Locs.second);
  1210. std::pair<AliasCacheTy::iterator, bool> Pair =
  1211. AliasCache.insert(std::make_pair(Locs, MayAlias));
  1212. if (!Pair.second)
  1213. return Pair.first->second;
  1214. // FIXME: This isn't aggressively handling alias(GEP, PHI) for example: if the
  1215. // GEP can't simplify, we don't even look at the PHI cases.
  1216. if (!isa<GEPOperator>(V1) && isa<GEPOperator>(V2)) {
  1217. std::swap(V1, V2);
  1218. std::swap(V1Size, V2Size);
  1219. std::swap(O1, O2);
  1220. std::swap(V1AAInfo, V2AAInfo);
  1221. }
  1222. if (const GEPOperator *GV1 = dyn_cast<GEPOperator>(V1)) {
  1223. AliasResult Result =
  1224. aliasGEP(GV1, V1Size, V1AAInfo, V2, V2Size, V2AAInfo, O1, O2);
  1225. if (Result != MayAlias)
  1226. return AliasCache[Locs] = Result;
  1227. }
  1228. if (isa<PHINode>(V2) && !isa<PHINode>(V1)) {
  1229. std::swap(V1, V2);
  1230. std::swap(V1Size, V2Size);
  1231. std::swap(V1AAInfo, V2AAInfo);
  1232. }
  1233. if (const PHINode *PN = dyn_cast<PHINode>(V1)) {
  1234. AliasResult Result = aliasPHI(PN, V1Size, V1AAInfo, V2, V2Size, V2AAInfo);
  1235. if (Result != MayAlias)
  1236. return AliasCache[Locs] = Result;
  1237. }
  1238. if (isa<SelectInst>(V2) && !isa<SelectInst>(V1)) {
  1239. std::swap(V1, V2);
  1240. std::swap(V1Size, V2Size);
  1241. std::swap(V1AAInfo, V2AAInfo);
  1242. }
  1243. if (const SelectInst *S1 = dyn_cast<SelectInst>(V1)) {
  1244. AliasResult Result =
  1245. aliasSelect(S1, V1Size, V1AAInfo, V2, V2Size, V2AAInfo);
  1246. if (Result != MayAlias)
  1247. return AliasCache[Locs] = Result;
  1248. }
  1249. // If both pointers are pointing into the same object and one of them
  1250. // accesses the entire object, then the accesses must overlap in some way.
  1251. if (O1 == O2)
  1252. if ((V1Size != MemoryLocation::UnknownSize &&
  1253. isObjectSize(O1, V1Size, DL, TLI)) ||
  1254. (V2Size != MemoryLocation::UnknownSize &&
  1255. isObjectSize(O2, V2Size, DL, TLI)))
  1256. return AliasCache[Locs] = PartialAlias;
  1257. // Recurse back into the best AA results we have, potentially with refined
  1258. // memory locations. We have already ensured that BasicAA has a MayAlias
  1259. // cache result for these, so any recursion back into BasicAA won't loop.
  1260. AliasResult Result = getBestAAResults().alias(Locs.first, Locs.second);
  1261. return AliasCache[Locs] = Result;
  1262. }
  1263. /// Check whether two Values can be considered equivalent.
  1264. ///
  1265. /// In addition to pointer equivalence of \p V1 and \p V2 this checks whether
  1266. /// they can not be part of a cycle in the value graph by looking at all
  1267. /// visited phi nodes an making sure that the phis cannot reach the value. We
  1268. /// have to do this because we are looking through phi nodes (That is we say
  1269. /// noalias(V, phi(VA, VB)) if noalias(V, VA) and noalias(V, VB).
  1270. bool BasicAAResult::isValueEqualInPotentialCycles(const Value *V,
  1271. const Value *V2) {
  1272. if (V != V2)
  1273. return false;
  1274. const Instruction *Inst = dyn_cast<Instruction>(V);
  1275. if (!Inst)
  1276. return true;
  1277. if (VisitedPhiBBs.empty())
  1278. return true;
  1279. if (VisitedPhiBBs.size() > MaxNumPhiBBsValueReachabilityCheck)
  1280. return false;
  1281. // Make sure that the visited phis cannot reach the Value. This ensures that
  1282. // the Values cannot come from different iterations of a potential cycle the
  1283. // phi nodes could be involved in.
  1284. for (auto *P : VisitedPhiBBs)
  1285. if (isPotentiallyReachable(&P->front(), Inst, DT, LI))
  1286. return false;
  1287. return true;
  1288. }
  1289. /// Computes the symbolic difference between two de-composed GEPs.
  1290. ///
  1291. /// Dest and Src are the variable indices from two decomposed GetElementPtr
  1292. /// instructions GEP1 and GEP2 which have common base pointers.
  1293. void BasicAAResult::GetIndexDifference(
  1294. SmallVectorImpl<VariableGEPIndex> &Dest,
  1295. const SmallVectorImpl<VariableGEPIndex> &Src) {
  1296. if (Src.empty())
  1297. return;
  1298. for (unsigned i = 0, e = Src.size(); i != e; ++i) {
  1299. const Value *V = Src[i].V;
  1300. unsigned ZExtBits = Src[i].ZExtBits, SExtBits = Src[i].SExtBits;
  1301. int64_t Scale = Src[i].Scale;
  1302. // Find V in Dest. This is N^2, but pointer indices almost never have more
  1303. // than a few variable indexes.
  1304. for (unsigned j = 0, e = Dest.size(); j != e; ++j) {
  1305. if (!isValueEqualInPotentialCycles(Dest[j].V, V) ||
  1306. Dest[j].ZExtBits != ZExtBits || Dest[j].SExtBits != SExtBits)
  1307. continue;
  1308. // If we found it, subtract off Scale V's from the entry in Dest. If it
  1309. // goes to zero, remove the entry.
  1310. if (Dest[j].Scale != Scale)
  1311. Dest[j].Scale -= Scale;
  1312. else
  1313. Dest.erase(Dest.begin() + j);
  1314. Scale = 0;
  1315. break;
  1316. }
  1317. // If we didn't consume this entry, add it to the end of the Dest list.
  1318. if (Scale) {
  1319. VariableGEPIndex Entry = {V, ZExtBits, SExtBits, -Scale};
  1320. Dest.push_back(Entry);
  1321. }
  1322. }
  1323. }
  1324. bool BasicAAResult::constantOffsetHeuristic(
  1325. const SmallVectorImpl<VariableGEPIndex> &VarIndices, uint64_t V1Size,
  1326. uint64_t V2Size, int64_t BaseOffset, AssumptionCache *AC,
  1327. DominatorTree *DT) {
  1328. if (VarIndices.size() != 2 || V1Size == MemoryLocation::UnknownSize ||
  1329. V2Size == MemoryLocation::UnknownSize)
  1330. return false;
  1331. const VariableGEPIndex &Var0 = VarIndices[0], &Var1 = VarIndices[1];
  1332. if (Var0.ZExtBits != Var1.ZExtBits || Var0.SExtBits != Var1.SExtBits ||
  1333. Var0.Scale != -Var1.Scale)
  1334. return false;
  1335. unsigned Width = Var1.V->getType()->getIntegerBitWidth();
  1336. // We'll strip off the Extensions of Var0 and Var1 and do another round
  1337. // of GetLinearExpression decomposition. In the example above, if Var0
  1338. // is zext(%x + 1) we should get V1 == %x and V1Offset == 1.
  1339. APInt V0Scale(Width, 0), V0Offset(Width, 0), V1Scale(Width, 0),
  1340. V1Offset(Width, 0);
  1341. bool NSW = true, NUW = true;
  1342. unsigned V0ZExtBits = 0, V0SExtBits = 0, V1ZExtBits = 0, V1SExtBits = 0;
  1343. const Value *V0 = GetLinearExpression(Var0.V, V0Scale, V0Offset, V0ZExtBits,
  1344. V0SExtBits, DL, 0, AC, DT, NSW, NUW);
  1345. NSW = true, NUW = true;
  1346. const Value *V1 = GetLinearExpression(Var1.V, V1Scale, V1Offset, V1ZExtBits,
  1347. V1SExtBits, DL, 0, AC, DT, NSW, NUW);
  1348. if (V0Scale != V1Scale || V0ZExtBits != V1ZExtBits ||
  1349. V0SExtBits != V1SExtBits || !isValueEqualInPotentialCycles(V0, V1))
  1350. return false;
  1351. // We have a hit - Var0 and Var1 only differ by a constant offset!
  1352. // If we've been sext'ed then zext'd the maximum difference between Var0 and
  1353. // Var1 is possible to calculate, but we're just interested in the absolute
  1354. // minimum difference between the two. The minimum distance may occur due to
  1355. // wrapping; consider "add i3 %i, 5": if %i == 7 then 7 + 5 mod 8 == 4, and so
  1356. // the minimum distance between %i and %i + 5 is 3.
  1357. APInt MinDiff = V0Offset - V1Offset, Wrapped = -MinDiff;
  1358. MinDiff = APIntOps::umin(MinDiff, Wrapped);
  1359. uint64_t MinDiffBytes = MinDiff.getZExtValue() * std::abs(Var0.Scale);
  1360. // We can't definitely say whether GEP1 is before or after V2 due to wrapping
  1361. // arithmetic (i.e. for some values of GEP1 and V2 GEP1 < V2, and for other
  1362. // values GEP1 > V2). We'll therefore only declare NoAlias if both V1Size and
  1363. // V2Size can fit in the MinDiffBytes gap.
  1364. return V1Size + std::abs(BaseOffset) <= MinDiffBytes &&
  1365. V2Size + std::abs(BaseOffset) <= MinDiffBytes;
  1366. }
  1367. //===----------------------------------------------------------------------===//
  1368. // BasicAliasAnalysis Pass
  1369. //===----------------------------------------------------------------------===//
  1370. char BasicAA::PassID;
  1371. BasicAAResult BasicAA::run(Function &F, AnalysisManager<Function> *AM) {
  1372. return BasicAAResult(F.getParent()->getDataLayout(),
  1373. AM->getResult<TargetLibraryAnalysis>(F),
  1374. AM->getResult<AssumptionAnalysis>(F),
  1375. AM->getCachedResult<DominatorTreeAnalysis>(F),
  1376. AM->getCachedResult<LoopAnalysis>(F));
  1377. }
  1378. BasicAAWrapperPass::BasicAAWrapperPass() : FunctionPass(ID) {
  1379. initializeBasicAAWrapperPassPass(*PassRegistry::getPassRegistry());
  1380. }
  1381. char BasicAAWrapperPass::ID = 0;
  1382. void BasicAAWrapperPass::anchor() {}
  1383. INITIALIZE_PASS_BEGIN(BasicAAWrapperPass, "basicaa",
  1384. "Basic Alias Analysis (stateless AA impl)", true, true)
  1385. INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
  1386. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  1387. INITIALIZE_PASS_END(BasicAAWrapperPass, "basicaa",
  1388. "Basic Alias Analysis (stateless AA impl)", true, true)
  1389. FunctionPass *llvm::createBasicAAWrapperPass() {
  1390. return new BasicAAWrapperPass();
  1391. }
  1392. bool BasicAAWrapperPass::runOnFunction(Function &F) {
  1393. auto &ACT = getAnalysis<AssumptionCacheTracker>();
  1394. auto &TLIWP = getAnalysis<TargetLibraryInfoWrapperPass>();
  1395. auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
  1396. auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>();
  1397. Result.reset(new BasicAAResult(F.getParent()->getDataLayout(), TLIWP.getTLI(),
  1398. ACT.getAssumptionCache(F),
  1399. DTWP ? &DTWP->getDomTree() : nullptr,
  1400. LIWP ? &LIWP->getLoopInfo() : nullptr));
  1401. return false;
  1402. }
  1403. void BasicAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
  1404. AU.setPreservesAll();
  1405. AU.addRequired<AssumptionCacheTracker>();
  1406. AU.addRequired<TargetLibraryInfoWrapperPass>();
  1407. }
  1408. BasicAAResult llvm::createLegacyPMBasicAAResult(Pass &P, Function &F) {
  1409. return BasicAAResult(
  1410. F.getParent()->getDataLayout(),
  1411. P.getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(),
  1412. P.getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F));
  1413. }