blob: e4b805aca91831414e07a9371e028fed0a5f655e [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"
Eugene Zelenko48666a62017-07-24 23:16:33 +000027#include "llvm/IR/Value.h"
Chris Lattner7606fb62003-02-24 20:37:56 +000028#include "llvm/Pass.h"
Eugene Zelenko48666a62017-07-24 23:16:33 +000029#include "llvm/Support/AtomicOrdering.h"
30#include "llvm/Support/Casting.h"
31#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/Compiler.h"
David Greene2ec90032009-12-23 19:27:59 +000033#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000034#include "llvm/Support/ErrorHandling.h"
Chris Lattnerb1d782b2009-08-23 05:17:37 +000035#include "llvm/Support/raw_ostream.h"
Eugene Zelenko48666a62017-07-24 23:16:33 +000036#include <cassert>
37#include <cstdint>
38#include <vector>
39
Chris Lattner0a140602003-12-14 04:52:11 +000040using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000041
Michael Kuperstein41898f02016-08-19 17:05:22 +000042static cl::opt<unsigned>
43 SaturationThreshold("alias-set-saturation-threshold", cl::Hidden,
44 cl::init(250),
45 cl::desc("The maximum number of pointers may-alias "
46 "sets may contain before degradation"));
47
Chris Lattner24bba4d2004-11-27 18:37:42 +000048/// mergeSetIn - Merge the specified alias set into this alias set.
Chris Lattner647df642002-09-26 21:49:07 +000049///
Chris Lattner24bba4d2004-11-27 18:37:42 +000050void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
Chris Lattner7606fb62003-02-24 20:37:56 +000051 assert(!AS.Forward && "Alias set is already forwarding!");
52 assert(!Forward && "This set is a forwarding set!!");
Chris Lattner647df642002-09-26 21:49:07 +000053
Michael Kuperstein41898f02016-08-19 17:05:22 +000054 bool WasMustAlias = (Alias == SetMustAlias);
Chris Lattner647df642002-09-26 21:49:07 +000055 // Update the alias and access types of this set...
Chandler Carrutha561d752015-06-22 02:12:52 +000056 Access |= AS.Access;
57 Alias |= AS.Alias;
Chris Lattnerdc8070e2010-08-29 04:14:47 +000058 Volatile |= AS.Volatile;
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 {
145 // 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
Eli Friedmanae8161e2011-07-27 00:46:46 +0000172 if (!I->mayWriteToMemory()) {
Chandler Carrutha561d752015-06-22 02:12:52 +0000173 Alias = SetMayAlias;
Hal Finkel1140e172015-10-28 22:13:41 +0000174 Access |= RefAccess;
Duncan Sands68b6f502007-12-01 07:51:45 +0000175 return;
Chris Lattner21c60f12004-03-15 04:08:36 +0000176 }
177
Hal Finkel1140e172015-10-28 22:13:41 +0000178 // FIXME: This should use mod/ref information to make this not suck so bad
Chandler Carrutha561d752015-06-22 02:12:52 +0000179 Alias = SetMayAlias;
Hal Finkel1140e172015-10-28 22:13:41 +0000180 Access = ModRefAccess;
Chris Lattner7606fb62003-02-24 20:37:56 +0000181}
182
183/// aliasesPointer - Return true if the specified pointer "may" (or must)
Chris Lattner647df642002-09-26 21:49:07 +0000184/// alias one of the members in the set.
185///
George Burgess IV319be3a2018-05-25 21:16:58 +0000186bool AliasSet::aliasesPointer(const Value *Ptr, LocationSize Size,
Hal Finkelcc39b672014-07-24 12:16:19 +0000187 const AAMDNodes &AAInfo,
Hal Finkel1140e172015-10-28 22:13:41 +0000188 AliasAnalysis &AA) const {
Michael Kuperstein41898f02016-08-19 17:05:22 +0000189 if (AliasAny)
190 return true;
191
Chandler Carrutha561d752015-06-22 02:12:52 +0000192 if (Alias == SetMustAlias) {
Eli Friedmanae8161e2011-07-27 00:46:46 +0000193 assert(UnknownInsts.empty() && "Illegal must alias set!");
Chris Lattner7f04ebc2004-03-15 06:28:07 +0000194
Chris Lattner7606fb62003-02-24 20:37:56 +0000195 // If this is a set of MustAliases, only check to see if the pointer aliases
Chris Lattnereef6b192010-08-29 04:13:43 +0000196 // SOME value in the set.
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000197 PointerRec *SomePtr = getSomePointer();
Chris Lattner7606fb62003-02-24 20:37:56 +0000198 assert(SomePtr && "Empty must-alias set??");
Chandler Carruthac80dc72015-06-17 07:18:54 +0000199 return AA.alias(MemoryLocation(SomePtr->getValue(), SomePtr->getSize(),
200 SomePtr->getAAInfo()),
201 MemoryLocation(Ptr, Size, AAInfo));
Chris Lattner7606fb62003-02-24 20:37:56 +0000202 }
203
204 // If this is a may-alias set, we have to check all of the pointers in the set
205 // to be sure it doesn't alias the set...
206 for (iterator I = begin(), E = end(); I != E; ++I)
Chandler Carruthac80dc72015-06-17 07:18:54 +0000207 if (AA.alias(MemoryLocation(Ptr, Size, AAInfo),
208 MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo())))
Chris Lattner7606fb62003-02-24 20:37:56 +0000209 return true;
210
Eli Friedmanae8161e2011-07-27 00:46:46 +0000211 // Check the unknown instructions...
212 if (!UnknownInsts.empty()) {
213 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i)
Sanjoy Das3f1e8e02017-03-11 01:15:48 +0000214 if (auto *Inst = getUnknownInst(i))
Alina Sbirlea63d22502017-12-05 20:12:23 +0000215 if (isModOrRefSet(
216 AA.getModRefInfo(Inst, MemoryLocation(Ptr, Size, AAInfo))))
Sanjoy Das3f1e8e02017-03-11 01:15:48 +0000217 return true;
Chris Lattner9b323c32004-07-27 02:20:26 +0000218 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000219
Hal Finkel1140e172015-10-28 22:13:41 +0000220 return false;
Chris Lattner647df642002-09-26 21:49:07 +0000221}
222
Pete Cooper4bf388d2015-05-13 01:12:12 +0000223bool AliasSet::aliasesUnknownInst(const Instruction *Inst,
Hal Finkel1140e172015-10-28 22:13:41 +0000224 AliasAnalysis &AA) const {
Michael Kuperstein41898f02016-08-19 17:05:22 +0000225
226 if (AliasAny)
227 return true;
228
Eli Friedmanae8161e2011-07-27 00:46:46 +0000229 if (!Inst->mayReadOrWriteMemory())
Duncan Sands68b6f502007-12-01 07:51:45 +0000230 return false;
Chris Lattner21c60f12004-03-15 04:08:36 +0000231
Eli Friedmanae8161e2011-07-27 00:46:46 +0000232 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
Xin Tong70f75122017-06-25 12:55:11 +0000233 if (auto *UnknownInst = getUnknownInst(i)) {
234 ImmutableCallSite C1(UnknownInst), C2(Inst);
Alina Sbirlea63d22502017-12-05 20:12:23 +0000235 if (!C1 || !C2 || isModOrRefSet(AA.getModRefInfo(C1, C2)) ||
236 isModOrRefSet(AA.getModRefInfo(C2, C1)))
Sanjoy Das3f1e8e02017-03-11 01:15:48 +0000237 return true;
238 }
Chris Lattnerf58382e2010-08-29 18:42:23 +0000239 }
Chris Lattner9b323c32004-07-27 02:20:26 +0000240
Hal Finkel1140e172015-10-28 22:13:41 +0000241 for (iterator I = begin(), E = end(); I != E; ++I)
Alina Sbirlea63d22502017-12-05 20:12:23 +0000242 if (isModOrRefSet(AA.getModRefInfo(
243 Inst, MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo()))))
Hal Finkel1140e172015-10-28 22:13:41 +0000244 return true;
245
246 return false;
Chris Lattner647df642002-09-26 21:49:07 +0000247}
248
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000249void AliasSetTracker::clear() {
250 // Delete all the PointerRec entries.
Dan Gohmanf4362da2009-07-30 20:21:41 +0000251 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
252 I != E; ++I)
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000253 I->second->eraseFromList();
254
255 PointerMap.clear();
256
257 // The alias sets should all be clear now.
258 AliasSets.clear();
259}
260
Chris Lattner647df642002-09-26 21:49:07 +0000261
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000262/// mergeAliasSetsForPointer - Given a pointer, merge all alias sets that may
263/// alias the pointer. Return the unified set, or nullptr if no set that aliases
264/// the pointer was found.
265AliasSet *AliasSetTracker::mergeAliasSetsForPointer(const Value *Ptr,
George Burgess IV319be3a2018-05-25 21:16:58 +0000266 LocationSize Size,
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000267 const AAMDNodes &AAInfo) {
Craig Topper9f008862014-04-15 04:59:12 +0000268 AliasSet *FoundSet = nullptr;
David Majnemerb7adf342014-11-19 09:41:05 +0000269 for (iterator I = begin(), E = end(); I != E;) {
270 iterator Cur = I++;
Hal Finkel1140e172015-10-28 22:13:41 +0000271 if (Cur->Forward || !Cur->aliasesPointer(Ptr, Size, AAInfo, AA)) continue;
Chris Lattnerafb70742010-08-29 04:06:55 +0000272
Craig Topper9f008862014-04-15 04:59:12 +0000273 if (!FoundSet) { // If this is the first alias set ptr can go into.
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000274 FoundSet = &*Cur; // Remember it.
Chris Lattnerafb70742010-08-29 04:06:55 +0000275 } else { // Otherwise, we must merge the sets.
David Majnemerb7adf342014-11-19 09:41:05 +0000276 FoundSet->mergeSetIn(*Cur, *this); // Merge in contents.
Chris Lattner647df642002-09-26 21:49:07 +0000277 }
Chris Lattnerafb70742010-08-29 04:06:55 +0000278 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000279
280 return FoundSet;
281}
282
Pete Cooper4bf388d2015-05-13 01:12:12 +0000283bool AliasSetTracker::containsUnknown(const Instruction *Inst) const {
Benjamin Krameraa209152016-06-26 17:27:42 +0000284 for (const AliasSet &AS : *this)
285 if (!AS.Forward && AS.aliasesUnknownInst(Inst, AA))
Hal Finkel840257a2014-11-03 23:19:16 +0000286 return true;
287 return false;
288}
Chris Lattnereeaa29c2004-11-26 21:36:25 +0000289
Hal Finkel1140e172015-10-28 22:13:41 +0000290AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) {
Craig Topper9f008862014-04-15 04:59:12 +0000291 AliasSet *FoundSet = nullptr;
David Majnemerb7adf342014-11-19 09:41:05 +0000292 for (iterator I = begin(), E = end(); I != E;) {
293 iterator Cur = I++;
Hal Finkel1140e172015-10-28 22:13:41 +0000294 if (Cur->Forward || !Cur->aliasesUnknownInst(Inst, AA))
Chris Lattnerafb70742010-08-29 04:06:55 +0000295 continue;
Craig Topper9f008862014-04-15 04:59:12 +0000296 if (!FoundSet) // If this is the first alias set ptr can go into.
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000297 FoundSet = &*Cur; // Remember it.
David Majnemerb7adf342014-11-19 09:41:05 +0000298 else if (!Cur->Forward) // Otherwise, we must merge the sets.
299 FoundSet->mergeSetIn(*Cur, *this); // Merge in contents.
Chris Lattnerafb70742010-08-29 04:06:55 +0000300 }
Chris Lattner647df642002-09-26 21:49:07 +0000301 return FoundSet;
302}
303
Chris Lattner7606fb62003-02-24 20:37:56 +0000304/// getAliasSetForPointer - Return the alias set that the specified pointer
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000305/// lives in.
George Burgess IV319be3a2018-05-25 21:16:58 +0000306AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer,
307 LocationSize Size,
Chad Rosier16970a82016-10-19 18:50:32 +0000308 const AAMDNodes &AAInfo) {
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000309 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
Chris Lattner7606fb62003-02-24 20:37:56 +0000310
Michael Kuperstein41898f02016-08-19 17:05:22 +0000311 if (AliasAnyAS) {
312 // At this point, the AST is saturated, so we only have one active alias
313 // set. That means we already know which alias set we want to return, and
314 // just need to add the pointer to that set to keep the data structure
315 // consistent.
316 // This, of course, means that we will never need a merge here.
317 if (Entry.hasAliasSet()) {
318 Entry.updateSizeAndAAInfo(Size, AAInfo);
319 assert(Entry.getAliasSet(*this) == AliasAnyAS &&
320 "Entry in saturated AST must belong to only alias set");
321 } else {
322 AliasAnyAS->addPointer(*this, Entry, Size, AAInfo);
323 }
324 return *AliasAnyAS;
325 }
326
Chris Lattnereef6b192010-08-29 04:13:43 +0000327 // Check to see if the pointer is already known.
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000328 if (Entry.hasAliasSet()) {
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000329 // If the size changed, we may need to merge several alias sets.
330 // Note that we can *not* return the result of mergeAliasSetsForPointer
331 // due to a quirk of alias analysis behavior. Since alias(undef, undef)
332 // is NoAlias, mergeAliasSetsForPointer(undef, ...) will not find the
333 // the right set for undef, even if it exists.
334 if (Entry.updateSizeAndAAInfo(Size, AAInfo))
335 mergeAliasSetsForPointer(Pointer, Size, AAInfo);
Chris Lattner7606fb62003-02-24 20:37:56 +0000336 // Return the set!
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000337 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattnerafb70742010-08-29 04:06:55 +0000338 }
339
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000340 if (AliasSet *AS = mergeAliasSetsForPointer(Pointer, Size, AAInfo)) {
Chris Lattnereef6b192010-08-29 04:13:43 +0000341 // Add it to the alias set it aliases.
Hal Finkel1140e172015-10-28 22:13:41 +0000342 AS->addPointer(*this, Entry, Size, AAInfo);
Chris Lattner7606fb62003-02-24 20:37:56 +0000343 return *AS;
Chris Lattner647df642002-09-26 21:49:07 +0000344 }
Chris Lattnerafb70742010-08-29 04:06:55 +0000345
Chris Lattnereef6b192010-08-29 04:13:43 +0000346 // Otherwise create a new alias set to hold the loaded pointer.
Chris Lattnerafb70742010-08-29 04:06:55 +0000347 AliasSets.push_back(new AliasSet());
Hal Finkel1140e172015-10-28 22:13:41 +0000348 AliasSets.back().addPointer(*this, Entry, Size, AAInfo);
Chris Lattnerafb70742010-08-29 04:06:55 +0000349 return AliasSets.back();
Chris Lattner647df642002-09-26 21:49:07 +0000350}
351
George Burgess IV319be3a2018-05-25 21:16:58 +0000352void AliasSetTracker::add(Value *Ptr, LocationSize Size,
353 const AAMDNodes &AAInfo) {
Chad Rosier16970a82016-10-19 18:50:32 +0000354 addPointer(Ptr, Size, AAInfo, AliasSet::NoAccess);
Chris Lattnerbf8c3c42004-07-26 05:50:23 +0000355}
356
Chad Rosier16970a82016-10-19 18:50:32 +0000357void AliasSetTracker::add(LoadInst *LI) {
JF Bastien800f87a2016-04-06 21:19:33 +0000358 if (isStrongerThanMonotonic(LI->getOrdering())) return addUnknown(LI);
Hal Finkelcc39b672014-07-24 12:16:19 +0000359
360 AAMDNodes AAInfo;
361 LI->getAAMetadata(AAInfo);
362
Hal Finkel1140e172015-10-28 22:13:41 +0000363 AliasSet::AccessLattice Access = AliasSet::RefAccess;
Chandler Carruth50fee932015-08-06 02:05:46 +0000364 const DataLayout &DL = LI->getModule()->getDataLayout();
Chris Lattner2cfaef22004-07-21 05:18:04 +0000365 AliasSet &AS = addPointer(LI->getOperand(0),
Chad Rosier16970a82016-10-19 18:50:32 +0000366 DL.getTypeStoreSize(LI->getType()), AAInfo, Access);
Chris Lattner0a140602003-12-14 04:52:11 +0000367 if (LI->isVolatile()) AS.setVolatile();
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
373 AAMDNodes AAInfo;
374 SI->getAAMetadata(AAInfo);
375
Hal Finkel1140e172015-10-28 22:13:41 +0000376 AliasSet::AccessLattice Access = AliasSet::ModAccess;
Chandler Carruth50fee932015-08-06 02:05:46 +0000377 const DataLayout &DL = SI->getModule()->getDataLayout();
Chris Lattner2cfaef22004-07-21 05:18:04 +0000378 Value *Val = SI->getOperand(0);
Chad Rosier16970a82016-10-19 18:50:32 +0000379 AliasSet &AS = addPointer(
380 SI->getOperand(1), DL.getTypeStoreSize(Val->getType()), AAInfo, Access);
Chris Lattner0a140602003-12-14 04:52:11 +0000381 if (SI->isVolatile()) AS.setVolatile();
Chris Lattner647df642002-09-26 21:49:07 +0000382}
383
Chad Rosier16970a82016-10-19 18:50:32 +0000384void AliasSetTracker::add(VAArgInst *VAAI) {
Hal Finkelcc39b672014-07-24 12:16:19 +0000385 AAMDNodes AAInfo;
386 VAAI->getAAMetadata(AAInfo);
387
Chandler Carruthecbd1682015-06-17 07:21:38 +0000388 addPointer(VAAI->getOperand(0), MemoryLocation::UnknownSize, AAInfo,
Chad Rosier16970a82016-10-19 18:50:32 +0000389 AliasSet::ModRefAccess);
Dan Gohman2cd8e382008-04-14 18:34:50 +0000390}
391
Chad Rosier16970a82016-10-19 18:50:32 +0000392void AliasSetTracker::add(MemSetInst *MSI) {
Haicheng Wu5cf99092016-02-17 02:01:50 +0000393 AAMDNodes AAInfo;
394 MSI->getAAMetadata(AAInfo);
395
Haicheng Wu5cf99092016-02-17 02:01:50 +0000396 uint64_t Len;
397
398 if (ConstantInt *C = dyn_cast<ConstantInt>(MSI->getLength()))
399 Len = C->getZExtValue();
400 else
401 Len = MemoryLocation::UnknownSize;
402
403 AliasSet &AS =
Chad Rosier16970a82016-10-19 18:50:32 +0000404 addPointer(MSI->getRawDest(), Len, AAInfo, AliasSet::ModAccess);
Haicheng Wu5cf99092016-02-17 02:01:50 +0000405 if (MSI->isVolatile())
406 AS.setVolatile();
Haicheng Wu5cf99092016-02-17 02:01:50 +0000407}
Chris Lattner0a140602003-12-14 04:52:11 +0000408
Chad Rosier6e3a92e2016-10-19 19:09:03 +0000409void AliasSetTracker::add(MemTransferInst *MTI) {
410 AAMDNodes AAInfo;
411 MTI->getAAMetadata(AAInfo);
412
413 uint64_t Len;
414 if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
415 Len = C->getZExtValue();
416 else
417 Len = MemoryLocation::UnknownSize;
418
419 AliasSet &ASSrc =
420 addPointer(MTI->getRawSource(), Len, AAInfo, AliasSet::RefAccess);
421 if (MTI->isVolatile())
422 ASSrc.setVolatile();
423
424 AliasSet &ASDst =
425 addPointer(MTI->getRawDest(), Len, AAInfo, AliasSet::ModAccess);
426 if (MTI->isVolatile())
427 ASDst.setVolatile();
428}
429
Chad Rosier16970a82016-10-19 18:50:32 +0000430void AliasSetTracker::addUnknown(Instruction *Inst) {
431 if (isa<DbgInfoIntrinsic>(Inst))
432 return; // Ignore DbgInfo Intrinsics.
Chad Rosier8f348012016-11-07 14:11:45 +0000433
434 if (auto *II = dyn_cast<IntrinsicInst>(Inst)) {
435 // These intrinsics will show up as affecting memory, but they are just
436 // markers.
437 switch (II->getIntrinsicID()) {
438 default:
439 break;
440 // FIXME: Add lifetime/invariant intrinsics (See: PR30807).
441 case Intrinsic::assume:
Dan Gohman2c74fe92017-11-08 21:59:51 +0000442 case Intrinsic::sideeffect:
Chad Rosier8f348012016-11-07 14:11:45 +0000443 return;
444 }
445 }
Eli Friedmanae8161e2011-07-27 00:46:46 +0000446 if (!Inst->mayReadOrWriteMemory())
Chad Rosier16970a82016-10-19 18:50:32 +0000447 return; // doesn't alias anything
Chris Lattner7f04ebc2004-03-15 06:28:07 +0000448
Hal Finkel1140e172015-10-28 22:13:41 +0000449 AliasSet *AS = findAliasSetForUnknownInst(Inst);
Chris Lattnerafb70742010-08-29 04:06:55 +0000450 if (AS) {
Hal Finkel1140e172015-10-28 22:13:41 +0000451 AS->addUnknownInst(Inst, AA);
Chad Rosier16970a82016-10-19 18:50:32 +0000452 return;
Chris Lattner7606fb62003-02-24 20:37:56 +0000453 }
Chris Lattnerafb70742010-08-29 04:06:55 +0000454 AliasSets.push_back(new AliasSet());
455 AS = &AliasSets.back();
Hal Finkel1140e172015-10-28 22:13:41 +0000456 AS->addUnknownInst(Inst, AA);
Chris Lattner647df642002-09-26 21:49:07 +0000457}
458
Chad Rosier16970a82016-10-19 18:50:32 +0000459void AliasSetTracker::add(Instruction *I) {
Chris Lattnerafb70742010-08-29 04:06:55 +0000460 // Dispatch to one of the other add methods.
Chris Lattner7606fb62003-02-24 20:37:56 +0000461 if (LoadInst *LI = dyn_cast<LoadInst>(I))
Chris Lattner2cfaef22004-07-21 05:18:04 +0000462 return add(LI);
Chris Lattnerafb70742010-08-29 04:06:55 +0000463 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner2cfaef22004-07-21 05:18:04 +0000464 return add(SI);
Chris Lattnerafb70742010-08-29 04:06:55 +0000465 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
Dan Gohman2cd8e382008-04-14 18:34:50 +0000466 return add(VAAI);
Haicheng Wu5cf99092016-02-17 02:01:50 +0000467 if (MemSetInst *MSI = dyn_cast<MemSetInst>(I))
468 return add(MSI);
Chad Rosier6e3a92e2016-10-19 19:09:03 +0000469 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(I))
470 return add(MTI);
Eli Friedmanae8161e2011-07-27 00:46:46 +0000471 return addUnknown(I);
Chris Lattner647df642002-09-26 21:49:07 +0000472}
473
Chris Lattnerc048bb32003-03-03 23:28:05 +0000474void AliasSetTracker::add(BasicBlock &BB) {
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000475 for (auto &I : BB)
476 add(&I);
Chris Lattnerc048bb32003-03-03 23:28:05 +0000477}
478
479void AliasSetTracker::add(const AliasSetTracker &AST) {
480 assert(&AA == &AST.AA &&
481 "Merging AliasSetTracker objects with different Alias Analyses!");
482
483 // Loop over all of the alias sets in AST, adding the pointers contained
484 // therein into the current alias sets. This can cause alias sets to be
485 // merged together in the current AST.
Benjamin Krameraa209152016-06-26 17:27:42 +0000486 for (const AliasSet &AS : AST) {
487 if (AS.Forward)
488 continue; // Ignore forwarding alias sets
Chris Lattnerc048bb32003-03-03 23:28:05 +0000489
Chris Lattnerafb70742010-08-29 04:06:55 +0000490 // If there are any call sites in the alias set, add them to this AST.
Eli Friedmanae8161e2011-07-27 00:46:46 +0000491 for (unsigned i = 0, e = AS.UnknownInsts.size(); i != e; ++i)
Sanjoy Das3f1e8e02017-03-11 01:15:48 +0000492 if (auto *Inst = AS.getUnknownInst(i))
493 add(Inst);
Chris Lattnerc048bb32003-03-03 23:28:05 +0000494
Chris Lattnerafb70742010-08-29 04:06:55 +0000495 // Loop over all of the pointers in this alias set.
Chris Lattnerafb70742010-08-29 04:06:55 +0000496 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) {
Chad Rosier16970a82016-10-19 18:50:32 +0000497 AliasSet &NewAS =
498 addPointer(ASI.getPointer(), ASI.getSize(), ASI.getAAInfo(),
499 (AliasSet::AccessLattice)AS.Access);
Chris Lattnerafb70742010-08-29 04:06:55 +0000500 if (AS.isVolatile()) NewAS.setVolatile();
Chris Lattnerc048bb32003-03-03 23:28:05 +0000501 }
Chris Lattnerafb70742010-08-29 04:06:55 +0000502 }
Chris Lattnerc048bb32003-03-03 23:28:05 +0000503}
504
Chris Lattner746e1e12004-05-23 21:10:58 +0000505// deleteValue method - This method is used to remove a pointer value from the
Chris Lattner44fea542003-12-18 08:11:56 +0000506// AliasSetTracker entirely. It should be used when an instruction is deleted
507// from the program to update the AST. If you don't use this, you would have
508// dangling pointers to deleted instructions.
509//
Chris Lattner746e1e12004-05-23 21:10:58 +0000510void AliasSetTracker::deleteValue(Value *PtrVal) {
Chris Lattnerab644812004-09-14 19:15:32 +0000511 // First, look up the PointerRec for this pointer.
Benjamin Kramere2ef47c2012-06-30 22:37:15 +0000512 PointerMapType::iterator I = PointerMap.find_as(PtrVal);
Chris Lattner44fea542003-12-18 08:11:56 +0000513 if (I == PointerMap.end()) return; // Noop
514
515 // If we found one, remove the pointer from the alias set it is in.
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000516 AliasSet::PointerRec *PtrValEnt = I->second;
517 AliasSet *AS = PtrValEnt->getAliasSet(*this);
Chris Lattner44fea542003-12-18 08:11:56 +0000518
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000519 // Unlink and delete from the list of values.
520 PtrValEnt->eraseFromList();
Michael Kuperstein41898f02016-08-19 17:05:22 +0000521
522 if (AS->Alias == AliasSet::SetMayAlias) {
523 AS->SetSize--;
524 TotalMayAliasSetSize--;
525 }
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000526
527 // Stop using the alias set.
Chris Lattner053427f2004-07-22 07:58:18 +0000528 AS->dropRef(*this);
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000529
Chris Lattner44fea542003-12-18 08:11:56 +0000530 PointerMap.erase(I);
531}
532
Chris Lattnerab644812004-09-14 19:15:32 +0000533// copyValue - This method should be used whenever a preexisting value in the
534// program is copied or cloned, introducing a new value. Note that it is ok for
535// clients that use this method to introduce the same value multiple times: if
536// the tracker already knows about a value, it will ignore the request.
537//
538void AliasSetTracker::copyValue(Value *From, Value *To) {
Chris Lattnerab644812004-09-14 19:15:32 +0000539 // First, look up the PointerRec for this pointer.
Benjamin Kramere2ef47c2012-06-30 22:37:15 +0000540 PointerMapType::iterator I = PointerMap.find_as(From);
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000541 if (I == PointerMap.end())
Chris Lattnerab644812004-09-14 19:15:32 +0000542 return; // Noop
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000543 assert(I->second->hasAliasSet() && "Dead entry?");
Chris Lattnerab644812004-09-14 19:15:32 +0000544
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000545 AliasSet::PointerRec &Entry = getEntryFor(To);
546 if (Entry.hasAliasSet()) return; // Already in the tracker!
Chris Lattnerab644812004-09-14 19:15:32 +0000547
Xinliang David Li1ce88fa2016-08-11 23:09:56 +0000548 // getEntryFor above may invalidate iterator \c I, so reinitialize it.
Benjamin Kramere2ef47c2012-06-30 22:37:15 +0000549 I = PointerMap.find_as(From);
Xinliang David Li1ce88fa2016-08-11 23:09:56 +0000550 // Add it to the alias set it aliases...
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000551 AliasSet *AS = I->second->getAliasSet(*this);
Dan Gohman408beac2010-10-18 21:28:00 +0000552 AS->addPointer(*this, Entry, I->second->getSize(),
Hal Finkel1140e172015-10-28 22:13:41 +0000553 I->second->getAAInfo(),
Dan Gohman71af9db2010-10-18 20:44:50 +0000554 true);
Chris Lattnerab644812004-09-14 19:15:32 +0000555}
556
Michael Kuperstein41898f02016-08-19 17:05:22 +0000557AliasSet &AliasSetTracker::mergeAllAliasSets() {
558 assert(!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold) &&
559 "Full merge should happen once, when the saturation threshold is "
560 "reached");
Chris Lattnerab644812004-09-14 19:15:32 +0000561
Michael Kuperstein41898f02016-08-19 17:05:22 +0000562 // Collect all alias sets, so that we can drop references with impunity
563 // without worrying about iterator invalidation.
564 std::vector<AliasSet *> ASVector;
565 ASVector.reserve(SaturationThreshold);
566 for (iterator I = begin(), E = end(); I != E; I++)
567 ASVector.push_back(&*I);
568
569 // Copy all instructions and pointers into a new set, and forward all other
570 // sets to it.
571 AliasSets.push_back(new AliasSet());
572 AliasAnyAS = &AliasSets.back();
573 AliasAnyAS->Alias = AliasSet::SetMayAlias;
574 AliasAnyAS->Access = AliasSet::ModRefAccess;
575 AliasAnyAS->AliasAny = true;
576
577 for (auto Cur : ASVector) {
Michael Kuperstein41898f02016-08-19 17:05:22 +0000578 // If Cur was already forwarding, just forward to the new AS instead.
579 AliasSet *FwdTo = Cur->Forward;
580 if (FwdTo) {
581 Cur->Forward = AliasAnyAS;
Alina Sbirlea63d22502017-12-05 20:12:23 +0000582 AliasAnyAS->addRef();
Michael Kuperstein41898f02016-08-19 17:05:22 +0000583 FwdTo->dropRef(*this);
584 continue;
585 }
586
587 // Otherwise, perform the actual merge.
588 AliasAnyAS->mergeSetIn(*Cur, *this);
589 }
590
591 return *AliasAnyAS;
592}
593
George Burgess IV319be3a2018-05-25 21:16:58 +0000594AliasSet &AliasSetTracker::addPointer(Value *P, LocationSize Size,
Michael Kuperstein41898f02016-08-19 17:05:22 +0000595 const AAMDNodes &AAInfo,
Chad Rosier16970a82016-10-19 18:50:32 +0000596 AliasSet::AccessLattice E) {
Chad Rosier16970a82016-10-19 18:50:32 +0000597 AliasSet &AS = getAliasSetForPointer(P, Size, AAInfo);
Michael Kuperstein41898f02016-08-19 17:05:22 +0000598 AS.Access |= E;
599
600 if (!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold)) {
601 // The AST is now saturated. From here on, we conservatively consider all
602 // pointers to alias each-other.
603 return mergeAllAliasSets();
604 }
605
606 return AS;
607}
Chris Lattner44fea542003-12-18 08:11:56 +0000608
Chris Lattner7606fb62003-02-24 20:37:56 +0000609//===----------------------------------------------------------------------===//
610// AliasSet/AliasSetTracker Printing Support
611//===----------------------------------------------------------------------===//
612
Chris Lattnerb1d782b2009-08-23 05:17:37 +0000613void AliasSet::print(raw_ostream &OS) const {
Roman Divackyad06cee2012-09-05 22:26:57 +0000614 OS << " AliasSet[" << (const void*)this << ", " << RefCount << "] ";
Chandler Carrutha561d752015-06-22 02:12:52 +0000615 OS << (Alias == SetMustAlias ? "must" : "may") << " alias, ";
616 switch (Access) {
Hal Finkel1140e172015-10-28 22:13:41 +0000617 case NoAccess: OS << "No access "; break;
618 case RefAccess: OS << "Ref "; break;
619 case ModAccess: OS << "Mod "; break;
620 case ModRefAccess: OS << "Mod/Ref "; break;
Chandler Carrutha561d752015-06-22 02:12:52 +0000621 default: llvm_unreachable("Bad value for Access!");
Chris Lattner647df642002-09-26 21:49:07 +0000622 }
Chris Lattner0a140602003-12-14 04:52:11 +0000623 if (isVolatile()) OS << "[volatile] ";
Chris Lattner7606fb62003-02-24 20:37:56 +0000624 if (Forward)
625 OS << " forwarding to " << (void*)Forward;
626
Dan Gohmanc731c972007-10-03 19:26:29 +0000627 if (!empty()) {
Chris Lattner7606fb62003-02-24 20:37:56 +0000628 OS << "Pointers: ";
629 for (iterator I = begin(), E = end(); I != E; ++I) {
630 if (I != begin()) OS << ", ";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000631 I.getPointer()->printAsOperand(OS << "(");
Chris Lattner053427f2004-07-22 07:58:18 +0000632 OS << ", " << I.getSize() << ")";
Chris Lattner7606fb62003-02-24 20:37:56 +0000633 }
634 }
Eli Friedmanae8161e2011-07-27 00:46:46 +0000635 if (!UnknownInsts.empty()) {
636 OS << "\n " << UnknownInsts.size() << " Unknown instructions: ";
637 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
Chris Lattner7606fb62003-02-24 20:37:56 +0000638 if (i) OS << ", ";
Sanjoy Das3f1e8e02017-03-11 01:15:48 +0000639 if (auto *I = getUnknownInst(i))
640 I->printAsOperand(OS);
Misha Brukman01808ca2005-04-21 21:13:18 +0000641 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000642 }
643 OS << "\n";
644}
645
Chris Lattnerb1d782b2009-08-23 05:17:37 +0000646void AliasSetTracker::print(raw_ostream &OS) const {
Chris Lattner7606fb62003-02-24 20:37:56 +0000647 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
648 << PointerMap.size() << " pointer values.\n";
Benjamin Krameraa209152016-06-26 17:27:42 +0000649 for (const AliasSet &AS : *this)
650 AS.print(OS);
Chris Lattner7606fb62003-02-24 20:37:56 +0000651 OS << "\n";
652}
653
Aaron Ballman615eb472017-10-15 14:32:27 +0000654#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000655LLVM_DUMP_METHOD void AliasSet::dump() const { print(dbgs()); }
656LLVM_DUMP_METHOD void AliasSetTracker::dump() const { print(dbgs()); }
Manman Renc3366cc2012-09-06 19:55:56 +0000657#endif
Chris Lattner7606fb62003-02-24 20:37:56 +0000658
Chris Lattner7606fb62003-02-24 20:37:56 +0000659//===----------------------------------------------------------------------===//
Dan Gohmanf4362da2009-07-30 20:21:41 +0000660// ASTCallbackVH Class Implementation
661//===----------------------------------------------------------------------===//
662
663void AliasSetTracker::ASTCallbackVH::deleted() {
664 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
665 AST->deleteValue(getValPtr());
666 // this now dangles!
667}
668
Eli Friedman17822fc2011-04-09 06:55:46 +0000669void AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(Value *V) {
670 AST->copyValue(getValPtr(), V);
671}
672
Dan Gohmanf4362da2009-07-30 20:21:41 +0000673AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
Dan Gohmandc2b1b02009-07-31 18:21:48 +0000674 : CallbackVH(V), AST(ast) {}
675
676AliasSetTracker::ASTCallbackVH &
677AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
678 return *this = ASTCallbackVH(V, AST);
679}
Dan Gohmanf4362da2009-07-30 20:21:41 +0000680
681//===----------------------------------------------------------------------===//
Chris Lattner7606fb62003-02-24 20:37:56 +0000682// AliasSetPrinter Pass
683//===----------------------------------------------------------------------===//
684
685namespace {
Eugene Zelenko48666a62017-07-24 23:16:33 +0000686
Nick Lewycky02d5f772009-10-25 06:33:48 +0000687 class AliasSetPrinter : public FunctionPass {
Chris Lattner7606fb62003-02-24 20:37:56 +0000688 AliasSetTracker *Tracker;
Eugene Zelenko48666a62017-07-24 23:16:33 +0000689
Chris Lattner7606fb62003-02-24 20:37:56 +0000690 public:
Nick Lewyckye7da2d62007-05-06 13:37:16 +0000691 static char ID; // Pass identification, replacement for typeid
Eugene Zelenko48666a62017-07-24 23:16:33 +0000692
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000693 AliasSetPrinter() : FunctionPass(ID) {
694 initializeAliasSetPrinterPass(*PassRegistry::getPassRegistry());
695 }
Devang Patel09f162c2007-05-01 21:15:47 +0000696
Craig Toppere9ba7592014-03-05 07:30:04 +0000697 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner7606fb62003-02-24 20:37:56 +0000698 AU.setPreservesAll();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000699 AU.addRequired<AAResultsWrapperPass>();
Chris Lattner7606fb62003-02-24 20:37:56 +0000700 }
701
Craig Toppere9ba7592014-03-05 07:30:04 +0000702 bool runOnFunction(Function &F) override {
Chandler Carruth7b560d42015-09-09 17:55:00 +0000703 auto &AAWP = getAnalysis<AAResultsWrapperPass>();
704 Tracker = new AliasSetTracker(AAWP.getAAResults());
Michael Kuperstein41898f02016-08-19 17:05:22 +0000705 errs() << "Alias sets for function '" << F.getName() << "':\n";
Chris Lattner7606fb62003-02-24 20:37:56 +0000706 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner2d3a7a62004-04-27 15:13:33 +0000707 Tracker->add(&*I);
David Greene0295ecf2009-12-23 22:49:57 +0000708 Tracker->print(errs());
Chris Lattner44497852006-01-03 06:05:22 +0000709 delete Tracker;
Chris Lattner7606fb62003-02-24 20:37:56 +0000710 return false;
711 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000712 };
Eugene Zelenko48666a62017-07-24 23:16:33 +0000713
714} // end anonymous namespace
Dan Gohmand78c4002008-05-13 00:00:25 +0000715
716char AliasSetPrinter::ID = 0;
Eugene Zelenko48666a62017-07-24 23:16:33 +0000717
Owen Anderson8ac477f2010-10-12 19:48:12 +0000718INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets",
719 "Alias Set Printer", false, true)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000720INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Owen Anderson8ac477f2010-10-12 19:48:12 +0000721INITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000722 "Alias Set Printer", false, true)