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"
26 /// mergeSetIn - Merge the specified alias set into this alias set...
28 void AliasSet::mergeSetIn(AliasSet &AS) {
29 assert(!AS.Forward && "Alias set is already forwarding!");
30 assert(!Forward && "This set is a forwarding set!!");
32 // Update the alias and access types of this set...
33 AccessTy |= AS.AccessTy;
34 AliasTy |= AS.AliasTy;
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());
44 // FIXME: If AS's refcount is zero, nuke it now...
45 assert(RefCount != 0);
47 AS.Forward = this; // Forward across AS now...
48 RefCount++; // AS is now pointing to us...
50 // Merge the list of constituent pointers...
52 *PtrListEnd = AS.PtrList;
53 AS.PtrList->second.setPrevInList(PtrListEnd);
54 PtrListEnd = AS.PtrListEnd;
57 AS.PtrListEnd = &AS.PtrList;
61 void AliasSetTracker::removeAliasSet(AliasSet *AS) {
65 void AliasSet::removeFromTracker(AliasSetTracker &AST) {
66 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
67 AST.removeAliasSet(this);
70 void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
72 assert(!Entry.second.hasAliasSet() && "Entry already in set!");
74 AliasAnalysis &AA = AST.getAliasAnalysis();
76 if (isMustAlias()) // Check to see if we have to downgrade to _may_ alias
77 if (HashNodePair *P = getSomePointer()) {
78 AliasAnalysis::AliasResult Result =
79 AA.alias(P->first, P->second.getSize(), Entry.first, Size);
80 if (Result == AliasAnalysis::MayAlias)
82 else // First entry of must alias must have maximum size!
83 P->second.updateSize(Size);
84 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
87 Entry.second.setAliasSet(this);
88 Entry.second.updateSize(Size);
90 // Add it to the end of the list...
91 assert(*PtrListEnd == 0 && "End of list is not null?");
93 PtrListEnd = Entry.second.setPrevInList(PtrListEnd);
94 RefCount++; // Entry points to alias set...
97 void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
98 CallSites.push_back(CS);
100 if (Function *F = CS.getCalledFunction()) {
101 if (AA.doesNotAccessMemory(F))
103 else if (AA.onlyReadsMemory(F)) {
110 // FIXME: This should use mod/ref information to make this not suck so bad
115 /// aliasesPointer - Return true if the specified pointer "may" (or must)
116 /// alias one of the members in the set.
118 bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
119 AliasAnalysis &AA) const {
120 if (AliasTy == MustAlias) {
121 assert(CallSites.empty() && "Illegal must alias set!");
123 // If this is a set of MustAliases, only check to see if the pointer aliases
124 // SOME value in the set...
125 HashNodePair *SomePtr = getSomePointer();
126 assert(SomePtr && "Empty must-alias set??");
127 return AA.alias(SomePtr->first, SomePtr->second.getSize(), Ptr, Size);
130 // If this is a may-alias set, we have to check all of the pointers in the set
131 // to be sure it doesn't alias the set...
132 for (iterator I = begin(), E = end(); I != E; ++I)
133 if (AA.alias(Ptr, Size, I->first, I->second.getSize()))
136 // Check the call sites list and invoke list...
137 if (!CallSites.empty())
138 // FIXME: this is pessimistic!
144 bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
145 // FIXME: Use mod/ref information to prune this better!
146 if (Function *F = CS.getCalledFunction())
147 if (AA.doesNotAccessMemory(F))
154 /// findAliasSetForPointer - Given a pointer, find the one alias set to put the
155 /// instruction referring to the pointer into. If there are multiple alias sets
156 /// that may alias the pointer, merge them together and return the unified set.
158 AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
160 AliasSet *FoundSet = 0;
161 for (iterator I = begin(), E = end(); I != E; ++I)
162 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
163 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
164 FoundSet = I; // Remember it.
165 } else { // Otherwise, we must merge the sets...
166 FoundSet->mergeSetIn(*I); // Merge in contents...
173 AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
174 AliasSet *FoundSet = 0;
175 for (iterator I = begin(), E = end(); I != E; ++I)
176 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
177 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
178 FoundSet = I; // Remember it.
179 } else if (!I->Forward) { // Otherwise, we must merge the sets...
180 FoundSet->mergeSetIn(*I); // Merge in contents...
190 /// getAliasSetForPointer - Return the alias set that the specified pointer
192 AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size){
193 AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
195 // Check to see if the pointer is already known...
196 if (Entry.second.hasAliasSet() && Size <= Entry.second.getSize()) {
198 return *Entry.second.getAliasSet(*this)->getForwardedTarget(*this);
199 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
200 // Add it to the alias set it aliases...
201 AS->addPointer(*this, Entry, Size);
204 // Otherwise create a new alias set to hold the loaded pointer...
205 AliasSets.push_back(AliasSet());
206 AliasSets.back().addPointer(*this, Entry, Size);
207 return AliasSets.back();
211 void AliasSetTracker::add(LoadInst *LI) {
213 addPointer(LI->getOperand(0),
214 AA.getTargetData().getTypeSize(LI->getType()), AliasSet::Refs);
215 if (LI->isVolatile()) AS.setVolatile();
218 void AliasSetTracker::add(StoreInst *SI) {
220 addPointer(SI->getOperand(1),
221 AA.getTargetData().getTypeSize(SI->getOperand(0)->getType()),
223 if (SI->isVolatile()) AS.setVolatile();
227 void AliasSetTracker::add(CallSite CS) {
228 if (Function *F = CS.getCalledFunction())
229 if (AA.doesNotAccessMemory(F))
232 AliasSet *AS = findAliasSetForCallSite(CS);
234 AliasSets.push_back(AliasSet());
235 AS = &AliasSets.back();
237 AS->addCallSite(CS, AA);
240 void AliasSetTracker::add(Instruction *I) {
241 // Dispatch to one of the other add methods...
242 if (LoadInst *LI = dyn_cast<LoadInst>(I))
244 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
246 else if (CallInst *CI = dyn_cast<CallInst>(I))
248 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
252 void AliasSetTracker::add(BasicBlock &BB) {
253 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
257 void AliasSetTracker::add(const AliasSetTracker &AST) {
258 assert(&AA == &AST.AA &&
259 "Merging AliasSetTracker objects with different Alias Analyses!");
261 // Loop over all of the alias sets in AST, adding the pointers contained
262 // therein into the current alias sets. This can cause alias sets to be
263 // merged together in the current AST.
264 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
265 if (!I->Forward) { // Ignore forwarding alias sets
266 AliasSet &AS = const_cast<AliasSet&>(*I);
268 // If there are any call sites in the alias set, add them to this AST.
269 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
270 add(AS.CallSites[i]);
272 // Loop over all of the pointers in this alias set...
273 AliasSet::iterator I = AS.begin(), E = AS.end();
275 addPointer(I->first, I->second.getSize(),
276 (AliasSet::AccessType)AS.AccessTy);
281 // deleteValue method - This method is used to remove a pointer value from the
282 // AliasSetTracker entirely. It should be used when an instruction is deleted
283 // from the program to update the AST. If you don't use this, you would have
284 // dangling pointers to deleted instructions.
286 void AliasSetTracker::deleteValue(Value *PtrVal) {
287 // First, look up the PointerRec for this pointer...
288 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(PtrVal);
289 if (I == PointerMap.end()) return; // Noop
291 // If we found one, remove the pointer from the alias set it is in.
292 AliasSet::HashNodePair &PtrValEnt = *I;
293 AliasSet *AS = PtrValEnt.second.getAliasSet(*this);
295 // Unlink from the list of values...
296 PtrValEnt.second.removeFromList();
297 // Stop using the alias set
298 if (--AS->RefCount == 0)
299 AS->removeFromTracker(*this);
305 //===----------------------------------------------------------------------===//
306 // AliasSet/AliasSetTracker Printing Support
307 //===----------------------------------------------------------------------===//
309 void AliasSet::print(std::ostream &OS) const {
310 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
311 OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, ";
313 case NoModRef: OS << "No access "; break;
314 case Refs : OS << "Ref "; break;
315 case Mods : OS << "Mod "; break;
316 case ModRef : OS << "Mod/Ref "; break;
317 default: assert(0 && "Bad value for AccessTy!");
319 if (isVolatile()) OS << "[volatile] ";
321 OS << " forwarding to " << (void*)Forward;
324 if (begin() != end()) {
326 for (iterator I = begin(), E = end(); I != E; ++I) {
327 if (I != begin()) OS << ", ";
328 WriteAsOperand(OS << "(", I->first);
329 OS << ", " << I->second.getSize() << ")";
332 if (!CallSites.empty()) {
333 OS << "\n " << CallSites.size() << " Call Sites: ";
334 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
336 WriteAsOperand(OS, CallSites[i].getCalledValue());
342 void AliasSetTracker::print(std::ostream &OS) const {
343 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
344 << PointerMap.size() << " pointer values.\n";
345 for (const_iterator I = begin(), E = end(); I != E; ++I)
350 void AliasSet::dump() const { print (std::cerr); }
351 void AliasSetTracker::dump() const { print(std::cerr); }
353 //===----------------------------------------------------------------------===//
354 // AliasSetPrinter Pass
355 //===----------------------------------------------------------------------===//
358 class AliasSetPrinter : public FunctionPass {
359 AliasSetTracker *Tracker;
361 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
362 AU.setPreservesAll();
363 AU.addRequired<AliasAnalysis>();
366 virtual bool runOnFunction(Function &F) {
367 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
369 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
374 /// print - Convert to human readable form
375 virtual void print(std::ostream &OS) const {
379 virtual void releaseMemory() {
383 RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer",
384 PassInfo::Analysis | PassInfo::Optimization);