blob: 5a163a2ab0f259a1a8d17bd481db5d1195a86460 [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) {
Chris Lattnerb6e2dc92010-08-29 18:42:23 +0000119 CallSites.push_back(CS.getInstruction());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120
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
Chris Lattnerb6e2dc92010-08-29 18:42:23 +0000170 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
171 if (AA.getModRefInfo(getCallSite(i), CS) != AliasAnalysis::NoModRef ||
172 AA.getModRefInfo(CS, getCallSite(i)) != AliasAnalysis::NoModRef)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 return true;
Chris Lattnerb6e2dc92010-08-29 18:42:23 +0000174 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175
176 for (iterator I = begin(), E = end(); I != E; ++I)
177 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
178 AliasAnalysis::NoModRef)
179 return true;
180
181 return false;
182}
183
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000184void AliasSetTracker::clear() {
185 // Delete all the PointerRec entries.
Dan Gohman63b64a72009-07-30 20:21:41 +0000186 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
187 I != E; ++I)
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000188 I->second->eraseFromList();
189
190 PointerMap.clear();
191
192 // The alias sets should all be clear now.
193 AliasSets.clear();
194}
195
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196
197/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
198/// instruction referring to the pointer into. If there are multiple alias sets
199/// that may alias the pointer, merge them together and return the unified set.
200///
201AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
202 unsigned Size) {
203 AliasSet *FoundSet = 0;
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000204 for (iterator I = begin(), E = end(); I != E; ++I) {
205 if (I->Forward || !I->aliasesPointer(Ptr, Size, AA)) continue;
206
207 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
208 FoundSet = I; // Remember it.
209 } else { // Otherwise, we must merge the sets.
210 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 }
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000212 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213
214 return FoundSet;
215}
216
217/// containsPointer - Return true if the specified location is represented by
218/// this alias set, false otherwise. This does not modify the AST object or
219/// alias sets.
220bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const {
221 for (const_iterator I = begin(), E = end(); I != E; ++I)
222 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA))
223 return true;
224 return false;
225}
226
227
228
229AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
230 AliasSet *FoundSet = 0;
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000231 for (iterator I = begin(), E = end(); I != E; ++I) {
232 if (I->Forward || !I->aliasesCallSite(CS, AA))
233 continue;
234
Chris Lattnerb6e2dc92010-08-29 18:42:23 +0000235 if (FoundSet == 0) // If this is the first alias set ptr can go into.
236 FoundSet = I; // Remember it.
237 else if (!I->Forward) // Otherwise, we must merge the sets.
Chris Lattnerd0c8b892010-08-29 04:06:55 +0000238 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
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
Chris Lattnerb6e2dc92010-08-29 18:42:23 +0000461 // AliasSet (if present).
462 if (CallSite CS = PtrVal) {
463 if (!AA.doesNotAccessMemory(CS)) {
464 // Scan all the alias sets to see if this call site is contained.
465 for (iterator I = begin(), E = end(); I != E; ++I) {
466 if (I->Forward) continue;
467
468 I->removeCallSite(CS);
469 }
470 }
471 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000472
473 // First, look up the PointerRec for this pointer.
Dan Gohman63b64a72009-07-30 20:21:41 +0000474 PointerMapType::iterator I = PointerMap.find(PtrVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000475 if (I == PointerMap.end()) return; // Noop
476
477 // If we found one, remove the pointer from the alias set it is in.
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000478 AliasSet::PointerRec *PtrValEnt = I->second;
479 AliasSet *AS = PtrValEnt->getAliasSet(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000481 // Unlink and delete from the list of values.
482 PtrValEnt->eraseFromList();
483
484 // Stop using the alias set.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485 AS->dropRef(*this);
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000486
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000487 PointerMap.erase(I);
488}
489
490// copyValue - This method should be used whenever a preexisting value in the
491// program is copied or cloned, introducing a new value. Note that it is ok for
492// clients that use this method to introduce the same value multiple times: if
493// the tracker already knows about a value, it will ignore the request.
494//
495void AliasSetTracker::copyValue(Value *From, Value *To) {
496 // Notify the alias analysis implementation that this value is copied.
497 AA.copyValue(From, To);
498
499 // First, look up the PointerRec for this pointer.
Dan Gohman63b64a72009-07-30 20:21:41 +0000500 PointerMapType::iterator I = PointerMap.find(From);
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000501 if (I == PointerMap.end())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000502 return; // Noop
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000503 assert(I->second->hasAliasSet() && "Dead entry?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000504
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000505 AliasSet::PointerRec &Entry = getEntryFor(To);
506 if (Entry.hasAliasSet()) return; // Already in the tracker!
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000507
508 // Add it to the alias set it aliases...
Devang Patel5d11d432009-03-30 18:34:47 +0000509 I = PointerMap.find(From);
Chris Lattner47b3b8c2009-03-09 05:11:09 +0000510 AliasSet *AS = I->second->getAliasSet(*this);
511 AS->addPointer(*this, Entry, I->second->getSize(), true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512}
513
514
515
516//===----------------------------------------------------------------------===//
517// AliasSet/AliasSetTracker Printing Support
518//===----------------------------------------------------------------------===//
519
Chris Lattnera7a9daa2009-08-23 05:17:37 +0000520void AliasSet::print(raw_ostream &OS) const {
521 OS << " AliasSet[" << format("0x%p", (void*)this) << "," << RefCount << "] ";
Dan Gohmancbec0762008-04-21 19:48:48 +0000522 OS << (AliasTy == MustAlias ? "must" : "may") << " alias, ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000523 switch (AccessTy) {
524 case NoModRef: OS << "No access "; break;
525 case Refs : OS << "Ref "; break;
526 case Mods : OS << "Mod "; break;
527 case ModRef : OS << "Mod/Ref "; break;
Edwin Törökbd448e32009-07-14 16:55:14 +0000528 default: llvm_unreachable("Bad value for AccessTy!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000529 }
530 if (isVolatile()) OS << "[volatile] ";
531 if (Forward)
532 OS << " forwarding to " << (void*)Forward;
533
534
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000535 if (!empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000536 OS << "Pointers: ";
537 for (iterator I = begin(), E = end(); I != E; ++I) {
538 if (I != begin()) OS << ", ";
539 WriteAsOperand(OS << "(", I.getPointer());
540 OS << ", " << I.getSize() << ")";
541 }
542 }
543 if (!CallSites.empty()) {
544 OS << "\n " << CallSites.size() << " Call Sites: ";
545 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
546 if (i) OS << ", ";
Chris Lattnerb6e2dc92010-08-29 18:42:23 +0000547 WriteAsOperand(OS, CallSites[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000548 }
549 }
550 OS << "\n";
551}
552
Chris Lattnera7a9daa2009-08-23 05:17:37 +0000553void AliasSetTracker::print(raw_ostream &OS) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000554 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
555 << PointerMap.size() << " pointer values.\n";
556 for (const_iterator I = begin(), E = end(); I != E; ++I)
557 I->print(OS);
558 OS << "\n";
559}
560
David Greenee7ef6a92009-12-23 19:27:59 +0000561void AliasSet::dump() const { print(dbgs()); }
562void AliasSetTracker::dump() const { print(dbgs()); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563
564//===----------------------------------------------------------------------===//
Dan Gohman63b64a72009-07-30 20:21:41 +0000565// ASTCallbackVH Class Implementation
566//===----------------------------------------------------------------------===//
567
568void AliasSetTracker::ASTCallbackVH::deleted() {
569 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
570 AST->deleteValue(getValPtr());
571 // this now dangles!
572}
573
574AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
Dan Gohman1e5054e2009-07-31 18:21:48 +0000575 : CallbackVH(V), AST(ast) {}
576
577AliasSetTracker::ASTCallbackVH &
578AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
579 return *this = ASTCallbackVH(V, AST);
580}
Dan Gohman63b64a72009-07-30 20:21:41 +0000581
582//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583// AliasSetPrinter Pass
584//===----------------------------------------------------------------------===//
585
586namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +0000587 class AliasSetPrinter : public FunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000588 AliasSetTracker *Tracker;
589 public:
590 static char ID; // Pass identification, replacement for typeid
Owen Anderson75693222010-08-06 18:33:48 +0000591 AliasSetPrinter() : FunctionPass(ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000592
593 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
594 AU.setPreservesAll();
595 AU.addRequired<AliasAnalysis>();
596 }
597
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598 virtual bool runOnFunction(Function &F) {
599 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
600
601 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
602 Tracker->add(&*I);
David Greene092316b2009-12-23 22:49:57 +0000603 Tracker->print(errs());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000604 delete Tracker;
605 return false;
606 }
607 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000608}
Dan Gohman089efff2008-05-13 00:00:25 +0000609
610char AliasSetPrinter::ID = 0;
Owen Anderson6374c3d2010-07-21 22:09:45 +0000611INITIALIZE_PASS(AliasSetPrinter, "print-alias-sets",
612 "Alias Set Printer", false, true);