AliasSetTracker.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. //===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the AliasSetTracker and AliasSet classes.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Analysis/AliasSetTracker.h"
  14. #include "llvm/Analysis/AliasAnalysis.h"
  15. #include "llvm/Analysis/MemoryLocation.h"
  16. #include "llvm/Config/llvm-config.h"
  17. #include "llvm/IR/CallSite.h"
  18. #include "llvm/IR/Constants.h"
  19. #include "llvm/IR/DataLayout.h"
  20. #include "llvm/IR/Function.h"
  21. #include "llvm/IR/InstIterator.h"
  22. #include "llvm/IR/Instruction.h"
  23. #include "llvm/IR/Instructions.h"
  24. #include "llvm/IR/IntrinsicInst.h"
  25. #include "llvm/IR/Module.h"
  26. #include "llvm/IR/Value.h"
  27. #include "llvm/Pass.h"
  28. #include "llvm/Support/AtomicOrdering.h"
  29. #include "llvm/Support/Casting.h"
  30. #include "llvm/Support/CommandLine.h"
  31. #include "llvm/Support/Compiler.h"
  32. #include "llvm/Support/Debug.h"
  33. #include "llvm/Support/ErrorHandling.h"
  34. #include "llvm/Support/raw_ostream.h"
  35. #include <cassert>
  36. #include <cstdint>
  37. #include <vector>
  38. using namespace llvm;
  39. static cl::opt<unsigned>
  40. SaturationThreshold("alias-set-saturation-threshold", cl::Hidden,
  41. cl::init(250),
  42. cl::desc("The maximum number of pointers may-alias "
  43. "sets may contain before degradation"));
  44. /// mergeSetIn - Merge the specified alias set into this alias set.
  45. ///
  46. void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
  47. assert(!AS.Forward && "Alias set is already forwarding!");
  48. assert(!Forward && "This set is a forwarding set!!");
  49. bool WasMustAlias = (Alias == SetMustAlias);
  50. // Update the alias and access types of this set...
  51. Access |= AS.Access;
  52. Alias |= AS.Alias;
  53. Volatile |= AS.Volatile;
  54. if (Alias == SetMustAlias) {
  55. // Check that these two merged sets really are must aliases. Since both
  56. // used to be must-alias sets, we can just check any pointer from each set
  57. // for aliasing.
  58. AliasAnalysis &AA = AST.getAliasAnalysis();
  59. PointerRec *L = getSomePointer();
  60. PointerRec *R = AS.getSomePointer();
  61. // If the pointers are not a must-alias pair, this set becomes a may alias.
  62. if (AA.alias(MemoryLocation(L->getValue(), L->getSize(), L->getAAInfo()),
  63. MemoryLocation(R->getValue(), R->getSize(), R->getAAInfo())) !=
  64. MustAlias)
  65. Alias = SetMayAlias;
  66. }
  67. if (Alias == SetMayAlias) {
  68. if (WasMustAlias)
  69. AST.TotalMayAliasSetSize += size();
  70. if (AS.Alias == SetMustAlias)
  71. AST.TotalMayAliasSetSize += AS.size();
  72. }
  73. bool ASHadUnknownInsts = !AS.UnknownInsts.empty();
  74. if (UnknownInsts.empty()) { // Merge call sites...
  75. if (ASHadUnknownInsts) {
  76. std::swap(UnknownInsts, AS.UnknownInsts);
  77. addRef();
  78. }
  79. } else if (ASHadUnknownInsts) {
  80. UnknownInsts.insert(UnknownInsts.end(), AS.UnknownInsts.begin(), AS.UnknownInsts.end());
  81. AS.UnknownInsts.clear();
  82. }
  83. AS.Forward = this; // Forward across AS now...
  84. addRef(); // AS is now pointing to us...
  85. // Merge the list of constituent pointers...
  86. if (AS.PtrList) {
  87. SetSize += AS.size();
  88. AS.SetSize = 0;
  89. *PtrListEnd = AS.PtrList;
  90. AS.PtrList->setPrevInList(PtrListEnd);
  91. PtrListEnd = AS.PtrListEnd;
  92. AS.PtrList = nullptr;
  93. AS.PtrListEnd = &AS.PtrList;
  94. assert(*AS.PtrListEnd == nullptr && "End of list is not null?");
  95. }
  96. if (ASHadUnknownInsts)
  97. AS.dropRef(AST);
  98. }
  99. void AliasSetTracker::removeAliasSet(AliasSet *AS) {
  100. if (AliasSet *Fwd = AS->Forward) {
  101. Fwd->dropRef(*this);
  102. AS->Forward = nullptr;
  103. }
  104. if (AS->Alias == AliasSet::SetMayAlias)
  105. TotalMayAliasSetSize -= AS->size();
  106. AliasSets.erase(AS);
  107. }
  108. void AliasSet::removeFromTracker(AliasSetTracker &AST) {
  109. assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
  110. AST.removeAliasSet(this);
  111. }
  112. void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
  113. LocationSize Size, const AAMDNodes &AAInfo,
  114. bool KnownMustAlias) {
  115. assert(!Entry.hasAliasSet() && "Entry already in set!");
  116. // Check to see if we have to downgrade to _may_ alias.
  117. if (isMustAlias() && !KnownMustAlias)
  118. if (PointerRec *P = getSomePointer()) {
  119. AliasAnalysis &AA = AST.getAliasAnalysis();
  120. AliasResult Result =
  121. AA.alias(MemoryLocation(P->getValue(), P->getSize(), P->getAAInfo()),
  122. MemoryLocation(Entry.getValue(), Size, AAInfo));
  123. if (Result != MustAlias) {
  124. Alias = SetMayAlias;
  125. AST.TotalMayAliasSetSize += size();
  126. } else {
  127. // First entry of must alias must have maximum size!
  128. P->updateSizeAndAAInfo(Size, AAInfo);
  129. }
  130. assert(Result != NoAlias && "Cannot be part of must set!");
  131. }
  132. Entry.setAliasSet(this);
  133. Entry.updateSizeAndAAInfo(Size, AAInfo);
  134. // Add it to the end of the list...
  135. ++SetSize;
  136. assert(*PtrListEnd == nullptr && "End of list is not null?");
  137. *PtrListEnd = &Entry;
  138. PtrListEnd = Entry.setPrevInList(PtrListEnd);
  139. assert(*PtrListEnd == nullptr && "End of list is not null?");
  140. // Entry points to alias set.
  141. addRef();
  142. if (Alias == SetMayAlias)
  143. AST.TotalMayAliasSetSize++;
  144. }
  145. void AliasSet::addUnknownInst(Instruction *I, AliasAnalysis &AA) {
  146. if (UnknownInsts.empty())
  147. addRef();
  148. UnknownInsts.emplace_back(I);
  149. if (!I->mayWriteToMemory()) {
  150. Alias = SetMayAlias;
  151. Access |= RefAccess;
  152. return;
  153. }
  154. // FIXME: This should use mod/ref information to make this not suck so bad
  155. Alias = SetMayAlias;
  156. Access = ModRefAccess;
  157. }
  158. /// aliasesPointer - Return true if the specified pointer "may" (or must)
  159. /// alias one of the members in the set.
  160. ///
  161. bool AliasSet::aliasesPointer(const Value *Ptr, LocationSize Size,
  162. const AAMDNodes &AAInfo,
  163. AliasAnalysis &AA) const {
  164. if (AliasAny)
  165. return true;
  166. if (Alias == SetMustAlias) {
  167. assert(UnknownInsts.empty() && "Illegal must alias set!");
  168. // If this is a set of MustAliases, only check to see if the pointer aliases
  169. // SOME value in the set.
  170. PointerRec *SomePtr = getSomePointer();
  171. assert(SomePtr && "Empty must-alias set??");
  172. return AA.alias(MemoryLocation(SomePtr->getValue(), SomePtr->getSize(),
  173. SomePtr->getAAInfo()),
  174. MemoryLocation(Ptr, Size, AAInfo));
  175. }
  176. // If this is a may-alias set, we have to check all of the pointers in the set
  177. // to be sure it doesn't alias the set...
  178. for (iterator I = begin(), E = end(); I != E; ++I)
  179. if (AA.alias(MemoryLocation(Ptr, Size, AAInfo),
  180. MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo())))
  181. return true;
  182. // Check the unknown instructions...
  183. if (!UnknownInsts.empty()) {
  184. for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i)
  185. if (auto *Inst = getUnknownInst(i))
  186. if (isModOrRefSet(
  187. AA.getModRefInfo(Inst, MemoryLocation(Ptr, Size, AAInfo))))
  188. return true;
  189. }
  190. return false;
  191. }
  192. bool AliasSet::aliasesUnknownInst(const Instruction *Inst,
  193. AliasAnalysis &AA) const {
  194. if (AliasAny)
  195. return true;
  196. if (!Inst->mayReadOrWriteMemory())
  197. return false;
  198. for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
  199. if (auto *UnknownInst = getUnknownInst(i)) {
  200. ImmutableCallSite C1(UnknownInst), C2(Inst);
  201. if (!C1 || !C2 || isModOrRefSet(AA.getModRefInfo(C1, C2)) ||
  202. isModOrRefSet(AA.getModRefInfo(C2, C1)))
  203. return true;
  204. }
  205. }
  206. for (iterator I = begin(), E = end(); I != E; ++I)
  207. if (isModOrRefSet(AA.getModRefInfo(
  208. Inst, MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo()))))
  209. return true;
  210. return false;
  211. }
  212. void AliasSetTracker::clear() {
  213. // Delete all the PointerRec entries.
  214. for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
  215. I != E; ++I)
  216. I->second->eraseFromList();
  217. PointerMap.clear();
  218. // The alias sets should all be clear now.
  219. AliasSets.clear();
  220. }
  221. /// mergeAliasSetsForPointer - Given a pointer, merge all alias sets that may
  222. /// alias the pointer. Return the unified set, or nullptr if no set that aliases
  223. /// the pointer was found.
  224. AliasSet *AliasSetTracker::mergeAliasSetsForPointer(const Value *Ptr,
  225. LocationSize Size,
  226. const AAMDNodes &AAInfo) {
  227. AliasSet *FoundSet = nullptr;
  228. for (iterator I = begin(), E = end(); I != E;) {
  229. iterator Cur = I++;
  230. if (Cur->Forward || !Cur->aliasesPointer(Ptr, Size, AAInfo, AA)) continue;
  231. if (!FoundSet) { // If this is the first alias set ptr can go into.
  232. FoundSet = &*Cur; // Remember it.
  233. } else { // Otherwise, we must merge the sets.
  234. FoundSet->mergeSetIn(*Cur, *this); // Merge in contents.
  235. }
  236. }
  237. return FoundSet;
  238. }
  239. bool AliasSetTracker::containsUnknown(const Instruction *Inst) const {
  240. for (const AliasSet &AS : *this)
  241. if (!AS.Forward && AS.aliasesUnknownInst(Inst, AA))
  242. return true;
  243. return false;
  244. }
  245. AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) {
  246. AliasSet *FoundSet = nullptr;
  247. for (iterator I = begin(), E = end(); I != E;) {
  248. iterator Cur = I++;
  249. if (Cur->Forward || !Cur->aliasesUnknownInst(Inst, AA))
  250. continue;
  251. if (!FoundSet) // If this is the first alias set ptr can go into.
  252. FoundSet = &*Cur; // Remember it.
  253. else if (!Cur->Forward) // Otherwise, we must merge the sets.
  254. FoundSet->mergeSetIn(*Cur, *this); // Merge in contents.
  255. }
  256. return FoundSet;
  257. }
  258. /// getAliasSetForPointer - Return the alias set that the specified pointer
  259. /// lives in.
  260. AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer,
  261. LocationSize Size,
  262. const AAMDNodes &AAInfo) {
  263. AliasSet::PointerRec &Entry = getEntryFor(Pointer);
  264. if (AliasAnyAS) {
  265. // At this point, the AST is saturated, so we only have one active alias
  266. // set. That means we already know which alias set we want to return, and
  267. // just need to add the pointer to that set to keep the data structure
  268. // consistent.
  269. // This, of course, means that we will never need a merge here.
  270. if (Entry.hasAliasSet()) {
  271. Entry.updateSizeAndAAInfo(Size, AAInfo);
  272. assert(Entry.getAliasSet(*this) == AliasAnyAS &&
  273. "Entry in saturated AST must belong to only alias set");
  274. } else {
  275. AliasAnyAS->addPointer(*this, Entry, Size, AAInfo);
  276. }
  277. return *AliasAnyAS;
  278. }
  279. // Check to see if the pointer is already known.
  280. if (Entry.hasAliasSet()) {
  281. // If the size changed, we may need to merge several alias sets.
  282. // Note that we can *not* return the result of mergeAliasSetsForPointer
  283. // due to a quirk of alias analysis behavior. Since alias(undef, undef)
  284. // is NoAlias, mergeAliasSetsForPointer(undef, ...) will not find the
  285. // the right set for undef, even if it exists.
  286. if (Entry.updateSizeAndAAInfo(Size, AAInfo))
  287. mergeAliasSetsForPointer(Pointer, Size, AAInfo);
  288. // Return the set!
  289. return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
  290. }
  291. if (AliasSet *AS = mergeAliasSetsForPointer(Pointer, Size, AAInfo)) {
  292. // Add it to the alias set it aliases.
  293. AS->addPointer(*this, Entry, Size, AAInfo);
  294. return *AS;
  295. }
  296. // Otherwise create a new alias set to hold the loaded pointer.
  297. AliasSets.push_back(new AliasSet());
  298. AliasSets.back().addPointer(*this, Entry, Size, AAInfo);
  299. return AliasSets.back();
  300. }
  301. void AliasSetTracker::add(Value *Ptr, LocationSize Size,
  302. const AAMDNodes &AAInfo) {
  303. addPointer(Ptr, Size, AAInfo, AliasSet::NoAccess);
  304. }
  305. void AliasSetTracker::add(LoadInst *LI) {
  306. if (isStrongerThanMonotonic(LI->getOrdering())) return addUnknown(LI);
  307. AliasSet &AS = addPointer(MemoryLocation::get(LI), AliasSet::RefAccess);
  308. if (LI->isVolatile()) AS.setVolatile();
  309. }
  310. void AliasSetTracker::add(StoreInst *SI) {
  311. if (isStrongerThanMonotonic(SI->getOrdering())) return addUnknown(SI);
  312. AliasSet &AS = addPointer(MemoryLocation::get(SI), AliasSet::ModAccess);
  313. if (SI->isVolatile()) AS.setVolatile();
  314. }
  315. void AliasSetTracker::add(VAArgInst *VAAI) {
  316. addPointer(MemoryLocation::get(VAAI), AliasSet::ModRefAccess);
  317. }
  318. void AliasSetTracker::add(AnyMemSetInst *MSI) {
  319. auto MemLoc = MemoryLocation::getForDest(MSI);
  320. AliasSet &AS = addPointer(MemLoc, AliasSet::ModAccess);
  321. auto *MS = dyn_cast<MemSetInst>(MSI);
  322. if (MS && MS->isVolatile())
  323. AS.setVolatile();
  324. }
  325. void AliasSetTracker::add(AnyMemTransferInst *MTI) {
  326. auto SrcLoc = MemoryLocation::getForSource(MTI);
  327. AliasSet &ASSrc = addPointer(SrcLoc, AliasSet::RefAccess);
  328. auto DestLoc = MemoryLocation::getForDest(MTI);
  329. AliasSet &ASDst = addPointer(DestLoc, AliasSet::ModAccess);
  330. auto* MT = dyn_cast<MemTransferInst>(MTI);
  331. if (MT && MT->isVolatile()) {
  332. ASSrc.setVolatile();
  333. ASDst.setVolatile();
  334. }
  335. }
  336. void AliasSetTracker::addUnknown(Instruction *Inst) {
  337. if (isa<DbgInfoIntrinsic>(Inst))
  338. return; // Ignore DbgInfo Intrinsics.
  339. if (auto *II = dyn_cast<IntrinsicInst>(Inst)) {
  340. // These intrinsics will show up as affecting memory, but they are just
  341. // markers.
  342. switch (II->getIntrinsicID()) {
  343. default:
  344. break;
  345. // FIXME: Add lifetime/invariant intrinsics (See: PR30807).
  346. case Intrinsic::assume:
  347. case Intrinsic::sideeffect:
  348. return;
  349. }
  350. }
  351. if (!Inst->mayReadOrWriteMemory())
  352. return; // doesn't alias anything
  353. AliasSet *AS = findAliasSetForUnknownInst(Inst);
  354. if (AS) {
  355. AS->addUnknownInst(Inst, AA);
  356. return;
  357. }
  358. AliasSets.push_back(new AliasSet());
  359. AS = &AliasSets.back();
  360. AS->addUnknownInst(Inst, AA);
  361. }
  362. void AliasSetTracker::add(Instruction *I) {
  363. // Dispatch to one of the other add methods.
  364. if (LoadInst *LI = dyn_cast<LoadInst>(I))
  365. return add(LI);
  366. if (StoreInst *SI = dyn_cast<StoreInst>(I))
  367. return add(SI);
  368. if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
  369. return add(VAAI);
  370. if (AnyMemSetInst *MSI = dyn_cast<AnyMemSetInst>(I))
  371. return add(MSI);
  372. if (AnyMemTransferInst *MTI = dyn_cast<AnyMemTransferInst>(I))
  373. return add(MTI);
  374. return addUnknown(I);
  375. }
  376. void AliasSetTracker::add(BasicBlock &BB) {
  377. for (auto &I : BB)
  378. add(&I);
  379. }
  380. void AliasSetTracker::add(const AliasSetTracker &AST) {
  381. assert(&AA == &AST.AA &&
  382. "Merging AliasSetTracker objects with different Alias Analyses!");
  383. // Loop over all of the alias sets in AST, adding the pointers contained
  384. // therein into the current alias sets. This can cause alias sets to be
  385. // merged together in the current AST.
  386. for (const AliasSet &AS : AST) {
  387. if (AS.Forward)
  388. continue; // Ignore forwarding alias sets
  389. // If there are any call sites in the alias set, add them to this AST.
  390. for (unsigned i = 0, e = AS.UnknownInsts.size(); i != e; ++i)
  391. if (auto *Inst = AS.getUnknownInst(i))
  392. add(Inst);
  393. // Loop over all of the pointers in this alias set.
  394. for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) {
  395. AliasSet &NewAS =
  396. addPointer(ASI.getPointer(), ASI.getSize(), ASI.getAAInfo(),
  397. (AliasSet::AccessLattice)AS.Access);
  398. if (AS.isVolatile()) NewAS.setVolatile();
  399. }
  400. }
  401. }
  402. // deleteValue method - This method is used to remove a pointer value from the
  403. // AliasSetTracker entirely. It should be used when an instruction is deleted
  404. // from the program to update the AST. If you don't use this, you would have
  405. // dangling pointers to deleted instructions.
  406. //
  407. void AliasSetTracker::deleteValue(Value *PtrVal) {
  408. // First, look up the PointerRec for this pointer.
  409. PointerMapType::iterator I = PointerMap.find_as(PtrVal);
  410. if (I == PointerMap.end()) return; // Noop
  411. // If we found one, remove the pointer from the alias set it is in.
  412. AliasSet::PointerRec *PtrValEnt = I->second;
  413. AliasSet *AS = PtrValEnt->getAliasSet(*this);
  414. // Unlink and delete from the list of values.
  415. PtrValEnt->eraseFromList();
  416. if (AS->Alias == AliasSet::SetMayAlias) {
  417. AS->SetSize--;
  418. TotalMayAliasSetSize--;
  419. }
  420. // Stop using the alias set.
  421. AS->dropRef(*this);
  422. PointerMap.erase(I);
  423. }
  424. // copyValue - This method should be used whenever a preexisting value in the
  425. // program is copied or cloned, introducing a new value. Note that it is ok for
  426. // clients that use this method to introduce the same value multiple times: if
  427. // the tracker already knows about a value, it will ignore the request.
  428. //
  429. void AliasSetTracker::copyValue(Value *From, Value *To) {
  430. // First, look up the PointerRec for this pointer.
  431. PointerMapType::iterator I = PointerMap.find_as(From);
  432. if (I == PointerMap.end())
  433. return; // Noop
  434. assert(I->second->hasAliasSet() && "Dead entry?");
  435. AliasSet::PointerRec &Entry = getEntryFor(To);
  436. if (Entry.hasAliasSet()) return; // Already in the tracker!
  437. // getEntryFor above may invalidate iterator \c I, so reinitialize it.
  438. I = PointerMap.find_as(From);
  439. // Add it to the alias set it aliases...
  440. AliasSet *AS = I->second->getAliasSet(*this);
  441. AS->addPointer(*this, Entry, I->second->getSize(),
  442. I->second->getAAInfo(),
  443. true);
  444. }
  445. AliasSet &AliasSetTracker::mergeAllAliasSets() {
  446. assert(!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold) &&
  447. "Full merge should happen once, when the saturation threshold is "
  448. "reached");
  449. // Collect all alias sets, so that we can drop references with impunity
  450. // without worrying about iterator invalidation.
  451. std::vector<AliasSet *> ASVector;
  452. ASVector.reserve(SaturationThreshold);
  453. for (iterator I = begin(), E = end(); I != E; I++)
  454. ASVector.push_back(&*I);
  455. // Copy all instructions and pointers into a new set, and forward all other
  456. // sets to it.
  457. AliasSets.push_back(new AliasSet());
  458. AliasAnyAS = &AliasSets.back();
  459. AliasAnyAS->Alias = AliasSet::SetMayAlias;
  460. AliasAnyAS->Access = AliasSet::ModRefAccess;
  461. AliasAnyAS->AliasAny = true;
  462. for (auto Cur : ASVector) {
  463. // If Cur was already forwarding, just forward to the new AS instead.
  464. AliasSet *FwdTo = Cur->Forward;
  465. if (FwdTo) {
  466. Cur->Forward = AliasAnyAS;
  467. AliasAnyAS->addRef();
  468. FwdTo->dropRef(*this);
  469. continue;
  470. }
  471. // Otherwise, perform the actual merge.
  472. AliasAnyAS->mergeSetIn(*Cur, *this);
  473. }
  474. return *AliasAnyAS;
  475. }
  476. AliasSet &AliasSetTracker::addPointer(Value *P, LocationSize Size,
  477. const AAMDNodes &AAInfo,
  478. AliasSet::AccessLattice E) {
  479. AliasSet &AS = getAliasSetForPointer(P, Size, AAInfo);
  480. AS.Access |= E;
  481. if (!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold)) {
  482. // The AST is now saturated. From here on, we conservatively consider all
  483. // pointers to alias each-other.
  484. return mergeAllAliasSets();
  485. }
  486. return AS;
  487. }
  488. //===----------------------------------------------------------------------===//
  489. // AliasSet/AliasSetTracker Printing Support
  490. //===----------------------------------------------------------------------===//
  491. void AliasSet::print(raw_ostream &OS) const {
  492. OS << " AliasSet[" << (const void*)this << ", " << RefCount << "] ";
  493. OS << (Alias == SetMustAlias ? "must" : "may") << " alias, ";
  494. switch (Access) {
  495. case NoAccess: OS << "No access "; break;
  496. case RefAccess: OS << "Ref "; break;
  497. case ModAccess: OS << "Mod "; break;
  498. case ModRefAccess: OS << "Mod/Ref "; break;
  499. default: llvm_unreachable("Bad value for Access!");
  500. }
  501. if (isVolatile()) OS << "[volatile] ";
  502. if (Forward)
  503. OS << " forwarding to " << (void*)Forward;
  504. if (!empty()) {
  505. OS << "Pointers: ";
  506. for (iterator I = begin(), E = end(); I != E; ++I) {
  507. if (I != begin()) OS << ", ";
  508. I.getPointer()->printAsOperand(OS << "(");
  509. OS << ", " << I.getSize() << ")";
  510. }
  511. }
  512. if (!UnknownInsts.empty()) {
  513. OS << "\n " << UnknownInsts.size() << " Unknown instructions: ";
  514. for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
  515. if (i) OS << ", ";
  516. if (auto *I = getUnknownInst(i)) {
  517. if (I->hasName())
  518. I->printAsOperand(OS);
  519. else
  520. I->print(OS);
  521. }
  522. }
  523. }
  524. OS << "\n";
  525. }
  526. void AliasSetTracker::print(raw_ostream &OS) const {
  527. OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
  528. << PointerMap.size() << " pointer values.\n";
  529. for (const AliasSet &AS : *this)
  530. AS.print(OS);
  531. OS << "\n";
  532. }
  533. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  534. LLVM_DUMP_METHOD void AliasSet::dump() const { print(dbgs()); }
  535. LLVM_DUMP_METHOD void AliasSetTracker::dump() const { print(dbgs()); }
  536. #endif
  537. //===----------------------------------------------------------------------===//
  538. // ASTCallbackVH Class Implementation
  539. //===----------------------------------------------------------------------===//
  540. void AliasSetTracker::ASTCallbackVH::deleted() {
  541. assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
  542. AST->deleteValue(getValPtr());
  543. // this now dangles!
  544. }
  545. void AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(Value *V) {
  546. AST->copyValue(getValPtr(), V);
  547. }
  548. AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
  549. : CallbackVH(V), AST(ast) {}
  550. AliasSetTracker::ASTCallbackVH &
  551. AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
  552. return *this = ASTCallbackVH(V, AST);
  553. }
  554. //===----------------------------------------------------------------------===//
  555. // AliasSetPrinter Pass
  556. //===----------------------------------------------------------------------===//
  557. namespace {
  558. class AliasSetPrinter : public FunctionPass {
  559. AliasSetTracker *Tracker;
  560. public:
  561. static char ID; // Pass identification, replacement for typeid
  562. AliasSetPrinter() : FunctionPass(ID) {
  563. initializeAliasSetPrinterPass(*PassRegistry::getPassRegistry());
  564. }
  565. void getAnalysisUsage(AnalysisUsage &AU) const override {
  566. AU.setPreservesAll();
  567. AU.addRequired<AAResultsWrapperPass>();
  568. }
  569. bool runOnFunction(Function &F) override {
  570. auto &AAWP = getAnalysis<AAResultsWrapperPass>();
  571. Tracker = new AliasSetTracker(AAWP.getAAResults());
  572. errs() << "Alias sets for function '" << F.getName() << "':\n";
  573. for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
  574. Tracker->add(&*I);
  575. Tracker->print(errs());
  576. delete Tracker;
  577. return false;
  578. }
  579. };
  580. } // end anonymous namespace
  581. char AliasSetPrinter::ID = 0;
  582. INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets",
  583. "Alias Set Printer", false, true)
  584. INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
  585. INITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets",
  586. "Alias Set Printer", false, true)