blob: 18c2b66505f636811c4338b0974bb3bb1b3464e6 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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"
Zhou Sheng7e4286e2009-03-03 06:02:04 +000017#include "llvm/IntrinsicInst.h"
Chris Lattner9971ac42003-02-24 20:37:56 +000018#include "llvm/Pass.h"
Chris Lattner5b3a4552005-03-17 15:38:16 +000019#include "llvm/Type.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"
Reid Spencerd7d83db2007-02-05 23:42:17 +000022#include "llvm/Support/Compiler.h"
Chris Lattner9971ac42003-02-24 20:37:56 +000023#include "llvm/Support/InstIterator.h"
Bill Wendling6f81b512006-11-28 22:46:12 +000024#include "llvm/Support/Streams.h"
Chris Lattnere2609242003-12-14 04:52:11 +000025using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000026
Chris Lattnercc8d5242004-11-27 18:37:42 +000027/// mergeSetIn - Merge the specified alias set into this alias set.
Chris Lattner009cc3d2002-09-26 21:49:07 +000028///
Chris Lattnercc8d5242004-11-27 18:37:42 +000029void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
Chris Lattner9971ac42003-02-24 20:37:56 +000030 assert(!AS.Forward && "Alias set is already forwarding!");
31 assert(!Forward && "This set is a forwarding set!!");
Chris Lattner009cc3d2002-09-26 21:49:07 +000032
33 // Update the alias and access types of this set...
Chris Lattner9971ac42003-02-24 20:37:56 +000034 AccessTy |= AS.AccessTy;
35 AliasTy |= AS.AliasTy;
36
Chris Lattnercc8d5242004-11-27 18:37:42 +000037 if (AliasTy == MustAlias) {
38 // Check that these two merged sets really are must aliases. Since both
39 // used to be must-alias sets, we can just check any pointer from each set
40 // for aliasing.
41 AliasAnalysis &AA = AST.getAliasAnalysis();
Chris Lattnerd7168dd2009-03-09 05:11:09 +000042 PointerRec *L = getSomePointer();
43 PointerRec *R = AS.getSomePointer();
Chris Lattnercc8d5242004-11-27 18:37:42 +000044
45 // If the pointers are not a must-alias pair, this set becomes a may alias.
Chris Lattnerd7168dd2009-03-09 05:11:09 +000046 if (AA.alias(L->getValue(), L->getSize(), R->getValue(), R->getSize())
Chris Lattnercc8d5242004-11-27 18:37:42 +000047 != AliasAnalysis::MustAlias)
48 AliasTy = MayAlias;
49 }
50
Chris Lattner9971ac42003-02-24 20:37:56 +000051 if (CallSites.empty()) { // Merge call sites...
52 if (!AS.CallSites.empty())
53 std::swap(CallSites, AS.CallSites);
54 } else if (!AS.CallSites.empty()) {
55 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
56 AS.CallSites.clear();
57 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000058
Chris Lattner9971ac42003-02-24 20:37:56 +000059 AS.Forward = this; // Forward across AS now...
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000060 addRef(); // AS is now pointing to us...
Chris Lattner9971ac42003-02-24 20:37:56 +000061
62 // Merge the list of constituent pointers...
Chris Lattner2cffeec2003-12-18 08:11:56 +000063 if (AS.PtrList) {
64 *PtrListEnd = AS.PtrList;
Chris Lattnerd7168dd2009-03-09 05:11:09 +000065 AS.PtrList->setPrevInList(PtrListEnd);
Chris Lattner2cffeec2003-12-18 08:11:56 +000066 PtrListEnd = AS.PtrListEnd;
67
68 AS.PtrList = 0;
69 AS.PtrListEnd = &AS.PtrList;
Chris Lattner3080b602004-09-15 16:59:47 +000070 assert(*AS.PtrListEnd == 0 && "End of list is not null?");
Chris Lattner2cffeec2003-12-18 08:11:56 +000071 }
Chris Lattner009cc3d2002-09-26 21:49:07 +000072}
73
Chris Lattner9971ac42003-02-24 20:37:56 +000074void AliasSetTracker::removeAliasSet(AliasSet *AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000075 if (AliasSet *Fwd = AS->Forward) {
76 Fwd->dropRef(*this);
77 AS->Forward = 0;
78 }
Chris Lattner9971ac42003-02-24 20:37:56 +000079 AliasSets.erase(AS);
80}
81
82void AliasSet::removeFromTracker(AliasSetTracker &AST) {
83 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
84 AST.removeAliasSet(this);
85}
86
Chris Lattnerd7168dd2009-03-09 05:11:09 +000087void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
Chris Lattnerb66e6482004-09-14 19:15:32 +000088 unsigned Size, bool KnownMustAlias) {
Chris Lattnerd7168dd2009-03-09 05:11:09 +000089 assert(!Entry.hasAliasSet() && "Entry already in set!");
Chris Lattner9971ac42003-02-24 20:37:56 +000090
Chris Lattnerb66e6482004-09-14 19:15:32 +000091 // Check to see if we have to downgrade to _may_ alias.
92 if (isMustAlias() && !KnownMustAlias)
Chris Lattnerd7168dd2009-03-09 05:11:09 +000093 if (PointerRec *P = getSomePointer()) {
Chris Lattner3080b602004-09-15 16:59:47 +000094 AliasAnalysis &AA = AST.getAliasAnalysis();
Chris Lattner31a9d182003-02-26 22:11:00 +000095 AliasAnalysis::AliasResult Result =
Chris Lattnerd7168dd2009-03-09 05:11:09 +000096 AA.alias(P->getValue(), P->getSize(), Entry.getValue(), Size);
Chris Lattner31a9d182003-02-26 22:11:00 +000097 if (Result == AliasAnalysis::MayAlias)
Chris Lattner9971ac42003-02-24 20:37:56 +000098 AliasTy = MayAlias;
Chris Lattner31a9d182003-02-26 22:11:00 +000099 else // First entry of must alias must have maximum size!
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000100 P->updateSize(Size);
Chris Lattner31a9d182003-02-26 22:11:00 +0000101 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
102 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000103
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000104 Entry.setAliasSet(this);
105 Entry.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000106
107 // Add it to the end of the list...
Chris Lattner2cffeec2003-12-18 08:11:56 +0000108 assert(*PtrListEnd == 0 && "End of list is not null?");
109 *PtrListEnd = &Entry;
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000110 PtrListEnd = Entry.setPrevInList(PtrListEnd);
Chris Lattner3080b602004-09-15 16:59:47 +0000111 assert(*PtrListEnd == 0 && "End of list is not null?");
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000112 addRef(); // Entry points to alias set...
Chris Lattner9971ac42003-02-24 20:37:56 +0000113}
114
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000115void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000116 CallSites.push_back(CS);
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000117
Duncan Sandsdff67102007-12-01 07:51:45 +0000118 AliasAnalysis::ModRefBehavior Behavior = AA.getModRefBehavior(CS);
119 if (Behavior == AliasAnalysis::DoesNotAccessMemory)
120 return;
121 else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
122 AliasTy = MayAlias;
123 AccessTy |= Refs;
124 return;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000125 }
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 Lattnerd7168dd2009-03-09 05:11:09 +0000142 PointerRec *SomePtr = getSomePointer();
Chris Lattner9971ac42003-02-24 20:37:56 +0000143 assert(SomePtr && "Empty must-alias set??");
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000144 return AA.alias(SomePtr->getValue(), SomePtr->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 {
Duncan Sandsdff67102007-12-01 07:51:45 +0000168 if (AA.doesNotAccessMemory(CS))
169 return false;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000170
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000171 if (AA.hasNoModRefInfoForCalls())
172 return true;
173
174 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
175 if (AA.getModRefInfo(CallSites[i], CS) != AliasAnalysis::NoModRef ||
176 AA.getModRefInfo(CS, CallSites[i]) != AliasAnalysis::NoModRef)
177 return true;
178
179 for (iterator I = begin(), E = end(); I != E; ++I)
180 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
181 AliasAnalysis::NoModRef)
182 return true;
183
184 return false;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000185}
186
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000187void AliasSetTracker::clear() {
188 // Delete all the PointerRec entries.
189 for (DenseMap<Value*, AliasSet::PointerRec*>::iterator I = PointerMap.begin(),
190 E = PointerMap.end(); I != E; ++I)
191 I->second->eraseFromList();
192
193 PointerMap.clear();
194
195 // The alias sets should all be clear now.
196 AliasSets.clear();
197}
198
Chris Lattner009cc3d2002-09-26 21:49:07 +0000199
Chris Lattner009cc3d2002-09-26 21:49:07 +0000200/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
201/// instruction referring to the pointer into. If there are multiple alias sets
202/// that may alias the pointer, merge them together and return the unified set.
203///
Chris Lattner31a9d182003-02-26 22:11:00 +0000204AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
205 unsigned Size) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000206 AliasSet *FoundSet = 0;
Chris Lattner9971ac42003-02-24 20:37:56 +0000207 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner31a9d182003-02-26 22:11:00 +0000208 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
Chris Lattnercc8d5242004-11-27 18:37:42 +0000209 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
Chris Lattner9971ac42003-02-24 20:37:56 +0000210 FoundSet = I; // Remember it.
Chris Lattnercc8d5242004-11-27 18:37:42 +0000211 } else { // Otherwise, we must merge the sets.
212 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
Chris Lattner009cc3d2002-09-26 21:49:07 +0000213 }
214 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000215
216 return FoundSet;
217}
218
Chris Lattner07bfa522004-11-26 21:36:25 +0000219/// containsPointer - Return true if the specified location is represented by
220/// this alias set, false otherwise. This does not modify the AST object or
221/// alias sets.
222bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const {
223 for (const_iterator I = begin(), E = end(); I != E; ++I)
224 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA))
225 return true;
226 return false;
227}
228
229
230
Chris Lattner9971ac42003-02-24 20:37:56 +0000231AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
232 AliasSet *FoundSet = 0;
233 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000234 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
Chris Lattnercc8d5242004-11-27 18:37:42 +0000235 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
Chris Lattner9971ac42003-02-24 20:37:56 +0000236 FoundSet = I; // Remember it.
Chris Lattnercc8d5242004-11-27 18:37:42 +0000237 } else if (!I->Forward) { // Otherwise, we must merge the sets.
238 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
Chris Lattner9971ac42003-02-24 20:37:56 +0000239 }
240 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000241
242 return FoundSet;
243}
244
245
Chris Lattner009cc3d2002-09-26 21:49:07 +0000246
Chris Lattner9971ac42003-02-24 20:37:56 +0000247
248/// getAliasSetForPointer - Return the alias set that the specified pointer
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000249/// lives in.
Chris Lattner12c11552004-07-21 05:18:04 +0000250AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
251 bool *New) {
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000252 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
Chris Lattner9971ac42003-02-24 20:37:56 +0000253
254 // Check to see if the pointer is already known...
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000255 if (Entry.hasAliasSet()) {
256 Entry.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000257 // Return the set!
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000258 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattner31a9d182003-02-26 22:11:00 +0000259 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000260 // Add it to the alias set it aliases...
Chris Lattner31a9d182003-02-26 22:11:00 +0000261 AS->addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000262 return *AS;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000263 } else {
Chris Lattner12c11552004-07-21 05:18:04 +0000264 if (New) *New = true;
Chris Lattner9971ac42003-02-24 20:37:56 +0000265 // Otherwise create a new alias set to hold the loaded pointer...
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000266 AliasSets.push_back(new AliasSet());
Chris Lattner31a9d182003-02-26 22:11:00 +0000267 AliasSets.back().addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000268 return AliasSets.back();
Chris Lattner009cc3d2002-09-26 21:49:07 +0000269 }
270}
271
Chris Lattner61d46272004-07-26 05:50:23 +0000272bool AliasSetTracker::add(Value *Ptr, unsigned Size) {
273 bool NewPtr;
274 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
275 return NewPtr;
276}
277
278
Chris Lattner12c11552004-07-21 05:18:04 +0000279bool AliasSetTracker::add(LoadInst *LI) {
280 bool NewPtr;
281 AliasSet &AS = addPointer(LI->getOperand(0),
Duncan Sands514ab342007-11-01 20:53:16 +0000282 AA.getTargetData().getTypeStoreSize(LI->getType()),
Chris Lattner12c11552004-07-21 05:18:04 +0000283 AliasSet::Refs, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000284 if (LI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000285 return NewPtr;
Chris Lattner9971ac42003-02-24 20:37:56 +0000286}
287
Chris Lattner12c11552004-07-21 05:18:04 +0000288bool AliasSetTracker::add(StoreInst *SI) {
289 bool NewPtr;
290 Value *Val = SI->getOperand(0);
291 AliasSet &AS = addPointer(SI->getOperand(1),
Duncan Sands514ab342007-11-01 20:53:16 +0000292 AA.getTargetData().getTypeStoreSize(Val->getType()),
Chris Lattner12c11552004-07-21 05:18:04 +0000293 AliasSet::Mods, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000294 if (SI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000295 return NewPtr;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000296}
297
Chris Lattner5c882602004-07-25 07:57:37 +0000298bool AliasSetTracker::add(FreeInst *FI) {
299 bool NewPtr;
Chris Lattner05a24e52008-05-22 03:23:06 +0000300 addPointer(FI->getOperand(0), ~0, AliasSet::Mods, NewPtr);
Chris Lattner5c882602004-07-25 07:57:37 +0000301 return NewPtr;
302}
303
Dan Gohman235fc572008-04-14 18:34:50 +0000304bool AliasSetTracker::add(VAArgInst *VAAI) {
305 bool NewPtr;
Chris Lattner05a24e52008-05-22 03:23:06 +0000306 addPointer(VAAI->getOperand(0), ~0, AliasSet::ModRef, NewPtr);
Dan Gohman235fc572008-04-14 18:34:50 +0000307 return NewPtr;
308}
309
Chris Lattnere2609242003-12-14 04:52:11 +0000310
Chris Lattner12c11552004-07-21 05:18:04 +0000311bool AliasSetTracker::add(CallSite CS) {
Zhou Sheng7e4286e2009-03-03 06:02:04 +0000312 if (isa<DbgInfoIntrinsic>(CS.getInstruction()))
313 return true; // Ignore DbgInfo Intrinsics.
Duncan Sandsdff67102007-12-01 07:51:45 +0000314 if (AA.doesNotAccessMemory(CS))
315 return true; // doesn't alias anything
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000316
Chris Lattner9971ac42003-02-24 20:37:56 +0000317 AliasSet *AS = findAliasSetForCallSite(CS);
318 if (!AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000319 AliasSets.push_back(new AliasSet());
Chris Lattner9971ac42003-02-24 20:37:56 +0000320 AS = &AliasSets.back();
Chris Lattner12c11552004-07-21 05:18:04 +0000321 AS->addCallSite(CS, AA);
322 return true;
323 } else {
324 AS->addCallSite(CS, AA);
325 return false;
Chris Lattner9971ac42003-02-24 20:37:56 +0000326 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000327}
328
Chris Lattner12c11552004-07-21 05:18:04 +0000329bool AliasSetTracker::add(Instruction *I) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000330 // Dispatch to one of the other add methods...
331 if (LoadInst *LI = dyn_cast<LoadInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000332 return add(LI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000333 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000334 return add(SI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000335 else if (CallInst *CI = dyn_cast<CallInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000336 return add(CI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000337 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000338 return add(II);
Chris Lattner5c882602004-07-25 07:57:37 +0000339 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
340 return add(FI);
Dan Gohman235fc572008-04-14 18:34:50 +0000341 else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
342 return add(VAAI);
Chris Lattner12c11552004-07-21 05:18:04 +0000343 return true;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000344}
345
Chris Lattner319d05b2003-03-03 23:28:05 +0000346void AliasSetTracker::add(BasicBlock &BB) {
347 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
348 add(I);
349}
350
351void AliasSetTracker::add(const AliasSetTracker &AST) {
352 assert(&AA == &AST.AA &&
353 "Merging AliasSetTracker objects with different Alias Analyses!");
354
355 // Loop over all of the alias sets in AST, adding the pointers contained
356 // therein into the current alias sets. This can cause alias sets to be
357 // merged together in the current AST.
358 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
359 if (!I->Forward) { // Ignore forwarding alias sets
360 AliasSet &AS = const_cast<AliasSet&>(*I);
361
362 // If there are any call sites in the alias set, add them to this AST.
363 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
364 add(AS.CallSites[i]);
365
366 // Loop over all of the pointers in this alias set...
367 AliasSet::iterator I = AS.begin(), E = AS.end();
Chris Lattner12c11552004-07-21 05:18:04 +0000368 bool X;
Chris Lattnerf711fb72007-05-23 06:36:35 +0000369 for (; I != E; ++I) {
370 AliasSet &NewAS = addPointer(I.getPointer(), I.getSize(),
371 (AliasSet::AccessType)AS.AccessTy, X);
372 if (AS.isVolatile()) NewAS.setVolatile();
373 }
Chris Lattner319d05b2003-03-03 23:28:05 +0000374 }
375}
376
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000377/// remove - Remove the specified (potentially non-empty) alias set from the
378/// tracker.
379void AliasSetTracker::remove(AliasSet &AS) {
Chris Lattnerf2998572006-06-27 23:48:59 +0000380 // Drop all call sites.
381 AS.CallSites.clear();
382
383 // Clear the alias set.
384 unsigned NumRefs = 0;
385 while (!AS.empty()) {
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000386 AliasSet::PointerRec *P = AS.PtrList;
387
388 Value *ValToRemove = P->getValue();
Chris Lattnerf2998572006-06-27 23:48:59 +0000389
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000390 // Unlink and delete entry from the list of values.
391 P->eraseFromList();
Chris Lattnerf2998572006-06-27 23:48:59 +0000392
393 // Remember how many references need to be dropped.
394 ++NumRefs;
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000395
Chris Lattnerf2998572006-06-27 23:48:59 +0000396 // Finally, remove the entry.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000397 PointerMap.erase(ValToRemove);
Chris Lattnerf2998572006-06-27 23:48:59 +0000398 }
399
400 // Stop using the alias set, removing it.
Chris Lattner356d8c22006-06-27 23:56:13 +0000401 AS.RefCount -= NumRefs;
402 if (AS.RefCount == 0)
403 AS.removeFromTracker(*this);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000404}
405
Chris Lattner61d46272004-07-26 05:50:23 +0000406bool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
407 AliasSet *AS = findAliasSetForPointer(Ptr, Size);
408 if (!AS) return false;
409 remove(*AS);
410 return true;
411}
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000412
413bool AliasSetTracker::remove(LoadInst *LI) {
Duncan Sands514ab342007-11-01 20:53:16 +0000414 unsigned Size = AA.getTargetData().getTypeStoreSize(LI->getType());
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000415 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
416 if (!AS) return false;
417 remove(*AS);
418 return true;
419}
420
421bool AliasSetTracker::remove(StoreInst *SI) {
Duncan Sands514ab342007-11-01 20:53:16 +0000422 unsigned Size =
423 AA.getTargetData().getTypeStoreSize(SI->getOperand(0)->getType());
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000424 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
425 if (!AS) return false;
426 remove(*AS);
427 return true;
428}
429
Chris Lattner5c882602004-07-25 07:57:37 +0000430bool AliasSetTracker::remove(FreeInst *FI) {
431 AliasSet *AS = findAliasSetForPointer(FI->getOperand(0), ~0);
432 if (!AS) return false;
433 remove(*AS);
434 return true;
435}
436
Dan Gohman235fc572008-04-14 18:34:50 +0000437bool AliasSetTracker::remove(VAArgInst *VAAI) {
438 AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0), ~0);
439 if (!AS) return false;
440 remove(*AS);
441 return true;
442}
443
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000444bool AliasSetTracker::remove(CallSite CS) {
Duncan Sandsdff67102007-12-01 07:51:45 +0000445 if (AA.doesNotAccessMemory(CS))
446 return false; // doesn't alias anything
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000447
448 AliasSet *AS = findAliasSetForCallSite(CS);
449 if (!AS) return false;
450 remove(*AS);
451 return true;
452}
453
454bool AliasSetTracker::remove(Instruction *I) {
455 // Dispatch to one of the other remove methods...
456 if (LoadInst *LI = dyn_cast<LoadInst>(I))
457 return remove(LI);
458 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
459 return remove(SI);
460 else if (CallInst *CI = dyn_cast<CallInst>(I))
461 return remove(CI);
Chris Lattner5c882602004-07-25 07:57:37 +0000462 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
463 return remove(FI);
Dan Gohman235fc572008-04-14 18:34:50 +0000464 else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
465 return remove(VAAI);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000466 return true;
467}
468
Chris Lattner2cffeec2003-12-18 08:11:56 +0000469
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000470// deleteValue method - This method is used to remove a pointer value from the
Chris Lattner2cffeec2003-12-18 08:11:56 +0000471// AliasSetTracker entirely. It should be used when an instruction is deleted
472// from the program to update the AST. If you don't use this, you would have
473// dangling pointers to deleted instructions.
474//
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000475void AliasSetTracker::deleteValue(Value *PtrVal) {
Chris Lattnerb66e6482004-09-14 19:15:32 +0000476 // Notify the alias analysis implementation that this value is gone.
477 AA.deleteValue(PtrVal);
478
Chris Lattner959e3212006-06-26 19:20:48 +0000479 // If this is a call instruction, remove the callsite from the appropriate
480 // AliasSet.
481 CallSite CS = CallSite::get(PtrVal);
Duncan Sandsdff67102007-12-01 07:51:45 +0000482 if (CS.getInstruction())
483 if (!AA.doesNotAccessMemory(CS))
Chris Lattner959e3212006-06-26 19:20:48 +0000484 if (AliasSet *AS = findAliasSetForCallSite(CS))
485 AS->removeCallSite(CS);
Chris Lattner959e3212006-06-26 19:20:48 +0000486
Chris Lattnerb66e6482004-09-14 19:15:32 +0000487 // First, look up the PointerRec for this pointer.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000488 DenseMap<Value*, AliasSet::PointerRec*>::iterator I = PointerMap.find(PtrVal);
Chris Lattner2cffeec2003-12-18 08:11:56 +0000489 if (I == PointerMap.end()) return; // Noop
490
491 // If we found one, remove the pointer from the alias set it is in.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000492 AliasSet::PointerRec *PtrValEnt = I->second;
493 AliasSet *AS = PtrValEnt->getAliasSet(*this);
Chris Lattner2cffeec2003-12-18 08:11:56 +0000494
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000495 // Unlink and delete from the list of values.
496 PtrValEnt->eraseFromList();
497
498 // Stop using the alias set.
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000499 AS->dropRef(*this);
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000500
Chris Lattner2cffeec2003-12-18 08:11:56 +0000501 PointerMap.erase(I);
502}
503
Chris Lattnerb66e6482004-09-14 19:15:32 +0000504// copyValue - This method should be used whenever a preexisting value in the
505// program is copied or cloned, introducing a new value. Note that it is ok for
506// clients that use this method to introduce the same value multiple times: if
507// the tracker already knows about a value, it will ignore the request.
508//
509void AliasSetTracker::copyValue(Value *From, Value *To) {
510 // Notify the alias analysis implementation that this value is copied.
511 AA.copyValue(From, To);
512
513 // First, look up the PointerRec for this pointer.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000514 DenseMap<Value*, AliasSet::PointerRec*>::iterator I = PointerMap.find(From);
515 if (I == PointerMap.end())
Chris Lattnerb66e6482004-09-14 19:15:32 +0000516 return; // Noop
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000517 assert(I->second->hasAliasSet() && "Dead entry?");
Chris Lattnerb66e6482004-09-14 19:15:32 +0000518
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000519 AliasSet::PointerRec &Entry = getEntryFor(To);
520 if (Entry.hasAliasSet()) return; // Already in the tracker!
Chris Lattnerb66e6482004-09-14 19:15:32 +0000521
522 // Add it to the alias set it aliases...
Devang Patel6d9a2df2009-03-30 18:34:47 +0000523 I = PointerMap.find(From);
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000524 AliasSet *AS = I->second->getAliasSet(*this);
525 AS->addPointer(*this, Entry, I->second->getSize(), true);
Chris Lattnerb66e6482004-09-14 19:15:32 +0000526}
527
528
Chris Lattner2cffeec2003-12-18 08:11:56 +0000529
Chris Lattner9971ac42003-02-24 20:37:56 +0000530//===----------------------------------------------------------------------===//
531// AliasSet/AliasSetTracker Printing Support
532//===----------------------------------------------------------------------===//
533
534void AliasSet::print(std::ostream &OS) const {
535 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
Dan Gohman92c7b652008-04-21 19:48:48 +0000536 OS << (AliasTy == MustAlias ? "must" : "may") << " alias, ";
Chris Lattner9971ac42003-02-24 20:37:56 +0000537 switch (AccessTy) {
538 case NoModRef: OS << "No access "; break;
539 case Refs : OS << "Ref "; break;
540 case Mods : OS << "Mod "; break;
541 case ModRef : OS << "Mod/Ref "; break;
542 default: assert(0 && "Bad value for AccessTy!");
Chris Lattner009cc3d2002-09-26 21:49:07 +0000543 }
Chris Lattnere2609242003-12-14 04:52:11 +0000544 if (isVolatile()) OS << "[volatile] ";
Chris Lattner9971ac42003-02-24 20:37:56 +0000545 if (Forward)
546 OS << " forwarding to " << (void*)Forward;
547
548
Dan Gohmancb406c22007-10-03 19:26:29 +0000549 if (!empty()) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000550 OS << "Pointers: ";
551 for (iterator I = begin(), E = end(); I != E; ++I) {
552 if (I != begin()) OS << ", ";
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000553 WriteAsOperand(OS << "(", I.getPointer());
554 OS << ", " << I.getSize() << ")";
Chris Lattner9971ac42003-02-24 20:37:56 +0000555 }
556 }
557 if (!CallSites.empty()) {
558 OS << "\n " << CallSites.size() << " Call Sites: ";
559 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
560 if (i) OS << ", ";
561 WriteAsOperand(OS, CallSites[i].getCalledValue());
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000562 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000563 }
564 OS << "\n";
565}
566
567void AliasSetTracker::print(std::ostream &OS) const {
568 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
569 << PointerMap.size() << " pointer values.\n";
570 for (const_iterator I = begin(), E = end(); I != E; ++I)
571 I->print(OS);
572 OS << "\n";
573}
574
Bill Wendlinge8156192006-12-07 01:30:32 +0000575void AliasSet::dump() const { print (cerr); }
576void AliasSetTracker::dump() const { print(cerr); }
Chris Lattner9971ac42003-02-24 20:37:56 +0000577
Chris Lattner9971ac42003-02-24 20:37:56 +0000578//===----------------------------------------------------------------------===//
579// AliasSetPrinter Pass
580//===----------------------------------------------------------------------===//
581
582namespace {
Reid Spencerd7d83db2007-02-05 23:42:17 +0000583 class VISIBILITY_HIDDEN AliasSetPrinter : public FunctionPass {
Chris Lattner9971ac42003-02-24 20:37:56 +0000584 AliasSetTracker *Tracker;
585 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +0000586 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +0000587 AliasSetPrinter() : FunctionPass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +0000588
Chris Lattner9971ac42003-02-24 20:37:56 +0000589 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
590 AU.setPreservesAll();
591 AU.addRequired<AliasAnalysis>();
592 }
593
594 virtual bool runOnFunction(Function &F) {
595 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
596
597 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner6ffe5512004-04-27 15:13:33 +0000598 Tracker->add(&*I);
Bill Wendlinge8156192006-12-07 01:30:32 +0000599 Tracker->print(cerr);
Chris Lattner4983cf72006-01-03 06:05:22 +0000600 delete Tracker;
Chris Lattner9971ac42003-02-24 20:37:56 +0000601 return false;
602 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000603 };
Chris Lattner009cc3d2002-09-26 21:49:07 +0000604}
Dan Gohman844731a2008-05-13 00:00:25 +0000605
606char AliasSetPrinter::ID = 0;
607static RegisterPass<AliasSetPrinter>
608X("print-alias-sets", "Alias Set Printer", false, true);