blob: 9ae4044b0e01297aa78f350a67d814c5d0b594de [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//
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.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner009cc3d2002-09-26 21:49:07 +00009//
10// This file implements the AliasSetTracker and AliasSet classes.
Misha Brukman2b37d7c2005-04-21 21:13:18 +000011//
Chris Lattner009cc3d2002-09-26 21:49:07 +000012//===----------------------------------------------------------------------===//
13
14#include "llvm/Analysis/AliasSetTracker.h"
15#include "llvm/Analysis/AliasAnalysis.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000016#include "llvm/Instructions.h"
Chris Lattner9971ac42003-02-24 20:37:56 +000017#include "llvm/Pass.h"
Chris Lattner5b3a4552005-03-17 15:38:16 +000018#include "llvm/Type.h"
Chris Lattner31a9d182003-02-26 22:11:00 +000019#include "llvm/Target/TargetData.h"
Chris Lattner9971ac42003-02-24 20:37:56 +000020#include "llvm/Assembly/Writer.h"
21#include "llvm/Support/InstIterator.h"
Reid Spencer954da372004-07-04 12:19:56 +000022#include <iostream>
Chris Lattnere2609242003-12-14 04:52:11 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Chris Lattnercc8d5242004-11-27 18:37:42 +000025/// mergeSetIn - Merge the specified alias set into this alias set.
Chris Lattner009cc3d2002-09-26 21:49:07 +000026///
Chris Lattnercc8d5242004-11-27 18:37:42 +000027void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
Chris Lattner9971ac42003-02-24 20:37:56 +000028 assert(!AS.Forward && "Alias set is already forwarding!");
29 assert(!Forward && "This set is a forwarding set!!");
Chris Lattner009cc3d2002-09-26 21:49:07 +000030
31 // Update the alias and access types of this set...
Chris Lattner9971ac42003-02-24 20:37:56 +000032 AccessTy |= AS.AccessTy;
33 AliasTy |= AS.AliasTy;
34
Chris Lattnercc8d5242004-11-27 18:37:42 +000035 if (AliasTy == MustAlias) {
36 // Check that these two merged sets really are must aliases. Since both
37 // used to be must-alias sets, we can just check any pointer from each set
38 // for aliasing.
39 AliasAnalysis &AA = AST.getAliasAnalysis();
40 HashNodePair *L = getSomePointer();
41 HashNodePair *R = AS.getSomePointer();
42
43 // If the pointers are not a must-alias pair, this set becomes a may alias.
44 if (AA.alias(L->first, L->second.getSize(), R->first, R->second.getSize())
45 != AliasAnalysis::MustAlias)
46 AliasTy = MayAlias;
47 }
48
Chris Lattner9971ac42003-02-24 20:37:56 +000049 if (CallSites.empty()) { // Merge call sites...
50 if (!AS.CallSites.empty())
51 std::swap(CallSites, AS.CallSites);
52 } else if (!AS.CallSites.empty()) {
53 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
54 AS.CallSites.clear();
55 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000056
Chris Lattner9971ac42003-02-24 20:37:56 +000057 AS.Forward = this; // Forward across AS now...
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000058 addRef(); // AS is now pointing to us...
Chris Lattner9971ac42003-02-24 20:37:56 +000059
60 // Merge the list of constituent pointers...
Chris Lattner2cffeec2003-12-18 08:11:56 +000061 if (AS.PtrList) {
62 *PtrListEnd = AS.PtrList;
63 AS.PtrList->second.setPrevInList(PtrListEnd);
64 PtrListEnd = AS.PtrListEnd;
65
66 AS.PtrList = 0;
67 AS.PtrListEnd = &AS.PtrList;
Chris Lattner3080b602004-09-15 16:59:47 +000068 assert(*AS.PtrListEnd == 0 && "End of list is not null?");
Chris Lattner2cffeec2003-12-18 08:11:56 +000069 }
Chris Lattner009cc3d2002-09-26 21:49:07 +000070}
71
Chris Lattner9971ac42003-02-24 20:37:56 +000072void AliasSetTracker::removeAliasSet(AliasSet *AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000073 if (AliasSet *Fwd = AS->Forward) {
74 Fwd->dropRef(*this);
75 AS->Forward = 0;
76 }
Chris Lattner9971ac42003-02-24 20:37:56 +000077 AliasSets.erase(AS);
78}
79
80void AliasSet::removeFromTracker(AliasSetTracker &AST) {
81 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
82 AST.removeAliasSet(this);
83}
84
Chris Lattner31a9d182003-02-26 22:11:00 +000085void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
Chris Lattnerb66e6482004-09-14 19:15:32 +000086 unsigned Size, bool KnownMustAlias) {
Chris Lattner9971ac42003-02-24 20:37:56 +000087 assert(!Entry.second.hasAliasSet() && "Entry already in set!");
88
Chris Lattnerb66e6482004-09-14 19:15:32 +000089 // Check to see if we have to downgrade to _may_ alias.
90 if (isMustAlias() && !KnownMustAlias)
Chris Lattner31a9d182003-02-26 22:11:00 +000091 if (HashNodePair *P = getSomePointer()) {
Chris Lattner3080b602004-09-15 16:59:47 +000092 AliasAnalysis &AA = AST.getAliasAnalysis();
Chris Lattner31a9d182003-02-26 22:11:00 +000093 AliasAnalysis::AliasResult Result =
94 AA.alias(P->first, P->second.getSize(), Entry.first, Size);
95 if (Result == AliasAnalysis::MayAlias)
Chris Lattner9971ac42003-02-24 20:37:56 +000096 AliasTy = MayAlias;
Chris Lattner31a9d182003-02-26 22:11:00 +000097 else // First entry of must alias must have maximum size!
98 P->second.updateSize(Size);
99 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
100 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000101
102 Entry.second.setAliasSet(this);
Chris Lattner31a9d182003-02-26 22:11:00 +0000103 Entry.second.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000104
105 // Add it to the end of the list...
Chris Lattner2cffeec2003-12-18 08:11:56 +0000106 assert(*PtrListEnd == 0 && "End of list is not null?");
107 *PtrListEnd = &Entry;
108 PtrListEnd = Entry.second.setPrevInList(PtrListEnd);
Chris Lattner3080b602004-09-15 16:59:47 +0000109 assert(*PtrListEnd == 0 && "End of list is not null?");
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000110 addRef(); // Entry points to alias set...
Chris Lattner9971ac42003-02-24 20:37:56 +0000111}
112
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000113void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000114 CallSites.push_back(CS);
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000115
116 if (Function *F = CS.getCalledFunction()) {
Chris Lattner0af024c2004-12-15 07:22:13 +0000117 AliasAnalysis::ModRefBehavior Behavior = AA.getModRefBehavior(F, CS);
118 if (Behavior == AliasAnalysis::DoesNotAccessMemory)
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000119 return;
Chris Lattner0af024c2004-12-15 07:22:13 +0000120 else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000121 AliasTy = MayAlias;
Chris Lattner4781bc72004-03-17 23:22:04 +0000122 AccessTy |= Refs;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000123 return;
124 }
125 }
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 {
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000168 if (Function *F = CS.getCalledFunction())
169 if (AA.doesNotAccessMemory(F))
170 return false;
171
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000172 if (AA.hasNoModRefInfoForCalls())
173 return true;
174
175 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
176 if (AA.getModRefInfo(CallSites[i], CS) != AliasAnalysis::NoModRef ||
177 AA.getModRefInfo(CS, CallSites[i]) != AliasAnalysis::NoModRef)
178 return true;
179
180 for (iterator I = begin(), E = end(); I != E; ++I)
181 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
182 AliasAnalysis::NoModRef)
183 return true;
184
185 return false;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000186}
187
188
Chris Lattner009cc3d2002-09-26 21:49:07 +0000189/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
190/// instruction referring to the pointer into. If there are multiple alias sets
191/// that may alias the pointer, merge them together and return the unified set.
192///
Chris Lattner31a9d182003-02-26 22:11:00 +0000193AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
194 unsigned Size) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000195 AliasSet *FoundSet = 0;
Chris Lattner9971ac42003-02-24 20:37:56 +0000196 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner31a9d182003-02-26 22:11:00 +0000197 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
Chris Lattnercc8d5242004-11-27 18:37:42 +0000198 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
Chris Lattner9971ac42003-02-24 20:37:56 +0000199 FoundSet = I; // Remember it.
Chris Lattnercc8d5242004-11-27 18:37:42 +0000200 } else { // Otherwise, we must merge the sets.
201 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
Chris Lattner009cc3d2002-09-26 21:49:07 +0000202 }
203 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000204
205 return FoundSet;
206}
207
Chris Lattner07bfa522004-11-26 21:36:25 +0000208/// containsPointer - Return true if the specified location is represented by
209/// this alias set, false otherwise. This does not modify the AST object or
210/// alias sets.
211bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const {
212 for (const_iterator I = begin(), E = end(); I != E; ++I)
213 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA))
214 return true;
215 return false;
216}
217
218
219
Chris Lattner9971ac42003-02-24 20:37:56 +0000220AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
221 AliasSet *FoundSet = 0;
222 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000223 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
Chris Lattnercc8d5242004-11-27 18:37:42 +0000224 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
Chris Lattner9971ac42003-02-24 20:37:56 +0000225 FoundSet = I; // Remember it.
Chris Lattnercc8d5242004-11-27 18:37:42 +0000226 } else if (!I->Forward) { // Otherwise, we must merge the sets.
227 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
Chris Lattner9971ac42003-02-24 20:37:56 +0000228 }
229 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000230
231 return FoundSet;
232}
233
234
Chris Lattner009cc3d2002-09-26 21:49:07 +0000235
Chris Lattner9971ac42003-02-24 20:37:56 +0000236
237/// getAliasSetForPointer - Return the alias set that the specified pointer
238/// lives in...
Chris Lattner12c11552004-07-21 05:18:04 +0000239AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
240 bool *New) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000241 AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
242
243 // Check to see if the pointer is already known...
Chris Lattner34a10052004-07-25 18:32:01 +0000244 if (Entry.second.hasAliasSet()) {
245 Entry.second.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000246 // Return the set!
247 return *Entry.second.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattner31a9d182003-02-26 22:11:00 +0000248 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000249 // Add it to the alias set it aliases...
Chris Lattner31a9d182003-02-26 22:11:00 +0000250 AS->addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000251 return *AS;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000252 } else {
Chris Lattner12c11552004-07-21 05:18:04 +0000253 if (New) *New = true;
Chris Lattner9971ac42003-02-24 20:37:56 +0000254 // Otherwise create a new alias set to hold the loaded pointer...
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000255 AliasSets.push_back(new AliasSet());
Chris Lattner31a9d182003-02-26 22:11:00 +0000256 AliasSets.back().addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000257 return AliasSets.back();
Chris Lattner009cc3d2002-09-26 21:49:07 +0000258 }
259}
260
Chris Lattner61d46272004-07-26 05:50:23 +0000261bool AliasSetTracker::add(Value *Ptr, unsigned Size) {
262 bool NewPtr;
263 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
264 return NewPtr;
265}
266
267
Chris Lattner12c11552004-07-21 05:18:04 +0000268bool AliasSetTracker::add(LoadInst *LI) {
269 bool NewPtr;
270 AliasSet &AS = addPointer(LI->getOperand(0),
271 AA.getTargetData().getTypeSize(LI->getType()),
272 AliasSet::Refs, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000273 if (LI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000274 return NewPtr;
Chris Lattner9971ac42003-02-24 20:37:56 +0000275}
276
Chris Lattner12c11552004-07-21 05:18:04 +0000277bool AliasSetTracker::add(StoreInst *SI) {
278 bool NewPtr;
279 Value *Val = SI->getOperand(0);
280 AliasSet &AS = addPointer(SI->getOperand(1),
281 AA.getTargetData().getTypeSize(Val->getType()),
282 AliasSet::Mods, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000283 if (SI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000284 return NewPtr;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000285}
286
Chris Lattner5c882602004-07-25 07:57:37 +0000287bool AliasSetTracker::add(FreeInst *FI) {
288 bool NewPtr;
289 AliasSet &AS = addPointer(FI->getOperand(0), ~0,
290 AliasSet::Mods, NewPtr);
Chris Lattner2958eea2005-03-25 05:49:37 +0000291
292 // Free operations are volatile ops (cannot be moved).
293 AS.setVolatile();
Chris Lattner5c882602004-07-25 07:57:37 +0000294 return NewPtr;
295}
296
Chris Lattnere2609242003-12-14 04:52:11 +0000297
Chris Lattner12c11552004-07-21 05:18:04 +0000298bool AliasSetTracker::add(CallSite CS) {
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000299 if (Function *F = CS.getCalledFunction())
300 if (AA.doesNotAccessMemory(F))
Chris Lattner12c11552004-07-21 05:18:04 +0000301 return true; // doesn't alias anything
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000302
Chris Lattner9971ac42003-02-24 20:37:56 +0000303 AliasSet *AS = findAliasSetForCallSite(CS);
304 if (!AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000305 AliasSets.push_back(new AliasSet());
Chris Lattner9971ac42003-02-24 20:37:56 +0000306 AS = &AliasSets.back();
Chris Lattner12c11552004-07-21 05:18:04 +0000307 AS->addCallSite(CS, AA);
308 return true;
309 } else {
310 AS->addCallSite(CS, AA);
311 return false;
Chris Lattner9971ac42003-02-24 20:37:56 +0000312 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000313}
314
Chris Lattner12c11552004-07-21 05:18:04 +0000315bool AliasSetTracker::add(Instruction *I) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000316 // Dispatch to one of the other add methods...
317 if (LoadInst *LI = dyn_cast<LoadInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000318 return add(LI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000319 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000320 return add(SI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000321 else if (CallInst *CI = dyn_cast<CallInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000322 return add(CI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000323 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000324 return add(II);
Chris Lattner5c882602004-07-25 07:57:37 +0000325 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
326 return add(FI);
Chris Lattner12c11552004-07-21 05:18:04 +0000327 return true;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000328}
329
Chris Lattner319d05b2003-03-03 23:28:05 +0000330void AliasSetTracker::add(BasicBlock &BB) {
331 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
332 add(I);
333}
334
335void AliasSetTracker::add(const AliasSetTracker &AST) {
336 assert(&AA == &AST.AA &&
337 "Merging AliasSetTracker objects with different Alias Analyses!");
338
339 // Loop over all of the alias sets in AST, adding the pointers contained
340 // therein into the current alias sets. This can cause alias sets to be
341 // merged together in the current AST.
342 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
343 if (!I->Forward) { // Ignore forwarding alias sets
344 AliasSet &AS = const_cast<AliasSet&>(*I);
345
346 // If there are any call sites in the alias set, add them to this AST.
347 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
348 add(AS.CallSites[i]);
349
350 // Loop over all of the pointers in this alias set...
351 AliasSet::iterator I = AS.begin(), E = AS.end();
Chris Lattner12c11552004-07-21 05:18:04 +0000352 bool X;
Chris Lattner319d05b2003-03-03 23:28:05 +0000353 for (; I != E; ++I)
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000354 addPointer(I.getPointer(), I.getSize(),
Chris Lattner12c11552004-07-21 05:18:04 +0000355 (AliasSet::AccessType)AS.AccessTy, X);
Chris Lattner319d05b2003-03-03 23:28:05 +0000356 }
357}
358
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000359/// remove - Remove the specified (potentially non-empty) alias set from the
360/// tracker.
361void AliasSetTracker::remove(AliasSet &AS) {
362 bool SetDead;
363 do {
364 AliasSet::iterator I = AS.begin();
365 Value *Ptr = I.getPointer(); ++I;
366
367 // deleteValue will delete the set automatically when the last pointer
368 // reference is destroyed. "Predict" when this will happen.
369 SetDead = I == AS.end();
370 deleteValue(Ptr); // Delete all of the pointers from the set
371 } while (!SetDead);
372}
373
Chris Lattner61d46272004-07-26 05:50:23 +0000374bool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
375 AliasSet *AS = findAliasSetForPointer(Ptr, Size);
376 if (!AS) return false;
377 remove(*AS);
378 return true;
379}
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000380
381bool AliasSetTracker::remove(LoadInst *LI) {
382 unsigned Size = AA.getTargetData().getTypeSize(LI->getType());
383 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
384 if (!AS) return false;
385 remove(*AS);
386 return true;
387}
388
389bool AliasSetTracker::remove(StoreInst *SI) {
390 unsigned Size = AA.getTargetData().getTypeSize(SI->getOperand(0)->getType());
391 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
392 if (!AS) return false;
393 remove(*AS);
394 return true;
395}
396
Chris Lattner5c882602004-07-25 07:57:37 +0000397bool AliasSetTracker::remove(FreeInst *FI) {
398 AliasSet *AS = findAliasSetForPointer(FI->getOperand(0), ~0);
399 if (!AS) return false;
400 remove(*AS);
401 return true;
402}
403
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000404bool AliasSetTracker::remove(CallSite CS) {
405 if (Function *F = CS.getCalledFunction())
406 if (AA.doesNotAccessMemory(F))
407 return false; // doesn't alias anything
408
409 AliasSet *AS = findAliasSetForCallSite(CS);
410 if (!AS) return false;
411 remove(*AS);
412 return true;
413}
414
415bool AliasSetTracker::remove(Instruction *I) {
416 // Dispatch to one of the other remove methods...
417 if (LoadInst *LI = dyn_cast<LoadInst>(I))
418 return remove(LI);
419 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
420 return remove(SI);
421 else if (CallInst *CI = dyn_cast<CallInst>(I))
422 return remove(CI);
Chris Lattner5c882602004-07-25 07:57:37 +0000423 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
424 return remove(FI);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000425 return true;
426}
427
Chris Lattner2cffeec2003-12-18 08:11:56 +0000428
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000429// deleteValue method - This method is used to remove a pointer value from the
Chris Lattner2cffeec2003-12-18 08:11:56 +0000430// AliasSetTracker entirely. It should be used when an instruction is deleted
431// from the program to update the AST. If you don't use this, you would have
432// dangling pointers to deleted instructions.
433//
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000434void AliasSetTracker::deleteValue(Value *PtrVal) {
Chris Lattnerb66e6482004-09-14 19:15:32 +0000435 // Notify the alias analysis implementation that this value is gone.
436 AA.deleteValue(PtrVal);
437
438 // First, look up the PointerRec for this pointer.
Chris Lattner2cffeec2003-12-18 08:11:56 +0000439 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(PtrVal);
440 if (I == PointerMap.end()) return; // Noop
441
442 // If we found one, remove the pointer from the alias set it is in.
443 AliasSet::HashNodePair &PtrValEnt = *I;
444 AliasSet *AS = PtrValEnt.second.getAliasSet(*this);
445
446 // Unlink from the list of values...
447 PtrValEnt.second.removeFromList();
448 // Stop using the alias set
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000449 AS->dropRef(*this);
Chris Lattner2cffeec2003-12-18 08:11:56 +0000450 PointerMap.erase(I);
451}
452
Chris Lattnerb66e6482004-09-14 19:15:32 +0000453// copyValue - This method should be used whenever a preexisting value in the
454// program is copied or cloned, introducing a new value. Note that it is ok for
455// clients that use this method to introduce the same value multiple times: if
456// the tracker already knows about a value, it will ignore the request.
457//
458void AliasSetTracker::copyValue(Value *From, Value *To) {
459 // Notify the alias analysis implementation that this value is copied.
460 AA.copyValue(From, To);
461
462 // First, look up the PointerRec for this pointer.
463 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(From);
464 if (I == PointerMap.end() || !I->second.hasAliasSet())
465 return; // Noop
466
467 AliasSet::HashNodePair &Entry = getEntryFor(To);
468 if (Entry.second.hasAliasSet()) return; // Already in the tracker!
469
470 // Add it to the alias set it aliases...
471 AliasSet *AS = I->second.getAliasSet(*this);
472 AS->addPointer(*this, Entry, I->second.getSize(), true);
473}
474
475
Chris Lattner2cffeec2003-12-18 08:11:56 +0000476
Chris Lattner9971ac42003-02-24 20:37:56 +0000477//===----------------------------------------------------------------------===//
478// AliasSet/AliasSetTracker Printing Support
479//===----------------------------------------------------------------------===//
480
481void AliasSet::print(std::ostream &OS) const {
482 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
483 OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, ";
484 switch (AccessTy) {
485 case NoModRef: OS << "No access "; break;
486 case Refs : OS << "Ref "; break;
487 case Mods : OS << "Mod "; break;
488 case ModRef : OS << "Mod/Ref "; break;
489 default: assert(0 && "Bad value for AccessTy!");
Chris Lattner009cc3d2002-09-26 21:49:07 +0000490 }
Chris Lattnere2609242003-12-14 04:52:11 +0000491 if (isVolatile()) OS << "[volatile] ";
Chris Lattner9971ac42003-02-24 20:37:56 +0000492 if (Forward)
493 OS << " forwarding to " << (void*)Forward;
494
495
496 if (begin() != end()) {
497 OS << "Pointers: ";
498 for (iterator I = begin(), E = end(); I != E; ++I) {
499 if (I != begin()) OS << ", ";
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000500 WriteAsOperand(OS << "(", I.getPointer());
501 OS << ", " << I.getSize() << ")";
Chris Lattner9971ac42003-02-24 20:37:56 +0000502 }
503 }
504 if (!CallSites.empty()) {
505 OS << "\n " << CallSites.size() << " Call Sites: ";
506 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
507 if (i) OS << ", ";
508 WriteAsOperand(OS, CallSites[i].getCalledValue());
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000509 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000510 }
511 OS << "\n";
512}
513
514void AliasSetTracker::print(std::ostream &OS) const {
515 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
516 << PointerMap.size() << " pointer values.\n";
517 for (const_iterator I = begin(), E = end(); I != E; ++I)
518 I->print(OS);
519 OS << "\n";
520}
521
522void AliasSet::dump() const { print (std::cerr); }
523void AliasSetTracker::dump() const { print(std::cerr); }
524
Chris Lattner9971ac42003-02-24 20:37:56 +0000525//===----------------------------------------------------------------------===//
526// AliasSetPrinter Pass
527//===----------------------------------------------------------------------===//
528
529namespace {
530 class AliasSetPrinter : public FunctionPass {
531 AliasSetTracker *Tracker;
532 public:
533 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
534 AU.setPreservesAll();
535 AU.addRequired<AliasAnalysis>();
536 }
537
538 virtual bool runOnFunction(Function &F) {
539 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
540
541 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner6ffe5512004-04-27 15:13:33 +0000542 Tracker->add(&*I);
Chris Lattner9971ac42003-02-24 20:37:56 +0000543 return false;
544 }
545
546 /// print - Convert to human readable form
Reid Spencerce9653c2004-12-07 04:03:45 +0000547 virtual void print(std::ostream &OS, const Module* = 0) const {
Chris Lattner9971ac42003-02-24 20:37:56 +0000548 Tracker->print(OS);
549 }
550
551 virtual void releaseMemory() {
552 delete Tracker;
553 }
554 };
555 RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer",
556 PassInfo::Analysis | PassInfo::Optimization);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000557}