blob: fcdd1b3399961d7f441cdb99ca6057a79f6c9fdc [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
2//
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//===----------------------------------------------------------------------===//
9//
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/Instructions.h"
17#include "llvm/Pass.h"
18#include "llvm/Type.h"
19#include "llvm/Target/TargetData.h"
20#include "llvm/Assembly/Writer.h"
21#include "llvm/Support/Compiler.h"
22#include "llvm/Support/InstIterator.h"
23#include "llvm/Support/Streams.h"
24using namespace llvm;
25
26/// mergeSetIn - Merge the specified alias set into this alias set.
27///
28void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
29 assert(!AS.Forward && "Alias set is already forwarding!");
30 assert(!Forward && "This set is a forwarding set!!");
31
32 // Update the alias and access types of this set...
33 AccessTy |= AS.AccessTy;
34 AliasTy |= AS.AliasTy;
35
36 if (AliasTy == MustAlias) {
37 // Check that these two merged sets really are must aliases. Since both
38 // used to be must-alias sets, we can just check any pointer from each set
39 // for aliasing.
40 AliasAnalysis &AA = AST.getAliasAnalysis();
41 HashNodePair *L = getSomePointer();
42 HashNodePair *R = AS.getSomePointer();
43
44 // If the pointers are not a must-alias pair, this set becomes a may alias.
45 if (AA.alias(L->first, L->second.getSize(), R->first, R->second.getSize())
46 != AliasAnalysis::MustAlias)
47 AliasTy = MayAlias;
48 }
49
50 if (CallSites.empty()) { // Merge call sites...
51 if (!AS.CallSites.empty())
52 std::swap(CallSites, AS.CallSites);
53 } else if (!AS.CallSites.empty()) {
54 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
55 AS.CallSites.clear();
56 }
57
58 AS.Forward = this; // Forward across AS now...
59 addRef(); // AS is now pointing to us...
60
61 // Merge the list of constituent pointers...
62 if (AS.PtrList) {
63 *PtrListEnd = AS.PtrList;
64 AS.PtrList->second.setPrevInList(PtrListEnd);
65 PtrListEnd = AS.PtrListEnd;
66
67 AS.PtrList = 0;
68 AS.PtrListEnd = &AS.PtrList;
69 assert(*AS.PtrListEnd == 0 && "End of list is not null?");
70 }
71}
72
73void AliasSetTracker::removeAliasSet(AliasSet *AS) {
74 if (AliasSet *Fwd = AS->Forward) {
75 Fwd->dropRef(*this);
76 AS->Forward = 0;
77 }
78 AliasSets.erase(AS);
79}
80
81void AliasSet::removeFromTracker(AliasSetTracker &AST) {
82 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
83 AST.removeAliasSet(this);
84}
85
86void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
87 unsigned Size, bool KnownMustAlias) {
88 assert(!Entry.second.hasAliasSet() && "Entry already in set!");
89
90 // Check to see if we have to downgrade to _may_ alias.
91 if (isMustAlias() && !KnownMustAlias)
92 if (HashNodePair *P = getSomePointer()) {
93 AliasAnalysis &AA = AST.getAliasAnalysis();
94 AliasAnalysis::AliasResult Result =
95 AA.alias(P->first, P->second.getSize(), Entry.first, Size);
96 if (Result == AliasAnalysis::MayAlias)
97 AliasTy = MayAlias;
98 else // First entry of must alias must have maximum size!
99 P->second.updateSize(Size);
100 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
101 }
102
103 Entry.second.setAliasSet(this);
104 Entry.second.updateSize(Size);
105
106 // Add it to the end of the list...
107 assert(*PtrListEnd == 0 && "End of list is not null?");
108 *PtrListEnd = &Entry;
109 PtrListEnd = Entry.second.setPrevInList(PtrListEnd);
110 assert(*PtrListEnd == 0 && "End of list is not null?");
111 addRef(); // Entry points to alias set...
112}
113
114void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
115 CallSites.push_back(CS);
116
117 if (Function *F = CS.getCalledFunction()) {
118 AliasAnalysis::ModRefBehavior Behavior = AA.getModRefBehavior(F, CS);
119 if (Behavior == AliasAnalysis::DoesNotAccessMemory)
120 return;
121 else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
122 AliasTy = MayAlias;
123 AccessTy |= Refs;
124 return;
125 }
126 }
127
128 // FIXME: This should use mod/ref information to make this not suck so bad
129 AliasTy = MayAlias;
130 AccessTy = ModRef;
131}
132
133/// aliasesPointer - Return true if the specified pointer "may" (or must)
134/// alias one of the members in the set.
135///
136bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
137 AliasAnalysis &AA) const {
138 if (AliasTy == MustAlias) {
139 assert(CallSites.empty() && "Illegal must alias set!");
140
141 // If this is a set of MustAliases, only check to see if the pointer aliases
142 // SOME value in the set...
143 HashNodePair *SomePtr = getSomePointer();
144 assert(SomePtr && "Empty must-alias set??");
145 return AA.alias(SomePtr->first, SomePtr->second.getSize(), Ptr, Size);
146 }
147
148 // If this is a may-alias set, we have to check all of the pointers in the set
149 // to be sure it doesn't alias the set...
150 for (iterator I = begin(), E = end(); I != E; ++I)
151 if (AA.alias(Ptr, Size, I.getPointer(), I.getSize()))
152 return true;
153
154 // Check the call sites list and invoke list...
155 if (!CallSites.empty()) {
156 if (AA.hasNoModRefInfoForCalls())
157 return true;
158
159 for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
160 if (AA.getModRefInfo(CallSites[i], const_cast<Value*>(Ptr), Size)
161 != AliasAnalysis::NoModRef)
162 return true;
163 }
164
165 return false;
166}
167
168bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
169 if (Function *F = CS.getCalledFunction())
170 if (AA.doesNotAccessMemory(F))
171 return false;
172
173 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;
187}
188
189
190/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
191/// instruction referring to the pointer into. If there are multiple alias sets
192/// that may alias the pointer, merge them together and return the unified set.
193///
194AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
195 unsigned Size) {
196 AliasSet *FoundSet = 0;
197 for (iterator I = begin(), E = end(); I != E; ++I)
198 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
199 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
200 FoundSet = I; // Remember it.
201 } else { // Otherwise, we must merge the sets.
202 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
203 }
204 }
205
206 return FoundSet;
207}
208
209/// containsPointer - Return true if the specified location is represented by
210/// this alias set, false otherwise. This does not modify the AST object or
211/// alias sets.
212bool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const {
213 for (const_iterator I = begin(), E = end(); I != E; ++I)
214 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA))
215 return true;
216 return false;
217}
218
219
220
221AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
222 AliasSet *FoundSet = 0;
223 for (iterator I = begin(), E = end(); I != E; ++I)
224 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
225 if (FoundSet == 0) { // If this is the first alias set ptr can go into.
226 FoundSet = I; // Remember it.
227 } else if (!I->Forward) { // Otherwise, we must merge the sets.
228 FoundSet->mergeSetIn(*I, *this); // Merge in contents.
229 }
230 }
231
232 return FoundSet;
233}
234
235
236
237
238/// getAliasSetForPointer - Return the alias set that the specified pointer
239/// lives in...
240AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
241 bool *New) {
242 AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
243
244 // Check to see if the pointer is already known...
245 if (Entry.second.hasAliasSet()) {
246 Entry.second.updateSize(Size);
247 // Return the set!
248 return *Entry.second.getAliasSet(*this)->getForwardedTarget(*this);
249 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
250 // Add it to the alias set it aliases...
251 AS->addPointer(*this, Entry, Size);
252 return *AS;
253 } else {
254 if (New) *New = true;
255 // Otherwise create a new alias set to hold the loaded pointer...
256 AliasSets.push_back(new AliasSet());
257 AliasSets.back().addPointer(*this, Entry, Size);
258 return AliasSets.back();
259 }
260}
261
262bool AliasSetTracker::add(Value *Ptr, unsigned Size) {
263 bool NewPtr;
264 addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
265 return NewPtr;
266}
267
268
269bool AliasSetTracker::add(LoadInst *LI) {
270 bool NewPtr;
271 AliasSet &AS = addPointer(LI->getOperand(0),
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000272 AA.getTargetData().getTypeStoreSize(LI->getType()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273 AliasSet::Refs, NewPtr);
274 if (LI->isVolatile()) AS.setVolatile();
275 return NewPtr;
276}
277
278bool AliasSetTracker::add(StoreInst *SI) {
279 bool NewPtr;
280 Value *Val = SI->getOperand(0);
281 AliasSet &AS = addPointer(SI->getOperand(1),
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000282 AA.getTargetData().getTypeStoreSize(Val->getType()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 AliasSet::Mods, NewPtr);
284 if (SI->isVolatile()) AS.setVolatile();
285 return NewPtr;
286}
287
288bool AliasSetTracker::add(FreeInst *FI) {
289 bool NewPtr;
290 AliasSet &AS = addPointer(FI->getOperand(0), ~0,
291 AliasSet::Mods, NewPtr);
292
293 // Free operations are volatile ops (cannot be moved).
294 AS.setVolatile();
295 return NewPtr;
296}
297
298
299bool AliasSetTracker::add(CallSite CS) {
300 if (Function *F = CS.getCalledFunction())
301 if (AA.doesNotAccessMemory(F))
302 return true; // doesn't alias anything
303
304 AliasSet *AS = findAliasSetForCallSite(CS);
305 if (!AS) {
306 AliasSets.push_back(new AliasSet());
307 AS = &AliasSets.back();
308 AS->addCallSite(CS, AA);
309 return true;
310 } else {
311 AS->addCallSite(CS, AA);
312 return false;
313 }
314}
315
316bool AliasSetTracker::add(Instruction *I) {
317 // Dispatch to one of the other add methods...
318 if (LoadInst *LI = dyn_cast<LoadInst>(I))
319 return add(LI);
320 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
321 return add(SI);
322 else if (CallInst *CI = dyn_cast<CallInst>(I))
323 return add(CI);
324 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
325 return add(II);
326 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
327 return add(FI);
328 return true;
329}
330
331void AliasSetTracker::add(BasicBlock &BB) {
332 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
333 add(I);
334}
335
336void AliasSetTracker::add(const AliasSetTracker &AST) {
337 assert(&AA == &AST.AA &&
338 "Merging AliasSetTracker objects with different Alias Analyses!");
339
340 // Loop over all of the alias sets in AST, adding the pointers contained
341 // therein into the current alias sets. This can cause alias sets to be
342 // merged together in the current AST.
343 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
344 if (!I->Forward) { // Ignore forwarding alias sets
345 AliasSet &AS = const_cast<AliasSet&>(*I);
346
347 // If there are any call sites in the alias set, add them to this AST.
348 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
349 add(AS.CallSites[i]);
350
351 // Loop over all of the pointers in this alias set...
352 AliasSet::iterator I = AS.begin(), E = AS.end();
353 bool X;
354 for (; I != E; ++I) {
355 AliasSet &NewAS = addPointer(I.getPointer(), I.getSize(),
356 (AliasSet::AccessType)AS.AccessTy, X);
357 if (AS.isVolatile()) NewAS.setVolatile();
358 }
359 }
360}
361
362/// remove - Remove the specified (potentially non-empty) alias set from the
363/// tracker.
364void AliasSetTracker::remove(AliasSet &AS) {
365 // Drop all call sites.
366 AS.CallSites.clear();
367
368 // Clear the alias set.
369 unsigned NumRefs = 0;
370 while (!AS.empty()) {
371 AliasSet::HashNodePair *P = AS.PtrList;
372
373 // Unlink from the list of values.
374 P->second.removeFromList();
375
376 // Remember how many references need to be dropped.
377 ++NumRefs;
378
379 // Finally, remove the entry.
380 Value *Remove = P->first; // Take a copy because it is invalid to pass
381 PointerMap.erase(Remove); // a reference to the data being erased.
382 }
383
384 // Stop using the alias set, removing it.
385 AS.RefCount -= NumRefs;
386 if (AS.RefCount == 0)
387 AS.removeFromTracker(*this);
388}
389
390bool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
391 AliasSet *AS = findAliasSetForPointer(Ptr, Size);
392 if (!AS) return false;
393 remove(*AS);
394 return true;
395}
396
397bool AliasSetTracker::remove(LoadInst *LI) {
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000398 unsigned Size = AA.getTargetData().getTypeStoreSize(LI->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399 AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
400 if (!AS) return false;
401 remove(*AS);
402 return true;
403}
404
405bool AliasSetTracker::remove(StoreInst *SI) {
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000406 unsigned Size =
407 AA.getTargetData().getTypeStoreSize(SI->getOperand(0)->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000408 AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
409 if (!AS) return false;
410 remove(*AS);
411 return true;
412}
413
414bool AliasSetTracker::remove(FreeInst *FI) {
415 AliasSet *AS = findAliasSetForPointer(FI->getOperand(0), ~0);
416 if (!AS) return false;
417 remove(*AS);
418 return true;
419}
420
421bool AliasSetTracker::remove(CallSite CS) {
422 if (Function *F = CS.getCalledFunction())
423 if (AA.doesNotAccessMemory(F))
424 return false; // doesn't alias anything
425
426 AliasSet *AS = findAliasSetForCallSite(CS);
427 if (!AS) return false;
428 remove(*AS);
429 return true;
430}
431
432bool AliasSetTracker::remove(Instruction *I) {
433 // Dispatch to one of the other remove methods...
434 if (LoadInst *LI = dyn_cast<LoadInst>(I))
435 return remove(LI);
436 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
437 return remove(SI);
438 else if (CallInst *CI = dyn_cast<CallInst>(I))
439 return remove(CI);
440 else if (FreeInst *FI = dyn_cast<FreeInst>(I))
441 return remove(FI);
442 return true;
443}
444
445
446// deleteValue method - This method is used to remove a pointer value from the
447// AliasSetTracker entirely. It should be used when an instruction is deleted
448// from the program to update the AST. If you don't use this, you would have
449// dangling pointers to deleted instructions.
450//
451void AliasSetTracker::deleteValue(Value *PtrVal) {
452 // Notify the alias analysis implementation that this value is gone.
453 AA.deleteValue(PtrVal);
454
455 // If this is a call instruction, remove the callsite from the appropriate
456 // AliasSet.
457 CallSite CS = CallSite::get(PtrVal);
458 if (CS.getInstruction()) {
459 Function *F = CS.getCalledFunction();
460 if (!F || !AA.doesNotAccessMemory(F)) {
461 if (AliasSet *AS = findAliasSetForCallSite(CS))
462 AS->removeCallSite(CS);
463 }
464 }
465
466 // First, look up the PointerRec for this pointer.
467 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(PtrVal);
468 if (I == PointerMap.end()) return; // Noop
469
470 // If we found one, remove the pointer from the alias set it is in.
471 AliasSet::HashNodePair &PtrValEnt = *I;
472 AliasSet *AS = PtrValEnt.second.getAliasSet(*this);
473
474 // Unlink from the list of values...
475 PtrValEnt.second.removeFromList();
476 // Stop using the alias set
477 AS->dropRef(*this);
478 PointerMap.erase(I);
479}
480
481// copyValue - This method should be used whenever a preexisting value in the
482// program is copied or cloned, introducing a new value. Note that it is ok for
483// clients that use this method to introduce the same value multiple times: if
484// the tracker already knows about a value, it will ignore the request.
485//
486void AliasSetTracker::copyValue(Value *From, Value *To) {
487 // Notify the alias analysis implementation that this value is copied.
488 AA.copyValue(From, To);
489
490 // First, look up the PointerRec for this pointer.
491 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(From);
492 if (I == PointerMap.end() || !I->second.hasAliasSet())
493 return; // Noop
494
495 AliasSet::HashNodePair &Entry = getEntryFor(To);
496 if (Entry.second.hasAliasSet()) return; // Already in the tracker!
497
498 // Add it to the alias set it aliases...
499 AliasSet *AS = I->second.getAliasSet(*this);
500 AS->addPointer(*this, Entry, I->second.getSize(), true);
501}
502
503
504
505//===----------------------------------------------------------------------===//
506// AliasSet/AliasSetTracker Printing Support
507//===----------------------------------------------------------------------===//
508
509void AliasSet::print(std::ostream &OS) const {
510 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
511 OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, ";
512 switch (AccessTy) {
513 case NoModRef: OS << "No access "; break;
514 case Refs : OS << "Ref "; break;
515 case Mods : OS << "Mod "; break;
516 case ModRef : OS << "Mod/Ref "; break;
517 default: assert(0 && "Bad value for AccessTy!");
518 }
519 if (isVolatile()) OS << "[volatile] ";
520 if (Forward)
521 OS << " forwarding to " << (void*)Forward;
522
523
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000524 if (!empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525 OS << "Pointers: ";
526 for (iterator I = begin(), E = end(); I != E; ++I) {
527 if (I != begin()) OS << ", ";
528 WriteAsOperand(OS << "(", I.getPointer());
529 OS << ", " << I.getSize() << ")";
530 }
531 }
532 if (!CallSites.empty()) {
533 OS << "\n " << CallSites.size() << " Call Sites: ";
534 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
535 if (i) OS << ", ";
536 WriteAsOperand(OS, CallSites[i].getCalledValue());
537 }
538 }
539 OS << "\n";
540}
541
542void AliasSetTracker::print(std::ostream &OS) const {
543 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
544 << PointerMap.size() << " pointer values.\n";
545 for (const_iterator I = begin(), E = end(); I != E; ++I)
546 I->print(OS);
547 OS << "\n";
548}
549
550void AliasSet::dump() const { print (cerr); }
551void AliasSetTracker::dump() const { print(cerr); }
552
553//===----------------------------------------------------------------------===//
554// AliasSetPrinter Pass
555//===----------------------------------------------------------------------===//
556
557namespace {
558 class VISIBILITY_HIDDEN AliasSetPrinter : public FunctionPass {
559 AliasSetTracker *Tracker;
560 public:
561 static char ID; // Pass identification, replacement for typeid
562 AliasSetPrinter() : FunctionPass((intptr_t)&ID) {}
563
564 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
565 AU.setPreservesAll();
566 AU.addRequired<AliasAnalysis>();
567 }
568
569 virtual bool runOnFunction(Function &F) {
570 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
571
572 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
573 Tracker->add(&*I);
574 Tracker->print(cerr);
575 delete Tracker;
576 return false;
577 }
578 };
579 char AliasSetPrinter::ID = 0;
580 RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer");
581}