blob: c2800326c20f946b3dafc92054575c6550593707 [file] [log] [blame]
Chris Lattner009cc3d2002-09-26 21:49:07 +00001//===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner009cc3d2002-09-26 21:49:07 +00009//
10// This file implements the AliasSetTracker and AliasSet classes.
11//
12//===----------------------------------------------------------------------===//
13
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"
Chris Lattner9971ac42003-02-24 20:37:56 +000019#include "llvm/Pass.h"
Chris Lattner31a9d182003-02-26 22:11:00 +000020#include "llvm/Target/TargetData.h"
Chris Lattner9971ac42003-02-24 20:37:56 +000021#include "llvm/Assembly/Writer.h"
22#include "llvm/Support/InstIterator.h"
Chris Lattner009cc3d2002-09-26 21:49:07 +000023
24/// mergeSetIn - Merge the specified alias set into this alias set...
25///
Chris Lattner9971ac42003-02-24 20:37:56 +000026void AliasSet::mergeSetIn(AliasSet &AS) {
27 assert(!AS.Forward && "Alias set is already forwarding!");
28 assert(!Forward && "This set is a forwarding set!!");
Chris Lattner009cc3d2002-09-26 21:49:07 +000029
30 // Update the alias and access types of this set...
Chris Lattner9971ac42003-02-24 20:37:56 +000031 AccessTy |= AS.AccessTy;
32 AliasTy |= AS.AliasTy;
33
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());
39 AS.CallSites.clear();
40 }
41
42 // FIXME: If AS's refcount is zero, nuke it now...
43 assert(RefCount != 0);
44
45 AS.Forward = this; // Forward across AS now...
46 RefCount++; // AS is now pointing to us...
47
48 // Merge the list of constituent pointers...
49 PtrListTail->second.setTail(AS.PtrListHead);
50 PtrListTail = AS.PtrListTail;
51 AS.PtrListHead = AS.PtrListTail = 0;
Chris Lattner009cc3d2002-09-26 21:49:07 +000052}
53
Chris Lattner9971ac42003-02-24 20:37:56 +000054void AliasSetTracker::removeAliasSet(AliasSet *AS) {
55 AliasSets.erase(AS);
56}
57
58void AliasSet::removeFromTracker(AliasSetTracker &AST) {
59 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
60 AST.removeAliasSet(this);
61}
62
Chris Lattner31a9d182003-02-26 22:11:00 +000063void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
64 unsigned Size) {
Chris Lattner9971ac42003-02-24 20:37:56 +000065 assert(!Entry.second.hasAliasSet() && "Entry already in set!");
66
67 AliasAnalysis &AA = AST.getAliasAnalysis();
68
69 if (isMustAlias()) // Check to see if we have to downgrade to _may_ alias
Chris Lattner31a9d182003-02-26 22:11:00 +000070 if (HashNodePair *P = getSomePointer()) {
71 AliasAnalysis::AliasResult Result =
72 AA.alias(P->first, P->second.getSize(), Entry.first, Size);
73 if (Result == AliasAnalysis::MayAlias)
Chris Lattner9971ac42003-02-24 20:37:56 +000074 AliasTy = MayAlias;
Chris Lattner31a9d182003-02-26 22:11:00 +000075 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!");
78 }
Chris Lattner9971ac42003-02-24 20:37:56 +000079
80 Entry.second.setAliasSet(this);
Chris Lattner31a9d182003-02-26 22:11:00 +000081 Entry.second.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +000082
83 // Add it to the end of the list...
84 if (PtrListTail)
85 PtrListTail->second.setTail(&Entry);
86 else
87 PtrListHead = &Entry;
88 PtrListTail = &Entry;
89 RefCount++; // Entry points to alias set...
90}
91
92void AliasSet::addCallSite(CallSite CS) {
93 CallSites.push_back(CS);
Chris Lattner31a9d182003-02-26 22:11:00 +000094 AliasTy = MayAlias; // FIXME: Too conservative?
Chris Lattner577385e2003-05-03 03:42:08 +000095 AccessTy = ModRef;
Chris Lattner9971ac42003-02-24 20:37:56 +000096}
97
98/// aliasesPointer - Return true if the specified pointer "may" (or must)
Chris Lattner009cc3d2002-09-26 21:49:07 +000099/// alias one of the members in the set.
100///
Chris Lattner31a9d182003-02-26 22:11:00 +0000101bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
102 AliasAnalysis &AA) const {
Chris Lattner9971ac42003-02-24 20:37:56 +0000103 if (AliasTy == MustAlias) {
104 assert(CallSites.empty() && "Illegal must alias set!");
105
106 // If this is a set of MustAliases, only check to see if the pointer aliases
107 // SOME value in the set...
Chris Lattner31a9d182003-02-26 22:11:00 +0000108 HashNodePair *SomePtr = getSomePointer();
Chris Lattner9971ac42003-02-24 20:37:56 +0000109 assert(SomePtr && "Empty must-alias set??");
Chris Lattner31a9d182003-02-26 22:11:00 +0000110 return AA.alias(SomePtr->first, SomePtr->second.getSize(), Ptr, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000111 }
112
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)
Chris Lattner31a9d182003-02-26 22:11:00 +0000116 if (AA.alias(Ptr, Size, I->first, I->second.getSize()))
Chris Lattner9971ac42003-02-24 20:37:56 +0000117 return true;
118
119 // Check the call sites list and invoke list...
120 if (!CallSites.empty())
121 // FIXME: this is pessimistic!
Chris Lattner009cc3d2002-09-26 21:49:07 +0000122 return true;
Chris Lattner9971ac42003-02-24 20:37:56 +0000123
Chris Lattner009cc3d2002-09-26 21:49:07 +0000124 return false;
125}
126
Chris Lattner9971ac42003-02-24 20:37:56 +0000127bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
128 // FIXME: Too conservative!
129 return true;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000130}
131
132
Chris Lattner009cc3d2002-09-26 21:49:07 +0000133/// 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.
136///
Chris Lattner31a9d182003-02-26 22:11:00 +0000137AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
138 unsigned Size) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000139 AliasSet *FoundSet = 0;
Chris Lattner9971ac42003-02-24 20:37:56 +0000140 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner31a9d182003-02-26 22:11:00 +0000141 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000142 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
Chris Lattner9971ac42003-02-24 20:37:56 +0000143 FoundSet = I; // Remember it.
Chris Lattner009cc3d2002-09-26 21:49:07 +0000144 } else { // Otherwise, we must merge the sets...
Chris Lattner9971ac42003-02-24 20:37:56 +0000145 FoundSet->mergeSetIn(*I); // Merge in contents...
Chris Lattner009cc3d2002-09-26 21:49:07 +0000146 }
147 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000148
149 return FoundSet;
150}
151
152AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
153 AliasSet *FoundSet = 0;
154 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000155 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000156 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
157 FoundSet = I; // Remember it.
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000158 } else if (!I->Forward) { // Otherwise, we must merge the sets...
Chris Lattner9971ac42003-02-24 20:37:56 +0000159 FoundSet->mergeSetIn(*I); // Merge in contents...
160 }
161 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000162
163 return FoundSet;
164}
165
166
Chris Lattner009cc3d2002-09-26 21:49:07 +0000167
Chris Lattner9971ac42003-02-24 20:37:56 +0000168
169/// getAliasSetForPointer - Return the alias set that the specified pointer
170/// lives in...
Chris Lattner31a9d182003-02-26 22:11:00 +0000171AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size){
Chris Lattner9971ac42003-02-24 20:37:56 +0000172 AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
173
174 // Check to see if the pointer is already known...
Chris Lattner31a9d182003-02-26 22:11:00 +0000175 if (Entry.second.hasAliasSet() && Size <= Entry.second.getSize()) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000176 // Return the set!
177 return *Entry.second.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattner31a9d182003-02-26 22:11:00 +0000178 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000179 // Add it to the alias set it aliases...
Chris Lattner31a9d182003-02-26 22:11:00 +0000180 AS->addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000181 return *AS;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000182 } else {
Chris Lattner9971ac42003-02-24 20:37:56 +0000183 // Otherwise create a new alias set to hold the loaded pointer...
Chris Lattner009cc3d2002-09-26 21:49:07 +0000184 AliasSets.push_back(AliasSet());
Chris Lattner31a9d182003-02-26 22:11:00 +0000185 AliasSets.back().addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000186 return AliasSets.back();
Chris Lattner009cc3d2002-09-26 21:49:07 +0000187 }
188}
189
Chris Lattner9971ac42003-02-24 20:37:56 +0000190void AliasSetTracker::add(LoadInst *LI) {
Chris Lattner31a9d182003-02-26 22:11:00 +0000191 addPointer(LI->getOperand(0),
192 AA.getTargetData().getTypeSize(LI->getType()), AliasSet::Refs);
Chris Lattner9971ac42003-02-24 20:37:56 +0000193}
194
Chris Lattner009cc3d2002-09-26 21:49:07 +0000195void AliasSetTracker::add(StoreInst *SI) {
Chris Lattner31a9d182003-02-26 22:11:00 +0000196 addPointer(SI->getOperand(1),
197 AA.getTargetData().getTypeSize(SI->getOperand(0)->getType()),
198 AliasSet::Mods);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000199}
200
Chris Lattner9971ac42003-02-24 20:37:56 +0000201void AliasSetTracker::add(CallSite CS) {
202 AliasSet *AS = findAliasSetForCallSite(CS);
203 if (!AS) {
204 AliasSets.push_back(AliasSet());
205 AS = &AliasSets.back();
206 }
207 AS->addCallSite(CS);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000208}
209
Chris Lattner9971ac42003-02-24 20:37:56 +0000210void AliasSetTracker::add(Instruction *I) {
211 // Dispatch to one of the other add methods...
212 if (LoadInst *LI = dyn_cast<LoadInst>(I))
213 add(LI);
214 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
215 add(SI);
216 else if (CallInst *CI = dyn_cast<CallInst>(I))
217 add(CI);
218 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
219 add(II);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000220}
221
Chris Lattner319d05b2003-03-03 23:28:05 +0000222void AliasSetTracker::add(BasicBlock &BB) {
223 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
224 add(I);
225}
226
227void AliasSetTracker::add(const AliasSetTracker &AST) {
228 assert(&AA == &AST.AA &&
229 "Merging AliasSetTracker objects with different Alias Analyses!");
230
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);
237
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]);
241
242 // Loop over all of the pointers in this alias set...
243 AliasSet::iterator I = AS.begin(), E = AS.end();
244 for (; I != E; ++I)
245 addPointer(I->first, I->second.getSize(),
246 (AliasSet::AccessType)AS.AccessTy);
247 }
248}
249
Chris Lattner9971ac42003-02-24 20:37:56 +0000250//===----------------------------------------------------------------------===//
251// AliasSet/AliasSetTracker Printing Support
252//===----------------------------------------------------------------------===//
253
254void AliasSet::print(std::ostream &OS) const {
255 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
256 OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, ";
257 switch (AccessTy) {
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!");
Chris Lattner009cc3d2002-09-26 21:49:07 +0000263 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000264 if (Forward)
265 OS << " forwarding to " << (void*)Forward;
266
267
268 if (begin() != end()) {
269 OS << "Pointers: ";
270 for (iterator I = begin(), E = end(); I != E; ++I) {
271 if (I != begin()) OS << ", ";
Chris Lattner31a9d182003-02-26 22:11:00 +0000272 WriteAsOperand(OS << "(", I->first);
273 OS << ", " << I->second.getSize() << ")";
Chris Lattner9971ac42003-02-24 20:37:56 +0000274 }
275 }
276 if (!CallSites.empty()) {
277 OS << "\n " << CallSites.size() << " Call Sites: ";
278 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
279 if (i) OS << ", ";
280 WriteAsOperand(OS, CallSites[i].getCalledValue());
281 }
282 }
283 OS << "\n";
284}
285
286void 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)
290 I->print(OS);
291 OS << "\n";
292}
293
294void AliasSet::dump() const { print (std::cerr); }
295void AliasSetTracker::dump() const { print(std::cerr); }
296
297
298//===----------------------------------------------------------------------===//
299// AliasSetPrinter Pass
300//===----------------------------------------------------------------------===//
301
302namespace {
303 class AliasSetPrinter : public FunctionPass {
304 AliasSetTracker *Tracker;
305 public:
306 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
307 AU.setPreservesAll();
308 AU.addRequired<AliasAnalysis>();
309 }
310
311 virtual bool runOnFunction(Function &F) {
312 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
313
314 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
315 Tracker->add(*I);
316 return false;
317 }
318
319 /// print - Convert to human readable form
320 virtual void print(std::ostream &OS) const {
321 Tracker->print(OS);
322 }
323
324 virtual void releaseMemory() {
325 delete Tracker;
326 }
327 };
328 RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer",
329 PassInfo::Analysis | PassInfo::Optimization);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000330}