blob: 68603e53b64ee40138123f257a9147dd8ee7b910 [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"
Chris Lattner9971ac42003-02-24 20:37:56 +000017#include "llvm/Pass.h"
Chris Lattner5b3a4552005-03-17 15:38:16 +000018#include "llvm/Type.h"
Chris Lattner31a9d182003-02-26 22:11:00 +000019#include "llvm/Target/TargetData.h"
Chris Lattner9971ac42003-02-24 20:37:56 +000020#include "llvm/Assembly/Writer.h"
Reid Spencerd7d83db2007-02-05 23:42:17 +000021#include "llvm/Support/Compiler.h"
Chris Lattner9971ac42003-02-24 20:37:56 +000022#include "llvm/Support/InstIterator.h"
Bill Wendling6f81b512006-11-28 22:46:12 +000023#include "llvm/Support/Streams.h"
Chris Lattnere2609242003-12-14 04:52:11 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattnercc8d5242004-11-27 18:37:42 +000026/// mergeSetIn - Merge the specified alias set into this alias set.
Chris Lattner009cc3d2002-09-26 21:49:07 +000027///
Chris Lattnercc8d5242004-11-27 18:37:42 +000028void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
Chris Lattner9971ac42003-02-24 20:37:56 +000029 assert(!AS.Forward && "Alias set is already forwarding!");
30 assert(!Forward && "This set is a forwarding set!!");
Chris Lattner009cc3d2002-09-26 21:49:07 +000031
32 // Update the alias and access types of this set...
Chris Lattner9971ac42003-02-24 20:37:56 +000033 AccessTy |= AS.AccessTy;
34 AliasTy |= AS.AliasTy;
35
Chris Lattnercc8d5242004-11-27 18:37:42 +000036 if (AliasTy == MustAlias) {
37 // Check that these two merged sets really are must aliases. Since both
38 // used to be must-alias sets, we can just check any pointer from each set
39 // for aliasing.
40 AliasAnalysis &AA = AST.getAliasAnalysis();
41 HashNodePair *L = getSomePointer();
42 HashNodePair *R = AS.getSomePointer();
43
44 // If the pointers are not a must-alias pair, this set becomes a may alias.
45 if (AA.alias(L->first, L->second.getSize(), R->first, R->second.getSize())
46 != AliasAnalysis::MustAlias)
47 AliasTy = MayAlias;
48 }
49
Chris Lattner9971ac42003-02-24 20:37:56 +000050 if (CallSites.empty()) { // Merge call sites...
51 if (!AS.CallSites.empty())
52 std::swap(CallSites, AS.CallSites);
53 } else if (!AS.CallSites.empty()) {
54 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
55 AS.CallSites.clear();
56 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000057
Chris Lattner9971ac42003-02-24 20:37:56 +000058 AS.Forward = this; // Forward across AS now...
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000059 addRef(); // AS is now pointing to us...
Chris Lattner9971ac42003-02-24 20:37:56 +000060
61 // Merge the list of constituent pointers...
Chris Lattner2cffeec2003-12-18 08:11:56 +000062 if (AS.PtrList) {
63 *PtrListEnd = AS.PtrList;
64 AS.PtrList->second.setPrevInList(PtrListEnd);
65 PtrListEnd = AS.PtrListEnd;
66
67 AS.PtrList = 0;
68 AS.PtrListEnd = &AS.PtrList;
Chris Lattner3080b602004-09-15 16:59:47 +000069 assert(*AS.PtrListEnd == 0 && "End of list is not null?");
Chris Lattner2cffeec2003-12-18 08:11:56 +000070 }
Chris Lattner009cc3d2002-09-26 21:49:07 +000071}
72
Chris Lattner9971ac42003-02-24 20:37:56 +000073void AliasSetTracker::removeAliasSet(AliasSet *AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000074 if (AliasSet *Fwd = AS->Forward) {
75 Fwd->dropRef(*this);
76 AS->Forward = 0;
77 }
Chris Lattner9971ac42003-02-24 20:37:56 +000078 AliasSets.erase(AS);
79}
80
81void AliasSet::removeFromTracker(AliasSetTracker &AST) {
82 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
83 AST.removeAliasSet(this);
84}
85
Chris Lattner31a9d182003-02-26 22:11:00 +000086void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
Chris Lattnerb66e6482004-09-14 19:15:32 +000087 unsigned Size, bool KnownMustAlias) {
Chris Lattner9971ac42003-02-24 20:37:56 +000088 assert(!Entry.second.hasAliasSet() && "Entry already in set!");
89
Chris Lattnerb66e6482004-09-14 19:15:32 +000090 // Check to see if we have to downgrade to _may_ alias.
91 if (isMustAlias() && !KnownMustAlias)
Chris Lattner31a9d182003-02-26 22:11:00 +000092 if (HashNodePair *P = getSomePointer()) {
Chris Lattner3080b602004-09-15 16:59:47 +000093 AliasAnalysis &AA = AST.getAliasAnalysis();
Chris Lattner31a9d182003-02-26 22:11:00 +000094 AliasAnalysis::AliasResult Result =
95 AA.alias(P->first, P->second.getSize(), Entry.first, Size);
96 if (Result == AliasAnalysis::MayAlias)
Chris Lattner9971ac42003-02-24 20:37:56 +000097 AliasTy = MayAlias;
Chris Lattner31a9d182003-02-26 22:11:00 +000098 else // First entry of must alias must have maximum size!
99 P->second.updateSize(Size);
100 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
101 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000102
103 Entry.second.setAliasSet(this);
Chris Lattner31a9d182003-02-26 22:11:00 +0000104 Entry.second.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000105
106 // Add it to the end of the list...
Chris Lattner2cffeec2003-12-18 08:11:56 +0000107 assert(*PtrListEnd == 0 && "End of list is not null?");
108 *PtrListEnd = &Entry;
109 PtrListEnd = Entry.second.setPrevInList(PtrListEnd);
Chris Lattner3080b602004-09-15 16:59:47 +0000110 assert(*PtrListEnd == 0 && "End of list is not null?");
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000111 addRef(); // Entry points to alias set...
Chris Lattner9971ac42003-02-24 20:37:56 +0000112}
113
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000114void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000115 CallSites.push_back(CS);
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000116
Duncan Sandsdff67102007-12-01 07:51:45 +0000117 AliasAnalysis::ModRefBehavior Behavior = AA.getModRefBehavior(CS);
118 if (Behavior == AliasAnalysis::DoesNotAccessMemory)
119 return;
120 else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
121 AliasTy = MayAlias;
122 AccessTy |= Refs;
123 return;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000124 }
125
126 // FIXME: This should use mod/ref information to make this not suck so bad
127 AliasTy = MayAlias;
Chris Lattner577385e2003-05-03 03:42:08 +0000128 AccessTy = ModRef;
Chris Lattner9971ac42003-02-24 20:37:56 +0000129}
130
131/// aliasesPointer - Return true if the specified pointer "may" (or must)
Chris Lattner009cc3d2002-09-26 21:49:07 +0000132/// alias one of the members in the set.
133///
Chris Lattner31a9d182003-02-26 22:11:00 +0000134bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
135 AliasAnalysis &AA) const {
Chris Lattner9971ac42003-02-24 20:37:56 +0000136 if (AliasTy == MustAlias) {
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000137 assert(CallSites.empty() && "Illegal must alias set!");
138
Chris Lattner9971ac42003-02-24 20:37:56 +0000139 // If this is a set of MustAliases, only check to see if the pointer aliases
140 // SOME value in the set...
Chris Lattner31a9d182003-02-26 22:11:00 +0000141 HashNodePair *SomePtr = getSomePointer();
Chris Lattner9971ac42003-02-24 20:37:56 +0000142 assert(SomePtr && "Empty must-alias set??");
Chris Lattner31a9d182003-02-26 22:11:00 +0000143 return AA.alias(SomePtr->first, SomePtr->second.getSize(), Ptr, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000144 }
145
146 // If this is a may-alias set, we have to check all of the pointers in the set
147 // to be sure it doesn't alias the set...
148 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000149 if (AA.alias(Ptr, Size, I.getPointer(), I.getSize()))
Chris Lattner9971ac42003-02-24 20:37:56 +0000150 return true;
151
152 // Check the call sites list and invoke list...
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000153 if (!CallSites.empty()) {
154 if (AA.hasNoModRefInfoForCalls())
155 return true;
156
157 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
158 if (AA.getModRefInfo(CallSites[i], const_cast<Value*>(Ptr), Size)
159 != AliasAnalysis::NoModRef)
160 return true;
161 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000162
Chris Lattner009cc3d2002-09-26 21:49:07 +0000163 return false;
164}
165
Chris Lattner9971ac42003-02-24 20:37:56 +0000166bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
Duncan Sandsdff67102007-12-01 07:51:45 +0000167 if (AA.doesNotAccessMemory(CS))
168 return false;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000169
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000170 if (AA.hasNoModRefInfoForCalls())
171 return true;
172
173 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
174 if (AA.getModRefInfo(CallSites[i], CS) != AliasAnalysis::NoModRef ||
175 AA.getModRefInfo(CS, CallSites[i]) != AliasAnalysis::NoModRef)
176 return true;
177
178 for (iterator I = begin(), E = end(); I != E; ++I)
179 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
180 AliasAnalysis::NoModRef)
181 return true;
182
183 return false;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000184}
185
186
Chris Lattner009cc3d2002-09-26 21:49:07 +0000187/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
188/// instruction referring to the pointer into. If there are multiple alias sets
189/// that may alias the pointer, merge them together and return the unified set.
190///
Chris Lattner31a9d182003-02-26 22:11:00 +0000191AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
192 unsigned Size) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000193 AliasSet *FoundSet = 0;
Chris Lattner9971ac42003-02-24 20:37:56 +0000194 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner31a9d182003-02-26 22:11:00 +0000195 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
Chris Lattnercc8d5242004-11-27 18:37:42 +0000196 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
Chris Lattner9971ac42003-02-24 20:37:56 +0000197 FoundSet = I; // Remember it.
Chris Lattnercc8d5242004-11-27 18:37:42 +0000198 } else { // Otherwise, we must merge the sets.
199 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
Chris Lattner009cc3d2002-09-26 21:49:07 +0000200 }
201 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000202
203 return FoundSet;
204}
205
Chris Lattner07bfa522004-11-26 21:36:25 +0000206/// containsPointer - Return true if the specified location is represented by
207/// this alias set, false otherwise. This does not modify the AST object or
208/// alias sets.
209bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const {
210 for (const_iterator I = begin(), E = end(); I != E; ++I)
211 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA))
212 return true;
213 return false;
214}
215
216
217
Chris Lattner9971ac42003-02-24 20:37:56 +0000218AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
219 AliasSet *FoundSet = 0;
220 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000221 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
Chris Lattnercc8d5242004-11-27 18:37:42 +0000222 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
Chris Lattner9971ac42003-02-24 20:37:56 +0000223 FoundSet = I; // Remember it.
Chris Lattnercc8d5242004-11-27 18:37:42 +0000224 } else if (!I->Forward) { // Otherwise, we must merge the sets.
225 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
Chris Lattner9971ac42003-02-24 20:37:56 +0000226 }
227 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000228
229 return FoundSet;
230}
231
232
Chris Lattner009cc3d2002-09-26 21:49:07 +0000233
Chris Lattner9971ac42003-02-24 20:37:56 +0000234
235/// getAliasSetForPointer - Return the alias set that the specified pointer
236/// lives in...
Chris Lattner12c11552004-07-21 05:18:04 +0000237AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
238 bool *New) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000239 AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
240
241 // Check to see if the pointer is already known...
Chris Lattner34a10052004-07-25 18:32:01 +0000242 if (Entry.second.hasAliasSet()) {
243 Entry.second.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000244 // Return the set!
245 return *Entry.second.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattner31a9d182003-02-26 22:11:00 +0000246 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000247 // Add it to the alias set it aliases...
Chris Lattner31a9d182003-02-26 22:11:00 +0000248 AS->addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000249 return *AS;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000250 } else {
Chris Lattner12c11552004-07-21 05:18:04 +0000251 if (New) *New = true;
Chris Lattner9971ac42003-02-24 20:37:56 +0000252 // Otherwise create a new alias set to hold the loaded pointer...
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000253 AliasSets.push_back(new AliasSet());
Chris Lattner31a9d182003-02-26 22:11:00 +0000254 AliasSets.back().addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000255 return AliasSets.back();
Chris Lattner009cc3d2002-09-26 21:49:07 +0000256 }
257}
258
Chris Lattner61d46272004-07-26 05:50:23 +0000259bool AliasSetTracker::add(Value *Ptr, unsigned Size) {
260 bool NewPtr;
261 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
262 return NewPtr;
263}
264
265
Chris Lattner12c11552004-07-21 05:18:04 +0000266bool AliasSetTracker::add(LoadInst *LI) {
267 bool NewPtr;
268 AliasSet &AS = addPointer(LI->getOperand(0),
Duncan Sands514ab342007-11-01 20:53:16 +0000269 AA.getTargetData().getTypeStoreSize(LI->getType()),
Chris Lattner12c11552004-07-21 05:18:04 +0000270 AliasSet::Refs, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000271 if (LI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000272 return NewPtr;
Chris Lattner9971ac42003-02-24 20:37:56 +0000273}
274
Chris Lattner12c11552004-07-21 05:18:04 +0000275bool AliasSetTracker::add(StoreInst *SI) {
276 bool NewPtr;
277 Value *Val = SI->getOperand(0);
278 AliasSet &AS = addPointer(SI->getOperand(1),
Duncan Sands514ab342007-11-01 20:53:16 +0000279 AA.getTargetData().getTypeStoreSize(Val->getType()),
Chris Lattner12c11552004-07-21 05:18:04 +0000280 AliasSet::Mods, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000281 if (SI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000282 return NewPtr;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000283}
284
Chris Lattner5c882602004-07-25 07:57:37 +0000285bool AliasSetTracker::add(FreeInst *FI) {
286 bool NewPtr;
287 AliasSet &AS = addPointer(FI->getOperand(0), ~0,
288 AliasSet::Mods, NewPtr);
Chris Lattner2958eea2005-03-25 05:49:37 +0000289
290 // Free operations are volatile ops (cannot be moved).
291 AS.setVolatile();
Chris Lattner5c882602004-07-25 07:57:37 +0000292 return NewPtr;
293}
294
Dan Gohman235fc572008-04-14 18:34:50 +0000295bool AliasSetTracker::add(VAArgInst *VAAI) {
296 bool NewPtr;
297 addPointer(VAAI->getOperand(0), ~0, AliasSet::ModRef, NewPtr);
298 return NewPtr;
299}
300
Chris Lattnere2609242003-12-14 04:52:11 +0000301
Chris Lattner12c11552004-07-21 05:18:04 +0000302bool AliasSetTracker::add(CallSite CS) {
Duncan Sandsdff67102007-12-01 07:51:45 +0000303 if (AA.doesNotAccessMemory(CS))
304 return true; // doesn't alias anything
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000305
Chris Lattner9971ac42003-02-24 20:37:56 +0000306 AliasSet *AS = findAliasSetForCallSite(CS);
307 if (!AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000308 AliasSets.push_back(new AliasSet());
Chris Lattner9971ac42003-02-24 20:37:56 +0000309 AS = &AliasSets.back();
Chris Lattner12c11552004-07-21 05:18:04 +0000310 AS->addCallSite(CS, AA);
311 return true;
312 } else {
313 AS->addCallSite(CS, AA);
314 return false;
Chris Lattner9971ac42003-02-24 20:37:56 +0000315 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000316}
317
Chris Lattner12c11552004-07-21 05:18:04 +0000318bool AliasSetTracker::add(Instruction *I) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000319 // Dispatch to one of the other add methods...
320 if (LoadInst *LI = dyn_cast<LoadInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000321 return add(LI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000322 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000323 return add(SI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000324 else if (CallInst *CI = dyn_cast<CallInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000325 return add(CI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000326 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000327 return add(II);
Chris Lattner5c882602004-07-25 07:57:37 +0000328 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
329 return add(FI);
Dan Gohman235fc572008-04-14 18:34:50 +0000330 else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
331 return add(VAAI);
Chris Lattner12c11552004-07-21 05:18:04 +0000332 return true;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000333}
334
Chris Lattner319d05b2003-03-03 23:28:05 +0000335void AliasSetTracker::add(BasicBlock &BB) {
336 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
337 add(I);
338}
339
340void AliasSetTracker::add(const AliasSetTracker &AST) {
341 assert(&AA == &AST.AA &&
342 "Merging AliasSetTracker objects with different Alias Analyses!");
343
344 // Loop over all of the alias sets in AST, adding the pointers contained
345 // therein into the current alias sets. This can cause alias sets to be
346 // merged together in the current AST.
347 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
348 if (!I->Forward) { // Ignore forwarding alias sets
349 AliasSet &AS = const_cast<AliasSet&>(*I);
350
351 // If there are any call sites in the alias set, add them to this AST.
352 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
353 add(AS.CallSites[i]);
354
355 // Loop over all of the pointers in this alias set...
356 AliasSet::iterator I = AS.begin(), E = AS.end();
Chris Lattner12c11552004-07-21 05:18:04 +0000357 bool X;
Chris Lattnerf711fb72007-05-23 06:36:35 +0000358 for (; I != E; ++I) {
359 AliasSet &NewAS = addPointer(I.getPointer(), I.getSize(),
360 (AliasSet::AccessType)AS.AccessTy, X);
361 if (AS.isVolatile()) NewAS.setVolatile();
362 }
Chris Lattner319d05b2003-03-03 23:28:05 +0000363 }
364}
365
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000366/// remove - Remove the specified (potentially non-empty) alias set from the
367/// tracker.
368void AliasSetTracker::remove(AliasSet &AS) {
Chris Lattnerf2998572006-06-27 23:48:59 +0000369 // Drop all call sites.
370 AS.CallSites.clear();
371
372 // Clear the alias set.
373 unsigned NumRefs = 0;
374 while (!AS.empty()) {
375 AliasSet::HashNodePair *P = AS.PtrList;
376
377 // Unlink from the list of values.
378 P->second.removeFromList();
379
380 // Remember how many references need to be dropped.
381 ++NumRefs;
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000382
Chris Lattnerf2998572006-06-27 23:48:59 +0000383 // Finally, remove the entry.
Nick Lewyckye81f7252006-09-17 17:51:00 +0000384 Value *Remove = P->first; // Take a copy because it is invalid to pass
385 PointerMap.erase(Remove); // a reference to the data being erased.
Chris Lattnerf2998572006-06-27 23:48:59 +0000386 }
387
388 // Stop using the alias set, removing it.
Chris Lattner356d8c22006-06-27 23:56:13 +0000389 AS.RefCount -= NumRefs;
390 if (AS.RefCount == 0)
391 AS.removeFromTracker(*this);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000392}
393
Chris Lattner61d46272004-07-26 05:50:23 +0000394bool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
395 AliasSet *AS = findAliasSetForPointer(Ptr, Size);
396 if (!AS) return false;
397 remove(*AS);
398 return true;
399}
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000400
401bool AliasSetTracker::remove(LoadInst *LI) {
Duncan Sands514ab342007-11-01 20:53:16 +0000402 unsigned Size = AA.getTargetData().getTypeStoreSize(LI->getType());
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000403 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
404 if (!AS) return false;
405 remove(*AS);
406 return true;
407}
408
409bool AliasSetTracker::remove(StoreInst *SI) {
Duncan Sands514ab342007-11-01 20:53:16 +0000410 unsigned Size =
411 AA.getTargetData().getTypeStoreSize(SI->getOperand(0)->getType());
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000412 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
413 if (!AS) return false;
414 remove(*AS);
415 return true;
416}
417
Chris Lattner5c882602004-07-25 07:57:37 +0000418bool AliasSetTracker::remove(FreeInst *FI) {
419 AliasSet *AS = findAliasSetForPointer(FI->getOperand(0), ~0);
420 if (!AS) return false;
421 remove(*AS);
422 return true;
423}
424
Dan Gohman235fc572008-04-14 18:34:50 +0000425bool AliasSetTracker::remove(VAArgInst *VAAI) {
426 AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0), ~0);
427 if (!AS) return false;
428 remove(*AS);
429 return true;
430}
431
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000432bool AliasSetTracker::remove(CallSite CS) {
Duncan Sandsdff67102007-12-01 07:51:45 +0000433 if (AA.doesNotAccessMemory(CS))
434 return false; // doesn't alias anything
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000435
436 AliasSet *AS = findAliasSetForCallSite(CS);
437 if (!AS) return false;
438 remove(*AS);
439 return true;
440}
441
442bool AliasSetTracker::remove(Instruction *I) {
443 // Dispatch to one of the other remove methods...
444 if (LoadInst *LI = dyn_cast<LoadInst>(I))
445 return remove(LI);
446 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
447 return remove(SI);
448 else if (CallInst *CI = dyn_cast<CallInst>(I))
449 return remove(CI);
Chris Lattner5c882602004-07-25 07:57:37 +0000450 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
451 return remove(FI);
Dan Gohman235fc572008-04-14 18:34:50 +0000452 else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
453 return remove(VAAI);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000454 return true;
455}
456
Chris Lattner2cffeec2003-12-18 08:11:56 +0000457
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000458// deleteValue method - This method is used to remove a pointer value from the
Chris Lattner2cffeec2003-12-18 08:11:56 +0000459// AliasSetTracker entirely. It should be used when an instruction is deleted
460// from the program to update the AST. If you don't use this, you would have
461// dangling pointers to deleted instructions.
462//
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000463void AliasSetTracker::deleteValue(Value *PtrVal) {
Chris Lattnerb66e6482004-09-14 19:15:32 +0000464 // Notify the alias analysis implementation that this value is gone.
465 AA.deleteValue(PtrVal);
466
Chris Lattner959e3212006-06-26 19:20:48 +0000467 // If this is a call instruction, remove the callsite from the appropriate
468 // AliasSet.
469 CallSite CS = CallSite::get(PtrVal);
Duncan Sandsdff67102007-12-01 07:51:45 +0000470 if (CS.getInstruction())
471 if (!AA.doesNotAccessMemory(CS))
Chris Lattner959e3212006-06-26 19:20:48 +0000472 if (AliasSet *AS = findAliasSetForCallSite(CS))
473 AS->removeCallSite(CS);
Chris Lattner959e3212006-06-26 19:20:48 +0000474
Chris Lattnerb66e6482004-09-14 19:15:32 +0000475 // First, look up the PointerRec for this pointer.
Chris Lattner2cffeec2003-12-18 08:11:56 +0000476 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(PtrVal);
477 if (I == PointerMap.end()) return; // Noop
478
479 // If we found one, remove the pointer from the alias set it is in.
480 AliasSet::HashNodePair &PtrValEnt = *I;
481 AliasSet *AS = PtrValEnt.second.getAliasSet(*this);
482
483 // Unlink from the list of values...
484 PtrValEnt.second.removeFromList();
485 // Stop using the alias set
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000486 AS->dropRef(*this);
Chris Lattner2cffeec2003-12-18 08:11:56 +0000487 PointerMap.erase(I);
488}
489
Chris Lattnerb66e6482004-09-14 19:15:32 +0000490// 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.
500 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(From);
501 if (I == PointerMap.end() || !I->second.hasAliasSet())
502 return; // Noop
503
504 AliasSet::HashNodePair &Entry = getEntryFor(To);
505 if (Entry.second.hasAliasSet()) return; // Already in the tracker!
506
507 // Add it to the alias set it aliases...
508 AliasSet *AS = I->second.getAliasSet(*this);
509 AS->addPointer(*this, Entry, I->second.getSize(), true);
510}
511
512
Chris Lattner2cffeec2003-12-18 08:11:56 +0000513
Chris Lattner9971ac42003-02-24 20:37:56 +0000514//===----------------------------------------------------------------------===//
515// AliasSet/AliasSetTracker Printing Support
516//===----------------------------------------------------------------------===//
517
518void AliasSet::print(std::ostream &OS) const {
519 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
Dan Gohman92c7b652008-04-21 19:48:48 +0000520 OS << (AliasTy == MustAlias ? "must" : "may") << " alias, ";
Chris Lattner9971ac42003-02-24 20:37:56 +0000521 switch (AccessTy) {
522 case NoModRef: OS << "No access "; break;
523 case Refs : OS << "Ref "; break;
524 case Mods : OS << "Mod "; break;
525 case ModRef : OS << "Mod/Ref "; break;
526 default: assert(0 && "Bad value for AccessTy!");
Chris Lattner009cc3d2002-09-26 21:49:07 +0000527 }
Chris Lattnere2609242003-12-14 04:52:11 +0000528 if (isVolatile()) OS << "[volatile] ";
Chris Lattner9971ac42003-02-24 20:37:56 +0000529 if (Forward)
530 OS << " forwarding to " << (void*)Forward;
531
532
Dan Gohmancb406c22007-10-03 19:26:29 +0000533 if (!empty()) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000534 OS << "Pointers: ";
535 for (iterator I = begin(), E = end(); I != E; ++I) {
536 if (I != begin()) OS << ", ";
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000537 WriteAsOperand(OS << "(", I.getPointer());
538 OS << ", " << I.getSize() << ")";
Chris Lattner9971ac42003-02-24 20:37:56 +0000539 }
540 }
541 if (!CallSites.empty()) {
542 OS << "\n " << CallSites.size() << " Call Sites: ";
543 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
544 if (i) OS << ", ";
545 WriteAsOperand(OS, CallSites[i].getCalledValue());
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000546 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000547 }
548 OS << "\n";
549}
550
551void AliasSetTracker::print(std::ostream &OS) const {
552 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
553 << PointerMap.size() << " pointer values.\n";
554 for (const_iterator I = begin(), E = end(); I != E; ++I)
555 I->print(OS);
556 OS << "\n";
557}
558
Bill Wendlinge8156192006-12-07 01:30:32 +0000559void AliasSet::dump() const { print (cerr); }
560void AliasSetTracker::dump() const { print(cerr); }
Chris Lattner9971ac42003-02-24 20:37:56 +0000561
Chris Lattner9971ac42003-02-24 20:37:56 +0000562//===----------------------------------------------------------------------===//
563// AliasSetPrinter Pass
564//===----------------------------------------------------------------------===//
565
566namespace {
Reid Spencerd7d83db2007-02-05 23:42:17 +0000567 class VISIBILITY_HIDDEN AliasSetPrinter : public FunctionPass {
Chris Lattner9971ac42003-02-24 20:37:56 +0000568 AliasSetTracker *Tracker;
569 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +0000570 static char ID; // Pass identification, replacement for typeid
Devang Patelc7582092008-03-19 21:56:59 +0000571 AliasSetPrinter() : FunctionPass((intptr_t)&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +0000572
Chris Lattner9971ac42003-02-24 20:37:56 +0000573 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
574 AU.setPreservesAll();
575 AU.addRequired<AliasAnalysis>();
576 }
577
578 virtual bool runOnFunction(Function &F) {
579 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
580
581 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner6ffe5512004-04-27 15:13:33 +0000582 Tracker->add(&*I);
Bill Wendlinge8156192006-12-07 01:30:32 +0000583 Tracker->print(cerr);
Chris Lattner4983cf72006-01-03 06:05:22 +0000584 delete Tracker;
Chris Lattner9971ac42003-02-24 20:37:56 +0000585 return false;
586 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000587 };
Chris Lattner009cc3d2002-09-26 21:49:07 +0000588}
Dan Gohman844731a2008-05-13 00:00:25 +0000589
590char AliasSetPrinter::ID = 0;
591static RegisterPass<AliasSetPrinter>
592X("print-alias-sets", "Alias Set Printer", false, true);