blob: 2eb0bf34eda648c7bc520772d569f5beb581c7ca [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"
Owen Andersonfbb585b2009-02-03 06:27:22 +000031#include "llvm/IntrinsicInst.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/Instructions.h"
33#include "llvm/Type.h"
34#include "llvm/Target/TargetData.h"
35using namespace llvm;
36
37// Register the AliasAnalysis interface, providing a nice name to refer to.
Dan Gohman089efff2008-05-13 00:00:25 +000038static RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039char 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
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052bool AliasAnalysis::pointsToConstantMemory(const Value *P) {
53 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
54 return AA->pointsToConstantMemory(P);
55}
56
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057void AliasAnalysis::deleteValue(Value *V) {
58 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
59 AA->deleteValue(V);
60}
61
62void AliasAnalysis::copyValue(Value *From, Value *To) {
63 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
64 AA->copyValue(From, To);
65}
66
67AliasAnalysis::ModRefResult
Dan Gohman503e2042010-08-03 21:48:53 +000068AliasAnalysis::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
70 return AA->getModRefInfo(CS1, CS2);
71}
72
73
74//===----------------------------------------------------------------------===//
75// AliasAnalysis non-virtual helper method implementation
76//===----------------------------------------------------------------------===//
77
78AliasAnalysis::ModRefResult
Dan Gohman503e2042010-08-03 21:48:53 +000079AliasAnalysis::getModRefInfo(const LoadInst *L, const Value *P, unsigned Size) {
Dan Gohman99247942010-08-03 17:27:43 +000080 // If the load address doesn't alias the given address, it doesn't read
81 // or write the specified memory.
82 if (!alias(L->getOperand(0), getTypeStoreSize(L->getType()), P, Size))
83 return NoModRef;
84
85 // Be conservative in the face of volatile.
86 if (L->isVolatile())
87 return ModRef;
88
89 // Otherwise, a load just reads.
90 return Ref;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091}
92
93AliasAnalysis::ModRefResult
Dan Gohman503e2042010-08-03 21:48:53 +000094AliasAnalysis::getModRefInfo(const StoreInst *S, const Value *P, unsigned Size) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 // If the stored address cannot alias the pointer in question, then the
96 // pointer cannot be modified by the store.
Duncan Sandsf99fdc62007-11-01 20:53:16 +000097 if (!alias(S->getOperand(1),
Dan Gohman624f9612009-07-25 00:48:42 +000098 getTypeStoreSize(S->getOperand(0)->getType()), P, Size))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 return NoModRef;
100
Dan Gohman99247942010-08-03 17:27:43 +0000101 // Be conservative in the face of volatile.
102 if (S->isVolatile())
103 return ModRef;
104
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 // If the pointer is a pointer to constant memory, then it could not have been
106 // modified by this store.
Dan Gohman99247942010-08-03 17:27:43 +0000107 if (pointsToConstantMemory(P))
108 return NoModRef;
109
110 // Otherwise, a store just writes.
111 return Mod;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112}
113
Duncan Sands00b24b52007-12-01 07:51:45 +0000114AliasAnalysis::ModRefBehavior
Dan Gohman62377cb2010-08-03 23:08:10 +0000115AliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
Duncan Sands6c48de42007-12-12 16:01:40 +0000116 if (CS.doesNotAccessMemory())
Duncan Sands00b24b52007-12-01 07:51:45 +0000117 // Can't do better than this.
118 return DoesNotAccessMemory;
Dan Gohman62377cb2010-08-03 23:08:10 +0000119 ModRefBehavior MRB = getModRefBehavior(CS.getCalledFunction());
Duncan Sands6c48de42007-12-12 16:01:40 +0000120 if (MRB != DoesNotAccessMemory && CS.onlyReadsMemory())
Duncan Sands00b24b52007-12-01 07:51:45 +0000121 return OnlyReadsMemory;
122 return MRB;
123}
124
125AliasAnalysis::ModRefBehavior
Dan Gohman62377cb2010-08-03 23:08:10 +0000126AliasAnalysis::getModRefBehavior(const Function *F) {
Duncan Sands038b4e72009-02-13 17:32:26 +0000127 if (F) {
128 if (F->doesNotAccessMemory())
129 // Can't do better than this.
130 return DoesNotAccessMemory;
Duncan Sands555ddcf2009-02-14 10:56:35 +0000131 if (F->onlyReadsMemory())
Duncan Sands038b4e72009-02-13 17:32:26 +0000132 return OnlyReadsMemory;
Duncan Sandsf68032c2010-01-06 08:45:52 +0000133 if (unsigned id = F->getIntrinsicID())
Dan Gohman503e2042010-08-03 21:48:53 +0000134 return getIntrinsicModRefBehavior(id);
Duncan Sandsf68032c2010-01-06 08:45:52 +0000135 }
136 return UnknownModRefBehavior;
137}
138
Dan Gohman503e2042010-08-03 21:48:53 +0000139AliasAnalysis::ModRefBehavior
140AliasAnalysis::getIntrinsicModRefBehavior(unsigned iid) {
Duncan Sands555ddcf2009-02-14 10:56:35 +0000141#define GET_INTRINSIC_MODREF_BEHAVIOR
142#include "llvm/Intrinsics.gen"
143#undef GET_INTRINSIC_MODREF_BEHAVIOR
Duncan Sands00b24b52007-12-01 07:51:45 +0000144}
145
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146AliasAnalysis::ModRefResult
Dan Gohman503e2042010-08-03 21:48:53 +0000147AliasAnalysis::getModRefInfo(ImmutableCallSite CS,
148 const Value *P, unsigned Size) {
Duncan Sands00b24b52007-12-01 07:51:45 +0000149 ModRefBehavior MRB = getModRefBehavior(CS);
Duncan Sands038b4e72009-02-13 17:32:26 +0000150 if (MRB == DoesNotAccessMemory)
Duncan Sands00b24b52007-12-01 07:51:45 +0000151 return NoModRef;
Chris Lattner913c8ad2009-11-23 16:46:41 +0000152
153 ModRefResult Mask = ModRef;
154 if (MRB == OnlyReadsMemory)
Duncan Sands038b4e72009-02-13 17:32:26 +0000155 Mask = Ref;
Owen Andersonab465642009-02-05 23:36:27 +0000156 else if (MRB == AliasAnalysis::AccessesArguments) {
157 bool doesAlias = false;
Dan Gohman503e2042010-08-03 21:48:53 +0000158 for (ImmutableCallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
Owen Andersonab465642009-02-05 23:36:27 +0000159 AI != AE; ++AI)
Chris Lattner913c8ad2009-11-23 16:46:41 +0000160 if (!isNoAlias(*AI, ~0U, P, Size)) {
Owen Andersonab465642009-02-05 23:36:27 +0000161 doesAlias = true;
162 break;
163 }
Duncan Sands038b4e72009-02-13 17:32:26 +0000164
Owen Andersonab465642009-02-05 23:36:27 +0000165 if (!doesAlias)
166 return NoModRef;
167 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168
169 if (!AA) return Mask;
170
171 // If P points to a constant memory location, the call definitely could not
172 // modify the memory location.
173 if ((Mask & Mod) && AA->pointsToConstantMemory(P))
174 Mask = ModRefResult(Mask & ~Mod);
175
176 return ModRefResult(Mask & AA->getModRefInfo(CS, P, Size));
177}
178
179// AliasAnalysis destructor: DO NOT move this to the header file for
180// AliasAnalysis or else clients of the AliasAnalysis class may not depend on
181// the AliasAnalysis.o file in the current .a file, causing alias analysis
182// support to not be included in the tool correctly!
183//
184AliasAnalysis::~AliasAnalysis() {}
185
Dan Gohman5284bb22008-05-30 00:02:02 +0000186/// InitializeAliasAnalysis - Subclasses must call this method to initialize the
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187/// AliasAnalysis interface before any other methods are called.
188///
189void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
Dan Gohman624f9612009-07-25 00:48:42 +0000190 TD = P->getAnalysisIfAvailable<TargetData>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 AA = &P->getAnalysis<AliasAnalysis>();
192}
193
194// getAnalysisUsage - All alias analysis implementations should invoke this
Dan Gohman624f9612009-07-25 00:48:42 +0000195// directly (using AliasAnalysis::getAnalysisUsage(AU)).
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 AU.addRequired<AliasAnalysis>(); // All AA's chain
198}
199
Dan Gohman624f9612009-07-25 00:48:42 +0000200/// getTypeStoreSize - Return the TargetData store size for the given type,
201/// if known, or a conservative value otherwise.
202///
203unsigned AliasAnalysis::getTypeStoreSize(const Type *Ty) {
204 return TD ? TD->getTypeStoreSize(Ty) : ~0u;
205}
206
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207/// canBasicBlockModify - Return true if it is possible for execution of the
208/// specified basic block to modify the value pointed to by Ptr.
209///
210bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
211 const Value *Ptr, unsigned Size) {
212 return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size);
213}
214
215/// canInstructionRangeModify - Return true if it is possible for the execution
216/// of the specified instructions to modify the value pointed to by Ptr. The
217/// instructions to consider are all of the instructions in the range of [I1,I2]
218/// INCLUSIVE. I1 and I2 must be in the same basic block.
219///
220bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
221 const Instruction &I2,
222 const Value *Ptr, unsigned Size) {
223 assert(I1.getParent() == I2.getParent() &&
224 "Instructions not in same basic block!");
Dan Gohman503e2042010-08-03 21:48:53 +0000225 BasicBlock::const_iterator I = &I1;
226 BasicBlock::const_iterator E = &I2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 ++E; // Convert from inclusive to exclusive range.
228
229 for (; I != E; ++I) // Check every instruction in range
Dan Gohman503e2042010-08-03 21:48:53 +0000230 if (getModRefInfo(I, Ptr, Size) & Mod)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 return true;
232 return false;
233}
234
Dan Gohmand4b0e322009-02-03 01:28:32 +0000235/// isNoAliasCall - Return true if this pointer is returned by a noalias
236/// function.
237bool llvm::isNoAliasCall(const Value *V) {
238 if (isa<CallInst>(V) || isa<InvokeInst>(V))
Dan Gohman503e2042010-08-03 21:48:53 +0000239 return ImmutableCallSite(cast<Instruction>(V))
Dan Gohmand4b0e322009-02-03 01:28:32 +0000240 .paramHasAttr(0, Attribute::NoAlias);
241 return false;
242}
243
244/// isIdentifiedObject - Return true if this pointer refers to a distinct and
245/// identifiable object. This returns true for:
Dan Gohman3c845512009-08-27 17:52:56 +0000246/// Global Variables and Functions (but not Global Aliases)
Dan Gohmand4b0e322009-02-03 01:28:32 +0000247/// Allocas and Mallocs
Dan Gohman83a01ad22010-07-07 14:27:09 +0000248/// ByVal and NoAlias Arguments
249/// NoAlias returns
Dan Gohmand4b0e322009-02-03 01:28:32 +0000250///
Dan Gohman83a01ad22010-07-07 14:27:09 +0000251bool llvm::isIdentifiedObject(const Value *V) {
Dan Gohmanf9e8abc2010-06-29 00:50:39 +0000252 if (isa<AllocaInst>(V))
Dan Gohman3c845512009-08-27 17:52:56 +0000253 return true;
254 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
Dan Gohmand4b0e322009-02-03 01:28:32 +0000255 return true;
Dan Gohman83a01ad22010-07-07 14:27:09 +0000256 if (isNoAliasCall(V))
257 return true;
258 if (const Argument *A = dyn_cast<Argument>(V))
259 return A->hasNoAliasAttr() || A->hasByValAttr();
Dan Gohmand4b0e322009-02-03 01:28:32 +0000260 return false;
261}
262
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263// Because of the way .a files work, we must force the BasicAA implementation to
264// be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run
265// the risk of AliasAnalysis being used, but the default implementation not
266// being linked into the tool that uses it.
267DEFINING_FILE_FOR(AliasAnalysis)