2
0

AliasSetTracker.cpp 22 KB

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