blob: 0f13af6ab066ff2fc0ff5bf45e75f57e8cf71397 [file] [log] [blame]
Chris Lattner009cc3d2002-09-26 21:49:07 +00001//===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner009cc3d2002-09-26 21:49:07 +00009//
10// This file implements the AliasSetTracker and AliasSet classes.
Misha Brukman2b37d7c2005-04-21 21:13:18 +000011//
Chris Lattner009cc3d2002-09-26 21:49:07 +000012//===----------------------------------------------------------------------===//
13
14#include "llvm/Analysis/AliasSetTracker.h"
15#include "llvm/Analysis/AliasAnalysis.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000016#include "llvm/Instructions.h"
Zhou Sheng7e4286e2009-03-03 06:02:04 +000017#include "llvm/IntrinsicInst.h"
Dan Gohmana8702ea2010-10-18 20:44:50 +000018#include "llvm/LLVMContext.h"
Chris Lattner9971ac42003-02-24 20:37:56 +000019#include "llvm/Pass.h"
Chris Lattner5b3a4552005-03-17 15:38:16 +000020#include "llvm/Type.h"
Chris Lattner31a9d182003-02-26 22:11:00 +000021#include "llvm/Target/TargetData.h"
Chris Lattner9971ac42003-02-24 20:37:56 +000022#include "llvm/Assembly/Writer.h"
David Greene43c48ac2009-12-23 19:27:59 +000023#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000024#include "llvm/Support/ErrorHandling.h"
Chris Lattner9971ac42003-02-24 20:37:56 +000025#include "llvm/Support/InstIterator.h"
Chris Lattner791102f2009-08-23 05:17:37 +000026#include "llvm/Support/raw_ostream.h"
Chris Lattnere2609242003-12-14 04:52:11 +000027using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattnercc8d5242004-11-27 18:37:42 +000029/// mergeSetIn - Merge the specified alias set into this alias set.
Chris Lattner009cc3d2002-09-26 21:49:07 +000030///
Chris Lattnercc8d5242004-11-27 18:37:42 +000031void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
Chris Lattner9971ac42003-02-24 20:37:56 +000032 assert(!AS.Forward && "Alias set is already forwarding!");
33 assert(!Forward && "This set is a forwarding set!!");
Chris Lattner009cc3d2002-09-26 21:49:07 +000034
35 // Update the alias and access types of this set...
Chris Lattner9971ac42003-02-24 20:37:56 +000036 AccessTy |= AS.AccessTy;
37 AliasTy |= AS.AliasTy;
Chris Lattnerfedac7d2010-08-29 04:14:47 +000038 Volatile |= AS.Volatile;
Chris Lattner9971ac42003-02-24 20:37:56 +000039
Chris Lattnercc8d5242004-11-27 18:37:42 +000040 if (AliasTy == MustAlias) {
41 // 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 Lattnerd7168dd2009-03-09 05:11:09 +000045 PointerRec *L = getSomePointer();
46 PointerRec *R = AS.getSomePointer();
Chris Lattnercc8d5242004-11-27 18:37:42 +000047
48 // If the pointers are not a must-alias pair, this set becomes a may alias.
Chris Lattnerd7168dd2009-03-09 05:11:09 +000049 if (AA.alias(L->getValue(), L->getSize(), R->getValue(), R->getSize())
Chris Lattnercc8d5242004-11-27 18:37:42 +000050 != AliasAnalysis::MustAlias)
51 AliasTy = MayAlias;
52 }
53
Chris Lattner9971ac42003-02-24 20:37:56 +000054 if (CallSites.empty()) { // Merge call sites...
55 if (!AS.CallSites.empty())
56 std::swap(CallSites, AS.CallSites);
57 } else if (!AS.CallSites.empty()) {
58 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
59 AS.CallSites.clear();
60 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000061
Chris Lattner9971ac42003-02-24 20:37:56 +000062 AS.Forward = this; // Forward across AS now...
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000063 addRef(); // AS is now pointing to us...
Chris Lattner9971ac42003-02-24 20:37:56 +000064
65 // Merge the list of constituent pointers...
Chris Lattner2cffeec2003-12-18 08:11:56 +000066 if (AS.PtrList) {
67 *PtrListEnd = AS.PtrList;
Chris Lattnerd7168dd2009-03-09 05:11:09 +000068 AS.PtrList->setPrevInList(PtrListEnd);
Chris Lattner2cffeec2003-12-18 08:11:56 +000069 PtrListEnd = AS.PtrListEnd;
70
71 AS.PtrList = 0;
72 AS.PtrListEnd = &AS.PtrList;
Chris Lattner3080b602004-09-15 16:59:47 +000073 assert(*AS.PtrListEnd == 0 && "End of list is not null?");
Chris Lattner2cffeec2003-12-18 08:11:56 +000074 }
Chris Lattner009cc3d2002-09-26 21:49:07 +000075}
76
Chris Lattner9971ac42003-02-24 20:37:56 +000077void AliasSetTracker::removeAliasSet(AliasSet *AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000078 if (AliasSet *Fwd = AS->Forward) {
79 Fwd->dropRef(*this);
80 AS->Forward = 0;
81 }
Chris Lattner9971ac42003-02-24 20:37:56 +000082 AliasSets.erase(AS);
83}
84
85void AliasSet::removeFromTracker(AliasSetTracker &AST) {
86 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
87 AST.removeAliasSet(this);
88}
89
Chris Lattnerd7168dd2009-03-09 05:11:09 +000090void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
Dan Gohmana8702ea2010-10-18 20:44:50 +000091 unsigned Size, const MDNode *TBAAInfo,
92 bool KnownMustAlias) {
Chris Lattnerd7168dd2009-03-09 05:11:09 +000093 assert(!Entry.hasAliasSet() && "Entry already in set!");
Chris Lattner9971ac42003-02-24 20:37:56 +000094
Chris Lattnerb66e6482004-09-14 19:15:32 +000095 // Check to see if we have to downgrade to _may_ alias.
96 if (isMustAlias() && !KnownMustAlias)
Chris Lattnerd7168dd2009-03-09 05:11:09 +000097 if (PointerRec *P = getSomePointer()) {
Chris Lattner3080b602004-09-15 16:59:47 +000098 AliasAnalysis &AA = AST.getAliasAnalysis();
Chris Lattner31a9d182003-02-26 22:11:00 +000099 AliasAnalysis::AliasResult Result =
Dan Gohmana8702ea2010-10-18 20:44:50 +0000100 AA.alias(AliasAnalysis::Location(P->getValue(), P->getSize(),
101 P->getTBAAInfo()),
102 AliasAnalysis::Location(Entry.getValue(), Size, TBAAInfo));
Chris Lattner31a9d182003-02-26 22:11:00 +0000103 if (Result == AliasAnalysis::MayAlias)
Chris Lattner9971ac42003-02-24 20:37:56 +0000104 AliasTy = MayAlias;
Chris Lattner31a9d182003-02-26 22:11:00 +0000105 else // First entry of must alias must have maximum size!
Dan Gohmana8702ea2010-10-18 20:44:50 +0000106 P->updateSizeAndTBAAInfo(Size, TBAAInfo);
Chris Lattner31a9d182003-02-26 22:11:00 +0000107 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
108 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000109
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000110 Entry.setAliasSet(this);
Dan Gohmana8702ea2010-10-18 20:44:50 +0000111 Entry.updateSizeAndTBAAInfo(Size, TBAAInfo);
Chris Lattner9971ac42003-02-24 20:37:56 +0000112
113 // Add it to the end of the list...
Chris Lattner2cffeec2003-12-18 08:11:56 +0000114 assert(*PtrListEnd == 0 && "End of list is not null?");
115 *PtrListEnd = &Entry;
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000116 PtrListEnd = Entry.setPrevInList(PtrListEnd);
Chris Lattner3080b602004-09-15 16:59:47 +0000117 assert(*PtrListEnd == 0 && "End of list is not null?");
Chris Lattner9476d742010-08-29 04:13:43 +0000118 addRef(); // Entry points to alias set.
Chris Lattner9971ac42003-02-24 20:37:56 +0000119}
120
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000121void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
Chris Lattnercb7f6532010-08-29 18:42:23 +0000122 CallSites.push_back(CS.getInstruction());
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000123
Duncan Sandsdff67102007-12-01 07:51:45 +0000124 AliasAnalysis::ModRefBehavior Behavior = AA.getModRefBehavior(CS);
125 if (Behavior == AliasAnalysis::DoesNotAccessMemory)
126 return;
127 else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
128 AliasTy = MayAlias;
129 AccessTy |= Refs;
130 return;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000131 }
132
133 // FIXME: This should use mod/ref information to make this not suck so bad
134 AliasTy = MayAlias;
Chris Lattner577385e2003-05-03 03:42:08 +0000135 AccessTy = ModRef;
Chris Lattner9971ac42003-02-24 20:37:56 +0000136}
137
138/// aliasesPointer - Return true if the specified pointer "may" (or must)
Chris Lattner009cc3d2002-09-26 21:49:07 +0000139/// alias one of the members in the set.
140///
Chris Lattner31a9d182003-02-26 22:11:00 +0000141bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
Dan Gohmana8702ea2010-10-18 20:44:50 +0000142 const MDNode *TBAAInfo,
Chris Lattner31a9d182003-02-26 22:11:00 +0000143 AliasAnalysis &AA) const {
Chris Lattner9971ac42003-02-24 20:37:56 +0000144 if (AliasTy == MustAlias) {
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000145 assert(CallSites.empty() && "Illegal must alias set!");
146
Chris Lattner9971ac42003-02-24 20:37:56 +0000147 // If this is a set of MustAliases, only check to see if the pointer aliases
Chris Lattner9476d742010-08-29 04:13:43 +0000148 // SOME value in the set.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000149 PointerRec *SomePtr = getSomePointer();
Chris Lattner9971ac42003-02-24 20:37:56 +0000150 assert(SomePtr && "Empty must-alias set??");
Dan Gohmana8702ea2010-10-18 20:44:50 +0000151 return AA.alias(AliasAnalysis::Location(SomePtr->getValue(),
152 SomePtr->getSize(),
153 SomePtr->getTBAAInfo()),
154 AliasAnalysis::Location(Ptr, Size, TBAAInfo));
Chris Lattner9971ac42003-02-24 20:37:56 +0000155 }
156
157 // If this is a may-alias set, we have to check all of the pointers in the set
158 // to be sure it doesn't alias the set...
159 for (iterator I = begin(), E = end(); I != E; ++I)
Dan Gohmana8702ea2010-10-18 20:44:50 +0000160 if (AA.alias(AliasAnalysis::Location(Ptr, Size, TBAAInfo),
161 AliasAnalysis::Location(I.getPointer(), I.getSize(), I.getTBAAInfo())))
Chris Lattner9971ac42003-02-24 20:37:56 +0000162 return true;
163
164 // Check the call sites list and invoke list...
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000165 if (!CallSites.empty()) {
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000166 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
Dan Gohmana8702ea2010-10-18 20:44:50 +0000167 if (AA.getModRefInfo(CallSites[i],
168 AliasAnalysis::Location(Ptr, Size, TBAAInfo)) !=
169 AliasAnalysis::NoModRef)
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000170 return true;
171 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000172
Chris Lattner009cc3d2002-09-26 21:49:07 +0000173 return false;
174}
175
Chris Lattner9971ac42003-02-24 20:37:56 +0000176bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
Duncan Sandsdff67102007-12-01 07:51:45 +0000177 if (AA.doesNotAccessMemory(CS))
178 return false;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000179
Chris Lattnercb7f6532010-08-29 18:42:23 +0000180 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
181 if (AA.getModRefInfo(getCallSite(i), CS) != AliasAnalysis::NoModRef ||
182 AA.getModRefInfo(CS, getCallSite(i)) != AliasAnalysis::NoModRef)
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000183 return true;
Chris Lattnercb7f6532010-08-29 18:42:23 +0000184 }
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000185
186 for (iterator I = begin(), E = end(); I != E; ++I)
187 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
188 AliasAnalysis::NoModRef)
189 return true;
190
191 return false;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000192}
193
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000194void AliasSetTracker::clear() {
195 // Delete all the PointerRec entries.
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000196 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
197 I != E; ++I)
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000198 I->second->eraseFromList();
199
200 PointerMap.clear();
201
202 // The alias sets should all be clear now.
203 AliasSets.clear();
204}
205
Chris Lattner009cc3d2002-09-26 21:49:07 +0000206
Chris Lattner009cc3d2002-09-26 21:49:07 +0000207/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
208/// instruction referring to the pointer into. If there are multiple alias sets
209/// that may alias the pointer, merge them together and return the unified set.
210///
Chris Lattner31a9d182003-02-26 22:11:00 +0000211AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
Dan Gohmana8702ea2010-10-18 20:44:50 +0000212 unsigned Size,
213 const MDNode *TBAAInfo) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000214 AliasSet *FoundSet = 0;
Chris Lattner6e1f5102010-08-29 04:06:55 +0000215 for (iterator I = begin(), E = end(); I != E; ++I) {
Dan Gohmana8702ea2010-10-18 20:44:50 +0000216 if (I->Forward || !I->aliasesPointer(Ptr, Size, TBAAInfo, AA)) continue;
Chris Lattner6e1f5102010-08-29 04:06:55 +0000217
218 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
219 FoundSet = I; // Remember it.
220 } else { // Otherwise, we must merge the sets.
221 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
Chris Lattner009cc3d2002-09-26 21:49:07 +0000222 }
Chris Lattner6e1f5102010-08-29 04:06:55 +0000223 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000224
225 return FoundSet;
226}
227
Chris Lattner07bfa522004-11-26 21:36:25 +0000228/// containsPointer - Return true if the specified location is represented by
229/// this alias set, false otherwise. This does not modify the AST object or
230/// alias sets.
Dan Gohmana8702ea2010-10-18 20:44:50 +0000231bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size,
232 const MDNode *TBAAInfo) const {
Chris Lattner07bfa522004-11-26 21:36:25 +0000233 for (const_iterator I = begin(), E = end(); I != E; ++I)
Dan Gohmana8702ea2010-10-18 20:44:50 +0000234 if (!I->Forward && I->aliasesPointer(Ptr, Size, TBAAInfo, AA))
Chris Lattner07bfa522004-11-26 21:36:25 +0000235 return true;
236 return false;
237}
238
239
240
Chris Lattner9971ac42003-02-24 20:37:56 +0000241AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
242 AliasSet *FoundSet = 0;
Chris Lattner6e1f5102010-08-29 04:06:55 +0000243 for (iterator I = begin(), E = end(); I != E; ++I) {
244 if (I->Forward || !I->aliasesCallSite(CS, AA))
245 continue;
246
Chris Lattnercb7f6532010-08-29 18:42:23 +0000247 if (FoundSet == 0) // If this is the first alias set ptr can go into.
248 FoundSet = I; // Remember it.
249 else if (!I->Forward) // Otherwise, we must merge the sets.
Chris Lattner6e1f5102010-08-29 04:06:55 +0000250 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
Chris Lattner6e1f5102010-08-29 04:06:55 +0000251 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000252 return FoundSet;
253}
254
255
Chris Lattner009cc3d2002-09-26 21:49:07 +0000256
Chris Lattner9971ac42003-02-24 20:37:56 +0000257
258/// getAliasSetForPointer - Return the alias set that the specified pointer
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000259/// lives in.
Chris Lattner12c11552004-07-21 05:18:04 +0000260AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
Dan Gohmana8702ea2010-10-18 20:44:50 +0000261 const MDNode *TBAAInfo,
Chris Lattner12c11552004-07-21 05:18:04 +0000262 bool *New) {
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000263 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
Chris Lattner9971ac42003-02-24 20:37:56 +0000264
Chris Lattner9476d742010-08-29 04:13:43 +0000265 // Check to see if the pointer is already known.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000266 if (Entry.hasAliasSet()) {
Dan Gohmana8702ea2010-10-18 20:44:50 +0000267 Entry.updateSizeAndTBAAInfo(Size, TBAAInfo);
Chris Lattner9971ac42003-02-24 20:37:56 +0000268 // Return the set!
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000269 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000270 }
271
Dan Gohmana8702ea2010-10-18 20:44:50 +0000272 if (AliasSet *AS = findAliasSetForPointer(Pointer, Size, TBAAInfo)) {
Chris Lattner9476d742010-08-29 04:13:43 +0000273 // Add it to the alias set it aliases.
Dan Gohmana8702ea2010-10-18 20:44:50 +0000274 AS->addPointer(*this, Entry, Size, TBAAInfo);
Chris Lattner9971ac42003-02-24 20:37:56 +0000275 return *AS;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000276 }
Chris Lattner6e1f5102010-08-29 04:06:55 +0000277
278 if (New) *New = true;
Chris Lattner9476d742010-08-29 04:13:43 +0000279 // Otherwise create a new alias set to hold the loaded pointer.
Chris Lattner6e1f5102010-08-29 04:06:55 +0000280 AliasSets.push_back(new AliasSet());
Dan Gohmana8702ea2010-10-18 20:44:50 +0000281 AliasSets.back().addPointer(*this, Entry, Size, TBAAInfo);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000282 return AliasSets.back();
Chris Lattner009cc3d2002-09-26 21:49:07 +0000283}
284
Dan Gohmana8702ea2010-10-18 20:44:50 +0000285bool AliasSetTracker::add(Value *Ptr, unsigned Size, const MDNode *TBAAInfo) {
Chris Lattner61d46272004-07-26 05:50:23 +0000286 bool NewPtr;
Dan Gohmana8702ea2010-10-18 20:44:50 +0000287 addPointer(Ptr, Size, TBAAInfo, AliasSet::NoModRef, NewPtr);
Chris Lattner61d46272004-07-26 05:50:23 +0000288 return NewPtr;
289}
290
291
Chris Lattner12c11552004-07-21 05:18:04 +0000292bool AliasSetTracker::add(LoadInst *LI) {
293 bool NewPtr;
294 AliasSet &AS = addPointer(LI->getOperand(0),
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000295 AA.getTypeStoreSize(LI->getType()),
Dan Gohmana8702ea2010-10-18 20:44:50 +0000296 LI->getMetadata(LLVMContext::MD_tbaa),
Chris Lattner12c11552004-07-21 05:18:04 +0000297 AliasSet::Refs, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000298 if (LI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000299 return NewPtr;
Chris Lattner9971ac42003-02-24 20:37:56 +0000300}
301
Chris Lattner12c11552004-07-21 05:18:04 +0000302bool AliasSetTracker::add(StoreInst *SI) {
303 bool NewPtr;
304 Value *Val = SI->getOperand(0);
305 AliasSet &AS = addPointer(SI->getOperand(1),
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000306 AA.getTypeStoreSize(Val->getType()),
Dan Gohmana8702ea2010-10-18 20:44:50 +0000307 SI->getMetadata(LLVMContext::MD_tbaa),
Chris Lattner12c11552004-07-21 05:18:04 +0000308 AliasSet::Mods, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000309 if (SI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000310 return NewPtr;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000311}
312
Dan Gohman235fc572008-04-14 18:34:50 +0000313bool AliasSetTracker::add(VAArgInst *VAAI) {
314 bool NewPtr;
Dan Gohmana8702ea2010-10-18 20:44:50 +0000315 addPointer(VAAI->getOperand(0), AliasAnalysis::UnknownSize,
316 VAAI->getMetadata(LLVMContext::MD_tbaa),
317 AliasSet::ModRef, NewPtr);
Dan Gohman235fc572008-04-14 18:34:50 +0000318 return NewPtr;
319}
320
Chris Lattnere2609242003-12-14 04:52:11 +0000321
Chris Lattner12c11552004-07-21 05:18:04 +0000322bool AliasSetTracker::add(CallSite CS) {
Zhou Sheng7e4286e2009-03-03 06:02:04 +0000323 if (isa<DbgInfoIntrinsic>(CS.getInstruction()))
324 return true; // Ignore DbgInfo Intrinsics.
Duncan Sandsdff67102007-12-01 07:51:45 +0000325 if (AA.doesNotAccessMemory(CS))
326 return true; // doesn't alias anything
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000327
Chris Lattner9971ac42003-02-24 20:37:56 +0000328 AliasSet *AS = findAliasSetForCallSite(CS);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000329 if (AS) {
Chris Lattner12c11552004-07-21 05:18:04 +0000330 AS->addCallSite(CS, AA);
331 return false;
Chris Lattner9971ac42003-02-24 20:37:56 +0000332 }
Chris Lattner6e1f5102010-08-29 04:06:55 +0000333 AliasSets.push_back(new AliasSet());
334 AS = &AliasSets.back();
335 AS->addCallSite(CS, AA);
336 return true;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000337}
338
Chris Lattner12c11552004-07-21 05:18:04 +0000339bool AliasSetTracker::add(Instruction *I) {
Chris Lattner6e1f5102010-08-29 04:06:55 +0000340 // Dispatch to one of the other add methods.
Chris Lattner9971ac42003-02-24 20:37:56 +0000341 if (LoadInst *LI = dyn_cast<LoadInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000342 return add(LI);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000343 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000344 return add(SI);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000345 if (CallInst *CI = dyn_cast<CallInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000346 return add(CI);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000347 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000348 return add(II);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000349 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
Dan Gohman235fc572008-04-14 18:34:50 +0000350 return add(VAAI);
Chris Lattner12c11552004-07-21 05:18:04 +0000351 return true;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000352}
353
Chris Lattner319d05b2003-03-03 23:28:05 +0000354void AliasSetTracker::add(BasicBlock &BB) {
355 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
356 add(I);
357}
358
359void AliasSetTracker::add(const AliasSetTracker &AST) {
360 assert(&AA == &AST.AA &&
361 "Merging AliasSetTracker objects with different Alias Analyses!");
362
363 // Loop over all of the alias sets in AST, adding the pointers contained
364 // therein into the current alias sets. This can cause alias sets to be
365 // merged together in the current AST.
Chris Lattner6e1f5102010-08-29 04:06:55 +0000366 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I) {
367 if (I->Forward) continue; // Ignore forwarding alias sets
368
369 AliasSet &AS = const_cast<AliasSet&>(*I);
Chris Lattner319d05b2003-03-03 23:28:05 +0000370
Chris Lattner6e1f5102010-08-29 04:06:55 +0000371 // If there are any call sites in the alias set, add them to this AST.
372 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
373 add(AS.CallSites[i]);
Chris Lattner319d05b2003-03-03 23:28:05 +0000374
Chris Lattner6e1f5102010-08-29 04:06:55 +0000375 // Loop over all of the pointers in this alias set.
376 bool X;
377 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) {
378 AliasSet &NewAS = addPointer(ASI.getPointer(), ASI.getSize(),
Dan Gohmana8702ea2010-10-18 20:44:50 +0000379 ASI.getTBAAInfo(),
Chris Lattner6e1f5102010-08-29 04:06:55 +0000380 (AliasSet::AccessType)AS.AccessTy, X);
381 if (AS.isVolatile()) NewAS.setVolatile();
Chris Lattner319d05b2003-03-03 23:28:05 +0000382 }
Chris Lattner6e1f5102010-08-29 04:06:55 +0000383 }
Chris Lattner319d05b2003-03-03 23:28:05 +0000384}
385
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000386/// remove - Remove the specified (potentially non-empty) alias set from the
387/// tracker.
388void AliasSetTracker::remove(AliasSet &AS) {
Chris Lattnerf2998572006-06-27 23:48:59 +0000389 // Drop all call sites.
390 AS.CallSites.clear();
391
392 // Clear the alias set.
393 unsigned NumRefs = 0;
394 while (!AS.empty()) {
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000395 AliasSet::PointerRec *P = AS.PtrList;
396
397 Value *ValToRemove = P->getValue();
Chris Lattnerf2998572006-06-27 23:48:59 +0000398
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000399 // Unlink and delete entry from the list of values.
400 P->eraseFromList();
Chris Lattnerf2998572006-06-27 23:48:59 +0000401
402 // Remember how many references need to be dropped.
403 ++NumRefs;
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000404
Chris Lattnerf2998572006-06-27 23:48:59 +0000405 // Finally, remove the entry.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000406 PointerMap.erase(ValToRemove);
Chris Lattnerf2998572006-06-27 23:48:59 +0000407 }
408
409 // Stop using the alias set, removing it.
Chris Lattner356d8c22006-06-27 23:56:13 +0000410 AS.RefCount -= NumRefs;
411 if (AS.RefCount == 0)
412 AS.removeFromTracker(*this);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000413}
414
Dan Gohmana8702ea2010-10-18 20:44:50 +0000415bool
416AliasSetTracker::remove(Value *Ptr, unsigned Size, const MDNode *TBAAInfo) {
417 AliasSet *AS = findAliasSetForPointer(Ptr, Size, TBAAInfo);
Chris Lattner61d46272004-07-26 05:50:23 +0000418 if (!AS) return false;
419 remove(*AS);
420 return true;
421}
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000422
423bool AliasSetTracker::remove(LoadInst *LI) {
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000424 unsigned Size = AA.getTypeStoreSize(LI->getType());
Dan Gohmana8702ea2010-10-18 20:44:50 +0000425 const MDNode *TBAAInfo = LI->getMetadata(LLVMContext::MD_tbaa);
426 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size, TBAAInfo);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000427 if (!AS) return false;
428 remove(*AS);
429 return true;
430}
431
432bool AliasSetTracker::remove(StoreInst *SI) {
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000433 unsigned Size = AA.getTypeStoreSize(SI->getOperand(0)->getType());
Dan Gohmana8702ea2010-10-18 20:44:50 +0000434 const MDNode *TBAAInfo = SI->getMetadata(LLVMContext::MD_tbaa);
435 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size, TBAAInfo);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000436 if (!AS) return false;
437 remove(*AS);
438 return true;
439}
440
Dan Gohman235fc572008-04-14 18:34:50 +0000441bool AliasSetTracker::remove(VAArgInst *VAAI) {
Dan Gohmana8702ea2010-10-18 20:44:50 +0000442 AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0),
443 AliasAnalysis::UnknownSize,
444 VAAI->getMetadata(LLVMContext::MD_tbaa));
Dan Gohman235fc572008-04-14 18:34:50 +0000445 if (!AS) return false;
446 remove(*AS);
447 return true;
448}
449
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000450bool AliasSetTracker::remove(CallSite CS) {
Duncan Sandsdff67102007-12-01 07:51:45 +0000451 if (AA.doesNotAccessMemory(CS))
452 return false; // doesn't alias anything
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000453
454 AliasSet *AS = findAliasSetForCallSite(CS);
455 if (!AS) return false;
456 remove(*AS);
457 return true;
458}
459
460bool AliasSetTracker::remove(Instruction *I) {
461 // Dispatch to one of the other remove methods...
462 if (LoadInst *LI = dyn_cast<LoadInst>(I))
463 return remove(LI);
Chris Lattner9476d742010-08-29 04:13:43 +0000464 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000465 return remove(SI);
Chris Lattner9476d742010-08-29 04:13:43 +0000466 if (CallInst *CI = dyn_cast<CallInst>(I))
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000467 return remove(CI);
Chris Lattner9476d742010-08-29 04:13:43 +0000468 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
Dan Gohman235fc572008-04-14 18:34:50 +0000469 return remove(VAAI);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000470 return true;
471}
472
Chris Lattner2cffeec2003-12-18 08:11:56 +0000473
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000474// deleteValue method - This method is used to remove a pointer value from the
Chris Lattner2cffeec2003-12-18 08:11:56 +0000475// AliasSetTracker entirely. It should be used when an instruction is deleted
476// from the program to update the AST. If you don't use this, you would have
477// dangling pointers to deleted instructions.
478//
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000479void AliasSetTracker::deleteValue(Value *PtrVal) {
Chris Lattnerb66e6482004-09-14 19:15:32 +0000480 // Notify the alias analysis implementation that this value is gone.
481 AA.deleteValue(PtrVal);
482
Chris Lattner959e3212006-06-26 19:20:48 +0000483 // If this is a call instruction, remove the callsite from the appropriate
Chris Lattnercb7f6532010-08-29 18:42:23 +0000484 // AliasSet (if present).
485 if (CallSite CS = PtrVal) {
486 if (!AA.doesNotAccessMemory(CS)) {
487 // Scan all the alias sets to see if this call site is contained.
488 for (iterator I = begin(), E = end(); I != E; ++I) {
489 if (I->Forward) continue;
490
491 I->removeCallSite(CS);
492 }
493 }
494 }
Chris Lattner959e3212006-06-26 19:20:48 +0000495
Chris Lattnerb66e6482004-09-14 19:15:32 +0000496 // First, look up the PointerRec for this pointer.
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000497 PointerMapType::iterator I = PointerMap.find(PtrVal);
Chris Lattner2cffeec2003-12-18 08:11:56 +0000498 if (I == PointerMap.end()) return; // Noop
499
500 // If we found one, remove the pointer from the alias set it is in.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000501 AliasSet::PointerRec *PtrValEnt = I->second;
502 AliasSet *AS = PtrValEnt->getAliasSet(*this);
Chris Lattner2cffeec2003-12-18 08:11:56 +0000503
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000504 // Unlink and delete from the list of values.
505 PtrValEnt->eraseFromList();
506
507 // Stop using the alias set.
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000508 AS->dropRef(*this);
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000509
Chris Lattner2cffeec2003-12-18 08:11:56 +0000510 PointerMap.erase(I);
511}
512
Chris Lattnerb66e6482004-09-14 19:15:32 +0000513// copyValue - This method should be used whenever a preexisting value in the
514// program is copied or cloned, introducing a new value. Note that it is ok for
515// clients that use this method to introduce the same value multiple times: if
516// the tracker already knows about a value, it will ignore the request.
517//
518void AliasSetTracker::copyValue(Value *From, Value *To) {
519 // Notify the alias analysis implementation that this value is copied.
520 AA.copyValue(From, To);
521
522 // First, look up the PointerRec for this pointer.
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000523 PointerMapType::iterator I = PointerMap.find(From);
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000524 if (I == PointerMap.end())
Chris Lattnerb66e6482004-09-14 19:15:32 +0000525 return; // Noop
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000526 assert(I->second->hasAliasSet() && "Dead entry?");
Chris Lattnerb66e6482004-09-14 19:15:32 +0000527
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000528 AliasSet::PointerRec &Entry = getEntryFor(To);
529 if (Entry.hasAliasSet()) return; // Already in the tracker!
Chris Lattnerb66e6482004-09-14 19:15:32 +0000530
531 // Add it to the alias set it aliases...
Devang Patel6d9a2df2009-03-30 18:34:47 +0000532 I = PointerMap.find(From);
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000533 AliasSet *AS = I->second->getAliasSet(*this);
Dan Gohmana8702ea2010-10-18 20:44:50 +0000534 AS->addPointer(*this, Entry, I->second->getSize(), I->second->getTBAAInfo(),
535 true);
Chris Lattnerb66e6482004-09-14 19:15:32 +0000536}
537
538
Chris Lattner2cffeec2003-12-18 08:11:56 +0000539
Chris Lattner9971ac42003-02-24 20:37:56 +0000540//===----------------------------------------------------------------------===//
541// AliasSet/AliasSetTracker Printing Support
542//===----------------------------------------------------------------------===//
543
Chris Lattner791102f2009-08-23 05:17:37 +0000544void AliasSet::print(raw_ostream &OS) const {
Benjamin Kramer03392742010-08-30 14:46:53 +0000545 OS << " AliasSet[" << (void*)this << ", " << RefCount << "] ";
Dan Gohman92c7b652008-04-21 19:48:48 +0000546 OS << (AliasTy == MustAlias ? "must" : "may") << " alias, ";
Chris Lattner9971ac42003-02-24 20:37:56 +0000547 switch (AccessTy) {
548 case NoModRef: OS << "No access "; break;
549 case Refs : OS << "Ref "; break;
550 case Mods : OS << "Mod "; break;
551 case ModRef : OS << "Mod/Ref "; break;
Torok Edwinc23197a2009-07-14 16:55:14 +0000552 default: llvm_unreachable("Bad value for AccessTy!");
Chris Lattner009cc3d2002-09-26 21:49:07 +0000553 }
Chris Lattnere2609242003-12-14 04:52:11 +0000554 if (isVolatile()) OS << "[volatile] ";
Chris Lattner9971ac42003-02-24 20:37:56 +0000555 if (Forward)
556 OS << " forwarding to " << (void*)Forward;
557
558
Dan Gohmancb406c22007-10-03 19:26:29 +0000559 if (!empty()) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000560 OS << "Pointers: ";
561 for (iterator I = begin(), E = end(); I != E; ++I) {
562 if (I != begin()) OS << ", ";
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000563 WriteAsOperand(OS << "(", I.getPointer());
564 OS << ", " << I.getSize() << ")";
Chris Lattner9971ac42003-02-24 20:37:56 +0000565 }
566 }
567 if (!CallSites.empty()) {
568 OS << "\n " << CallSites.size() << " Call Sites: ";
569 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
570 if (i) OS << ", ";
Chris Lattnercb7f6532010-08-29 18:42:23 +0000571 WriteAsOperand(OS, CallSites[i]);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000572 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000573 }
574 OS << "\n";
575}
576
Chris Lattner791102f2009-08-23 05:17:37 +0000577void AliasSetTracker::print(raw_ostream &OS) const {
Chris Lattner9971ac42003-02-24 20:37:56 +0000578 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
579 << PointerMap.size() << " pointer values.\n";
580 for (const_iterator I = begin(), E = end(); I != E; ++I)
581 I->print(OS);
582 OS << "\n";
583}
584
David Greene43c48ac2009-12-23 19:27:59 +0000585void AliasSet::dump() const { print(dbgs()); }
586void AliasSetTracker::dump() const { print(dbgs()); }
Chris Lattner9971ac42003-02-24 20:37:56 +0000587
Chris Lattner9971ac42003-02-24 20:37:56 +0000588//===----------------------------------------------------------------------===//
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000589// ASTCallbackVH Class Implementation
590//===----------------------------------------------------------------------===//
591
592void AliasSetTracker::ASTCallbackVH::deleted() {
593 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
594 AST->deleteValue(getValPtr());
595 // this now dangles!
596}
597
598AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
Dan Gohmana818c302009-07-31 18:21:48 +0000599 : CallbackVH(V), AST(ast) {}
600
601AliasSetTracker::ASTCallbackVH &
602AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
603 return *this = ASTCallbackVH(V, AST);
604}
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000605
606//===----------------------------------------------------------------------===//
Chris Lattner9971ac42003-02-24 20:37:56 +0000607// AliasSetPrinter Pass
608//===----------------------------------------------------------------------===//
609
610namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000611 class AliasSetPrinter : public FunctionPass {
Chris Lattner9971ac42003-02-24 20:37:56 +0000612 AliasSetTracker *Tracker;
613 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +0000614 static char ID; // Pass identification, replacement for typeid
Owen Anderson90c579d2010-08-06 18:33:48 +0000615 AliasSetPrinter() : FunctionPass(ID) {}
Devang Patel794fd752007-05-01 21:15:47 +0000616
Chris Lattner9971ac42003-02-24 20:37:56 +0000617 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
618 AU.setPreservesAll();
619 AU.addRequired<AliasAnalysis>();
620 }
621
622 virtual bool runOnFunction(Function &F) {
623 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
624
625 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner6ffe5512004-04-27 15:13:33 +0000626 Tracker->add(&*I);
David Greene4850a892009-12-23 22:49:57 +0000627 Tracker->print(errs());
Chris Lattner4983cf72006-01-03 06:05:22 +0000628 delete Tracker;
Chris Lattner9971ac42003-02-24 20:37:56 +0000629 return false;
630 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000631 };
Chris Lattner009cc3d2002-09-26 21:49:07 +0000632}
Dan Gohman844731a2008-05-13 00:00:25 +0000633
634char AliasSetPrinter::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +0000635INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets",
636 "Alias Set Printer", false, true)
637INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
638INITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets",
Owen Andersonce665bd2010-10-07 22:25:06 +0000639 "Alias Set Printer", false, true)