blob: 789843c45b85bdb24f63bee27038d4648a7a6ea5 [file] [log] [blame]
Chris Lattner53ad0ed2002-08-22 18:25:32 +00001//===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==//
2//
3// This file implements the generic AliasAnalysis interface which is used as the
4// common interface used by all clients and implementations of alias analysis.
5//
6// This file also implements the default version of the AliasAnalysis interface
7// that is to be used when no other implementation is specified. This does some
8// simple tests that detect obvious cases: two different global pointers cannot
9// alias, a global cannot alias a malloc, two different mallocs cannot alias,
10// etc.
11//
12// This alias analysis implementation really isn't very good for anything, but
13// it is very fast, and makes a nice clean default implementation. Because it
14// handles lots of little corner cases, other, more complex, alias analysis
15// implementations may choose to rely on this pass to resolve these simple and
16// easy cases.
17//
18//===----------------------------------------------------------------------===//
19
20#include "llvm/Analysis/BasicAliasAnalysis.h"
21#include "llvm/BasicBlock.h"
22#include "llvm/Support/InstVisitor.h"
23#include "llvm/iMemory.h"
Chris Lattnera6299342002-09-08 18:45:18 +000024#include "llvm/iOther.h"
Chris Lattner53ad0ed2002-08-22 18:25:32 +000025#include "llvm/Constants.h"
26#include "llvm/GlobalValue.h"
Chris Lattner22d8cd62002-08-22 18:57:09 +000027#include "llvm/DerivedTypes.h"
Chris Lattnerdc1ad192003-02-03 21:16:17 +000028#include "Support/Statistic.h"
Chris Lattner53ad0ed2002-08-22 18:25:32 +000029
30// Register the AliasAnalysis interface, providing a nice name to refer to.
Chris Lattnerdc1ad192003-02-03 21:16:17 +000031namespace {
32 RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis");
33 Statistic<> NumNoAlias ("basic-aa", "Number of 'no alias' replies");
34 Statistic<> NumMayAlias ("basic-aa", "Number of 'may alias' replies");
35 Statistic<> NumMustAlias("basic-aa", "Number of 'must alias' replies");
36}
Chris Lattner53ad0ed2002-08-22 18:25:32 +000037
38// CanModify - Define a little visitor class that is used to check to see if
39// arbitrary chunks of code can modify a specified pointer.
40//
41namespace {
42 struct CanModify : public InstVisitor<CanModify, bool> {
Vikram S. Adve75310d52002-11-06 17:17:55 +000043 AliasAnalysis &AA;
Chris Lattner53ad0ed2002-08-22 18:25:32 +000044 const Value *Ptr;
45
Vikram S. Adve75310d52002-11-06 17:17:55 +000046 CanModify(AliasAnalysis *aa, const Value *ptr)
Chris Lattner53ad0ed2002-08-22 18:25:32 +000047 : AA(*aa), Ptr(ptr) {}
48
49 bool visitInvokeInst(InvokeInst &II) {
50 return AA.canInvokeModify(II, Ptr);
51 }
52 bool visitCallInst(CallInst &CI) {
53 return AA.canCallModify(CI, Ptr);
54 }
55 bool visitStoreInst(StoreInst &SI) {
Chris Lattner53ad0ed2002-08-22 18:25:32 +000056 return AA.alias(Ptr, SI.getOperand(1));
57 }
58
59 // Other instructions do not alias anything.
60 bool visitInstruction(Instruction &I) { return false; }
61 };
62}
63
64// AliasAnalysis destructor: DO NOT move this to the header file for
65// AliasAnalysis or else clients of the AliasAnalysis class may not depend on
66// the AliasAnalysis.o file in the current .a file, causing alias analysis
67// support to not be included in the tool correctly!
68//
69AliasAnalysis::~AliasAnalysis() {}
70
Chris Lattnerf9355f62002-08-22 22:46:39 +000071/// canBasicBlockModify - Return true if it is possible for execution of the
72/// specified basic block to modify the value pointed to by Ptr.
73///
Chris Lattner53ad0ed2002-08-22 18:25:32 +000074bool AliasAnalysis::canBasicBlockModify(const BasicBlock &bb,
Vikram S. Adve75310d52002-11-06 17:17:55 +000075 const Value *Ptr) {
Chris Lattner53ad0ed2002-08-22 18:25:32 +000076 CanModify CM(this, Ptr);
77 BasicBlock &BB = const_cast<BasicBlock&>(bb);
78
79 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
80 if (CM.visit(I)) // Check every instruction in the basic block...
81 return true;
82
83 return false;
84}
85
Chris Lattnerf9355f62002-08-22 22:46:39 +000086/// canInstructionRangeModify - Return true if it is possible for the execution
87/// of the specified instructions to modify the value pointed to by Ptr. The
88/// instructions to consider are all of the instructions in the range of [I1,I2]
89/// INCLUSIVE. I1 and I2 must be in the same basic block.
90///
Chris Lattner53ad0ed2002-08-22 18:25:32 +000091bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
92 const Instruction &I2,
Vikram S. Adve75310d52002-11-06 17:17:55 +000093 const Value *Ptr) {
Chris Lattner53ad0ed2002-08-22 18:25:32 +000094 assert(I1.getParent() == I2.getParent() &&
95 "Instructions not in same basic block!");
96 CanModify CM(this, Ptr);
97 BasicBlock::iterator I = const_cast<Instruction*>(&I1);
98 BasicBlock::iterator E = const_cast<Instruction*>(&I2);
99 ++E; // Convert from inclusive to exclusive range.
100
101 for (; I != E; ++I)
102 if (CM.visit(I)) // Check every instruction in the basic block...
103 return true;
104
105 return false;
106}
107
108//===----------------------------------------------------------------------===//
109// BasicAliasAnalysis Pass Implementation
110//===----------------------------------------------------------------------===//
111//
112// Because of the way .a files work, the implementation of the
113// BasicAliasAnalysis class MUST be in the AliasAnalysis file itself, or else we
114// run the risk of AliasAnalysis being used, but the default implementation not
115// being linked into the tool that uses it. As such, we register and implement
116// the class here.
117//
118namespace {
119 // Register this pass...
120 RegisterOpt<BasicAliasAnalysis>
121 X("basicaa", "Basic Alias Analysis (default AA impl)");
122
123 // Declare that we implement the AliasAnalysis interface
124 RegisterAnalysisGroup<AliasAnalysis, BasicAliasAnalysis, true> Y;
125} // End of anonymous namespace
126
127
128
129// hasUniqueAddress - Return true if the
130static inline bool hasUniqueAddress(const Value *V) {
131 return isa<GlobalValue>(V) || isa<MallocInst>(V) || isa<AllocaInst>(V);
132}
133
Chris Lattnera6299342002-09-08 18:45:18 +0000134static const Value *getUnderlyingObject(const Value *V) {
135 if (!isa<PointerType>(V->getType())) return 0;
136
137 // If we are at some type of object... return it.
138 if (hasUniqueAddress(V)) return V;
139
140 // Traverse through different addressing mechanisms...
141 if (const Instruction *I = dyn_cast<Instruction>(V)) {
142 if (isa<CastInst>(I) || isa<GetElementPtrInst>(I))
143 return getUnderlyingObject(I->getOperand(0));
144 }
145 return 0;
146}
147
Chris Lattnerdc1ad192003-02-03 21:16:17 +0000148static inline AliasAnalysis::Result MustAlias() {
149 ++NumMustAlias;
150 return AliasAnalysis::MustAlias;
151}
152
153static inline AliasAnalysis::Result MayAlias() {
154 ++NumMayAlias;
155 return AliasAnalysis::MayAlias;
156}
157
158static inline AliasAnalysis::Result NoAlias() {
159 ++NumNoAlias;
160 return AliasAnalysis::NoAlias;
161}
162
Chris Lattnera6299342002-09-08 18:45:18 +0000163// alias - Provide a bunch of ad-hoc rules to disambiguate in common cases, such
164// as array references. Note that this function is heavily tail recursive.
165// Hopefully we have a smart C++ compiler. :)
166//
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000167AliasAnalysis::Result BasicAliasAnalysis::alias(const Value *V1,
Vikram S. Adve75310d52002-11-06 17:17:55 +0000168 const Value *V2) {
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000169 // Strip off constant pointer refs if they exist
170 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V1))
171 V1 = CPR->getValue();
172 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V2))
173 V2 = CPR->getValue();
174
175 // Are we checking for alias of the same value?
Chris Lattnerdc1ad192003-02-03 21:16:17 +0000176 if (V1 == V2) return ::MustAlias();
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000177
Chris Lattnera6299342002-09-08 18:45:18 +0000178 if ((!isa<PointerType>(V1->getType()) || !isa<PointerType>(V2->getType())) &&
179 V1->getType() != Type::LongTy && V2->getType() != Type::LongTy)
Chris Lattnerdc1ad192003-02-03 21:16:17 +0000180 return ::NoAlias(); // Scalars cannot alias each other
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000181
Chris Lattnera6299342002-09-08 18:45:18 +0000182 // Strip off cast instructions...
183 if (const Instruction *I = dyn_cast<CastInst>(V1))
184 return alias(I->getOperand(0), V2);
185 if (const Instruction *I = dyn_cast<CastInst>(V2))
186 return alias(I->getOperand(0), V1);
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000187
Chris Lattnera6299342002-09-08 18:45:18 +0000188 // If we have two gep instructions with identical indices, return an alias
189 // result equal to the alias result of the original pointer...
190 //
191 if (const GetElementPtrInst *GEP1 = dyn_cast<GetElementPtrInst>(V1))
192 if (const GetElementPtrInst *GEP2 = dyn_cast<GetElementPtrInst>(V2))
193 if (GEP1->getNumOperands() == GEP2->getNumOperands() &&
194 GEP1->getOperand(0)->getType() == GEP2->getOperand(0)->getType()) {
195 if (std::equal(GEP1->op_begin()+1, GEP1->op_end(), GEP2->op_begin()+1))
196 return alias(GEP1->getOperand(0), GEP2->getOperand(0));
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000197
Chris Lattnera6299342002-09-08 18:45:18 +0000198 // If all of the indexes to the getelementptr are constant, but
199 // different (well we already know they are different), then we know
200 // that there cannot be an alias here if the two base pointers DO alias.
201 //
202 bool AllConstant = true;
203 for (unsigned i = 1, e = GEP1->getNumOperands(); i != e; ++i)
204 if (!isa<Constant>(GEP1->getOperand(i)) ||
205 !isa<Constant>(GEP2->getOperand(i))) {
206 AllConstant = false;
207 break;
208 }
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000209
Chris Lattnera6299342002-09-08 18:45:18 +0000210 // If we are all constant, then look at where the the base pointers
211 // alias. If they are known not to alias, then we are dealing with two
212 // different arrays or something, so no alias is possible. If they are
213 // known to be the same object, then we cannot alias because we are
214 // indexing into a different part of the object. As usual, MayAlias
215 // doesn't tell us anything.
216 //
217 if (AllConstant &&
218 alias(GEP1->getOperand(0), GEP2->getOperand(1)) != MayAlias)
Chris Lattnerdc1ad192003-02-03 21:16:17 +0000219 return ::NoAlias();
Chris Lattnera6299342002-09-08 18:45:18 +0000220 }
221
222 // Figure out what objects these things are pointing to if we can...
223 const Value *O1 = getUnderlyingObject(V1);
224 const Value *O2 = getUnderlyingObject(V2);
225
226 // Pointing at a discernable object?
227 if (O1 && O2) {
228 // If they are two different objects, we know that we have no alias...
Chris Lattnerdc1ad192003-02-03 21:16:17 +0000229 if (O1 != O2) return ::NoAlias();
Chris Lattnera6299342002-09-08 18:45:18 +0000230
231 // If they are the same object, they we can look at the indexes. If they
232 // index off of the object is the same for both pointers, they must alias.
233 // If they are provably different, they must not alias. Otherwise, we can't
234 // tell anything.
235 } else if (O1 && isa<ConstantPointerNull>(V2)) {
Chris Lattnerdc1ad192003-02-03 21:16:17 +0000236 return ::NoAlias(); // Unique values don't alias null
Chris Lattnera6299342002-09-08 18:45:18 +0000237 } else if (O2 && isa<ConstantPointerNull>(V1)) {
Chris Lattnerdc1ad192003-02-03 21:16:17 +0000238 return ::NoAlias(); // Unique values don't alias null
Chris Lattnera6299342002-09-08 18:45:18 +0000239 }
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000240
241 return MayAlias;
242}