blob: a6e5b9fab558d09b6fd3d3da0530443e72ca3c5f [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Misha Brukman01808ca2005-04-21 21:13:18 +00006//
John Criswell482202a2003-10-20 19:43:21 +00007//===----------------------------------------------------------------------===//
Chris Lattner647df642002-09-26 21:49:07 +00008//
9// This file implements the AliasSetTracker and AliasSet classes.
Misha Brukman01808ca2005-04-21 21:13:18 +000010//
Chris Lattner647df642002-09-26 21:49:07 +000011//===----------------------------------------------------------------------===//
12
13#include "llvm/Analysis/AliasSetTracker.h"
14#include "llvm/Analysis/AliasAnalysis.h"
Max Kazantsev3c284bd2018-08-30 03:39:16 +000015#include "llvm/Analysis/GuardUtils.h"
Alina Sbirlea6cba96e2019-02-06 20:25:17 +000016#include "llvm/Analysis/LoopInfo.h"
Eugene Zelenko48666a62017-07-24 23:16:33 +000017#include "llvm/Analysis/MemoryLocation.h"
Alina Sbirlea6cba96e2019-02-06 20:25:17 +000018#include "llvm/Analysis/MemorySSA.h"
Nico Weber432a3882018-04-30 14:59:11 +000019#include "llvm/Config/llvm-config.h"
Eugene Zelenko48666a62017-07-24 23:16:33 +000020#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;
Alina Sbirleafd9722fb2018-11-01 23:37:51 +0000117 } else // Update TotalMayAliasSetSize only if not forwarding.
118 if (AS->Alias == AliasSet::SetMayAlias)
119 TotalMayAliasSetSize -= AS->size();
Michael Kuperstein41898f02016-08-19 17:05:22 +0000120
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,
Alina Sbirlea910c6be2019-02-06 19:55:12 +0000131 bool KnownMustAlias, bool SkipSizeUpdate) {
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.
Alina Sbirlea910c6be2019-02-06 19:55:12 +0000135 if (isMustAlias())
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000136 if (PointerRec *P = getSomePointer()) {
Alina Sbirlea910c6be2019-02-06 19:55:12 +0000137 if (!KnownMustAlias) {
138 AliasAnalysis &AA = AST.getAliasAnalysis();
139 AliasResult Result = AA.alias(
140 MemoryLocation(P->getValue(), P->getSize(), P->getAAInfo()),
141 MemoryLocation(Entry.getValue(), Size, AAInfo));
142 if (Result != MustAlias) {
143 Alias = SetMayAlias;
144 AST.TotalMayAliasSetSize += size();
145 }
146 assert(Result != NoAlias && "Cannot be part of must set!");
147 } else if (!SkipSizeUpdate)
Hal Finkelcc39b672014-07-24 12:16:19 +0000148 P->updateSizeAndAAInfo(Size, AAInfo);
Chris Lattnerb5b0b7a2003-02-26 22:11:00 +0000149 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000150
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000151 Entry.setAliasSet(this);
Hal Finkelcc39b672014-07-24 12:16:19 +0000152 Entry.updateSizeAndAAInfo(Size, AAInfo);
Chris Lattner7606fb62003-02-24 20:37:56 +0000153
154 // Add it to the end of the list...
Michael Kuperstein41898f02016-08-19 17:05:22 +0000155 ++SetSize;
Craig Topper9f008862014-04-15 04:59:12 +0000156 assert(*PtrListEnd == nullptr && "End of list is not null?");
Chris Lattner44fea542003-12-18 08:11:56 +0000157 *PtrListEnd = &Entry;
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000158 PtrListEnd = Entry.setPrevInList(PtrListEnd);
Craig Topper9f008862014-04-15 04:59:12 +0000159 assert(*PtrListEnd == nullptr && "End of list is not null?");
Michael Kuperstein41898f02016-08-19 17:05:22 +0000160 // Entry points to alias set.
161 addRef();
162
163 if (Alias == SetMayAlias)
164 AST.TotalMayAliasSetSize++;
Chris Lattner7606fb62003-02-24 20:37:56 +0000165}
166
Hal Finkel1140e172015-10-28 22:13:41 +0000167void AliasSet::addUnknownInst(Instruction *I, AliasAnalysis &AA) {
David Majnemerb7adf342014-11-19 09:41:05 +0000168 if (UnknownInsts.empty())
169 addRef();
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000170 UnknownInsts.emplace_back(I);
Chris Lattner21c60f12004-03-15 04:08:36 +0000171
Max Kazantsev5a10d122018-08-15 06:21:02 +0000172 // Guards are marked as modifying memory for control flow modelling purposes,
173 // but don't actually modify any specific memory location.
174 using namespace PatternMatch;
Max Kazantsev3c284bd2018-08-30 03:39:16 +0000175 bool MayWriteMemory = I->mayWriteToMemory() && !isGuard(I) &&
Philip Reamesa5a85462018-08-21 00:55:35 +0000176 !(I->use_empty() && match(I, m_Intrinsic<Intrinsic::invariant_start>()));
Max Kazantsev5a10d122018-08-15 06:21:02 +0000177 if (!MayWriteMemory) {
Chandler Carrutha561d752015-06-22 02:12:52 +0000178 Alias = SetMayAlias;
Hal Finkel1140e172015-10-28 22:13:41 +0000179 Access |= RefAccess;
Duncan Sands68b6f502007-12-01 07:51:45 +0000180 return;
Chris Lattner21c60f12004-03-15 04:08:36 +0000181 }
182
Hal Finkel1140e172015-10-28 22:13:41 +0000183 // FIXME: This should use mod/ref information to make this not suck so bad
Chandler Carrutha561d752015-06-22 02:12:52 +0000184 Alias = SetMayAlias;
Hal Finkel1140e172015-10-28 22:13:41 +0000185 Access = ModRefAccess;
Chris Lattner7606fb62003-02-24 20:37:56 +0000186}
187
Alina Sbirlea3d1d95c2019-01-28 18:30:05 +0000188/// aliasesPointer - If the specified pointer "may" (or must) alias one of the
189/// members in the set return the appropriate AliasResult. Otherwise return
190/// NoAlias.
Chris Lattner647df642002-09-26 21:49:07 +0000191///
Alina Sbirlea3d1d95c2019-01-28 18:30:05 +0000192AliasResult AliasSet::aliasesPointer(const Value *Ptr, LocationSize Size,
193 const AAMDNodes &AAInfo,
194 AliasAnalysis &AA) const {
Michael Kuperstein41898f02016-08-19 17:05:22 +0000195 if (AliasAny)
Alina Sbirlea3d1d95c2019-01-28 18:30:05 +0000196 return MayAlias;
Michael Kuperstein41898f02016-08-19 17:05:22 +0000197
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)
Alina Sbirlea3d1d95c2019-01-28 18:30:05 +0000213 if (AliasResult AR = AA.alias(
214 MemoryLocation(Ptr, Size, AAInfo),
215 MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo())))
216 return AR;
Chris Lattner7606fb62003-02-24 20:37:56 +0000217
Eli Friedmanae8161e2011-07-27 00:46:46 +0000218 // Check the unknown instructions...
219 if (!UnknownInsts.empty()) {
220 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i)
Sanjoy Das3f1e8e02017-03-11 01:15:48 +0000221 if (auto *Inst = getUnknownInst(i))
Alina Sbirlea63d22502017-12-05 20:12:23 +0000222 if (isModOrRefSet(
223 AA.getModRefInfo(Inst, MemoryLocation(Ptr, Size, AAInfo))))
Alina Sbirlea3d1d95c2019-01-28 18:30:05 +0000224 return MayAlias;
Chris Lattner9b323c32004-07-27 02:20:26 +0000225 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000226
Alina Sbirlea3d1d95c2019-01-28 18:30:05 +0000227 return NoAlias;
Chris Lattner647df642002-09-26 21:49:07 +0000228}
229
Pete Cooper4bf388d2015-05-13 01:12:12 +0000230bool AliasSet::aliasesUnknownInst(const Instruction *Inst,
Hal Finkel1140e172015-10-28 22:13:41 +0000231 AliasAnalysis &AA) const {
Michael Kuperstein41898f02016-08-19 17:05:22 +0000232
233 if (AliasAny)
234 return true;
235
Alina Sbirleafd9722fb2018-11-01 23:37:51 +0000236 assert(Inst->mayReadOrWriteMemory() &&
237 "Instruction must either read or write memory.");
Chris Lattner21c60f12004-03-15 04:08:36 +0000238
Eli Friedmanae8161e2011-07-27 00:46:46 +0000239 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
Xin Tong70f75122017-06-25 12:55:11 +0000240 if (auto *UnknownInst = getUnknownInst(i)) {
Chandler Carruth363ac682019-01-07 05:42:51 +0000241 const auto *C1 = dyn_cast<CallBase>(UnknownInst);
242 const auto *C2 = dyn_cast<CallBase>(Inst);
Alina Sbirlea63d22502017-12-05 20:12:23 +0000243 if (!C1 || !C2 || isModOrRefSet(AA.getModRefInfo(C1, C2)) ||
244 isModOrRefSet(AA.getModRefInfo(C2, C1)))
Sanjoy Das3f1e8e02017-03-11 01:15:48 +0000245 return true;
246 }
Chris Lattnerf58382e2010-08-29 18:42:23 +0000247 }
Chris Lattner9b323c32004-07-27 02:20:26 +0000248
Hal Finkel1140e172015-10-28 22:13:41 +0000249 for (iterator I = begin(), E = end(); I != E; ++I)
Alina Sbirlea63d22502017-12-05 20:12:23 +0000250 if (isModOrRefSet(AA.getModRefInfo(
251 Inst, MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo()))))
Hal Finkel1140e172015-10-28 22:13:41 +0000252 return true;
253
254 return false;
Chris Lattner647df642002-09-26 21:49:07 +0000255}
256
Philip Reames825c74c2018-08-22 03:32:52 +0000257Instruction* AliasSet::getUniqueInstruction() {
Philip Reames825c74c2018-08-22 03:32:52 +0000258 if (AliasAny)
259 // May have collapses alias set
260 return nullptr;
Philip Reamesf562fc82018-08-29 21:49:30 +0000261 if (begin() != end()) {
262 if (!UnknownInsts.empty())
263 // Another instruction found
264 return nullptr;
265 if (std::next(begin()) != end())
266 // Another instruction found
267 return nullptr;
268 Value *Addr = begin()->getValue();
269 assert(!Addr->user_empty() &&
270 "where's the instruction which added this pointer?");
271 if (std::next(Addr->user_begin()) != Addr->user_end())
272 // Another instruction found -- this is really restrictive
273 // TODO: generalize!
274 return nullptr;
275 return cast<Instruction>(*(Addr->user_begin()));
276 }
Philip Reamesfdd73b52018-08-22 03:36:42 +0000277 if (1 != UnknownInsts.size())
Philip Reames825c74c2018-08-22 03:32:52 +0000278 return nullptr;
279 return cast<Instruction>(UnknownInsts[0]);
280}
281
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000282void AliasSetTracker::clear() {
283 // Delete all the PointerRec entries.
Dan Gohmanf4362da2009-07-30 20:21:41 +0000284 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
285 I != E; ++I)
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000286 I->second->eraseFromList();
Fangrui Songf78650a2018-07-30 19:41:25 +0000287
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000288 PointerMap.clear();
Fangrui Songf78650a2018-07-30 19:41:25 +0000289
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000290 // The alias sets should all be clear now.
291 AliasSets.clear();
292}
293
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000294/// mergeAliasSetsForPointer - Given a pointer, merge all alias sets that may
295/// alias the pointer. Return the unified set, or nullptr if no set that aliases
Alina Sbirlea910c6be2019-02-06 19:55:12 +0000296/// the pointer was found. MustAliasAll is updated to true/false if the pointer
297/// is found to MustAlias all the sets it merged.
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000298AliasSet *AliasSetTracker::mergeAliasSetsForPointer(const Value *Ptr,
George Burgess IV319be3a2018-05-25 21:16:58 +0000299 LocationSize Size,
Alina Sbirlea910c6be2019-02-06 19:55:12 +0000300 const AAMDNodes &AAInfo,
301 bool &MustAliasAll) {
Craig Topper9f008862014-04-15 04:59:12 +0000302 AliasSet *FoundSet = nullptr;
Alina Sbirlea910c6be2019-02-06 19:55:12 +0000303 AliasResult AllAR = MustAlias;
David Majnemerb7adf342014-11-19 09:41:05 +0000304 for (iterator I = begin(), E = end(); I != E;) {
305 iterator Cur = I++;
Alina Sbirlea910c6be2019-02-06 19:55:12 +0000306 if (Cur->Forward)
307 continue;
308
309 AliasResult AR = Cur->aliasesPointer(Ptr, Size, AAInfo, AA);
310 if (AR == NoAlias)
311 continue;
312
313 AllAR =
314 AliasResult(AllAR & AR); // Possible downgrade to May/Partial, even No
Fangrui Songf78650a2018-07-30 19:41:25 +0000315
Alina Sbirlea8e1d65772019-01-28 19:38:03 +0000316 if (!FoundSet) {
317 // If this is the first alias set ptr can go into, remember it.
318 FoundSet = &*Cur;
319 } else {
320 // Otherwise, we must merge the sets.
321 FoundSet->mergeSetIn(*Cur, *this);
Chris Lattner647df642002-09-26 21:49:07 +0000322 }
Chris Lattnerafb70742010-08-29 04:06:55 +0000323 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000324
Alina Sbirlea910c6be2019-02-06 19:55:12 +0000325 MustAliasAll = (AllAR == MustAlias);
Chris Lattner7606fb62003-02-24 20:37:56 +0000326 return FoundSet;
327}
328
Hal Finkel1140e172015-10-28 22:13:41 +0000329AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) {
Craig Topper9f008862014-04-15 04:59:12 +0000330 AliasSet *FoundSet = nullptr;
David Majnemerb7adf342014-11-19 09:41:05 +0000331 for (iterator I = begin(), E = end(); I != E;) {
332 iterator Cur = I++;
Hal Finkel1140e172015-10-28 22:13:41 +0000333 if (Cur->Forward || !Cur->aliasesUnknownInst(Inst, AA))
Chris Lattnerafb70742010-08-29 04:06:55 +0000334 continue;
Alina Sbirlead8c829b2019-01-28 19:01:32 +0000335 if (!FoundSet) {
336 // If this is the first alias set ptr can go into, remember it.
337 FoundSet = &*Cur;
338 } else {
339 // Otherwise, we must merge the sets.
340 FoundSet->mergeSetIn(*Cur, *this);
341 }
Chris Lattnerafb70742010-08-29 04:06:55 +0000342 }
Chris Lattner647df642002-09-26 21:49:07 +0000343 return FoundSet;
344}
345
Philip Reames0e2f9b92018-08-16 20:11:15 +0000346AliasSet &AliasSetTracker::getAliasSetFor(const MemoryLocation &MemLoc) {
347
348 Value * const Pointer = const_cast<Value*>(MemLoc.Ptr);
349 const LocationSize Size = MemLoc.Size;
350 const AAMDNodes &AAInfo = MemLoc.AATags;
Alina Sbirlead8c829b2019-01-28 19:01:32 +0000351
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000352 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
Chris Lattner7606fb62003-02-24 20:37:56 +0000353
Michael Kuperstein41898f02016-08-19 17:05:22 +0000354 if (AliasAnyAS) {
355 // At this point, the AST is saturated, so we only have one active alias
356 // set. That means we already know which alias set we want to return, and
357 // just need to add the pointer to that set to keep the data structure
358 // consistent.
359 // This, of course, means that we will never need a merge here.
360 if (Entry.hasAliasSet()) {
361 Entry.updateSizeAndAAInfo(Size, AAInfo);
362 assert(Entry.getAliasSet(*this) == AliasAnyAS &&
363 "Entry in saturated AST must belong to only alias set");
364 } else {
365 AliasAnyAS->addPointer(*this, Entry, Size, AAInfo);
366 }
367 return *AliasAnyAS;
368 }
369
Alina Sbirlea910c6be2019-02-06 19:55:12 +0000370 bool MustAliasAll = false;
Chris Lattnereef6b192010-08-29 04:13:43 +0000371 // Check to see if the pointer is already known.
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000372 if (Entry.hasAliasSet()) {
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000373 // If the size changed, we may need to merge several alias sets.
374 // Note that we can *not* return the result of mergeAliasSetsForPointer
375 // due to a quirk of alias analysis behavior. Since alias(undef, undef)
376 // is NoAlias, mergeAliasSetsForPointer(undef, ...) will not find the
377 // the right set for undef, even if it exists.
378 if (Entry.updateSizeAndAAInfo(Size, AAInfo))
Alina Sbirlea910c6be2019-02-06 19:55:12 +0000379 mergeAliasSetsForPointer(Pointer, Size, AAInfo, MustAliasAll);
Chris Lattner7606fb62003-02-24 20:37:56 +0000380 // Return the set!
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000381 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattnerafb70742010-08-29 04:06:55 +0000382 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000383
Alina Sbirlea910c6be2019-02-06 19:55:12 +0000384 if (AliasSet *AS =
385 mergeAliasSetsForPointer(Pointer, Size, AAInfo, MustAliasAll)) {
Chris Lattnereef6b192010-08-29 04:13:43 +0000386 // Add it to the alias set it aliases.
Alina Sbirlea910c6be2019-02-06 19:55:12 +0000387 AS->addPointer(*this, Entry, Size, AAInfo, MustAliasAll);
Chris Lattner7606fb62003-02-24 20:37:56 +0000388 return *AS;
Chris Lattner647df642002-09-26 21:49:07 +0000389 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000390
Chris Lattnereef6b192010-08-29 04:13:43 +0000391 // Otherwise create a new alias set to hold the loaded pointer.
Chris Lattnerafb70742010-08-29 04:06:55 +0000392 AliasSets.push_back(new AliasSet());
Alina Sbirlea910c6be2019-02-06 19:55:12 +0000393 AliasSets.back().addPointer(*this, Entry, Size, AAInfo, true);
Chris Lattnerafb70742010-08-29 04:06:55 +0000394 return AliasSets.back();
Chris Lattner647df642002-09-26 21:49:07 +0000395}
396
George Burgess IV319be3a2018-05-25 21:16:58 +0000397void AliasSetTracker::add(Value *Ptr, LocationSize Size,
398 const AAMDNodes &AAInfo) {
Alina Sbirlea4f5d3372018-10-29 22:25:59 +0000399 addPointer(MemoryLocation(Ptr, Size, AAInfo), AliasSet::NoAccess);
Chris Lattnerbf8c3c42004-07-26 05:50:23 +0000400}
401
Chad Rosier16970a82016-10-19 18:50:32 +0000402void AliasSetTracker::add(LoadInst *LI) {
Philip Reamesf8681ce2018-08-22 19:30:46 +0000403 if (isStrongerThanMonotonic(LI->getOrdering()))
404 return addUnknown(LI);
Philip Reamesc3c23e82018-08-21 17:59:11 +0000405 addPointer(MemoryLocation::get(LI), AliasSet::RefAccess);
Chris Lattner7606fb62003-02-24 20:37:56 +0000406}
407
Chad Rosier16970a82016-10-19 18:50:32 +0000408void AliasSetTracker::add(StoreInst *SI) {
Philip Reamesf8681ce2018-08-22 19:30:46 +0000409 if (isStrongerThanMonotonic(SI->getOrdering()))
410 return addUnknown(SI);
Philip Reamesc3c23e82018-08-21 17:59:11 +0000411 addPointer(MemoryLocation::get(SI), AliasSet::ModAccess);
Chris Lattner647df642002-09-26 21:49:07 +0000412}
413
Chad Rosier16970a82016-10-19 18:50:32 +0000414void AliasSetTracker::add(VAArgInst *VAAI) {
Philip Reames90bffb32018-08-13 22:34:14 +0000415 addPointer(MemoryLocation::get(VAAI), AliasSet::ModRefAccess);
Dan Gohman2cd8e382008-04-14 18:34:50 +0000416}
417
Daniel Neilson6b23fb72018-05-30 14:43:39 +0000418void AliasSetTracker::add(AnyMemSetInst *MSI) {
Philip Reamesf8681ce2018-08-22 19:30:46 +0000419 addPointer(MemoryLocation::getForDest(MSI), AliasSet::ModAccess);
Haicheng Wu5cf99092016-02-17 02:01:50 +0000420}
Chris Lattner0a140602003-12-14 04:52:11 +0000421
Daniel Neilson6b23fb72018-05-30 14:43:39 +0000422void AliasSetTracker::add(AnyMemTransferInst *MTI) {
Philip Reamesf8681ce2018-08-22 19:30:46 +0000423 addPointer(MemoryLocation::getForDest(MTI), AliasSet::ModAccess);
Philip Reames5660bd42018-09-10 16:00:27 +0000424 addPointer(MemoryLocation::getForSource(MTI), AliasSet::RefAccess);
Chad Rosier6e3a92e2016-10-19 19:09:03 +0000425}
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:
Dan Gohman2c74fe92017-11-08 21:59:51 +0000439 case Intrinsic::sideeffect:
Chad Rosier8f348012016-11-07 14:11:45 +0000440 return;
441 }
442 }
Eli Friedmanae8161e2011-07-27 00:46:46 +0000443 if (!Inst->mayReadOrWriteMemory())
Chad Rosier16970a82016-10-19 18:50:32 +0000444 return; // doesn't alias anything
Chris Lattner7f04ebc2004-03-15 06:28:07 +0000445
Philip Reames00ae46b2019-02-06 03:46:40 +0000446 if (AliasSet *AS = findAliasSetForUnknownInst(Inst)) {
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());
Philip Reames00ae46b2019-02-06 03:46:40 +0000451 AliasSets.back().addUnknownInst(Inst, AA);
Chris Lattner647df642002-09-26 21:49:07 +0000452}
453
Chad Rosier16970a82016-10-19 18:50:32 +0000454void AliasSetTracker::add(Instruction *I) {
Chris Lattnerafb70742010-08-29 04:06:55 +0000455 // Dispatch to one of the other add methods.
Chris Lattner7606fb62003-02-24 20:37:56 +0000456 if (LoadInst *LI = dyn_cast<LoadInst>(I))
Chris Lattner2cfaef22004-07-21 05:18:04 +0000457 return add(LI);
Chris Lattnerafb70742010-08-29 04:06:55 +0000458 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner2cfaef22004-07-21 05:18:04 +0000459 return add(SI);
Chris Lattnerafb70742010-08-29 04:06:55 +0000460 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
Dan Gohman2cd8e382008-04-14 18:34:50 +0000461 return add(VAAI);
Daniel Neilson6b23fb72018-05-30 14:43:39 +0000462 if (AnyMemSetInst *MSI = dyn_cast<AnyMemSetInst>(I))
Haicheng Wu5cf99092016-02-17 02:01:50 +0000463 return add(MSI);
Daniel Neilson6b23fb72018-05-30 14:43:39 +0000464 if (AnyMemTransferInst *MTI = dyn_cast<AnyMemTransferInst>(I))
Chad Rosier6e3a92e2016-10-19 19:09:03 +0000465 return add(MTI);
Philip Reamescb8b3272018-09-07 21:36:11 +0000466
467 // Handle all calls with known mod/ref sets genericall
Chandler Carruth363ac682019-01-07 05:42:51 +0000468 if (auto *Call = dyn_cast<CallBase>(I))
469 if (Call->onlyAccessesArgMemory()) {
470 auto getAccessFromModRef = [](ModRefInfo MRI) {
471 if (isRefSet(MRI) && isModSet(MRI))
472 return AliasSet::ModRefAccess;
473 else if (isModSet(MRI))
474 return AliasSet::ModAccess;
475 else if (isRefSet(MRI))
476 return AliasSet::RefAccess;
477 else
478 return AliasSet::NoAccess;
479 };
Philip Reamescb8b3272018-09-07 21:36:11 +0000480
Chandler Carruth363ac682019-01-07 05:42:51 +0000481 ModRefInfo CallMask = createModRefInfo(AA.getModRefBehavior(Call));
Philip Reamescb8b3272018-09-07 21:36:11 +0000482
Chandler Carruth363ac682019-01-07 05:42:51 +0000483 // Some intrinsics are marked as modifying memory for control flow
484 // modelling purposes, but don't actually modify any specific memory
485 // location.
486 using namespace PatternMatch;
487 if (Call->use_empty() &&
488 match(Call, m_Intrinsic<Intrinsic::invariant_start>()))
489 CallMask = clearMod(CallMask);
490
491 for (auto IdxArgPair : enumerate(Call->args())) {
492 int ArgIdx = IdxArgPair.index();
493 const Value *Arg = IdxArgPair.value();
494 if (!Arg->getType()->isPointerTy())
495 continue;
496 MemoryLocation ArgLoc =
497 MemoryLocation::getForArgument(Call, ArgIdx, nullptr);
498 ModRefInfo ArgMask = AA.getArgModRefInfo(Call, ArgIdx);
499 ArgMask = intersectModRef(CallMask, ArgMask);
500 if (!isNoModRef(ArgMask))
501 addPointer(ArgLoc, getAccessFromModRef(ArgMask));
502 }
503 return;
Philip Reamescb8b3272018-09-07 21:36:11 +0000504 }
Chandler Carruth363ac682019-01-07 05:42:51 +0000505
Eli Friedmanae8161e2011-07-27 00:46:46 +0000506 return addUnknown(I);
Chris Lattner647df642002-09-26 21:49:07 +0000507}
508
Chris Lattnerc048bb32003-03-03 23:28:05 +0000509void AliasSetTracker::add(BasicBlock &BB) {
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000510 for (auto &I : BB)
511 add(&I);
Chris Lattnerc048bb32003-03-03 23:28:05 +0000512}
513
514void AliasSetTracker::add(const AliasSetTracker &AST) {
515 assert(&AA == &AST.AA &&
516 "Merging AliasSetTracker objects with different Alias Analyses!");
517
518 // Loop over all of the alias sets in AST, adding the pointers contained
519 // therein into the current alias sets. This can cause alias sets to be
520 // merged together in the current AST.
Benjamin Krameraa209152016-06-26 17:27:42 +0000521 for (const AliasSet &AS : AST) {
522 if (AS.Forward)
523 continue; // Ignore forwarding alias sets
Chris Lattnerc048bb32003-03-03 23:28:05 +0000524
Chris Lattnerafb70742010-08-29 04:06:55 +0000525 // If there are any call sites in the alias set, add them to this AST.
Eli Friedmanae8161e2011-07-27 00:46:46 +0000526 for (unsigned i = 0, e = AS.UnknownInsts.size(); i != e; ++i)
Sanjoy Das3f1e8e02017-03-11 01:15:48 +0000527 if (auto *Inst = AS.getUnknownInst(i))
528 add(Inst);
Chris Lattnerc048bb32003-03-03 23:28:05 +0000529
Chris Lattnerafb70742010-08-29 04:06:55 +0000530 // Loop over all of the pointers in this alias set.
Philip Reamesc3c23e82018-08-21 17:59:11 +0000531 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI)
Alina Sbirlea4f5d3372018-10-29 22:25:59 +0000532 addPointer(
533 MemoryLocation(ASI.getPointer(), ASI.getSize(), ASI.getAAInfo()),
534 (AliasSet::AccessLattice)AS.Access);
Chris Lattnerafb70742010-08-29 04:06:55 +0000535 }
Chris Lattnerc048bb32003-03-03 23:28:05 +0000536}
537
Alina Sbirlea6cba96e2019-02-06 20:25:17 +0000538void AliasSetTracker::addAllInstructionsInLoopUsingMSSA() {
539 assert(MSSA && L && "MSSA and L must be available");
540 for (const BasicBlock *BB : L->blocks())
541 if (auto *Accesses = MSSA->getBlockAccesses(BB))
542 for (auto &Access : *Accesses)
543 if (auto *MUD = dyn_cast<MemoryUseOrDef>(&Access))
544 add(MUD->getMemoryInst());
545}
546
Chris Lattner746e1e12004-05-23 21:10:58 +0000547// deleteValue method - This method is used to remove a pointer value from the
Chris Lattner44fea542003-12-18 08:11:56 +0000548// AliasSetTracker entirely. It should be used when an instruction is deleted
549// from the program to update the AST. If you don't use this, you would have
550// dangling pointers to deleted instructions.
551//
Chris Lattner746e1e12004-05-23 21:10:58 +0000552void AliasSetTracker::deleteValue(Value *PtrVal) {
Chris Lattnerab644812004-09-14 19:15:32 +0000553 // First, look up the PointerRec for this pointer.
Benjamin Kramere2ef47c2012-06-30 22:37:15 +0000554 PointerMapType::iterator I = PointerMap.find_as(PtrVal);
Chris Lattner44fea542003-12-18 08:11:56 +0000555 if (I == PointerMap.end()) return; // Noop
556
557 // If we found one, remove the pointer from the alias set it is in.
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000558 AliasSet::PointerRec *PtrValEnt = I->second;
559 AliasSet *AS = PtrValEnt->getAliasSet(*this);
Chris Lattner44fea542003-12-18 08:11:56 +0000560
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000561 // Unlink and delete from the list of values.
562 PtrValEnt->eraseFromList();
Michael Kuperstein41898f02016-08-19 17:05:22 +0000563
564 if (AS->Alias == AliasSet::SetMayAlias) {
565 AS->SetSize--;
566 TotalMayAliasSetSize--;
567 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000568
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000569 // Stop using the alias set.
Chris Lattner053427f2004-07-22 07:58:18 +0000570 AS->dropRef(*this);
Fangrui Songf78650a2018-07-30 19:41:25 +0000571
Chris Lattner44fea542003-12-18 08:11:56 +0000572 PointerMap.erase(I);
573}
574
Chris Lattnerab644812004-09-14 19:15:32 +0000575// copyValue - This method should be used whenever a preexisting value in the
576// program is copied or cloned, introducing a new value. Note that it is ok for
577// clients that use this method to introduce the same value multiple times: if
578// the tracker already knows about a value, it will ignore the request.
579//
580void AliasSetTracker::copyValue(Value *From, Value *To) {
Chris Lattnerab644812004-09-14 19:15:32 +0000581 // First, look up the PointerRec for this pointer.
Benjamin Kramere2ef47c2012-06-30 22:37:15 +0000582 PointerMapType::iterator I = PointerMap.find_as(From);
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000583 if (I == PointerMap.end())
Chris Lattnerab644812004-09-14 19:15:32 +0000584 return; // Noop
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000585 assert(I->second->hasAliasSet() && "Dead entry?");
Chris Lattnerab644812004-09-14 19:15:32 +0000586
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000587 AliasSet::PointerRec &Entry = getEntryFor(To);
588 if (Entry.hasAliasSet()) return; // Already in the tracker!
Chris Lattnerab644812004-09-14 19:15:32 +0000589
Xinliang David Li1ce88fa2016-08-11 23:09:56 +0000590 // getEntryFor above may invalidate iterator \c I, so reinitialize it.
Benjamin Kramere2ef47c2012-06-30 22:37:15 +0000591 I = PointerMap.find_as(From);
Xinliang David Li1ce88fa2016-08-11 23:09:56 +0000592 // Add it to the alias set it aliases...
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000593 AliasSet *AS = I->second->getAliasSet(*this);
Alina Sbirlea910c6be2019-02-06 19:55:12 +0000594 AS->addPointer(*this, Entry, I->second->getSize(), I->second->getAAInfo(),
595 true, true);
Chris Lattnerab644812004-09-14 19:15:32 +0000596}
597
Michael Kuperstein41898f02016-08-19 17:05:22 +0000598AliasSet &AliasSetTracker::mergeAllAliasSets() {
599 assert(!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold) &&
600 "Full merge should happen once, when the saturation threshold is "
601 "reached");
Chris Lattnerab644812004-09-14 19:15:32 +0000602
Michael Kuperstein41898f02016-08-19 17:05:22 +0000603 // Collect all alias sets, so that we can drop references with impunity
604 // without worrying about iterator invalidation.
605 std::vector<AliasSet *> ASVector;
606 ASVector.reserve(SaturationThreshold);
607 for (iterator I = begin(), E = end(); I != E; I++)
608 ASVector.push_back(&*I);
609
610 // Copy all instructions and pointers into a new set, and forward all other
611 // sets to it.
612 AliasSets.push_back(new AliasSet());
613 AliasAnyAS = &AliasSets.back();
614 AliasAnyAS->Alias = AliasSet::SetMayAlias;
615 AliasAnyAS->Access = AliasSet::ModRefAccess;
616 AliasAnyAS->AliasAny = true;
617
618 for (auto Cur : ASVector) {
Michael Kuperstein41898f02016-08-19 17:05:22 +0000619 // If Cur was already forwarding, just forward to the new AS instead.
620 AliasSet *FwdTo = Cur->Forward;
621 if (FwdTo) {
622 Cur->Forward = AliasAnyAS;
Alina Sbirlea63d22502017-12-05 20:12:23 +0000623 AliasAnyAS->addRef();
Michael Kuperstein41898f02016-08-19 17:05:22 +0000624 FwdTo->dropRef(*this);
625 continue;
626 }
627
628 // Otherwise, perform the actual merge.
629 AliasAnyAS->mergeSetIn(*Cur, *this);
630 }
631
632 return *AliasAnyAS;
633}
634
Alina Sbirlea4f5d3372018-10-29 22:25:59 +0000635AliasSet &AliasSetTracker::addPointer(MemoryLocation Loc,
Chad Rosier16970a82016-10-19 18:50:32 +0000636 AliasSet::AccessLattice E) {
Alina Sbirlea4f5d3372018-10-29 22:25:59 +0000637 AliasSet &AS = getAliasSetFor(Loc);
Michael Kuperstein41898f02016-08-19 17:05:22 +0000638 AS.Access |= E;
639
640 if (!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold)) {
641 // The AST is now saturated. From here on, we conservatively consider all
642 // pointers to alias each-other.
643 return mergeAllAliasSets();
644 }
645
646 return AS;
647}
Chris Lattner44fea542003-12-18 08:11:56 +0000648
Chris Lattner7606fb62003-02-24 20:37:56 +0000649//===----------------------------------------------------------------------===//
650// AliasSet/AliasSetTracker Printing Support
651//===----------------------------------------------------------------------===//
652
Chris Lattnerb1d782b2009-08-23 05:17:37 +0000653void AliasSet::print(raw_ostream &OS) const {
Roman Divackyad06cee2012-09-05 22:26:57 +0000654 OS << " AliasSet[" << (const void*)this << ", " << RefCount << "] ";
Chandler Carrutha561d752015-06-22 02:12:52 +0000655 OS << (Alias == SetMustAlias ? "must" : "may") << " alias, ";
656 switch (Access) {
Hal Finkel1140e172015-10-28 22:13:41 +0000657 case NoAccess: OS << "No access "; break;
658 case RefAccess: OS << "Ref "; break;
659 case ModAccess: OS << "Mod "; break;
660 case ModRefAccess: OS << "Mod/Ref "; break;
Chandler Carrutha561d752015-06-22 02:12:52 +0000661 default: llvm_unreachable("Bad value for Access!");
Chris Lattner647df642002-09-26 21:49:07 +0000662 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000663 if (Forward)
664 OS << " forwarding to " << (void*)Forward;
665
Dan Gohmanc731c972007-10-03 19:26:29 +0000666 if (!empty()) {
Chris Lattner7606fb62003-02-24 20:37:56 +0000667 OS << "Pointers: ";
668 for (iterator I = begin(), E = end(); I != E; ++I) {
669 if (I != begin()) OS << ", ";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000670 I.getPointer()->printAsOperand(OS << "(");
George Burgess IV6ef80022018-10-10 21:28:44 +0000671 if (I.getSize() == LocationSize::unknown())
Philip Reames96bc0762018-08-17 23:17:31 +0000672 OS << ", unknown)";
673 else
674 OS << ", " << I.getSize() << ")";
Chris Lattner7606fb62003-02-24 20:37:56 +0000675 }
676 }
Eli Friedmanae8161e2011-07-27 00:46:46 +0000677 if (!UnknownInsts.empty()) {
678 OS << "\n " << UnknownInsts.size() << " Unknown instructions: ";
679 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
Chris Lattner7606fb62003-02-24 20:37:56 +0000680 if (i) OS << ", ";
Jakub Kuderski555e41b2018-06-27 16:34:30 +0000681 if (auto *I = getUnknownInst(i)) {
682 if (I->hasName())
683 I->printAsOperand(OS);
684 else
685 I->print(OS);
686 }
Misha Brukman01808ca2005-04-21 21:13:18 +0000687 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000688 }
689 OS << "\n";
690}
691
Chris Lattnerb1d782b2009-08-23 05:17:37 +0000692void AliasSetTracker::print(raw_ostream &OS) const {
Chris Lattner7606fb62003-02-24 20:37:56 +0000693 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
694 << PointerMap.size() << " pointer values.\n";
Benjamin Krameraa209152016-06-26 17:27:42 +0000695 for (const AliasSet &AS : *this)
696 AS.print(OS);
Chris Lattner7606fb62003-02-24 20:37:56 +0000697 OS << "\n";
698}
699
Aaron Ballman615eb472017-10-15 14:32:27 +0000700#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000701LLVM_DUMP_METHOD void AliasSet::dump() const { print(dbgs()); }
702LLVM_DUMP_METHOD void AliasSetTracker::dump() const { print(dbgs()); }
Manman Renc3366cc2012-09-06 19:55:56 +0000703#endif
Chris Lattner7606fb62003-02-24 20:37:56 +0000704
Chris Lattner7606fb62003-02-24 20:37:56 +0000705//===----------------------------------------------------------------------===//
Dan Gohmanf4362da2009-07-30 20:21:41 +0000706// ASTCallbackVH Class Implementation
707//===----------------------------------------------------------------------===//
708
709void AliasSetTracker::ASTCallbackVH::deleted() {
710 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
711 AST->deleteValue(getValPtr());
712 // this now dangles!
713}
714
Eli Friedman17822fc2011-04-09 06:55:46 +0000715void AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(Value *V) {
716 AST->copyValue(getValPtr(), V);
717}
718
Dan Gohmanf4362da2009-07-30 20:21:41 +0000719AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
Dan Gohmandc2b1b02009-07-31 18:21:48 +0000720 : CallbackVH(V), AST(ast) {}
721
722AliasSetTracker::ASTCallbackVH &
723AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
724 return *this = ASTCallbackVH(V, AST);
725}
Dan Gohmanf4362da2009-07-30 20:21:41 +0000726
727//===----------------------------------------------------------------------===//
Chris Lattner7606fb62003-02-24 20:37:56 +0000728// AliasSetPrinter Pass
729//===----------------------------------------------------------------------===//
730
731namespace {
Eugene Zelenko48666a62017-07-24 23:16:33 +0000732
Nick Lewycky02d5f772009-10-25 06:33:48 +0000733 class AliasSetPrinter : public FunctionPass {
Chris Lattner7606fb62003-02-24 20:37:56 +0000734 AliasSetTracker *Tracker;
Eugene Zelenko48666a62017-07-24 23:16:33 +0000735
Chris Lattner7606fb62003-02-24 20:37:56 +0000736 public:
Nick Lewyckye7da2d62007-05-06 13:37:16 +0000737 static char ID; // Pass identification, replacement for typeid
Eugene Zelenko48666a62017-07-24 23:16:33 +0000738
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000739 AliasSetPrinter() : FunctionPass(ID) {
740 initializeAliasSetPrinterPass(*PassRegistry::getPassRegistry());
741 }
Devang Patel09f162c2007-05-01 21:15:47 +0000742
Craig Toppere9ba7592014-03-05 07:30:04 +0000743 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner7606fb62003-02-24 20:37:56 +0000744 AU.setPreservesAll();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000745 AU.addRequired<AAResultsWrapperPass>();
Chris Lattner7606fb62003-02-24 20:37:56 +0000746 }
747
Craig Toppere9ba7592014-03-05 07:30:04 +0000748 bool runOnFunction(Function &F) override {
Chandler Carruth7b560d42015-09-09 17:55:00 +0000749 auto &AAWP = getAnalysis<AAResultsWrapperPass>();
750 Tracker = new AliasSetTracker(AAWP.getAAResults());
Michael Kuperstein41898f02016-08-19 17:05:22 +0000751 errs() << "Alias sets for function '" << F.getName() << "':\n";
Chris Lattner7606fb62003-02-24 20:37:56 +0000752 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner2d3a7a62004-04-27 15:13:33 +0000753 Tracker->add(&*I);
David Greene0295ecf2009-12-23 22:49:57 +0000754 Tracker->print(errs());
Chris Lattner44497852006-01-03 06:05:22 +0000755 delete Tracker;
Chris Lattner7606fb62003-02-24 20:37:56 +0000756 return false;
757 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000758 };
Eugene Zelenko48666a62017-07-24 23:16:33 +0000759
760} // end anonymous namespace
Dan Gohmand78c4002008-05-13 00:00:25 +0000761
762char AliasSetPrinter::ID = 0;
Eugene Zelenko48666a62017-07-24 23:16:33 +0000763
Owen Anderson8ac477f2010-10-12 19:48:12 +0000764INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets",
765 "Alias Set Printer", false, true)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000766INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Owen Anderson8ac477f2010-10-12 19:48:12 +0000767INITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000768 "Alias Set Printer", false, true)