blob: 7ba98dd4ce85dbc89b15ba1aafe2866920fc9971 [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"
Torok Edwinc25e7582009-07-11 20:10:48 +000023#include "llvm/Support/ErrorHandling.h"
Chris Lattner9971ac42003-02-24 20:37:56 +000024#include "llvm/Support/InstIterator.h"
Bill Wendling6f81b512006-11-28 22:46:12 +000025#include "llvm/Support/Streams.h"
Chris Lattnere2609242003-12-14 04:52:11 +000026using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Chris Lattnercc8d5242004-11-27 18:37:42 +000028/// mergeSetIn - Merge the specified alias set into this alias set.
Chris Lattner009cc3d2002-09-26 21:49:07 +000029///
Chris Lattnercc8d5242004-11-27 18:37:42 +000030void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
Chris Lattner9971ac42003-02-24 20:37:56 +000031 assert(!AS.Forward && "Alias set is already forwarding!");
32 assert(!Forward && "This set is a forwarding set!!");
Chris Lattner009cc3d2002-09-26 21:49:07 +000033
34 // Update the alias and access types of this set...
Chris Lattner9971ac42003-02-24 20:37:56 +000035 AccessTy |= AS.AccessTy;
36 AliasTy |= AS.AliasTy;
37
Chris Lattnercc8d5242004-11-27 18:37:42 +000038 if (AliasTy == MustAlias) {
39 // Check that these two merged sets really are must aliases. Since both
40 // used to be must-alias sets, we can just check any pointer from each set
41 // for aliasing.
42 AliasAnalysis &AA = AST.getAliasAnalysis();
Chris Lattnerd7168dd2009-03-09 05:11:09 +000043 PointerRec *L = getSomePointer();
44 PointerRec *R = AS.getSomePointer();
Chris Lattnercc8d5242004-11-27 18:37:42 +000045
46 // If the pointers are not a must-alias pair, this set becomes a may alias.
Chris Lattnerd7168dd2009-03-09 05:11:09 +000047 if (AA.alias(L->getValue(), L->getSize(), R->getValue(), R->getSize())
Chris Lattnercc8d5242004-11-27 18:37:42 +000048 != AliasAnalysis::MustAlias)
49 AliasTy = MayAlias;
50 }
51
Chris Lattner9971ac42003-02-24 20:37:56 +000052 if (CallSites.empty()) { // Merge call sites...
53 if (!AS.CallSites.empty())
54 std::swap(CallSites, AS.CallSites);
55 } else if (!AS.CallSites.empty()) {
56 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
57 AS.CallSites.clear();
58 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000059
Chris Lattner9971ac42003-02-24 20:37:56 +000060 AS.Forward = this; // Forward across AS now...
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000061 addRef(); // AS is now pointing to us...
Chris Lattner9971ac42003-02-24 20:37:56 +000062
63 // Merge the list of constituent pointers...
Chris Lattner2cffeec2003-12-18 08:11:56 +000064 if (AS.PtrList) {
65 *PtrListEnd = AS.PtrList;
Chris Lattnerd7168dd2009-03-09 05:11:09 +000066 AS.PtrList->setPrevInList(PtrListEnd);
Chris Lattner2cffeec2003-12-18 08:11:56 +000067 PtrListEnd = AS.PtrListEnd;
68
69 AS.PtrList = 0;
70 AS.PtrListEnd = &AS.PtrList;
Chris Lattner3080b602004-09-15 16:59:47 +000071 assert(*AS.PtrListEnd == 0 && "End of list is not null?");
Chris Lattner2cffeec2003-12-18 08:11:56 +000072 }
Chris Lattner009cc3d2002-09-26 21:49:07 +000073}
74
Chris Lattner9971ac42003-02-24 20:37:56 +000075void AliasSetTracker::removeAliasSet(AliasSet *AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000076 if (AliasSet *Fwd = AS->Forward) {
77 Fwd->dropRef(*this);
78 AS->Forward = 0;
79 }
Chris Lattner9971ac42003-02-24 20:37:56 +000080 AliasSets.erase(AS);
81}
82
83void AliasSet::removeFromTracker(AliasSetTracker &AST) {
84 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
85 AST.removeAliasSet(this);
86}
87
Chris Lattnerd7168dd2009-03-09 05:11:09 +000088void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
Chris Lattnerb66e6482004-09-14 19:15:32 +000089 unsigned Size, bool KnownMustAlias) {
Chris Lattnerd7168dd2009-03-09 05:11:09 +000090 assert(!Entry.hasAliasSet() && "Entry already in set!");
Chris Lattner9971ac42003-02-24 20:37:56 +000091
Chris Lattnerb66e6482004-09-14 19:15:32 +000092 // Check to see if we have to downgrade to _may_ alias.
93 if (isMustAlias() && !KnownMustAlias)
Chris Lattnerd7168dd2009-03-09 05:11:09 +000094 if (PointerRec *P = getSomePointer()) {
Chris Lattner3080b602004-09-15 16:59:47 +000095 AliasAnalysis &AA = AST.getAliasAnalysis();
Chris Lattner31a9d182003-02-26 22:11:00 +000096 AliasAnalysis::AliasResult Result =
Chris Lattnerd7168dd2009-03-09 05:11:09 +000097 AA.alias(P->getValue(), P->getSize(), Entry.getValue(), Size);
Chris Lattner31a9d182003-02-26 22:11:00 +000098 if (Result == AliasAnalysis::MayAlias)
Chris Lattner9971ac42003-02-24 20:37:56 +000099 AliasTy = MayAlias;
Chris Lattner31a9d182003-02-26 22:11:00 +0000100 else // First entry of must alias must have maximum size!
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000101 P->updateSize(Size);
Chris Lattner31a9d182003-02-26 22:11:00 +0000102 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
103 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000104
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000105 Entry.setAliasSet(this);
106 Entry.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000107
108 // Add it to the end of the list...
Chris Lattner2cffeec2003-12-18 08:11:56 +0000109 assert(*PtrListEnd == 0 && "End of list is not null?");
110 *PtrListEnd = &Entry;
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000111 PtrListEnd = Entry.setPrevInList(PtrListEnd);
Chris Lattner3080b602004-09-15 16:59:47 +0000112 assert(*PtrListEnd == 0 && "End of list is not null?");
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000113 addRef(); // Entry points to alias set...
Chris Lattner9971ac42003-02-24 20:37:56 +0000114}
115
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000116void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000117 CallSites.push_back(CS);
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000118
Duncan Sandsdff67102007-12-01 07:51:45 +0000119 AliasAnalysis::ModRefBehavior Behavior = AA.getModRefBehavior(CS);
120 if (Behavior == AliasAnalysis::DoesNotAccessMemory)
121 return;
122 else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
123 AliasTy = MayAlias;
124 AccessTy |= Refs;
125 return;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000126 }
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 Lattnerd7168dd2009-03-09 05:11:09 +0000143 PointerRec *SomePtr = getSomePointer();
Chris Lattner9971ac42003-02-24 20:37:56 +0000144 assert(SomePtr && "Empty must-alias set??");
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000145 return AA.alias(SomePtr->getValue(), SomePtr->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 {
Duncan Sandsdff67102007-12-01 07:51:45 +0000169 if (AA.doesNotAccessMemory(CS))
170 return false;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000171
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
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000188void AliasSetTracker::clear() {
189 // Delete all the PointerRec entries.
190 for (DenseMap<Value*, AliasSet::PointerRec*>::iterator I = PointerMap.begin(),
191 E = PointerMap.end(); I != E; ++I)
192 I->second->eraseFromList();
193
194 PointerMap.clear();
195
196 // The alias sets should all be clear now.
197 AliasSets.clear();
198}
199
Chris Lattner009cc3d2002-09-26 21:49:07 +0000200
Chris Lattner009cc3d2002-09-26 21:49:07 +0000201/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
202/// instruction referring to the pointer into. If there are multiple alias sets
203/// that may alias the pointer, merge them together and return the unified set.
204///
Chris Lattner31a9d182003-02-26 22:11:00 +0000205AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
206 unsigned Size) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000207 AliasSet *FoundSet = 0;
Chris Lattner9971ac42003-02-24 20:37:56 +0000208 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner31a9d182003-02-26 22:11:00 +0000209 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
Chris Lattnercc8d5242004-11-27 18:37:42 +0000210 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
Chris Lattner9971ac42003-02-24 20:37:56 +0000211 FoundSet = I; // Remember it.
Chris Lattnercc8d5242004-11-27 18:37:42 +0000212 } else { // Otherwise, we must merge the sets.
213 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
Chris Lattner009cc3d2002-09-26 21:49:07 +0000214 }
215 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000216
217 return FoundSet;
218}
219
Chris Lattner07bfa522004-11-26 21:36:25 +0000220/// containsPointer - Return true if the specified location is represented by
221/// this alias set, false otherwise. This does not modify the AST object or
222/// alias sets.
223bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const {
224 for (const_iterator I = begin(), E = end(); I != E; ++I)
225 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA))
226 return true;
227 return false;
228}
229
230
231
Chris Lattner9971ac42003-02-24 20:37:56 +0000232AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
233 AliasSet *FoundSet = 0;
234 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000235 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
Chris Lattnercc8d5242004-11-27 18:37:42 +0000236 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
Chris Lattner9971ac42003-02-24 20:37:56 +0000237 FoundSet = I; // Remember it.
Chris Lattnercc8d5242004-11-27 18:37:42 +0000238 } else if (!I->Forward) { // Otherwise, we must merge the sets.
239 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
Chris Lattner9971ac42003-02-24 20:37:56 +0000240 }
241 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000242
243 return FoundSet;
244}
245
246
Chris Lattner009cc3d2002-09-26 21:49:07 +0000247
Chris Lattner9971ac42003-02-24 20:37:56 +0000248
249/// getAliasSetForPointer - Return the alias set that the specified pointer
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000250/// lives in.
Chris Lattner12c11552004-07-21 05:18:04 +0000251AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
252 bool *New) {
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000253 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
Chris Lattner9971ac42003-02-24 20:37:56 +0000254
255 // Check to see if the pointer is already known...
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000256 if (Entry.hasAliasSet()) {
257 Entry.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000258 // Return the set!
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000259 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattner31a9d182003-02-26 22:11:00 +0000260 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000261 // Add it to the alias set it aliases...
Chris Lattner31a9d182003-02-26 22:11:00 +0000262 AS->addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000263 return *AS;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000264 } else {
Chris Lattner12c11552004-07-21 05:18:04 +0000265 if (New) *New = true;
Chris Lattner9971ac42003-02-24 20:37:56 +0000266 // Otherwise create a new alias set to hold the loaded pointer...
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000267 AliasSets.push_back(new AliasSet());
Chris Lattner31a9d182003-02-26 22:11:00 +0000268 AliasSets.back().addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000269 return AliasSets.back();
Chris Lattner009cc3d2002-09-26 21:49:07 +0000270 }
271}
272
Chris Lattner61d46272004-07-26 05:50:23 +0000273bool AliasSetTracker::add(Value *Ptr, unsigned Size) {
274 bool NewPtr;
275 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
276 return NewPtr;
277}
278
279
Chris Lattner12c11552004-07-21 05:18:04 +0000280bool AliasSetTracker::add(LoadInst *LI) {
281 bool NewPtr;
282 AliasSet &AS = addPointer(LI->getOperand(0),
Duncan Sands514ab342007-11-01 20:53:16 +0000283 AA.getTargetData().getTypeStoreSize(LI->getType()),
Chris Lattner12c11552004-07-21 05:18:04 +0000284 AliasSet::Refs, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000285 if (LI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000286 return NewPtr;
Chris Lattner9971ac42003-02-24 20:37:56 +0000287}
288
Chris Lattner12c11552004-07-21 05:18:04 +0000289bool AliasSetTracker::add(StoreInst *SI) {
290 bool NewPtr;
291 Value *Val = SI->getOperand(0);
292 AliasSet &AS = addPointer(SI->getOperand(1),
Duncan Sands514ab342007-11-01 20:53:16 +0000293 AA.getTargetData().getTypeStoreSize(Val->getType()),
Chris Lattner12c11552004-07-21 05:18:04 +0000294 AliasSet::Mods, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000295 if (SI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000296 return NewPtr;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000297}
298
Chris Lattner5c882602004-07-25 07:57:37 +0000299bool AliasSetTracker::add(FreeInst *FI) {
300 bool NewPtr;
Chris Lattner05a24e52008-05-22 03:23:06 +0000301 addPointer(FI->getOperand(0), ~0, AliasSet::Mods, NewPtr);
Chris Lattner5c882602004-07-25 07:57:37 +0000302 return NewPtr;
303}
304
Dan Gohman235fc572008-04-14 18:34:50 +0000305bool AliasSetTracker::add(VAArgInst *VAAI) {
306 bool NewPtr;
Chris Lattner05a24e52008-05-22 03:23:06 +0000307 addPointer(VAAI->getOperand(0), ~0, AliasSet::ModRef, NewPtr);
Dan Gohman235fc572008-04-14 18:34:50 +0000308 return NewPtr;
309}
310
Chris Lattnere2609242003-12-14 04:52:11 +0000311
Chris Lattner12c11552004-07-21 05:18:04 +0000312bool AliasSetTracker::add(CallSite CS) {
Zhou Sheng7e4286e2009-03-03 06:02:04 +0000313 if (isa<DbgInfoIntrinsic>(CS.getInstruction()))
314 return true; // Ignore DbgInfo Intrinsics.
Duncan Sandsdff67102007-12-01 07:51:45 +0000315 if (AA.doesNotAccessMemory(CS))
316 return true; // doesn't alias anything
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000317
Chris Lattner9971ac42003-02-24 20:37:56 +0000318 AliasSet *AS = findAliasSetForCallSite(CS);
319 if (!AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000320 AliasSets.push_back(new AliasSet());
Chris Lattner9971ac42003-02-24 20:37:56 +0000321 AS = &AliasSets.back();
Chris Lattner12c11552004-07-21 05:18:04 +0000322 AS->addCallSite(CS, AA);
323 return true;
324 } else {
325 AS->addCallSite(CS, AA);
326 return false;
Chris Lattner9971ac42003-02-24 20:37:56 +0000327 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000328}
329
Chris Lattner12c11552004-07-21 05:18:04 +0000330bool AliasSetTracker::add(Instruction *I) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000331 // Dispatch to one of the other add methods...
332 if (LoadInst *LI = dyn_cast<LoadInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000333 return add(LI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000334 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000335 return add(SI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000336 else if (CallInst *CI = dyn_cast<CallInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000337 return add(CI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000338 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000339 return add(II);
Chris Lattner5c882602004-07-25 07:57:37 +0000340 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
341 return add(FI);
Dan Gohman235fc572008-04-14 18:34:50 +0000342 else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
343 return add(VAAI);
Chris Lattner12c11552004-07-21 05:18:04 +0000344 return true;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000345}
346
Chris Lattner319d05b2003-03-03 23:28:05 +0000347void AliasSetTracker::add(BasicBlock &BB) {
348 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
349 add(I);
350}
351
352void AliasSetTracker::add(const AliasSetTracker &AST) {
353 assert(&AA == &AST.AA &&
354 "Merging AliasSetTracker objects with different Alias Analyses!");
355
356 // Loop over all of the alias sets in AST, adding the pointers contained
357 // therein into the current alias sets. This can cause alias sets to be
358 // merged together in the current AST.
359 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
360 if (!I->Forward) { // Ignore forwarding alias sets
361 AliasSet &AS = const_cast<AliasSet&>(*I);
362
363 // If there are any call sites in the alias set, add them to this AST.
364 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
365 add(AS.CallSites[i]);
366
367 // Loop over all of the pointers in this alias set...
368 AliasSet::iterator I = AS.begin(), E = AS.end();
Chris Lattner12c11552004-07-21 05:18:04 +0000369 bool X;
Chris Lattnerf711fb72007-05-23 06:36:35 +0000370 for (; I != E; ++I) {
371 AliasSet &NewAS = addPointer(I.getPointer(), I.getSize(),
372 (AliasSet::AccessType)AS.AccessTy, X);
373 if (AS.isVolatile()) NewAS.setVolatile();
374 }
Chris Lattner319d05b2003-03-03 23:28:05 +0000375 }
376}
377
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000378/// remove - Remove the specified (potentially non-empty) alias set from the
379/// tracker.
380void AliasSetTracker::remove(AliasSet &AS) {
Chris Lattnerf2998572006-06-27 23:48:59 +0000381 // Drop all call sites.
382 AS.CallSites.clear();
383
384 // Clear the alias set.
385 unsigned NumRefs = 0;
386 while (!AS.empty()) {
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000387 AliasSet::PointerRec *P = AS.PtrList;
388
389 Value *ValToRemove = P->getValue();
Chris Lattnerf2998572006-06-27 23:48:59 +0000390
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000391 // Unlink and delete entry from the list of values.
392 P->eraseFromList();
Chris Lattnerf2998572006-06-27 23:48:59 +0000393
394 // Remember how many references need to be dropped.
395 ++NumRefs;
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000396
Chris Lattnerf2998572006-06-27 23:48:59 +0000397 // Finally, remove the entry.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000398 PointerMap.erase(ValToRemove);
Chris Lattnerf2998572006-06-27 23:48:59 +0000399 }
400
401 // Stop using the alias set, removing it.
Chris Lattner356d8c22006-06-27 23:56:13 +0000402 AS.RefCount -= NumRefs;
403 if (AS.RefCount == 0)
404 AS.removeFromTracker(*this);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000405}
406
Chris Lattner61d46272004-07-26 05:50:23 +0000407bool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
408 AliasSet *AS = findAliasSetForPointer(Ptr, Size);
409 if (!AS) return false;
410 remove(*AS);
411 return true;
412}
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000413
414bool AliasSetTracker::remove(LoadInst *LI) {
Duncan Sands514ab342007-11-01 20:53:16 +0000415 unsigned Size = AA.getTargetData().getTypeStoreSize(LI->getType());
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000416 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
417 if (!AS) return false;
418 remove(*AS);
419 return true;
420}
421
422bool AliasSetTracker::remove(StoreInst *SI) {
Duncan Sands514ab342007-11-01 20:53:16 +0000423 unsigned Size =
424 AA.getTargetData().getTypeStoreSize(SI->getOperand(0)->getType());
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000425 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
426 if (!AS) return false;
427 remove(*AS);
428 return true;
429}
430
Chris Lattner5c882602004-07-25 07:57:37 +0000431bool AliasSetTracker::remove(FreeInst *FI) {
432 AliasSet *AS = findAliasSetForPointer(FI->getOperand(0), ~0);
433 if (!AS) return false;
434 remove(*AS);
435 return true;
436}
437
Dan Gohman235fc572008-04-14 18:34:50 +0000438bool AliasSetTracker::remove(VAArgInst *VAAI) {
439 AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0), ~0);
440 if (!AS) return false;
441 remove(*AS);
442 return true;
443}
444
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000445bool AliasSetTracker::remove(CallSite CS) {
Duncan Sandsdff67102007-12-01 07:51:45 +0000446 if (AA.doesNotAccessMemory(CS))
447 return false; // doesn't alias anything
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000448
449 AliasSet *AS = findAliasSetForCallSite(CS);
450 if (!AS) return false;
451 remove(*AS);
452 return true;
453}
454
455bool AliasSetTracker::remove(Instruction *I) {
456 // Dispatch to one of the other remove methods...
457 if (LoadInst *LI = dyn_cast<LoadInst>(I))
458 return remove(LI);
459 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
460 return remove(SI);
461 else if (CallInst *CI = dyn_cast<CallInst>(I))
462 return remove(CI);
Chris Lattner5c882602004-07-25 07:57:37 +0000463 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
464 return remove(FI);
Dan Gohman235fc572008-04-14 18:34:50 +0000465 else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
466 return remove(VAAI);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000467 return true;
468}
469
Chris Lattner2cffeec2003-12-18 08:11:56 +0000470
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000471// deleteValue method - This method is used to remove a pointer value from the
Chris Lattner2cffeec2003-12-18 08:11:56 +0000472// AliasSetTracker entirely. It should be used when an instruction is deleted
473// from the program to update the AST. If you don't use this, you would have
474// dangling pointers to deleted instructions.
475//
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000476void AliasSetTracker::deleteValue(Value *PtrVal) {
Chris Lattnerb66e6482004-09-14 19:15:32 +0000477 // Notify the alias analysis implementation that this value is gone.
478 AA.deleteValue(PtrVal);
479
Chris Lattner959e3212006-06-26 19:20:48 +0000480 // If this is a call instruction, remove the callsite from the appropriate
481 // AliasSet.
482 CallSite CS = CallSite::get(PtrVal);
Duncan Sandsdff67102007-12-01 07:51:45 +0000483 if (CS.getInstruction())
484 if (!AA.doesNotAccessMemory(CS))
Chris Lattner959e3212006-06-26 19:20:48 +0000485 if (AliasSet *AS = findAliasSetForCallSite(CS))
486 AS->removeCallSite(CS);
Chris Lattner959e3212006-06-26 19:20:48 +0000487
Chris Lattnerb66e6482004-09-14 19:15:32 +0000488 // First, look up the PointerRec for this pointer.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000489 DenseMap<Value*, AliasSet::PointerRec*>::iterator I = PointerMap.find(PtrVal);
Chris Lattner2cffeec2003-12-18 08:11:56 +0000490 if (I == PointerMap.end()) return; // Noop
491
492 // If we found one, remove the pointer from the alias set it is in.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000493 AliasSet::PointerRec *PtrValEnt = I->second;
494 AliasSet *AS = PtrValEnt->getAliasSet(*this);
Chris Lattner2cffeec2003-12-18 08:11:56 +0000495
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000496 // Unlink and delete from the list of values.
497 PtrValEnt->eraseFromList();
498
499 // Stop using the alias set.
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000500 AS->dropRef(*this);
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000501
Chris Lattner2cffeec2003-12-18 08:11:56 +0000502 PointerMap.erase(I);
503}
504
Chris Lattnerb66e6482004-09-14 19:15:32 +0000505// copyValue - This method should be used whenever a preexisting value in the
506// program is copied or cloned, introducing a new value. Note that it is ok for
507// clients that use this method to introduce the same value multiple times: if
508// the tracker already knows about a value, it will ignore the request.
509//
510void AliasSetTracker::copyValue(Value *From, Value *To) {
511 // Notify the alias analysis implementation that this value is copied.
512 AA.copyValue(From, To);
513
514 // First, look up the PointerRec for this pointer.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000515 DenseMap<Value*, AliasSet::PointerRec*>::iterator I = PointerMap.find(From);
516 if (I == PointerMap.end())
Chris Lattnerb66e6482004-09-14 19:15:32 +0000517 return; // Noop
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000518 assert(I->second->hasAliasSet() && "Dead entry?");
Chris Lattnerb66e6482004-09-14 19:15:32 +0000519
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000520 AliasSet::PointerRec &Entry = getEntryFor(To);
521 if (Entry.hasAliasSet()) return; // Already in the tracker!
Chris Lattnerb66e6482004-09-14 19:15:32 +0000522
523 // Add it to the alias set it aliases...
Devang Patel6d9a2df2009-03-30 18:34:47 +0000524 I = PointerMap.find(From);
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000525 AliasSet *AS = I->second->getAliasSet(*this);
526 AS->addPointer(*this, Entry, I->second->getSize(), true);
Chris Lattnerb66e6482004-09-14 19:15:32 +0000527}
528
529
Chris Lattner2cffeec2003-12-18 08:11:56 +0000530
Chris Lattner9971ac42003-02-24 20:37:56 +0000531//===----------------------------------------------------------------------===//
532// AliasSet/AliasSetTracker Printing Support
533//===----------------------------------------------------------------------===//
534
535void AliasSet::print(std::ostream &OS) const {
536 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
Dan Gohman92c7b652008-04-21 19:48:48 +0000537 OS << (AliasTy == MustAlias ? "must" : "may") << " alias, ";
Chris Lattner9971ac42003-02-24 20:37:56 +0000538 switch (AccessTy) {
539 case NoModRef: OS << "No access "; break;
540 case Refs : OS << "Ref "; break;
541 case Mods : OS << "Mod "; break;
542 case ModRef : OS << "Mod/Ref "; break;
Torok Edwinc25e7582009-07-11 20:10:48 +0000543 default: LLVM_UNREACHABLE("Bad value for AccessTy!");
Chris Lattner009cc3d2002-09-26 21:49:07 +0000544 }
Chris Lattnere2609242003-12-14 04:52:11 +0000545 if (isVolatile()) OS << "[volatile] ";
Chris Lattner9971ac42003-02-24 20:37:56 +0000546 if (Forward)
547 OS << " forwarding to " << (void*)Forward;
548
549
Dan Gohmancb406c22007-10-03 19:26:29 +0000550 if (!empty()) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000551 OS << "Pointers: ";
552 for (iterator I = begin(), E = end(); I != E; ++I) {
553 if (I != begin()) OS << ", ";
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000554 WriteAsOperand(OS << "(", I.getPointer());
555 OS << ", " << I.getSize() << ")";
Chris Lattner9971ac42003-02-24 20:37:56 +0000556 }
557 }
558 if (!CallSites.empty()) {
559 OS << "\n " << CallSites.size() << " Call Sites: ";
560 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
561 if (i) OS << ", ";
562 WriteAsOperand(OS, CallSites[i].getCalledValue());
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000563 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000564 }
565 OS << "\n";
566}
567
568void AliasSetTracker::print(std::ostream &OS) const {
569 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
570 << PointerMap.size() << " pointer values.\n";
571 for (const_iterator I = begin(), E = end(); I != E; ++I)
572 I->print(OS);
573 OS << "\n";
574}
575
Bill Wendlinge8156192006-12-07 01:30:32 +0000576void AliasSet::dump() const { print (cerr); }
577void AliasSetTracker::dump() const { print(cerr); }
Chris Lattner9971ac42003-02-24 20:37:56 +0000578
Chris Lattner9971ac42003-02-24 20:37:56 +0000579//===----------------------------------------------------------------------===//
580// AliasSetPrinter Pass
581//===----------------------------------------------------------------------===//
582
583namespace {
Reid Spencerd7d83db2007-02-05 23:42:17 +0000584 class VISIBILITY_HIDDEN AliasSetPrinter : public FunctionPass {
Chris Lattner9971ac42003-02-24 20:37:56 +0000585 AliasSetTracker *Tracker;
586 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +0000587 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +0000588 AliasSetPrinter() : FunctionPass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +0000589
Chris Lattner9971ac42003-02-24 20:37:56 +0000590 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
591 AU.setPreservesAll();
592 AU.addRequired<AliasAnalysis>();
593 }
594
595 virtual bool runOnFunction(Function &F) {
596 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
597
598 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner6ffe5512004-04-27 15:13:33 +0000599 Tracker->add(&*I);
Bill Wendlinge8156192006-12-07 01:30:32 +0000600 Tracker->print(cerr);
Chris Lattner4983cf72006-01-03 06:05:22 +0000601 delete Tracker;
Chris Lattner9971ac42003-02-24 20:37:56 +0000602 return false;
603 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000604 };
Chris Lattner009cc3d2002-09-26 21:49:07 +0000605}
Dan Gohman844731a2008-05-13 00:00:25 +0000606
607char AliasSetPrinter::ID = 0;
608static RegisterPass<AliasSetPrinter>
609X("print-alias-sets", "Alias Set Printer", false, true);