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