blob: 4a293448ed32106417384824cbef5461109c4e1b [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
Brian Gaeked0fde302003-11-11 22:41:34 +000024namespace llvm {
25
Chris Lattner009cc3d2002-09-26 21:49:07 +000026/// mergeSetIn - Merge the specified alias set into this alias set...
27///
Chris Lattner9971ac42003-02-24 20:37:56 +000028void AliasSet::mergeSetIn(AliasSet &AS) {
29 assert(!AS.Forward && "Alias set is already forwarding!");
30 assert(!Forward && "This set is a forwarding set!!");
Chris Lattner009cc3d2002-09-26 21:49:07 +000031
32 // Update the alias and access types of this set...
Chris Lattner9971ac42003-02-24 20:37:56 +000033 AccessTy |= AS.AccessTy;
34 AliasTy |= AS.AliasTy;
35
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());
41 AS.CallSites.clear();
42 }
43
44 // FIXME: If AS's refcount is zero, nuke it now...
45 assert(RefCount != 0);
46
47 AS.Forward = this; // Forward across AS now...
48 RefCount++; // AS is now pointing to us...
49
50 // Merge the list of constituent pointers...
51 PtrListTail->second.setTail(AS.PtrListHead);
52 PtrListTail = AS.PtrListTail;
53 AS.PtrListHead = AS.PtrListTail = 0;
Chris Lattner009cc3d2002-09-26 21:49:07 +000054}
55
Chris Lattner9971ac42003-02-24 20:37:56 +000056void AliasSetTracker::removeAliasSet(AliasSet *AS) {
57 AliasSets.erase(AS);
58}
59
60void AliasSet::removeFromTracker(AliasSetTracker &AST) {
61 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
62 AST.removeAliasSet(this);
63}
64
Chris Lattner31a9d182003-02-26 22:11:00 +000065void AliasSet::addPointer(AliasSetTracker &AST, HashNodePair &Entry,
66 unsigned Size) {
Chris Lattner9971ac42003-02-24 20:37:56 +000067 assert(!Entry.second.hasAliasSet() && "Entry already in set!");
68
69 AliasAnalysis &AA = AST.getAliasAnalysis();
70
71 if (isMustAlias()) // Check to see if we have to downgrade to _may_ alias
Chris Lattner31a9d182003-02-26 22:11:00 +000072 if (HashNodePair *P = getSomePointer()) {
73 AliasAnalysis::AliasResult Result =
74 AA.alias(P->first, P->second.getSize(), Entry.first, Size);
75 if (Result == AliasAnalysis::MayAlias)
Chris Lattner9971ac42003-02-24 20:37:56 +000076 AliasTy = MayAlias;
Chris Lattner31a9d182003-02-26 22:11:00 +000077 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!");
80 }
Chris Lattner9971ac42003-02-24 20:37:56 +000081
82 Entry.second.setAliasSet(this);
Chris Lattner31a9d182003-02-26 22:11:00 +000083 Entry.second.updateSize(Size);
Chris Lattner9971ac42003-02-24 20:37:56 +000084
85 // Add it to the end of the list...
86 if (PtrListTail)
87 PtrListTail->second.setTail(&Entry);
88 else
89 PtrListHead = &Entry;
90 PtrListTail = &Entry;
91 RefCount++; // Entry points to alias set...
92}
93
94void AliasSet::addCallSite(CallSite CS) {
95 CallSites.push_back(CS);
Chris Lattner31a9d182003-02-26 22:11:00 +000096 AliasTy = MayAlias; // FIXME: Too conservative?
Chris Lattner577385e2003-05-03 03:42:08 +000097 AccessTy = ModRef;
Chris Lattner9971ac42003-02-24 20:37:56 +000098}
99
100/// aliasesPointer - Return true if the specified pointer "may" (or must)
Chris Lattner009cc3d2002-09-26 21:49:07 +0000101/// alias one of the members in the set.
102///
Chris Lattner31a9d182003-02-26 22:11:00 +0000103bool AliasSet::aliasesPointer(const Value *Ptr, unsigned Size,
104 AliasAnalysis &AA) const {
Chris Lattner9971ac42003-02-24 20:37:56 +0000105 if (AliasTy == MustAlias) {
106 assert(CallSites.empty() && "Illegal must alias set!");
107
108 // If this is a set of MustAliases, only check to see if the pointer aliases
109 // SOME value in the set...
Chris Lattner31a9d182003-02-26 22:11:00 +0000110 HashNodePair *SomePtr = getSomePointer();
Chris Lattner9971ac42003-02-24 20:37:56 +0000111 assert(SomePtr && "Empty must-alias set??");
Chris Lattner31a9d182003-02-26 22:11:00 +0000112 return AA.alias(SomePtr->first, SomePtr->second.getSize(), Ptr, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000113 }
114
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)
Chris Lattner31a9d182003-02-26 22:11:00 +0000118 if (AA.alias(Ptr, Size, I->first, I->second.getSize()))
Chris Lattner9971ac42003-02-24 20:37:56 +0000119 return true;
120
121 // Check the call sites list and invoke list...
122 if (!CallSites.empty())
123 // FIXME: this is pessimistic!
Chris Lattner009cc3d2002-09-26 21:49:07 +0000124 return true;
Chris Lattner9971ac42003-02-24 20:37:56 +0000125
Chris Lattner009cc3d2002-09-26 21:49:07 +0000126 return false;
127}
128
Chris Lattner9971ac42003-02-24 20:37:56 +0000129bool AliasSet::aliasesCallSite(CallSite CS, AliasAnalysis &AA) const {
130 // FIXME: Too conservative!
131 return true;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000132}
133
134
Chris Lattner009cc3d2002-09-26 21:49:07 +0000135/// 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.
138///
Chris Lattner31a9d182003-02-26 22:11:00 +0000139AliasSet *AliasSetTracker::findAliasSetForPointer(const Value *Ptr,
140 unsigned Size) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000141 AliasSet *FoundSet = 0;
Chris Lattner9971ac42003-02-24 20:37:56 +0000142 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner31a9d182003-02-26 22:11:00 +0000143 if (!I->Forward && I->aliasesPointer(Ptr, Size, AA)) {
Chris Lattner009cc3d2002-09-26 21:49:07 +0000144 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
Chris Lattner9971ac42003-02-24 20:37:56 +0000145 FoundSet = I; // Remember it.
Chris Lattner009cc3d2002-09-26 21:49:07 +0000146 } else { // Otherwise, we must merge the sets...
Chris Lattner9971ac42003-02-24 20:37:56 +0000147 FoundSet->mergeSetIn(*I); // Merge in contents...
Chris Lattner009cc3d2002-09-26 21:49:07 +0000148 }
149 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000150
151 return FoundSet;
152}
153
154AliasSet *AliasSetTracker::findAliasSetForCallSite(CallSite CS) {
155 AliasSet *FoundSet = 0;
156 for (iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000157 if (!I->Forward && I->aliasesCallSite(CS, AA)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000158 if (FoundSet == 0) { // If this is the first alias set ptr can go into...
159 FoundSet = I; // Remember it.
Chris Lattner2d0a4a42003-02-26 19:28:57 +0000160 } else if (!I->Forward) { // Otherwise, we must merge the sets...
Chris Lattner9971ac42003-02-24 20:37:56 +0000161 FoundSet->mergeSetIn(*I); // Merge in contents...
162 }
163 }
Chris Lattner009cc3d2002-09-26 21:49:07 +0000164
165 return FoundSet;
166}
167
168
Chris Lattner009cc3d2002-09-26 21:49:07 +0000169
Chris Lattner9971ac42003-02-24 20:37:56 +0000170
171/// getAliasSetForPointer - Return the alias set that the specified pointer
172/// lives in...
Chris Lattner31a9d182003-02-26 22:11:00 +0000173AliasSet &AliasSetTracker::getAliasSetForPointer(Value *Pointer, unsigned Size){
Chris Lattner9971ac42003-02-24 20:37:56 +0000174 AliasSet::HashNodePair &Entry = getEntryFor(Pointer);
175
176 // Check to see if the pointer is already known...
Chris Lattner31a9d182003-02-26 22:11:00 +0000177 if (Entry.second.hasAliasSet() && Size <= Entry.second.getSize()) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000178 // Return the set!
179 return *Entry.second.getAliasSet(*this)->getForwardedTarget(*this);
Chris Lattner31a9d182003-02-26 22:11:00 +0000180 } else if (AliasSet *AS = findAliasSetForPointer(Pointer, Size)) {
Chris Lattner9971ac42003-02-24 20:37:56 +0000181 // Add it to the alias set it aliases...
Chris Lattner31a9d182003-02-26 22:11:00 +0000182 AS->addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000183 return *AS;
Chris Lattner009cc3d2002-09-26 21:49:07 +0000184 } else {
Chris Lattner9971ac42003-02-24 20:37:56 +0000185 // Otherwise create a new alias set to hold the loaded pointer...
Chris Lattner009cc3d2002-09-26 21:49:07 +0000186 AliasSets.push_back(AliasSet());
Chris Lattner31a9d182003-02-26 22:11:00 +0000187 AliasSets.back().addPointer(*this, Entry, Size);
Chris Lattner9971ac42003-02-24 20:37:56 +0000188 return AliasSets.back();
Chris Lattner009cc3d2002-09-26 21:49:07 +0000189 }
190}
191
Chris Lattner9971ac42003-02-24 20:37:56 +0000192void AliasSetTracker::add(LoadInst *LI) {
Chris Lattner31a9d182003-02-26 22:11:00 +0000193 addPointer(LI->getOperand(0),
194 AA.getTargetData().getTypeSize(LI->getType()), AliasSet::Refs);
Chris Lattner9971ac42003-02-24 20:37:56 +0000195}
196
Chris Lattner009cc3d2002-09-26 21:49:07 +0000197void AliasSetTracker::add(StoreInst *SI) {
Chris Lattner31a9d182003-02-26 22:11:00 +0000198 addPointer(SI->getOperand(1),
199 AA.getTargetData().getTypeSize(SI->getOperand(0)->getType()),
200 AliasSet::Mods);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000201}
202
Chris Lattner9971ac42003-02-24 20:37:56 +0000203void AliasSetTracker::add(CallSite CS) {
204 AliasSet *AS = findAliasSetForCallSite(CS);
205 if (!AS) {
206 AliasSets.push_back(AliasSet());
207 AS = &AliasSets.back();
208 }
209 AS->addCallSite(CS);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000210}
211
Chris Lattner9971ac42003-02-24 20:37:56 +0000212void AliasSetTracker::add(Instruction *I) {
213 // Dispatch to one of the other add methods...
214 if (LoadInst *LI = dyn_cast<LoadInst>(I))
215 add(LI);
216 else if (StoreInst *SI = dyn_cast<StoreInst>(I))
217 add(SI);
218 else if (CallInst *CI = dyn_cast<CallInst>(I))
219 add(CI);
220 else if (InvokeInst *II = dyn_cast<InvokeInst>(I))
221 add(II);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000222}
223
Chris Lattner319d05b2003-03-03 23:28:05 +0000224void AliasSetTracker::add(BasicBlock &BB) {
225 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
226 add(I);
227}
228
229void AliasSetTracker::add(const AliasSetTracker &AST) {
230 assert(&AA == &AST.AA &&
231 "Merging AliasSetTracker objects with different Alias Analyses!");
232
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);
239
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]);
243
244 // Loop over all of the pointers in this alias set...
245 AliasSet::iterator I = AS.begin(), E = AS.end();
246 for (; I != E; ++I)
247 addPointer(I->first, I->second.getSize(),
248 (AliasSet::AccessType)AS.AccessTy);
249 }
250}
251
Chris Lattner9971ac42003-02-24 20:37:56 +0000252//===----------------------------------------------------------------------===//
253// AliasSet/AliasSetTracker Printing Support
254//===----------------------------------------------------------------------===//
255
256void AliasSet::print(std::ostream &OS) const {
257 OS << " AliasSet[" << (void*)this << "," << RefCount << "] ";
258 OS << (AliasTy == MustAlias ? "must" : "may ") << " alias, ";
259 switch (AccessTy) {
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!");
Chris Lattner009cc3d2002-09-26 21:49:07 +0000265 }
Chris Lattner9971ac42003-02-24 20:37:56 +0000266 if (Forward)
267 OS << " forwarding to " << (void*)Forward;
268
269
270 if (begin() != end()) {
271 OS << "Pointers: ";
272 for (iterator I = begin(), E = end(); I != E; ++I) {
273 if (I != begin()) OS << ", ";
Chris Lattner31a9d182003-02-26 22:11:00 +0000274 WriteAsOperand(OS << "(", I->first);
275 OS << ", " << I->second.getSize() << ")";
Chris Lattner9971ac42003-02-24 20:37:56 +0000276 }
277 }
278 if (!CallSites.empty()) {
279 OS << "\n " << CallSites.size() << " Call Sites: ";
280 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
281 if (i) OS << ", ";
282 WriteAsOperand(OS, CallSites[i].getCalledValue());
283 }
284 }
285 OS << "\n";
286}
287
288void 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)
292 I->print(OS);
293 OS << "\n";
294}
295
296void AliasSet::dump() const { print (std::cerr); }
297void AliasSetTracker::dump() const { print(std::cerr); }
298
Chris Lattner9971ac42003-02-24 20:37:56 +0000299//===----------------------------------------------------------------------===//
300// AliasSetPrinter Pass
301//===----------------------------------------------------------------------===//
302
303namespace {
304 class AliasSetPrinter : public FunctionPass {
305 AliasSetTracker *Tracker;
306 public:
307 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
308 AU.setPreservesAll();
309 AU.addRequired<AliasAnalysis>();
310 }
311
312 virtual bool runOnFunction(Function &F) {
313 Tracker = new AliasSetTracker(getAnalysis<AliasAnalysis>());
314
315 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
316 Tracker->add(*I);
317 return false;
318 }
319
320 /// print - Convert to human readable form
321 virtual void print(std::ostream &OS) const {
322 Tracker->print(OS);
323 }
324
325 virtual void releaseMemory() {
326 delete Tracker;
327 }
328 };
329 RegisterPass<AliasSetPrinter> X("print-alias-sets", "Alias Set Printer",
330 PassInfo::Analysis | PassInfo::Optimization);
Chris Lattner009cc3d2002-09-26 21:49:07 +0000331}
Brian Gaeked0fde302003-11-11 22:41:34 +0000332
333} // End llvm namespace