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...
51 PtrListTail->second.setTail(AS.PtrListHead);
52 PtrListTail = AS.PtrListTail;
53 AS.PtrListHead = AS.PtrListTail = 0;
56 void AliasSetTracker::removeAliasSet(AliasSet *AS) {
60 void AliasSet::removeFromTracker(AliasSetTracker &AST) {
61 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
62 AST.removeAliasSet(this);
65 void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
67 assert(!Entry.second.hasAliasSet() && "Entry already in set!");
69 AliasAnalysis &AA = AST.getAliasAnalysis();
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)
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!");
82 Entry.second.setAliasSet(this);
83 Entry.second.updateSize(Size);
85 // Add it to the end of the list...
87 PtrListTail->second.setTail(&Entry);
91 RefCount++; // Entry points to alias set...
94 void AliasSet::addCallSite(CallSite CS) {
95 CallSites.push_back(CS);
96 AliasTy = MayAlias; // FIXME: Too conservative?
100 /// aliasesPointer - Return true if the specified pointer "may" (or must)
101 /// alias one of the members in the set.
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!");
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);
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()))
121 // Check the call sites list and invoke list...
122 if (!CallSites.empty())
123 // FIXME: this is pessimistic!
129 bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
130 // FIXME: Too conservative!
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.
139 AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
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...
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...
171 /// getAliasSetForPointer - Return the alias set that the specified pointer
173 AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size){
174 AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
176 // Check to see if the pointer is already known...
177 if (Entry.second.hasAliasSet() && Size <= Entry.second.getSize()) {
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);
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();
192 void AliasSetTracker::add(LoadInst *LI) {
193 addPointer(LI->getOperand(0),
194 AA.getTargetData().getTypeSize(LI->getType()), AliasSet::Refs);
197 void AliasSetTracker::add(StoreInst *SI) {
198 addPointer(SI->getOperand(1),
199 AA.getTargetData().getTypeSize(SI->getOperand(0)->getType()),
203 void AliasSetTracker::add(CallSite CS) {
204 AliasSet *AS = findAliasSetForCallSite(CS);
206 AliasSets.push_back(AliasSet());
207 AS = &AliasSets.back();
212 void AliasSetTracker::add(Instruction *I) {
213 // Dispatch to one of the other add methods...
214 if (LoadInst *LI = dyn_cast<LoadInst>(I))
216 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
218 else if (CallInst *CI = dyn_cast<CallInst>(I))
220 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
224 void AliasSetTracker::add(BasicBlock &BB) {
225 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
229 void AliasSetTracker::add(const AliasSetTracker &AST) {
230 assert(&AA == &AST.AA &&
231 "Merging AliasSetTracker objects with different Alias Analyses!");
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);
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]);
244 // Loop over all of the pointers in this alias set...
245 AliasSet::iterator I = AS.begin(), E = AS.end();
247 addPointer(I->first, I->second.getSize(),
248 (AliasSet::AccessType)AS.AccessTy);
252 //===----------------------------------------------------------------------===//
253 // AliasSet/AliasSetTracker Printing Support
254 //===----------------------------------------------------------------------===//
256 void AliasSet::print(std::ostream &OS) const {
257 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
258 OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, ";
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!");
267 OS << " forwarding to " << (void*)Forward;
270 if (begin() != end()) {
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() << ")";
278 if (!CallSites.empty()) {
279 OS << "\n " << CallSites.size() << " Call Sites: ";
280 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
282 WriteAsOperand(OS, CallSites[i].getCalledValue());
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)
296 void AliasSet::dump() const { print (std::cerr); }
297 void AliasSetTracker::dump() const { print(std::cerr); }
299 //===----------------------------------------------------------------------===//
300 // AliasSetPrinter Pass
301 //===----------------------------------------------------------------------===//
304 class AliasSetPrinter : public FunctionPass {
305 AliasSetTracker *Tracker;
307 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
308 AU.setPreservesAll();
309 AU.addRequired<AliasAnalysis>();
312 virtual bool runOnFunction(Function &F) {
313 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
315 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
320 /// print - Convert to human readable form
321 virtual void print(std::ostream &OS) const {
325 virtual void releaseMemory() {
329 RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer",
330 PassInfo::Analysis | PassInfo::Optimization);
333 } // End llvm namespace