blob: 025e8119debb12720e8f8fff0a594b54efc43b21 [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"
17#include "llvm/IR/CallSite.h"
18#include "llvm/IR/Constants.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/DataLayout.h"
Eugene Zelenko48666a62017-07-24 23:16:33 +000020#include "llvm/IR/Function.h"
Chandler Carruth83948572014-03-04 10:30:26 +000021#include "llvm/IR/InstIterator.h"
Eugene Zelenko48666a62017-07-24 23:16:33 +000022#include "llvm/IR/Instruction.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Instructions.h"
24#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000025#include "llvm/IR/Module.h"
Eugene Zelenko48666a62017-07-24 23:16:33 +000026#include "llvm/IR/Value.h"
Chris Lattner7606fb62003-02-24 20:37:56 +000027#include "llvm/Pass.h"
Eugene Zelenko48666a62017-07-24 23:16:33 +000028#include "llvm/Support/AtomicOrdering.h"
29#include "llvm/Support/Casting.h"
30#include "llvm/Support/CommandLine.h"
31#include "llvm/Support/Compiler.h"
David Greene2ec90032009-12-23 19:27:59 +000032#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000033#include "llvm/Support/ErrorHandling.h"
Chris Lattnerb1d782b2009-08-23 05:17:37 +000034#include "llvm/Support/raw_ostream.h"
Eugene Zelenko48666a62017-07-24 23:16:33 +000035#include <cassert>
36#include <cstdint>
37#include <vector>
38
Chris Lattner0a140602003-12-14 04:52:11 +000039using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000040
Michael Kuperstein41898f02016-08-19 17:05:22 +000041static cl::opt<unsigned>
42 SaturationThreshold("alias-set-saturation-threshold", cl::Hidden,
43 cl::init(250),
44 cl::desc("The maximum number of pointers may-alias "
45 "sets may contain before degradation"));
46
Chris Lattner24bba4d2004-11-27 18:37:42 +000047/// mergeSetIn - Merge the specified alias set into this alias set.
Chris Lattner647df642002-09-26 21:49:07 +000048///
Chris Lattner24bba4d2004-11-27 18:37:42 +000049void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
Chris Lattner7606fb62003-02-24 20:37:56 +000050 assert(!AS.Forward && "Alias set is already forwarding!");
51 assert(!Forward && "This set is a forwarding set!!");
Chris Lattner647df642002-09-26 21:49:07 +000052
Michael Kuperstein41898f02016-08-19 17:05:22 +000053 bool WasMustAlias = (Alias == SetMustAlias);
Chris Lattner647df642002-09-26 21:49:07 +000054 // Update the alias and access types of this set...
Chandler Carrutha561d752015-06-22 02:12:52 +000055 Access |= AS.Access;
56 Alias |= AS.Alias;
Chris Lattnerdc8070e2010-08-29 04:14:47 +000057 Volatile |= AS.Volatile;
Chris Lattner7606fb62003-02-24 20:37:56 +000058
Chandler Carrutha561d752015-06-22 02:12:52 +000059 if (Alias == SetMustAlias) {
Chris Lattner24bba4d2004-11-27 18:37:42 +000060 // Check that these two merged sets really are must aliases. Since both
61 // used to be must-alias sets, we can just check any pointer from each set
62 // for aliasing.
63 AliasAnalysis &AA = AST.getAliasAnalysis();
Chris Lattner0eab5ec2009-03-09 05:11:09 +000064 PointerRec *L = getSomePointer();
65 PointerRec *R = AS.getSomePointer();
Chris Lattner24bba4d2004-11-27 18:37:42 +000066
67 // If the pointers are not a must-alias pair, this set becomes a may alias.
Chandler Carruthac80dc72015-06-17 07:18:54 +000068 if (AA.alias(MemoryLocation(L->getValue(), L->getSize(), L->getAAInfo()),
69 MemoryLocation(R->getValue(), R->getSize(), R->getAAInfo())) !=
Chandler Carruthc3f49eb2015-06-22 02:16:51 +000070 MustAlias)
Chandler Carrutha561d752015-06-22 02:12:52 +000071 Alias = SetMayAlias;
Chris Lattner24bba4d2004-11-27 18:37:42 +000072 }
73
Michael Kuperstein41898f02016-08-19 17:05:22 +000074 if (Alias == SetMayAlias) {
75 if (WasMustAlias)
76 AST.TotalMayAliasSetSize += size();
77 if (AS.Alias == SetMustAlias)
78 AST.TotalMayAliasSetSize += AS.size();
79 }
80
David Majnemer35639382014-11-19 19:36:18 +000081 bool ASHadUnknownInsts = !AS.UnknownInsts.empty();
Eli Friedmanae8161e2011-07-27 00:46:46 +000082 if (UnknownInsts.empty()) { // Merge call sites...
David Majnemer35639382014-11-19 19:36:18 +000083 if (ASHadUnknownInsts) {
Eli Friedmanae8161e2011-07-27 00:46:46 +000084 std::swap(UnknownInsts, AS.UnknownInsts);
David Majnemerb7adf342014-11-19 09:41:05 +000085 addRef();
86 }
David Majnemer35639382014-11-19 19:36:18 +000087 } else if (ASHadUnknownInsts) {
Eli Friedmanae8161e2011-07-27 00:46:46 +000088 UnknownInsts.insert(UnknownInsts.end(), AS.UnknownInsts.begin(), AS.UnknownInsts.end());
89 AS.UnknownInsts.clear();
Chris Lattner7606fb62003-02-24 20:37:56 +000090 }
Misha Brukman01808ca2005-04-21 21:13:18 +000091
Michael Kuperstein41898f02016-08-19 17:05:22 +000092 AS.Forward = this; // Forward across AS now...
93 addRef(); // AS is now pointing to us...
Chris Lattner7606fb62003-02-24 20:37:56 +000094
95 // Merge the list of constituent pointers...
Chris Lattner44fea542003-12-18 08:11:56 +000096 if (AS.PtrList) {
Michael Kuperstein41898f02016-08-19 17:05:22 +000097 SetSize += AS.size();
98 AS.SetSize = 0;
Chris Lattner44fea542003-12-18 08:11:56 +000099 *PtrListEnd = AS.PtrList;
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000100 AS.PtrList->setPrevInList(PtrListEnd);
Chris Lattner44fea542003-12-18 08:11:56 +0000101 PtrListEnd = AS.PtrListEnd;
102
Craig Topper9f008862014-04-15 04:59:12 +0000103 AS.PtrList = nullptr;
Chris Lattner44fea542003-12-18 08:11:56 +0000104 AS.PtrListEnd = &AS.PtrList;
Craig Topper9f008862014-04-15 04:59:12 +0000105 assert(*AS.PtrListEnd == nullptr && "End of list is not null?");
Chris Lattner44fea542003-12-18 08:11:56 +0000106 }
David Majnemerb7adf342014-11-19 09:41:05 +0000107 if (ASHadUnknownInsts)
108 AS.dropRef(AST);
Chris Lattner647df642002-09-26 21:49:07 +0000109}
110
Chris Lattner7606fb62003-02-24 20:37:56 +0000111void AliasSetTracker::removeAliasSet(AliasSet *AS) {
Chris Lattner053427f2004-07-22 07:58:18 +0000112 if (AliasSet *Fwd = AS->Forward) {
113 Fwd->dropRef(*this);
Craig Topper9f008862014-04-15 04:59:12 +0000114 AS->Forward = nullptr;
Chris Lattner053427f2004-07-22 07:58:18 +0000115 }
Michael Kuperstein41898f02016-08-19 17:05:22 +0000116
117 if (AS->Alias == AliasSet::SetMayAlias)
118 TotalMayAliasSetSize -= AS->size();
119
Chris Lattner7606fb62003-02-24 20:37:56 +0000120 AliasSets.erase(AS);
121}
122
123void AliasSet::removeFromTracker(AliasSetTracker &AST) {
124 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
125 AST.removeAliasSet(this);
126}
127
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000128void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
Hal Finkelcc39b672014-07-24 12:16:19 +0000129 uint64_t Size, const AAMDNodes &AAInfo,
Hal Finkel1140e172015-10-28 22:13:41 +0000130 bool KnownMustAlias) {
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000131 assert(!Entry.hasAliasSet() && "Entry already in set!");
Chris Lattner7606fb62003-02-24 20:37:56 +0000132
Chris Lattnerab644812004-09-14 19:15:32 +0000133 // Check to see if we have to downgrade to _may_ alias.
134 if (isMustAlias() && !KnownMustAlias)
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000135 if (PointerRec *P = getSomePointer()) {
Chris Lattner6fa96652004-09-15 16:59:47 +0000136 AliasAnalysis &AA = AST.getAliasAnalysis();
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000137 AliasResult Result =
Chandler Carruthac80dc72015-06-17 07:18:54 +0000138 AA.alias(MemoryLocation(P->getValue(), P->getSize(), P->getAAInfo()),
139 MemoryLocation(Entry.getValue(), Size, AAInfo));
Michael Kuperstein41898f02016-08-19 17:05:22 +0000140 if (Result != MustAlias) {
Chandler Carrutha561d752015-06-22 02:12:52 +0000141 Alias = SetMayAlias;
Michael Kuperstein41898f02016-08-19 17:05:22 +0000142 AST.TotalMayAliasSetSize += size();
143 } else {
144 // First entry of must alias must have maximum size!
Hal Finkelcc39b672014-07-24 12:16:19 +0000145 P->updateSizeAndAAInfo(Size, AAInfo);
Michael Kuperstein41898f02016-08-19 17:05:22 +0000146 }
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000147 assert(Result != NoAlias && "Cannot be part of must set!");
Chris Lattnerb5b0b7a2003-02-26 22:11:00 +0000148 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000149
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000150 Entry.setAliasSet(this);
Hal Finkelcc39b672014-07-24 12:16:19 +0000151 Entry.updateSizeAndAAInfo(Size, AAInfo);
Chris Lattner7606fb62003-02-24 20:37:56 +0000152
153 // Add it to the end of the list...
Michael Kuperstein41898f02016-08-19 17:05:22 +0000154 ++SetSize;
Craig Topper9f008862014-04-15 04:59:12 +0000155 assert(*PtrListEnd == nullptr && "End of list is not null?");
Chris Lattner44fea542003-12-18 08:11:56 +0000156 *PtrListEnd = &Entry;
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000157 PtrListEnd = Entry.setPrevInList(PtrListEnd);
Craig Topper9f008862014-04-15 04:59:12 +0000158 assert(*PtrListEnd == nullptr && "End of list is not null?");
Michael Kuperstein41898f02016-08-19 17:05:22 +0000159 // Entry points to alias set.
160 addRef();
161
162 if (Alias == SetMayAlias)
163 AST.TotalMayAliasSetSize++;
Chris Lattner7606fb62003-02-24 20:37:56 +0000164}
165
Hal Finkel1140e172015-10-28 22:13:41 +0000166void AliasSet::addUnknownInst(Instruction *I, AliasAnalysis &AA) {
David Majnemerb7adf342014-11-19 09:41:05 +0000167 if (UnknownInsts.empty())
168 addRef();
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000169 UnknownInsts.emplace_back(I);
Chris Lattner21c60f12004-03-15 04:08:36 +0000170
Eli Friedmanae8161e2011-07-27 00:46:46 +0000171 if (!I->mayWriteToMemory()) {
Chandler Carrutha561d752015-06-22 02:12:52 +0000172 Alias = SetMayAlias;
Hal Finkel1140e172015-10-28 22:13:41 +0000173 Access |= RefAccess;
Duncan Sands68b6f502007-12-01 07:51:45 +0000174 return;
Chris Lattner21c60f12004-03-15 04:08:36 +0000175 }
176
Hal Finkel1140e172015-10-28 22:13:41 +0000177 // FIXME: This should use mod/ref information to make this not suck so bad
Chandler Carrutha561d752015-06-22 02:12:52 +0000178 Alias = SetMayAlias;
Hal Finkel1140e172015-10-28 22:13:41 +0000179 Access = ModRefAccess;
Chris Lattner7606fb62003-02-24 20:37:56 +0000180}
181
182/// aliasesPointer - Return true if the specified pointer "may" (or must)
Chris Lattner647df642002-09-26 21:49:07 +0000183/// alias one of the members in the set.
184///
Dan Gohmanf372cf82010-10-19 22:54:46 +0000185bool AliasSet::aliasesPointer(const Value *Ptr, uint64_t Size,
Hal Finkelcc39b672014-07-24 12:16:19 +0000186 const AAMDNodes &AAInfo,
Hal Finkel1140e172015-10-28 22:13:41 +0000187 AliasAnalysis &AA) const {
Michael Kuperstein41898f02016-08-19 17:05:22 +0000188 if (AliasAny)
189 return true;
190
Chandler Carrutha561d752015-06-22 02:12:52 +0000191 if (Alias == SetMustAlias) {
Eli Friedmanae8161e2011-07-27 00:46:46 +0000192 assert(UnknownInsts.empty() && "Illegal must alias set!");
Chris Lattner7f04ebc2004-03-15 06:28:07 +0000193
Chris Lattner7606fb62003-02-24 20:37:56 +0000194 // If this is a set of MustAliases, only check to see if the pointer aliases
Chris Lattnereef6b192010-08-29 04:13:43 +0000195 // SOME value in the set.
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000196 PointerRec *SomePtr = getSomePointer();
Chris Lattner7606fb62003-02-24 20:37:56 +0000197 assert(SomePtr && "Empty must-alias set??");
Chandler Carruthac80dc72015-06-17 07:18:54 +0000198 return AA.alias(MemoryLocation(SomePtr->getValue(), SomePtr->getSize(),
199 SomePtr->getAAInfo()),
200 MemoryLocation(Ptr, Size, AAInfo));
Chris Lattner7606fb62003-02-24 20:37:56 +0000201 }
202
203 // If this is a may-alias set, we have to check all of the pointers in the set
204 // to be sure it doesn't alias the set...
205 for (iterator I = begin(), E = end(); I != E; ++I)
Chandler Carruthac80dc72015-06-17 07:18:54 +0000206 if (AA.alias(MemoryLocation(Ptr, Size, AAInfo),
207 MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo())))
Chris Lattner7606fb62003-02-24 20:37:56 +0000208 return true;
209
Eli Friedmanae8161e2011-07-27 00:46:46 +0000210 // Check the unknown instructions...
211 if (!UnknownInsts.empty()) {
212 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i)
Sanjoy Das3f1e8e02017-03-11 01:15:48 +0000213 if (auto *Inst = getUnknownInst(i))
214 if (AA.getModRefInfo(Inst, MemoryLocation(Ptr, Size, AAInfo)) !=
215 MRI_NoModRef)
216 return true;
Chris Lattner9b323c32004-07-27 02:20:26 +0000217 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000218
Hal Finkel1140e172015-10-28 22:13:41 +0000219 return false;
Chris Lattner647df642002-09-26 21:49:07 +0000220}
221
Pete Cooper4bf388d2015-05-13 01:12:12 +0000222bool AliasSet::aliasesUnknownInst(const Instruction *Inst,
Hal Finkel1140e172015-10-28 22:13:41 +0000223 AliasAnalysis &AA) const {
Michael Kuperstein41898f02016-08-19 17:05:22 +0000224
225 if (AliasAny)
226 return true;
227
Eli Friedmanae8161e2011-07-27 00:46:46 +0000228 if (!Inst->mayReadOrWriteMemory())
Duncan Sands68b6f502007-12-01 07:51:45 +0000229 return false;
Chris Lattner21c60f12004-03-15 04:08:36 +0000230
Eli Friedmanae8161e2011-07-27 00:46:46 +0000231 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
Xin Tong70f75122017-06-25 12:55:11 +0000232 if (auto *UnknownInst = getUnknownInst(i)) {
233 ImmutableCallSite C1(UnknownInst), C2(Inst);
Sanjoy Das3f1e8e02017-03-11 01:15:48 +0000234 if (!C1 || !C2 || AA.getModRefInfo(C1, C2) != MRI_NoModRef ||
235 AA.getModRefInfo(C2, C1) != MRI_NoModRef)
236 return true;
237 }
Chris Lattnerf58382e2010-08-29 18:42:23 +0000238 }
Chris Lattner9b323c32004-07-27 02:20:26 +0000239
Hal Finkel1140e172015-10-28 22:13:41 +0000240 for (iterator I = begin(), E = end(); I != E; ++I)
241 if (AA.getModRefInfo(Inst, MemoryLocation(I.getPointer(), I.getSize(),
242 I.getAAInfo())) != MRI_NoModRef)
243 return true;
244
245 return false;
Chris Lattner647df642002-09-26 21:49:07 +0000246}
247
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000248void AliasSetTracker::clear() {
249 // Delete all the PointerRec entries.
Dan Gohmanf4362da2009-07-30 20:21:41 +0000250 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
251 I != E; ++I)
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000252 I->second->eraseFromList();
253
254 PointerMap.clear();
255
256 // The alias sets should all be clear now.
257 AliasSets.clear();
258}
259
Chris Lattner647df642002-09-26 21:49:07 +0000260
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000261/// mergeAliasSetsForPointer - Given a pointer, merge all alias sets that may
262/// alias the pointer. Return the unified set, or nullptr if no set that aliases
263/// the pointer was found.
264AliasSet *AliasSetTracker::mergeAliasSetsForPointer(const Value *Ptr,
265 uint64_t Size,
266 const AAMDNodes &AAInfo) {
Craig Topper9f008862014-04-15 04:59:12 +0000267 AliasSet *FoundSet = nullptr;
David Majnemerb7adf342014-11-19 09:41:05 +0000268 for (iterator I = begin(), E = end(); I != E;) {
269 iterator Cur = I++;
Hal Finkel1140e172015-10-28 22:13:41 +0000270 if (Cur->Forward || !Cur->aliasesPointer(Ptr, Size, AAInfo, AA)) continue;
Chris Lattnerafb70742010-08-29 04:06:55 +0000271
Craig Topper9f008862014-04-15 04:59:12 +0000272 if (!FoundSet) { // If this is the first alias set ptr can go into.
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000273 FoundSet = &*Cur; // Remember it.
Chris Lattnerafb70742010-08-29 04:06:55 +0000274 } else { // Otherwise, we must merge the sets.
David Majnemerb7adf342014-11-19 09:41:05 +0000275 FoundSet->mergeSetIn(*Cur, *this); // Merge in contents.
Chris Lattner647df642002-09-26 21:49:07 +0000276 }
Chris Lattnerafb70742010-08-29 04:06:55 +0000277 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000278
279 return FoundSet;
280}
281
Pete Cooper4bf388d2015-05-13 01:12:12 +0000282bool AliasSetTracker::containsUnknown(const Instruction *Inst) const {
Benjamin Krameraa209152016-06-26 17:27:42 +0000283 for (const AliasSet &AS : *this)
284 if (!AS.Forward && AS.aliasesUnknownInst(Inst, AA))
Hal Finkel840257a2014-11-03 23:19:16 +0000285 return true;
286 return false;
287}
Chris Lattnereeaa29c2004-11-26 21:36:25 +0000288
Hal Finkel1140e172015-10-28 22:13:41 +0000289AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) {
Craig Topper9f008862014-04-15 04:59:12 +0000290 AliasSet *FoundSet = nullptr;
David Majnemerb7adf342014-11-19 09:41:05 +0000291 for (iterator I = begin(), E = end(); I != E;) {
292 iterator Cur = I++;
Hal Finkel1140e172015-10-28 22:13:41 +0000293 if (Cur->Forward || !Cur->aliasesUnknownInst(Inst, AA))
Chris Lattnerafb70742010-08-29 04:06:55 +0000294 continue;
Craig Topper9f008862014-04-15 04:59:12 +0000295 if (!FoundSet) // If this is the first alias set ptr can go into.
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000296 FoundSet = &*Cur; // Remember it.
David Majnemerb7adf342014-11-19 09:41:05 +0000297 else if (!Cur->Forward) // Otherwise, we must merge the sets.
298 FoundSet->mergeSetIn(*Cur, *this); // Merge in contents.
Chris Lattnerafb70742010-08-29 04:06:55 +0000299 }
Chris Lattner647df642002-09-26 21:49:07 +0000300 return FoundSet;
301}
302
Chris Lattner7606fb62003-02-24 20:37:56 +0000303/// getAliasSetForPointer - Return the alias set that the specified pointer
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000304/// lives in.
Dan Gohmanf372cf82010-10-19 22:54:46 +0000305AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, uint64_t Size,
Chad Rosier16970a82016-10-19 18:50:32 +0000306 const AAMDNodes &AAInfo) {
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000307 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
Chris Lattner7606fb62003-02-24 20:37:56 +0000308
Michael Kuperstein41898f02016-08-19 17:05:22 +0000309 if (AliasAnyAS) {
310 // At this point, the AST is saturated, so we only have one active alias
311 // set. That means we already know which alias set we want to return, and
312 // just need to add the pointer to that set to keep the data structure
313 // consistent.
314 // This, of course, means that we will never need a merge here.
315 if (Entry.hasAliasSet()) {
316 Entry.updateSizeAndAAInfo(Size, AAInfo);
317 assert(Entry.getAliasSet(*this) == AliasAnyAS &&
318 "Entry in saturated AST must belong to only alias set");
319 } else {
320 AliasAnyAS->addPointer(*this, Entry, Size, AAInfo);
321 }
322 return *AliasAnyAS;
323 }
324
Chris Lattnereef6b192010-08-29 04:13:43 +0000325 // Check to see if the pointer is already known.
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000326 if (Entry.hasAliasSet()) {
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000327 // If the size changed, we may need to merge several alias sets.
328 // Note that we can *not* return the result of mergeAliasSetsForPointer
329 // due to a quirk of alias analysis behavior. Since alias(undef, undef)
330 // is NoAlias, mergeAliasSetsForPointer(undef, ...) will not find the
331 // the right set for undef, even if it exists.
332 if (Entry.updateSizeAndAAInfo(Size, AAInfo))
333 mergeAliasSetsForPointer(Pointer, Size, AAInfo);
Chris Lattner7606fb62003-02-24 20:37:56 +0000334 // Return the set!
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000335 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattnerafb70742010-08-29 04:06:55 +0000336 }
337
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000338 if (AliasSet *AS = mergeAliasSetsForPointer(Pointer, Size, AAInfo)) {
Chris Lattnereef6b192010-08-29 04:13:43 +0000339 // Add it to the alias set it aliases.
Hal Finkel1140e172015-10-28 22:13:41 +0000340 AS->addPointer(*this, Entry, Size, AAInfo);
Chris Lattner7606fb62003-02-24 20:37:56 +0000341 return *AS;
Chris Lattner647df642002-09-26 21:49:07 +0000342 }
Chris Lattnerafb70742010-08-29 04:06:55 +0000343
Chris Lattnereef6b192010-08-29 04:13:43 +0000344 // Otherwise create a new alias set to hold the loaded pointer.
Chris Lattnerafb70742010-08-29 04:06:55 +0000345 AliasSets.push_back(new AliasSet());
Hal Finkel1140e172015-10-28 22:13:41 +0000346 AliasSets.back().addPointer(*this, Entry, Size, AAInfo);
Chris Lattnerafb70742010-08-29 04:06:55 +0000347 return AliasSets.back();
Chris Lattner647df642002-09-26 21:49:07 +0000348}
349
Chad Rosier16970a82016-10-19 18:50:32 +0000350void AliasSetTracker::add(Value *Ptr, uint64_t Size, const AAMDNodes &AAInfo) {
351 addPointer(Ptr, Size, AAInfo, AliasSet::NoAccess);
Chris Lattnerbf8c3c42004-07-26 05:50:23 +0000352}
353
Chad Rosier16970a82016-10-19 18:50:32 +0000354void AliasSetTracker::add(LoadInst *LI) {
JF Bastien800f87a2016-04-06 21:19:33 +0000355 if (isStrongerThanMonotonic(LI->getOrdering())) return addUnknown(LI);
Hal Finkelcc39b672014-07-24 12:16:19 +0000356
357 AAMDNodes AAInfo;
358 LI->getAAMetadata(AAInfo);
359
Hal Finkel1140e172015-10-28 22:13:41 +0000360 AliasSet::AccessLattice Access = AliasSet::RefAccess;
Chandler Carruth50fee932015-08-06 02:05:46 +0000361 const DataLayout &DL = LI->getModule()->getDataLayout();
Chris Lattner2cfaef22004-07-21 05:18:04 +0000362 AliasSet &AS = addPointer(LI->getOperand(0),
Chad Rosier16970a82016-10-19 18:50:32 +0000363 DL.getTypeStoreSize(LI->getType()), AAInfo, Access);
Chris Lattner0a140602003-12-14 04:52:11 +0000364 if (LI->isVolatile()) AS.setVolatile();
Chris Lattner7606fb62003-02-24 20:37:56 +0000365}
366
Chad Rosier16970a82016-10-19 18:50:32 +0000367void AliasSetTracker::add(StoreInst *SI) {
JF Bastien800f87a2016-04-06 21:19:33 +0000368 if (isStrongerThanMonotonic(SI->getOrdering())) return addUnknown(SI);
Hal Finkelcc39b672014-07-24 12:16:19 +0000369
370 AAMDNodes AAInfo;
371 SI->getAAMetadata(AAInfo);
372
Hal Finkel1140e172015-10-28 22:13:41 +0000373 AliasSet::AccessLattice Access = AliasSet::ModAccess;
Chandler Carruth50fee932015-08-06 02:05:46 +0000374 const DataLayout &DL = SI->getModule()->getDataLayout();
Chris Lattner2cfaef22004-07-21 05:18:04 +0000375 Value *Val = SI->getOperand(0);
Chad Rosier16970a82016-10-19 18:50:32 +0000376 AliasSet &AS = addPointer(
377 SI->getOperand(1), DL.getTypeStoreSize(Val->getType()), AAInfo, Access);
Chris Lattner0a140602003-12-14 04:52:11 +0000378 if (SI->isVolatile()) AS.setVolatile();
Chris Lattner647df642002-09-26 21:49:07 +0000379}
380
Chad Rosier16970a82016-10-19 18:50:32 +0000381void AliasSetTracker::add(VAArgInst *VAAI) {
Hal Finkelcc39b672014-07-24 12:16:19 +0000382 AAMDNodes AAInfo;
383 VAAI->getAAMetadata(AAInfo);
384
Chandler Carruthecbd1682015-06-17 07:21:38 +0000385 addPointer(VAAI->getOperand(0), MemoryLocation::UnknownSize, AAInfo,
Chad Rosier16970a82016-10-19 18:50:32 +0000386 AliasSet::ModRefAccess);
Dan Gohman2cd8e382008-04-14 18:34:50 +0000387}
388
Chad Rosier16970a82016-10-19 18:50:32 +0000389void AliasSetTracker::add(MemSetInst *MSI) {
Haicheng Wu5cf99092016-02-17 02:01:50 +0000390 AAMDNodes AAInfo;
391 MSI->getAAMetadata(AAInfo);
392
Haicheng Wu5cf99092016-02-17 02:01:50 +0000393 uint64_t Len;
394
395 if (ConstantInt *C = dyn_cast<ConstantInt>(MSI->getLength()))
396 Len = C->getZExtValue();
397 else
398 Len = MemoryLocation::UnknownSize;
399
400 AliasSet &AS =
Chad Rosier16970a82016-10-19 18:50:32 +0000401 addPointer(MSI->getRawDest(), Len, AAInfo, AliasSet::ModAccess);
Haicheng Wu5cf99092016-02-17 02:01:50 +0000402 if (MSI->isVolatile())
403 AS.setVolatile();
Haicheng Wu5cf99092016-02-17 02:01:50 +0000404}
Chris Lattner0a140602003-12-14 04:52:11 +0000405
Chad Rosier6e3a92e2016-10-19 19:09:03 +0000406void AliasSetTracker::add(MemTransferInst *MTI) {
407 AAMDNodes AAInfo;
408 MTI->getAAMetadata(AAInfo);
409
410 uint64_t Len;
411 if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
412 Len = C->getZExtValue();
413 else
414 Len = MemoryLocation::UnknownSize;
415
416 AliasSet &ASSrc =
417 addPointer(MTI->getRawSource(), Len, AAInfo, AliasSet::RefAccess);
418 if (MTI->isVolatile())
419 ASSrc.setVolatile();
420
421 AliasSet &ASDst =
422 addPointer(MTI->getRawDest(), Len, AAInfo, AliasSet::ModAccess);
423 if (MTI->isVolatile())
424 ASDst.setVolatile();
425}
426
Chad Rosier16970a82016-10-19 18:50:32 +0000427void AliasSetTracker::addUnknown(Instruction *Inst) {
428 if (isa<DbgInfoIntrinsic>(Inst))
429 return; // Ignore DbgInfo Intrinsics.
Chad Rosier8f348012016-11-07 14:11:45 +0000430
431 if (auto *II = dyn_cast<IntrinsicInst>(Inst)) {
432 // These intrinsics will show up as affecting memory, but they are just
433 // markers.
434 switch (II->getIntrinsicID()) {
435 default:
436 break;
437 // FIXME: Add lifetime/invariant intrinsics (See: PR30807).
438 case Intrinsic::assume:
439 return;
440 }
441 }
Eli Friedmanae8161e2011-07-27 00:46:46 +0000442 if (!Inst->mayReadOrWriteMemory())
Chad Rosier16970a82016-10-19 18:50:32 +0000443 return; // doesn't alias anything
Chris Lattner7f04ebc2004-03-15 06:28:07 +0000444
Hal Finkel1140e172015-10-28 22:13:41 +0000445 AliasSet *AS = findAliasSetForUnknownInst(Inst);
Chris Lattnerafb70742010-08-29 04:06:55 +0000446 if (AS) {
Hal Finkel1140e172015-10-28 22:13:41 +0000447 AS->addUnknownInst(Inst, AA);
Chad Rosier16970a82016-10-19 18:50:32 +0000448 return;
Chris Lattner7606fb62003-02-24 20:37:56 +0000449 }
Chris Lattnerafb70742010-08-29 04:06:55 +0000450 AliasSets.push_back(new AliasSet());
451 AS = &AliasSets.back();
Hal Finkel1140e172015-10-28 22:13:41 +0000452 AS->addUnknownInst(Inst, AA);
Chris Lattner647df642002-09-26 21:49:07 +0000453}
454
Chad Rosier16970a82016-10-19 18:50:32 +0000455void AliasSetTracker::add(Instruction *I) {
Chris Lattnerafb70742010-08-29 04:06:55 +0000456 // Dispatch to one of the other add methods.
Chris Lattner7606fb62003-02-24 20:37:56 +0000457 if (LoadInst *LI = dyn_cast<LoadInst>(I))
Chris Lattner2cfaef22004-07-21 05:18:04 +0000458 return add(LI);
Chris Lattnerafb70742010-08-29 04:06:55 +0000459 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner2cfaef22004-07-21 05:18:04 +0000460 return add(SI);
Chris Lattnerafb70742010-08-29 04:06:55 +0000461 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
Dan Gohman2cd8e382008-04-14 18:34:50 +0000462 return add(VAAI);
Haicheng Wu5cf99092016-02-17 02:01:50 +0000463 if (MemSetInst *MSI = dyn_cast<MemSetInst>(I))
464 return add(MSI);
Chad Rosier6e3a92e2016-10-19 19:09:03 +0000465 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(I))
466 return add(MTI);
Eli Friedmanae8161e2011-07-27 00:46:46 +0000467 return addUnknown(I);
Chris Lattner647df642002-09-26 21:49:07 +0000468}
469
Chris Lattnerc048bb32003-03-03 23:28:05 +0000470void AliasSetTracker::add(BasicBlock &BB) {
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000471 for (auto &I : BB)
472 add(&I);
Chris Lattnerc048bb32003-03-03 23:28:05 +0000473}
474
475void AliasSetTracker::add(const AliasSetTracker &AST) {
476 assert(&AA == &AST.AA &&
477 "Merging AliasSetTracker objects with different Alias Analyses!");
478
479 // Loop over all of the alias sets in AST, adding the pointers contained
480 // therein into the current alias sets. This can cause alias sets to be
481 // merged together in the current AST.
Benjamin Krameraa209152016-06-26 17:27:42 +0000482 for (const AliasSet &AS : AST) {
483 if (AS.Forward)
484 continue; // Ignore forwarding alias sets
Chris Lattnerc048bb32003-03-03 23:28:05 +0000485
Chris Lattnerafb70742010-08-29 04:06:55 +0000486 // If there are any call sites in the alias set, add them to this AST.
Eli Friedmanae8161e2011-07-27 00:46:46 +0000487 for (unsigned i = 0, e = AS.UnknownInsts.size(); i != e; ++i)
Sanjoy Das3f1e8e02017-03-11 01:15:48 +0000488 if (auto *Inst = AS.getUnknownInst(i))
489 add(Inst);
Chris Lattnerc048bb32003-03-03 23:28:05 +0000490
Chris Lattnerafb70742010-08-29 04:06:55 +0000491 // Loop over all of the pointers in this alias set.
Chris Lattnerafb70742010-08-29 04:06:55 +0000492 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) {
Chad Rosier16970a82016-10-19 18:50:32 +0000493 AliasSet &NewAS =
494 addPointer(ASI.getPointer(), ASI.getSize(), ASI.getAAInfo(),
495 (AliasSet::AccessLattice)AS.Access);
Chris Lattnerafb70742010-08-29 04:06:55 +0000496 if (AS.isVolatile()) NewAS.setVolatile();
Chris Lattnerc048bb32003-03-03 23:28:05 +0000497 }
Chris Lattnerafb70742010-08-29 04:06:55 +0000498 }
Chris Lattnerc048bb32003-03-03 23:28:05 +0000499}
500
Chris Lattner746e1e12004-05-23 21:10:58 +0000501// deleteValue method - This method is used to remove a pointer value from the
Chris Lattner44fea542003-12-18 08:11:56 +0000502// AliasSetTracker entirely. It should be used when an instruction is deleted
503// from the program to update the AST. If you don't use this, you would have
504// dangling pointers to deleted instructions.
505//
Chris Lattner746e1e12004-05-23 21:10:58 +0000506void AliasSetTracker::deleteValue(Value *PtrVal) {
Chris Lattnerab644812004-09-14 19:15:32 +0000507 // First, look up the PointerRec for this pointer.
Benjamin Kramere2ef47c2012-06-30 22:37:15 +0000508 PointerMapType::iterator I = PointerMap.find_as(PtrVal);
Chris Lattner44fea542003-12-18 08:11:56 +0000509 if (I == PointerMap.end()) return; // Noop
510
511 // If we found one, remove the pointer from the alias set it is in.
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000512 AliasSet::PointerRec *PtrValEnt = I->second;
513 AliasSet *AS = PtrValEnt->getAliasSet(*this);
Chris Lattner44fea542003-12-18 08:11:56 +0000514
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000515 // Unlink and delete from the list of values.
516 PtrValEnt->eraseFromList();
Michael Kuperstein41898f02016-08-19 17:05:22 +0000517
518 if (AS->Alias == AliasSet::SetMayAlias) {
519 AS->SetSize--;
520 TotalMayAliasSetSize--;
521 }
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000522
523 // Stop using the alias set.
Chris Lattner053427f2004-07-22 07:58:18 +0000524 AS->dropRef(*this);
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000525
Chris Lattner44fea542003-12-18 08:11:56 +0000526 PointerMap.erase(I);
527}
528
Chris Lattnerab644812004-09-14 19:15:32 +0000529// copyValue - This method should be used whenever a preexisting value in the
530// program is copied or cloned, introducing a new value. Note that it is ok for
531// clients that use this method to introduce the same value multiple times: if
532// the tracker already knows about a value, it will ignore the request.
533//
534void AliasSetTracker::copyValue(Value *From, Value *To) {
Chris Lattnerab644812004-09-14 19:15:32 +0000535 // First, look up the PointerRec for this pointer.
Benjamin Kramere2ef47c2012-06-30 22:37:15 +0000536 PointerMapType::iterator I = PointerMap.find_as(From);
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000537 if (I == PointerMap.end())
Chris Lattnerab644812004-09-14 19:15:32 +0000538 return; // Noop
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000539 assert(I->second->hasAliasSet() && "Dead entry?");
Chris Lattnerab644812004-09-14 19:15:32 +0000540
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000541 AliasSet::PointerRec &Entry = getEntryFor(To);
542 if (Entry.hasAliasSet()) return; // Already in the tracker!
Chris Lattnerab644812004-09-14 19:15:32 +0000543
Xinliang David Li1ce88fa2016-08-11 23:09:56 +0000544 // getEntryFor above may invalidate iterator \c I, so reinitialize it.
Benjamin Kramere2ef47c2012-06-30 22:37:15 +0000545 I = PointerMap.find_as(From);
Xinliang David Li1ce88fa2016-08-11 23:09:56 +0000546 // Add it to the alias set it aliases...
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000547 AliasSet *AS = I->second->getAliasSet(*this);
Dan Gohman408beac2010-10-18 21:28:00 +0000548 AS->addPointer(*this, Entry, I->second->getSize(),
Hal Finkel1140e172015-10-28 22:13:41 +0000549 I->second->getAAInfo(),
Dan Gohman71af9db2010-10-18 20:44:50 +0000550 true);
Chris Lattnerab644812004-09-14 19:15:32 +0000551}
552
Michael Kuperstein41898f02016-08-19 17:05:22 +0000553AliasSet &AliasSetTracker::mergeAllAliasSets() {
554 assert(!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold) &&
555 "Full merge should happen once, when the saturation threshold is "
556 "reached");
Chris Lattnerab644812004-09-14 19:15:32 +0000557
Michael Kuperstein41898f02016-08-19 17:05:22 +0000558 // Collect all alias sets, so that we can drop references with impunity
559 // without worrying about iterator invalidation.
560 std::vector<AliasSet *> ASVector;
561 ASVector.reserve(SaturationThreshold);
562 for (iterator I = begin(), E = end(); I != E; I++)
563 ASVector.push_back(&*I);
564
565 // Copy all instructions and pointers into a new set, and forward all other
566 // sets to it.
567 AliasSets.push_back(new AliasSet());
568 AliasAnyAS = &AliasSets.back();
569 AliasAnyAS->Alias = AliasSet::SetMayAlias;
570 AliasAnyAS->Access = AliasSet::ModRefAccess;
571 AliasAnyAS->AliasAny = true;
572
573 for (auto Cur : ASVector) {
574
575 // If Cur was already forwarding, just forward to the new AS instead.
576 AliasSet *FwdTo = Cur->Forward;
577 if (FwdTo) {
578 Cur->Forward = AliasAnyAS;
579 AliasAnyAS->addRef();
580 FwdTo->dropRef(*this);
581 continue;
582 }
583
584 // Otherwise, perform the actual merge.
585 AliasAnyAS->mergeSetIn(*Cur, *this);
586 }
587
588 return *AliasAnyAS;
589}
590
591AliasSet &AliasSetTracker::addPointer(Value *P, uint64_t Size,
592 const AAMDNodes &AAInfo,
Chad Rosier16970a82016-10-19 18:50:32 +0000593 AliasSet::AccessLattice E) {
Chad Rosier16970a82016-10-19 18:50:32 +0000594 AliasSet &AS = getAliasSetForPointer(P, Size, AAInfo);
Michael Kuperstein41898f02016-08-19 17:05:22 +0000595 AS.Access |= E;
596
597 if (!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold)) {
598 // The AST is now saturated. From here on, we conservatively consider all
599 // pointers to alias each-other.
600 return mergeAllAliasSets();
601 }
602
603 return AS;
604}
Chris Lattner44fea542003-12-18 08:11:56 +0000605
Chris Lattner7606fb62003-02-24 20:37:56 +0000606//===----------------------------------------------------------------------===//
607// AliasSet/AliasSetTracker Printing Support
608//===----------------------------------------------------------------------===//
609
Chris Lattnerb1d782b2009-08-23 05:17:37 +0000610void AliasSet::print(raw_ostream &OS) const {
Roman Divackyad06cee2012-09-05 22:26:57 +0000611 OS << " AliasSet[" << (const void*)this << ", " << RefCount << "] ";
Chandler Carrutha561d752015-06-22 02:12:52 +0000612 OS << (Alias == SetMustAlias ? "must" : "may") << " alias, ";
613 switch (Access) {
Hal Finkel1140e172015-10-28 22:13:41 +0000614 case NoAccess: OS << "No access "; break;
615 case RefAccess: OS << "Ref "; break;
616 case ModAccess: OS << "Mod "; break;
617 case ModRefAccess: OS << "Mod/Ref "; break;
Chandler Carrutha561d752015-06-22 02:12:52 +0000618 default: llvm_unreachable("Bad value for Access!");
Chris Lattner647df642002-09-26 21:49:07 +0000619 }
Chris Lattner0a140602003-12-14 04:52:11 +0000620 if (isVolatile()) OS << "[volatile] ";
Chris Lattner7606fb62003-02-24 20:37:56 +0000621 if (Forward)
622 OS << " forwarding to " << (void*)Forward;
623
Dan Gohmanc731c972007-10-03 19:26:29 +0000624 if (!empty()) {
Chris Lattner7606fb62003-02-24 20:37:56 +0000625 OS << "Pointers: ";
626 for (iterator I = begin(), E = end(); I != E; ++I) {
627 if (I != begin()) OS << ", ";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000628 I.getPointer()->printAsOperand(OS << "(");
Chris Lattner053427f2004-07-22 07:58:18 +0000629 OS << ", " << I.getSize() << ")";
Chris Lattner7606fb62003-02-24 20:37:56 +0000630 }
631 }
Eli Friedmanae8161e2011-07-27 00:46:46 +0000632 if (!UnknownInsts.empty()) {
633 OS << "\n " << UnknownInsts.size() << " Unknown instructions: ";
634 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
Chris Lattner7606fb62003-02-24 20:37:56 +0000635 if (i) OS << ", ";
Sanjoy Das3f1e8e02017-03-11 01:15:48 +0000636 if (auto *I = getUnknownInst(i))
637 I->printAsOperand(OS);
Misha Brukman01808ca2005-04-21 21:13:18 +0000638 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000639 }
640 OS << "\n";
641}
642
Chris Lattnerb1d782b2009-08-23 05:17:37 +0000643void AliasSetTracker::print(raw_ostream &OS) const {
Chris Lattner7606fb62003-02-24 20:37:56 +0000644 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
645 << PointerMap.size() << " pointer values.\n";
Benjamin Krameraa209152016-06-26 17:27:42 +0000646 for (const AliasSet &AS : *this)
647 AS.print(OS);
Chris Lattner7606fb62003-02-24 20:37:56 +0000648 OS << "\n";
649}
650
Aaron Ballman615eb472017-10-15 14:32:27 +0000651#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000652LLVM_DUMP_METHOD void AliasSet::dump() const { print(dbgs()); }
653LLVM_DUMP_METHOD void AliasSetTracker::dump() const { print(dbgs()); }
Manman Renc3366cc2012-09-06 19:55:56 +0000654#endif
Chris Lattner7606fb62003-02-24 20:37:56 +0000655
Chris Lattner7606fb62003-02-24 20:37:56 +0000656//===----------------------------------------------------------------------===//
Dan Gohmanf4362da2009-07-30 20:21:41 +0000657// ASTCallbackVH Class Implementation
658//===----------------------------------------------------------------------===//
659
660void AliasSetTracker::ASTCallbackVH::deleted() {
661 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
662 AST->deleteValue(getValPtr());
663 // this now dangles!
664}
665
Eli Friedman17822fc2011-04-09 06:55:46 +0000666void AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(Value *V) {
667 AST->copyValue(getValPtr(), V);
668}
669
Dan Gohmanf4362da2009-07-30 20:21:41 +0000670AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
Dan Gohmandc2b1b02009-07-31 18:21:48 +0000671 : CallbackVH(V), AST(ast) {}
672
673AliasSetTracker::ASTCallbackVH &
674AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
675 return *this = ASTCallbackVH(V, AST);
676}
Dan Gohmanf4362da2009-07-30 20:21:41 +0000677
678//===----------------------------------------------------------------------===//
Chris Lattner7606fb62003-02-24 20:37:56 +0000679// AliasSetPrinter Pass
680//===----------------------------------------------------------------------===//
681
682namespace {
Eugene Zelenko48666a62017-07-24 23:16:33 +0000683
Nick Lewycky02d5f772009-10-25 06:33:48 +0000684 class AliasSetPrinter : public FunctionPass {
Chris Lattner7606fb62003-02-24 20:37:56 +0000685 AliasSetTracker *Tracker;
Eugene Zelenko48666a62017-07-24 23:16:33 +0000686
Chris Lattner7606fb62003-02-24 20:37:56 +0000687 public:
Nick Lewyckye7da2d62007-05-06 13:37:16 +0000688 static char ID; // Pass identification, replacement for typeid
Eugene Zelenko48666a62017-07-24 23:16:33 +0000689
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000690 AliasSetPrinter() : FunctionPass(ID) {
691 initializeAliasSetPrinterPass(*PassRegistry::getPassRegistry());
692 }
Devang Patel09f162c2007-05-01 21:15:47 +0000693
Craig Toppere9ba7592014-03-05 07:30:04 +0000694 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner7606fb62003-02-24 20:37:56 +0000695 AU.setPreservesAll();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000696 AU.addRequired<AAResultsWrapperPass>();
Chris Lattner7606fb62003-02-24 20:37:56 +0000697 }
698
Craig Toppere9ba7592014-03-05 07:30:04 +0000699 bool runOnFunction(Function &F) override {
Chandler Carruth7b560d42015-09-09 17:55:00 +0000700 auto &AAWP = getAnalysis<AAResultsWrapperPass>();
701 Tracker = new AliasSetTracker(AAWP.getAAResults());
Michael Kuperstein41898f02016-08-19 17:05:22 +0000702 errs() << "Alias sets for function '" << F.getName() << "':\n";
Chris Lattner7606fb62003-02-24 20:37:56 +0000703 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner2d3a7a62004-04-27 15:13:33 +0000704 Tracker->add(&*I);
David Greene0295ecf2009-12-23 22:49:57 +0000705 Tracker->print(errs());
Chris Lattner44497852006-01-03 06:05:22 +0000706 delete Tracker;
Chris Lattner7606fb62003-02-24 20:37:56 +0000707 return false;
708 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000709 };
Eugene Zelenko48666a62017-07-24 23:16:33 +0000710
711} // end anonymous namespace
Dan Gohmand78c4002008-05-13 00:00:25 +0000712
713char AliasSetPrinter::ID = 0;
Eugene Zelenko48666a62017-07-24 23:16:33 +0000714
Owen Anderson8ac477f2010-10-12 19:48:12 +0000715INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets",
716 "Alias Set Printer", false, true)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000717INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Owen Anderson8ac477f2010-10-12 19:48:12 +0000718INITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000719 "Alias Set Printer", false, true)