1//===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// 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/Assembly/Writer.h"
17#include "llvm/IR/DataLayout.h"
18#include "llvm/IR/Instructions.h"
19#include "llvm/IR/IntrinsicInst.h"
20#include "llvm/IR/LLVMContext.h"
21#include "llvm/IR/Type.h"
22#include "llvm/Pass.h"
23#include "llvm/Support/Debug.h"
24#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/InstIterator.h"
26#include "llvm/Support/raw_ostream.h"
27using namespace llvm;
28
29/// mergeSetIn - Merge the specified alias set into this alias set.
30///
31void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
32  assert(!AS.Forward && "Alias set is already forwarding!");
33  assert(!Forward && "This set is a forwarding set!!");
34
35  // Update the alias and access types of this set...
36  AccessTy |= AS.AccessTy;
37  AliasTy  |= AS.AliasTy;
38  Volatile |= AS.Volatile;
39
40  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();
45    PointerRec *L = getSomePointer();
46    PointerRec *R = AS.getSomePointer();
47
48    // If the pointers are not a must-alias pair, this set becomes a may alias.
49    if (AA.alias(AliasAnalysis::Location(L->getValue(),
50                                         L->getSize(),
51                                         L->getTBAAInfo()),
52                 AliasAnalysis::Location(R->getValue(),
53                                         R->getSize(),
54                                         R->getTBAAInfo()))
55        != AliasAnalysis::MustAlias)
56      AliasTy = MayAlias;
57  }
58
59  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();
65  }
66
67  AS.Forward = this;  // Forward across AS now...
68  addRef();           // AS is now pointing to us...
69
70  // Merge the list of constituent pointers...
71  if (AS.PtrList) {
72    *PtrListEnd = AS.PtrList;
73    AS.PtrList->setPrevInList(PtrListEnd);
74    PtrListEnd = AS.PtrListEnd;
75
76    AS.PtrList = 0;
77    AS.PtrListEnd = &AS.PtrList;
78    assert(*AS.PtrListEnd == 0 && "End of list is not null?");
79  }
80}
81
82void AliasSetTracker::removeAliasSet(AliasSet *AS) {
83  if (AliasSet *Fwd = AS->Forward) {
84    Fwd->dropRef(*this);
85    AS->Forward = 0;
86  }
87  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
95void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
96                          uint64_t Size, const MDNode *TBAAInfo,
97                          bool KnownMustAlias) {
98  assert(!Entry.hasAliasSet() && "Entry already in set!");
99
100  // Check to see if we have to downgrade to _may_ alias.
101  if (isMustAlias() && !KnownMustAlias)
102    if (PointerRec *P = getSomePointer()) {
103      AliasAnalysis &AA = AST.getAliasAnalysis();
104      AliasAnalysis::AliasResult Result =
105        AA.alias(AliasAnalysis::Location(P->getValue(), P->getSize(),
106                                         P->getTBAAInfo()),
107                 AliasAnalysis::Location(Entry.getValue(), Size, TBAAInfo));
108      if (Result != AliasAnalysis::MustAlias)
109        AliasTy = MayAlias;
110      else                  // First entry of must alias must have maximum size!
111        P->updateSizeAndTBAAInfo(Size, TBAAInfo);
112      assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
113    }
114
115  Entry.setAliasSet(this);
116  Entry.updateSizeAndTBAAInfo(Size, TBAAInfo);
117
118  // Add it to the end of the list...
119  assert(*PtrListEnd == 0 && "End of list is not null?");
120  *PtrListEnd = &Entry;
121  PtrListEnd = Entry.setPrevInList(PtrListEnd);
122  assert(*PtrListEnd == 0 && "End of list is not null?");
123  addRef();               // Entry points to alias set.
124}
125
126void AliasSet::addUnknownInst(Instruction *I, AliasAnalysis &AA) {
127  UnknownInsts.push_back(I);
128
129  if (!I->mayWriteToMemory()) {
130    AliasTy = MayAlias;
131    AccessTy |= Refs;
132    return;
133  }
134
135  // FIXME: This should use mod/ref information to make this not suck so bad
136  AliasTy = MayAlias;
137  AccessTy = ModRef;
138}
139
140/// aliasesPointer - Return true if the specified pointer "may" (or must)
141/// alias one of the members in the set.
142///
143bool AliasSet::aliasesPointer(const Value *Ptr, uint64_t Size,
144                              const MDNode *TBAAInfo,
145                              AliasAnalysis &AA) const {
146  if (AliasTy == MustAlias) {
147    assert(UnknownInsts.empty() && "Illegal must alias set!");
148
149    // If this is a set of MustAliases, only check to see if the pointer aliases
150    // SOME value in the set.
151    PointerRec *SomePtr = getSomePointer();
152    assert(SomePtr && "Empty must-alias set??");
153    return AA.alias(AliasAnalysis::Location(SomePtr->getValue(),
154                                            SomePtr->getSize(),
155                                            SomePtr->getTBAAInfo()),
156                    AliasAnalysis::Location(Ptr, Size, TBAAInfo));
157  }
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)
162    if (AA.alias(AliasAnalysis::Location(Ptr, Size, TBAAInfo),
163                 AliasAnalysis::Location(I.getPointer(), I.getSize(),
164                                         I.getTBAAInfo())))
165      return true;
166
167  // 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],
171                           AliasAnalysis::Location(Ptr, Size, TBAAInfo)) !=
172            AliasAnalysis::NoModRef)
173        return true;
174  }
175
176  return false;
177}
178
179bool AliasSet::aliasesUnknownInst(Instruction *Inst, AliasAnalysis &AA) const {
180  if (!Inst->mayReadOrWriteMemory())
181    return false;
182
183  for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
184    CallSite C1 = getUnknownInst(i), C2 = Inst;
185    if (!C1 || !C2 ||
186        AA.getModRefInfo(C1, C2) != AliasAnalysis::NoModRef ||
187        AA.getModRefInfo(C2, C1) != AliasAnalysis::NoModRef)
188      return true;
189  }
190
191  for (iterator I = begin(), E = end(); I != E; ++I)
192    if (AA.getModRefInfo(Inst, AliasAnalysis::Location(I.getPointer(),
193                                                       I.getSize(),
194                                                       I.getTBAAInfo())) !=
195           AliasAnalysis::NoModRef)
196      return true;
197
198  return false;
199}
200
201void AliasSetTracker::clear() {
202  // Delete all the PointerRec entries.
203  for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
204       I != E; ++I)
205    I->second->eraseFromList();
206
207  PointerMap.clear();
208
209  // The alias sets should all be clear now.
210  AliasSets.clear();
211}
212
213
214/// 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///
218AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
219                                                  uint64_t Size,
220                                                  const MDNode *TBAAInfo) {
221  AliasSet *FoundSet = 0;
222  for (iterator I = begin(), E = end(); I != E; ++I) {
223    if (I->Forward || !I->aliasesPointer(Ptr, Size, TBAAInfo, AA)) continue;
224
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.
229    }
230  }
231
232  return FoundSet;
233}
234
235/// 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.
238bool AliasSetTracker::containsPointer(Value *Ptr, uint64_t Size,
239                                      const MDNode *TBAAInfo) const {
240  for (const_iterator I = begin(), E = end(); I != E; ++I)
241    if (!I->Forward && I->aliasesPointer(Ptr, Size, TBAAInfo, AA))
242      return true;
243  return false;
244}
245
246
247
248AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) {
249  AliasSet *FoundSet = 0;
250  for (iterator I = begin(), E = end(); I != E; ++I) {
251    if (I->Forward || !I->aliasesUnknownInst(Inst, AA))
252      continue;
253
254    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.
257      FoundSet->mergeSetIn(*I, *this);     // Merge in contents.
258  }
259  return FoundSet;
260}
261
262
263
264
265/// getAliasSetForPointer - Return the alias set that the specified pointer
266/// lives in.
267AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, uint64_t Size,
268                                                 const MDNode *TBAAInfo,
269                                                 bool *New) {
270  AliasSet::PointerRec &Entry = getEntryFor(Pointer);
271
272  // Check to see if the pointer is already known.
273  if (Entry.hasAliasSet()) {
274    Entry.updateSizeAndTBAAInfo(Size, TBAAInfo);
275    // Return the set!
276    return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
277  }
278
279  if (AliasSet *AS = findAliasSetForPointer(Pointer, Size, TBAAInfo)) {
280    // Add it to the alias set it aliases.
281    AS->addPointer(*this, Entry, Size, TBAAInfo);
282    return *AS;
283  }
284
285  if (New) *New = true;
286  // Otherwise create a new alias set to hold the loaded pointer.
287  AliasSets.push_back(new AliasSet());
288  AliasSets.back().addPointer(*this, Entry, Size, TBAAInfo);
289  return AliasSets.back();
290}
291
292bool AliasSetTracker::add(Value *Ptr, uint64_t Size, const MDNode *TBAAInfo) {
293  bool NewPtr;
294  addPointer(Ptr, Size, TBAAInfo, AliasSet::NoModRef, NewPtr);
295  return NewPtr;
296}
297
298
299bool AliasSetTracker::add(LoadInst *LI) {
300  if (LI->getOrdering() > Monotonic) return addUnknown(LI);
301  AliasSet::AccessType ATy = AliasSet::Refs;
302  bool NewPtr;
303  AliasSet &AS = addPointer(LI->getOperand(0),
304                            AA.getTypeStoreSize(LI->getType()),
305                            LI->getMetadata(LLVMContext::MD_tbaa),
306                            ATy, NewPtr);
307  if (LI->isVolatile()) AS.setVolatile();
308  return NewPtr;
309}
310
311bool AliasSetTracker::add(StoreInst *SI) {
312  if (SI->getOrdering() > Monotonic) return addUnknown(SI);
313  AliasSet::AccessType ATy = AliasSet::Mods;
314  bool NewPtr;
315  Value *Val = SI->getOperand(0);
316  AliasSet &AS = addPointer(SI->getOperand(1),
317                            AA.getTypeStoreSize(Val->getType()),
318                            SI->getMetadata(LLVMContext::MD_tbaa),
319                            ATy, NewPtr);
320  if (SI->isVolatile()) AS.setVolatile();
321  return NewPtr;
322}
323
324bool AliasSetTracker::add(VAArgInst *VAAI) {
325  bool NewPtr;
326  addPointer(VAAI->getOperand(0), AliasAnalysis::UnknownSize,
327             VAAI->getMetadata(LLVMContext::MD_tbaa),
328             AliasSet::ModRef, NewPtr);
329  return NewPtr;
330}
331
332
333bool AliasSetTracker::addUnknown(Instruction *Inst) {
334  if (isa<DbgInfoIntrinsic>(Inst))
335    return true; // Ignore DbgInfo Intrinsics.
336  if (!Inst->mayReadOrWriteMemory())
337    return true; // doesn't alias anything
338
339  AliasSet *AS = findAliasSetForUnknownInst(Inst);
340  if (AS) {
341    AS->addUnknownInst(Inst, AA);
342    return false;
343  }
344  AliasSets.push_back(new AliasSet());
345  AS = &AliasSets.back();
346  AS->addUnknownInst(Inst, AA);
347  return true;
348}
349
350bool AliasSetTracker::add(Instruction *I) {
351  // Dispatch to one of the other add methods.
352  if (LoadInst *LI = dyn_cast<LoadInst>(I))
353    return add(LI);
354  if (StoreInst *SI = dyn_cast<StoreInst>(I))
355    return add(SI);
356  if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
357    return add(VAAI);
358  return addUnknown(I);
359}
360
361void AliasSetTracker::add(BasicBlock &BB) {
362  for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
363    add(I);
364}
365
366void AliasSetTracker::add(const AliasSetTracker &AST) {
367  assert(&AA == &AST.AA &&
368         "Merging AliasSetTracker objects with different Alias Analyses!");
369
370  // Loop over all of the alias sets in AST, adding the pointers contained
371  // therein into the current alias sets.  This can cause alias sets to be
372  // merged together in the current AST.
373  for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I) {
374    if (I->Forward) continue;   // Ignore forwarding alias sets
375
376    AliasSet &AS = const_cast<AliasSet&>(*I);
377
378    // If there are any call sites in the alias set, add them to this AST.
379    for (unsigned i = 0, e = AS.UnknownInsts.size(); i != e; ++i)
380      add(AS.UnknownInsts[i]);
381
382    // Loop over all of the pointers in this alias set.
383    bool X;
384    for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) {
385      AliasSet &NewAS = addPointer(ASI.getPointer(), ASI.getSize(),
386                                   ASI.getTBAAInfo(),
387                                   (AliasSet::AccessType)AS.AccessTy, X);
388      if (AS.isVolatile()) NewAS.setVolatile();
389    }
390  }
391}
392
393/// remove - Remove the specified (potentially non-empty) alias set from the
394/// tracker.
395void AliasSetTracker::remove(AliasSet &AS) {
396  // Drop all call sites.
397  AS.UnknownInsts.clear();
398
399  // Clear the alias set.
400  unsigned NumRefs = 0;
401  while (!AS.empty()) {
402    AliasSet::PointerRec *P = AS.PtrList;
403
404    Value *ValToRemove = P->getValue();
405
406    // Unlink and delete entry from the list of values.
407    P->eraseFromList();
408
409    // Remember how many references need to be dropped.
410    ++NumRefs;
411
412    // Finally, remove the entry.
413    PointerMap.erase(ValToRemove);
414  }
415
416  // Stop using the alias set, removing it.
417  AS.RefCount -= NumRefs;
418  if (AS.RefCount == 0)
419    AS.removeFromTracker(*this);
420}
421
422bool
423AliasSetTracker::remove(Value *Ptr, uint64_t Size, const MDNode *TBAAInfo) {
424  AliasSet *AS = findAliasSetForPointer(Ptr, Size, TBAAInfo);
425  if (!AS) return false;
426  remove(*AS);
427  return true;
428}
429
430bool AliasSetTracker::remove(LoadInst *LI) {
431  uint64_t Size = AA.getTypeStoreSize(LI->getType());
432  const MDNode *TBAAInfo = LI->getMetadata(LLVMContext::MD_tbaa);
433  AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size, TBAAInfo);
434  if (!AS) return false;
435  remove(*AS);
436  return true;
437}
438
439bool AliasSetTracker::remove(StoreInst *SI) {
440  uint64_t Size = AA.getTypeStoreSize(SI->getOperand(0)->getType());
441  const MDNode *TBAAInfo = SI->getMetadata(LLVMContext::MD_tbaa);
442  AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size, TBAAInfo);
443  if (!AS) return false;
444  remove(*AS);
445  return true;
446}
447
448bool AliasSetTracker::remove(VAArgInst *VAAI) {
449  AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0),
450                                        AliasAnalysis::UnknownSize,
451                                        VAAI->getMetadata(LLVMContext::MD_tbaa));
452  if (!AS) return false;
453  remove(*AS);
454  return true;
455}
456
457bool AliasSetTracker::removeUnknown(Instruction *I) {
458  if (!I->mayReadOrWriteMemory())
459    return false; // doesn't alias anything
460
461  AliasSet *AS = findAliasSetForUnknownInst(I);
462  if (!AS) return false;
463  remove(*AS);
464  return true;
465}
466
467bool AliasSetTracker::remove(Instruction *I) {
468  // Dispatch to one of the other remove methods...
469  if (LoadInst *LI = dyn_cast<LoadInst>(I))
470    return remove(LI);
471  if (StoreInst *SI = dyn_cast<StoreInst>(I))
472    return remove(SI);
473  if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
474    return remove(VAAI);
475  return removeUnknown(I);
476}
477
478
479// deleteValue method - This method is used to remove a pointer value from the
480// AliasSetTracker entirely.  It should be used when an instruction is deleted
481// from the program to update the AST.  If you don't use this, you would have
482// dangling pointers to deleted instructions.
483//
484void AliasSetTracker::deleteValue(Value *PtrVal) {
485  // Notify the alias analysis implementation that this value is gone.
486  AA.deleteValue(PtrVal);
487
488  // If this is a call instruction, remove the callsite from the appropriate
489  // AliasSet (if present).
490  if (Instruction *Inst = dyn_cast<Instruction>(PtrVal)) {
491    if (Inst->mayReadOrWriteMemory()) {
492      // Scan all the alias sets to see if this call site is contained.
493      for (iterator I = begin(), E = end(); I != E; ++I) {
494        if (I->Forward) continue;
495
496        I->removeUnknownInst(Inst);
497      }
498    }
499  }
500
501  // First, look up the PointerRec for this pointer.
502  PointerMapType::iterator I = PointerMap.find_as(PtrVal);
503  if (I == PointerMap.end()) return;  // Noop
504
505  // If we found one, remove the pointer from the alias set it is in.
506  AliasSet::PointerRec *PtrValEnt = I->second;
507  AliasSet *AS = PtrValEnt->getAliasSet(*this);
508
509  // Unlink and delete from the list of values.
510  PtrValEnt->eraseFromList();
511
512  // Stop using the alias set.
513  AS->dropRef(*this);
514
515  PointerMap.erase(I);
516}
517
518// copyValue - This method should be used whenever a preexisting value in the
519// program is copied or cloned, introducing a new value.  Note that it is ok for
520// clients that use this method to introduce the same value multiple times: if
521// the tracker already knows about a value, it will ignore the request.
522//
523void AliasSetTracker::copyValue(Value *From, Value *To) {
524  // Notify the alias analysis implementation that this value is copied.
525  AA.copyValue(From, To);
526
527  // First, look up the PointerRec for this pointer.
528  PointerMapType::iterator I = PointerMap.find_as(From);
529  if (I == PointerMap.end())
530    return;  // Noop
531  assert(I->second->hasAliasSet() && "Dead entry?");
532
533  AliasSet::PointerRec &Entry = getEntryFor(To);
534  if (Entry.hasAliasSet()) return;    // Already in the tracker!
535
536  // Add it to the alias set it aliases...
537  I = PointerMap.find_as(From);
538  AliasSet *AS = I->second->getAliasSet(*this);
539  AS->addPointer(*this, Entry, I->second->getSize(),
540                 I->second->getTBAAInfo(),
541                 true);
542}
543
544
545
546//===----------------------------------------------------------------------===//
547//               AliasSet/AliasSetTracker Printing Support
548//===----------------------------------------------------------------------===//
549
550void AliasSet::print(raw_ostream &OS) const {
551  OS << "  AliasSet[" << (const void*)this << ", " << RefCount << "] ";
552  OS << (AliasTy == MustAlias ? "must" : "may") << " alias, ";
553  switch (AccessTy) {
554  case NoModRef: OS << "No access "; break;
555  case Refs    : OS << "Ref       "; break;
556  case Mods    : OS << "Mod       "; break;
557  case ModRef  : OS << "Mod/Ref   "; break;
558  default: llvm_unreachable("Bad value for AccessTy!");
559  }
560  if (isVolatile()) OS << "[volatile] ";
561  if (Forward)
562    OS << " forwarding to " << (void*)Forward;
563
564
565  if (!empty()) {
566    OS << "Pointers: ";
567    for (iterator I = begin(), E = end(); I != E; ++I) {
568      if (I != begin()) OS << ", ";
569      WriteAsOperand(OS << "(", I.getPointer());
570      OS << ", " << I.getSize() << ")";
571    }
572  }
573  if (!UnknownInsts.empty()) {
574    OS << "\n    " << UnknownInsts.size() << " Unknown instructions: ";
575    for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
576      if (i) OS << ", ";
577      WriteAsOperand(OS, UnknownInsts[i]);
578    }
579  }
580  OS << "\n";
581}
582
583void AliasSetTracker::print(raw_ostream &OS) const {
584  OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
585     << PointerMap.size() << " pointer values.\n";
586  for (const_iterator I = begin(), E = end(); I != E; ++I)
587    I->print(OS);
588  OS << "\n";
589}
590
591#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
592void AliasSet::dump() const { print(dbgs()); }
593void AliasSetTracker::dump() const { print(dbgs()); }
594#endif
595
596//===----------------------------------------------------------------------===//
597//                     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
606void AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(Value *V) {
607  AST->copyValue(getValPtr(), V);
608}
609
610AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
611  : CallbackVH(V), AST(ast) {}
612
613AliasSetTracker::ASTCallbackVH &
614AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
615  return *this = ASTCallbackVH(V, AST);
616}
617
618//===----------------------------------------------------------------------===//
619//                            AliasSetPrinter Pass
620//===----------------------------------------------------------------------===//
621
622namespace {
623  class AliasSetPrinter : public FunctionPass {
624    AliasSetTracker *Tracker;
625  public:
626    static char ID; // Pass identification, replacement for typeid
627    AliasSetPrinter() : FunctionPass(ID) {
628      initializeAliasSetPrinterPass(*PassRegistry::getPassRegistry());
629    }
630
631    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)
640        Tracker->add(&*I);
641      Tracker->print(errs());
642      delete Tracker;
643      return false;
644    }
645  };
646}
647
648char AliasSetPrinter::ID = 0;
649INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets",
650                "Alias Set Printer", false, true)
651INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
652INITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets",
653                "Alias Set Printer", false, true)
654