blob: 8863297b31f304faab406806fb86d3cea04f929b [file] [log] [blame]
Chris Lattner009cc3d2002-09-26 21:49:07 +00001//===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner009cc3d2002-09-26 21:49:07 +00009//
10// This file implements the AliasSetTracker and AliasSet classes.
11//
12//===----------------------------------------------------------------------===//
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 Lattner31a9d182003-02-26 22:11:00 +000018#include "llvm/Target/TargetData.h"
Chris Lattner9971ac42003-02-24 20:37:56 +000019#include "llvm/Assembly/Writer.h"
20#include "llvm/Support/InstIterator.h"
Reid Spencer954da372004-07-04 12:19:56 +000021#include <iostream>
Chris Lattnere2609242003-12-14 04:52:11 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Chris Lattnercc8d5242004-11-27 18:37:42 +000024/// mergeSetIn - Merge the specified alias set into this alias set.
Chris Lattner009cc3d2002-09-26 21:49:07 +000025///
Chris Lattnercc8d5242004-11-27 18:37:42 +000026void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
Chris Lattner9971ac42003-02-24 20:37:56 +000027 assert(!AS.Forward && "Alias set is already forwarding!");
28 assert(!Forward && "This set is a forwarding set!!");
Chris Lattner009cc3d2002-09-26 21:49:07 +000029
30 // Update the alias and access types of this set...
Chris Lattner9971ac42003-02-24 20:37:56 +000031 AccessTy |= AS.AccessTy;
32 AliasTy |= AS.AliasTy;
33
Chris Lattnercc8d5242004-11-27 18:37:42 +000034 if (AliasTy == MustAlias) {
35 // Check that these two merged sets really are must aliases. Since both
36 // used to be must-alias sets, we can just check any pointer from each set
37 // for aliasing.
38 AliasAnalysis &AA = AST.getAliasAnalysis();
39 HashNodePair *L = getSomePointer();
40 HashNodePair *R = AS.getSomePointer();
41
42 // If the pointers are not a must-alias pair, this set becomes a may alias.
43 if (AA.alias(L->first, L->second.getSize(), R->first, R->second.getSize())
44 != AliasAnalysis::MustAlias)
45 AliasTy = MayAlias;
46 }
47
Chris Lattner9971ac42003-02-24 20:37:56 +000048 if (CallSites.empty()) { // Merge call sites...
49 if (!AS.CallSites.empty())
50 std::swap(CallSites, AS.CallSites);
51 } else if (!AS.CallSites.empty()) {
52 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
53 AS.CallSites.clear();
54 }
55
Chris Lattner9971ac42003-02-24 20:37:56 +000056 AS.Forward = this; // Forward across AS now...
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000057 addRef(); // AS is now pointing to us...
Chris Lattner9971ac42003-02-24 20:37:56 +000058
59 // Merge the list of constituent pointers...
Chris Lattner2cffeec2003-12-18 08:11:56 +000060 if (AS.PtrList) {
61 *PtrListEnd = AS.PtrList;
62 AS.PtrList->second.setPrevInList(PtrListEnd);
63 PtrListEnd = AS.PtrListEnd;
64
65 AS.PtrList = 0;
66 AS.PtrListEnd = &AS.PtrList;
Chris Lattner3080b602004-09-15 16:59:47 +000067 assert(*AS.PtrListEnd == 0 && "End of list is not null?");
Chris Lattner2cffeec2003-12-18 08:11:56 +000068 }
Chris Lattner009cc3d2002-09-26 21:49:07 +000069}
70
Chris Lattner9971ac42003-02-24 20:37:56 +000071void AliasSetTracker::removeAliasSet(AliasSet *AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000072 if (AliasSet *Fwd = AS->Forward) {
73 Fwd->dropRef(*this);
74 AS->Forward = 0;
75 }
Chris Lattner9971ac42003-02-24 20:37:56 +000076 AliasSets.erase(AS);
77}
78
79void AliasSet::removeFromTracker(AliasSetTracker &AST) {
80 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
81 AST.removeAliasSet(this);
82}
83
Chris Lattner31a9d182003-02-26 22:11:00 +000084void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
Chris Lattnerb66e6482004-09-14 19:15:32 +000085 unsigned Size, bool KnownMustAlias) {
Chris Lattner9971ac42003-02-24 20:37:56 +000086 assert(!Entry.second.hasAliasSet() && "Entry already in set!");
87
Chris Lattnerb66e6482004-09-14 19:15:32 +000088 // Check to see if we have to downgrade to _may_ alias.
89 if (isMustAlias() && !KnownMustAlias)
Chris Lattner31a9d182003-02-26 22:11:00 +000090 if (HashNodePair *P = getSomePointer()) {
Chris Lattner3080b602004-09-15 16:59:47 +000091 AliasAnalysis &AA = AST.getAliasAnalysis();
Chris Lattner31a9d182003-02-26 22:11:00 +000092 AliasAnalysis::AliasResult Result =
93 AA.alias(P->first, P->second.getSize(), Entry.first, Size);
94 if (Result == AliasAnalysis::MayAlias)
Chris Lattner9971ac42003-02-24 20:37:56 +000095 AliasTy = MayAlias;
Chris Lattner31a9d182003-02-26 22:11:00 +000096 else // First entry of must alias must have maximum size!
97 P->second.updateSize(Size);
98 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
99 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000100
101 Entry.second.setAliasSet(this);
Chris Lattner31a9d182003-02-26 22:11:00 +0000102 Entry.second.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000103
104 // Add it to the end of the list...
Chris Lattner2cffeec2003-12-18 08:11:56 +0000105 assert(*PtrListEnd == 0 && "End of list is not null?");
106 *PtrListEnd = &Entry;
107 PtrListEnd = Entry.second.setPrevInList(PtrListEnd);
Chris Lattner3080b602004-09-15 16:59:47 +0000108 assert(*PtrListEnd == 0 && "End of list is not null?");
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000109 addRef(); // Entry points to alias set...
Chris Lattner9971ac42003-02-24 20:37:56 +0000110}
111
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000112void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000113 CallSites.push_back(CS);
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000114
115 if (Function *F = CS.getCalledFunction()) {
Chris Lattner0af024c2004-12-15 07:22:13 +0000116 AliasAnalysis::ModRefBehavior Behavior = AA.getModRefBehavior(F, CS);
117 if (Behavior == AliasAnalysis::DoesNotAccessMemory)
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000118 return;
Chris Lattner0af024c2004-12-15 07:22:13 +0000119 else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000120 AliasTy = MayAlias;
Chris Lattner4781bc72004-03-17 23:22:04 +0000121 AccessTy |= Refs;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000122 return;
123 }
124 }
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 {
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000167 if (Function *F = CS.getCalledFunction())
168 if (AA.doesNotAccessMemory(F))
169 return false;
170
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),
270 AA.getTargetData().getTypeSize(LI->getType()),
271 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),
280 AA.getTargetData().getTypeSize(Val->getType()),
281 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;
288 AliasSet &AS = addPointer(FI->getOperand(0), ~0,
289 AliasSet::Mods, NewPtr);
290 return NewPtr;
291}
292
Chris Lattnere2609242003-12-14 04:52:11 +0000293
Chris Lattner12c11552004-07-21 05:18:04 +0000294bool AliasSetTracker::add(CallSite CS) {
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000295 if (Function *F = CS.getCalledFunction())
296 if (AA.doesNotAccessMemory(F))
Chris Lattner12c11552004-07-21 05:18:04 +0000297 return true; // doesn't alias anything
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000298
Chris Lattner9971ac42003-02-24 20:37:56 +0000299 AliasSet *AS = findAliasSetForCallSite(CS);
300 if (!AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000301 AliasSets.push_back(new AliasSet());
Chris Lattner9971ac42003-02-24 20:37:56 +0000302 AS = &AliasSets.back();
Chris Lattner12c11552004-07-21 05:18:04 +0000303 AS->addCallSite(CS, AA);
304 return true;
305 } else {
306 AS->addCallSite(CS, AA);
307 return false;
Chris Lattner9971ac42003-02-24 20:37:56 +0000308 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000309}
310
Chris Lattner12c11552004-07-21 05:18:04 +0000311bool AliasSetTracker::add(Instruction *I) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000312 // Dispatch to one of the other add methods...
313 if (LoadInst *LI = dyn_cast<LoadInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000314 return add(LI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000315 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000316 return add(SI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000317 else if (CallInst *CI = dyn_cast<CallInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000318 return add(CI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000319 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000320 return add(II);
Chris Lattner5c882602004-07-25 07:57:37 +0000321 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
322 return add(FI);
Chris Lattner12c11552004-07-21 05:18:04 +0000323 return true;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000324}
325
Chris Lattner319d05b2003-03-03 23:28:05 +0000326void AliasSetTracker::add(BasicBlock &BB) {
327 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
328 add(I);
329}
330
331void AliasSetTracker::add(const AliasSetTracker &AST) {
332 assert(&AA == &AST.AA &&
333 "Merging AliasSetTracker objects with different Alias Analyses!");
334
335 // Loop over all of the alias sets in AST, adding the pointers contained
336 // therein into the current alias sets. This can cause alias sets to be
337 // merged together in the current AST.
338 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
339 if (!I->Forward) { // Ignore forwarding alias sets
340 AliasSet &AS = const_cast<AliasSet&>(*I);
341
342 // If there are any call sites in the alias set, add them to this AST.
343 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
344 add(AS.CallSites[i]);
345
346 // Loop over all of the pointers in this alias set...
347 AliasSet::iterator I = AS.begin(), E = AS.end();
Chris Lattner12c11552004-07-21 05:18:04 +0000348 bool X;
Chris Lattner319d05b2003-03-03 23:28:05 +0000349 for (; I != E; ++I)
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000350 addPointer(I.getPointer(), I.getSize(),
Chris Lattner12c11552004-07-21 05:18:04 +0000351 (AliasSet::AccessType)AS.AccessTy, X);
Chris Lattner319d05b2003-03-03 23:28:05 +0000352 }
353}
354
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000355/// remove - Remove the specified (potentially non-empty) alias set from the
356/// tracker.
357void AliasSetTracker::remove(AliasSet &AS) {
358 bool SetDead;
359 do {
360 AliasSet::iterator I = AS.begin();
361 Value *Ptr = I.getPointer(); ++I;
362
363 // deleteValue will delete the set automatically when the last pointer
364 // reference is destroyed. "Predict" when this will happen.
365 SetDead = I == AS.end();
366 deleteValue(Ptr); // Delete all of the pointers from the set
367 } while (!SetDead);
368}
369
Chris Lattner61d46272004-07-26 05:50:23 +0000370bool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
371 AliasSet *AS = findAliasSetForPointer(Ptr, Size);
372 if (!AS) return false;
373 remove(*AS);
374 return true;
375}
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000376
377bool AliasSetTracker::remove(LoadInst *LI) {
378 unsigned Size = AA.getTargetData().getTypeSize(LI->getType());
379 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
380 if (!AS) return false;
381 remove(*AS);
382 return true;
383}
384
385bool AliasSetTracker::remove(StoreInst *SI) {
386 unsigned Size = AA.getTargetData().getTypeSize(SI->getOperand(0)->getType());
387 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
388 if (!AS) return false;
389 remove(*AS);
390 return true;
391}
392
Chris Lattner5c882602004-07-25 07:57:37 +0000393bool AliasSetTracker::remove(FreeInst *FI) {
394 AliasSet *AS = findAliasSetForPointer(FI->getOperand(0), ~0);
395 if (!AS) return false;
396 remove(*AS);
397 return true;
398}
399
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000400bool AliasSetTracker::remove(CallSite CS) {
401 if (Function *F = CS.getCalledFunction())
402 if (AA.doesNotAccessMemory(F))
403 return false; // doesn't alias anything
404
405 AliasSet *AS = findAliasSetForCallSite(CS);
406 if (!AS) return false;
407 remove(*AS);
408 return true;
409}
410
411bool AliasSetTracker::remove(Instruction *I) {
412 // Dispatch to one of the other remove methods...
413 if (LoadInst *LI = dyn_cast<LoadInst>(I))
414 return remove(LI);
415 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
416 return remove(SI);
417 else if (CallInst *CI = dyn_cast<CallInst>(I))
418 return remove(CI);
Chris Lattner5c882602004-07-25 07:57:37 +0000419 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
420 return remove(FI);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000421 return true;
422}
423
Chris Lattner2cffeec2003-12-18 08:11:56 +0000424
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000425// deleteValue method - This method is used to remove a pointer value from the
Chris Lattner2cffeec2003-12-18 08:11:56 +0000426// AliasSetTracker entirely. It should be used when an instruction is deleted
427// from the program to update the AST. If you don't use this, you would have
428// dangling pointers to deleted instructions.
429//
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000430void AliasSetTracker::deleteValue(Value *PtrVal) {
Chris Lattnerb66e6482004-09-14 19:15:32 +0000431 // Notify the alias analysis implementation that this value is gone.
432 AA.deleteValue(PtrVal);
433
434 // First, look up the PointerRec for this pointer.
Chris Lattner2cffeec2003-12-18 08:11:56 +0000435 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(PtrVal);
436 if (I == PointerMap.end()) return; // Noop
437
438 // If we found one, remove the pointer from the alias set it is in.
439 AliasSet::HashNodePair &PtrValEnt = *I;
440 AliasSet *AS = PtrValEnt.second.getAliasSet(*this);
441
442 // Unlink from the list of values...
443 PtrValEnt.second.removeFromList();
444 // Stop using the alias set
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000445 AS->dropRef(*this);
Chris Lattner2cffeec2003-12-18 08:11:56 +0000446 PointerMap.erase(I);
447}
448
Chris Lattnerb66e6482004-09-14 19:15:32 +0000449// copyValue - This method should be used whenever a preexisting value in the
450// program is copied or cloned, introducing a new value. Note that it is ok for
451// clients that use this method to introduce the same value multiple times: if
452// the tracker already knows about a value, it will ignore the request.
453//
454void AliasSetTracker::copyValue(Value *From, Value *To) {
455 // Notify the alias analysis implementation that this value is copied.
456 AA.copyValue(From, To);
457
458 // First, look up the PointerRec for this pointer.
459 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(From);
460 if (I == PointerMap.end() || !I->second.hasAliasSet())
461 return; // Noop
462
463 AliasSet::HashNodePair &Entry = getEntryFor(To);
464 if (Entry.second.hasAliasSet()) return; // Already in the tracker!
465
466 // Add it to the alias set it aliases...
467 AliasSet *AS = I->second.getAliasSet(*this);
468 AS->addPointer(*this, Entry, I->second.getSize(), true);
469}
470
471
Chris Lattner2cffeec2003-12-18 08:11:56 +0000472
Chris Lattner9971ac42003-02-24 20:37:56 +0000473//===----------------------------------------------------------------------===//
474// AliasSet/AliasSetTracker Printing Support
475//===----------------------------------------------------------------------===//
476
477void AliasSet::print(std::ostream &OS) const {
478 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
479 OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, ";
480 switch (AccessTy) {
481 case NoModRef: OS << "No access "; break;
482 case Refs : OS << "Ref "; break;
483 case Mods : OS << "Mod "; break;
484 case ModRef : OS << "Mod/Ref "; break;
485 default: assert(0 && "Bad value for AccessTy!");
Chris Lattner009cc3d2002-09-26 21:49:07 +0000486 }
Chris Lattnere2609242003-12-14 04:52:11 +0000487 if (isVolatile()) OS << "[volatile] ";
Chris Lattner9971ac42003-02-24 20:37:56 +0000488 if (Forward)
489 OS << " forwarding to " << (void*)Forward;
490
491
492 if (begin() != end()) {
493 OS << "Pointers: ";
494 for (iterator I = begin(), E = end(); I != E; ++I) {
495 if (I != begin()) OS << ", ";
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000496 WriteAsOperand(OS << "(", I.getPointer());
497 OS << ", " << I.getSize() << ")";
Chris Lattner9971ac42003-02-24 20:37:56 +0000498 }
499 }
500 if (!CallSites.empty()) {
501 OS << "\n " << CallSites.size() << " Call Sites: ";
502 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
503 if (i) OS << ", ";
504 WriteAsOperand(OS, CallSites[i].getCalledValue());
505 }
506 }
507 OS << "\n";
508}
509
510void AliasSetTracker::print(std::ostream &OS) const {
511 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
512 << PointerMap.size() << " pointer values.\n";
513 for (const_iterator I = begin(), E = end(); I != E; ++I)
514 I->print(OS);
515 OS << "\n";
516}
517
518void AliasSet::dump() const { print (std::cerr); }
519void AliasSetTracker::dump() const { print(std::cerr); }
520
Chris Lattner9971ac42003-02-24 20:37:56 +0000521//===----------------------------------------------------------------------===//
522// AliasSetPrinter Pass
523//===----------------------------------------------------------------------===//
524
525namespace {
526 class AliasSetPrinter : public FunctionPass {
527 AliasSetTracker *Tracker;
528 public:
529 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
530 AU.setPreservesAll();
531 AU.addRequired<AliasAnalysis>();
532 }
533
534 virtual bool runOnFunction(Function &F) {
535 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
536
537 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner6ffe5512004-04-27 15:13:33 +0000538 Tracker->add(&*I);
Chris Lattner9971ac42003-02-24 20:37:56 +0000539 return false;
540 }
541
542 /// print - Convert to human readable form
Reid Spencerce9653c2004-12-07 04:03:45 +0000543 virtual void print(std::ostream &OS, const Module* = 0) const {
Chris Lattner9971ac42003-02-24 20:37:56 +0000544 Tracker->print(OS);
545 }
546
547 virtual void releaseMemory() {
548 delete Tracker;
549 }
550 };
551 RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer",
552 PassInfo::Analysis | PassInfo::Optimization);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000553}