1 //===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the AliasSetTracker and AliasSet classes.
12 //===----------------------------------------------------------------------===//
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"
24 /// mergeSetIn - Merge the specified alias set into this alias set...
26 void AliasSet::mergeSetIn(AliasSet &AS) {
27 assert(!AS.Forward && "Alias set is already forwarding!");
28 assert(!Forward && "This set is a forwarding set!!");
30 // Update the alias and access types of this set...
31 AccessTy |= AS.AccessTy;
32 AliasTy |= AS.AliasTy;
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());
42 // FIXME: If AS's refcount is zero, nuke it now...
43 assert(RefCount != 0);
45 AS.Forward = this; // Forward across AS now...
46 RefCount++; // AS is now pointing to us...
48 // Merge the list of constituent pointers...
49 PtrListTail->second.setTail(AS.PtrListHead);
50 PtrListTail = AS.PtrListTail;
51 AS.PtrListHead = AS.PtrListTail = 0;
54 void AliasSetTracker::removeAliasSet(AliasSet *AS) {
58 void AliasSet::removeFromTracker(AliasSetTracker &AST) {
59 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
60 AST.removeAliasSet(this);
63 void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
65 assert(!Entry.second.hasAliasSet() && "Entry already in set!");
67 AliasAnalysis &AA = AST.getAliasAnalysis();
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)
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!");
80 Entry.second.setAliasSet(this);
81 Entry.second.updateSize(Size);
83 // Add it to the end of the list...
85 PtrListTail->second.setTail(&Entry);
89 RefCount++; // Entry points to alias set...
92 void AliasSet::addCallSite(CallSite CS) {
93 CallSites.push_back(CS);
94 AliasTy = MayAlias; // FIXME: Too conservative?
98 /// aliasesPointer - Return true if the specified pointer "may" (or must)
99 /// alias one of the members in the set.
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!");
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);
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()))
119 // Check the call sites list and invoke list...
120 if (!CallSites.empty())
121 // FIXME: this is pessimistic!
127 bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
128 // FIXME: Too conservative!
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.
137 AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
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...
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...
169 /// getAliasSetForPointer - Return the alias set that the specified pointer
171 AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size){
172 AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
174 // Check to see if the pointer is already known...
175 if (Entry.second.hasAliasSet() && Size <= Entry.second.getSize()) {
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);
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();
190 void AliasSetTracker::add(LoadInst *LI) {
191 addPointer(LI->getOperand(0),
192 AA.getTargetData().getTypeSize(LI->getType()), AliasSet::Refs);
195 void AliasSetTracker::add(StoreInst *SI) {
196 addPointer(SI->getOperand(1),
197 AA.getTargetData().getTypeSize(SI->getOperand(0)->getType()),
201 void AliasSetTracker::add(CallSite CS) {
202 AliasSet *AS = findAliasSetForCallSite(CS);
204 AliasSets.push_back(AliasSet());
205 AS = &AliasSets.back();
210 void AliasSetTracker::add(Instruction *I) {
211 // Dispatch to one of the other add methods...
212 if (LoadInst *LI = dyn_cast<LoadInst>(I))
214 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
216 else if (CallInst *CI = dyn_cast<CallInst>(I))
218 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
222 void AliasSetTracker::add(BasicBlock &BB) {
223 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
227 void AliasSetTracker::add(const AliasSetTracker &AST) {
228 assert(&AA == &AST.AA &&
229 "Merging AliasSetTracker objects with different Alias Analyses!");
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);
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]);
242 // Loop over all of the pointers in this alias set...
243 AliasSet::iterator I = AS.begin(), E = AS.end();
245 addPointer(I->first, I->second.getSize(),
246 (AliasSet::AccessType)AS.AccessTy);
250 //===----------------------------------------------------------------------===//
251 // AliasSet/AliasSetTracker Printing Support
252 //===----------------------------------------------------------------------===//
254 void AliasSet::print(std::ostream &OS) const {
255 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
256 OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, ";
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!");
265 OS << " forwarding to " << (void*)Forward;
268 if (begin() != end()) {
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() << ")";
276 if (!CallSites.empty()) {
277 OS << "\n " << CallSites.size() << " Call Sites: ";
278 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
280 WriteAsOperand(OS, CallSites[i].getCalledValue());
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)
294 void AliasSet::dump() const { print (std::cerr); }
295 void AliasSetTracker::dump() const { print(std::cerr); }
298 //===----------------------------------------------------------------------===//
299 // AliasSetPrinter Pass
300 //===----------------------------------------------------------------------===//
303 class AliasSetPrinter : public FunctionPass {
304 AliasSetTracker *Tracker;
306 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
307 AU.setPreservesAll();
308 AU.addRequired<AliasAnalysis>();
311 virtual bool runOnFunction(Function &F) {
312 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
314 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
319 /// print - Convert to human readable form
320 virtual void print(std::ostream &OS) const {
324 virtual void releaseMemory() {
328 RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer",
329 PassInfo::Analysis | PassInfo::Optimization);