blob: d266c2a5a07992ecf48363ebb94551edcd883273 [file] [log] [blame]
Chris Lattner647df642002-09-26 21:49:07 +00001//===- AliasSetTracker.cpp - Alias Sets Tracker implementation-------------===//
John Criswell482202a2003-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 Lattner647df642002-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 Lattner7606fb62003-02-24 20:37:56 +000019#include "llvm/Pass.h"
Chris Lattnerb5b0b7a2003-02-26 22:11:00 +000020#include "llvm/Target/TargetData.h"
Chris Lattner7606fb62003-02-24 20:37:56 +000021#include "llvm/Assembly/Writer.h"
22#include "llvm/Support/InstIterator.h"
Chris Lattner0a140602003-12-14 04:52:11 +000023using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000024
Chris Lattner647df642002-09-26 21:49:07 +000025/// mergeSetIn - Merge the specified alias set into this alias set...
26///
Chris Lattner7606fb62003-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 Lattner647df642002-09-26 21:49:07 +000030
31 // Update the alias and access types of this set...
Chris Lattner7606fb62003-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 Lattner44fea542003-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 Lattner647df642002-09-26 21:49:07 +000058}
59
Chris Lattner7606fb62003-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 Lattnerb5b0b7a2003-02-26 22:11:00 +000069void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
70 unsigned Size) {
Chris Lattner7606fb62003-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 Lattnerb5b0b7a2003-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 Lattner7606fb62003-02-24 20:37:56 +000080 AliasTy = MayAlias;
Chris Lattnerb5b0b7a2003-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 Lattner7606fb62003-02-24 20:37:56 +000085
86 Entry.second.setAliasSet(this);
Chris Lattnerb5b0b7a2003-02-26 22:11:00 +000087 Entry.second.updateSize(Size);
Chris Lattner7606fb62003-02-24 20:37:56 +000088
89 // Add it to the end of the list...
Chris Lattner44fea542003-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 Lattner7606fb62003-02-24 20:37:56 +000093 RefCount++; // Entry points to alias set...
94}
95
Chris Lattner21c60f12004-03-15 04:08:36 +000096void AliasSet::addCallSite(CallSite CS, AliasAnalysis &AA) {
Chris Lattner7606fb62003-02-24 20:37:56 +000097 CallSites.push_back(CS);
Chris Lattner21c60f12004-03-15 04:08:36 +000098
99 if (Function *F = CS.getCalledFunction()) {
100 if (AA.doesNotAccessMemory(F))
101 return;
102 else if (AA.onlyReadsMemory(F)) {
103 AliasTy = MayAlias;
104 AccessTy = Refs;
105 return;
106 }
107 }
108
109 // FIXME: This should use mod/ref information to make this not suck so bad
110 AliasTy = MayAlias;
Chris Lattnerd14c2002003-05-03 03:42:08 +0000111 AccessTy = ModRef;
Chris Lattner7606fb62003-02-24 20:37:56 +0000112}
113
114/// aliasesPointer - Return true if the specified pointer "may" (or must)
Chris Lattner647df642002-09-26 21:49:07 +0000115/// alias one of the members in the set.
116///
Chris Lattnerb5b0b7a2003-02-26 22:11:00 +0000117bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
118 AliasAnalysis &AA) const {
Chris Lattner7606fb62003-02-24 20:37:56 +0000119 if (AliasTy == MustAlias) {
120 assert(CallSites.empty() && "Illegal must alias set!");
121
122 // If this is a set of MustAliases, only check to see if the pointer aliases
123 // SOME value in the set...
Chris Lattnerb5b0b7a2003-02-26 22:11:00 +0000124 HashNodePair *SomePtr = getSomePointer();
Chris Lattner7606fb62003-02-24 20:37:56 +0000125 assert(SomePtr && "Empty must-alias set??");
Chris Lattnerb5b0b7a2003-02-26 22:11:00 +0000126 return AA.alias(SomePtr->first, SomePtr->second.getSize(), Ptr, Size);
Chris Lattner7606fb62003-02-24 20:37:56 +0000127 }
128
129 // If this is a may-alias set, we have to check all of the pointers in the set
130 // to be sure it doesn't alias the set...
131 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattnerb5b0b7a2003-02-26 22:11:00 +0000132 if (AA.alias(Ptr, Size, I->first, I->second.getSize()))
Chris Lattner7606fb62003-02-24 20:37:56 +0000133 return true;
134
135 // Check the call sites list and invoke list...
136 if (!CallSites.empty())
137 // FIXME: this is pessimistic!
Chris Lattner647df642002-09-26 21:49:07 +0000138 return true;
Chris Lattner7606fb62003-02-24 20:37:56 +0000139
Chris Lattner647df642002-09-26 21:49:07 +0000140 return false;
141}
142
Chris Lattner7606fb62003-02-24 20:37:56 +0000143bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
Chris Lattner21c60f12004-03-15 04:08:36 +0000144 // FIXME: Use mod/ref information to prune this better!
145 if (Function *F = CS.getCalledFunction())
146 if (AA.doesNotAccessMemory(F))
147 return false;
148
Chris Lattner7606fb62003-02-24 20:37:56 +0000149 return true;
Chris Lattner647df642002-09-26 21:49:07 +0000150}
151
152
Chris Lattner647df642002-09-26 21:49:07 +0000153/// findAliasSetForPointer - Given a pointer, find the one alias set to put the
154/// instruction referring to the pointer into. If there are multiple alias sets
155/// that may alias the pointer, merge them together and return the unified set.
156///
Chris Lattnerb5b0b7a2003-02-26 22:11:00 +0000157AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
158 unsigned Size) {
Chris Lattner647df642002-09-26 21:49:07 +0000159 AliasSet *FoundSet = 0;
Chris Lattner7606fb62003-02-24 20:37:56 +0000160 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattnerb5b0b7a2003-02-26 22:11:00 +0000161 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
Chris Lattner647df642002-09-26 21:49:07 +0000162 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
Chris Lattner7606fb62003-02-24 20:37:56 +0000163 FoundSet = I; // Remember it.
Chris Lattner647df642002-09-26 21:49:07 +0000164 } else { // Otherwise, we must merge the sets...
Chris Lattner7606fb62003-02-24 20:37:56 +0000165 FoundSet->mergeSetIn(*I); // Merge in contents...
Chris Lattner647df642002-09-26 21:49:07 +0000166 }
167 }
Chris Lattner7606fb62003-02-24 20:37:56 +0000168
169 return FoundSet;
170}
171
172AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
173 AliasSet *FoundSet = 0;
174 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner71b82bb2003-02-26 19:28:57 +0000175 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
Chris Lattner7606fb62003-02-24 20:37:56 +0000176 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
177 FoundSet = I; // Remember it.
Chris Lattner71b82bb2003-02-26 19:28:57 +0000178 } else if (!I->Forward) { // Otherwise, we must merge the sets...
Chris Lattner7606fb62003-02-24 20:37:56 +0000179 FoundSet->mergeSetIn(*I); // Merge in contents...
180 }
181 }
Chris Lattner647df642002-09-26 21:49:07 +0000182
183 return FoundSet;
184}
185
186
Chris Lattner647df642002-09-26 21:49:07 +0000187
Chris Lattner7606fb62003-02-24 20:37:56 +0000188
189/// getAliasSetForPointer - Return the alias set that the specified pointer
190/// lives in...
Chris Lattnerb5b0b7a2003-02-26 22:11:00 +0000191AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size){
Chris Lattner7606fb62003-02-24 20:37:56 +0000192 AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
193
194 // Check to see if the pointer is already known...
Chris Lattnerb5b0b7a2003-02-26 22:11:00 +0000195 if (Entry.second.hasAliasSet() && Size <= Entry.second.getSize()) {
Chris Lattner7606fb62003-02-24 20:37:56 +0000196 // Return the set!
197 return *Entry.second.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattnerb5b0b7a2003-02-26 22:11:00 +0000198 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
Chris Lattner7606fb62003-02-24 20:37:56 +0000199 // Add it to the alias set it aliases...
Chris Lattnerb5b0b7a2003-02-26 22:11:00 +0000200 AS->addPointer(*this, Entry, Size);
Chris Lattner7606fb62003-02-24 20:37:56 +0000201 return *AS;
Chris Lattner647df642002-09-26 21:49:07 +0000202 } else {
Chris Lattner7606fb62003-02-24 20:37:56 +0000203 // Otherwise create a new alias set to hold the loaded pointer...
Chris Lattner647df642002-09-26 21:49:07 +0000204 AliasSets.push_back(AliasSet());
Chris Lattnerb5b0b7a2003-02-26 22:11:00 +0000205 AliasSets.back().addPointer(*this, Entry, Size);
Chris Lattner7606fb62003-02-24 20:37:56 +0000206 return AliasSets.back();
Chris Lattner647df642002-09-26 21:49:07 +0000207 }
208}
209
Chris Lattner7606fb62003-02-24 20:37:56 +0000210void AliasSetTracker::add(LoadInst *LI) {
Chris Lattner0a140602003-12-14 04:52:11 +0000211 AliasSet &AS =
212 addPointer(LI->getOperand(0),
213 AA.getTargetData().getTypeSize(LI->getType()), AliasSet::Refs);
214 if (LI->isVolatile()) AS.setVolatile();
Chris Lattner7606fb62003-02-24 20:37:56 +0000215}
216
Chris Lattner647df642002-09-26 21:49:07 +0000217void AliasSetTracker::add(StoreInst *SI) {
Chris Lattner0a140602003-12-14 04:52:11 +0000218 AliasSet &AS =
219 addPointer(SI->getOperand(1),
220 AA.getTargetData().getTypeSize(SI->getOperand(0)->getType()),
221 AliasSet::Mods);
222 if (SI->isVolatile()) AS.setVolatile();
Chris Lattner647df642002-09-26 21:49:07 +0000223}
224
Chris Lattner0a140602003-12-14 04:52:11 +0000225
Chris Lattner7606fb62003-02-24 20:37:56 +0000226void AliasSetTracker::add(CallSite CS) {
227 AliasSet *AS = findAliasSetForCallSite(CS);
228 if (!AS) {
229 AliasSets.push_back(AliasSet());
230 AS = &AliasSets.back();
231 }
Chris Lattner21c60f12004-03-15 04:08:36 +0000232 AS->addCallSite(CS, AA);
Chris Lattner647df642002-09-26 21:49:07 +0000233}
234
Chris Lattner7606fb62003-02-24 20:37:56 +0000235void AliasSetTracker::add(Instruction *I) {
236 // Dispatch to one of the other add methods...
237 if (LoadInst *LI = dyn_cast<LoadInst>(I))
238 add(LI);
239 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
240 add(SI);
241 else if (CallInst *CI = dyn_cast<CallInst>(I))
242 add(CI);
243 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
244 add(II);
Chris Lattner647df642002-09-26 21:49:07 +0000245}
246
Chris Lattnerc048bb32003-03-03 23:28:05 +0000247void AliasSetTracker::add(BasicBlock &BB) {
248 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
249 add(I);
250}
251
252void AliasSetTracker::add(const AliasSetTracker &AST) {
253 assert(&AA == &AST.AA &&
254 "Merging AliasSetTracker objects with different Alias Analyses!");
255
256 // Loop over all of the alias sets in AST, adding the pointers contained
257 // therein into the current alias sets. This can cause alias sets to be
258 // merged together in the current AST.
259 for (const_iterator I = AST.begin(), E = AST.end(); I != E; ++I)
260 if (!I->Forward) { // Ignore forwarding alias sets
261 AliasSet &AS = const_cast<AliasSet&>(*I);
262
263 // If there are any call sites in the alias set, add them to this AST.
264 for (unsigned i = 0, e = AS.CallSites.size(); i != e; ++i)
265 add(AS.CallSites[i]);
266
267 // Loop over all of the pointers in this alias set...
268 AliasSet::iterator I = AS.begin(), E = AS.end();
269 for (; I != E; ++I)
270 addPointer(I->first, I->second.getSize(),
271 (AliasSet::AccessType)AS.AccessTy);
272 }
273}
274
Chris Lattner44fea542003-12-18 08:11:56 +0000275
276// remove method - This method is used to remove a pointer value from the
277// AliasSetTracker entirely. It should be used when an instruction is deleted
278// from the program to update the AST. If you don't use this, you would have
279// dangling pointers to deleted instructions.
280//
281void AliasSetTracker::remove(Value *PtrVal) {
282 // First, look up the PointerRec for this pointer...
283 hash_map<Value*, AliasSet::PointerRec>::iterator I = PointerMap.find(PtrVal);
284 if (I == PointerMap.end()) return; // Noop
285
286 // If we found one, remove the pointer from the alias set it is in.
287 AliasSet::HashNodePair &PtrValEnt = *I;
288 AliasSet *AS = PtrValEnt.second.getAliasSet(*this);
289
290 // Unlink from the list of values...
291 PtrValEnt.second.removeFromList();
292 // Stop using the alias set
293 if (--AS->RefCount == 0)
294 AS->removeFromTracker(*this);
295
296 PointerMap.erase(I);
297}
298
299
Chris Lattner7606fb62003-02-24 20:37:56 +0000300//===----------------------------------------------------------------------===//
301// AliasSet/AliasSetTracker Printing Support
302//===----------------------------------------------------------------------===//
303
304void AliasSet::print(std::ostream &OS) const {
305 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
306 OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, ";
307 switch (AccessTy) {
308 case NoModRef: OS << "No access "; break;
309 case Refs : OS << "Ref "; break;
310 case Mods : OS << "Mod "; break;
311 case ModRef : OS << "Mod/Ref "; break;
312 default: assert(0 && "Bad value for AccessTy!");
Chris Lattner647df642002-09-26 21:49:07 +0000313 }
Chris Lattner0a140602003-12-14 04:52:11 +0000314 if (isVolatile()) OS << "[volatile] ";
Chris Lattner7606fb62003-02-24 20:37:56 +0000315 if (Forward)
316 OS << " forwarding to " << (void*)Forward;
317
318
319 if (begin() != end()) {
320 OS << "Pointers: ";
321 for (iterator I = begin(), E = end(); I != E; ++I) {
322 if (I != begin()) OS << ", ";
Chris Lattnerb5b0b7a2003-02-26 22:11:00 +0000323 WriteAsOperand(OS << "(", I->first);
324 OS << ", " << I->second.getSize() << ")";
Chris Lattner7606fb62003-02-24 20:37:56 +0000325 }
326 }
327 if (!CallSites.empty()) {
328 OS << "\n " << CallSites.size() << " Call Sites: ";
329 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
330 if (i) OS << ", ";
331 WriteAsOperand(OS, CallSites[i].getCalledValue());
332 }
333 }
334 OS << "\n";
335}
336
337void AliasSetTracker::print(std::ostream &OS) const {
338 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
339 << PointerMap.size() << " pointer values.\n";
340 for (const_iterator I = begin(), E = end(); I != E; ++I)
341 I->print(OS);
342 OS << "\n";
343}
344
345void AliasSet::dump() const { print (std::cerr); }
346void AliasSetTracker::dump() const { print(std::cerr); }
347
Chris Lattner7606fb62003-02-24 20:37:56 +0000348//===----------------------------------------------------------------------===//
349// AliasSetPrinter Pass
350//===----------------------------------------------------------------------===//
351
352namespace {
353 class AliasSetPrinter : public FunctionPass {
354 AliasSetTracker *Tracker;
355 public:
356 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
357 AU.setPreservesAll();
358 AU.addRequired<AliasAnalysis>();
359 }
360
361 virtual bool runOnFunction(Function &F) {
362 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
363
364 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
365 Tracker->add(*I);
366 return false;
367 }
368
369 /// print - Convert to human readable form
370 virtual void print(std::ostream &OS) const {
371 Tracker->print(OS);
372 }
373
374 virtual void releaseMemory() {
375 delete Tracker;
376 }
377 };
378 RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer",
379 PassInfo::Analysis | PassInfo::Optimization);
Chris Lattner647df642002-09-26 21:49:07 +0000380}