blob: 0d0277e9c34e45673d621ee41f1a35d9b7e5cfc6 [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"
Max Kazantsev3c284bd2018-08-30 03:39:16 +000016#include "llvm/Analysis/GuardUtils.h"
Eugene Zelenko48666a62017-07-24 23:16:33 +000017#include "llvm/Analysis/MemoryLocation.h"
Nico Weber432a3882018-04-30 14:59:11 +000018#include "llvm/Config/llvm-config.h"
Eugene Zelenko48666a62017-07-24 23:16:33 +000019#include "llvm/IR/CallSite.h"
20#include "llvm/IR/Constants.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/DataLayout.h"
Eugene Zelenko48666a62017-07-24 23:16:33 +000022#include "llvm/IR/Function.h"
Chandler Carruth83948572014-03-04 10:30:26 +000023#include "llvm/IR/InstIterator.h"
Eugene Zelenko48666a62017-07-24 23:16:33 +000024#include "llvm/IR/Instruction.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Instructions.h"
26#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000027#include "llvm/IR/Module.h"
Max Kazantsev5a10d122018-08-15 06:21:02 +000028#include "llvm/IR/PatternMatch.h"
Eugene Zelenko48666a62017-07-24 23:16:33 +000029#include "llvm/IR/Value.h"
Chris Lattner7606fb62003-02-24 20:37:56 +000030#include "llvm/Pass.h"
Eugene Zelenko48666a62017-07-24 23:16:33 +000031#include "llvm/Support/AtomicOrdering.h"
32#include "llvm/Support/Casting.h"
33#include "llvm/Support/CommandLine.h"
34#include "llvm/Support/Compiler.h"
David Greene2ec90032009-12-23 19:27:59 +000035#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000036#include "llvm/Support/ErrorHandling.h"
Chris Lattnerb1d782b2009-08-23 05:17:37 +000037#include "llvm/Support/raw_ostream.h"
Eugene Zelenko48666a62017-07-24 23:16:33 +000038#include <cassert>
39#include <cstdint>
40#include <vector>
41
Chris Lattner0a140602003-12-14 04:52:11 +000042using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000043
Michael Kuperstein41898f02016-08-19 17:05:22 +000044static cl::opt<unsigned>
45 SaturationThreshold("alias-set-saturation-threshold", cl::Hidden,
46 cl::init(250),
47 cl::desc("The maximum number of pointers may-alias "
48 "sets may contain before degradation"));
49
Chris Lattner24bba4d2004-11-27 18:37:42 +000050/// mergeSetIn - Merge the specified alias set into this alias set.
Chris Lattner647df642002-09-26 21:49:07 +000051///
Chris Lattner24bba4d2004-11-27 18:37:42 +000052void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
Chris Lattner7606fb62003-02-24 20:37:56 +000053 assert(!AS.Forward && "Alias set is already forwarding!");
54 assert(!Forward && "This set is a forwarding set!!");
Chris Lattner647df642002-09-26 21:49:07 +000055
Michael Kuperstein41898f02016-08-19 17:05:22 +000056 bool WasMustAlias = (Alias == SetMustAlias);
Chris Lattner647df642002-09-26 21:49:07 +000057 // Update the alias and access types of this set...
Chandler Carrutha561d752015-06-22 02:12:52 +000058 Access |= AS.Access;
59 Alias |= AS.Alias;
Chris Lattner7606fb62003-02-24 20:37:56 +000060
Chandler Carrutha561d752015-06-22 02:12:52 +000061 if (Alias == SetMustAlias) {
Chris Lattner24bba4d2004-11-27 18:37:42 +000062 // Check that these two merged sets really are must aliases. Since both
63 // used to be must-alias sets, we can just check any pointer from each set
64 // for aliasing.
65 AliasAnalysis &AA = AST.getAliasAnalysis();
Chris Lattner0eab5ec2009-03-09 05:11:09 +000066 PointerRec *L = getSomePointer();
67 PointerRec *R = AS.getSomePointer();
Chris Lattner24bba4d2004-11-27 18:37:42 +000068
69 // If the pointers are not a must-alias pair, this set becomes a may alias.
Chandler Carruthac80dc72015-06-17 07:18:54 +000070 if (AA.alias(MemoryLocation(L->getValue(), L->getSize(), L->getAAInfo()),
71 MemoryLocation(R->getValue(), R->getSize(), R->getAAInfo())) !=
Chandler Carruthc3f49eb2015-06-22 02:16:51 +000072 MustAlias)
Chandler Carrutha561d752015-06-22 02:12:52 +000073 Alias = SetMayAlias;
Chris Lattner24bba4d2004-11-27 18:37:42 +000074 }
75
Michael Kuperstein41898f02016-08-19 17:05:22 +000076 if (Alias == SetMayAlias) {
77 if (WasMustAlias)
78 AST.TotalMayAliasSetSize += size();
79 if (AS.Alias == SetMustAlias)
80 AST.TotalMayAliasSetSize += AS.size();
81 }
82
David Majnemer35639382014-11-19 19:36:18 +000083 bool ASHadUnknownInsts = !AS.UnknownInsts.empty();
Eli Friedmanae8161e2011-07-27 00:46:46 +000084 if (UnknownInsts.empty()) { // Merge call sites...
David Majnemer35639382014-11-19 19:36:18 +000085 if (ASHadUnknownInsts) {
Eli Friedmanae8161e2011-07-27 00:46:46 +000086 std::swap(UnknownInsts, AS.UnknownInsts);
David Majnemerb7adf342014-11-19 09:41:05 +000087 addRef();
88 }
David Majnemer35639382014-11-19 19:36:18 +000089 } else if (ASHadUnknownInsts) {
Eli Friedmanae8161e2011-07-27 00:46:46 +000090 UnknownInsts.insert(UnknownInsts.end(), AS.UnknownInsts.begin(), AS.UnknownInsts.end());
91 AS.UnknownInsts.clear();
Chris Lattner7606fb62003-02-24 20:37:56 +000092 }
Misha Brukman01808ca2005-04-21 21:13:18 +000093
Michael Kuperstein41898f02016-08-19 17:05:22 +000094 AS.Forward = this; // Forward across AS now...
95 addRef(); // AS is now pointing to us...
Chris Lattner7606fb62003-02-24 20:37:56 +000096
97 // Merge the list of constituent pointers...
Chris Lattner44fea542003-12-18 08:11:56 +000098 if (AS.PtrList) {
Michael Kuperstein41898f02016-08-19 17:05:22 +000099 SetSize += AS.size();
100 AS.SetSize = 0;
Chris Lattner44fea542003-12-18 08:11:56 +0000101 *PtrListEnd = AS.PtrList;
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000102 AS.PtrList->setPrevInList(PtrListEnd);
Chris Lattner44fea542003-12-18 08:11:56 +0000103 PtrListEnd = AS.PtrListEnd;
104
Craig Topper9f008862014-04-15 04:59:12 +0000105 AS.PtrList = nullptr;
Chris Lattner44fea542003-12-18 08:11:56 +0000106 AS.PtrListEnd = &AS.PtrList;
Craig Topper9f008862014-04-15 04:59:12 +0000107 assert(*AS.PtrListEnd == nullptr && "End of list is not null?");
Chris Lattner44fea542003-12-18 08:11:56 +0000108 }
David Majnemerb7adf342014-11-19 09:41:05 +0000109 if (ASHadUnknownInsts)
110 AS.dropRef(AST);
Chris Lattner647df642002-09-26 21:49:07 +0000111}
112
Chris Lattner7606fb62003-02-24 20:37:56 +0000113void AliasSetTracker::removeAliasSet(AliasSet *AS) {
Chris Lattner053427f2004-07-22 07:58:18 +0000114 if (AliasSet *Fwd = AS->Forward) {
115 Fwd->dropRef(*this);
Craig Topper9f008862014-04-15 04:59:12 +0000116 AS->Forward = nullptr;
Chris Lattner053427f2004-07-22 07:58:18 +0000117 }
Michael Kuperstein41898f02016-08-19 17:05:22 +0000118
119 if (AS->Alias == AliasSet::SetMayAlias)
120 TotalMayAliasSetSize -= AS->size();
121
Chris Lattner7606fb62003-02-24 20:37:56 +0000122 AliasSets.erase(AS);
123}
124
125void AliasSet::removeFromTracker(AliasSetTracker &AST) {
126 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
127 AST.removeAliasSet(this);
128}
129
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000130void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
George Burgess IV319be3a2018-05-25 21:16:58 +0000131 LocationSize Size, const AAMDNodes &AAInfo,
Hal Finkel1140e172015-10-28 22:13:41 +0000132 bool KnownMustAlias) {
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000133 assert(!Entry.hasAliasSet() && "Entry already in set!");
Chris Lattner7606fb62003-02-24 20:37:56 +0000134
Chris Lattnerab644812004-09-14 19:15:32 +0000135 // Check to see if we have to downgrade to _may_ alias.
136 if (isMustAlias() && !KnownMustAlias)
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000137 if (PointerRec *P = getSomePointer()) {
Chris Lattner6fa96652004-09-15 16:59:47 +0000138 AliasAnalysis &AA = AST.getAliasAnalysis();
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000139 AliasResult Result =
Chandler Carruthac80dc72015-06-17 07:18:54 +0000140 AA.alias(MemoryLocation(P->getValue(), P->getSize(), P->getAAInfo()),
141 MemoryLocation(Entry.getValue(), Size, AAInfo));
Michael Kuperstein41898f02016-08-19 17:05:22 +0000142 if (Result != MustAlias) {
Chandler Carrutha561d752015-06-22 02:12:52 +0000143 Alias = SetMayAlias;
Michael Kuperstein41898f02016-08-19 17:05:22 +0000144 AST.TotalMayAliasSetSize += size();
145 } else {
Fangrui Songf78650a2018-07-30 19:41:25 +0000146 // First entry of must alias must have maximum size!
Hal Finkelcc39b672014-07-24 12:16:19 +0000147 P->updateSizeAndAAInfo(Size, AAInfo);
Michael Kuperstein41898f02016-08-19 17:05:22 +0000148 }
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000149 assert(Result != NoAlias && "Cannot be part of must set!");
Chris Lattnerb5b0b7a2003-02-26 22:11:00 +0000150 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000151
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000152 Entry.setAliasSet(this);
Hal Finkelcc39b672014-07-24 12:16:19 +0000153 Entry.updateSizeAndAAInfo(Size, AAInfo);
Chris Lattner7606fb62003-02-24 20:37:56 +0000154
155 // Add it to the end of the list...
Michael Kuperstein41898f02016-08-19 17:05:22 +0000156 ++SetSize;
Craig Topper9f008862014-04-15 04:59:12 +0000157 assert(*PtrListEnd == nullptr && "End of list is not null?");
Chris Lattner44fea542003-12-18 08:11:56 +0000158 *PtrListEnd = &Entry;
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000159 PtrListEnd = Entry.setPrevInList(PtrListEnd);
Craig Topper9f008862014-04-15 04:59:12 +0000160 assert(*PtrListEnd == nullptr && "End of list is not null?");
Michael Kuperstein41898f02016-08-19 17:05:22 +0000161 // Entry points to alias set.
162 addRef();
163
164 if (Alias == SetMayAlias)
165 AST.TotalMayAliasSetSize++;
Chris Lattner7606fb62003-02-24 20:37:56 +0000166}
167
Hal Finkel1140e172015-10-28 22:13:41 +0000168void AliasSet::addUnknownInst(Instruction *I, AliasAnalysis &AA) {
David Majnemerb7adf342014-11-19 09:41:05 +0000169 if (UnknownInsts.empty())
170 addRef();
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000171 UnknownInsts.emplace_back(I);
Chris Lattner21c60f12004-03-15 04:08:36 +0000172
Max Kazantsev5a10d122018-08-15 06:21:02 +0000173 // Guards are marked as modifying memory for control flow modelling purposes,
174 // but don't actually modify any specific memory location.
175 using namespace PatternMatch;
Max Kazantsev3c284bd2018-08-30 03:39:16 +0000176 bool MayWriteMemory = I->mayWriteToMemory() && !isGuard(I) &&
Philip Reamesa5a85462018-08-21 00:55:35 +0000177 !(I->use_empty() && match(I, m_Intrinsic<Intrinsic::invariant_start>()));
Max Kazantsev5a10d122018-08-15 06:21:02 +0000178 if (!MayWriteMemory) {
Chandler Carrutha561d752015-06-22 02:12:52 +0000179 Alias = SetMayAlias;
Hal Finkel1140e172015-10-28 22:13:41 +0000180 Access |= RefAccess;
Duncan Sands68b6f502007-12-01 07:51:45 +0000181 return;
Chris Lattner21c60f12004-03-15 04:08:36 +0000182 }
183
Hal Finkel1140e172015-10-28 22:13:41 +0000184 // FIXME: This should use mod/ref information to make this not suck so bad
Chandler Carrutha561d752015-06-22 02:12:52 +0000185 Alias = SetMayAlias;
Hal Finkel1140e172015-10-28 22:13:41 +0000186 Access = ModRefAccess;
Chris Lattner7606fb62003-02-24 20:37:56 +0000187}
188
189/// aliasesPointer - Return true if the specified pointer "may" (or must)
Chris Lattner647df642002-09-26 21:49:07 +0000190/// alias one of the members in the set.
191///
George Burgess IV319be3a2018-05-25 21:16:58 +0000192bool AliasSet::aliasesPointer(const Value *Ptr, LocationSize Size,
Hal Finkelcc39b672014-07-24 12:16:19 +0000193 const AAMDNodes &AAInfo,
Hal Finkel1140e172015-10-28 22:13:41 +0000194 AliasAnalysis &AA) const {
Michael Kuperstein41898f02016-08-19 17:05:22 +0000195 if (AliasAny)
196 return true;
197
Chandler Carrutha561d752015-06-22 02:12:52 +0000198 if (Alias == SetMustAlias) {
Eli Friedmanae8161e2011-07-27 00:46:46 +0000199 assert(UnknownInsts.empty() && "Illegal must alias set!");
Chris Lattner7f04ebc2004-03-15 06:28:07 +0000200
Chris Lattner7606fb62003-02-24 20:37:56 +0000201 // If this is a set of MustAliases, only check to see if the pointer aliases
Chris Lattnereef6b192010-08-29 04:13:43 +0000202 // SOME value in the set.
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000203 PointerRec *SomePtr = getSomePointer();
Chris Lattner7606fb62003-02-24 20:37:56 +0000204 assert(SomePtr && "Empty must-alias set??");
Chandler Carruthac80dc72015-06-17 07:18:54 +0000205 return AA.alias(MemoryLocation(SomePtr->getValue(), SomePtr->getSize(),
206 SomePtr->getAAInfo()),
207 MemoryLocation(Ptr, Size, AAInfo));
Chris Lattner7606fb62003-02-24 20:37:56 +0000208 }
209
210 // If this is a may-alias set, we have to check all of the pointers in the set
211 // to be sure it doesn't alias the set...
212 for (iterator I = begin(), E = end(); I != E; ++I)
Chandler Carruthac80dc72015-06-17 07:18:54 +0000213 if (AA.alias(MemoryLocation(Ptr, Size, AAInfo),
214 MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo())))
Chris Lattner7606fb62003-02-24 20:37:56 +0000215 return true;
216
Eli Friedmanae8161e2011-07-27 00:46:46 +0000217 // Check the unknown instructions...
218 if (!UnknownInsts.empty()) {
219 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i)
Sanjoy Das3f1e8e02017-03-11 01:15:48 +0000220 if (auto *Inst = getUnknownInst(i))
Alina Sbirlea63d22502017-12-05 20:12:23 +0000221 if (isModOrRefSet(
222 AA.getModRefInfo(Inst, MemoryLocation(Ptr, Size, AAInfo))))
Sanjoy Das3f1e8e02017-03-11 01:15:48 +0000223 return true;
Chris Lattner9b323c32004-07-27 02:20:26 +0000224 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000225
Hal Finkel1140e172015-10-28 22:13:41 +0000226 return false;
Chris Lattner647df642002-09-26 21:49:07 +0000227}
228
Pete Cooper4bf388d2015-05-13 01:12:12 +0000229bool AliasSet::aliasesUnknownInst(const Instruction *Inst,
Hal Finkel1140e172015-10-28 22:13:41 +0000230 AliasAnalysis &AA) const {
Michael Kuperstein41898f02016-08-19 17:05:22 +0000231
232 if (AliasAny)
233 return true;
234
Eli Friedmanae8161e2011-07-27 00:46:46 +0000235 if (!Inst->mayReadOrWriteMemory())
Duncan Sands68b6f502007-12-01 07:51:45 +0000236 return false;
Chris Lattner21c60f12004-03-15 04:08:36 +0000237
Eli Friedmanae8161e2011-07-27 00:46:46 +0000238 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
Xin Tong70f75122017-06-25 12:55:11 +0000239 if (auto *UnknownInst = getUnknownInst(i)) {
240 ImmutableCallSite C1(UnknownInst), C2(Inst);
Alina Sbirlea63d22502017-12-05 20:12:23 +0000241 if (!C1 || !C2 || isModOrRefSet(AA.getModRefInfo(C1, C2)) ||
242 isModOrRefSet(AA.getModRefInfo(C2, C1)))
Sanjoy Das3f1e8e02017-03-11 01:15:48 +0000243 return true;
244 }
Chris Lattnerf58382e2010-08-29 18:42:23 +0000245 }
Chris Lattner9b323c32004-07-27 02:20:26 +0000246
Hal Finkel1140e172015-10-28 22:13:41 +0000247 for (iterator I = begin(), E = end(); I != E; ++I)
Alina Sbirlea63d22502017-12-05 20:12:23 +0000248 if (isModOrRefSet(AA.getModRefInfo(
249 Inst, MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo()))))
Hal Finkel1140e172015-10-28 22:13:41 +0000250 return true;
251
252 return false;
Chris Lattner647df642002-09-26 21:49:07 +0000253}
254
Philip Reames825c74c2018-08-22 03:32:52 +0000255Instruction* AliasSet::getUniqueInstruction() {
Philip Reames825c74c2018-08-22 03:32:52 +0000256 if (AliasAny)
257 // May have collapses alias set
258 return nullptr;
Philip Reamesf562fc82018-08-29 21:49:30 +0000259 if (begin() != end()) {
260 if (!UnknownInsts.empty())
261 // Another instruction found
262 return nullptr;
263 if (std::next(begin()) != end())
264 // Another instruction found
265 return nullptr;
266 Value *Addr = begin()->getValue();
267 assert(!Addr->user_empty() &&
268 "where's the instruction which added this pointer?");
269 if (std::next(Addr->user_begin()) != Addr->user_end())
270 // Another instruction found -- this is really restrictive
271 // TODO: generalize!
272 return nullptr;
273 return cast<Instruction>(*(Addr->user_begin()));
274 }
Philip Reamesfdd73b52018-08-22 03:36:42 +0000275 if (1 != UnknownInsts.size())
Philip Reames825c74c2018-08-22 03:32:52 +0000276 return nullptr;
277 return cast<Instruction>(UnknownInsts[0]);
278}
279
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000280void AliasSetTracker::clear() {
281 // Delete all the PointerRec entries.
Dan Gohmanf4362da2009-07-30 20:21:41 +0000282 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
283 I != E; ++I)
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000284 I->second->eraseFromList();
Fangrui Songf78650a2018-07-30 19:41:25 +0000285
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000286 PointerMap.clear();
Fangrui Songf78650a2018-07-30 19:41:25 +0000287
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000288 // The alias sets should all be clear now.
289 AliasSets.clear();
290}
291
Chris Lattner647df642002-09-26 21:49:07 +0000292
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000293/// mergeAliasSetsForPointer - Given a pointer, merge all alias sets that may
294/// alias the pointer. Return the unified set, or nullptr if no set that aliases
295/// the pointer was found.
296AliasSet *AliasSetTracker::mergeAliasSetsForPointer(const Value *Ptr,
George Burgess IV319be3a2018-05-25 21:16:58 +0000297 LocationSize Size,
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000298 const AAMDNodes &AAInfo) {
Craig Topper9f008862014-04-15 04:59:12 +0000299 AliasSet *FoundSet = nullptr;
David Majnemerb7adf342014-11-19 09:41:05 +0000300 for (iterator I = begin(), E = end(); I != E;) {
301 iterator Cur = I++;
Hal Finkel1140e172015-10-28 22:13:41 +0000302 if (Cur->Forward || !Cur->aliasesPointer(Ptr, Size, AAInfo, AA)) continue;
Fangrui Songf78650a2018-07-30 19:41:25 +0000303
Craig Topper9f008862014-04-15 04:59:12 +0000304 if (!FoundSet) { // If this is the first alias set ptr can go into.
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000305 FoundSet = &*Cur; // Remember it.
Chris Lattnerafb70742010-08-29 04:06:55 +0000306 } else { // Otherwise, we must merge the sets.
David Majnemerb7adf342014-11-19 09:41:05 +0000307 FoundSet->mergeSetIn(*Cur, *this); // Merge in contents.
Chris Lattner647df642002-09-26 21:49:07 +0000308 }
Chris Lattnerafb70742010-08-29 04:06:55 +0000309 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000310
311 return FoundSet;
312}
313
Pete Cooper4bf388d2015-05-13 01:12:12 +0000314bool AliasSetTracker::containsUnknown(const Instruction *Inst) const {
Benjamin Krameraa209152016-06-26 17:27:42 +0000315 for (const AliasSet &AS : *this)
316 if (!AS.Forward && AS.aliasesUnknownInst(Inst, AA))
Hal Finkel840257a2014-11-03 23:19:16 +0000317 return true;
318 return false;
319}
Chris Lattnereeaa29c2004-11-26 21:36:25 +0000320
Hal Finkel1140e172015-10-28 22:13:41 +0000321AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) {
Craig Topper9f008862014-04-15 04:59:12 +0000322 AliasSet *FoundSet = nullptr;
David Majnemerb7adf342014-11-19 09:41:05 +0000323 for (iterator I = begin(), E = end(); I != E;) {
324 iterator Cur = I++;
Hal Finkel1140e172015-10-28 22:13:41 +0000325 if (Cur->Forward || !Cur->aliasesUnknownInst(Inst, AA))
Chris Lattnerafb70742010-08-29 04:06:55 +0000326 continue;
Craig Topper9f008862014-04-15 04:59:12 +0000327 if (!FoundSet) // If this is the first alias set ptr can go into.
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000328 FoundSet = &*Cur; // Remember it.
David Majnemerb7adf342014-11-19 09:41:05 +0000329 else if (!Cur->Forward) // Otherwise, we must merge the sets.
330 FoundSet->mergeSetIn(*Cur, *this); // Merge in contents.
Chris Lattnerafb70742010-08-29 04:06:55 +0000331 }
Chris Lattner647df642002-09-26 21:49:07 +0000332 return FoundSet;
333}
334
Philip Reames0e2f9b92018-08-16 20:11:15 +0000335AliasSet &AliasSetTracker::getAliasSetFor(const MemoryLocation &MemLoc) {
336
337 Value * const Pointer = const_cast<Value*>(MemLoc.Ptr);
338 const LocationSize Size = MemLoc.Size;
339 const AAMDNodes &AAInfo = MemLoc.AATags;
340
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000341 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
Chris Lattner7606fb62003-02-24 20:37:56 +0000342
Michael Kuperstein41898f02016-08-19 17:05:22 +0000343 if (AliasAnyAS) {
344 // At this point, the AST is saturated, so we only have one active alias
345 // set. That means we already know which alias set we want to return, and
346 // just need to add the pointer to that set to keep the data structure
347 // consistent.
348 // This, of course, means that we will never need a merge here.
349 if (Entry.hasAliasSet()) {
350 Entry.updateSizeAndAAInfo(Size, AAInfo);
351 assert(Entry.getAliasSet(*this) == AliasAnyAS &&
352 "Entry in saturated AST must belong to only alias set");
353 } else {
354 AliasAnyAS->addPointer(*this, Entry, Size, AAInfo);
355 }
356 return *AliasAnyAS;
357 }
358
Chris Lattnereef6b192010-08-29 04:13:43 +0000359 // Check to see if the pointer is already known.
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000360 if (Entry.hasAliasSet()) {
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000361 // If the size changed, we may need to merge several alias sets.
362 // Note that we can *not* return the result of mergeAliasSetsForPointer
363 // due to a quirk of alias analysis behavior. Since alias(undef, undef)
364 // is NoAlias, mergeAliasSetsForPointer(undef, ...) will not find the
365 // the right set for undef, even if it exists.
366 if (Entry.updateSizeAndAAInfo(Size, AAInfo))
367 mergeAliasSetsForPointer(Pointer, Size, AAInfo);
Chris Lattner7606fb62003-02-24 20:37:56 +0000368 // Return the set!
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000369 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattnerafb70742010-08-29 04:06:55 +0000370 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000371
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000372 if (AliasSet *AS = mergeAliasSetsForPointer(Pointer, Size, AAInfo)) {
Chris Lattnereef6b192010-08-29 04:13:43 +0000373 // Add it to the alias set it aliases.
Hal Finkel1140e172015-10-28 22:13:41 +0000374 AS->addPointer(*this, Entry, Size, AAInfo);
Chris Lattner7606fb62003-02-24 20:37:56 +0000375 return *AS;
Chris Lattner647df642002-09-26 21:49:07 +0000376 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000377
Chris Lattnereef6b192010-08-29 04:13:43 +0000378 // Otherwise create a new alias set to hold the loaded pointer.
Chris Lattnerafb70742010-08-29 04:06:55 +0000379 AliasSets.push_back(new AliasSet());
Hal Finkel1140e172015-10-28 22:13:41 +0000380 AliasSets.back().addPointer(*this, Entry, Size, AAInfo);
Chris Lattnerafb70742010-08-29 04:06:55 +0000381 return AliasSets.back();
Chris Lattner647df642002-09-26 21:49:07 +0000382}
383
George Burgess IV319be3a2018-05-25 21:16:58 +0000384void AliasSetTracker::add(Value *Ptr, LocationSize Size,
385 const AAMDNodes &AAInfo) {
Chad Rosier16970a82016-10-19 18:50:32 +0000386 addPointer(Ptr, Size, AAInfo, AliasSet::NoAccess);
Chris Lattnerbf8c3c42004-07-26 05:50:23 +0000387}
388
Chad Rosier16970a82016-10-19 18:50:32 +0000389void AliasSetTracker::add(LoadInst *LI) {
Philip Reamesf8681ce2018-08-22 19:30:46 +0000390 if (isStrongerThanMonotonic(LI->getOrdering()))
391 return addUnknown(LI);
Philip Reamesc3c23e82018-08-21 17:59:11 +0000392 addPointer(MemoryLocation::get(LI), AliasSet::RefAccess);
Chris Lattner7606fb62003-02-24 20:37:56 +0000393}
394
Chad Rosier16970a82016-10-19 18:50:32 +0000395void AliasSetTracker::add(StoreInst *SI) {
Philip Reamesf8681ce2018-08-22 19:30:46 +0000396 if (isStrongerThanMonotonic(SI->getOrdering()))
397 return addUnknown(SI);
Philip Reamesc3c23e82018-08-21 17:59:11 +0000398 addPointer(MemoryLocation::get(SI), AliasSet::ModAccess);
Chris Lattner647df642002-09-26 21:49:07 +0000399}
400
Chad Rosier16970a82016-10-19 18:50:32 +0000401void AliasSetTracker::add(VAArgInst *VAAI) {
Philip Reames90bffb32018-08-13 22:34:14 +0000402 addPointer(MemoryLocation::get(VAAI), AliasSet::ModRefAccess);
Dan Gohman2cd8e382008-04-14 18:34:50 +0000403}
404
Daniel Neilson6b23fb72018-05-30 14:43:39 +0000405void AliasSetTracker::add(AnyMemSetInst *MSI) {
Philip Reamesf8681ce2018-08-22 19:30:46 +0000406 addPointer(MemoryLocation::getForDest(MSI), AliasSet::ModAccess);
Haicheng Wu5cf99092016-02-17 02:01:50 +0000407}
Chris Lattner0a140602003-12-14 04:52:11 +0000408
Daniel Neilson6b23fb72018-05-30 14:43:39 +0000409void AliasSetTracker::add(AnyMemTransferInst *MTI) {
Philip Reamesf8681ce2018-08-22 19:30:46 +0000410 addPointer(MemoryLocation::getForDest(MTI), AliasSet::ModAccess);
Philip Reames5660bd42018-09-10 16:00:27 +0000411 addPointer(MemoryLocation::getForSource(MTI), AliasSet::RefAccess);
Chad Rosier6e3a92e2016-10-19 19:09:03 +0000412}
413
Chad Rosier16970a82016-10-19 18:50:32 +0000414void AliasSetTracker::addUnknown(Instruction *Inst) {
415 if (isa<DbgInfoIntrinsic>(Inst))
416 return; // Ignore DbgInfo Intrinsics.
Chad Rosier8f348012016-11-07 14:11:45 +0000417
418 if (auto *II = dyn_cast<IntrinsicInst>(Inst)) {
419 // These intrinsics will show up as affecting memory, but they are just
420 // markers.
421 switch (II->getIntrinsicID()) {
422 default:
423 break;
424 // FIXME: Add lifetime/invariant intrinsics (See: PR30807).
425 case Intrinsic::assume:
Dan Gohman2c74fe92017-11-08 21:59:51 +0000426 case Intrinsic::sideeffect:
Chad Rosier8f348012016-11-07 14:11:45 +0000427 return;
428 }
429 }
Eli Friedmanae8161e2011-07-27 00:46:46 +0000430 if (!Inst->mayReadOrWriteMemory())
Chad Rosier16970a82016-10-19 18:50:32 +0000431 return; // doesn't alias anything
Chris Lattner7f04ebc2004-03-15 06:28:07 +0000432
Hal Finkel1140e172015-10-28 22:13:41 +0000433 AliasSet *AS = findAliasSetForUnknownInst(Inst);
Chris Lattnerafb70742010-08-29 04:06:55 +0000434 if (AS) {
Hal Finkel1140e172015-10-28 22:13:41 +0000435 AS->addUnknownInst(Inst, AA);
Chad Rosier16970a82016-10-19 18:50:32 +0000436 return;
Chris Lattner7606fb62003-02-24 20:37:56 +0000437 }
Chris Lattnerafb70742010-08-29 04:06:55 +0000438 AliasSets.push_back(new AliasSet());
439 AS = &AliasSets.back();
Hal Finkel1140e172015-10-28 22:13:41 +0000440 AS->addUnknownInst(Inst, AA);
Chris Lattner647df642002-09-26 21:49:07 +0000441}
442
Chad Rosier16970a82016-10-19 18:50:32 +0000443void AliasSetTracker::add(Instruction *I) {
Chris Lattnerafb70742010-08-29 04:06:55 +0000444 // Dispatch to one of the other add methods.
Chris Lattner7606fb62003-02-24 20:37:56 +0000445 if (LoadInst *LI = dyn_cast<LoadInst>(I))
Chris Lattner2cfaef22004-07-21 05:18:04 +0000446 return add(LI);
Chris Lattnerafb70742010-08-29 04:06:55 +0000447 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner2cfaef22004-07-21 05:18:04 +0000448 return add(SI);
Chris Lattnerafb70742010-08-29 04:06:55 +0000449 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
Dan Gohman2cd8e382008-04-14 18:34:50 +0000450 return add(VAAI);
Daniel Neilson6b23fb72018-05-30 14:43:39 +0000451 if (AnyMemSetInst *MSI = dyn_cast<AnyMemSetInst>(I))
Haicheng Wu5cf99092016-02-17 02:01:50 +0000452 return add(MSI);
Daniel Neilson6b23fb72018-05-30 14:43:39 +0000453 if (AnyMemTransferInst *MTI = dyn_cast<AnyMemTransferInst>(I))
Chad Rosier6e3a92e2016-10-19 19:09:03 +0000454 return add(MTI);
Philip Reamescb8b3272018-09-07 21:36:11 +0000455
456 // Handle all calls with known mod/ref sets genericall
457 CallSite CS(I);
458 if (CS && CS.onlyAccessesArgMemory()) {
459 auto getAccessFromModRef = [](ModRefInfo MRI) {
460 if (isRefSet(MRI) && isModSet(MRI))
461 return AliasSet::ModRefAccess;
462 else if (isModSet(MRI))
463 return AliasSet::ModAccess;
464 else if (isRefSet(MRI))
465 return AliasSet::RefAccess;
466 else
467 return AliasSet::NoAccess;
468
469 };
470
471 ModRefInfo CallMask = createModRefInfo(AA.getModRefBehavior(CS));
472
473 // Some intrinsics are marked as modifying memory for control flow
474 // modelling purposes, but don't actually modify any specific memory
475 // location.
476 using namespace PatternMatch;
477 if (I->use_empty() && match(I, m_Intrinsic<Intrinsic::invariant_start>()))
478 CallMask = clearMod(CallMask);
479
480 for (auto AI = CS.arg_begin(), AE = CS.arg_end(); AI != AE; ++AI) {
481 const Value *Arg = *AI;
482 if (!Arg->getType()->isPointerTy())
483 continue;
484 unsigned ArgIdx = std::distance(CS.arg_begin(), AI);
485 MemoryLocation ArgLoc = MemoryLocation::getForArgument(CS, ArgIdx,
486 nullptr);
487 ModRefInfo ArgMask = AA.getArgModRefInfo(CS, ArgIdx);
488 ArgMask = intersectModRef(CallMask, ArgMask);
489 if (!isNoModRef(ArgMask))
490 addPointer(ArgLoc, getAccessFromModRef(ArgMask));
491 }
492 return;
493 }
494
Eli Friedmanae8161e2011-07-27 00:46:46 +0000495 return addUnknown(I);
Chris Lattner647df642002-09-26 21:49:07 +0000496}
497
Chris Lattnerc048bb32003-03-03 23:28:05 +0000498void AliasSetTracker::add(BasicBlock &BB) {
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000499 for (auto &I : BB)
500 add(&I);
Chris Lattnerc048bb32003-03-03 23:28:05 +0000501}
502
503void AliasSetTracker::add(const AliasSetTracker &AST) {
504 assert(&AA == &AST.AA &&
505 "Merging AliasSetTracker objects with different Alias Analyses!");
506
507 // Loop over all of the alias sets in AST, adding the pointers contained
508 // therein into the current alias sets. This can cause alias sets to be
509 // merged together in the current AST.
Benjamin Krameraa209152016-06-26 17:27:42 +0000510 for (const AliasSet &AS : AST) {
511 if (AS.Forward)
512 continue; // Ignore forwarding alias sets
Chris Lattnerc048bb32003-03-03 23:28:05 +0000513
Chris Lattnerafb70742010-08-29 04:06:55 +0000514 // If there are any call sites in the alias set, add them to this AST.
Eli Friedmanae8161e2011-07-27 00:46:46 +0000515 for (unsigned i = 0, e = AS.UnknownInsts.size(); i != e; ++i)
Sanjoy Das3f1e8e02017-03-11 01:15:48 +0000516 if (auto *Inst = AS.getUnknownInst(i))
517 add(Inst);
Chris Lattnerc048bb32003-03-03 23:28:05 +0000518
Chris Lattnerafb70742010-08-29 04:06:55 +0000519 // Loop over all of the pointers in this alias set.
Philip Reamesc3c23e82018-08-21 17:59:11 +0000520 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI)
521 addPointer(ASI.getPointer(), ASI.getSize(), ASI.getAAInfo(),
522 (AliasSet::AccessLattice)AS.Access);
Chris Lattnerafb70742010-08-29 04:06:55 +0000523 }
Chris Lattnerc048bb32003-03-03 23:28:05 +0000524}
525
Chris Lattner746e1e12004-05-23 21:10:58 +0000526// deleteValue method - This method is used to remove a pointer value from the
Chris Lattner44fea542003-12-18 08:11:56 +0000527// AliasSetTracker entirely. It should be used when an instruction is deleted
528// from the program to update the AST. If you don't use this, you would have
529// dangling pointers to deleted instructions.
530//
Chris Lattner746e1e12004-05-23 21:10:58 +0000531void AliasSetTracker::deleteValue(Value *PtrVal) {
Chris Lattnerab644812004-09-14 19:15:32 +0000532 // First, look up the PointerRec for this pointer.
Benjamin Kramere2ef47c2012-06-30 22:37:15 +0000533 PointerMapType::iterator I = PointerMap.find_as(PtrVal);
Chris Lattner44fea542003-12-18 08:11:56 +0000534 if (I == PointerMap.end()) return; // Noop
535
536 // If we found one, remove the pointer from the alias set it is in.
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000537 AliasSet::PointerRec *PtrValEnt = I->second;
538 AliasSet *AS = PtrValEnt->getAliasSet(*this);
Chris Lattner44fea542003-12-18 08:11:56 +0000539
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000540 // Unlink and delete from the list of values.
541 PtrValEnt->eraseFromList();
Michael Kuperstein41898f02016-08-19 17:05:22 +0000542
543 if (AS->Alias == AliasSet::SetMayAlias) {
544 AS->SetSize--;
545 TotalMayAliasSetSize--;
546 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000547
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000548 // Stop using the alias set.
Chris Lattner053427f2004-07-22 07:58:18 +0000549 AS->dropRef(*this);
Fangrui Songf78650a2018-07-30 19:41:25 +0000550
Chris Lattner44fea542003-12-18 08:11:56 +0000551 PointerMap.erase(I);
552}
553
Chris Lattnerab644812004-09-14 19:15:32 +0000554// copyValue - This method should be used whenever a preexisting value in the
555// program is copied or cloned, introducing a new value. Note that it is ok for
556// clients that use this method to introduce the same value multiple times: if
557// the tracker already knows about a value, it will ignore the request.
558//
559void AliasSetTracker::copyValue(Value *From, Value *To) {
Chris Lattnerab644812004-09-14 19:15:32 +0000560 // First, look up the PointerRec for this pointer.
Benjamin Kramere2ef47c2012-06-30 22:37:15 +0000561 PointerMapType::iterator I = PointerMap.find_as(From);
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000562 if (I == PointerMap.end())
Chris Lattnerab644812004-09-14 19:15:32 +0000563 return; // Noop
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000564 assert(I->second->hasAliasSet() && "Dead entry?");
Chris Lattnerab644812004-09-14 19:15:32 +0000565
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000566 AliasSet::PointerRec &Entry = getEntryFor(To);
567 if (Entry.hasAliasSet()) return; // Already in the tracker!
Chris Lattnerab644812004-09-14 19:15:32 +0000568
Xinliang David Li1ce88fa2016-08-11 23:09:56 +0000569 // getEntryFor above may invalidate iterator \c I, so reinitialize it.
Benjamin Kramere2ef47c2012-06-30 22:37:15 +0000570 I = PointerMap.find_as(From);
Xinliang David Li1ce88fa2016-08-11 23:09:56 +0000571 // Add it to the alias set it aliases...
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000572 AliasSet *AS = I->second->getAliasSet(*this);
Dan Gohman408beac2010-10-18 21:28:00 +0000573 AS->addPointer(*this, Entry, I->second->getSize(),
Hal Finkel1140e172015-10-28 22:13:41 +0000574 I->second->getAAInfo(),
Dan Gohman71af9db2010-10-18 20:44:50 +0000575 true);
Chris Lattnerab644812004-09-14 19:15:32 +0000576}
577
Michael Kuperstein41898f02016-08-19 17:05:22 +0000578AliasSet &AliasSetTracker::mergeAllAliasSets() {
579 assert(!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold) &&
580 "Full merge should happen once, when the saturation threshold is "
581 "reached");
Chris Lattnerab644812004-09-14 19:15:32 +0000582
Michael Kuperstein41898f02016-08-19 17:05:22 +0000583 // Collect all alias sets, so that we can drop references with impunity
584 // without worrying about iterator invalidation.
585 std::vector<AliasSet *> ASVector;
586 ASVector.reserve(SaturationThreshold);
587 for (iterator I = begin(), E = end(); I != E; I++)
588 ASVector.push_back(&*I);
589
590 // Copy all instructions and pointers into a new set, and forward all other
591 // sets to it.
592 AliasSets.push_back(new AliasSet());
593 AliasAnyAS = &AliasSets.back();
594 AliasAnyAS->Alias = AliasSet::SetMayAlias;
595 AliasAnyAS->Access = AliasSet::ModRefAccess;
596 AliasAnyAS->AliasAny = true;
597
598 for (auto Cur : ASVector) {
Michael Kuperstein41898f02016-08-19 17:05:22 +0000599 // If Cur was already forwarding, just forward to the new AS instead.
600 AliasSet *FwdTo = Cur->Forward;
601 if (FwdTo) {
602 Cur->Forward = AliasAnyAS;
Alina Sbirlea63d22502017-12-05 20:12:23 +0000603 AliasAnyAS->addRef();
Michael Kuperstein41898f02016-08-19 17:05:22 +0000604 FwdTo->dropRef(*this);
605 continue;
606 }
607
608 // Otherwise, perform the actual merge.
609 AliasAnyAS->mergeSetIn(*Cur, *this);
610 }
611
612 return *AliasAnyAS;
613}
614
George Burgess IV319be3a2018-05-25 21:16:58 +0000615AliasSet &AliasSetTracker::addPointer(Value *P, LocationSize Size,
Michael Kuperstein41898f02016-08-19 17:05:22 +0000616 const AAMDNodes &AAInfo,
Chad Rosier16970a82016-10-19 18:50:32 +0000617 AliasSet::AccessLattice E) {
Philip Reames0e2f9b92018-08-16 20:11:15 +0000618 AliasSet &AS = getAliasSetFor(MemoryLocation(P, Size, AAInfo));
Michael Kuperstein41898f02016-08-19 17:05:22 +0000619 AS.Access |= E;
620
621 if (!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold)) {
622 // The AST is now saturated. From here on, we conservatively consider all
623 // pointers to alias each-other.
624 return mergeAllAliasSets();
625 }
626
627 return AS;
628}
Chris Lattner44fea542003-12-18 08:11:56 +0000629
Chris Lattner7606fb62003-02-24 20:37:56 +0000630//===----------------------------------------------------------------------===//
631// AliasSet/AliasSetTracker Printing Support
632//===----------------------------------------------------------------------===//
633
Chris Lattnerb1d782b2009-08-23 05:17:37 +0000634void AliasSet::print(raw_ostream &OS) const {
Roman Divackyad06cee2012-09-05 22:26:57 +0000635 OS << " AliasSet[" << (const void*)this << ", " << RefCount << "] ";
Chandler Carrutha561d752015-06-22 02:12:52 +0000636 OS << (Alias == SetMustAlias ? "must" : "may") << " alias, ";
637 switch (Access) {
Hal Finkel1140e172015-10-28 22:13:41 +0000638 case NoAccess: OS << "No access "; break;
639 case RefAccess: OS << "Ref "; break;
640 case ModAccess: OS << "Mod "; break;
641 case ModRefAccess: OS << "Mod/Ref "; break;
Chandler Carrutha561d752015-06-22 02:12:52 +0000642 default: llvm_unreachable("Bad value for Access!");
Chris Lattner647df642002-09-26 21:49:07 +0000643 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000644 if (Forward)
645 OS << " forwarding to " << (void*)Forward;
646
Dan Gohmanc731c972007-10-03 19:26:29 +0000647 if (!empty()) {
Chris Lattner7606fb62003-02-24 20:37:56 +0000648 OS << "Pointers: ";
649 for (iterator I = begin(), E = end(); I != E; ++I) {
650 if (I != begin()) OS << ", ";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000651 I.getPointer()->printAsOperand(OS << "(");
Philip Reames96bc0762018-08-17 23:17:31 +0000652 if (I.getSize() == MemoryLocation::UnknownSize)
653 OS << ", unknown)";
654 else
655 OS << ", " << I.getSize() << ")";
Chris Lattner7606fb62003-02-24 20:37:56 +0000656 }
657 }
Eli Friedmanae8161e2011-07-27 00:46:46 +0000658 if (!UnknownInsts.empty()) {
659 OS << "\n " << UnknownInsts.size() << " Unknown instructions: ";
660 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
Chris Lattner7606fb62003-02-24 20:37:56 +0000661 if (i) OS << ", ";
Jakub Kuderski555e41b2018-06-27 16:34:30 +0000662 if (auto *I = getUnknownInst(i)) {
663 if (I->hasName())
664 I->printAsOperand(OS);
665 else
666 I->print(OS);
667 }
Misha Brukman01808ca2005-04-21 21:13:18 +0000668 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000669 }
670 OS << "\n";
671}
672
Chris Lattnerb1d782b2009-08-23 05:17:37 +0000673void AliasSetTracker::print(raw_ostream &OS) const {
Chris Lattner7606fb62003-02-24 20:37:56 +0000674 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
675 << PointerMap.size() << " pointer values.\n";
Benjamin Krameraa209152016-06-26 17:27:42 +0000676 for (const AliasSet &AS : *this)
677 AS.print(OS);
Chris Lattner7606fb62003-02-24 20:37:56 +0000678 OS << "\n";
679}
680
Aaron Ballman615eb472017-10-15 14:32:27 +0000681#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000682LLVM_DUMP_METHOD void AliasSet::dump() const { print(dbgs()); }
683LLVM_DUMP_METHOD void AliasSetTracker::dump() const { print(dbgs()); }
Manman Renc3366cc2012-09-06 19:55:56 +0000684#endif
Chris Lattner7606fb62003-02-24 20:37:56 +0000685
Chris Lattner7606fb62003-02-24 20:37:56 +0000686//===----------------------------------------------------------------------===//
Dan Gohmanf4362da2009-07-30 20:21:41 +0000687// ASTCallbackVH Class Implementation
688//===----------------------------------------------------------------------===//
689
690void AliasSetTracker::ASTCallbackVH::deleted() {
691 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
692 AST->deleteValue(getValPtr());
693 // this now dangles!
694}
695
Eli Friedman17822fc2011-04-09 06:55:46 +0000696void AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(Value *V) {
697 AST->copyValue(getValPtr(), V);
698}
699
Dan Gohmanf4362da2009-07-30 20:21:41 +0000700AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
Dan Gohmandc2b1b02009-07-31 18:21:48 +0000701 : CallbackVH(V), AST(ast) {}
702
703AliasSetTracker::ASTCallbackVH &
704AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
705 return *this = ASTCallbackVH(V, AST);
706}
Dan Gohmanf4362da2009-07-30 20:21:41 +0000707
708//===----------------------------------------------------------------------===//
Chris Lattner7606fb62003-02-24 20:37:56 +0000709// AliasSetPrinter Pass
710//===----------------------------------------------------------------------===//
711
712namespace {
Eugene Zelenko48666a62017-07-24 23:16:33 +0000713
Nick Lewycky02d5f772009-10-25 06:33:48 +0000714 class AliasSetPrinter : public FunctionPass {
Chris Lattner7606fb62003-02-24 20:37:56 +0000715 AliasSetTracker *Tracker;
Eugene Zelenko48666a62017-07-24 23:16:33 +0000716
Chris Lattner7606fb62003-02-24 20:37:56 +0000717 public:
Nick Lewyckye7da2d62007-05-06 13:37:16 +0000718 static char ID; // Pass identification, replacement for typeid
Eugene Zelenko48666a62017-07-24 23:16:33 +0000719
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000720 AliasSetPrinter() : FunctionPass(ID) {
721 initializeAliasSetPrinterPass(*PassRegistry::getPassRegistry());
722 }
Devang Patel09f162c2007-05-01 21:15:47 +0000723
Craig Toppere9ba7592014-03-05 07:30:04 +0000724 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner7606fb62003-02-24 20:37:56 +0000725 AU.setPreservesAll();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000726 AU.addRequired<AAResultsWrapperPass>();
Chris Lattner7606fb62003-02-24 20:37:56 +0000727 }
728
Craig Toppere9ba7592014-03-05 07:30:04 +0000729 bool runOnFunction(Function &F) override {
Chandler Carruth7b560d42015-09-09 17:55:00 +0000730 auto &AAWP = getAnalysis<AAResultsWrapperPass>();
731 Tracker = new AliasSetTracker(AAWP.getAAResults());
Michael Kuperstein41898f02016-08-19 17:05:22 +0000732 errs() << "Alias sets for function '" << F.getName() << "':\n";
Chris Lattner7606fb62003-02-24 20:37:56 +0000733 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner2d3a7a62004-04-27 15:13:33 +0000734 Tracker->add(&*I);
David Greene0295ecf2009-12-23 22:49:57 +0000735 Tracker->print(errs());
Chris Lattner44497852006-01-03 06:05:22 +0000736 delete Tracker;
Chris Lattner7606fb62003-02-24 20:37:56 +0000737 return false;
738 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000739 };
Eugene Zelenko48666a62017-07-24 23:16:33 +0000740
741} // end anonymous namespace
Dan Gohmand78c4002008-05-13 00:00:25 +0000742
743char AliasSetPrinter::ID = 0;
Eugene Zelenko48666a62017-07-24 23:16:33 +0000744
Owen Anderson8ac477f2010-10-12 19:48:12 +0000745INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets",
746 "Alias Set Printer", false, true)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000747INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Owen Anderson8ac477f2010-10-12 19:48:12 +0000748INITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000749 "Alias Set Printer", false, true)