Put all LLVM code into the llvm namespace, as per bug 109.
[oota-llvm.git] / lib / Analysis / AliasSetTracker.cpp
1 //===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the AliasSetTracker and AliasSet classes.
11 // 
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Analysis/AliasSetTracker.h"
15 #include "llvm/Analysis/AliasAnalysis.h"
16 #include "llvm/iMemory.h"
17 #include "llvm/iOther.h"
18 #include "llvm/iTerminators.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Target/TargetData.h"
21 #include "llvm/Assembly/Writer.h"
22 #include "llvm/Support/InstIterator.h"
23
24 namespace llvm {
25
26 /// mergeSetIn - Merge the specified alias set into this alias set...
27 ///
28 void AliasSet::mergeSetIn(AliasSet &AS) {
29   assert(!AS.Forward && "Alias set is already forwarding!");
30   assert(!Forward && "This set is a forwarding set!!");
31
32   // Update the alias and access types of this set...
33   AccessTy |= AS.AccessTy;
34   AliasTy  |= AS.AliasTy;
35
36   if (CallSites.empty()) {            // Merge call sites...
37     if (!AS.CallSites.empty())
38       std::swap(CallSites, AS.CallSites);
39   } else if (!AS.CallSites.empty()) {
40     CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
41     AS.CallSites.clear();
42   }
43   
44   // FIXME: If AS's refcount is zero, nuke it now...
45   assert(RefCount != 0);
46
47   AS.Forward = this;  // Forward across AS now...
48   RefCount++;         // AS is now pointing to us...
49
50   // Merge the list of constituent pointers...
51   PtrListTail->second.setTail(AS.PtrListHead);
52   PtrListTail = AS.PtrListTail;
53   AS.PtrListHead = AS.PtrListTail = 0;
54 }
55
56 void AliasSetTracker::removeAliasSet(AliasSet *AS) {
57   AliasSets.erase(AS);
58 }
59
60 void AliasSet::removeFromTracker(AliasSetTracker &AST) {
61   assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
62   AST.removeAliasSet(this);
63 }
64
65 void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
66                           unsigned Size) {
67   assert(!Entry.second.hasAliasSet() && "Entry already in set!");
68
69   AliasAnalysis &AA = AST.getAliasAnalysis();
70
71   if (isMustAlias())    // Check to see if we have to downgrade to _may_ alias
72     if (HashNodePair *P = getSomePointer()) {
73       AliasAnalysis::AliasResult Result =
74         AA.alias(P->first, P->second.getSize(), Entry.first, Size);
75       if (Result == AliasAnalysis::MayAlias)
76         AliasTy = MayAlias;
77       else                  // First entry of must alias must have maximum size!
78         P->second.updateSize(Size);
79       assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
80     }
81
82   Entry.second.setAliasSet(this);
83   Entry.second.updateSize(Size);
84
85   // Add it to the end of the list...
86   if (PtrListTail)
87     PtrListTail->second.setTail(&Entry);
88   else
89     PtrListHead = &Entry;
90   PtrListTail = &Entry;
91   RefCount++;               // Entry points to alias set...
92 }
93
94 void AliasSet::addCallSite(CallSite CS) {
95   CallSites.push_back(CS);
96   AliasTy = MayAlias;         // FIXME: Too conservative?
97   AccessTy = ModRef;
98 }
99
100 /// aliasesPointer - Return true if the specified pointer "may" (or must)
101 /// alias one of the members in the set.
102 ///
103 bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
104                               AliasAnalysis &AA) const {
105   if (AliasTy == MustAlias) {
106     assert(CallSites.empty() && "Illegal must alias set!");
107
108     // If this is a set of MustAliases, only check to see if the pointer aliases
109     // SOME value in the set...
110     HashNodePair *SomePtr = getSomePointer();
111     assert(SomePtr && "Empty must-alias set??");
112     return AA.alias(SomePtr->first, SomePtr->second.getSize(), Ptr, Size);
113   }
114
115   // If this is a may-alias set, we have to check all of the pointers in the set
116   // to be sure it doesn't alias the set...
117   for (iterator I = begin(), E = end(); I != E; ++I)
118     if (AA.alias(Ptr, Size, I->first, I->second.getSize()))
119       return true;
120
121   // Check the call sites list and invoke list...
122   if (!CallSites.empty())
123     // FIXME: this is pessimistic!
124     return true;
125
126   return false;
127 }
128
129 bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
130   // FIXME: Too conservative!
131   return true;
132 }
133
134
135 /// findAliasSetForPointer - Given a pointer, find the one alias set to put the
136 /// instruction referring to the pointer into.  If there are multiple alias sets
137 /// that may alias the pointer, merge them together and return the unified set.
138 ///
139 AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
140                                                   unsigned Size) {
141   AliasSet *FoundSet = 0;
142   for (iterator I = begin(), E = end(); I != E; ++I)
143     if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
144       if (FoundSet == 0) {  // If this is the first alias set ptr can go into...
145         FoundSet = I;       // Remember it.
146       } else {              // Otherwise, we must merge the sets...
147         FoundSet->mergeSetIn(*I);     // Merge in contents...
148       }
149     }
150
151   return FoundSet;
152 }
153
154 AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
155   AliasSet *FoundSet = 0;
156   for (iterator I = begin(), E = end(); I != E; ++I)
157     if (!I->Forward && I->aliasesCallSite(CS, AA)) {
158       if (FoundSet == 0) {  // If this is the first alias set ptr can go into...
159         FoundSet = I;       // Remember it.
160       } else if (!I->Forward) {     // Otherwise, we must merge the sets...
161         FoundSet->mergeSetIn(*I);     // Merge in contents...
162       }
163     }
164
165   return FoundSet;
166 }
167
168
169
170
171 /// getAliasSetForPointer - Return the alias set that the specified pointer
172 /// lives in...
173 AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size){
174   AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
175
176   // Check to see if the pointer is already known...
177   if (Entry.second.hasAliasSet() && Size <= Entry.second.getSize()) {
178     // Return the set!
179     return *Entry.second.getAliasSet(*this)->getForwardedTarget(*this);
180   } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
181     // Add it to the alias set it aliases...
182     AS->addPointer(*this, Entry, Size);
183     return *AS;
184   } else {
185     // Otherwise create a new alias set to hold the loaded pointer...
186     AliasSets.push_back(AliasSet());
187     AliasSets.back().addPointer(*this, Entry, Size);
188     return AliasSets.back();
189   }
190 }
191
192 void AliasSetTracker::add(LoadInst *LI) {
193   addPointer(LI->getOperand(0),
194              AA.getTargetData().getTypeSize(LI->getType()), AliasSet::Refs);
195 }
196
197 void AliasSetTracker::add(StoreInst *SI) {
198   addPointer(SI->getOperand(1),
199              AA.getTargetData().getTypeSize(SI->getOperand(0)->getType()),
200              AliasSet::Mods);
201 }
202
203 void AliasSetTracker::add(CallSite CS) {
204   AliasSet *AS = findAliasSetForCallSite(CS);
205   if (!AS) {
206     AliasSets.push_back(AliasSet());
207     AS = &AliasSets.back();
208   }
209   AS->addCallSite(CS); 
210 }
211
212 void AliasSetTracker::add(Instruction *I) {
213   // Dispatch to one of the other add methods...
214   if (LoadInst *LI = dyn_cast<LoadInst>(I))
215     add(LI);
216   else if (StoreInst *SI = dyn_cast<StoreInst>(I))
217     add(SI);
218   else if (CallInst *CI = dyn_cast<CallInst>(I))
219     add(CI);
220   else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
221     add(II);
222 }
223
224 void AliasSetTracker::add(BasicBlock &BB) {
225   for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
226     add(I);
227 }
228
229 void AliasSetTracker::add(const AliasSetTracker &AST) {
230   assert(&AA == &AST.AA &&
231          "Merging AliasSetTracker objects with different Alias Analyses!");
232
233   // Loop over all of the alias sets in AST, adding the pointers contained
234   // therein into the current alias sets.  This can cause alias sets to be
235   // merged together in the current AST.
236   for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
237     if (!I->Forward) {   // Ignore forwarding alias sets
238       AliasSet &AS = const_cast<AliasSet&>(*I);
239
240       // If there are any call sites in the alias set, add them to this AST.
241       for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
242         add(AS.CallSites[i]);
243
244       // Loop over all of the pointers in this alias set...
245       AliasSet::iterator I = AS.begin(), E = AS.end();
246       for (; I != E; ++I)
247         addPointer(I->first, I->second.getSize(),
248                    (AliasSet::AccessType)AS.AccessTy);
249     }
250 }
251
252 //===----------------------------------------------------------------------===//
253 //               AliasSet/AliasSetTracker Printing Support
254 //===----------------------------------------------------------------------===//
255
256 void AliasSet::print(std::ostream &OS) const {
257   OS << "  AliasSet[" << (void*)this << "," << RefCount << "] ";
258   OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, ";
259   switch (AccessTy) {
260   case NoModRef: OS << "No access "; break;
261   case Refs    : OS << "Ref       "; break;
262   case Mods    : OS << "Mod       "; break;
263   case ModRef  : OS << "Mod/Ref   "; break;
264   default: assert(0 && "Bad value for AccessTy!");
265   }
266   if (Forward)
267     OS << " forwarding to " << (void*)Forward;
268
269
270   if (begin() != end()) {
271     OS << "Pointers: ";
272     for (iterator I = begin(), E = end(); I != E; ++I) {
273       if (I != begin()) OS << ", ";
274       WriteAsOperand(OS << "(", I->first);
275       OS << ", " << I->second.getSize() << ")";
276     }
277   }
278   if (!CallSites.empty()) {
279     OS << "\n    " << CallSites.size() << " Call Sites: ";
280     for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
281       if (i) OS << ", ";
282       WriteAsOperand(OS, CallSites[i].getCalledValue());
283     }      
284   }
285   OS << "\n";
286 }
287
288 void AliasSetTracker::print(std::ostream &OS) const {
289   OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
290      << PointerMap.size() << " pointer values.\n";
291   for (const_iterator I = begin(), E = end(); I != E; ++I)
292     I->print(OS);
293   OS << "\n";
294 }
295
296 void AliasSet::dump() const { print (std::cerr); }
297 void AliasSetTracker::dump() const { print(std::cerr); }
298
299 //===----------------------------------------------------------------------===//
300 //                            AliasSetPrinter Pass
301 //===----------------------------------------------------------------------===//
302
303 namespace {
304   class AliasSetPrinter : public FunctionPass {
305     AliasSetTracker *Tracker;
306   public:
307     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
308       AU.setPreservesAll();
309       AU.addRequired<AliasAnalysis>();
310     }
311
312     virtual bool runOnFunction(Function &F) {
313       Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
314
315       for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
316         Tracker->add(*I);
317       return false;
318     }
319
320     /// print - Convert to human readable form
321     virtual void print(std::ostream &OS) const {
322       Tracker->print(OS);
323     }
324
325     virtual void releaseMemory() {
326       delete Tracker;
327     }
328   };
329   RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer",
330                                   PassInfo::Analysis | PassInfo::Optimization);
331 }
332
333 } // End llvm namespace