blob: 3a46976d66f7ac0f0f027aea4d4fcc108c81fba7 [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"
Dan Gohmana8702ea2010-10-18 20:44:50 +000018#include "llvm/LLVMContext.h"
Chris Lattner9971ac42003-02-24 20:37:56 +000019#include "llvm/Pass.h"
Chris Lattner5b3a4552005-03-17 15:38:16 +000020#include "llvm/Type.h"
Chris Lattner31a9d182003-02-26 22:11:00 +000021#include "llvm/Target/TargetData.h"
Chris Lattner9971ac42003-02-24 20:37:56 +000022#include "llvm/Assembly/Writer.h"
David Greene43c48ac2009-12-23 19:27:59 +000023#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000024#include "llvm/Support/ErrorHandling.h"
Chris Lattner9971ac42003-02-24 20:37:56 +000025#include "llvm/Support/InstIterator.h"
Chris Lattner791102f2009-08-23 05:17:37 +000026#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.
Dan Gohman888cbda2010-11-11 21:27:26 +000049 if (AA.alias(AliasAnalysis::Location(L->getValue(),
50 L->getSize(),
51 L->getTBAAInfo()),
52 AliasAnalysis::Location(R->getValue(),
53 R->getSize(),
54 R->getTBAAInfo()))
Chris Lattnercc8d5242004-11-27 18:37:42 +000055 != AliasAnalysis::MustAlias)
56 AliasTy = MayAlias;
57 }
58
Chris Lattner9971ac42003-02-24 20:37:56 +000059 if (CallSites.empty()) { // Merge call sites...
60 if (!AS.CallSites.empty())
61 std::swap(CallSites, AS.CallSites);
62 } else if (!AS.CallSites.empty()) {
63 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
64 AS.CallSites.clear();
65 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000066
Chris Lattner9971ac42003-02-24 20:37:56 +000067 AS.Forward = this; // Forward across AS now...
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000068 addRef(); // AS is now pointing to us...
Chris Lattner9971ac42003-02-24 20:37:56 +000069
70 // Merge the list of constituent pointers...
Chris Lattner2cffeec2003-12-18 08:11:56 +000071 if (AS.PtrList) {
72 *PtrListEnd = AS.PtrList;
Chris Lattnerd7168dd2009-03-09 05:11:09 +000073 AS.PtrList->setPrevInList(PtrListEnd);
Chris Lattner2cffeec2003-12-18 08:11:56 +000074 PtrListEnd = AS.PtrListEnd;
75
76 AS.PtrList = 0;
77 AS.PtrListEnd = &AS.PtrList;
Chris Lattner3080b602004-09-15 16:59:47 +000078 assert(*AS.PtrListEnd == 0 && "End of list is not null?");
Chris Lattner2cffeec2003-12-18 08:11:56 +000079 }
Chris Lattner009cc3d2002-09-26 21:49:07 +000080}
81
Chris Lattner9971ac42003-02-24 20:37:56 +000082void AliasSetTracker::removeAliasSet(AliasSet *AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000083 if (AliasSet *Fwd = AS->Forward) {
84 Fwd->dropRef(*this);
85 AS->Forward = 0;
86 }
Chris Lattner9971ac42003-02-24 20:37:56 +000087 AliasSets.erase(AS);
88}
89
90void AliasSet::removeFromTracker(AliasSetTracker &AST) {
91 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
92 AST.removeAliasSet(this);
93}
94
Chris Lattnerd7168dd2009-03-09 05:11:09 +000095void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
Dan Gohman3da848b2010-10-19 22:54:46 +000096 uint64_t Size, const MDNode *TBAAInfo,
Dan Gohmana8702ea2010-10-18 20:44:50 +000097 bool KnownMustAlias) {
Chris Lattnerd7168dd2009-03-09 05:11:09 +000098 assert(!Entry.hasAliasSet() && "Entry already in set!");
Chris Lattner9971ac42003-02-24 20:37:56 +000099
Chris Lattnerb66e6482004-09-14 19:15:32 +0000100 // Check to see if we have to downgrade to _may_ alias.
101 if (isMustAlias() && !KnownMustAlias)
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000102 if (PointerRec *P = getSomePointer()) {
Chris Lattner3080b602004-09-15 16:59:47 +0000103 AliasAnalysis &AA = AST.getAliasAnalysis();
Chris Lattner31a9d182003-02-26 22:11:00 +0000104 AliasAnalysis::AliasResult Result =
Dan Gohmana8702ea2010-10-18 20:44:50 +0000105 AA.alias(AliasAnalysis::Location(P->getValue(), P->getSize(),
106 P->getTBAAInfo()),
107 AliasAnalysis::Location(Entry.getValue(), Size, TBAAInfo));
Dan Gohman2c2f4c82010-12-10 19:40:47 +0000108 if (Result != AliasAnalysis::MustAlias)
Chris Lattner9971ac42003-02-24 20:37:56 +0000109 AliasTy = MayAlias;
Chris Lattner31a9d182003-02-26 22:11:00 +0000110 else // First entry of must alias must have maximum size!
Dan Gohmana8702ea2010-10-18 20:44:50 +0000111 P->updateSizeAndTBAAInfo(Size, TBAAInfo);
Chris Lattner31a9d182003-02-26 22:11:00 +0000112 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
113 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000114
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000115 Entry.setAliasSet(this);
Dan Gohmana8702ea2010-10-18 20:44:50 +0000116 Entry.updateSizeAndTBAAInfo(Size, TBAAInfo);
Chris Lattner9971ac42003-02-24 20:37:56 +0000117
118 // Add it to the end of the list...
Chris Lattner2cffeec2003-12-18 08:11:56 +0000119 assert(*PtrListEnd == 0 && "End of list is not null?");
120 *PtrListEnd = &Entry;
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000121 PtrListEnd = Entry.setPrevInList(PtrListEnd);
Chris Lattner3080b602004-09-15 16:59:47 +0000122 assert(*PtrListEnd == 0 && "End of list is not null?");
Chris Lattner9476d742010-08-29 04:13:43 +0000123 addRef(); // Entry points to alias set.
Chris Lattner9971ac42003-02-24 20:37:56 +0000124}
125
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000126void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
Chris Lattnercb7f6532010-08-29 18:42:23 +0000127 CallSites.push_back(CS.getInstruction());
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000128
Duncan Sandsdff67102007-12-01 07:51:45 +0000129 AliasAnalysis::ModRefBehavior Behavior = AA.getModRefBehavior(CS);
130 if (Behavior == AliasAnalysis::DoesNotAccessMemory)
131 return;
Dan Gohmancd93f3b2010-11-09 19:58:21 +0000132 if (AliasAnalysis::onlyReadsMemory(Behavior)) {
Duncan Sandsdff67102007-12-01 07:51:45 +0000133 AliasTy = MayAlias;
134 AccessTy |= Refs;
135 return;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000136 }
137
138 // FIXME: This should use mod/ref information to make this not suck so bad
139 AliasTy = MayAlias;
Chris Lattner577385e2003-05-03 03:42:08 +0000140 AccessTy = ModRef;
Chris Lattner9971ac42003-02-24 20:37:56 +0000141}
142
143/// aliasesPointer - Return true if the specified pointer "may" (or must)
Chris Lattner009cc3d2002-09-26 21:49:07 +0000144/// alias one of the members in the set.
145///
Dan Gohman3da848b2010-10-19 22:54:46 +0000146bool AliasSet::aliasesPointer(const Value *Ptr, uint64_t Size,
Dan Gohmana8702ea2010-10-18 20:44:50 +0000147 const MDNode *TBAAInfo,
Chris Lattner31a9d182003-02-26 22:11:00 +0000148 AliasAnalysis &AA) const {
Chris Lattner9971ac42003-02-24 20:37:56 +0000149 if (AliasTy == MustAlias) {
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000150 assert(CallSites.empty() && "Illegal must alias set!");
151
Chris Lattner9971ac42003-02-24 20:37:56 +0000152 // If this is a set of MustAliases, only check to see if the pointer aliases
Chris Lattner9476d742010-08-29 04:13:43 +0000153 // SOME value in the set.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000154 PointerRec *SomePtr = getSomePointer();
Chris Lattner9971ac42003-02-24 20:37:56 +0000155 assert(SomePtr && "Empty must-alias set??");
Dan Gohmana8702ea2010-10-18 20:44:50 +0000156 return AA.alias(AliasAnalysis::Location(SomePtr->getValue(),
157 SomePtr->getSize(),
158 SomePtr->getTBAAInfo()),
159 AliasAnalysis::Location(Ptr, Size, TBAAInfo));
Chris Lattner9971ac42003-02-24 20:37:56 +0000160 }
161
162 // If this is a may-alias set, we have to check all of the pointers in the set
163 // to be sure it doesn't alias the set...
164 for (iterator I = begin(), E = end(); I != E; ++I)
Dan Gohmana8702ea2010-10-18 20:44:50 +0000165 if (AA.alias(AliasAnalysis::Location(Ptr, Size, TBAAInfo),
Dan Gohmanfb8096d2010-10-18 21:28:00 +0000166 AliasAnalysis::Location(I.getPointer(), I.getSize(),
167 I.getTBAAInfo())))
Chris Lattner9971ac42003-02-24 20:37:56 +0000168 return true;
169
170 // Check the call sites list and invoke list...
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000171 if (!CallSites.empty()) {
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000172 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
Dan Gohmana8702ea2010-10-18 20:44:50 +0000173 if (AA.getModRefInfo(CallSites[i],
174 AliasAnalysis::Location(Ptr, Size, TBAAInfo)) !=
175 AliasAnalysis::NoModRef)
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000176 return true;
177 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000178
Chris Lattner009cc3d2002-09-26 21:49:07 +0000179 return false;
180}
181
Chris Lattner9971ac42003-02-24 20:37:56 +0000182bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
Duncan Sandsdff67102007-12-01 07:51:45 +0000183 if (AA.doesNotAccessMemory(CS))
184 return false;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000185
Chris Lattnercb7f6532010-08-29 18:42:23 +0000186 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
187 if (AA.getModRefInfo(getCallSite(i), CS) != AliasAnalysis::NoModRef ||
188 AA.getModRefInfo(CS, getCallSite(i)) != AliasAnalysis::NoModRef)
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000189 return true;
Chris Lattnercb7f6532010-08-29 18:42:23 +0000190 }
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000191
192 for (iterator I = begin(), E = end(); I != E; ++I)
193 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
194 AliasAnalysis::NoModRef)
195 return true;
196
197 return false;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000198}
199
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000200void AliasSetTracker::clear() {
201 // Delete all the PointerRec entries.
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000202 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
203 I != E; ++I)
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000204 I->second->eraseFromList();
205
206 PointerMap.clear();
207
208 // The alias sets should all be clear now.
209 AliasSets.clear();
210}
211
Chris Lattner009cc3d2002-09-26 21:49:07 +0000212
Chris Lattner009cc3d2002-09-26 21:49:07 +0000213/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
214/// instruction referring to the pointer into. If there are multiple alias sets
215/// that may alias the pointer, merge them together and return the unified set.
216///
Chris Lattner31a9d182003-02-26 22:11:00 +0000217AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
Dan Gohman3da848b2010-10-19 22:54:46 +0000218 uint64_t Size,
Dan Gohmana8702ea2010-10-18 20:44:50 +0000219 const MDNode *TBAAInfo) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000220 AliasSet *FoundSet = 0;
Chris Lattner6e1f5102010-08-29 04:06:55 +0000221 for (iterator I = begin(), E = end(); I != E; ++I) {
Dan Gohmana8702ea2010-10-18 20:44:50 +0000222 if (I->Forward || !I->aliasesPointer(Ptr, Size, TBAAInfo, AA)) continue;
Chris Lattner6e1f5102010-08-29 04:06:55 +0000223
224 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
225 FoundSet = I; // Remember it.
226 } else { // Otherwise, we must merge the sets.
227 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
Chris Lattner009cc3d2002-09-26 21:49:07 +0000228 }
Chris Lattner6e1f5102010-08-29 04:06:55 +0000229 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000230
231 return FoundSet;
232}
233
Chris Lattner07bfa522004-11-26 21:36:25 +0000234/// containsPointer - Return true if the specified location is represented by
235/// this alias set, false otherwise. This does not modify the AST object or
236/// alias sets.
Dan Gohman3da848b2010-10-19 22:54:46 +0000237bool AliasSetTracker::containsPointer(Value *Ptr, uint64_t Size,
Dan Gohmana8702ea2010-10-18 20:44:50 +0000238 const MDNode *TBAAInfo) const {
Chris Lattner07bfa522004-11-26 21:36:25 +0000239 for (const_iterator I = begin(), E = end(); I != E; ++I)
Dan Gohmana8702ea2010-10-18 20:44:50 +0000240 if (!I->Forward && I->aliasesPointer(Ptr, Size, TBAAInfo, AA))
Chris Lattner07bfa522004-11-26 21:36:25 +0000241 return true;
242 return false;
243}
244
245
246
Chris Lattner9971ac42003-02-24 20:37:56 +0000247AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
248 AliasSet *FoundSet = 0;
Chris Lattner6e1f5102010-08-29 04:06:55 +0000249 for (iterator I = begin(), E = end(); I != E; ++I) {
250 if (I->Forward || !I->aliasesCallSite(CS, AA))
251 continue;
252
Chris Lattnercb7f6532010-08-29 18:42:23 +0000253 if (FoundSet == 0) // If this is the first alias set ptr can go into.
254 FoundSet = I; // Remember it.
255 else if (!I->Forward) // Otherwise, we must merge the sets.
Chris Lattner6e1f5102010-08-29 04:06:55 +0000256 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
Chris Lattner6e1f5102010-08-29 04:06:55 +0000257 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000258 return FoundSet;
259}
260
261
Chris Lattner009cc3d2002-09-26 21:49:07 +0000262
Chris Lattner9971ac42003-02-24 20:37:56 +0000263
264/// getAliasSetForPointer - Return the alias set that the specified pointer
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000265/// lives in.
Dan Gohman3da848b2010-10-19 22:54:46 +0000266AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, uint64_t Size,
Dan Gohmana8702ea2010-10-18 20:44:50 +0000267 const MDNode *TBAAInfo,
Chris Lattner12c11552004-07-21 05:18:04 +0000268 bool *New) {
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000269 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
Chris Lattner9971ac42003-02-24 20:37:56 +0000270
Chris Lattner9476d742010-08-29 04:13:43 +0000271 // Check to see if the pointer is already known.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000272 if (Entry.hasAliasSet()) {
Dan Gohmana8702ea2010-10-18 20:44:50 +0000273 Entry.updateSizeAndTBAAInfo(Size, TBAAInfo);
Chris Lattner9971ac42003-02-24 20:37:56 +0000274 // Return the set!
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000275 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000276 }
277
Dan Gohmana8702ea2010-10-18 20:44:50 +0000278 if (AliasSet *AS = findAliasSetForPointer(Pointer, Size, TBAAInfo)) {
Chris Lattner9476d742010-08-29 04:13:43 +0000279 // Add it to the alias set it aliases.
Dan Gohmana8702ea2010-10-18 20:44:50 +0000280 AS->addPointer(*this, Entry, Size, TBAAInfo);
Chris Lattner9971ac42003-02-24 20:37:56 +0000281 return *AS;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000282 }
Chris Lattner6e1f5102010-08-29 04:06:55 +0000283
284 if (New) *New = true;
Chris Lattner9476d742010-08-29 04:13:43 +0000285 // Otherwise create a new alias set to hold the loaded pointer.
Chris Lattner6e1f5102010-08-29 04:06:55 +0000286 AliasSets.push_back(new AliasSet());
Dan Gohmana8702ea2010-10-18 20:44:50 +0000287 AliasSets.back().addPointer(*this, Entry, Size, TBAAInfo);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000288 return AliasSets.back();
Chris Lattner009cc3d2002-09-26 21:49:07 +0000289}
290
Dan Gohman3da848b2010-10-19 22:54:46 +0000291bool AliasSetTracker::add(Value *Ptr, uint64_t Size, const MDNode *TBAAInfo) {
Chris Lattner61d46272004-07-26 05:50:23 +0000292 bool NewPtr;
Dan Gohmana8702ea2010-10-18 20:44:50 +0000293 addPointer(Ptr, Size, TBAAInfo, AliasSet::NoModRef, NewPtr);
Chris Lattner61d46272004-07-26 05:50:23 +0000294 return NewPtr;
295}
296
297
Chris Lattner12c11552004-07-21 05:18:04 +0000298bool AliasSetTracker::add(LoadInst *LI) {
299 bool NewPtr;
300 AliasSet &AS = addPointer(LI->getOperand(0),
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000301 AA.getTypeStoreSize(LI->getType()),
Dan Gohmana8702ea2010-10-18 20:44:50 +0000302 LI->getMetadata(LLVMContext::MD_tbaa),
Chris Lattner12c11552004-07-21 05:18:04 +0000303 AliasSet::Refs, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000304 if (LI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000305 return NewPtr;
Chris Lattner9971ac42003-02-24 20:37:56 +0000306}
307
Chris Lattner12c11552004-07-21 05:18:04 +0000308bool AliasSetTracker::add(StoreInst *SI) {
309 bool NewPtr;
310 Value *Val = SI->getOperand(0);
311 AliasSet &AS = addPointer(SI->getOperand(1),
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000312 AA.getTypeStoreSize(Val->getType()),
Dan Gohmana8702ea2010-10-18 20:44:50 +0000313 SI->getMetadata(LLVMContext::MD_tbaa),
Chris Lattner12c11552004-07-21 05:18:04 +0000314 AliasSet::Mods, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000315 if (SI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000316 return NewPtr;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000317}
318
Dan Gohman235fc572008-04-14 18:34:50 +0000319bool AliasSetTracker::add(VAArgInst *VAAI) {
320 bool NewPtr;
Dan Gohmana8702ea2010-10-18 20:44:50 +0000321 addPointer(VAAI->getOperand(0), AliasAnalysis::UnknownSize,
322 VAAI->getMetadata(LLVMContext::MD_tbaa),
323 AliasSet::ModRef, NewPtr);
Dan Gohman235fc572008-04-14 18:34:50 +0000324 return NewPtr;
325}
326
Chris Lattnere2609242003-12-14 04:52:11 +0000327
Chris Lattner12c11552004-07-21 05:18:04 +0000328bool AliasSetTracker::add(CallSite CS) {
Zhou Sheng7e4286e2009-03-03 06:02:04 +0000329 if (isa<DbgInfoIntrinsic>(CS.getInstruction()))
330 return true; // Ignore DbgInfo Intrinsics.
Duncan Sandsdff67102007-12-01 07:51:45 +0000331 if (AA.doesNotAccessMemory(CS))
332 return true; // doesn't alias anything
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000333
Chris Lattner9971ac42003-02-24 20:37:56 +0000334 AliasSet *AS = findAliasSetForCallSite(CS);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000335 if (AS) {
Chris Lattner12c11552004-07-21 05:18:04 +0000336 AS->addCallSite(CS, AA);
337 return false;
Chris Lattner9971ac42003-02-24 20:37:56 +0000338 }
Chris Lattner6e1f5102010-08-29 04:06:55 +0000339 AliasSets.push_back(new AliasSet());
340 AS = &AliasSets.back();
341 AS->addCallSite(CS, AA);
342 return true;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000343}
344
Chris Lattner12c11552004-07-21 05:18:04 +0000345bool AliasSetTracker::add(Instruction *I) {
Chris Lattner6e1f5102010-08-29 04:06:55 +0000346 // Dispatch to one of the other add methods.
Chris Lattner9971ac42003-02-24 20:37:56 +0000347 if (LoadInst *LI = dyn_cast<LoadInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000348 return add(LI);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000349 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000350 return add(SI);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000351 if (CallInst *CI = dyn_cast<CallInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000352 return add(CI);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000353 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000354 return add(II);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000355 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
Dan Gohman235fc572008-04-14 18:34:50 +0000356 return add(VAAI);
Chris Lattner12c11552004-07-21 05:18:04 +0000357 return true;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000358}
359
Chris Lattner319d05b2003-03-03 23:28:05 +0000360void AliasSetTracker::add(BasicBlock &BB) {
361 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
362 add(I);
363}
364
365void AliasSetTracker::add(const AliasSetTracker &AST) {
366 assert(&AA == &AST.AA &&
367 "Merging AliasSetTracker objects with different Alias Analyses!");
368
369 // Loop over all of the alias sets in AST, adding the pointers contained
370 // therein into the current alias sets. This can cause alias sets to be
371 // merged together in the current AST.
Chris Lattner6e1f5102010-08-29 04:06:55 +0000372 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I) {
373 if (I->Forward) continue; // Ignore forwarding alias sets
374
375 AliasSet &AS = const_cast<AliasSet&>(*I);
Chris Lattner319d05b2003-03-03 23:28:05 +0000376
Chris Lattner6e1f5102010-08-29 04:06:55 +0000377 // If there are any call sites in the alias set, add them to this AST.
378 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
379 add(AS.CallSites[i]);
Chris Lattner319d05b2003-03-03 23:28:05 +0000380
Chris Lattner6e1f5102010-08-29 04:06:55 +0000381 // Loop over all of the pointers in this alias set.
382 bool X;
383 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) {
384 AliasSet &NewAS = addPointer(ASI.getPointer(), ASI.getSize(),
Dan Gohmaneee54002010-10-18 23:31:47 +0000385 ASI.getTBAAInfo(),
Chris Lattner6e1f5102010-08-29 04:06:55 +0000386 (AliasSet::AccessType)AS.AccessTy, X);
387 if (AS.isVolatile()) NewAS.setVolatile();
Chris Lattner319d05b2003-03-03 23:28:05 +0000388 }
Chris Lattner6e1f5102010-08-29 04:06:55 +0000389 }
Chris Lattner319d05b2003-03-03 23:28:05 +0000390}
391
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000392/// remove - Remove the specified (potentially non-empty) alias set from the
393/// tracker.
394void AliasSetTracker::remove(AliasSet &AS) {
Chris Lattnerf2998572006-06-27 23:48:59 +0000395 // Drop all call sites.
396 AS.CallSites.clear();
397
398 // Clear the alias set.
399 unsigned NumRefs = 0;
400 while (!AS.empty()) {
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000401 AliasSet::PointerRec *P = AS.PtrList;
402
403 Value *ValToRemove = P->getValue();
Chris Lattnerf2998572006-06-27 23:48:59 +0000404
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000405 // Unlink and delete entry from the list of values.
406 P->eraseFromList();
Chris Lattnerf2998572006-06-27 23:48:59 +0000407
408 // Remember how many references need to be dropped.
409 ++NumRefs;
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000410
Chris Lattnerf2998572006-06-27 23:48:59 +0000411 // Finally, remove the entry.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000412 PointerMap.erase(ValToRemove);
Chris Lattnerf2998572006-06-27 23:48:59 +0000413 }
414
415 // Stop using the alias set, removing it.
Chris Lattner356d8c22006-06-27 23:56:13 +0000416 AS.RefCount -= NumRefs;
417 if (AS.RefCount == 0)
418 AS.removeFromTracker(*this);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000419}
420
Dan Gohmana8702ea2010-10-18 20:44:50 +0000421bool
Dan Gohman3da848b2010-10-19 22:54:46 +0000422AliasSetTracker::remove(Value *Ptr, uint64_t Size, const MDNode *TBAAInfo) {
Dan Gohmana8702ea2010-10-18 20:44:50 +0000423 AliasSet *AS = findAliasSetForPointer(Ptr, Size, TBAAInfo);
Chris Lattner61d46272004-07-26 05:50:23 +0000424 if (!AS) return false;
425 remove(*AS);
426 return true;
427}
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000428
429bool AliasSetTracker::remove(LoadInst *LI) {
Dan Gohman3da848b2010-10-19 22:54:46 +0000430 uint64_t Size = AA.getTypeStoreSize(LI->getType());
Dan Gohmana8702ea2010-10-18 20:44:50 +0000431 const MDNode *TBAAInfo = LI->getMetadata(LLVMContext::MD_tbaa);
432 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size, TBAAInfo);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000433 if (!AS) return false;
434 remove(*AS);
435 return true;
436}
437
438bool AliasSetTracker::remove(StoreInst *SI) {
Dan Gohman3da848b2010-10-19 22:54:46 +0000439 uint64_t Size = AA.getTypeStoreSize(SI->getOperand(0)->getType());
Dan Gohmana8702ea2010-10-18 20:44:50 +0000440 const MDNode *TBAAInfo = SI->getMetadata(LLVMContext::MD_tbaa);
441 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size, TBAAInfo);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000442 if (!AS) return false;
443 remove(*AS);
444 return true;
445}
446
Dan Gohman235fc572008-04-14 18:34:50 +0000447bool AliasSetTracker::remove(VAArgInst *VAAI) {
Dan Gohmana8702ea2010-10-18 20:44:50 +0000448 AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0),
449 AliasAnalysis::UnknownSize,
450 VAAI->getMetadata(LLVMContext::MD_tbaa));
Dan Gohman235fc572008-04-14 18:34:50 +0000451 if (!AS) return false;
452 remove(*AS);
453 return true;
454}
455
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000456bool AliasSetTracker::remove(CallSite CS) {
Duncan Sandsdff67102007-12-01 07:51:45 +0000457 if (AA.doesNotAccessMemory(CS))
458 return false; // doesn't alias anything
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000459
460 AliasSet *AS = findAliasSetForCallSite(CS);
461 if (!AS) return false;
462 remove(*AS);
463 return true;
464}
465
466bool AliasSetTracker::remove(Instruction *I) {
467 // Dispatch to one of the other remove methods...
468 if (LoadInst *LI = dyn_cast<LoadInst>(I))
469 return remove(LI);
Chris Lattner9476d742010-08-29 04:13:43 +0000470 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000471 return remove(SI);
Chris Lattner9476d742010-08-29 04:13:43 +0000472 if (CallInst *CI = dyn_cast<CallInst>(I))
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000473 return remove(CI);
Chris Lattner9476d742010-08-29 04:13:43 +0000474 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
Dan Gohman235fc572008-04-14 18:34:50 +0000475 return remove(VAAI);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000476 return true;
477}
478
Chris Lattner2cffeec2003-12-18 08:11:56 +0000479
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000480// deleteValue method - This method is used to remove a pointer value from the
Chris Lattner2cffeec2003-12-18 08:11:56 +0000481// AliasSetTracker entirely. It should be used when an instruction is deleted
482// from the program to update the AST. If you don't use this, you would have
483// dangling pointers to deleted instructions.
484//
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000485void AliasSetTracker::deleteValue(Value *PtrVal) {
Chris Lattnerb66e6482004-09-14 19:15:32 +0000486 // Notify the alias analysis implementation that this value is gone.
487 AA.deleteValue(PtrVal);
488
Chris Lattner959e3212006-06-26 19:20:48 +0000489 // If this is a call instruction, remove the callsite from the appropriate
Chris Lattnercb7f6532010-08-29 18:42:23 +0000490 // AliasSet (if present).
491 if (CallSite CS = PtrVal) {
492 if (!AA.doesNotAccessMemory(CS)) {
493 // Scan all the alias sets to see if this call site is contained.
494 for (iterator I = begin(), E = end(); I != E; ++I) {
495 if (I->Forward) continue;
496
497 I->removeCallSite(CS);
498 }
499 }
500 }
Chris Lattner959e3212006-06-26 19:20:48 +0000501
Chris Lattnerb66e6482004-09-14 19:15:32 +0000502 // First, look up the PointerRec for this pointer.
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000503 PointerMapType::iterator I = PointerMap.find(PtrVal);
Chris Lattner2cffeec2003-12-18 08:11:56 +0000504 if (I == PointerMap.end()) return; // Noop
505
506 // If we found one, remove the pointer from the alias set it is in.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000507 AliasSet::PointerRec *PtrValEnt = I->second;
508 AliasSet *AS = PtrValEnt->getAliasSet(*this);
Chris Lattner2cffeec2003-12-18 08:11:56 +0000509
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000510 // Unlink and delete from the list of values.
511 PtrValEnt->eraseFromList();
512
513 // Stop using the alias set.
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000514 AS->dropRef(*this);
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000515
Chris Lattner2cffeec2003-12-18 08:11:56 +0000516 PointerMap.erase(I);
517}
518
Chris Lattnerb66e6482004-09-14 19:15:32 +0000519// copyValue - This method should be used whenever a preexisting value in the
520// program is copied or cloned, introducing a new value. Note that it is ok for
521// clients that use this method to introduce the same value multiple times: if
522// the tracker already knows about a value, it will ignore the request.
523//
524void AliasSetTracker::copyValue(Value *From, Value *To) {
525 // Notify the alias analysis implementation that this value is copied.
526 AA.copyValue(From, To);
527
528 // First, look up the PointerRec for this pointer.
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000529 PointerMapType::iterator I = PointerMap.find(From);
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000530 if (I == PointerMap.end())
Chris Lattnerb66e6482004-09-14 19:15:32 +0000531 return; // Noop
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000532 assert(I->second->hasAliasSet() && "Dead entry?");
Chris Lattnerb66e6482004-09-14 19:15:32 +0000533
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000534 AliasSet::PointerRec &Entry = getEntryFor(To);
535 if (Entry.hasAliasSet()) return; // Already in the tracker!
Chris Lattnerb66e6482004-09-14 19:15:32 +0000536
537 // Add it to the alias set it aliases...
Devang Patel6d9a2df2009-03-30 18:34:47 +0000538 I = PointerMap.find(From);
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000539 AliasSet *AS = I->second->getAliasSet(*this);
Dan Gohmanfb8096d2010-10-18 21:28:00 +0000540 AS->addPointer(*this, Entry, I->second->getSize(),
Dan Gohmaneee54002010-10-18 23:31:47 +0000541 I->second->getTBAAInfo(),
Dan Gohmana8702ea2010-10-18 20:44:50 +0000542 true);
Chris Lattnerb66e6482004-09-14 19:15:32 +0000543}
544
545
Chris Lattner2cffeec2003-12-18 08:11:56 +0000546
Chris Lattner9971ac42003-02-24 20:37:56 +0000547//===----------------------------------------------------------------------===//
548// AliasSet/AliasSetTracker Printing Support
549//===----------------------------------------------------------------------===//
550
Chris Lattner791102f2009-08-23 05:17:37 +0000551void AliasSet::print(raw_ostream &OS) const {
Benjamin Kramer03392742010-08-30 14:46:53 +0000552 OS << " AliasSet[" << (void*)this << ", " << RefCount << "] ";
Dan Gohman92c7b652008-04-21 19:48:48 +0000553 OS << (AliasTy == MustAlias ? "must" : "may") << " alias, ";
Chris Lattner9971ac42003-02-24 20:37:56 +0000554 switch (AccessTy) {
555 case NoModRef: OS << "No access "; break;
556 case Refs : OS << "Ref "; break;
557 case Mods : OS << "Mod "; break;
558 case ModRef : OS << "Mod/Ref "; break;
Torok Edwinc23197a2009-07-14 16:55:14 +0000559 default: llvm_unreachable("Bad value for AccessTy!");
Chris Lattner009cc3d2002-09-26 21:49:07 +0000560 }
Chris Lattnere2609242003-12-14 04:52:11 +0000561 if (isVolatile()) OS << "[volatile] ";
Chris Lattner9971ac42003-02-24 20:37:56 +0000562 if (Forward)
563 OS << " forwarding to " << (void*)Forward;
564
565
Dan Gohmancb406c22007-10-03 19:26:29 +0000566 if (!empty()) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000567 OS << "Pointers: ";
568 for (iterator I = begin(), E = end(); I != E; ++I) {
569 if (I != begin()) OS << ", ";
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000570 WriteAsOperand(OS << "(", I.getPointer());
571 OS << ", " << I.getSize() << ")";
Chris Lattner9971ac42003-02-24 20:37:56 +0000572 }
573 }
574 if (!CallSites.empty()) {
575 OS << "\n " << CallSites.size() << " Call Sites: ";
576 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
577 if (i) OS << ", ";
Chris Lattnercb7f6532010-08-29 18:42:23 +0000578 WriteAsOperand(OS, CallSites[i]);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000579 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000580 }
581 OS << "\n";
582}
583
Chris Lattner791102f2009-08-23 05:17:37 +0000584void AliasSetTracker::print(raw_ostream &OS) const {
Chris Lattner9971ac42003-02-24 20:37:56 +0000585 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
586 << PointerMap.size() << " pointer values.\n";
587 for (const_iterator I = begin(), E = end(); I != E; ++I)
588 I->print(OS);
589 OS << "\n";
590}
591
David Greene43c48ac2009-12-23 19:27:59 +0000592void AliasSet::dump() const { print(dbgs()); }
593void AliasSetTracker::dump() const { print(dbgs()); }
Chris Lattner9971ac42003-02-24 20:37:56 +0000594
Chris Lattner9971ac42003-02-24 20:37:56 +0000595//===----------------------------------------------------------------------===//
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000596// ASTCallbackVH Class Implementation
597//===----------------------------------------------------------------------===//
598
599void AliasSetTracker::ASTCallbackVH::deleted() {
600 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
601 AST->deleteValue(getValPtr());
602 // this now dangles!
603}
604
605AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
Dan Gohmana818c302009-07-31 18:21:48 +0000606 : CallbackVH(V), AST(ast) {}
607
608AliasSetTracker::ASTCallbackVH &
609AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
610 return *this = ASTCallbackVH(V, AST);
611}
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000612
613//===----------------------------------------------------------------------===//
Chris Lattner9971ac42003-02-24 20:37:56 +0000614// AliasSetPrinter Pass
615//===----------------------------------------------------------------------===//
616
617namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000618 class AliasSetPrinter : public FunctionPass {
Chris Lattner9971ac42003-02-24 20:37:56 +0000619 AliasSetTracker *Tracker;
620 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +0000621 static char ID; // Pass identification, replacement for typeid
Owen Anderson081c34b2010-10-19 17:21:58 +0000622 AliasSetPrinter() : FunctionPass(ID) {
623 initializeAliasSetPrinterPass(*PassRegistry::getPassRegistry());
624 }
Devang Patel794fd752007-05-01 21:15:47 +0000625
Chris Lattner9971ac42003-02-24 20:37:56 +0000626 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
627 AU.setPreservesAll();
628 AU.addRequired<AliasAnalysis>();
629 }
630
631 virtual bool runOnFunction(Function &F) {
632 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
633
634 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner6ffe5512004-04-27 15:13:33 +0000635 Tracker->add(&*I);
David Greene4850a892009-12-23 22:49:57 +0000636 Tracker->print(errs());
Chris Lattner4983cf72006-01-03 06:05:22 +0000637 delete Tracker;
Chris Lattner9971ac42003-02-24 20:37:56 +0000638 return false;
639 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000640 };
Chris Lattner009cc3d2002-09-26 21:49:07 +0000641}
Dan Gohman844731a2008-05-13 00:00:25 +0000642
643char AliasSetPrinter::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +0000644INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets",
645 "Alias Set Printer", false, true)
646INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
647INITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets",
Owen Andersonce665bd2010-10-07 22:25:06 +0000648 "Alias Set Printer", false, true)