blob: 90923e81da54a94a78fa1b8c5633efcc74eb09f1 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AliasSetTracker and AliasSet classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Analysis/AliasSetTracker.h"
15#include "llvm/Analysis/AliasAnalysis.h"
16#include "llvm/Instructions.h"
Zhou Shengf74f7a82009-03-03 06:02:04 +000017#include "llvm/IntrinsicInst.h"
Dan Gohman83fd7aa2010-10-18 20:44:50 +000018#include "llvm/LLVMContext.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/Pass.h"
20#include "llvm/Type.h"
21#include "llvm/Target/TargetData.h"
22#include "llvm/Assembly/Writer.h"
David Greenee7ef6a92009-12-23 19:27:59 +000023#include "llvm/Support/Debug.h"
Edwin Török675d5622009-07-11 20:10:48 +000024#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/Support/InstIterator.h"
Chris Lattnera7a9daa2009-08-23 05:17:37 +000026#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027using namespace llvm;
28
29/// mergeSetIn - Merge the specified alias set into this alias set.
30///
31void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
32 assert(!AS.Forward && "Alias set is already forwarding!");
33 assert(!Forward && "This set is a forwarding set!!");
34
35 // Update the alias and access types of this set...
36 AccessTy |= AS.AccessTy;
37 AliasTy |= AS.AliasTy;
Chris Lattner7e9b6e92010-08-29 04:14:47 +000038 Volatile |= AS.Volatile;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039
40 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 Lattner47b3b8c2009-03-09 05:11:09 +000045 PointerRec *L = getSomePointer();
46 PointerRec *R = AS.getSomePointer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047
48 // If the pointers are not a must-alias pair, this set becomes a may alias.
Chris Lattner47b3b8c2009-03-09 05:11:09 +000049 if (AA.alias(L->getValue(), L->getSize(), R->getValue(), R->getSize())
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 != AliasAnalysis::MustAlias)
51 AliasTy = MayAlias;
52 }
53
54 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 }
61
62 AS.Forward = this; // Forward across AS now...
63 addRef(); // AS is now pointing to us...
64
65 // Merge the list of constituent pointers...
66 if (AS.PtrList) {
67 *PtrListEnd = AS.PtrList;
Chris Lattner47b3b8c2009-03-09 05:11:09 +000068 AS.PtrList->setPrevInList(PtrListEnd);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 PtrListEnd = AS.PtrListEnd;
70
71 AS.PtrList = 0;
72 AS.PtrListEnd = &AS.PtrList;
73 assert(*AS.PtrListEnd == 0 && "End of list is not null?");
74 }
75}
76
77void AliasSetTracker::removeAliasSet(AliasSet *AS) {
78 if (AliasSet *Fwd = AS->Forward) {
79 Fwd->dropRef(*this);
80 AS->Forward = 0;
81 }
82 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 Lattner47b3b8c2009-03-09 05:11:09 +000090void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
Dan Gohman83fd7aa2010-10-18 20:44:50 +000091 unsigned Size, const MDNode *TBAAInfo,
92 bool KnownMustAlias) {
Chris Lattner47b3b8c2009-03-09 05:11:09 +000093 assert(!Entry.hasAliasSet() && "Entry already in set!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094
95 // Check to see if we have to downgrade to _may_ alias.
96 if (isMustAlias() && !KnownMustAlias)
Chris Lattner47b3b8c2009-03-09 05:11:09 +000097 if (PointerRec *P = getSomePointer()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 AliasAnalysis &AA = AST.getAliasAnalysis();
99 AliasAnalysis::AliasResult Result =
Dan Gohman83fd7aa2010-10-18 20:44:50 +0000100 AA.alias(AliasAnalysis::Location(P->getValue(), P->getSize(),
101 P->getTBAAInfo()),
102 AliasAnalysis::Location(Entry.getValue(), Size, TBAAInfo));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103 if (Result == AliasAnalysis::MayAlias)
104 AliasTy = MayAlias;
105 else // First entry of must alias must have maximum size!
Dan Gohman83fd7aa2010-10-18 20:44:50 +0000106 P->updateSizeAndTBAAInfo(Size, TBAAInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
108 }
109
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000110 Entry.setAliasSet(this);
Dan Gohman83fd7aa2010-10-18 20:44:50 +0000111 Entry.updateSizeAndTBAAInfo(Size, TBAAInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112
113 // Add it to the end of the list...
114 assert(*PtrListEnd == 0 && "End of list is not null?");
115 *PtrListEnd = &Entry;
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000116 PtrListEnd = Entry.setPrevInList(PtrListEnd);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 assert(*PtrListEnd == 0 && "End of list is not null?");
Chris Lattner23dbc5b2010-08-29 04:13:43 +0000118 addRef(); // Entry points to alias set.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119}
120
121void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
Chris Lattnerb6e2dc92010-08-29 18:42:23 +0000122 CallSites.push_back(CS.getInstruction());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123
Duncan Sands00b24b52007-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;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 }
132
133 // FIXME: This should use mod/ref information to make this not suck so bad
134 AliasTy = MayAlias;
135 AccessTy = ModRef;
136}
137
138/// aliasesPointer - Return true if the specified pointer "may" (or must)
139/// alias one of the members in the set.
140///
141bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
Dan Gohman83fd7aa2010-10-18 20:44:50 +0000142 const MDNode *TBAAInfo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 AliasAnalysis &AA) const {
144 if (AliasTy == MustAlias) {
145 assert(CallSites.empty() && "Illegal must alias set!");
146
147 // If this is a set of MustAliases, only check to see if the pointer aliases
Chris Lattner23dbc5b2010-08-29 04:13:43 +0000148 // SOME value in the set.
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000149 PointerRec *SomePtr = getSomePointer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 assert(SomePtr && "Empty must-alias set??");
Dan Gohman83fd7aa2010-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));
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Gohman83fd7aa2010-10-18 20:44:50 +0000160 if (AA.alias(AliasAnalysis::Location(Ptr, Size, TBAAInfo),
Dan Gohman609cbe22010-10-18 21:28:00 +0000161 AliasAnalysis::Location(I.getPointer(), I.getSize(),
162 I.getTBAAInfo())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 return true;
164
165 // Check the call sites list and invoke list...
166 if (!CallSites.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
Dan Gohman83fd7aa2010-10-18 20:44:50 +0000168 if (AA.getModRefInfo(CallSites[i],
169 AliasAnalysis::Location(Ptr, Size, TBAAInfo)) !=
170 AliasAnalysis::NoModRef)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 return true;
172 }
173
174 return false;
175}
176
177bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
Duncan Sands00b24b52007-12-01 07:51:45 +0000178 if (AA.doesNotAccessMemory(CS))
179 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180
Chris Lattnerb6e2dc92010-08-29 18:42:23 +0000181 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
182 if (AA.getModRefInfo(getCallSite(i), CS) != AliasAnalysis::NoModRef ||
183 AA.getModRefInfo(CS, getCallSite(i)) != AliasAnalysis::NoModRef)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 return true;
Chris Lattnerb6e2dc92010-08-29 18:42:23 +0000185 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186
187 for (iterator I = begin(), E = end(); I != E; ++I)
188 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
189 AliasAnalysis::NoModRef)
190 return true;
191
192 return false;
193}
194
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000195void AliasSetTracker::clear() {
196 // Delete all the PointerRec entries.
Dan Gohman63b64a72009-07-30 20:21:41 +0000197 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
198 I != E; ++I)
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000199 I->second->eraseFromList();
200
201 PointerMap.clear();
202
203 // The alias sets should all be clear now.
204 AliasSets.clear();
205}
206
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207
208/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
209/// instruction referring to the pointer into. If there are multiple alias sets
210/// that may alias the pointer, merge them together and return the unified set.
211///
212AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
Dan Gohman83fd7aa2010-10-18 20:44:50 +0000213 unsigned Size,
214 const MDNode *TBAAInfo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 AliasSet *FoundSet = 0;
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000216 for (iterator I = begin(), E = end(); I != E; ++I) {
Dan Gohman83fd7aa2010-10-18 20:44:50 +0000217 if (I->Forward || !I->aliasesPointer(Ptr, Size, TBAAInfo, AA)) continue;
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000218
219 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
220 FoundSet = I; // Remember it.
221 } else { // Otherwise, we must merge the sets.
222 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 }
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000224 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225
226 return FoundSet;
227}
228
229/// containsPointer - Return true if the specified location is represented by
230/// this alias set, false otherwise. This does not modify the AST object or
231/// alias sets.
Dan Gohman83fd7aa2010-10-18 20:44:50 +0000232bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size,
233 const MDNode *TBAAInfo) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 for (const_iterator I = begin(), E = end(); I != E; ++I)
Dan Gohman83fd7aa2010-10-18 20:44:50 +0000235 if (!I->Forward && I->aliasesPointer(Ptr, Size, TBAAInfo, AA))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 return true;
237 return false;
238}
239
240
241
242AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
243 AliasSet *FoundSet = 0;
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000244 for (iterator I = begin(), E = end(); I != E; ++I) {
245 if (I->Forward || !I->aliasesCallSite(CS, AA))
246 continue;
247
Chris Lattnerb6e2dc92010-08-29 18:42:23 +0000248 if (FoundSet == 0) // If this is the first alias set ptr can go into.
249 FoundSet = I; // Remember it.
250 else if (!I->Forward) // Otherwise, we must merge the sets.
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000251 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000252 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 return FoundSet;
254}
255
256
257
258
259/// getAliasSetForPointer - Return the alias set that the specified pointer
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000260/// lives in.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
Dan Gohman83fd7aa2010-10-18 20:44:50 +0000262 const MDNode *TBAAInfo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 bool *New) {
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000264 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265
Chris Lattner23dbc5b2010-08-29 04:13:43 +0000266 // Check to see if the pointer is already known.
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000267 if (Entry.hasAliasSet()) {
Dan Gohman83fd7aa2010-10-18 20:44:50 +0000268 Entry.updateSizeAndTBAAInfo(Size, TBAAInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 // Return the set!
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000270 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000271 }
272
Dan Gohman83fd7aa2010-10-18 20:44:50 +0000273 if (AliasSet *AS = findAliasSetForPointer(Pointer, Size, TBAAInfo)) {
Chris Lattner23dbc5b2010-08-29 04:13:43 +0000274 // Add it to the alias set it aliases.
Dan Gohman83fd7aa2010-10-18 20:44:50 +0000275 AS->addPointer(*this, Entry, Size, TBAAInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 return *AS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 }
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000278
279 if (New) *New = true;
Chris Lattner23dbc5b2010-08-29 04:13:43 +0000280 // Otherwise create a new alias set to hold the loaded pointer.
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000281 AliasSets.push_back(new AliasSet());
Dan Gohman83fd7aa2010-10-18 20:44:50 +0000282 AliasSets.back().addPointer(*this, Entry, Size, TBAAInfo);
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000283 return AliasSets.back();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284}
285
Dan Gohman83fd7aa2010-10-18 20:44:50 +0000286bool AliasSetTracker::add(Value *Ptr, unsigned Size, const MDNode *TBAAInfo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 bool NewPtr;
Dan Gohman83fd7aa2010-10-18 20:44:50 +0000288 addPointer(Ptr, Size, TBAAInfo, AliasSet::NoModRef, NewPtr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 return NewPtr;
290}
291
292
293bool AliasSetTracker::add(LoadInst *LI) {
294 bool NewPtr;
295 AliasSet &AS = addPointer(LI->getOperand(0),
Dan Gohman624f9612009-07-25 00:48:42 +0000296 AA.getTypeStoreSize(LI->getType()),
Dan Gohman83fd7aa2010-10-18 20:44:50 +0000297 LI->getMetadata(LLVMContext::MD_tbaa),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 AliasSet::Refs, NewPtr);
299 if (LI->isVolatile()) AS.setVolatile();
300 return NewPtr;
301}
302
303bool AliasSetTracker::add(StoreInst *SI) {
304 bool NewPtr;
305 Value *Val = SI->getOperand(0);
306 AliasSet &AS = addPointer(SI->getOperand(1),
Dan Gohman624f9612009-07-25 00:48:42 +0000307 AA.getTypeStoreSize(Val->getType()),
Dan Gohman83fd7aa2010-10-18 20:44:50 +0000308 SI->getMetadata(LLVMContext::MD_tbaa),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309 AliasSet::Mods, NewPtr);
310 if (SI->isVolatile()) AS.setVolatile();
311 return NewPtr;
312}
313
Dan Gohmanddcc8562008-04-14 18:34:50 +0000314bool AliasSetTracker::add(VAArgInst *VAAI) {
315 bool NewPtr;
Dan Gohman83fd7aa2010-10-18 20:44:50 +0000316 addPointer(VAAI->getOperand(0), AliasAnalysis::UnknownSize,
317 VAAI->getMetadata(LLVMContext::MD_tbaa),
318 AliasSet::ModRef, NewPtr);
Dan Gohmanddcc8562008-04-14 18:34:50 +0000319 return NewPtr;
320}
321
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322
323bool AliasSetTracker::add(CallSite CS) {
Zhou Shengf74f7a82009-03-03 06:02:04 +0000324 if (isa<DbgInfoIntrinsic>(CS.getInstruction()))
325 return true; // Ignore DbgInfo Intrinsics.
Duncan Sands00b24b52007-12-01 07:51:45 +0000326 if (AA.doesNotAccessMemory(CS))
327 return true; // doesn't alias anything
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328
329 AliasSet *AS = findAliasSetForCallSite(CS);
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000330 if (AS) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 AS->addCallSite(CS, AA);
332 return false;
333 }
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000334 AliasSets.push_back(new AliasSet());
335 AS = &AliasSets.back();
336 AS->addCallSite(CS, AA);
337 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338}
339
340bool AliasSetTracker::add(Instruction *I) {
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000341 // Dispatch to one of the other add methods.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000342 if (LoadInst *LI = dyn_cast<LoadInst>(I))
343 return add(LI);
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000344 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 return add(SI);
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000346 if (CallInst *CI = dyn_cast<CallInst>(I))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000347 return add(CI);
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000348 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349 return add(II);
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000350 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
Dan Gohmanddcc8562008-04-14 18:34:50 +0000351 return add(VAAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000352 return true;
353}
354
355void AliasSetTracker::add(BasicBlock &BB) {
356 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
357 add(I);
358}
359
360void AliasSetTracker::add(const AliasSetTracker &AST) {
361 assert(&AA == &AST.AA &&
362 "Merging AliasSetTracker objects with different Alias Analyses!");
363
364 // Loop over all of the alias sets in AST, adding the pointers contained
365 // therein into the current alias sets. This can cause alias sets to be
366 // merged together in the current AST.
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000367 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I) {
368 if (I->Forward) continue; // Ignore forwarding alias sets
369
370 AliasSet &AS = const_cast<AliasSet&>(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000371
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000372 // If there are any call sites in the alias set, add them to this AST.
373 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
374 add(AS.CallSites[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000376 // Loop over all of the pointers in this alias set.
377 bool X;
378 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) {
379 AliasSet &NewAS = addPointer(ASI.getPointer(), ASI.getSize(),
Dan Gohman176c5322010-10-18 23:31:47 +0000380 ASI.getTBAAInfo(),
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000381 (AliasSet::AccessType)AS.AccessTy, X);
382 if (AS.isVolatile()) NewAS.setVolatile();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383 }
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000384 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385}
386
387/// remove - Remove the specified (potentially non-empty) alias set from the
388/// tracker.
389void AliasSetTracker::remove(AliasSet &AS) {
390 // Drop all call sites.
391 AS.CallSites.clear();
392
393 // Clear the alias set.
394 unsigned NumRefs = 0;
395 while (!AS.empty()) {
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000396 AliasSet::PointerRec *P = AS.PtrList;
397
398 Value *ValToRemove = P->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000400 // Unlink and delete entry from the list of values.
401 P->eraseFromList();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402
403 // Remember how many references need to be dropped.
404 ++NumRefs;
405
406 // Finally, remove the entry.
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000407 PointerMap.erase(ValToRemove);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000408 }
409
410 // Stop using the alias set, removing it.
411 AS.RefCount -= NumRefs;
412 if (AS.RefCount == 0)
413 AS.removeFromTracker(*this);
414}
415
Dan Gohman83fd7aa2010-10-18 20:44:50 +0000416bool
417AliasSetTracker::remove(Value *Ptr, unsigned Size, const MDNode *TBAAInfo) {
418 AliasSet *AS = findAliasSetForPointer(Ptr, Size, TBAAInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000419 if (!AS) return false;
420 remove(*AS);
421 return true;
422}
423
424bool AliasSetTracker::remove(LoadInst *LI) {
Dan Gohman624f9612009-07-25 00:48:42 +0000425 unsigned Size = AA.getTypeStoreSize(LI->getType());
Dan Gohman83fd7aa2010-10-18 20:44:50 +0000426 const MDNode *TBAAInfo = LI->getMetadata(LLVMContext::MD_tbaa);
427 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size, TBAAInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428 if (!AS) return false;
429 remove(*AS);
430 return true;
431}
432
433bool AliasSetTracker::remove(StoreInst *SI) {
Dan Gohman624f9612009-07-25 00:48:42 +0000434 unsigned Size = AA.getTypeStoreSize(SI->getOperand(0)->getType());
Dan Gohman83fd7aa2010-10-18 20:44:50 +0000435 const MDNode *TBAAInfo = SI->getMetadata(LLVMContext::MD_tbaa);
436 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size, TBAAInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437 if (!AS) return false;
438 remove(*AS);
439 return true;
440}
441
Dan Gohmanddcc8562008-04-14 18:34:50 +0000442bool AliasSetTracker::remove(VAArgInst *VAAI) {
Dan Gohman83fd7aa2010-10-18 20:44:50 +0000443 AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0),
444 AliasAnalysis::UnknownSize,
445 VAAI->getMetadata(LLVMContext::MD_tbaa));
Dan Gohmanddcc8562008-04-14 18:34:50 +0000446 if (!AS) return false;
447 remove(*AS);
448 return true;
449}
450
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451bool AliasSetTracker::remove(CallSite CS) {
Duncan Sands00b24b52007-12-01 07:51:45 +0000452 if (AA.doesNotAccessMemory(CS))
453 return false; // doesn't alias anything
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000454
455 AliasSet *AS = findAliasSetForCallSite(CS);
456 if (!AS) return false;
457 remove(*AS);
458 return true;
459}
460
461bool AliasSetTracker::remove(Instruction *I) {
462 // Dispatch to one of the other remove methods...
463 if (LoadInst *LI = dyn_cast<LoadInst>(I))
464 return remove(LI);
Chris Lattner23dbc5b2010-08-29 04:13:43 +0000465 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000466 return remove(SI);
Chris Lattner23dbc5b2010-08-29 04:13:43 +0000467 if (CallInst *CI = dyn_cast<CallInst>(I))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000468 return remove(CI);
Chris Lattner23dbc5b2010-08-29 04:13:43 +0000469 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
Dan Gohmanddcc8562008-04-14 18:34:50 +0000470 return remove(VAAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000471 return true;
472}
473
474
475// deleteValue method - This method is used to remove a pointer value from the
476// AliasSetTracker entirely. It should be used when an instruction is deleted
477// from the program to update the AST. If you don't use this, you would have
478// dangling pointers to deleted instructions.
479//
480void AliasSetTracker::deleteValue(Value *PtrVal) {
481 // Notify the alias analysis implementation that this value is gone.
482 AA.deleteValue(PtrVal);
483
484 // If this is a call instruction, remove the callsite from the appropriate
Chris Lattnerb6e2dc92010-08-29 18:42:23 +0000485 // AliasSet (if present).
486 if (CallSite CS = PtrVal) {
487 if (!AA.doesNotAccessMemory(CS)) {
488 // Scan all the alias sets to see if this call site is contained.
489 for (iterator I = begin(), E = end(); I != E; ++I) {
490 if (I->Forward) continue;
491
492 I->removeCallSite(CS);
493 }
494 }
495 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000496
497 // First, look up the PointerRec for this pointer.
Dan Gohman63b64a72009-07-30 20:21:41 +0000498 PointerMapType::iterator I = PointerMap.find(PtrVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000499 if (I == PointerMap.end()) return; // Noop
500
501 // If we found one, remove the pointer from the alias set it is in.
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000502 AliasSet::PointerRec *PtrValEnt = I->second;
503 AliasSet *AS = PtrValEnt->getAliasSet(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000504
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000505 // Unlink and delete from the list of values.
506 PtrValEnt->eraseFromList();
507
508 // Stop using the alias set.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000509 AS->dropRef(*this);
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000510
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000511 PointerMap.erase(I);
512}
513
514// copyValue - This method should be used whenever a preexisting value in the
515// program is copied or cloned, introducing a new value. Note that it is ok for
516// clients that use this method to introduce the same value multiple times: if
517// the tracker already knows about a value, it will ignore the request.
518//
519void AliasSetTracker::copyValue(Value *From, Value *To) {
520 // Notify the alias analysis implementation that this value is copied.
521 AA.copyValue(From, To);
522
523 // First, look up the PointerRec for this pointer.
Dan Gohman63b64a72009-07-30 20:21:41 +0000524 PointerMapType::iterator I = PointerMap.find(From);
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000525 if (I == PointerMap.end())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000526 return; // Noop
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000527 assert(I->second->hasAliasSet() && "Dead entry?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000528
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000529 AliasSet::PointerRec &Entry = getEntryFor(To);
530 if (Entry.hasAliasSet()) return; // Already in the tracker!
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000531
532 // Add it to the alias set it aliases...
Devang Patel5d11d432009-03-30 18:34:47 +0000533 I = PointerMap.find(From);
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000534 AliasSet *AS = I->second->getAliasSet(*this);
Dan Gohman609cbe22010-10-18 21:28:00 +0000535 AS->addPointer(*this, Entry, I->second->getSize(),
Dan Gohman176c5322010-10-18 23:31:47 +0000536 I->second->getTBAAInfo(),
Dan Gohman83fd7aa2010-10-18 20:44:50 +0000537 true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538}
539
540
541
542//===----------------------------------------------------------------------===//
543// AliasSet/AliasSetTracker Printing Support
544//===----------------------------------------------------------------------===//
545
Chris Lattnera7a9daa2009-08-23 05:17:37 +0000546void AliasSet::print(raw_ostream &OS) const {
Benjamin Kramer708c25a2010-08-30 14:46:53 +0000547 OS << " AliasSet[" << (void*)this << ", " << RefCount << "] ";
Dan Gohmancbec0762008-04-21 19:48:48 +0000548 OS << (AliasTy == MustAlias ? "must" : "may") << " alias, ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000549 switch (AccessTy) {
550 case NoModRef: OS << "No access "; break;
551 case Refs : OS << "Ref "; break;
552 case Mods : OS << "Mod "; break;
553 case ModRef : OS << "Mod/Ref "; break;
Edwin Törökbd448e32009-07-14 16:55:14 +0000554 default: llvm_unreachable("Bad value for AccessTy!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000555 }
556 if (isVolatile()) OS << "[volatile] ";
557 if (Forward)
558 OS << " forwarding to " << (void*)Forward;
559
560
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000561 if (!empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000562 OS << "Pointers: ";
563 for (iterator I = begin(), E = end(); I != E; ++I) {
564 if (I != begin()) OS << ", ";
565 WriteAsOperand(OS << "(", I.getPointer());
566 OS << ", " << I.getSize() << ")";
567 }
568 }
569 if (!CallSites.empty()) {
570 OS << "\n " << CallSites.size() << " Call Sites: ";
571 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
572 if (i) OS << ", ";
Chris Lattnerb6e2dc92010-08-29 18:42:23 +0000573 WriteAsOperand(OS, CallSites[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574 }
575 }
576 OS << "\n";
577}
578
Chris Lattnera7a9daa2009-08-23 05:17:37 +0000579void AliasSetTracker::print(raw_ostream &OS) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
581 << PointerMap.size() << " pointer values.\n";
582 for (const_iterator I = begin(), E = end(); I != E; ++I)
583 I->print(OS);
584 OS << "\n";
585}
586
David Greenee7ef6a92009-12-23 19:27:59 +0000587void AliasSet::dump() const { print(dbgs()); }
588void AliasSetTracker::dump() const { print(dbgs()); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000589
590//===----------------------------------------------------------------------===//
Dan Gohman63b64a72009-07-30 20:21:41 +0000591// ASTCallbackVH Class Implementation
592//===----------------------------------------------------------------------===//
593
594void AliasSetTracker::ASTCallbackVH::deleted() {
595 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
596 AST->deleteValue(getValPtr());
597 // this now dangles!
598}
599
600AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
Dan Gohman1e5054e2009-07-31 18:21:48 +0000601 : CallbackVH(V), AST(ast) {}
602
603AliasSetTracker::ASTCallbackVH &
604AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
605 return *this = ASTCallbackVH(V, AST);
606}
Dan Gohman63b64a72009-07-30 20:21:41 +0000607
608//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609// AliasSetPrinter Pass
610//===----------------------------------------------------------------------===//
611
612namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +0000613 class AliasSetPrinter : public FunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000614 AliasSetTracker *Tracker;
615 public:
616 static char ID; // Pass identification, replacement for typeid
Owen Andersona65d6a62010-10-19 17:21:58 +0000617 AliasSetPrinter() : FunctionPass(ID) {
618 initializeAliasSetPrinterPass(*PassRegistry::getPassRegistry());
619 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000620
621 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
622 AU.setPreservesAll();
623 AU.addRequired<AliasAnalysis>();
624 }
625
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000626 virtual bool runOnFunction(Function &F) {
627 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
628
629 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
630 Tracker->add(&*I);
David Greene092316b2009-12-23 22:49:57 +0000631 Tracker->print(errs());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000632 delete Tracker;
633 return false;
634 }
635 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000636}
Dan Gohman089efff2008-05-13 00:00:25 +0000637
638char AliasSetPrinter::ID = 0;
Owen Andersoncdb0c032010-10-12 19:48:12 +0000639INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets",
640 "Alias Set Printer", false, true)
641INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
642INITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets",
Owen Anderson1434dfa2010-10-07 22:25:06 +0000643 "Alias Set Printer", false, true)