1 //===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
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,
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
25 //===----------------------------------------------------------------------===//
27 #include "llvm/Analysis/AliasAnalysis.h"
28 #include "llvm/Pass.h"
29 #include "llvm/BasicBlock.h"
30 #include "llvm/Function.h"
31 #include "llvm/IntrinsicInst.h"
32 #include "llvm/Instructions.h"
33 #include "llvm/LLVMContext.h"
34 #include "llvm/Type.h"
35 #include "llvm/Target/TargetData.h"
38 // Register the AliasAnalysis interface, providing a nice name to refer to.
39 INITIALIZE_ANALYSIS_GROUP(AliasAnalysis
, "Alias Analysis", NoAA
)
40 char AliasAnalysis::ID
= 0;
42 //===----------------------------------------------------------------------===//
43 // Default chaining methods
44 //===----------------------------------------------------------------------===//
46 AliasAnalysis::AliasResult
47 AliasAnalysis::alias(const Location
&LocA
, const Location
&LocB
) {
48 assert(AA
&& "AA didn't call InitializeAliasAnalysis in its run method!");
49 return AA
->alias(LocA
, LocB
);
52 bool AliasAnalysis::pointsToConstantMemory(const Location
&Loc
) {
53 assert(AA
&& "AA didn't call InitializeAliasAnalysis in its run method!");
54 return AA
->pointsToConstantMemory(Loc
);
57 void AliasAnalysis::deleteValue(Value
*V
) {
58 assert(AA
&& "AA didn't call InitializeAliasAnalysis in its run method!");
62 void AliasAnalysis::copyValue(Value
*From
, Value
*To
) {
63 assert(AA
&& "AA didn't call InitializeAliasAnalysis in its run method!");
64 AA
->copyValue(From
, To
);
67 AliasAnalysis::ModRefResult
68 AliasAnalysis::getModRefInfo(ImmutableCallSite CS
,
69 const Location
&Loc
) {
70 assert(AA
&& "AA didn't call InitializeAliasAnalysis in its run method!");
72 ModRefBehavior MRB
= getModRefBehavior(CS
);
73 if (MRB
== DoesNotAccessMemory
)
76 ModRefResult Mask
= ModRef
;
77 if (MRB
== OnlyReadsMemory
)
79 else if (MRB
== AliasAnalysis::AccessesArguments
) {
80 bool doesAlias
= false;
81 for (ImmutableCallSite::arg_iterator AI
= CS
.arg_begin(), AE
= CS
.arg_end();
83 if (!isNoAlias(Location(*AI
), Loc
)) {
92 // If Loc is a constant memory location, the call definitely could not
93 // modify the memory location.
94 if ((Mask
& Mod
) && pointsToConstantMemory(Loc
))
95 Mask
= ModRefResult(Mask
& ~Mod
);
97 // If this is the end of the chain, don't forward.
100 // Otherwise, fall back to the next AA in the chain. But we can merge
101 // in any mask we've managed to compute.
102 return ModRefResult(AA
->getModRefInfo(CS
, Loc
) & Mask
);
105 AliasAnalysis::ModRefResult
106 AliasAnalysis::getModRefInfo(ImmutableCallSite CS1
, ImmutableCallSite CS2
) {
107 assert(AA
&& "AA didn't call InitializeAliasAnalysis in its run method!");
109 // If CS1 or CS2 are readnone, they don't interact.
110 ModRefBehavior CS1B
= getModRefBehavior(CS1
);
111 if (CS1B
== DoesNotAccessMemory
) return NoModRef
;
113 ModRefBehavior CS2B
= getModRefBehavior(CS2
);
114 if (CS2B
== DoesNotAccessMemory
) return NoModRef
;
116 // If they both only read from memory, there is no dependence.
117 if (CS1B
== OnlyReadsMemory
&& CS2B
== OnlyReadsMemory
)
120 AliasAnalysis::ModRefResult Mask
= ModRef
;
122 // If CS1 only reads memory, the only dependence on CS2 can be
123 // from CS1 reading memory written by CS2.
124 if (CS1B
== OnlyReadsMemory
)
125 Mask
= ModRefResult(Mask
& Ref
);
127 // If CS2 only access memory through arguments, accumulate the mod/ref
128 // information from CS1's references to the memory referenced by
130 if (CS2B
== AccessesArguments
) {
131 AliasAnalysis::ModRefResult R
= NoModRef
;
132 for (ImmutableCallSite::arg_iterator
133 I
= CS2
.arg_begin(), E
= CS2
.arg_end(); I
!= E
; ++I
) {
134 R
= ModRefResult((R
| getModRefInfo(CS1
, *I
, UnknownSize
)) & Mask
);
141 // If CS1 only accesses memory through arguments, check if CS2 references
142 // any of the memory referenced by CS1's arguments. If not, return NoModRef.
143 if (CS1B
== AccessesArguments
) {
144 AliasAnalysis::ModRefResult R
= NoModRef
;
145 for (ImmutableCallSite::arg_iterator
146 I
= CS1
.arg_begin(), E
= CS1
.arg_end(); I
!= E
; ++I
)
147 if (getModRefInfo(CS2
, *I
, UnknownSize
) != NoModRef
) {
155 // If this is the end of the chain, don't forward.
156 if (!AA
) return Mask
;
158 // Otherwise, fall back to the next AA in the chain. But we can merge
159 // in any mask we've managed to compute.
160 return ModRefResult(AA
->getModRefInfo(CS1
, CS2
) & Mask
);
163 AliasAnalysis::ModRefBehavior
164 AliasAnalysis::getModRefBehavior(ImmutableCallSite CS
) {
165 assert(AA
&& "AA didn't call InitializeAliasAnalysis in its run method!");
167 ModRefBehavior Min
= UnknownModRefBehavior
;
169 // Call back into the alias analysis with the other form of getModRefBehavior
170 // to see if it can give a better response.
171 if (const Function
*F
= CS
.getCalledFunction())
172 Min
= getModRefBehavior(F
);
174 // If this is the end of the chain, don't forward.
177 // Otherwise, fall back to the next AA in the chain. But we can merge
178 // in any result we've managed to compute.
179 return std::min(AA
->getModRefBehavior(CS
), Min
);
182 AliasAnalysis::ModRefBehavior
183 AliasAnalysis::getModRefBehavior(const Function
*F
) {
184 assert(AA
&& "AA didn't call InitializeAliasAnalysis in its run method!");
185 return AA
->getModRefBehavior(F
);
188 //===----------------------------------------------------------------------===//
189 // AliasAnalysis non-virtual helper method implementation
190 //===----------------------------------------------------------------------===//
192 AliasAnalysis::ModRefResult
193 AliasAnalysis::getModRefInfo(const LoadInst
*L
, const Location
&Loc
) {
194 // Be conservative in the face of volatile.
198 // If the load address doesn't alias the given address, it doesn't read
199 // or write the specified memory.
200 if (!alias(Location(L
->getOperand(0),
201 getTypeStoreSize(L
->getType()),
202 L
->getMetadata(LLVMContext::MD_tbaa
)),
206 // Otherwise, a load just reads.
210 AliasAnalysis::ModRefResult
211 AliasAnalysis::getModRefInfo(const StoreInst
*S
, const Location
&Loc
) {
212 // Be conservative in the face of volatile.
216 // If the store address cannot alias the pointer in question, then the
217 // specified memory cannot be modified by the store.
218 if (!alias(Location(S
->getOperand(1),
219 getTypeStoreSize(S
->getOperand(0)->getType()),
220 S
->getMetadata(LLVMContext::MD_tbaa
)),
224 // If the pointer is a pointer to constant memory, then it could not have been
225 // modified by this store.
226 if (pointsToConstantMemory(Loc
))
229 // Otherwise, a store just writes.
233 AliasAnalysis::ModRefResult
234 AliasAnalysis::getModRefInfo(const VAArgInst
*V
, const Location
&Loc
) {
235 // If the va_arg address cannot alias the pointer in question, then the
236 // specified memory cannot be accessed by the va_arg.
237 if (!alias(Location(V
->getOperand(0),
239 V
->getMetadata(LLVMContext::MD_tbaa
)),
243 // If the pointer is a pointer to constant memory, then it could not have been
244 // modified by this va_arg.
245 if (pointsToConstantMemory(Loc
))
248 // Otherwise, a va_arg reads and writes.
252 AliasAnalysis::ModRefBehavior
253 AliasAnalysis::getIntrinsicModRefBehavior(unsigned iid
) {
254 #define GET_INTRINSIC_MODREF_BEHAVIOR
255 #include "llvm/Intrinsics.gen"
256 #undef GET_INTRINSIC_MODREF_BEHAVIOR
259 // AliasAnalysis destructor: DO NOT move this to the header file for
260 // AliasAnalysis or else clients of the AliasAnalysis class may not depend on
261 // the AliasAnalysis.o file in the current .a file, causing alias analysis
262 // support to not be included in the tool correctly!
264 AliasAnalysis::~AliasAnalysis() {}
266 /// InitializeAliasAnalysis - Subclasses must call this method to initialize the
267 /// AliasAnalysis interface before any other methods are called.
269 void AliasAnalysis::InitializeAliasAnalysis(Pass
*P
) {
270 TD
= P
->getAnalysisIfAvailable
<TargetData
>();
271 AA
= &P
->getAnalysis
<AliasAnalysis
>();
274 // getAnalysisUsage - All alias analysis implementations should invoke this
275 // directly (using AliasAnalysis::getAnalysisUsage(AU)).
276 void AliasAnalysis::getAnalysisUsage(AnalysisUsage
&AU
) const {
277 AU
.addRequired
<AliasAnalysis
>(); // All AA's chain
280 /// getTypeStoreSize - Return the TargetData store size for the given type,
281 /// if known, or a conservative value otherwise.
283 uint64_t AliasAnalysis::getTypeStoreSize(const Type
*Ty
) {
284 return TD
? TD
->getTypeStoreSize(Ty
) : UnknownSize
;
287 /// canBasicBlockModify - Return true if it is possible for execution of the
288 /// specified basic block to modify the value pointed to by Ptr.
290 bool AliasAnalysis::canBasicBlockModify(const BasicBlock
&BB
,
291 const Location
&Loc
) {
292 return canInstructionRangeModify(BB
.front(), BB
.back(), Loc
);
295 /// canInstructionRangeModify - Return true if it is possible for the execution
296 /// of the specified instructions to modify the value pointed to by Ptr. The
297 /// instructions to consider are all of the instructions in the range of [I1,I2]
298 /// INCLUSIVE. I1 and I2 must be in the same basic block.
300 bool AliasAnalysis::canInstructionRangeModify(const Instruction
&I1
,
301 const Instruction
&I2
,
302 const Location
&Loc
) {
303 assert(I1
.getParent() == I2
.getParent() &&
304 "Instructions not in same basic block!");
305 BasicBlock::const_iterator I
= &I1
;
306 BasicBlock::const_iterator E
= &I2
;
307 ++E
; // Convert from inclusive to exclusive range.
309 for (; I
!= E
; ++I
) // Check every instruction in range
310 if (getModRefInfo(I
, Loc
) & Mod
)
315 /// isNoAliasCall - Return true if this pointer is returned by a noalias
317 bool llvm::isNoAliasCall(const Value
*V
) {
318 if (isa
<CallInst
>(V
) || isa
<InvokeInst
>(V
))
319 return ImmutableCallSite(cast
<Instruction
>(V
))
320 .paramHasAttr(0, Attribute::NoAlias
);
324 /// isIdentifiedObject - Return true if this pointer refers to a distinct and
325 /// identifiable object. This returns true for:
326 /// Global Variables and Functions (but not Global Aliases)
327 /// Allocas and Mallocs
328 /// ByVal and NoAlias Arguments
331 bool llvm::isIdentifiedObject(const Value
*V
) {
332 if (isa
<AllocaInst
>(V
))
334 if (isa
<GlobalValue
>(V
) && !isa
<GlobalAlias
>(V
))
336 if (isNoAliasCall(V
))
338 if (const Argument
*A
= dyn_cast
<Argument
>(V
))
339 return A
->hasNoAliasAttr() || A
->hasByValAttr();