Teach AliasAnalysis that a bunch of the atomic intrinsics only dereference their...
[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->doesNotAccessMemory())
154     // Can't do better than this.
155     return DoesNotAccessMemory;
156   ModRefBehavior MRB = getModRefBehavior(F, CallSite(), Info);
157   if (MRB != DoesNotAccessMemory && F->onlyReadsMemory())
158     return OnlyReadsMemory;
159   return MRB;
160 }
161
162 AliasAnalysis::ModRefResult
163 AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
164   ModRefResult Mask = ModRef;
165   ModRefBehavior MRB = getModRefBehavior(CS);
166   if (MRB == OnlyReadsMemory)
167     Mask = Ref;
168   else if (MRB == DoesNotAccessMemory)
169     return NoModRef;
170
171   if (!AA) return Mask;
172
173   // If P points to a constant memory location, the call definitely could not
174   // modify the memory location.
175   if ((Mask & Mod) && AA->pointsToConstantMemory(P))
176     Mask = ModRefResult(Mask & ~Mod);
177
178   return ModRefResult(Mask & AA->getModRefInfo(CS, P, Size));
179 }
180
181 // AliasAnalysis destructor: DO NOT move this to the header file for
182 // AliasAnalysis or else clients of the AliasAnalysis class may not depend on
183 // the AliasAnalysis.o file in the current .a file, causing alias analysis
184 // support to not be included in the tool correctly!
185 //
186 AliasAnalysis::~AliasAnalysis() {}
187
188 /// InitializeAliasAnalysis - Subclasses must call this method to initialize the
189 /// AliasAnalysis interface before any other methods are called.
190 ///
191 void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
192   TD = &P->getAnalysis<TargetData>();
193   AA = &P->getAnalysis<AliasAnalysis>();
194 }
195
196 // getAnalysisUsage - All alias analysis implementations should invoke this
197 // directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that
198 // TargetData is required by the pass.
199 void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
200   AU.addRequired<TargetData>();            // All AA's need TargetData.
201   AU.addRequired<AliasAnalysis>();         // All AA's chain
202 }
203
204 /// canBasicBlockModify - Return true if it is possible for execution of the
205 /// specified basic block to modify the value pointed to by Ptr.
206 ///
207 bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
208                                         const Value *Ptr, unsigned Size) {
209   return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size);
210 }
211
212 /// canInstructionRangeModify - Return true if it is possible for the execution
213 /// of the specified instructions to modify the value pointed to by Ptr.  The
214 /// instructions to consider are all of the instructions in the range of [I1,I2]
215 /// INCLUSIVE.  I1 and I2 must be in the same basic block.
216 ///
217 bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
218                                               const Instruction &I2,
219                                               const Value *Ptr, unsigned Size) {
220   assert(I1.getParent() == I2.getParent() &&
221          "Instructions not in same basic block!");
222   BasicBlock::iterator I = const_cast<Instruction*>(&I1);
223   BasicBlock::iterator E = const_cast<Instruction*>(&I2);
224   ++E;  // Convert from inclusive to exclusive range.
225
226   for (; I != E; ++I) // Check every instruction in range
227     if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod)
228       return true;
229   return false;
230 }
231
232 /// isNoAliasCall - Return true if this pointer is returned by a noalias
233 /// function.
234 bool llvm::isNoAliasCall(const Value *V) {
235   if (isa<CallInst>(V) || isa<InvokeInst>(V))
236     return CallSite(const_cast<Instruction*>(cast<Instruction>(V)))
237       .paramHasAttr(0, Attribute::NoAlias);
238   return false;
239 }
240
241 /// isIdentifiedObject - Return true if this pointer refers to a distinct and
242 /// identifiable object.  This returns true for:
243 ///    Global Variables and Functions
244 ///    Allocas and Mallocs
245 ///    ByVal and NoAlias Arguments
246 ///    NoAlias returns
247 ///
248 bool llvm::isIdentifiedObject(const Value *V) {
249   if (isa<GlobalValue>(V) || isa<AllocationInst>(V) || isNoAliasCall(V))
250     return true;
251   if (const Argument *A = dyn_cast<Argument>(V))
252     return A->hasNoAliasAttr() || A->hasByValAttr();
253   return false;
254 }
255
256 // Because of the way .a files work, we must force the BasicAA implementation to
257 // be pulled in if the AliasAnalysis classes are pulled in.  Otherwise we run
258 // the risk of AliasAnalysis being used, but the default implementation not
259 // being linked into the tool that uses it.
260 DEFINING_FILE_FOR(AliasAnalysis)