blob: 22396b5d8724f5cf2a417ceac4bc6ab8670a1e05 [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"
16#include "llvm/iMemory.h"
17#include "llvm/iOther.h"
18#include "llvm/iTerminators.h"
Chris Lattner9971ac42003-02-24 20:37:56 +000019#include "llvm/Pass.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"
22#include "llvm/Support/InstIterator.h"
Reid Spencer954da372004-07-04 12:19:56 +000023#include <iostream>
Chris Lattnere2609242003-12-14 04:52:11 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattner009cc3d2002-09-26 21:49:07 +000026/// mergeSetIn - Merge the specified alias set into this alias set...
27///
Chris Lattner9971ac42003-02-24 20:37:56 +000028void AliasSet::mergeSetIn(AliasSet &AS) {
29 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
36 if (CallSites.empty()) { // Merge call sites...
37 if (!AS.CallSites.empty())
38 std::swap(CallSites, AS.CallSites);
39 } else if (!AS.CallSites.empty()) {
40 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
41 AS.CallSites.clear();
42 }
43
44 // FIXME: If AS's refcount is zero, nuke it now...
45 assert(RefCount != 0);
46
47 AS.Forward = this; // Forward across AS now...
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000048 addRef(); // AS is now pointing to us...
Chris Lattner9971ac42003-02-24 20:37:56 +000049
50 // Merge the list of constituent pointers...
Chris Lattner2cffeec2003-12-18 08:11:56 +000051 if (AS.PtrList) {
52 *PtrListEnd = AS.PtrList;
53 AS.PtrList->second.setPrevInList(PtrListEnd);
54 PtrListEnd = AS.PtrListEnd;
55
56 AS.PtrList = 0;
57 AS.PtrListEnd = &AS.PtrList;
58 }
Chris Lattner009cc3d2002-09-26 21:49:07 +000059}
60
Chris Lattner9971ac42003-02-24 20:37:56 +000061void AliasSetTracker::removeAliasSet(AliasSet *AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000062 if (AliasSet *Fwd = AS->Forward) {
63 Fwd->dropRef(*this);
64 AS->Forward = 0;
65 }
Chris Lattner9971ac42003-02-24 20:37:56 +000066 AliasSets.erase(AS);
67}
68
69void AliasSet::removeFromTracker(AliasSetTracker &AST) {
70 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
71 AST.removeAliasSet(this);
72}
73
Chris Lattner31a9d182003-02-26 22:11:00 +000074void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
75 unsigned Size) {
Chris Lattner9971ac42003-02-24 20:37:56 +000076 assert(!Entry.second.hasAliasSet() && "Entry already in set!");
77
78 AliasAnalysis &AA = AST.getAliasAnalysis();
79
80 if (isMustAlias()) // Check to see if we have to downgrade to _may_ alias
Chris Lattner31a9d182003-02-26 22:11:00 +000081 if (HashNodePair *P = getSomePointer()) {
82 AliasAnalysis::AliasResult Result =
83 AA.alias(P->first, P->second.getSize(), Entry.first, Size);
84 if (Result == AliasAnalysis::MayAlias)
Chris Lattner9971ac42003-02-24 20:37:56 +000085 AliasTy = MayAlias;
Chris Lattner31a9d182003-02-26 22:11:00 +000086 else // First entry of must alias must have maximum size!
87 P->second.updateSize(Size);
88 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
89 }
Chris Lattner9971ac42003-02-24 20:37:56 +000090
91 Entry.second.setAliasSet(this);
Chris Lattner31a9d182003-02-26 22:11:00 +000092 Entry.second.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +000093
94 // Add it to the end of the list...
Chris Lattner2cffeec2003-12-18 08:11:56 +000095 assert(*PtrListEnd == 0 && "End of list is not null?");
96 *PtrListEnd = &Entry;
97 PtrListEnd = Entry.second.setPrevInList(PtrListEnd);
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000098 addRef(); // Entry points to alias set...
Chris Lattner9971ac42003-02-24 20:37:56 +000099}
100
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000101void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000102 CallSites.push_back(CS);
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000103
104 if (Function *F = CS.getCalledFunction()) {
105 if (AA.doesNotAccessMemory(F))
106 return;
107 else if (AA.onlyReadsMemory(F)) {
108 AliasTy = MayAlias;
Chris Lattner4781bc72004-03-17 23:22:04 +0000109 AccessTy |= Refs;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000110 return;
111 }
112 }
113
114 // FIXME: This should use mod/ref information to make this not suck so bad
115 AliasTy = MayAlias;
Chris Lattner577385e2003-05-03 03:42:08 +0000116 AccessTy = ModRef;
Chris Lattner9971ac42003-02-24 20:37:56 +0000117}
118
119/// aliasesPointer - Return true if the specified pointer "may" (or must)
Chris Lattner009cc3d2002-09-26 21:49:07 +0000120/// alias one of the members in the set.
121///
Chris Lattner31a9d182003-02-26 22:11:00 +0000122bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
123 AliasAnalysis &AA) const {
Chris Lattner9971ac42003-02-24 20:37:56 +0000124 if (AliasTy == MustAlias) {
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000125 assert(CallSites.empty() && "Illegal must alias set!");
126
Chris Lattner9971ac42003-02-24 20:37:56 +0000127 // If this is a set of MustAliases, only check to see if the pointer aliases
128 // SOME value in the set...
Chris Lattner31a9d182003-02-26 22:11:00 +0000129 HashNodePair *SomePtr = getSomePointer();
Chris Lattner9971ac42003-02-24 20:37:56 +0000130 assert(SomePtr && "Empty must-alias set??");
Chris Lattner31a9d182003-02-26 22:11:00 +0000131 return AA.alias(SomePtr->first, SomePtr->second.getSize(), Ptr, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000132 }
133
134 // If this is a may-alias set, we have to check all of the pointers in the set
135 // to be sure it doesn't alias the set...
136 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000137 if (AA.alias(Ptr, Size, I.getPointer(), I.getSize()))
Chris Lattner9971ac42003-02-24 20:37:56 +0000138 return true;
139
140 // Check the call sites list and invoke list...
141 if (!CallSites.empty())
142 // FIXME: this is pessimistic!
Chris Lattner009cc3d2002-09-26 21:49:07 +0000143 return true;
Chris Lattner9971ac42003-02-24 20:37:56 +0000144
Chris Lattner009cc3d2002-09-26 21:49:07 +0000145 return false;
146}
147
Chris Lattner9971ac42003-02-24 20:37:56 +0000148bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000149 // FIXME: Use mod/ref information to prune this better!
150 if (Function *F = CS.getCalledFunction())
151 if (AA.doesNotAccessMemory(F))
152 return false;
153
Chris Lattner9971ac42003-02-24 20:37:56 +0000154 return true;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000155}
156
157
Chris Lattner009cc3d2002-09-26 21:49:07 +0000158/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
159/// instruction referring to the pointer into. If there are multiple alias sets
160/// that may alias the pointer, merge them together and return the unified set.
161///
Chris Lattner31a9d182003-02-26 22:11:00 +0000162AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
163 unsigned Size) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000164 AliasSet *FoundSet = 0;
Chris Lattner9971ac42003-02-24 20:37:56 +0000165 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner31a9d182003-02-26 22:11:00 +0000166 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000167 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
Chris Lattner9971ac42003-02-24 20:37:56 +0000168 FoundSet = I; // Remember it.
Chris Lattner009cc3d2002-09-26 21:49:07 +0000169 } else { // Otherwise, we must merge the sets...
Chris Lattner9971ac42003-02-24 20:37:56 +0000170 FoundSet->mergeSetIn(*I); // Merge in contents...
Chris Lattner009cc3d2002-09-26 21:49:07 +0000171 }
172 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000173
174 return FoundSet;
175}
176
177AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
178 AliasSet *FoundSet = 0;
179 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000180 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000181 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
182 FoundSet = I; // Remember it.
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000183 } else if (!I->Forward) { // Otherwise, we must merge the sets...
Chris Lattner9971ac42003-02-24 20:37:56 +0000184 FoundSet->mergeSetIn(*I); // Merge in contents...
185 }
186 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000187
188 return FoundSet;
189}
190
191
Chris Lattner009cc3d2002-09-26 21:49:07 +0000192
Chris Lattner9971ac42003-02-24 20:37:56 +0000193
194/// getAliasSetForPointer - Return the alias set that the specified pointer
195/// lives in...
Chris Lattner12c11552004-07-21 05:18:04 +0000196AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
197 bool *New) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000198 AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
199
200 // Check to see if the pointer is already known...
Chris Lattner31a9d182003-02-26 22:11:00 +0000201 if (Entry.second.hasAliasSet() && Size <= Entry.second.getSize()) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000202 // Return the set!
203 return *Entry.second.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattner31a9d182003-02-26 22:11:00 +0000204 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000205 // Add it to the alias set it aliases...
Chris Lattner31a9d182003-02-26 22:11:00 +0000206 AS->addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000207 return *AS;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000208 } else {
Chris Lattner12c11552004-07-21 05:18:04 +0000209 if (New) *New = true;
Chris Lattner9971ac42003-02-24 20:37:56 +0000210 // Otherwise create a new alias set to hold the loaded pointer...
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000211 AliasSets.push_back(new AliasSet());
Chris Lattner31a9d182003-02-26 22:11:00 +0000212 AliasSets.back().addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000213 return AliasSets.back();
Chris Lattner009cc3d2002-09-26 21:49:07 +0000214 }
215}
216
Chris Lattner12c11552004-07-21 05:18:04 +0000217bool AliasSetTracker::add(LoadInst *LI) {
218 bool NewPtr;
219 AliasSet &AS = addPointer(LI->getOperand(0),
220 AA.getTargetData().getTypeSize(LI->getType()),
221 AliasSet::Refs, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000222 if (LI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000223 return NewPtr;
Chris Lattner9971ac42003-02-24 20:37:56 +0000224}
225
Chris Lattner12c11552004-07-21 05:18:04 +0000226bool AliasSetTracker::add(StoreInst *SI) {
227 bool NewPtr;
228 Value *Val = SI->getOperand(0);
229 AliasSet &AS = addPointer(SI->getOperand(1),
230 AA.getTargetData().getTypeSize(Val->getType()),
231 AliasSet::Mods, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000232 if (SI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000233 return NewPtr;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000234}
235
Chris Lattner5c882602004-07-25 07:57:37 +0000236bool AliasSetTracker::add(FreeInst *FI) {
237 bool NewPtr;
238 AliasSet &AS = addPointer(FI->getOperand(0), ~0,
239 AliasSet::Mods, NewPtr);
240 return NewPtr;
241}
242
Chris Lattnere2609242003-12-14 04:52:11 +0000243
Chris Lattner12c11552004-07-21 05:18:04 +0000244bool AliasSetTracker::add(CallSite CS) {
245 bool NewPtr;
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000246 if (Function *F = CS.getCalledFunction())
247 if (AA.doesNotAccessMemory(F))
Chris Lattner12c11552004-07-21 05:18:04 +0000248 return true; // doesn't alias anything
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000249
Chris Lattner9971ac42003-02-24 20:37:56 +0000250 AliasSet *AS = findAliasSetForCallSite(CS);
251 if (!AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000252 AliasSets.push_back(new AliasSet());
Chris Lattner9971ac42003-02-24 20:37:56 +0000253 AS = &AliasSets.back();
Chris Lattner12c11552004-07-21 05:18:04 +0000254 AS->addCallSite(CS, AA);
255 return true;
256 } else {
257 AS->addCallSite(CS, AA);
258 return false;
Chris Lattner9971ac42003-02-24 20:37:56 +0000259 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000260}
261
Chris Lattner12c11552004-07-21 05:18:04 +0000262bool AliasSetTracker::add(Instruction *I) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000263 // Dispatch to one of the other add methods...
264 if (LoadInst *LI = dyn_cast<LoadInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000265 return add(LI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000266 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000267 return add(SI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000268 else if (CallInst *CI = dyn_cast<CallInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000269 return add(CI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000270 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000271 return add(II);
Chris Lattner5c882602004-07-25 07:57:37 +0000272 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
273 return add(FI);
Chris Lattner12c11552004-07-21 05:18:04 +0000274 return true;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000275}
276
Chris Lattner319d05b2003-03-03 23:28:05 +0000277void AliasSetTracker::add(BasicBlock &BB) {
278 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
279 add(I);
280}
281
282void AliasSetTracker::add(const AliasSetTracker &AST) {
283 assert(&AA == &AST.AA &&
284 "Merging AliasSetTracker objects with different Alias Analyses!");
285
286 // Loop over all of the alias sets in AST, adding the pointers contained
287 // therein into the current alias sets. This can cause alias sets to be
288 // merged together in the current AST.
289 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
290 if (!I->Forward) { // Ignore forwarding alias sets
291 AliasSet &AS = const_cast<AliasSet&>(*I);
292
293 // If there are any call sites in the alias set, add them to this AST.
294 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
295 add(AS.CallSites[i]);
296
297 // Loop over all of the pointers in this alias set...
298 AliasSet::iterator I = AS.begin(), E = AS.end();
Chris Lattner12c11552004-07-21 05:18:04 +0000299 bool X;
Chris Lattner319d05b2003-03-03 23:28:05 +0000300 for (; I != E; ++I)
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000301 addPointer(I.getPointer(), I.getSize(),
Chris Lattner12c11552004-07-21 05:18:04 +0000302 (AliasSet::AccessType)AS.AccessTy, X);
Chris Lattner319d05b2003-03-03 23:28:05 +0000303 }
304}
305
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000306/// remove - Remove the specified (potentially non-empty) alias set from the
307/// tracker.
308void AliasSetTracker::remove(AliasSet &AS) {
309 bool SetDead;
310 do {
311 AliasSet::iterator I = AS.begin();
312 Value *Ptr = I.getPointer(); ++I;
313
314 // deleteValue will delete the set automatically when the last pointer
315 // reference is destroyed. "Predict" when this will happen.
316 SetDead = I == AS.end();
317 deleteValue(Ptr); // Delete all of the pointers from the set
318 } while (!SetDead);
319}
320
321
322bool AliasSetTracker::remove(LoadInst *LI) {
323 unsigned Size = AA.getTargetData().getTypeSize(LI->getType());
324 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
325 if (!AS) return false;
326 remove(*AS);
327 return true;
328}
329
330bool AliasSetTracker::remove(StoreInst *SI) {
331 unsigned Size = AA.getTargetData().getTypeSize(SI->getOperand(0)->getType());
332 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
333 if (!AS) return false;
334 remove(*AS);
335 return true;
336}
337
Chris Lattner5c882602004-07-25 07:57:37 +0000338bool AliasSetTracker::remove(FreeInst *FI) {
339 AliasSet *AS = findAliasSetForPointer(FI->getOperand(0), ~0);
340 if (!AS) return false;
341 remove(*AS);
342 return true;
343}
344
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000345bool AliasSetTracker::remove(CallSite CS) {
346 if (Function *F = CS.getCalledFunction())
347 if (AA.doesNotAccessMemory(F))
348 return false; // doesn't alias anything
349
350 AliasSet *AS = findAliasSetForCallSite(CS);
351 if (!AS) return false;
352 remove(*AS);
353 return true;
354}
355
356bool AliasSetTracker::remove(Instruction *I) {
357 // Dispatch to one of the other remove methods...
358 if (LoadInst *LI = dyn_cast<LoadInst>(I))
359 return remove(LI);
360 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
361 return remove(SI);
362 else if (CallInst *CI = dyn_cast<CallInst>(I))
363 return remove(CI);
Chris Lattner5c882602004-07-25 07:57:37 +0000364 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
365 return remove(FI);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000366 return true;
367}
368
Chris Lattner2cffeec2003-12-18 08:11:56 +0000369
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000370// deleteValue method - This method is used to remove a pointer value from the
Chris Lattner2cffeec2003-12-18 08:11:56 +0000371// AliasSetTracker entirely. It should be used when an instruction is deleted
372// from the program to update the AST. If you don't use this, you would have
373// dangling pointers to deleted instructions.
374//
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000375void AliasSetTracker::deleteValue(Value *PtrVal) {
Chris Lattner2cffeec2003-12-18 08:11:56 +0000376 // First, look up the PointerRec for this pointer...
377 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(PtrVal);
378 if (I == PointerMap.end()) return; // Noop
379
380 // If we found one, remove the pointer from the alias set it is in.
381 AliasSet::HashNodePair &PtrValEnt = *I;
382 AliasSet *AS = PtrValEnt.second.getAliasSet(*this);
383
384 // Unlink from the list of values...
385 PtrValEnt.second.removeFromList();
386 // Stop using the alias set
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000387 AS->dropRef(*this);
Chris Lattner2cffeec2003-12-18 08:11:56 +0000388 PointerMap.erase(I);
389}
390
391
Chris Lattner9971ac42003-02-24 20:37:56 +0000392//===----------------------------------------------------------------------===//
393// AliasSet/AliasSetTracker Printing Support
394//===----------------------------------------------------------------------===//
395
396void AliasSet::print(std::ostream &OS) const {
397 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
398 OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, ";
399 switch (AccessTy) {
400 case NoModRef: OS << "No access "; break;
401 case Refs : OS << "Ref "; break;
402 case Mods : OS << "Mod "; break;
403 case ModRef : OS << "Mod/Ref "; break;
404 default: assert(0 && "Bad value for AccessTy!");
Chris Lattner009cc3d2002-09-26 21:49:07 +0000405 }
Chris Lattnere2609242003-12-14 04:52:11 +0000406 if (isVolatile()) OS << "[volatile] ";
Chris Lattner9971ac42003-02-24 20:37:56 +0000407 if (Forward)
408 OS << " forwarding to " << (void*)Forward;
409
410
411 if (begin() != end()) {
412 OS << "Pointers: ";
413 for (iterator I = begin(), E = end(); I != E; ++I) {
414 if (I != begin()) OS << ", ";
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000415 WriteAsOperand(OS << "(", I.getPointer());
416 OS << ", " << I.getSize() << ")";
Chris Lattner9971ac42003-02-24 20:37:56 +0000417 }
418 }
419 if (!CallSites.empty()) {
420 OS << "\n " << CallSites.size() << " Call Sites: ";
421 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
422 if (i) OS << ", ";
423 WriteAsOperand(OS, CallSites[i].getCalledValue());
424 }
425 }
426 OS << "\n";
427}
428
429void AliasSetTracker::print(std::ostream &OS) const {
430 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
431 << PointerMap.size() << " pointer values.\n";
432 for (const_iterator I = begin(), E = end(); I != E; ++I)
433 I->print(OS);
434 OS << "\n";
435}
436
437void AliasSet::dump() const { print (std::cerr); }
438void AliasSetTracker::dump() const { print(std::cerr); }
439
Chris Lattner9971ac42003-02-24 20:37:56 +0000440//===----------------------------------------------------------------------===//
441// AliasSetPrinter Pass
442//===----------------------------------------------------------------------===//
443
444namespace {
445 class AliasSetPrinter : public FunctionPass {
446 AliasSetTracker *Tracker;
447 public:
448 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
449 AU.setPreservesAll();
450 AU.addRequired<AliasAnalysis>();
451 }
452
453 virtual bool runOnFunction(Function &F) {
454 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
455
456 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner6ffe5512004-04-27 15:13:33 +0000457 Tracker->add(&*I);
Chris Lattner9971ac42003-02-24 20:37:56 +0000458 return false;
459 }
460
461 /// print - Convert to human readable form
462 virtual void print(std::ostream &OS) const {
463 Tracker->print(OS);
464 }
465
466 virtual void releaseMemory() {
467 delete Tracker;
468 }
469 };
470 RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer",
471 PassInfo::Analysis | PassInfo::Optimization);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000472}