blob: 7ed23af55896cb8d637a8b35d3c248ea0143f028 [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
Daniel Neilson6b23fb72018-05-30 14:43:39 +0000392void AliasSetTracker::add(AnyMemSetInst *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);
Daniel Neilson6b23fb72018-05-30 14:43:39 +0000405 auto *MS = dyn_cast<MemSetInst>(MSI);
406 if (MS && MS->isVolatile())
Haicheng Wu5cf99092016-02-17 02:01:50 +0000407 AS.setVolatile();
Haicheng Wu5cf99092016-02-17 02:01:50 +0000408}
Chris Lattner0a140602003-12-14 04:52:11 +0000409
Daniel Neilson6b23fb72018-05-30 14:43:39 +0000410void AliasSetTracker::add(AnyMemTransferInst *MTI) {
Chad Rosier6e3a92e2016-10-19 19:09:03 +0000411 AAMDNodes AAInfo;
412 MTI->getAAMetadata(AAInfo);
413
414 uint64_t Len;
415 if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
416 Len = C->getZExtValue();
417 else
418 Len = MemoryLocation::UnknownSize;
419
420 AliasSet &ASSrc =
421 addPointer(MTI->getRawSource(), Len, AAInfo, AliasSet::RefAccess);
Chad Rosier6e3a92e2016-10-19 19:09:03 +0000422
423 AliasSet &ASDst =
424 addPointer(MTI->getRawDest(), Len, AAInfo, AliasSet::ModAccess);
Daniel Neilson6b23fb72018-05-30 14:43:39 +0000425
426 auto* MT = dyn_cast<MemTransferInst>(MTI);
427 if (MT && MT->isVolatile()) {
428 ASSrc.setVolatile();
Chad Rosier6e3a92e2016-10-19 19:09:03 +0000429 ASDst.setVolatile();
Daniel Neilson6b23fb72018-05-30 14:43:39 +0000430 }
Chad Rosier6e3a92e2016-10-19 19:09:03 +0000431}
432
Chad Rosier16970a82016-10-19 18:50:32 +0000433void AliasSetTracker::addUnknown(Instruction *Inst) {
434 if (isa<DbgInfoIntrinsic>(Inst))
435 return; // Ignore DbgInfo Intrinsics.
Chad Rosier8f348012016-11-07 14:11:45 +0000436
437 if (auto *II = dyn_cast<IntrinsicInst>(Inst)) {
438 // These intrinsics will show up as affecting memory, but they are just
439 // markers.
440 switch (II->getIntrinsicID()) {
441 default:
442 break;
443 // FIXME: Add lifetime/invariant intrinsics (See: PR30807).
444 case Intrinsic::assume:
Dan Gohman2c74fe92017-11-08 21:59:51 +0000445 case Intrinsic::sideeffect:
Chad Rosier8f348012016-11-07 14:11:45 +0000446 return;
447 }
448 }
Eli Friedmanae8161e2011-07-27 00:46:46 +0000449 if (!Inst->mayReadOrWriteMemory())
Chad Rosier16970a82016-10-19 18:50:32 +0000450 return; // doesn't alias anything
Chris Lattner7f04ebc2004-03-15 06:28:07 +0000451
Hal Finkel1140e172015-10-28 22:13:41 +0000452 AliasSet *AS = findAliasSetForUnknownInst(Inst);
Chris Lattnerafb70742010-08-29 04:06:55 +0000453 if (AS) {
Hal Finkel1140e172015-10-28 22:13:41 +0000454 AS->addUnknownInst(Inst, AA);
Chad Rosier16970a82016-10-19 18:50:32 +0000455 return;
Chris Lattner7606fb62003-02-24 20:37:56 +0000456 }
Chris Lattnerafb70742010-08-29 04:06:55 +0000457 AliasSets.push_back(new AliasSet());
458 AS = &AliasSets.back();
Hal Finkel1140e172015-10-28 22:13:41 +0000459 AS->addUnknownInst(Inst, AA);
Chris Lattner647df642002-09-26 21:49:07 +0000460}
461
Chad Rosier16970a82016-10-19 18:50:32 +0000462void AliasSetTracker::add(Instruction *I) {
Chris Lattnerafb70742010-08-29 04:06:55 +0000463 // Dispatch to one of the other add methods.
Chris Lattner7606fb62003-02-24 20:37:56 +0000464 if (LoadInst *LI = dyn_cast<LoadInst>(I))
Chris Lattner2cfaef22004-07-21 05:18:04 +0000465 return add(LI);
Chris Lattnerafb70742010-08-29 04:06:55 +0000466 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner2cfaef22004-07-21 05:18:04 +0000467 return add(SI);
Chris Lattnerafb70742010-08-29 04:06:55 +0000468 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
Dan Gohman2cd8e382008-04-14 18:34:50 +0000469 return add(VAAI);
Daniel Neilson6b23fb72018-05-30 14:43:39 +0000470 if (AnyMemSetInst *MSI = dyn_cast<AnyMemSetInst>(I))
Haicheng Wu5cf99092016-02-17 02:01:50 +0000471 return add(MSI);
Daniel Neilson6b23fb72018-05-30 14:43:39 +0000472 if (AnyMemTransferInst *MTI = dyn_cast<AnyMemTransferInst>(I))
Chad Rosier6e3a92e2016-10-19 19:09:03 +0000473 return add(MTI);
Eli Friedmanae8161e2011-07-27 00:46:46 +0000474 return addUnknown(I);
Chris Lattner647df642002-09-26 21:49:07 +0000475}
476
Chris Lattnerc048bb32003-03-03 23:28:05 +0000477void AliasSetTracker::add(BasicBlock &BB) {
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000478 for (auto &I : BB)
479 add(&I);
Chris Lattnerc048bb32003-03-03 23:28:05 +0000480}
481
482void AliasSetTracker::add(const AliasSetTracker &AST) {
483 assert(&AA == &AST.AA &&
484 "Merging AliasSetTracker objects with different Alias Analyses!");
485
486 // Loop over all of the alias sets in AST, adding the pointers contained
487 // therein into the current alias sets. This can cause alias sets to be
488 // merged together in the current AST.
Benjamin Krameraa209152016-06-26 17:27:42 +0000489 for (const AliasSet &AS : AST) {
490 if (AS.Forward)
491 continue; // Ignore forwarding alias sets
Chris Lattnerc048bb32003-03-03 23:28:05 +0000492
Chris Lattnerafb70742010-08-29 04:06:55 +0000493 // If there are any call sites in the alias set, add them to this AST.
Eli Friedmanae8161e2011-07-27 00:46:46 +0000494 for (unsigned i = 0, e = AS.UnknownInsts.size(); i != e; ++i)
Sanjoy Das3f1e8e02017-03-11 01:15:48 +0000495 if (auto *Inst = AS.getUnknownInst(i))
496 add(Inst);
Chris Lattnerc048bb32003-03-03 23:28:05 +0000497
Chris Lattnerafb70742010-08-29 04:06:55 +0000498 // Loop over all of the pointers in this alias set.
Chris Lattnerafb70742010-08-29 04:06:55 +0000499 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) {
Chad Rosier16970a82016-10-19 18:50:32 +0000500 AliasSet &NewAS =
501 addPointer(ASI.getPointer(), ASI.getSize(), ASI.getAAInfo(),
502 (AliasSet::AccessLattice)AS.Access);
Chris Lattnerafb70742010-08-29 04:06:55 +0000503 if (AS.isVolatile()) NewAS.setVolatile();
Chris Lattnerc048bb32003-03-03 23:28:05 +0000504 }
Chris Lattnerafb70742010-08-29 04:06:55 +0000505 }
Chris Lattnerc048bb32003-03-03 23:28:05 +0000506}
507
Chris Lattner746e1e12004-05-23 21:10:58 +0000508// deleteValue method - This method is used to remove a pointer value from the
Chris Lattner44fea542003-12-18 08:11:56 +0000509// AliasSetTracker entirely. It should be used when an instruction is deleted
510// from the program to update the AST. If you don't use this, you would have
511// dangling pointers to deleted instructions.
512//
Chris Lattner746e1e12004-05-23 21:10:58 +0000513void AliasSetTracker::deleteValue(Value *PtrVal) {
Chris Lattnerab644812004-09-14 19:15:32 +0000514 // First, look up the PointerRec for this pointer.
Benjamin Kramere2ef47c2012-06-30 22:37:15 +0000515 PointerMapType::iterator I = PointerMap.find_as(PtrVal);
Chris Lattner44fea542003-12-18 08:11:56 +0000516 if (I == PointerMap.end()) return; // Noop
517
518 // If we found one, remove the pointer from the alias set it is in.
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000519 AliasSet::PointerRec *PtrValEnt = I->second;
520 AliasSet *AS = PtrValEnt->getAliasSet(*this);
Chris Lattner44fea542003-12-18 08:11:56 +0000521
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000522 // Unlink and delete from the list of values.
523 PtrValEnt->eraseFromList();
Michael Kuperstein41898f02016-08-19 17:05:22 +0000524
525 if (AS->Alias == AliasSet::SetMayAlias) {
526 AS->SetSize--;
527 TotalMayAliasSetSize--;
528 }
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000529
530 // Stop using the alias set.
Chris Lattner053427f2004-07-22 07:58:18 +0000531 AS->dropRef(*this);
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000532
Chris Lattner44fea542003-12-18 08:11:56 +0000533 PointerMap.erase(I);
534}
535
Chris Lattnerab644812004-09-14 19:15:32 +0000536// copyValue - This method should be used whenever a preexisting value in the
537// program is copied or cloned, introducing a new value. Note that it is ok for
538// clients that use this method to introduce the same value multiple times: if
539// the tracker already knows about a value, it will ignore the request.
540//
541void AliasSetTracker::copyValue(Value *From, Value *To) {
Chris Lattnerab644812004-09-14 19:15:32 +0000542 // First, look up the PointerRec for this pointer.
Benjamin Kramere2ef47c2012-06-30 22:37:15 +0000543 PointerMapType::iterator I = PointerMap.find_as(From);
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000544 if (I == PointerMap.end())
Chris Lattnerab644812004-09-14 19:15:32 +0000545 return; // Noop
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000546 assert(I->second->hasAliasSet() && "Dead entry?");
Chris Lattnerab644812004-09-14 19:15:32 +0000547
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000548 AliasSet::PointerRec &Entry = getEntryFor(To);
549 if (Entry.hasAliasSet()) return; // Already in the tracker!
Chris Lattnerab644812004-09-14 19:15:32 +0000550
Xinliang David Li1ce88fa2016-08-11 23:09:56 +0000551 // getEntryFor above may invalidate iterator \c I, so reinitialize it.
Benjamin Kramere2ef47c2012-06-30 22:37:15 +0000552 I = PointerMap.find_as(From);
Xinliang David Li1ce88fa2016-08-11 23:09:56 +0000553 // Add it to the alias set it aliases...
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000554 AliasSet *AS = I->second->getAliasSet(*this);
Dan Gohman408beac2010-10-18 21:28:00 +0000555 AS->addPointer(*this, Entry, I->second->getSize(),
Hal Finkel1140e172015-10-28 22:13:41 +0000556 I->second->getAAInfo(),
Dan Gohman71af9db2010-10-18 20:44:50 +0000557 true);
Chris Lattnerab644812004-09-14 19:15:32 +0000558}
559
Michael Kuperstein41898f02016-08-19 17:05:22 +0000560AliasSet &AliasSetTracker::mergeAllAliasSets() {
561 assert(!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold) &&
562 "Full merge should happen once, when the saturation threshold is "
563 "reached");
Chris Lattnerab644812004-09-14 19:15:32 +0000564
Michael Kuperstein41898f02016-08-19 17:05:22 +0000565 // Collect all alias sets, so that we can drop references with impunity
566 // without worrying about iterator invalidation.
567 std::vector<AliasSet *> ASVector;
568 ASVector.reserve(SaturationThreshold);
569 for (iterator I = begin(), E = end(); I != E; I++)
570 ASVector.push_back(&*I);
571
572 // Copy all instructions and pointers into a new set, and forward all other
573 // sets to it.
574 AliasSets.push_back(new AliasSet());
575 AliasAnyAS = &AliasSets.back();
576 AliasAnyAS->Alias = AliasSet::SetMayAlias;
577 AliasAnyAS->Access = AliasSet::ModRefAccess;
578 AliasAnyAS->AliasAny = true;
579
580 for (auto Cur : ASVector) {
Michael Kuperstein41898f02016-08-19 17:05:22 +0000581 // If Cur was already forwarding, just forward to the new AS instead.
582 AliasSet *FwdTo = Cur->Forward;
583 if (FwdTo) {
584 Cur->Forward = AliasAnyAS;
Alina Sbirlea63d22502017-12-05 20:12:23 +0000585 AliasAnyAS->addRef();
Michael Kuperstein41898f02016-08-19 17:05:22 +0000586 FwdTo->dropRef(*this);
587 continue;
588 }
589
590 // Otherwise, perform the actual merge.
591 AliasAnyAS->mergeSetIn(*Cur, *this);
592 }
593
594 return *AliasAnyAS;
595}
596
George Burgess IV319be3a2018-05-25 21:16:58 +0000597AliasSet &AliasSetTracker::addPointer(Value *P, LocationSize Size,
Michael Kuperstein41898f02016-08-19 17:05:22 +0000598 const AAMDNodes &AAInfo,
Chad Rosier16970a82016-10-19 18:50:32 +0000599 AliasSet::AccessLattice E) {
Chad Rosier16970a82016-10-19 18:50:32 +0000600 AliasSet &AS = getAliasSetForPointer(P, Size, AAInfo);
Michael Kuperstein41898f02016-08-19 17:05:22 +0000601 AS.Access |= E;
602
603 if (!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold)) {
604 // The AST is now saturated. From here on, we conservatively consider all
605 // pointers to alias each-other.
606 return mergeAllAliasSets();
607 }
608
609 return AS;
610}
Chris Lattner44fea542003-12-18 08:11:56 +0000611
Chris Lattner7606fb62003-02-24 20:37:56 +0000612//===----------------------------------------------------------------------===//
613// AliasSet/AliasSetTracker Printing Support
614//===----------------------------------------------------------------------===//
615
Chris Lattnerb1d782b2009-08-23 05:17:37 +0000616void AliasSet::print(raw_ostream &OS) const {
Roman Divackyad06cee2012-09-05 22:26:57 +0000617 OS << " AliasSet[" << (const void*)this << ", " << RefCount << "] ";
Chandler Carrutha561d752015-06-22 02:12:52 +0000618 OS << (Alias == SetMustAlias ? "must" : "may") << " alias, ";
619 switch (Access) {
Hal Finkel1140e172015-10-28 22:13:41 +0000620 case NoAccess: OS << "No access "; break;
621 case RefAccess: OS << "Ref "; break;
622 case ModAccess: OS << "Mod "; break;
623 case ModRefAccess: OS << "Mod/Ref "; break;
Chandler Carrutha561d752015-06-22 02:12:52 +0000624 default: llvm_unreachable("Bad value for Access!");
Chris Lattner647df642002-09-26 21:49:07 +0000625 }
Chris Lattner0a140602003-12-14 04:52:11 +0000626 if (isVolatile()) OS << "[volatile] ";
Chris Lattner7606fb62003-02-24 20:37:56 +0000627 if (Forward)
628 OS << " forwarding to " << (void*)Forward;
629
Dan Gohmanc731c972007-10-03 19:26:29 +0000630 if (!empty()) {
Chris Lattner7606fb62003-02-24 20:37:56 +0000631 OS << "Pointers: ";
632 for (iterator I = begin(), E = end(); I != E; ++I) {
633 if (I != begin()) OS << ", ";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000634 I.getPointer()->printAsOperand(OS << "(");
Chris Lattner053427f2004-07-22 07:58:18 +0000635 OS << ", " << I.getSize() << ")";
Chris Lattner7606fb62003-02-24 20:37:56 +0000636 }
637 }
Eli Friedmanae8161e2011-07-27 00:46:46 +0000638 if (!UnknownInsts.empty()) {
639 OS << "\n " << UnknownInsts.size() << " Unknown instructions: ";
640 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
Chris Lattner7606fb62003-02-24 20:37:56 +0000641 if (i) OS << ", ";
Sanjoy Das3f1e8e02017-03-11 01:15:48 +0000642 if (auto *I = getUnknownInst(i))
643 I->printAsOperand(OS);
Misha Brukman01808ca2005-04-21 21:13:18 +0000644 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000645 }
646 OS << "\n";
647}
648
Chris Lattnerb1d782b2009-08-23 05:17:37 +0000649void AliasSetTracker::print(raw_ostream &OS) const {
Chris Lattner7606fb62003-02-24 20:37:56 +0000650 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
651 << PointerMap.size() << " pointer values.\n";
Benjamin Krameraa209152016-06-26 17:27:42 +0000652 for (const AliasSet &AS : *this)
653 AS.print(OS);
Chris Lattner7606fb62003-02-24 20:37:56 +0000654 OS << "\n";
655}
656
Aaron Ballman615eb472017-10-15 14:32:27 +0000657#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000658LLVM_DUMP_METHOD void AliasSet::dump() const { print(dbgs()); }
659LLVM_DUMP_METHOD void AliasSetTracker::dump() const { print(dbgs()); }
Manman Renc3366cc2012-09-06 19:55:56 +0000660#endif
Chris Lattner7606fb62003-02-24 20:37:56 +0000661
Chris Lattner7606fb62003-02-24 20:37:56 +0000662//===----------------------------------------------------------------------===//
Dan Gohmanf4362da2009-07-30 20:21:41 +0000663// ASTCallbackVH Class Implementation
664//===----------------------------------------------------------------------===//
665
666void AliasSetTracker::ASTCallbackVH::deleted() {
667 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
668 AST->deleteValue(getValPtr());
669 // this now dangles!
670}
671
Eli Friedman17822fc2011-04-09 06:55:46 +0000672void AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(Value *V) {
673 AST->copyValue(getValPtr(), V);
674}
675
Dan Gohmanf4362da2009-07-30 20:21:41 +0000676AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
Dan Gohmandc2b1b02009-07-31 18:21:48 +0000677 : CallbackVH(V), AST(ast) {}
678
679AliasSetTracker::ASTCallbackVH &
680AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
681 return *this = ASTCallbackVH(V, AST);
682}
Dan Gohmanf4362da2009-07-30 20:21:41 +0000683
684//===----------------------------------------------------------------------===//
Chris Lattner7606fb62003-02-24 20:37:56 +0000685// AliasSetPrinter Pass
686//===----------------------------------------------------------------------===//
687
688namespace {
Eugene Zelenko48666a62017-07-24 23:16:33 +0000689
Nick Lewycky02d5f772009-10-25 06:33:48 +0000690 class AliasSetPrinter : public FunctionPass {
Chris Lattner7606fb62003-02-24 20:37:56 +0000691 AliasSetTracker *Tracker;
Eugene Zelenko48666a62017-07-24 23:16:33 +0000692
Chris Lattner7606fb62003-02-24 20:37:56 +0000693 public:
Nick Lewyckye7da2d62007-05-06 13:37:16 +0000694 static char ID; // Pass identification, replacement for typeid
Eugene Zelenko48666a62017-07-24 23:16:33 +0000695
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000696 AliasSetPrinter() : FunctionPass(ID) {
697 initializeAliasSetPrinterPass(*PassRegistry::getPassRegistry());
698 }
Devang Patel09f162c2007-05-01 21:15:47 +0000699
Craig Toppere9ba7592014-03-05 07:30:04 +0000700 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner7606fb62003-02-24 20:37:56 +0000701 AU.setPreservesAll();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000702 AU.addRequired<AAResultsWrapperPass>();
Chris Lattner7606fb62003-02-24 20:37:56 +0000703 }
704
Craig Toppere9ba7592014-03-05 07:30:04 +0000705 bool runOnFunction(Function &F) override {
Chandler Carruth7b560d42015-09-09 17:55:00 +0000706 auto &AAWP = getAnalysis<AAResultsWrapperPass>();
707 Tracker = new AliasSetTracker(AAWP.getAAResults());
Michael Kuperstein41898f02016-08-19 17:05:22 +0000708 errs() << "Alias sets for function '" << F.getName() << "':\n";
Chris Lattner7606fb62003-02-24 20:37:56 +0000709 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner2d3a7a62004-04-27 15:13:33 +0000710 Tracker->add(&*I);
David Greene0295ecf2009-12-23 22:49:57 +0000711 Tracker->print(errs());
Chris Lattner44497852006-01-03 06:05:22 +0000712 delete Tracker;
Chris Lattner7606fb62003-02-24 20:37:56 +0000713 return false;
714 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000715 };
Eugene Zelenko48666a62017-07-24 23:16:33 +0000716
717} // end anonymous namespace
Dan Gohmand78c4002008-05-13 00:00:25 +0000718
719char AliasSetPrinter::ID = 0;
Eugene Zelenko48666a62017-07-24 23:16:33 +0000720
Owen Anderson8ac477f2010-10-12 19:48:12 +0000721INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets",
722 "Alias Set Printer", false, true)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000723INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Owen Anderson8ac477f2010-10-12 19:48:12 +0000724INITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000725 "Alias Set Printer", false, true)