blob: d52085e575447c9dfbd7d0c54609821cf2f2b36c [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 Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/Pass.h"
19#include "llvm/Type.h"
20#include "llvm/Target/TargetData.h"
21#include "llvm/Assembly/Writer.h"
David Greenee7ef6a92009-12-23 19:27:59 +000022#include "llvm/Support/Debug.h"
Edwin Török675d5622009-07-11 20:10:48 +000023#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include "llvm/Support/InstIterator.h"
Chris Lattnera7a9daa2009-08-23 05:17:37 +000025#include "llvm/Support/Format.h"
26#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 Gohmanf17a25c2007-07-18 16:29:46 +000091 unsigned Size, bool KnownMustAlias) {
Chris Lattner47b3b8c2009-03-09 05:11:09 +000092 assert(!Entry.hasAliasSet() && "Entry already in set!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093
94 // Check to see if we have to downgrade to _may_ alias.
95 if (isMustAlias() && !KnownMustAlias)
Chris Lattner47b3b8c2009-03-09 05:11:09 +000096 if (PointerRec *P = getSomePointer()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 AliasAnalysis &AA = AST.getAliasAnalysis();
98 AliasAnalysis::AliasResult Result =
Chris Lattner47b3b8c2009-03-09 05:11:09 +000099 AA.alias(P->getValue(), P->getSize(), Entry.getValue(), Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100 if (Result == AliasAnalysis::MayAlias)
101 AliasTy = MayAlias;
102 else // First entry of must alias must have maximum size!
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000103 P->updateSize(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
105 }
106
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000107 Entry.setAliasSet(this);
108 Entry.updateSize(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109
110 // Add it to the end of the list...
111 assert(*PtrListEnd == 0 && "End of list is not null?");
112 *PtrListEnd = &Entry;
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000113 PtrListEnd = Entry.setPrevInList(PtrListEnd);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 assert(*PtrListEnd == 0 && "End of list is not null?");
Chris Lattner23dbc5b2010-08-29 04:13:43 +0000115 addRef(); // Entry points to alias set.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116}
117
118void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
119 CallSites.push_back(CS);
120
Duncan Sands00b24b52007-12-01 07:51:45 +0000121 AliasAnalysis::ModRefBehavior Behavior = AA.getModRefBehavior(CS);
122 if (Behavior == AliasAnalysis::DoesNotAccessMemory)
123 return;
124 else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
125 AliasTy = MayAlias;
126 AccessTy |= Refs;
127 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128 }
129
130 // FIXME: This should use mod/ref information to make this not suck so bad
131 AliasTy = MayAlias;
132 AccessTy = ModRef;
133}
134
135/// aliasesPointer - Return true if the specified pointer "may" (or must)
136/// alias one of the members in the set.
137///
138bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
139 AliasAnalysis &AA) const {
140 if (AliasTy == MustAlias) {
141 assert(CallSites.empty() && "Illegal must alias set!");
142
143 // If this is a set of MustAliases, only check to see if the pointer aliases
Chris Lattner23dbc5b2010-08-29 04:13:43 +0000144 // SOME value in the set.
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000145 PointerRec *SomePtr = getSomePointer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 assert(SomePtr && "Empty must-alias set??");
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000147 return AA.alias(SomePtr->getValue(), SomePtr->getSize(), Ptr, Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 }
149
150 // If this is a may-alias set, we have to check all of the pointers in the set
151 // to be sure it doesn't alias the set...
152 for (iterator I = begin(), E = end(); I != E; ++I)
153 if (AA.alias(Ptr, Size, I.getPointer(), I.getSize()))
154 return true;
155
156 // Check the call sites list and invoke list...
157 if (!CallSites.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000159 if (AA.getModRefInfo(CallSites[i], Ptr, Size) != AliasAnalysis::NoModRef)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 return true;
161 }
162
163 return false;
164}
165
166bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
Duncan Sands00b24b52007-12-01 07:51:45 +0000167 if (AA.doesNotAccessMemory(CS))
168 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
171 if (AA.getModRefInfo(CallSites[i], CS) != AliasAnalysis::NoModRef ||
172 AA.getModRefInfo(CS, CallSites[i]) != AliasAnalysis::NoModRef)
173 return true;
174
175 for (iterator I = begin(), E = end(); I != E; ++I)
176 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
177 AliasAnalysis::NoModRef)
178 return true;
179
180 return false;
181}
182
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000183void AliasSetTracker::clear() {
184 // Delete all the PointerRec entries.
Dan Gohman63b64a72009-07-30 20:21:41 +0000185 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
186 I != E; ++I)
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000187 I->second->eraseFromList();
188
189 PointerMap.clear();
190
191 // The alias sets should all be clear now.
192 AliasSets.clear();
193}
194
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195
196/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
197/// instruction referring to the pointer into. If there are multiple alias sets
198/// that may alias the pointer, merge them together and return the unified set.
199///
200AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
201 unsigned Size) {
202 AliasSet *FoundSet = 0;
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000203 for (iterator I = begin(), E = end(); I != E; ++I) {
204 if (I->Forward || !I->aliasesPointer(Ptr, Size, AA)) continue;
205
206 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
207 FoundSet = I; // Remember it.
208 } else { // Otherwise, we must merge the sets.
209 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210 }
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000211 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212
213 return FoundSet;
214}
215
216/// containsPointer - Return true if the specified location is represented by
217/// this alias set, false otherwise. This does not modify the AST object or
218/// alias sets.
219bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const {
220 for (const_iterator I = begin(), E = end(); I != E; ++I)
221 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA))
222 return true;
223 return false;
224}
225
226
227
228AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
229 AliasSet *FoundSet = 0;
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000230 for (iterator I = begin(), E = end(); I != E; ++I) {
231 if (I->Forward || !I->aliasesCallSite(CS, AA))
232 continue;
233
234 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
235 FoundSet = I; // Remember it.
236 } else if (!I->Forward) { // Otherwise, we must merge the sets.
237 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 }
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000239 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 return FoundSet;
241}
242
243
244
245
246/// getAliasSetForPointer - Return the alias set that the specified pointer
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000247/// lives in.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
249 bool *New) {
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000250 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251
Chris Lattner23dbc5b2010-08-29 04:13:43 +0000252 // Check to see if the pointer is already known.
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000253 if (Entry.hasAliasSet()) {
254 Entry.updateSize(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 // Return the set!
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000256 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000257 }
258
259 if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
Chris Lattner23dbc5b2010-08-29 04:13:43 +0000260 // Add it to the alias set it aliases.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 AS->addPointer(*this, Entry, Size);
262 return *AS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 }
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000264
265 if (New) *New = true;
Chris Lattner23dbc5b2010-08-29 04:13:43 +0000266 // Otherwise create a new alias set to hold the loaded pointer.
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000267 AliasSets.push_back(new AliasSet());
268 AliasSets.back().addPointer(*this, Entry, Size);
269 return AliasSets.back();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270}
271
272bool AliasSetTracker::add(Value *Ptr, unsigned Size) {
273 bool NewPtr;
274 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
275 return NewPtr;
276}
277
278
279bool AliasSetTracker::add(LoadInst *LI) {
280 bool NewPtr;
281 AliasSet &AS = addPointer(LI->getOperand(0),
Dan Gohman624f9612009-07-25 00:48:42 +0000282 AA.getTypeStoreSize(LI->getType()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 AliasSet::Refs, NewPtr);
284 if (LI->isVolatile()) AS.setVolatile();
285 return NewPtr;
286}
287
288bool AliasSetTracker::add(StoreInst *SI) {
289 bool NewPtr;
290 Value *Val = SI->getOperand(0);
291 AliasSet &AS = addPointer(SI->getOperand(1),
Dan Gohman624f9612009-07-25 00:48:42 +0000292 AA.getTypeStoreSize(Val->getType()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000293 AliasSet::Mods, NewPtr);
294 if (SI->isVolatile()) AS.setVolatile();
295 return NewPtr;
296}
297
Dan Gohmanddcc8562008-04-14 18:34:50 +0000298bool AliasSetTracker::add(VAArgInst *VAAI) {
299 bool NewPtr;
Chris Lattner81f40a92008-05-22 03:23:06 +0000300 addPointer(VAAI->getOperand(0), ~0, AliasSet::ModRef, NewPtr);
Dan Gohmanddcc8562008-04-14 18:34:50 +0000301 return NewPtr;
302}
303
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304
305bool AliasSetTracker::add(CallSite CS) {
Zhou Shengf74f7a82009-03-03 06:02:04 +0000306 if (isa<DbgInfoIntrinsic>(CS.getInstruction()))
307 return true; // Ignore DbgInfo Intrinsics.
Duncan Sands00b24b52007-12-01 07:51:45 +0000308 if (AA.doesNotAccessMemory(CS))
309 return true; // doesn't alias anything
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310
311 AliasSet *AS = findAliasSetForCallSite(CS);
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000312 if (AS) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000313 AS->addCallSite(CS, AA);
314 return false;
315 }
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000316 AliasSets.push_back(new AliasSet());
317 AS = &AliasSets.back();
318 AS->addCallSite(CS, AA);
319 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000320}
321
322bool AliasSetTracker::add(Instruction *I) {
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000323 // Dispatch to one of the other add methods.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324 if (LoadInst *LI = dyn_cast<LoadInst>(I))
325 return add(LI);
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000326 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327 return add(SI);
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000328 if (CallInst *CI = dyn_cast<CallInst>(I))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000329 return add(CI);
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000330 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 return add(II);
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000332 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
Dan Gohmanddcc8562008-04-14 18:34:50 +0000333 return add(VAAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334 return true;
335}
336
337void AliasSetTracker::add(BasicBlock &BB) {
338 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
339 add(I);
340}
341
342void AliasSetTracker::add(const AliasSetTracker &AST) {
343 assert(&AA == &AST.AA &&
344 "Merging AliasSetTracker objects with different Alias Analyses!");
345
346 // Loop over all of the alias sets in AST, adding the pointers contained
347 // therein into the current alias sets. This can cause alias sets to be
348 // merged together in the current AST.
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000349 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I) {
350 if (I->Forward) continue; // Ignore forwarding alias sets
351
352 AliasSet &AS = const_cast<AliasSet&>(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000354 // If there are any call sites in the alias set, add them to this AST.
355 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
356 add(AS.CallSites[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000357
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000358 // Loop over all of the pointers in this alias set.
359 bool X;
360 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) {
361 AliasSet &NewAS = addPointer(ASI.getPointer(), ASI.getSize(),
362 (AliasSet::AccessType)AS.AccessTy, X);
363 if (AS.isVolatile()) NewAS.setVolatile();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364 }
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000365 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000366}
367
368/// remove - Remove the specified (potentially non-empty) alias set from the
369/// tracker.
370void AliasSetTracker::remove(AliasSet &AS) {
371 // Drop all call sites.
372 AS.CallSites.clear();
373
374 // Clear the alias set.
375 unsigned NumRefs = 0;
376 while (!AS.empty()) {
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000377 AliasSet::PointerRec *P = AS.PtrList;
378
379 Value *ValToRemove = P->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000381 // Unlink and delete entry from the list of values.
382 P->eraseFromList();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383
384 // Remember how many references need to be dropped.
385 ++NumRefs;
386
387 // Finally, remove the entry.
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000388 PointerMap.erase(ValToRemove);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000389 }
390
391 // Stop using the alias set, removing it.
392 AS.RefCount -= NumRefs;
393 if (AS.RefCount == 0)
394 AS.removeFromTracker(*this);
395}
396
397bool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
398 AliasSet *AS = findAliasSetForPointer(Ptr, Size);
399 if (!AS) return false;
400 remove(*AS);
401 return true;
402}
403
404bool AliasSetTracker::remove(LoadInst *LI) {
Dan Gohman624f9612009-07-25 00:48:42 +0000405 unsigned Size = AA.getTypeStoreSize(LI->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000406 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
407 if (!AS) return false;
408 remove(*AS);
409 return true;
410}
411
412bool AliasSetTracker::remove(StoreInst *SI) {
Dan Gohman624f9612009-07-25 00:48:42 +0000413 unsigned Size = AA.getTypeStoreSize(SI->getOperand(0)->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000414 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
415 if (!AS) return false;
416 remove(*AS);
417 return true;
418}
419
Dan Gohmanddcc8562008-04-14 18:34:50 +0000420bool AliasSetTracker::remove(VAArgInst *VAAI) {
421 AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0), ~0);
422 if (!AS) return false;
423 remove(*AS);
424 return true;
425}
426
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427bool AliasSetTracker::remove(CallSite CS) {
Duncan Sands00b24b52007-12-01 07:51:45 +0000428 if (AA.doesNotAccessMemory(CS))
429 return false; // doesn't alias anything
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430
431 AliasSet *AS = findAliasSetForCallSite(CS);
432 if (!AS) return false;
433 remove(*AS);
434 return true;
435}
436
437bool AliasSetTracker::remove(Instruction *I) {
438 // Dispatch to one of the other remove methods...
439 if (LoadInst *LI = dyn_cast<LoadInst>(I))
440 return remove(LI);
Chris Lattner23dbc5b2010-08-29 04:13:43 +0000441 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442 return remove(SI);
Chris Lattner23dbc5b2010-08-29 04:13:43 +0000443 if (CallInst *CI = dyn_cast<CallInst>(I))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000444 return remove(CI);
Chris Lattner23dbc5b2010-08-29 04:13:43 +0000445 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
Dan Gohmanddcc8562008-04-14 18:34:50 +0000446 return remove(VAAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000447 return true;
448}
449
450
451// deleteValue method - This method is used to remove a pointer value from the
452// AliasSetTracker entirely. It should be used when an instruction is deleted
453// from the program to update the AST. If you don't use this, you would have
454// dangling pointers to deleted instructions.
455//
456void AliasSetTracker::deleteValue(Value *PtrVal) {
457 // Notify the alias analysis implementation that this value is gone.
458 AA.deleteValue(PtrVal);
459
460 // If this is a call instruction, remove the callsite from the appropriate
461 // AliasSet.
Gabor Greif617e3d62010-07-28 10:46:09 +0000462 if (CallSite CS = PtrVal)
Duncan Sands00b24b52007-12-01 07:51:45 +0000463 if (!AA.doesNotAccessMemory(CS))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464 if (AliasSet *AS = findAliasSetForCallSite(CS))
465 AS->removeCallSite(CS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000466
467 // First, look up the PointerRec for this pointer.
Dan Gohman63b64a72009-07-30 20:21:41 +0000468 PointerMapType::iterator I = PointerMap.find(PtrVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000469 if (I == PointerMap.end()) return; // Noop
470
471 // If we found one, remove the pointer from the alias set it is in.
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000472 AliasSet::PointerRec *PtrValEnt = I->second;
473 AliasSet *AS = PtrValEnt->getAliasSet(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000474
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000475 // Unlink and delete from the list of values.
476 PtrValEnt->eraseFromList();
477
478 // Stop using the alias set.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000479 AS->dropRef(*this);
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000480
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000481 PointerMap.erase(I);
482}
483
484// copyValue - This method should be used whenever a preexisting value in the
485// program is copied or cloned, introducing a new value. Note that it is ok for
486// clients that use this method to introduce the same value multiple times: if
487// the tracker already knows about a value, it will ignore the request.
488//
489void AliasSetTracker::copyValue(Value *From, Value *To) {
490 // Notify the alias analysis implementation that this value is copied.
491 AA.copyValue(From, To);
492
493 // First, look up the PointerRec for this pointer.
Dan Gohman63b64a72009-07-30 20:21:41 +0000494 PointerMapType::iterator I = PointerMap.find(From);
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000495 if (I == PointerMap.end())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000496 return; // Noop
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000497 assert(I->second->hasAliasSet() && "Dead entry?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000498
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000499 AliasSet::PointerRec &Entry = getEntryFor(To);
500 if (Entry.hasAliasSet()) return; // Already in the tracker!
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000501
502 // Add it to the alias set it aliases...
Devang Patel5d11d432009-03-30 18:34:47 +0000503 I = PointerMap.find(From);
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000504 AliasSet *AS = I->second->getAliasSet(*this);
505 AS->addPointer(*this, Entry, I->second->getSize(), true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000506}
507
508
509
510//===----------------------------------------------------------------------===//
511// AliasSet/AliasSetTracker Printing Support
512//===----------------------------------------------------------------------===//
513
Chris Lattnera7a9daa2009-08-23 05:17:37 +0000514void AliasSet::print(raw_ostream &OS) const {
515 OS << " AliasSet[" << format("0x%p", (void*)this) << "," << RefCount << "] ";
Dan Gohmancbec0762008-04-21 19:48:48 +0000516 OS << (AliasTy == MustAlias ? "must" : "may") << " alias, ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517 switch (AccessTy) {
518 case NoModRef: OS << "No access "; break;
519 case Refs : OS << "Ref "; break;
520 case Mods : OS << "Mod "; break;
521 case ModRef : OS << "Mod/Ref "; break;
Edwin Törökbd448e32009-07-14 16:55:14 +0000522 default: llvm_unreachable("Bad value for AccessTy!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000523 }
524 if (isVolatile()) OS << "[volatile] ";
525 if (Forward)
526 OS << " forwarding to " << (void*)Forward;
527
528
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000529 if (!empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000530 OS << "Pointers: ";
531 for (iterator I = begin(), E = end(); I != E; ++I) {
532 if (I != begin()) OS << ", ";
533 WriteAsOperand(OS << "(", I.getPointer());
534 OS << ", " << I.getSize() << ")";
535 }
536 }
537 if (!CallSites.empty()) {
538 OS << "\n " << CallSites.size() << " Call Sites: ";
539 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
540 if (i) OS << ", ";
541 WriteAsOperand(OS, CallSites[i].getCalledValue());
542 }
543 }
544 OS << "\n";
545}
546
Chris Lattnera7a9daa2009-08-23 05:17:37 +0000547void AliasSetTracker::print(raw_ostream &OS) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000548 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
549 << PointerMap.size() << " pointer values.\n";
550 for (const_iterator I = begin(), E = end(); I != E; ++I)
551 I->print(OS);
552 OS << "\n";
553}
554
David Greenee7ef6a92009-12-23 19:27:59 +0000555void AliasSet::dump() const { print(dbgs()); }
556void AliasSetTracker::dump() const { print(dbgs()); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000557
558//===----------------------------------------------------------------------===//
Dan Gohman63b64a72009-07-30 20:21:41 +0000559// ASTCallbackVH Class Implementation
560//===----------------------------------------------------------------------===//
561
562void AliasSetTracker::ASTCallbackVH::deleted() {
563 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
564 AST->deleteValue(getValPtr());
565 // this now dangles!
566}
567
568AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
Dan Gohman1e5054e2009-07-31 18:21:48 +0000569 : CallbackVH(V), AST(ast) {}
570
571AliasSetTracker::ASTCallbackVH &
572AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
573 return *this = ASTCallbackVH(V, AST);
574}
Dan Gohman63b64a72009-07-30 20:21:41 +0000575
576//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000577// AliasSetPrinter Pass
578//===----------------------------------------------------------------------===//
579
580namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +0000581 class AliasSetPrinter : public FunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000582 AliasSetTracker *Tracker;
583 public:
584 static char ID; // Pass identification, replacement for typeid
Owen Anderson75693222010-08-06 18:33:48 +0000585 AliasSetPrinter() : FunctionPass(ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586
587 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
588 AU.setPreservesAll();
589 AU.addRequired<AliasAnalysis>();
590 }
591
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000592 virtual bool runOnFunction(Function &F) {
593 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
594
595 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
596 Tracker->add(&*I);
David Greene092316b2009-12-23 22:49:57 +0000597 Tracker->print(errs());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598 delete Tracker;
599 return false;
600 }
601 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000602}
Dan Gohman089efff2008-05-13 00:00:25 +0000603
604char AliasSetPrinter::ID = 0;
Owen Anderson6374c3d2010-07-21 22:09:45 +0000605INITIALIZE_PASS(AliasSetPrinter, "print-alias-sets",
606 "Alias Set Printer", false, true);