1 //===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
3 // This file implements the AliasSetTracker and AliasSet classes.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/Analysis/AliasSetTracker.h"
8 #include "llvm/Analysis/AliasAnalysis.h"
9 #include "llvm/iMemory.h"
10 #include "llvm/iOther.h"
11 #include "llvm/iTerminators.h"
12 #include "llvm/Pass.h"
13 #include "llvm/Assembly/Writer.h"
14 #include "llvm/Support/InstIterator.h"
16 /// mergeSetIn - Merge the specified alias set into this alias set...
18 void AliasSet::mergeSetIn(AliasSet &AS) {
19 assert(!AS.Forward && "Alias set is already forwarding!");
20 assert(!Forward && "This set is a forwarding set!!");
22 // Update the alias and access types of this set...
23 AccessTy |= AS.AccessTy;
24 AliasTy |= AS.AliasTy;
26 if (CallSites.empty()) { // Merge call sites...
27 if (!AS.CallSites.empty())
28 std::swap(CallSites, AS.CallSites);
29 } else if (!AS.CallSites.empty()) {
30 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
34 // FIXME: If AS's refcount is zero, nuke it now...
35 assert(RefCount != 0);
37 AS.Forward = this; // Forward across AS now...
38 RefCount++; // AS is now pointing to us...
40 // Merge the list of constituent pointers...
41 PtrListTail->second.setTail(AS.PtrListHead);
42 PtrListTail = AS.PtrListTail;
43 AS.PtrListHead = AS.PtrListTail = 0;
46 void AliasSetTracker::removeAliasSet(AliasSet *AS) {
50 void AliasSet::removeFromTracker(AliasSetTracker &AST) {
51 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
52 AST.removeAliasSet(this);
55 void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry){
56 assert(!Entry.second.hasAliasSet() && "Entry already in set!");
58 AliasAnalysis &AA = AST.getAliasAnalysis();
60 if (isMustAlias()) // Check to see if we have to downgrade to _may_ alias
61 if (Value *V = getSomePointer())
62 if (AA.alias(V, Entry.first) == AliasAnalysis::MayAlias)
65 Entry.second.setAliasSet(this);
67 // Add it to the end of the list...
69 PtrListTail->second.setTail(&Entry);
73 RefCount++; // Entry points to alias set...
76 void AliasSet::addCallSite(CallSite CS) {
77 CallSites.push_back(CS);
78 AliasTy = MayAlias; // FIXME: Too conservative
81 /// aliasesPointer - Return true if the specified pointer "may" (or must)
82 /// alias one of the members in the set.
84 bool AliasSet::aliasesPointer(const Value *Ptr, AliasAnalysis &AA) const {
85 if (AliasTy == MustAlias) {
86 assert(CallSites.empty() && "Illegal must alias set!");
88 // If this is a set of MustAliases, only check to see if the pointer aliases
89 // SOME value in the set...
90 Value *SomePtr = getSomePointer();
91 assert(SomePtr && "Empty must-alias set??");
92 return AA.alias(SomePtr, Ptr);
95 // If this is a may-alias set, we have to check all of the pointers in the set
96 // to be sure it doesn't alias the set...
97 for (iterator I = begin(), E = end(); I != E; ++I)
98 if (AA.alias(Ptr, *I))
101 // Check the call sites list and invoke list...
102 if (!CallSites.empty())
103 // FIXME: this is pessimistic!
109 bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
110 // FIXME: Too conservative!
115 /// findAliasSetForPointer - Given a pointer, find the one alias set to put the
116 /// instruction referring to the pointer into. If there are multiple alias sets
117 /// that may alias the pointer, merge them together and return the unified set.
119 AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr) {
120 AliasSet *FoundSet = 0;
121 for (iterator I = begin(), E = end(); I != E; ++I)
122 if (I->aliasesPointer(Ptr, AA)) {
123 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
124 FoundSet = I; // Remember it.
125 } else { // Otherwise, we must merge the sets...
126 FoundSet->mergeSetIn(*I); // Merge in contents...
133 AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
134 AliasSet *FoundSet = 0;
135 for (iterator I = begin(), E = end(); I != E; ++I)
136 if (I->aliasesCallSite(CS, AA)) {
137 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
138 FoundSet = I; // Remember it.
139 } else { // Otherwise, we must merge the sets...
140 FoundSet->mergeSetIn(*I); // Merge in contents...
150 /// getAliasSetForPointer - Return the alias set that the specified pointer
152 AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer) {
153 AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
155 // Check to see if the pointer is already known...
156 if (Entry.second.hasAliasSet()) {
158 return *Entry.second.getAliasSet(*this)->getForwardedTarget(*this);
159 } else if (AliasSet *AS = findAliasSetForPointer(Pointer)) {
160 // Add it to the alias set it aliases...
161 AS->addPointer(*this, Entry);
164 // Otherwise create a new alias set to hold the loaded pointer...
165 AliasSets.push_back(AliasSet());
166 AliasSets.back().addPointer(*this, Entry);
167 return AliasSets.back();
171 void AliasSetTracker::add(LoadInst *LI) {
172 addPointer(LI->getOperand(0), AliasSet::Refs);
175 void AliasSetTracker::add(StoreInst *SI) {
176 addPointer(SI->getOperand(1), AliasSet::Mods);
179 void AliasSetTracker::add(CallSite CS) {
180 AliasSet *AS = findAliasSetForCallSite(CS);
182 AliasSets.push_back(AliasSet());
183 AS = &AliasSets.back();
188 void AliasSetTracker::add(Instruction *I) {
189 // Dispatch to one of the other add methods...
190 if (LoadInst *LI = dyn_cast<LoadInst>(I))
192 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
194 else if (CallInst *CI = dyn_cast<CallInst>(I))
196 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
200 //===----------------------------------------------------------------------===//
201 // AliasSet/AliasSetTracker Printing Support
202 //===----------------------------------------------------------------------===//
204 void AliasSet::print(std::ostream &OS) const {
205 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
206 OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, ";
208 case NoModRef: OS << "No access "; break;
209 case Refs : OS << "Ref "; break;
210 case Mods : OS << "Mod "; break;
211 case ModRef : OS << "Mod/Ref "; break;
212 default: assert(0 && "Bad value for AccessTy!");
215 OS << " forwarding to " << (void*)Forward;
218 if (begin() != end()) {
220 for (iterator I = begin(), E = end(); I != E; ++I) {
221 if (I != begin()) OS << ", ";
222 WriteAsOperand(OS, *I);
225 if (!CallSites.empty()) {
226 OS << "\n " << CallSites.size() << " Call Sites: ";
227 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
229 WriteAsOperand(OS, CallSites[i].getCalledValue());
235 void AliasSetTracker::print(std::ostream &OS) const {
236 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
237 << PointerMap.size() << " pointer values.\n";
238 for (const_iterator I = begin(), E = end(); I != E; ++I)
243 void AliasSet::dump() const { print (std::cerr); }
244 void AliasSetTracker::dump() const { print(std::cerr); }
247 //===----------------------------------------------------------------------===//
248 // AliasSetPrinter Pass
249 //===----------------------------------------------------------------------===//
252 class AliasSetPrinter : public FunctionPass {
253 AliasSetTracker *Tracker;
255 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
256 AU.setPreservesAll();
257 AU.addRequired<AliasAnalysis>();
260 virtual bool runOnFunction(Function &F) {
261 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
263 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
268 /// print - Convert to human readable form
269 virtual void print(std::ostream &OS) const {
273 virtual void releaseMemory() {
277 RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer",
278 PassInfo::Analysis | PassInfo::Optimization);