1 //===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
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,
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
25 //===----------------------------------------------------------------------===//
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"
37 // Register the AliasAnalysis interface, providing a nice name to refer to.
38 static RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis");
39 char AliasAnalysis::ID = 0;
41 //===----------------------------------------------------------------------===//
42 // Default chaining methods
43 //===----------------------------------------------------------------------===//
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);
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);
57 bool AliasAnalysis::pointsToConstantMemory(const Value *P) {
58 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
59 return AA->pointsToConstantMemory(P);
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);
69 bool AliasAnalysis::hasNoModRefInfoForCalls() const {
70 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
71 return AA->hasNoModRefInfoForCalls();
74 void AliasAnalysis::deleteValue(Value *V) {
75 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
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);
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);
92 //===----------------------------------------------------------------------===//
93 // AliasAnalysis non-virtual helper method implementation
94 //===----------------------------------------------------------------------===//
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;
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))
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;
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;
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;
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;
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)
168 else if (MRB == DoesNotAccessMemory)
171 if (!AA) return Mask;
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);
178 return ModRefResult(Mask & AA->getModRefInfo(CS, P, Size));
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!
186 AliasAnalysis::~AliasAnalysis() {}
188 /// InitializeAliasAnalysis - Subclasses must call this method to initialize the
189 /// AliasAnalysis interface before any other methods are called.
191 void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
192 TD = &P->getAnalysis<TargetData>();
193 AA = &P->getAnalysis<AliasAnalysis>();
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
204 /// canBasicBlockModify - Return true if it is possible for execution of the
205 /// specified basic block to modify the value pointed to by Ptr.
207 bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
208 const Value *Ptr, unsigned Size) {
209 return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size);
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.
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.
226 for (; I != E; ++I) // Check every instruction in range
227 if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod)
232 /// isNoAliasCall - Return true if this pointer is returned by a noalias
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);
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
248 bool llvm::isIdentifiedObject(const Value *V) {
249 if (isa<GlobalValue>(V) || isa<AllocationInst>(V) || isNoAliasCall(V))
251 if (const Argument *A = dyn_cast<Argument>(V))
252 return A->hasNoAliasAttr() || A->hasByValAttr();
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)