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/Target/TargetData.h"
14 #include "llvm/Assembly/Writer.h"
15 #include "llvm/Support/InstIterator.h"
17 /// mergeSetIn - Merge the specified alias set into this alias set...
19 void AliasSet::mergeSetIn(AliasSet &AS) {
20 assert(!AS.Forward && "Alias set is already forwarding!");
21 assert(!Forward && "This set is a forwarding set!!");
23 // Update the alias and access types of this set...
24 AccessTy |= AS.AccessTy;
25 AliasTy |= AS.AliasTy;
27 if (CallSites.empty()) { // Merge call sites...
28 if (!AS.CallSites.empty())
29 std::swap(CallSites, AS.CallSites);
30 } else if (!AS.CallSites.empty()) {
31 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
35 // FIXME: If AS's refcount is zero, nuke it now...
36 assert(RefCount != 0);
38 AS.Forward = this; // Forward across AS now...
39 RefCount++; // AS is now pointing to us...
41 // Merge the list of constituent pointers...
42 PtrListTail->second.setTail(AS.PtrListHead);
43 PtrListTail = AS.PtrListTail;
44 AS.PtrListHead = AS.PtrListTail = 0;
47 void AliasSetTracker::removeAliasSet(AliasSet *AS) {
51 void AliasSet::removeFromTracker(AliasSetTracker &AST) {
52 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
53 AST.removeAliasSet(this);
56 void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
58 assert(!Entry.second.hasAliasSet() && "Entry already in set!");
60 AliasAnalysis &AA = AST.getAliasAnalysis();
62 if (isMustAlias()) // Check to see if we have to downgrade to _may_ alias
63 if (HashNodePair *P = getSomePointer()) {
64 AliasAnalysis::AliasResult Result =
65 AA.alias(P->first, P->second.getSize(), Entry.first, Size);
66 if (Result == AliasAnalysis::MayAlias)
68 else // First entry of must alias must have maximum size!
69 P->second.updateSize(Size);
70 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
73 Entry.second.setAliasSet(this);
74 Entry.second.updateSize(Size);
76 // Add it to the end of the list...
78 PtrListTail->second.setTail(&Entry);
82 RefCount++; // Entry points to alias set...
85 void AliasSet::addCallSite(CallSite CS) {
86 CallSites.push_back(CS);
87 AliasTy = MayAlias; // FIXME: Too conservative?
90 /// aliasesPointer - Return true if the specified pointer "may" (or must)
91 /// alias one of the members in the set.
93 bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
94 AliasAnalysis &AA) const {
95 if (AliasTy == MustAlias) {
96 assert(CallSites.empty() && "Illegal must alias set!");
98 // If this is a set of MustAliases, only check to see if the pointer aliases
99 // SOME value in the set...
100 HashNodePair *SomePtr = getSomePointer();
101 assert(SomePtr && "Empty must-alias set??");
102 return AA.alias(SomePtr->first, SomePtr->second.getSize(), Ptr, Size);
105 // If this is a may-alias set, we have to check all of the pointers in the set
106 // to be sure it doesn't alias the set...
107 for (iterator I = begin(), E = end(); I != E; ++I)
108 if (AA.alias(Ptr, Size, I->first, I->second.getSize()))
111 // Check the call sites list and invoke list...
112 if (!CallSites.empty())
113 // FIXME: this is pessimistic!
119 bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
120 // FIXME: Too conservative!
125 /// findAliasSetForPointer - Given a pointer, find the one alias set to put the
126 /// instruction referring to the pointer into. If there are multiple alias sets
127 /// that may alias the pointer, merge them together and return the unified set.
129 AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
131 AliasSet *FoundSet = 0;
132 for (iterator I = begin(), E = end(); I != E; ++I)
133 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
134 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
135 FoundSet = I; // Remember it.
136 } else { // Otherwise, we must merge the sets...
137 FoundSet->mergeSetIn(*I); // Merge in contents...
144 AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
145 AliasSet *FoundSet = 0;
146 for (iterator I = begin(), E = end(); I != E; ++I)
147 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
148 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
149 FoundSet = I; // Remember it.
150 } else if (!I->Forward) { // Otherwise, we must merge the sets...
151 FoundSet->mergeSetIn(*I); // Merge in contents...
161 /// getAliasSetForPointer - Return the alias set that the specified pointer
163 AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size){
164 AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
166 // Check to see if the pointer is already known...
167 if (Entry.second.hasAliasSet() && Size <= Entry.second.getSize()) {
169 return *Entry.second.getAliasSet(*this)->getForwardedTarget(*this);
170 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
171 // Add it to the alias set it aliases...
172 AS->addPointer(*this, Entry, Size);
175 // Otherwise create a new alias set to hold the loaded pointer...
176 AliasSets.push_back(AliasSet());
177 AliasSets.back().addPointer(*this, Entry, Size);
178 return AliasSets.back();
182 void AliasSetTracker::add(LoadInst *LI) {
183 addPointer(LI->getOperand(0),
184 AA.getTargetData().getTypeSize(LI->getType()), AliasSet::Refs);
187 void AliasSetTracker::add(StoreInst *SI) {
188 addPointer(SI->getOperand(1),
189 AA.getTargetData().getTypeSize(SI->getOperand(0)->getType()),
193 void AliasSetTracker::add(CallSite CS) {
194 AliasSet *AS = findAliasSetForCallSite(CS);
196 AliasSets.push_back(AliasSet());
197 AS = &AliasSets.back();
202 void AliasSetTracker::add(Instruction *I) {
203 // Dispatch to one of the other add methods...
204 if (LoadInst *LI = dyn_cast<LoadInst>(I))
206 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
208 else if (CallInst *CI = dyn_cast<CallInst>(I))
210 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
214 void AliasSetTracker::add(BasicBlock &BB) {
215 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
219 void AliasSetTracker::add(const AliasSetTracker &AST) {
220 assert(&AA == &AST.AA &&
221 "Merging AliasSetTracker objects with different Alias Analyses!");
223 // Loop over all of the alias sets in AST, adding the pointers contained
224 // therein into the current alias sets. This can cause alias sets to be
225 // merged together in the current AST.
226 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
227 if (!I->Forward) { // Ignore forwarding alias sets
228 AliasSet &AS = const_cast<AliasSet&>(*I);
230 // If there are any call sites in the alias set, add them to this AST.
231 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
232 add(AS.CallSites[i]);
234 // Loop over all of the pointers in this alias set...
235 AliasSet::iterator I = AS.begin(), E = AS.end();
237 addPointer(I->first, I->second.getSize(),
238 (AliasSet::AccessType)AS.AccessTy);
242 //===----------------------------------------------------------------------===//
243 // AliasSet/AliasSetTracker Printing Support
244 //===----------------------------------------------------------------------===//
246 void AliasSet::print(std::ostream &OS) const {
247 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
248 OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, ";
250 case NoModRef: OS << "No access "; break;
251 case Refs : OS << "Ref "; break;
252 case Mods : OS << "Mod "; break;
253 case ModRef : OS << "Mod/Ref "; break;
254 default: assert(0 && "Bad value for AccessTy!");
257 OS << " forwarding to " << (void*)Forward;
260 if (begin() != end()) {
262 for (iterator I = begin(), E = end(); I != E; ++I) {
263 if (I != begin()) OS << ", ";
264 WriteAsOperand(OS << "(", I->first);
265 OS << ", " << I->second.getSize() << ")";
268 if (!CallSites.empty()) {
269 OS << "\n " << CallSites.size() << " Call Sites: ";
270 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
272 WriteAsOperand(OS, CallSites[i].getCalledValue());
278 void AliasSetTracker::print(std::ostream &OS) const {
279 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
280 << PointerMap.size() << " pointer values.\n";
281 for (const_iterator I = begin(), E = end(); I != E; ++I)
286 void AliasSet::dump() const { print (std::cerr); }
287 void AliasSetTracker::dump() const { print(std::cerr); }
290 //===----------------------------------------------------------------------===//
291 // AliasSetPrinter Pass
292 //===----------------------------------------------------------------------===//
295 class AliasSetPrinter : public FunctionPass {
296 AliasSetTracker *Tracker;
298 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
299 AU.setPreservesAll();
300 AU.addRequired<AliasAnalysis>();
303 virtual bool runOnFunction(Function &F) {
304 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
306 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
311 /// print - Convert to human readable form
312 virtual void print(std::ostream &OS) const {
316 virtual void releaseMemory() {
320 RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer",
321 PassInfo::Analysis | PassInfo::Optimization);