blob: fca612c85fb0af5a6117ea9ab166b6243d4f995c [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"
Reid Spencerd7d83db2007-02-05 23:42:17 +000021#include "llvm/Support/Compiler.h"
Chris Lattner9971ac42003-02-24 20:37:56 +000022#include "llvm/Support/InstIterator.h"
Bill Wendling6f81b512006-11-28 22:46:12 +000023#include "llvm/Support/Streams.h"
Chris Lattnere2609242003-12-14 04:52:11 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattnercc8d5242004-11-27 18:37:42 +000026/// mergeSetIn - Merge the specified alias set into this alias set.
Chris Lattner009cc3d2002-09-26 21:49:07 +000027///
Chris Lattnercc8d5242004-11-27 18:37:42 +000028void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
Chris Lattner9971ac42003-02-24 20:37:56 +000029 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
Chris Lattnercc8d5242004-11-27 18:37:42 +000036 if (AliasTy == MustAlias) {
37 // Check that these two merged sets really are must aliases. Since both
38 // used to be must-alias sets, we can just check any pointer from each set
39 // for aliasing.
40 AliasAnalysis &AA = AST.getAliasAnalysis();
41 HashNodePair *L = getSomePointer();
42 HashNodePair *R = AS.getSomePointer();
43
44 // If the pointers are not a must-alias pair, this set becomes a may alias.
45 if (AA.alias(L->first, L->second.getSize(), R->first, R->second.getSize())
46 != AliasAnalysis::MustAlias)
47 AliasTy = MayAlias;
48 }
49
Chris Lattner9971ac42003-02-24 20:37:56 +000050 if (CallSites.empty()) { // Merge call sites...
51 if (!AS.CallSites.empty())
52 std::swap(CallSites, AS.CallSites);
53 } else if (!AS.CallSites.empty()) {
54 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
55 AS.CallSites.clear();
56 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000057
Chris Lattner9971ac42003-02-24 20:37:56 +000058 AS.Forward = this; // Forward across AS now...
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000059 addRef(); // AS is now pointing to us...
Chris Lattner9971ac42003-02-24 20:37:56 +000060
61 // Merge the list of constituent pointers...
Chris Lattner2cffeec2003-12-18 08:11:56 +000062 if (AS.PtrList) {
63 *PtrListEnd = AS.PtrList;
64 AS.PtrList->second.setPrevInList(PtrListEnd);
65 PtrListEnd = AS.PtrListEnd;
66
67 AS.PtrList = 0;
68 AS.PtrListEnd = &AS.PtrList;
Chris Lattner3080b602004-09-15 16:59:47 +000069 assert(*AS.PtrListEnd == 0 && "End of list is not null?");
Chris Lattner2cffeec2003-12-18 08:11:56 +000070 }
Chris Lattner009cc3d2002-09-26 21:49:07 +000071}
72
Chris Lattner9971ac42003-02-24 20:37:56 +000073void AliasSetTracker::removeAliasSet(AliasSet *AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000074 if (AliasSet *Fwd = AS->Forward) {
75 Fwd->dropRef(*this);
76 AS->Forward = 0;
77 }
Chris Lattner9971ac42003-02-24 20:37:56 +000078 AliasSets.erase(AS);
79}
80
81void AliasSet::removeFromTracker(AliasSetTracker &AST) {
82 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
83 AST.removeAliasSet(this);
84}
85
Chris Lattner31a9d182003-02-26 22:11:00 +000086void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
Chris Lattnerb66e6482004-09-14 19:15:32 +000087 unsigned Size, bool KnownMustAlias) {
Chris Lattner9971ac42003-02-24 20:37:56 +000088 assert(!Entry.second.hasAliasSet() && "Entry already in set!");
89
Chris Lattnerb66e6482004-09-14 19:15:32 +000090 // Check to see if we have to downgrade to _may_ alias.
91 if (isMustAlias() && !KnownMustAlias)
Chris Lattner31a9d182003-02-26 22:11:00 +000092 if (HashNodePair *P = getSomePointer()) {
Chris Lattner3080b602004-09-15 16:59:47 +000093 AliasAnalysis &AA = AST.getAliasAnalysis();
Chris Lattner31a9d182003-02-26 22:11:00 +000094 AliasAnalysis::AliasResult Result =
95 AA.alias(P->first, P->second.getSize(), Entry.first, Size);
96 if (Result == AliasAnalysis::MayAlias)
Chris Lattner9971ac42003-02-24 20:37:56 +000097 AliasTy = MayAlias;
Chris Lattner31a9d182003-02-26 22:11:00 +000098 else // First entry of must alias must have maximum size!
99 P->second.updateSize(Size);
100 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
101 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000102
103 Entry.second.setAliasSet(this);
Chris Lattner31a9d182003-02-26 22:11:00 +0000104 Entry.second.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000105
106 // Add it to the end of the list...
Chris Lattner2cffeec2003-12-18 08:11:56 +0000107 assert(*PtrListEnd == 0 && "End of list is not null?");
108 *PtrListEnd = &Entry;
109 PtrListEnd = Entry.second.setPrevInList(PtrListEnd);
Chris Lattner3080b602004-09-15 16:59:47 +0000110 assert(*PtrListEnd == 0 && "End of list is not null?");
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000111 addRef(); // Entry points to alias set...
Chris Lattner9971ac42003-02-24 20:37:56 +0000112}
113
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000114void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000115 CallSites.push_back(CS);
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000116
117 if (Function *F = CS.getCalledFunction()) {
Chris Lattner0af024c2004-12-15 07:22:13 +0000118 AliasAnalysis::ModRefBehavior Behavior = AA.getModRefBehavior(F, CS);
119 if (Behavior == AliasAnalysis::DoesNotAccessMemory)
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000120 return;
Chris Lattner0af024c2004-12-15 07:22:13 +0000121 else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000122 AliasTy = MayAlias;
Chris Lattner4781bc72004-03-17 23:22:04 +0000123 AccessTy |= Refs;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000124 return;
125 }
126 }
127
128 // FIXME: This should use mod/ref information to make this not suck so bad
129 AliasTy = MayAlias;
Chris Lattner577385e2003-05-03 03:42:08 +0000130 AccessTy = ModRef;
Chris Lattner9971ac42003-02-24 20:37:56 +0000131}
132
133/// aliasesPointer - Return true if the specified pointer "may" (or must)
Chris Lattner009cc3d2002-09-26 21:49:07 +0000134/// alias one of the members in the set.
135///
Chris Lattner31a9d182003-02-26 22:11:00 +0000136bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
137 AliasAnalysis &AA) const {
Chris Lattner9971ac42003-02-24 20:37:56 +0000138 if (AliasTy == MustAlias) {
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000139 assert(CallSites.empty() && "Illegal must alias set!");
140
Chris Lattner9971ac42003-02-24 20:37:56 +0000141 // If this is a set of MustAliases, only check to see if the pointer aliases
142 // SOME value in the set...
Chris Lattner31a9d182003-02-26 22:11:00 +0000143 HashNodePair *SomePtr = getSomePointer();
Chris Lattner9971ac42003-02-24 20:37:56 +0000144 assert(SomePtr && "Empty must-alias set??");
Chris Lattner31a9d182003-02-26 22:11:00 +0000145 return AA.alias(SomePtr->first, SomePtr->second.getSize(), Ptr, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000146 }
147
148 // If this is a may-alias set, we have to check all of the pointers in the set
149 // to be sure it doesn't alias the set...
150 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000151 if (AA.alias(Ptr, Size, I.getPointer(), I.getSize()))
Chris Lattner9971ac42003-02-24 20:37:56 +0000152 return true;
153
154 // Check the call sites list and invoke list...
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000155 if (!CallSites.empty()) {
156 if (AA.hasNoModRefInfoForCalls())
157 return true;
158
159 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
160 if (AA.getModRefInfo(CallSites[i], const_cast<Value*>(Ptr), Size)
161 != AliasAnalysis::NoModRef)
162 return true;
163 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000164
Chris Lattner009cc3d2002-09-26 21:49:07 +0000165 return false;
166}
167
Chris Lattner9971ac42003-02-24 20:37:56 +0000168bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000169 if (Function *F = CS.getCalledFunction())
170 if (AA.doesNotAccessMemory(F))
171 return false;
172
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000173 if (AA.hasNoModRefInfoForCalls())
174 return true;
175
176 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
177 if (AA.getModRefInfo(CallSites[i], CS) != AliasAnalysis::NoModRef ||
178 AA.getModRefInfo(CS, CallSites[i]) != AliasAnalysis::NoModRef)
179 return true;
180
181 for (iterator I = begin(), E = end(); I != E; ++I)
182 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
183 AliasAnalysis::NoModRef)
184 return true;
185
186 return false;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000187}
188
189
Chris Lattner009cc3d2002-09-26 21:49:07 +0000190/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
191/// instruction referring to the pointer into. If there are multiple alias sets
192/// that may alias the pointer, merge them together and return the unified set.
193///
Chris Lattner31a9d182003-02-26 22:11:00 +0000194AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
195 unsigned Size) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000196 AliasSet *FoundSet = 0;
Chris Lattner9971ac42003-02-24 20:37:56 +0000197 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner31a9d182003-02-26 22:11:00 +0000198 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
Chris Lattnercc8d5242004-11-27 18:37:42 +0000199 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
Chris Lattner9971ac42003-02-24 20:37:56 +0000200 FoundSet = I; // Remember it.
Chris Lattnercc8d5242004-11-27 18:37:42 +0000201 } else { // Otherwise, we must merge the sets.
202 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
Chris Lattner009cc3d2002-09-26 21:49:07 +0000203 }
204 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000205
206 return FoundSet;
207}
208
Chris Lattner07bfa522004-11-26 21:36:25 +0000209/// containsPointer - Return true if the specified location is represented by
210/// this alias set, false otherwise. This does not modify the AST object or
211/// alias sets.
212bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const {
213 for (const_iterator I = begin(), E = end(); I != E; ++I)
214 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA))
215 return true;
216 return false;
217}
218
219
220
Chris Lattner9971ac42003-02-24 20:37:56 +0000221AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
222 AliasSet *FoundSet = 0;
223 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000224 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
Chris Lattnercc8d5242004-11-27 18:37:42 +0000225 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
Chris Lattner9971ac42003-02-24 20:37:56 +0000226 FoundSet = I; // Remember it.
Chris Lattnercc8d5242004-11-27 18:37:42 +0000227 } else if (!I->Forward) { // Otherwise, we must merge the sets.
228 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
Chris Lattner9971ac42003-02-24 20:37:56 +0000229 }
230 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000231
232 return FoundSet;
233}
234
235
Chris Lattner009cc3d2002-09-26 21:49:07 +0000236
Chris Lattner9971ac42003-02-24 20:37:56 +0000237
238/// getAliasSetForPointer - Return the alias set that the specified pointer
239/// lives in...
Chris Lattner12c11552004-07-21 05:18:04 +0000240AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
241 bool *New) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000242 AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
243
244 // Check to see if the pointer is already known...
Chris Lattner34a10052004-07-25 18:32:01 +0000245 if (Entry.second.hasAliasSet()) {
246 Entry.second.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000247 // Return the set!
248 return *Entry.second.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattner31a9d182003-02-26 22:11:00 +0000249 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000250 // Add it to the alias set it aliases...
Chris Lattner31a9d182003-02-26 22:11:00 +0000251 AS->addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000252 return *AS;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000253 } else {
Chris Lattner12c11552004-07-21 05:18:04 +0000254 if (New) *New = true;
Chris Lattner9971ac42003-02-24 20:37:56 +0000255 // Otherwise create a new alias set to hold the loaded pointer...
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000256 AliasSets.push_back(new AliasSet());
Chris Lattner31a9d182003-02-26 22:11:00 +0000257 AliasSets.back().addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000258 return AliasSets.back();
Chris Lattner009cc3d2002-09-26 21:49:07 +0000259 }
260}
261
Chris Lattner61d46272004-07-26 05:50:23 +0000262bool AliasSetTracker::add(Value *Ptr, unsigned Size) {
263 bool NewPtr;
264 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
265 return NewPtr;
266}
267
268
Chris Lattner12c11552004-07-21 05:18:04 +0000269bool AliasSetTracker::add(LoadInst *LI) {
270 bool NewPtr;
271 AliasSet &AS = addPointer(LI->getOperand(0),
272 AA.getTargetData().getTypeSize(LI->getType()),
273 AliasSet::Refs, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000274 if (LI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000275 return NewPtr;
Chris Lattner9971ac42003-02-24 20:37:56 +0000276}
277
Chris Lattner12c11552004-07-21 05:18:04 +0000278bool AliasSetTracker::add(StoreInst *SI) {
279 bool NewPtr;
280 Value *Val = SI->getOperand(0);
281 AliasSet &AS = addPointer(SI->getOperand(1),
282 AA.getTargetData().getTypeSize(Val->getType()),
283 AliasSet::Mods, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000284 if (SI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000285 return NewPtr;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000286}
287
Chris Lattner5c882602004-07-25 07:57:37 +0000288bool AliasSetTracker::add(FreeInst *FI) {
289 bool NewPtr;
290 AliasSet &AS = addPointer(FI->getOperand(0), ~0,
291 AliasSet::Mods, NewPtr);
Chris Lattner2958eea2005-03-25 05:49:37 +0000292
293 // Free operations are volatile ops (cannot be moved).
294 AS.setVolatile();
Chris Lattner5c882602004-07-25 07:57:37 +0000295 return NewPtr;
296}
297
Chris Lattnere2609242003-12-14 04:52:11 +0000298
Chris Lattner12c11552004-07-21 05:18:04 +0000299bool AliasSetTracker::add(CallSite CS) {
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000300 if (Function *F = CS.getCalledFunction())
301 if (AA.doesNotAccessMemory(F))
Chris Lattner12c11552004-07-21 05:18:04 +0000302 return true; // doesn't alias anything
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000303
Chris Lattner9971ac42003-02-24 20:37:56 +0000304 AliasSet *AS = findAliasSetForCallSite(CS);
305 if (!AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000306 AliasSets.push_back(new AliasSet());
Chris Lattner9971ac42003-02-24 20:37:56 +0000307 AS = &AliasSets.back();
Chris Lattner12c11552004-07-21 05:18:04 +0000308 AS->addCallSite(CS, AA);
309 return true;
310 } else {
311 AS->addCallSite(CS, AA);
312 return false;
Chris Lattner9971ac42003-02-24 20:37:56 +0000313 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000314}
315
Chris Lattner12c11552004-07-21 05:18:04 +0000316bool AliasSetTracker::add(Instruction *I) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000317 // Dispatch to one of the other add methods...
318 if (LoadInst *LI = dyn_cast<LoadInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000319 return add(LI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000320 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000321 return add(SI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000322 else if (CallInst *CI = dyn_cast<CallInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000323 return add(CI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000324 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000325 return add(II);
Chris Lattner5c882602004-07-25 07:57:37 +0000326 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
327 return add(FI);
Chris Lattner12c11552004-07-21 05:18:04 +0000328 return true;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000329}
330
Chris Lattner319d05b2003-03-03 23:28:05 +0000331void AliasSetTracker::add(BasicBlock &BB) {
332 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
333 add(I);
334}
335
336void AliasSetTracker::add(const AliasSetTracker &AST) {
337 assert(&AA == &AST.AA &&
338 "Merging AliasSetTracker objects with different Alias Analyses!");
339
340 // Loop over all of the alias sets in AST, adding the pointers contained
341 // therein into the current alias sets. This can cause alias sets to be
342 // merged together in the current AST.
343 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
344 if (!I->Forward) { // Ignore forwarding alias sets
345 AliasSet &AS = const_cast<AliasSet&>(*I);
346
347 // If there are any call sites in the alias set, add them to this AST.
348 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
349 add(AS.CallSites[i]);
350
351 // Loop over all of the pointers in this alias set...
352 AliasSet::iterator I = AS.begin(), E = AS.end();
Chris Lattner12c11552004-07-21 05:18:04 +0000353 bool X;
Chris Lattner319d05b2003-03-03 23:28:05 +0000354 for (; I != E; ++I)
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000355 addPointer(I.getPointer(), I.getSize(),
Chris Lattner12c11552004-07-21 05:18:04 +0000356 (AliasSet::AccessType)AS.AccessTy, X);
Chris Lattner319d05b2003-03-03 23:28:05 +0000357 }
358}
359
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000360/// remove - Remove the specified (potentially non-empty) alias set from the
361/// tracker.
362void AliasSetTracker::remove(AliasSet &AS) {
Chris Lattnerf2998572006-06-27 23:48:59 +0000363 // Drop all call sites.
364 AS.CallSites.clear();
365
366 // Clear the alias set.
367 unsigned NumRefs = 0;
368 while (!AS.empty()) {
369 AliasSet::HashNodePair *P = AS.PtrList;
370
371 // Unlink from the list of values.
372 P->second.removeFromList();
373
374 // Remember how many references need to be dropped.
375 ++NumRefs;
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000376
Chris Lattnerf2998572006-06-27 23:48:59 +0000377 // Finally, remove the entry.
Nick Lewyckye81f7252006-09-17 17:51:00 +0000378 Value *Remove = P->first; // Take a copy because it is invalid to pass
379 PointerMap.erase(Remove); // a reference to the data being erased.
Chris Lattnerf2998572006-06-27 23:48:59 +0000380 }
381
382 // Stop using the alias set, removing it.
Chris Lattner356d8c22006-06-27 23:56:13 +0000383 AS.RefCount -= NumRefs;
384 if (AS.RefCount == 0)
385 AS.removeFromTracker(*this);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000386}
387
Chris Lattner61d46272004-07-26 05:50:23 +0000388bool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
389 AliasSet *AS = findAliasSetForPointer(Ptr, Size);
390 if (!AS) return false;
391 remove(*AS);
392 return true;
393}
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000394
395bool AliasSetTracker::remove(LoadInst *LI) {
396 unsigned Size = AA.getTargetData().getTypeSize(LI->getType());
397 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
398 if (!AS) return false;
399 remove(*AS);
400 return true;
401}
402
403bool AliasSetTracker::remove(StoreInst *SI) {
404 unsigned Size = AA.getTargetData().getTypeSize(SI->getOperand(0)->getType());
405 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
406 if (!AS) return false;
407 remove(*AS);
408 return true;
409}
410
Chris Lattner5c882602004-07-25 07:57:37 +0000411bool AliasSetTracker::remove(FreeInst *FI) {
412 AliasSet *AS = findAliasSetForPointer(FI->getOperand(0), ~0);
413 if (!AS) return false;
414 remove(*AS);
415 return true;
416}
417
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000418bool AliasSetTracker::remove(CallSite CS) {
419 if (Function *F = CS.getCalledFunction())
420 if (AA.doesNotAccessMemory(F))
421 return false; // doesn't alias anything
422
423 AliasSet *AS = findAliasSetForCallSite(CS);
424 if (!AS) return false;
425 remove(*AS);
426 return true;
427}
428
429bool AliasSetTracker::remove(Instruction *I) {
430 // Dispatch to one of the other remove methods...
431 if (LoadInst *LI = dyn_cast<LoadInst>(I))
432 return remove(LI);
433 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
434 return remove(SI);
435 else if (CallInst *CI = dyn_cast<CallInst>(I))
436 return remove(CI);
Chris Lattner5c882602004-07-25 07:57:37 +0000437 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
438 return remove(FI);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000439 return true;
440}
441
Chris Lattner2cffeec2003-12-18 08:11:56 +0000442
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000443// deleteValue method - This method is used to remove a pointer value from the
Chris Lattner2cffeec2003-12-18 08:11:56 +0000444// AliasSetTracker entirely. It should be used when an instruction is deleted
445// from the program to update the AST. If you don't use this, you would have
446// dangling pointers to deleted instructions.
447//
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000448void AliasSetTracker::deleteValue(Value *PtrVal) {
Chris Lattnerb66e6482004-09-14 19:15:32 +0000449 // Notify the alias analysis implementation that this value is gone.
450 AA.deleteValue(PtrVal);
451
Chris Lattner959e3212006-06-26 19:20:48 +0000452 // If this is a call instruction, remove the callsite from the appropriate
453 // AliasSet.
454 CallSite CS = CallSite::get(PtrVal);
455 if (CS.getInstruction()) {
456 Function *F = CS.getCalledFunction();
457 if (!F || !AA.doesNotAccessMemory(F)) {
458 if (AliasSet *AS = findAliasSetForCallSite(CS))
459 AS->removeCallSite(CS);
460 }
461 }
462
Chris Lattnerb66e6482004-09-14 19:15:32 +0000463 // First, look up the PointerRec for this pointer.
Chris Lattner2cffeec2003-12-18 08:11:56 +0000464 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(PtrVal);
465 if (I == PointerMap.end()) return; // Noop
466
467 // If we found one, remove the pointer from the alias set it is in.
468 AliasSet::HashNodePair &PtrValEnt = *I;
469 AliasSet *AS = PtrValEnt.second.getAliasSet(*this);
470
471 // Unlink from the list of values...
472 PtrValEnt.second.removeFromList();
473 // Stop using the alias set
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000474 AS->dropRef(*this);
Chris Lattner2cffeec2003-12-18 08:11:56 +0000475 PointerMap.erase(I);
476}
477
Chris Lattnerb66e6482004-09-14 19:15:32 +0000478// copyValue - This method should be used whenever a preexisting value in the
479// program is copied or cloned, introducing a new value. Note that it is ok for
480// clients that use this method to introduce the same value multiple times: if
481// the tracker already knows about a value, it will ignore the request.
482//
483void AliasSetTracker::copyValue(Value *From, Value *To) {
484 // Notify the alias analysis implementation that this value is copied.
485 AA.copyValue(From, To);
486
487 // First, look up the PointerRec for this pointer.
488 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(From);
489 if (I == PointerMap.end() || !I->second.hasAliasSet())
490 return; // Noop
491
492 AliasSet::HashNodePair &Entry = getEntryFor(To);
493 if (Entry.second.hasAliasSet()) return; // Already in the tracker!
494
495 // Add it to the alias set it aliases...
496 AliasSet *AS = I->second.getAliasSet(*this);
497 AS->addPointer(*this, Entry, I->second.getSize(), true);
498}
499
500
Chris Lattner2cffeec2003-12-18 08:11:56 +0000501
Chris Lattner9971ac42003-02-24 20:37:56 +0000502//===----------------------------------------------------------------------===//
503// AliasSet/AliasSetTracker Printing Support
504//===----------------------------------------------------------------------===//
505
506void AliasSet::print(std::ostream &OS) const {
507 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
508 OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, ";
509 switch (AccessTy) {
510 case NoModRef: OS << "No access "; break;
511 case Refs : OS << "Ref "; break;
512 case Mods : OS << "Mod "; break;
513 case ModRef : OS << "Mod/Ref "; break;
514 default: assert(0 && "Bad value for AccessTy!");
Chris Lattner009cc3d2002-09-26 21:49:07 +0000515 }
Chris Lattnere2609242003-12-14 04:52:11 +0000516 if (isVolatile()) OS << "[volatile] ";
Chris Lattner9971ac42003-02-24 20:37:56 +0000517 if (Forward)
518 OS << " forwarding to " << (void*)Forward;
519
520
521 if (begin() != end()) {
522 OS << "Pointers: ";
523 for (iterator I = begin(), E = end(); I != E; ++I) {
524 if (I != begin()) OS << ", ";
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000525 WriteAsOperand(OS << "(", I.getPointer());
526 OS << ", " << I.getSize() << ")";
Chris Lattner9971ac42003-02-24 20:37:56 +0000527 }
528 }
529 if (!CallSites.empty()) {
530 OS << "\n " << CallSites.size() << " Call Sites: ";
531 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
532 if (i) OS << ", ";
533 WriteAsOperand(OS, CallSites[i].getCalledValue());
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000534 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000535 }
536 OS << "\n";
537}
538
539void AliasSetTracker::print(std::ostream &OS) const {
540 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
541 << PointerMap.size() << " pointer values.\n";
542 for (const_iterator I = begin(), E = end(); I != E; ++I)
543 I->print(OS);
544 OS << "\n";
545}
546
Bill Wendlinge8156192006-12-07 01:30:32 +0000547void AliasSet::dump() const { print (cerr); }
548void AliasSetTracker::dump() const { print(cerr); }
Chris Lattner9971ac42003-02-24 20:37:56 +0000549
Chris Lattner9971ac42003-02-24 20:37:56 +0000550//===----------------------------------------------------------------------===//
551// AliasSetPrinter Pass
552//===----------------------------------------------------------------------===//
553
554namespace {
Reid Spencerd7d83db2007-02-05 23:42:17 +0000555 class VISIBILITY_HIDDEN AliasSetPrinter : public FunctionPass {
Chris Lattner9971ac42003-02-24 20:37:56 +0000556 AliasSetTracker *Tracker;
557 public:
Devang Patel19974732007-05-03 01:11:54 +0000558 static char ID; // Pass identifcation, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +0000559 AliasSetPrinter() : FunctionPass((intptr_t)&ID) {}
560
Chris Lattner9971ac42003-02-24 20:37:56 +0000561 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
562 AU.setPreservesAll();
563 AU.addRequired<AliasAnalysis>();
564 }
565
566 virtual bool runOnFunction(Function &F) {
567 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
568
569 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner6ffe5512004-04-27 15:13:33 +0000570 Tracker->add(&*I);
Bill Wendlinge8156192006-12-07 01:30:32 +0000571 Tracker->print(cerr);
Chris Lattner4983cf72006-01-03 06:05:22 +0000572 delete Tracker;
Chris Lattner9971ac42003-02-24 20:37:56 +0000573 return false;
574 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000575 };
Devang Patel19974732007-05-03 01:11:54 +0000576 char AliasSetPrinter::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +0000577 RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer");
Chris Lattner009cc3d2002-09-26 21:49:07 +0000578}