blob: 2dbff6aa5c77dcad99a8615c4af7ee1daa381ee8 [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 Lattner53ad0ed2002-08-22 18:25:32 +000028
29// Register the AliasAnalysis interface, providing a nice name to refer to.
Chris Lattnerdc1ad192003-02-03 21:16:17 +000030namespace {
31 RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis");
Chris Lattnerdc1ad192003-02-03 21:16:17 +000032}
Chris Lattner53ad0ed2002-08-22 18:25:32 +000033
34// CanModify - Define a little visitor class that is used to check to see if
35// arbitrary chunks of code can modify a specified pointer.
36//
37namespace {
38 struct CanModify : public InstVisitor<CanModify, bool> {
Vikram S. Adve75310d52002-11-06 17:17:55 +000039 AliasAnalysis &AA;
Chris Lattner53ad0ed2002-08-22 18:25:32 +000040 const Value *Ptr;
41
Vikram S. Adve75310d52002-11-06 17:17:55 +000042 CanModify(AliasAnalysis *aa, const Value *ptr)
Chris Lattner53ad0ed2002-08-22 18:25:32 +000043 : AA(*aa), Ptr(ptr) {}
44
45 bool visitInvokeInst(InvokeInst &II) {
46 return AA.canInvokeModify(II, Ptr);
47 }
48 bool visitCallInst(CallInst &CI) {
49 return AA.canCallModify(CI, Ptr);
50 }
51 bool visitStoreInst(StoreInst &SI) {
Chris Lattner53ad0ed2002-08-22 18:25:32 +000052 return AA.alias(Ptr, SI.getOperand(1));
53 }
54
55 // Other instructions do not alias anything.
56 bool visitInstruction(Instruction &I) { return false; }
57 };
58}
59
60// AliasAnalysis destructor: DO NOT move this to the header file for
61// AliasAnalysis or else clients of the AliasAnalysis class may not depend on
62// the AliasAnalysis.o file in the current .a file, causing alias analysis
63// support to not be included in the tool correctly!
64//
65AliasAnalysis::~AliasAnalysis() {}
66
Chris Lattnerf9355f62002-08-22 22:46:39 +000067/// canBasicBlockModify - Return true if it is possible for execution of the
68/// specified basic block to modify the value pointed to by Ptr.
69///
Chris Lattner53ad0ed2002-08-22 18:25:32 +000070bool AliasAnalysis::canBasicBlockModify(const BasicBlock &bb,
Vikram S. Adve75310d52002-11-06 17:17:55 +000071 const Value *Ptr) {
Chris Lattner53ad0ed2002-08-22 18:25:32 +000072 CanModify CM(this, Ptr);
73 BasicBlock &BB = const_cast<BasicBlock&>(bb);
74
75 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
76 if (CM.visit(I)) // Check every instruction in the basic block...
77 return true;
78
79 return false;
80}
81
Chris Lattnerf9355f62002-08-22 22:46:39 +000082/// canInstructionRangeModify - Return true if it is possible for the execution
83/// of the specified instructions to modify the value pointed to by Ptr. The
84/// instructions to consider are all of the instructions in the range of [I1,I2]
85/// INCLUSIVE. I1 and I2 must be in the same basic block.
86///
Chris Lattner53ad0ed2002-08-22 18:25:32 +000087bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
88 const Instruction &I2,
Vikram S. Adve75310d52002-11-06 17:17:55 +000089 const Value *Ptr) {
Chris Lattner53ad0ed2002-08-22 18:25:32 +000090 assert(I1.getParent() == I2.getParent() &&
91 "Instructions not in same basic block!");
92 CanModify CM(this, Ptr);
93 BasicBlock::iterator I = const_cast<Instruction*>(&I1);
94 BasicBlock::iterator E = const_cast<Instruction*>(&I2);
95 ++E; // Convert from inclusive to exclusive range.
96
97 for (; I != E; ++I)
98 if (CM.visit(I)) // Check every instruction in the basic block...
99 return true;
100
101 return false;
102}
103
104//===----------------------------------------------------------------------===//
105// BasicAliasAnalysis Pass Implementation
106//===----------------------------------------------------------------------===//
107//
108// Because of the way .a files work, the implementation of the
109// BasicAliasAnalysis class MUST be in the AliasAnalysis file itself, or else we
110// run the risk of AliasAnalysis being used, but the default implementation not
111// being linked into the tool that uses it. As such, we register and implement
112// the class here.
113//
114namespace {
115 // Register this pass...
116 RegisterOpt<BasicAliasAnalysis>
117 X("basicaa", "Basic Alias Analysis (default AA impl)");
118
119 // Declare that we implement the AliasAnalysis interface
120 RegisterAnalysisGroup<AliasAnalysis, BasicAliasAnalysis, true> Y;
121} // End of anonymous namespace
122
123
124
125// hasUniqueAddress - Return true if the
126static inline bool hasUniqueAddress(const Value *V) {
127 return isa<GlobalValue>(V) || isa<MallocInst>(V) || isa<AllocaInst>(V);
128}
129
Chris Lattnera6299342002-09-08 18:45:18 +0000130static const Value *getUnderlyingObject(const Value *V) {
131 if (!isa<PointerType>(V->getType())) return 0;
132
133 // If we are at some type of object... return it.
134 if (hasUniqueAddress(V)) return V;
135
136 // Traverse through different addressing mechanisms...
137 if (const Instruction *I = dyn_cast<Instruction>(V)) {
138 if (isa<CastInst>(I) || isa<GetElementPtrInst>(I))
139 return getUnderlyingObject(I->getOperand(0));
140 }
141 return 0;
142}
143
Chris Lattnerdc1ad192003-02-03 21:16:17 +0000144
Chris Lattnera6299342002-09-08 18:45:18 +0000145// alias - Provide a bunch of ad-hoc rules to disambiguate in common cases, such
146// as array references. Note that this function is heavily tail recursive.
147// Hopefully we have a smart C++ compiler. :)
148//
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000149AliasAnalysis::Result BasicAliasAnalysis::alias(const Value *V1,
Vikram S. Adve75310d52002-11-06 17:17:55 +0000150 const Value *V2) {
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000151 // Strip off constant pointer refs if they exist
152 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V1))
153 V1 = CPR->getValue();
154 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V2))
155 V2 = CPR->getValue();
156
157 // Are we checking for alias of the same value?
Chris Lattner1dbd1b82003-02-07 20:39:48 +0000158 if (V1 == V2) return MustAlias;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000159
Chris Lattnera6299342002-09-08 18:45:18 +0000160 if ((!isa<PointerType>(V1->getType()) || !isa<PointerType>(V2->getType())) &&
161 V1->getType() != Type::LongTy && V2->getType() != Type::LongTy)
Chris Lattner1dbd1b82003-02-07 20:39:48 +0000162 return NoAlias; // Scalars cannot alias each other
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000163
Chris Lattnera6299342002-09-08 18:45:18 +0000164 // Strip off cast instructions...
165 if (const Instruction *I = dyn_cast<CastInst>(V1))
166 return alias(I->getOperand(0), V2);
167 if (const Instruction *I = dyn_cast<CastInst>(V2))
168 return alias(I->getOperand(0), V1);
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000169
Chris Lattnera6299342002-09-08 18:45:18 +0000170 // If we have two gep instructions with identical indices, return an alias
171 // result equal to the alias result of the original pointer...
172 //
173 if (const GetElementPtrInst *GEP1 = dyn_cast<GetElementPtrInst>(V1))
174 if (const GetElementPtrInst *GEP2 = dyn_cast<GetElementPtrInst>(V2))
175 if (GEP1->getNumOperands() == GEP2->getNumOperands() &&
176 GEP1->getOperand(0)->getType() == GEP2->getOperand(0)->getType()) {
177 if (std::equal(GEP1->op_begin()+1, GEP1->op_end(), GEP2->op_begin()+1))
178 return alias(GEP1->getOperand(0), GEP2->getOperand(0));
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000179
Chris Lattnera6299342002-09-08 18:45:18 +0000180 // If all of the indexes to the getelementptr are constant, but
181 // different (well we already know they are different), then we know
182 // that there cannot be an alias here if the two base pointers DO alias.
183 //
184 bool AllConstant = true;
185 for (unsigned i = 1, e = GEP1->getNumOperands(); i != e; ++i)
186 if (!isa<Constant>(GEP1->getOperand(i)) ||
187 !isa<Constant>(GEP2->getOperand(i))) {
188 AllConstant = false;
189 break;
190 }
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000191
Chris Lattnera6299342002-09-08 18:45:18 +0000192 // If we are all constant, then look at where the the base pointers
193 // alias. If they are known not to alias, then we are dealing with two
194 // different arrays or something, so no alias is possible. If they are
195 // known to be the same object, then we cannot alias because we are
196 // indexing into a different part of the object. As usual, MayAlias
197 // doesn't tell us anything.
198 //
199 if (AllConstant &&
200 alias(GEP1->getOperand(0), GEP2->getOperand(1)) != MayAlias)
Chris Lattner1dbd1b82003-02-07 20:39:48 +0000201 return NoAlias;
Chris Lattnera6299342002-09-08 18:45:18 +0000202 }
203
204 // Figure out what objects these things are pointing to if we can...
205 const Value *O1 = getUnderlyingObject(V1);
206 const Value *O2 = getUnderlyingObject(V2);
207
208 // Pointing at a discernable object?
209 if (O1 && O2) {
210 // If they are two different objects, we know that we have no alias...
Chris Lattner1dbd1b82003-02-07 20:39:48 +0000211 if (O1 != O2) return NoAlias;
Chris Lattnera6299342002-09-08 18:45:18 +0000212
213 // If they are the same object, they we can look at the indexes. If they
214 // index off of the object is the same for both pointers, they must alias.
215 // If they are provably different, they must not alias. Otherwise, we can't
216 // tell anything.
217 } else if (O1 && isa<ConstantPointerNull>(V2)) {
Chris Lattner1dbd1b82003-02-07 20:39:48 +0000218 return NoAlias; // Unique values don't alias null
Chris Lattnera6299342002-09-08 18:45:18 +0000219 } else if (O2 && isa<ConstantPointerNull>(V1)) {
Chris Lattner1dbd1b82003-02-07 20:39:48 +0000220 return NoAlias; // Unique values don't alias null
Chris Lattnera6299342002-09-08 18:45:18 +0000221 }
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000222
223 return MayAlias;
224}