AliasAnalysis.cpp 34 KB

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