blob: b056d0091a099a0f2ffe134d482b333418badee1 [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"
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;
38
Chris Lattnercc8d5242004-11-27 18:37:42 +000039 if (AliasTy == MustAlias) {
40 // Check that these two merged sets really are must aliases. Since both
41 // used to be must-alias sets, we can just check any pointer from each set
42 // for aliasing.
43 AliasAnalysis &AA = AST.getAliasAnalysis();
Chris Lattnerd7168dd2009-03-09 05:11:09 +000044 PointerRec *L = getSomePointer();
45 PointerRec *R = AS.getSomePointer();
Chris Lattnercc8d5242004-11-27 18:37:42 +000046
47 // If the pointers are not a must-alias pair, this set becomes a may alias.
Chris Lattnerd7168dd2009-03-09 05:11:09 +000048 if (AA.alias(L->getValue(), L->getSize(), R->getValue(), R->getSize())
Chris Lattnercc8d5242004-11-27 18:37:42 +000049 != AliasAnalysis::MustAlias)
50 AliasTy = MayAlias;
51 }
52
Chris Lattner9971ac42003-02-24 20:37:56 +000053 if (CallSites.empty()) { // Merge call sites...
54 if (!AS.CallSites.empty())
55 std::swap(CallSites, AS.CallSites);
56 } else if (!AS.CallSites.empty()) {
57 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
58 AS.CallSites.clear();
59 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000060
Chris Lattner9971ac42003-02-24 20:37:56 +000061 AS.Forward = this; // Forward across AS now...
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000062 addRef(); // AS is now pointing to us...
Chris Lattner9971ac42003-02-24 20:37:56 +000063
64 // Merge the list of constituent pointers...
Chris Lattner2cffeec2003-12-18 08:11:56 +000065 if (AS.PtrList) {
66 *PtrListEnd = AS.PtrList;
Chris Lattnerd7168dd2009-03-09 05:11:09 +000067 AS.PtrList->setPrevInList(PtrListEnd);
Chris Lattner2cffeec2003-12-18 08:11:56 +000068 PtrListEnd = AS.PtrListEnd;
69
70 AS.PtrList = 0;
71 AS.PtrListEnd = &AS.PtrList;
Chris Lattner3080b602004-09-15 16:59:47 +000072 assert(*AS.PtrListEnd == 0 && "End of list is not null?");
Chris Lattner2cffeec2003-12-18 08:11:56 +000073 }
Chris Lattner009cc3d2002-09-26 21:49:07 +000074}
75
Chris Lattner9971ac42003-02-24 20:37:56 +000076void AliasSetTracker::removeAliasSet(AliasSet *AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +000077 if (AliasSet *Fwd = AS->Forward) {
78 Fwd->dropRef(*this);
79 AS->Forward = 0;
80 }
Chris Lattner9971ac42003-02-24 20:37:56 +000081 AliasSets.erase(AS);
82}
83
84void AliasSet::removeFromTracker(AliasSetTracker &AST) {
85 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
86 AST.removeAliasSet(this);
87}
88
Chris Lattnerd7168dd2009-03-09 05:11:09 +000089void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
Chris Lattnerb66e6482004-09-14 19:15:32 +000090 unsigned Size, bool KnownMustAlias) {
Chris Lattnerd7168dd2009-03-09 05:11:09 +000091 assert(!Entry.hasAliasSet() && "Entry already in set!");
Chris Lattner9971ac42003-02-24 20:37:56 +000092
Chris Lattnerb66e6482004-09-14 19:15:32 +000093 // Check to see if we have to downgrade to _may_ alias.
94 if (isMustAlias() && !KnownMustAlias)
Chris Lattnerd7168dd2009-03-09 05:11:09 +000095 if (PointerRec *P = getSomePointer()) {
Chris Lattner3080b602004-09-15 16:59:47 +000096 AliasAnalysis &AA = AST.getAliasAnalysis();
Chris Lattner31a9d182003-02-26 22:11:00 +000097 AliasAnalysis::AliasResult Result =
Chris Lattnerd7168dd2009-03-09 05:11:09 +000098 AA.alias(P->getValue(), P->getSize(), Entry.getValue(), Size);
Chris Lattner31a9d182003-02-26 22:11:00 +000099 if (Result == AliasAnalysis::MayAlias)
Chris Lattner9971ac42003-02-24 20:37:56 +0000100 AliasTy = MayAlias;
Chris Lattner31a9d182003-02-26 22:11:00 +0000101 else // First entry of must alias must have maximum size!
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000102 P->updateSize(Size);
Chris Lattner31a9d182003-02-26 22:11:00 +0000103 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
104 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000105
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000106 Entry.setAliasSet(this);
107 Entry.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000108
109 // Add it to the end of the list...
Chris Lattner2cffeec2003-12-18 08:11:56 +0000110 assert(*PtrListEnd == 0 && "End of list is not null?");
111 *PtrListEnd = &Entry;
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000112 PtrListEnd = Entry.setPrevInList(PtrListEnd);
Chris Lattner3080b602004-09-15 16:59:47 +0000113 assert(*PtrListEnd == 0 && "End of list is not null?");
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000114 addRef(); // Entry points to alias set...
Chris Lattner9971ac42003-02-24 20:37:56 +0000115}
116
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000117void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000118 CallSites.push_back(CS);
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000119
Duncan Sandsdff67102007-12-01 07:51:45 +0000120 AliasAnalysis::ModRefBehavior Behavior = AA.getModRefBehavior(CS);
121 if (Behavior == AliasAnalysis::DoesNotAccessMemory)
122 return;
123 else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
124 AliasTy = MayAlias;
125 AccessTy |= Refs;
126 return;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000127 }
128
129 // FIXME: This should use mod/ref information to make this not suck so bad
130 AliasTy = MayAlias;
Chris Lattner577385e2003-05-03 03:42:08 +0000131 AccessTy = ModRef;
Chris Lattner9971ac42003-02-24 20:37:56 +0000132}
133
134/// aliasesPointer - Return true if the specified pointer "may" (or must)
Chris Lattner009cc3d2002-09-26 21:49:07 +0000135/// alias one of the members in the set.
136///
Chris Lattner31a9d182003-02-26 22:11:00 +0000137bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
138 AliasAnalysis &AA) const {
Chris Lattner9971ac42003-02-24 20:37:56 +0000139 if (AliasTy == MustAlias) {
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000140 assert(CallSites.empty() && "Illegal must alias set!");
141
Chris Lattner9971ac42003-02-24 20:37:56 +0000142 // If this is a set of MustAliases, only check to see if the pointer aliases
143 // SOME value in the set...
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000144 PointerRec *SomePtr = getSomePointer();
Chris Lattner9971ac42003-02-24 20:37:56 +0000145 assert(SomePtr && "Empty must-alias set??");
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000146 return AA.alias(SomePtr->getValue(), SomePtr->getSize(), Ptr, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000147 }
148
149 // If this is a may-alias set, we have to check all of the pointers in the set
150 // to be sure it doesn't alias the set...
151 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000152 if (AA.alias(Ptr, Size, I.getPointer(), I.getSize()))
Chris Lattner9971ac42003-02-24 20:37:56 +0000153 return true;
154
155 // Check the call sites list and invoke list...
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000156 if (!CallSites.empty()) {
157 if (AA.hasNoModRefInfoForCalls())
158 return true;
159
160 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
161 if (AA.getModRefInfo(CallSites[i], const_cast<Value*>(Ptr), Size)
162 != AliasAnalysis::NoModRef)
163 return true;
164 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000165
Chris Lattner009cc3d2002-09-26 21:49:07 +0000166 return false;
167}
168
Chris Lattner9971ac42003-02-24 20:37:56 +0000169bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
Duncan Sandsdff67102007-12-01 07:51:45 +0000170 if (AA.doesNotAccessMemory(CS))
171 return false;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000172
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000173 if (AA.hasNoModRefInfoForCalls())
174 return true;
175
176 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
177 if (AA.getModRefInfo(CallSites[i], CS) != AliasAnalysis::NoModRef ||
178 AA.getModRefInfo(CS, CallSites[i]) != AliasAnalysis::NoModRef)
179 return true;
180
181 for (iterator I = begin(), E = end(); I != E; ++I)
182 if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
183 AliasAnalysis::NoModRef)
184 return true;
185
186 return false;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000187}
188
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000189void AliasSetTracker::clear() {
190 // Delete all the PointerRec entries.
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000191 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
192 I != E; ++I)
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000193 I->second->eraseFromList();
194
195 PointerMap.clear();
196
197 // The alias sets should all be clear now.
198 AliasSets.clear();
199}
200
Chris Lattner009cc3d2002-09-26 21:49:07 +0000201
Chris Lattner009cc3d2002-09-26 21:49:07 +0000202/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
203/// instruction referring to the pointer into. If there are multiple alias sets
204/// that may alias the pointer, merge them together and return the unified set.
205///
Chris Lattner31a9d182003-02-26 22:11:00 +0000206AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
207 unsigned Size) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000208 AliasSet *FoundSet = 0;
Chris Lattner9971ac42003-02-24 20:37:56 +0000209 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner31a9d182003-02-26 22:11:00 +0000210 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
Chris Lattnercc8d5242004-11-27 18:37:42 +0000211 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
Chris Lattner9971ac42003-02-24 20:37:56 +0000212 FoundSet = I; // Remember it.
Chris Lattnercc8d5242004-11-27 18:37:42 +0000213 } else { // Otherwise, we must merge the sets.
214 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
Chris Lattner009cc3d2002-09-26 21:49:07 +0000215 }
216 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000217
218 return FoundSet;
219}
220
Chris Lattner07bfa522004-11-26 21:36:25 +0000221/// containsPointer - Return true if the specified location is represented by
222/// this alias set, false otherwise. This does not modify the AST object or
223/// alias sets.
224bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const {
225 for (const_iterator I = begin(), E = end(); I != E; ++I)
226 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA))
227 return true;
228 return false;
229}
230
231
232
Chris Lattner9971ac42003-02-24 20:37:56 +0000233AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
234 AliasSet *FoundSet = 0;
235 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000236 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
Chris Lattnercc8d5242004-11-27 18:37:42 +0000237 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
Chris Lattner9971ac42003-02-24 20:37:56 +0000238 FoundSet = I; // Remember it.
Chris Lattnercc8d5242004-11-27 18:37:42 +0000239 } else if (!I->Forward) { // Otherwise, we must merge the sets.
240 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
Chris Lattner9971ac42003-02-24 20:37:56 +0000241 }
242 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000243
244 return FoundSet;
245}
246
247
Chris Lattner009cc3d2002-09-26 21:49:07 +0000248
Chris Lattner9971ac42003-02-24 20:37:56 +0000249
250/// getAliasSetForPointer - Return the alias set that the specified pointer
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000251/// lives in.
Chris Lattner12c11552004-07-21 05:18:04 +0000252AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
253 bool *New) {
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000254 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
Chris Lattner9971ac42003-02-24 20:37:56 +0000255
256 // Check to see if the pointer is already known...
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000257 if (Entry.hasAliasSet()) {
258 Entry.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000259 // Return the set!
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000260 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattner31a9d182003-02-26 22:11:00 +0000261 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000262 // Add it to the alias set it aliases...
Chris Lattner31a9d182003-02-26 22:11:00 +0000263 AS->addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000264 return *AS;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000265 } else {
Chris Lattner12c11552004-07-21 05:18:04 +0000266 if (New) *New = true;
Chris Lattner9971ac42003-02-24 20:37:56 +0000267 // Otherwise create a new alias set to hold the loaded pointer...
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000268 AliasSets.push_back(new AliasSet());
Chris Lattner31a9d182003-02-26 22:11:00 +0000269 AliasSets.back().addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000270 return AliasSets.back();
Chris Lattner009cc3d2002-09-26 21:49:07 +0000271 }
272}
273
Chris Lattner61d46272004-07-26 05:50:23 +0000274bool AliasSetTracker::add(Value *Ptr, unsigned Size) {
275 bool NewPtr;
276 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
277 return NewPtr;
278}
279
280
Chris Lattner12c11552004-07-21 05:18:04 +0000281bool AliasSetTracker::add(LoadInst *LI) {
282 bool NewPtr;
283 AliasSet &AS = addPointer(LI->getOperand(0),
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000284 AA.getTypeStoreSize(LI->getType()),
Chris Lattner12c11552004-07-21 05:18:04 +0000285 AliasSet::Refs, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000286 if (LI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000287 return NewPtr;
Chris Lattner9971ac42003-02-24 20:37:56 +0000288}
289
Chris Lattner12c11552004-07-21 05:18:04 +0000290bool AliasSetTracker::add(StoreInst *SI) {
291 bool NewPtr;
292 Value *Val = SI->getOperand(0);
293 AliasSet &AS = addPointer(SI->getOperand(1),
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000294 AA.getTypeStoreSize(Val->getType()),
Chris Lattner12c11552004-07-21 05:18:04 +0000295 AliasSet::Mods, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000296 if (SI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000297 return NewPtr;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000298}
299
Chris Lattner5c882602004-07-25 07:57:37 +0000300bool AliasSetTracker::add(FreeInst *FI) {
301 bool NewPtr;
Chris Lattner05a24e52008-05-22 03:23:06 +0000302 addPointer(FI->getOperand(0), ~0, AliasSet::Mods, NewPtr);
Chris Lattner5c882602004-07-25 07:57:37 +0000303 return NewPtr;
304}
305
Dan Gohman235fc572008-04-14 18:34:50 +0000306bool AliasSetTracker::add(VAArgInst *VAAI) {
307 bool NewPtr;
Chris Lattner05a24e52008-05-22 03:23:06 +0000308 addPointer(VAAI->getOperand(0), ~0, AliasSet::ModRef, NewPtr);
Dan Gohman235fc572008-04-14 18:34:50 +0000309 return NewPtr;
310}
311
Chris Lattnere2609242003-12-14 04:52:11 +0000312
Chris Lattner12c11552004-07-21 05:18:04 +0000313bool AliasSetTracker::add(CallSite CS) {
Zhou Sheng7e4286e2009-03-03 06:02:04 +0000314 if (isa<DbgInfoIntrinsic>(CS.getInstruction()))
315 return true; // Ignore DbgInfo Intrinsics.
Duncan Sandsdff67102007-12-01 07:51:45 +0000316 if (AA.doesNotAccessMemory(CS))
317 return true; // doesn't alias anything
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000318
Chris Lattner9971ac42003-02-24 20:37:56 +0000319 AliasSet *AS = findAliasSetForCallSite(CS);
320 if (!AS) {
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000321 AliasSets.push_back(new AliasSet());
Chris Lattner9971ac42003-02-24 20:37:56 +0000322 AS = &AliasSets.back();
Chris Lattner12c11552004-07-21 05:18:04 +0000323 AS->addCallSite(CS, AA);
324 return true;
325 } else {
326 AS->addCallSite(CS, AA);
327 return false;
Chris Lattner9971ac42003-02-24 20:37:56 +0000328 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000329}
330
Chris Lattner12c11552004-07-21 05:18:04 +0000331bool AliasSetTracker::add(Instruction *I) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000332 // Dispatch to one of the other add methods...
333 if (LoadInst *LI = dyn_cast<LoadInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000334 return add(LI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000335 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000336 return add(SI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000337 else if (CallInst *CI = dyn_cast<CallInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000338 return add(CI);
Chris Lattner9971ac42003-02-24 20:37:56 +0000339 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000340 return add(II);
Chris Lattner5c882602004-07-25 07:57:37 +0000341 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
342 return add(FI);
Dan Gohman235fc572008-04-14 18:34:50 +0000343 else if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
344 return add(VAAI);
Chris Lattner12c11552004-07-21 05:18:04 +0000345 return true;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000346}
347
Chris Lattner319d05b2003-03-03 23:28:05 +0000348void AliasSetTracker::add(BasicBlock &BB) {
349 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
350 add(I);
351}
352
353void AliasSetTracker::add(const AliasSetTracker &AST) {
354 assert(&AA == &AST.AA &&
355 "Merging AliasSetTracker objects with different Alias Analyses!");
356
357 // Loop over all of the alias sets in AST, adding the pointers contained
358 // therein into the current alias sets. This can cause alias sets to be
359 // merged together in the current AST.
360 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
361 if (!I->Forward) { // Ignore forwarding alias sets
362 AliasSet &AS = const_cast<AliasSet&>(*I);
363
364 // If there are any call sites in the alias set, add them to this AST.
365 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
366 add(AS.CallSites[i]);
367
368 // Loop over all of the pointers in this alias set...
369 AliasSet::iterator I = AS.begin(), E = AS.end();
Chris Lattner12c11552004-07-21 05:18:04 +0000370 bool X;
Chris Lattnerf711fb72007-05-23 06:36:35 +0000371 for (; I != E; ++I) {
372 AliasSet &NewAS = addPointer(I.getPointer(), I.getSize(),
373 (AliasSet::AccessType)AS.AccessTy, X);
374 if (AS.isVolatile()) NewAS.setVolatile();
375 }
Chris Lattner319d05b2003-03-03 23:28:05 +0000376 }
377}
378
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000379/// remove - Remove the specified (potentially non-empty) alias set from the
380/// tracker.
381void AliasSetTracker::remove(AliasSet &AS) {
Chris Lattnerf2998572006-06-27 23:48:59 +0000382 // Drop all call sites.
383 AS.CallSites.clear();
384
385 // Clear the alias set.
386 unsigned NumRefs = 0;
387 while (!AS.empty()) {
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000388 AliasSet::PointerRec *P = AS.PtrList;
389
390 Value *ValToRemove = P->getValue();
Chris Lattnerf2998572006-06-27 23:48:59 +0000391
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000392 // Unlink and delete entry from the list of values.
393 P->eraseFromList();
Chris Lattnerf2998572006-06-27 23:48:59 +0000394
395 // Remember how many references need to be dropped.
396 ++NumRefs;
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000397
Chris Lattnerf2998572006-06-27 23:48:59 +0000398 // Finally, remove the entry.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000399 PointerMap.erase(ValToRemove);
Chris Lattnerf2998572006-06-27 23:48:59 +0000400 }
401
402 // Stop using the alias set, removing it.
Chris Lattner356d8c22006-06-27 23:56:13 +0000403 AS.RefCount -= NumRefs;
404 if (AS.RefCount == 0)
405 AS.removeFromTracker(*this);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000406}
407
Chris Lattner61d46272004-07-26 05:50:23 +0000408bool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
409 AliasSet *AS = findAliasSetForPointer(Ptr, Size);
410 if (!AS) return false;
411 remove(*AS);
412 return true;
413}
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000414
415bool AliasSetTracker::remove(LoadInst *LI) {
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000416 unsigned Size = AA.getTypeStoreSize(LI->getType());
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000417 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
418 if (!AS) return false;
419 remove(*AS);
420 return true;
421}
422
423bool AliasSetTracker::remove(StoreInst *SI) {
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000424 unsigned Size = AA.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.
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000489 PointerMapType::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.
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000515 PointerMapType::iterator I = PointerMap.find(From);
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000516 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
Chris Lattner791102f2009-08-23 05:17:37 +0000535void AliasSet::print(raw_ostream &OS) const {
536 OS << " AliasSet[" << format("0x%p", (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 Edwinc23197a2009-07-14 16:55:14 +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
Chris Lattner791102f2009-08-23 05:17:37 +0000568void AliasSetTracker::print(raw_ostream &OS) const {
Chris Lattner9971ac42003-02-24 20:37:56 +0000569 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
Chris Lattner791102f2009-08-23 05:17:37 +0000576void AliasSet::dump() const { print(errs()); }
577void AliasSetTracker::dump() const { print(errs()); }
Chris Lattner9971ac42003-02-24 20:37:56 +0000578
Chris Lattner9971ac42003-02-24 20:37:56 +0000579//===----------------------------------------------------------------------===//
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000580// ASTCallbackVH Class Implementation
581//===----------------------------------------------------------------------===//
582
583void AliasSetTracker::ASTCallbackVH::deleted() {
584 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
585 AST->deleteValue(getValPtr());
586 // this now dangles!
587}
588
589AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
Dan Gohmana818c302009-07-31 18:21:48 +0000590 : CallbackVH(V), AST(ast) {}
591
592AliasSetTracker::ASTCallbackVH &
593AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
594 return *this = ASTCallbackVH(V, AST);
595}
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000596
597//===----------------------------------------------------------------------===//
Chris Lattner9971ac42003-02-24 20:37:56 +0000598// AliasSetPrinter Pass
599//===----------------------------------------------------------------------===//
600
601namespace {
Reid Spencerd7d83db2007-02-05 23:42:17 +0000602 class VISIBILITY_HIDDEN AliasSetPrinter : public FunctionPass {
Chris Lattner9971ac42003-02-24 20:37:56 +0000603 AliasSetTracker *Tracker;
604 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +0000605 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +0000606 AliasSetPrinter() : FunctionPass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +0000607
Chris Lattner9971ac42003-02-24 20:37:56 +0000608 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
609 AU.setPreservesAll();
610 AU.addRequired<AliasAnalysis>();
611 }
612
613 virtual bool runOnFunction(Function &F) {
614 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
615
616 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner6ffe5512004-04-27 15:13:33 +0000617 Tracker->add(&*I);
Chris Lattner791102f2009-08-23 05:17:37 +0000618 Tracker->print(errs());
Chris Lattner4983cf72006-01-03 06:05:22 +0000619 delete Tracker;
Chris Lattner9971ac42003-02-24 20:37:56 +0000620 return false;
621 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000622 };
Chris Lattner009cc3d2002-09-26 21:49:07 +0000623}
Dan Gohman844731a2008-05-13 00:00:25 +0000624
625char AliasSetPrinter::ID = 0;
626static RegisterPass<AliasSetPrinter>
627X("print-alias-sets", "Alias Set Printer", false, true);