Delete getIntrinsicModRefBehavior. Clients can just use the normal
[oota-llvm.git] / lib / Analysis / AliasAnalysis.cpp
1 //===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the generic AliasAnalysis interface which is used as the
11 // common interface used by all clients and implementations of alias analysis.
12 //
13 // This file also implements the default version of the AliasAnalysis interface
14 // that is to be used when no other implementation is specified.  This does some
15 // simple tests that detect obvious cases: two different global pointers cannot
16 // alias, a global cannot alias a malloc, two different mallocs cannot alias,
17 // etc.
18 //
19 // This alias analysis implementation really isn't very good for anything, but
20 // it is very fast, and makes a nice clean default implementation.  Because it
21 // handles lots of little corner cases, other, more complex, alias analysis
22 // implementations may choose to rely on this pass to resolve these simple and
23 // easy cases.
24 //
25 //===----------------------------------------------------------------------===//
26
27 #include "llvm/Analysis/AliasAnalysis.h"
28 #include "llvm/Pass.h"
29 #include "llvm/BasicBlock.h"
30 #include "llvm/Function.h"
31 #include "llvm/IntrinsicInst.h"
32 #include "llvm/Instructions.h"
33 #include "llvm/LLVMContext.h"
34 #include "llvm/Type.h"
35 #include "llvm/Target/TargetData.h"
36 using namespace llvm;
37
38 // Register the AliasAnalysis interface, providing a nice name to refer to.
39 INITIALIZE_ANALYSIS_GROUP(AliasAnalysis, "Alias Analysis", NoAA)
40 char AliasAnalysis::ID = 0;
41
42 //===----------------------------------------------------------------------===//
43 // Default chaining methods
44 //===----------------------------------------------------------------------===//
45
46 AliasAnalysis::AliasResult
47 AliasAnalysis::alias(const Location &LocA, const Location &LocB) {
48   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
49   return AA->alias(LocA, LocB);
50 }
51
52 bool AliasAnalysis::pointsToConstantMemory(const Location &Loc) {
53   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
54   return AA->pointsToConstantMemory(Loc);
55 }
56
57 void AliasAnalysis::deleteValue(Value *V) {
58   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
59   AA->deleteValue(V);
60 }
61
62 void AliasAnalysis::copyValue(Value *From, Value *To) {
63   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
64   AA->copyValue(From, To);
65 }
66
67 AliasAnalysis::ModRefResult
68 AliasAnalysis::getModRefInfo(ImmutableCallSite CS,
69                              const Location &Loc) {
70   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
71
72   ModRefBehavior MRB = getModRefBehavior(CS);
73   if (MRB == DoesNotAccessMemory)
74     return NoModRef;
75
76   ModRefResult Mask = ModRef;
77   if (MRB == OnlyReadsMemory)
78     Mask = Ref;
79   else if (MRB == AliasAnalysis::AccessesArguments) {
80     bool doesAlias = false;
81     for (ImmutableCallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
82          AI != AE; ++AI)
83       if (!isNoAlias(Location(*AI), Loc)) {
84         doesAlias = true;
85         break;
86       }
87
88     if (!doesAlias)
89       return NoModRef;
90   }
91
92   // If Loc is a constant memory location, the call definitely could not
93   // modify the memory location.
94   if ((Mask & Mod) && pointsToConstantMemory(Loc))
95     Mask = ModRefResult(Mask & ~Mod);
96
97   // If this is the end of the chain, don't forward.
98   if (!AA) return Mask;
99
100   // Otherwise, fall back to the next AA in the chain. But we can merge
101   // in any mask we've managed to compute.
102   return ModRefResult(AA->getModRefInfo(CS, Loc) & Mask);
103 }
104
105 AliasAnalysis::ModRefResult
106 AliasAnalysis::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) {
107   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
108
109   // If CS1 or CS2 are readnone, they don't interact.
110   ModRefBehavior CS1B = getModRefBehavior(CS1);
111   if (CS1B == DoesNotAccessMemory) return NoModRef;
112
113   ModRefBehavior CS2B = getModRefBehavior(CS2);
114   if (CS2B == DoesNotAccessMemory) return NoModRef;
115
116   // If they both only read from memory, there is no dependence.
117   if (CS1B == OnlyReadsMemory && CS2B == OnlyReadsMemory)
118     return NoModRef;
119
120   AliasAnalysis::ModRefResult Mask = ModRef;
121
122   // If CS1 only reads memory, the only dependence on CS2 can be
123   // from CS1 reading memory written by CS2.
124   if (CS1B == OnlyReadsMemory)
125     Mask = ModRefResult(Mask & Ref);
126
127   // If CS2 only access memory through arguments, accumulate the mod/ref
128   // information from CS1's references to the memory referenced by
129   // CS2's arguments.
130   if (CS2B == AccessesArguments) {
131     AliasAnalysis::ModRefResult R = NoModRef;
132     for (ImmutableCallSite::arg_iterator
133          I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) {
134       R = ModRefResult((R | getModRefInfo(CS1, *I, UnknownSize)) & Mask);
135       if (R == Mask)
136         break;
137     }
138     return R;
139   }
140
141   // If CS1 only accesses memory through arguments, check if CS2 references
142   // any of the memory referenced by CS1's arguments. If not, return NoModRef.
143   if (CS1B == AccessesArguments) {
144     AliasAnalysis::ModRefResult R = NoModRef;
145     for (ImmutableCallSite::arg_iterator
146          I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I)
147       if (getModRefInfo(CS2, *I, UnknownSize) != NoModRef) {
148         R = Mask;
149         break;
150       }
151     if (R == NoModRef)
152       return R;
153   }
154
155   // If this is the end of the chain, don't forward.
156   if (!AA) return Mask;
157
158   // Otherwise, fall back to the next AA in the chain. But we can merge
159   // in any mask we've managed to compute.
160   return ModRefResult(AA->getModRefInfo(CS1, CS2) & Mask);
161 }
162
163 AliasAnalysis::ModRefBehavior
164 AliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
165   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
166
167   ModRefBehavior Min = UnknownModRefBehavior;
168
169   // Call back into the alias analysis with the other form of getModRefBehavior
170   // to see if it can give a better response.
171   if (const Function *F = CS.getCalledFunction())
172     Min = getModRefBehavior(F);
173
174   // If this is the end of the chain, don't forward.
175   if (!AA) return Min;
176
177   // Otherwise, fall back to the next AA in the chain. But we can merge
178   // in any result we've managed to compute.
179   return std::min(AA->getModRefBehavior(CS), Min);
180 }
181
182 AliasAnalysis::ModRefBehavior
183 AliasAnalysis::getModRefBehavior(const Function *F) {
184   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
185   return AA->getModRefBehavior(F);
186 }
187
188 //===----------------------------------------------------------------------===//
189 // AliasAnalysis non-virtual helper method implementation
190 //===----------------------------------------------------------------------===//
191
192 AliasAnalysis::ModRefResult
193 AliasAnalysis::getModRefInfo(const LoadInst *L, const Location &Loc) {
194   // Be conservative in the face of volatile.
195   if (L->isVolatile())
196     return ModRef;
197
198   // If the load address doesn't alias the given address, it doesn't read
199   // or write the specified memory.
200   if (!alias(Location(L->getOperand(0),
201                       getTypeStoreSize(L->getType()),
202                       L->getMetadata(LLVMContext::MD_tbaa)),
203              Loc))
204     return NoModRef;
205
206   // Otherwise, a load just reads.
207   return Ref;
208 }
209
210 AliasAnalysis::ModRefResult
211 AliasAnalysis::getModRefInfo(const StoreInst *S, const Location &Loc) {
212   // Be conservative in the face of volatile.
213   if (S->isVolatile())
214     return ModRef;
215
216   // If the store address cannot alias the pointer in question, then the
217   // specified memory cannot be modified by the store.
218   if (!alias(Location(S->getOperand(1),
219                       getTypeStoreSize(S->getOperand(0)->getType()),
220                       S->getMetadata(LLVMContext::MD_tbaa)),
221              Loc))
222     return NoModRef;
223
224   // If the pointer is a pointer to constant memory, then it could not have been
225   // modified by this store.
226   if (pointsToConstantMemory(Loc))
227     return NoModRef;
228
229   // Otherwise, a store just writes.
230   return Mod;
231 }
232
233 AliasAnalysis::ModRefResult
234 AliasAnalysis::getModRefInfo(const VAArgInst *V, const Location &Loc) {
235   // If the va_arg address cannot alias the pointer in question, then the
236   // specified memory cannot be accessed by the va_arg.
237   if (!alias(Location(V->getOperand(0),
238                       UnknownSize,
239                       V->getMetadata(LLVMContext::MD_tbaa)),
240              Loc))
241     return NoModRef;
242
243   // If the pointer is a pointer to constant memory, then it could not have been
244   // modified by this va_arg.
245   if (pointsToConstantMemory(Loc))
246     return NoModRef;
247
248   // Otherwise, a va_arg reads and writes.
249   return ModRef;
250 }
251
252 // AliasAnalysis destructor: DO NOT move this to the header file for
253 // AliasAnalysis or else clients of the AliasAnalysis class may not depend on
254 // the AliasAnalysis.o file in the current .a file, causing alias analysis
255 // support to not be included in the tool correctly!
256 //
257 AliasAnalysis::~AliasAnalysis() {}
258
259 /// InitializeAliasAnalysis - Subclasses must call this method to initialize the
260 /// AliasAnalysis interface before any other methods are called.
261 ///
262 void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
263   TD = P->getAnalysisIfAvailable<TargetData>();
264   AA = &P->getAnalysis<AliasAnalysis>();
265 }
266
267 // getAnalysisUsage - All alias analysis implementations should invoke this
268 // directly (using AliasAnalysis::getAnalysisUsage(AU)).
269 void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
270   AU.addRequired<AliasAnalysis>();         // All AA's chain
271 }
272
273 /// getTypeStoreSize - Return the TargetData store size for the given type,
274 /// if known, or a conservative value otherwise.
275 ///
276 uint64_t AliasAnalysis::getTypeStoreSize(const Type *Ty) {
277   return TD ? TD->getTypeStoreSize(Ty) : UnknownSize;
278 }
279
280 /// canBasicBlockModify - Return true if it is possible for execution of the
281 /// specified basic block to modify the value pointed to by Ptr.
282 ///
283 bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
284                                         const Location &Loc) {
285   return canInstructionRangeModify(BB.front(), BB.back(), Loc);
286 }
287
288 /// canInstructionRangeModify - Return true if it is possible for the execution
289 /// of the specified instructions to modify the value pointed to by Ptr.  The
290 /// instructions to consider are all of the instructions in the range of [I1,I2]
291 /// INCLUSIVE.  I1 and I2 must be in the same basic block.
292 ///
293 bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
294                                               const Instruction &I2,
295                                               const Location &Loc) {
296   assert(I1.getParent() == I2.getParent() &&
297          "Instructions not in same basic block!");
298   BasicBlock::const_iterator I = &I1;
299   BasicBlock::const_iterator E = &I2;
300   ++E;  // Convert from inclusive to exclusive range.
301
302   for (; I != E; ++I) // Check every instruction in range
303     if (getModRefInfo(I, Loc) & Mod)
304       return true;
305   return false;
306 }
307
308 /// isNoAliasCall - Return true if this pointer is returned by a noalias
309 /// function.
310 bool llvm::isNoAliasCall(const Value *V) {
311   if (isa<CallInst>(V) || isa<InvokeInst>(V))
312     return ImmutableCallSite(cast<Instruction>(V))
313       .paramHasAttr(0, Attribute::NoAlias);
314   return false;
315 }
316
317 /// isIdentifiedObject - Return true if this pointer refers to a distinct and
318 /// identifiable object.  This returns true for:
319 ///    Global Variables and Functions (but not Global Aliases)
320 ///    Allocas and Mallocs
321 ///    ByVal and NoAlias Arguments
322 ///    NoAlias returns
323 ///
324 bool llvm::isIdentifiedObject(const Value *V) {
325   if (isa<AllocaInst>(V))
326     return true;
327   if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
328     return true;
329   if (isNoAliasCall(V))
330     return true;
331   if (const Argument *A = dyn_cast<Argument>(V))
332     return A->hasNoAliasAttr() || A->hasByValAttr();
333   return false;
334 }