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