blob: 5a163a2ab0f259a1a8d17bd481db5d1195a86460 [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"
David Greene43c48ac2009-12-23 19:27:59 +000022#include "llvm/Support/Debug.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"
Chris Lattner791102f2009-08-23 05:17:37 +000025#include "llvm/Support/Format.h"
26#include "llvm/Support/raw_ostream.h"
Chris Lattnere2609242003-12-14 04:52:11 +000027using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattnercc8d5242004-11-27 18:37:42 +000029/// mergeSetIn - Merge the specified alias set into this alias set.
Chris Lattner009cc3d2002-09-26 21:49:07 +000030///
Chris Lattnercc8d5242004-11-27 18:37:42 +000031void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
Chris Lattner9971ac42003-02-24 20:37:56 +000032 assert(!AS.Forward && "Alias set is already forwarding!");
33 assert(!Forward && "This set is a forwarding set!!");
Chris Lattner009cc3d2002-09-26 21:49:07 +000034
35 // Update the alias and access types of this set...
Chris Lattner9971ac42003-02-24 20:37:56 +000036 AccessTy |= AS.AccessTy;
37 AliasTy |= AS.AliasTy;
Chris Lattnerfedac7d2010-08-29 04:14:47 +000038 Volatile |= AS.Volatile;
Chris Lattner9971ac42003-02-24 20:37:56 +000039
Chris Lattnercc8d5242004-11-27 18:37:42 +000040 if (AliasTy == MustAlias) {
41 // Check that these two merged sets really are must aliases. Since both
42 // used to be must-alias sets, we can just check any pointer from each set
43 // for aliasing.
44 AliasAnalysis &AA = AST.getAliasAnalysis();
Chris Lattnerd7168dd2009-03-09 05:11:09 +000045 PointerRec *L = getSomePointer();
46 PointerRec *R = AS.getSomePointer();
Chris Lattnercc8d5242004-11-27 18:37:42 +000047
48 // If the pointers are not a must-alias pair, this set becomes a may alias.
Chris Lattnerd7168dd2009-03-09 05:11:09 +000049 if (AA.alias(L->getValue(), L->getSize(), R->getValue(), R->getSize())
Chris Lattnercc8d5242004-11-27 18:37:42 +000050 != AliasAnalysis::MustAlias)
51 AliasTy = MayAlias;
52 }
53
Chris Lattner9971ac42003-02-24 20:37:56 +000054 if (CallSites.empty()) { // Merge call sites...
55 if (!AS.CallSites.empty())
56 std::swap(CallSites, AS.CallSites);
57 } else if (!AS.CallSites.empty()) {
58 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
59 AS.CallSites.clear();
60 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000061
Chris Lattner9971ac42003-02-24 20:37:56 +000062 AS.Forward = this; // Forward across AS now...
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000063 addRef(); // AS is now pointing to us...
Chris Lattner9971ac42003-02-24 20:37:56 +000064
65 // Merge the list of constituent pointers...
Chris Lattner2cffeec2003-12-18 08:11:56 +000066 if (AS.PtrList) {
67 *PtrListEnd = AS.PtrList;
Chris Lattnerd7168dd2009-03-09 05:11:09 +000068 AS.PtrList->setPrevInList(PtrListEnd);
Chris Lattner2cffeec2003-12-18 08:11:56 +000069 PtrListEnd = AS.PtrListEnd;
70
71 AS.PtrList = 0;
72 AS.PtrListEnd = &AS.PtrList;
Chris Lattner3080b602004-09-15 16:59:47 +000073 assert(*AS.PtrListEnd == 0 && "End of list is not null?");
Chris Lattner2cffeec2003-12-18 08:11:56 +000074 }
Chris Lattner009cc3d2002-09-26 21:49:07 +000075}
76
Chris Lattner9971ac42003-02-24 20:37:56 +000077void AliasSetTracker::removeAliasSet(AliasSet *AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000078 if (AliasSet *Fwd = AS->Forward) {
79 Fwd->dropRef(*this);
80 AS->Forward = 0;
81 }
Chris Lattner9971ac42003-02-24 20:37:56 +000082 AliasSets.erase(AS);
83}
84
85void AliasSet::removeFromTracker(AliasSetTracker &AST) {
86 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
87 AST.removeAliasSet(this);
88}
89
Chris Lattnerd7168dd2009-03-09 05:11:09 +000090void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
Chris Lattnerb66e6482004-09-14 19:15:32 +000091 unsigned Size, bool KnownMustAlias) {
Chris Lattnerd7168dd2009-03-09 05:11:09 +000092 assert(!Entry.hasAliasSet() && "Entry already in set!");
Chris Lattner9971ac42003-02-24 20:37:56 +000093
Chris Lattnerb66e6482004-09-14 19:15:32 +000094 // Check to see if we have to downgrade to _may_ alias.
95 if (isMustAlias() && !KnownMustAlias)
Chris Lattnerd7168dd2009-03-09 05:11:09 +000096 if (PointerRec *P = getSomePointer()) {
Chris Lattner3080b602004-09-15 16:59:47 +000097 AliasAnalysis &AA = AST.getAliasAnalysis();
Chris Lattner31a9d182003-02-26 22:11:00 +000098 AliasAnalysis::AliasResult Result =
Chris Lattnerd7168dd2009-03-09 05:11:09 +000099 AA.alias(P->getValue(), P->getSize(), Entry.getValue(), Size);
Chris Lattner31a9d182003-02-26 22:11:00 +0000100 if (Result == AliasAnalysis::MayAlias)
Chris Lattner9971ac42003-02-24 20:37:56 +0000101 AliasTy = MayAlias;
Chris Lattner31a9d182003-02-26 22:11:00 +0000102 else // First entry of must alias must have maximum size!
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000103 P->updateSize(Size);
Chris Lattner31a9d182003-02-26 22:11:00 +0000104 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
105 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000106
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000107 Entry.setAliasSet(this);
108 Entry.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000109
110 // Add it to the end of the list...
Chris Lattner2cffeec2003-12-18 08:11:56 +0000111 assert(*PtrListEnd == 0 && "End of list is not null?");
112 *PtrListEnd = &Entry;
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000113 PtrListEnd = Entry.setPrevInList(PtrListEnd);
Chris Lattner3080b602004-09-15 16:59:47 +0000114 assert(*PtrListEnd == 0 && "End of list is not null?");
Chris Lattner9476d742010-08-29 04:13:43 +0000115 addRef(); // Entry points to alias set.
Chris Lattner9971ac42003-02-24 20:37:56 +0000116}
117
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000118void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
Chris Lattnercb7f6532010-08-29 18:42:23 +0000119 CallSites.push_back(CS.getInstruction());
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000120
Duncan Sandsdff67102007-12-01 07:51:45 +0000121 AliasAnalysis::ModRefBehavior Behavior = AA.getModRefBehavior(CS);
122 if (Behavior == AliasAnalysis::DoesNotAccessMemory)
123 return;
124 else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
125 AliasTy = MayAlias;
126 AccessTy |= Refs;
127 return;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000128 }
129
130 // FIXME: This should use mod/ref information to make this not suck so bad
131 AliasTy = MayAlias;
Chris Lattner577385e2003-05-03 03:42:08 +0000132 AccessTy = ModRef;
Chris Lattner9971ac42003-02-24 20:37:56 +0000133}
134
135/// aliasesPointer - Return true if the specified pointer "may" (or must)
Chris Lattner009cc3d2002-09-26 21:49:07 +0000136/// alias one of the members in the set.
137///
Chris Lattner31a9d182003-02-26 22:11:00 +0000138bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
139 AliasAnalysis &AA) const {
Chris Lattner9971ac42003-02-24 20:37:56 +0000140 if (AliasTy == MustAlias) {
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000141 assert(CallSites.empty() && "Illegal must alias set!");
142
Chris Lattner9971ac42003-02-24 20:37:56 +0000143 // If this is a set of MustAliases, only check to see if the pointer aliases
Chris Lattner9476d742010-08-29 04:13:43 +0000144 // SOME value in the set.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000145 PointerRec *SomePtr = getSomePointer();
Chris Lattner9971ac42003-02-24 20:37:56 +0000146 assert(SomePtr && "Empty must-alias set??");
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000147 return AA.alias(SomePtr->getValue(), SomePtr->getSize(), Ptr, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000148 }
149
150 // If this is a may-alias set, we have to check all of the pointers in the set
151 // to be sure it doesn't alias the set...
152 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000153 if (AA.alias(Ptr, Size, I.getPointer(), I.getSize()))
Chris Lattner9971ac42003-02-24 20:37:56 +0000154 return true;
155
156 // Check the call sites list and invoke list...
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000157 if (!CallSites.empty()) {
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000158 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
Chris Lattner6e1f5102010-08-29 04:06:55 +0000159 if (AA.getModRefInfo(CallSites[i], Ptr, Size) != AliasAnalysis::NoModRef)
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000160 return true;
161 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000162
Chris Lattner009cc3d2002-09-26 21:49:07 +0000163 return false;
164}
165
Chris Lattner9971ac42003-02-24 20:37:56 +0000166bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
Duncan Sandsdff67102007-12-01 07:51:45 +0000167 if (AA.doesNotAccessMemory(CS))
168 return false;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000169
Chris Lattnercb7f6532010-08-29 18:42:23 +0000170 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
171 if (AA.getModRefInfo(getCallSite(i), CS) != AliasAnalysis::NoModRef ||
172 AA.getModRefInfo(CS, getCallSite(i)) != AliasAnalysis::NoModRef)
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000173 return true;
Chris Lattnercb7f6532010-08-29 18:42:23 +0000174 }
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000175
176 for (iterator I = begin(), E = end(); I != E; ++I)
177 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
178 AliasAnalysis::NoModRef)
179 return true;
180
181 return false;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000182}
183
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000184void AliasSetTracker::clear() {
185 // Delete all the PointerRec entries.
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000186 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
187 I != E; ++I)
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000188 I->second->eraseFromList();
189
190 PointerMap.clear();
191
192 // The alias sets should all be clear now.
193 AliasSets.clear();
194}
195
Chris Lattner009cc3d2002-09-26 21:49:07 +0000196
Chris Lattner009cc3d2002-09-26 21:49:07 +0000197/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
198/// instruction referring to the pointer into. If there are multiple alias sets
199/// that may alias the pointer, merge them together and return the unified set.
200///
Chris Lattner31a9d182003-02-26 22:11:00 +0000201AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
202 unsigned Size) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000203 AliasSet *FoundSet = 0;
Chris Lattner6e1f5102010-08-29 04:06:55 +0000204 for (iterator I = begin(), E = end(); I != E; ++I) {
205 if (I->Forward || !I->aliasesPointer(Ptr, Size, AA)) continue;
206
207 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
208 FoundSet = I; // Remember it.
209 } else { // Otherwise, we must merge the sets.
210 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
Chris Lattner009cc3d2002-09-26 21:49:07 +0000211 }
Chris Lattner6e1f5102010-08-29 04:06:55 +0000212 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000213
214 return FoundSet;
215}
216
Chris Lattner07bfa522004-11-26 21:36:25 +0000217/// containsPointer - Return true if the specified location is represented by
218/// this alias set, false otherwise. This does not modify the AST object or
219/// alias sets.
220bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const {
221 for (const_iterator I = begin(), E = end(); I != E; ++I)
222 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA))
223 return true;
224 return false;
225}
226
227
228
Chris Lattner9971ac42003-02-24 20:37:56 +0000229AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
230 AliasSet *FoundSet = 0;
Chris Lattner6e1f5102010-08-29 04:06:55 +0000231 for (iterator I = begin(), E = end(); I != E; ++I) {
232 if (I->Forward || !I->aliasesCallSite(CS, AA))
233 continue;
234
Chris Lattnercb7f6532010-08-29 18:42:23 +0000235 if (FoundSet == 0) // If this is the first alias set ptr can go into.
236 FoundSet = I; // Remember it.
237 else if (!I->Forward) // Otherwise, we must merge the sets.
Chris Lattner6e1f5102010-08-29 04:06:55 +0000238 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
Chris Lattner6e1f5102010-08-29 04:06:55 +0000239 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000240 return FoundSet;
241}
242
243
Chris Lattner009cc3d2002-09-26 21:49:07 +0000244
Chris Lattner9971ac42003-02-24 20:37:56 +0000245
246/// getAliasSetForPointer - Return the alias set that the specified pointer
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000247/// lives in.
Chris Lattner12c11552004-07-21 05:18:04 +0000248AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
249 bool *New) {
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000250 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
Chris Lattner9971ac42003-02-24 20:37:56 +0000251
Chris Lattner9476d742010-08-29 04:13:43 +0000252 // Check to see if the pointer is already known.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000253 if (Entry.hasAliasSet()) {
254 Entry.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000255 // Return the set!
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000256 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000257 }
258
259 if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
Chris Lattner9476d742010-08-29 04:13:43 +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 }
Chris Lattner6e1f5102010-08-29 04:06:55 +0000264
265 if (New) *New = true;
Chris Lattner9476d742010-08-29 04:13:43 +0000266 // Otherwise create a new alias set to hold the loaded pointer.
Chris Lattner6e1f5102010-08-29 04:06:55 +0000267 AliasSets.push_back(new AliasSet());
268 AliasSets.back().addPointer(*this, Entry, Size);
269 return AliasSets.back();
Chris Lattner009cc3d2002-09-26 21:49:07 +0000270}
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),
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000282 AA.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),
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000292 AA.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
Dan Gohman235fc572008-04-14 18:34:50 +0000298bool AliasSetTracker::add(VAArgInst *VAAI) {
299 bool NewPtr;
Chris Lattner05a24e52008-05-22 03:23:06 +0000300 addPointer(VAAI->getOperand(0), ~0, AliasSet::ModRef, NewPtr);
Dan Gohman235fc572008-04-14 18:34:50 +0000301 return NewPtr;
302}
303
Chris Lattnere2609242003-12-14 04:52:11 +0000304
Chris Lattner12c11552004-07-21 05:18:04 +0000305bool AliasSetTracker::add(CallSite CS) {
Zhou Sheng7e4286e2009-03-03 06:02:04 +0000306 if (isa<DbgInfoIntrinsic>(CS.getInstruction()))
307 return true; // Ignore DbgInfo Intrinsics.
Duncan Sandsdff67102007-12-01 07:51:45 +0000308 if (AA.doesNotAccessMemory(CS))
309 return true; // doesn't alias anything
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000310
Chris Lattner9971ac42003-02-24 20:37:56 +0000311 AliasSet *AS = findAliasSetForCallSite(CS);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000312 if (AS) {
Chris Lattner12c11552004-07-21 05:18:04 +0000313 AS->addCallSite(CS, AA);
314 return false;
Chris Lattner9971ac42003-02-24 20:37:56 +0000315 }
Chris Lattner6e1f5102010-08-29 04:06:55 +0000316 AliasSets.push_back(new AliasSet());
317 AS = &AliasSets.back();
318 AS->addCallSite(CS, AA);
319 return true;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000320}
321
Chris Lattner12c11552004-07-21 05:18:04 +0000322bool AliasSetTracker::add(Instruction *I) {
Chris Lattner6e1f5102010-08-29 04:06:55 +0000323 // Dispatch to one of the other add methods.
Chris Lattner9971ac42003-02-24 20:37:56 +0000324 if (LoadInst *LI = dyn_cast<LoadInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000325 return add(LI);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000326 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000327 return add(SI);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000328 if (CallInst *CI = dyn_cast<CallInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000329 return add(CI);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000330 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000331 return add(II);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000332 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
Dan Gohman235fc572008-04-14 18:34:50 +0000333 return add(VAAI);
Chris Lattner12c11552004-07-21 05:18:04 +0000334 return true;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000335}
336
Chris Lattner319d05b2003-03-03 23:28:05 +0000337void AliasSetTracker::add(BasicBlock &BB) {
338 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
339 add(I);
340}
341
342void AliasSetTracker::add(const AliasSetTracker &AST) {
343 assert(&AA == &AST.AA &&
344 "Merging AliasSetTracker objects with different Alias Analyses!");
345
346 // Loop over all of the alias sets in AST, adding the pointers contained
347 // therein into the current alias sets. This can cause alias sets to be
348 // merged together in the current AST.
Chris Lattner6e1f5102010-08-29 04:06:55 +0000349 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I) {
350 if (I->Forward) continue; // Ignore forwarding alias sets
351
352 AliasSet &AS = const_cast<AliasSet&>(*I);
Chris Lattner319d05b2003-03-03 23:28:05 +0000353
Chris Lattner6e1f5102010-08-29 04:06:55 +0000354 // If there are any call sites in the alias set, add them to this AST.
355 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
356 add(AS.CallSites[i]);
Chris Lattner319d05b2003-03-03 23:28:05 +0000357
Chris Lattner6e1f5102010-08-29 04:06:55 +0000358 // Loop over all of the pointers in this alias set.
359 bool X;
360 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) {
361 AliasSet &NewAS = addPointer(ASI.getPointer(), ASI.getSize(),
362 (AliasSet::AccessType)AS.AccessTy, X);
363 if (AS.isVolatile()) NewAS.setVolatile();
Chris Lattner319d05b2003-03-03 23:28:05 +0000364 }
Chris Lattner6e1f5102010-08-29 04:06:55 +0000365 }
Chris Lattner319d05b2003-03-03 23:28:05 +0000366}
367
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000368/// remove - Remove the specified (potentially non-empty) alias set from the
369/// tracker.
370void AliasSetTracker::remove(AliasSet &AS) {
Chris Lattnerf2998572006-06-27 23:48:59 +0000371 // Drop all call sites.
372 AS.CallSites.clear();
373
374 // Clear the alias set.
375 unsigned NumRefs = 0;
376 while (!AS.empty()) {
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000377 AliasSet::PointerRec *P = AS.PtrList;
378
379 Value *ValToRemove = P->getValue();
Chris Lattnerf2998572006-06-27 23:48:59 +0000380
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000381 // Unlink and delete entry from the list of values.
382 P->eraseFromList();
Chris Lattnerf2998572006-06-27 23:48:59 +0000383
384 // Remember how many references need to be dropped.
385 ++NumRefs;
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000386
Chris Lattnerf2998572006-06-27 23:48:59 +0000387 // Finally, remove the entry.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000388 PointerMap.erase(ValToRemove);
Chris Lattnerf2998572006-06-27 23:48:59 +0000389 }
390
391 // Stop using the alias set, removing it.
Chris Lattner356d8c22006-06-27 23:56:13 +0000392 AS.RefCount -= NumRefs;
393 if (AS.RefCount == 0)
394 AS.removeFromTracker(*this);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000395}
396
Chris Lattner61d46272004-07-26 05:50:23 +0000397bool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
398 AliasSet *AS = findAliasSetForPointer(Ptr, Size);
399 if (!AS) return false;
400 remove(*AS);
401 return true;
402}
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000403
404bool AliasSetTracker::remove(LoadInst *LI) {
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000405 unsigned Size = AA.getTypeStoreSize(LI->getType());
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000406 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
407 if (!AS) return false;
408 remove(*AS);
409 return true;
410}
411
412bool AliasSetTracker::remove(StoreInst *SI) {
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000413 unsigned Size = AA.getTypeStoreSize(SI->getOperand(0)->getType());
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000414 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
415 if (!AS) return false;
416 remove(*AS);
417 return true;
418}
419
Dan Gohman235fc572008-04-14 18:34:50 +0000420bool AliasSetTracker::remove(VAArgInst *VAAI) {
421 AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0), ~0);
422 if (!AS) return false;
423 remove(*AS);
424 return true;
425}
426
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000427bool AliasSetTracker::remove(CallSite CS) {
Duncan Sandsdff67102007-12-01 07:51:45 +0000428 if (AA.doesNotAccessMemory(CS))
429 return false; // doesn't alias anything
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000430
431 AliasSet *AS = findAliasSetForCallSite(CS);
432 if (!AS) return false;
433 remove(*AS);
434 return true;
435}
436
437bool AliasSetTracker::remove(Instruction *I) {
438 // Dispatch to one of the other remove methods...
439 if (LoadInst *LI = dyn_cast<LoadInst>(I))
440 return remove(LI);
Chris Lattner9476d742010-08-29 04:13:43 +0000441 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000442 return remove(SI);
Chris Lattner9476d742010-08-29 04:13:43 +0000443 if (CallInst *CI = dyn_cast<CallInst>(I))
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000444 return remove(CI);
Chris Lattner9476d742010-08-29 04:13:43 +0000445 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
Dan Gohman235fc572008-04-14 18:34:50 +0000446 return remove(VAAI);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000447 return true;
448}
449
Chris Lattner2cffeec2003-12-18 08:11:56 +0000450
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000451// deleteValue method - This method is used to remove a pointer value from the
Chris Lattner2cffeec2003-12-18 08:11:56 +0000452// AliasSetTracker entirely. It should be used when an instruction is deleted
453// from the program to update the AST. If you don't use this, you would have
454// dangling pointers to deleted instructions.
455//
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000456void AliasSetTracker::deleteValue(Value *PtrVal) {
Chris Lattnerb66e6482004-09-14 19:15:32 +0000457 // Notify the alias analysis implementation that this value is gone.
458 AA.deleteValue(PtrVal);
459
Chris Lattner959e3212006-06-26 19:20:48 +0000460 // If this is a call instruction, remove the callsite from the appropriate
Chris Lattnercb7f6532010-08-29 18:42:23 +0000461 // AliasSet (if present).
462 if (CallSite CS = PtrVal) {
463 if (!AA.doesNotAccessMemory(CS)) {
464 // Scan all the alias sets to see if this call site is contained.
465 for (iterator I = begin(), E = end(); I != E; ++I) {
466 if (I->Forward) continue;
467
468 I->removeCallSite(CS);
469 }
470 }
471 }
Chris Lattner959e3212006-06-26 19:20:48 +0000472
Chris Lattnerb66e6482004-09-14 19:15:32 +0000473 // First, look up the PointerRec for this pointer.
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000474 PointerMapType::iterator I = PointerMap.find(PtrVal);
Chris Lattner2cffeec2003-12-18 08:11:56 +0000475 if (I == PointerMap.end()) return; // Noop
476
477 // If we found one, remove the pointer from the alias set it is in.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000478 AliasSet::PointerRec *PtrValEnt = I->second;
479 AliasSet *AS = PtrValEnt->getAliasSet(*this);
Chris Lattner2cffeec2003-12-18 08:11:56 +0000480
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000481 // Unlink and delete from the list of values.
482 PtrValEnt->eraseFromList();
483
484 // Stop using the alias set.
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000485 AS->dropRef(*this);
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000486
Chris Lattner2cffeec2003-12-18 08:11:56 +0000487 PointerMap.erase(I);
488}
489
Chris Lattnerb66e6482004-09-14 19:15:32 +0000490// copyValue - This method should be used whenever a preexisting value in the
491// program is copied or cloned, introducing a new value. Note that it is ok for
492// clients that use this method to introduce the same value multiple times: if
493// the tracker already knows about a value, it will ignore the request.
494//
495void AliasSetTracker::copyValue(Value *From, Value *To) {
496 // Notify the alias analysis implementation that this value is copied.
497 AA.copyValue(From, To);
498
499 // First, look up the PointerRec for this pointer.
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000500 PointerMapType::iterator I = PointerMap.find(From);
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000501 if (I == PointerMap.end())
Chris Lattnerb66e6482004-09-14 19:15:32 +0000502 return; // Noop
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000503 assert(I->second->hasAliasSet() && "Dead entry?");
Chris Lattnerb66e6482004-09-14 19:15:32 +0000504
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000505 AliasSet::PointerRec &Entry = getEntryFor(To);
506 if (Entry.hasAliasSet()) return; // Already in the tracker!
Chris Lattnerb66e6482004-09-14 19:15:32 +0000507
508 // Add it to the alias set it aliases...
Devang Patel6d9a2df2009-03-30 18:34:47 +0000509 I = PointerMap.find(From);
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000510 AliasSet *AS = I->second->getAliasSet(*this);
511 AS->addPointer(*this, Entry, I->second->getSize(), true);
Chris Lattnerb66e6482004-09-14 19:15:32 +0000512}
513
514
Chris Lattner2cffeec2003-12-18 08:11:56 +0000515
Chris Lattner9971ac42003-02-24 20:37:56 +0000516//===----------------------------------------------------------------------===//
517// AliasSet/AliasSetTracker Printing Support
518//===----------------------------------------------------------------------===//
519
Chris Lattner791102f2009-08-23 05:17:37 +0000520void AliasSet::print(raw_ostream &OS) const {
521 OS << " AliasSet[" << format("0x%p", (void*)this) << "," << RefCount << "] ";
Dan Gohman92c7b652008-04-21 19:48:48 +0000522 OS << (AliasTy == MustAlias ? "must" : "may") << " alias, ";
Chris Lattner9971ac42003-02-24 20:37:56 +0000523 switch (AccessTy) {
524 case NoModRef: OS << "No access "; break;
525 case Refs : OS << "Ref "; break;
526 case Mods : OS << "Mod "; break;
527 case ModRef : OS << "Mod/Ref "; break;
Torok Edwinc23197a2009-07-14 16:55:14 +0000528 default: llvm_unreachable("Bad value for AccessTy!");
Chris Lattner009cc3d2002-09-26 21:49:07 +0000529 }
Chris Lattnere2609242003-12-14 04:52:11 +0000530 if (isVolatile()) OS << "[volatile] ";
Chris Lattner9971ac42003-02-24 20:37:56 +0000531 if (Forward)
532 OS << " forwarding to " << (void*)Forward;
533
534
Dan Gohmancb406c22007-10-03 19:26:29 +0000535 if (!empty()) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000536 OS << "Pointers: ";
537 for (iterator I = begin(), E = end(); I != E; ++I) {
538 if (I != begin()) OS << ", ";
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000539 WriteAsOperand(OS << "(", I.getPointer());
540 OS << ", " << I.getSize() << ")";
Chris Lattner9971ac42003-02-24 20:37:56 +0000541 }
542 }
543 if (!CallSites.empty()) {
544 OS << "\n " << CallSites.size() << " Call Sites: ";
545 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
546 if (i) OS << ", ";
Chris Lattnercb7f6532010-08-29 18:42:23 +0000547 WriteAsOperand(OS, CallSites[i]);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000548 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000549 }
550 OS << "\n";
551}
552
Chris Lattner791102f2009-08-23 05:17:37 +0000553void AliasSetTracker::print(raw_ostream &OS) const {
Chris Lattner9971ac42003-02-24 20:37:56 +0000554 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
555 << PointerMap.size() << " pointer values.\n";
556 for (const_iterator I = begin(), E = end(); I != E; ++I)
557 I->print(OS);
558 OS << "\n";
559}
560
David Greene43c48ac2009-12-23 19:27:59 +0000561void AliasSet::dump() const { print(dbgs()); }
562void AliasSetTracker::dump() const { print(dbgs()); }
Chris Lattner9971ac42003-02-24 20:37:56 +0000563
Chris Lattner9971ac42003-02-24 20:37:56 +0000564//===----------------------------------------------------------------------===//
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000565// ASTCallbackVH Class Implementation
566//===----------------------------------------------------------------------===//
567
568void AliasSetTracker::ASTCallbackVH::deleted() {
569 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
570 AST->deleteValue(getValPtr());
571 // this now dangles!
572}
573
574AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
Dan Gohmana818c302009-07-31 18:21:48 +0000575 : CallbackVH(V), AST(ast) {}
576
577AliasSetTracker::ASTCallbackVH &
578AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
579 return *this = ASTCallbackVH(V, AST);
580}
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000581
582//===----------------------------------------------------------------------===//
Chris Lattner9971ac42003-02-24 20:37:56 +0000583// AliasSetPrinter Pass
584//===----------------------------------------------------------------------===//
585
586namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000587 class AliasSetPrinter : public FunctionPass {
Chris Lattner9971ac42003-02-24 20:37:56 +0000588 AliasSetTracker *Tracker;
589 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +0000590 static char ID; // Pass identification, replacement for typeid
Owen Anderson90c579d2010-08-06 18:33:48 +0000591 AliasSetPrinter() : FunctionPass(ID) {}
Devang Patel794fd752007-05-01 21:15:47 +0000592
Chris Lattner9971ac42003-02-24 20:37:56 +0000593 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
594 AU.setPreservesAll();
595 AU.addRequired<AliasAnalysis>();
596 }
597
598 virtual bool runOnFunction(Function &F) {
599 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
600
601 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner6ffe5512004-04-27 15:13:33 +0000602 Tracker->add(&*I);
David Greene4850a892009-12-23 22:49:57 +0000603 Tracker->print(errs());
Chris Lattner4983cf72006-01-03 06:05:22 +0000604 delete Tracker;
Chris Lattner9971ac42003-02-24 20:37:56 +0000605 return false;
606 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000607 };
Chris Lattner009cc3d2002-09-26 21:49:07 +0000608}
Dan Gohman844731a2008-05-13 00:00:25 +0000609
610char AliasSetPrinter::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +0000611INITIALIZE_PASS(AliasSetPrinter, "print-alias-sets",
612 "Alias Set Printer", false, true);