AliasSetTracker.cpp revision 212904
1193323Sed//===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This file implements the AliasSetTracker and AliasSet classes.
11193323Sed//
12193323Sed//===----------------------------------------------------------------------===//
13193323Sed
14193323Sed#include "llvm/Analysis/AliasSetTracker.h"
15193323Sed#include "llvm/Analysis/AliasAnalysis.h"
16193323Sed#include "llvm/Instructions.h"
17193323Sed#include "llvm/IntrinsicInst.h"
18193323Sed#include "llvm/Pass.h"
19193323Sed#include "llvm/Type.h"
20193323Sed#include "llvm/Target/TargetData.h"
21193323Sed#include "llvm/Assembly/Writer.h"
22201360Srdivacky#include "llvm/Support/Debug.h"
23198090Srdivacky#include "llvm/Support/ErrorHandling.h"
24193323Sed#include "llvm/Support/InstIterator.h"
25198090Srdivacky#include "llvm/Support/raw_ostream.h"
26193323Sedusing namespace llvm;
27193323Sed
28193323Sed/// mergeSetIn - Merge the specified alias set into this alias set.
29193323Sed///
30193323Sedvoid AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
31193323Sed  assert(!AS.Forward && "Alias set is already forwarding!");
32193323Sed  assert(!Forward && "This set is a forwarding set!!");
33193323Sed
34193323Sed  // Update the alias and access types of this set...
35193323Sed  AccessTy |= AS.AccessTy;
36193323Sed  AliasTy  |= AS.AliasTy;
37212904Sdim  Volatile |= AS.Volatile;
38193323Sed
39193323Sed  if (AliasTy == MustAlias) {
40193323Sed    // Check that these two merged sets really are must aliases.  Since both
41193323Sed    // used to be must-alias sets, we can just check any pointer from each set
42193323Sed    // for aliasing.
43193323Sed    AliasAnalysis &AA = AST.getAliasAnalysis();
44193323Sed    PointerRec *L = getSomePointer();
45193323Sed    PointerRec *R = AS.getSomePointer();
46193323Sed
47193323Sed    // If the pointers are not a must-alias pair, this set becomes a may alias.
48193323Sed    if (AA.alias(L->getValue(), L->getSize(), R->getValue(), R->getSize())
49193323Sed        != AliasAnalysis::MustAlias)
50193323Sed      AliasTy = MayAlias;
51193323Sed  }
52193323Sed
53193323Sed  if (CallSites.empty()) {            // Merge call sites...
54193323Sed    if (!AS.CallSites.empty())
55193323Sed      std::swap(CallSites, AS.CallSites);
56193323Sed  } else if (!AS.CallSites.empty()) {
57193323Sed    CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
58193323Sed    AS.CallSites.clear();
59193323Sed  }
60193323Sed
61193323Sed  AS.Forward = this;  // Forward across AS now...
62193323Sed  addRef();           // AS is now pointing to us...
63193323Sed
64193323Sed  // Merge the list of constituent pointers...
65193323Sed  if (AS.PtrList) {
66193323Sed    *PtrListEnd = AS.PtrList;
67193323Sed    AS.PtrList->setPrevInList(PtrListEnd);
68193323Sed    PtrListEnd = AS.PtrListEnd;
69193323Sed
70193323Sed    AS.PtrList = 0;
71193323Sed    AS.PtrListEnd = &AS.PtrList;
72193323Sed    assert(*AS.PtrListEnd == 0 && "End of list is not null?");
73193323Sed  }
74193323Sed}
75193323Sed
76193323Sedvoid AliasSetTracker::removeAliasSet(AliasSet *AS) {
77193323Sed  if (AliasSet *Fwd = AS->Forward) {
78193323Sed    Fwd->dropRef(*this);
79193323Sed    AS->Forward = 0;
80193323Sed  }
81193323Sed  AliasSets.erase(AS);
82193323Sed}
83193323Sed
84193323Sedvoid AliasSet::removeFromTracker(AliasSetTracker &AST) {
85193323Sed  assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
86193323Sed  AST.removeAliasSet(this);
87193323Sed}
88193323Sed
89193323Sedvoid AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
90193323Sed                          unsigned Size, bool KnownMustAlias) {
91193323Sed  assert(!Entry.hasAliasSet() && "Entry already in set!");
92193323Sed
93193323Sed  // Check to see if we have to downgrade to _may_ alias.
94193323Sed  if (isMustAlias() && !KnownMustAlias)
95193323Sed    if (PointerRec *P = getSomePointer()) {
96193323Sed      AliasAnalysis &AA = AST.getAliasAnalysis();
97193323Sed      AliasAnalysis::AliasResult Result =
98193323Sed        AA.alias(P->getValue(), P->getSize(), Entry.getValue(), Size);
99193323Sed      if (Result == AliasAnalysis::MayAlias)
100193323Sed        AliasTy = MayAlias;
101193323Sed      else                  // First entry of must alias must have maximum size!
102193323Sed        P->updateSize(Size);
103193323Sed      assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
104193323Sed    }
105193323Sed
106193323Sed  Entry.setAliasSet(this);
107193323Sed  Entry.updateSize(Size);
108193323Sed
109193323Sed  // Add it to the end of the list...
110193323Sed  assert(*PtrListEnd == 0 && "End of list is not null?");
111193323Sed  *PtrListEnd = &Entry;
112193323Sed  PtrListEnd = Entry.setPrevInList(PtrListEnd);
113193323Sed  assert(*PtrListEnd == 0 && "End of list is not null?");
114212904Sdim  addRef();               // Entry points to alias set.
115193323Sed}
116193323Sed
117193323Sedvoid AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
118212904Sdim  CallSites.push_back(CS.getInstruction());
119193323Sed
120193323Sed  AliasAnalysis::ModRefBehavior Behavior = AA.getModRefBehavior(CS);
121193323Sed  if (Behavior == AliasAnalysis::DoesNotAccessMemory)
122193323Sed    return;
123193323Sed  else if (Behavior == AliasAnalysis::OnlyReadsMemory) {
124193323Sed    AliasTy = MayAlias;
125193323Sed    AccessTy |= Refs;
126193323Sed    return;
127193323Sed  }
128193323Sed
129193323Sed  // FIXME: This should use mod/ref information to make this not suck so bad
130193323Sed  AliasTy = MayAlias;
131193323Sed  AccessTy = ModRef;
132193323Sed}
133193323Sed
134193323Sed/// aliasesPointer - Return true if the specified pointer "may" (or must)
135193323Sed/// alias one of the members in the set.
136193323Sed///
137193323Sedbool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
138193323Sed                              AliasAnalysis &AA) const {
139193323Sed  if (AliasTy == MustAlias) {
140193323Sed    assert(CallSites.empty() && "Illegal must alias set!");
141193323Sed
142193323Sed    // If this is a set of MustAliases, only check to see if the pointer aliases
143212904Sdim    // SOME value in the set.
144193323Sed    PointerRec *SomePtr = getSomePointer();
145193323Sed    assert(SomePtr && "Empty must-alias set??");
146193323Sed    return AA.alias(SomePtr->getValue(), SomePtr->getSize(), Ptr, Size);
147193323Sed  }
148193323Sed
149193323Sed  // If this is a may-alias set, we have to check all of the pointers in the set
150193323Sed  // to be sure it doesn't alias the set...
151193323Sed  for (iterator I = begin(), E = end(); I != E; ++I)
152193323Sed    if (AA.alias(Ptr, Size, I.getPointer(), I.getSize()))
153193323Sed      return true;
154193323Sed
155193323Sed  // Check the call sites list and invoke list...
156193323Sed  if (!CallSites.empty()) {
157193323Sed    for (unsigned i = 0, e = CallSites.size(); i != e; ++i)
158212904Sdim      if (AA.getModRefInfo(CallSites[i], Ptr, Size) != AliasAnalysis::NoModRef)
159193323Sed        return true;
160193323Sed  }
161193323Sed
162193323Sed  return false;
163193323Sed}
164193323Sed
165193323Sedbool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
166193323Sed  if (AA.doesNotAccessMemory(CS))
167193323Sed    return false;
168193323Sed
169212904Sdim  for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
170212904Sdim    if (AA.getModRefInfo(getCallSite(i), CS) != AliasAnalysis::NoModRef ||
171212904Sdim        AA.getModRefInfo(CS, getCallSite(i)) != AliasAnalysis::NoModRef)
172193323Sed      return true;
173212904Sdim  }
174193323Sed
175193323Sed  for (iterator I = begin(), E = end(); I != E; ++I)
176193323Sed    if (AA.getModRefInfo(CS, I.getPointer(), I.getSize()) !=
177193323Sed           AliasAnalysis::NoModRef)
178193323Sed      return true;
179193323Sed
180193323Sed  return false;
181193323Sed}
182193323Sed
183193323Sedvoid AliasSetTracker::clear() {
184193323Sed  // Delete all the PointerRec entries.
185198090Srdivacky  for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
186198090Srdivacky       I != E; ++I)
187193323Sed    I->second->eraseFromList();
188193323Sed
189193323Sed  PointerMap.clear();
190193323Sed
191193323Sed  // The alias sets should all be clear now.
192193323Sed  AliasSets.clear();
193193323Sed}
194193323Sed
195193323Sed
196193323Sed/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
197193323Sed/// instruction referring to the pointer into.  If there are multiple alias sets
198193323Sed/// that may alias the pointer, merge them together and return the unified set.
199193323Sed///
200193323SedAliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
201193323Sed                                                  unsigned Size) {
202193323Sed  AliasSet *FoundSet = 0;
203212904Sdim  for (iterator I = begin(), E = end(); I != E; ++I) {
204212904Sdim    if (I->Forward || !I->aliasesPointer(Ptr, Size, AA)) continue;
205212904Sdim
206212904Sdim    if (FoundSet == 0) {  // If this is the first alias set ptr can go into.
207212904Sdim      FoundSet = I;       // Remember it.
208212904Sdim    } else {              // Otherwise, we must merge the sets.
209212904Sdim      FoundSet->mergeSetIn(*I, *this);     // Merge in contents.
210193323Sed    }
211212904Sdim  }
212193323Sed
213193323Sed  return FoundSet;
214193323Sed}
215193323Sed
216193323Sed/// containsPointer - Return true if the specified location is represented by
217193323Sed/// this alias set, false otherwise.  This does not modify the AST object or
218193323Sed/// alias sets.
219193323Sedbool AliasSetTracker::containsPointer(Value *Ptr, unsigned Size) const {
220193323Sed  for (const_iterator I = begin(), E = end(); I != E; ++I)
221193323Sed    if (!I->Forward && I->aliasesPointer(Ptr, Size, AA))
222193323Sed      return true;
223193323Sed  return false;
224193323Sed}
225193323Sed
226193323Sed
227193323Sed
228193323SedAliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
229193323Sed  AliasSet *FoundSet = 0;
230212904Sdim  for (iterator I = begin(), E = end(); I != E; ++I) {
231212904Sdim    if (I->Forward || !I->aliasesCallSite(CS, AA))
232212904Sdim      continue;
233212904Sdim
234212904Sdim    if (FoundSet == 0)        // If this is the first alias set ptr can go into.
235212904Sdim      FoundSet = I;           // Remember it.
236212904Sdim    else if (!I->Forward)     // Otherwise, we must merge the sets.
237212904Sdim      FoundSet->mergeSetIn(*I, *this);     // Merge in contents.
238212904Sdim  }
239193323Sed  return FoundSet;
240193323Sed}
241193323Sed
242193323Sed
243193323Sed
244193323Sed
245193323Sed/// getAliasSetForPointer - Return the alias set that the specified pointer
246193323Sed/// lives in.
247193323SedAliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size,
248193323Sed                                                 bool *New) {
249193323Sed  AliasSet::PointerRec &Entry = getEntryFor(Pointer);
250193323Sed
251212904Sdim  // Check to see if the pointer is already known.
252193323Sed  if (Entry.hasAliasSet()) {
253193323Sed    Entry.updateSize(Size);
254193323Sed    // Return the set!
255193323Sed    return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
256212904Sdim  }
257212904Sdim
258212904Sdim  if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
259212904Sdim    // Add it to the alias set it aliases.
260193323Sed    AS->addPointer(*this, Entry, Size);
261193323Sed    return *AS;
262193323Sed  }
263212904Sdim
264212904Sdim  if (New) *New = true;
265212904Sdim  // Otherwise create a new alias set to hold the loaded pointer.
266212904Sdim  AliasSets.push_back(new AliasSet());
267212904Sdim  AliasSets.back().addPointer(*this, Entry, Size);
268212904Sdim  return AliasSets.back();
269193323Sed}
270193323Sed
271193323Sedbool AliasSetTracker::add(Value *Ptr, unsigned Size) {
272193323Sed  bool NewPtr;
273193323Sed  addPointer(Ptr, Size, AliasSet::NoModRef, NewPtr);
274193323Sed  return NewPtr;
275193323Sed}
276193323Sed
277193323Sed
278193323Sedbool AliasSetTracker::add(LoadInst *LI) {
279193323Sed  bool NewPtr;
280193323Sed  AliasSet &AS = addPointer(LI->getOperand(0),
281198090Srdivacky                            AA.getTypeStoreSize(LI->getType()),
282193323Sed                            AliasSet::Refs, NewPtr);
283193323Sed  if (LI->isVolatile()) AS.setVolatile();
284193323Sed  return NewPtr;
285193323Sed}
286193323Sed
287193323Sedbool AliasSetTracker::add(StoreInst *SI) {
288193323Sed  bool NewPtr;
289193323Sed  Value *Val = SI->getOperand(0);
290193323Sed  AliasSet &AS = addPointer(SI->getOperand(1),
291198090Srdivacky                            AA.getTypeStoreSize(Val->getType()),
292193323Sed                            AliasSet::Mods, NewPtr);
293193323Sed  if (SI->isVolatile()) AS.setVolatile();
294193323Sed  return NewPtr;
295193323Sed}
296193323Sed
297193323Sedbool AliasSetTracker::add(VAArgInst *VAAI) {
298193323Sed  bool NewPtr;
299193323Sed  addPointer(VAAI->getOperand(0), ~0, AliasSet::ModRef, NewPtr);
300193323Sed  return NewPtr;
301193323Sed}
302193323Sed
303193323Sed
304193323Sedbool AliasSetTracker::add(CallSite CS) {
305193323Sed  if (isa<DbgInfoIntrinsic>(CS.getInstruction()))
306193323Sed    return true; // Ignore DbgInfo Intrinsics.
307193323Sed  if (AA.doesNotAccessMemory(CS))
308193323Sed    return true; // doesn't alias anything
309193323Sed
310193323Sed  AliasSet *AS = findAliasSetForCallSite(CS);
311212904Sdim  if (AS) {
312193323Sed    AS->addCallSite(CS, AA);
313193323Sed    return false;
314193323Sed  }
315212904Sdim  AliasSets.push_back(new AliasSet());
316212904Sdim  AS = &AliasSets.back();
317212904Sdim  AS->addCallSite(CS, AA);
318212904Sdim  return true;
319193323Sed}
320193323Sed
321193323Sedbool AliasSetTracker::add(Instruction *I) {
322212904Sdim  // Dispatch to one of the other add methods.
323193323Sed  if (LoadInst *LI = dyn_cast<LoadInst>(I))
324193323Sed    return add(LI);
325212904Sdim  if (StoreInst *SI = dyn_cast<StoreInst>(I))
326193323Sed    return add(SI);
327212904Sdim  if (CallInst *CI = dyn_cast<CallInst>(I))
328193323Sed    return add(CI);
329212904Sdim  if (InvokeInst *II = dyn_cast<InvokeInst>(I))
330193323Sed    return add(II);
331212904Sdim  if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
332193323Sed    return add(VAAI);
333193323Sed  return true;
334193323Sed}
335193323Sed
336193323Sedvoid AliasSetTracker::add(BasicBlock &BB) {
337193323Sed  for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
338193323Sed    add(I);
339193323Sed}
340193323Sed
341193323Sedvoid AliasSetTracker::add(const AliasSetTracker &AST) {
342193323Sed  assert(&AA == &AST.AA &&
343193323Sed         "Merging AliasSetTracker objects with different Alias Analyses!");
344193323Sed
345193323Sed  // Loop over all of the alias sets in AST, adding the pointers contained
346193323Sed  // therein into the current alias sets.  This can cause alias sets to be
347193323Sed  // merged together in the current AST.
348212904Sdim  for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I) {
349212904Sdim    if (I->Forward) continue;   // Ignore forwarding alias sets
350212904Sdim
351212904Sdim    AliasSet &AS = const_cast<AliasSet&>(*I);
352193323Sed
353212904Sdim    // If there are any call sites in the alias set, add them to this AST.
354212904Sdim    for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
355212904Sdim      add(AS.CallSites[i]);
356193323Sed
357212904Sdim    // Loop over all of the pointers in this alias set.
358212904Sdim    bool X;
359212904Sdim    for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI) {
360212904Sdim      AliasSet &NewAS = addPointer(ASI.getPointer(), ASI.getSize(),
361212904Sdim                                   (AliasSet::AccessType)AS.AccessTy, X);
362212904Sdim      if (AS.isVolatile()) NewAS.setVolatile();
363193323Sed    }
364212904Sdim  }
365193323Sed}
366193323Sed
367193323Sed/// remove - Remove the specified (potentially non-empty) alias set from the
368193323Sed/// tracker.
369193323Sedvoid AliasSetTracker::remove(AliasSet &AS) {
370193323Sed  // Drop all call sites.
371193323Sed  AS.CallSites.clear();
372193323Sed
373193323Sed  // Clear the alias set.
374193323Sed  unsigned NumRefs = 0;
375193323Sed  while (!AS.empty()) {
376193323Sed    AliasSet::PointerRec *P = AS.PtrList;
377193323Sed
378193323Sed    Value *ValToRemove = P->getValue();
379193323Sed
380193323Sed    // Unlink and delete entry from the list of values.
381193323Sed    P->eraseFromList();
382193323Sed
383193323Sed    // Remember how many references need to be dropped.
384193323Sed    ++NumRefs;
385193323Sed
386193323Sed    // Finally, remove the entry.
387193323Sed    PointerMap.erase(ValToRemove);
388193323Sed  }
389193323Sed
390193323Sed  // Stop using the alias set, removing it.
391193323Sed  AS.RefCount -= NumRefs;
392193323Sed  if (AS.RefCount == 0)
393193323Sed    AS.removeFromTracker(*this);
394193323Sed}
395193323Sed
396193323Sedbool AliasSetTracker::remove(Value *Ptr, unsigned Size) {
397193323Sed  AliasSet *AS = findAliasSetForPointer(Ptr, Size);
398193323Sed  if (!AS) return false;
399193323Sed  remove(*AS);
400193323Sed  return true;
401193323Sed}
402193323Sed
403193323Sedbool AliasSetTracker::remove(LoadInst *LI) {
404198090Srdivacky  unsigned Size = AA.getTypeStoreSize(LI->getType());
405193323Sed  AliasSet *AS = findAliasSetForPointer(LI->getOperand(0), Size);
406193323Sed  if (!AS) return false;
407193323Sed  remove(*AS);
408193323Sed  return true;
409193323Sed}
410193323Sed
411193323Sedbool AliasSetTracker::remove(StoreInst *SI) {
412198090Srdivacky  unsigned Size = AA.getTypeStoreSize(SI->getOperand(0)->getType());
413193323Sed  AliasSet *AS = findAliasSetForPointer(SI->getOperand(1), Size);
414193323Sed  if (!AS) return false;
415193323Sed  remove(*AS);
416193323Sed  return true;
417193323Sed}
418193323Sed
419193323Sedbool AliasSetTracker::remove(VAArgInst *VAAI) {
420193323Sed  AliasSet *AS = findAliasSetForPointer(VAAI->getOperand(0), ~0);
421193323Sed  if (!AS) return false;
422193323Sed  remove(*AS);
423193323Sed  return true;
424193323Sed}
425193323Sed
426193323Sedbool AliasSetTracker::remove(CallSite CS) {
427193323Sed  if (AA.doesNotAccessMemory(CS))
428193323Sed    return false; // doesn't alias anything
429193323Sed
430193323Sed  AliasSet *AS = findAliasSetForCallSite(CS);
431193323Sed  if (!AS) return false;
432193323Sed  remove(*AS);
433193323Sed  return true;
434193323Sed}
435193323Sed
436193323Sedbool AliasSetTracker::remove(Instruction *I) {
437193323Sed  // Dispatch to one of the other remove methods...
438193323Sed  if (LoadInst *LI = dyn_cast<LoadInst>(I))
439193323Sed    return remove(LI);
440212904Sdim  if (StoreInst *SI = dyn_cast<StoreInst>(I))
441193323Sed    return remove(SI);
442212904Sdim  if (CallInst *CI = dyn_cast<CallInst>(I))
443193323Sed    return remove(CI);
444212904Sdim  if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
445193323Sed    return remove(VAAI);
446193323Sed  return true;
447193323Sed}
448193323Sed
449193323Sed
450193323Sed// deleteValue method - This method is used to remove a pointer value from the
451193323Sed// AliasSetTracker entirely.  It should be used when an instruction is deleted
452193323Sed// from the program to update the AST.  If you don't use this, you would have
453193323Sed// dangling pointers to deleted instructions.
454193323Sed//
455193323Sedvoid AliasSetTracker::deleteValue(Value *PtrVal) {
456193323Sed  // Notify the alias analysis implementation that this value is gone.
457193323Sed  AA.deleteValue(PtrVal);
458193323Sed
459193323Sed  // If this is a call instruction, remove the callsite from the appropriate
460212904Sdim  // AliasSet (if present).
461212904Sdim  if (CallSite CS = PtrVal) {
462212904Sdim    if (!AA.doesNotAccessMemory(CS)) {
463212904Sdim      // Scan all the alias sets to see if this call site is contained.
464212904Sdim      for (iterator I = begin(), E = end(); I != E; ++I) {
465212904Sdim        if (I->Forward) continue;
466212904Sdim
467212904Sdim        I->removeCallSite(CS);
468212904Sdim      }
469212904Sdim    }
470212904Sdim  }
471193323Sed
472193323Sed  // First, look up the PointerRec for this pointer.
473198090Srdivacky  PointerMapType::iterator I = PointerMap.find(PtrVal);
474193323Sed  if (I == PointerMap.end()) return;  // Noop
475193323Sed
476193323Sed  // If we found one, remove the pointer from the alias set it is in.
477193323Sed  AliasSet::PointerRec *PtrValEnt = I->second;
478193323Sed  AliasSet *AS = PtrValEnt->getAliasSet(*this);
479193323Sed
480193323Sed  // Unlink and delete from the list of values.
481193323Sed  PtrValEnt->eraseFromList();
482193323Sed
483193323Sed  // Stop using the alias set.
484193323Sed  AS->dropRef(*this);
485193323Sed
486193323Sed  PointerMap.erase(I);
487193323Sed}
488193323Sed
489193323Sed// copyValue - This method should be used whenever a preexisting value in the
490193323Sed// program is copied or cloned, introducing a new value.  Note that it is ok for
491193323Sed// clients that use this method to introduce the same value multiple times: if
492193323Sed// the tracker already knows about a value, it will ignore the request.
493193323Sed//
494193323Sedvoid AliasSetTracker::copyValue(Value *From, Value *To) {
495193323Sed  // Notify the alias analysis implementation that this value is copied.
496193323Sed  AA.copyValue(From, To);
497193323Sed
498193323Sed  // First, look up the PointerRec for this pointer.
499198090Srdivacky  PointerMapType::iterator I = PointerMap.find(From);
500193323Sed  if (I == PointerMap.end())
501193323Sed    return;  // Noop
502193323Sed  assert(I->second->hasAliasSet() && "Dead entry?");
503193323Sed
504193323Sed  AliasSet::PointerRec &Entry = getEntryFor(To);
505193323Sed  if (Entry.hasAliasSet()) return;    // Already in the tracker!
506193323Sed
507193323Sed  // Add it to the alias set it aliases...
508193323Sed  I = PointerMap.find(From);
509193323Sed  AliasSet *AS = I->second->getAliasSet(*this);
510193323Sed  AS->addPointer(*this, Entry, I->second->getSize(), true);
511193323Sed}
512193323Sed
513193323Sed
514193323Sed
515193323Sed//===----------------------------------------------------------------------===//
516193323Sed//               AliasSet/AliasSetTracker Printing Support
517193323Sed//===----------------------------------------------------------------------===//
518193323Sed
519198090Srdivackyvoid AliasSet::print(raw_ostream &OS) const {
520212904Sdim  OS << "  AliasSet[" << (void*)this << ", " << RefCount << "] ";
521193323Sed  OS << (AliasTy == MustAlias ? "must" : "may") << " alias, ";
522193323Sed  switch (AccessTy) {
523193323Sed  case NoModRef: OS << "No access "; break;
524193323Sed  case Refs    : OS << "Ref       "; break;
525193323Sed  case Mods    : OS << "Mod       "; break;
526193323Sed  case ModRef  : OS << "Mod/Ref   "; break;
527198090Srdivacky  default: llvm_unreachable("Bad value for AccessTy!");
528193323Sed  }
529193323Sed  if (isVolatile()) OS << "[volatile] ";
530193323Sed  if (Forward)
531193323Sed    OS << " forwarding to " << (void*)Forward;
532193323Sed
533193323Sed
534193323Sed  if (!empty()) {
535193323Sed    OS << "Pointers: ";
536193323Sed    for (iterator I = begin(), E = end(); I != E; ++I) {
537193323Sed      if (I != begin()) OS << ", ";
538193323Sed      WriteAsOperand(OS << "(", I.getPointer());
539193323Sed      OS << ", " << I.getSize() << ")";
540193323Sed    }
541193323Sed  }
542193323Sed  if (!CallSites.empty()) {
543193323Sed    OS << "\n    " << CallSites.size() << " Call Sites: ";
544193323Sed    for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
545193323Sed      if (i) OS << ", ";
546212904Sdim      WriteAsOperand(OS, CallSites[i]);
547193323Sed    }
548193323Sed  }
549193323Sed  OS << "\n";
550193323Sed}
551193323Sed
552198090Srdivackyvoid AliasSetTracker::print(raw_ostream &OS) const {
553193323Sed  OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
554193323Sed     << PointerMap.size() << " pointer values.\n";
555193323Sed  for (const_iterator I = begin(), E = end(); I != E; ++I)
556193323Sed    I->print(OS);
557193323Sed  OS << "\n";
558193323Sed}
559193323Sed
560201360Srdivackyvoid AliasSet::dump() const { print(dbgs()); }
561201360Srdivackyvoid AliasSetTracker::dump() const { print(dbgs()); }
562193323Sed
563193323Sed//===----------------------------------------------------------------------===//
564198090Srdivacky//                     ASTCallbackVH Class Implementation
565198090Srdivacky//===----------------------------------------------------------------------===//
566198090Srdivacky
567198090Srdivackyvoid AliasSetTracker::ASTCallbackVH::deleted() {
568198090Srdivacky  assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
569198090Srdivacky  AST->deleteValue(getValPtr());
570198090Srdivacky  // this now dangles!
571198090Srdivacky}
572198090Srdivacky
573198090SrdivackyAliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
574198090Srdivacky  : CallbackVH(V), AST(ast) {}
575198090Srdivacky
576198090SrdivackyAliasSetTracker::ASTCallbackVH &
577198090SrdivackyAliasSetTracker::ASTCallbackVH::operator=(Value *V) {
578198090Srdivacky  return *this = ASTCallbackVH(V, AST);
579198090Srdivacky}
580198090Srdivacky
581198090Srdivacky//===----------------------------------------------------------------------===//
582193323Sed//                            AliasSetPrinter Pass
583193323Sed//===----------------------------------------------------------------------===//
584193323Sed
585193323Sednamespace {
586198892Srdivacky  class AliasSetPrinter : public FunctionPass {
587193323Sed    AliasSetTracker *Tracker;
588193323Sed  public:
589193323Sed    static char ID; // Pass identification, replacement for typeid
590212904Sdim    AliasSetPrinter() : FunctionPass(ID) {}
591193323Sed
592193323Sed    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
593193323Sed      AU.setPreservesAll();
594193323Sed      AU.addRequired<AliasAnalysis>();
595193323Sed    }
596193323Sed
597193323Sed    virtual bool runOnFunction(Function &F) {
598193323Sed      Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
599193323Sed
600193323Sed      for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
601193323Sed        Tracker->add(&*I);
602198090Srdivacky      Tracker->print(errs());
603193323Sed      delete Tracker;
604193323Sed      return false;
605193323Sed    }
606193323Sed  };
607193323Sed}
608193323Sed
609193323Sedchar AliasSetPrinter::ID = 0;
610212904SdimINITIALIZE_PASS(AliasSetPrinter, "print-alias-sets",
611212904Sdim                "Alias Set Printer", false, true);
612