blob: 2571492118b42356fa8cecc80b1afb2748704a15 [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 Lattner0af024c2004-12-15 07:22:13 +000062AliasAnalysis::ModRefBehavior
63AliasAnalysis::getModRefBehavior(Function *F, CallSite CS,
64 std::vector<PointerAccessInfo> *Info) {
Chris Lattner5a24d702004-05-23 21:15:48 +000065 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Chris Lattner0af024c2004-12-15 07:22:13 +000066 return AA->getModRefBehavior(F, CS, Info);
Chris Lattner5a24d702004-05-23 21:15:48 +000067}
68
69bool AliasAnalysis::hasNoModRefInfoForCalls() const {
70 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
71 return AA->hasNoModRefInfoForCalls();
72}
73
74void AliasAnalysis::deleteValue(Value *V) {
75 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
76 AA->deleteValue(V);
77}
78
79void AliasAnalysis::copyValue(Value *From, Value *To) {
80 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
81 AA->copyValue(From, To);
82}
83
84AliasAnalysis::ModRefResult
85AliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) {
86 // FIXME: we can do better.
87 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
88 return AA->getModRefInfo(CS1, CS2);
89}
90
91
92//===----------------------------------------------------------------------===//
93// AliasAnalysis non-virtual helper method implementation
94//===----------------------------------------------------------------------===//
95
Chris Lattner14ac8772003-02-26 19:26:51 +000096AliasAnalysis::ModRefResult
97AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) {
Duncan Sands514ab342007-11-01 20:53:16 +000098 return alias(L->getOperand(0), TD->getTypeStoreSize(L->getType()),
Chris Lattner14ac8772003-02-26 19:26:51 +000099 P, Size) ? Ref : NoModRef;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000100}
101
Chris Lattner14ac8772003-02-26 19:26:51 +0000102AliasAnalysis::ModRefResult
103AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) {
Chris Lattnerf4d904d2004-01-30 22:16:42 +0000104 // If the stored address cannot alias the pointer in question, then the
105 // pointer cannot be modified by the store.
Duncan Sands514ab342007-11-01 20:53:16 +0000106 if (!alias(S->getOperand(1),
107 TD->getTypeStoreSize(S->getOperand(0)->getType()), P, Size))
Chris Lattnerf4d904d2004-01-30 22:16:42 +0000108 return NoModRef;
109
110 // If the pointer is a pointer to constant memory, then it could not have been
111 // modified by this store.
112 return pointsToConstantMemory(P) ? NoModRef : Mod;
Chris Lattner14ac8772003-02-26 19:26:51 +0000113}
114
Duncan Sandsdff67102007-12-01 07:51:45 +0000115AliasAnalysis::ModRefBehavior
116AliasAnalysis::getModRefBehavior(CallSite CS,
117 std::vector<PointerAccessInfo> *Info) {
Owen Andersoncd895252009-02-03 06:27:22 +0000118 if (IntrinsicInst* II = dyn_cast<IntrinsicInst>(CS.getInstruction())) {
119 switch (II->getIntrinsicID()) {
120 case Intrinsic::atomic_cmp_swap:
121 case Intrinsic::atomic_load_add:
122 case Intrinsic::atomic_load_and:
123 case Intrinsic::atomic_load_max:
124 case Intrinsic::atomic_load_min:
125 case Intrinsic::atomic_load_nand:
126 case Intrinsic::atomic_load_or:
127 case Intrinsic::atomic_load_sub:
128 case Intrinsic::atomic_load_umax:
129 case Intrinsic::atomic_load_umin:
130 case Intrinsic::atomic_load_xor:
131 case Intrinsic::atomic_swap:
132 // CAS and related intrinsics only access their arguments.
133 return AliasAnalysis::AccessesArguments;
134 default:
135 break;
136 }
137 }
138
Duncan Sands7915cbe2007-12-12 16:01:40 +0000139 if (CS.doesNotAccessMemory())
Duncan Sandsdff67102007-12-01 07:51:45 +0000140 // Can't do better than this.
141 return DoesNotAccessMemory;
142 ModRefBehavior MRB = UnknownModRefBehavior;
143 if (Function *F = CS.getCalledFunction())
144 MRB = getModRefBehavior(F, CS, Info);
Duncan Sands7915cbe2007-12-12 16:01:40 +0000145 if (MRB != DoesNotAccessMemory && CS.onlyReadsMemory())
Duncan Sandsdff67102007-12-01 07:51:45 +0000146 return OnlyReadsMemory;
147 return MRB;
148}
149
150AliasAnalysis::ModRefBehavior
151AliasAnalysis::getModRefBehavior(Function *F,
152 std::vector<PointerAccessInfo> *Info) {
Duncan Sands7915cbe2007-12-12 16:01:40 +0000153 if (F->doesNotAccessMemory())
Duncan Sandsdff67102007-12-01 07:51:45 +0000154 // Can't do better than this.
155 return DoesNotAccessMemory;
156 ModRefBehavior MRB = getModRefBehavior(F, CallSite(), Info);
Duncan Sands7915cbe2007-12-12 16:01:40 +0000157 if (MRB != DoesNotAccessMemory && F->onlyReadsMemory())
Duncan Sandsdff67102007-12-01 07:51:45 +0000158 return OnlyReadsMemory;
159 return MRB;
160}
161
Chris Lattner992860c2004-03-15 04:07:29 +0000162AliasAnalysis::ModRefResult
163AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
Chris Lattner5a24d702004-05-23 21:15:48 +0000164 ModRefResult Mask = ModRef;
Duncan Sandsdff67102007-12-01 07:51:45 +0000165 ModRefBehavior MRB = getModRefBehavior(CS);
166 if (MRB == OnlyReadsMemory)
167 Mask = Ref;
168 else if (MRB == DoesNotAccessMemory)
169 return NoModRef;
Chris Lattner992860c2004-03-15 04:07:29 +0000170
Chris Lattner5a24d702004-05-23 21:15:48 +0000171 if (!AA) return Mask;
172
Chris Lattner992860c2004-03-15 04:07:29 +0000173 // If P points to a constant memory location, the call definitely could not
174 // modify the memory location.
Chris Lattner5a24d702004-05-23 21:15:48 +0000175 if ((Mask & Mod) && AA->pointsToConstantMemory(P))
Chris Lattnerd433bde2005-03-23 22:06:41 +0000176 Mask = ModRefResult(Mask & ~Mod);
Chris Lattner992860c2004-03-15 04:07:29 +0000177
Chris Lattner5a24d702004-05-23 21:15:48 +0000178 return ModRefResult(Mask & AA->getModRefInfo(CS, P, Size));
Chris Lattner992860c2004-03-15 04:07:29 +0000179}
180
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000181// AliasAnalysis destructor: DO NOT move this to the header file for
182// AliasAnalysis or else clients of the AliasAnalysis class may not depend on
183// the AliasAnalysis.o file in the current .a file, causing alias analysis
184// support to not be included in the tool correctly!
185//
186AliasAnalysis::~AliasAnalysis() {}
187
Dan Gohman5a56bf62008-05-30 00:02:02 +0000188/// InitializeAliasAnalysis - Subclasses must call this method to initialize the
Chris Lattner14ac8772003-02-26 19:26:51 +0000189/// AliasAnalysis interface before any other methods are called.
190///
191void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
192 TD = &P->getAnalysis<TargetData>();
Chris Lattner5a24d702004-05-23 21:15:48 +0000193 AA = &P->getAnalysis<AliasAnalysis>();
Chris Lattner14ac8772003-02-26 19:26:51 +0000194}
195
196// getAnalysisUsage - All alias analysis implementations should invoke this
197// directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that
198// TargetData is required by the pass.
199void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
200 AU.addRequired<TargetData>(); // All AA's need TargetData.
Chris Lattner5a24d702004-05-23 21:15:48 +0000201 AU.addRequired<AliasAnalysis>(); // All AA's chain
Chris Lattner14ac8772003-02-26 19:26:51 +0000202}
203
Chris Lattnerf9355f62002-08-22 22:46:39 +0000204/// canBasicBlockModify - Return true if it is possible for execution of the
205/// specified basic block to modify the value pointed to by Ptr.
206///
Chris Lattner14ac8772003-02-26 19:26:51 +0000207bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
208 const Value *Ptr, unsigned Size) {
209 return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size);
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000210}
211
Chris Lattnerf9355f62002-08-22 22:46:39 +0000212/// canInstructionRangeModify - Return true if it is possible for the execution
213/// of the specified instructions to modify the value pointed to by Ptr. The
214/// instructions to consider are all of the instructions in the range of [I1,I2]
215/// INCLUSIVE. I1 and I2 must be in the same basic block.
216///
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000217bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
218 const Instruction &I2,
Chris Lattner14ac8772003-02-26 19:26:51 +0000219 const Value *Ptr, unsigned Size) {
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000220 assert(I1.getParent() == I2.getParent() &&
221 "Instructions not in same basic block!");
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000222 BasicBlock::iterator I = const_cast<Instruction*>(&I1);
223 BasicBlock::iterator E = const_cast<Instruction*>(&I2);
224 ++E; // Convert from inclusive to exclusive range.
225
Chris Lattner14ac8772003-02-26 19:26:51 +0000226 for (; I != E; ++I) // Check every instruction in range
227 if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod)
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000228 return true;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000229 return false;
230}
231
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000232/// isNoAliasCall - Return true if this pointer is returned by a noalias
233/// function.
234bool llvm::isNoAliasCall(const Value *V) {
235 if (isa<CallInst>(V) || isa<InvokeInst>(V))
236 return CallSite(const_cast<Instruction*>(cast<Instruction>(V)))
237 .paramHasAttr(0, Attribute::NoAlias);
238 return false;
239}
240
241/// isIdentifiedObject - Return true if this pointer refers to a distinct and
242/// identifiable object. This returns true for:
243/// Global Variables and Functions
244/// Allocas and Mallocs
245/// ByVal and NoAlias Arguments
246/// NoAlias returns
247///
248bool llvm::isIdentifiedObject(const Value *V) {
249 if (isa<GlobalValue>(V) || isa<AllocationInst>(V) || isNoAliasCall(V))
250 return true;
251 if (const Argument *A = dyn_cast<Argument>(V))
252 return A->hasNoAliasAttr() || A->hasByValAttr();
253 return false;
254}
255
Chris Lattnerd501c132003-02-26 19:41:54 +0000256// Because of the way .a files work, we must force the BasicAA implementation to
257// be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run
258// the risk of AliasAnalysis being used, but the default implementation not
259// being linked into the tool that uses it.
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000260DEFINING_FILE_FOR(AliasAnalysis)