AliasSetTracker.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. //===- AliasSetTracker.cpp - Alias Sets Tracker 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 AliasSetTracker and AliasSet classes.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Analysis/AliasSetTracker.h"
  13. #include "llvm/Analysis/AliasAnalysis.h"
  14. #include "llvm/Analysis/GuardUtils.h"
  15. #include "llvm/Analysis/MemoryLocation.h"
  16. #include "llvm/Config/llvm-config.h"
  17. #include "llvm/IR/Function.h"
  18. #include "llvm/IR/InstIterator.h"
  19. #include "llvm/IR/Instructions.h"
  20. #include "llvm/IR/IntrinsicInst.h"
  21. #include "llvm/IR/PassManager.h"
  22. #include "llvm/IR/PatternMatch.h"
  23. #include "llvm/IR/Value.h"
  24. #include "llvm/InitializePasses.h"
  25. #include "llvm/Pass.h"
  26. #include "llvm/Support/AtomicOrdering.h"
  27. #include "llvm/Support/CommandLine.h"
  28. #include "llvm/Support/Compiler.h"
  29. #include "llvm/Support/Debug.h"
  30. #include "llvm/Support/ErrorHandling.h"
  31. #include "llvm/Support/raw_ostream.h"
  32. using namespace llvm;
  33. static cl::opt<unsigned>
  34. SaturationThreshold("alias-set-saturation-threshold", cl::Hidden,
  35. cl::init(250),
  36. cl::desc("The maximum number of pointers may-alias "
  37. "sets may contain before degradation"));
  38. /// mergeSetIn - Merge the specified alias set into this alias set.
  39. void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST,
  40. BatchAAResults &BatchAA) {
  41. assert(!AS.Forward && "Alias set is already forwarding!");
  42. assert(!Forward && "This set is a forwarding set!!");
  43. bool WasMustAlias = (Alias == SetMustAlias);
  44. // Update the alias and access types of this set...
  45. Access |= AS.Access;
  46. Alias |= AS.Alias;
  47. if (Alias == SetMustAlias) {
  48. // Check that these two merged sets really are must aliases. Since both
  49. // used to be must-alias sets, we can just check any pointer from each set
  50. // for aliasing.
  51. PointerRec *L = getSomePointer();
  52. PointerRec *R = AS.getSomePointer();
  53. // If the pointers are not a must-alias pair, this set becomes a may alias.
  54. if (!BatchAA.isMustAlias(
  55. MemoryLocation(L->getValue(), L->getSize(), L->getAAInfo()),
  56. MemoryLocation(R->getValue(), R->getSize(), R->getAAInfo())))
  57. Alias = SetMayAlias;
  58. }
  59. if (Alias == SetMayAlias) {
  60. if (WasMustAlias)
  61. AST.TotalMayAliasSetSize += size();
  62. if (AS.Alias == SetMustAlias)
  63. AST.TotalMayAliasSetSize += AS.size();
  64. }
  65. bool ASHadUnknownInsts = !AS.UnknownInsts.empty();
  66. if (UnknownInsts.empty()) { // Merge call sites...
  67. if (ASHadUnknownInsts) {
  68. std::swap(UnknownInsts, AS.UnknownInsts);
  69. addRef();
  70. }
  71. } else if (ASHadUnknownInsts) {
  72. llvm::append_range(UnknownInsts, AS.UnknownInsts);
  73. AS.UnknownInsts.clear();
  74. }
  75. AS.Forward = this; // Forward across AS now...
  76. addRef(); // AS is now pointing to us...
  77. // Merge the list of constituent pointers...
  78. if (AS.PtrList) {
  79. SetSize += AS.size();
  80. AS.SetSize = 0;
  81. *PtrListEnd = AS.PtrList;
  82. AS.PtrList->setPrevInList(PtrListEnd);
  83. PtrListEnd = AS.PtrListEnd;
  84. AS.PtrList = nullptr;
  85. AS.PtrListEnd = &AS.PtrList;
  86. assert(*AS.PtrListEnd == nullptr && "End of list is not null?");
  87. }
  88. if (ASHadUnknownInsts)
  89. AS.dropRef(AST);
  90. }
  91. void AliasSetTracker::removeAliasSet(AliasSet *AS) {
  92. if (AliasSet *Fwd = AS->Forward) {
  93. Fwd->dropRef(*this);
  94. AS->Forward = nullptr;
  95. } else // Update TotalMayAliasSetSize only if not forwarding.
  96. if (AS->Alias == AliasSet::SetMayAlias)
  97. TotalMayAliasSetSize -= AS->size();
  98. AliasSets.erase(AS);
  99. // If we've removed the saturated alias set, set saturated marker back to
  100. // nullptr and ensure this tracker is empty.
  101. if (AS == AliasAnyAS) {
  102. AliasAnyAS = nullptr;
  103. assert(AliasSets.empty() && "Tracker not empty");
  104. }
  105. }
  106. void AliasSet::removeFromTracker(AliasSetTracker &AST) {
  107. assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
  108. AST.removeAliasSet(this);
  109. }
  110. void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
  111. LocationSize Size, const AAMDNodes &AAInfo,
  112. bool KnownMustAlias, bool SkipSizeUpdate) {
  113. assert(!Entry.hasAliasSet() && "Entry already in set!");
  114. // Check to see if we have to downgrade to _may_ alias.
  115. if (isMustAlias())
  116. if (PointerRec *P = getSomePointer()) {
  117. if (!KnownMustAlias) {
  118. BatchAAResults &AA = AST.getAliasAnalysis();
  119. AliasResult Result = AA.alias(
  120. MemoryLocation(P->getValue(), P->getSize(), P->getAAInfo()),
  121. MemoryLocation(Entry.getValue(), Size, AAInfo));
  122. if (Result != AliasResult::MustAlias) {
  123. Alias = SetMayAlias;
  124. AST.TotalMayAliasSetSize += size();
  125. }
  126. assert(Result != AliasResult::NoAlias && "Cannot be part of must set!");
  127. } else if (!SkipSizeUpdate)
  128. P->updateSizeAndAAInfo(Size, AAInfo);
  129. }
  130. Entry.setAliasSet(this);
  131. Entry.updateSizeAndAAInfo(Size, AAInfo);
  132. // Add it to the end of the list...
  133. ++SetSize;
  134. assert(*PtrListEnd == nullptr && "End of list is not null?");
  135. *PtrListEnd = &Entry;
  136. PtrListEnd = Entry.setPrevInList(PtrListEnd);
  137. assert(*PtrListEnd == nullptr && "End of list is not null?");
  138. // Entry points to alias set.
  139. addRef();
  140. if (Alias == SetMayAlias)
  141. AST.TotalMayAliasSetSize++;
  142. }
  143. void AliasSet::addUnknownInst(Instruction *I, BatchAAResults &AA) {
  144. if (UnknownInsts.empty())
  145. addRef();
  146. UnknownInsts.emplace_back(I);
  147. // Guards are marked as modifying memory for control flow modelling purposes,
  148. // but don't actually modify any specific memory location.
  149. using namespace PatternMatch;
  150. bool MayWriteMemory = I->mayWriteToMemory() && !isGuard(I) &&
  151. !(I->use_empty() && match(I, m_Intrinsic<Intrinsic::invariant_start>()));
  152. if (!MayWriteMemory) {
  153. Alias = SetMayAlias;
  154. Access |= RefAccess;
  155. return;
  156. }
  157. // FIXME: This should use mod/ref information to make this not suck so bad
  158. Alias = SetMayAlias;
  159. Access = ModRefAccess;
  160. }
  161. /// aliasesPointer - If the specified pointer "may" (or must) alias one of the
  162. /// members in the set return the appropriate AliasResult. Otherwise return
  163. /// NoAlias.
  164. ///
  165. AliasResult AliasSet::aliasesPointer(const Value *Ptr, LocationSize Size,
  166. const AAMDNodes &AAInfo,
  167. BatchAAResults &AA) const {
  168. if (AliasAny)
  169. return AliasResult::MayAlias;
  170. if (Alias == SetMustAlias) {
  171. assert(UnknownInsts.empty() && "Illegal must alias set!");
  172. // If this is a set of MustAliases, only check to see if the pointer aliases
  173. // SOME value in the set.
  174. PointerRec *SomePtr = getSomePointer();
  175. assert(SomePtr && "Empty must-alias set??");
  176. return AA.alias(MemoryLocation(SomePtr->getValue(), SomePtr->getSize(),
  177. SomePtr->getAAInfo()),
  178. MemoryLocation(Ptr, Size, AAInfo));
  179. }
  180. // If this is a may-alias set, we have to check all of the pointers in the set
  181. // to be sure it doesn't alias the set...
  182. for (iterator I = begin(), E = end(); I != E; ++I) {
  183. AliasResult AR =
  184. AA.alias(MemoryLocation(Ptr, Size, AAInfo),
  185. MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo()));
  186. if (AR != AliasResult::NoAlias)
  187. return AR;
  188. }
  189. // Check the unknown instructions...
  190. if (!UnknownInsts.empty()) {
  191. for (Instruction *Inst : UnknownInsts)
  192. if (isModOrRefSet(
  193. AA.getModRefInfo(Inst, MemoryLocation(Ptr, Size, AAInfo))))
  194. return AliasResult::MayAlias;
  195. }
  196. return AliasResult::NoAlias;
  197. }
  198. ModRefInfo AliasSet::aliasesUnknownInst(const Instruction *Inst,
  199. BatchAAResults &AA) const {
  200. if (AliasAny)
  201. return ModRefInfo::ModRef;
  202. if (!Inst->mayReadOrWriteMemory())
  203. return ModRefInfo::NoModRef;
  204. for (Instruction *UnknownInst : UnknownInsts) {
  205. const auto *C1 = dyn_cast<CallBase>(UnknownInst);
  206. const auto *C2 = dyn_cast<CallBase>(Inst);
  207. if (!C1 || !C2 || isModOrRefSet(AA.getModRefInfo(C1, C2)) ||
  208. isModOrRefSet(AA.getModRefInfo(C2, C1))) {
  209. // TODO: Could be more precise, but not really useful right now.
  210. return ModRefInfo::ModRef;
  211. }
  212. }
  213. ModRefInfo MR = ModRefInfo::NoModRef;
  214. for (iterator I = begin(), E = end(); I != E; ++I) {
  215. MR |= AA.getModRefInfo(
  216. Inst, MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo()));
  217. if (isModAndRefSet(MR))
  218. return MR;
  219. }
  220. return MR;
  221. }
  222. void AliasSetTracker::clear() {
  223. // Delete all the PointerRec entries.
  224. for (auto &I : PointerMap)
  225. I.second->eraseFromList();
  226. PointerMap.clear();
  227. // The alias sets should all be clear now.
  228. AliasSets.clear();
  229. }
  230. /// mergeAliasSetsForPointer - Given a pointer, merge all alias sets that may
  231. /// alias the pointer. Return the unified set, or nullptr if no set that aliases
  232. /// the pointer was found. MustAliasAll is updated to true/false if the pointer
  233. /// is found to MustAlias all the sets it merged.
  234. AliasSet *AliasSetTracker::mergeAliasSetsForPointer(const Value *Ptr,
  235. LocationSize Size,
  236. const AAMDNodes &AAInfo,
  237. bool &MustAliasAll) {
  238. AliasSet *FoundSet = nullptr;
  239. MustAliasAll = true;
  240. for (AliasSet &AS : llvm::make_early_inc_range(*this)) {
  241. if (AS.Forward)
  242. continue;
  243. AliasResult AR = AS.aliasesPointer(Ptr, Size, AAInfo, AA);
  244. if (AR == AliasResult::NoAlias)
  245. continue;
  246. if (AR != AliasResult::MustAlias)
  247. MustAliasAll = false;
  248. if (!FoundSet) {
  249. // If this is the first alias set ptr can go into, remember it.
  250. FoundSet = &AS;
  251. } else {
  252. // Otherwise, we must merge the sets.
  253. FoundSet->mergeSetIn(AS, *this, AA);
  254. }
  255. }
  256. return FoundSet;
  257. }
  258. AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) {
  259. AliasSet *FoundSet = nullptr;
  260. for (AliasSet &AS : llvm::make_early_inc_range(*this)) {
  261. if (AS.Forward || !isModOrRefSet(AS.aliasesUnknownInst(Inst, AA)))
  262. continue;
  263. if (!FoundSet) {
  264. // If this is the first alias set ptr can go into, remember it.
  265. FoundSet = &AS;
  266. } else {
  267. // Otherwise, we must merge the sets.
  268. FoundSet->mergeSetIn(AS, *this, AA);
  269. }
  270. }
  271. return FoundSet;
  272. }
  273. AliasSet &AliasSetTracker::getAliasSetFor(const MemoryLocation &MemLoc) {
  274. Value * const Pointer = const_cast<Value*>(MemLoc.Ptr);
  275. const LocationSize Size = MemLoc.Size;
  276. const AAMDNodes &AAInfo = MemLoc.AATags;
  277. AliasSet::PointerRec &Entry = getEntryFor(Pointer);
  278. if (AliasAnyAS) {
  279. // At this point, the AST is saturated, so we only have one active alias
  280. // set. That means we already know which alias set we want to return, and
  281. // just need to add the pointer to that set to keep the data structure
  282. // consistent.
  283. // This, of course, means that we will never need a merge here.
  284. if (Entry.hasAliasSet()) {
  285. Entry.updateSizeAndAAInfo(Size, AAInfo);
  286. assert(Entry.getAliasSet(*this) == AliasAnyAS &&
  287. "Entry in saturated AST must belong to only alias set");
  288. } else {
  289. AliasAnyAS->addPointer(*this, Entry, Size, AAInfo);
  290. }
  291. return *AliasAnyAS;
  292. }
  293. bool MustAliasAll = false;
  294. // Check to see if the pointer is already known.
  295. if (Entry.hasAliasSet()) {
  296. // If the size changed, we may need to merge several alias sets.
  297. // Note that we can *not* return the result of mergeAliasSetsForPointer
  298. // due to a quirk of alias analysis behavior. Since alias(undef, undef)
  299. // is NoAlias, mergeAliasSetsForPointer(undef, ...) will not find the
  300. // the right set for undef, even if it exists.
  301. if (Entry.updateSizeAndAAInfo(Size, AAInfo))
  302. mergeAliasSetsForPointer(Pointer, Size, AAInfo, MustAliasAll);
  303. // Return the set!
  304. return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
  305. }
  306. if (AliasSet *AS =
  307. mergeAliasSetsForPointer(Pointer, Size, AAInfo, MustAliasAll)) {
  308. // Add it to the alias set it aliases.
  309. AS->addPointer(*this, Entry, Size, AAInfo, MustAliasAll);
  310. return *AS;
  311. }
  312. // Otherwise create a new alias set to hold the loaded pointer.
  313. AliasSets.push_back(new AliasSet());
  314. AliasSets.back().addPointer(*this, Entry, Size, AAInfo, true);
  315. return AliasSets.back();
  316. }
  317. void AliasSetTracker::add(Value *Ptr, LocationSize Size,
  318. const AAMDNodes &AAInfo) {
  319. addPointer(MemoryLocation(Ptr, Size, AAInfo), AliasSet::NoAccess);
  320. }
  321. void AliasSetTracker::add(LoadInst *LI) {
  322. if (isStrongerThanMonotonic(LI->getOrdering()))
  323. return addUnknown(LI);
  324. addPointer(MemoryLocation::get(LI), AliasSet::RefAccess);
  325. }
  326. void AliasSetTracker::add(StoreInst *SI) {
  327. if (isStrongerThanMonotonic(SI->getOrdering()))
  328. return addUnknown(SI);
  329. addPointer(MemoryLocation::get(SI), AliasSet::ModAccess);
  330. }
  331. void AliasSetTracker::add(VAArgInst *VAAI) {
  332. addPointer(MemoryLocation::get(VAAI), AliasSet::ModRefAccess);
  333. }
  334. void AliasSetTracker::add(AnyMemSetInst *MSI) {
  335. addPointer(MemoryLocation::getForDest(MSI), AliasSet::ModAccess);
  336. }
  337. void AliasSetTracker::add(AnyMemTransferInst *MTI) {
  338. addPointer(MemoryLocation::getForDest(MTI), AliasSet::ModAccess);
  339. addPointer(MemoryLocation::getForSource(MTI), AliasSet::RefAccess);
  340. }
  341. void AliasSetTracker::addUnknown(Instruction *Inst) {
  342. if (isa<DbgInfoIntrinsic>(Inst))
  343. return; // Ignore DbgInfo Intrinsics.
  344. if (auto *II = dyn_cast<IntrinsicInst>(Inst)) {
  345. // These intrinsics will show up as affecting memory, but they are just
  346. // markers.
  347. switch (II->getIntrinsicID()) {
  348. default:
  349. break;
  350. // FIXME: Add lifetime/invariant intrinsics (See: PR30807).
  351. case Intrinsic::assume:
  352. case Intrinsic::experimental_noalias_scope_decl:
  353. case Intrinsic::sideeffect:
  354. case Intrinsic::pseudoprobe:
  355. return;
  356. }
  357. }
  358. if (!Inst->mayReadOrWriteMemory())
  359. return; // doesn't alias anything
  360. if (AliasSet *AS = findAliasSetForUnknownInst(Inst)) {
  361. AS->addUnknownInst(Inst, AA);
  362. return;
  363. }
  364. AliasSets.push_back(new AliasSet());
  365. AliasSets.back().addUnknownInst(Inst, AA);
  366. }
  367. void AliasSetTracker::add(Instruction *I) {
  368. // Dispatch to one of the other add methods.
  369. if (LoadInst *LI = dyn_cast<LoadInst>(I))
  370. return add(LI);
  371. if (StoreInst *SI = dyn_cast<StoreInst>(I))
  372. return add(SI);
  373. if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
  374. return add(VAAI);
  375. if (AnyMemSetInst *MSI = dyn_cast<AnyMemSetInst>(I))
  376. return add(MSI);
  377. if (AnyMemTransferInst *MTI = dyn_cast<AnyMemTransferInst>(I))
  378. return add(MTI);
  379. // Handle all calls with known mod/ref sets genericall
  380. if (auto *Call = dyn_cast<CallBase>(I))
  381. if (Call->onlyAccessesArgMemory()) {
  382. auto getAccessFromModRef = [](ModRefInfo MRI) {
  383. if (isRefSet(MRI) && isModSet(MRI))
  384. return AliasSet::ModRefAccess;
  385. else if (isModSet(MRI))
  386. return AliasSet::ModAccess;
  387. else if (isRefSet(MRI))
  388. return AliasSet::RefAccess;
  389. else
  390. return AliasSet::NoAccess;
  391. };
  392. ModRefInfo CallMask = AA.getMemoryEffects(Call).getModRef();
  393. // Some intrinsics are marked as modifying memory for control flow
  394. // modelling purposes, but don't actually modify any specific memory
  395. // location.
  396. using namespace PatternMatch;
  397. if (Call->use_empty() &&
  398. match(Call, m_Intrinsic<Intrinsic::invariant_start>()))
  399. CallMask &= ModRefInfo::Ref;
  400. for (auto IdxArgPair : enumerate(Call->args())) {
  401. int ArgIdx = IdxArgPair.index();
  402. const Value *Arg = IdxArgPair.value();
  403. if (!Arg->getType()->isPointerTy())
  404. continue;
  405. MemoryLocation ArgLoc =
  406. MemoryLocation::getForArgument(Call, ArgIdx, nullptr);
  407. ModRefInfo ArgMask = AA.getArgModRefInfo(Call, ArgIdx);
  408. ArgMask &= CallMask;
  409. if (!isNoModRef(ArgMask))
  410. addPointer(ArgLoc, getAccessFromModRef(ArgMask));
  411. }
  412. return;
  413. }
  414. return addUnknown(I);
  415. }
  416. void AliasSetTracker::add(BasicBlock &BB) {
  417. for (auto &I : BB)
  418. add(&I);
  419. }
  420. void AliasSetTracker::add(const AliasSetTracker &AST) {
  421. assert(&AA == &AST.AA &&
  422. "Merging AliasSetTracker objects with different Alias Analyses!");
  423. // Loop over all of the alias sets in AST, adding the pointers contained
  424. // therein into the current alias sets. This can cause alias sets to be
  425. // merged together in the current AST.
  426. for (const AliasSet &AS : AST) {
  427. if (AS.Forward)
  428. continue; // Ignore forwarding alias sets
  429. // If there are any call sites in the alias set, add them to this AST.
  430. for (Instruction *Inst : AS.UnknownInsts)
  431. add(Inst);
  432. // Loop over all of the pointers in this alias set.
  433. for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI)
  434. addPointer(
  435. MemoryLocation(ASI.getPointer(), ASI.getSize(), ASI.getAAInfo()),
  436. (AliasSet::AccessLattice)AS.Access);
  437. }
  438. }
  439. AliasSet &AliasSetTracker::mergeAllAliasSets() {
  440. assert(!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold) &&
  441. "Full merge should happen once, when the saturation threshold is "
  442. "reached");
  443. // Collect all alias sets, so that we can drop references with impunity
  444. // without worrying about iterator invalidation.
  445. std::vector<AliasSet *> ASVector;
  446. ASVector.reserve(SaturationThreshold);
  447. for (AliasSet &AS : *this)
  448. ASVector.push_back(&AS);
  449. // Copy all instructions and pointers into a new set, and forward all other
  450. // sets to it.
  451. AliasSets.push_back(new AliasSet());
  452. AliasAnyAS = &AliasSets.back();
  453. AliasAnyAS->Alias = AliasSet::SetMayAlias;
  454. AliasAnyAS->Access = AliasSet::ModRefAccess;
  455. AliasAnyAS->AliasAny = true;
  456. for (auto *Cur : ASVector) {
  457. // If Cur was already forwarding, just forward to the new AS instead.
  458. AliasSet *FwdTo = Cur->Forward;
  459. if (FwdTo) {
  460. Cur->Forward = AliasAnyAS;
  461. AliasAnyAS->addRef();
  462. FwdTo->dropRef(*this);
  463. continue;
  464. }
  465. // Otherwise, perform the actual merge.
  466. AliasAnyAS->mergeSetIn(*Cur, *this, AA);
  467. }
  468. return *AliasAnyAS;
  469. }
  470. AliasSet &AliasSetTracker::addPointer(MemoryLocation Loc,
  471. AliasSet::AccessLattice E) {
  472. AliasSet &AS = getAliasSetFor(Loc);
  473. AS.Access |= E;
  474. if (!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold)) {
  475. // The AST is now saturated. From here on, we conservatively consider all
  476. // pointers to alias each-other.
  477. return mergeAllAliasSets();
  478. }
  479. return AS;
  480. }
  481. //===----------------------------------------------------------------------===//
  482. // AliasSet/AliasSetTracker Printing Support
  483. //===----------------------------------------------------------------------===//
  484. void AliasSet::print(raw_ostream &OS) const {
  485. OS << " AliasSet[" << (const void*)this << ", " << RefCount << "] ";
  486. OS << (Alias == SetMustAlias ? "must" : "may") << " alias, ";
  487. switch (Access) {
  488. case NoAccess: OS << "No access "; break;
  489. case RefAccess: OS << "Ref "; break;
  490. case ModAccess: OS << "Mod "; break;
  491. case ModRefAccess: OS << "Mod/Ref "; break;
  492. default: llvm_unreachable("Bad value for Access!");
  493. }
  494. if (Forward)
  495. OS << " forwarding to " << (void*)Forward;
  496. if (!empty()) {
  497. OS << "Pointers: ";
  498. for (iterator I = begin(), E = end(); I != E; ++I) {
  499. if (I != begin()) OS << ", ";
  500. I.getPointer()->printAsOperand(OS << "(");
  501. if (I.getSize() == LocationSize::afterPointer())
  502. OS << ", unknown after)";
  503. else if (I.getSize() == LocationSize::beforeOrAfterPointer())
  504. OS << ", unknown before-or-after)";
  505. else
  506. OS << ", " << I.getSize() << ")";
  507. }
  508. }
  509. if (!UnknownInsts.empty()) {
  510. ListSeparator LS;
  511. OS << "\n " << UnknownInsts.size() << " Unknown instructions: ";
  512. for (Instruction *I : UnknownInsts) {
  513. OS << LS;
  514. if (I->hasName())
  515. I->printAsOperand(OS);
  516. else
  517. I->print(OS);
  518. }
  519. }
  520. OS << "\n";
  521. }
  522. void AliasSetTracker::print(raw_ostream &OS) const {
  523. OS << "Alias Set Tracker: " << AliasSets.size();
  524. if (AliasAnyAS)
  525. OS << " (Saturated)";
  526. OS << " alias sets for " << PointerMap.size() << " pointer values.\n";
  527. for (const AliasSet &AS : *this)
  528. AS.print(OS);
  529. OS << "\n";
  530. }
  531. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  532. LLVM_DUMP_METHOD void AliasSet::dump() const { print(dbgs()); }
  533. LLVM_DUMP_METHOD void AliasSetTracker::dump() const { print(dbgs()); }
  534. #endif
  535. //===----------------------------------------------------------------------===//
  536. // AliasSetPrinter Pass
  537. //===----------------------------------------------------------------------===//
  538. AliasSetsPrinterPass::AliasSetsPrinterPass(raw_ostream &OS) : OS(OS) {}
  539. PreservedAnalyses AliasSetsPrinterPass::run(Function &F,
  540. FunctionAnalysisManager &AM) {
  541. auto &AA = AM.getResult<AAManager>(F);
  542. BatchAAResults BatchAA(AA);
  543. AliasSetTracker Tracker(BatchAA);
  544. OS << "Alias sets for function '" << F.getName() << "':\n";
  545. for (Instruction &I : instructions(F))
  546. Tracker.add(&I);
  547. Tracker.print(OS);
  548. return PreservedAnalyses::all();
  549. }