blob: d349ac51a9b98f46d52061c75746ae48969cc624 [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"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/DataLayout.h"
Chandler Carruth83948572014-03-04 10:30:26 +000017#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Instructions.h"
19#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth50fee932015-08-06 02:05:46 +000020#include "llvm/IR/Module.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/LLVMContext.h"
22#include "llvm/IR/Type.h"
Chris Lattner7606fb62003-02-24 20:37:56 +000023#include "llvm/Pass.h"
David Greene2ec90032009-12-23 19:27:59 +000024#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000025#include "llvm/Support/ErrorHandling.h"
Chris Lattnerb1d782b2009-08-23 05:17:37 +000026#include "llvm/Support/raw_ostream.h"
Chris Lattner0a140602003-12-14 04:52:11 +000027using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000028
Chris Lattner24bba4d2004-11-27 18:37:42 +000029/// mergeSetIn - Merge the specified alias set into this alias set.
Chris Lattner647df642002-09-26 21:49:07 +000030///
Chris Lattner24bba4d2004-11-27 18:37:42 +000031void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
Chris Lattner7606fb62003-02-24 20:37:56 +000032 assert(!AS.Forward && "Alias set is already forwarding!");
33 assert(!Forward && "This set is a forwarding set!!");
Chris Lattner647df642002-09-26 21:49:07 +000034
35 // Update the alias and access types of this set...
Chandler Carrutha561d752015-06-22 02:12:52 +000036 Access |= AS.Access;
37 Alias |= AS.Alias;
Chris Lattnerdc8070e2010-08-29 04:14:47 +000038 Volatile |= AS.Volatile;
Chris Lattner7606fb62003-02-24 20:37:56 +000039
Chandler Carrutha561d752015-06-22 02:12:52 +000040 if (Alias == SetMustAlias) {
Chris Lattner24bba4d2004-11-27 18:37:42 +000041 // Check that these two merged sets really are must aliases. Since both
42 // used to be must-alias sets, we can just check any pointer from each set
43 // for aliasing.
44 AliasAnalysis &AA = AST.getAliasAnalysis();
Chris Lattner0eab5ec2009-03-09 05:11:09 +000045 PointerRec *L = getSomePointer();
46 PointerRec *R = AS.getSomePointer();
Chris Lattner24bba4d2004-11-27 18:37:42 +000047
48 // If the pointers are not a must-alias pair, this set becomes a may alias.
Chandler Carruthac80dc72015-06-17 07:18:54 +000049 if (AA.alias(MemoryLocation(L->getValue(), L->getSize(), L->getAAInfo()),
50 MemoryLocation(R->getValue(), R->getSize(), R->getAAInfo())) !=
Chandler Carruthc3f49eb2015-06-22 02:16:51 +000051 MustAlias)
Chandler Carrutha561d752015-06-22 02:12:52 +000052 Alias = SetMayAlias;
Chris Lattner24bba4d2004-11-27 18:37:42 +000053 }
54
David Majnemer35639382014-11-19 19:36:18 +000055 bool ASHadUnknownInsts = !AS.UnknownInsts.empty();
Eli Friedmanae8161e2011-07-27 00:46:46 +000056 if (UnknownInsts.empty()) { // Merge call sites...
David Majnemer35639382014-11-19 19:36:18 +000057 if (ASHadUnknownInsts) {
Eli Friedmanae8161e2011-07-27 00:46:46 +000058 std::swap(UnknownInsts, AS.UnknownInsts);
David Majnemerb7adf342014-11-19 09:41:05 +000059 addRef();
60 }
David Majnemer35639382014-11-19 19:36:18 +000061 } else if (ASHadUnknownInsts) {
Eli Friedmanae8161e2011-07-27 00:46:46 +000062 UnknownInsts.insert(UnknownInsts.end(), AS.UnknownInsts.begin(), AS.UnknownInsts.end());
63 AS.UnknownInsts.clear();
Chris Lattner7606fb62003-02-24 20:37:56 +000064 }
Misha Brukman01808ca2005-04-21 21:13:18 +000065
Chris Lattner7606fb62003-02-24 20:37:56 +000066 AS.Forward = this; // Forward across AS now...
Chris Lattner053427f2004-07-22 07:58:18 +000067 addRef(); // AS is now pointing to us...
Chris Lattner7606fb62003-02-24 20:37:56 +000068
69 // Merge the list of constituent pointers...
Chris Lattner44fea542003-12-18 08:11:56 +000070 if (AS.PtrList) {
71 *PtrListEnd = AS.PtrList;
Chris Lattner0eab5ec2009-03-09 05:11:09 +000072 AS.PtrList->setPrevInList(PtrListEnd);
Chris Lattner44fea542003-12-18 08:11:56 +000073 PtrListEnd = AS.PtrListEnd;
74
Craig Topper9f008862014-04-15 04:59:12 +000075 AS.PtrList = nullptr;
Chris Lattner44fea542003-12-18 08:11:56 +000076 AS.PtrListEnd = &AS.PtrList;
Craig Topper9f008862014-04-15 04:59:12 +000077 assert(*AS.PtrListEnd == nullptr && "End of list is not null?");
Chris Lattner44fea542003-12-18 08:11:56 +000078 }
David Majnemerb7adf342014-11-19 09:41:05 +000079 if (ASHadUnknownInsts)
80 AS.dropRef(AST);
Chris Lattner647df642002-09-26 21:49:07 +000081}
82
Chris Lattner7606fb62003-02-24 20:37:56 +000083void AliasSetTracker::removeAliasSet(AliasSet *AS) {
Chris Lattner053427f2004-07-22 07:58:18 +000084 if (AliasSet *Fwd = AS->Forward) {
85 Fwd->dropRef(*this);
Craig Topper9f008862014-04-15 04:59:12 +000086 AS->Forward = nullptr;
Chris Lattner053427f2004-07-22 07:58:18 +000087 }
Chris Lattner7606fb62003-02-24 20:37:56 +000088 AliasSets.erase(AS);
89}
90
91void AliasSet::removeFromTracker(AliasSetTracker &AST) {
92 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
93 AST.removeAliasSet(this);
94}
95
Chris Lattner0eab5ec2009-03-09 05:11:09 +000096void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
Hal Finkelcc39b672014-07-24 12:16:19 +000097 uint64_t Size, const AAMDNodes &AAInfo,
Hal Finkel1140e172015-10-28 22:13:41 +000098 bool KnownMustAlias) {
Chris Lattner0eab5ec2009-03-09 05:11:09 +000099 assert(!Entry.hasAliasSet() && "Entry already in set!");
Chris Lattner7606fb62003-02-24 20:37:56 +0000100
Chris Lattnerab644812004-09-14 19:15:32 +0000101 // Check to see if we have to downgrade to _may_ alias.
102 if (isMustAlias() && !KnownMustAlias)
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000103 if (PointerRec *P = getSomePointer()) {
Chris Lattner6fa96652004-09-15 16:59:47 +0000104 AliasAnalysis &AA = AST.getAliasAnalysis();
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000105 AliasResult Result =
Chandler Carruthac80dc72015-06-17 07:18:54 +0000106 AA.alias(MemoryLocation(P->getValue(), P->getSize(), P->getAAInfo()),
107 MemoryLocation(Entry.getValue(), Size, AAInfo));
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000108 if (Result != MustAlias)
Chandler Carrutha561d752015-06-22 02:12:52 +0000109 Alias = SetMayAlias;
Chris Lattnerb5b0b7a2003-02-26 22:11:00 +0000110 else // First entry of must alias must have maximum size!
Hal Finkelcc39b672014-07-24 12:16:19 +0000111 P->updateSizeAndAAInfo(Size, AAInfo);
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000112 assert(Result != NoAlias && "Cannot be part of must set!");
Chris Lattnerb5b0b7a2003-02-26 22:11:00 +0000113 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000114
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000115 Entry.setAliasSet(this);
Hal Finkelcc39b672014-07-24 12:16:19 +0000116 Entry.updateSizeAndAAInfo(Size, AAInfo);
Chris Lattner7606fb62003-02-24 20:37:56 +0000117
118 // Add it to the end of the list...
Craig Topper9f008862014-04-15 04:59:12 +0000119 assert(*PtrListEnd == nullptr && "End of list is not null?");
Chris Lattner44fea542003-12-18 08:11:56 +0000120 *PtrListEnd = &Entry;
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000121 PtrListEnd = Entry.setPrevInList(PtrListEnd);
Craig Topper9f008862014-04-15 04:59:12 +0000122 assert(*PtrListEnd == nullptr && "End of list is not null?");
Chris Lattnereef6b192010-08-29 04:13:43 +0000123 addRef(); // Entry points to alias set.
Chris Lattner7606fb62003-02-24 20:37:56 +0000124}
125
Hal Finkel1140e172015-10-28 22:13:41 +0000126void AliasSet::addUnknownInst(Instruction *I, AliasAnalysis &AA) {
David Majnemerb7adf342014-11-19 09:41:05 +0000127 if (UnknownInsts.empty())
128 addRef();
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000129 UnknownInsts.emplace_back(I);
Chris Lattner21c60f12004-03-15 04:08:36 +0000130
Eli Friedmanae8161e2011-07-27 00:46:46 +0000131 if (!I->mayWriteToMemory()) {
Chandler Carrutha561d752015-06-22 02:12:52 +0000132 Alias = SetMayAlias;
Hal Finkel1140e172015-10-28 22:13:41 +0000133 Access |= RefAccess;
Duncan Sands68b6f502007-12-01 07:51:45 +0000134 return;
Chris Lattner21c60f12004-03-15 04:08:36 +0000135 }
136
Hal Finkel1140e172015-10-28 22:13:41 +0000137 // FIXME: This should use mod/ref information to make this not suck so bad
Chandler Carrutha561d752015-06-22 02:12:52 +0000138 Alias = SetMayAlias;
Hal Finkel1140e172015-10-28 22:13:41 +0000139 Access = ModRefAccess;
Chris Lattner7606fb62003-02-24 20:37:56 +0000140}
141
142/// aliasesPointer - Return true if the specified pointer "may" (or must)
Chris Lattner647df642002-09-26 21:49:07 +0000143/// alias one of the members in the set.
144///
Dan Gohmanf372cf82010-10-19 22:54:46 +0000145bool AliasSet::aliasesPointer(const Value *Ptr, uint64_t Size,
Hal Finkelcc39b672014-07-24 12:16:19 +0000146 const AAMDNodes &AAInfo,
Hal Finkel1140e172015-10-28 22:13:41 +0000147 AliasAnalysis &AA) const {
Chandler Carrutha561d752015-06-22 02:12:52 +0000148 if (Alias == SetMustAlias) {
Eli Friedmanae8161e2011-07-27 00:46:46 +0000149 assert(UnknownInsts.empty() && "Illegal must alias set!");
Chris Lattner7f04ebc2004-03-15 06:28:07 +0000150
Chris Lattner7606fb62003-02-24 20:37:56 +0000151 // If this is a set of MustAliases, only check to see if the pointer aliases
Chris Lattnereef6b192010-08-29 04:13:43 +0000152 // SOME value in the set.
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000153 PointerRec *SomePtr = getSomePointer();
Chris Lattner7606fb62003-02-24 20:37:56 +0000154 assert(SomePtr && "Empty must-alias set??");
Chandler Carruthac80dc72015-06-17 07:18:54 +0000155 return AA.alias(MemoryLocation(SomePtr->getValue(), SomePtr->getSize(),
156 SomePtr->getAAInfo()),
157 MemoryLocation(Ptr, Size, AAInfo));
Chris Lattner7606fb62003-02-24 20:37:56 +0000158 }
159
160 // If this is a may-alias set, we have to check all of the pointers in the set
161 // to be sure it doesn't alias the set...
162 for (iterator I = begin(), E = end(); I != E; ++I)
Chandler Carruthac80dc72015-06-17 07:18:54 +0000163 if (AA.alias(MemoryLocation(Ptr, Size, AAInfo),
164 MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo())))
Chris Lattner7606fb62003-02-24 20:37:56 +0000165 return true;
166
Eli Friedmanae8161e2011-07-27 00:46:46 +0000167 // Check the unknown instructions...
168 if (!UnknownInsts.empty()) {
169 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i)
Hal Finkel1140e172015-10-28 22:13:41 +0000170 if (AA.getModRefInfo(UnknownInsts[i],
171 MemoryLocation(Ptr, Size, AAInfo)) != MRI_NoModRef)
172 return true;
Chris Lattner9b323c32004-07-27 02:20:26 +0000173 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000174
Hal Finkel1140e172015-10-28 22:13:41 +0000175 return false;
Chris Lattner647df642002-09-26 21:49:07 +0000176}
177
Pete Cooper4bf388d2015-05-13 01:12:12 +0000178bool AliasSet::aliasesUnknownInst(const Instruction *Inst,
Hal Finkel1140e172015-10-28 22:13:41 +0000179 AliasAnalysis &AA) const {
Eli Friedmanae8161e2011-07-27 00:46:46 +0000180 if (!Inst->mayReadOrWriteMemory())
Duncan Sands68b6f502007-12-01 07:51:45 +0000181 return false;
Chris Lattner21c60f12004-03-15 04:08:36 +0000182
Eli Friedmanae8161e2011-07-27 00:46:46 +0000183 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
Pete Cooper4bf388d2015-05-13 01:12:12 +0000184 ImmutableCallSite C1(getUnknownInst(i)), C2(Inst);
Hal Finkel1140e172015-10-28 22:13:41 +0000185 if (!C1 || !C2 || AA.getModRefInfo(C1, C2) != MRI_NoModRef ||
186 AA.getModRefInfo(C2, C1) != MRI_NoModRef)
187 return true;
Chris Lattnerf58382e2010-08-29 18:42:23 +0000188 }
Chris Lattner9b323c32004-07-27 02:20:26 +0000189
Hal Finkel1140e172015-10-28 22:13:41 +0000190 for (iterator I = begin(), E = end(); I != E; ++I)
191 if (AA.getModRefInfo(Inst, MemoryLocation(I.getPointer(), I.getSize(),
192 I.getAAInfo())) != MRI_NoModRef)
193 return true;
194
195 return false;
Chris Lattner647df642002-09-26 21:49:07 +0000196}
197
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000198void AliasSetTracker::clear() {
199 // Delete all the PointerRec entries.
Dan Gohmanf4362da2009-07-30 20:21:41 +0000200 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
201 I != E; ++I)
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000202 I->second->eraseFromList();
203
204 PointerMap.clear();
205
206 // The alias sets should all be clear now.
207 AliasSets.clear();
208}
209
Chris Lattner647df642002-09-26 21:49:07 +0000210
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000211/// mergeAliasSetsForPointer - Given a pointer, merge all alias sets that may
212/// alias the pointer. Return the unified set, or nullptr if no set that aliases
213/// the pointer was found.
214AliasSet *AliasSetTracker::mergeAliasSetsForPointer(const Value *Ptr,
215 uint64_t Size,
216 const AAMDNodes &AAInfo) {
Craig Topper9f008862014-04-15 04:59:12 +0000217 AliasSet *FoundSet = nullptr;
David Majnemerb7adf342014-11-19 09:41:05 +0000218 for (iterator I = begin(), E = end(); I != E;) {
219 iterator Cur = I++;
Hal Finkel1140e172015-10-28 22:13:41 +0000220 if (Cur->Forward || !Cur->aliasesPointer(Ptr, Size, AAInfo, AA)) continue;
Chris Lattnerafb70742010-08-29 04:06:55 +0000221
Craig Topper9f008862014-04-15 04:59:12 +0000222 if (!FoundSet) { // If this is the first alias set ptr can go into.
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000223 FoundSet = &*Cur; // Remember it.
Chris Lattnerafb70742010-08-29 04:06:55 +0000224 } else { // Otherwise, we must merge the sets.
David Majnemerb7adf342014-11-19 09:41:05 +0000225 FoundSet->mergeSetIn(*Cur, *this); // Merge in contents.
Chris Lattner647df642002-09-26 21:49:07 +0000226 }
Chris Lattnerafb70742010-08-29 04:06:55 +0000227 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000228
229 return FoundSet;
230}
231
Chris Lattnereeaa29c2004-11-26 21:36:25 +0000232/// containsPointer - Return true if the specified location is represented by
233/// this alias set, false otherwise. This does not modify the AST object or
234/// alias sets.
Pete Cooper4bf388d2015-05-13 01:12:12 +0000235bool AliasSetTracker::containsPointer(const Value *Ptr, uint64_t Size,
Hal Finkelcc39b672014-07-24 12:16:19 +0000236 const AAMDNodes &AAInfo) const {
Benjamin Krameraa209152016-06-26 17:27:42 +0000237 for (const AliasSet &AS : *this)
238 if (!AS.Forward && AS.aliasesPointer(Ptr, Size, AAInfo, AA))
Chris Lattnereeaa29c2004-11-26 21:36:25 +0000239 return true;
240 return false;
241}
242
Pete Cooper4bf388d2015-05-13 01:12:12 +0000243bool AliasSetTracker::containsUnknown(const Instruction *Inst) const {
Benjamin Krameraa209152016-06-26 17:27:42 +0000244 for (const AliasSet &AS : *this)
245 if (!AS.Forward && AS.aliasesUnknownInst(Inst, AA))
Hal Finkel840257a2014-11-03 23:19:16 +0000246 return true;
247 return false;
248}
Chris Lattnereeaa29c2004-11-26 21:36:25 +0000249
Hal Finkel1140e172015-10-28 22:13:41 +0000250AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) {
Craig Topper9f008862014-04-15 04:59:12 +0000251 AliasSet *FoundSet = nullptr;
David Majnemerb7adf342014-11-19 09:41:05 +0000252 for (iterator I = begin(), E = end(); I != E;) {
253 iterator Cur = I++;
Hal Finkel1140e172015-10-28 22:13:41 +0000254 if (Cur->Forward || !Cur->aliasesUnknownInst(Inst, AA))
Chris Lattnerafb70742010-08-29 04:06:55 +0000255 continue;
Craig Topper9f008862014-04-15 04:59:12 +0000256 if (!FoundSet) // If this is the first alias set ptr can go into.
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000257 FoundSet = &*Cur; // Remember it.
David Majnemerb7adf342014-11-19 09:41:05 +0000258 else if (!Cur->Forward) // Otherwise, we must merge the sets.
259 FoundSet->mergeSetIn(*Cur, *this); // Merge in contents.
Chris Lattnerafb70742010-08-29 04:06:55 +0000260 }
Chris Lattner647df642002-09-26 21:49:07 +0000261 return FoundSet;
262}
263
264
Chris Lattner647df642002-09-26 21:49:07 +0000265
Chris Lattner7606fb62003-02-24 20:37:56 +0000266
267/// getAliasSetForPointer - Return the alias set that the specified pointer
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000268/// lives in.
Dan Gohmanf372cf82010-10-19 22:54:46 +0000269AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, uint64_t Size,
Hal Finkelcc39b672014-07-24 12:16:19 +0000270 const AAMDNodes &AAInfo,
Chris Lattner2cfaef22004-07-21 05:18:04 +0000271 bool *New) {
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000272 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
Chris Lattner7606fb62003-02-24 20:37:56 +0000273
Chris Lattnereef6b192010-08-29 04:13:43 +0000274 // Check to see if the pointer is already known.
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000275 if (Entry.hasAliasSet()) {
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000276 // If the size changed, we may need to merge several alias sets.
277 // Note that we can *not* return the result of mergeAliasSetsForPointer
278 // due to a quirk of alias analysis behavior. Since alias(undef, undef)
279 // is NoAlias, mergeAliasSetsForPointer(undef, ...) will not find the
280 // the right set for undef, even if it exists.
281 if (Entry.updateSizeAndAAInfo(Size, AAInfo))
282 mergeAliasSetsForPointer(Pointer, Size, AAInfo);
Chris Lattner7606fb62003-02-24 20:37:56 +0000283 // Return the set!
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000284 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattnerafb70742010-08-29 04:06:55 +0000285 }
286
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000287 if (AliasSet *AS = mergeAliasSetsForPointer(Pointer, Size, AAInfo)) {
Chris Lattnereef6b192010-08-29 04:13:43 +0000288 // Add it to the alias set it aliases.
Hal Finkel1140e172015-10-28 22:13:41 +0000289 AS->addPointer(*this, Entry, Size, AAInfo);
Chris Lattner7606fb62003-02-24 20:37:56 +0000290 return *AS;
Chris Lattner647df642002-09-26 21:49:07 +0000291 }
Chris Lattnerafb70742010-08-29 04:06:55 +0000292
293 if (New) *New = true;
Chris Lattnereef6b192010-08-29 04:13:43 +0000294 // Otherwise create a new alias set to hold the loaded pointer.
Chris Lattnerafb70742010-08-29 04:06:55 +0000295 AliasSets.push_back(new AliasSet());
Hal Finkel1140e172015-10-28 22:13:41 +0000296 AliasSets.back().addPointer(*this, Entry, Size, AAInfo);
Chris Lattnerafb70742010-08-29 04:06:55 +0000297 return AliasSets.back();
Chris Lattner647df642002-09-26 21:49:07 +0000298}
299
Hal Finkelcc39b672014-07-24 12:16:19 +0000300bool AliasSetTracker::add(Value *Ptr, uint64_t Size, const AAMDNodes &AAInfo) {
Chris Lattnerbf8c3c42004-07-26 05:50:23 +0000301 bool NewPtr;
Hal Finkel1140e172015-10-28 22:13:41 +0000302 addPointer(Ptr, Size, AAInfo, AliasSet::NoAccess, NewPtr);
Chris Lattnerbf8c3c42004-07-26 05:50:23 +0000303 return NewPtr;
304}
305
306
Chris Lattner2cfaef22004-07-21 05:18:04 +0000307bool AliasSetTracker::add(LoadInst *LI) {
JF Bastien800f87a2016-04-06 21:19:33 +0000308 if (isStrongerThanMonotonic(LI->getOrdering())) return addUnknown(LI);
Hal Finkelcc39b672014-07-24 12:16:19 +0000309
310 AAMDNodes AAInfo;
311 LI->getAAMetadata(AAInfo);
312
Hal Finkel1140e172015-10-28 22:13:41 +0000313 AliasSet::AccessLattice Access = AliasSet::RefAccess;
Chris Lattner2cfaef22004-07-21 05:18:04 +0000314 bool NewPtr;
Chandler Carruth50fee932015-08-06 02:05:46 +0000315 const DataLayout &DL = LI->getModule()->getDataLayout();
Chris Lattner2cfaef22004-07-21 05:18:04 +0000316 AliasSet &AS = addPointer(LI->getOperand(0),
Chandler Carruth50fee932015-08-06 02:05:46 +0000317 DL.getTypeStoreSize(LI->getType()),
Chandler Carrutha561d752015-06-22 02:12:52 +0000318 AAInfo, Access, NewPtr);
Chris Lattner0a140602003-12-14 04:52:11 +0000319 if (LI->isVolatile()) AS.setVolatile();
Chris Lattner2cfaef22004-07-21 05:18:04 +0000320 return NewPtr;
Chris Lattner7606fb62003-02-24 20:37:56 +0000321}
322
Chris Lattner2cfaef22004-07-21 05:18:04 +0000323bool AliasSetTracker::add(StoreInst *SI) {
JF Bastien800f87a2016-04-06 21:19:33 +0000324 if (isStrongerThanMonotonic(SI->getOrdering())) return addUnknown(SI);
Hal Finkelcc39b672014-07-24 12:16:19 +0000325
326 AAMDNodes AAInfo;
327 SI->getAAMetadata(AAInfo);
328
Hal Finkel1140e172015-10-28 22:13:41 +0000329 AliasSet::AccessLattice Access = AliasSet::ModAccess;
Chris Lattner2cfaef22004-07-21 05:18:04 +0000330 bool NewPtr;
Chandler Carruth50fee932015-08-06 02:05:46 +0000331 const DataLayout &DL = SI->getModule()->getDataLayout();
Chris Lattner2cfaef22004-07-21 05:18:04 +0000332 Value *Val = SI->getOperand(0);
333 AliasSet &AS = addPointer(SI->getOperand(1),
Chandler Carruth50fee932015-08-06 02:05:46 +0000334 DL.getTypeStoreSize(Val->getType()),
Chandler Carrutha561d752015-06-22 02:12:52 +0000335 AAInfo, Access, NewPtr);
Chris Lattner0a140602003-12-14 04:52:11 +0000336 if (SI->isVolatile()) AS.setVolatile();
Chris Lattner2cfaef22004-07-21 05:18:04 +0000337 return NewPtr;
Chris Lattner647df642002-09-26 21:49:07 +0000338}
339
Dan Gohman2cd8e382008-04-14 18:34:50 +0000340bool AliasSetTracker::add(VAArgInst *VAAI) {
Hal Finkelcc39b672014-07-24 12:16:19 +0000341 AAMDNodes AAInfo;
342 VAAI->getAAMetadata(AAInfo);
343
Dan Gohman2cd8e382008-04-14 18:34:50 +0000344 bool NewPtr;
Chandler Carruthecbd1682015-06-17 07:21:38 +0000345 addPointer(VAAI->getOperand(0), MemoryLocation::UnknownSize, AAInfo,
Hal Finkel1140e172015-10-28 22:13:41 +0000346 AliasSet::ModRefAccess, NewPtr);
Dan Gohman2cd8e382008-04-14 18:34:50 +0000347 return NewPtr;
348}
349
Haicheng Wu5cf99092016-02-17 02:01:50 +0000350bool AliasSetTracker::add(MemSetInst *MSI) {
351 AAMDNodes AAInfo;
352 MSI->getAAMetadata(AAInfo);
353
354 bool NewPtr;
355 uint64_t Len;
356
357 if (ConstantInt *C = dyn_cast<ConstantInt>(MSI->getLength()))
358 Len = C->getZExtValue();
359 else
360 Len = MemoryLocation::UnknownSize;
361
362 AliasSet &AS =
Michael Kupersteinb7860fe2016-03-14 18:34:29 +0000363 addPointer(MSI->getRawDest(), Len, AAInfo, AliasSet::ModAccess, NewPtr);
Haicheng Wu5cf99092016-02-17 02:01:50 +0000364 if (MSI->isVolatile())
365 AS.setVolatile();
366 return NewPtr;
367}
Chris Lattner0a140602003-12-14 04:52:11 +0000368
Eli Friedmanae8161e2011-07-27 00:46:46 +0000369bool AliasSetTracker::addUnknown(Instruction *Inst) {
370 if (isa<DbgInfoIntrinsic>(Inst))
Zhou Sheng506035102009-03-03 06:02:04 +0000371 return true; // Ignore DbgInfo Intrinsics.
Eli Friedmanae8161e2011-07-27 00:46:46 +0000372 if (!Inst->mayReadOrWriteMemory())
Duncan Sands68b6f502007-12-01 07:51:45 +0000373 return true; // doesn't alias anything
Chris Lattner7f04ebc2004-03-15 06:28:07 +0000374
Hal Finkel1140e172015-10-28 22:13:41 +0000375 AliasSet *AS = findAliasSetForUnknownInst(Inst);
Chris Lattnerafb70742010-08-29 04:06:55 +0000376 if (AS) {
Hal Finkel1140e172015-10-28 22:13:41 +0000377 AS->addUnknownInst(Inst, AA);
Chris Lattner2cfaef22004-07-21 05:18:04 +0000378 return false;
Chris Lattner7606fb62003-02-24 20:37:56 +0000379 }
Chris Lattnerafb70742010-08-29 04:06:55 +0000380 AliasSets.push_back(new AliasSet());
381 AS = &AliasSets.back();
Hal Finkel1140e172015-10-28 22:13:41 +0000382 AS->addUnknownInst(Inst, AA);
Chris Lattnerafb70742010-08-29 04:06:55 +0000383 return true;
Chris Lattner647df642002-09-26 21:49:07 +0000384}
385
Chris Lattner2cfaef22004-07-21 05:18:04 +0000386bool AliasSetTracker::add(Instruction *I) {
Chris Lattnerafb70742010-08-29 04:06:55 +0000387 // Dispatch to one of the other add methods.
Chris Lattner7606fb62003-02-24 20:37:56 +0000388 if (LoadInst *LI = dyn_cast<LoadInst>(I))
Chris Lattner2cfaef22004-07-21 05:18:04 +0000389 return add(LI);
Chris Lattnerafb70742010-08-29 04:06:55 +0000390 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner2cfaef22004-07-21 05:18:04 +0000391 return add(SI);
Chris Lattnerafb70742010-08-29 04:06:55 +0000392 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
Dan Gohman2cd8e382008-04-14 18:34:50 +0000393 return add(VAAI);
Haicheng Wu5cf99092016-02-17 02:01:50 +0000394 if (MemSetInst *MSI = dyn_cast<MemSetInst>(I))
395 return add(MSI);
Eli Friedmanae8161e2011-07-27 00:46:46 +0000396 return addUnknown(I);
Haicheng Wu5cf99092016-02-17 02:01:50 +0000397 // FIXME: add support of memcpy and memmove.
Chris Lattner647df642002-09-26 21:49:07 +0000398}
399
Chris Lattnerc048bb32003-03-03 23:28:05 +0000400void AliasSetTracker::add(BasicBlock &BB) {
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000401 for (auto &I : BB)
402 add(&I);
Chris Lattnerc048bb32003-03-03 23:28:05 +0000403}
404
405void AliasSetTracker::add(const AliasSetTracker &AST) {
406 assert(&AA == &AST.AA &&
407 "Merging AliasSetTracker objects with different Alias Analyses!");
408
409 // Loop over all of the alias sets in AST, adding the pointers contained
410 // therein into the current alias sets. This can cause alias sets to be
411 // merged together in the current AST.
Benjamin Krameraa209152016-06-26 17:27:42 +0000412 for (const AliasSet &AS : AST) {
413 if (AS.Forward)
414 continue; // Ignore forwarding alias sets
Chris Lattnerc048bb32003-03-03 23:28:05 +0000415
Chris Lattnerafb70742010-08-29 04:06:55 +0000416 // If there are any call sites in the alias set, add them to this AST.
Eli Friedmanae8161e2011-07-27 00:46:46 +0000417 for (unsigned i = 0, e = AS.UnknownInsts.size(); i != e; ++i)
418 add(AS.UnknownInsts[i]);
Chris Lattnerc048bb32003-03-03 23:28:05 +0000419
Chris Lattnerafb70742010-08-29 04:06:55 +0000420 // Loop over all of the pointers in this alias set.
421 bool X;
422 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) {
423 AliasSet &NewAS = addPointer(ASI.getPointer(), ASI.getSize(),
Hal Finkelcc39b672014-07-24 12:16:19 +0000424 ASI.getAAInfo(),
Chandler Carrutha561d752015-06-22 02:12:52 +0000425 (AliasSet::AccessLattice)AS.Access, X);
Chris Lattnerafb70742010-08-29 04:06:55 +0000426 if (AS.isVolatile()) NewAS.setVolatile();
Chris Lattnerc048bb32003-03-03 23:28:05 +0000427 }
Chris Lattnerafb70742010-08-29 04:06:55 +0000428 }
Chris Lattnerc048bb32003-03-03 23:28:05 +0000429}
430
Chris Lattnerabc4f452004-07-21 07:04:26 +0000431/// remove - Remove the specified (potentially non-empty) alias set from the
432/// tracker.
433void AliasSetTracker::remove(AliasSet &AS) {
Chris Lattnerf4e06532006-06-27 23:48:59 +0000434 // Drop all call sites.
David Majnemerb7adf342014-11-19 09:41:05 +0000435 if (!AS.UnknownInsts.empty())
436 AS.dropRef(*this);
Eli Friedmanae8161e2011-07-27 00:46:46 +0000437 AS.UnknownInsts.clear();
Chris Lattnerf4e06532006-06-27 23:48:59 +0000438
439 // Clear the alias set.
440 unsigned NumRefs = 0;
441 while (!AS.empty()) {
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000442 AliasSet::PointerRec *P = AS.PtrList;
443
444 Value *ValToRemove = P->getValue();
Chris Lattnerf4e06532006-06-27 23:48:59 +0000445
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000446 // Unlink and delete entry from the list of values.
447 P->eraseFromList();
Chris Lattnerf4e06532006-06-27 23:48:59 +0000448
449 // Remember how many references need to be dropped.
450 ++NumRefs;
Chris Lattnerabc4f452004-07-21 07:04:26 +0000451
Chris Lattnerf4e06532006-06-27 23:48:59 +0000452 // Finally, remove the entry.
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000453 PointerMap.erase(ValToRemove);
Chris Lattnerf4e06532006-06-27 23:48:59 +0000454 }
455
456 // Stop using the alias set, removing it.
Chris Lattner90213c62006-06-27 23:56:13 +0000457 AS.RefCount -= NumRefs;
458 if (AS.RefCount == 0)
459 AS.removeFromTracker(*this);
Chris Lattnerabc4f452004-07-21 07:04:26 +0000460}
461
Dan Gohman71af9db2010-10-18 20:44:50 +0000462bool
Hal Finkelcc39b672014-07-24 12:16:19 +0000463AliasSetTracker::remove(Value *Ptr, uint64_t Size, const AAMDNodes &AAInfo) {
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000464 AliasSet *AS = mergeAliasSetsForPointer(Ptr, Size, AAInfo);
Chris Lattnerbf8c3c42004-07-26 05:50:23 +0000465 if (!AS) return false;
466 remove(*AS);
467 return true;
468}
Chris Lattnerabc4f452004-07-21 07:04:26 +0000469
470bool AliasSetTracker::remove(LoadInst *LI) {
Chandler Carruth50fee932015-08-06 02:05:46 +0000471 const DataLayout &DL = LI->getModule()->getDataLayout();
472 uint64_t Size = DL.getTypeStoreSize(LI->getType());
Hal Finkelcc39b672014-07-24 12:16:19 +0000473
474 AAMDNodes AAInfo;
475 LI->getAAMetadata(AAInfo);
476
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000477 AliasSet *AS = mergeAliasSetsForPointer(LI->getOperand(0), Size, AAInfo);
Chris Lattnerabc4f452004-07-21 07:04:26 +0000478 if (!AS) return false;
479 remove(*AS);
480 return true;
481}
482
483bool AliasSetTracker::remove(StoreInst *SI) {
Chandler Carruth50fee932015-08-06 02:05:46 +0000484 const DataLayout &DL = SI->getModule()->getDataLayout();
485 uint64_t Size = DL.getTypeStoreSize(SI->getOperand(0)->getType());
Hal Finkelcc39b672014-07-24 12:16:19 +0000486
487 AAMDNodes AAInfo;
488 SI->getAAMetadata(AAInfo);
489
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000490 AliasSet *AS = mergeAliasSetsForPointer(SI->getOperand(1), Size, AAInfo);
Chris Lattnerabc4f452004-07-21 07:04:26 +0000491 if (!AS) return false;
492 remove(*AS);
493 return true;
494}
495
Dan Gohman2cd8e382008-04-14 18:34:50 +0000496bool AliasSetTracker::remove(VAArgInst *VAAI) {
Hal Finkelcc39b672014-07-24 12:16:19 +0000497 AAMDNodes AAInfo;
498 VAAI->getAAMetadata(AAInfo);
499
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000500 AliasSet *AS = mergeAliasSetsForPointer(VAAI->getOperand(0),
501 MemoryLocation::UnknownSize, AAInfo);
Dan Gohman2cd8e382008-04-14 18:34:50 +0000502 if (!AS) return false;
503 remove(*AS);
504 return true;
505}
506
Haicheng Wu5cf99092016-02-17 02:01:50 +0000507bool AliasSetTracker::remove(MemSetInst *MSI) {
508 AAMDNodes AAInfo;
509 MSI->getAAMetadata(AAInfo);
510 uint64_t Len;
511
512 if (ConstantInt *C = dyn_cast<ConstantInt>(MSI->getLength()))
513 Len = C->getZExtValue();
514 else
515 Len = MemoryLocation::UnknownSize;
516
Michael Kuperstein16f13e22016-04-14 22:00:11 +0000517 AliasSet *AS = mergeAliasSetsForPointer(MSI->getRawDest(), Len, AAInfo);
Haicheng Wu5cf99092016-02-17 02:01:50 +0000518 if (!AS)
519 return false;
520 remove(*AS);
521 return true;
522}
523
Eli Friedmanae8161e2011-07-27 00:46:46 +0000524bool AliasSetTracker::removeUnknown(Instruction *I) {
525 if (!I->mayReadOrWriteMemory())
Duncan Sands68b6f502007-12-01 07:51:45 +0000526 return false; // doesn't alias anything
Chris Lattnerabc4f452004-07-21 07:04:26 +0000527
Eli Friedmanae8161e2011-07-27 00:46:46 +0000528 AliasSet *AS = findAliasSetForUnknownInst(I);
Chris Lattnerabc4f452004-07-21 07:04:26 +0000529 if (!AS) return false;
530 remove(*AS);
531 return true;
532}
533
534bool AliasSetTracker::remove(Instruction *I) {
535 // Dispatch to one of the other remove methods...
536 if (LoadInst *LI = dyn_cast<LoadInst>(I))
537 return remove(LI);
Chris Lattnereef6b192010-08-29 04:13:43 +0000538 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattnerabc4f452004-07-21 07:04:26 +0000539 return remove(SI);
Chris Lattnereef6b192010-08-29 04:13:43 +0000540 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
Dan Gohman2cd8e382008-04-14 18:34:50 +0000541 return remove(VAAI);
Haicheng Wu5cf99092016-02-17 02:01:50 +0000542 if (MemSetInst *MSI = dyn_cast<MemSetInst>(I))
543 return remove(MSI);
Eli Friedmanae8161e2011-07-27 00:46:46 +0000544 return removeUnknown(I);
Haicheng Wu5cf99092016-02-17 02:01:50 +0000545 // FIXME: add support of memcpy and memmove.
Chris Lattnerabc4f452004-07-21 07:04:26 +0000546}
547
Chris Lattner44fea542003-12-18 08:11:56 +0000548
Chris Lattner746e1e12004-05-23 21:10:58 +0000549// deleteValue method - This method is used to remove a pointer value from the
Chris Lattner44fea542003-12-18 08:11:56 +0000550// AliasSetTracker entirely. It should be used when an instruction is deleted
551// from the program to update the AST. If you don't use this, you would have
552// dangling pointers to deleted instructions.
553//
Chris Lattner746e1e12004-05-23 21:10:58 +0000554void AliasSetTracker::deleteValue(Value *PtrVal) {
Chris Lattnere7d4e562006-06-26 19:20:48 +0000555 // If this is a call instruction, remove the callsite from the appropriate
Chris Lattnerf58382e2010-08-29 18:42:23 +0000556 // AliasSet (if present).
Eli Friedmanae8161e2011-07-27 00:46:46 +0000557 if (Instruction *Inst = dyn_cast<Instruction>(PtrVal)) {
558 if (Inst->mayReadOrWriteMemory()) {
Chris Lattnerf58382e2010-08-29 18:42:23 +0000559 // Scan all the alias sets to see if this call site is contained.
David Majnemerb7adf342014-11-19 09:41:05 +0000560 for (iterator I = begin(), E = end(); I != E;) {
561 iterator Cur = I++;
562 if (!Cur->Forward)
563 Cur->removeUnknownInst(*this, Inst);
Chris Lattnerf58382e2010-08-29 18:42:23 +0000564 }
565 }
566 }
Chris Lattnere7d4e562006-06-26 19:20:48 +0000567
Chris Lattnerab644812004-09-14 19:15:32 +0000568 // First, look up the PointerRec for this pointer.
Benjamin Kramere2ef47c2012-06-30 22:37:15 +0000569 PointerMapType::iterator I = PointerMap.find_as(PtrVal);
Chris Lattner44fea542003-12-18 08:11:56 +0000570 if (I == PointerMap.end()) return; // Noop
571
572 // If we found one, remove the pointer from the alias set it is in.
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000573 AliasSet::PointerRec *PtrValEnt = I->second;
574 AliasSet *AS = PtrValEnt->getAliasSet(*this);
Chris Lattner44fea542003-12-18 08:11:56 +0000575
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000576 // Unlink and delete from the list of values.
577 PtrValEnt->eraseFromList();
578
579 // Stop using the alias set.
Chris Lattner053427f2004-07-22 07:58:18 +0000580 AS->dropRef(*this);
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000581
Chris Lattner44fea542003-12-18 08:11:56 +0000582 PointerMap.erase(I);
583}
584
Chris Lattnerab644812004-09-14 19:15:32 +0000585// copyValue - This method should be used whenever a preexisting value in the
586// program is copied or cloned, introducing a new value. Note that it is ok for
587// clients that use this method to introduce the same value multiple times: if
588// the tracker already knows about a value, it will ignore the request.
589//
590void AliasSetTracker::copyValue(Value *From, Value *To) {
Chris Lattnerab644812004-09-14 19:15:32 +0000591 // First, look up the PointerRec for this pointer.
Benjamin Kramere2ef47c2012-06-30 22:37:15 +0000592 PointerMapType::iterator I = PointerMap.find_as(From);
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000593 if (I == PointerMap.end())
Chris Lattnerab644812004-09-14 19:15:32 +0000594 return; // Noop
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000595 assert(I->second->hasAliasSet() && "Dead entry?");
Chris Lattnerab644812004-09-14 19:15:32 +0000596
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000597 AliasSet::PointerRec &Entry = getEntryFor(To);
598 if (Entry.hasAliasSet()) return; // Already in the tracker!
Chris Lattnerab644812004-09-14 19:15:32 +0000599
600 // Add it to the alias set it aliases...
Benjamin Kramere2ef47c2012-06-30 22:37:15 +0000601 I = PointerMap.find_as(From);
Chris Lattner0eab5ec2009-03-09 05:11:09 +0000602 AliasSet *AS = I->second->getAliasSet(*this);
Dan Gohman408beac2010-10-18 21:28:00 +0000603 AS->addPointer(*this, Entry, I->second->getSize(),
Hal Finkel1140e172015-10-28 22:13:41 +0000604 I->second->getAAInfo(),
Dan Gohman71af9db2010-10-18 20:44:50 +0000605 true);
Chris Lattnerab644812004-09-14 19:15:32 +0000606}
607
608
Chris Lattner44fea542003-12-18 08:11:56 +0000609
Chris Lattner7606fb62003-02-24 20:37:56 +0000610//===----------------------------------------------------------------------===//
611// AliasSet/AliasSetTracker Printing Support
612//===----------------------------------------------------------------------===//
613
Chris Lattnerb1d782b2009-08-23 05:17:37 +0000614void AliasSet::print(raw_ostream &OS) const {
Roman Divackyad06cee2012-09-05 22:26:57 +0000615 OS << " AliasSet[" << (const void*)this << ", " << RefCount << "] ";
Chandler Carrutha561d752015-06-22 02:12:52 +0000616 OS << (Alias == SetMustAlias ? "must" : "may") << " alias, ";
617 switch (Access) {
Hal Finkel1140e172015-10-28 22:13:41 +0000618 case NoAccess: OS << "No access "; break;
619 case RefAccess: OS << "Ref "; break;
620 case ModAccess: OS << "Mod "; break;
621 case ModRefAccess: OS << "Mod/Ref "; break;
Chandler Carrutha561d752015-06-22 02:12:52 +0000622 default: llvm_unreachable("Bad value for Access!");
Chris Lattner647df642002-09-26 21:49:07 +0000623 }
Chris Lattner0a140602003-12-14 04:52:11 +0000624 if (isVolatile()) OS << "[volatile] ";
Chris Lattner7606fb62003-02-24 20:37:56 +0000625 if (Forward)
626 OS << " forwarding to " << (void*)Forward;
627
628
Dan Gohmanc731c972007-10-03 19:26:29 +0000629 if (!empty()) {
Chris Lattner7606fb62003-02-24 20:37:56 +0000630 OS << "Pointers: ";
631 for (iterator I = begin(), E = end(); I != E; ++I) {
632 if (I != begin()) OS << ", ";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000633 I.getPointer()->printAsOperand(OS << "(");
Chris Lattner053427f2004-07-22 07:58:18 +0000634 OS << ", " << I.getSize() << ")";
Chris Lattner7606fb62003-02-24 20:37:56 +0000635 }
636 }
Eli Friedmanae8161e2011-07-27 00:46:46 +0000637 if (!UnknownInsts.empty()) {
638 OS << "\n " << UnknownInsts.size() << " Unknown instructions: ";
639 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
Chris Lattner7606fb62003-02-24 20:37:56 +0000640 if (i) OS << ", ";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000641 UnknownInsts[i]->printAsOperand(OS);
Misha Brukman01808ca2005-04-21 21:13:18 +0000642 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000643 }
644 OS << "\n";
645}
646
Chris Lattnerb1d782b2009-08-23 05:17:37 +0000647void AliasSetTracker::print(raw_ostream &OS) const {
Chris Lattner7606fb62003-02-24 20:37:56 +0000648 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
649 << PointerMap.size() << " pointer values.\n";
Benjamin Krameraa209152016-06-26 17:27:42 +0000650 for (const AliasSet &AS : *this)
651 AS.print(OS);
Chris Lattner7606fb62003-02-24 20:37:56 +0000652 OS << "\n";
653}
654
Manman Ren49d684e2012-09-12 05:06:18 +0000655#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +0000656LLVM_DUMP_METHOD void AliasSet::dump() const { print(dbgs()); }
657LLVM_DUMP_METHOD void AliasSetTracker::dump() const { print(dbgs()); }
Manman Renc3366cc2012-09-06 19:55:56 +0000658#endif
Chris Lattner7606fb62003-02-24 20:37:56 +0000659
Chris Lattner7606fb62003-02-24 20:37:56 +0000660//===----------------------------------------------------------------------===//
Dan Gohmanf4362da2009-07-30 20:21:41 +0000661// ASTCallbackVH Class Implementation
662//===----------------------------------------------------------------------===//
663
664void AliasSetTracker::ASTCallbackVH::deleted() {
665 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
666 AST->deleteValue(getValPtr());
667 // this now dangles!
668}
669
Eli Friedman17822fc2011-04-09 06:55:46 +0000670void AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(Value *V) {
671 AST->copyValue(getValPtr(), V);
672}
673
Dan Gohmanf4362da2009-07-30 20:21:41 +0000674AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
Dan Gohmandc2b1b02009-07-31 18:21:48 +0000675 : CallbackVH(V), AST(ast) {}
676
677AliasSetTracker::ASTCallbackVH &
678AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
679 return *this = ASTCallbackVH(V, AST);
680}
Dan Gohmanf4362da2009-07-30 20:21:41 +0000681
682//===----------------------------------------------------------------------===//
Chris Lattner7606fb62003-02-24 20:37:56 +0000683// AliasSetPrinter Pass
684//===----------------------------------------------------------------------===//
685
686namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +0000687 class AliasSetPrinter : public FunctionPass {
Chris Lattner7606fb62003-02-24 20:37:56 +0000688 AliasSetTracker *Tracker;
689 public:
Nick Lewyckye7da2d62007-05-06 13:37:16 +0000690 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000691 AliasSetPrinter() : FunctionPass(ID) {
692 initializeAliasSetPrinterPass(*PassRegistry::getPassRegistry());
693 }
Devang Patel09f162c2007-05-01 21:15:47 +0000694
Craig Toppere9ba7592014-03-05 07:30:04 +0000695 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner7606fb62003-02-24 20:37:56 +0000696 AU.setPreservesAll();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000697 AU.addRequired<AAResultsWrapperPass>();
Chris Lattner7606fb62003-02-24 20:37:56 +0000698 }
699
Craig Toppere9ba7592014-03-05 07:30:04 +0000700 bool runOnFunction(Function &F) override {
Chandler Carruth7b560d42015-09-09 17:55:00 +0000701 auto &AAWP = getAnalysis<AAResultsWrapperPass>();
702 Tracker = new AliasSetTracker(AAWP.getAAResults());
Chris Lattner7606fb62003-02-24 20:37:56 +0000703
704 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner2d3a7a62004-04-27 15:13:33 +0000705 Tracker->add(&*I);
David Greene0295ecf2009-12-23 22:49:57 +0000706 Tracker->print(errs());
Chris Lattner44497852006-01-03 06:05:22 +0000707 delete Tracker;
Chris Lattner7606fb62003-02-24 20:37:56 +0000708 return false;
709 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000710 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000711}
Dan Gohmand78c4002008-05-13 00:00:25 +0000712
713char AliasSetPrinter::ID = 0;
Owen Anderson8ac477f2010-10-12 19:48:12 +0000714INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets",
715 "Alias Set Printer", false, true)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000716INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Owen Anderson8ac477f2010-10-12 19:48:12 +0000717INITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000718 "Alias Set Printer", false, true)