blob: 67cef76d57931b864ecb499fc23d793f350d8a18 [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 Lattnere2609242003-12-14 04:52:11 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Chris Lattner009cc3d2002-09-26 21:49:07 +000025/// mergeSetIn - Merge the specified alias set into this alias set...
26///
Chris Lattner9971ac42003-02-24 20:37:56 +000027void AliasSet::mergeSetIn(AliasSet &AS) {
28 assert(!AS.Forward && "Alias set is already forwarding!");
29 assert(!Forward && "This set is a forwarding set!!");
Chris Lattner009cc3d2002-09-26 21:49:07 +000030
31 // Update the alias and access types of this set...
Chris Lattner9971ac42003-02-24 20:37:56 +000032 AccessTy |= AS.AccessTy;
33 AliasTy |= AS.AliasTy;
34
35 if (CallSites.empty()) { // Merge call sites...
36 if (!AS.CallSites.empty())
37 std::swap(CallSites, AS.CallSites);
38 } else if (!AS.CallSites.empty()) {
39 CallSites.insert(CallSites.end(), AS.CallSites.begin(), AS.CallSites.end());
40 AS.CallSites.clear();
41 }
42
43 // FIXME: If AS's refcount is zero, nuke it now...
44 assert(RefCount != 0);
45
46 AS.Forward = this; // Forward across AS now...
47 RefCount++; // AS is now pointing to us...
48
49 // Merge the list of constituent pointers...
Chris Lattner2cffeec2003-12-18 08:11:56 +000050 if (AS.PtrList) {
51 *PtrListEnd = AS.PtrList;
52 AS.PtrList->second.setPrevInList(PtrListEnd);
53 PtrListEnd = AS.PtrListEnd;
54
55 AS.PtrList = 0;
56 AS.PtrListEnd = &AS.PtrList;
57 }
Chris Lattner009cc3d2002-09-26 21:49:07 +000058}
59
Chris Lattner9971ac42003-02-24 20:37:56 +000060void AliasSetTracker::removeAliasSet(AliasSet *AS) {
61 AliasSets.erase(AS);
62}
63
64void AliasSet::removeFromTracker(AliasSetTracker &AST) {
65 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
66 AST.removeAliasSet(this);
67}
68
Chris Lattner31a9d182003-02-26 22:11:00 +000069void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
70 unsigned Size) {
Chris Lattner9971ac42003-02-24 20:37:56 +000071 assert(!Entry.second.hasAliasSet() && "Entry already in set!");
72
73 AliasAnalysis &AA = AST.getAliasAnalysis();
74
75 if (isMustAlias()) // Check to see if we have to downgrade to _may_ alias
Chris Lattner31a9d182003-02-26 22:11:00 +000076 if (HashNodePair *P = getSomePointer()) {
77 AliasAnalysis::AliasResult Result =
78 AA.alias(P->first, P->second.getSize(), Entry.first, Size);
79 if (Result == AliasAnalysis::MayAlias)
Chris Lattner9971ac42003-02-24 20:37:56 +000080 AliasTy = MayAlias;
Chris Lattner31a9d182003-02-26 22:11:00 +000081 else // First entry of must alias must have maximum size!
82 P->second.updateSize(Size);
83 assert(Result != AliasAnalysis::NoAlias && "Cannot be part of must set!");
84 }
Chris Lattner9971ac42003-02-24 20:37:56 +000085
86 Entry.second.setAliasSet(this);
Chris Lattner31a9d182003-02-26 22:11:00 +000087 Entry.second.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +000088
89 // Add it to the end of the list...
Chris Lattner2cffeec2003-12-18 08:11:56 +000090 assert(*PtrListEnd == 0 && "End of list is not null?");
91 *PtrListEnd = &Entry;
92 PtrListEnd = Entry.second.setPrevInList(PtrListEnd);
Chris Lattner9971ac42003-02-24 20:37:56 +000093 RefCount++; // Entry points to alias set...
94}
95
96void AliasSet::addCallSite(CallSite CS) {
97 CallSites.push_back(CS);
Chris Lattner31a9d182003-02-26 22:11:00 +000098 AliasTy = MayAlias; // FIXME: Too conservative?
Chris Lattner577385e2003-05-03 03:42:08 +000099 AccessTy = ModRef;
Chris Lattner9971ac42003-02-24 20:37:56 +0000100}
101
102/// aliasesPointer - Return true if the specified pointer "may" (or must)
Chris Lattner009cc3d2002-09-26 21:49:07 +0000103/// alias one of the members in the set.
104///
Chris Lattner31a9d182003-02-26 22:11:00 +0000105bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
106 AliasAnalysis &AA) const {
Chris Lattner9971ac42003-02-24 20:37:56 +0000107 if (AliasTy == MustAlias) {
108 assert(CallSites.empty() && "Illegal must alias set!");
109
110 // If this is a set of MustAliases, only check to see if the pointer aliases
111 // SOME value in the set...
Chris Lattner31a9d182003-02-26 22:11:00 +0000112 HashNodePair *SomePtr = getSomePointer();
Chris Lattner9971ac42003-02-24 20:37:56 +0000113 assert(SomePtr && "Empty must-alias set??");
Chris Lattner31a9d182003-02-26 22:11:00 +0000114 return AA.alias(SomePtr->first, SomePtr->second.getSize(), Ptr, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000115 }
116
117 // If this is a may-alias set, we have to check all of the pointers in the set
118 // to be sure it doesn't alias the set...
119 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner31a9d182003-02-26 22:11:00 +0000120 if (AA.alias(Ptr, Size, I->first, I->second.getSize()))
Chris Lattner9971ac42003-02-24 20:37:56 +0000121 return true;
122
123 // Check the call sites list and invoke list...
124 if (!CallSites.empty())
125 // FIXME: this is pessimistic!
Chris Lattner009cc3d2002-09-26 21:49:07 +0000126 return true;
Chris Lattner9971ac42003-02-24 20:37:56 +0000127
Chris Lattner009cc3d2002-09-26 21:49:07 +0000128 return false;
129}
130
Chris Lattner9971ac42003-02-24 20:37:56 +0000131bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
132 // FIXME: Too conservative!
133 return true;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000134}
135
136
Chris Lattner009cc3d2002-09-26 21:49:07 +0000137/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
138/// instruction referring to the pointer into. If there are multiple alias sets
139/// that may alias the pointer, merge them together and return the unified set.
140///
Chris Lattner31a9d182003-02-26 22:11:00 +0000141AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
142 unsigned Size) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000143 AliasSet *FoundSet = 0;
Chris Lattner9971ac42003-02-24 20:37:56 +0000144 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner31a9d182003-02-26 22:11:00 +0000145 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000146 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
Chris Lattner9971ac42003-02-24 20:37:56 +0000147 FoundSet = I; // Remember it.
Chris Lattner009cc3d2002-09-26 21:49:07 +0000148 } else { // Otherwise, we must merge the sets...
Chris Lattner9971ac42003-02-24 20:37:56 +0000149 FoundSet->mergeSetIn(*I); // Merge in contents...
Chris Lattner009cc3d2002-09-26 21:49:07 +0000150 }
151 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000152
153 return FoundSet;
154}
155
156AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
157 AliasSet *FoundSet = 0;
158 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000159 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000160 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
161 FoundSet = I; // Remember it.
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000162 } else if (!I->Forward) { // Otherwise, we must merge the sets...
Chris Lattner9971ac42003-02-24 20:37:56 +0000163 FoundSet->mergeSetIn(*I); // Merge in contents...
164 }
165 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000166
167 return FoundSet;
168}
169
170
Chris Lattner009cc3d2002-09-26 21:49:07 +0000171
Chris Lattner9971ac42003-02-24 20:37:56 +0000172
173/// getAliasSetForPointer - Return the alias set that the specified pointer
174/// lives in...
Chris Lattner31a9d182003-02-26 22:11:00 +0000175AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size){
Chris Lattner9971ac42003-02-24 20:37:56 +0000176 AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
177
178 // Check to see if the pointer is already known...
Chris Lattner31a9d182003-02-26 22:11:00 +0000179 if (Entry.second.hasAliasSet() && Size <= Entry.second.getSize()) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000180 // Return the set!
181 return *Entry.second.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattner31a9d182003-02-26 22:11:00 +0000182 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000183 // Add it to the alias set it aliases...
Chris Lattner31a9d182003-02-26 22:11:00 +0000184 AS->addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000185 return *AS;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000186 } else {
Chris Lattner9971ac42003-02-24 20:37:56 +0000187 // Otherwise create a new alias set to hold the loaded pointer...
Chris Lattner009cc3d2002-09-26 21:49:07 +0000188 AliasSets.push_back(AliasSet());
Chris Lattner31a9d182003-02-26 22:11:00 +0000189 AliasSets.back().addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000190 return AliasSets.back();
Chris Lattner009cc3d2002-09-26 21:49:07 +0000191 }
192}
193
Chris Lattner9971ac42003-02-24 20:37:56 +0000194void AliasSetTracker::add(LoadInst *LI) {
Chris Lattnere2609242003-12-14 04:52:11 +0000195 AliasSet &AS =
196 addPointer(LI->getOperand(0),
197 AA.getTargetData().getTypeSize(LI->getType()), AliasSet::Refs);
198 if (LI->isVolatile()) AS.setVolatile();
Chris Lattner9971ac42003-02-24 20:37:56 +0000199}
200
Chris Lattner009cc3d2002-09-26 21:49:07 +0000201void AliasSetTracker::add(StoreInst *SI) {
Chris Lattnere2609242003-12-14 04:52:11 +0000202 AliasSet &AS =
203 addPointer(SI->getOperand(1),
204 AA.getTargetData().getTypeSize(SI->getOperand(0)->getType()),
205 AliasSet::Mods);
206 if (SI->isVolatile()) AS.setVolatile();
Chris Lattner009cc3d2002-09-26 21:49:07 +0000207}
208
Chris Lattnere2609242003-12-14 04:52:11 +0000209
Chris Lattner9971ac42003-02-24 20:37:56 +0000210void AliasSetTracker::add(CallSite CS) {
211 AliasSet *AS = findAliasSetForCallSite(CS);
212 if (!AS) {
213 AliasSets.push_back(AliasSet());
214 AS = &AliasSets.back();
215 }
216 AS->addCallSite(CS);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000217}
218
Chris Lattner9971ac42003-02-24 20:37:56 +0000219void AliasSetTracker::add(Instruction *I) {
220 // Dispatch to one of the other add methods...
221 if (LoadInst *LI = dyn_cast<LoadInst>(I))
222 add(LI);
223 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
224 add(SI);
225 else if (CallInst *CI = dyn_cast<CallInst>(I))
226 add(CI);
227 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
228 add(II);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000229}
230
Chris Lattner319d05b2003-03-03 23:28:05 +0000231void AliasSetTracker::add(BasicBlock &BB) {
232 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
233 add(I);
234}
235
236void AliasSetTracker::add(const AliasSetTracker &AST) {
237 assert(&AA == &AST.AA &&
238 "Merging AliasSetTracker objects with different Alias Analyses!");
239
240 // Loop over all of the alias sets in AST, adding the pointers contained
241 // therein into the current alias sets. This can cause alias sets to be
242 // merged together in the current AST.
243 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
244 if (!I->Forward) { // Ignore forwarding alias sets
245 AliasSet &AS = const_cast<AliasSet&>(*I);
246
247 // If there are any call sites in the alias set, add them to this AST.
248 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
249 add(AS.CallSites[i]);
250
251 // Loop over all of the pointers in this alias set...
252 AliasSet::iterator I = AS.begin(), E = AS.end();
253 for (; I != E; ++I)
254 addPointer(I->first, I->second.getSize(),
255 (AliasSet::AccessType)AS.AccessTy);
256 }
257}
258
Chris Lattner2cffeec2003-12-18 08:11:56 +0000259
260// remove method - This method is used to remove a pointer value from the
261// AliasSetTracker entirely. It should be used when an instruction is deleted
262// from the program to update the AST. If you don't use this, you would have
263// dangling pointers to deleted instructions.
264//
265void AliasSetTracker::remove(Value *PtrVal) {
266 // First, look up the PointerRec for this pointer...
267 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(PtrVal);
268 if (I == PointerMap.end()) return; // Noop
269
270 // If we found one, remove the pointer from the alias set it is in.
271 AliasSet::HashNodePair &PtrValEnt = *I;
272 AliasSet *AS = PtrValEnt.second.getAliasSet(*this);
273
274 // Unlink from the list of values...
275 PtrValEnt.second.removeFromList();
276 // Stop using the alias set
277 if (--AS->RefCount == 0)
278 AS->removeFromTracker(*this);
279
280 PointerMap.erase(I);
281}
282
283
Chris Lattner9971ac42003-02-24 20:37:56 +0000284//===----------------------------------------------------------------------===//
285// AliasSet/AliasSetTracker Printing Support
286//===----------------------------------------------------------------------===//
287
288void AliasSet::print(std::ostream &OS) const {
289 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
290 OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, ";
291 switch (AccessTy) {
292 case NoModRef: OS << "No access "; break;
293 case Refs : OS << "Ref "; break;
294 case Mods : OS << "Mod "; break;
295 case ModRef : OS << "Mod/Ref "; break;
296 default: assert(0 && "Bad value for AccessTy!");
Chris Lattner009cc3d2002-09-26 21:49:07 +0000297 }
Chris Lattnere2609242003-12-14 04:52:11 +0000298 if (isVolatile()) OS << "[volatile] ";
Chris Lattner9971ac42003-02-24 20:37:56 +0000299 if (Forward)
300 OS << " forwarding to " << (void*)Forward;
301
302
303 if (begin() != end()) {
304 OS << "Pointers: ";
305 for (iterator I = begin(), E = end(); I != E; ++I) {
306 if (I != begin()) OS << ", ";
Chris Lattner31a9d182003-02-26 22:11:00 +0000307 WriteAsOperand(OS << "(", I->first);
308 OS << ", " << I->second.getSize() << ")";
Chris Lattner9971ac42003-02-24 20:37:56 +0000309 }
310 }
311 if (!CallSites.empty()) {
312 OS << "\n " << CallSites.size() << " Call Sites: ";
313 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
314 if (i) OS << ", ";
315 WriteAsOperand(OS, CallSites[i].getCalledValue());
316 }
317 }
318 OS << "\n";
319}
320
321void AliasSetTracker::print(std::ostream &OS) const {
322 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
323 << PointerMap.size() << " pointer values.\n";
324 for (const_iterator I = begin(), E = end(); I != E; ++I)
325 I->print(OS);
326 OS << "\n";
327}
328
329void AliasSet::dump() const { print (std::cerr); }
330void AliasSetTracker::dump() const { print(std::cerr); }
331
Chris Lattner9971ac42003-02-24 20:37:56 +0000332//===----------------------------------------------------------------------===//
333// AliasSetPrinter Pass
334//===----------------------------------------------------------------------===//
335
336namespace {
337 class AliasSetPrinter : public FunctionPass {
338 AliasSetTracker *Tracker;
339 public:
340 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
341 AU.setPreservesAll();
342 AU.addRequired<AliasAnalysis>();
343 }
344
345 virtual bool runOnFunction(Function &F) {
346 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
347
348 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
349 Tracker->add(*I);
350 return false;
351 }
352
353 /// print - Convert to human readable form
354 virtual void print(std::ostream &OS) const {
355 Tracker->print(OS);
356 }
357
358 virtual void releaseMemory() {
359 delete Tracker;
360 }
361 };
362 RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer",
363 PassInfo::Analysis | PassInfo::Optimization);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000364}