blob: c5523ec4634d8e4a83aa33d4afcbb1cf91acf18a [file] [log] [blame]
Chris Lattner53ad0ed2002-08-22 18:25:32 +00001//===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner53ad0ed2002-08-22 18:25:32 +00009//
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
Chris Lattnerd501c132003-02-26 19:41:54 +000027#include "llvm/Analysis/AliasAnalysis.h"
Reid Spencer6df60a92006-06-07 20:00:19 +000028#include "llvm/Pass.h"
Chris Lattner53ad0ed2002-08-22 18:25:32 +000029#include "llvm/BasicBlock.h"
Duncan Sandsdff67102007-12-01 07:51:45 +000030#include "llvm/Function.h"
Owen Andersoncd895252009-02-03 06:27:22 +000031#include "llvm/IntrinsicInst.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000032#include "llvm/Instructions.h"
Chris Lattner5b3a4552005-03-17 15:38:16 +000033#include "llvm/Type.h"
Chris Lattner14ac8772003-02-26 19:26:51 +000034#include "llvm/Target/TargetData.h"
Chris Lattner992860c2004-03-15 04:07:29 +000035using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000036
Chris Lattner53ad0ed2002-08-22 18:25:32 +000037// Register the AliasAnalysis interface, providing a nice name to refer to.
Dan Gohman844731a2008-05-13 00:00:25 +000038static RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis");
Devang Patel19974732007-05-03 01:11:54 +000039char AliasAnalysis::ID = 0;
Chris Lattner53ad0ed2002-08-22 18:25:32 +000040
Chris Lattner5a24d702004-05-23 21:15:48 +000041//===----------------------------------------------------------------------===//
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
Chris Lattner5a24d702004-05-23 21:15:48 +000062bool AliasAnalysis::hasNoModRefInfoForCalls() const {
63 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
64 return AA->hasNoModRefInfoForCalls();
65}
66
67void AliasAnalysis::deleteValue(Value *V) {
68 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
69 AA->deleteValue(V);
70}
71
72void AliasAnalysis::copyValue(Value *From, Value *To) {
73 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
74 AA->copyValue(From, To);
75}
76
77AliasAnalysis::ModRefResult
78AliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) {
79 // FIXME: we can do better.
80 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
81 return AA->getModRefInfo(CS1, CS2);
82}
83
84
85//===----------------------------------------------------------------------===//
86// AliasAnalysis non-virtual helper method implementation
87//===----------------------------------------------------------------------===//
88
Chris Lattner14ac8772003-02-26 19:26:51 +000089AliasAnalysis::ModRefResult
90AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) {
Duncan Sands514ab342007-11-01 20:53:16 +000091 return alias(L->getOperand(0), TD->getTypeStoreSize(L->getType()),
Chris Lattner14ac8772003-02-26 19:26:51 +000092 P, Size) ? Ref : NoModRef;
Chris Lattner53ad0ed2002-08-22 18:25:32 +000093}
94
Chris Lattner14ac8772003-02-26 19:26:51 +000095AliasAnalysis::ModRefResult
96AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) {
Chris Lattnerf4d904d2004-01-30 22:16:42 +000097 // If the stored address cannot alias the pointer in question, then the
98 // pointer cannot be modified by the store.
Duncan Sands514ab342007-11-01 20:53:16 +000099 if (!alias(S->getOperand(1),
100 TD->getTypeStoreSize(S->getOperand(0)->getType()), P, Size))
Chris Lattnerf4d904d2004-01-30 22:16:42 +0000101 return NoModRef;
102
103 // If the pointer is a pointer to constant memory, then it could not have been
104 // modified by this store.
105 return pointsToConstantMemory(P) ? NoModRef : Mod;
Chris Lattner14ac8772003-02-26 19:26:51 +0000106}
107
Duncan Sandsdff67102007-12-01 07:51:45 +0000108AliasAnalysis::ModRefBehavior
109AliasAnalysis::getModRefBehavior(CallSite CS,
110 std::vector<PointerAccessInfo> *Info) {
Duncan Sands7915cbe2007-12-12 16:01:40 +0000111 if (CS.doesNotAccessMemory())
Duncan Sandsdff67102007-12-01 07:51:45 +0000112 // Can't do better than this.
113 return DoesNotAccessMemory;
Owen Andersone7942202009-02-05 23:36:27 +0000114 ModRefBehavior MRB = getModRefBehavior(CS.getCalledFunction(), Info);
Duncan Sands7915cbe2007-12-12 16:01:40 +0000115 if (MRB != DoesNotAccessMemory && CS.onlyReadsMemory())
Duncan Sandsdff67102007-12-01 07:51:45 +0000116 return OnlyReadsMemory;
117 return MRB;
118}
119
120AliasAnalysis::ModRefBehavior
121AliasAnalysis::getModRefBehavior(Function *F,
122 std::vector<PointerAccessInfo> *Info) {
Duncan Sandsddd8c452009-02-13 17:32:26 +0000123 if (F) {
124 if (F->doesNotAccessMemory())
125 // Can't do better than this.
126 return DoesNotAccessMemory;
Duncan Sandsd869b382009-02-14 10:56:35 +0000127 if (F->onlyReadsMemory())
Duncan Sandsddd8c452009-02-13 17:32:26 +0000128 return OnlyReadsMemory;
Duncan Sandsd869b382009-02-14 10:56:35 +0000129 if (unsigned id = F->getIntrinsicID()) {
130#define GET_INTRINSIC_MODREF_BEHAVIOR
131#include "llvm/Intrinsics.gen"
132#undef GET_INTRINSIC_MODREF_BEHAVIOR
133 }
Duncan Sandsddd8c452009-02-13 17:32:26 +0000134 }
Owen Andersone7942202009-02-05 23:36:27 +0000135 return UnknownModRefBehavior;
Duncan Sandsdff67102007-12-01 07:51:45 +0000136}
137
Chris Lattner992860c2004-03-15 04:07:29 +0000138AliasAnalysis::ModRefResult
139AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
Chris Lattner5a24d702004-05-23 21:15:48 +0000140 ModRefResult Mask = ModRef;
Duncan Sandsdff67102007-12-01 07:51:45 +0000141 ModRefBehavior MRB = getModRefBehavior(CS);
Duncan Sandsddd8c452009-02-13 17:32:26 +0000142 if (MRB == DoesNotAccessMemory)
Duncan Sandsdff67102007-12-01 07:51:45 +0000143 return NoModRef;
Duncan Sandsddd8c452009-02-13 17:32:26 +0000144 else if (MRB == OnlyReadsMemory)
145 Mask = Ref;
Owen Andersone7942202009-02-05 23:36:27 +0000146 else if (MRB == AliasAnalysis::AccessesArguments) {
147 bool doesAlias = false;
148 for (CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
149 AI != AE; ++AI)
150 if (alias(*AI, ~0U, P, Size) != NoAlias) {
151 doesAlias = true;
152 break;
153 }
Duncan Sandsddd8c452009-02-13 17:32:26 +0000154
Owen Andersone7942202009-02-05 23:36:27 +0000155 if (!doesAlias)
156 return NoModRef;
157 }
Chris Lattner992860c2004-03-15 04:07:29 +0000158
Chris Lattner5a24d702004-05-23 21:15:48 +0000159 if (!AA) return Mask;
160
Chris Lattner992860c2004-03-15 04:07:29 +0000161 // If P points to a constant memory location, the call definitely could not
162 // modify the memory location.
Chris Lattner5a24d702004-05-23 21:15:48 +0000163 if ((Mask & Mod) && AA->pointsToConstantMemory(P))
Chris Lattnerd433bde2005-03-23 22:06:41 +0000164 Mask = ModRefResult(Mask & ~Mod);
Chris Lattner992860c2004-03-15 04:07:29 +0000165
Chris Lattner5a24d702004-05-23 21:15:48 +0000166 return ModRefResult(Mask & AA->getModRefInfo(CS, P, Size));
Chris Lattner992860c2004-03-15 04:07:29 +0000167}
168
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000169// AliasAnalysis destructor: DO NOT move this to the header file for
170// AliasAnalysis or else clients of the AliasAnalysis class may not depend on
171// the AliasAnalysis.o file in the current .a file, causing alias analysis
172// support to not be included in the tool correctly!
173//
174AliasAnalysis::~AliasAnalysis() {}
175
Dan Gohman5a56bf62008-05-30 00:02:02 +0000176/// InitializeAliasAnalysis - Subclasses must call this method to initialize the
Chris Lattner14ac8772003-02-26 19:26:51 +0000177/// AliasAnalysis interface before any other methods are called.
178///
179void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
180 TD = &P->getAnalysis<TargetData>();
Chris Lattner5a24d702004-05-23 21:15:48 +0000181 AA = &P->getAnalysis<AliasAnalysis>();
Chris Lattner14ac8772003-02-26 19:26:51 +0000182}
183
184// getAnalysisUsage - All alias analysis implementations should invoke this
185// directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that
186// TargetData is required by the pass.
187void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
188 AU.addRequired<TargetData>(); // All AA's need TargetData.
Chris Lattner5a24d702004-05-23 21:15:48 +0000189 AU.addRequired<AliasAnalysis>(); // All AA's chain
Chris Lattner14ac8772003-02-26 19:26:51 +0000190}
191
Chris Lattnerf9355f62002-08-22 22:46:39 +0000192/// canBasicBlockModify - Return true if it is possible for execution of the
193/// specified basic block to modify the value pointed to by Ptr.
194///
Chris Lattner14ac8772003-02-26 19:26:51 +0000195bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
196 const Value *Ptr, unsigned Size) {
197 return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size);
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000198}
199
Chris Lattnerf9355f62002-08-22 22:46:39 +0000200/// canInstructionRangeModify - Return true if it is possible for the execution
201/// of the specified instructions to modify the value pointed to by Ptr. The
202/// instructions to consider are all of the instructions in the range of [I1,I2]
203/// INCLUSIVE. I1 and I2 must be in the same basic block.
204///
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000205bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
206 const Instruction &I2,
Chris Lattner14ac8772003-02-26 19:26:51 +0000207 const Value *Ptr, unsigned Size) {
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000208 assert(I1.getParent() == I2.getParent() &&
209 "Instructions not in same basic block!");
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000210 BasicBlock::iterator I = const_cast<Instruction*>(&I1);
211 BasicBlock::iterator E = const_cast<Instruction*>(&I2);
212 ++E; // Convert from inclusive to exclusive range.
213
Chris Lattner14ac8772003-02-26 19:26:51 +0000214 for (; I != E; ++I) // Check every instruction in range
215 if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod)
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000216 return true;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000217 return false;
218}
219
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000220/// isNoAliasCall - Return true if this pointer is returned by a noalias
221/// function.
222bool llvm::isNoAliasCall(const Value *V) {
223 if (isa<CallInst>(V) || isa<InvokeInst>(V))
224 return CallSite(const_cast<Instruction*>(cast<Instruction>(V)))
225 .paramHasAttr(0, Attribute::NoAlias);
226 return false;
227}
228
229/// isIdentifiedObject - Return true if this pointer refers to a distinct and
230/// identifiable object. This returns true for:
231/// Global Variables and Functions
232/// Allocas and Mallocs
233/// ByVal and NoAlias Arguments
234/// NoAlias returns
235///
236bool llvm::isIdentifiedObject(const Value *V) {
237 if (isa<GlobalValue>(V) || isa<AllocationInst>(V) || isNoAliasCall(V))
238 return true;
239 if (const Argument *A = dyn_cast<Argument>(V))
240 return A->hasNoAliasAttr() || A->hasByValAttr();
241 return false;
242}
243
Chris Lattnerd501c132003-02-26 19:41:54 +0000244// Because of the way .a files work, we must force the BasicAA implementation to
245// be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run
246// the risk of AliasAnalysis being used, but the default implementation not
247// being linked into the tool that uses it.
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000248DEFINING_FILE_FOR(AliasAnalysis)