1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     The LLVM Compiler Infrastructure
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details.
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file implements the AliasSetTracker and AliasSet classes.
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Analysis/AliasSetTracker.h"
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Analysis/AliasAnalysis.h"
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Instructions.h"
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/IntrinsicInst.h"
1819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/LLVMContext.h"
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Pass.h"
20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Type.h"
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetData.h"
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Assembly/Writer.h"
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/Debug.h"
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/ErrorHandling.h"
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/InstIterator.h"
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/raw_ostream.h"
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanusing namespace llvm;
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// mergeSetIn - Merge the specified alias set into this alias set.
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(!AS.Forward && "Alias set is already forwarding!");
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(!Forward && "This set is a forwarding set!!");
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Update the alias and access types of this set...
36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AccessTy |= AS.AccessTy;
37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AliasTy  |= AS.AliasTy;
3819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Volatile |= AS.Volatile;
39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (AliasTy == MustAlias) {
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Check that these two merged sets really are must aliases.  Since both
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // used to be must-alias sets, we can just check any pointer from each set
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // for aliasing.
44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    AliasAnalysis &AA = AST.getAliasAnalysis();
45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PointerRec *L = getSomePointer();
46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PointerRec *R = AS.getSomePointer();
47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If the pointers are not a must-alias pair, this set becomes a may alias.
4919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (AA.alias(AliasAnalysis::Location(L->getValue(),
5019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                         L->getSize(),
5119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                         L->getTBAAInfo()),
5219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                 AliasAnalysis::Location(R->getValue(),
5319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                         R->getSize(),
5419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                         R->getTBAAInfo()))
55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        != AliasAnalysis::MustAlias)
56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      AliasTy = MayAlias;
57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
5919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (UnknownInsts.empty()) {            // Merge call sites...
6019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!AS.UnknownInsts.empty())
6119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      std::swap(UnknownInsts, AS.UnknownInsts);
6219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else if (!AS.UnknownInsts.empty()) {
6319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    UnknownInsts.insert(UnknownInsts.end(), AS.UnknownInsts.begin(), AS.UnknownInsts.end());
6419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    AS.UnknownInsts.clear();
65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
66894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
67894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AS.Forward = this;  // Forward across AS now...
68894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  addRef();           // AS is now pointing to us...
69894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
70894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Merge the list of constituent pointers...
71894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (AS.PtrList) {
72894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    *PtrListEnd = AS.PtrList;
73894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    AS.PtrList->setPrevInList(PtrListEnd);
74894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PtrListEnd = AS.PtrListEnd;
75894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
76894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    AS.PtrList = 0;
77894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    AS.PtrListEnd = &AS.PtrList;
78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(*AS.PtrListEnd == 0 && "End of list is not null?");
79894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AliasSetTracker::removeAliasSet(AliasSet *AS) {
83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (AliasSet *Fwd = AS->Forward) {
84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Fwd->dropRef(*this);
85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    AS->Forward = 0;
86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AliasSets.erase(AS);
88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
90894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AliasSet::removeFromTracker(AliasSetTracker &AST) {
91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AST.removeAliasSet(this);
93894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
94894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
9619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                          uint64_t Size, const MDNode *TBAAInfo,
9719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                          bool KnownMustAlias) {
98894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(!Entry.hasAliasSet() && "Entry already in set!");
99894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Check to see if we have to downgrade to _may_ alias.
101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isMustAlias() && !KnownMustAlias)
102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (PointerRec *P = getSomePointer()) {
103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      AliasAnalysis &AA = AST.getAliasAnalysis();
104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      AliasAnalysis::AliasResult Result =
10519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        AA.alias(AliasAnalysis::Location(P->getValue(), P->getSize(),
10619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                         P->getTBAAInfo()),
10719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                 AliasAnalysis::Location(Entry.getValue(), Size, TBAAInfo));
10819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (Result != AliasAnalysis::MustAlias)
109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        AliasTy = MayAlias;
110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else                  // First entry of must alias must have maximum size!
11119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        P->updateSizeAndTBAAInfo(Size, TBAAInfo);
112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Entry.setAliasSet(this);
11619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Entry.updateSizeAndTBAAInfo(Size, TBAAInfo);
117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Add it to the end of the list...
119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(*PtrListEnd == 0 && "End of list is not null?");
120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  *PtrListEnd = &Entry;
121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  PtrListEnd = Entry.setPrevInList(PtrListEnd);
122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(*PtrListEnd == 0 && "End of list is not null?");
12319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  addRef();               // Entry points to alias set.
124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid AliasSet::addUnknownInst(Instruction *I, AliasAnalysis &AA) {
12719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  UnknownInsts.push_back(I);
128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!I->mayWriteToMemory()) {
130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    AliasTy = MayAlias;
131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    AccessTy |= Refs;
132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: This should use mod/ref information to make this not suck so bad
136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AliasTy = MayAlias;
137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AccessTy = ModRef;
138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// aliasesPointer - Return true if the specified pointer "may" (or must)
141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// alias one of the members in the set.
142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
14319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool AliasSet::aliasesPointer(const Value *Ptr, uint64_t Size,
14419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                              const MDNode *TBAAInfo,
145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              AliasAnalysis &AA) const {
146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (AliasTy == MustAlias) {
14719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(UnknownInsts.empty() && "Illegal must alias set!");
148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If this is a set of MustAliases, only check to see if the pointer aliases
15019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // SOME value in the set.
151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PointerRec *SomePtr = getSomePointer();
152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(SomePtr && "Empty must-alias set??");
15319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return AA.alias(AliasAnalysis::Location(SomePtr->getValue(),
15419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                            SomePtr->getSize(),
15519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                            SomePtr->getTBAAInfo()),
15619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                    AliasAnalysis::Location(Ptr, Size, TBAAInfo));
157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If this is a may-alias set, we have to check all of the pointers in the set
160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // to be sure it doesn't alias the set...
161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (iterator I = begin(), E = end(); I != E; ++I)
16219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (AA.alias(AliasAnalysis::Location(Ptr, Size, TBAAInfo),
16319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                 AliasAnalysis::Location(I.getPointer(), I.getSize(),
16419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                         I.getTBAAInfo())))
165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
16719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Check the unknown instructions...
16819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!UnknownInsts.empty()) {
16919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i)
17019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (AA.getModRefInfo(UnknownInsts[i],
17119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                           AliasAnalysis::Location(Ptr, Size, TBAAInfo)) !=
17219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            AliasAnalysis::NoModRef)
173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return true;
174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
17919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool AliasSet::aliasesUnknownInst(Instruction *Inst, AliasAnalysis &AA) const {
18019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!Inst->mayReadOrWriteMemory())
181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
18319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
18419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    CallSite C1 = getUnknownInst(i), C2 = Inst;
18519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!C1 || !C2 ||
18619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        AA.getModRefInfo(C1, C2) != AliasAnalysis::NoModRef ||
18719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        AA.getModRefInfo(C2, C1) != AliasAnalysis::NoModRef)
188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
18919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (iterator I = begin(), E = end(); I != E; ++I)
19219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (AA.getModRefInfo(Inst, I.getPointer(), I.getSize()) !=
193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           AliasAnalysis::NoModRef)
194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AliasSetTracker::clear() {
200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Delete all the PointerRec entries.
201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       I != E; ++I)
203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    I->second->eraseFromList();
204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  PointerMap.clear();
206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // The alias sets should all be clear now.
208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AliasSets.clear();
209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// instruction referring to the pointer into.  If there are multiple alias sets
214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// that may alias the pointer, merge them together and return the unified set.
215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanAliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
21719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                  uint64_t Size,
21819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                  const MDNode *TBAAInfo) {
219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AliasSet *FoundSet = 0;
22019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (iterator I = begin(), E = end(); I != E; ++I) {
22119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (I->Forward || !I->aliasesPointer(Ptr, Size, TBAAInfo, AA)) continue;
22219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
22319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (FoundSet == 0) {  // If this is the first alias set ptr can go into.
22419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      FoundSet = I;       // Remember it.
22519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    } else {              // Otherwise, we must merge the sets.
22619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      FoundSet->mergeSetIn(*I, *this);     // Merge in contents.
227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
22819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return FoundSet;
231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// containsPointer - Return true if the specified location is represented by
234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// this alias set, false otherwise.  This does not modify the AST object or
235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// alias sets.
23619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool AliasSetTracker::containsPointer(Value *Ptr, uint64_t Size,
23719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                      const MDNode *TBAAInfo) const {
238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (const_iterator I = begin(), E = end(); I != E; ++I)
23919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!I->Forward && I->aliasesPointer(Ptr, Size, TBAAInfo, AA))
240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
24619bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanAliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) {
247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AliasSet *FoundSet = 0;
24819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (iterator I = begin(), E = end(); I != E; ++I) {
24919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (I->Forward || !I->aliasesUnknownInst(Inst, AA))
25019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      continue;
25119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
25219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (FoundSet == 0)        // If this is the first alias set ptr can go into.
25319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      FoundSet = I;           // Remember it.
25419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    else if (!I->Forward)     // Otherwise, we must merge the sets.
25519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      FoundSet->mergeSetIn(*I, *this);     // Merge in contents.
25619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return FoundSet;
258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getAliasSetForPointer - Return the alias set that the specified pointer
264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// lives in.
26519bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanAliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, uint64_t Size,
26619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                 const MDNode *TBAAInfo,
267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 bool *New) {
268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AliasSet::PointerRec &Entry = getEntryFor(Pointer);
269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
27019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Check to see if the pointer is already known.
271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Entry.hasAliasSet()) {
27219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Entry.updateSizeAndTBAAInfo(Size, TBAAInfo);
273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Return the set!
274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
27519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
27619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
27719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (AliasSet *AS = findAliasSetForPointer(Pointer, Size, TBAAInfo)) {
27819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Add it to the alias set it aliases.
27919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    AS->addPointer(*this, Entry, Size, TBAAInfo);
280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return *AS;
281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
28219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
28319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (New) *New = true;
28419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Otherwise create a new alias set to hold the loaded pointer.
28519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  AliasSets.push_back(new AliasSet());
28619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  AliasSets.back().addPointer(*this, Entry, Size, TBAAInfo);
28719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return AliasSets.back();
288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
29019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool AliasSetTracker::add(Value *Ptr, uint64_t Size, const MDNode *TBAAInfo) {
291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool NewPtr;
29219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  addPointer(Ptr, Size, TBAAInfo, AliasSet::NoModRef, NewPtr);
293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return NewPtr;
294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool AliasSetTracker::add(LoadInst *LI) {
29819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (LI->getOrdering() > Monotonic) return addUnknown(LI);
29919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  AliasSet::AccessType ATy = AliasSet::Refs;
30019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!LI->isUnordered()) ATy = AliasSet::ModRef;
301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool NewPtr;
302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AliasSet &AS = addPointer(LI->getOperand(0),
303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            AA.getTypeStoreSize(LI->getType()),
30419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                            LI->getMetadata(LLVMContext::MD_tbaa),
30519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                            ATy, NewPtr);
306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (LI->isVolatile()) AS.setVolatile();
307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return NewPtr;
308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool AliasSetTracker::add(StoreInst *SI) {
31119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (SI->getOrdering() > Monotonic) return addUnknown(SI);
31219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  AliasSet::AccessType ATy = AliasSet::Mods;
31319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!SI->isUnordered()) ATy = AliasSet::ModRef;
314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool NewPtr;
315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Value *Val = SI->getOperand(0);
316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AliasSet &AS = addPointer(SI->getOperand(1),
317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            AA.getTypeStoreSize(Val->getType()),
31819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                            SI->getMetadata(LLVMContext::MD_tbaa),
31919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                            ATy, NewPtr);
320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (SI->isVolatile()) AS.setVolatile();
321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return NewPtr;
322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
32419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool AliasSetTracker::add(VAArgInst *VAAI) {
32519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool NewPtr;
32619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  addPointer(VAAI->getOperand(0), AliasAnalysis::UnknownSize,
32719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman             VAAI->getMetadata(LLVMContext::MD_tbaa),
32819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman             AliasSet::ModRef, NewPtr);
32919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return NewPtr;
33019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
33119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
33219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
33319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool AliasSetTracker::addUnknown(Instruction *Inst) {
33419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (isa<DbgInfoIntrinsic>(Inst))
335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return true; // Ignore DbgInfo Intrinsics.
33619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!Inst->mayReadOrWriteMemory())
337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return true; // doesn't alias anything
338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
33919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  AliasSet *AS = findAliasSetForUnknownInst(Inst);
34019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (AS) {
34119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    AS->addUnknownInst(Inst, AA);
342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
34419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  AliasSets.push_back(new AliasSet());
34519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  AS = &AliasSets.back();
34619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  AS->addUnknownInst(Inst, AA);
34719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return true;
348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool AliasSetTracker::add(Instruction *I) {
35119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Dispatch to one of the other add methods.
352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (LoadInst *LI = dyn_cast<LoadInst>(I))
353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return add(LI);
35419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (StoreInst *SI = dyn_cast<StoreInst>(I))
355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return add(SI);
35619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
35719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return add(VAAI);
35819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return addUnknown(I);
359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AliasSetTracker::add(BasicBlock &BB) {
362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    add(I);
364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AliasSetTracker::add(const AliasSetTracker &AST) {
367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(&AA == &AST.AA &&
368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Merging AliasSetTracker objects with different Alias Analyses!");
369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Loop over all of the alias sets in AST, adding the pointers contained
371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // therein into the current alias sets.  This can cause alias sets to be
372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // merged together in the current AST.
37319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I) {
37419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (I->Forward) continue;   // Ignore forwarding alias sets
37519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
37619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    AliasSet &AS = const_cast<AliasSet&>(*I);
37719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
37819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // If there are any call sites in the alias set, add them to this AST.
37919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (unsigned i = 0, e = AS.UnknownInsts.size(); i != e; ++i)
38019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      add(AS.UnknownInsts[i]);
38119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
38219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Loop over all of the pointers in this alias set.
38319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    bool X;
38419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) {
38519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      AliasSet &NewAS = addPointer(ASI.getPointer(), ASI.getSize(),
38619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                   ASI.getTBAAInfo(),
38719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                   (AliasSet::AccessType)AS.AccessTy, X);
38819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (AS.isVolatile()) NewAS.setVolatile();
389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
39019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// remove - Remove the specified (potentially non-empty) alias set from the
394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// tracker.
395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AliasSetTracker::remove(AliasSet &AS) {
396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Drop all call sites.
39719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  AS.UnknownInsts.clear();
398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Clear the alias set.
400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumRefs = 0;
401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  while (!AS.empty()) {
402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    AliasSet::PointerRec *P = AS.PtrList;
403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Value *ValToRemove = P->getValue();
405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Unlink and delete entry from the list of values.
407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    P->eraseFromList();
408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Remember how many references need to be dropped.
410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ++NumRefs;
411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Finally, remove the entry.
413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PointerMap.erase(ValToRemove);
414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Stop using the alias set, removing it.
417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AS.RefCount -= NumRefs;
418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (AS.RefCount == 0)
419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    AS.removeFromTracker(*this);
420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
42219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool
42319bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanAliasSetTracker::remove(Value *Ptr, uint64_t Size, const MDNode *TBAAInfo) {
42419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  AliasSet *AS = findAliasSetForPointer(Ptr, Size, TBAAInfo);
425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!AS) return false;
426894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  remove(*AS);
427894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
428894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
430894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool AliasSetTracker::remove(LoadInst *LI) {
43119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  uint64_t Size = AA.getTypeStoreSize(LI->getType());
43219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const MDNode *TBAAInfo = LI->getMetadata(LLVMContext::MD_tbaa);
43319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size, TBAAInfo);
434894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!AS) return false;
435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  remove(*AS);
436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool AliasSetTracker::remove(StoreInst *SI) {
44019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  uint64_t Size = AA.getTypeStoreSize(SI->getOperand(0)->getType());
44119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const MDNode *TBAAInfo = SI->getMetadata(LLVMContext::MD_tbaa);
44219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size, TBAAInfo);
44319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!AS) return false;
44419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  remove(*AS);
44519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return true;
44619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
44719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
44819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool AliasSetTracker::remove(VAArgInst *VAAI) {
44919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0),
45019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                        AliasAnalysis::UnknownSize,
45119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                        VAAI->getMetadata(LLVMContext::MD_tbaa));
452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!AS) return false;
453894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  remove(*AS);
454894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
456894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
45719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanbool AliasSetTracker::removeUnknown(Instruction *I) {
45819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!I->mayReadOrWriteMemory())
459894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false; // doesn't alias anything
460894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
46119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  AliasSet *AS = findAliasSetForUnknownInst(I);
462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!AS) return false;
463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  remove(*AS);
464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
466894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool AliasSetTracker::remove(Instruction *I) {
468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Dispatch to one of the other remove methods...
469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (LoadInst *LI = dyn_cast<LoadInst>(I))
470894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return remove(LI);
47119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (StoreInst *SI = dyn_cast<StoreInst>(I))
472894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return remove(SI);
47319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
47419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return remove(VAAI);
47519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return removeUnknown(I);
476894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
477894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
478894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
479894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// deleteValue method - This method is used to remove a pointer value from the
480894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// AliasSetTracker entirely.  It should be used when an instruction is deleted
481894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// from the program to update the AST.  If you don't use this, you would have
482894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// dangling pointers to deleted instructions.
483894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
484894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AliasSetTracker::deleteValue(Value *PtrVal) {
485894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Notify the alias analysis implementation that this value is gone.
486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AA.deleteValue(PtrVal);
487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If this is a call instruction, remove the callsite from the appropriate
48919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // AliasSet (if present).
49019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Instruction *Inst = dyn_cast<Instruction>(PtrVal)) {
49119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Inst->mayReadOrWriteMemory()) {
49219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // Scan all the alias sets to see if this call site is contained.
49319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      for (iterator I = begin(), E = end(); I != E; ++I) {
49419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (I->Forward) continue;
49519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
49619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        I->removeUnknownInst(Inst);
49719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
49819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
49919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // First, look up the PointerRec for this pointer.
502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  PointerMapType::iterator I = PointerMap.find(PtrVal);
503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (I == PointerMap.end()) return;  // Noop
504894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
505894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If we found one, remove the pointer from the alias set it is in.
506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AliasSet::PointerRec *PtrValEnt = I->second;
507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AliasSet *AS = PtrValEnt->getAliasSet(*this);
508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
509894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Unlink and delete from the list of values.
510894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  PtrValEnt->eraseFromList();
511894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
512894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Stop using the alias set.
513894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AS->dropRef(*this);
514894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  PointerMap.erase(I);
516894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
517894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// copyValue - This method should be used whenever a preexisting value in the
519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// program is copied or cloned, introducing a new value.  Note that it is ok for
520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// clients that use this method to introduce the same value multiple times: if
521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// the tracker already knows about a value, it will ignore the request.
522894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
523894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AliasSetTracker::copyValue(Value *From, Value *To) {
524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Notify the alias analysis implementation that this value is copied.
525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AA.copyValue(From, To);
526894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // First, look up the PointerRec for this pointer.
528894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  PointerMapType::iterator I = PointerMap.find(From);
529894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (I == PointerMap.end())
530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;  // Noop
531894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(I->second->hasAliasSet() && "Dead entry?");
532894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
533894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AliasSet::PointerRec &Entry = getEntryFor(To);
534894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Entry.hasAliasSet()) return;    // Already in the tracker!
535894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
536894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Add it to the alias set it aliases...
537894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  I = PointerMap.find(From);
538894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AliasSet *AS = I->second->getAliasSet(*this);
53919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  AS->addPointer(*this, Entry, I->second->getSize(),
54019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                 I->second->getTBAAInfo(),
54119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                 true);
542894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
543894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
544894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
545894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
546894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
547894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//               AliasSet/AliasSetTracker Printing Support
548894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
549894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
550894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AliasSet::print(raw_ostream &OS) const {
55119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  OS << "  AliasSet[" << (void*)this << ", " << RefCount << "] ";
552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  OS << (AliasTy == MustAlias ? "must" : "may") << " alias, ";
553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (AccessTy) {
554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case NoModRef: OS << "No access "; break;
555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Refs    : OS << "Ref       "; break;
556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Mods    : OS << "Mod       "; break;
557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ModRef  : OS << "Mod/Ref   "; break;
558894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: llvm_unreachable("Bad value for AccessTy!");
559894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isVolatile()) OS << "[volatile] ";
561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Forward)
562894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OS << " forwarding to " << (void*)Forward;
563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!empty()) {
566894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OS << "Pointers: ";
567894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (iterator I = begin(), E = end(); I != E; ++I) {
568894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (I != begin()) OS << ", ";
569894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      WriteAsOperand(OS << "(", I.getPointer());
570894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      OS << ", " << I.getSize() << ")";
571894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
572894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
57319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!UnknownInsts.empty()) {
57419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    OS << "\n    " << UnknownInsts.size() << " Unknown instructions: ";
57519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
576894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (i) OS << ", ";
57719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      WriteAsOperand(OS, UnknownInsts[i]);
578894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
579894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
580894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  OS << "\n";
581894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
582894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
583894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AliasSetTracker::print(raw_ostream &OS) const {
584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman     << PointerMap.size() << " pointer values.\n";
586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (const_iterator I = begin(), E = end(); I != E; ++I)
587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    I->print(OS);
588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  OS << "\n";
589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AliasSet::dump() const { print(dbgs()); }
592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AliasSetTracker::dump() const { print(dbgs()); }
593894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     ASTCallbackVH Class Implementation
596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
598894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid AliasSetTracker::ASTCallbackVH::deleted() {
599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
600894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  AST->deleteValue(getValPtr());
601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // this now dangles!
602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
603894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
60419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(Value *V) {
60519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  AST->copyValue(getValPtr(), V);
60619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
60719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanAliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  : CallbackVH(V), AST(ast) {}
610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanAliasSetTracker::ASTCallbackVH &
612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanAliasSetTracker::ASTCallbackVH::operator=(Value *V) {
613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return *this = ASTCallbackVH(V, AST);
614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
615894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                            AliasSetPrinter Pass
618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
620894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace {
621894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  class AliasSetPrinter : public FunctionPass {
622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    AliasSetTracker *Tracker;
623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  public:
624894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    static char ID; // Pass identification, replacement for typeid
62519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    AliasSetPrinter() : FunctionPass(ID) {
62619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      initializeAliasSetPrinterPass(*PassRegistry::getPassRegistry());
62719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
629894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      AU.setPreservesAll();
631894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      AU.addRequired<AliasAnalysis>();
632894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    virtual bool runOnFunction(Function &F) {
635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Tracker->add(&*I);
639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Tracker->print(errs());
640894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      delete Tracker;
641894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
642894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  };
644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
645894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
646894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanchar AliasSetPrinter::ID = 0;
64719bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanINITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets",
64819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                "Alias Set Printer", false, true)
64919bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanINITIALIZE_AG_DEPENDENCY(AliasAnalysis)
65019bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanINITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets",
65119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                "Alias Set Printer", false, true)
652