blob: ee17ad3ba58635483340d3143198e5e2f6a13d4a [file] [log] [blame]
Chris Lattner647df642002-09-26 21:49:07 +00001//===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
Misha Brukman01808ca2005-04-21 21:13:18 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukman01808ca2005-04-21 21:13:18 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner647df642002-09-26 21:49:07 +00009//
10// This file implements the AliasSetTracker and AliasSet classes.
Misha Brukman01808ca2005-04-21 21:13:18 +000011//
Chris Lattner647df642002-09-26 21:49:07 +000012//===----------------------------------------------------------------------===//
13
14#include "llvm/Analysis/AliasSetTracker.h"
15#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/DataLayout.h"
Chandler Carruth83948572014-03-04 10:30:26 +000017#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Instructions.h"
19#include "llvm/IR/IntrinsicInst.h"
20#include "llvm/IR/LLVMContext.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000021#include "llvm/IR/Module.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Type.h"
Chris Lattner7606fb62003-02-24 20:37:56 +000023#include "llvm/Pass.h"
David Greene2ec90032009-12-23 19:27:59 +000024#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000025#include "llvm/Support/ErrorHandling.h"
Chris Lattnerb1d782b2009-08-23 05:17:37 +000026#include "llvm/Support/raw_ostream.h"
Chris Lattner0a140602003-12-14 04:52:11 +000027using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000028
Michael Kuperstein41898f02016-08-19 17:05:22 +000029static cl::opt<unsigned>
30 SaturationThreshold("alias-set-saturation-threshold", cl::Hidden,
31 cl::init(250),
32 cl::desc("The maximum number of pointers may-alias "
33 "sets may contain before degradation"));
34
Chris Lattner24bba4d2004-11-27 18:37:42 +000035/// mergeSetIn - Merge the specified alias set into this alias set.
Chris Lattner647df642002-09-26 21:49:07 +000036///
Chris Lattner24bba4d2004-11-27 18:37:42 +000037void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
Chris Lattner7606fb62003-02-24 20:37:56 +000038 assert(!AS.Forward && "Alias set is already forwarding!");
39 assert(!Forward && "This set is a forwarding set!!");
Chris Lattner647df642002-09-26 21:49:07 +000040
Michael Kuperstein41898f02016-08-19 17:05:22 +000041 bool WasMustAlias = (Alias == SetMustAlias);
Chris Lattner647df642002-09-26 21:49:07 +000042 // Update the alias and access types of this set...
Chandler Carrutha561d752015-06-22 02:12:52 +000043 Access |= AS.Access;
44 Alias |= AS.Alias;
Chris Lattnerdc8070e2010-08-29 04:14:47 +000045 Volatile |= AS.Volatile;
Chris Lattner7606fb62003-02-24 20:37:56 +000046
Chandler Carrutha561d752015-06-22 02:12:52 +000047 if (Alias == SetMustAlias) {
Chris Lattner24bba4d2004-11-27 18:37:42 +000048 // Check that these two merged sets really are must aliases. Since both
49 // used to be must-alias sets, we can just check any pointer from each set
50 // for aliasing.
51 AliasAnalysis &AA = AST.getAliasAnalysis();
Chris Lattner0eab5ec2009-03-09 05:11:09 +000052 PointerRec *L = getSomePointer();
53 PointerRec *R = AS.getSomePointer();
Chris Lattner24bba4d2004-11-27 18:37:42 +000054
55 // If the pointers are not a must-alias pair, this set becomes a may alias.
Chandler Carruthac80dc72015-06-17 07:18:54 +000056 if (AA.alias(MemoryLocation(L->getValue(), L->getSize(), L->getAAInfo()),
57 MemoryLocation(R->getValue(), R->getSize(), R->getAAInfo())) !=
Chandler Carruthc3f49eb2015-06-22 02:16:51 +000058 MustAlias)
Chandler Carrutha561d752015-06-22 02:12:52 +000059 Alias = SetMayAlias;
Chris Lattner24bba4d2004-11-27 18:37:42 +000060 }
61
Michael Kuperstein41898f02016-08-19 17:05:22 +000062 if (Alias == SetMayAlias) {
63 if (WasMustAlias)
64 AST.TotalMayAliasSetSize += size();
65 if (AS.Alias == SetMustAlias)
66 AST.TotalMayAliasSetSize += AS.size();
67 }
68
David Majnemer35639382014-11-19 19:36:18 +000069 bool ASHadUnknownInsts = !AS.UnknownInsts.empty();
Eli Friedmanae8161e2011-07-27 00:46:46 +000070 if (UnknownInsts.empty()) { // Merge call sites...
David Majnemer35639382014-11-19 19:36:18 +000071 if (ASHadUnknownInsts) {
Eli Friedmanae8161e2011-07-27 00:46:46 +000072 std::swap(UnknownInsts, AS.UnknownInsts);
David Majnemerb7adf342014-11-19 09:41:05 +000073 addRef();
74 }
David Majnemer35639382014-11-19 19:36:18 +000075 } else if (ASHadUnknownInsts) {
Eli Friedmanae8161e2011-07-27 00:46:46 +000076 UnknownInsts.insert(UnknownInsts.end(), AS.UnknownInsts.begin(), AS.UnknownInsts.end());
77 AS.UnknownInsts.clear();
Chris Lattner7606fb62003-02-24 20:37:56 +000078 }
Misha Brukman01808ca2005-04-21 21:13:18 +000079
Michael Kuperstein41898f02016-08-19 17:05:22 +000080 AS.Forward = this; // Forward across AS now...
81 addRef(); // AS is now pointing to us...
Chris Lattner7606fb62003-02-24 20:37:56 +000082
83 // Merge the list of constituent pointers...
Chris Lattner44fea542003-12-18 08:11:56 +000084 if (AS.PtrList) {
Michael Kuperstein41898f02016-08-19 17:05:22 +000085 SetSize += AS.size();
86 AS.SetSize = 0;
Chris Lattner44fea542003-12-18 08:11:56 +000087 *PtrListEnd = AS.PtrList;
Chris Lattner0eab5ec2009-03-09 05:11:09 +000088 AS.PtrList->setPrevInList(PtrListEnd);
Chris Lattner44fea542003-12-18 08:11:56 +000089 PtrListEnd = AS.PtrListEnd;
90
Craig Topper9f008862014-04-15 04:59:12 +000091 AS.PtrList = nullptr;
Chris Lattner44fea542003-12-18 08:11:56 +000092 AS.PtrListEnd = &AS.PtrList;
Craig Topper9f008862014-04-15 04:59:12 +000093 assert(*AS.PtrListEnd == nullptr && "End of list is not null?");
Chris Lattner44fea542003-12-18 08:11:56 +000094 }
David Majnemerb7adf342014-11-19 09:41:05 +000095 if (ASHadUnknownInsts)
96 AS.dropRef(AST);
Chris Lattner647df642002-09-26 21:49:07 +000097}
98
Chris Lattner7606fb62003-02-24 20:37:56 +000099void AliasSetTracker::removeAliasSet(AliasSet *AS) {
Chris Lattner053427f2004-07-22 07:58:18 +0000100 if (AliasSet *Fwd = AS->Forward) {
101 Fwd->dropRef(*this);
Craig Topper9f008862014-04-15 04:59:12 +0000102 AS->Forward = nullptr;
Chris Lattner053427f2004-07-22 07:58:18 +0000103 }
Michael Kuperstein41898f02016-08-19 17:05:22 +0000104
105 if (AS->Alias == AliasSet::SetMayAlias)
106 TotalMayAliasSetSize -= AS->size();
107
Chris Lattner7606fb62003-02-24 20:37:56 +0000108 AliasSets.erase(AS);
Michael Kuperstein41898f02016-08-19 17:05:22 +0000109
Chris Lattner7606fb62003-02-24 20:37:56 +0000110}
111
112void AliasSet::removeFromTracker(AliasSetTracker &AST) {
113 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
114 AST.removeAliasSet(this);
115}
116
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000117void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
Hal Finkelcc39b672014-07-24 12:16:19 +0000118 uint64_t Size, const AAMDNodes &AAInfo,
Hal Finkel1140e172015-10-28 22:13:41 +0000119 bool KnownMustAlias) {
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000120 assert(!Entry.hasAliasSet() && "Entry already in set!");
Chris Lattner7606fb62003-02-24 20:37:56 +0000121
Chris Lattnerab644812004-09-14 19:15:32 +0000122 // Check to see if we have to downgrade to _may_ alias.
123 if (isMustAlias() && !KnownMustAlias)
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000124 if (PointerRec *P = getSomePointer()) {
Chris Lattner6fa96652004-09-15 16:59:47 +0000125 AliasAnalysis &AA = AST.getAliasAnalysis();
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000126 AliasResult Result =
Chandler Carruthac80dc72015-06-17 07:18:54 +0000127 AA.alias(MemoryLocation(P->getValue(), P->getSize(), P->getAAInfo()),
128 MemoryLocation(Entry.getValue(), Size, AAInfo));
Michael Kuperstein41898f02016-08-19 17:05:22 +0000129 if (Result != MustAlias) {
Chandler Carrutha561d752015-06-22 02:12:52 +0000130 Alias = SetMayAlias;
Michael Kuperstein41898f02016-08-19 17:05:22 +0000131 AST.TotalMayAliasSetSize += size();
132 } else {
133 // First entry of must alias must have maximum size!
Hal Finkelcc39b672014-07-24 12:16:19 +0000134 P->updateSizeAndAAInfo(Size, AAInfo);
Michael Kuperstein41898f02016-08-19 17:05:22 +0000135 }
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000136 assert(Result != NoAlias && "Cannot be part of must set!");
Chris Lattnerb5b0b7a2003-02-26 22:11:00 +0000137 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000138
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000139 Entry.setAliasSet(this);
Hal Finkelcc39b672014-07-24 12:16:19 +0000140 Entry.updateSizeAndAAInfo(Size, AAInfo);
Chris Lattner7606fb62003-02-24 20:37:56 +0000141
142 // Add it to the end of the list...
Michael Kuperstein41898f02016-08-19 17:05:22 +0000143 ++SetSize;
Craig Topper9f008862014-04-15 04:59:12 +0000144 assert(*PtrListEnd == nullptr && "End of list is not null?");
Chris Lattner44fea542003-12-18 08:11:56 +0000145 *PtrListEnd = &Entry;
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000146 PtrListEnd = Entry.setPrevInList(PtrListEnd);
Craig Topper9f008862014-04-15 04:59:12 +0000147 assert(*PtrListEnd == nullptr && "End of list is not null?");
Michael Kuperstein41898f02016-08-19 17:05:22 +0000148 // Entry points to alias set.
149 addRef();
150
151 if (Alias == SetMayAlias)
152 AST.TotalMayAliasSetSize++;
Chris Lattner7606fb62003-02-24 20:37:56 +0000153}
154
Hal Finkel1140e172015-10-28 22:13:41 +0000155void AliasSet::addUnknownInst(Instruction *I, AliasAnalysis &AA) {
David Majnemerb7adf342014-11-19 09:41:05 +0000156 if (UnknownInsts.empty())
157 addRef();
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000158 UnknownInsts.emplace_back(I);
Chris Lattner21c60f12004-03-15 04:08:36 +0000159
Eli Friedmanae8161e2011-07-27 00:46:46 +0000160 if (!I->mayWriteToMemory()) {
Chandler Carrutha561d752015-06-22 02:12:52 +0000161 Alias = SetMayAlias;
Hal Finkel1140e172015-10-28 22:13:41 +0000162 Access |= RefAccess;
Duncan Sands68b6f502007-12-01 07:51:45 +0000163 return;
Chris Lattner21c60f12004-03-15 04:08:36 +0000164 }
165
Hal Finkel1140e172015-10-28 22:13:41 +0000166 // FIXME: This should use mod/ref information to make this not suck so bad
Chandler Carrutha561d752015-06-22 02:12:52 +0000167 Alias = SetMayAlias;
Hal Finkel1140e172015-10-28 22:13:41 +0000168 Access = ModRefAccess;
Chris Lattner7606fb62003-02-24 20:37:56 +0000169}
170
171/// aliasesPointer - Return true if the specified pointer "may" (or must)
Chris Lattner647df642002-09-26 21:49:07 +0000172/// alias one of the members in the set.
173///
Dan Gohmanf372cf82010-10-19 22:54:46 +0000174bool AliasSet::aliasesPointer(const Value *Ptr, uint64_t Size,
Hal Finkelcc39b672014-07-24 12:16:19 +0000175 const AAMDNodes &AAInfo,
Hal Finkel1140e172015-10-28 22:13:41 +0000176 AliasAnalysis &AA) const {
Michael Kuperstein41898f02016-08-19 17:05:22 +0000177 if (AliasAny)
178 return true;
179
Chandler Carrutha561d752015-06-22 02:12:52 +0000180 if (Alias == SetMustAlias) {
Eli Friedmanae8161e2011-07-27 00:46:46 +0000181 assert(UnknownInsts.empty() && "Illegal must alias set!");
Chris Lattner7f04ebc2004-03-15 06:28:07 +0000182
Chris Lattner7606fb62003-02-24 20:37:56 +0000183 // If this is a set of MustAliases, only check to see if the pointer aliases
Chris Lattnereef6b192010-08-29 04:13:43 +0000184 // SOME value in the set.
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000185 PointerRec *SomePtr = getSomePointer();
Chris Lattner7606fb62003-02-24 20:37:56 +0000186 assert(SomePtr && "Empty must-alias set??");
Chandler Carruthac80dc72015-06-17 07:18:54 +0000187 return AA.alias(MemoryLocation(SomePtr->getValue(), SomePtr->getSize(),
188 SomePtr->getAAInfo()),
189 MemoryLocation(Ptr, Size, AAInfo));
Chris Lattner7606fb62003-02-24 20:37:56 +0000190 }
191
192 // If this is a may-alias set, we have to check all of the pointers in the set
193 // to be sure it doesn't alias the set...
194 for (iterator I = begin(), E = end(); I != E; ++I)
Chandler Carruthac80dc72015-06-17 07:18:54 +0000195 if (AA.alias(MemoryLocation(Ptr, Size, AAInfo),
196 MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo())))
Chris Lattner7606fb62003-02-24 20:37:56 +0000197 return true;
198
Eli Friedmanae8161e2011-07-27 00:46:46 +0000199 // Check the unknown instructions...
200 if (!UnknownInsts.empty()) {
201 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i)
Sanjoy Das3f1e8e02017-03-11 01:15:48 +0000202 if (auto *Inst = getUnknownInst(i))
203 if (AA.getModRefInfo(Inst, MemoryLocation(Ptr, Size, AAInfo)) !=
204 MRI_NoModRef)
205 return true;
Chris Lattner9b323c32004-07-27 02:20:26 +0000206 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000207
Hal Finkel1140e172015-10-28 22:13:41 +0000208 return false;
Chris Lattner647df642002-09-26 21:49:07 +0000209}
210
Pete Cooper4bf388d2015-05-13 01:12:12 +0000211bool AliasSet::aliasesUnknownInst(const Instruction *Inst,
Hal Finkel1140e172015-10-28 22:13:41 +0000212 AliasAnalysis &AA) const {
Michael Kuperstein41898f02016-08-19 17:05:22 +0000213
214 if (AliasAny)
215 return true;
216
Eli Friedmanae8161e2011-07-27 00:46:46 +0000217 if (!Inst->mayReadOrWriteMemory())
Duncan Sands68b6f502007-12-01 07:51:45 +0000218 return false;
Chris Lattner21c60f12004-03-15 04:08:36 +0000219
Eli Friedmanae8161e2011-07-27 00:46:46 +0000220 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
Sanjoy Das3f1e8e02017-03-11 01:15:48 +0000221 if (auto *Inst = getUnknownInst(i)) {
222 ImmutableCallSite C1(Inst), C2(Inst);
223 if (!C1 || !C2 || AA.getModRefInfo(C1, C2) != MRI_NoModRef ||
224 AA.getModRefInfo(C2, C1) != MRI_NoModRef)
225 return true;
226 }
Chris Lattnerf58382e2010-08-29 18:42:23 +0000227 }
Chris Lattner9b323c32004-07-27 02:20:26 +0000228
Hal Finkel1140e172015-10-28 22:13:41 +0000229 for (iterator I = begin(), E = end(); I != E; ++I)
230 if (AA.getModRefInfo(Inst, MemoryLocation(I.getPointer(), I.getSize(),
231 I.getAAInfo())) != MRI_NoModRef)
232 return true;
233
234 return false;
Chris Lattner647df642002-09-26 21:49:07 +0000235}
236
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000237void AliasSetTracker::clear() {
238 // Delete all the PointerRec entries.
Dan Gohmanf4362da2009-07-30 20:21:41 +0000239 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
240 I != E; ++I)
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000241 I->second->eraseFromList();
242
243 PointerMap.clear();
244
245 // The alias sets should all be clear now.
246 AliasSets.clear();
247}
248
Chris Lattner647df642002-09-26 21:49:07 +0000249
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000250/// mergeAliasSetsForPointer - Given a pointer, merge all alias sets that may
251/// alias the pointer. Return the unified set, or nullptr if no set that aliases
252/// the pointer was found.
253AliasSet *AliasSetTracker::mergeAliasSetsForPointer(const Value *Ptr,
254 uint64_t Size,
255 const AAMDNodes &AAInfo) {
Craig Topper9f008862014-04-15 04:59:12 +0000256 AliasSet *FoundSet = nullptr;
David Majnemerb7adf342014-11-19 09:41:05 +0000257 for (iterator I = begin(), E = end(); I != E;) {
258 iterator Cur = I++;
Hal Finkel1140e172015-10-28 22:13:41 +0000259 if (Cur->Forward || !Cur->aliasesPointer(Ptr, Size, AAInfo, AA)) continue;
Chris Lattnerafb70742010-08-29 04:06:55 +0000260
Craig Topper9f008862014-04-15 04:59:12 +0000261 if (!FoundSet) { // If this is the first alias set ptr can go into.
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000262 FoundSet = &*Cur; // Remember it.
Chris Lattnerafb70742010-08-29 04:06:55 +0000263 } else { // Otherwise, we must merge the sets.
David Majnemerb7adf342014-11-19 09:41:05 +0000264 FoundSet->mergeSetIn(*Cur, *this); // Merge in contents.
Chris Lattner647df642002-09-26 21:49:07 +0000265 }
Chris Lattnerafb70742010-08-29 04:06:55 +0000266 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000267
268 return FoundSet;
269}
270
Pete Cooper4bf388d2015-05-13 01:12:12 +0000271bool AliasSetTracker::containsUnknown(const Instruction *Inst) const {
Benjamin Krameraa209152016-06-26 17:27:42 +0000272 for (const AliasSet &AS : *this)
273 if (!AS.Forward && AS.aliasesUnknownInst(Inst, AA))
Hal Finkel840257a2014-11-03 23:19:16 +0000274 return true;
275 return false;
276}
Chris Lattnereeaa29c2004-11-26 21:36:25 +0000277
Hal Finkel1140e172015-10-28 22:13:41 +0000278AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) {
Craig Topper9f008862014-04-15 04:59:12 +0000279 AliasSet *FoundSet = nullptr;
David Majnemerb7adf342014-11-19 09:41:05 +0000280 for (iterator I = begin(), E = end(); I != E;) {
281 iterator Cur = I++;
Hal Finkel1140e172015-10-28 22:13:41 +0000282 if (Cur->Forward || !Cur->aliasesUnknownInst(Inst, AA))
Chris Lattnerafb70742010-08-29 04:06:55 +0000283 continue;
Craig Topper9f008862014-04-15 04:59:12 +0000284 if (!FoundSet) // If this is the first alias set ptr can go into.
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000285 FoundSet = &*Cur; // Remember it.
David Majnemerb7adf342014-11-19 09:41:05 +0000286 else if (!Cur->Forward) // Otherwise, we must merge the sets.
287 FoundSet->mergeSetIn(*Cur, *this); // Merge in contents.
Chris Lattnerafb70742010-08-29 04:06:55 +0000288 }
Chris Lattner647df642002-09-26 21:49:07 +0000289 return FoundSet;
290}
291
Chris Lattner7606fb62003-02-24 20:37:56 +0000292/// getAliasSetForPointer - Return the alias set that the specified pointer
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000293/// lives in.
Dan Gohmanf372cf82010-10-19 22:54:46 +0000294AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, uint64_t Size,
Chad Rosier16970a82016-10-19 18:50:32 +0000295 const AAMDNodes &AAInfo) {
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000296 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
Chris Lattner7606fb62003-02-24 20:37:56 +0000297
Michael Kuperstein41898f02016-08-19 17:05:22 +0000298 if (AliasAnyAS) {
299 // At this point, the AST is saturated, so we only have one active alias
300 // set. That means we already know which alias set we want to return, and
301 // just need to add the pointer to that set to keep the data structure
302 // consistent.
303 // This, of course, means that we will never need a merge here.
304 if (Entry.hasAliasSet()) {
305 Entry.updateSizeAndAAInfo(Size, AAInfo);
306 assert(Entry.getAliasSet(*this) == AliasAnyAS &&
307 "Entry in saturated AST must belong to only alias set");
308 } else {
309 AliasAnyAS->addPointer(*this, Entry, Size, AAInfo);
310 }
311 return *AliasAnyAS;
312 }
313
Chris Lattnereef6b192010-08-29 04:13:43 +0000314 // Check to see if the pointer is already known.
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000315 if (Entry.hasAliasSet()) {
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000316 // If the size changed, we may need to merge several alias sets.
317 // Note that we can *not* return the result of mergeAliasSetsForPointer
318 // due to a quirk of alias analysis behavior. Since alias(undef, undef)
319 // is NoAlias, mergeAliasSetsForPointer(undef, ...) will not find the
320 // the right set for undef, even if it exists.
321 if (Entry.updateSizeAndAAInfo(Size, AAInfo))
322 mergeAliasSetsForPointer(Pointer, Size, AAInfo);
Chris Lattner7606fb62003-02-24 20:37:56 +0000323 // Return the set!
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000324 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattnerafb70742010-08-29 04:06:55 +0000325 }
326
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000327 if (AliasSet *AS = mergeAliasSetsForPointer(Pointer, Size, AAInfo)) {
Chris Lattnereef6b192010-08-29 04:13:43 +0000328 // Add it to the alias set it aliases.
Hal Finkel1140e172015-10-28 22:13:41 +0000329 AS->addPointer(*this, Entry, Size, AAInfo);
Chris Lattner7606fb62003-02-24 20:37:56 +0000330 return *AS;
Chris Lattner647df642002-09-26 21:49:07 +0000331 }
Chris Lattnerafb70742010-08-29 04:06:55 +0000332
Chris Lattnereef6b192010-08-29 04:13:43 +0000333 // Otherwise create a new alias set to hold the loaded pointer.
Chris Lattnerafb70742010-08-29 04:06:55 +0000334 AliasSets.push_back(new AliasSet());
Hal Finkel1140e172015-10-28 22:13:41 +0000335 AliasSets.back().addPointer(*this, Entry, Size, AAInfo);
Chris Lattnerafb70742010-08-29 04:06:55 +0000336 return AliasSets.back();
Chris Lattner647df642002-09-26 21:49:07 +0000337}
338
Chad Rosier16970a82016-10-19 18:50:32 +0000339void AliasSetTracker::add(Value *Ptr, uint64_t Size, const AAMDNodes &AAInfo) {
340 addPointer(Ptr, Size, AAInfo, AliasSet::NoAccess);
Chris Lattnerbf8c3c42004-07-26 05:50:23 +0000341}
342
Chad Rosier16970a82016-10-19 18:50:32 +0000343void AliasSetTracker::add(LoadInst *LI) {
JF Bastien800f87a2016-04-06 21:19:33 +0000344 if (isStrongerThanMonotonic(LI->getOrdering())) return addUnknown(LI);
Hal Finkelcc39b672014-07-24 12:16:19 +0000345
346 AAMDNodes AAInfo;
347 LI->getAAMetadata(AAInfo);
348
Hal Finkel1140e172015-10-28 22:13:41 +0000349 AliasSet::AccessLattice Access = AliasSet::RefAccess;
Chandler Carruth50fee932015-08-06 02:05:46 +0000350 const DataLayout &DL = LI->getModule()->getDataLayout();
Chris Lattner2cfaef22004-07-21 05:18:04 +0000351 AliasSet &AS = addPointer(LI->getOperand(0),
Chad Rosier16970a82016-10-19 18:50:32 +0000352 DL.getTypeStoreSize(LI->getType()), AAInfo, Access);
Chris Lattner0a140602003-12-14 04:52:11 +0000353 if (LI->isVolatile()) AS.setVolatile();
Chris Lattner7606fb62003-02-24 20:37:56 +0000354}
355
Chad Rosier16970a82016-10-19 18:50:32 +0000356void AliasSetTracker::add(StoreInst *SI) {
JF Bastien800f87a2016-04-06 21:19:33 +0000357 if (isStrongerThanMonotonic(SI->getOrdering())) return addUnknown(SI);
Hal Finkelcc39b672014-07-24 12:16:19 +0000358
359 AAMDNodes AAInfo;
360 SI->getAAMetadata(AAInfo);
361
Hal Finkel1140e172015-10-28 22:13:41 +0000362 AliasSet::AccessLattice Access = AliasSet::ModAccess;
Chandler Carruth50fee932015-08-06 02:05:46 +0000363 const DataLayout &DL = SI->getModule()->getDataLayout();
Chris Lattner2cfaef22004-07-21 05:18:04 +0000364 Value *Val = SI->getOperand(0);
Chad Rosier16970a82016-10-19 18:50:32 +0000365 AliasSet &AS = addPointer(
366 SI->getOperand(1), DL.getTypeStoreSize(Val->getType()), AAInfo, Access);
Chris Lattner0a140602003-12-14 04:52:11 +0000367 if (SI->isVolatile()) AS.setVolatile();
Chris Lattner647df642002-09-26 21:49:07 +0000368}
369
Chad Rosier16970a82016-10-19 18:50:32 +0000370void AliasSetTracker::add(VAArgInst *VAAI) {
Hal Finkelcc39b672014-07-24 12:16:19 +0000371 AAMDNodes AAInfo;
372 VAAI->getAAMetadata(AAInfo);
373
Chandler Carruthecbd1682015-06-17 07:21:38 +0000374 addPointer(VAAI->getOperand(0), MemoryLocation::UnknownSize, AAInfo,
Chad Rosier16970a82016-10-19 18:50:32 +0000375 AliasSet::ModRefAccess);
Dan Gohman2cd8e382008-04-14 18:34:50 +0000376}
377
Chad Rosier16970a82016-10-19 18:50:32 +0000378void AliasSetTracker::add(MemSetInst *MSI) {
Haicheng Wu5cf99092016-02-17 02:01:50 +0000379 AAMDNodes AAInfo;
380 MSI->getAAMetadata(AAInfo);
381
Haicheng Wu5cf99092016-02-17 02:01:50 +0000382 uint64_t Len;
383
384 if (ConstantInt *C = dyn_cast<ConstantInt>(MSI->getLength()))
385 Len = C->getZExtValue();
386 else
387 Len = MemoryLocation::UnknownSize;
388
389 AliasSet &AS =
Chad Rosier16970a82016-10-19 18:50:32 +0000390 addPointer(MSI->getRawDest(), Len, AAInfo, AliasSet::ModAccess);
Haicheng Wu5cf99092016-02-17 02:01:50 +0000391 if (MSI->isVolatile())
392 AS.setVolatile();
Haicheng Wu5cf99092016-02-17 02:01:50 +0000393}
Chris Lattner0a140602003-12-14 04:52:11 +0000394
Chad Rosier6e3a92e2016-10-19 19:09:03 +0000395void AliasSetTracker::add(MemTransferInst *MTI) {
396 AAMDNodes AAInfo;
397 MTI->getAAMetadata(AAInfo);
398
399 uint64_t Len;
400 if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
401 Len = C->getZExtValue();
402 else
403 Len = MemoryLocation::UnknownSize;
404
405 AliasSet &ASSrc =
406 addPointer(MTI->getRawSource(), Len, AAInfo, AliasSet::RefAccess);
407 if (MTI->isVolatile())
408 ASSrc.setVolatile();
409
410 AliasSet &ASDst =
411 addPointer(MTI->getRawDest(), Len, AAInfo, AliasSet::ModAccess);
412 if (MTI->isVolatile())
413 ASDst.setVolatile();
414}
415
Chad Rosier16970a82016-10-19 18:50:32 +0000416void AliasSetTracker::addUnknown(Instruction *Inst) {
417 if (isa<DbgInfoIntrinsic>(Inst))
418 return; // Ignore DbgInfo Intrinsics.
Chad Rosier8f348012016-11-07 14:11:45 +0000419
420 if (auto *II = dyn_cast<IntrinsicInst>(Inst)) {
421 // These intrinsics will show up as affecting memory, but they are just
422 // markers.
423 switch (II->getIntrinsicID()) {
424 default:
425 break;
426 // FIXME: Add lifetime/invariant intrinsics (See: PR30807).
427 case Intrinsic::assume:
428 return;
429 }
430 }
Eli Friedmanae8161e2011-07-27 00:46:46 +0000431 if (!Inst->mayReadOrWriteMemory())
Chad Rosier16970a82016-10-19 18:50:32 +0000432 return; // doesn't alias anything
Chris Lattner7f04ebc2004-03-15 06:28:07 +0000433
Hal Finkel1140e172015-10-28 22:13:41 +0000434 AliasSet *AS = findAliasSetForUnknownInst(Inst);
Chris Lattnerafb70742010-08-29 04:06:55 +0000435 if (AS) {
Hal Finkel1140e172015-10-28 22:13:41 +0000436 AS->addUnknownInst(Inst, AA);
Chad Rosier16970a82016-10-19 18:50:32 +0000437 return;
Chris Lattner7606fb62003-02-24 20:37:56 +0000438 }
Chris Lattnerafb70742010-08-29 04:06:55 +0000439 AliasSets.push_back(new AliasSet());
440 AS = &AliasSets.back();
Hal Finkel1140e172015-10-28 22:13:41 +0000441 AS->addUnknownInst(Inst, AA);
Chris Lattner647df642002-09-26 21:49:07 +0000442}
443
Chad Rosier16970a82016-10-19 18:50:32 +0000444void AliasSetTracker::add(Instruction *I) {
Chris Lattnerafb70742010-08-29 04:06:55 +0000445 // Dispatch to one of the other add methods.
Chris Lattner7606fb62003-02-24 20:37:56 +0000446 if (LoadInst *LI = dyn_cast<LoadInst>(I))
Chris Lattner2cfaef22004-07-21 05:18:04 +0000447 return add(LI);
Chris Lattnerafb70742010-08-29 04:06:55 +0000448 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner2cfaef22004-07-21 05:18:04 +0000449 return add(SI);
Chris Lattnerafb70742010-08-29 04:06:55 +0000450 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
Dan Gohman2cd8e382008-04-14 18:34:50 +0000451 return add(VAAI);
Haicheng Wu5cf99092016-02-17 02:01:50 +0000452 if (MemSetInst *MSI = dyn_cast<MemSetInst>(I))
453 return add(MSI);
Chad Rosier6e3a92e2016-10-19 19:09:03 +0000454 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(I))
455 return add(MTI);
Eli Friedmanae8161e2011-07-27 00:46:46 +0000456 return addUnknown(I);
Chris Lattner647df642002-09-26 21:49:07 +0000457}
458
Chris Lattnerc048bb32003-03-03 23:28:05 +0000459void AliasSetTracker::add(BasicBlock &BB) {
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000460 for (auto &I : BB)
461 add(&I);
Chris Lattnerc048bb32003-03-03 23:28:05 +0000462}
463
464void AliasSetTracker::add(const AliasSetTracker &AST) {
465 assert(&AA == &AST.AA &&
466 "Merging AliasSetTracker objects with different Alias Analyses!");
467
468 // Loop over all of the alias sets in AST, adding the pointers contained
469 // therein into the current alias sets. This can cause alias sets to be
470 // merged together in the current AST.
Benjamin Krameraa209152016-06-26 17:27:42 +0000471 for (const AliasSet &AS : AST) {
472 if (AS.Forward)
473 continue; // Ignore forwarding alias sets
Chris Lattnerc048bb32003-03-03 23:28:05 +0000474
Chris Lattnerafb70742010-08-29 04:06:55 +0000475 // If there are any call sites in the alias set, add them to this AST.
Eli Friedmanae8161e2011-07-27 00:46:46 +0000476 for (unsigned i = 0, e = AS.UnknownInsts.size(); i != e; ++i)
Sanjoy Das3f1e8e02017-03-11 01:15:48 +0000477 if (auto *Inst = AS.getUnknownInst(i))
478 add(Inst);
Chris Lattnerc048bb32003-03-03 23:28:05 +0000479
Chris Lattnerafb70742010-08-29 04:06:55 +0000480 // Loop over all of the pointers in this alias set.
Chris Lattnerafb70742010-08-29 04:06:55 +0000481 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) {
Chad Rosier16970a82016-10-19 18:50:32 +0000482 AliasSet &NewAS =
483 addPointer(ASI.getPointer(), ASI.getSize(), ASI.getAAInfo(),
484 (AliasSet::AccessLattice)AS.Access);
Chris Lattnerafb70742010-08-29 04:06:55 +0000485 if (AS.isVolatile()) NewAS.setVolatile();
Chris Lattnerc048bb32003-03-03 23:28:05 +0000486 }
Chris Lattnerafb70742010-08-29 04:06:55 +0000487 }
Chris Lattnerc048bb32003-03-03 23:28:05 +0000488}
489
Chris Lattner746e1e12004-05-23 21:10:58 +0000490// deleteValue method - This method is used to remove a pointer value from the
Chris Lattner44fea542003-12-18 08:11:56 +0000491// AliasSetTracker entirely. It should be used when an instruction is deleted
492// from the program to update the AST. If you don't use this, you would have
493// dangling pointers to deleted instructions.
494//
Chris Lattner746e1e12004-05-23 21:10:58 +0000495void AliasSetTracker::deleteValue(Value *PtrVal) {
Chris Lattnerab644812004-09-14 19:15:32 +0000496 // First, look up the PointerRec for this pointer.
Benjamin Kramere2ef47c2012-06-30 22:37:15 +0000497 PointerMapType::iterator I = PointerMap.find_as(PtrVal);
Chris Lattner44fea542003-12-18 08:11:56 +0000498 if (I == PointerMap.end()) return; // Noop
499
500 // If we found one, remove the pointer from the alias set it is in.
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000501 AliasSet::PointerRec *PtrValEnt = I->second;
502 AliasSet *AS = PtrValEnt->getAliasSet(*this);
Chris Lattner44fea542003-12-18 08:11:56 +0000503
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000504 // Unlink and delete from the list of values.
505 PtrValEnt->eraseFromList();
Michael Kuperstein41898f02016-08-19 17:05:22 +0000506
507 if (AS->Alias == AliasSet::SetMayAlias) {
508 AS->SetSize--;
509 TotalMayAliasSetSize--;
510 }
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000511
512 // Stop using the alias set.
Chris Lattner053427f2004-07-22 07:58:18 +0000513 AS->dropRef(*this);
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000514
Chris Lattner44fea542003-12-18 08:11:56 +0000515 PointerMap.erase(I);
516}
517
Chris Lattnerab644812004-09-14 19:15:32 +0000518// copyValue - This method should be used whenever a preexisting value in the
519// program is copied or cloned, introducing a new value. Note that it is ok for
520// clients that use this method to introduce the same value multiple times: if
521// the tracker already knows about a value, it will ignore the request.
522//
523void AliasSetTracker::copyValue(Value *From, Value *To) {
Chris Lattnerab644812004-09-14 19:15:32 +0000524 // First, look up the PointerRec for this pointer.
Benjamin Kramere2ef47c2012-06-30 22:37:15 +0000525 PointerMapType::iterator I = PointerMap.find_as(From);
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000526 if (I == PointerMap.end())
Chris Lattnerab644812004-09-14 19:15:32 +0000527 return; // Noop
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000528 assert(I->second->hasAliasSet() && "Dead entry?");
Chris Lattnerab644812004-09-14 19:15:32 +0000529
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000530 AliasSet::PointerRec &Entry = getEntryFor(To);
531 if (Entry.hasAliasSet()) return; // Already in the tracker!
Chris Lattnerab644812004-09-14 19:15:32 +0000532
Xinliang David Li1ce88fa2016-08-11 23:09:56 +0000533 // getEntryFor above may invalidate iterator \c I, so reinitialize it.
Benjamin Kramere2ef47c2012-06-30 22:37:15 +0000534 I = PointerMap.find_as(From);
Xinliang David Li1ce88fa2016-08-11 23:09:56 +0000535 // Add it to the alias set it aliases...
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000536 AliasSet *AS = I->second->getAliasSet(*this);
Dan Gohman408beac2010-10-18 21:28:00 +0000537 AS->addPointer(*this, Entry, I->second->getSize(),
Hal Finkel1140e172015-10-28 22:13:41 +0000538 I->second->getAAInfo(),
Dan Gohman71af9db2010-10-18 20:44:50 +0000539 true);
Chris Lattnerab644812004-09-14 19:15:32 +0000540}
541
Michael Kuperstein41898f02016-08-19 17:05:22 +0000542AliasSet &AliasSetTracker::mergeAllAliasSets() {
543 assert(!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold) &&
544 "Full merge should happen once, when the saturation threshold is "
545 "reached");
Chris Lattnerab644812004-09-14 19:15:32 +0000546
Michael Kuperstein41898f02016-08-19 17:05:22 +0000547 // Collect all alias sets, so that we can drop references with impunity
548 // without worrying about iterator invalidation.
549 std::vector<AliasSet *> ASVector;
550 ASVector.reserve(SaturationThreshold);
551 for (iterator I = begin(), E = end(); I != E; I++)
552 ASVector.push_back(&*I);
553
554 // Copy all instructions and pointers into a new set, and forward all other
555 // sets to it.
556 AliasSets.push_back(new AliasSet());
557 AliasAnyAS = &AliasSets.back();
558 AliasAnyAS->Alias = AliasSet::SetMayAlias;
559 AliasAnyAS->Access = AliasSet::ModRefAccess;
560 AliasAnyAS->AliasAny = true;
561
562 for (auto Cur : ASVector) {
563
564 // If Cur was already forwarding, just forward to the new AS instead.
565 AliasSet *FwdTo = Cur->Forward;
566 if (FwdTo) {
567 Cur->Forward = AliasAnyAS;
568 AliasAnyAS->addRef();
569 FwdTo->dropRef(*this);
570 continue;
571 }
572
573 // Otherwise, perform the actual merge.
574 AliasAnyAS->mergeSetIn(*Cur, *this);
575 }
576
577 return *AliasAnyAS;
578}
579
580AliasSet &AliasSetTracker::addPointer(Value *P, uint64_t Size,
581 const AAMDNodes &AAInfo,
Chad Rosier16970a82016-10-19 18:50:32 +0000582 AliasSet::AccessLattice E) {
Michael Kuperstein41898f02016-08-19 17:05:22 +0000583
Chad Rosier16970a82016-10-19 18:50:32 +0000584 AliasSet &AS = getAliasSetForPointer(P, Size, AAInfo);
Michael Kuperstein41898f02016-08-19 17:05:22 +0000585 AS.Access |= E;
586
587 if (!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold)) {
588 // The AST is now saturated. From here on, we conservatively consider all
589 // pointers to alias each-other.
590 return mergeAllAliasSets();
591 }
592
593 return AS;
594}
Chris Lattner44fea542003-12-18 08:11:56 +0000595
Chris Lattner7606fb62003-02-24 20:37:56 +0000596//===----------------------------------------------------------------------===//
597// AliasSet/AliasSetTracker Printing Support
598//===----------------------------------------------------------------------===//
599
Chris Lattnerb1d782b2009-08-23 05:17:37 +0000600void AliasSet::print(raw_ostream &OS) const {
Roman Divackyad06cee2012-09-05 22:26:57 +0000601 OS << " AliasSet[" << (const void*)this << ", " << RefCount << "] ";
Chandler Carrutha561d752015-06-22 02:12:52 +0000602 OS << (Alias == SetMustAlias ? "must" : "may") << " alias, ";
603 switch (Access) {
Hal Finkel1140e172015-10-28 22:13:41 +0000604 case NoAccess: OS << "No access "; break;
605 case RefAccess: OS << "Ref "; break;
606 case ModAccess: OS << "Mod "; break;
607 case ModRefAccess: OS << "Mod/Ref "; break;
Chandler Carrutha561d752015-06-22 02:12:52 +0000608 default: llvm_unreachable("Bad value for Access!");
Chris Lattner647df642002-09-26 21:49:07 +0000609 }
Chris Lattner0a140602003-12-14 04:52:11 +0000610 if (isVolatile()) OS << "[volatile] ";
Chris Lattner7606fb62003-02-24 20:37:56 +0000611 if (Forward)
612 OS << " forwarding to " << (void*)Forward;
613
614
Dan Gohmanc731c972007-10-03 19:26:29 +0000615 if (!empty()) {
Chris Lattner7606fb62003-02-24 20:37:56 +0000616 OS << "Pointers: ";
617 for (iterator I = begin(), E = end(); I != E; ++I) {
618 if (I != begin()) OS << ", ";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000619 I.getPointer()->printAsOperand(OS << "(");
Chris Lattner053427f2004-07-22 07:58:18 +0000620 OS << ", " << I.getSize() << ")";
Chris Lattner7606fb62003-02-24 20:37:56 +0000621 }
622 }
Eli Friedmanae8161e2011-07-27 00:46:46 +0000623 if (!UnknownInsts.empty()) {
624 OS << "\n " << UnknownInsts.size() << " Unknown instructions: ";
625 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
Chris Lattner7606fb62003-02-24 20:37:56 +0000626 if (i) OS << ", ";
Sanjoy Das3f1e8e02017-03-11 01:15:48 +0000627 if (auto *I = getUnknownInst(i))
628 I->printAsOperand(OS);
Misha Brukman01808ca2005-04-21 21:13:18 +0000629 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000630 }
631 OS << "\n";
632}
633
Chris Lattnerb1d782b2009-08-23 05:17:37 +0000634void AliasSetTracker::print(raw_ostream &OS) const {
Chris Lattner7606fb62003-02-24 20:37:56 +0000635 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
636 << PointerMap.size() << " pointer values.\n";
Benjamin Krameraa209152016-06-26 17:27:42 +0000637 for (const AliasSet &AS : *this)
638 AS.print(OS);
Chris Lattner7606fb62003-02-24 20:37:56 +0000639 OS << "\n";
640}
641
Manman Ren49d684e2012-09-12 05:06:18 +0000642#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000643LLVM_DUMP_METHOD void AliasSet::dump() const { print(dbgs()); }
644LLVM_DUMP_METHOD void AliasSetTracker::dump() const { print(dbgs()); }
Manman Renc3366cc2012-09-06 19:55:56 +0000645#endif
Chris Lattner7606fb62003-02-24 20:37:56 +0000646
Chris Lattner7606fb62003-02-24 20:37:56 +0000647//===----------------------------------------------------------------------===//
Dan Gohmanf4362da2009-07-30 20:21:41 +0000648// ASTCallbackVH Class Implementation
649//===----------------------------------------------------------------------===//
650
651void AliasSetTracker::ASTCallbackVH::deleted() {
652 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
653 AST->deleteValue(getValPtr());
654 // this now dangles!
655}
656
Eli Friedman17822fc2011-04-09 06:55:46 +0000657void AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(Value *V) {
658 AST->copyValue(getValPtr(), V);
659}
660
Dan Gohmanf4362da2009-07-30 20:21:41 +0000661AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
Dan Gohmandc2b1b02009-07-31 18:21:48 +0000662 : CallbackVH(V), AST(ast) {}
663
664AliasSetTracker::ASTCallbackVH &
665AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
666 return *this = ASTCallbackVH(V, AST);
667}
Dan Gohmanf4362da2009-07-30 20:21:41 +0000668
669//===----------------------------------------------------------------------===//
Chris Lattner7606fb62003-02-24 20:37:56 +0000670// AliasSetPrinter Pass
671//===----------------------------------------------------------------------===//
672
673namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +0000674 class AliasSetPrinter : public FunctionPass {
Chris Lattner7606fb62003-02-24 20:37:56 +0000675 AliasSetTracker *Tracker;
676 public:
Nick Lewyckye7da2d62007-05-06 13:37:16 +0000677 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000678 AliasSetPrinter() : FunctionPass(ID) {
679 initializeAliasSetPrinterPass(*PassRegistry::getPassRegistry());
680 }
Devang Patel09f162c2007-05-01 21:15:47 +0000681
Craig Toppere9ba7592014-03-05 07:30:04 +0000682 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner7606fb62003-02-24 20:37:56 +0000683 AU.setPreservesAll();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000684 AU.addRequired<AAResultsWrapperPass>();
Chris Lattner7606fb62003-02-24 20:37:56 +0000685 }
686
Craig Toppere9ba7592014-03-05 07:30:04 +0000687 bool runOnFunction(Function &F) override {
Chandler Carruth7b560d42015-09-09 17:55:00 +0000688 auto &AAWP = getAnalysis<AAResultsWrapperPass>();
689 Tracker = new AliasSetTracker(AAWP.getAAResults());
Michael Kuperstein41898f02016-08-19 17:05:22 +0000690 errs() << "Alias sets for function '" << F.getName() << "':\n";
Chris Lattner7606fb62003-02-24 20:37:56 +0000691 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner2d3a7a62004-04-27 15:13:33 +0000692 Tracker->add(&*I);
David Greene0295ecf2009-12-23 22:49:57 +0000693 Tracker->print(errs());
Chris Lattner44497852006-01-03 06:05:22 +0000694 delete Tracker;
Chris Lattner7606fb62003-02-24 20:37:56 +0000695 return false;
696 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000697 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000698}
Dan Gohmand78c4002008-05-13 00:00:25 +0000699
700char AliasSetPrinter::ID = 0;
Owen Anderson8ac477f2010-10-12 19:48:12 +0000701INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets",
702 "Alias Set Printer", false, true)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000703INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Owen Anderson8ac477f2010-10-12 19:48:12 +0000704INITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000705 "Alias Set Printer", false, true)