blob: a3d58d90e9faf42ab3127ca304b3f8ca5b397e9e [file] [log] [blame]
Chris Lattner009cc3d2002-09-26 21:49:07 +00001//===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner009cc3d2002-09-26 21:49:07 +00009//
10// This file implements the AliasSetTracker and AliasSet classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Analysis/AliasSetTracker.h"
15#include "llvm/Analysis/AliasAnalysis.h"
16#include "llvm/iMemory.h"
17#include "llvm/iOther.h"
18#include "llvm/iTerminators.h"
Chris Lattner9971ac42003-02-24 20:37:56 +000019#include "llvm/Pass.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"
22#include "llvm/Support/InstIterator.h"
Chris Lattnere2609242003-12-14 04:52:11 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Chris Lattner009cc3d2002-09-26 21:49:07 +000025/// mergeSetIn - Merge the specified alias set into this alias set...
26///
Chris Lattner9971ac42003-02-24 20:37:56 +000027void AliasSet::mergeSetIn(AliasSet &AS) {
28 assert(!AS.Forward && "Alias set is already forwarding!");
29 assert(!Forward && "This set is a forwarding set!!");
Chris Lattner009cc3d2002-09-26 21:49:07 +000030
31 // Update the alias and access types of this set...
Chris Lattner9971ac42003-02-24 20:37:56 +000032 AccessTy |= AS.AccessTy;
33 AliasTy |= AS.AliasTy;
34
35 if (CallSites.empty()) { // Merge call sites...
36 if (!AS.CallSites.empty())
37 std::swap(CallSites, AS.CallSites);
38 } else if (!AS.CallSites.empty()) {
39 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
40 AS.CallSites.clear();
41 }
42
43 // FIXME: If AS's refcount is zero, nuke it now...
44 assert(RefCount != 0);
45
46 AS.Forward = this; // Forward across AS now...
47 RefCount++; // AS is now pointing to us...
48
49 // Merge the list of constituent pointers...
50 PtrListTail->second.setTail(AS.PtrListHead);
51 PtrListTail = AS.PtrListTail;
52 AS.PtrListHead = AS.PtrListTail = 0;
Chris Lattner009cc3d2002-09-26 21:49:07 +000053}
54
Chris Lattner9971ac42003-02-24 20:37:56 +000055void AliasSetTracker::removeAliasSet(AliasSet *AS) {
56 AliasSets.erase(AS);
57}
58
59void AliasSet::removeFromTracker(AliasSetTracker &AST) {
60 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
61 AST.removeAliasSet(this);
62}
63
Chris Lattner31a9d182003-02-26 22:11:00 +000064void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
65 unsigned Size) {
Chris Lattner9971ac42003-02-24 20:37:56 +000066 assert(!Entry.second.hasAliasSet() && "Entry already in set!");
67
68 AliasAnalysis &AA = AST.getAliasAnalysis();
69
70 if (isMustAlias()) // Check to see if we have to downgrade to _may_ alias
Chris Lattner31a9d182003-02-26 22:11:00 +000071 if (HashNodePair *P = getSomePointer()) {
72 AliasAnalysis::AliasResult Result =
73 AA.alias(P->first, P->second.getSize(), Entry.first, Size);
74 if (Result == AliasAnalysis::MayAlias)
Chris Lattner9971ac42003-02-24 20:37:56 +000075 AliasTy = MayAlias;
Chris Lattner31a9d182003-02-26 22:11:00 +000076 else // First entry of must alias must have maximum size!
77 P->second.updateSize(Size);
78 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
79 }
Chris Lattner9971ac42003-02-24 20:37:56 +000080
81 Entry.second.setAliasSet(this);
Chris Lattner31a9d182003-02-26 22:11:00 +000082 Entry.second.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +000083
84 // Add it to the end of the list...
85 if (PtrListTail)
86 PtrListTail->second.setTail(&Entry);
87 else
88 PtrListHead = &Entry;
89 PtrListTail = &Entry;
90 RefCount++; // Entry points to alias set...
91}
92
93void AliasSet::addCallSite(CallSite CS) {
94 CallSites.push_back(CS);
Chris Lattner31a9d182003-02-26 22:11:00 +000095 AliasTy = MayAlias; // FIXME: Too conservative?
Chris Lattner577385e2003-05-03 03:42:08 +000096 AccessTy = ModRef;
Chris Lattner9971ac42003-02-24 20:37:56 +000097}
98
99/// aliasesPointer - Return true if the specified pointer "may" (or must)
Chris Lattner009cc3d2002-09-26 21:49:07 +0000100/// alias one of the members in the set.
101///
Chris Lattner31a9d182003-02-26 22:11:00 +0000102bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
103 AliasAnalysis &AA) const {
Chris Lattner9971ac42003-02-24 20:37:56 +0000104 if (AliasTy == MustAlias) {
105 assert(CallSites.empty() && "Illegal must alias set!");
106
107 // If this is a set of MustAliases, only check to see if the pointer aliases
108 // SOME value in the set...
Chris Lattner31a9d182003-02-26 22:11:00 +0000109 HashNodePair *SomePtr = getSomePointer();
Chris Lattner9971ac42003-02-24 20:37:56 +0000110 assert(SomePtr && "Empty must-alias set??");
Chris Lattner31a9d182003-02-26 22:11:00 +0000111 return AA.alias(SomePtr->first, SomePtr->second.getSize(), Ptr, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000112 }
113
114 // If this is a may-alias set, we have to check all of the pointers in the set
115 // to be sure it doesn't alias the set...
116 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner31a9d182003-02-26 22:11:00 +0000117 if (AA.alias(Ptr, Size, I->first, I->second.getSize()))
Chris Lattner9971ac42003-02-24 20:37:56 +0000118 return true;
119
120 // Check the call sites list and invoke list...
121 if (!CallSites.empty())
122 // FIXME: this is pessimistic!
Chris Lattner009cc3d2002-09-26 21:49:07 +0000123 return true;
Chris Lattner9971ac42003-02-24 20:37:56 +0000124
Chris Lattner009cc3d2002-09-26 21:49:07 +0000125 return false;
126}
127
Chris Lattner9971ac42003-02-24 20:37:56 +0000128bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
129 // FIXME: Too conservative!
130 return true;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000131}
132
133
Chris Lattner009cc3d2002-09-26 21:49:07 +0000134/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
135/// instruction referring to the pointer into. If there are multiple alias sets
136/// that may alias the pointer, merge them together and return the unified set.
137///
Chris Lattner31a9d182003-02-26 22:11:00 +0000138AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
139 unsigned Size) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000140 AliasSet *FoundSet = 0;
Chris Lattner9971ac42003-02-24 20:37:56 +0000141 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner31a9d182003-02-26 22:11:00 +0000142 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000143 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
Chris Lattner9971ac42003-02-24 20:37:56 +0000144 FoundSet = I; // Remember it.
Chris Lattner009cc3d2002-09-26 21:49:07 +0000145 } else { // Otherwise, we must merge the sets...
Chris Lattner9971ac42003-02-24 20:37:56 +0000146 FoundSet->mergeSetIn(*I); // Merge in contents...
Chris Lattner009cc3d2002-09-26 21:49:07 +0000147 }
148 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000149
150 return FoundSet;
151}
152
153AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
154 AliasSet *FoundSet = 0;
155 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000156 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000157 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
158 FoundSet = I; // Remember it.
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000159 } else if (!I->Forward) { // Otherwise, we must merge the sets...
Chris Lattner9971ac42003-02-24 20:37:56 +0000160 FoundSet->mergeSetIn(*I); // Merge in contents...
161 }
162 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000163
164 return FoundSet;
165}
166
167
Chris Lattner009cc3d2002-09-26 21:49:07 +0000168
Chris Lattner9971ac42003-02-24 20:37:56 +0000169
170/// getAliasSetForPointer - Return the alias set that the specified pointer
171/// lives in...
Chris Lattner31a9d182003-02-26 22:11:00 +0000172AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size){
Chris Lattner9971ac42003-02-24 20:37:56 +0000173 AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
174
175 // Check to see if the pointer is already known...
Chris Lattner31a9d182003-02-26 22:11:00 +0000176 if (Entry.second.hasAliasSet() && Size <= Entry.second.getSize()) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000177 // Return the set!
178 return *Entry.second.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattner31a9d182003-02-26 22:11:00 +0000179 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000180 // Add it to the alias set it aliases...
Chris Lattner31a9d182003-02-26 22:11:00 +0000181 AS->addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000182 return *AS;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000183 } else {
Chris Lattner9971ac42003-02-24 20:37:56 +0000184 // Otherwise create a new alias set to hold the loaded pointer...
Chris Lattner009cc3d2002-09-26 21:49:07 +0000185 AliasSets.push_back(AliasSet());
Chris Lattner31a9d182003-02-26 22:11:00 +0000186 AliasSets.back().addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000187 return AliasSets.back();
Chris Lattner009cc3d2002-09-26 21:49:07 +0000188 }
189}
190
Chris Lattner9971ac42003-02-24 20:37:56 +0000191void AliasSetTracker::add(LoadInst *LI) {
Chris Lattnere2609242003-12-14 04:52:11 +0000192 AliasSet &AS =
193 addPointer(LI->getOperand(0),
194 AA.getTargetData().getTypeSize(LI->getType()), AliasSet::Refs);
195 if (LI->isVolatile()) AS.setVolatile();
Chris Lattner9971ac42003-02-24 20:37:56 +0000196}
197
Chris Lattner009cc3d2002-09-26 21:49:07 +0000198void AliasSetTracker::add(StoreInst *SI) {
Chris Lattnere2609242003-12-14 04:52:11 +0000199 AliasSet &AS =
200 addPointer(SI->getOperand(1),
201 AA.getTargetData().getTypeSize(SI->getOperand(0)->getType()),
202 AliasSet::Mods);
203 if (SI->isVolatile()) AS.setVolatile();
Chris Lattner009cc3d2002-09-26 21:49:07 +0000204}
205
Chris Lattnere2609242003-12-14 04:52:11 +0000206
Chris Lattner9971ac42003-02-24 20:37:56 +0000207void AliasSetTracker::add(CallSite CS) {
208 AliasSet *AS = findAliasSetForCallSite(CS);
209 if (!AS) {
210 AliasSets.push_back(AliasSet());
211 AS = &AliasSets.back();
212 }
213 AS->addCallSite(CS);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000214}
215
Chris Lattner9971ac42003-02-24 20:37:56 +0000216void AliasSetTracker::add(Instruction *I) {
217 // Dispatch to one of the other add methods...
218 if (LoadInst *LI = dyn_cast<LoadInst>(I))
219 add(LI);
220 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
221 add(SI);
222 else if (CallInst *CI = dyn_cast<CallInst>(I))
223 add(CI);
224 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
225 add(II);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000226}
227
Chris Lattner319d05b2003-03-03 23:28:05 +0000228void AliasSetTracker::add(BasicBlock &BB) {
229 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
230 add(I);
231}
232
233void AliasSetTracker::add(const AliasSetTracker &AST) {
234 assert(&AA == &AST.AA &&
235 "Merging AliasSetTracker objects with different Alias Analyses!");
236
237 // Loop over all of the alias sets in AST, adding the pointers contained
238 // therein into the current alias sets. This can cause alias sets to be
239 // merged together in the current AST.
240 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
241 if (!I->Forward) { // Ignore forwarding alias sets
242 AliasSet &AS = const_cast<AliasSet&>(*I);
243
244 // If there are any call sites in the alias set, add them to this AST.
245 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
246 add(AS.CallSites[i]);
247
248 // Loop over all of the pointers in this alias set...
249 AliasSet::iterator I = AS.begin(), E = AS.end();
250 for (; I != E; ++I)
251 addPointer(I->first, I->second.getSize(),
252 (AliasSet::AccessType)AS.AccessTy);
253 }
254}
255
Chris Lattner9971ac42003-02-24 20:37:56 +0000256//===----------------------------------------------------------------------===//
257// AliasSet/AliasSetTracker Printing Support
258//===----------------------------------------------------------------------===//
259
260void AliasSet::print(std::ostream &OS) const {
261 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
262 OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, ";
263 switch (AccessTy) {
264 case NoModRef: OS << "No access "; break;
265 case Refs : OS << "Ref "; break;
266 case Mods : OS << "Mod "; break;
267 case ModRef : OS << "Mod/Ref "; break;
268 default: assert(0 && "Bad value for AccessTy!");
Chris Lattner009cc3d2002-09-26 21:49:07 +0000269 }
Chris Lattnere2609242003-12-14 04:52:11 +0000270 if (isVolatile()) OS << "[volatile] ";
Chris Lattner9971ac42003-02-24 20:37:56 +0000271 if (Forward)
272 OS << " forwarding to " << (void*)Forward;
273
274
275 if (begin() != end()) {
276 OS << "Pointers: ";
277 for (iterator I = begin(), E = end(); I != E; ++I) {
278 if (I != begin()) OS << ", ";
Chris Lattner31a9d182003-02-26 22:11:00 +0000279 WriteAsOperand(OS << "(", I->first);
280 OS << ", " << I->second.getSize() << ")";
Chris Lattner9971ac42003-02-24 20:37:56 +0000281 }
282 }
283 if (!CallSites.empty()) {
284 OS << "\n " << CallSites.size() << " Call Sites: ";
285 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
286 if (i) OS << ", ";
287 WriteAsOperand(OS, CallSites[i].getCalledValue());
288 }
289 }
290 OS << "\n";
291}
292
293void AliasSetTracker::print(std::ostream &OS) const {
294 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
295 << PointerMap.size() << " pointer values.\n";
296 for (const_iterator I = begin(), E = end(); I != E; ++I)
297 I->print(OS);
298 OS << "\n";
299}
300
301void AliasSet::dump() const { print (std::cerr); }
302void AliasSetTracker::dump() const { print(std::cerr); }
303
Chris Lattner9971ac42003-02-24 20:37:56 +0000304//===----------------------------------------------------------------------===//
305// AliasSetPrinter Pass
306//===----------------------------------------------------------------------===//
307
308namespace {
309 class AliasSetPrinter : public FunctionPass {
310 AliasSetTracker *Tracker;
311 public:
312 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
313 AU.setPreservesAll();
314 AU.addRequired<AliasAnalysis>();
315 }
316
317 virtual bool runOnFunction(Function &F) {
318 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
319
320 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
321 Tracker->add(*I);
322 return false;
323 }
324
325 /// print - Convert to human readable form
326 virtual void print(std::ostream &OS) const {
327 Tracker->print(OS);
328 }
329
330 virtual void releaseMemory() {
331 delete Tracker;
332 }
333 };
334 RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer",
335 PassInfo::Analysis | PassInfo::Optimization);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000336}