blob: f58aaa3e8154073f39fff421903ebd9ebac52398 [file] [log] [blame]
Chris Lattner009cc3d2002-09-26 21:49:07 +00001//===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
2//
3// This file implements the AliasSetTracker and AliasSet classes.
4//
5//===----------------------------------------------------------------------===//
6
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"
Chris Lattner9971ac42003-02-24 20:37:56 +000012#include "llvm/Pass.h"
Chris Lattner31a9d182003-02-26 22:11:00 +000013#include "llvm/Target/TargetData.h"
Chris Lattner9971ac42003-02-24 20:37:56 +000014#include "llvm/Assembly/Writer.h"
15#include "llvm/Support/InstIterator.h"
Chris Lattner009cc3d2002-09-26 21:49:07 +000016
17/// mergeSetIn - Merge the specified alias set into this alias set...
18///
Chris Lattner9971ac42003-02-24 20:37:56 +000019void AliasSet::mergeSetIn(AliasSet &AS) {
20 assert(!AS.Forward && "Alias set is already forwarding!");
21 assert(!Forward && "This set is a forwarding set!!");
Chris Lattner009cc3d2002-09-26 21:49:07 +000022
23 // Update the alias and access types of this set...
Chris Lattner9971ac42003-02-24 20:37:56 +000024 AccessTy |= AS.AccessTy;
25 AliasTy |= AS.AliasTy;
26
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());
32 AS.CallSites.clear();
33 }
34
35 // FIXME: If AS's refcount is zero, nuke it now...
36 assert(RefCount != 0);
37
38 AS.Forward = this; // Forward across AS now...
39 RefCount++; // AS is now pointing to us...
40
41 // Merge the list of constituent pointers...
42 PtrListTail->second.setTail(AS.PtrListHead);
43 PtrListTail = AS.PtrListTail;
44 AS.PtrListHead = AS.PtrListTail = 0;
Chris Lattner009cc3d2002-09-26 21:49:07 +000045}
46
Chris Lattner9971ac42003-02-24 20:37:56 +000047void AliasSetTracker::removeAliasSet(AliasSet *AS) {
48 AliasSets.erase(AS);
49}
50
51void AliasSet::removeFromTracker(AliasSetTracker &AST) {
52 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
53 AST.removeAliasSet(this);
54}
55
Chris Lattner31a9d182003-02-26 22:11:00 +000056void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
57 unsigned Size) {
Chris Lattner9971ac42003-02-24 20:37:56 +000058 assert(!Entry.second.hasAliasSet() && "Entry already in set!");
59
60 AliasAnalysis &AA = AST.getAliasAnalysis();
61
62 if (isMustAlias()) // Check to see if we have to downgrade to _may_ alias
Chris Lattner31a9d182003-02-26 22:11:00 +000063 if (HashNodePair *P = getSomePointer()) {
64 AliasAnalysis::AliasResult Result =
65 AA.alias(P->first, P->second.getSize(), Entry.first, Size);
66 if (Result == AliasAnalysis::MayAlias)
Chris Lattner9971ac42003-02-24 20:37:56 +000067 AliasTy = MayAlias;
Chris Lattner31a9d182003-02-26 22:11:00 +000068 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!");
71 }
Chris Lattner9971ac42003-02-24 20:37:56 +000072
73 Entry.second.setAliasSet(this);
Chris Lattner31a9d182003-02-26 22:11:00 +000074 Entry.second.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +000075
76 // Add it to the end of the list...
77 if (PtrListTail)
78 PtrListTail->second.setTail(&Entry);
79 else
80 PtrListHead = &Entry;
81 PtrListTail = &Entry;
82 RefCount++; // Entry points to alias set...
83}
84
85void AliasSet::addCallSite(CallSite CS) {
86 CallSites.push_back(CS);
Chris Lattner31a9d182003-02-26 22:11:00 +000087 AliasTy = MayAlias; // FIXME: Too conservative?
Chris Lattner9971ac42003-02-24 20:37:56 +000088}
89
90/// aliasesPointer - Return true if the specified pointer "may" (or must)
Chris Lattner009cc3d2002-09-26 21:49:07 +000091/// alias one of the members in the set.
92///
Chris Lattner31a9d182003-02-26 22:11:00 +000093bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
94 AliasAnalysis &AA) const {
Chris Lattner9971ac42003-02-24 20:37:56 +000095 if (AliasTy == MustAlias) {
96 assert(CallSites.empty() && "Illegal must alias set!");
97
98 // If this is a set of MustAliases, only check to see if the pointer aliases
99 // SOME value in the set...
Chris Lattner31a9d182003-02-26 22:11:00 +0000100 HashNodePair *SomePtr = getSomePointer();
Chris Lattner9971ac42003-02-24 20:37:56 +0000101 assert(SomePtr && "Empty must-alias set??");
Chris Lattner31a9d182003-02-26 22:11:00 +0000102 return AA.alias(SomePtr->first, SomePtr->second.getSize(), Ptr, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000103 }
104
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)
Chris Lattner31a9d182003-02-26 22:11:00 +0000108 if (AA.alias(Ptr, Size, I->first, I->second.getSize()))
Chris Lattner9971ac42003-02-24 20:37:56 +0000109 return true;
110
111 // Check the call sites list and invoke list...
112 if (!CallSites.empty())
113 // FIXME: this is pessimistic!
Chris Lattner009cc3d2002-09-26 21:49:07 +0000114 return true;
Chris Lattner9971ac42003-02-24 20:37:56 +0000115
Chris Lattner009cc3d2002-09-26 21:49:07 +0000116 return false;
117}
118
Chris Lattner9971ac42003-02-24 20:37:56 +0000119bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
120 // FIXME: Too conservative!
121 return true;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000122}
123
124
Chris Lattner009cc3d2002-09-26 21:49:07 +0000125/// 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.
128///
Chris Lattner31a9d182003-02-26 22:11:00 +0000129AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
130 unsigned Size) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000131 AliasSet *FoundSet = 0;
Chris Lattner9971ac42003-02-24 20:37:56 +0000132 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner31a9d182003-02-26 22:11:00 +0000133 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000134 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
Chris Lattner9971ac42003-02-24 20:37:56 +0000135 FoundSet = I; // Remember it.
Chris Lattner009cc3d2002-09-26 21:49:07 +0000136 } else { // Otherwise, we must merge the sets...
Chris Lattner9971ac42003-02-24 20:37:56 +0000137 FoundSet->mergeSetIn(*I); // Merge in contents...
Chris Lattner009cc3d2002-09-26 21:49:07 +0000138 }
139 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000140
141 return FoundSet;
142}
143
144AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
145 AliasSet *FoundSet = 0;
146 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000147 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000148 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
149 FoundSet = I; // Remember it.
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000150 } else if (!I->Forward) { // Otherwise, we must merge the sets...
Chris Lattner9971ac42003-02-24 20:37:56 +0000151 FoundSet->mergeSetIn(*I); // Merge in contents...
152 }
153 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000154
155 return FoundSet;
156}
157
158
Chris Lattner009cc3d2002-09-26 21:49:07 +0000159
Chris Lattner9971ac42003-02-24 20:37:56 +0000160
161/// getAliasSetForPointer - Return the alias set that the specified pointer
162/// lives in...
Chris Lattner31a9d182003-02-26 22:11:00 +0000163AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size){
Chris Lattner9971ac42003-02-24 20:37:56 +0000164 AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
165
166 // Check to see if the pointer is already known...
Chris Lattner31a9d182003-02-26 22:11:00 +0000167 if (Entry.second.hasAliasSet() && Size <= Entry.second.getSize()) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000168 // Return the set!
169 return *Entry.second.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattner31a9d182003-02-26 22:11:00 +0000170 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000171 // Add it to the alias set it aliases...
Chris Lattner31a9d182003-02-26 22:11:00 +0000172 AS->addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000173 return *AS;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000174 } else {
Chris Lattner9971ac42003-02-24 20:37:56 +0000175 // Otherwise create a new alias set to hold the loaded pointer...
Chris Lattner009cc3d2002-09-26 21:49:07 +0000176 AliasSets.push_back(AliasSet());
Chris Lattner31a9d182003-02-26 22:11:00 +0000177 AliasSets.back().addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000178 return AliasSets.back();
Chris Lattner009cc3d2002-09-26 21:49:07 +0000179 }
180}
181
Chris Lattner9971ac42003-02-24 20:37:56 +0000182void AliasSetTracker::add(LoadInst *LI) {
Chris Lattner31a9d182003-02-26 22:11:00 +0000183 addPointer(LI->getOperand(0),
184 AA.getTargetData().getTypeSize(LI->getType()), AliasSet::Refs);
Chris Lattner9971ac42003-02-24 20:37:56 +0000185}
186
Chris Lattner009cc3d2002-09-26 21:49:07 +0000187void AliasSetTracker::add(StoreInst *SI) {
Chris Lattner31a9d182003-02-26 22:11:00 +0000188 addPointer(SI->getOperand(1),
189 AA.getTargetData().getTypeSize(SI->getOperand(0)->getType()),
190 AliasSet::Mods);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000191}
192
Chris Lattner9971ac42003-02-24 20:37:56 +0000193void AliasSetTracker::add(CallSite CS) {
194 AliasSet *AS = findAliasSetForCallSite(CS);
195 if (!AS) {
196 AliasSets.push_back(AliasSet());
197 AS = &AliasSets.back();
198 }
199 AS->addCallSite(CS);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000200}
201
Chris Lattner9971ac42003-02-24 20:37:56 +0000202void AliasSetTracker::add(Instruction *I) {
203 // Dispatch to one of the other add methods...
204 if (LoadInst *LI = dyn_cast<LoadInst>(I))
205 add(LI);
206 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
207 add(SI);
208 else if (CallInst *CI = dyn_cast<CallInst>(I))
209 add(CI);
210 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
211 add(II);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000212}
213
Chris Lattner9971ac42003-02-24 20:37:56 +0000214//===----------------------------------------------------------------------===//
215// AliasSet/AliasSetTracker Printing Support
216//===----------------------------------------------------------------------===//
217
218void AliasSet::print(std::ostream &OS) const {
219 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
220 OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, ";
221 switch (AccessTy) {
222 case NoModRef: OS << "No access "; break;
223 case Refs : OS << "Ref "; break;
224 case Mods : OS << "Mod "; break;
225 case ModRef : OS << "Mod/Ref "; break;
226 default: assert(0 && "Bad value for AccessTy!");
Chris Lattner009cc3d2002-09-26 21:49:07 +0000227 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000228 if (Forward)
229 OS << " forwarding to " << (void*)Forward;
230
231
232 if (begin() != end()) {
233 OS << "Pointers: ";
234 for (iterator I = begin(), E = end(); I != E; ++I) {
235 if (I != begin()) OS << ", ";
Chris Lattner31a9d182003-02-26 22:11:00 +0000236 WriteAsOperand(OS << "(", I->first);
237 OS << ", " << I->second.getSize() << ")";
Chris Lattner9971ac42003-02-24 20:37:56 +0000238 }
239 }
240 if (!CallSites.empty()) {
241 OS << "\n " << CallSites.size() << " Call Sites: ";
242 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
243 if (i) OS << ", ";
244 WriteAsOperand(OS, CallSites[i].getCalledValue());
245 }
246 }
247 OS << "\n";
248}
249
250void AliasSetTracker::print(std::ostream &OS) const {
251 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
252 << PointerMap.size() << " pointer values.\n";
253 for (const_iterator I = begin(), E = end(); I != E; ++I)
254 I->print(OS);
255 OS << "\n";
256}
257
258void AliasSet::dump() const { print (std::cerr); }
259void AliasSetTracker::dump() const { print(std::cerr); }
260
261
262//===----------------------------------------------------------------------===//
263// AliasSetPrinter Pass
264//===----------------------------------------------------------------------===//
265
266namespace {
267 class AliasSetPrinter : public FunctionPass {
268 AliasSetTracker *Tracker;
269 public:
270 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
271 AU.setPreservesAll();
272 AU.addRequired<AliasAnalysis>();
273 }
274
275 virtual bool runOnFunction(Function &F) {
276 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
277
278 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
279 Tracker->add(*I);
280 return false;
281 }
282
283 /// print - Convert to human readable form
284 virtual void print(std::ostream &OS) const {
285 Tracker->print(OS);
286 }
287
288 virtual void releaseMemory() {
289 delete Tracker;
290 }
291 };
292 RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer",
293 PassInfo::Analysis | PassInfo::Optimization);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000294}