blob: 190a5a975577e6bff3efe4e90507ec9881c43784 [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"
Reid Spencer954da372004-07-04 12:19:56 +000023#include <iostream>
Chris Lattnere2609242003-12-14 04:52:11 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattner009cc3d2002-09-26 21:49:07 +000026/// mergeSetIn - Merge the specified alias set into this alias set...
27///
Chris Lattner9971ac42003-02-24 20:37:56 +000028void AliasSet::mergeSetIn(AliasSet &AS) {
29 assert(!AS.Forward && "Alias set is already forwarding!");
30 assert(!Forward && "This set is a forwarding set!!");
Chris Lattner009cc3d2002-09-26 21:49:07 +000031
32 // Update the alias and access types of this set...
Chris Lattner9971ac42003-02-24 20:37:56 +000033 AccessTy |= AS.AccessTy;
34 AliasTy |= AS.AliasTy;
35
36 if (CallSites.empty()) { // Merge call sites...
37 if (!AS.CallSites.empty())
38 std::swap(CallSites, AS.CallSites);
39 } else if (!AS.CallSites.empty()) {
40 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
41 AS.CallSites.clear();
42 }
43
44 // FIXME: If AS's refcount is zero, nuke it now...
45 assert(RefCount != 0);
46
47 AS.Forward = this; // Forward across AS now...
48 RefCount++; // AS is now pointing to us...
49
50 // Merge the list of constituent pointers...
Chris Lattner2cffeec2003-12-18 08:11:56 +000051 if (AS.PtrList) {
52 *PtrListEnd = AS.PtrList;
53 AS.PtrList->second.setPrevInList(PtrListEnd);
54 PtrListEnd = AS.PtrListEnd;
55
56 AS.PtrList = 0;
57 AS.PtrListEnd = &AS.PtrList;
58 }
Chris Lattner009cc3d2002-09-26 21:49:07 +000059}
60
Chris Lattner9971ac42003-02-24 20:37:56 +000061void AliasSetTracker::removeAliasSet(AliasSet *AS) {
62 AliasSets.erase(AS);
63}
64
65void AliasSet::removeFromTracker(AliasSetTracker &AST) {
66 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
67 AST.removeAliasSet(this);
68}
69
Chris Lattner31a9d182003-02-26 22:11:00 +000070void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
71 unsigned Size) {
Chris Lattner9971ac42003-02-24 20:37:56 +000072 assert(!Entry.second.hasAliasSet() && "Entry already in set!");
73
74 AliasAnalysis &AA = AST.getAliasAnalysis();
75
76 if (isMustAlias()) // Check to see if we have to downgrade to _may_ alias
Chris Lattner31a9d182003-02-26 22:11:00 +000077 if (HashNodePair *P = getSomePointer()) {
78 AliasAnalysis::AliasResult Result =
79 AA.alias(P->first, P->second.getSize(), Entry.first, Size);
80 if (Result == AliasAnalysis::MayAlias)
Chris Lattner9971ac42003-02-24 20:37:56 +000081 AliasTy = MayAlias;
Chris Lattner31a9d182003-02-26 22:11:00 +000082 else // First entry of must alias must have maximum size!
83 P->second.updateSize(Size);
84 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
85 }
Chris Lattner9971ac42003-02-24 20:37:56 +000086
87 Entry.second.setAliasSet(this);
Chris Lattner31a9d182003-02-26 22:11:00 +000088 Entry.second.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +000089
90 // Add it to the end of the list...
Chris Lattner2cffeec2003-12-18 08:11:56 +000091 assert(*PtrListEnd == 0 && "End of list is not null?");
92 *PtrListEnd = &Entry;
93 PtrListEnd = Entry.second.setPrevInList(PtrListEnd);
Chris Lattner9971ac42003-02-24 20:37:56 +000094 RefCount++; // Entry points to alias set...
95}
96
Chris Lattner5b5f7c12004-03-15 04:08:36 +000097void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
Chris Lattner9971ac42003-02-24 20:37:56 +000098 CallSites.push_back(CS);
Chris Lattner5b5f7c12004-03-15 04:08:36 +000099
100 if (Function *F = CS.getCalledFunction()) {
101 if (AA.doesNotAccessMemory(F))
102 return;
103 else if (AA.onlyReadsMemory(F)) {
104 AliasTy = MayAlias;
Chris Lattner4781bc72004-03-17 23:22:04 +0000105 AccessTy |= Refs;
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000106 return;
107 }
108 }
109
110 // FIXME: This should use mod/ref information to make this not suck so bad
111 AliasTy = MayAlias;
Chris Lattner577385e2003-05-03 03:42:08 +0000112 AccessTy = ModRef;
Chris Lattner9971ac42003-02-24 20:37:56 +0000113}
114
115/// aliasesPointer - Return true if the specified pointer "may" (or must)
Chris Lattner009cc3d2002-09-26 21:49:07 +0000116/// alias one of the members in the set.
117///
Chris Lattner31a9d182003-02-26 22:11:00 +0000118bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
119 AliasAnalysis &AA) const {
Chris Lattner9971ac42003-02-24 20:37:56 +0000120 if (AliasTy == MustAlias) {
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000121 assert(CallSites.empty() && "Illegal must alias set!");
122
Chris Lattner9971ac42003-02-24 20:37:56 +0000123 // If this is a set of MustAliases, only check to see if the pointer aliases
124 // SOME value in the set...
Chris Lattner31a9d182003-02-26 22:11:00 +0000125 HashNodePair *SomePtr = getSomePointer();
Chris Lattner9971ac42003-02-24 20:37:56 +0000126 assert(SomePtr && "Empty must-alias set??");
Chris Lattner31a9d182003-02-26 22:11:00 +0000127 return AA.alias(SomePtr->first, SomePtr->second.getSize(), Ptr, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000128 }
129
130 // If this is a may-alias set, we have to check all of the pointers in the set
131 // to be sure it doesn't alias the set...
132 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner31a9d182003-02-26 22:11:00 +0000133 if (AA.alias(Ptr, Size, I->first, I->second.getSize()))
Chris Lattner9971ac42003-02-24 20:37:56 +0000134 return true;
135
136 // Check the call sites list and invoke list...
137 if (!CallSites.empty())
138 // FIXME: this is pessimistic!
Chris Lattner009cc3d2002-09-26 21:49:07 +0000139 return true;
Chris Lattner9971ac42003-02-24 20:37:56 +0000140
Chris Lattner009cc3d2002-09-26 21:49:07 +0000141 return false;
142}
143
Chris Lattner9971ac42003-02-24 20:37:56 +0000144bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000145 // FIXME: Use mod/ref information to prune this better!
146 if (Function *F = CS.getCalledFunction())
147 if (AA.doesNotAccessMemory(F))
148 return false;
149
Chris Lattner9971ac42003-02-24 20:37:56 +0000150 return true;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000151}
152
153
Chris Lattner009cc3d2002-09-26 21:49:07 +0000154/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
155/// instruction referring to the pointer into. If there are multiple alias sets
156/// that may alias the pointer, merge them together and return the unified set.
157///
Chris Lattner31a9d182003-02-26 22:11:00 +0000158AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
159 unsigned Size) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000160 AliasSet *FoundSet = 0;
Chris Lattner9971ac42003-02-24 20:37:56 +0000161 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner31a9d182003-02-26 22:11:00 +0000162 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000163 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
Chris Lattner9971ac42003-02-24 20:37:56 +0000164 FoundSet = I; // Remember it.
Chris Lattner009cc3d2002-09-26 21:49:07 +0000165 } else { // Otherwise, we must merge the sets...
Chris Lattner9971ac42003-02-24 20:37:56 +0000166 FoundSet->mergeSetIn(*I); // Merge in contents...
Chris Lattner009cc3d2002-09-26 21:49:07 +0000167 }
168 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000169
170 return FoundSet;
171}
172
173AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
174 AliasSet *FoundSet = 0;
175 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000176 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000177 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
178 FoundSet = I; // Remember it.
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000179 } else if (!I->Forward) { // Otherwise, we must merge the sets...
Chris Lattner9971ac42003-02-24 20:37:56 +0000180 FoundSet->mergeSetIn(*I); // Merge in contents...
181 }
182 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000183
184 return FoundSet;
185}
186
187
Chris Lattner009cc3d2002-09-26 21:49:07 +0000188
Chris Lattner9971ac42003-02-24 20:37:56 +0000189
190/// getAliasSetForPointer - Return the alias set that the specified pointer
191/// lives in...
Chris Lattner31a9d182003-02-26 22:11:00 +0000192AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size){
Chris Lattner9971ac42003-02-24 20:37:56 +0000193 AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
194
195 // Check to see if the pointer is already known...
Chris Lattner31a9d182003-02-26 22:11:00 +0000196 if (Entry.second.hasAliasSet() && Size <= Entry.second.getSize()) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000197 // Return the set!
198 return *Entry.second.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattner31a9d182003-02-26 22:11:00 +0000199 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000200 // Add it to the alias set it aliases...
Chris Lattner31a9d182003-02-26 22:11:00 +0000201 AS->addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000202 return *AS;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000203 } else {
Chris Lattner9971ac42003-02-24 20:37:56 +0000204 // Otherwise create a new alias set to hold the loaded pointer...
Chris Lattner009cc3d2002-09-26 21:49:07 +0000205 AliasSets.push_back(AliasSet());
Chris Lattner31a9d182003-02-26 22:11:00 +0000206 AliasSets.back().addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000207 return AliasSets.back();
Chris Lattner009cc3d2002-09-26 21:49:07 +0000208 }
209}
210
Chris Lattner9971ac42003-02-24 20:37:56 +0000211void AliasSetTracker::add(LoadInst *LI) {
Chris Lattnere2609242003-12-14 04:52:11 +0000212 AliasSet &AS =
213 addPointer(LI->getOperand(0),
214 AA.getTargetData().getTypeSize(LI->getType()), AliasSet::Refs);
215 if (LI->isVolatile()) AS.setVolatile();
Chris Lattner9971ac42003-02-24 20:37:56 +0000216}
217
Chris Lattner009cc3d2002-09-26 21:49:07 +0000218void AliasSetTracker::add(StoreInst *SI) {
Chris Lattnere2609242003-12-14 04:52:11 +0000219 AliasSet &AS =
220 addPointer(SI->getOperand(1),
221 AA.getTargetData().getTypeSize(SI->getOperand(0)->getType()),
222 AliasSet::Mods);
223 if (SI->isVolatile()) AS.setVolatile();
Chris Lattner009cc3d2002-09-26 21:49:07 +0000224}
225
Chris Lattnere2609242003-12-14 04:52:11 +0000226
Chris Lattner9971ac42003-02-24 20:37:56 +0000227void AliasSetTracker::add(CallSite CS) {
Chris Lattnerfcead4f2004-03-15 06:28:07 +0000228 if (Function *F = CS.getCalledFunction())
229 if (AA.doesNotAccessMemory(F))
230 return;
231
Chris Lattner9971ac42003-02-24 20:37:56 +0000232 AliasSet *AS = findAliasSetForCallSite(CS);
233 if (!AS) {
234 AliasSets.push_back(AliasSet());
235 AS = &AliasSets.back();
236 }
Chris Lattner5b5f7c12004-03-15 04:08:36 +0000237 AS->addCallSite(CS, AA);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000238}
239
Chris Lattner9971ac42003-02-24 20:37:56 +0000240void AliasSetTracker::add(Instruction *I) {
241 // Dispatch to one of the other add methods...
242 if (LoadInst *LI = dyn_cast<LoadInst>(I))
243 add(LI);
244 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
245 add(SI);
246 else if (CallInst *CI = dyn_cast<CallInst>(I))
247 add(CI);
248 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
249 add(II);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000250}
251
Chris Lattner319d05b2003-03-03 23:28:05 +0000252void AliasSetTracker::add(BasicBlock &BB) {
253 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
254 add(I);
255}
256
257void AliasSetTracker::add(const AliasSetTracker &AST) {
258 assert(&AA == &AST.AA &&
259 "Merging AliasSetTracker objects with different Alias Analyses!");
260
261 // Loop over all of the alias sets in AST, adding the pointers contained
262 // therein into the current alias sets. This can cause alias sets to be
263 // merged together in the current AST.
264 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
265 if (!I->Forward) { // Ignore forwarding alias sets
266 AliasSet &AS = const_cast<AliasSet&>(*I);
267
268 // If there are any call sites in the alias set, add them to this AST.
269 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
270 add(AS.CallSites[i]);
271
272 // Loop over all of the pointers in this alias set...
273 AliasSet::iterator I = AS.begin(), E = AS.end();
274 for (; I != E; ++I)
275 addPointer(I->first, I->second.getSize(),
276 (AliasSet::AccessType)AS.AccessTy);
277 }
278}
279
Chris Lattner2cffeec2003-12-18 08:11:56 +0000280
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000281// deleteValue method - This method is used to remove a pointer value from the
Chris Lattner2cffeec2003-12-18 08:11:56 +0000282// AliasSetTracker entirely. It should be used when an instruction is deleted
283// from the program to update the AST. If you don't use this, you would have
284// dangling pointers to deleted instructions.
285//
Chris Lattnerc43e0ae2004-05-23 21:10:58 +0000286void AliasSetTracker::deleteValue(Value *PtrVal) {
Chris Lattner2cffeec2003-12-18 08:11:56 +0000287 // First, look up the PointerRec for this pointer...
288 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(PtrVal);
289 if (I == PointerMap.end()) return; // Noop
290
291 // If we found one, remove the pointer from the alias set it is in.
292 AliasSet::HashNodePair &PtrValEnt = *I;
293 AliasSet *AS = PtrValEnt.second.getAliasSet(*this);
294
295 // Unlink from the list of values...
296 PtrValEnt.second.removeFromList();
297 // Stop using the alias set
298 if (--AS->RefCount == 0)
299 AS->removeFromTracker(*this);
300
301 PointerMap.erase(I);
302}
303
304
Chris Lattner9971ac42003-02-24 20:37:56 +0000305//===----------------------------------------------------------------------===//
306// AliasSet/AliasSetTracker Printing Support
307//===----------------------------------------------------------------------===//
308
309void AliasSet::print(std::ostream &OS) const {
310 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
311 OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, ";
312 switch (AccessTy) {
313 case NoModRef: OS << "No access "; break;
314 case Refs : OS << "Ref "; break;
315 case Mods : OS << "Mod "; break;
316 case ModRef : OS << "Mod/Ref "; break;
317 default: assert(0 && "Bad value for AccessTy!");
Chris Lattner009cc3d2002-09-26 21:49:07 +0000318 }
Chris Lattnere2609242003-12-14 04:52:11 +0000319 if (isVolatile()) OS << "[volatile] ";
Chris Lattner9971ac42003-02-24 20:37:56 +0000320 if (Forward)
321 OS << " forwarding to " << (void*)Forward;
322
323
324 if (begin() != end()) {
325 OS << "Pointers: ";
326 for (iterator I = begin(), E = end(); I != E; ++I) {
327 if (I != begin()) OS << ", ";
Chris Lattner31a9d182003-02-26 22:11:00 +0000328 WriteAsOperand(OS << "(", I->first);
329 OS << ", " << I->second.getSize() << ")";
Chris Lattner9971ac42003-02-24 20:37:56 +0000330 }
331 }
332 if (!CallSites.empty()) {
333 OS << "\n " << CallSites.size() << " Call Sites: ";
334 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
335 if (i) OS << ", ";
336 WriteAsOperand(OS, CallSites[i].getCalledValue());
337 }
338 }
339 OS << "\n";
340}
341
342void AliasSetTracker::print(std::ostream &OS) const {
343 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
344 << PointerMap.size() << " pointer values.\n";
345 for (const_iterator I = begin(), E = end(); I != E; ++I)
346 I->print(OS);
347 OS << "\n";
348}
349
350void AliasSet::dump() const { print (std::cerr); }
351void AliasSetTracker::dump() const { print(std::cerr); }
352
Chris Lattner9971ac42003-02-24 20:37:56 +0000353//===----------------------------------------------------------------------===//
354// AliasSetPrinter Pass
355//===----------------------------------------------------------------------===//
356
357namespace {
358 class AliasSetPrinter : public FunctionPass {
359 AliasSetTracker *Tracker;
360 public:
361 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
362 AU.setPreservesAll();
363 AU.addRequired<AliasAnalysis>();
364 }
365
366 virtual bool runOnFunction(Function &F) {
367 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
368
369 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Chris Lattner6ffe5512004-04-27 15:13:33 +0000370 Tracker->add(&*I);
Chris Lattner9971ac42003-02-24 20:37:56 +0000371 return false;
372 }
373
374 /// print - Convert to human readable form
375 virtual void print(std::ostream &OS) const {
376 Tracker->print(OS);
377 }
378
379 virtual void releaseMemory() {
380 delete Tracker;
381 }
382 };
383 RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer",
384 PassInfo::Analysis | PassInfo::Optimization);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000385}