blob: e29c2bc22b3e22bbdbfa69378f08212f20abd6d1 [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
Eli Friedman6f3ba372011-07-27 00:46:46 +000059 if (UnknownInsts.empty()) { // Merge call sites...
60 if (!AS.UnknownInsts.empty())
61 std::swap(UnknownInsts, AS.UnknownInsts);
62 } else if (!AS.UnknownInsts.empty()) {
63 UnknownInsts.insert(UnknownInsts.end(), AS.UnknownInsts.begin(), AS.UnknownInsts.end());
64 AS.UnknownInsts.clear();
Chris Lattner9971ac42003-02-24 20:37:56 +000065 }
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
Eli Friedman6f3ba372011-07-27 00:46:46 +0000126void AliasSet::addUnknownInst(Instruction *I, AliasAnalysis &AA) {
127 UnknownInsts.push_back(I);
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000128
Eli Friedman6f3ba372011-07-27 00:46:46 +0000129 if (!I->mayWriteToMemory()) {
Duncan Sandsdff67102007-12-01 07:51:45 +0000130 AliasTy = MayAlias;
131 AccessTy |= Refs;
132 return;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000133 }
134
135 // FIXME: This should use mod/ref information to make this not suck so bad
136 AliasTy = MayAlias;
Chris Lattner577385e2003-05-03 03:42:08 +0000137 AccessTy = ModRef;
Chris Lattner9971ac42003-02-24 20:37:56 +0000138}
139
140/// aliasesPointer - Return true if the specified pointer "may" (or must)
Chris Lattner009cc3d2002-09-26 21:49:07 +0000141/// alias one of the members in the set.
142///
Dan Gohman3da848b2010-10-19 22:54:46 +0000143bool AliasSet::aliasesPointer(const Value *Ptr, uint64_t Size,
Dan Gohmana8702ea2010-10-18 20:44:50 +0000144 const MDNode *TBAAInfo,
Chris Lattner31a9d182003-02-26 22:11:00 +0000145 AliasAnalysis &AA) const {
Chris Lattner9971ac42003-02-24 20:37:56 +0000146 if (AliasTy == MustAlias) {
Eli Friedman6f3ba372011-07-27 00:46:46 +0000147 assert(UnknownInsts.empty() && "Illegal must alias set!");
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000148
Chris Lattner9971ac42003-02-24 20:37:56 +0000149 // If this is a set of MustAliases, only check to see if the pointer aliases
Chris Lattner9476d742010-08-29 04:13:43 +0000150 // SOME value in the set.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000151 PointerRec *SomePtr = getSomePointer();
Chris Lattner9971ac42003-02-24 20:37:56 +0000152 assert(SomePtr && "Empty must-alias set??");
Dan Gohmana8702ea2010-10-18 20:44:50 +0000153 return AA.alias(AliasAnalysis::Location(SomePtr->getValue(),
154 SomePtr->getSize(),
155 SomePtr->getTBAAInfo()),
156 AliasAnalysis::Location(Ptr, Size, TBAAInfo));
Chris Lattner9971ac42003-02-24 20:37:56 +0000157 }
158
159 // If this is a may-alias set, we have to check all of the pointers in the set
160 // to be sure it doesn't alias the set...
161 for (iterator I = begin(), E = end(); I != E; ++I)
Dan Gohmana8702ea2010-10-18 20:44:50 +0000162 if (AA.alias(AliasAnalysis::Location(Ptr, Size, TBAAInfo),
Dan Gohmanfb8096d2010-10-18 21:28:00 +0000163 AliasAnalysis::Location(I.getPointer(), I.getSize(),
164 I.getTBAAInfo())))
Chris Lattner9971ac42003-02-24 20:37:56 +0000165 return true;
166
Eli Friedman6f3ba372011-07-27 00:46:46 +0000167 // Check the unknown instructions...
168 if (!UnknownInsts.empty()) {
169 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i)
170 if (AA.getModRefInfo(UnknownInsts[i],
Dan Gohmana8702ea2010-10-18 20:44:50 +0000171 AliasAnalysis::Location(Ptr, Size, TBAAInfo)) !=
172 AliasAnalysis::NoModRef)
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000173 return true;
174 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000175
Chris Lattner009cc3d2002-09-26 21:49:07 +0000176 return false;
177}
178
Eli Friedman6f3ba372011-07-27 00:46:46 +0000179bool AliasSet::aliasesUnknownInst(Instruction *Inst, AliasAnalysis &AA) const {
180 if (!Inst->mayReadOrWriteMemory())
Duncan Sandsdff67102007-12-01 07:51:45 +0000181 return false;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000182
Eli Friedman6f3ba372011-07-27 00:46:46 +0000183 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
184 CallSite C1 = getUnknownInst(i), C2 = Inst;
185 if (!C1 || !C2 ||
Eli Friedman751bef72011-07-27 01:02:25 +0000186 AA.getModRefInfo(C1, C2) != AliasAnalysis::NoModRef ||
187 AA.getModRefInfo(C2, C1) != AliasAnalysis::NoModRef)
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000188 return true;
Chris Lattnercb7f6532010-08-29 18:42:23 +0000189 }
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000190
191 for (iterator I = begin(), E = end(); I != E; ++I)
Hal Finkelce0dd732012-02-10 15:52:39 +0000192 if (AA.getModRefInfo(Inst, AliasAnalysis::Location(I.getPointer(),
193 I.getSize(),
194 I.getTBAAInfo())) !=
Chris Lattnerefe30ef2004-07-27 02:20:26 +0000195 AliasAnalysis::NoModRef)
196 return true;
197
198 return false;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000199}
200
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000201void AliasSetTracker::clear() {
202 // Delete all the PointerRec entries.
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000203 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
204 I != E; ++I)
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000205 I->second->eraseFromList();
206
207 PointerMap.clear();
208
209 // The alias sets should all be clear now.
210 AliasSets.clear();
211}
212
Chris Lattner009cc3d2002-09-26 21:49:07 +0000213
Chris Lattner009cc3d2002-09-26 21:49:07 +0000214/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
215/// instruction referring to the pointer into. If there are multiple alias sets
216/// that may alias the pointer, merge them together and return the unified set.
217///
Chris Lattner31a9d182003-02-26 22:11:00 +0000218AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
Dan Gohman3da848b2010-10-19 22:54:46 +0000219 uint64_t Size,
Dan Gohmana8702ea2010-10-18 20:44:50 +0000220 const MDNode *TBAAInfo) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000221 AliasSet *FoundSet = 0;
Chris Lattner6e1f5102010-08-29 04:06:55 +0000222 for (iterator I = begin(), E = end(); I != E; ++I) {
Dan Gohmana8702ea2010-10-18 20:44:50 +0000223 if (I->Forward || !I->aliasesPointer(Ptr, Size, TBAAInfo, AA)) continue;
Chris Lattner6e1f5102010-08-29 04:06:55 +0000224
225 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
226 FoundSet = I; // Remember it.
227 } else { // Otherwise, we must merge the sets.
228 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
Chris Lattner009cc3d2002-09-26 21:49:07 +0000229 }
Chris Lattner6e1f5102010-08-29 04:06:55 +0000230 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000231
232 return FoundSet;
233}
234
Chris Lattner07bfa522004-11-26 21:36:25 +0000235/// containsPointer - Return true if the specified location is represented by
236/// this alias set, false otherwise. This does not modify the AST object or
237/// alias sets.
Dan Gohman3da848b2010-10-19 22:54:46 +0000238bool AliasSetTracker::containsPointer(Value *Ptr, uint64_t Size,
Dan Gohmana8702ea2010-10-18 20:44:50 +0000239 const MDNode *TBAAInfo) const {
Chris Lattner07bfa522004-11-26 21:36:25 +0000240 for (const_iterator I = begin(), E = end(); I != E; ++I)
Dan Gohmana8702ea2010-10-18 20:44:50 +0000241 if (!I->Forward && I->aliasesPointer(Ptr, Size, TBAAInfo, AA))
Chris Lattner07bfa522004-11-26 21:36:25 +0000242 return true;
243 return false;
244}
245
246
247
Eli Friedman6f3ba372011-07-27 00:46:46 +0000248AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000249 AliasSet *FoundSet = 0;
Chris Lattner6e1f5102010-08-29 04:06:55 +0000250 for (iterator I = begin(), E = end(); I != E; ++I) {
Eli Friedman6f3ba372011-07-27 00:46:46 +0000251 if (I->Forward || !I->aliasesUnknownInst(Inst, AA))
Chris Lattner6e1f5102010-08-29 04:06:55 +0000252 continue;
253
Chris Lattnercb7f6532010-08-29 18:42:23 +0000254 if (FoundSet == 0) // If this is the first alias set ptr can go into.
255 FoundSet = I; // Remember it.
256 else if (!I->Forward) // Otherwise, we must merge the sets.
Chris Lattner6e1f5102010-08-29 04:06:55 +0000257 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
Chris Lattner6e1f5102010-08-29 04:06:55 +0000258 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000259 return FoundSet;
260}
261
262
Chris Lattner009cc3d2002-09-26 21:49:07 +0000263
Chris Lattner9971ac42003-02-24 20:37:56 +0000264
265/// getAliasSetForPointer - Return the alias set that the specified pointer
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000266/// lives in.
Dan Gohman3da848b2010-10-19 22:54:46 +0000267AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, uint64_t Size,
Dan Gohmana8702ea2010-10-18 20:44:50 +0000268 const MDNode *TBAAInfo,
Chris Lattner12c11552004-07-21 05:18:04 +0000269 bool *New) {
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000270 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
Chris Lattner9971ac42003-02-24 20:37:56 +0000271
Chris Lattner9476d742010-08-29 04:13:43 +0000272 // Check to see if the pointer is already known.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000273 if (Entry.hasAliasSet()) {
Dan Gohmana8702ea2010-10-18 20:44:50 +0000274 Entry.updateSizeAndTBAAInfo(Size, TBAAInfo);
Chris Lattner9971ac42003-02-24 20:37:56 +0000275 // Return the set!
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000276 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000277 }
278
Dan Gohmana8702ea2010-10-18 20:44:50 +0000279 if (AliasSet *AS = findAliasSetForPointer(Pointer, Size, TBAAInfo)) {
Chris Lattner9476d742010-08-29 04:13:43 +0000280 // Add it to the alias set it aliases.
Dan Gohmana8702ea2010-10-18 20:44:50 +0000281 AS->addPointer(*this, Entry, Size, TBAAInfo);
Chris Lattner9971ac42003-02-24 20:37:56 +0000282 return *AS;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000283 }
Chris Lattner6e1f5102010-08-29 04:06:55 +0000284
285 if (New) *New = true;
Chris Lattner9476d742010-08-29 04:13:43 +0000286 // Otherwise create a new alias set to hold the loaded pointer.
Chris Lattner6e1f5102010-08-29 04:06:55 +0000287 AliasSets.push_back(new AliasSet());
Dan Gohmana8702ea2010-10-18 20:44:50 +0000288 AliasSets.back().addPointer(*this, Entry, Size, TBAAInfo);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000289 return AliasSets.back();
Chris Lattner009cc3d2002-09-26 21:49:07 +0000290}
291
Dan Gohman3da848b2010-10-19 22:54:46 +0000292bool AliasSetTracker::add(Value *Ptr, uint64_t Size, const MDNode *TBAAInfo) {
Chris Lattner61d46272004-07-26 05:50:23 +0000293 bool NewPtr;
Dan Gohmana8702ea2010-10-18 20:44:50 +0000294 addPointer(Ptr, Size, TBAAInfo, AliasSet::NoModRef, NewPtr);
Chris Lattner61d46272004-07-26 05:50:23 +0000295 return NewPtr;
296}
297
298
Chris Lattner12c11552004-07-21 05:18:04 +0000299bool AliasSetTracker::add(LoadInst *LI) {
Eli Friedman97671562011-08-15 20:52:09 +0000300 if (LI->getOrdering() > Monotonic) return addUnknown(LI);
301 AliasSet::AccessType ATy = AliasSet::Refs;
302 if (!LI->isUnordered()) ATy = AliasSet::ModRef;
Chris Lattner12c11552004-07-21 05:18:04 +0000303 bool NewPtr;
304 AliasSet &AS = addPointer(LI->getOperand(0),
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000305 AA.getTypeStoreSize(LI->getType()),
Dan Gohmana8702ea2010-10-18 20:44:50 +0000306 LI->getMetadata(LLVMContext::MD_tbaa),
Eli Friedman97671562011-08-15 20:52:09 +0000307 ATy, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000308 if (LI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000309 return NewPtr;
Chris Lattner9971ac42003-02-24 20:37:56 +0000310}
311
Chris Lattner12c11552004-07-21 05:18:04 +0000312bool AliasSetTracker::add(StoreInst *SI) {
Eli Friedman97671562011-08-15 20:52:09 +0000313 if (SI->getOrdering() > Monotonic) return addUnknown(SI);
314 AliasSet::AccessType ATy = AliasSet::Mods;
315 if (!SI->isUnordered()) ATy = AliasSet::ModRef;
Chris Lattner12c11552004-07-21 05:18:04 +0000316 bool NewPtr;
317 Value *Val = SI->getOperand(0);
318 AliasSet &AS = addPointer(SI->getOperand(1),
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000319 AA.getTypeStoreSize(Val->getType()),
Dan Gohmana8702ea2010-10-18 20:44:50 +0000320 SI->getMetadata(LLVMContext::MD_tbaa),
Eli Friedman97671562011-08-15 20:52:09 +0000321 ATy, NewPtr);
Chris Lattnere2609242003-12-14 04:52:11 +0000322 if (SI->isVolatile()) AS.setVolatile();
Chris Lattner12c11552004-07-21 05:18:04 +0000323 return NewPtr;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000324}
325
Dan Gohman235fc572008-04-14 18:34:50 +0000326bool AliasSetTracker::add(VAArgInst *VAAI) {
327 bool NewPtr;
Dan Gohmana8702ea2010-10-18 20:44:50 +0000328 addPointer(VAAI->getOperand(0), AliasAnalysis::UnknownSize,
329 VAAI->getMetadata(LLVMContext::MD_tbaa),
330 AliasSet::ModRef, NewPtr);
Dan Gohman235fc572008-04-14 18:34:50 +0000331 return NewPtr;
332}
333
Chris Lattnere2609242003-12-14 04:52:11 +0000334
Eli Friedman6f3ba372011-07-27 00:46:46 +0000335bool AliasSetTracker::addUnknown(Instruction *Inst) {
336 if (isa<DbgInfoIntrinsic>(Inst))
Zhou Sheng7e4286e2009-03-03 06:02:04 +0000337 return true; // Ignore DbgInfo Intrinsics.
Eli Friedman6f3ba372011-07-27 00:46:46 +0000338 if (!Inst->mayReadOrWriteMemory())
Duncan Sandsdff67102007-12-01 07:51:45 +0000339 return true; // doesn't alias anything
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000340
Eli Friedman6f3ba372011-07-27 00:46:46 +0000341 AliasSet *AS = findAliasSetForUnknownInst(Inst);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000342 if (AS) {
Eli Friedman6f3ba372011-07-27 00:46:46 +0000343 AS->addUnknownInst(Inst, AA);
Chris Lattner12c11552004-07-21 05:18:04 +0000344 return false;
Chris Lattner9971ac42003-02-24 20:37:56 +0000345 }
Chris Lattner6e1f5102010-08-29 04:06:55 +0000346 AliasSets.push_back(new AliasSet());
347 AS = &AliasSets.back();
Eli Friedman6f3ba372011-07-27 00:46:46 +0000348 AS->addUnknownInst(Inst, AA);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000349 return true;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000350}
351
Chris Lattner12c11552004-07-21 05:18:04 +0000352bool AliasSetTracker::add(Instruction *I) {
Chris Lattner6e1f5102010-08-29 04:06:55 +0000353 // Dispatch to one of the other add methods.
Chris Lattner9971ac42003-02-24 20:37:56 +0000354 if (LoadInst *LI = dyn_cast<LoadInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000355 return add(LI);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000356 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner12c11552004-07-21 05:18:04 +0000357 return add(SI);
Chris Lattner6e1f5102010-08-29 04:06:55 +0000358 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
Dan Gohman235fc572008-04-14 18:34:50 +0000359 return add(VAAI);
Eli Friedman6f3ba372011-07-27 00:46:46 +0000360 return addUnknown(I);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000361}
362
Chris Lattner319d05b2003-03-03 23:28:05 +0000363void AliasSetTracker::add(BasicBlock &BB) {
364 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
365 add(I);
366}
367
368void AliasSetTracker::add(const AliasSetTracker &AST) {
369 assert(&AA == &AST.AA &&
370 "Merging AliasSetTracker objects with different Alias Analyses!");
371
372 // Loop over all of the alias sets in AST, adding the pointers contained
373 // therein into the current alias sets. This can cause alias sets to be
374 // merged together in the current AST.
Chris Lattner6e1f5102010-08-29 04:06:55 +0000375 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I) {
376 if (I->Forward) continue; // Ignore forwarding alias sets
377
378 AliasSet &AS = const_cast<AliasSet&>(*I);
Chris Lattner319d05b2003-03-03 23:28:05 +0000379
Chris Lattner6e1f5102010-08-29 04:06:55 +0000380 // If there are any call sites in the alias set, add them to this AST.
Eli Friedman6f3ba372011-07-27 00:46:46 +0000381 for (unsigned i = 0, e = AS.UnknownInsts.size(); i != e; ++i)
382 add(AS.UnknownInsts[i]);
Chris Lattner319d05b2003-03-03 23:28:05 +0000383
Chris Lattner6e1f5102010-08-29 04:06:55 +0000384 // Loop over all of the pointers in this alias set.
385 bool X;
386 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) {
387 AliasSet &NewAS = addPointer(ASI.getPointer(), ASI.getSize(),
Dan Gohmaneee54002010-10-18 23:31:47 +0000388 ASI.getTBAAInfo(),
Chris Lattner6e1f5102010-08-29 04:06:55 +0000389 (AliasSet::AccessType)AS.AccessTy, X);
390 if (AS.isVolatile()) NewAS.setVolatile();
Chris Lattner319d05b2003-03-03 23:28:05 +0000391 }
Chris Lattner6e1f5102010-08-29 04:06:55 +0000392 }
Chris Lattner319d05b2003-03-03 23:28:05 +0000393}
394
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000395/// remove - Remove the specified (potentially non-empty) alias set from the
396/// tracker.
397void AliasSetTracker::remove(AliasSet &AS) {
Chris Lattnerf2998572006-06-27 23:48:59 +0000398 // Drop all call sites.
Eli Friedman6f3ba372011-07-27 00:46:46 +0000399 AS.UnknownInsts.clear();
Chris Lattnerf2998572006-06-27 23:48:59 +0000400
401 // Clear the alias set.
402 unsigned NumRefs = 0;
403 while (!AS.empty()) {
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000404 AliasSet::PointerRec *P = AS.PtrList;
405
406 Value *ValToRemove = P->getValue();
Chris Lattnerf2998572006-06-27 23:48:59 +0000407
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000408 // Unlink and delete entry from the list of values.
409 P->eraseFromList();
Chris Lattnerf2998572006-06-27 23:48:59 +0000410
411 // Remember how many references need to be dropped.
412 ++NumRefs;
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000413
Chris Lattnerf2998572006-06-27 23:48:59 +0000414 // Finally, remove the entry.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000415 PointerMap.erase(ValToRemove);
Chris Lattnerf2998572006-06-27 23:48:59 +0000416 }
417
418 // Stop using the alias set, removing it.
Chris Lattner356d8c22006-06-27 23:56:13 +0000419 AS.RefCount -= NumRefs;
420 if (AS.RefCount == 0)
421 AS.removeFromTracker(*this);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000422}
423
Dan Gohmana8702ea2010-10-18 20:44:50 +0000424bool
Dan Gohman3da848b2010-10-19 22:54:46 +0000425AliasSetTracker::remove(Value *Ptr, uint64_t Size, const MDNode *TBAAInfo) {
Dan Gohmana8702ea2010-10-18 20:44:50 +0000426 AliasSet *AS = findAliasSetForPointer(Ptr, Size, TBAAInfo);
Chris Lattner61d46272004-07-26 05:50:23 +0000427 if (!AS) return false;
428 remove(*AS);
429 return true;
430}
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000431
432bool AliasSetTracker::remove(LoadInst *LI) {
Dan Gohman3da848b2010-10-19 22:54:46 +0000433 uint64_t Size = AA.getTypeStoreSize(LI->getType());
Dan Gohmana8702ea2010-10-18 20:44:50 +0000434 const MDNode *TBAAInfo = LI->getMetadata(LLVMContext::MD_tbaa);
435 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size, TBAAInfo);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000436 if (!AS) return false;
437 remove(*AS);
438 return true;
439}
440
441bool AliasSetTracker::remove(StoreInst *SI) {
Dan Gohman3da848b2010-10-19 22:54:46 +0000442 uint64_t Size = AA.getTypeStoreSize(SI->getOperand(0)->getType());
Dan Gohmana8702ea2010-10-18 20:44:50 +0000443 const MDNode *TBAAInfo = SI->getMetadata(LLVMContext::MD_tbaa);
444 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size, TBAAInfo);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000445 if (!AS) return false;
446 remove(*AS);
447 return true;
448}
449
Dan Gohman235fc572008-04-14 18:34:50 +0000450bool AliasSetTracker::remove(VAArgInst *VAAI) {
Dan Gohmana8702ea2010-10-18 20:44:50 +0000451 AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0),
452 AliasAnalysis::UnknownSize,
453 VAAI->getMetadata(LLVMContext::MD_tbaa));
Dan Gohman235fc572008-04-14 18:34:50 +0000454 if (!AS) return false;
455 remove(*AS);
456 return true;
457}
458
Eli Friedman6f3ba372011-07-27 00:46:46 +0000459bool AliasSetTracker::removeUnknown(Instruction *I) {
460 if (!I->mayReadOrWriteMemory())
Duncan Sandsdff67102007-12-01 07:51:45 +0000461 return false; // doesn't alias anything
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000462
Eli Friedman6f3ba372011-07-27 00:46:46 +0000463 AliasSet *AS = findAliasSetForUnknownInst(I);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000464 if (!AS) return false;
465 remove(*AS);
466 return true;
467}
468
469bool AliasSetTracker::remove(Instruction *I) {
470 // Dispatch to one of the other remove methods...
471 if (LoadInst *LI = dyn_cast<LoadInst>(I))
472 return remove(LI);
Chris Lattner9476d742010-08-29 04:13:43 +0000473 if (StoreInst *SI = dyn_cast<StoreInst>(I))
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000474 return remove(SI);
Chris Lattner9476d742010-08-29 04:13:43 +0000475 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
Dan Gohman235fc572008-04-14 18:34:50 +0000476 return remove(VAAI);
Eli Friedman6f3ba372011-07-27 00:46:46 +0000477 return removeUnknown(I);
Chris Lattner6d4b0d72004-07-21 07:04:26 +0000478}
479
Chris Lattner2cffeec2003-12-18 08:11:56 +0000480
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000481// deleteValue method - This method is used to remove a pointer value from the
Chris Lattner2cffeec2003-12-18 08:11:56 +0000482// AliasSetTracker entirely. It should be used when an instruction is deleted
483// from the program to update the AST. If you don't use this, you would have
484// dangling pointers to deleted instructions.
485//
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000486void AliasSetTracker::deleteValue(Value *PtrVal) {
Chris Lattnerb66e6482004-09-14 19:15:32 +0000487 // Notify the alias analysis implementation that this value is gone.
488 AA.deleteValue(PtrVal);
489
Chris Lattner959e3212006-06-26 19:20:48 +0000490 // If this is a call instruction, remove the callsite from the appropriate
Chris Lattnercb7f6532010-08-29 18:42:23 +0000491 // AliasSet (if present).
Eli Friedman6f3ba372011-07-27 00:46:46 +0000492 if (Instruction *Inst = dyn_cast<Instruction>(PtrVal)) {
493 if (Inst->mayReadOrWriteMemory()) {
Chris Lattnercb7f6532010-08-29 18:42:23 +0000494 // Scan all the alias sets to see if this call site is contained.
495 for (iterator I = begin(), E = end(); I != E; ++I) {
496 if (I->Forward) continue;
497
Eli Friedman6f3ba372011-07-27 00:46:46 +0000498 I->removeUnknownInst(Inst);
Chris Lattnercb7f6532010-08-29 18:42:23 +0000499 }
500 }
501 }
Chris Lattner959e3212006-06-26 19:20:48 +0000502
Chris Lattnerb66e6482004-09-14 19:15:32 +0000503 // First, look up the PointerRec for this pointer.
Benjamin Kramer992c25a2012-06-30 22:37:15 +0000504 PointerMapType::iterator I = PointerMap.find_as(PtrVal);
Chris Lattner2cffeec2003-12-18 08:11:56 +0000505 if (I == PointerMap.end()) return; // Noop
506
507 // If we found one, remove the pointer from the alias set it is in.
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000508 AliasSet::PointerRec *PtrValEnt = I->second;
509 AliasSet *AS = PtrValEnt->getAliasSet(*this);
Chris Lattner2cffeec2003-12-18 08:11:56 +0000510
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000511 // Unlink and delete from the list of values.
512 PtrValEnt->eraseFromList();
513
514 // Stop using the alias set.
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000515 AS->dropRef(*this);
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000516
Chris Lattner2cffeec2003-12-18 08:11:56 +0000517 PointerMap.erase(I);
518}
519
Chris Lattnerb66e6482004-09-14 19:15:32 +0000520// copyValue - This method should be used whenever a preexisting value in the
521// program is copied or cloned, introducing a new value. Note that it is ok for
522// clients that use this method to introduce the same value multiple times: if
523// the tracker already knows about a value, it will ignore the request.
524//
525void AliasSetTracker::copyValue(Value *From, Value *To) {
526 // Notify the alias analysis implementation that this value is copied.
527 AA.copyValue(From, To);
528
529 // First, look up the PointerRec for this pointer.
Benjamin Kramer992c25a2012-06-30 22:37:15 +0000530 PointerMapType::iterator I = PointerMap.find_as(From);
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000531 if (I == PointerMap.end())
Chris Lattnerb66e6482004-09-14 19:15:32 +0000532 return; // Noop
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000533 assert(I->second->hasAliasSet() && "Dead entry?");
Chris Lattnerb66e6482004-09-14 19:15:32 +0000534
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000535 AliasSet::PointerRec &Entry = getEntryFor(To);
536 if (Entry.hasAliasSet()) return; // Already in the tracker!
Chris Lattnerb66e6482004-09-14 19:15:32 +0000537
538 // Add it to the alias set it aliases...
Benjamin Kramer992c25a2012-06-30 22:37:15 +0000539 I = PointerMap.find_as(From);
Chris Lattnerd7168dd2009-03-09 05:11:09 +0000540 AliasSet *AS = I->second->getAliasSet(*this);
Dan Gohmanfb8096d2010-10-18 21:28:00 +0000541 AS->addPointer(*this, Entry, I->second->getSize(),
Dan Gohmaneee54002010-10-18 23:31:47 +0000542 I->second->getTBAAInfo(),
Dan Gohmana8702ea2010-10-18 20:44:50 +0000543 true);
Chris Lattnerb66e6482004-09-14 19:15:32 +0000544}
545
546
Chris Lattner2cffeec2003-12-18 08:11:56 +0000547
Chris Lattner9971ac42003-02-24 20:37:56 +0000548//===----------------------------------------------------------------------===//
549// AliasSet/AliasSetTracker Printing Support
550//===----------------------------------------------------------------------===//
551
Chris Lattner791102f2009-08-23 05:17:37 +0000552void AliasSet::print(raw_ostream &OS) const {
Roman Divacky59324292012-09-05 22:26:57 +0000553 OS << " AliasSet[" << (const void*)this << ", " << RefCount << "] ";
Dan Gohman92c7b652008-04-21 19:48:48 +0000554 OS << (AliasTy == MustAlias ? "must" : "may") << " alias, ";
Chris Lattner9971ac42003-02-24 20:37:56 +0000555 switch (AccessTy) {
556 case NoModRef: OS << "No access "; break;
557 case Refs : OS << "Ref "; break;
558 case Mods : OS << "Mod "; break;
559 case ModRef : OS << "Mod/Ref "; break;
Torok Edwinc23197a2009-07-14 16:55:14 +0000560 default: llvm_unreachable("Bad value for AccessTy!");
Chris Lattner009cc3d2002-09-26 21:49:07 +0000561 }
Chris Lattnere2609242003-12-14 04:52:11 +0000562 if (isVolatile()) OS << "[volatile] ";
Chris Lattner9971ac42003-02-24 20:37:56 +0000563 if (Forward)
564 OS << " forwarding to " << (void*)Forward;
565
566
Dan Gohmancb406c22007-10-03 19:26:29 +0000567 if (!empty()) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000568 OS << "Pointers: ";
569 for (iterator I = begin(), E = end(); I != E; ++I) {
570 if (I != begin()) OS << ", ";
Chris Lattnerb8a31ac2004-07-22 07:58:18 +0000571 WriteAsOperand(OS << "(", I.getPointer());
572 OS << ", " << I.getSize() << ")";
Chris Lattner9971ac42003-02-24 20:37:56 +0000573 }
574 }
Eli Friedman6f3ba372011-07-27 00:46:46 +0000575 if (!UnknownInsts.empty()) {
576 OS << "\n " << UnknownInsts.size() << " Unknown instructions: ";
577 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000578 if (i) OS << ", ";
Eli Friedman6f3ba372011-07-27 00:46:46 +0000579 WriteAsOperand(OS, UnknownInsts[i]);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000580 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000581 }
582 OS << "\n";
583}
584
Chris Lattner791102f2009-08-23 05:17:37 +0000585void AliasSetTracker::print(raw_ostream &OS) const {
Chris Lattner9971ac42003-02-24 20:37:56 +0000586 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
587 << PointerMap.size() << " pointer values.\n";
588 for (const_iterator I = begin(), E = end(); I != E; ++I)
589 I->print(OS);
590 OS << "\n";
591}
592
David Greene43c48ac2009-12-23 19:27:59 +0000593void AliasSet::dump() const { print(dbgs()); }
594void AliasSetTracker::dump() const { print(dbgs()); }
Chris Lattner9971ac42003-02-24 20:37:56 +0000595
Chris Lattner9971ac42003-02-24 20:37:56 +0000596//===----------------------------------------------------------------------===//
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000597// ASTCallbackVH Class Implementation
598//===----------------------------------------------------------------------===//
599
600void AliasSetTracker::ASTCallbackVH::deleted() {
601 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
602 AST->deleteValue(getValPtr());
603 // this now dangles!
604}
605
Eli Friedman9055ccd2011-04-09 06:55:46 +0000606void AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(Value *V) {
607 AST->copyValue(getValPtr(), V);
608}
609
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000610AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
Dan Gohmana818c302009-07-31 18:21:48 +0000611 : CallbackVH(V), AST(ast) {}
612
613AliasSetTracker::ASTCallbackVH &
614AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
615 return *this = ASTCallbackVH(V, AST);
616}
Dan Gohmanb5b56ba2009-07-30 20:21:41 +0000617
618//===----------------------------------------------------------------------===//
Chris Lattner9971ac42003-02-24 20:37:56 +0000619// AliasSetPrinter Pass
620//===----------------------------------------------------------------------===//
621
622namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000623 class AliasSetPrinter : public FunctionPass {
Chris Lattner9971ac42003-02-24 20:37:56 +0000624 AliasSetTracker *Tracker;
625 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +0000626 static char ID; // Pass identification, replacement for typeid
Owen Anderson081c34b2010-10-19 17:21:58 +0000627 AliasSetPrinter() : FunctionPass(ID) {
628 initializeAliasSetPrinterPass(*PassRegistry::getPassRegistry());
629 }
Devang Patel794fd752007-05-01 21:15:47 +0000630
Chris Lattner9971ac42003-02-24 20:37:56 +0000631 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
632 AU.setPreservesAll();
633 AU.addRequired<AliasAnalysis>();
634 }
635
636 virtual bool runOnFunction(Function &F) {
637 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
638
639 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner6ffe5512004-04-27 15:13:33 +0000640 Tracker->add(&*I);
David Greene4850a892009-12-23 22:49:57 +0000641 Tracker->print(errs());
Chris Lattner4983cf72006-01-03 06:05:22 +0000642 delete Tracker;
Chris Lattner9971ac42003-02-24 20:37:56 +0000643 return false;
644 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000645 };
Chris Lattner009cc3d2002-09-26 21:49:07 +0000646}
Dan Gohman844731a2008-05-13 00:00:25 +0000647
648char AliasSetPrinter::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +0000649INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets",
650 "Alias Set Printer", false, true)
651INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
652INITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets",
Owen Andersonce665bd2010-10-07 22:25:06 +0000653 "Alias Set Printer", false, true)