blob: f6ad704cc914a4172a2018efca1f099d9c900942 [file] [log] [blame]
Chris Lattner009cc3d2002-09-26 21:49:07 +00001//===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner009cc3d2002-09-26 21:49:07 +00009//
10// This file implements the AliasSetTracker and AliasSet classes.
Misha Brukman2b37d7c2005-04-21 21:13:18 +000011//
Chris Lattner009cc3d2002-09-26 21:49:07 +000012//===----------------------------------------------------------------------===//
13
14#include "llvm/Analysis/AliasSetTracker.h"
15#include "llvm/Analysis/AliasAnalysis.h"
Max Kazantsev187d9182018-08-30 03:39:16 +000016#include "llvm/Analysis/GuardUtils.h"
Eugene Zelenko4114bcf2017-07-24 23:16:33 +000017#include "llvm/Analysis/MemoryLocation.h"
Nico Weber0f38c602018-04-30 14:59:11 +000018#include "llvm/Config/llvm-config.h"
Eugene Zelenko4114bcf2017-07-24 23:16:33 +000019#include "llvm/IR/Constants.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000020#include "llvm/IR/DataLayout.h"
Eugene Zelenko4114bcf2017-07-24 23:16:33 +000021#include "llvm/IR/Function.h"
Chandler Carruth876ac602014-03-04 10:30:26 +000022#include "llvm/IR/InstIterator.h"
Eugene Zelenko4114bcf2017-07-24 23:16:33 +000023#include "llvm/IR/Instruction.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000024#include "llvm/IR/Instructions.h"
25#include "llvm/IR/IntrinsicInst.h"
Chandler Carruthe3e43d92017-06-06 11:49:48 +000026#include "llvm/IR/Module.h"
Max Kazantsev9e9c1c42018-08-15 06:21:02 +000027#include "llvm/IR/PatternMatch.h"
Eugene Zelenko4114bcf2017-07-24 23:16:33 +000028#include "llvm/IR/Value.h"
Chris Lattner9971ac42003-02-24 20:37:56 +000029#include "llvm/Pass.h"
Eugene Zelenko4114bcf2017-07-24 23:16:33 +000030#include "llvm/Support/AtomicOrdering.h"
31#include "llvm/Support/Casting.h"
32#include "llvm/Support/CommandLine.h"
33#include "llvm/Support/Compiler.h"
David Greene43c48ac2009-12-23 19:27:59 +000034#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000035#include "llvm/Support/ErrorHandling.h"
Chris Lattner791102f2009-08-23 05:17:37 +000036#include "llvm/Support/raw_ostream.h"
Eugene Zelenko4114bcf2017-07-24 23:16:33 +000037#include <cassert>
38#include <cstdint>
39#include <vector>
40
Chris Lattnere2609242003-12-14 04:52:11 +000041using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000042
Michael Kuperstein15095e42016-08-19 17:05:22 +000043static cl::opt<unsigned>
44 SaturationThreshold("alias-set-saturation-threshold", cl::Hidden,
45 cl::init(250),
46 cl::desc("The maximum number of pointers may-alias "
47 "sets may contain before degradation"));
48
Chris Lattnercc8d5242004-11-27 18:37:42 +000049/// mergeSetIn - Merge the specified alias set into this alias set.
Chris Lattner009cc3d2002-09-26 21:49:07 +000050///
Chris Lattnercc8d5242004-11-27 18:37:42 +000051void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
Chris Lattner9971ac42003-02-24 20:37:56 +000052 assert(!AS.Forward && "Alias set is already forwarding!");
53 assert(!Forward && "This set is a forwarding set!!");
Chris Lattner009cc3d2002-09-26 21:49:07 +000054
Michael Kuperstein15095e42016-08-19 17:05:22 +000055 bool WasMustAlias = (Alias == SetMustAlias);
Chris Lattner009cc3d2002-09-26 21:49:07 +000056 // Update the alias and access types of this set...
Chandler Carruthab6ddad2015-06-22 02:12:52 +000057 Access |= AS.Access;
58 Alias |= AS.Alias;
Chris Lattner9971ac42003-02-24 20:37:56 +000059
Chandler Carruthab6ddad2015-06-22 02:12:52 +000060 if (Alias == SetMustAlias) {
Chris Lattnercc8d5242004-11-27 18:37:42 +000061 // Check that these two merged sets really are must aliases. Since both
62 // used to be must-alias sets, we can just check any pointer from each set
63 // for aliasing.
64 AliasAnalysis &AA = AST.getAliasAnalysis();
Chris Lattnerd7168dd2009-03-09 05:11:09 +000065 PointerRec *L = getSomePointer();
66 PointerRec *R = AS.getSomePointer();
Chris Lattnercc8d5242004-11-27 18:37:42 +000067
68 // If the pointers are not a must-alias pair, this set becomes a may alias.
Chandler Carruth4d7ed392015-06-17 07:18:54 +000069 if (AA.alias(MemoryLocation(L->getValue(), L->getSize(), L->getAAInfo()),
70 MemoryLocation(R->getValue(), R->getSize(), R->getAAInfo())) !=
Chandler Carruth1e3557d2015-06-22 02:16:51 +000071 MustAlias)
Chandler Carruthab6ddad2015-06-22 02:12:52 +000072 Alias = SetMayAlias;
Chris Lattnercc8d5242004-11-27 18:37:42 +000073 }
74
Michael Kuperstein15095e42016-08-19 17:05:22 +000075 if (Alias == SetMayAlias) {
76 if (WasMustAlias)
77 AST.TotalMayAliasSetSize += size();
78 if (AS.Alias == SetMustAlias)
79 AST.TotalMayAliasSetSize += AS.size();
80 }
81
David Majnemera44c5012014-11-19 19:36:18 +000082 bool ASHadUnknownInsts = !AS.UnknownInsts.empty();
Eli Friedman6f3ba372011-07-27 00:46:46 +000083 if (UnknownInsts.empty()) { // Merge call sites...
David Majnemera44c5012014-11-19 19:36:18 +000084 if (ASHadUnknownInsts) {
Eli Friedman6f3ba372011-07-27 00:46:46 +000085 std::swap(UnknownInsts, AS.UnknownInsts);
David Majnemerf47d3252014-11-19 09:41:05 +000086 addRef();
87 }
David Majnemera44c5012014-11-19 19:36:18 +000088 } else if (ASHadUnknownInsts) {
Eli Friedman6f3ba372011-07-27 00:46:46 +000089 UnknownInsts.insert(UnknownInsts.end(), AS.UnknownInsts.begin(), AS.UnknownInsts.end());
90 AS.UnknownInsts.clear();
Chris Lattner9971ac42003-02-24 20:37:56 +000091 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000092
Michael Kuperstein15095e42016-08-19 17:05:22 +000093 AS.Forward = this; // Forward across AS now...
94 addRef(); // AS is now pointing to us...
Chris Lattner9971ac42003-02-24 20:37:56 +000095
96 // Merge the list of constituent pointers...
Chris Lattner2cffeec2003-12-18 08:11:56 +000097 if (AS.PtrList) {
Michael Kuperstein15095e42016-08-19 17:05:22 +000098 SetSize += AS.size();
99 AS.SetSize = 0;
Chris Lattner2cffeec2003-12-18 08:11:56 +0000100 *PtrListEnd = AS.PtrList;
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000101 AS.PtrList->setPrevInList(PtrListEnd);
Chris Lattner2cffeec2003-12-18 08:11:56 +0000102 PtrListEnd = AS.PtrListEnd;
103
Craig Topper570e52c2014-04-15 04:59:12 +0000104 AS.PtrList = nullptr;
Chris Lattner2cffeec2003-12-18 08:11:56 +0000105 AS.PtrListEnd = &AS.PtrList;
Craig Topper570e52c2014-04-15 04:59:12 +0000106 assert(*AS.PtrListEnd == nullptr && "End of list is not null?");
Chris Lattner2cffeec2003-12-18 08:11:56 +0000107 }
David Majnemerf47d3252014-11-19 09:41:05 +0000108 if (ASHadUnknownInsts)
109 AS.dropRef(AST);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000110}
111
Chris Lattner9971ac42003-02-24 20:37:56 +0000112void AliasSetTracker::removeAliasSet(AliasSet *AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000113 if (AliasSet *Fwd = AS->Forward) {
114 Fwd->dropRef(*this);
Craig Topper570e52c2014-04-15 04:59:12 +0000115 AS->Forward = nullptr;
Alina Sbirlea782574b2018-11-01 23:37:51 +0000116 } else // Update TotalMayAliasSetSize only if not forwarding.
117 if (AS->Alias == AliasSet::SetMayAlias)
118 TotalMayAliasSetSize -= AS->size();
Michael Kuperstein15095e42016-08-19 17:05:22 +0000119
Chris Lattner9971ac42003-02-24 20:37:56 +0000120 AliasSets.erase(AS);
121}
122
123void AliasSet::removeFromTracker(AliasSetTracker &AST) {
124 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
125 AST.removeAliasSet(this);
126}
127
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000128void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
George Burgess IV3bf82982018-05-25 21:16:58 +0000129 LocationSize Size, const AAMDNodes &AAInfo,
Hal Finkel5b601e12015-10-28 22:13:41 +0000130 bool KnownMustAlias) {
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000131 assert(!Entry.hasAliasSet() && "Entry already in set!");
Chris Lattner9971ac42003-02-24 20:37:56 +0000132
Chris Lattnerb66e6482004-09-14 19:15:32 +0000133 // Check to see if we have to downgrade to _may_ alias.
134 if (isMustAlias() && !KnownMustAlias)
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000135 if (PointerRec *P = getSomePointer()) {
Chris Lattner3080b602004-09-15 16:59:47 +0000136 AliasAnalysis &AA = AST.getAliasAnalysis();
Chandler Carruth1e3557d2015-06-22 02:16:51 +0000137 AliasResult Result =
Chandler Carruth4d7ed392015-06-17 07:18:54 +0000138 AA.alias(MemoryLocation(P->getValue(), P->getSize(), P->getAAInfo()),
139 MemoryLocation(Entry.getValue(), Size, AAInfo));
Michael Kuperstein15095e42016-08-19 17:05:22 +0000140 if (Result != MustAlias) {
Chandler Carruthab6ddad2015-06-22 02:12:52 +0000141 Alias = SetMayAlias;
Michael Kuperstein15095e42016-08-19 17:05:22 +0000142 AST.TotalMayAliasSetSize += size();
143 } else {
Fangrui Songaf7b1832018-07-30 19:41:25 +0000144 // First entry of must alias must have maximum size!
Hal Finkel2c7c54c2014-07-24 12:16:19 +0000145 P->updateSizeAndAAInfo(Size, AAInfo);
Michael Kuperstein15095e42016-08-19 17:05:22 +0000146 }
Chandler Carruth1e3557d2015-06-22 02:16:51 +0000147 assert(Result != NoAlias && "Cannot be part of must set!");
Chris Lattner31a9d182003-02-26 22:11:00 +0000148 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000149
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000150 Entry.setAliasSet(this);
Hal Finkel2c7c54c2014-07-24 12:16:19 +0000151 Entry.updateSizeAndAAInfo(Size, AAInfo);
Chris Lattner9971ac42003-02-24 20:37:56 +0000152
153 // Add it to the end of the list...
Michael Kuperstein15095e42016-08-19 17:05:22 +0000154 ++SetSize;
Craig Topper570e52c2014-04-15 04:59:12 +0000155 assert(*PtrListEnd == nullptr && "End of list is not null?");
Chris Lattner2cffeec2003-12-18 08:11:56 +0000156 *PtrListEnd = &Entry;
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000157 PtrListEnd = Entry.setPrevInList(PtrListEnd);
Craig Topper570e52c2014-04-15 04:59:12 +0000158 assert(*PtrListEnd == nullptr && "End of list is not null?");
Michael Kuperstein15095e42016-08-19 17:05:22 +0000159 // Entry points to alias set.
160 addRef();
161
162 if (Alias == SetMayAlias)
163 AST.TotalMayAliasSetSize++;
Chris Lattner9971ac42003-02-24 20:37:56 +0000164}
165
Hal Finkel5b601e12015-10-28 22:13:41 +0000166void AliasSet::addUnknownInst(Instruction *I, AliasAnalysis &AA) {
David Majnemerf47d3252014-11-19 09:41:05 +0000167 if (UnknownInsts.empty())
168 addRef();
Benjamin Kramer9589ff82015-05-29 19:43:39 +0000169 UnknownInsts.emplace_back(I);
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000170
Max Kazantsev9e9c1c42018-08-15 06:21:02 +0000171 // Guards are marked as modifying memory for control flow modelling purposes,
172 // but don't actually modify any specific memory location.
173 using namespace PatternMatch;
Max Kazantsev187d9182018-08-30 03:39:16 +0000174 bool MayWriteMemory = I->mayWriteToMemory() && !isGuard(I) &&
Philip Reamesee5b70c2018-08-21 00:55:35 +0000175 !(I->use_empty() && match(I, m_Intrinsic<Intrinsic::invariant_start>()));
Max Kazantsev9e9c1c42018-08-15 06:21:02 +0000176 if (!MayWriteMemory) {
Chandler Carruthab6ddad2015-06-22 02:12:52 +0000177 Alias = SetMayAlias;
Hal Finkel5b601e12015-10-28 22:13:41 +0000178 Access |= RefAccess;
Duncan Sandsdff67102007-12-01 07:51:45 +0000179 return;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000180 }
181
Hal Finkel5b601e12015-10-28 22:13:41 +0000182 // FIXME: This should use mod/ref information to make this not suck so bad
Chandler Carruthab6ddad2015-06-22 02:12:52 +0000183 Alias = SetMayAlias;
Hal Finkel5b601e12015-10-28 22:13:41 +0000184 Access = ModRefAccess;
Chris Lattner9971ac42003-02-24 20:37:56 +0000185}
186
187/// aliasesPointer - Return true if the specified pointer "may" (or must)
Chris Lattner009cc3d2002-09-26 21:49:07 +0000188/// alias one of the members in the set.
189///
George Burgess IV3bf82982018-05-25 21:16:58 +0000190bool AliasSet::aliasesPointer(const Value *Ptr, LocationSize Size,
Hal Finkel2c7c54c2014-07-24 12:16:19 +0000191 const AAMDNodes &AAInfo,
Hal Finkel5b601e12015-10-28 22:13:41 +0000192 AliasAnalysis &AA) const {
Michael Kuperstein15095e42016-08-19 17:05:22 +0000193 if (AliasAny)
194 return true;
195
Chandler Carruthab6ddad2015-06-22 02:12:52 +0000196 if (Alias == SetMustAlias) {
Eli Friedman6f3ba372011-07-27 00:46:46 +0000197 assert(UnknownInsts.empty() && "Illegal must alias set!");
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000198
Chris Lattner9971ac42003-02-24 20:37:56 +0000199 // If this is a set of MustAliases, only check to see if the pointer aliases
Chris Lattner9476d742010-08-29 04:13:43 +0000200 // SOME value in the set.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000201 PointerRec *SomePtr = getSomePointer();
Chris Lattner9971ac42003-02-24 20:37:56 +0000202 assert(SomePtr && "Empty must-alias set??");
Chandler Carruth4d7ed392015-06-17 07:18:54 +0000203 return AA.alias(MemoryLocation(SomePtr->getValue(), SomePtr->getSize(),
204 SomePtr->getAAInfo()),
205 MemoryLocation(Ptr, Size, AAInfo));
Chris Lattner9971ac42003-02-24 20:37:56 +0000206 }
207
208 // If this is a may-alias set, we have to check all of the pointers in the set
209 // to be sure it doesn't alias the set...
210 for (iterator I = begin(), E = end(); I != E; ++I)
Chandler Carruth4d7ed392015-06-17 07:18:54 +0000211 if (AA.alias(MemoryLocation(Ptr, Size, AAInfo),
212 MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo())))
Chris Lattner9971ac42003-02-24 20:37:56 +0000213 return true;
214
Eli Friedman6f3ba372011-07-27 00:46:46 +0000215 // Check the unknown instructions...
216 if (!UnknownInsts.empty()) {
217 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i)
Sanjoy Dasc32a5732017-03-11 01:15:48 +0000218 if (auto *Inst = getUnknownInst(i))
Alina Sbirleaa2d30e92017-12-05 20:12:23 +0000219 if (isModOrRefSet(
220 AA.getModRefInfo(Inst, MemoryLocation(Ptr, Size, AAInfo))))
Sanjoy Dasc32a5732017-03-11 01:15:48 +0000221 return true;
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000222 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000223
Hal Finkel5b601e12015-10-28 22:13:41 +0000224 return false;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000225}
226
Pete Coopera0706f52015-05-13 01:12:12 +0000227bool AliasSet::aliasesUnknownInst(const Instruction *Inst,
Hal Finkel5b601e12015-10-28 22:13:41 +0000228 AliasAnalysis &AA) const {
Michael Kuperstein15095e42016-08-19 17:05:22 +0000229
230 if (AliasAny)
231 return true;
232
Alina Sbirlea782574b2018-11-01 23:37:51 +0000233 assert(Inst->mayReadOrWriteMemory() &&
234 "Instruction must either read or write memory.");
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000235
Eli Friedman6f3ba372011-07-27 00:46:46 +0000236 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
Xin Tong5b97b272017-06-25 12:55:11 +0000237 if (auto *UnknownInst = getUnknownInst(i)) {
Chandler Carruth81aa7122019-01-07 05:42:51 +0000238 const auto *C1 = dyn_cast<CallBase>(UnknownInst);
239 const auto *C2 = dyn_cast<CallBase>(Inst);
Alina Sbirleaa2d30e92017-12-05 20:12:23 +0000240 if (!C1 || !C2 || isModOrRefSet(AA.getModRefInfo(C1, C2)) ||
241 isModOrRefSet(AA.getModRefInfo(C2, C1)))
Sanjoy Dasc32a5732017-03-11 01:15:48 +0000242 return true;
243 }
Chris Lattnercb7f6532010-08-29 18:42:23 +0000244 }
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000245
Hal Finkel5b601e12015-10-28 22:13:41 +0000246 for (iterator I = begin(), E = end(); I != E; ++I)
Alina Sbirleaa2d30e92017-12-05 20:12:23 +0000247 if (isModOrRefSet(AA.getModRefInfo(
248 Inst, MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo()))))
Hal Finkel5b601e12015-10-28 22:13:41 +0000249 return true;
250
251 return false;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000252}
253
Philip Reames3df4cb12018-08-22 03:32:52 +0000254Instruction* AliasSet::getUniqueInstruction() {
Philip Reames3df4cb12018-08-22 03:32:52 +0000255 if (AliasAny)
256 // May have collapses alias set
257 return nullptr;
Philip Reamesa9f18e92018-08-29 21:49:30 +0000258 if (begin() != end()) {
259 if (!UnknownInsts.empty())
260 // Another instruction found
261 return nullptr;
262 if (std::next(begin()) != end())
263 // Another instruction found
264 return nullptr;
265 Value *Addr = begin()->getValue();
266 assert(!Addr->user_empty() &&
267 "where's the instruction which added this pointer?");
268 if (std::next(Addr->user_begin()) != Addr->user_end())
269 // Another instruction found -- this is really restrictive
270 // TODO: generalize!
271 return nullptr;
272 return cast<Instruction>(*(Addr->user_begin()));
273 }
Philip Reames52c037b2018-08-22 03:36:42 +0000274 if (1 != UnknownInsts.size())
Philip Reames3df4cb12018-08-22 03:32:52 +0000275 return nullptr;
276 return cast<Instruction>(UnknownInsts[0]);
277}
278
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000279void AliasSetTracker::clear() {
280 // Delete all the PointerRec entries.
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000281 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
282 I != E; ++I)
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000283 I->second->eraseFromList();
Fangrui Songaf7b1832018-07-30 19:41:25 +0000284
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000285 PointerMap.clear();
Fangrui Songaf7b1832018-07-30 19:41:25 +0000286
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000287 // The alias sets should all be clear now.
288 AliasSets.clear();
289}
290
Chris Lattner009cc3d2002-09-26 21:49:07 +0000291
Michael Kupersteinf66aab62016-04-14 22:00:11 +0000292/// mergeAliasSetsForPointer - Given a pointer, merge all alias sets that may
293/// alias the pointer. Return the unified set, or nullptr if no set that aliases
294/// the pointer was found.
295AliasSet *AliasSetTracker::mergeAliasSetsForPointer(const Value *Ptr,
George Burgess IV3bf82982018-05-25 21:16:58 +0000296 LocationSize Size,
Michael Kupersteinf66aab62016-04-14 22:00:11 +0000297 const AAMDNodes &AAInfo) {
Craig Topper570e52c2014-04-15 04:59:12 +0000298 AliasSet *FoundSet = nullptr;
David Majnemerf47d3252014-11-19 09:41:05 +0000299 for (iterator I = begin(), E = end(); I != E;) {
300 iterator Cur = I++;
Hal Finkel5b601e12015-10-28 22:13:41 +0000301 if (Cur->Forward || !Cur->aliasesPointer(Ptr, Size, AAInfo, AA)) continue;
Fangrui Songaf7b1832018-07-30 19:41:25 +0000302
Craig Topper570e52c2014-04-15 04:59:12 +0000303 if (!FoundSet) { // If this is the first alias set ptr can go into.
Duncan P. N. Exon Smithd3a5adc2015-10-10 00:53:03 +0000304 FoundSet = &*Cur; // Remember it.
Chris Lattner6e1f5102010-08-29 04:06:55 +0000305 } else { // Otherwise, we must merge the sets.
David Majnemerf47d3252014-11-19 09:41:05 +0000306 FoundSet->mergeSetIn(*Cur, *this); // Merge in contents.
Chris Lattner009cc3d2002-09-26 21:49:07 +0000307 }
Chris Lattner6e1f5102010-08-29 04:06:55 +0000308 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000309
310 return FoundSet;
311}
312
Hal Finkel5b601e12015-10-28 22:13:41 +0000313AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) {
Craig Topper570e52c2014-04-15 04:59:12 +0000314 AliasSet *FoundSet = nullptr;
David Majnemerf47d3252014-11-19 09:41:05 +0000315 for (iterator I = begin(), E = end(); I != E;) {
316 iterator Cur = I++;
Hal Finkel5b601e12015-10-28 22:13:41 +0000317 if (Cur->Forward || !Cur->aliasesUnknownInst(Inst, AA))
Chris Lattner6e1f5102010-08-29 04:06:55 +0000318 continue;
Craig Topper570e52c2014-04-15 04:59:12 +0000319 if (!FoundSet) // If this is the first alias set ptr can go into.
Duncan P. N. Exon Smithd3a5adc2015-10-10 00:53:03 +0000320 FoundSet = &*Cur; // Remember it.
Alina Sbirlea782574b2018-11-01 23:37:51 +0000321 else // Otherwise, we must merge the sets.
David Majnemerf47d3252014-11-19 09:41:05 +0000322 FoundSet->mergeSetIn(*Cur, *this); // Merge in contents.
Chris Lattner6e1f5102010-08-29 04:06:55 +0000323 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000324 return FoundSet;
325}
326
Philip Reames4a208882018-08-16 20:11:15 +0000327AliasSet &AliasSetTracker::getAliasSetFor(const MemoryLocation &MemLoc) {
328
329 Value * const Pointer = const_cast<Value*>(MemLoc.Ptr);
330 const LocationSize Size = MemLoc.Size;
331 const AAMDNodes &AAInfo = MemLoc.AATags;
332
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000333 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
Chris Lattner9971ac42003-02-24 20:37:56 +0000334
Michael Kuperstein15095e42016-08-19 17:05:22 +0000335 if (AliasAnyAS) {
336 // At this point, the AST is saturated, so we only have one active alias
337 // set. That means we already know which alias set we want to return, and
338 // just need to add the pointer to that set to keep the data structure
339 // consistent.
340 // This, of course, means that we will never need a merge here.
341 if (Entry.hasAliasSet()) {
342 Entry.updateSizeAndAAInfo(Size, AAInfo);
343 assert(Entry.getAliasSet(*this) == AliasAnyAS &&
344 "Entry in saturated AST must belong to only alias set");
345 } else {
346 AliasAnyAS->addPointer(*this, Entry, Size, AAInfo);
347 }
348 return *AliasAnyAS;
349 }
350
Chris Lattner9476d742010-08-29 04:13:43 +0000351 // Check to see if the pointer is already known.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000352 if (Entry.hasAliasSet()) {
Michael Kupersteinf66aab62016-04-14 22:00:11 +0000353 // If the size changed, we may need to merge several alias sets.
354 // Note that we can *not* return the result of mergeAliasSetsForPointer
355 // due to a quirk of alias analysis behavior. Since alias(undef, undef)
356 // is NoAlias, mergeAliasSetsForPointer(undef, ...) will not find the
357 // the right set for undef, even if it exists.
358 if (Entry.updateSizeAndAAInfo(Size, AAInfo))
359 mergeAliasSetsForPointer(Pointer, Size, AAInfo);
Chris Lattner9971ac42003-02-24 20:37:56 +0000360 // Return the set!
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000361 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000362 }
Fangrui Songaf7b1832018-07-30 19:41:25 +0000363
Michael Kupersteinf66aab62016-04-14 22:00:11 +0000364 if (AliasSet *AS = mergeAliasSetsForPointer(Pointer, Size, AAInfo)) {
Chris Lattner9476d742010-08-29 04:13:43 +0000365 // Add it to the alias set it aliases.
Hal Finkel5b601e12015-10-28 22:13:41 +0000366 AS->addPointer(*this, Entry, Size, AAInfo);
Chris Lattner9971ac42003-02-24 20:37:56 +0000367 return *AS;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000368 }
Fangrui Songaf7b1832018-07-30 19:41:25 +0000369
Chris Lattner9476d742010-08-29 04:13:43 +0000370 // Otherwise create a new alias set to hold the loaded pointer.
Chris Lattner6e1f5102010-08-29 04:06:55 +0000371 AliasSets.push_back(new AliasSet());
Hal Finkel5b601e12015-10-28 22:13:41 +0000372 AliasSets.back().addPointer(*this, Entry, Size, AAInfo);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000373 return AliasSets.back();
Chris Lattner009cc3d2002-09-26 21:49:07 +0000374}
375
George Burgess IV3bf82982018-05-25 21:16:58 +0000376void AliasSetTracker::add(Value *Ptr, LocationSize Size,
377 const AAMDNodes &AAInfo) {
Alina Sbirlea6be73e72018-10-29 22:25:59 +0000378 addPointer(MemoryLocation(Ptr, Size, AAInfo), AliasSet::NoAccess);
Chris Lattner61d46272004-07-26 05:50:23 +0000379}
380
Chad Rosier6d823ec2016-10-19 18:50:32 +0000381void AliasSetTracker::add(LoadInst *LI) {
Philip Reames80d595c2018-08-22 19:30:46 +0000382 if (isStrongerThanMonotonic(LI->getOrdering()))
383 return addUnknown(LI);
Philip Reames0ff071a2018-08-21 17:59:11 +0000384 addPointer(MemoryLocation::get(LI), AliasSet::RefAccess);
Chris Lattner9971ac42003-02-24 20:37:56 +0000385}
386
Chad Rosier6d823ec2016-10-19 18:50:32 +0000387void AliasSetTracker::add(StoreInst *SI) {
Philip Reames80d595c2018-08-22 19:30:46 +0000388 if (isStrongerThanMonotonic(SI->getOrdering()))
389 return addUnknown(SI);
Philip Reames0ff071a2018-08-21 17:59:11 +0000390 addPointer(MemoryLocation::get(SI), AliasSet::ModAccess);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000391}
392
Chad Rosier6d823ec2016-10-19 18:50:32 +0000393void AliasSetTracker::add(VAArgInst *VAAI) {
Philip Reames20a211a2018-08-13 22:34:14 +0000394 addPointer(MemoryLocation::get(VAAI), AliasSet::ModRefAccess);
Dan Gohman235fc572008-04-14 18:34:50 +0000395}
396
Daniel Neilson136d97e2018-05-30 14:43:39 +0000397void AliasSetTracker::add(AnyMemSetInst *MSI) {
Philip Reames80d595c2018-08-22 19:30:46 +0000398 addPointer(MemoryLocation::getForDest(MSI), AliasSet::ModAccess);
Haicheng Wu5ce6e322016-02-17 02:01:50 +0000399}
Chris Lattnere2609242003-12-14 04:52:11 +0000400
Daniel Neilson136d97e2018-05-30 14:43:39 +0000401void AliasSetTracker::add(AnyMemTransferInst *MTI) {
Philip Reames80d595c2018-08-22 19:30:46 +0000402 addPointer(MemoryLocation::getForDest(MTI), AliasSet::ModAccess);
Philip Reames3e626732018-09-10 16:00:27 +0000403 addPointer(MemoryLocation::getForSource(MTI), AliasSet::RefAccess);
Chad Rosierdb638de2016-10-19 19:09:03 +0000404}
405
Chad Rosier6d823ec2016-10-19 18:50:32 +0000406void AliasSetTracker::addUnknown(Instruction *Inst) {
407 if (isa<DbgInfoIntrinsic>(Inst))
408 return; // Ignore DbgInfo Intrinsics.
Chad Rosier4438f462016-11-07 14:11:45 +0000409
410 if (auto *II = dyn_cast<IntrinsicInst>(Inst)) {
411 // These intrinsics will show up as affecting memory, but they are just
412 // markers.
413 switch (II->getIntrinsicID()) {
414 default:
415 break;
416 // FIXME: Add lifetime/invariant intrinsics (See: PR30807).
417 case Intrinsic::assume:
Dan Gohmanb5e0bec2017-11-08 21:59:51 +0000418 case Intrinsic::sideeffect:
Chad Rosier4438f462016-11-07 14:11:45 +0000419 return;
420 }
421 }
Eli Friedman6f3ba372011-07-27 00:46:46 +0000422 if (!Inst->mayReadOrWriteMemory())
Chad Rosier6d823ec2016-10-19 18:50:32 +0000423 return; // doesn't alias anything
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000424
Hal Finkel5b601e12015-10-28 22:13:41 +0000425 AliasSet *AS = findAliasSetForUnknownInst(Inst);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000426 if (AS) {
Hal Finkel5b601e12015-10-28 22:13:41 +0000427 AS->addUnknownInst(Inst, AA);
Chad Rosier6d823ec2016-10-19 18:50:32 +0000428 return;
Chris Lattner9971ac42003-02-24 20:37:56 +0000429 }
Chris Lattner6e1f5102010-08-29 04:06:55 +0000430 AliasSets.push_back(new AliasSet());
431 AS = &AliasSets.back();
Hal Finkel5b601e12015-10-28 22:13:41 +0000432 AS->addUnknownInst(Inst, AA);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000433}
434
Chad Rosier6d823ec2016-10-19 18:50:32 +0000435void AliasSetTracker::add(Instruction *I) {
Chris Lattner6e1f5102010-08-29 04:06:55 +0000436 // Dispatch to one of the other add methods.
Chris Lattner9971ac42003-02-24 20:37:56 +0000437 if (LoadInst *LI = dyn_cast<LoadInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000438 return add(LI);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000439 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000440 return add(SI);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000441 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
Dan Gohman235fc572008-04-14 18:34:50 +0000442 return add(VAAI);
Daniel Neilson136d97e2018-05-30 14:43:39 +0000443 if (AnyMemSetInst *MSI = dyn_cast<AnyMemSetInst>(I))
Haicheng Wu5ce6e322016-02-17 02:01:50 +0000444 return add(MSI);
Daniel Neilson136d97e2018-05-30 14:43:39 +0000445 if (AnyMemTransferInst *MTI = dyn_cast<AnyMemTransferInst>(I))
Chad Rosierdb638de2016-10-19 19:09:03 +0000446 return add(MTI);
Philip Reamesfe1f0fe2018-09-07 21:36:11 +0000447
448 // Handle all calls with known mod/ref sets genericall
Chandler Carruth81aa7122019-01-07 05:42:51 +0000449 if (auto *Call = dyn_cast<CallBase>(I))
450 if (Call->onlyAccessesArgMemory()) {
451 auto getAccessFromModRef = [](ModRefInfo MRI) {
452 if (isRefSet(MRI) && isModSet(MRI))
453 return AliasSet::ModRefAccess;
454 else if (isModSet(MRI))
455 return AliasSet::ModAccess;
456 else if (isRefSet(MRI))
457 return AliasSet::RefAccess;
458 else
459 return AliasSet::NoAccess;
460 };
Philip Reamesfe1f0fe2018-09-07 21:36:11 +0000461
Chandler Carruth81aa7122019-01-07 05:42:51 +0000462 ModRefInfo CallMask = createModRefInfo(AA.getModRefBehavior(Call));
Philip Reamesfe1f0fe2018-09-07 21:36:11 +0000463
Chandler Carruth81aa7122019-01-07 05:42:51 +0000464 // Some intrinsics are marked as modifying memory for control flow
465 // modelling purposes, but don't actually modify any specific memory
466 // location.
467 using namespace PatternMatch;
468 if (Call->use_empty() &&
469 match(Call, m_Intrinsic<Intrinsic::invariant_start>()))
470 CallMask = clearMod(CallMask);
471
472 for (auto IdxArgPair : enumerate(Call->args())) {
473 int ArgIdx = IdxArgPair.index();
474 const Value *Arg = IdxArgPair.value();
475 if (!Arg->getType()->isPointerTy())
476 continue;
477 MemoryLocation ArgLoc =
478 MemoryLocation::getForArgument(Call, ArgIdx, nullptr);
479 ModRefInfo ArgMask = AA.getArgModRefInfo(Call, ArgIdx);
480 ArgMask = intersectModRef(CallMask, ArgMask);
481 if (!isNoModRef(ArgMask))
482 addPointer(ArgLoc, getAccessFromModRef(ArgMask));
483 }
484 return;
Philip Reamesfe1f0fe2018-09-07 21:36:11 +0000485 }
Chandler Carruth81aa7122019-01-07 05:42:51 +0000486
Eli Friedman6f3ba372011-07-27 00:46:46 +0000487 return addUnknown(I);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000488}
489
Chris Lattner319d05b2003-03-03 23:28:05 +0000490void AliasSetTracker::add(BasicBlock &BB) {
Duncan P. N. Exon Smithd3a5adc2015-10-10 00:53:03 +0000491 for (auto &I : BB)
492 add(&I);
Chris Lattner319d05b2003-03-03 23:28:05 +0000493}
494
495void AliasSetTracker::add(const AliasSetTracker &AST) {
496 assert(&AA == &AST.AA &&
497 "Merging AliasSetTracker objects with different Alias Analyses!");
498
499 // Loop over all of the alias sets in AST, adding the pointers contained
500 // therein into the current alias sets. This can cause alias sets to be
501 // merged together in the current AST.
Benjamin Kramer8d0d2b62016-06-26 17:27:42 +0000502 for (const AliasSet &AS : AST) {
503 if (AS.Forward)
504 continue; // Ignore forwarding alias sets
Chris Lattner319d05b2003-03-03 23:28:05 +0000505
Chris Lattner6e1f5102010-08-29 04:06:55 +0000506 // If there are any call sites in the alias set, add them to this AST.
Eli Friedman6f3ba372011-07-27 00:46:46 +0000507 for (unsigned i = 0, e = AS.UnknownInsts.size(); i != e; ++i)
Sanjoy Dasc32a5732017-03-11 01:15:48 +0000508 if (auto *Inst = AS.getUnknownInst(i))
509 add(Inst);
Chris Lattner319d05b2003-03-03 23:28:05 +0000510
Chris Lattner6e1f5102010-08-29 04:06:55 +0000511 // Loop over all of the pointers in this alias set.
Philip Reames0ff071a2018-08-21 17:59:11 +0000512 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI)
Alina Sbirlea6be73e72018-10-29 22:25:59 +0000513 addPointer(
514 MemoryLocation(ASI.getPointer(), ASI.getSize(), ASI.getAAInfo()),
515 (AliasSet::AccessLattice)AS.Access);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000516 }
Chris Lattner319d05b2003-03-03 23:28:05 +0000517}
518
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000519// deleteValue method - This method is used to remove a pointer value from the
Chris Lattner2cffeec2003-12-18 08:11:56 +0000520// AliasSetTracker entirely. It should be used when an instruction is deleted
521// from the program to update the AST. If you don't use this, you would have
522// dangling pointers to deleted instructions.
523//
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000524void AliasSetTracker::deleteValue(Value *PtrVal) {
Chris Lattnerb66e6482004-09-14 19:15:32 +0000525 // First, look up the PointerRec for this pointer.
Benjamin Kramer992c25a2012-06-30 22:37:15 +0000526 PointerMapType::iterator I = PointerMap.find_as(PtrVal);
Chris Lattner2cffeec2003-12-18 08:11:56 +0000527 if (I == PointerMap.end()) return; // Noop
528
529 // If we found one, remove the pointer from the alias set it is in.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000530 AliasSet::PointerRec *PtrValEnt = I->second;
531 AliasSet *AS = PtrValEnt->getAliasSet(*this);
Chris Lattner2cffeec2003-12-18 08:11:56 +0000532
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000533 // Unlink and delete from the list of values.
534 PtrValEnt->eraseFromList();
Michael Kuperstein15095e42016-08-19 17:05:22 +0000535
536 if (AS->Alias == AliasSet::SetMayAlias) {
537 AS->SetSize--;
538 TotalMayAliasSetSize--;
539 }
Fangrui Songaf7b1832018-07-30 19:41:25 +0000540
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000541 // Stop using the alias set.
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000542 AS->dropRef(*this);
Fangrui Songaf7b1832018-07-30 19:41:25 +0000543
Chris Lattner2cffeec2003-12-18 08:11:56 +0000544 PointerMap.erase(I);
545}
546
Chris Lattnerb66e6482004-09-14 19:15:32 +0000547// copyValue - This method should be used whenever a preexisting value in the
548// program is copied or cloned, introducing a new value. Note that it is ok for
549// clients that use this method to introduce the same value multiple times: if
550// the tracker already knows about a value, it will ignore the request.
551//
552void AliasSetTracker::copyValue(Value *From, Value *To) {
Chris Lattnerb66e6482004-09-14 19:15:32 +0000553 // First, look up the PointerRec for this pointer.
Benjamin Kramer992c25a2012-06-30 22:37:15 +0000554 PointerMapType::iterator I = PointerMap.find_as(From);
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000555 if (I == PointerMap.end())
Chris Lattnerb66e6482004-09-14 19:15:32 +0000556 return; // Noop
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000557 assert(I->second->hasAliasSet() && "Dead entry?");
Chris Lattnerb66e6482004-09-14 19:15:32 +0000558
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000559 AliasSet::PointerRec &Entry = getEntryFor(To);
560 if (Entry.hasAliasSet()) return; // Already in the tracker!
Chris Lattnerb66e6482004-09-14 19:15:32 +0000561
Xinliang David Lib9ecebc2016-08-11 23:09:56 +0000562 // getEntryFor above may invalidate iterator \c I, so reinitialize it.
Benjamin Kramer992c25a2012-06-30 22:37:15 +0000563 I = PointerMap.find_as(From);
Xinliang David Lib9ecebc2016-08-11 23:09:56 +0000564 // Add it to the alias set it aliases...
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000565 AliasSet *AS = I->second->getAliasSet(*this);
Dan Gohmanfb8096d2010-10-18 21:28:00 +0000566 AS->addPointer(*this, Entry, I->second->getSize(),
Hal Finkel5b601e12015-10-28 22:13:41 +0000567 I->second->getAAInfo(),
Dan Gohmana8702ea2010-10-18 20:44:50 +0000568 true);
Chris Lattnerb66e6482004-09-14 19:15:32 +0000569}
570
Michael Kuperstein15095e42016-08-19 17:05:22 +0000571AliasSet &AliasSetTracker::mergeAllAliasSets() {
572 assert(!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold) &&
573 "Full merge should happen once, when the saturation threshold is "
574 "reached");
Chris Lattnerb66e6482004-09-14 19:15:32 +0000575
Michael Kuperstein15095e42016-08-19 17:05:22 +0000576 // Collect all alias sets, so that we can drop references with impunity
577 // without worrying about iterator invalidation.
578 std::vector<AliasSet *> ASVector;
579 ASVector.reserve(SaturationThreshold);
580 for (iterator I = begin(), E = end(); I != E; I++)
581 ASVector.push_back(&*I);
582
583 // Copy all instructions and pointers into a new set, and forward all other
584 // sets to it.
585 AliasSets.push_back(new AliasSet());
586 AliasAnyAS = &AliasSets.back();
587 AliasAnyAS->Alias = AliasSet::SetMayAlias;
588 AliasAnyAS->Access = AliasSet::ModRefAccess;
589 AliasAnyAS->AliasAny = true;
590
591 for (auto Cur : ASVector) {
Michael Kuperstein15095e42016-08-19 17:05:22 +0000592 // If Cur was already forwarding, just forward to the new AS instead.
593 AliasSet *FwdTo = Cur->Forward;
594 if (FwdTo) {
595 Cur->Forward = AliasAnyAS;
Alina Sbirleaa2d30e92017-12-05 20:12:23 +0000596 AliasAnyAS->addRef();
Michael Kuperstein15095e42016-08-19 17:05:22 +0000597 FwdTo->dropRef(*this);
598 continue;
599 }
600
601 // Otherwise, perform the actual merge.
602 AliasAnyAS->mergeSetIn(*Cur, *this);
603 }
604
605 return *AliasAnyAS;
606}
607
Alina Sbirlea6be73e72018-10-29 22:25:59 +0000608AliasSet &AliasSetTracker::addPointer(MemoryLocation Loc,
Chad Rosier6d823ec2016-10-19 18:50:32 +0000609 AliasSet::AccessLattice E) {
Alina Sbirlea6be73e72018-10-29 22:25:59 +0000610 AliasSet &AS = getAliasSetFor(Loc);
Michael Kuperstein15095e42016-08-19 17:05:22 +0000611 AS.Access |= E;
612
613 if (!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold)) {
614 // The AST is now saturated. From here on, we conservatively consider all
615 // pointers to alias each-other.
616 return mergeAllAliasSets();
617 }
618
619 return AS;
620}
Chris Lattner2cffeec2003-12-18 08:11:56 +0000621
Chris Lattner9971ac42003-02-24 20:37:56 +0000622//===----------------------------------------------------------------------===//
623// AliasSet/AliasSetTracker Printing Support
624//===----------------------------------------------------------------------===//
625
Chris Lattner791102f2009-08-23 05:17:37 +0000626void AliasSet::print(raw_ostream &OS) const {
Roman Divacky59324292012-09-05 22:26:57 +0000627 OS << " AliasSet[" << (const void*)this << ", " << RefCount << "] ";
Chandler Carruthab6ddad2015-06-22 02:12:52 +0000628 OS << (Alias == SetMustAlias ? "must" : "may") << " alias, ";
629 switch (Access) {
Hal Finkel5b601e12015-10-28 22:13:41 +0000630 case NoAccess: OS << "No access "; break;
631 case RefAccess: OS << "Ref "; break;
632 case ModAccess: OS << "Mod "; break;
633 case ModRefAccess: OS << "Mod/Ref "; break;
Chandler Carruthab6ddad2015-06-22 02:12:52 +0000634 default: llvm_unreachable("Bad value for Access!");
Chris Lattner009cc3d2002-09-26 21:49:07 +0000635 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000636 if (Forward)
637 OS << " forwarding to " << (void*)Forward;
638
Dan Gohmancb406c22007-10-03 19:26:29 +0000639 if (!empty()) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000640 OS << "Pointers: ";
641 for (iterator I = begin(), E = end(); I != E; ++I) {
642 if (I != begin()) OS << ", ";
Chandler Carruth560e3952014-01-09 02:29:41 +0000643 I.getPointer()->printAsOperand(OS << "(");
George Burgess IVea46abe2018-10-10 21:28:44 +0000644 if (I.getSize() == LocationSize::unknown())
Philip Reamesc351fea2018-08-17 23:17:31 +0000645 OS << ", unknown)";
646 else
647 OS << ", " << I.getSize() << ")";
Chris Lattner9971ac42003-02-24 20:37:56 +0000648 }
649 }
Eli Friedman6f3ba372011-07-27 00:46:46 +0000650 if (!UnknownInsts.empty()) {
651 OS << "\n " << UnknownInsts.size() << " Unknown instructions: ";
652 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000653 if (i) OS << ", ";
Jakub Kuderskibdacec92018-06-27 16:34:30 +0000654 if (auto *I = getUnknownInst(i)) {
655 if (I->hasName())
656 I->printAsOperand(OS);
657 else
658 I->print(OS);
659 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000660 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000661 }
662 OS << "\n";
663}
664
Chris Lattner791102f2009-08-23 05:17:37 +0000665void AliasSetTracker::print(raw_ostream &OS) const {
Chris Lattner9971ac42003-02-24 20:37:56 +0000666 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
667 << PointerMap.size() << " pointer values.\n";
Benjamin Kramer8d0d2b62016-06-26 17:27:42 +0000668 for (const AliasSet &AS : *this)
669 AS.print(OS);
Chris Lattner9971ac42003-02-24 20:37:56 +0000670 OS << "\n";
671}
672
Aaron Ballman1d03d382017-10-15 14:32:27 +0000673#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Keren55307982016-01-29 20:50:44 +0000674LLVM_DUMP_METHOD void AliasSet::dump() const { print(dbgs()); }
675LLVM_DUMP_METHOD void AliasSetTracker::dump() const { print(dbgs()); }
Manman Rencc77eec2012-09-06 19:55:56 +0000676#endif
Chris Lattner9971ac42003-02-24 20:37:56 +0000677
Chris Lattner9971ac42003-02-24 20:37:56 +0000678//===----------------------------------------------------------------------===//
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000679// ASTCallbackVH Class Implementation
680//===----------------------------------------------------------------------===//
681
682void AliasSetTracker::ASTCallbackVH::deleted() {
683 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
684 AST->deleteValue(getValPtr());
685 // this now dangles!
686}
687
Eli Friedman9055ccd2011-04-09 06:55:46 +0000688void AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(Value *V) {
689 AST->copyValue(getValPtr(), V);
690}
691
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000692AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
Dan Gohmana818c302009-07-31 18:21:48 +0000693 : CallbackVH(V), AST(ast) {}
694
695AliasSetTracker::ASTCallbackVH &
696AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
697 return *this = ASTCallbackVH(V, AST);
698}
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000699
700//===----------------------------------------------------------------------===//
Chris Lattner9971ac42003-02-24 20:37:56 +0000701// AliasSetPrinter Pass
702//===----------------------------------------------------------------------===//
703
704namespace {
Eugene Zelenko4114bcf2017-07-24 23:16:33 +0000705
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000706 class AliasSetPrinter : public FunctionPass {
Chris Lattner9971ac42003-02-24 20:37:56 +0000707 AliasSetTracker *Tracker;
Eugene Zelenko4114bcf2017-07-24 23:16:33 +0000708
Chris Lattner9971ac42003-02-24 20:37:56 +0000709 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +0000710 static char ID; // Pass identification, replacement for typeid
Eugene Zelenko4114bcf2017-07-24 23:16:33 +0000711
Owen Anderson081c34b2010-10-19 17:21:58 +0000712 AliasSetPrinter() : FunctionPass(ID) {
713 initializeAliasSetPrinterPass(*PassRegistry::getPassRegistry());
714 }
Devang Patel794fd752007-05-01 21:15:47 +0000715
Craig Topperc37e6c02014-03-05 07:30:04 +0000716 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner9971ac42003-02-24 20:37:56 +0000717 AU.setPreservesAll();
Chandler Carruth91468332015-09-09 17:55:00 +0000718 AU.addRequired<AAResultsWrapperPass>();
Chris Lattner9971ac42003-02-24 20:37:56 +0000719 }
720
Craig Topperc37e6c02014-03-05 07:30:04 +0000721 bool runOnFunction(Function &F) override {
Chandler Carruth91468332015-09-09 17:55:00 +0000722 auto &AAWP = getAnalysis<AAResultsWrapperPass>();
723 Tracker = new AliasSetTracker(AAWP.getAAResults());
Michael Kuperstein15095e42016-08-19 17:05:22 +0000724 errs() << "Alias sets for function '" << F.getName() << "':\n";
Chris Lattner9971ac42003-02-24 20:37:56 +0000725 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner6ffe5512004-04-27 15:13:33 +0000726 Tracker->add(&*I);
David Greene4850a892009-12-23 22:49:57 +0000727 Tracker->print(errs());
Chris Lattner4983cf72006-01-03 06:05:22 +0000728 delete Tracker;
Chris Lattner9971ac42003-02-24 20:37:56 +0000729 return false;
730 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000731 };
Eugene Zelenko4114bcf2017-07-24 23:16:33 +0000732
733} // end anonymous namespace
Dan Gohman844731a2008-05-13 00:00:25 +0000734
735char AliasSetPrinter::ID = 0;
Eugene Zelenko4114bcf2017-07-24 23:16:33 +0000736
Owen Anderson2ab36d32010-10-12 19:48:12 +0000737INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets",
738 "Alias Set Printer", false, true)
Chandler Carruth91468332015-09-09 17:55:00 +0000739INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Owen Anderson2ab36d32010-10-12 19:48:12 +0000740INITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets",
Owen Andersonce665bd2010-10-07 22:25:06 +0000741 "Alias Set Printer", false, true)