AliasAnalysis.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. //==- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation --==//
  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 implements the generic AliasAnalysis interface which is used as the
  10. // common interface used by all clients and implementations of alias analysis.
  11. //
  12. // This file also implements the default version of the AliasAnalysis interface
  13. // that is to be used when no other implementation is specified. This does some
  14. // simple tests that detect obvious cases: two different global pointers cannot
  15. // alias, a global cannot alias a malloc, two different mallocs cannot alias,
  16. // etc.
  17. //
  18. // This alias analysis implementation really isn't very good for anything, but
  19. // it is very fast, and makes a nice clean default implementation. Because it
  20. // handles lots of little corner cases, other, more complex, alias analysis
  21. // implementations may choose to rely on this pass to resolve these simple and
  22. // easy cases.
  23. //
  24. //===----------------------------------------------------------------------===//
  25. #include "llvm/Analysis/AliasAnalysis.h"
  26. #include "llvm/ADT/Statistic.h"
  27. #include "llvm/Analysis/BasicAliasAnalysis.h"
  28. #include "llvm/Analysis/CFLAndersAliasAnalysis.h"
  29. #include "llvm/Analysis/CFLSteensAliasAnalysis.h"
  30. #include "llvm/Analysis/CaptureTracking.h"
  31. #include "llvm/Analysis/GlobalsModRef.h"
  32. #include "llvm/Analysis/MemoryLocation.h"
  33. #include "llvm/Analysis/ObjCARCAliasAnalysis.h"
  34. #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
  35. #include "llvm/Analysis/ScopedNoAliasAA.h"
  36. #include "llvm/Analysis/TargetLibraryInfo.h"
  37. #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
  38. #include "llvm/Analysis/ValueTracking.h"
  39. #include "llvm/IR/Argument.h"
  40. #include "llvm/IR/Attributes.h"
  41. #include "llvm/IR/BasicBlock.h"
  42. #include "llvm/IR/Instruction.h"
  43. #include "llvm/IR/Instructions.h"
  44. #include "llvm/IR/Module.h"
  45. #include "llvm/IR/Type.h"
  46. #include "llvm/IR/Value.h"
  47. #include "llvm/InitializePasses.h"
  48. #include "llvm/Pass.h"
  49. #include "llvm/Support/AtomicOrdering.h"
  50. #include "llvm/Support/Casting.h"
  51. #include "llvm/Support/CommandLine.h"
  52. #include <algorithm>
  53. #include <cassert>
  54. #include <functional>
  55. #include <iterator>
  56. #define DEBUG_TYPE "aa"
  57. using namespace llvm;
  58. STATISTIC(NumNoAlias, "Number of NoAlias results");
  59. STATISTIC(NumMayAlias, "Number of MayAlias results");
  60. STATISTIC(NumMustAlias, "Number of MustAlias results");
  61. namespace llvm {
  62. /// Allow disabling BasicAA from the AA results. This is particularly useful
  63. /// when testing to isolate a single AA implementation.
  64. cl::opt<bool> DisableBasicAA("disable-basic-aa", cl::Hidden, cl::init(false));
  65. } // namespace llvm
  66. #ifndef NDEBUG
  67. /// Print a trace of alias analysis queries and their results.
  68. static cl::opt<bool> EnableAATrace("aa-trace", cl::Hidden, cl::init(false));
  69. #else
  70. static const bool EnableAATrace = false;
  71. #endif
  72. AAResults::AAResults(AAResults &&Arg)
  73. : TLI(Arg.TLI), AAs(std::move(Arg.AAs)), AADeps(std::move(Arg.AADeps)) {
  74. for (auto &AA : AAs)
  75. AA->setAAResults(this);
  76. }
  77. AAResults::~AAResults() {
  78. // FIXME; It would be nice to at least clear out the pointers back to this
  79. // aggregation here, but we end up with non-nesting lifetimes in the legacy
  80. // pass manager that prevent this from working. In the legacy pass manager
  81. // we'll end up with dangling references here in some cases.
  82. #if 0
  83. for (auto &AA : AAs)
  84. AA->setAAResults(nullptr);
  85. #endif
  86. }
  87. bool AAResults::invalidate(Function &F, const PreservedAnalyses &PA,
  88. FunctionAnalysisManager::Invalidator &Inv) {
  89. // AAResults preserves the AAManager by default, due to the stateless nature
  90. // of AliasAnalysis. There is no need to check whether it has been preserved
  91. // explicitly. Check if any module dependency was invalidated and caused the
  92. // AAManager to be invalidated. Invalidate ourselves in that case.
  93. auto PAC = PA.getChecker<AAManager>();
  94. if (!PAC.preservedWhenStateless())
  95. return true;
  96. // Check if any of the function dependencies were invalidated, and invalidate
  97. // ourselves in that case.
  98. for (AnalysisKey *ID : AADeps)
  99. if (Inv.invalidate(ID, F, PA))
  100. return true;
  101. // Everything we depend on is still fine, so are we. Nothing to invalidate.
  102. return false;
  103. }
  104. //===----------------------------------------------------------------------===//
  105. // Default chaining methods
  106. //===----------------------------------------------------------------------===//
  107. AliasResult AAResults::alias(const MemoryLocation &LocA,
  108. const MemoryLocation &LocB) {
  109. SimpleAAQueryInfo AAQIP;
  110. return alias(LocA, LocB, AAQIP);
  111. }
  112. AliasResult AAResults::alias(const MemoryLocation &LocA,
  113. const MemoryLocation &LocB, AAQueryInfo &AAQI) {
  114. AliasResult Result = AliasResult::MayAlias;
  115. if (EnableAATrace) {
  116. for (unsigned I = 0; I < AAQI.Depth; ++I)
  117. dbgs() << " ";
  118. dbgs() << "Start " << *LocA.Ptr << " @ " << LocA.Size << ", "
  119. << *LocB.Ptr << " @ " << LocB.Size << "\n";
  120. }
  121. AAQI.Depth++;
  122. for (const auto &AA : AAs) {
  123. Result = AA->alias(LocA, LocB, AAQI);
  124. if (Result != AliasResult::MayAlias)
  125. break;
  126. }
  127. AAQI.Depth--;
  128. if (EnableAATrace) {
  129. for (unsigned I = 0; I < AAQI.Depth; ++I)
  130. dbgs() << " ";
  131. dbgs() << "End " << *LocA.Ptr << " @ " << LocA.Size << ", "
  132. << *LocB.Ptr << " @ " << LocB.Size << " = " << Result << "\n";
  133. }
  134. if (AAQI.Depth == 0) {
  135. if (Result == AliasResult::NoAlias)
  136. ++NumNoAlias;
  137. else if (Result == AliasResult::MustAlias)
  138. ++NumMustAlias;
  139. else
  140. ++NumMayAlias;
  141. }
  142. return Result;
  143. }
  144. bool AAResults::pointsToConstantMemory(const MemoryLocation &Loc,
  145. bool OrLocal) {
  146. SimpleAAQueryInfo AAQIP;
  147. return pointsToConstantMemory(Loc, AAQIP, OrLocal);
  148. }
  149. bool AAResults::pointsToConstantMemory(const MemoryLocation &Loc,
  150. AAQueryInfo &AAQI, bool OrLocal) {
  151. for (const auto &AA : AAs)
  152. if (AA->pointsToConstantMemory(Loc, AAQI, OrLocal))
  153. return true;
  154. return false;
  155. }
  156. ModRefInfo AAResults::getArgModRefInfo(const CallBase *Call, unsigned ArgIdx) {
  157. ModRefInfo Result = ModRefInfo::ModRef;
  158. for (const auto &AA : AAs) {
  159. Result = intersectModRef(Result, AA->getArgModRefInfo(Call, ArgIdx));
  160. // Early-exit the moment we reach the bottom of the lattice.
  161. if (isNoModRef(Result))
  162. return ModRefInfo::NoModRef;
  163. }
  164. return Result;
  165. }
  166. ModRefInfo AAResults::getModRefInfo(Instruction *I, const CallBase *Call2) {
  167. SimpleAAQueryInfo AAQIP;
  168. return getModRefInfo(I, Call2, AAQIP);
  169. }
  170. ModRefInfo AAResults::getModRefInfo(Instruction *I, const CallBase *Call2,
  171. AAQueryInfo &AAQI) {
  172. // We may have two calls.
  173. if (const auto *Call1 = dyn_cast<CallBase>(I)) {
  174. // Check if the two calls modify the same memory.
  175. return getModRefInfo(Call1, Call2, AAQI);
  176. }
  177. // If this is a fence, just return ModRef.
  178. if (I->isFenceLike())
  179. return ModRefInfo::ModRef;
  180. // Otherwise, check if the call modifies or references the
  181. // location this memory access defines. The best we can say
  182. // is that if the call references what this instruction
  183. // defines, it must be clobbered by this location.
  184. const MemoryLocation DefLoc = MemoryLocation::get(I);
  185. ModRefInfo MR = getModRefInfo(Call2, DefLoc, AAQI);
  186. if (isModOrRefSet(MR))
  187. return setModAndRef(MR);
  188. return ModRefInfo::NoModRef;
  189. }
  190. ModRefInfo AAResults::getModRefInfo(const CallBase *Call,
  191. const MemoryLocation &Loc) {
  192. SimpleAAQueryInfo AAQIP;
  193. return getModRefInfo(Call, Loc, AAQIP);
  194. }
  195. ModRefInfo AAResults::getModRefInfo(const CallBase *Call,
  196. const MemoryLocation &Loc,
  197. AAQueryInfo &AAQI) {
  198. ModRefInfo Result = ModRefInfo::ModRef;
  199. for (const auto &AA : AAs) {
  200. Result = intersectModRef(Result, AA->getModRefInfo(Call, Loc, AAQI));
  201. // Early-exit the moment we reach the bottom of the lattice.
  202. if (isNoModRef(Result))
  203. return ModRefInfo::NoModRef;
  204. }
  205. // Try to refine the mod-ref info further using other API entry points to the
  206. // aggregate set of AA results.
  207. auto MRB = getModRefBehavior(Call);
  208. if (onlyAccessesInaccessibleMem(MRB))
  209. return ModRefInfo::NoModRef;
  210. if (onlyReadsMemory(MRB))
  211. Result = clearMod(Result);
  212. else if (onlyWritesMemory(MRB))
  213. Result = clearRef(Result);
  214. if (onlyAccessesArgPointees(MRB) || onlyAccessesInaccessibleOrArgMem(MRB)) {
  215. bool IsMustAlias = true;
  216. ModRefInfo AllArgsMask = ModRefInfo::NoModRef;
  217. if (doesAccessArgPointees(MRB)) {
  218. for (const auto &I : llvm::enumerate(Call->args())) {
  219. const Value *Arg = I.value();
  220. if (!Arg->getType()->isPointerTy())
  221. continue;
  222. unsigned ArgIdx = I.index();
  223. MemoryLocation ArgLoc =
  224. MemoryLocation::getForArgument(Call, ArgIdx, TLI);
  225. AliasResult ArgAlias = alias(ArgLoc, Loc, AAQI);
  226. if (ArgAlias != AliasResult::NoAlias) {
  227. ModRefInfo ArgMask = getArgModRefInfo(Call, ArgIdx);
  228. AllArgsMask = unionModRef(AllArgsMask, ArgMask);
  229. }
  230. // Conservatively clear IsMustAlias unless only MustAlias is found.
  231. IsMustAlias &= (ArgAlias == AliasResult::MustAlias);
  232. }
  233. }
  234. // Return NoModRef if no alias found with any argument.
  235. if (isNoModRef(AllArgsMask))
  236. return ModRefInfo::NoModRef;
  237. // Logical & between other AA analyses and argument analysis.
  238. Result = intersectModRef(Result, AllArgsMask);
  239. // If only MustAlias found above, set Must bit.
  240. Result = IsMustAlias ? setMust(Result) : clearMust(Result);
  241. }
  242. // If Loc is a constant memory location, the call definitely could not
  243. // modify the memory location.
  244. if (isModSet(Result) && pointsToConstantMemory(Loc, AAQI, /*OrLocal*/ false))
  245. Result = clearMod(Result);
  246. return Result;
  247. }
  248. ModRefInfo AAResults::getModRefInfo(const CallBase *Call1,
  249. const CallBase *Call2) {
  250. SimpleAAQueryInfo AAQIP;
  251. return getModRefInfo(Call1, Call2, AAQIP);
  252. }
  253. ModRefInfo AAResults::getModRefInfo(const CallBase *Call1,
  254. const CallBase *Call2, AAQueryInfo &AAQI) {
  255. ModRefInfo Result = ModRefInfo::ModRef;
  256. for (const auto &AA : AAs) {
  257. Result = intersectModRef(Result, AA->getModRefInfo(Call1, Call2, AAQI));
  258. // Early-exit the moment we reach the bottom of the lattice.
  259. if (isNoModRef(Result))
  260. return ModRefInfo::NoModRef;
  261. }
  262. // Try to refine the mod-ref info further using other API entry points to the
  263. // aggregate set of AA results.
  264. // If Call1 or Call2 are readnone, they don't interact.
  265. auto Call1B = getModRefBehavior(Call1);
  266. if (Call1B == FMRB_DoesNotAccessMemory)
  267. return ModRefInfo::NoModRef;
  268. auto Call2B = getModRefBehavior(Call2);
  269. if (Call2B == FMRB_DoesNotAccessMemory)
  270. return ModRefInfo::NoModRef;
  271. // If they both only read from memory, there is no dependence.
  272. if (onlyReadsMemory(Call1B) && onlyReadsMemory(Call2B))
  273. return ModRefInfo::NoModRef;
  274. // If Call1 only reads memory, the only dependence on Call2 can be
  275. // from Call1 reading memory written by Call2.
  276. if (onlyReadsMemory(Call1B))
  277. Result = clearMod(Result);
  278. else if (onlyWritesMemory(Call1B))
  279. Result = clearRef(Result);
  280. // If Call2 only access memory through arguments, accumulate the mod/ref
  281. // information from Call1's references to the memory referenced by
  282. // Call2's arguments.
  283. if (onlyAccessesArgPointees(Call2B)) {
  284. if (!doesAccessArgPointees(Call2B))
  285. return ModRefInfo::NoModRef;
  286. ModRefInfo R = ModRefInfo::NoModRef;
  287. bool IsMustAlias = true;
  288. for (auto I = Call2->arg_begin(), E = Call2->arg_end(); I != E; ++I) {
  289. const Value *Arg = *I;
  290. if (!Arg->getType()->isPointerTy())
  291. continue;
  292. unsigned Call2ArgIdx = std::distance(Call2->arg_begin(), I);
  293. auto Call2ArgLoc =
  294. MemoryLocation::getForArgument(Call2, Call2ArgIdx, TLI);
  295. // ArgModRefC2 indicates what Call2 might do to Call2ArgLoc, and the
  296. // dependence of Call1 on that location is the inverse:
  297. // - If Call2 modifies location, dependence exists if Call1 reads or
  298. // writes.
  299. // - If Call2 only reads location, dependence exists if Call1 writes.
  300. ModRefInfo ArgModRefC2 = getArgModRefInfo(Call2, Call2ArgIdx);
  301. ModRefInfo ArgMask = ModRefInfo::NoModRef;
  302. if (isModSet(ArgModRefC2))
  303. ArgMask = ModRefInfo::ModRef;
  304. else if (isRefSet(ArgModRefC2))
  305. ArgMask = ModRefInfo::Mod;
  306. // ModRefC1 indicates what Call1 might do to Call2ArgLoc, and we use
  307. // above ArgMask to update dependence info.
  308. ModRefInfo ModRefC1 = getModRefInfo(Call1, Call2ArgLoc, AAQI);
  309. ArgMask = intersectModRef(ArgMask, ModRefC1);
  310. // Conservatively clear IsMustAlias unless only MustAlias is found.
  311. IsMustAlias &= isMustSet(ModRefC1);
  312. R = intersectModRef(unionModRef(R, ArgMask), Result);
  313. if (R == Result) {
  314. // On early exit, not all args were checked, cannot set Must.
  315. if (I + 1 != E)
  316. IsMustAlias = false;
  317. break;
  318. }
  319. }
  320. if (isNoModRef(R))
  321. return ModRefInfo::NoModRef;
  322. // If MustAlias found above, set Must bit.
  323. return IsMustAlias ? setMust(R) : clearMust(R);
  324. }
  325. // If Call1 only accesses memory through arguments, check if Call2 references
  326. // any of the memory referenced by Call1's arguments. If not, return NoModRef.
  327. if (onlyAccessesArgPointees(Call1B)) {
  328. if (!doesAccessArgPointees(Call1B))
  329. return ModRefInfo::NoModRef;
  330. ModRefInfo R = ModRefInfo::NoModRef;
  331. bool IsMustAlias = true;
  332. for (auto I = Call1->arg_begin(), E = Call1->arg_end(); I != E; ++I) {
  333. const Value *Arg = *I;
  334. if (!Arg->getType()->isPointerTy())
  335. continue;
  336. unsigned Call1ArgIdx = std::distance(Call1->arg_begin(), I);
  337. auto Call1ArgLoc =
  338. MemoryLocation::getForArgument(Call1, Call1ArgIdx, TLI);
  339. // ArgModRefC1 indicates what Call1 might do to Call1ArgLoc; if Call1
  340. // might Mod Call1ArgLoc, then we care about either a Mod or a Ref by
  341. // Call2. If Call1 might Ref, then we care only about a Mod by Call2.
  342. ModRefInfo ArgModRefC1 = getArgModRefInfo(Call1, Call1ArgIdx);
  343. ModRefInfo ModRefC2 = getModRefInfo(Call2, Call1ArgLoc, AAQI);
  344. if ((isModSet(ArgModRefC1) && isModOrRefSet(ModRefC2)) ||
  345. (isRefSet(ArgModRefC1) && isModSet(ModRefC2)))
  346. R = intersectModRef(unionModRef(R, ArgModRefC1), Result);
  347. // Conservatively clear IsMustAlias unless only MustAlias is found.
  348. IsMustAlias &= isMustSet(ModRefC2);
  349. if (R == Result) {
  350. // On early exit, not all args were checked, cannot set Must.
  351. if (I + 1 != E)
  352. IsMustAlias = false;
  353. break;
  354. }
  355. }
  356. if (isNoModRef(R))
  357. return ModRefInfo::NoModRef;
  358. // If MustAlias found above, set Must bit.
  359. return IsMustAlias ? setMust(R) : clearMust(R);
  360. }
  361. return Result;
  362. }
  363. FunctionModRefBehavior AAResults::getModRefBehavior(const CallBase *Call) {
  364. FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior;
  365. for (const auto &AA : AAs) {
  366. Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(Call));
  367. // Early-exit the moment we reach the bottom of the lattice.
  368. if (Result == FMRB_DoesNotAccessMemory)
  369. return Result;
  370. }
  371. return Result;
  372. }
  373. FunctionModRefBehavior AAResults::getModRefBehavior(const Function *F) {
  374. FunctionModRefBehavior Result = FMRB_UnknownModRefBehavior;
  375. for (const auto &AA : AAs) {
  376. Result = FunctionModRefBehavior(Result & AA->getModRefBehavior(F));
  377. // Early-exit the moment we reach the bottom of the lattice.
  378. if (Result == FMRB_DoesNotAccessMemory)
  379. return Result;
  380. }
  381. return Result;
  382. }
  383. raw_ostream &llvm::operator<<(raw_ostream &OS, AliasResult AR) {
  384. switch (AR) {
  385. case AliasResult::NoAlias:
  386. OS << "NoAlias";
  387. break;
  388. case AliasResult::MustAlias:
  389. OS << "MustAlias";
  390. break;
  391. case AliasResult::MayAlias:
  392. OS << "MayAlias";
  393. break;
  394. case AliasResult::PartialAlias:
  395. OS << "PartialAlias";
  396. if (AR.hasOffset())
  397. OS << " (off " << AR.getOffset() << ")";
  398. break;
  399. }
  400. return OS;
  401. }
  402. //===----------------------------------------------------------------------===//
  403. // Helper method implementation
  404. //===----------------------------------------------------------------------===//
  405. ModRefInfo AAResults::getModRefInfo(const LoadInst *L,
  406. const MemoryLocation &Loc) {
  407. SimpleAAQueryInfo AAQIP;
  408. return getModRefInfo(L, Loc, AAQIP);
  409. }
  410. ModRefInfo AAResults::getModRefInfo(const LoadInst *L,
  411. const MemoryLocation &Loc,
  412. AAQueryInfo &AAQI) {
  413. // Be conservative in the face of atomic.
  414. if (isStrongerThan(L->getOrdering(), AtomicOrdering::Unordered))
  415. return ModRefInfo::ModRef;
  416. // If the load address doesn't alias the given address, it doesn't read
  417. // or write the specified memory.
  418. if (Loc.Ptr) {
  419. AliasResult AR = alias(MemoryLocation::get(L), Loc, AAQI);
  420. if (AR == AliasResult::NoAlias)
  421. return ModRefInfo::NoModRef;
  422. if (AR == AliasResult::MustAlias)
  423. return ModRefInfo::MustRef;
  424. }
  425. // Otherwise, a load just reads.
  426. return ModRefInfo::Ref;
  427. }
  428. ModRefInfo AAResults::getModRefInfo(const StoreInst *S,
  429. const MemoryLocation &Loc) {
  430. SimpleAAQueryInfo AAQIP;
  431. return getModRefInfo(S, Loc, AAQIP);
  432. }
  433. ModRefInfo AAResults::getModRefInfo(const StoreInst *S,
  434. const MemoryLocation &Loc,
  435. AAQueryInfo &AAQI) {
  436. // Be conservative in the face of atomic.
  437. if (isStrongerThan(S->getOrdering(), AtomicOrdering::Unordered))
  438. return ModRefInfo::ModRef;
  439. if (Loc.Ptr) {
  440. AliasResult AR = alias(MemoryLocation::get(S), Loc, AAQI);
  441. // If the store address cannot alias the pointer in question, then the
  442. // specified memory cannot be modified by the store.
  443. if (AR == AliasResult::NoAlias)
  444. return ModRefInfo::NoModRef;
  445. // If the pointer is a pointer to constant memory, then it could not have
  446. // been modified by this store.
  447. if (pointsToConstantMemory(Loc, AAQI))
  448. return ModRefInfo::NoModRef;
  449. // If the store address aliases the pointer as must alias, set Must.
  450. if (AR == AliasResult::MustAlias)
  451. return ModRefInfo::MustMod;
  452. }
  453. // Otherwise, a store just writes.
  454. return ModRefInfo::Mod;
  455. }
  456. ModRefInfo AAResults::getModRefInfo(const FenceInst *S, const MemoryLocation &Loc) {
  457. SimpleAAQueryInfo AAQIP;
  458. return getModRefInfo(S, Loc, AAQIP);
  459. }
  460. ModRefInfo AAResults::getModRefInfo(const FenceInst *S,
  461. const MemoryLocation &Loc,
  462. AAQueryInfo &AAQI) {
  463. // If we know that the location is a constant memory location, the fence
  464. // cannot modify this location.
  465. if (Loc.Ptr && pointsToConstantMemory(Loc, AAQI))
  466. return ModRefInfo::Ref;
  467. return ModRefInfo::ModRef;
  468. }
  469. ModRefInfo AAResults::getModRefInfo(const VAArgInst *V,
  470. const MemoryLocation &Loc) {
  471. SimpleAAQueryInfo AAQIP;
  472. return getModRefInfo(V, Loc, AAQIP);
  473. }
  474. ModRefInfo AAResults::getModRefInfo(const VAArgInst *V,
  475. const MemoryLocation &Loc,
  476. AAQueryInfo &AAQI) {
  477. if (Loc.Ptr) {
  478. AliasResult AR = alias(MemoryLocation::get(V), Loc, AAQI);
  479. // If the va_arg address cannot alias the pointer in question, then the
  480. // specified memory cannot be accessed by the va_arg.
  481. if (AR == AliasResult::NoAlias)
  482. return ModRefInfo::NoModRef;
  483. // If the pointer is a pointer to constant memory, then it could not have
  484. // been modified by this va_arg.
  485. if (pointsToConstantMemory(Loc, AAQI))
  486. return ModRefInfo::NoModRef;
  487. // If the va_arg aliases the pointer as must alias, set Must.
  488. if (AR == AliasResult::MustAlias)
  489. return ModRefInfo::MustModRef;
  490. }
  491. // Otherwise, a va_arg reads and writes.
  492. return ModRefInfo::ModRef;
  493. }
  494. ModRefInfo AAResults::getModRefInfo(const CatchPadInst *CatchPad,
  495. const MemoryLocation &Loc) {
  496. SimpleAAQueryInfo AAQIP;
  497. return getModRefInfo(CatchPad, Loc, AAQIP);
  498. }
  499. ModRefInfo AAResults::getModRefInfo(const CatchPadInst *CatchPad,
  500. const MemoryLocation &Loc,
  501. AAQueryInfo &AAQI) {
  502. if (Loc.Ptr) {
  503. // If the pointer is a pointer to constant memory,
  504. // then it could not have been modified by this catchpad.
  505. if (pointsToConstantMemory(Loc, AAQI))
  506. return ModRefInfo::NoModRef;
  507. }
  508. // Otherwise, a catchpad reads and writes.
  509. return ModRefInfo::ModRef;
  510. }
  511. ModRefInfo AAResults::getModRefInfo(const CatchReturnInst *CatchRet,
  512. const MemoryLocation &Loc) {
  513. SimpleAAQueryInfo AAQIP;
  514. return getModRefInfo(CatchRet, Loc, AAQIP);
  515. }
  516. ModRefInfo AAResults::getModRefInfo(const CatchReturnInst *CatchRet,
  517. const MemoryLocation &Loc,
  518. AAQueryInfo &AAQI) {
  519. if (Loc.Ptr) {
  520. // If the pointer is a pointer to constant memory,
  521. // then it could not have been modified by this catchpad.
  522. if (pointsToConstantMemory(Loc, AAQI))
  523. return ModRefInfo::NoModRef;
  524. }
  525. // Otherwise, a catchret reads and writes.
  526. return ModRefInfo::ModRef;
  527. }
  528. ModRefInfo AAResults::getModRefInfo(const AtomicCmpXchgInst *CX,
  529. const MemoryLocation &Loc) {
  530. SimpleAAQueryInfo AAQIP;
  531. return getModRefInfo(CX, Loc, AAQIP);
  532. }
  533. ModRefInfo AAResults::getModRefInfo(const AtomicCmpXchgInst *CX,
  534. const MemoryLocation &Loc,
  535. AAQueryInfo &AAQI) {
  536. // Acquire/Release cmpxchg has properties that matter for arbitrary addresses.
  537. if (isStrongerThanMonotonic(CX->getSuccessOrdering()))
  538. return ModRefInfo::ModRef;
  539. if (Loc.Ptr) {
  540. AliasResult AR = alias(MemoryLocation::get(CX), Loc, AAQI);
  541. // If the cmpxchg address does not alias the location, it does not access
  542. // it.
  543. if (AR == AliasResult::NoAlias)
  544. return ModRefInfo::NoModRef;
  545. // If the cmpxchg address aliases the pointer as must alias, set Must.
  546. if (AR == AliasResult::MustAlias)
  547. return ModRefInfo::MustModRef;
  548. }
  549. return ModRefInfo::ModRef;
  550. }
  551. ModRefInfo AAResults::getModRefInfo(const AtomicRMWInst *RMW,
  552. const MemoryLocation &Loc) {
  553. SimpleAAQueryInfo AAQIP;
  554. return getModRefInfo(RMW, Loc, AAQIP);
  555. }
  556. ModRefInfo AAResults::getModRefInfo(const AtomicRMWInst *RMW,
  557. const MemoryLocation &Loc,
  558. AAQueryInfo &AAQI) {
  559. // Acquire/Release atomicrmw has properties that matter for arbitrary addresses.
  560. if (isStrongerThanMonotonic(RMW->getOrdering()))
  561. return ModRefInfo::ModRef;
  562. if (Loc.Ptr) {
  563. AliasResult AR = alias(MemoryLocation::get(RMW), Loc, AAQI);
  564. // If the atomicrmw address does not alias the location, it does not access
  565. // it.
  566. if (AR == AliasResult::NoAlias)
  567. return ModRefInfo::NoModRef;
  568. // If the atomicrmw address aliases the pointer as must alias, set Must.
  569. if (AR == AliasResult::MustAlias)
  570. return ModRefInfo::MustModRef;
  571. }
  572. return ModRefInfo::ModRef;
  573. }
  574. ModRefInfo AAResults::getModRefInfo(const Instruction *I,
  575. const Optional<MemoryLocation> &OptLoc,
  576. AAQueryInfo &AAQIP) {
  577. if (OptLoc == None) {
  578. if (const auto *Call = dyn_cast<CallBase>(I)) {
  579. return createModRefInfo(getModRefBehavior(Call));
  580. }
  581. }
  582. const MemoryLocation &Loc = OptLoc.getValueOr(MemoryLocation());
  583. switch (I->getOpcode()) {
  584. case Instruction::VAArg:
  585. return getModRefInfo((const VAArgInst *)I, Loc, AAQIP);
  586. case Instruction::Load:
  587. return getModRefInfo((const LoadInst *)I, Loc, AAQIP);
  588. case Instruction::Store:
  589. return getModRefInfo((const StoreInst *)I, Loc, AAQIP);
  590. case Instruction::Fence:
  591. return getModRefInfo((const FenceInst *)I, Loc, AAQIP);
  592. case Instruction::AtomicCmpXchg:
  593. return getModRefInfo((const AtomicCmpXchgInst *)I, Loc, AAQIP);
  594. case Instruction::AtomicRMW:
  595. return getModRefInfo((const AtomicRMWInst *)I, Loc, AAQIP);
  596. case Instruction::Call:
  597. case Instruction::CallBr:
  598. case Instruction::Invoke:
  599. return getModRefInfo((const CallBase *)I, Loc, AAQIP);
  600. case Instruction::CatchPad:
  601. return getModRefInfo((const CatchPadInst *)I, Loc, AAQIP);
  602. case Instruction::CatchRet:
  603. return getModRefInfo((const CatchReturnInst *)I, Loc, AAQIP);
  604. default:
  605. assert(!I->mayReadOrWriteMemory() &&
  606. "Unhandled memory access instruction!");
  607. return ModRefInfo::NoModRef;
  608. }
  609. }
  610. /// Return information about whether a particular call site modifies
  611. /// or reads the specified memory location \p MemLoc before instruction \p I
  612. /// in a BasicBlock.
  613. /// FIXME: this is really just shoring-up a deficiency in alias analysis.
  614. /// BasicAA isn't willing to spend linear time determining whether an alloca
  615. /// was captured before or after this particular call, while we are. However,
  616. /// with a smarter AA in place, this test is just wasting compile time.
  617. ModRefInfo AAResults::callCapturesBefore(const Instruction *I,
  618. const MemoryLocation &MemLoc,
  619. DominatorTree *DT,
  620. AAQueryInfo &AAQI) {
  621. if (!DT)
  622. return ModRefInfo::ModRef;
  623. const Value *Object = getUnderlyingObject(MemLoc.Ptr);
  624. if (!isIdentifiedFunctionLocal(Object))
  625. return ModRefInfo::ModRef;
  626. const auto *Call = dyn_cast<CallBase>(I);
  627. if (!Call || Call == Object)
  628. return ModRefInfo::ModRef;
  629. if (PointerMayBeCapturedBefore(Object, /* ReturnCaptures */ true,
  630. /* StoreCaptures */ true, I, DT,
  631. /* include Object */ true))
  632. return ModRefInfo::ModRef;
  633. unsigned ArgNo = 0;
  634. ModRefInfo R = ModRefInfo::NoModRef;
  635. bool IsMustAlias = true;
  636. // Set flag only if no May found and all operands processed.
  637. for (auto CI = Call->data_operands_begin(), CE = Call->data_operands_end();
  638. CI != CE; ++CI, ++ArgNo) {
  639. // Only look at the no-capture or byval pointer arguments. If this
  640. // pointer were passed to arguments that were neither of these, then it
  641. // couldn't be no-capture.
  642. if (!(*CI)->getType()->isPointerTy() ||
  643. (!Call->doesNotCapture(ArgNo) && ArgNo < Call->arg_size() &&
  644. !Call->isByValArgument(ArgNo)))
  645. continue;
  646. AliasResult AR = alias(
  647. MemoryLocation::getBeforeOrAfter(*CI),
  648. MemoryLocation::getBeforeOrAfter(Object), AAQI);
  649. // If this is a no-capture pointer argument, see if we can tell that it
  650. // is impossible to alias the pointer we're checking. If not, we have to
  651. // assume that the call could touch the pointer, even though it doesn't
  652. // escape.
  653. if (AR != AliasResult::MustAlias)
  654. IsMustAlias = false;
  655. if (AR == AliasResult::NoAlias)
  656. continue;
  657. if (Call->doesNotAccessMemory(ArgNo))
  658. continue;
  659. if (Call->onlyReadsMemory(ArgNo)) {
  660. R = ModRefInfo::Ref;
  661. continue;
  662. }
  663. // Not returning MustModRef since we have not seen all the arguments.
  664. return ModRefInfo::ModRef;
  665. }
  666. return IsMustAlias ? setMust(R) : clearMust(R);
  667. }
  668. /// canBasicBlockModify - Return true if it is possible for execution of the
  669. /// specified basic block to modify the location Loc.
  670. ///
  671. bool AAResults::canBasicBlockModify(const BasicBlock &BB,
  672. const MemoryLocation &Loc) {
  673. return canInstructionRangeModRef(BB.front(), BB.back(), Loc, ModRefInfo::Mod);
  674. }
  675. /// canInstructionRangeModRef - Return true if it is possible for the
  676. /// execution of the specified instructions to mod\ref (according to the
  677. /// mode) the location Loc. The instructions to consider are all
  678. /// of the instructions in the range of [I1,I2] INCLUSIVE.
  679. /// I1 and I2 must be in the same basic block.
  680. bool AAResults::canInstructionRangeModRef(const Instruction &I1,
  681. const Instruction &I2,
  682. const MemoryLocation &Loc,
  683. const ModRefInfo Mode) {
  684. assert(I1.getParent() == I2.getParent() &&
  685. "Instructions not in same basic block!");
  686. BasicBlock::const_iterator I = I1.getIterator();
  687. BasicBlock::const_iterator E = I2.getIterator();
  688. ++E; // Convert from inclusive to exclusive range.
  689. for (; I != E; ++I) // Check every instruction in range
  690. if (isModOrRefSet(intersectModRef(getModRefInfo(&*I, Loc), Mode)))
  691. return true;
  692. return false;
  693. }
  694. // Provide a definition for the root virtual destructor.
  695. AAResults::Concept::~Concept() = default;
  696. // Provide a definition for the static object used to identify passes.
  697. AnalysisKey AAManager::Key;
  698. ExternalAAWrapperPass::ExternalAAWrapperPass() : ImmutablePass(ID) {
  699. initializeExternalAAWrapperPassPass(*PassRegistry::getPassRegistry());
  700. }
  701. ExternalAAWrapperPass::ExternalAAWrapperPass(CallbackT CB)
  702. : ImmutablePass(ID), CB(std::move(CB)) {
  703. initializeExternalAAWrapperPassPass(*PassRegistry::getPassRegistry());
  704. }
  705. char ExternalAAWrapperPass::ID = 0;
  706. INITIALIZE_PASS(ExternalAAWrapperPass, "external-aa", "External Alias Analysis",
  707. false, true)
  708. ImmutablePass *
  709. llvm::createExternalAAWrapperPass(ExternalAAWrapperPass::CallbackT Callback) {
  710. return new ExternalAAWrapperPass(std::move(Callback));
  711. }
  712. AAResultsWrapperPass::AAResultsWrapperPass() : FunctionPass(ID) {
  713. initializeAAResultsWrapperPassPass(*PassRegistry::getPassRegistry());
  714. }
  715. char AAResultsWrapperPass::ID = 0;
  716. INITIALIZE_PASS_BEGIN(AAResultsWrapperPass, "aa",
  717. "Function Alias Analysis Results", false, true)
  718. INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass)
  719. INITIALIZE_PASS_DEPENDENCY(CFLAndersAAWrapperPass)
  720. INITIALIZE_PASS_DEPENDENCY(CFLSteensAAWrapperPass)
  721. INITIALIZE_PASS_DEPENDENCY(ExternalAAWrapperPass)
  722. INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
  723. INITIALIZE_PASS_DEPENDENCY(ObjCARCAAWrapperPass)
  724. INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass)
  725. INITIALIZE_PASS_DEPENDENCY(ScopedNoAliasAAWrapperPass)
  726. INITIALIZE_PASS_DEPENDENCY(TypeBasedAAWrapperPass)
  727. INITIALIZE_PASS_END(AAResultsWrapperPass, "aa",
  728. "Function Alias Analysis Results", false, true)
  729. FunctionPass *llvm::createAAResultsWrapperPass() {
  730. return new AAResultsWrapperPass();
  731. }
  732. /// Run the wrapper pass to rebuild an aggregation over known AA passes.
  733. ///
  734. /// This is the legacy pass manager's interface to the new-style AA results
  735. /// aggregation object. Because this is somewhat shoe-horned into the legacy
  736. /// pass manager, we hard code all the specific alias analyses available into
  737. /// it. While the particular set enabled is configured via commandline flags,
  738. /// adding a new alias analysis to LLVM will require adding support for it to
  739. /// this list.
  740. bool AAResultsWrapperPass::runOnFunction(Function &F) {
  741. // NB! This *must* be reset before adding new AA results to the new
  742. // AAResults object because in the legacy pass manager, each instance
  743. // of these will refer to the *same* immutable analyses, registering and
  744. // unregistering themselves with them. We need to carefully tear down the
  745. // previous object first, in this case replacing it with an empty one, before
  746. // registering new results.
  747. AAR.reset(
  748. new AAResults(getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F)));
  749. // BasicAA is always available for function analyses. Also, we add it first
  750. // so that it can trump TBAA results when it proves MustAlias.
  751. // FIXME: TBAA should have an explicit mode to support this and then we
  752. // should reconsider the ordering here.
  753. if (!DisableBasicAA)
  754. AAR->addAAResult(getAnalysis<BasicAAWrapperPass>().getResult());
  755. // Populate the results with the currently available AAs.
  756. if (auto *WrapperPass = getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>())
  757. AAR->addAAResult(WrapperPass->getResult());
  758. if (auto *WrapperPass = getAnalysisIfAvailable<TypeBasedAAWrapperPass>())
  759. AAR->addAAResult(WrapperPass->getResult());
  760. if (auto *WrapperPass =
  761. getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>())
  762. AAR->addAAResult(WrapperPass->getResult());
  763. if (auto *WrapperPass = getAnalysisIfAvailable<GlobalsAAWrapperPass>())
  764. AAR->addAAResult(WrapperPass->getResult());
  765. if (auto *WrapperPass = getAnalysisIfAvailable<SCEVAAWrapperPass>())
  766. AAR->addAAResult(WrapperPass->getResult());
  767. if (auto *WrapperPass = getAnalysisIfAvailable<CFLAndersAAWrapperPass>())
  768. AAR->addAAResult(WrapperPass->getResult());
  769. if (auto *WrapperPass = getAnalysisIfAvailable<CFLSteensAAWrapperPass>())
  770. AAR->addAAResult(WrapperPass->getResult());
  771. // If available, run an external AA providing callback over the results as
  772. // well.
  773. if (auto *WrapperPass = getAnalysisIfAvailable<ExternalAAWrapperPass>())
  774. if (WrapperPass->CB)
  775. WrapperPass->CB(*this, F, *AAR);
  776. // Analyses don't mutate the IR, so return false.
  777. return false;
  778. }
  779. void AAResultsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
  780. AU.setPreservesAll();
  781. AU.addRequiredTransitive<BasicAAWrapperPass>();
  782. AU.addRequiredTransitive<TargetLibraryInfoWrapperPass>();
  783. // We also need to mark all the alias analysis passes we will potentially
  784. // probe in runOnFunction as used here to ensure the legacy pass manager
  785. // preserves them. This hard coding of lists of alias analyses is specific to
  786. // the legacy pass manager.
  787. AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>();
  788. AU.addUsedIfAvailable<TypeBasedAAWrapperPass>();
  789. AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>();
  790. AU.addUsedIfAvailable<GlobalsAAWrapperPass>();
  791. AU.addUsedIfAvailable<SCEVAAWrapperPass>();
  792. AU.addUsedIfAvailable<CFLAndersAAWrapperPass>();
  793. AU.addUsedIfAvailable<CFLSteensAAWrapperPass>();
  794. AU.addUsedIfAvailable<ExternalAAWrapperPass>();
  795. }
  796. AAManager::Result AAManager::run(Function &F, FunctionAnalysisManager &AM) {
  797. Result R(AM.getResult<TargetLibraryAnalysis>(F));
  798. for (auto &Getter : ResultGetters)
  799. (*Getter)(F, AM, R);
  800. return R;
  801. }
  802. AAResults llvm::createLegacyPMAAResults(Pass &P, Function &F,
  803. BasicAAResult &BAR) {
  804. AAResults AAR(P.getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F));
  805. // Add in our explicitly constructed BasicAA results.
  806. if (!DisableBasicAA)
  807. AAR.addAAResult(BAR);
  808. // Populate the results with the other currently available AAs.
  809. if (auto *WrapperPass =
  810. P.getAnalysisIfAvailable<ScopedNoAliasAAWrapperPass>())
  811. AAR.addAAResult(WrapperPass->getResult());
  812. if (auto *WrapperPass = P.getAnalysisIfAvailable<TypeBasedAAWrapperPass>())
  813. AAR.addAAResult(WrapperPass->getResult());
  814. if (auto *WrapperPass =
  815. P.getAnalysisIfAvailable<objcarc::ObjCARCAAWrapperPass>())
  816. AAR.addAAResult(WrapperPass->getResult());
  817. if (auto *WrapperPass = P.getAnalysisIfAvailable<GlobalsAAWrapperPass>())
  818. AAR.addAAResult(WrapperPass->getResult());
  819. if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLAndersAAWrapperPass>())
  820. AAR.addAAResult(WrapperPass->getResult());
  821. if (auto *WrapperPass = P.getAnalysisIfAvailable<CFLSteensAAWrapperPass>())
  822. AAR.addAAResult(WrapperPass->getResult());
  823. if (auto *WrapperPass = P.getAnalysisIfAvailable<ExternalAAWrapperPass>())
  824. if (WrapperPass->CB)
  825. WrapperPass->CB(P, F, AAR);
  826. return AAR;
  827. }
  828. bool llvm::isNoAliasCall(const Value *V) {
  829. if (const auto *Call = dyn_cast<CallBase>(V))
  830. return Call->hasRetAttr(Attribute::NoAlias);
  831. return false;
  832. }
  833. static bool isNoAliasOrByValArgument(const Value *V) {
  834. if (const Argument *A = dyn_cast<Argument>(V))
  835. return A->hasNoAliasAttr() || A->hasByValAttr();
  836. return false;
  837. }
  838. bool llvm::isIdentifiedObject(const Value *V) {
  839. if (isa<AllocaInst>(V))
  840. return true;
  841. if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
  842. return true;
  843. if (isNoAliasCall(V))
  844. return true;
  845. if (isNoAliasOrByValArgument(V))
  846. return true;
  847. return false;
  848. }
  849. bool llvm::isIdentifiedFunctionLocal(const Value *V) {
  850. return isa<AllocaInst>(V) || isNoAliasCall(V) || isNoAliasOrByValArgument(V);
  851. }
  852. bool llvm::isNotVisibleOnUnwind(const Value *Object,
  853. bool &RequiresNoCaptureBeforeUnwind) {
  854. RequiresNoCaptureBeforeUnwind = false;
  855. // Alloca goes out of scope on unwind.
  856. if (isa<AllocaInst>(Object))
  857. return true;
  858. // Byval goes out of scope on unwind.
  859. if (auto *A = dyn_cast<Argument>(Object))
  860. return A->hasByValAttr();
  861. // A noalias return is not accessible from any other code. If the pointer
  862. // does not escape prior to the unwind, then the caller cannot access the
  863. // memory either.
  864. if (isNoAliasCall(Object)) {
  865. RequiresNoCaptureBeforeUnwind = true;
  866. return true;
  867. }
  868. return false;
  869. }
  870. void llvm::getAAResultsAnalysisUsage(AnalysisUsage &AU) {
  871. // This function needs to be in sync with llvm::createLegacyPMAAResults -- if
  872. // more alias analyses are added to llvm::createLegacyPMAAResults, they need
  873. // to be added here also.
  874. AU.addRequired<TargetLibraryInfoWrapperPass>();
  875. AU.addUsedIfAvailable<ScopedNoAliasAAWrapperPass>();
  876. AU.addUsedIfAvailable<TypeBasedAAWrapperPass>();
  877. AU.addUsedIfAvailable<objcarc::ObjCARCAAWrapperPass>();
  878. AU.addUsedIfAvailable<GlobalsAAWrapperPass>();
  879. AU.addUsedIfAvailable<CFLAndersAAWrapperPass>();
  880. AU.addUsedIfAvailable<CFLSteensAAWrapperPass>();
  881. AU.addUsedIfAvailable<ExternalAAWrapperPass>();
  882. }