blob: be42f749b4d33994527f158b7661d1761fa601e1 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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"
Duncan Sands00b24b52007-12-01 07:51:45 +000030#include "llvm/Function.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include "llvm/Instructions.h"
32#include "llvm/Type.h"
33#include "llvm/Target/TargetData.h"
34using namespace llvm;
35
36// Register the AliasAnalysis interface, providing a nice name to refer to.
Dan Gohman089efff2008-05-13 00:00:25 +000037static RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038char AliasAnalysis::ID = 0;
39
40//===----------------------------------------------------------------------===//
41// Default chaining methods
42//===----------------------------------------------------------------------===//
43
44AliasAnalysis::AliasResult
45AliasAnalysis::alias(const Value *V1, unsigned V1Size,
46 const Value *V2, unsigned V2Size) {
47 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
48 return AA->alias(V1, V1Size, V2, V2Size);
49}
50
51void AliasAnalysis::getMustAliases(Value *P, std::vector<Value*> &RetVals) {
52 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
53 return AA->getMustAliases(P, RetVals);
54}
55
56bool AliasAnalysis::pointsToConstantMemory(const Value *P) {
57 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
58 return AA->pointsToConstantMemory(P);
59}
60
61AliasAnalysis::ModRefBehavior
62AliasAnalysis::getModRefBehavior(Function *F, CallSite CS,
63 std::vector<PointerAccessInfo> *Info) {
64 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
65 return AA->getModRefBehavior(F, CS, Info);
66}
67
68bool AliasAnalysis::hasNoModRefInfoForCalls() const {
69 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
70 return AA->hasNoModRefInfoForCalls();
71}
72
73void AliasAnalysis::deleteValue(Value *V) {
74 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
75 AA->deleteValue(V);
76}
77
78void AliasAnalysis::copyValue(Value *From, Value *To) {
79 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
80 AA->copyValue(From, To);
81}
82
83AliasAnalysis::ModRefResult
84AliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) {
85 // FIXME: we can do better.
86 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
87 return AA->getModRefInfo(CS1, CS2);
88}
89
90
91//===----------------------------------------------------------------------===//
92// AliasAnalysis non-virtual helper method implementation
93//===----------------------------------------------------------------------===//
94
95AliasAnalysis::ModRefResult
96AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) {
Duncan Sandsf99fdc62007-11-01 20:53:16 +000097 return alias(L->getOperand(0), TD->getTypeStoreSize(L->getType()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 P, Size) ? Ref : NoModRef;
99}
100
101AliasAnalysis::ModRefResult
102AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) {
103 // If the stored address cannot alias the pointer in question, then the
104 // pointer cannot be modified by the store.
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000105 if (!alias(S->getOperand(1),
106 TD->getTypeStoreSize(S->getOperand(0)->getType()), P, Size))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 return NoModRef;
108
109 // If the pointer is a pointer to constant memory, then it could not have been
110 // modified by this store.
111 return pointsToConstantMemory(P) ? NoModRef : Mod;
112}
113
Duncan Sands00b24b52007-12-01 07:51:45 +0000114AliasAnalysis::ModRefBehavior
115AliasAnalysis::getModRefBehavior(CallSite CS,
116 std::vector<PointerAccessInfo> *Info) {
Duncan Sands6c48de42007-12-12 16:01:40 +0000117 if (CS.doesNotAccessMemory())
Duncan Sands00b24b52007-12-01 07:51:45 +0000118 // Can't do better than this.
119 return DoesNotAccessMemory;
120 ModRefBehavior MRB = UnknownModRefBehavior;
121 if (Function *F = CS.getCalledFunction())
122 MRB = getModRefBehavior(F, CS, Info);
Duncan Sands6c48de42007-12-12 16:01:40 +0000123 if (MRB != DoesNotAccessMemory && CS.onlyReadsMemory())
Duncan Sands00b24b52007-12-01 07:51:45 +0000124 return OnlyReadsMemory;
125 return MRB;
126}
127
128AliasAnalysis::ModRefBehavior
129AliasAnalysis::getModRefBehavior(Function *F,
130 std::vector<PointerAccessInfo> *Info) {
Duncan Sands6c48de42007-12-12 16:01:40 +0000131 if (F->doesNotAccessMemory())
Duncan Sands00b24b52007-12-01 07:51:45 +0000132 // Can't do better than this.
133 return DoesNotAccessMemory;
134 ModRefBehavior MRB = getModRefBehavior(F, CallSite(), Info);
Duncan Sands6c48de42007-12-12 16:01:40 +0000135 if (MRB != DoesNotAccessMemory && F->onlyReadsMemory())
Duncan Sands00b24b52007-12-01 07:51:45 +0000136 return OnlyReadsMemory;
137 return MRB;
138}
139
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140AliasAnalysis::ModRefResult
141AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
142 ModRefResult Mask = ModRef;
Duncan Sands00b24b52007-12-01 07:51:45 +0000143 ModRefBehavior MRB = getModRefBehavior(CS);
144 if (MRB == OnlyReadsMemory)
145 Mask = Ref;
146 else if (MRB == DoesNotAccessMemory)
147 return NoModRef;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148
149 if (!AA) return Mask;
150
151 // If P points to a constant memory location, the call definitely could not
152 // modify the memory location.
153 if ((Mask & Mod) && AA->pointsToConstantMemory(P))
154 Mask = ModRefResult(Mask & ~Mod);
155
156 return ModRefResult(Mask & AA->getModRefInfo(CS, P, Size));
157}
158
159// AliasAnalysis destructor: DO NOT move this to the header file for
160// AliasAnalysis or else clients of the AliasAnalysis class may not depend on
161// the AliasAnalysis.o file in the current .a file, causing alias analysis
162// support to not be included in the tool correctly!
163//
164AliasAnalysis::~AliasAnalysis() {}
165
166/// setTargetData - Subclasses must call this method to initialize the
167/// AliasAnalysis interface before any other methods are called.
168///
169void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
170 TD = &P->getAnalysis<TargetData>();
171 AA = &P->getAnalysis<AliasAnalysis>();
172}
173
174// getAnalysisUsage - All alias analysis implementations should invoke this
175// directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that
176// TargetData is required by the pass.
177void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
178 AU.addRequired<TargetData>(); // All AA's need TargetData.
179 AU.addRequired<AliasAnalysis>(); // All AA's chain
180}
181
182/// canBasicBlockModify - Return true if it is possible for execution of the
183/// specified basic block to modify the value pointed to by Ptr.
184///
185bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
186 const Value *Ptr, unsigned Size) {
187 return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size);
188}
189
190/// canInstructionRangeModify - Return true if it is possible for the execution
191/// of the specified instructions to modify the value pointed to by Ptr. The
192/// instructions to consider are all of the instructions in the range of [I1,I2]
193/// INCLUSIVE. I1 and I2 must be in the same basic block.
194///
195bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
196 const Instruction &I2,
197 const Value *Ptr, unsigned Size) {
198 assert(I1.getParent() == I2.getParent() &&
199 "Instructions not in same basic block!");
200 BasicBlock::iterator I = const_cast<Instruction*>(&I1);
201 BasicBlock::iterator E = const_cast<Instruction*>(&I2);
202 ++E; // Convert from inclusive to exclusive range.
203
204 for (; I != E; ++I) // Check every instruction in range
205 if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod)
206 return true;
207 return false;
208}
209
210// Because of the way .a files work, we must force the BasicAA implementation to
211// be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run
212// the risk of AliasAnalysis being used, but the default implementation not
213// being linked into the tool that uses it.
214DEFINING_FILE_FOR(AliasAnalysis)