AliasAnalysis.cpp revision 1997473cf72957d0e70322e2fe6fe2ab141c58a6
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/Instructions.h"
31#include "llvm/Type.h"
32#include "llvm/Target/TargetData.h"
33using namespace llvm;
34
35// Register the AliasAnalysis interface, providing a nice name to refer to.
36namespace {
37  RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis");
38}
39char AliasAnalysis::ID = 0;
40
41//===----------------------------------------------------------------------===//
42// Default chaining methods
43//===----------------------------------------------------------------------===//
44
45AliasAnalysis::AliasResult
46AliasAnalysis::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
52void 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
57bool AliasAnalysis::pointsToConstantMemory(const Value *P) {
58  assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
59  return AA->pointsToConstantMemory(P);
60}
61
62AliasAnalysis::ModRefBehavior
63AliasAnalysis::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
69bool AliasAnalysis::hasNoModRefInfoForCalls() const {
70  assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
71  return AA->hasNoModRefInfoForCalls();
72}
73
74void AliasAnalysis::deleteValue(Value *V) {
75  assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
76  AA->deleteValue(V);
77}
78
79void 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
84AliasAnalysis::ModRefResult
85AliasAnalysis::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
96AliasAnalysis::ModRefResult
97AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) {
98  return alias(L->getOperand(0), TD->getTypeSize(L->getType()),
99               P, Size) ? Ref : NoModRef;
100}
101
102AliasAnalysis::ModRefResult
103AliasAnalysis::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), TD->getTypeSize(S->getOperand(0)->getType()),
107             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
115AliasAnalysis::ModRefResult
116AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
117  ModRefResult Mask = ModRef;
118  if (Function *F = CS.getCalledFunction()) {
119    ModRefBehavior MRB = getModRefBehavior(F, CallSite());
120    if (MRB == OnlyReadsMemory)
121      Mask = Ref;
122    else if (MRB == DoesNotAccessMemory)
123      return NoModRef;
124  }
125
126  if (!AA) return Mask;
127
128  // If P points to a constant memory location, the call definitely could not
129  // modify the memory location.
130  if ((Mask & Mod) && AA->pointsToConstantMemory(P))
131    Mask = ModRefResult(Mask & ~Mod);
132
133  return ModRefResult(Mask & AA->getModRefInfo(CS, P, Size));
134}
135
136// AliasAnalysis destructor: DO NOT move this to the header file for
137// AliasAnalysis or else clients of the AliasAnalysis class may not depend on
138// the AliasAnalysis.o file in the current .a file, causing alias analysis
139// support to not be included in the tool correctly!
140//
141AliasAnalysis::~AliasAnalysis() {}
142
143/// setTargetData - Subclasses must call this method to initialize the
144/// AliasAnalysis interface before any other methods are called.
145///
146void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
147  TD = &P->getAnalysis<TargetData>();
148  AA = &P->getAnalysis<AliasAnalysis>();
149}
150
151// getAnalysisUsage - All alias analysis implementations should invoke this
152// directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that
153// TargetData is required by the pass.
154void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
155  AU.addRequired<TargetData>();            // All AA's need TargetData.
156  AU.addRequired<AliasAnalysis>();         // All AA's chain
157}
158
159/// canBasicBlockModify - Return true if it is possible for execution of the
160/// specified basic block to modify the value pointed to by Ptr.
161///
162bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
163                                        const Value *Ptr, unsigned Size) {
164  return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size);
165}
166
167/// canInstructionRangeModify - Return true if it is possible for the execution
168/// of the specified instructions to modify the value pointed to by Ptr.  The
169/// instructions to consider are all of the instructions in the range of [I1,I2]
170/// INCLUSIVE.  I1 and I2 must be in the same basic block.
171///
172bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
173                                              const Instruction &I2,
174                                              const Value *Ptr, unsigned Size) {
175  assert(I1.getParent() == I2.getParent() &&
176         "Instructions not in same basic block!");
177  BasicBlock::iterator I = const_cast<Instruction*>(&I1);
178  BasicBlock::iterator E = const_cast<Instruction*>(&I2);
179  ++E;  // Convert from inclusive to exclusive range.
180
181  for (; I != E; ++I) // Check every instruction in range
182    if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod)
183      return true;
184  return false;
185}
186
187// Because of the way .a files work, we must force the BasicAA implementation to
188// be pulled in if the AliasAnalysis classes are pulled in.  Otherwise we run
189// the risk of AliasAnalysis being used, but the default implementation not
190// being linked into the tool that uses it.
191DEFINING_FILE_FOR(AliasAnalysis)
192