blob: dc6ecd58b7b64173bfccf98c91b2edf3d0fcfda6 [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"
Eugene Zelenko48666a62017-07-24 23:16:33 +000016#include "llvm/Analysis/MemoryLocation.h"
Nico Weber432a3882018-04-30 14:59:11 +000017#include "llvm/Config/llvm-config.h"
Eugene Zelenko48666a62017-07-24 23:16:33 +000018#include "llvm/IR/CallSite.h"
19#include "llvm/IR/Constants.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/DataLayout.h"
Eugene Zelenko48666a62017-07-24 23:16:33 +000021#include "llvm/IR/Function.h"
Chandler Carruth83948572014-03-04 10:30:26 +000022#include "llvm/IR/InstIterator.h"
Eugene Zelenko48666a62017-07-24 23:16:33 +000023#include "llvm/IR/Instruction.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Instructions.h"
25#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000026#include "llvm/IR/Module.h"
Max Kazantsev5a10d122018-08-15 06:21:02 +000027#include "llvm/IR/PatternMatch.h"
Eugene Zelenko48666a62017-07-24 23:16:33 +000028#include "llvm/IR/Value.h"
Chris Lattner7606fb62003-02-24 20:37:56 +000029#include "llvm/Pass.h"
Eugene Zelenko48666a62017-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 Greene2ec90032009-12-23 19:27:59 +000034#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000035#include "llvm/Support/ErrorHandling.h"
Chris Lattnerb1d782b2009-08-23 05:17:37 +000036#include "llvm/Support/raw_ostream.h"
Eugene Zelenko48666a62017-07-24 23:16:33 +000037#include <cassert>
38#include <cstdint>
39#include <vector>
40
Chris Lattner0a140602003-12-14 04:52:11 +000041using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000042
Michael Kuperstein41898f02016-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 Lattner24bba4d2004-11-27 18:37:42 +000049/// mergeSetIn - Merge the specified alias set into this alias set.
Chris Lattner647df642002-09-26 21:49:07 +000050///
Chris Lattner24bba4d2004-11-27 18:37:42 +000051void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
Chris Lattner7606fb62003-02-24 20:37:56 +000052 assert(!AS.Forward && "Alias set is already forwarding!");
53 assert(!Forward && "This set is a forwarding set!!");
Chris Lattner647df642002-09-26 21:49:07 +000054
Michael Kuperstein41898f02016-08-19 17:05:22 +000055 bool WasMustAlias = (Alias == SetMustAlias);
Chris Lattner647df642002-09-26 21:49:07 +000056 // Update the alias and access types of this set...
Chandler Carrutha561d752015-06-22 02:12:52 +000057 Access |= AS.Access;
58 Alias |= AS.Alias;
Chris Lattner7606fb62003-02-24 20:37:56 +000059
Chandler Carrutha561d752015-06-22 02:12:52 +000060 if (Alias == SetMustAlias) {
Chris Lattner24bba4d2004-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 Lattner0eab5ec2009-03-09 05:11:09 +000065 PointerRec *L = getSomePointer();
66 PointerRec *R = AS.getSomePointer();
Chris Lattner24bba4d2004-11-27 18:37:42 +000067
68 // If the pointers are not a must-alias pair, this set becomes a may alias.
Chandler Carruthac80dc72015-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 Carruthc3f49eb2015-06-22 02:16:51 +000071 MustAlias)
Chandler Carrutha561d752015-06-22 02:12:52 +000072 Alias = SetMayAlias;
Chris Lattner24bba4d2004-11-27 18:37:42 +000073 }
74
Michael Kuperstein41898f02016-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 Majnemer35639382014-11-19 19:36:18 +000082 bool ASHadUnknownInsts = !AS.UnknownInsts.empty();
Eli Friedmanae8161e2011-07-27 00:46:46 +000083 if (UnknownInsts.empty()) { // Merge call sites...
David Majnemer35639382014-11-19 19:36:18 +000084 if (ASHadUnknownInsts) {
Eli Friedmanae8161e2011-07-27 00:46:46 +000085 std::swap(UnknownInsts, AS.UnknownInsts);
David Majnemerb7adf342014-11-19 09:41:05 +000086 addRef();
87 }
David Majnemer35639382014-11-19 19:36:18 +000088 } else if (ASHadUnknownInsts) {
Eli Friedmanae8161e2011-07-27 00:46:46 +000089 UnknownInsts.insert(UnknownInsts.end(), AS.UnknownInsts.begin(), AS.UnknownInsts.end());
90 AS.UnknownInsts.clear();
Chris Lattner7606fb62003-02-24 20:37:56 +000091 }
Misha Brukman01808ca2005-04-21 21:13:18 +000092
Michael Kuperstein41898f02016-08-19 17:05:22 +000093 AS.Forward = this; // Forward across AS now...
94 addRef(); // AS is now pointing to us...
Chris Lattner7606fb62003-02-24 20:37:56 +000095
96 // Merge the list of constituent pointers...
Chris Lattner44fea542003-12-18 08:11:56 +000097 if (AS.PtrList) {
Michael Kuperstein41898f02016-08-19 17:05:22 +000098 SetSize += AS.size();
99 AS.SetSize = 0;
Chris Lattner44fea542003-12-18 08:11:56 +0000100 *PtrListEnd = AS.PtrList;
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000101 AS.PtrList->setPrevInList(PtrListEnd);
Chris Lattner44fea542003-12-18 08:11:56 +0000102 PtrListEnd = AS.PtrListEnd;
103
Craig Topper9f008862014-04-15 04:59:12 +0000104 AS.PtrList = nullptr;
Chris Lattner44fea542003-12-18 08:11:56 +0000105 AS.PtrListEnd = &AS.PtrList;
Craig Topper9f008862014-04-15 04:59:12 +0000106 assert(*AS.PtrListEnd == nullptr && "End of list is not null?");
Chris Lattner44fea542003-12-18 08:11:56 +0000107 }
David Majnemerb7adf342014-11-19 09:41:05 +0000108 if (ASHadUnknownInsts)
109 AS.dropRef(AST);
Chris Lattner647df642002-09-26 21:49:07 +0000110}
111
Chris Lattner7606fb62003-02-24 20:37:56 +0000112void AliasSetTracker::removeAliasSet(AliasSet *AS) {
Chris Lattner053427f2004-07-22 07:58:18 +0000113 if (AliasSet *Fwd = AS->Forward) {
114 Fwd->dropRef(*this);
Craig Topper9f008862014-04-15 04:59:12 +0000115 AS->Forward = nullptr;
Chris Lattner053427f2004-07-22 07:58:18 +0000116 }
Michael Kuperstein41898f02016-08-19 17:05:22 +0000117
118 if (AS->Alias == AliasSet::SetMayAlias)
119 TotalMayAliasSetSize -= AS->size();
120
Chris Lattner7606fb62003-02-24 20:37:56 +0000121 AliasSets.erase(AS);
122}
123
124void AliasSet::removeFromTracker(AliasSetTracker &AST) {
125 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
126 AST.removeAliasSet(this);
127}
128
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000129void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
George Burgess IV319be3a2018-05-25 21:16:58 +0000130 LocationSize Size, const AAMDNodes &AAInfo,
Hal Finkel1140e172015-10-28 22:13:41 +0000131 bool KnownMustAlias) {
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000132 assert(!Entry.hasAliasSet() && "Entry already in set!");
Chris Lattner7606fb62003-02-24 20:37:56 +0000133
Chris Lattnerab644812004-09-14 19:15:32 +0000134 // Check to see if we have to downgrade to _may_ alias.
135 if (isMustAlias() && !KnownMustAlias)
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000136 if (PointerRec *P = getSomePointer()) {
Chris Lattner6fa96652004-09-15 16:59:47 +0000137 AliasAnalysis &AA = AST.getAliasAnalysis();
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000138 AliasResult Result =
Chandler Carruthac80dc72015-06-17 07:18:54 +0000139 AA.alias(MemoryLocation(P->getValue(), P->getSize(), P->getAAInfo()),
140 MemoryLocation(Entry.getValue(), Size, AAInfo));
Michael Kuperstein41898f02016-08-19 17:05:22 +0000141 if (Result != MustAlias) {
Chandler Carrutha561d752015-06-22 02:12:52 +0000142 Alias = SetMayAlias;
Michael Kuperstein41898f02016-08-19 17:05:22 +0000143 AST.TotalMayAliasSetSize += size();
144 } else {
Fangrui Songf78650a2018-07-30 19:41:25 +0000145 // First entry of must alias must have maximum size!
Hal Finkelcc39b672014-07-24 12:16:19 +0000146 P->updateSizeAndAAInfo(Size, AAInfo);
Michael Kuperstein41898f02016-08-19 17:05:22 +0000147 }
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000148 assert(Result != NoAlias && "Cannot be part of must set!");
Chris Lattnerb5b0b7a2003-02-26 22:11:00 +0000149 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000150
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000151 Entry.setAliasSet(this);
Hal Finkelcc39b672014-07-24 12:16:19 +0000152 Entry.updateSizeAndAAInfo(Size, AAInfo);
Chris Lattner7606fb62003-02-24 20:37:56 +0000153
154 // Add it to the end of the list...
Michael Kuperstein41898f02016-08-19 17:05:22 +0000155 ++SetSize;
Craig Topper9f008862014-04-15 04:59:12 +0000156 assert(*PtrListEnd == nullptr && "End of list is not null?");
Chris Lattner44fea542003-12-18 08:11:56 +0000157 *PtrListEnd = &Entry;
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000158 PtrListEnd = Entry.setPrevInList(PtrListEnd);
Craig Topper9f008862014-04-15 04:59:12 +0000159 assert(*PtrListEnd == nullptr && "End of list is not null?");
Michael Kuperstein41898f02016-08-19 17:05:22 +0000160 // Entry points to alias set.
161 addRef();
162
163 if (Alias == SetMayAlias)
164 AST.TotalMayAliasSetSize++;
Chris Lattner7606fb62003-02-24 20:37:56 +0000165}
166
Hal Finkel1140e172015-10-28 22:13:41 +0000167void AliasSet::addUnknownInst(Instruction *I, AliasAnalysis &AA) {
David Majnemerb7adf342014-11-19 09:41:05 +0000168 if (UnknownInsts.empty())
169 addRef();
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000170 UnknownInsts.emplace_back(I);
Chris Lattner21c60f12004-03-15 04:08:36 +0000171
Max Kazantsev5a10d122018-08-15 06:21:02 +0000172 // Guards are marked as modifying memory for control flow modelling purposes,
173 // but don't actually modify any specific memory location.
174 using namespace PatternMatch;
175 bool MayWriteMemory = I->mayWriteToMemory() &&
Philip Reamesa5a85462018-08-21 00:55:35 +0000176 !match(I, m_Intrinsic<Intrinsic::experimental_guard>()) &&
177 !(I->use_empty() && match(I, m_Intrinsic<Intrinsic::invariant_start>()));
Max Kazantsev5a10d122018-08-15 06:21:02 +0000178 if (!MayWriteMemory) {
Chandler Carrutha561d752015-06-22 02:12:52 +0000179 Alias = SetMayAlias;
Hal Finkel1140e172015-10-28 22:13:41 +0000180 Access |= RefAccess;
Duncan Sands68b6f502007-12-01 07:51:45 +0000181 return;
Chris Lattner21c60f12004-03-15 04:08:36 +0000182 }
183
Hal Finkel1140e172015-10-28 22:13:41 +0000184 // FIXME: This should use mod/ref information to make this not suck so bad
Chandler Carrutha561d752015-06-22 02:12:52 +0000185 Alias = SetMayAlias;
Hal Finkel1140e172015-10-28 22:13:41 +0000186 Access = ModRefAccess;
Chris Lattner7606fb62003-02-24 20:37:56 +0000187}
188
189/// aliasesPointer - Return true if the specified pointer "may" (or must)
Chris Lattner647df642002-09-26 21:49:07 +0000190/// alias one of the members in the set.
191///
George Burgess IV319be3a2018-05-25 21:16:58 +0000192bool AliasSet::aliasesPointer(const Value *Ptr, LocationSize Size,
Hal Finkelcc39b672014-07-24 12:16:19 +0000193 const AAMDNodes &AAInfo,
Hal Finkel1140e172015-10-28 22:13:41 +0000194 AliasAnalysis &AA) const {
Michael Kuperstein41898f02016-08-19 17:05:22 +0000195 if (AliasAny)
196 return true;
197
Chandler Carrutha561d752015-06-22 02:12:52 +0000198 if (Alias == SetMustAlias) {
Eli Friedmanae8161e2011-07-27 00:46:46 +0000199 assert(UnknownInsts.empty() && "Illegal must alias set!");
Chris Lattner7f04ebc2004-03-15 06:28:07 +0000200
Chris Lattner7606fb62003-02-24 20:37:56 +0000201 // If this is a set of MustAliases, only check to see if the pointer aliases
Chris Lattnereef6b192010-08-29 04:13:43 +0000202 // SOME value in the set.
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000203 PointerRec *SomePtr = getSomePointer();
Chris Lattner7606fb62003-02-24 20:37:56 +0000204 assert(SomePtr && "Empty must-alias set??");
Chandler Carruthac80dc72015-06-17 07:18:54 +0000205 return AA.alias(MemoryLocation(SomePtr->getValue(), SomePtr->getSize(),
206 SomePtr->getAAInfo()),
207 MemoryLocation(Ptr, Size, AAInfo));
Chris Lattner7606fb62003-02-24 20:37:56 +0000208 }
209
210 // If this is a may-alias set, we have to check all of the pointers in the set
211 // to be sure it doesn't alias the set...
212 for (iterator I = begin(), E = end(); I != E; ++I)
Chandler Carruthac80dc72015-06-17 07:18:54 +0000213 if (AA.alias(MemoryLocation(Ptr, Size, AAInfo),
214 MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo())))
Chris Lattner7606fb62003-02-24 20:37:56 +0000215 return true;
216
Eli Friedmanae8161e2011-07-27 00:46:46 +0000217 // Check the unknown instructions...
218 if (!UnknownInsts.empty()) {
219 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i)
Sanjoy Das3f1e8e02017-03-11 01:15:48 +0000220 if (auto *Inst = getUnknownInst(i))
Alina Sbirlea63d22502017-12-05 20:12:23 +0000221 if (isModOrRefSet(
222 AA.getModRefInfo(Inst, MemoryLocation(Ptr, Size, AAInfo))))
Sanjoy Das3f1e8e02017-03-11 01:15:48 +0000223 return true;
Chris Lattner9b323c32004-07-27 02:20:26 +0000224 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000225
Hal Finkel1140e172015-10-28 22:13:41 +0000226 return false;
Chris Lattner647df642002-09-26 21:49:07 +0000227}
228
Pete Cooper4bf388d2015-05-13 01:12:12 +0000229bool AliasSet::aliasesUnknownInst(const Instruction *Inst,
Hal Finkel1140e172015-10-28 22:13:41 +0000230 AliasAnalysis &AA) const {
Michael Kuperstein41898f02016-08-19 17:05:22 +0000231
232 if (AliasAny)
233 return true;
234
Eli Friedmanae8161e2011-07-27 00:46:46 +0000235 if (!Inst->mayReadOrWriteMemory())
Duncan Sands68b6f502007-12-01 07:51:45 +0000236 return false;
Chris Lattner21c60f12004-03-15 04:08:36 +0000237
Eli Friedmanae8161e2011-07-27 00:46:46 +0000238 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
Xin Tong70f75122017-06-25 12:55:11 +0000239 if (auto *UnknownInst = getUnknownInst(i)) {
240 ImmutableCallSite C1(UnknownInst), C2(Inst);
Alina Sbirlea63d22502017-12-05 20:12:23 +0000241 if (!C1 || !C2 || isModOrRefSet(AA.getModRefInfo(C1, C2)) ||
242 isModOrRefSet(AA.getModRefInfo(C2, C1)))
Sanjoy Das3f1e8e02017-03-11 01:15:48 +0000243 return true;
244 }
Chris Lattnerf58382e2010-08-29 18:42:23 +0000245 }
Chris Lattner9b323c32004-07-27 02:20:26 +0000246
Hal Finkel1140e172015-10-28 22:13:41 +0000247 for (iterator I = begin(), E = end(); I != E; ++I)
Alina Sbirlea63d22502017-12-05 20:12:23 +0000248 if (isModOrRefSet(AA.getModRefInfo(
249 Inst, MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo()))))
Hal Finkel1140e172015-10-28 22:13:41 +0000250 return true;
251
252 return false;
Chris Lattner647df642002-09-26 21:49:07 +0000253}
254
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000255void AliasSetTracker::clear() {
256 // Delete all the PointerRec entries.
Dan Gohmanf4362da2009-07-30 20:21:41 +0000257 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
258 I != E; ++I)
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000259 I->second->eraseFromList();
Fangrui Songf78650a2018-07-30 19:41:25 +0000260
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000261 PointerMap.clear();
Fangrui Songf78650a2018-07-30 19:41:25 +0000262
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000263 // The alias sets should all be clear now.
264 AliasSets.clear();
265}
266
Chris Lattner647df642002-09-26 21:49:07 +0000267
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000268/// mergeAliasSetsForPointer - Given a pointer, merge all alias sets that may
269/// alias the pointer. Return the unified set, or nullptr if no set that aliases
270/// the pointer was found.
271AliasSet *AliasSetTracker::mergeAliasSetsForPointer(const Value *Ptr,
George Burgess IV319be3a2018-05-25 21:16:58 +0000272 LocationSize Size,
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000273 const AAMDNodes &AAInfo) {
Craig Topper9f008862014-04-15 04:59:12 +0000274 AliasSet *FoundSet = nullptr;
David Majnemerb7adf342014-11-19 09:41:05 +0000275 for (iterator I = begin(), E = end(); I != E;) {
276 iterator Cur = I++;
Hal Finkel1140e172015-10-28 22:13:41 +0000277 if (Cur->Forward || !Cur->aliasesPointer(Ptr, Size, AAInfo, AA)) continue;
Fangrui Songf78650a2018-07-30 19:41:25 +0000278
Craig Topper9f008862014-04-15 04:59:12 +0000279 if (!FoundSet) { // If this is the first alias set ptr can go into.
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000280 FoundSet = &*Cur; // Remember it.
Chris Lattnerafb70742010-08-29 04:06:55 +0000281 } else { // Otherwise, we must merge the sets.
David Majnemerb7adf342014-11-19 09:41:05 +0000282 FoundSet->mergeSetIn(*Cur, *this); // Merge in contents.
Chris Lattner647df642002-09-26 21:49:07 +0000283 }
Chris Lattnerafb70742010-08-29 04:06:55 +0000284 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000285
286 return FoundSet;
287}
288
Pete Cooper4bf388d2015-05-13 01:12:12 +0000289bool AliasSetTracker::containsUnknown(const Instruction *Inst) const {
Benjamin Krameraa209152016-06-26 17:27:42 +0000290 for (const AliasSet &AS : *this)
291 if (!AS.Forward && AS.aliasesUnknownInst(Inst, AA))
Hal Finkel840257a2014-11-03 23:19:16 +0000292 return true;
293 return false;
294}
Chris Lattnereeaa29c2004-11-26 21:36:25 +0000295
Hal Finkel1140e172015-10-28 22:13:41 +0000296AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) {
Craig Topper9f008862014-04-15 04:59:12 +0000297 AliasSet *FoundSet = nullptr;
David Majnemerb7adf342014-11-19 09:41:05 +0000298 for (iterator I = begin(), E = end(); I != E;) {
299 iterator Cur = I++;
Hal Finkel1140e172015-10-28 22:13:41 +0000300 if (Cur->Forward || !Cur->aliasesUnknownInst(Inst, AA))
Chris Lattnerafb70742010-08-29 04:06:55 +0000301 continue;
Craig Topper9f008862014-04-15 04:59:12 +0000302 if (!FoundSet) // If this is the first alias set ptr can go into.
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000303 FoundSet = &*Cur; // Remember it.
David Majnemerb7adf342014-11-19 09:41:05 +0000304 else if (!Cur->Forward) // Otherwise, we must merge the sets.
305 FoundSet->mergeSetIn(*Cur, *this); // Merge in contents.
Chris Lattnerafb70742010-08-29 04:06:55 +0000306 }
Chris Lattner647df642002-09-26 21:49:07 +0000307 return FoundSet;
308}
309
Philip Reames0e2f9b92018-08-16 20:11:15 +0000310AliasSet &AliasSetTracker::getAliasSetFor(const MemoryLocation &MemLoc) {
311
312 Value * const Pointer = const_cast<Value*>(MemLoc.Ptr);
313 const LocationSize Size = MemLoc.Size;
314 const AAMDNodes &AAInfo = MemLoc.AATags;
315
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000316 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
Chris Lattner7606fb62003-02-24 20:37:56 +0000317
Michael Kuperstein41898f02016-08-19 17:05:22 +0000318 if (AliasAnyAS) {
319 // At this point, the AST is saturated, so we only have one active alias
320 // set. That means we already know which alias set we want to return, and
321 // just need to add the pointer to that set to keep the data structure
322 // consistent.
323 // This, of course, means that we will never need a merge here.
324 if (Entry.hasAliasSet()) {
325 Entry.updateSizeAndAAInfo(Size, AAInfo);
326 assert(Entry.getAliasSet(*this) == AliasAnyAS &&
327 "Entry in saturated AST must belong to only alias set");
328 } else {
329 AliasAnyAS->addPointer(*this, Entry, Size, AAInfo);
330 }
331 return *AliasAnyAS;
332 }
333
Chris Lattnereef6b192010-08-29 04:13:43 +0000334 // Check to see if the pointer is already known.
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000335 if (Entry.hasAliasSet()) {
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000336 // If the size changed, we may need to merge several alias sets.
337 // Note that we can *not* return the result of mergeAliasSetsForPointer
338 // due to a quirk of alias analysis behavior. Since alias(undef, undef)
339 // is NoAlias, mergeAliasSetsForPointer(undef, ...) will not find the
340 // the right set for undef, even if it exists.
341 if (Entry.updateSizeAndAAInfo(Size, AAInfo))
342 mergeAliasSetsForPointer(Pointer, Size, AAInfo);
Chris Lattner7606fb62003-02-24 20:37:56 +0000343 // Return the set!
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000344 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattnerafb70742010-08-29 04:06:55 +0000345 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000346
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000347 if (AliasSet *AS = mergeAliasSetsForPointer(Pointer, Size, AAInfo)) {
Chris Lattnereef6b192010-08-29 04:13:43 +0000348 // Add it to the alias set it aliases.
Hal Finkel1140e172015-10-28 22:13:41 +0000349 AS->addPointer(*this, Entry, Size, AAInfo);
Chris Lattner7606fb62003-02-24 20:37:56 +0000350 return *AS;
Chris Lattner647df642002-09-26 21:49:07 +0000351 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000352
Chris Lattnereef6b192010-08-29 04:13:43 +0000353 // Otherwise create a new alias set to hold the loaded pointer.
Chris Lattnerafb70742010-08-29 04:06:55 +0000354 AliasSets.push_back(new AliasSet());
Hal Finkel1140e172015-10-28 22:13:41 +0000355 AliasSets.back().addPointer(*this, Entry, Size, AAInfo);
Chris Lattnerafb70742010-08-29 04:06:55 +0000356 return AliasSets.back();
Chris Lattner647df642002-09-26 21:49:07 +0000357}
358
George Burgess IV319be3a2018-05-25 21:16:58 +0000359void AliasSetTracker::add(Value *Ptr, LocationSize Size,
360 const AAMDNodes &AAInfo) {
Chad Rosier16970a82016-10-19 18:50:32 +0000361 addPointer(Ptr, Size, AAInfo, AliasSet::NoAccess);
Chris Lattnerbf8c3c42004-07-26 05:50:23 +0000362}
363
Chad Rosier16970a82016-10-19 18:50:32 +0000364void AliasSetTracker::add(LoadInst *LI) {
JF Bastien800f87a2016-04-06 21:19:33 +0000365 if (isStrongerThanMonotonic(LI->getOrdering())) return addUnknown(LI);
Hal Finkelcc39b672014-07-24 12:16:19 +0000366
Philip Reamesc3c23e82018-08-21 17:59:11 +0000367 addPointer(MemoryLocation::get(LI), AliasSet::RefAccess);
Chris Lattner7606fb62003-02-24 20:37:56 +0000368}
369
Chad Rosier16970a82016-10-19 18:50:32 +0000370void AliasSetTracker::add(StoreInst *SI) {
JF Bastien800f87a2016-04-06 21:19:33 +0000371 if (isStrongerThanMonotonic(SI->getOrdering())) return addUnknown(SI);
Hal Finkelcc39b672014-07-24 12:16:19 +0000372
Philip Reamesc3c23e82018-08-21 17:59:11 +0000373 addPointer(MemoryLocation::get(SI), AliasSet::ModAccess);
Chris Lattner647df642002-09-26 21:49:07 +0000374}
375
Chad Rosier16970a82016-10-19 18:50:32 +0000376void AliasSetTracker::add(VAArgInst *VAAI) {
Philip Reames90bffb32018-08-13 22:34:14 +0000377 addPointer(MemoryLocation::get(VAAI), AliasSet::ModRefAccess);
Dan Gohman2cd8e382008-04-14 18:34:50 +0000378}
379
Daniel Neilson6b23fb72018-05-30 14:43:39 +0000380void AliasSetTracker::add(AnyMemSetInst *MSI) {
Philip Reames0f396692018-08-13 22:25:16 +0000381 auto MemLoc = MemoryLocation::getForDest(MSI);
Philip Reamesc3c23e82018-08-21 17:59:11 +0000382 addPointer(MemLoc, AliasSet::ModAccess);
Haicheng Wu5cf99092016-02-17 02:01:50 +0000383}
Chris Lattner0a140602003-12-14 04:52:11 +0000384
Daniel Neilson6b23fb72018-05-30 14:43:39 +0000385void AliasSetTracker::add(AnyMemTransferInst *MTI) {
Philip Reames0f396692018-08-13 22:25:16 +0000386 auto SrcLoc = MemoryLocation::getForSource(MTI);
Philip Reamesc3c23e82018-08-21 17:59:11 +0000387 addPointer(SrcLoc, AliasSet::RefAccess);
Chad Rosier6e3a92e2016-10-19 19:09:03 +0000388
Philip Reames90bffb32018-08-13 22:34:14 +0000389 auto DestLoc = MemoryLocation::getForDest(MTI);
Philip Reamesc3c23e82018-08-21 17:59:11 +0000390 addPointer(DestLoc, AliasSet::ModAccess);
Chad Rosier6e3a92e2016-10-19 19:09:03 +0000391}
392
Chad Rosier16970a82016-10-19 18:50:32 +0000393void AliasSetTracker::addUnknown(Instruction *Inst) {
394 if (isa<DbgInfoIntrinsic>(Inst))
395 return; // Ignore DbgInfo Intrinsics.
Chad Rosier8f348012016-11-07 14:11:45 +0000396
397 if (auto *II = dyn_cast<IntrinsicInst>(Inst)) {
398 // These intrinsics will show up as affecting memory, but they are just
399 // markers.
400 switch (II->getIntrinsicID()) {
401 default:
402 break;
403 // FIXME: Add lifetime/invariant intrinsics (See: PR30807).
404 case Intrinsic::assume:
Dan Gohman2c74fe92017-11-08 21:59:51 +0000405 case Intrinsic::sideeffect:
Chad Rosier8f348012016-11-07 14:11:45 +0000406 return;
407 }
408 }
Eli Friedmanae8161e2011-07-27 00:46:46 +0000409 if (!Inst->mayReadOrWriteMemory())
Chad Rosier16970a82016-10-19 18:50:32 +0000410 return; // doesn't alias anything
Chris Lattner7f04ebc2004-03-15 06:28:07 +0000411
Hal Finkel1140e172015-10-28 22:13:41 +0000412 AliasSet *AS = findAliasSetForUnknownInst(Inst);
Chris Lattnerafb70742010-08-29 04:06:55 +0000413 if (AS) {
Hal Finkel1140e172015-10-28 22:13:41 +0000414 AS->addUnknownInst(Inst, AA);
Chad Rosier16970a82016-10-19 18:50:32 +0000415 return;
Chris Lattner7606fb62003-02-24 20:37:56 +0000416 }
Chris Lattnerafb70742010-08-29 04:06:55 +0000417 AliasSets.push_back(new AliasSet());
418 AS = &AliasSets.back();
Hal Finkel1140e172015-10-28 22:13:41 +0000419 AS->addUnknownInst(Inst, AA);
Chris Lattner647df642002-09-26 21:49:07 +0000420}
421
Chad Rosier16970a82016-10-19 18:50:32 +0000422void AliasSetTracker::add(Instruction *I) {
Chris Lattnerafb70742010-08-29 04:06:55 +0000423 // Dispatch to one of the other add methods.
Chris Lattner7606fb62003-02-24 20:37:56 +0000424 if (LoadInst *LI = dyn_cast<LoadInst>(I))
Chris Lattner2cfaef22004-07-21 05:18:04 +0000425 return add(LI);
Chris Lattnerafb70742010-08-29 04:06:55 +0000426 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner2cfaef22004-07-21 05:18:04 +0000427 return add(SI);
Chris Lattnerafb70742010-08-29 04:06:55 +0000428 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
Dan Gohman2cd8e382008-04-14 18:34:50 +0000429 return add(VAAI);
Daniel Neilson6b23fb72018-05-30 14:43:39 +0000430 if (AnyMemSetInst *MSI = dyn_cast<AnyMemSetInst>(I))
Haicheng Wu5cf99092016-02-17 02:01:50 +0000431 return add(MSI);
Daniel Neilson6b23fb72018-05-30 14:43:39 +0000432 if (AnyMemTransferInst *MTI = dyn_cast<AnyMemTransferInst>(I))
Chad Rosier6e3a92e2016-10-19 19:09:03 +0000433 return add(MTI);
Eli Friedmanae8161e2011-07-27 00:46:46 +0000434 return addUnknown(I);
Chris Lattner647df642002-09-26 21:49:07 +0000435}
436
Chris Lattnerc048bb32003-03-03 23:28:05 +0000437void AliasSetTracker::add(BasicBlock &BB) {
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000438 for (auto &I : BB)
439 add(&I);
Chris Lattnerc048bb32003-03-03 23:28:05 +0000440}
441
442void AliasSetTracker::add(const AliasSetTracker &AST) {
443 assert(&AA == &AST.AA &&
444 "Merging AliasSetTracker objects with different Alias Analyses!");
445
446 // Loop over all of the alias sets in AST, adding the pointers contained
447 // therein into the current alias sets. This can cause alias sets to be
448 // merged together in the current AST.
Benjamin Krameraa209152016-06-26 17:27:42 +0000449 for (const AliasSet &AS : AST) {
450 if (AS.Forward)
451 continue; // Ignore forwarding alias sets
Chris Lattnerc048bb32003-03-03 23:28:05 +0000452
Chris Lattnerafb70742010-08-29 04:06:55 +0000453 // If there are any call sites in the alias set, add them to this AST.
Eli Friedmanae8161e2011-07-27 00:46:46 +0000454 for (unsigned i = 0, e = AS.UnknownInsts.size(); i != e; ++i)
Sanjoy Das3f1e8e02017-03-11 01:15:48 +0000455 if (auto *Inst = AS.getUnknownInst(i))
456 add(Inst);
Chris Lattnerc048bb32003-03-03 23:28:05 +0000457
Chris Lattnerafb70742010-08-29 04:06:55 +0000458 // Loop over all of the pointers in this alias set.
Philip Reamesc3c23e82018-08-21 17:59:11 +0000459 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI)
460 addPointer(ASI.getPointer(), ASI.getSize(), ASI.getAAInfo(),
461 (AliasSet::AccessLattice)AS.Access);
Chris Lattnerafb70742010-08-29 04:06:55 +0000462 }
Chris Lattnerc048bb32003-03-03 23:28:05 +0000463}
464
Chris Lattner746e1e12004-05-23 21:10:58 +0000465// deleteValue method - This method is used to remove a pointer value from the
Chris Lattner44fea542003-12-18 08:11:56 +0000466// AliasSetTracker entirely. It should be used when an instruction is deleted
467// from the program to update the AST. If you don't use this, you would have
468// dangling pointers to deleted instructions.
469//
Chris Lattner746e1e12004-05-23 21:10:58 +0000470void AliasSetTracker::deleteValue(Value *PtrVal) {
Chris Lattnerab644812004-09-14 19:15:32 +0000471 // First, look up the PointerRec for this pointer.
Benjamin Kramere2ef47c2012-06-30 22:37:15 +0000472 PointerMapType::iterator I = PointerMap.find_as(PtrVal);
Chris Lattner44fea542003-12-18 08:11:56 +0000473 if (I == PointerMap.end()) return; // Noop
474
475 // If we found one, remove the pointer from the alias set it is in.
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000476 AliasSet::PointerRec *PtrValEnt = I->second;
477 AliasSet *AS = PtrValEnt->getAliasSet(*this);
Chris Lattner44fea542003-12-18 08:11:56 +0000478
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000479 // Unlink and delete from the list of values.
480 PtrValEnt->eraseFromList();
Michael Kuperstein41898f02016-08-19 17:05:22 +0000481
482 if (AS->Alias == AliasSet::SetMayAlias) {
483 AS->SetSize--;
484 TotalMayAliasSetSize--;
485 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000486
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000487 // Stop using the alias set.
Chris Lattner053427f2004-07-22 07:58:18 +0000488 AS->dropRef(*this);
Fangrui Songf78650a2018-07-30 19:41:25 +0000489
Chris Lattner44fea542003-12-18 08:11:56 +0000490 PointerMap.erase(I);
491}
492
Chris Lattnerab644812004-09-14 19:15:32 +0000493// copyValue - This method should be used whenever a preexisting value in the
494// program is copied or cloned, introducing a new value. Note that it is ok for
495// clients that use this method to introduce the same value multiple times: if
496// the tracker already knows about a value, it will ignore the request.
497//
498void AliasSetTracker::copyValue(Value *From, Value *To) {
Chris Lattnerab644812004-09-14 19:15:32 +0000499 // First, look up the PointerRec for this pointer.
Benjamin Kramere2ef47c2012-06-30 22:37:15 +0000500 PointerMapType::iterator I = PointerMap.find_as(From);
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000501 if (I == PointerMap.end())
Chris Lattnerab644812004-09-14 19:15:32 +0000502 return; // Noop
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000503 assert(I->second->hasAliasSet() && "Dead entry?");
Chris Lattnerab644812004-09-14 19:15:32 +0000504
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000505 AliasSet::PointerRec &Entry = getEntryFor(To);
506 if (Entry.hasAliasSet()) return; // Already in the tracker!
Chris Lattnerab644812004-09-14 19:15:32 +0000507
Xinliang David Li1ce88fa2016-08-11 23:09:56 +0000508 // getEntryFor above may invalidate iterator \c I, so reinitialize it.
Benjamin Kramere2ef47c2012-06-30 22:37:15 +0000509 I = PointerMap.find_as(From);
Xinliang David Li1ce88fa2016-08-11 23:09:56 +0000510 // Add it to the alias set it aliases...
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000511 AliasSet *AS = I->second->getAliasSet(*this);
Dan Gohman408beac2010-10-18 21:28:00 +0000512 AS->addPointer(*this, Entry, I->second->getSize(),
Hal Finkel1140e172015-10-28 22:13:41 +0000513 I->second->getAAInfo(),
Dan Gohman71af9db2010-10-18 20:44:50 +0000514 true);
Chris Lattnerab644812004-09-14 19:15:32 +0000515}
516
Michael Kuperstein41898f02016-08-19 17:05:22 +0000517AliasSet &AliasSetTracker::mergeAllAliasSets() {
518 assert(!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold) &&
519 "Full merge should happen once, when the saturation threshold is "
520 "reached");
Chris Lattnerab644812004-09-14 19:15:32 +0000521
Michael Kuperstein41898f02016-08-19 17:05:22 +0000522 // Collect all alias sets, so that we can drop references with impunity
523 // without worrying about iterator invalidation.
524 std::vector<AliasSet *> ASVector;
525 ASVector.reserve(SaturationThreshold);
526 for (iterator I = begin(), E = end(); I != E; I++)
527 ASVector.push_back(&*I);
528
529 // Copy all instructions and pointers into a new set, and forward all other
530 // sets to it.
531 AliasSets.push_back(new AliasSet());
532 AliasAnyAS = &AliasSets.back();
533 AliasAnyAS->Alias = AliasSet::SetMayAlias;
534 AliasAnyAS->Access = AliasSet::ModRefAccess;
535 AliasAnyAS->AliasAny = true;
536
537 for (auto Cur : ASVector) {
Michael Kuperstein41898f02016-08-19 17:05:22 +0000538 // If Cur was already forwarding, just forward to the new AS instead.
539 AliasSet *FwdTo = Cur->Forward;
540 if (FwdTo) {
541 Cur->Forward = AliasAnyAS;
Alina Sbirlea63d22502017-12-05 20:12:23 +0000542 AliasAnyAS->addRef();
Michael Kuperstein41898f02016-08-19 17:05:22 +0000543 FwdTo->dropRef(*this);
544 continue;
545 }
546
547 // Otherwise, perform the actual merge.
548 AliasAnyAS->mergeSetIn(*Cur, *this);
549 }
550
551 return *AliasAnyAS;
552}
553
George Burgess IV319be3a2018-05-25 21:16:58 +0000554AliasSet &AliasSetTracker::addPointer(Value *P, LocationSize Size,
Michael Kuperstein41898f02016-08-19 17:05:22 +0000555 const AAMDNodes &AAInfo,
Chad Rosier16970a82016-10-19 18:50:32 +0000556 AliasSet::AccessLattice E) {
Philip Reames0e2f9b92018-08-16 20:11:15 +0000557 AliasSet &AS = getAliasSetFor(MemoryLocation(P, Size, AAInfo));
Michael Kuperstein41898f02016-08-19 17:05:22 +0000558 AS.Access |= E;
559
560 if (!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold)) {
561 // The AST is now saturated. From here on, we conservatively consider all
562 // pointers to alias each-other.
563 return mergeAllAliasSets();
564 }
565
566 return AS;
567}
Chris Lattner44fea542003-12-18 08:11:56 +0000568
Chris Lattner7606fb62003-02-24 20:37:56 +0000569//===----------------------------------------------------------------------===//
570// AliasSet/AliasSetTracker Printing Support
571//===----------------------------------------------------------------------===//
572
Chris Lattnerb1d782b2009-08-23 05:17:37 +0000573void AliasSet::print(raw_ostream &OS) const {
Roman Divackyad06cee2012-09-05 22:26:57 +0000574 OS << " AliasSet[" << (const void*)this << ", " << RefCount << "] ";
Chandler Carrutha561d752015-06-22 02:12:52 +0000575 OS << (Alias == SetMustAlias ? "must" : "may") << " alias, ";
576 switch (Access) {
Hal Finkel1140e172015-10-28 22:13:41 +0000577 case NoAccess: OS << "No access "; break;
578 case RefAccess: OS << "Ref "; break;
579 case ModAccess: OS << "Mod "; break;
580 case ModRefAccess: OS << "Mod/Ref "; break;
Chandler Carrutha561d752015-06-22 02:12:52 +0000581 default: llvm_unreachable("Bad value for Access!");
Chris Lattner647df642002-09-26 21:49:07 +0000582 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000583 if (Forward)
584 OS << " forwarding to " << (void*)Forward;
585
Dan Gohmanc731c972007-10-03 19:26:29 +0000586 if (!empty()) {
Chris Lattner7606fb62003-02-24 20:37:56 +0000587 OS << "Pointers: ";
588 for (iterator I = begin(), E = end(); I != E; ++I) {
589 if (I != begin()) OS << ", ";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000590 I.getPointer()->printAsOperand(OS << "(");
Philip Reames96bc0762018-08-17 23:17:31 +0000591 if (I.getSize() == MemoryLocation::UnknownSize)
592 OS << ", unknown)";
593 else
594 OS << ", " << I.getSize() << ")";
Chris Lattner7606fb62003-02-24 20:37:56 +0000595 }
596 }
Eli Friedmanae8161e2011-07-27 00:46:46 +0000597 if (!UnknownInsts.empty()) {
598 OS << "\n " << UnknownInsts.size() << " Unknown instructions: ";
599 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
Chris Lattner7606fb62003-02-24 20:37:56 +0000600 if (i) OS << ", ";
Jakub Kuderski555e41b2018-06-27 16:34:30 +0000601 if (auto *I = getUnknownInst(i)) {
602 if (I->hasName())
603 I->printAsOperand(OS);
604 else
605 I->print(OS);
606 }
Misha Brukman01808ca2005-04-21 21:13:18 +0000607 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000608 }
609 OS << "\n";
610}
611
Chris Lattnerb1d782b2009-08-23 05:17:37 +0000612void AliasSetTracker::print(raw_ostream &OS) const {
Chris Lattner7606fb62003-02-24 20:37:56 +0000613 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
614 << PointerMap.size() << " pointer values.\n";
Benjamin Krameraa209152016-06-26 17:27:42 +0000615 for (const AliasSet &AS : *this)
616 AS.print(OS);
Chris Lattner7606fb62003-02-24 20:37:56 +0000617 OS << "\n";
618}
619
Aaron Ballman615eb472017-10-15 14:32:27 +0000620#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000621LLVM_DUMP_METHOD void AliasSet::dump() const { print(dbgs()); }
622LLVM_DUMP_METHOD void AliasSetTracker::dump() const { print(dbgs()); }
Manman Renc3366cc2012-09-06 19:55:56 +0000623#endif
Chris Lattner7606fb62003-02-24 20:37:56 +0000624
Chris Lattner7606fb62003-02-24 20:37:56 +0000625//===----------------------------------------------------------------------===//
Dan Gohmanf4362da2009-07-30 20:21:41 +0000626// ASTCallbackVH Class Implementation
627//===----------------------------------------------------------------------===//
628
629void AliasSetTracker::ASTCallbackVH::deleted() {
630 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
631 AST->deleteValue(getValPtr());
632 // this now dangles!
633}
634
Eli Friedman17822fc2011-04-09 06:55:46 +0000635void AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(Value *V) {
636 AST->copyValue(getValPtr(), V);
637}
638
Dan Gohmanf4362da2009-07-30 20:21:41 +0000639AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
Dan Gohmandc2b1b02009-07-31 18:21:48 +0000640 : CallbackVH(V), AST(ast) {}
641
642AliasSetTracker::ASTCallbackVH &
643AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
644 return *this = ASTCallbackVH(V, AST);
645}
Dan Gohmanf4362da2009-07-30 20:21:41 +0000646
647//===----------------------------------------------------------------------===//
Chris Lattner7606fb62003-02-24 20:37:56 +0000648// AliasSetPrinter Pass
649//===----------------------------------------------------------------------===//
650
651namespace {
Eugene Zelenko48666a62017-07-24 23:16:33 +0000652
Nick Lewycky02d5f772009-10-25 06:33:48 +0000653 class AliasSetPrinter : public FunctionPass {
Chris Lattner7606fb62003-02-24 20:37:56 +0000654 AliasSetTracker *Tracker;
Eugene Zelenko48666a62017-07-24 23:16:33 +0000655
Chris Lattner7606fb62003-02-24 20:37:56 +0000656 public:
Nick Lewyckye7da2d62007-05-06 13:37:16 +0000657 static char ID; // Pass identification, replacement for typeid
Eugene Zelenko48666a62017-07-24 23:16:33 +0000658
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000659 AliasSetPrinter() : FunctionPass(ID) {
660 initializeAliasSetPrinterPass(*PassRegistry::getPassRegistry());
661 }
Devang Patel09f162c2007-05-01 21:15:47 +0000662
Craig Toppere9ba7592014-03-05 07:30:04 +0000663 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner7606fb62003-02-24 20:37:56 +0000664 AU.setPreservesAll();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000665 AU.addRequired<AAResultsWrapperPass>();
Chris Lattner7606fb62003-02-24 20:37:56 +0000666 }
667
Craig Toppere9ba7592014-03-05 07:30:04 +0000668 bool runOnFunction(Function &F) override {
Chandler Carruth7b560d42015-09-09 17:55:00 +0000669 auto &AAWP = getAnalysis<AAResultsWrapperPass>();
670 Tracker = new AliasSetTracker(AAWP.getAAResults());
Michael Kuperstein41898f02016-08-19 17:05:22 +0000671 errs() << "Alias sets for function '" << F.getName() << "':\n";
Chris Lattner7606fb62003-02-24 20:37:56 +0000672 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner2d3a7a62004-04-27 15:13:33 +0000673 Tracker->add(&*I);
David Greene0295ecf2009-12-23 22:49:57 +0000674 Tracker->print(errs());
Chris Lattner44497852006-01-03 06:05:22 +0000675 delete Tracker;
Chris Lattner7606fb62003-02-24 20:37:56 +0000676 return false;
677 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000678 };
Eugene Zelenko48666a62017-07-24 23:16:33 +0000679
680} // end anonymous namespace
Dan Gohmand78c4002008-05-13 00:00:25 +0000681
682char AliasSetPrinter::ID = 0;
Eugene Zelenko48666a62017-07-24 23:16:33 +0000683
Owen Anderson8ac477f2010-10-12 19:48:12 +0000684INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets",
685 "Alias Set Printer", false, true)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000686INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Owen Anderson8ac477f2010-10-12 19:48:12 +0000687INITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000688 "Alias Set Printer", false, true)