use the new isNoAlias method to simplify some code, only do an escaping check if
[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/Type.h"
34 #include "llvm/Target/TargetData.h"
35 using namespace llvm;
36
37 // Register the AliasAnalysis interface, providing a nice name to refer to.
38 static RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis");
39 char AliasAnalysis::ID = 0;
40
41 //===----------------------------------------------------------------------===//
42 // Default chaining methods
43 //===----------------------------------------------------------------------===//
44
45 AliasAnalysis::AliasResult
46 AliasAnalysis::alias(const Value *V1, unsigned V1Size,
47                      const Value *V2, unsigned V2Size) {
48   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
49   return AA->alias(V1, V1Size, V2, V2Size);
50 }
51
52 bool AliasAnalysis::pointsToConstantMemory(const Value *P) {
53   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
54   return AA->pointsToConstantMemory(P);
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(CallSite CS1, CallSite CS2) {
69   // FIXME: we can do better.
70   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
71   return AA->getModRefInfo(CS1, CS2);
72 }
73
74
75 //===----------------------------------------------------------------------===//
76 // AliasAnalysis non-virtual helper method implementation
77 //===----------------------------------------------------------------------===//
78
79 AliasAnalysis::ModRefResult
80 AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) {
81   return alias(L->getOperand(0), getTypeStoreSize(L->getType()),
82                P, Size) ? Ref : NoModRef;
83 }
84
85 AliasAnalysis::ModRefResult
86 AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) {
87   // If the stored address cannot alias the pointer in question, then the
88   // pointer cannot be modified by the store.
89   if (!alias(S->getOperand(1),
90              getTypeStoreSize(S->getOperand(0)->getType()), P, Size))
91     return NoModRef;
92
93   // If the pointer is a pointer to constant memory, then it could not have been
94   // modified by this store.
95   return pointsToConstantMemory(P) ? NoModRef : Mod;
96 }
97
98 AliasAnalysis::ModRefBehavior
99 AliasAnalysis::getModRefBehavior(CallSite CS,
100                                  std::vector<PointerAccessInfo> *Info) {
101   if (CS.doesNotAccessMemory())
102     // Can't do better than this.
103     return DoesNotAccessMemory;
104   ModRefBehavior MRB = getModRefBehavior(CS.getCalledFunction(), Info);
105   if (MRB != DoesNotAccessMemory && CS.onlyReadsMemory())
106     return OnlyReadsMemory;
107   return MRB;
108 }
109
110 AliasAnalysis::ModRefBehavior
111 AliasAnalysis::getModRefBehavior(Function *F,
112                                  std::vector<PointerAccessInfo> *Info) {
113   if (F) {
114     if (F->doesNotAccessMemory())
115       // Can't do better than this.
116       return DoesNotAccessMemory;
117     if (F->onlyReadsMemory())
118       return OnlyReadsMemory;
119     if (unsigned id = F->getIntrinsicID()) {
120 #define GET_INTRINSIC_MODREF_BEHAVIOR
121 #include "llvm/Intrinsics.gen"
122 #undef GET_INTRINSIC_MODREF_BEHAVIOR
123     }
124   }
125   return UnknownModRefBehavior;
126 }
127
128 AliasAnalysis::ModRefResult
129 AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
130   ModRefBehavior MRB = getModRefBehavior(CS);
131   if (MRB == DoesNotAccessMemory)
132     return NoModRef;
133   
134   ModRefResult Mask = ModRef;
135   if (MRB == OnlyReadsMemory)
136     Mask = Ref;
137   else if (MRB == AliasAnalysis::AccessesArguments) {
138     bool doesAlias = false;
139     for (CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
140          AI != AE; ++AI)
141       if (!isNoAlias(*AI, ~0U, P, Size)) {
142         doesAlias = true;
143         break;
144       }
145
146     if (!doesAlias)
147       return NoModRef;
148   }
149
150   if (!AA) return Mask;
151
152   // If P points to a constant memory location, the call definitely could not
153   // modify the memory location.
154   if ((Mask & Mod) && AA->pointsToConstantMemory(P))
155     Mask = ModRefResult(Mask & ~Mod);
156
157   return ModRefResult(Mask & AA->getModRefInfo(CS, P, Size));
158 }
159
160 // AliasAnalysis destructor: DO NOT move this to the header file for
161 // AliasAnalysis or else clients of the AliasAnalysis class may not depend on
162 // the AliasAnalysis.o file in the current .a file, causing alias analysis
163 // support to not be included in the tool correctly!
164 //
165 AliasAnalysis::~AliasAnalysis() {}
166
167 /// InitializeAliasAnalysis - Subclasses must call this method to initialize the
168 /// AliasAnalysis interface before any other methods are called.
169 ///
170 void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
171   TD = P->getAnalysisIfAvailable<TargetData>();
172   AA = &P->getAnalysis<AliasAnalysis>();
173 }
174
175 // getAnalysisUsage - All alias analysis implementations should invoke this
176 // directly (using AliasAnalysis::getAnalysisUsage(AU)).
177 void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
178   AU.addRequired<AliasAnalysis>();         // All AA's chain
179 }
180
181 /// getTypeStoreSize - Return the TargetData store size for the given type,
182 /// if known, or a conservative value otherwise.
183 ///
184 unsigned AliasAnalysis::getTypeStoreSize(const Type *Ty) {
185   return TD ? TD->getTypeStoreSize(Ty) : ~0u;
186 }
187
188 /// canBasicBlockModify - Return true if it is possible for execution of the
189 /// specified basic block to modify the value pointed to by Ptr.
190 ///
191 bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
192                                         const Value *Ptr, unsigned Size) {
193   return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size);
194 }
195
196 /// canInstructionRangeModify - Return true if it is possible for the execution
197 /// of the specified instructions to modify the value pointed to by Ptr.  The
198 /// instructions to consider are all of the instructions in the range of [I1,I2]
199 /// INCLUSIVE.  I1 and I2 must be in the same basic block.
200 ///
201 bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
202                                               const Instruction &I2,
203                                               const Value *Ptr, unsigned Size) {
204   assert(I1.getParent() == I2.getParent() &&
205          "Instructions not in same basic block!");
206   BasicBlock::iterator I = const_cast<Instruction*>(&I1);
207   BasicBlock::iterator E = const_cast<Instruction*>(&I2);
208   ++E;  // Convert from inclusive to exclusive range.
209
210   for (; I != E; ++I) // Check every instruction in range
211     if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod)
212       return true;
213   return false;
214 }
215
216 /// isNoAliasCall - Return true if this pointer is returned by a noalias
217 /// function.
218 bool llvm::isNoAliasCall(const Value *V) {
219   if (isa<CallInst>(V) || isa<InvokeInst>(V))
220     return CallSite(const_cast<Instruction*>(cast<Instruction>(V)))
221       .paramHasAttr(0, Attribute::NoAlias);
222   return false;
223 }
224
225 /// isIdentifiedObject - Return true if this pointer refers to a distinct and
226 /// identifiable object.  This returns true for:
227 ///    Global Variables and Functions (but not Global Aliases)
228 ///    Allocas and Mallocs
229 ///    ByVal and NoAlias Arguments
230 ///    NoAlias returns
231 ///
232 bool llvm::isIdentifiedObject(const Value *V) {
233   if (isa<AllocaInst>(V) || isNoAliasCall(V))
234     return true;
235   if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
236     return true;
237   if (const Argument *A = dyn_cast<Argument>(V))
238     return A->hasNoAliasAttr() || A->hasByValAttr();
239   return false;
240 }
241
242 // Because of the way .a files work, we must force the BasicAA implementation to
243 // be pulled in if the AliasAnalysis classes are pulled in.  Otherwise we run
244 // the risk of AliasAnalysis being used, but the default implementation not
245 // being linked into the tool that uses it.
246 DEFINING_FILE_FOR(AliasAnalysis)