blob: dee9b535871a0c02d397a48b7fafb69e6f8a188f [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
Chris Lattner5a24d702004-05-23 21:15:48 +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
Chris Lattner5a24d702004-05-23 21:15:48 +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
68AliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) {
69 // FIXME: we can do better.
70 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
71 return AA->getModRefInfo(CS1, CS2);
72}
73
74
75//===----------------------------------------------------------------------===//
76// AliasAnalysis non-virtual helper method implementation
77//===----------------------------------------------------------------------===//
78
Chris Lattner14ac8772003-02-26 19:26:51 +000079AliasAnalysis::ModRefResult
80AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) {
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +000081 return alias(L->getOperand(0), getTypeStoreSize(L->getType()),
Chris Lattner14ac8772003-02-26 19:26:51 +000082 P, Size) ? Ref : NoModRef;
Chris Lattner53ad0ed2002-08-22 18:25:32 +000083}
84
Chris Lattner14ac8772003-02-26 19:26:51 +000085AliasAnalysis::ModRefResult
86AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) {
Chris Lattnerf4d904d2004-01-30 22:16:42 +000087 // If the stored address cannot alias the pointer in question, then the
88 // pointer cannot be modified by the store.
Duncan Sands514ab342007-11-01 20:53:16 +000089 if (!alias(S->getOperand(1),
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +000090 getTypeStoreSize(S->getOperand(0)->getType()), P, Size))
Chris Lattnerf4d904d2004-01-30 22:16:42 +000091 return NoModRef;
92
93 // If the pointer is a pointer to constant memory, then it could not have been
94 // modified by this store.
95 return pointsToConstantMemory(P) ? NoModRef : Mod;
Chris Lattner14ac8772003-02-26 19:26:51 +000096}
97
Duncan Sandsdff67102007-12-01 07:51:45 +000098AliasAnalysis::ModRefBehavior
99AliasAnalysis::getModRefBehavior(CallSite CS,
100 std::vector<PointerAccessInfo> *Info) {
Duncan Sands7915cbe2007-12-12 16:01:40 +0000101 if (CS.doesNotAccessMemory())
Duncan Sandsdff67102007-12-01 07:51:45 +0000102 // Can't do better than this.
103 return DoesNotAccessMemory;
Owen Andersone7942202009-02-05 23:36:27 +0000104 ModRefBehavior MRB = getModRefBehavior(CS.getCalledFunction(), Info);
Duncan Sands7915cbe2007-12-12 16:01:40 +0000105 if (MRB != DoesNotAccessMemory && CS.onlyReadsMemory())
Duncan Sandsdff67102007-12-01 07:51:45 +0000106 return OnlyReadsMemory;
107 return MRB;
108}
109
110AliasAnalysis::ModRefBehavior
111AliasAnalysis::getModRefBehavior(Function *F,
112 std::vector<PointerAccessInfo> *Info) {
Duncan Sandsddd8c452009-02-13 17:32:26 +0000113 if (F) {
114 if (F->doesNotAccessMemory())
115 // Can't do better than this.
116 return DoesNotAccessMemory;
Duncan Sandsd869b382009-02-14 10:56:35 +0000117 if (F->onlyReadsMemory())
Duncan Sandsddd8c452009-02-13 17:32:26 +0000118 return OnlyReadsMemory;
Duncan Sandsd869b382009-02-14 10:56:35 +0000119 if (unsigned id = F->getIntrinsicID()) {
120#define GET_INTRINSIC_MODREF_BEHAVIOR
121#include "llvm/Intrinsics.gen"
122#undef GET_INTRINSIC_MODREF_BEHAVIOR
123 }
Duncan Sandsddd8c452009-02-13 17:32:26 +0000124 }
Owen Andersone7942202009-02-05 23:36:27 +0000125 return UnknownModRefBehavior;
Duncan Sandsdff67102007-12-01 07:51:45 +0000126}
127
Chris Lattner992860c2004-03-15 04:07:29 +0000128AliasAnalysis::ModRefResult
129AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
Duncan Sandsdff67102007-12-01 07:51:45 +0000130 ModRefBehavior MRB = getModRefBehavior(CS);
Duncan Sandsddd8c452009-02-13 17:32:26 +0000131 if (MRB == DoesNotAccessMemory)
Duncan Sandsdff67102007-12-01 07:51:45 +0000132 return NoModRef;
Chris Lattner403ac2e2009-11-23 16:46:41 +0000133
134 ModRefResult Mask = ModRef;
135 if (MRB == OnlyReadsMemory)
Duncan Sandsddd8c452009-02-13 17:32:26 +0000136 Mask = Ref;
Owen Andersone7942202009-02-05 23:36:27 +0000137 else if (MRB == AliasAnalysis::AccessesArguments) {
138 bool doesAlias = false;
139 for (CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
140 AI != AE; ++AI)
Chris Lattner403ac2e2009-11-23 16:46:41 +0000141 if (!isNoAlias(*AI, ~0U, P, Size)) {
Owen Andersone7942202009-02-05 23:36:27 +0000142 doesAlias = true;
143 break;
144 }
Duncan Sandsddd8c452009-02-13 17:32:26 +0000145
Owen Andersone7942202009-02-05 23:36:27 +0000146 if (!doesAlias)
147 return NoModRef;
148 }
Chris Lattner992860c2004-03-15 04:07:29 +0000149
Chris Lattner5a24d702004-05-23 21:15:48 +0000150 if (!AA) return Mask;
151
Chris Lattner992860c2004-03-15 04:07:29 +0000152 // If P points to a constant memory location, the call definitely could not
153 // modify the memory location.
Chris Lattner5a24d702004-05-23 21:15:48 +0000154 if ((Mask & Mod) && AA->pointsToConstantMemory(P))
Chris Lattnerd433bde2005-03-23 22:06:41 +0000155 Mask = ModRefResult(Mask & ~Mod);
Chris Lattner992860c2004-03-15 04:07:29 +0000156
Chris Lattner5a24d702004-05-23 21:15:48 +0000157 return ModRefResult(Mask & AA->getModRefInfo(CS, P, Size));
Chris Lattner992860c2004-03-15 04:07:29 +0000158}
159
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000160// AliasAnalysis destructor: DO NOT move this to the header file for
161// AliasAnalysis or else clients of the AliasAnalysis class may not depend on
162// the AliasAnalysis.o file in the current .a file, causing alias analysis
163// support to not be included in the tool correctly!
164//
165AliasAnalysis::~AliasAnalysis() {}
166
Dan Gohman5a56bf62008-05-30 00:02:02 +0000167/// InitializeAliasAnalysis - Subclasses must call this method to initialize the
Chris Lattner14ac8772003-02-26 19:26:51 +0000168/// AliasAnalysis interface before any other methods are called.
169///
170void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000171 TD = P->getAnalysisIfAvailable<TargetData>();
Chris Lattner5a24d702004-05-23 21:15:48 +0000172 AA = &P->getAnalysis<AliasAnalysis>();
Chris Lattner14ac8772003-02-26 19:26:51 +0000173}
174
175// getAnalysisUsage - All alias analysis implementations should invoke this
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000176// directly (using AliasAnalysis::getAnalysisUsage(AU)).
Chris Lattner14ac8772003-02-26 19:26:51 +0000177void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5a24d702004-05-23 21:15:48 +0000178 AU.addRequired<AliasAnalysis>(); // All AA's chain
Chris Lattner14ac8772003-02-26 19:26:51 +0000179}
180
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000181/// getTypeStoreSize - Return the TargetData store size for the given type,
182/// if known, or a conservative value otherwise.
183///
184unsigned AliasAnalysis::getTypeStoreSize(const Type *Ty) {
185 return TD ? TD->getTypeStoreSize(Ty) : ~0u;
186}
187
Chris Lattnerf9355f62002-08-22 22:46:39 +0000188/// canBasicBlockModify - Return true if it is possible for execution of the
189/// specified basic block to modify the value pointed to by Ptr.
190///
Chris Lattner14ac8772003-02-26 19:26:51 +0000191bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
192 const Value *Ptr, unsigned Size) {
193 return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size);
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000194}
195
Chris Lattnerf9355f62002-08-22 22:46:39 +0000196/// canInstructionRangeModify - Return true if it is possible for the execution
197/// of the specified instructions to modify the value pointed to by Ptr. The
198/// instructions to consider are all of the instructions in the range of [I1,I2]
199/// INCLUSIVE. I1 and I2 must be in the same basic block.
200///
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000201bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
202 const Instruction &I2,
Chris Lattner14ac8772003-02-26 19:26:51 +0000203 const Value *Ptr, unsigned Size) {
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000204 assert(I1.getParent() == I2.getParent() &&
205 "Instructions not in same basic block!");
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000206 BasicBlock::iterator I = const_cast<Instruction*>(&I1);
207 BasicBlock::iterator E = const_cast<Instruction*>(&I2);
208 ++E; // Convert from inclusive to exclusive range.
209
Chris Lattner14ac8772003-02-26 19:26:51 +0000210 for (; I != E; ++I) // Check every instruction in range
211 if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod)
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000212 return true;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000213 return false;
214}
215
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000216/// isNoAliasCall - Return true if this pointer is returned by a noalias
217/// function.
218bool llvm::isNoAliasCall(const Value *V) {
219 if (isa<CallInst>(V) || isa<InvokeInst>(V))
220 return CallSite(const_cast<Instruction*>(cast<Instruction>(V)))
221 .paramHasAttr(0, Attribute::NoAlias);
222 return false;
223}
224
225/// isIdentifiedObject - Return true if this pointer refers to a distinct and
226/// identifiable object. This returns true for:
Dan Gohman5753a4a2009-08-27 17:52:56 +0000227/// Global Variables and Functions (but not Global Aliases)
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000228/// Allocas and Mallocs
229/// ByVal and NoAlias Arguments
230/// NoAlias returns
231///
232bool llvm::isIdentifiedObject(const Value *V) {
Victor Hernandez7b929da2009-10-23 21:09:37 +0000233 if (isa<AllocaInst>(V) || isNoAliasCall(V))
Dan Gohman5753a4a2009-08-27 17:52:56 +0000234 return true;
235 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000236 return true;
237 if (const Argument *A = dyn_cast<Argument>(V))
238 return A->hasNoAliasAttr() || A->hasByValAttr();
239 return false;
240}
241
Chris Lattnerd501c132003-02-26 19:41:54 +0000242// Because of the way .a files work, we must force the BasicAA implementation to
243// be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run
244// the risk of AliasAnalysis being used, but the default implementation not
245// being linked into the tool that uses it.
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000246DEFINING_FILE_FOR(AliasAnalysis)