blob: b351874e883fae05e200ae03b860a4c50fea0d2b [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 Lattner009cc3d2002-09-26 21:49:07 +000024/// mergeSetIn - Merge the specified alias set into this alias set...
25///
Chris Lattner9971ac42003-02-24 20:37:56 +000026void AliasSet::mergeSetIn(AliasSet &AS) {
27 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
34 if (CallSites.empty()) { // Merge call sites...
35 if (!AS.CallSites.empty())
36 std::swap(CallSites, AS.CallSites);
37 } else if (!AS.CallSites.empty()) {
38 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
39 AS.CallSites.clear();
40 }
41
Chris Lattner9971ac42003-02-24 20:37:56 +000042 AS.Forward = this; // Forward across AS now...
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000043 addRef(); // AS is now pointing to us...
Chris Lattner9971ac42003-02-24 20:37:56 +000044
45 // Merge the list of constituent pointers...
Chris Lattner2cffeec2003-12-18 08:11:56 +000046 if (AS.PtrList) {
47 *PtrListEnd = AS.PtrList;
48 AS.PtrList->second.setPrevInList(PtrListEnd);
49 PtrListEnd = AS.PtrListEnd;
50
51 AS.PtrList = 0;
52 AS.PtrListEnd = &AS.PtrList;
Chris Lattner3080b602004-09-15 16:59:47 +000053 assert(*AS.PtrListEnd == 0 && "End of list is not null?");
Chris Lattner2cffeec2003-12-18 08:11:56 +000054 }
Chris Lattner009cc3d2002-09-26 21:49:07 +000055}
56
Chris Lattner9971ac42003-02-24 20:37:56 +000057void AliasSetTracker::removeAliasSet(AliasSet *AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000058 if (AliasSet *Fwd = AS->Forward) {
59 Fwd->dropRef(*this);
60 AS->Forward = 0;
61 }
Chris Lattner9971ac42003-02-24 20:37:56 +000062 AliasSets.erase(AS);
63}
64
65void AliasSet::removeFromTracker(AliasSetTracker &AST) {
66 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
67 AST.removeAliasSet(this);
68}
69
Chris Lattner31a9d182003-02-26 22:11:00 +000070void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
Chris Lattnerb66e6482004-09-14 19:15:32 +000071 unsigned Size, bool KnownMustAlias) {
Chris Lattner9971ac42003-02-24 20:37:56 +000072 assert(!Entry.second.hasAliasSet() && "Entry already in set!");
73
Chris Lattnerb66e6482004-09-14 19:15:32 +000074 // Check to see if we have to downgrade to _may_ alias.
75 if (isMustAlias() && !KnownMustAlias)
Chris Lattner31a9d182003-02-26 22:11:00 +000076 if (HashNodePair *P = getSomePointer()) {
Chris Lattner3080b602004-09-15 16:59:47 +000077 AliasAnalysis &AA = AST.getAliasAnalysis();
Chris Lattner31a9d182003-02-26 22:11:00 +000078 AliasAnalysis::AliasResult Result =
79 AA.alias(P->first, P->second.getSize(), Entry.first, Size);
80 if (Result == AliasAnalysis::MayAlias)
Chris Lattner9971ac42003-02-24 20:37:56 +000081 AliasTy = MayAlias;
Chris Lattner31a9d182003-02-26 22:11:00 +000082 else // First entry of must alias must have maximum size!
83 P->second.updateSize(Size);
84 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
85 }
Chris Lattner9971ac42003-02-24 20:37:56 +000086
87 Entry.second.setAliasSet(this);
Chris Lattner31a9d182003-02-26 22:11:00 +000088 Entry.second.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +000089
90 // Add it to the end of the list...
Chris Lattner2cffeec2003-12-18 08:11:56 +000091 assert(*PtrListEnd == 0 && "End of list is not null?");
92 *PtrListEnd = &Entry;
93 PtrListEnd = Entry.second.setPrevInList(PtrListEnd);
Chris Lattner3080b602004-09-15 16:59:47 +000094 assert(*PtrListEnd == 0 && "End of list is not null?");
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000095 addRef(); // Entry points to alias set...
Chris Lattner9971ac42003-02-24 20:37:56 +000096}
97
Chris Lattner5b5f7c12004-03-15 04:08:36 +000098void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
Chris Lattner9971ac42003-02-24 20:37:56 +000099 CallSites.push_back(CS);
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000100
101 if (Function *F = CS.getCalledFunction()) {
102 if (AA.doesNotAccessMemory(F))
103 return;
104 else if (AA.onlyReadsMemory(F)) {
105 AliasTy = MayAlias;
Chris Lattner4781bc72004-03-17 23:22:04 +0000106 AccessTy |= Refs;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000107 return;
108 }
109 }
110
111 // FIXME: This should use mod/ref information to make this not suck so bad
112 AliasTy = MayAlias;
Chris Lattner577385e2003-05-03 03:42:08 +0000113 AccessTy = ModRef;
Chris Lattner9971ac42003-02-24 20:37:56 +0000114}
115
116/// aliasesPointer - Return true if the specified pointer "may" (or must)
Chris Lattner009cc3d2002-09-26 21:49:07 +0000117/// alias one of the members in the set.
118///
Chris Lattner31a9d182003-02-26 22:11:00 +0000119bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
120 AliasAnalysis &AA) const {
Chris Lattner9971ac42003-02-24 20:37:56 +0000121 if (AliasTy == MustAlias) {
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000122 assert(CallSites.empty() && "Illegal must alias set!");
123
Chris Lattner9971ac42003-02-24 20:37:56 +0000124 // If this is a set of MustAliases, only check to see if the pointer aliases
125 // SOME value in the set...
Chris Lattner31a9d182003-02-26 22:11:00 +0000126 HashNodePair *SomePtr = getSomePointer();
Chris Lattner9971ac42003-02-24 20:37:56 +0000127 assert(SomePtr && "Empty must-alias set??");
Chris Lattner31a9d182003-02-26 22:11:00 +0000128 return AA.alias(SomePtr->first, SomePtr->second.getSize(), Ptr, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000129 }
130
131 // If this is a may-alias set, we have to check all of the pointers in the set
132 // to be sure it doesn't alias the set...
133 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000134 if (AA.alias(Ptr, Size, I.getPointer(), I.getSize()))
Chris Lattner9971ac42003-02-24 20:37:56 +0000135 return true;
136
137 // Check the call sites list and invoke list...
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000138 if (!CallSites.empty()) {
139 if (AA.hasNoModRefInfoForCalls())
140 return true;
141
142 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
143 if (AA.getModRefInfo(CallSites[i], const_cast<Value*>(Ptr), Size)
144 != AliasAnalysis::NoModRef)
145 return true;
146 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000147
Chris Lattner009cc3d2002-09-26 21:49:07 +0000148 return false;
149}
150
Chris Lattner9971ac42003-02-24 20:37:56 +0000151bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000152 if (Function *F = CS.getCalledFunction())
153 if (AA.doesNotAccessMemory(F))
154 return false;
155
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000156 if (AA.hasNoModRefInfoForCalls())
157 return true;
158
159 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
160 if (AA.getModRefInfo(CallSites[i], CS) != AliasAnalysis::NoModRef ||
161 AA.getModRefInfo(CS, CallSites[i]) != AliasAnalysis::NoModRef)
162 return true;
163
164 for (iterator I = begin(), E = end(); I != E; ++I)
165 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
166 AliasAnalysis::NoModRef)
167 return true;
168
169 return false;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000170}
171
172
Chris Lattner009cc3d2002-09-26 21:49:07 +0000173/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
174/// instruction referring to the pointer into. If there are multiple alias sets
175/// that may alias the pointer, merge them together and return the unified set.
176///
Chris Lattner31a9d182003-02-26 22:11:00 +0000177AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
178 unsigned Size) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000179 AliasSet *FoundSet = 0;
Chris Lattner9971ac42003-02-24 20:37:56 +0000180 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner31a9d182003-02-26 22:11:00 +0000181 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000182 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
Chris Lattner9971ac42003-02-24 20:37:56 +0000183 FoundSet = I; // Remember it.
Chris Lattner009cc3d2002-09-26 21:49:07 +0000184 } else { // Otherwise, we must merge the sets...
Chris Lattner9971ac42003-02-24 20:37:56 +0000185 FoundSet->mergeSetIn(*I); // Merge in contents...
Chris Lattner009cc3d2002-09-26 21:49:07 +0000186 }
187 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000188
189 return FoundSet;
190}
191
Chris Lattner07bfa522004-11-26 21:36:25 +0000192/// containsPointer - Return true if the specified location is represented by
193/// this alias set, false otherwise. This does not modify the AST object or
194/// alias sets.
195bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const {
196 for (const_iterator I = begin(), E = end(); I != E; ++I)
197 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA))
198 return true;
199 return false;
200}
201
202
203
Chris Lattner9971ac42003-02-24 20:37:56 +0000204AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
205 AliasSet *FoundSet = 0;
206 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000207 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000208 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
209 FoundSet = I; // Remember it.
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000210 } else if (!I->Forward) { // Otherwise, we must merge the sets...
Chris Lattner9971ac42003-02-24 20:37:56 +0000211 FoundSet->mergeSetIn(*I); // Merge in contents...
212 }
213 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000214
215 return FoundSet;
216}
217
218
Chris Lattner009cc3d2002-09-26 21:49:07 +0000219
Chris Lattner9971ac42003-02-24 20:37:56 +0000220
221/// getAliasSetForPointer - Return the alias set that the specified pointer
222/// lives in...
Chris Lattner12c11552004-07-21 05:18:04 +0000223AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
224 bool *New) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000225 AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
226
227 // Check to see if the pointer is already known...
Chris Lattner34a10052004-07-25 18:32:01 +0000228 if (Entry.second.hasAliasSet()) {
229 Entry.second.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000230 // Return the set!
231 return *Entry.second.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattner31a9d182003-02-26 22:11:00 +0000232 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000233 // Add it to the alias set it aliases...
Chris Lattner31a9d182003-02-26 22:11:00 +0000234 AS->addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000235 return *AS;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000236 } else {
Chris Lattner12c11552004-07-21 05:18:04 +0000237 if (New) *New = true;
Chris Lattner9971ac42003-02-24 20:37:56 +0000238 // Otherwise create a new alias set to hold the loaded pointer...
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000239 AliasSets.push_back(new AliasSet());
Chris Lattner31a9d182003-02-26 22:11:00 +0000240 AliasSets.back().addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000241 return AliasSets.back();
Chris Lattner009cc3d2002-09-26 21:49:07 +0000242 }
243}
244
Chris Lattner61d46272004-07-26 05:50:23 +0000245bool AliasSetTracker::add(Value *Ptr, unsigned Size) {
246 bool NewPtr;
247 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
248 return NewPtr;
249}
250
251
Chris Lattner12c11552004-07-21 05:18:04 +0000252bool AliasSetTracker::add(LoadInst *LI) {
253 bool NewPtr;
254 AliasSet &AS = addPointer(LI->getOperand(0),
255 AA.getTargetData().getTypeSize(LI->getType()),
256 AliasSet::Refs, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000257 if (LI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000258 return NewPtr;
Chris Lattner9971ac42003-02-24 20:37:56 +0000259}
260
Chris Lattner12c11552004-07-21 05:18:04 +0000261bool AliasSetTracker::add(StoreInst *SI) {
262 bool NewPtr;
263 Value *Val = SI->getOperand(0);
264 AliasSet &AS = addPointer(SI->getOperand(1),
265 AA.getTargetData().getTypeSize(Val->getType()),
266 AliasSet::Mods, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000267 if (SI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000268 return NewPtr;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000269}
270
Chris Lattner5c882602004-07-25 07:57:37 +0000271bool AliasSetTracker::add(FreeInst *FI) {
272 bool NewPtr;
273 AliasSet &AS = addPointer(FI->getOperand(0), ~0,
274 AliasSet::Mods, NewPtr);
275 return NewPtr;
276}
277
Chris Lattnere2609242003-12-14 04:52:11 +0000278
Chris Lattner12c11552004-07-21 05:18:04 +0000279bool AliasSetTracker::add(CallSite CS) {
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000280 if (Function *F = CS.getCalledFunction())
281 if (AA.doesNotAccessMemory(F))
Chris Lattner12c11552004-07-21 05:18:04 +0000282 return true; // doesn't alias anything
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000283
Chris Lattner9971ac42003-02-24 20:37:56 +0000284 AliasSet *AS = findAliasSetForCallSite(CS);
285 if (!AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000286 AliasSets.push_back(new AliasSet());
Chris Lattner9971ac42003-02-24 20:37:56 +0000287 AS = &AliasSets.back();
Chris Lattner12c11552004-07-21 05:18:04 +0000288 AS->addCallSite(CS, AA);
289 return true;
290 } else {
291 AS->addCallSite(CS, AA);
292 return false;
Chris Lattner9971ac42003-02-24 20:37:56 +0000293 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000294}
295
Chris Lattner12c11552004-07-21 05:18:04 +0000296bool AliasSetTracker::add(Instruction *I) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000297 // Dispatch to one of the other add methods...
298 if (LoadInst *LI = dyn_cast<LoadInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000299 return add(LI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000300 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000301 return add(SI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000302 else if (CallInst *CI = dyn_cast<CallInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000303 return add(CI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000304 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000305 return add(II);
Chris Lattner5c882602004-07-25 07:57:37 +0000306 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
307 return add(FI);
Chris Lattner12c11552004-07-21 05:18:04 +0000308 return true;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000309}
310
Chris Lattner319d05b2003-03-03 23:28:05 +0000311void AliasSetTracker::add(BasicBlock &BB) {
312 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
313 add(I);
314}
315
316void AliasSetTracker::add(const AliasSetTracker &AST) {
317 assert(&AA == &AST.AA &&
318 "Merging AliasSetTracker objects with different Alias Analyses!");
319
320 // Loop over all of the alias sets in AST, adding the pointers contained
321 // therein into the current alias sets. This can cause alias sets to be
322 // merged together in the current AST.
323 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
324 if (!I->Forward) { // Ignore forwarding alias sets
325 AliasSet &AS = const_cast<AliasSet&>(*I);
326
327 // If there are any call sites in the alias set, add them to this AST.
328 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
329 add(AS.CallSites[i]);
330
331 // Loop over all of the pointers in this alias set...
332 AliasSet::iterator I = AS.begin(), E = AS.end();
Chris Lattner12c11552004-07-21 05:18:04 +0000333 bool X;
Chris Lattner319d05b2003-03-03 23:28:05 +0000334 for (; I != E; ++I)
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000335 addPointer(I.getPointer(), I.getSize(),
Chris Lattner12c11552004-07-21 05:18:04 +0000336 (AliasSet::AccessType)AS.AccessTy, X);
Chris Lattner319d05b2003-03-03 23:28:05 +0000337 }
338}
339
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000340/// remove - Remove the specified (potentially non-empty) alias set from the
341/// tracker.
342void AliasSetTracker::remove(AliasSet &AS) {
343 bool SetDead;
344 do {
345 AliasSet::iterator I = AS.begin();
346 Value *Ptr = I.getPointer(); ++I;
347
348 // deleteValue will delete the set automatically when the last pointer
349 // reference is destroyed. "Predict" when this will happen.
350 SetDead = I == AS.end();
351 deleteValue(Ptr); // Delete all of the pointers from the set
352 } while (!SetDead);
353}
354
Chris Lattner61d46272004-07-26 05:50:23 +0000355bool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
356 AliasSet *AS = findAliasSetForPointer(Ptr, Size);
357 if (!AS) return false;
358 remove(*AS);
359 return true;
360}
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000361
362bool AliasSetTracker::remove(LoadInst *LI) {
363 unsigned Size = AA.getTargetData().getTypeSize(LI->getType());
364 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
365 if (!AS) return false;
366 remove(*AS);
367 return true;
368}
369
370bool AliasSetTracker::remove(StoreInst *SI) {
371 unsigned Size = AA.getTargetData().getTypeSize(SI->getOperand(0)->getType());
372 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
373 if (!AS) return false;
374 remove(*AS);
375 return true;
376}
377
Chris Lattner5c882602004-07-25 07:57:37 +0000378bool AliasSetTracker::remove(FreeInst *FI) {
379 AliasSet *AS = findAliasSetForPointer(FI->getOperand(0), ~0);
380 if (!AS) return false;
381 remove(*AS);
382 return true;
383}
384
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000385bool AliasSetTracker::remove(CallSite CS) {
386 if (Function *F = CS.getCalledFunction())
387 if (AA.doesNotAccessMemory(F))
388 return false; // doesn't alias anything
389
390 AliasSet *AS = findAliasSetForCallSite(CS);
391 if (!AS) return false;
392 remove(*AS);
393 return true;
394}
395
396bool AliasSetTracker::remove(Instruction *I) {
397 // Dispatch to one of the other remove methods...
398 if (LoadInst *LI = dyn_cast<LoadInst>(I))
399 return remove(LI);
400 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
401 return remove(SI);
402 else if (CallInst *CI = dyn_cast<CallInst>(I))
403 return remove(CI);
Chris Lattner5c882602004-07-25 07:57:37 +0000404 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
405 return remove(FI);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000406 return true;
407}
408
Chris Lattner2cffeec2003-12-18 08:11:56 +0000409
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000410// deleteValue method - This method is used to remove a pointer value from the
Chris Lattner2cffeec2003-12-18 08:11:56 +0000411// AliasSetTracker entirely. It should be used when an instruction is deleted
412// from the program to update the AST. If you don't use this, you would have
413// dangling pointers to deleted instructions.
414//
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000415void AliasSetTracker::deleteValue(Value *PtrVal) {
Chris Lattnerb66e6482004-09-14 19:15:32 +0000416 // Notify the alias analysis implementation that this value is gone.
417 AA.deleteValue(PtrVal);
418
419 // First, look up the PointerRec for this pointer.
Chris Lattner2cffeec2003-12-18 08:11:56 +0000420 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(PtrVal);
421 if (I == PointerMap.end()) return; // Noop
422
423 // If we found one, remove the pointer from the alias set it is in.
424 AliasSet::HashNodePair &PtrValEnt = *I;
425 AliasSet *AS = PtrValEnt.second.getAliasSet(*this);
426
427 // Unlink from the list of values...
428 PtrValEnt.second.removeFromList();
429 // Stop using the alias set
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000430 AS->dropRef(*this);
Chris Lattner2cffeec2003-12-18 08:11:56 +0000431 PointerMap.erase(I);
432}
433
Chris Lattnerb66e6482004-09-14 19:15:32 +0000434// copyValue - This method should be used whenever a preexisting value in the
435// program is copied or cloned, introducing a new value. Note that it is ok for
436// clients that use this method to introduce the same value multiple times: if
437// the tracker already knows about a value, it will ignore the request.
438//
439void AliasSetTracker::copyValue(Value *From, Value *To) {
440 // Notify the alias analysis implementation that this value is copied.
441 AA.copyValue(From, To);
442
443 // First, look up the PointerRec for this pointer.
444 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(From);
445 if (I == PointerMap.end() || !I->second.hasAliasSet())
446 return; // Noop
447
448 AliasSet::HashNodePair &Entry = getEntryFor(To);
449 if (Entry.second.hasAliasSet()) return; // Already in the tracker!
450
451 // Add it to the alias set it aliases...
452 AliasSet *AS = I->second.getAliasSet(*this);
453 AS->addPointer(*this, Entry, I->second.getSize(), true);
454}
455
456
Chris Lattner2cffeec2003-12-18 08:11:56 +0000457
Chris Lattner9971ac42003-02-24 20:37:56 +0000458//===----------------------------------------------------------------------===//
459// AliasSet/AliasSetTracker Printing Support
460//===----------------------------------------------------------------------===//
461
462void AliasSet::print(std::ostream &OS) const {
463 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
464 OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, ";
465 switch (AccessTy) {
466 case NoModRef: OS << "No access "; break;
467 case Refs : OS << "Ref "; break;
468 case Mods : OS << "Mod "; break;
469 case ModRef : OS << "Mod/Ref "; break;
470 default: assert(0 && "Bad value for AccessTy!");
Chris Lattner009cc3d2002-09-26 21:49:07 +0000471 }
Chris Lattnere2609242003-12-14 04:52:11 +0000472 if (isVolatile()) OS << "[volatile] ";
Chris Lattner9971ac42003-02-24 20:37:56 +0000473 if (Forward)
474 OS << " forwarding to " << (void*)Forward;
475
476
477 if (begin() != end()) {
478 OS << "Pointers: ";
479 for (iterator I = begin(), E = end(); I != E; ++I) {
480 if (I != begin()) OS << ", ";
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000481 WriteAsOperand(OS << "(", I.getPointer());
482 OS << ", " << I.getSize() << ")";
Chris Lattner9971ac42003-02-24 20:37:56 +0000483 }
484 }
485 if (!CallSites.empty()) {
486 OS << "\n " << CallSites.size() << " Call Sites: ";
487 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
488 if (i) OS << ", ";
489 WriteAsOperand(OS, CallSites[i].getCalledValue());
490 }
491 }
492 OS << "\n";
493}
494
495void AliasSetTracker::print(std::ostream &OS) const {
496 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
497 << PointerMap.size() << " pointer values.\n";
498 for (const_iterator I = begin(), E = end(); I != E; ++I)
499 I->print(OS);
500 OS << "\n";
501}
502
503void AliasSet::dump() const { print (std::cerr); }
504void AliasSetTracker::dump() const { print(std::cerr); }
505
Chris Lattner9971ac42003-02-24 20:37:56 +0000506//===----------------------------------------------------------------------===//
507// AliasSetPrinter Pass
508//===----------------------------------------------------------------------===//
509
510namespace {
511 class AliasSetPrinter : public FunctionPass {
512 AliasSetTracker *Tracker;
513 public:
514 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
515 AU.setPreservesAll();
516 AU.addRequired<AliasAnalysis>();
517 }
518
519 virtual bool runOnFunction(Function &F) {
520 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
521
522 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner6ffe5512004-04-27 15:13:33 +0000523 Tracker->add(&*I);
Chris Lattner9971ac42003-02-24 20:37:56 +0000524 return false;
525 }
526
527 /// print - Convert to human readable form
528 virtual void print(std::ostream &OS) const {
529 Tracker->print(OS);
530 }
531
532 virtual void releaseMemory() {
533 delete Tracker;
534 }
535 };
536 RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer",
537 PassInfo::Analysis | PassInfo::Optimization);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000538}