blob: 6dd79b47b9599ab51135f9696a4cd43d5df234b1 [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 Gohman6e3f2b62010-08-06 01:25:49 +000068AliasAnalysis::getModRefInfo(ImmutableCallSite CS,
69 const Value *P, unsigned Size) {
70 // Don't assert AA because BasicAA calls us in order to make use of the
71 // logic here.
72
73 ModRefBehavior MRB = getModRefBehavior(CS);
74 if (MRB == DoesNotAccessMemory)
75 return NoModRef;
76
77 ModRefResult Mask = ModRef;
78 if (MRB == OnlyReadsMemory)
79 Mask = Ref;
80 else if (MRB == AliasAnalysis::AccessesArguments) {
81 bool doesAlias = false;
82 for (ImmutableCallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
83 AI != AE; ++AI)
84 if (!isNoAlias(*AI, ~0U, P, Size)) {
85 doesAlias = true;
86 break;
87 }
88
89 if (!doesAlias)
90 return NoModRef;
91 }
92
93 // If P points to a constant memory location, the call definitely could not
94 // modify the memory location.
95 if ((Mask & Mod) && pointsToConstantMemory(P))
96 Mask = ModRefResult(Mask & ~Mod);
97
98 // If this is BasicAA, don't forward.
99 if (!AA) return Mask;
100
101 // Otherwise, fall back to the next AA in the chain. But we can merge
102 // in any mask we've managed to compute.
103 return ModRefResult(AA->getModRefInfo(CS, P, Size) & Mask);
104}
105
106AliasAnalysis::ModRefResult
Dan Gohman503e2042010-08-03 21:48:53 +0000107AliasAnalysis::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) {
Dan Gohman6e3f2b62010-08-06 01:25:49 +0000108 // Don't assert AA because BasicAA calls us in order to make use of the
109 // logic here.
110
111 // If CS1 or CS2 are readnone, they don't interact.
112 ModRefBehavior CS1B = getModRefBehavior(CS1);
113 if (CS1B == DoesNotAccessMemory) return NoModRef;
114
115 ModRefBehavior CS2B = getModRefBehavior(CS2);
116 if (CS2B == DoesNotAccessMemory) return NoModRef;
117
118 // If they both only read from memory, there is no dependence.
119 if (CS1B == OnlyReadsMemory && CS2B == OnlyReadsMemory)
120 return NoModRef;
121
122 AliasAnalysis::ModRefResult Mask = ModRef;
123
124 // If CS1 only reads memory, the only dependence on CS2 can be
125 // from CS1 reading memory written by CS2.
126 if (CS1B == OnlyReadsMemory)
127 Mask = ModRefResult(Mask & Ref);
128
129 // If CS2 only access memory through arguments, accumulate the mod/ref
130 // information from CS1's references to the memory referenced by
131 // CS2's arguments.
132 if (CS2B == AccessesArguments) {
133 AliasAnalysis::ModRefResult R = NoModRef;
134 for (ImmutableCallSite::arg_iterator
135 I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) {
136 R = ModRefResult((R | getModRefInfo(CS1, *I, UnknownSize)) & Mask);
137 if (R == Mask)
138 break;
139 }
140 return R;
141 }
142
143 // If CS1 only accesses memory through arguments, check if CS2 references
144 // any of the memory referenced by CS1's arguments. If not, return NoModRef.
145 if (CS1B == AccessesArguments) {
146 AliasAnalysis::ModRefResult R = NoModRef;
147 for (ImmutableCallSite::arg_iterator
148 I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I)
149 if (getModRefInfo(CS2, *I, UnknownSize) != NoModRef) {
150 R = Mask;
151 break;
152 }
153 if (R == NoModRef)
154 return R;
155 }
156
157 // If this is BasicAA, don't forward.
158 if (!AA) return Mask;
159
160 // Otherwise, fall back to the next AA in the chain. But we can merge
161 // in any mask we've managed to compute.
162 return ModRefResult(AA->getModRefInfo(CS1, CS2) & Mask);
163}
164
165AliasAnalysis::ModRefBehavior
166AliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
167 // Don't assert AA because BasicAA calls us in order to make use of the
168 // logic here.
169
170 ModRefBehavior Min = UnknownModRefBehavior;
171
172 // Call back into the alias analysis with the other form of getModRefBehavior
173 // to see if it can give a better response.
174 if (const Function *F = CS.getCalledFunction())
175 Min = getModRefBehavior(F);
176
177 // If this is BasicAA, don't forward.
178 if (!AA) return Min;
179
180 // Otherwise, fall back to the next AA in the chain. But we can merge
181 // in any result we've managed to compute.
182 return std::min(AA->getModRefBehavior(CS), Min);
183}
184
185AliasAnalysis::ModRefBehavior
186AliasAnalysis::getModRefBehavior(const Function *F) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman6e3f2b62010-08-06 01:25:49 +0000188 return AA->getModRefBehavior(F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189}
190
191
192//===----------------------------------------------------------------------===//
193// AliasAnalysis non-virtual helper method implementation
194//===----------------------------------------------------------------------===//
195
196AliasAnalysis::ModRefResult
Dan Gohman503e2042010-08-03 21:48:53 +0000197AliasAnalysis::getModRefInfo(const LoadInst *L, const Value *P, unsigned Size) {
Dan Gohman99247942010-08-03 17:27:43 +0000198 // If the load address doesn't alias the given address, it doesn't read
199 // or write the specified memory.
200 if (!alias(L->getOperand(0), getTypeStoreSize(L->getType()), P, Size))
201 return NoModRef;
202
203 // Be conservative in the face of volatile.
204 if (L->isVolatile())
205 return ModRef;
206
207 // Otherwise, a load just reads.
208 return Ref;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209}
210
211AliasAnalysis::ModRefResult
Dan Gohman503e2042010-08-03 21:48:53 +0000212AliasAnalysis::getModRefInfo(const StoreInst *S, const Value *P, unsigned Size) {
Dan Gohmanc9bfcba2010-08-06 18:10:45 +0000213 // If the store address cannot alias the pointer in question, then the
214 // specified memory cannot be modified by the store.
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000215 if (!alias(S->getOperand(1),
Dan Gohman624f9612009-07-25 00:48:42 +0000216 getTypeStoreSize(S->getOperand(0)->getType()), P, Size))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 return NoModRef;
218
Dan Gohman99247942010-08-03 17:27:43 +0000219 // Be conservative in the face of volatile.
220 if (S->isVolatile())
221 return ModRef;
222
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 // If the pointer is a pointer to constant memory, then it could not have been
224 // modified by this store.
Dan Gohman99247942010-08-03 17:27:43 +0000225 if (pointsToConstantMemory(P))
226 return NoModRef;
227
228 // Otherwise, a store just writes.
229 return Mod;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230}
231
Duncan Sands00b24b52007-12-01 07:51:45 +0000232AliasAnalysis::ModRefBehavior
Dan Gohman503e2042010-08-03 21:48:53 +0000233AliasAnalysis::getIntrinsicModRefBehavior(unsigned iid) {
Duncan Sands555ddcf2009-02-14 10:56:35 +0000234#define GET_INTRINSIC_MODREF_BEHAVIOR
235#include "llvm/Intrinsics.gen"
236#undef GET_INTRINSIC_MODREF_BEHAVIOR
Duncan Sands00b24b52007-12-01 07:51:45 +0000237}
238
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239// AliasAnalysis destructor: DO NOT move this to the header file for
240// AliasAnalysis or else clients of the AliasAnalysis class may not depend on
241// the AliasAnalysis.o file in the current .a file, causing alias analysis
242// support to not be included in the tool correctly!
243//
244AliasAnalysis::~AliasAnalysis() {}
245
Dan Gohman5284bb22008-05-30 00:02:02 +0000246/// InitializeAliasAnalysis - Subclasses must call this method to initialize the
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247/// AliasAnalysis interface before any other methods are called.
248///
249void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
Dan Gohman624f9612009-07-25 00:48:42 +0000250 TD = P->getAnalysisIfAvailable<TargetData>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 AA = &P->getAnalysis<AliasAnalysis>();
252}
253
254// getAnalysisUsage - All alias analysis implementations should invoke this
Dan Gohman624f9612009-07-25 00:48:42 +0000255// directly (using AliasAnalysis::getAnalysisUsage(AU)).
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 AU.addRequired<AliasAnalysis>(); // All AA's chain
258}
259
Dan Gohman624f9612009-07-25 00:48:42 +0000260/// getTypeStoreSize - Return the TargetData store size for the given type,
261/// if known, or a conservative value otherwise.
262///
263unsigned AliasAnalysis::getTypeStoreSize(const Type *Ty) {
264 return TD ? TD->getTypeStoreSize(Ty) : ~0u;
265}
266
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000267/// canBasicBlockModify - Return true if it is possible for execution of the
268/// specified basic block to modify the value pointed to by Ptr.
269///
270bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
271 const Value *Ptr, unsigned Size) {
272 return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size);
273}
274
275/// canInstructionRangeModify - Return true if it is possible for the execution
276/// of the specified instructions to modify the value pointed to by Ptr. The
277/// instructions to consider are all of the instructions in the range of [I1,I2]
278/// INCLUSIVE. I1 and I2 must be in the same basic block.
279///
280bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
281 const Instruction &I2,
282 const Value *Ptr, unsigned Size) {
283 assert(I1.getParent() == I2.getParent() &&
284 "Instructions not in same basic block!");
Dan Gohman503e2042010-08-03 21:48:53 +0000285 BasicBlock::const_iterator I = &I1;
286 BasicBlock::const_iterator E = &I2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 ++E; // Convert from inclusive to exclusive range.
288
289 for (; I != E; ++I) // Check every instruction in range
Dan Gohman503e2042010-08-03 21:48:53 +0000290 if (getModRefInfo(I, Ptr, Size) & Mod)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291 return true;
292 return false;
293}
294
Dan Gohmand4b0e322009-02-03 01:28:32 +0000295/// isNoAliasCall - Return true if this pointer is returned by a noalias
296/// function.
297bool llvm::isNoAliasCall(const Value *V) {
298 if (isa<CallInst>(V) || isa<InvokeInst>(V))
Dan Gohman503e2042010-08-03 21:48:53 +0000299 return ImmutableCallSite(cast<Instruction>(V))
Dan Gohmand4b0e322009-02-03 01:28:32 +0000300 .paramHasAttr(0, Attribute::NoAlias);
301 return false;
302}
303
304/// isIdentifiedObject - Return true if this pointer refers to a distinct and
305/// identifiable object. This returns true for:
Dan Gohman3c845512009-08-27 17:52:56 +0000306/// Global Variables and Functions (but not Global Aliases)
Dan Gohmand4b0e322009-02-03 01:28:32 +0000307/// Allocas and Mallocs
Dan Gohman83a01ad22010-07-07 14:27:09 +0000308/// ByVal and NoAlias Arguments
309/// NoAlias returns
Dan Gohmand4b0e322009-02-03 01:28:32 +0000310///
Dan Gohman83a01ad22010-07-07 14:27:09 +0000311bool llvm::isIdentifiedObject(const Value *V) {
Dan Gohmanf9e8abc2010-06-29 00:50:39 +0000312 if (isa<AllocaInst>(V))
Dan Gohman3c845512009-08-27 17:52:56 +0000313 return true;
314 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
Dan Gohmand4b0e322009-02-03 01:28:32 +0000315 return true;
Dan Gohman83a01ad22010-07-07 14:27:09 +0000316 if (isNoAliasCall(V))
317 return true;
318 if (const Argument *A = dyn_cast<Argument>(V))
319 return A->hasNoAliasAttr() || A->hasByValAttr();
Dan Gohmand4b0e322009-02-03 01:28:32 +0000320 return false;
321}
322
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000323// Because of the way .a files work, we must force the BasicAA implementation to
324// be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run
325// the risk of AliasAnalysis being used, but the default implementation not
326// being linked into the tool that uses it.
327DEFINING_FILE_FOR(AliasAnalysis)