blob: 8eeb7f6c601a3457223ac30576a8403de9a5b6cd [file] [log] [blame]
Chris Lattner009cc3d2002-09-26 21:49:07 +00001//===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner009cc3d2002-09-26 21:49:07 +00009//
10// This file implements the AliasSetTracker and AliasSet classes.
Misha Brukman2b37d7c2005-04-21 21:13:18 +000011//
Chris Lattner009cc3d2002-09-26 21:49:07 +000012//===----------------------------------------------------------------------===//
13
14#include "llvm/Analysis/AliasSetTracker.h"
15#include "llvm/Analysis/AliasAnalysis.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000016#include "llvm/Instructions.h"
Zhou Sheng7e4286e2009-03-03 06:02:04 +000017#include "llvm/IntrinsicInst.h"
Chris Lattner9971ac42003-02-24 20:37:56 +000018#include "llvm/Pass.h"
Chris Lattner5b3a4552005-03-17 15:38:16 +000019#include "llvm/Type.h"
Chris Lattner31a9d182003-02-26 22:11:00 +000020#include "llvm/Target/TargetData.h"
Chris Lattner9971ac42003-02-24 20:37:56 +000021#include "llvm/Assembly/Writer.h"
Reid Spencerd7d83db2007-02-05 23:42:17 +000022#include "llvm/Support/Compiler.h"
Chris Lattner9971ac42003-02-24 20:37:56 +000023#include "llvm/Support/InstIterator.h"
Bill Wendling6f81b512006-11-28 22:46:12 +000024#include "llvm/Support/Streams.h"
Chris Lattnere2609242003-12-14 04:52:11 +000025using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000026
Chris Lattnercc8d5242004-11-27 18:37:42 +000027/// mergeSetIn - Merge the specified alias set into this alias set.
Chris Lattner009cc3d2002-09-26 21:49:07 +000028///
Chris Lattnercc8d5242004-11-27 18:37:42 +000029void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
Chris Lattner9971ac42003-02-24 20:37:56 +000030 assert(!AS.Forward && "Alias set is already forwarding!");
31 assert(!Forward && "This set is a forwarding set!!");
Chris Lattner009cc3d2002-09-26 21:49:07 +000032
33 // Update the alias and access types of this set...
Chris Lattner9971ac42003-02-24 20:37:56 +000034 AccessTy |= AS.AccessTy;
35 AliasTy |= AS.AliasTy;
36
Chris Lattnercc8d5242004-11-27 18:37:42 +000037 if (AliasTy == MustAlias) {
38 // Check that these two merged sets really are must aliases. Since both
39 // used to be must-alias sets, we can just check any pointer from each set
40 // for aliasing.
41 AliasAnalysis &AA = AST.getAliasAnalysis();
42 HashNodePair *L = getSomePointer();
43 HashNodePair *R = AS.getSomePointer();
44
45 // If the pointers are not a must-alias pair, this set becomes a may alias.
46 if (AA.alias(L->first, L->second.getSize(), R->first, R->second.getSize())
47 != AliasAnalysis::MustAlias)
48 AliasTy = MayAlias;
49 }
50
Chris Lattner9971ac42003-02-24 20:37:56 +000051 if (CallSites.empty()) { // Merge call sites...
52 if (!AS.CallSites.empty())
53 std::swap(CallSites, AS.CallSites);
54 } else if (!AS.CallSites.empty()) {
55 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
56 AS.CallSites.clear();
57 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000058
Chris Lattner9971ac42003-02-24 20:37:56 +000059 AS.Forward = this; // Forward across AS now...
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000060 addRef(); // AS is now pointing to us...
Chris Lattner9971ac42003-02-24 20:37:56 +000061
62 // Merge the list of constituent pointers...
Chris Lattner2cffeec2003-12-18 08:11:56 +000063 if (AS.PtrList) {
64 *PtrListEnd = AS.PtrList;
65 AS.PtrList->second.setPrevInList(PtrListEnd);
66 PtrListEnd = AS.PtrListEnd;
67
68 AS.PtrList = 0;
69 AS.PtrListEnd = &AS.PtrList;
Chris Lattner3080b602004-09-15 16:59:47 +000070 assert(*AS.PtrListEnd == 0 && "End of list is not null?");
Chris Lattner2cffeec2003-12-18 08:11:56 +000071 }
Chris Lattner009cc3d2002-09-26 21:49:07 +000072}
73
Chris Lattner9971ac42003-02-24 20:37:56 +000074void AliasSetTracker::removeAliasSet(AliasSet *AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000075 if (AliasSet *Fwd = AS->Forward) {
76 Fwd->dropRef(*this);
77 AS->Forward = 0;
78 }
Chris Lattner9971ac42003-02-24 20:37:56 +000079 AliasSets.erase(AS);
80}
81
82void AliasSet::removeFromTracker(AliasSetTracker &AST) {
83 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
84 AST.removeAliasSet(this);
85}
86
Chris Lattner31a9d182003-02-26 22:11:00 +000087void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
Chris Lattnerb66e6482004-09-14 19:15:32 +000088 unsigned Size, bool KnownMustAlias) {
Chris Lattner9971ac42003-02-24 20:37:56 +000089 assert(!Entry.second.hasAliasSet() && "Entry already in set!");
90
Chris Lattnerb66e6482004-09-14 19:15:32 +000091 // Check to see if we have to downgrade to _may_ alias.
92 if (isMustAlias() && !KnownMustAlias)
Chris Lattner31a9d182003-02-26 22:11:00 +000093 if (HashNodePair *P = getSomePointer()) {
Chris Lattner3080b602004-09-15 16:59:47 +000094 AliasAnalysis &AA = AST.getAliasAnalysis();
Chris Lattner31a9d182003-02-26 22:11:00 +000095 AliasAnalysis::AliasResult Result =
96 AA.alias(P->first, P->second.getSize(), Entry.first, Size);
97 if (Result == AliasAnalysis::MayAlias)
Chris Lattner9971ac42003-02-24 20:37:56 +000098 AliasTy = MayAlias;
Chris Lattner31a9d182003-02-26 22:11:00 +000099 else // First entry of must alias must have maximum size!
100 P->second.updateSize(Size);
101 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
102 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000103
104 Entry.second.setAliasSet(this);
Chris Lattner31a9d182003-02-26 22:11:00 +0000105 Entry.second.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000106
107 // Add it to the end of the list...
Chris Lattner2cffeec2003-12-18 08:11:56 +0000108 assert(*PtrListEnd == 0 && "End of list is not null?");
109 *PtrListEnd = &Entry;
110 PtrListEnd = Entry.second.setPrevInList(PtrListEnd);
Chris Lattner3080b602004-09-15 16:59:47 +0000111 assert(*PtrListEnd == 0 && "End of list is not null?");
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000112 addRef(); // Entry points to alias set...
Chris Lattner9971ac42003-02-24 20:37:56 +0000113}
114
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000115void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000116 CallSites.push_back(CS);
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000117
Duncan Sandsdff67102007-12-01 07:51:45 +0000118 AliasAnalysis::ModRefBehavior Behavior = AA.getModRefBehavior(CS);
119 if (Behavior == AliasAnalysis::DoesNotAccessMemory)
120 return;
121 else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
122 AliasTy = MayAlias;
123 AccessTy |= Refs;
124 return;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000125 }
126
127 // FIXME: This should use mod/ref information to make this not suck so bad
128 AliasTy = MayAlias;
Chris Lattner577385e2003-05-03 03:42:08 +0000129 AccessTy = ModRef;
Chris Lattner9971ac42003-02-24 20:37:56 +0000130}
131
132/// aliasesPointer - Return true if the specified pointer "may" (or must)
Chris Lattner009cc3d2002-09-26 21:49:07 +0000133/// alias one of the members in the set.
134///
Chris Lattner31a9d182003-02-26 22:11:00 +0000135bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
136 AliasAnalysis &AA) const {
Chris Lattner9971ac42003-02-24 20:37:56 +0000137 if (AliasTy == MustAlias) {
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000138 assert(CallSites.empty() && "Illegal must alias set!");
139
Chris Lattner9971ac42003-02-24 20:37:56 +0000140 // If this is a set of MustAliases, only check to see if the pointer aliases
141 // SOME value in the set...
Chris Lattner31a9d182003-02-26 22:11:00 +0000142 HashNodePair *SomePtr = getSomePointer();
Chris Lattner9971ac42003-02-24 20:37:56 +0000143 assert(SomePtr && "Empty must-alias set??");
Chris Lattner31a9d182003-02-26 22:11:00 +0000144 return AA.alias(SomePtr->first, SomePtr->second.getSize(), Ptr, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000145 }
146
147 // If this is a may-alias set, we have to check all of the pointers in the set
148 // to be sure it doesn't alias the set...
149 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000150 if (AA.alias(Ptr, Size, I.getPointer(), I.getSize()))
Chris Lattner9971ac42003-02-24 20:37:56 +0000151 return true;
152
153 // Check the call sites list and invoke list...
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000154 if (!CallSites.empty()) {
155 if (AA.hasNoModRefInfoForCalls())
156 return true;
157
158 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
159 if (AA.getModRefInfo(CallSites[i], const_cast<Value*>(Ptr), Size)
160 != AliasAnalysis::NoModRef)
161 return true;
162 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000163
Chris Lattner009cc3d2002-09-26 21:49:07 +0000164 return false;
165}
166
Chris Lattner9971ac42003-02-24 20:37:56 +0000167bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
Duncan Sandsdff67102007-12-01 07:51:45 +0000168 if (AA.doesNotAccessMemory(CS))
169 return false;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000170
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000171 if (AA.hasNoModRefInfoForCalls())
172 return true;
173
174 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
175 if (AA.getModRefInfo(CallSites[i], CS) != AliasAnalysis::NoModRef ||
176 AA.getModRefInfo(CS, CallSites[i]) != AliasAnalysis::NoModRef)
177 return true;
178
179 for (iterator I = begin(), E = end(); I != E; ++I)
180 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
181 AliasAnalysis::NoModRef)
182 return true;
183
184 return false;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000185}
186
187
Chris Lattner009cc3d2002-09-26 21:49:07 +0000188/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
189/// instruction referring to the pointer into. If there are multiple alias sets
190/// that may alias the pointer, merge them together and return the unified set.
191///
Chris Lattner31a9d182003-02-26 22:11:00 +0000192AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
193 unsigned Size) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000194 AliasSet *FoundSet = 0;
Chris Lattner9971ac42003-02-24 20:37:56 +0000195 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner31a9d182003-02-26 22:11:00 +0000196 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
Chris Lattnercc8d5242004-11-27 18:37:42 +0000197 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
Chris Lattner9971ac42003-02-24 20:37:56 +0000198 FoundSet = I; // Remember it.
Chris Lattnercc8d5242004-11-27 18:37:42 +0000199 } else { // Otherwise, we must merge the sets.
200 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
Chris Lattner009cc3d2002-09-26 21:49:07 +0000201 }
202 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000203
204 return FoundSet;
205}
206
Chris Lattner07bfa522004-11-26 21:36:25 +0000207/// containsPointer - Return true if the specified location is represented by
208/// this alias set, false otherwise. This does not modify the AST object or
209/// alias sets.
210bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const {
211 for (const_iterator I = begin(), E = end(); I != E; ++I)
212 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA))
213 return true;
214 return false;
215}
216
217
218
Chris Lattner9971ac42003-02-24 20:37:56 +0000219AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
220 AliasSet *FoundSet = 0;
221 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000222 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
Chris Lattnercc8d5242004-11-27 18:37:42 +0000223 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
Chris Lattner9971ac42003-02-24 20:37:56 +0000224 FoundSet = I; // Remember it.
Chris Lattnercc8d5242004-11-27 18:37:42 +0000225 } else if (!I->Forward) { // Otherwise, we must merge the sets.
226 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
Chris Lattner9971ac42003-02-24 20:37:56 +0000227 }
228 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000229
230 return FoundSet;
231}
232
233
Chris Lattner009cc3d2002-09-26 21:49:07 +0000234
Chris Lattner9971ac42003-02-24 20:37:56 +0000235
236/// getAliasSetForPointer - Return the alias set that the specified pointer
237/// lives in...
Chris Lattner12c11552004-07-21 05:18:04 +0000238AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
239 bool *New) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000240 AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
241
242 // Check to see if the pointer is already known...
Chris Lattner34a10052004-07-25 18:32:01 +0000243 if (Entry.second.hasAliasSet()) {
244 Entry.second.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000245 // Return the set!
246 return *Entry.second.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattner31a9d182003-02-26 22:11:00 +0000247 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000248 // Add it to the alias set it aliases...
Chris Lattner31a9d182003-02-26 22:11:00 +0000249 AS->addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000250 return *AS;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000251 } else {
Chris Lattner12c11552004-07-21 05:18:04 +0000252 if (New) *New = true;
Chris Lattner9971ac42003-02-24 20:37:56 +0000253 // Otherwise create a new alias set to hold the loaded pointer...
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000254 AliasSets.push_back(new AliasSet());
Chris Lattner31a9d182003-02-26 22:11:00 +0000255 AliasSets.back().addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000256 return AliasSets.back();
Chris Lattner009cc3d2002-09-26 21:49:07 +0000257 }
258}
259
Chris Lattner61d46272004-07-26 05:50:23 +0000260bool AliasSetTracker::add(Value *Ptr, unsigned Size) {
261 bool NewPtr;
262 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
263 return NewPtr;
264}
265
266
Chris Lattner12c11552004-07-21 05:18:04 +0000267bool AliasSetTracker::add(LoadInst *LI) {
268 bool NewPtr;
269 AliasSet &AS = addPointer(LI->getOperand(0),
Duncan Sands514ab342007-11-01 20:53:16 +0000270 AA.getTargetData().getTypeStoreSize(LI->getType()),
Chris Lattner12c11552004-07-21 05:18:04 +0000271 AliasSet::Refs, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000272 if (LI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000273 return NewPtr;
Chris Lattner9971ac42003-02-24 20:37:56 +0000274}
275
Chris Lattner12c11552004-07-21 05:18:04 +0000276bool AliasSetTracker::add(StoreInst *SI) {
277 bool NewPtr;
278 Value *Val = SI->getOperand(0);
279 AliasSet &AS = addPointer(SI->getOperand(1),
Duncan Sands514ab342007-11-01 20:53:16 +0000280 AA.getTargetData().getTypeStoreSize(Val->getType()),
Chris Lattner12c11552004-07-21 05:18:04 +0000281 AliasSet::Mods, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000282 if (SI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000283 return NewPtr;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000284}
285
Chris Lattner5c882602004-07-25 07:57:37 +0000286bool AliasSetTracker::add(FreeInst *FI) {
287 bool NewPtr;
Chris Lattner05a24e52008-05-22 03:23:06 +0000288 addPointer(FI->getOperand(0), ~0, AliasSet::Mods, NewPtr);
Chris Lattner5c882602004-07-25 07:57:37 +0000289 return NewPtr;
290}
291
Dan Gohman235fc572008-04-14 18:34:50 +0000292bool AliasSetTracker::add(VAArgInst *VAAI) {
293 bool NewPtr;
Chris Lattner05a24e52008-05-22 03:23:06 +0000294 addPointer(VAAI->getOperand(0), ~0, AliasSet::ModRef, NewPtr);
Dan Gohman235fc572008-04-14 18:34:50 +0000295 return NewPtr;
296}
297
Chris Lattnere2609242003-12-14 04:52:11 +0000298
Chris Lattner12c11552004-07-21 05:18:04 +0000299bool AliasSetTracker::add(CallSite CS) {
Zhou Sheng7e4286e2009-03-03 06:02:04 +0000300 if (isa<DbgInfoIntrinsic>(CS.getInstruction()))
301 return true; // Ignore DbgInfo Intrinsics.
Duncan Sandsdff67102007-12-01 07:51:45 +0000302 if (AA.doesNotAccessMemory(CS))
303 return true; // doesn't alias anything
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000304
Chris Lattner9971ac42003-02-24 20:37:56 +0000305 AliasSet *AS = findAliasSetForCallSite(CS);
306 if (!AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000307 AliasSets.push_back(new AliasSet());
Chris Lattner9971ac42003-02-24 20:37:56 +0000308 AS = &AliasSets.back();
Chris Lattner12c11552004-07-21 05:18:04 +0000309 AS->addCallSite(CS, AA);
310 return true;
311 } else {
312 AS->addCallSite(CS, AA);
313 return false;
Chris Lattner9971ac42003-02-24 20:37:56 +0000314 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000315}
316
Chris Lattner12c11552004-07-21 05:18:04 +0000317bool AliasSetTracker::add(Instruction *I) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000318 // Dispatch to one of the other add methods...
319 if (LoadInst *LI = dyn_cast<LoadInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000320 return add(LI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000321 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000322 return add(SI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000323 else if (CallInst *CI = dyn_cast<CallInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000324 return add(CI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000325 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000326 return add(II);
Chris Lattner5c882602004-07-25 07:57:37 +0000327 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
328 return add(FI);
Dan Gohman235fc572008-04-14 18:34:50 +0000329 else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
330 return add(VAAI);
Chris Lattner12c11552004-07-21 05:18:04 +0000331 return true;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000332}
333
Chris Lattner319d05b2003-03-03 23:28:05 +0000334void AliasSetTracker::add(BasicBlock &BB) {
335 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
336 add(I);
337}
338
339void AliasSetTracker::add(const AliasSetTracker &AST) {
340 assert(&AA == &AST.AA &&
341 "Merging AliasSetTracker objects with different Alias Analyses!");
342
343 // Loop over all of the alias sets in AST, adding the pointers contained
344 // therein into the current alias sets. This can cause alias sets to be
345 // merged together in the current AST.
346 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
347 if (!I->Forward) { // Ignore forwarding alias sets
348 AliasSet &AS = const_cast<AliasSet&>(*I);
349
350 // If there are any call sites in the alias set, add them to this AST.
351 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
352 add(AS.CallSites[i]);
353
354 // Loop over all of the pointers in this alias set...
355 AliasSet::iterator I = AS.begin(), E = AS.end();
Chris Lattner12c11552004-07-21 05:18:04 +0000356 bool X;
Chris Lattnerf711fb72007-05-23 06:36:35 +0000357 for (; I != E; ++I) {
358 AliasSet &NewAS = addPointer(I.getPointer(), I.getSize(),
359 (AliasSet::AccessType)AS.AccessTy, X);
360 if (AS.isVolatile()) NewAS.setVolatile();
361 }
Chris Lattner319d05b2003-03-03 23:28:05 +0000362 }
363}
364
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000365/// remove - Remove the specified (potentially non-empty) alias set from the
366/// tracker.
367void AliasSetTracker::remove(AliasSet &AS) {
Chris Lattnerf2998572006-06-27 23:48:59 +0000368 // Drop all call sites.
369 AS.CallSites.clear();
370
371 // Clear the alias set.
372 unsigned NumRefs = 0;
373 while (!AS.empty()) {
374 AliasSet::HashNodePair *P = AS.PtrList;
375
376 // Unlink from the list of values.
377 P->second.removeFromList();
378
379 // Remember how many references need to be dropped.
380 ++NumRefs;
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000381
Chris Lattnerf2998572006-06-27 23:48:59 +0000382 // Finally, remove the entry.
Nick Lewyckye81f7252006-09-17 17:51:00 +0000383 Value *Remove = P->first; // Take a copy because it is invalid to pass
384 PointerMap.erase(Remove); // a reference to the data being erased.
Chris Lattnerf2998572006-06-27 23:48:59 +0000385 }
386
387 // Stop using the alias set, removing it.
Chris Lattner356d8c22006-06-27 23:56:13 +0000388 AS.RefCount -= NumRefs;
389 if (AS.RefCount == 0)
390 AS.removeFromTracker(*this);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000391}
392
Chris Lattner61d46272004-07-26 05:50:23 +0000393bool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
394 AliasSet *AS = findAliasSetForPointer(Ptr, Size);
395 if (!AS) return false;
396 remove(*AS);
397 return true;
398}
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000399
400bool AliasSetTracker::remove(LoadInst *LI) {
Duncan Sands514ab342007-11-01 20:53:16 +0000401 unsigned Size = AA.getTargetData().getTypeStoreSize(LI->getType());
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000402 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
403 if (!AS) return false;
404 remove(*AS);
405 return true;
406}
407
408bool AliasSetTracker::remove(StoreInst *SI) {
Duncan Sands514ab342007-11-01 20:53:16 +0000409 unsigned Size =
410 AA.getTargetData().getTypeStoreSize(SI->getOperand(0)->getType());
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000411 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
412 if (!AS) return false;
413 remove(*AS);
414 return true;
415}
416
Chris Lattner5c882602004-07-25 07:57:37 +0000417bool AliasSetTracker::remove(FreeInst *FI) {
418 AliasSet *AS = findAliasSetForPointer(FI->getOperand(0), ~0);
419 if (!AS) return false;
420 remove(*AS);
421 return true;
422}
423
Dan Gohman235fc572008-04-14 18:34:50 +0000424bool AliasSetTracker::remove(VAArgInst *VAAI) {
425 AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0), ~0);
426 if (!AS) return false;
427 remove(*AS);
428 return true;
429}
430
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000431bool AliasSetTracker::remove(CallSite CS) {
Duncan Sandsdff67102007-12-01 07:51:45 +0000432 if (AA.doesNotAccessMemory(CS))
433 return false; // doesn't alias anything
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000434
435 AliasSet *AS = findAliasSetForCallSite(CS);
436 if (!AS) return false;
437 remove(*AS);
438 return true;
439}
440
441bool AliasSetTracker::remove(Instruction *I) {
442 // Dispatch to one of the other remove methods...
443 if (LoadInst *LI = dyn_cast<LoadInst>(I))
444 return remove(LI);
445 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
446 return remove(SI);
447 else if (CallInst *CI = dyn_cast<CallInst>(I))
448 return remove(CI);
Chris Lattner5c882602004-07-25 07:57:37 +0000449 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
450 return remove(FI);
Dan Gohman235fc572008-04-14 18:34:50 +0000451 else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
452 return remove(VAAI);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000453 return true;
454}
455
Chris Lattner2cffeec2003-12-18 08:11:56 +0000456
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000457// deleteValue method - This method is used to remove a pointer value from the
Chris Lattner2cffeec2003-12-18 08:11:56 +0000458// AliasSetTracker entirely. It should be used when an instruction is deleted
459// from the program to update the AST. If you don't use this, you would have
460// dangling pointers to deleted instructions.
461//
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000462void AliasSetTracker::deleteValue(Value *PtrVal) {
Chris Lattnerb66e6482004-09-14 19:15:32 +0000463 // Notify the alias analysis implementation that this value is gone.
464 AA.deleteValue(PtrVal);
465
Chris Lattner959e3212006-06-26 19:20:48 +0000466 // If this is a call instruction, remove the callsite from the appropriate
467 // AliasSet.
468 CallSite CS = CallSite::get(PtrVal);
Duncan Sandsdff67102007-12-01 07:51:45 +0000469 if (CS.getInstruction())
470 if (!AA.doesNotAccessMemory(CS))
Chris Lattner959e3212006-06-26 19:20:48 +0000471 if (AliasSet *AS = findAliasSetForCallSite(CS))
472 AS->removeCallSite(CS);
Chris Lattner959e3212006-06-26 19:20:48 +0000473
Chris Lattnerb66e6482004-09-14 19:15:32 +0000474 // First, look up the PointerRec for this pointer.
Chris Lattner2cffeec2003-12-18 08:11:56 +0000475 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(PtrVal);
476 if (I == PointerMap.end()) return; // Noop
477
478 // If we found one, remove the pointer from the alias set it is in.
479 AliasSet::HashNodePair &PtrValEnt = *I;
480 AliasSet *AS = PtrValEnt.second.getAliasSet(*this);
481
482 // Unlink from the list of values...
483 PtrValEnt.second.removeFromList();
484 // Stop using the alias set
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000485 AS->dropRef(*this);
Chris Lattner2cffeec2003-12-18 08:11:56 +0000486 PointerMap.erase(I);
487}
488
Chris Lattnerb66e6482004-09-14 19:15:32 +0000489// copyValue - This method should be used whenever a preexisting value in the
490// program is copied or cloned, introducing a new value. Note that it is ok for
491// clients that use this method to introduce the same value multiple times: if
492// the tracker already knows about a value, it will ignore the request.
493//
494void AliasSetTracker::copyValue(Value *From, Value *To) {
495 // Notify the alias analysis implementation that this value is copied.
496 AA.copyValue(From, To);
497
498 // First, look up the PointerRec for this pointer.
499 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(From);
500 if (I == PointerMap.end() || !I->second.hasAliasSet())
501 return; // Noop
502
503 AliasSet::HashNodePair &Entry = getEntryFor(To);
504 if (Entry.second.hasAliasSet()) return; // Already in the tracker!
505
506 // Add it to the alias set it aliases...
507 AliasSet *AS = I->second.getAliasSet(*this);
508 AS->addPointer(*this, Entry, I->second.getSize(), true);
509}
510
511
Chris Lattner2cffeec2003-12-18 08:11:56 +0000512
Chris Lattner9971ac42003-02-24 20:37:56 +0000513//===----------------------------------------------------------------------===//
514// AliasSet/AliasSetTracker Printing Support
515//===----------------------------------------------------------------------===//
516
517void AliasSet::print(std::ostream &OS) const {
518 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
Dan Gohman92c7b652008-04-21 19:48:48 +0000519 OS << (AliasTy == MustAlias ? "must" : "may") << " alias, ";
Chris Lattner9971ac42003-02-24 20:37:56 +0000520 switch (AccessTy) {
521 case NoModRef: OS << "No access "; break;
522 case Refs : OS << "Ref "; break;
523 case Mods : OS << "Mod "; break;
524 case ModRef : OS << "Mod/Ref "; break;
525 default: assert(0 && "Bad value for AccessTy!");
Chris Lattner009cc3d2002-09-26 21:49:07 +0000526 }
Chris Lattnere2609242003-12-14 04:52:11 +0000527 if (isVolatile()) OS << "[volatile] ";
Chris Lattner9971ac42003-02-24 20:37:56 +0000528 if (Forward)
529 OS << " forwarding to " << (void*)Forward;
530
531
Dan Gohmancb406c22007-10-03 19:26:29 +0000532 if (!empty()) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000533 OS << "Pointers: ";
534 for (iterator I = begin(), E = end(); I != E; ++I) {
535 if (I != begin()) OS << ", ";
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000536 WriteAsOperand(OS << "(", I.getPointer());
537 OS << ", " << I.getSize() << ")";
Chris Lattner9971ac42003-02-24 20:37:56 +0000538 }
539 }
540 if (!CallSites.empty()) {
541 OS << "\n " << CallSites.size() << " Call Sites: ";
542 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
543 if (i) OS << ", ";
544 WriteAsOperand(OS, CallSites[i].getCalledValue());
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000545 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000546 }
547 OS << "\n";
548}
549
550void AliasSetTracker::print(std::ostream &OS) const {
551 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
552 << PointerMap.size() << " pointer values.\n";
553 for (const_iterator I = begin(), E = end(); I != E; ++I)
554 I->print(OS);
555 OS << "\n";
556}
557
Bill Wendlinge8156192006-12-07 01:30:32 +0000558void AliasSet::dump() const { print (cerr); }
559void AliasSetTracker::dump() const { print(cerr); }
Chris Lattner9971ac42003-02-24 20:37:56 +0000560
Chris Lattner9971ac42003-02-24 20:37:56 +0000561//===----------------------------------------------------------------------===//
562// AliasSetPrinter Pass
563//===----------------------------------------------------------------------===//
564
565namespace {
Reid Spencerd7d83db2007-02-05 23:42:17 +0000566 class VISIBILITY_HIDDEN AliasSetPrinter : public FunctionPass {
Chris Lattner9971ac42003-02-24 20:37:56 +0000567 AliasSetTracker *Tracker;
568 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +0000569 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +0000570 AliasSetPrinter() : FunctionPass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +0000571
Chris Lattner9971ac42003-02-24 20:37:56 +0000572 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
573 AU.setPreservesAll();
574 AU.addRequired<AliasAnalysis>();
575 }
576
577 virtual bool runOnFunction(Function &F) {
578 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
579
580 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner6ffe5512004-04-27 15:13:33 +0000581 Tracker->add(&*I);
Bill Wendlinge8156192006-12-07 01:30:32 +0000582 Tracker->print(cerr);
Chris Lattner4983cf72006-01-03 06:05:22 +0000583 delete Tracker;
Chris Lattner9971ac42003-02-24 20:37:56 +0000584 return false;
585 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000586 };
Chris Lattner009cc3d2002-09-26 21:49:07 +0000587}
Dan Gohman844731a2008-05-13 00:00:25 +0000588
589char AliasSetPrinter::ID = 0;
590static RegisterPass<AliasSetPrinter>
591X("print-alias-sets", "Alias Set Printer", false, true);