Finish making AliasAnalysis aware of the fact that most atomic intrinsics only derefe...
[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 void AliasAnalysis::getMustAliases(Value *P, std::vector<Value*> &RetVals) {
53   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
54   return AA->getMustAliases(P, RetVals);
55 }
56
57 bool AliasAnalysis::pointsToConstantMemory(const Value *P) {
58   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
59   return AA->pointsToConstantMemory(P);
60 }
61
62 AliasAnalysis::ModRefBehavior
63 AliasAnalysis::getModRefBehavior(Function *F, CallSite CS,
64                                  std::vector<PointerAccessInfo> *Info) {
65   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
66   return AA->getModRefBehavior(F, CS, Info);
67 }
68
69 bool AliasAnalysis::hasNoModRefInfoForCalls() const {
70   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
71   return AA->hasNoModRefInfoForCalls();
72 }
73
74 void AliasAnalysis::deleteValue(Value *V) {
75   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
76   AA->deleteValue(V);
77 }
78
79 void AliasAnalysis::copyValue(Value *From, Value *To) {
80   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
81   AA->copyValue(From, To);
82 }
83
84 AliasAnalysis::ModRefResult
85 AliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) {
86   // FIXME: we can do better.
87   assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
88   return AA->getModRefInfo(CS1, CS2);
89 }
90
91
92 //===----------------------------------------------------------------------===//
93 // AliasAnalysis non-virtual helper method implementation
94 //===----------------------------------------------------------------------===//
95
96 AliasAnalysis::ModRefResult
97 AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) {
98   return alias(L->getOperand(0), TD->getTypeStoreSize(L->getType()),
99                P, Size) ? Ref : NoModRef;
100 }
101
102 AliasAnalysis::ModRefResult
103 AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) {
104   // If the stored address cannot alias the pointer in question, then the
105   // pointer cannot be modified by the store.
106   if (!alias(S->getOperand(1),
107              TD->getTypeStoreSize(S->getOperand(0)->getType()), P, Size))
108     return NoModRef;
109
110   // If the pointer is a pointer to constant memory, then it could not have been
111   // modified by this store.
112   return pointsToConstantMemory(P) ? NoModRef : Mod;
113 }
114
115 AliasAnalysis::ModRefBehavior
116 AliasAnalysis::getModRefBehavior(CallSite CS,
117                                  std::vector<PointerAccessInfo> *Info) {
118   if (IntrinsicInst* II = dyn_cast<IntrinsicInst>(CS.getInstruction())) {
119     switch (II->getIntrinsicID()) {
120       case Intrinsic::atomic_cmp_swap:
121       case Intrinsic::atomic_load_add:
122       case Intrinsic::atomic_load_and:
123       case Intrinsic::atomic_load_max:
124       case Intrinsic::atomic_load_min:
125       case Intrinsic::atomic_load_nand:
126       case Intrinsic::atomic_load_or:
127       case Intrinsic::atomic_load_sub:
128       case Intrinsic::atomic_load_umax:
129       case Intrinsic::atomic_load_umin:
130       case Intrinsic::atomic_load_xor:
131       case Intrinsic::atomic_swap:
132         // CAS and related intrinsics only access their arguments.
133         return AliasAnalysis::AccessesArguments;
134       default:
135         break;
136     }
137   }
138   
139   if (CS.doesNotAccessMemory())
140     // Can't do better than this.
141     return DoesNotAccessMemory;
142   ModRefBehavior MRB = UnknownModRefBehavior;
143   if (Function *F = CS.getCalledFunction())
144     MRB = getModRefBehavior(F, CS, Info);
145   if (MRB != DoesNotAccessMemory && CS.onlyReadsMemory())
146     return OnlyReadsMemory;
147   return MRB;
148 }
149
150 AliasAnalysis::ModRefBehavior
151 AliasAnalysis::getModRefBehavior(Function *F,
152                                  std::vector<PointerAccessInfo> *Info) {
153   if (F->isIntrinsic()) {
154     switch (F->getIntrinsicID()) {
155       case Intrinsic::atomic_cmp_swap:
156       case Intrinsic::atomic_load_add:
157       case Intrinsic::atomic_load_and:
158       case Intrinsic::atomic_load_max:
159       case Intrinsic::atomic_load_min:
160       case Intrinsic::atomic_load_nand:
161       case Intrinsic::atomic_load_or:
162       case Intrinsic::atomic_load_sub:
163       case Intrinsic::atomic_load_umax:
164       case Intrinsic::atomic_load_umin:
165       case Intrinsic::atomic_load_xor:
166       case Intrinsic::atomic_swap:
167         // CAS and related intrinsics only access their arguments.
168         return AliasAnalysis::AccessesArguments;
169       default:
170         break;
171     }
172   }
173
174   if (F->doesNotAccessMemory())
175     // Can't do better than this.
176     return DoesNotAccessMemory;
177   ModRefBehavior MRB = getModRefBehavior(F, CallSite(), Info);
178   if (MRB != DoesNotAccessMemory && F->onlyReadsMemory())
179     return OnlyReadsMemory;
180   return MRB;
181 }
182
183 AliasAnalysis::ModRefResult
184 AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
185   ModRefResult Mask = ModRef;
186   ModRefBehavior MRB = getModRefBehavior(CS);
187   if (MRB == OnlyReadsMemory)
188     Mask = Ref;
189   else if (MRB == DoesNotAccessMemory)
190     return NoModRef;
191
192   if (!AA) return Mask;
193
194   // If P points to a constant memory location, the call definitely could not
195   // modify the memory location.
196   if ((Mask & Mod) && AA->pointsToConstantMemory(P))
197     Mask = ModRefResult(Mask & ~Mod);
198
199   return ModRefResult(Mask & AA->getModRefInfo(CS, P, Size));
200 }
201
202 // AliasAnalysis destructor: DO NOT move this to the header file for
203 // AliasAnalysis or else clients of the AliasAnalysis class may not depend on
204 // the AliasAnalysis.o file in the current .a file, causing alias analysis
205 // support to not be included in the tool correctly!
206 //
207 AliasAnalysis::~AliasAnalysis() {}
208
209 /// InitializeAliasAnalysis - Subclasses must call this method to initialize the
210 /// AliasAnalysis interface before any other methods are called.
211 ///
212 void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
213   TD = &P->getAnalysis<TargetData>();
214   AA = &P->getAnalysis<AliasAnalysis>();
215 }
216
217 // getAnalysisUsage - All alias analysis implementations should invoke this
218 // directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that
219 // TargetData is required by the pass.
220 void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
221   AU.addRequired<TargetData>();            // All AA's need TargetData.
222   AU.addRequired<AliasAnalysis>();         // All AA's chain
223 }
224
225 /// canBasicBlockModify - Return true if it is possible for execution of the
226 /// specified basic block to modify the value pointed to by Ptr.
227 ///
228 bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
229                                         const Value *Ptr, unsigned Size) {
230   return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size);
231 }
232
233 /// canInstructionRangeModify - Return true if it is possible for the execution
234 /// of the specified instructions to modify the value pointed to by Ptr.  The
235 /// instructions to consider are all of the instructions in the range of [I1,I2]
236 /// INCLUSIVE.  I1 and I2 must be in the same basic block.
237 ///
238 bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
239                                               const Instruction &I2,
240                                               const Value *Ptr, unsigned Size) {
241   assert(I1.getParent() == I2.getParent() &&
242          "Instructions not in same basic block!");
243   BasicBlock::iterator I = const_cast<Instruction*>(&I1);
244   BasicBlock::iterator E = const_cast<Instruction*>(&I2);
245   ++E;  // Convert from inclusive to exclusive range.
246
247   for (; I != E; ++I) // Check every instruction in range
248     if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod)
249       return true;
250   return false;
251 }
252
253 /// isNoAliasCall - Return true if this pointer is returned by a noalias
254 /// function.
255 bool llvm::isNoAliasCall(const Value *V) {
256   if (isa<CallInst>(V) || isa<InvokeInst>(V))
257     return CallSite(const_cast<Instruction*>(cast<Instruction>(V)))
258       .paramHasAttr(0, Attribute::NoAlias);
259   return false;
260 }
261
262 /// isIdentifiedObject - Return true if this pointer refers to a distinct and
263 /// identifiable object.  This returns true for:
264 ///    Global Variables and Functions
265 ///    Allocas and Mallocs
266 ///    ByVal and NoAlias Arguments
267 ///    NoAlias returns
268 ///
269 bool llvm::isIdentifiedObject(const Value *V) {
270   if (isa<GlobalValue>(V) || isa<AllocationInst>(V) || isNoAliasCall(V))
271     return true;
272   if (const Argument *A = dyn_cast<Argument>(V))
273     return A->hasNoAliasAttr() || A->hasByValAttr();
274   return false;
275 }
276
277 // Because of the way .a files work, we must force the BasicAA implementation to
278 // be pulled in if the AliasAnalysis classes are pulled in.  Otherwise we run
279 // the risk of AliasAnalysis being used, but the default implementation not
280 // being linked into the tool that uses it.
281 DEFINING_FILE_FOR(AliasAnalysis)