BasicAliasAnalysis.cpp 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922
  1. //===- BasicAliasAnalysis.cpp - Stateless Alias Analysis Impl -------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines the primary stateless implementation of the
  10. // Alias Analysis interface that implements identities (two different
  11. // globals cannot alias, etc), but does no stateful analysis.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/Analysis/BasicAliasAnalysis.h"
  15. #include "llvm/ADT/APInt.h"
  16. #include "llvm/ADT/ScopeExit.h"
  17. #include "llvm/ADT/SmallPtrSet.h"
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/ADT/Statistic.h"
  20. #include "llvm/Analysis/AliasAnalysis.h"
  21. #include "llvm/Analysis/AssumptionCache.h"
  22. #include "llvm/Analysis/CFG.h"
  23. #include "llvm/Analysis/CaptureTracking.h"
  24. #include "llvm/Analysis/InstructionSimplify.h"
  25. #include "llvm/Analysis/MemoryBuiltins.h"
  26. #include "llvm/Analysis/MemoryLocation.h"
  27. #include "llvm/Analysis/PhiValues.h"
  28. #include "llvm/Analysis/TargetLibraryInfo.h"
  29. #include "llvm/Analysis/ValueTracking.h"
  30. #include "llvm/IR/Argument.h"
  31. #include "llvm/IR/Attributes.h"
  32. #include "llvm/IR/Constant.h"
  33. #include "llvm/IR/ConstantRange.h"
  34. #include "llvm/IR/Constants.h"
  35. #include "llvm/IR/DataLayout.h"
  36. #include "llvm/IR/DerivedTypes.h"
  37. #include "llvm/IR/Dominators.h"
  38. #include "llvm/IR/Function.h"
  39. #include "llvm/IR/GetElementPtrTypeIterator.h"
  40. #include "llvm/IR/GlobalAlias.h"
  41. #include "llvm/IR/GlobalVariable.h"
  42. #include "llvm/IR/InstrTypes.h"
  43. #include "llvm/IR/Instruction.h"
  44. #include "llvm/IR/Instructions.h"
  45. #include "llvm/IR/IntrinsicInst.h"
  46. #include "llvm/IR/Intrinsics.h"
  47. #include "llvm/IR/Metadata.h"
  48. #include "llvm/IR/Operator.h"
  49. #include "llvm/IR/Type.h"
  50. #include "llvm/IR/User.h"
  51. #include "llvm/IR/Value.h"
  52. #include "llvm/InitializePasses.h"
  53. #include "llvm/Pass.h"
  54. #include "llvm/Support/Casting.h"
  55. #include "llvm/Support/CommandLine.h"
  56. #include "llvm/Support/Compiler.h"
  57. #include "llvm/Support/KnownBits.h"
  58. #include <cassert>
  59. #include <cstdint>
  60. #include <cstdlib>
  61. #include <utility>
  62. #define DEBUG_TYPE "basicaa"
  63. using namespace llvm;
  64. /// Enable analysis of recursive PHI nodes.
  65. static cl::opt<bool> EnableRecPhiAnalysis("basic-aa-recphi", cl::Hidden,
  66. cl::init(true));
  67. /// SearchLimitReached / SearchTimes shows how often the limit of
  68. /// to decompose GEPs is reached. It will affect the precision
  69. /// of basic alias analysis.
  70. STATISTIC(SearchLimitReached, "Number of times the limit to "
  71. "decompose GEPs is reached");
  72. STATISTIC(SearchTimes, "Number of times a GEP is decomposed");
  73. /// Cutoff after which to stop analysing a set of phi nodes potentially involved
  74. /// in a cycle. Because we are analysing 'through' phi nodes, we need to be
  75. /// careful with value equivalence. We use reachability to make sure a value
  76. /// cannot be involved in a cycle.
  77. const unsigned MaxNumPhiBBsValueReachabilityCheck = 20;
  78. // The max limit of the search depth in DecomposeGEPExpression() and
  79. // getUnderlyingObject().
  80. static const unsigned MaxLookupSearchDepth = 6;
  81. bool BasicAAResult::invalidate(Function &Fn, const PreservedAnalyses &PA,
  82. FunctionAnalysisManager::Invalidator &Inv) {
  83. // We don't care if this analysis itself is preserved, it has no state. But
  84. // we need to check that the analyses it depends on have been. Note that we
  85. // may be created without handles to some analyses and in that case don't
  86. // depend on them.
  87. if (Inv.invalidate<AssumptionAnalysis>(Fn, PA) ||
  88. (DT && Inv.invalidate<DominatorTreeAnalysis>(Fn, PA)) ||
  89. (PV && Inv.invalidate<PhiValuesAnalysis>(Fn, PA)))
  90. return true;
  91. // Otherwise this analysis result remains valid.
  92. return false;
  93. }
  94. //===----------------------------------------------------------------------===//
  95. // Useful predicates
  96. //===----------------------------------------------------------------------===//
  97. /// Returns true if the pointer is one which would have been considered an
  98. /// escape by isNonEscapingLocalObject.
  99. static bool isEscapeSource(const Value *V) {
  100. if (isa<CallBase>(V))
  101. return true;
  102. // The load case works because isNonEscapingLocalObject considers all
  103. // stores to be escapes (it passes true for the StoreCaptures argument
  104. // to PointerMayBeCaptured).
  105. if (isa<LoadInst>(V))
  106. return true;
  107. // The inttoptr case works because isNonEscapingLocalObject considers all
  108. // means of converting or equating a pointer to an int (ptrtoint, ptr store
  109. // which could be followed by an integer load, ptr<->int compare) as
  110. // escaping, and objects located at well-known addresses via platform-specific
  111. // means cannot be considered non-escaping local objects.
  112. if (isa<IntToPtrInst>(V))
  113. return true;
  114. return false;
  115. }
  116. /// Returns the size of the object specified by V or UnknownSize if unknown.
  117. static uint64_t getObjectSize(const Value *V, const DataLayout &DL,
  118. const TargetLibraryInfo &TLI,
  119. bool NullIsValidLoc,
  120. bool RoundToAlign = false) {
  121. uint64_t Size;
  122. ObjectSizeOpts Opts;
  123. Opts.RoundToAlign = RoundToAlign;
  124. Opts.NullIsUnknownSize = NullIsValidLoc;
  125. if (getObjectSize(V, Size, DL, &TLI, Opts))
  126. return Size;
  127. return MemoryLocation::UnknownSize;
  128. }
  129. /// Returns true if we can prove that the object specified by V is smaller than
  130. /// Size.
  131. static bool isObjectSmallerThan(const Value *V, uint64_t Size,
  132. const DataLayout &DL,
  133. const TargetLibraryInfo &TLI,
  134. bool NullIsValidLoc) {
  135. // Note that the meanings of the "object" are slightly different in the
  136. // following contexts:
  137. // c1: llvm::getObjectSize()
  138. // c2: llvm.objectsize() intrinsic
  139. // c3: isObjectSmallerThan()
  140. // c1 and c2 share the same meaning; however, the meaning of "object" in c3
  141. // refers to the "entire object".
  142. //
  143. // Consider this example:
  144. // char *p = (char*)malloc(100)
  145. // char *q = p+80;
  146. //
  147. // In the context of c1 and c2, the "object" pointed by q refers to the
  148. // stretch of memory of q[0:19]. So, getObjectSize(q) should return 20.
  149. //
  150. // However, in the context of c3, the "object" refers to the chunk of memory
  151. // being allocated. So, the "object" has 100 bytes, and q points to the middle
  152. // the "object". In case q is passed to isObjectSmallerThan() as the 1st
  153. // parameter, before the llvm::getObjectSize() is called to get the size of
  154. // entire object, we should:
  155. // - either rewind the pointer q to the base-address of the object in
  156. // question (in this case rewind to p), or
  157. // - just give up. It is up to caller to make sure the pointer is pointing
  158. // to the base address the object.
  159. //
  160. // We go for 2nd option for simplicity.
  161. if (!isIdentifiedObject(V))
  162. return false;
  163. // This function needs to use the aligned object size because we allow
  164. // reads a bit past the end given sufficient alignment.
  165. uint64_t ObjectSize = getObjectSize(V, DL, TLI, NullIsValidLoc,
  166. /*RoundToAlign*/ true);
  167. return ObjectSize != MemoryLocation::UnknownSize && ObjectSize < Size;
  168. }
  169. /// Return the minimal extent from \p V to the end of the underlying object,
  170. /// assuming the result is used in an aliasing query. E.g., we do use the query
  171. /// location size and the fact that null pointers cannot alias here.
  172. static uint64_t getMinimalExtentFrom(const Value &V,
  173. const LocationSize &LocSize,
  174. const DataLayout &DL,
  175. bool NullIsValidLoc) {
  176. // If we have dereferenceability information we know a lower bound for the
  177. // extent as accesses for a lower offset would be valid. We need to exclude
  178. // the "or null" part if null is a valid pointer. We can ignore frees, as an
  179. // access after free would be undefined behavior.
  180. bool CanBeNull, CanBeFreed;
  181. uint64_t DerefBytes =
  182. V.getPointerDereferenceableBytes(DL, CanBeNull, CanBeFreed);
  183. DerefBytes = (CanBeNull && NullIsValidLoc) ? 0 : DerefBytes;
  184. // If queried with a precise location size, we assume that location size to be
  185. // accessed, thus valid.
  186. if (LocSize.isPrecise())
  187. DerefBytes = std::max(DerefBytes, LocSize.getValue());
  188. return DerefBytes;
  189. }
  190. /// Returns true if we can prove that the object specified by V has size Size.
  191. static bool isObjectSize(const Value *V, uint64_t Size, const DataLayout &DL,
  192. const TargetLibraryInfo &TLI, bool NullIsValidLoc) {
  193. uint64_t ObjectSize = getObjectSize(V, DL, TLI, NullIsValidLoc);
  194. return ObjectSize != MemoryLocation::UnknownSize && ObjectSize == Size;
  195. }
  196. //===----------------------------------------------------------------------===//
  197. // CaptureInfo implementations
  198. //===----------------------------------------------------------------------===//
  199. CaptureInfo::~CaptureInfo() = default;
  200. bool SimpleCaptureInfo::isNotCapturedBeforeOrAt(const Value *Object,
  201. const Instruction *I) {
  202. return isNonEscapingLocalObject(Object, &IsCapturedCache);
  203. }
  204. bool EarliestEscapeInfo::isNotCapturedBeforeOrAt(const Value *Object,
  205. const Instruction *I) {
  206. if (!isIdentifiedFunctionLocal(Object))
  207. return false;
  208. auto Iter = EarliestEscapes.insert({Object, nullptr});
  209. if (Iter.second) {
  210. Instruction *EarliestCapture = FindEarliestCapture(
  211. Object, *const_cast<Function *>(I->getFunction()),
  212. /*ReturnCaptures=*/false, /*StoreCaptures=*/true, DT);
  213. if (EarliestCapture) {
  214. auto Ins = Inst2Obj.insert({EarliestCapture, {}});
  215. Ins.first->second.push_back(Object);
  216. }
  217. Iter.first->second = EarliestCapture;
  218. }
  219. // No capturing instruction.
  220. if (!Iter.first->second)
  221. return true;
  222. return I != Iter.first->second &&
  223. !isPotentiallyReachable(Iter.first->second, I, nullptr, &DT, &LI);
  224. }
  225. void EarliestEscapeInfo::removeInstruction(Instruction *I) {
  226. auto Iter = Inst2Obj.find(I);
  227. if (Iter != Inst2Obj.end()) {
  228. for (const Value *Obj : Iter->second)
  229. EarliestEscapes.erase(Obj);
  230. Inst2Obj.erase(I);
  231. }
  232. }
  233. //===----------------------------------------------------------------------===//
  234. // GetElementPtr Instruction Decomposition and Analysis
  235. //===----------------------------------------------------------------------===//
  236. namespace {
  237. /// Represents zext(sext(trunc(V))).
  238. struct CastedValue {
  239. const Value *V;
  240. unsigned ZExtBits = 0;
  241. unsigned SExtBits = 0;
  242. unsigned TruncBits = 0;
  243. explicit CastedValue(const Value *V) : V(V) {}
  244. explicit CastedValue(const Value *V, unsigned ZExtBits, unsigned SExtBits,
  245. unsigned TruncBits)
  246. : V(V), ZExtBits(ZExtBits), SExtBits(SExtBits), TruncBits(TruncBits) {}
  247. unsigned getBitWidth() const {
  248. return V->getType()->getPrimitiveSizeInBits() - TruncBits + ZExtBits +
  249. SExtBits;
  250. }
  251. CastedValue withValue(const Value *NewV) const {
  252. return CastedValue(NewV, ZExtBits, SExtBits, TruncBits);
  253. }
  254. /// Replace V with zext(NewV)
  255. CastedValue withZExtOfValue(const Value *NewV) const {
  256. unsigned ExtendBy = V->getType()->getPrimitiveSizeInBits() -
  257. NewV->getType()->getPrimitiveSizeInBits();
  258. if (ExtendBy <= TruncBits)
  259. return CastedValue(NewV, ZExtBits, SExtBits, TruncBits - ExtendBy);
  260. // zext(sext(zext(NewV))) == zext(zext(zext(NewV)))
  261. ExtendBy -= TruncBits;
  262. return CastedValue(NewV, ZExtBits + SExtBits + ExtendBy, 0, 0);
  263. }
  264. /// Replace V with sext(NewV)
  265. CastedValue withSExtOfValue(const Value *NewV) const {
  266. unsigned ExtendBy = V->getType()->getPrimitiveSizeInBits() -
  267. NewV->getType()->getPrimitiveSizeInBits();
  268. if (ExtendBy <= TruncBits)
  269. return CastedValue(NewV, ZExtBits, SExtBits, TruncBits - ExtendBy);
  270. // zext(sext(sext(NewV)))
  271. ExtendBy -= TruncBits;
  272. return CastedValue(NewV, ZExtBits, SExtBits + ExtendBy, 0);
  273. }
  274. APInt evaluateWith(APInt N) const {
  275. assert(N.getBitWidth() == V->getType()->getPrimitiveSizeInBits() &&
  276. "Incompatible bit width");
  277. if (TruncBits) N = N.trunc(N.getBitWidth() - TruncBits);
  278. if (SExtBits) N = N.sext(N.getBitWidth() + SExtBits);
  279. if (ZExtBits) N = N.zext(N.getBitWidth() + ZExtBits);
  280. return N;
  281. }
  282. ConstantRange evaluateWith(ConstantRange N) const {
  283. assert(N.getBitWidth() == V->getType()->getPrimitiveSizeInBits() &&
  284. "Incompatible bit width");
  285. if (TruncBits) N = N.truncate(N.getBitWidth() - TruncBits);
  286. if (SExtBits) N = N.signExtend(N.getBitWidth() + SExtBits);
  287. if (ZExtBits) N = N.zeroExtend(N.getBitWidth() + ZExtBits);
  288. return N;
  289. }
  290. bool canDistributeOver(bool NUW, bool NSW) const {
  291. // zext(x op<nuw> y) == zext(x) op<nuw> zext(y)
  292. // sext(x op<nsw> y) == sext(x) op<nsw> sext(y)
  293. // trunc(x op y) == trunc(x) op trunc(y)
  294. return (!ZExtBits || NUW) && (!SExtBits || NSW);
  295. }
  296. bool hasSameCastsAs(const CastedValue &Other) const {
  297. return ZExtBits == Other.ZExtBits && SExtBits == Other.SExtBits &&
  298. TruncBits == Other.TruncBits;
  299. }
  300. };
  301. /// Represents zext(sext(trunc(V))) * Scale + Offset.
  302. struct LinearExpression {
  303. CastedValue Val;
  304. APInt Scale;
  305. APInt Offset;
  306. /// True if all operations in this expression are NSW.
  307. bool IsNSW;
  308. LinearExpression(const CastedValue &Val, const APInt &Scale,
  309. const APInt &Offset, bool IsNSW)
  310. : Val(Val), Scale(Scale), Offset(Offset), IsNSW(IsNSW) {}
  311. LinearExpression(const CastedValue &Val) : Val(Val), IsNSW(true) {
  312. unsigned BitWidth = Val.getBitWidth();
  313. Scale = APInt(BitWidth, 1);
  314. Offset = APInt(BitWidth, 0);
  315. }
  316. LinearExpression mul(const APInt &Other, bool MulIsNSW) const {
  317. // The check for zero offset is necessary, because generally
  318. // (X +nsw Y) *nsw Z does not imply (X *nsw Z) +nsw (Y *nsw Z).
  319. bool NSW = IsNSW && (Other.isOne() || (MulIsNSW && Offset.isZero()));
  320. return LinearExpression(Val, Scale * Other, Offset * Other, NSW);
  321. }
  322. };
  323. }
  324. /// Analyzes the specified value as a linear expression: "A*V + B", where A and
  325. /// B are constant integers.
  326. static LinearExpression GetLinearExpression(
  327. const CastedValue &Val, const DataLayout &DL, unsigned Depth,
  328. AssumptionCache *AC, DominatorTree *DT) {
  329. // Limit our recursion depth.
  330. if (Depth == 6)
  331. return Val;
  332. if (const ConstantInt *Const = dyn_cast<ConstantInt>(Val.V))
  333. return LinearExpression(Val, APInt(Val.getBitWidth(), 0),
  334. Val.evaluateWith(Const->getValue()), true);
  335. if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(Val.V)) {
  336. if (ConstantInt *RHSC = dyn_cast<ConstantInt>(BOp->getOperand(1))) {
  337. APInt RHS = Val.evaluateWith(RHSC->getValue());
  338. // The only non-OBO case we deal with is or, and only limited to the
  339. // case where it is both nuw and nsw.
  340. bool NUW = true, NSW = true;
  341. if (isa<OverflowingBinaryOperator>(BOp)) {
  342. NUW &= BOp->hasNoUnsignedWrap();
  343. NSW &= BOp->hasNoSignedWrap();
  344. }
  345. if (!Val.canDistributeOver(NUW, NSW))
  346. return Val;
  347. // While we can distribute over trunc, we cannot preserve nowrap flags
  348. // in that case.
  349. if (Val.TruncBits)
  350. NUW = NSW = false;
  351. LinearExpression E(Val);
  352. switch (BOp->getOpcode()) {
  353. default:
  354. // We don't understand this instruction, so we can't decompose it any
  355. // further.
  356. return Val;
  357. case Instruction::Or:
  358. // X|C == X+C if all the bits in C are unset in X. Otherwise we can't
  359. // analyze it.
  360. if (!MaskedValueIsZero(BOp->getOperand(0), RHSC->getValue(), DL, 0, AC,
  361. BOp, DT))
  362. return Val;
  363. LLVM_FALLTHROUGH;
  364. case Instruction::Add: {
  365. E = GetLinearExpression(Val.withValue(BOp->getOperand(0)), DL,
  366. Depth + 1, AC, DT);
  367. E.Offset += RHS;
  368. E.IsNSW &= NSW;
  369. break;
  370. }
  371. case Instruction::Sub: {
  372. E = GetLinearExpression(Val.withValue(BOp->getOperand(0)), DL,
  373. Depth + 1, AC, DT);
  374. E.Offset -= RHS;
  375. E.IsNSW &= NSW;
  376. break;
  377. }
  378. case Instruction::Mul:
  379. E = GetLinearExpression(Val.withValue(BOp->getOperand(0)), DL,
  380. Depth + 1, AC, DT)
  381. .mul(RHS, NSW);
  382. break;
  383. case Instruction::Shl:
  384. // We're trying to linearize an expression of the kind:
  385. // shl i8 -128, 36
  386. // where the shift count exceeds the bitwidth of the type.
  387. // We can't decompose this further (the expression would return
  388. // a poison value).
  389. if (RHS.getLimitedValue() > Val.getBitWidth())
  390. return Val;
  391. E = GetLinearExpression(Val.withValue(BOp->getOperand(0)), DL,
  392. Depth + 1, AC, DT);
  393. E.Offset <<= RHS.getLimitedValue();
  394. E.Scale <<= RHS.getLimitedValue();
  395. E.IsNSW &= NSW;
  396. break;
  397. }
  398. return E;
  399. }
  400. }
  401. if (isa<ZExtInst>(Val.V))
  402. return GetLinearExpression(
  403. Val.withZExtOfValue(cast<CastInst>(Val.V)->getOperand(0)),
  404. DL, Depth + 1, AC, DT);
  405. if (isa<SExtInst>(Val.V))
  406. return GetLinearExpression(
  407. Val.withSExtOfValue(cast<CastInst>(Val.V)->getOperand(0)),
  408. DL, Depth + 1, AC, DT);
  409. return Val;
  410. }
  411. /// To ensure a pointer offset fits in an integer of size IndexSize
  412. /// (in bits) when that size is smaller than the maximum index size. This is
  413. /// an issue, for example, in particular for 32b pointers with negative indices
  414. /// that rely on two's complement wrap-arounds for precise alias information
  415. /// where the maximum index size is 64b.
  416. static APInt adjustToIndexSize(const APInt &Offset, unsigned IndexSize) {
  417. assert(IndexSize <= Offset.getBitWidth() && "Invalid IndexSize!");
  418. unsigned ShiftBits = Offset.getBitWidth() - IndexSize;
  419. return (Offset << ShiftBits).ashr(ShiftBits);
  420. }
  421. namespace {
  422. // A linear transformation of a Value; this class represents
  423. // ZExt(SExt(Trunc(V, TruncBits), SExtBits), ZExtBits) * Scale.
  424. struct VariableGEPIndex {
  425. CastedValue Val;
  426. APInt Scale;
  427. // Context instruction to use when querying information about this index.
  428. const Instruction *CxtI;
  429. /// True if all operations in this expression are NSW.
  430. bool IsNSW;
  431. void dump() const {
  432. print(dbgs());
  433. dbgs() << "\n";
  434. }
  435. void print(raw_ostream &OS) const {
  436. OS << "(V=" << Val.V->getName()
  437. << ", zextbits=" << Val.ZExtBits
  438. << ", sextbits=" << Val.SExtBits
  439. << ", truncbits=" << Val.TruncBits
  440. << ", scale=" << Scale << ")";
  441. }
  442. };
  443. }
  444. // Represents the internal structure of a GEP, decomposed into a base pointer,
  445. // constant offsets, and variable scaled indices.
  446. struct BasicAAResult::DecomposedGEP {
  447. // Base pointer of the GEP
  448. const Value *Base;
  449. // Total constant offset from base.
  450. APInt Offset;
  451. // Scaled variable (non-constant) indices.
  452. SmallVector<VariableGEPIndex, 4> VarIndices;
  453. // Are all operations inbounds GEPs or non-indexing operations?
  454. // (None iff expression doesn't involve any geps)
  455. Optional<bool> InBounds;
  456. void dump() const {
  457. print(dbgs());
  458. dbgs() << "\n";
  459. }
  460. void print(raw_ostream &OS) const {
  461. OS << "(DecomposedGEP Base=" << Base->getName()
  462. << ", Offset=" << Offset
  463. << ", VarIndices=[";
  464. for (size_t i = 0; i < VarIndices.size(); i++) {
  465. if (i != 0)
  466. OS << ", ";
  467. VarIndices[i].print(OS);
  468. }
  469. OS << "])";
  470. }
  471. };
  472. /// If V is a symbolic pointer expression, decompose it into a base pointer
  473. /// with a constant offset and a number of scaled symbolic offsets.
  474. ///
  475. /// The scaled symbolic offsets (represented by pairs of a Value* and a scale
  476. /// in the VarIndices vector) are Value*'s that are known to be scaled by the
  477. /// specified amount, but which may have other unrepresented high bits. As
  478. /// such, the gep cannot necessarily be reconstructed from its decomposed form.
  479. BasicAAResult::DecomposedGEP
  480. BasicAAResult::DecomposeGEPExpression(const Value *V, const DataLayout &DL,
  481. AssumptionCache *AC, DominatorTree *DT) {
  482. // Limit recursion depth to limit compile time in crazy cases.
  483. unsigned MaxLookup = MaxLookupSearchDepth;
  484. SearchTimes++;
  485. const Instruction *CxtI = dyn_cast<Instruction>(V);
  486. unsigned MaxIndexSize = DL.getMaxIndexSizeInBits();
  487. DecomposedGEP Decomposed;
  488. Decomposed.Offset = APInt(MaxIndexSize, 0);
  489. do {
  490. // See if this is a bitcast or GEP.
  491. const Operator *Op = dyn_cast<Operator>(V);
  492. if (!Op) {
  493. // The only non-operator case we can handle are GlobalAliases.
  494. if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
  495. if (!GA->isInterposable()) {
  496. V = GA->getAliasee();
  497. continue;
  498. }
  499. }
  500. Decomposed.Base = V;
  501. return Decomposed;
  502. }
  503. if (Op->getOpcode() == Instruction::BitCast ||
  504. Op->getOpcode() == Instruction::AddrSpaceCast) {
  505. V = Op->getOperand(0);
  506. continue;
  507. }
  508. const GEPOperator *GEPOp = dyn_cast<GEPOperator>(Op);
  509. if (!GEPOp) {
  510. if (const auto *PHI = dyn_cast<PHINode>(V)) {
  511. // Look through single-arg phi nodes created by LCSSA.
  512. if (PHI->getNumIncomingValues() == 1) {
  513. V = PHI->getIncomingValue(0);
  514. continue;
  515. }
  516. } else if (const auto *Call = dyn_cast<CallBase>(V)) {
  517. // CaptureTracking can know about special capturing properties of some
  518. // intrinsics like launder.invariant.group, that can't be expressed with
  519. // the attributes, but have properties like returning aliasing pointer.
  520. // Because some analysis may assume that nocaptured pointer is not
  521. // returned from some special intrinsic (because function would have to
  522. // be marked with returns attribute), it is crucial to use this function
  523. // because it should be in sync with CaptureTracking. Not using it may
  524. // cause weird miscompilations where 2 aliasing pointers are assumed to
  525. // noalias.
  526. if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) {
  527. V = RP;
  528. continue;
  529. }
  530. }
  531. Decomposed.Base = V;
  532. return Decomposed;
  533. }
  534. // Track whether we've seen at least one in bounds gep, and if so, whether
  535. // all geps parsed were in bounds.
  536. if (Decomposed.InBounds == None)
  537. Decomposed.InBounds = GEPOp->isInBounds();
  538. else if (!GEPOp->isInBounds())
  539. Decomposed.InBounds = false;
  540. assert(GEPOp->getSourceElementType()->isSized() && "GEP must be sized");
  541. // Don't attempt to analyze GEPs if index scale is not a compile-time
  542. // constant.
  543. if (isa<ScalableVectorType>(GEPOp->getSourceElementType())) {
  544. Decomposed.Base = V;
  545. return Decomposed;
  546. }
  547. unsigned AS = GEPOp->getPointerAddressSpace();
  548. // Walk the indices of the GEP, accumulating them into BaseOff/VarIndices.
  549. gep_type_iterator GTI = gep_type_begin(GEPOp);
  550. unsigned IndexSize = DL.getIndexSizeInBits(AS);
  551. // Assume all GEP operands are constants until proven otherwise.
  552. bool GepHasConstantOffset = true;
  553. for (User::const_op_iterator I = GEPOp->op_begin() + 1, E = GEPOp->op_end();
  554. I != E; ++I, ++GTI) {
  555. const Value *Index = *I;
  556. // Compute the (potentially symbolic) offset in bytes for this index.
  557. if (StructType *STy = GTI.getStructTypeOrNull()) {
  558. // For a struct, add the member offset.
  559. unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue();
  560. if (FieldNo == 0)
  561. continue;
  562. Decomposed.Offset += DL.getStructLayout(STy)->getElementOffset(FieldNo);
  563. continue;
  564. }
  565. // For an array/pointer, add the element offset, explicitly scaled.
  566. if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Index)) {
  567. if (CIdx->isZero())
  568. continue;
  569. Decomposed.Offset +=
  570. DL.getTypeAllocSize(GTI.getIndexedType()).getFixedSize() *
  571. CIdx->getValue().sextOrTrunc(MaxIndexSize);
  572. continue;
  573. }
  574. GepHasConstantOffset = false;
  575. // If the integer type is smaller than the index size, it is implicitly
  576. // sign extended or truncated to index size.
  577. unsigned Width = Index->getType()->getIntegerBitWidth();
  578. unsigned SExtBits = IndexSize > Width ? IndexSize - Width : 0;
  579. unsigned TruncBits = IndexSize < Width ? Width - IndexSize : 0;
  580. LinearExpression LE = GetLinearExpression(
  581. CastedValue(Index, 0, SExtBits, TruncBits), DL, 0, AC, DT);
  582. // Scale by the type size.
  583. unsigned TypeSize =
  584. DL.getTypeAllocSize(GTI.getIndexedType()).getFixedSize();
  585. LE = LE.mul(APInt(IndexSize, TypeSize), GEPOp->isInBounds());
  586. Decomposed.Offset += LE.Offset.sextOrSelf(MaxIndexSize);
  587. APInt Scale = LE.Scale.sextOrSelf(MaxIndexSize);
  588. // If we already had an occurrence of this index variable, merge this
  589. // scale into it. For example, we want to handle:
  590. // A[x][x] -> x*16 + x*4 -> x*20
  591. // This also ensures that 'x' only appears in the index list once.
  592. for (unsigned i = 0, e = Decomposed.VarIndices.size(); i != e; ++i) {
  593. if (Decomposed.VarIndices[i].Val.V == LE.Val.V &&
  594. Decomposed.VarIndices[i].Val.hasSameCastsAs(LE.Val)) {
  595. Scale += Decomposed.VarIndices[i].Scale;
  596. Decomposed.VarIndices.erase(Decomposed.VarIndices.begin() + i);
  597. break;
  598. }
  599. }
  600. // Make sure that we have a scale that makes sense for this target's
  601. // index size.
  602. Scale = adjustToIndexSize(Scale, IndexSize);
  603. if (!!Scale) {
  604. VariableGEPIndex Entry = {LE.Val, Scale, CxtI, LE.IsNSW};
  605. Decomposed.VarIndices.push_back(Entry);
  606. }
  607. }
  608. // Take care of wrap-arounds
  609. if (GepHasConstantOffset)
  610. Decomposed.Offset = adjustToIndexSize(Decomposed.Offset, IndexSize);
  611. // Analyze the base pointer next.
  612. V = GEPOp->getOperand(0);
  613. } while (--MaxLookup);
  614. // If the chain of expressions is too deep, just return early.
  615. Decomposed.Base = V;
  616. SearchLimitReached++;
  617. return Decomposed;
  618. }
  619. /// Returns whether the given pointer value points to memory that is local to
  620. /// the function, with global constants being considered local to all
  621. /// functions.
  622. bool BasicAAResult::pointsToConstantMemory(const MemoryLocation &Loc,
  623. AAQueryInfo &AAQI, bool OrLocal) {
  624. assert(Visited.empty() && "Visited must be cleared after use!");
  625. unsigned MaxLookup = 8;
  626. SmallVector<const Value *, 16> Worklist;
  627. Worklist.push_back(Loc.Ptr);
  628. do {
  629. const Value *V = getUnderlyingObject(Worklist.pop_back_val());
  630. if (!Visited.insert(V).second) {
  631. Visited.clear();
  632. return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal);
  633. }
  634. // An alloca instruction defines local memory.
  635. if (OrLocal && isa<AllocaInst>(V))
  636. continue;
  637. // A global constant counts as local memory for our purposes.
  638. if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
  639. // Note: this doesn't require GV to be "ODR" because it isn't legal for a
  640. // global to be marked constant in some modules and non-constant in
  641. // others. GV may even be a declaration, not a definition.
  642. if (!GV->isConstant()) {
  643. Visited.clear();
  644. return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal);
  645. }
  646. continue;
  647. }
  648. // If both select values point to local memory, then so does the select.
  649. if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
  650. Worklist.push_back(SI->getTrueValue());
  651. Worklist.push_back(SI->getFalseValue());
  652. continue;
  653. }
  654. // If all values incoming to a phi node point to local memory, then so does
  655. // the phi.
  656. if (const PHINode *PN = dyn_cast<PHINode>(V)) {
  657. // Don't bother inspecting phi nodes with many operands.
  658. if (PN->getNumIncomingValues() > MaxLookup) {
  659. Visited.clear();
  660. return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal);
  661. }
  662. append_range(Worklist, PN->incoming_values());
  663. continue;
  664. }
  665. // Otherwise be conservative.
  666. Visited.clear();
  667. return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal);
  668. } while (!Worklist.empty() && --MaxLookup);
  669. Visited.clear();
  670. return Worklist.empty();
  671. }
  672. static bool isIntrinsicCall(const CallBase *Call, Intrinsic::ID IID) {
  673. const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Call);
  674. return II && II->getIntrinsicID() == IID;
  675. }
  676. /// Returns the behavior when calling the given call site.
  677. FunctionModRefBehavior BasicAAResult::getModRefBehavior(const CallBase *Call) {
  678. if (Call->doesNotAccessMemory())
  679. // Can't do better than this.
  680. return FMRB_DoesNotAccessMemory;
  681. FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior;
  682. // If the callsite knows it only reads memory, don't return worse
  683. // than that.
  684. if (Call->onlyReadsMemory())
  685. Min = FMRB_OnlyReadsMemory;
  686. else if (Call->onlyWritesMemory())
  687. Min = FMRB_OnlyWritesMemory;
  688. if (Call->onlyAccessesArgMemory())
  689. Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesArgumentPointees);
  690. else if (Call->onlyAccessesInaccessibleMemory())
  691. Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesInaccessibleMem);
  692. else if (Call->onlyAccessesInaccessibleMemOrArgMem())
  693. Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesInaccessibleOrArgMem);
  694. // If the call has operand bundles then aliasing attributes from the function
  695. // it calls do not directly apply to the call. This can be made more precise
  696. // in the future.
  697. if (!Call->hasOperandBundles())
  698. if (const Function *F = Call->getCalledFunction())
  699. Min =
  700. FunctionModRefBehavior(Min & getBestAAResults().getModRefBehavior(F));
  701. return Min;
  702. }
  703. /// Returns the behavior when calling the given function. For use when the call
  704. /// site is not known.
  705. FunctionModRefBehavior BasicAAResult::getModRefBehavior(const Function *F) {
  706. // If the function declares it doesn't access memory, we can't do better.
  707. if (F->doesNotAccessMemory())
  708. return FMRB_DoesNotAccessMemory;
  709. FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior;
  710. // If the function declares it only reads memory, go with that.
  711. if (F->onlyReadsMemory())
  712. Min = FMRB_OnlyReadsMemory;
  713. else if (F->onlyWritesMemory())
  714. Min = FMRB_OnlyWritesMemory;
  715. if (F->onlyAccessesArgMemory())
  716. Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesArgumentPointees);
  717. else if (F->onlyAccessesInaccessibleMemory())
  718. Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesInaccessibleMem);
  719. else if (F->onlyAccessesInaccessibleMemOrArgMem())
  720. Min = FunctionModRefBehavior(Min & FMRB_OnlyAccessesInaccessibleOrArgMem);
  721. return Min;
  722. }
  723. /// Returns true if this is a writeonly (i.e Mod only) parameter.
  724. static bool isWriteOnlyParam(const CallBase *Call, unsigned ArgIdx,
  725. const TargetLibraryInfo &TLI) {
  726. if (Call->paramHasAttr(ArgIdx, Attribute::WriteOnly))
  727. return true;
  728. // We can bound the aliasing properties of memset_pattern16 just as we can
  729. // for memcpy/memset. This is particularly important because the
  730. // LoopIdiomRecognizer likes to turn loops into calls to memset_pattern16
  731. // whenever possible.
  732. // FIXME Consider handling this in InferFunctionAttr.cpp together with other
  733. // attributes.
  734. LibFunc F;
  735. if (Call->getCalledFunction() &&
  736. TLI.getLibFunc(*Call->getCalledFunction(), F) &&
  737. F == LibFunc_memset_pattern16 && TLI.has(F))
  738. if (ArgIdx == 0)
  739. return true;
  740. // TODO: memset_pattern4, memset_pattern8
  741. // TODO: _chk variants
  742. // TODO: strcmp, strcpy
  743. return false;
  744. }
  745. ModRefInfo BasicAAResult::getArgModRefInfo(const CallBase *Call,
  746. unsigned ArgIdx) {
  747. // Checking for known builtin intrinsics and target library functions.
  748. if (isWriteOnlyParam(Call, ArgIdx, TLI))
  749. return ModRefInfo::Mod;
  750. if (Call->paramHasAttr(ArgIdx, Attribute::ReadOnly))
  751. return ModRefInfo::Ref;
  752. if (Call->paramHasAttr(ArgIdx, Attribute::ReadNone))
  753. return ModRefInfo::NoModRef;
  754. return AAResultBase::getArgModRefInfo(Call, ArgIdx);
  755. }
  756. #ifndef NDEBUG
  757. static const Function *getParent(const Value *V) {
  758. if (const Instruction *inst = dyn_cast<Instruction>(V)) {
  759. if (!inst->getParent())
  760. return nullptr;
  761. return inst->getParent()->getParent();
  762. }
  763. if (const Argument *arg = dyn_cast<Argument>(V))
  764. return arg->getParent();
  765. return nullptr;
  766. }
  767. static bool notDifferentParent(const Value *O1, const Value *O2) {
  768. const Function *F1 = getParent(O1);
  769. const Function *F2 = getParent(O2);
  770. return !F1 || !F2 || F1 == F2;
  771. }
  772. #endif
  773. AliasResult BasicAAResult::alias(const MemoryLocation &LocA,
  774. const MemoryLocation &LocB,
  775. AAQueryInfo &AAQI) {
  776. assert(notDifferentParent(LocA.Ptr, LocB.Ptr) &&
  777. "BasicAliasAnalysis doesn't support interprocedural queries.");
  778. return aliasCheck(LocA.Ptr, LocA.Size, LocB.Ptr, LocB.Size, AAQI);
  779. }
  780. /// Checks to see if the specified callsite can clobber the specified memory
  781. /// object.
  782. ///
  783. /// Since we only look at local properties of this function, we really can't
  784. /// say much about this query. We do, however, use simple "address taken"
  785. /// analysis on local objects.
  786. ModRefInfo BasicAAResult::getModRefInfo(const CallBase *Call,
  787. const MemoryLocation &Loc,
  788. AAQueryInfo &AAQI) {
  789. assert(notDifferentParent(Call, Loc.Ptr) &&
  790. "AliasAnalysis query involving multiple functions!");
  791. const Value *Object = getUnderlyingObject(Loc.Ptr);
  792. // Calls marked 'tail' cannot read or write allocas from the current frame
  793. // because the current frame might be destroyed by the time they run. However,
  794. // a tail call may use an alloca with byval. Calling with byval copies the
  795. // contents of the alloca into argument registers or stack slots, so there is
  796. // no lifetime issue.
  797. if (isa<AllocaInst>(Object))
  798. if (const CallInst *CI = dyn_cast<CallInst>(Call))
  799. if (CI->isTailCall() &&
  800. !CI->getAttributes().hasAttrSomewhere(Attribute::ByVal))
  801. return ModRefInfo::NoModRef;
  802. // Stack restore is able to modify unescaped dynamic allocas. Assume it may
  803. // modify them even though the alloca is not escaped.
  804. if (auto *AI = dyn_cast<AllocaInst>(Object))
  805. if (!AI->isStaticAlloca() && isIntrinsicCall(Call, Intrinsic::stackrestore))
  806. return ModRefInfo::Mod;
  807. // If the pointer is to a locally allocated object that does not escape,
  808. // then the call can not mod/ref the pointer unless the call takes the pointer
  809. // as an argument, and itself doesn't capture it.
  810. if (!isa<Constant>(Object) && Call != Object &&
  811. AAQI.CI->isNotCapturedBeforeOrAt(Object, Call)) {
  812. // Optimistically assume that call doesn't touch Object and check this
  813. // assumption in the following loop.
  814. ModRefInfo Result = ModRefInfo::NoModRef;
  815. bool IsMustAlias = true;
  816. unsigned OperandNo = 0;
  817. for (auto CI = Call->data_operands_begin(), CE = Call->data_operands_end();
  818. CI != CE; ++CI, ++OperandNo) {
  819. // Only look at the no-capture or byval pointer arguments. If this
  820. // pointer were passed to arguments that were neither of these, then it
  821. // couldn't be no-capture.
  822. if (!(*CI)->getType()->isPointerTy() ||
  823. (!Call->doesNotCapture(OperandNo) && OperandNo < Call->arg_size() &&
  824. !Call->isByValArgument(OperandNo)))
  825. continue;
  826. // Call doesn't access memory through this operand, so we don't care
  827. // if it aliases with Object.
  828. if (Call->doesNotAccessMemory(OperandNo))
  829. continue;
  830. // If this is a no-capture pointer argument, see if we can tell that it
  831. // is impossible to alias the pointer we're checking.
  832. AliasResult AR = getBestAAResults().alias(
  833. MemoryLocation::getBeforeOrAfter(*CI),
  834. MemoryLocation::getBeforeOrAfter(Object), AAQI);
  835. if (AR != AliasResult::MustAlias)
  836. IsMustAlias = false;
  837. // Operand doesn't alias 'Object', continue looking for other aliases
  838. if (AR == AliasResult::NoAlias)
  839. continue;
  840. // Operand aliases 'Object', but call doesn't modify it. Strengthen
  841. // initial assumption and keep looking in case if there are more aliases.
  842. if (Call->onlyReadsMemory(OperandNo)) {
  843. Result = setRef(Result);
  844. continue;
  845. }
  846. // Operand aliases 'Object' but call only writes into it.
  847. if (Call->onlyWritesMemory(OperandNo)) {
  848. Result = setMod(Result);
  849. continue;
  850. }
  851. // This operand aliases 'Object' and call reads and writes into it.
  852. // Setting ModRef will not yield an early return below, MustAlias is not
  853. // used further.
  854. Result = ModRefInfo::ModRef;
  855. break;
  856. }
  857. // No operand aliases, reset Must bit. Add below if at least one aliases
  858. // and all aliases found are MustAlias.
  859. if (isNoModRef(Result))
  860. IsMustAlias = false;
  861. // Early return if we improved mod ref information
  862. if (!isModAndRefSet(Result)) {
  863. if (isNoModRef(Result))
  864. return ModRefInfo::NoModRef;
  865. return IsMustAlias ? setMust(Result) : clearMust(Result);
  866. }
  867. }
  868. // If the call is malloc/calloc like, we can assume that it doesn't
  869. // modify any IR visible value. This is only valid because we assume these
  870. // routines do not read values visible in the IR. TODO: Consider special
  871. // casing realloc and strdup routines which access only their arguments as
  872. // well. Or alternatively, replace all of this with inaccessiblememonly once
  873. // that's implemented fully.
  874. if (isMallocOrCallocLikeFn(Call, &TLI)) {
  875. // Be conservative if the accessed pointer may alias the allocation -
  876. // fallback to the generic handling below.
  877. if (getBestAAResults().alias(MemoryLocation::getBeforeOrAfter(Call), Loc,
  878. AAQI) == AliasResult::NoAlias)
  879. return ModRefInfo::NoModRef;
  880. }
  881. // Ideally, there should be no need to special case for memcpy/memove
  882. // intrinsics here since general machinery (based on memory attributes) should
  883. // already handle it just fine. Unfortunately, it doesn't due to deficiency in
  884. // operand bundles support. At the moment it's not clear if complexity behind
  885. // enhancing general mechanism worths it.
  886. // TODO: Consider improving operand bundles support in general mechanism.
  887. if (auto *Inst = dyn_cast<AnyMemTransferInst>(Call)) {
  888. AliasResult SrcAA =
  889. getBestAAResults().alias(MemoryLocation::getForSource(Inst), Loc, AAQI);
  890. AliasResult DestAA =
  891. getBestAAResults().alias(MemoryLocation::getForDest(Inst), Loc, AAQI);
  892. // It's also possible for Loc to alias both src and dest, or neither.
  893. ModRefInfo rv = ModRefInfo::NoModRef;
  894. if (SrcAA != AliasResult::NoAlias || Call->hasReadingOperandBundles())
  895. rv = setRef(rv);
  896. if (DestAA != AliasResult::NoAlias || Call->hasClobberingOperandBundles())
  897. rv = setMod(rv);
  898. return rv;
  899. }
  900. // Guard intrinsics are marked as arbitrarily writing so that proper control
  901. // dependencies are maintained but they never mods any particular memory
  902. // location.
  903. //
  904. // *Unlike* assumes, guard intrinsics are modeled as reading memory since the
  905. // heap state at the point the guard is issued needs to be consistent in case
  906. // the guard invokes the "deopt" continuation.
  907. if (isIntrinsicCall(Call, Intrinsic::experimental_guard))
  908. return ModRefInfo::Ref;
  909. // The same applies to deoptimize which is essentially a guard(false).
  910. if (isIntrinsicCall(Call, Intrinsic::experimental_deoptimize))
  911. return ModRefInfo::Ref;
  912. // Like assumes, invariant.start intrinsics were also marked as arbitrarily
  913. // writing so that proper control dependencies are maintained but they never
  914. // mod any particular memory location visible to the IR.
  915. // *Unlike* assumes (which are now modeled as NoModRef), invariant.start
  916. // intrinsic is now modeled as reading memory. This prevents hoisting the
  917. // invariant.start intrinsic over stores. Consider:
  918. // *ptr = 40;
  919. // *ptr = 50;
  920. // invariant_start(ptr)
  921. // int val = *ptr;
  922. // print(val);
  923. //
  924. // This cannot be transformed to:
  925. //
  926. // *ptr = 40;
  927. // invariant_start(ptr)
  928. // *ptr = 50;
  929. // int val = *ptr;
  930. // print(val);
  931. //
  932. // The transformation will cause the second store to be ignored (based on
  933. // rules of invariant.start) and print 40, while the first program always
  934. // prints 50.
  935. if (isIntrinsicCall(Call, Intrinsic::invariant_start))
  936. return ModRefInfo::Ref;
  937. // The AAResultBase base class has some smarts, lets use them.
  938. return AAResultBase::getModRefInfo(Call, Loc, AAQI);
  939. }
  940. ModRefInfo BasicAAResult::getModRefInfo(const CallBase *Call1,
  941. const CallBase *Call2,
  942. AAQueryInfo &AAQI) {
  943. // Guard intrinsics are marked as arbitrarily writing so that proper control
  944. // dependencies are maintained but they never mods any particular memory
  945. // location.
  946. //
  947. // *Unlike* assumes, guard intrinsics are modeled as reading memory since the
  948. // heap state at the point the guard is issued needs to be consistent in case
  949. // the guard invokes the "deopt" continuation.
  950. // NB! This function is *not* commutative, so we special case two
  951. // possibilities for guard intrinsics.
  952. if (isIntrinsicCall(Call1, Intrinsic::experimental_guard))
  953. return isModSet(createModRefInfo(getModRefBehavior(Call2)))
  954. ? ModRefInfo::Ref
  955. : ModRefInfo::NoModRef;
  956. if (isIntrinsicCall(Call2, Intrinsic::experimental_guard))
  957. return isModSet(createModRefInfo(getModRefBehavior(Call1)))
  958. ? ModRefInfo::Mod
  959. : ModRefInfo::NoModRef;
  960. // The AAResultBase base class has some smarts, lets use them.
  961. return AAResultBase::getModRefInfo(Call1, Call2, AAQI);
  962. }
  963. /// Return true if we know V to the base address of the corresponding memory
  964. /// object. This implies that any address less than V must be out of bounds
  965. /// for the underlying object. Note that just being isIdentifiedObject() is
  966. /// not enough - For example, a negative offset from a noalias argument or call
  967. /// can be inbounds w.r.t the actual underlying object.
  968. static bool isBaseOfObject(const Value *V) {
  969. // TODO: We can handle other cases here
  970. // 1) For GC languages, arguments to functions are often required to be
  971. // base pointers.
  972. // 2) Result of allocation routines are often base pointers. Leverage TLI.
  973. return (isa<AllocaInst>(V) || isa<GlobalVariable>(V));
  974. }
  975. /// Provides a bunch of ad-hoc rules to disambiguate a GEP instruction against
  976. /// another pointer.
  977. ///
  978. /// We know that V1 is a GEP, but we don't know anything about V2.
  979. /// UnderlyingV1 is getUnderlyingObject(GEP1), UnderlyingV2 is the same for
  980. /// V2.
  981. AliasResult BasicAAResult::aliasGEP(
  982. const GEPOperator *GEP1, LocationSize V1Size,
  983. const Value *V2, LocationSize V2Size,
  984. const Value *UnderlyingV1, const Value *UnderlyingV2, AAQueryInfo &AAQI) {
  985. if (!V1Size.hasValue() && !V2Size.hasValue()) {
  986. // TODO: This limitation exists for compile-time reasons. Relax it if we
  987. // can avoid exponential pathological cases.
  988. if (!isa<GEPOperator>(V2))
  989. return AliasResult::MayAlias;
  990. // If both accesses have unknown size, we can only check whether the base
  991. // objects don't alias.
  992. AliasResult BaseAlias = getBestAAResults().alias(
  993. MemoryLocation::getBeforeOrAfter(UnderlyingV1),
  994. MemoryLocation::getBeforeOrAfter(UnderlyingV2), AAQI);
  995. return BaseAlias == AliasResult::NoAlias ? AliasResult::NoAlias
  996. : AliasResult::MayAlias;
  997. }
  998. DecomposedGEP DecompGEP1 = DecomposeGEPExpression(GEP1, DL, &AC, DT);
  999. DecomposedGEP DecompGEP2 = DecomposeGEPExpression(V2, DL, &AC, DT);
  1000. // Bail if we were not able to decompose anything.
  1001. if (DecompGEP1.Base == GEP1 && DecompGEP2.Base == V2)
  1002. return AliasResult::MayAlias;
  1003. // Subtract the GEP2 pointer from the GEP1 pointer to find out their
  1004. // symbolic difference.
  1005. subtractDecomposedGEPs(DecompGEP1, DecompGEP2);
  1006. // If an inbounds GEP would have to start from an out of bounds address
  1007. // for the two to alias, then we can assume noalias.
  1008. if (*DecompGEP1.InBounds && DecompGEP1.VarIndices.empty() &&
  1009. V2Size.hasValue() && DecompGEP1.Offset.sge(V2Size.getValue()) &&
  1010. isBaseOfObject(DecompGEP2.Base))
  1011. return AliasResult::NoAlias;
  1012. if (isa<GEPOperator>(V2)) {
  1013. // Symmetric case to above.
  1014. if (*DecompGEP2.InBounds && DecompGEP1.VarIndices.empty() &&
  1015. V1Size.hasValue() && DecompGEP1.Offset.sle(-V1Size.getValue()) &&
  1016. isBaseOfObject(DecompGEP1.Base))
  1017. return AliasResult::NoAlias;
  1018. }
  1019. // For GEPs with identical offsets, we can preserve the size and AAInfo
  1020. // when performing the alias check on the underlying objects.
  1021. if (DecompGEP1.Offset == 0 && DecompGEP1.VarIndices.empty())
  1022. return getBestAAResults().alias(MemoryLocation(DecompGEP1.Base, V1Size),
  1023. MemoryLocation(DecompGEP2.Base, V2Size),
  1024. AAQI);
  1025. // Do the base pointers alias?
  1026. AliasResult BaseAlias = getBestAAResults().alias(
  1027. MemoryLocation::getBeforeOrAfter(DecompGEP1.Base),
  1028. MemoryLocation::getBeforeOrAfter(DecompGEP2.Base), AAQI);
  1029. // If we get a No or May, then return it immediately, no amount of analysis
  1030. // will improve this situation.
  1031. if (BaseAlias != AliasResult::MustAlias) {
  1032. assert(BaseAlias == AliasResult::NoAlias ||
  1033. BaseAlias == AliasResult::MayAlias);
  1034. return BaseAlias;
  1035. }
  1036. // If there is a constant difference between the pointers, but the difference
  1037. // is less than the size of the associated memory object, then we know
  1038. // that the objects are partially overlapping. If the difference is
  1039. // greater, we know they do not overlap.
  1040. if (DecompGEP1.VarIndices.empty()) {
  1041. APInt &Off = DecompGEP1.Offset;
  1042. // Initialize for Off >= 0 (V2 <= GEP1) case.
  1043. const Value *LeftPtr = V2;
  1044. const Value *RightPtr = GEP1;
  1045. LocationSize VLeftSize = V2Size;
  1046. LocationSize VRightSize = V1Size;
  1047. const bool Swapped = Off.isNegative();
  1048. if (Swapped) {
  1049. // Swap if we have the situation where:
  1050. // + +
  1051. // | BaseOffset |
  1052. // ---------------->|
  1053. // |-->V1Size |-------> V2Size
  1054. // GEP1 V2
  1055. std::swap(LeftPtr, RightPtr);
  1056. std::swap(VLeftSize, VRightSize);
  1057. Off = -Off;
  1058. }
  1059. if (!VLeftSize.hasValue())
  1060. return AliasResult::MayAlias;
  1061. const uint64_t LSize = VLeftSize.getValue();
  1062. if (Off.ult(LSize)) {
  1063. // Conservatively drop processing if a phi was visited and/or offset is
  1064. // too big.
  1065. AliasResult AR = AliasResult::PartialAlias;
  1066. if (VRightSize.hasValue() && Off.ule(INT32_MAX) &&
  1067. (Off + VRightSize.getValue()).ule(LSize)) {
  1068. // Memory referenced by right pointer is nested. Save the offset in
  1069. // cache. Note that originally offset estimated as GEP1-V2, but
  1070. // AliasResult contains the shift that represents GEP1+Offset=V2.
  1071. AR.setOffset(-Off.getSExtValue());
  1072. AR.swap(Swapped);
  1073. }
  1074. return AR;
  1075. }
  1076. return AliasResult::NoAlias;
  1077. }
  1078. // We need to know both acess sizes for all the following heuristics.
  1079. if (!V1Size.hasValue() || !V2Size.hasValue())
  1080. return AliasResult::MayAlias;
  1081. APInt GCD;
  1082. ConstantRange OffsetRange = ConstantRange(DecompGEP1.Offset);
  1083. for (unsigned i = 0, e = DecompGEP1.VarIndices.size(); i != e; ++i) {
  1084. const VariableGEPIndex &Index = DecompGEP1.VarIndices[i];
  1085. const APInt &Scale = Index.Scale;
  1086. APInt ScaleForGCD = Scale;
  1087. if (!Index.IsNSW)
  1088. ScaleForGCD = APInt::getOneBitSet(Scale.getBitWidth(),
  1089. Scale.countTrailingZeros());
  1090. if (i == 0)
  1091. GCD = ScaleForGCD.abs();
  1092. else
  1093. GCD = APIntOps::GreatestCommonDivisor(GCD, ScaleForGCD.abs());
  1094. ConstantRange CR = computeConstantRange(Index.Val.V, /* ForSigned */ false,
  1095. true, &AC, Index.CxtI);
  1096. KnownBits Known =
  1097. computeKnownBits(Index.Val.V, DL, 0, &AC, Index.CxtI, DT);
  1098. CR = CR.intersectWith(
  1099. ConstantRange::fromKnownBits(Known, /* Signed */ true),
  1100. ConstantRange::Signed);
  1101. CR = Index.Val.evaluateWith(CR).sextOrTrunc(OffsetRange.getBitWidth());
  1102. assert(OffsetRange.getBitWidth() == Scale.getBitWidth() &&
  1103. "Bit widths are normalized to MaxIndexSize");
  1104. if (Index.IsNSW)
  1105. OffsetRange = OffsetRange.add(CR.smul_sat(ConstantRange(Scale)));
  1106. else
  1107. OffsetRange = OffsetRange.add(CR.smul_fast(ConstantRange(Scale)));
  1108. }
  1109. // We now have accesses at two offsets from the same base:
  1110. // 1. (...)*GCD + DecompGEP1.Offset with size V1Size
  1111. // 2. 0 with size V2Size
  1112. // Using arithmetic modulo GCD, the accesses are at
  1113. // [ModOffset..ModOffset+V1Size) and [0..V2Size). If the first access fits
  1114. // into the range [V2Size..GCD), then we know they cannot overlap.
  1115. APInt ModOffset = DecompGEP1.Offset.srem(GCD);
  1116. if (ModOffset.isNegative())
  1117. ModOffset += GCD; // We want mod, not rem.
  1118. if (ModOffset.uge(V2Size.getValue()) &&
  1119. (GCD - ModOffset).uge(V1Size.getValue()))
  1120. return AliasResult::NoAlias;
  1121. // Compute ranges of potentially accessed bytes for both accesses. If the
  1122. // interseciton is empty, there can be no overlap.
  1123. unsigned BW = OffsetRange.getBitWidth();
  1124. ConstantRange Range1 = OffsetRange.add(
  1125. ConstantRange(APInt(BW, 0), APInt(BW, V1Size.getValue())));
  1126. ConstantRange Range2 =
  1127. ConstantRange(APInt(BW, 0), APInt(BW, V2Size.getValue()));
  1128. if (Range1.intersectWith(Range2).isEmptySet())
  1129. return AliasResult::NoAlias;
  1130. // Try to determine the range of values for VarIndex such that
  1131. // VarIndex <= -MinAbsVarIndex || MinAbsVarIndex <= VarIndex.
  1132. Optional<APInt> MinAbsVarIndex;
  1133. if (DecompGEP1.VarIndices.size() == 1) {
  1134. // VarIndex = Scale*V.
  1135. const VariableGEPIndex &Var = DecompGEP1.VarIndices[0];
  1136. if (Var.Val.TruncBits == 0 &&
  1137. isKnownNonZero(Var.Val.V, DL, 0, &AC, Var.CxtI, DT)) {
  1138. // If V != 0 then abs(VarIndex) >= abs(Scale).
  1139. MinAbsVarIndex = Var.Scale.abs();
  1140. }
  1141. } else if (DecompGEP1.VarIndices.size() == 2) {
  1142. // VarIndex = Scale*V0 + (-Scale)*V1.
  1143. // If V0 != V1 then abs(VarIndex) >= abs(Scale).
  1144. // Check that VisitedPhiBBs is empty, to avoid reasoning about
  1145. // inequality of values across loop iterations.
  1146. const VariableGEPIndex &Var0 = DecompGEP1.VarIndices[0];
  1147. const VariableGEPIndex &Var1 = DecompGEP1.VarIndices[1];
  1148. if (Var0.Scale == -Var1.Scale && Var0.Val.TruncBits == 0 &&
  1149. Var0.Val.hasSameCastsAs(Var1.Val) && VisitedPhiBBs.empty() &&
  1150. isKnownNonEqual(Var0.Val.V, Var1.Val.V, DL, &AC, /* CxtI */ nullptr,
  1151. DT))
  1152. MinAbsVarIndex = Var0.Scale.abs();
  1153. }
  1154. if (MinAbsVarIndex) {
  1155. // The constant offset will have added at least +/-MinAbsVarIndex to it.
  1156. APInt OffsetLo = DecompGEP1.Offset - *MinAbsVarIndex;
  1157. APInt OffsetHi = DecompGEP1.Offset + *MinAbsVarIndex;
  1158. // We know that Offset <= OffsetLo || Offset >= OffsetHi
  1159. if (OffsetLo.isNegative() && (-OffsetLo).uge(V1Size.getValue()) &&
  1160. OffsetHi.isNonNegative() && OffsetHi.uge(V2Size.getValue()))
  1161. return AliasResult::NoAlias;
  1162. }
  1163. if (constantOffsetHeuristic(DecompGEP1, V1Size, V2Size, &AC, DT))
  1164. return AliasResult::NoAlias;
  1165. // Statically, we can see that the base objects are the same, but the
  1166. // pointers have dynamic offsets which we can't resolve. And none of our
  1167. // little tricks above worked.
  1168. return AliasResult::MayAlias;
  1169. }
  1170. static AliasResult MergeAliasResults(AliasResult A, AliasResult B) {
  1171. // If the results agree, take it.
  1172. if (A == B)
  1173. return A;
  1174. // A mix of PartialAlias and MustAlias is PartialAlias.
  1175. if ((A == AliasResult::PartialAlias && B == AliasResult::MustAlias) ||
  1176. (B == AliasResult::PartialAlias && A == AliasResult::MustAlias))
  1177. return AliasResult::PartialAlias;
  1178. // Otherwise, we don't know anything.
  1179. return AliasResult::MayAlias;
  1180. }
  1181. /// Provides a bunch of ad-hoc rules to disambiguate a Select instruction
  1182. /// against another.
  1183. AliasResult
  1184. BasicAAResult::aliasSelect(const SelectInst *SI, LocationSize SISize,
  1185. const Value *V2, LocationSize V2Size,
  1186. AAQueryInfo &AAQI) {
  1187. // If the values are Selects with the same condition, we can do a more precise
  1188. // check: just check for aliases between the values on corresponding arms.
  1189. if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2))
  1190. if (SI->getCondition() == SI2->getCondition()) {
  1191. AliasResult Alias = getBestAAResults().alias(
  1192. MemoryLocation(SI->getTrueValue(), SISize),
  1193. MemoryLocation(SI2->getTrueValue(), V2Size), AAQI);
  1194. if (Alias == AliasResult::MayAlias)
  1195. return AliasResult::MayAlias;
  1196. AliasResult ThisAlias = getBestAAResults().alias(
  1197. MemoryLocation(SI->getFalseValue(), SISize),
  1198. MemoryLocation(SI2->getFalseValue(), V2Size), AAQI);
  1199. return MergeAliasResults(ThisAlias, Alias);
  1200. }
  1201. // If both arms of the Select node NoAlias or MustAlias V2, then returns
  1202. // NoAlias / MustAlias. Otherwise, returns MayAlias.
  1203. AliasResult Alias = getBestAAResults().alias(
  1204. MemoryLocation(V2, V2Size),
  1205. MemoryLocation(SI->getTrueValue(), SISize), AAQI);
  1206. if (Alias == AliasResult::MayAlias)
  1207. return AliasResult::MayAlias;
  1208. AliasResult ThisAlias = getBestAAResults().alias(
  1209. MemoryLocation(V2, V2Size),
  1210. MemoryLocation(SI->getFalseValue(), SISize), AAQI);
  1211. return MergeAliasResults(ThisAlias, Alias);
  1212. }
  1213. /// Provide a bunch of ad-hoc rules to disambiguate a PHI instruction against
  1214. /// another.
  1215. AliasResult BasicAAResult::aliasPHI(const PHINode *PN, LocationSize PNSize,
  1216. const Value *V2, LocationSize V2Size,
  1217. AAQueryInfo &AAQI) {
  1218. if (!PN->getNumIncomingValues())
  1219. return AliasResult::NoAlias;
  1220. // If the values are PHIs in the same block, we can do a more precise
  1221. // as well as efficient check: just check for aliases between the values
  1222. // on corresponding edges.
  1223. if (const PHINode *PN2 = dyn_cast<PHINode>(V2))
  1224. if (PN2->getParent() == PN->getParent()) {
  1225. Optional<AliasResult> Alias;
  1226. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
  1227. AliasResult ThisAlias = getBestAAResults().alias(
  1228. MemoryLocation(PN->getIncomingValue(i), PNSize),
  1229. MemoryLocation(
  1230. PN2->getIncomingValueForBlock(PN->getIncomingBlock(i)), V2Size),
  1231. AAQI);
  1232. if (Alias)
  1233. *Alias = MergeAliasResults(*Alias, ThisAlias);
  1234. else
  1235. Alias = ThisAlias;
  1236. if (*Alias == AliasResult::MayAlias)
  1237. break;
  1238. }
  1239. return *Alias;
  1240. }
  1241. SmallVector<Value *, 4> V1Srcs;
  1242. // If a phi operand recurses back to the phi, we can still determine NoAlias
  1243. // if we don't alias the underlying objects of the other phi operands, as we
  1244. // know that the recursive phi needs to be based on them in some way.
  1245. bool isRecursive = false;
  1246. auto CheckForRecPhi = [&](Value *PV) {
  1247. if (!EnableRecPhiAnalysis)
  1248. return false;
  1249. if (getUnderlyingObject(PV) == PN) {
  1250. isRecursive = true;
  1251. return true;
  1252. }
  1253. return false;
  1254. };
  1255. if (PV) {
  1256. // If we have PhiValues then use it to get the underlying phi values.
  1257. const PhiValues::ValueSet &PhiValueSet = PV->getValuesForPhi(PN);
  1258. // If we have more phi values than the search depth then return MayAlias
  1259. // conservatively to avoid compile time explosion. The worst possible case
  1260. // is if both sides are PHI nodes. In which case, this is O(m x n) time
  1261. // where 'm' and 'n' are the number of PHI sources.
  1262. if (PhiValueSet.size() > MaxLookupSearchDepth)
  1263. return AliasResult::MayAlias;
  1264. // Add the values to V1Srcs
  1265. for (Value *PV1 : PhiValueSet) {
  1266. if (CheckForRecPhi(PV1))
  1267. continue;
  1268. V1Srcs.push_back(PV1);
  1269. }
  1270. } else {
  1271. // If we don't have PhiInfo then just look at the operands of the phi itself
  1272. // FIXME: Remove this once we can guarantee that we have PhiInfo always
  1273. SmallPtrSet<Value *, 4> UniqueSrc;
  1274. Value *OnePhi = nullptr;
  1275. for (Value *PV1 : PN->incoming_values()) {
  1276. if (isa<PHINode>(PV1)) {
  1277. if (OnePhi && OnePhi != PV1) {
  1278. // To control potential compile time explosion, we choose to be
  1279. // conserviate when we have more than one Phi input. It is important
  1280. // that we handle the single phi case as that lets us handle LCSSA
  1281. // phi nodes and (combined with the recursive phi handling) simple
  1282. // pointer induction variable patterns.
  1283. return AliasResult::MayAlias;
  1284. }
  1285. OnePhi = PV1;
  1286. }
  1287. if (CheckForRecPhi(PV1))
  1288. continue;
  1289. if (UniqueSrc.insert(PV1).second)
  1290. V1Srcs.push_back(PV1);
  1291. }
  1292. if (OnePhi && UniqueSrc.size() > 1)
  1293. // Out of an abundance of caution, allow only the trivial lcssa and
  1294. // recursive phi cases.
  1295. return AliasResult::MayAlias;
  1296. }
  1297. // If V1Srcs is empty then that means that the phi has no underlying non-phi
  1298. // value. This should only be possible in blocks unreachable from the entry
  1299. // block, but return MayAlias just in case.
  1300. if (V1Srcs.empty())
  1301. return AliasResult::MayAlias;
  1302. // If this PHI node is recursive, indicate that the pointer may be moved
  1303. // across iterations. We can only prove NoAlias if different underlying
  1304. // objects are involved.
  1305. if (isRecursive)
  1306. PNSize = LocationSize::beforeOrAfterPointer();
  1307. // In the recursive alias queries below, we may compare values from two
  1308. // different loop iterations. Keep track of visited phi blocks, which will
  1309. // be used when determining value equivalence.
  1310. bool BlockInserted = VisitedPhiBBs.insert(PN->getParent()).second;
  1311. auto _ = make_scope_exit([&]() {
  1312. if (BlockInserted)
  1313. VisitedPhiBBs.erase(PN->getParent());
  1314. });
  1315. // If we inserted a block into VisitedPhiBBs, alias analysis results that
  1316. // have been cached earlier may no longer be valid. Perform recursive queries
  1317. // with a new AAQueryInfo.
  1318. AAQueryInfo NewAAQI = AAQI.withEmptyCache();
  1319. AAQueryInfo *UseAAQI = BlockInserted ? &NewAAQI : &AAQI;
  1320. AliasResult Alias = getBestAAResults().alias(
  1321. MemoryLocation(V2, V2Size),
  1322. MemoryLocation(V1Srcs[0], PNSize), *UseAAQI);
  1323. // Early exit if the check of the first PHI source against V2 is MayAlias.
  1324. // Other results are not possible.
  1325. if (Alias == AliasResult::MayAlias)
  1326. return AliasResult::MayAlias;
  1327. // With recursive phis we cannot guarantee that MustAlias/PartialAlias will
  1328. // remain valid to all elements and needs to conservatively return MayAlias.
  1329. if (isRecursive && Alias != AliasResult::NoAlias)
  1330. return AliasResult::MayAlias;
  1331. // If all sources of the PHI node NoAlias or MustAlias V2, then returns
  1332. // NoAlias / MustAlias. Otherwise, returns MayAlias.
  1333. for (unsigned i = 1, e = V1Srcs.size(); i != e; ++i) {
  1334. Value *V = V1Srcs[i];
  1335. AliasResult ThisAlias = getBestAAResults().alias(
  1336. MemoryLocation(V2, V2Size), MemoryLocation(V, PNSize), *UseAAQI);
  1337. Alias = MergeAliasResults(ThisAlias, Alias);
  1338. if (Alias == AliasResult::MayAlias)
  1339. break;
  1340. }
  1341. return Alias;
  1342. }
  1343. /// Provides a bunch of ad-hoc rules to disambiguate in common cases, such as
  1344. /// array references.
  1345. AliasResult BasicAAResult::aliasCheck(const Value *V1, LocationSize V1Size,
  1346. const Value *V2, LocationSize V2Size,
  1347. AAQueryInfo &AAQI) {
  1348. // If either of the memory references is empty, it doesn't matter what the
  1349. // pointer values are.
  1350. if (V1Size.isZero() || V2Size.isZero())
  1351. return AliasResult::NoAlias;
  1352. // Strip off any casts if they exist.
  1353. V1 = V1->stripPointerCastsForAliasAnalysis();
  1354. V2 = V2->stripPointerCastsForAliasAnalysis();
  1355. // If V1 or V2 is undef, the result is NoAlias because we can always pick a
  1356. // value for undef that aliases nothing in the program.
  1357. if (isa<UndefValue>(V1) || isa<UndefValue>(V2))
  1358. return AliasResult::NoAlias;
  1359. // Are we checking for alias of the same value?
  1360. // Because we look 'through' phi nodes, we could look at "Value" pointers from
  1361. // different iterations. We must therefore make sure that this is not the
  1362. // case. The function isValueEqualInPotentialCycles ensures that this cannot
  1363. // happen by looking at the visited phi nodes and making sure they cannot
  1364. // reach the value.
  1365. if (isValueEqualInPotentialCycles(V1, V2))
  1366. return AliasResult::MustAlias;
  1367. if (!V1->getType()->isPointerTy() || !V2->getType()->isPointerTy())
  1368. return AliasResult::NoAlias; // Scalars cannot alias each other
  1369. // Figure out what objects these things are pointing to if we can.
  1370. const Value *O1 = getUnderlyingObject(V1, MaxLookupSearchDepth);
  1371. const Value *O2 = getUnderlyingObject(V2, MaxLookupSearchDepth);
  1372. // Null values in the default address space don't point to any object, so they
  1373. // don't alias any other pointer.
  1374. if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O1))
  1375. if (!NullPointerIsDefined(&F, CPN->getType()->getAddressSpace()))
  1376. return AliasResult::NoAlias;
  1377. if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O2))
  1378. if (!NullPointerIsDefined(&F, CPN->getType()->getAddressSpace()))
  1379. return AliasResult::NoAlias;
  1380. if (O1 != O2) {
  1381. // If V1/V2 point to two different objects, we know that we have no alias.
  1382. if (isIdentifiedObject(O1) && isIdentifiedObject(O2))
  1383. return AliasResult::NoAlias;
  1384. // Constant pointers can't alias with non-const isIdentifiedObject objects.
  1385. if ((isa<Constant>(O1) && isIdentifiedObject(O2) && !isa<Constant>(O2)) ||
  1386. (isa<Constant>(O2) && isIdentifiedObject(O1) && !isa<Constant>(O1)))
  1387. return AliasResult::NoAlias;
  1388. // Function arguments can't alias with things that are known to be
  1389. // unambigously identified at the function level.
  1390. if ((isa<Argument>(O1) && isIdentifiedFunctionLocal(O2)) ||
  1391. (isa<Argument>(O2) && isIdentifiedFunctionLocal(O1)))
  1392. return AliasResult::NoAlias;
  1393. // If one pointer is the result of a call/invoke or load and the other is a
  1394. // non-escaping local object within the same function, then we know the
  1395. // object couldn't escape to a point where the call could return it.
  1396. //
  1397. // Note that if the pointers are in different functions, there are a
  1398. // variety of complications. A call with a nocapture argument may still
  1399. // temporary store the nocapture argument's value in a temporary memory
  1400. // location if that memory location doesn't escape. Or it may pass a
  1401. // nocapture value to other functions as long as they don't capture it.
  1402. if (isEscapeSource(O1) &&
  1403. AAQI.CI->isNotCapturedBeforeOrAt(O2, cast<Instruction>(O1)))
  1404. return AliasResult::NoAlias;
  1405. if (isEscapeSource(O2) &&
  1406. AAQI.CI->isNotCapturedBeforeOrAt(O1, cast<Instruction>(O2)))
  1407. return AliasResult::NoAlias;
  1408. }
  1409. // If the size of one access is larger than the entire object on the other
  1410. // side, then we know such behavior is undefined and can assume no alias.
  1411. bool NullIsValidLocation = NullPointerIsDefined(&F);
  1412. if ((isObjectSmallerThan(
  1413. O2, getMinimalExtentFrom(*V1, V1Size, DL, NullIsValidLocation), DL,
  1414. TLI, NullIsValidLocation)) ||
  1415. (isObjectSmallerThan(
  1416. O1, getMinimalExtentFrom(*V2, V2Size, DL, NullIsValidLocation), DL,
  1417. TLI, NullIsValidLocation)))
  1418. return AliasResult::NoAlias;
  1419. // If one the accesses may be before the accessed pointer, canonicalize this
  1420. // by using unknown after-pointer sizes for both accesses. This is
  1421. // equivalent, because regardless of which pointer is lower, one of them
  1422. // will always came after the other, as long as the underlying objects aren't
  1423. // disjoint. We do this so that the rest of BasicAA does not have to deal
  1424. // with accesses before the base pointer, and to improve cache utilization by
  1425. // merging equivalent states.
  1426. if (V1Size.mayBeBeforePointer() || V2Size.mayBeBeforePointer()) {
  1427. V1Size = LocationSize::afterPointer();
  1428. V2Size = LocationSize::afterPointer();
  1429. }
  1430. // FIXME: If this depth limit is hit, then we may cache sub-optimal results
  1431. // for recursive queries. For this reason, this limit is chosen to be large
  1432. // enough to be very rarely hit, while still being small enough to avoid
  1433. // stack overflows.
  1434. if (AAQI.Depth >= 512)
  1435. return AliasResult::MayAlias;
  1436. // Check the cache before climbing up use-def chains. This also terminates
  1437. // otherwise infinitely recursive queries.
  1438. AAQueryInfo::LocPair Locs({V1, V1Size}, {V2, V2Size});
  1439. const bool Swapped = V1 > V2;
  1440. if (Swapped)
  1441. std::swap(Locs.first, Locs.second);
  1442. const auto &Pair = AAQI.AliasCache.try_emplace(
  1443. Locs, AAQueryInfo::CacheEntry{AliasResult::NoAlias, 0});
  1444. if (!Pair.second) {
  1445. auto &Entry = Pair.first->second;
  1446. if (!Entry.isDefinitive()) {
  1447. // Remember that we used an assumption.
  1448. ++Entry.NumAssumptionUses;
  1449. ++AAQI.NumAssumptionUses;
  1450. }
  1451. // Cache contains sorted {V1,V2} pairs but we should return original order.
  1452. auto Result = Entry.Result;
  1453. Result.swap(Swapped);
  1454. return Result;
  1455. }
  1456. int OrigNumAssumptionUses = AAQI.NumAssumptionUses;
  1457. unsigned OrigNumAssumptionBasedResults = AAQI.AssumptionBasedResults.size();
  1458. AliasResult Result =
  1459. aliasCheckRecursive(V1, V1Size, V2, V2Size, AAQI, O1, O2);
  1460. auto It = AAQI.AliasCache.find(Locs);
  1461. assert(It != AAQI.AliasCache.end() && "Must be in cache");
  1462. auto &Entry = It->second;
  1463. // Check whether a NoAlias assumption has been used, but disproven.
  1464. bool AssumptionDisproven =
  1465. Entry.NumAssumptionUses > 0 && Result != AliasResult::NoAlias;
  1466. if (AssumptionDisproven)
  1467. Result = AliasResult::MayAlias;
  1468. // This is a definitive result now, when considered as a root query.
  1469. AAQI.NumAssumptionUses -= Entry.NumAssumptionUses;
  1470. Entry.Result = Result;
  1471. // Cache contains sorted {V1,V2} pairs.
  1472. Entry.Result.swap(Swapped);
  1473. Entry.NumAssumptionUses = -1;
  1474. // If the assumption has been disproven, remove any results that may have
  1475. // been based on this assumption. Do this after the Entry updates above to
  1476. // avoid iterator invalidation.
  1477. if (AssumptionDisproven)
  1478. while (AAQI.AssumptionBasedResults.size() > OrigNumAssumptionBasedResults)
  1479. AAQI.AliasCache.erase(AAQI.AssumptionBasedResults.pop_back_val());
  1480. // The result may still be based on assumptions higher up in the chain.
  1481. // Remember it, so it can be purged from the cache later.
  1482. if (OrigNumAssumptionUses != AAQI.NumAssumptionUses &&
  1483. Result != AliasResult::MayAlias)
  1484. AAQI.AssumptionBasedResults.push_back(Locs);
  1485. return Result;
  1486. }
  1487. AliasResult BasicAAResult::aliasCheckRecursive(
  1488. const Value *V1, LocationSize V1Size,
  1489. const Value *V2, LocationSize V2Size,
  1490. AAQueryInfo &AAQI, const Value *O1, const Value *O2) {
  1491. if (const GEPOperator *GV1 = dyn_cast<GEPOperator>(V1)) {
  1492. AliasResult Result = aliasGEP(GV1, V1Size, V2, V2Size, O1, O2, AAQI);
  1493. if (Result != AliasResult::MayAlias)
  1494. return Result;
  1495. } else if (const GEPOperator *GV2 = dyn_cast<GEPOperator>(V2)) {
  1496. AliasResult Result = aliasGEP(GV2, V2Size, V1, V1Size, O2, O1, AAQI);
  1497. Result.swap();
  1498. if (Result != AliasResult::MayAlias)
  1499. return Result;
  1500. }
  1501. if (const PHINode *PN = dyn_cast<PHINode>(V1)) {
  1502. AliasResult Result = aliasPHI(PN, V1Size, V2, V2Size, AAQI);
  1503. if (Result != AliasResult::MayAlias)
  1504. return Result;
  1505. } else if (const PHINode *PN = dyn_cast<PHINode>(V2)) {
  1506. AliasResult Result = aliasPHI(PN, V2Size, V1, V1Size, AAQI);
  1507. Result.swap();
  1508. if (Result != AliasResult::MayAlias)
  1509. return Result;
  1510. }
  1511. if (const SelectInst *S1 = dyn_cast<SelectInst>(V1)) {
  1512. AliasResult Result = aliasSelect(S1, V1Size, V2, V2Size, AAQI);
  1513. if (Result != AliasResult::MayAlias)
  1514. return Result;
  1515. } else if (const SelectInst *S2 = dyn_cast<SelectInst>(V2)) {
  1516. AliasResult Result = aliasSelect(S2, V2Size, V1, V1Size, AAQI);
  1517. Result.swap();
  1518. if (Result != AliasResult::MayAlias)
  1519. return Result;
  1520. }
  1521. // If both pointers are pointing into the same object and one of them
  1522. // accesses the entire object, then the accesses must overlap in some way.
  1523. if (O1 == O2) {
  1524. bool NullIsValidLocation = NullPointerIsDefined(&F);
  1525. if (V1Size.isPrecise() && V2Size.isPrecise() &&
  1526. (isObjectSize(O1, V1Size.getValue(), DL, TLI, NullIsValidLocation) ||
  1527. isObjectSize(O2, V2Size.getValue(), DL, TLI, NullIsValidLocation)))
  1528. return AliasResult::PartialAlias;
  1529. }
  1530. return AliasResult::MayAlias;
  1531. }
  1532. /// Check whether two Values can be considered equivalent.
  1533. ///
  1534. /// In addition to pointer equivalence of \p V1 and \p V2 this checks whether
  1535. /// they can not be part of a cycle in the value graph by looking at all
  1536. /// visited phi nodes an making sure that the phis cannot reach the value. We
  1537. /// have to do this because we are looking through phi nodes (That is we say
  1538. /// noalias(V, phi(VA, VB)) if noalias(V, VA) and noalias(V, VB).
  1539. bool BasicAAResult::isValueEqualInPotentialCycles(const Value *V,
  1540. const Value *V2) {
  1541. if (V != V2)
  1542. return false;
  1543. const Instruction *Inst = dyn_cast<Instruction>(V);
  1544. if (!Inst)
  1545. return true;
  1546. if (VisitedPhiBBs.empty())
  1547. return true;
  1548. if (VisitedPhiBBs.size() > MaxNumPhiBBsValueReachabilityCheck)
  1549. return false;
  1550. // Make sure that the visited phis cannot reach the Value. This ensures that
  1551. // the Values cannot come from different iterations of a potential cycle the
  1552. // phi nodes could be involved in.
  1553. for (auto *P : VisitedPhiBBs)
  1554. if (isPotentiallyReachable(&P->front(), Inst, nullptr, DT))
  1555. return false;
  1556. return true;
  1557. }
  1558. /// Computes the symbolic difference between two de-composed GEPs.
  1559. void BasicAAResult::subtractDecomposedGEPs(DecomposedGEP &DestGEP,
  1560. const DecomposedGEP &SrcGEP) {
  1561. DestGEP.Offset -= SrcGEP.Offset;
  1562. for (const VariableGEPIndex &Src : SrcGEP.VarIndices) {
  1563. // Find V in Dest. This is N^2, but pointer indices almost never have more
  1564. // than a few variable indexes.
  1565. bool Found = false;
  1566. for (auto I : enumerate(DestGEP.VarIndices)) {
  1567. VariableGEPIndex &Dest = I.value();
  1568. if (!isValueEqualInPotentialCycles(Dest.Val.V, Src.Val.V) ||
  1569. !Dest.Val.hasSameCastsAs(Src.Val))
  1570. continue;
  1571. // If we found it, subtract off Scale V's from the entry in Dest. If it
  1572. // goes to zero, remove the entry.
  1573. if (Dest.Scale != Src.Scale) {
  1574. Dest.Scale -= Src.Scale;
  1575. Dest.IsNSW = false;
  1576. } else {
  1577. DestGEP.VarIndices.erase(DestGEP.VarIndices.begin() + I.index());
  1578. }
  1579. Found = true;
  1580. break;
  1581. }
  1582. // If we didn't consume this entry, add it to the end of the Dest list.
  1583. if (!Found) {
  1584. VariableGEPIndex Entry = {Src.Val, -Src.Scale, Src.CxtI, Src.IsNSW};
  1585. DestGEP.VarIndices.push_back(Entry);
  1586. }
  1587. }
  1588. }
  1589. bool BasicAAResult::constantOffsetHeuristic(
  1590. const DecomposedGEP &GEP, LocationSize MaybeV1Size,
  1591. LocationSize MaybeV2Size, AssumptionCache *AC, DominatorTree *DT) {
  1592. if (GEP.VarIndices.size() != 2 || !MaybeV1Size.hasValue() ||
  1593. !MaybeV2Size.hasValue())
  1594. return false;
  1595. const uint64_t V1Size = MaybeV1Size.getValue();
  1596. const uint64_t V2Size = MaybeV2Size.getValue();
  1597. const VariableGEPIndex &Var0 = GEP.VarIndices[0], &Var1 = GEP.VarIndices[1];
  1598. if (Var0.Val.TruncBits != 0 || !Var0.Val.hasSameCastsAs(Var1.Val) ||
  1599. Var0.Scale != -Var1.Scale ||
  1600. Var0.Val.V->getType() != Var1.Val.V->getType())
  1601. return false;
  1602. // We'll strip off the Extensions of Var0 and Var1 and do another round
  1603. // of GetLinearExpression decomposition. In the example above, if Var0
  1604. // is zext(%x + 1) we should get V1 == %x and V1Offset == 1.
  1605. LinearExpression E0 =
  1606. GetLinearExpression(CastedValue(Var0.Val.V), DL, 0, AC, DT);
  1607. LinearExpression E1 =
  1608. GetLinearExpression(CastedValue(Var1.Val.V), DL, 0, AC, DT);
  1609. if (E0.Scale != E1.Scale || !E0.Val.hasSameCastsAs(E1.Val) ||
  1610. !isValueEqualInPotentialCycles(E0.Val.V, E1.Val.V))
  1611. return false;
  1612. // We have a hit - Var0 and Var1 only differ by a constant offset!
  1613. // If we've been sext'ed then zext'd the maximum difference between Var0 and
  1614. // Var1 is possible to calculate, but we're just interested in the absolute
  1615. // minimum difference between the two. The minimum distance may occur due to
  1616. // wrapping; consider "add i3 %i, 5": if %i == 7 then 7 + 5 mod 8 == 4, and so
  1617. // the minimum distance between %i and %i + 5 is 3.
  1618. APInt MinDiff = E0.Offset - E1.Offset, Wrapped = -MinDiff;
  1619. MinDiff = APIntOps::umin(MinDiff, Wrapped);
  1620. APInt MinDiffBytes =
  1621. MinDiff.zextOrTrunc(Var0.Scale.getBitWidth()) * Var0.Scale.abs();
  1622. // We can't definitely say whether GEP1 is before or after V2 due to wrapping
  1623. // arithmetic (i.e. for some values of GEP1 and V2 GEP1 < V2, and for other
  1624. // values GEP1 > V2). We'll therefore only declare NoAlias if both V1Size and
  1625. // V2Size can fit in the MinDiffBytes gap.
  1626. return MinDiffBytes.uge(V1Size + GEP.Offset.abs()) &&
  1627. MinDiffBytes.uge(V2Size + GEP.Offset.abs());
  1628. }
  1629. //===----------------------------------------------------------------------===//
  1630. // BasicAliasAnalysis Pass
  1631. //===----------------------------------------------------------------------===//
  1632. AnalysisKey BasicAA::Key;
  1633. BasicAAResult BasicAA::run(Function &F, FunctionAnalysisManager &AM) {
  1634. auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
  1635. auto &AC = AM.getResult<AssumptionAnalysis>(F);
  1636. auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
  1637. auto *PV = AM.getCachedResult<PhiValuesAnalysis>(F);
  1638. return BasicAAResult(F.getParent()->getDataLayout(), F, TLI, AC, DT, PV);
  1639. }
  1640. BasicAAWrapperPass::BasicAAWrapperPass() : FunctionPass(ID) {
  1641. initializeBasicAAWrapperPassPass(*PassRegistry::getPassRegistry());
  1642. }
  1643. char BasicAAWrapperPass::ID = 0;
  1644. void BasicAAWrapperPass::anchor() {}
  1645. INITIALIZE_PASS_BEGIN(BasicAAWrapperPass, "basic-aa",
  1646. "Basic Alias Analysis (stateless AA impl)", true, true)
  1647. INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
  1648. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  1649. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  1650. INITIALIZE_PASS_DEPENDENCY(PhiValuesWrapperPass)
  1651. INITIALIZE_PASS_END(BasicAAWrapperPass, "basic-aa",
  1652. "Basic Alias Analysis (stateless AA impl)", true, true)
  1653. FunctionPass *llvm::createBasicAAWrapperPass() {
  1654. return new BasicAAWrapperPass();
  1655. }
  1656. bool BasicAAWrapperPass::runOnFunction(Function &F) {
  1657. auto &ACT = getAnalysis<AssumptionCacheTracker>();
  1658. auto &TLIWP = getAnalysis<TargetLibraryInfoWrapperPass>();
  1659. auto &DTWP = getAnalysis<DominatorTreeWrapperPass>();
  1660. auto *PVWP = getAnalysisIfAvailable<PhiValuesWrapperPass>();
  1661. Result.reset(new BasicAAResult(F.getParent()->getDataLayout(), F,
  1662. TLIWP.getTLI(F), ACT.getAssumptionCache(F),
  1663. &DTWP.getDomTree(),
  1664. PVWP ? &PVWP->getResult() : nullptr));
  1665. return false;
  1666. }
  1667. void BasicAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
  1668. AU.setPreservesAll();
  1669. AU.addRequiredTransitive<AssumptionCacheTracker>();
  1670. AU.addRequiredTransitive<DominatorTreeWrapperPass>();
  1671. AU.addRequiredTransitive<TargetLibraryInfoWrapperPass>();
  1672. AU.addUsedIfAvailable<PhiValuesWrapperPass>();
  1673. }
  1674. BasicAAResult llvm::createLegacyPMBasicAAResult(Pass &P, Function &F) {
  1675. return BasicAAResult(
  1676. F.getParent()->getDataLayout(), F,
  1677. P.getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F),
  1678. P.getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F));
  1679. }