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
,
54 assert(AA
&& "AA didn't call InitializeAliasAnalysis in its run method!");
55 return AA
->pointsToConstantMemory(Loc
, OrLocal
);
58 void AliasAnalysis::deleteValue(Value
*V
) {
59 assert(AA
&& "AA didn't call InitializeAliasAnalysis in its run method!");
63 void AliasAnalysis::copyValue(Value
*From
, Value
*To
) {
64 assert(AA
&& "AA didn't call InitializeAliasAnalysis in its run method!");
65 AA
->copyValue(From
, To
);
68 void AliasAnalysis::addEscapingUse(Use
&U
) {
69 assert(AA
&& "AA didn't call InitializeAliasAnalysis in its run method!");
70 AA
->addEscapingUse(U
);
74 AliasAnalysis::ModRefResult
75 AliasAnalysis::getModRefInfo(ImmutableCallSite CS
,
76 const Location
&Loc
) {
77 assert(AA
&& "AA didn't call InitializeAliasAnalysis in its run method!");
79 ModRefBehavior MRB
= getModRefBehavior(CS
);
80 if (MRB
== DoesNotAccessMemory
)
83 ModRefResult Mask
= ModRef
;
84 if (onlyReadsMemory(MRB
))
87 if (onlyAccessesArgPointees(MRB
)) {
88 bool doesAlias
= false;
89 if (doesAccessArgPointees(MRB
))
90 for (ImmutableCallSite::arg_iterator AI
= CS
.arg_begin(), AE
= CS
.arg_end();
92 if (!isNoAlias(Location(*AI
), Loc
)) {
101 // If Loc is a constant memory location, the call definitely could not
102 // modify the memory location.
103 if ((Mask
& Mod
) && pointsToConstantMemory(Loc
))
104 Mask
= ModRefResult(Mask
& ~Mod
);
106 // If this is the end of the chain, don't forward.
107 if (!AA
) return Mask
;
109 // Otherwise, fall back to the next AA in the chain. But we can merge
110 // in any mask we've managed to compute.
111 return ModRefResult(AA
->getModRefInfo(CS
, Loc
) & Mask
);
114 AliasAnalysis::ModRefResult
115 AliasAnalysis::getModRefInfo(ImmutableCallSite CS1
, ImmutableCallSite CS2
) {
116 assert(AA
&& "AA didn't call InitializeAliasAnalysis in its run method!");
118 // If CS1 or CS2 are readnone, they don't interact.
119 ModRefBehavior CS1B
= getModRefBehavior(CS1
);
120 if (CS1B
== DoesNotAccessMemory
) return NoModRef
;
122 ModRefBehavior CS2B
= getModRefBehavior(CS2
);
123 if (CS2B
== DoesNotAccessMemory
) return NoModRef
;
125 // If they both only read from memory, there is no dependence.
126 if (onlyReadsMemory(CS1B
) && onlyReadsMemory(CS2B
))
129 AliasAnalysis::ModRefResult Mask
= ModRef
;
131 // If CS1 only reads memory, the only dependence on CS2 can be
132 // from CS1 reading memory written by CS2.
133 if (onlyReadsMemory(CS1B
))
134 Mask
= ModRefResult(Mask
& Ref
);
136 // If CS2 only access memory through arguments, accumulate the mod/ref
137 // information from CS1's references to the memory referenced by
139 if (onlyAccessesArgPointees(CS2B
)) {
140 AliasAnalysis::ModRefResult R
= NoModRef
;
141 if (doesAccessArgPointees(CS2B
))
142 for (ImmutableCallSite::arg_iterator
143 I
= CS2
.arg_begin(), E
= CS2
.arg_end(); I
!= E
; ++I
) {
144 R
= ModRefResult((R
| getModRefInfo(CS1
, *I
, UnknownSize
)) & Mask
);
151 // If CS1 only accesses memory through arguments, check if CS2 references
152 // any of the memory referenced by CS1's arguments. If not, return NoModRef.
153 if (onlyAccessesArgPointees(CS1B
)) {
154 AliasAnalysis::ModRefResult R
= NoModRef
;
155 if (doesAccessArgPointees(CS1B
))
156 for (ImmutableCallSite::arg_iterator
157 I
= CS1
.arg_begin(), E
= CS1
.arg_end(); I
!= E
; ++I
)
158 if (getModRefInfo(CS2
, *I
, UnknownSize
) != NoModRef
) {
166 // If this is the end of the chain, don't forward.
167 if (!AA
) return Mask
;
169 // Otherwise, fall back to the next AA in the chain. But we can merge
170 // in any mask we've managed to compute.
171 return ModRefResult(AA
->getModRefInfo(CS1
, CS2
) & Mask
);
174 AliasAnalysis::ModRefBehavior
175 AliasAnalysis::getModRefBehavior(ImmutableCallSite CS
) {
176 assert(AA
&& "AA didn't call InitializeAliasAnalysis in its run method!");
178 ModRefBehavior Min
= UnknownModRefBehavior
;
180 // Call back into the alias analysis with the other form of getModRefBehavior
181 // to see if it can give a better response.
182 if (const Function
*F
= CS
.getCalledFunction())
183 Min
= getModRefBehavior(F
);
185 // If this is the end of the chain, don't forward.
188 // Otherwise, fall back to the next AA in the chain. But we can merge
189 // in any result we've managed to compute.
190 return ModRefBehavior(AA
->getModRefBehavior(CS
) & Min
);
193 AliasAnalysis::ModRefBehavior
194 AliasAnalysis::getModRefBehavior(const Function
*F
) {
195 assert(AA
&& "AA didn't call InitializeAliasAnalysis in its run method!");
196 return AA
->getModRefBehavior(F
);
199 //===----------------------------------------------------------------------===//
200 // AliasAnalysis non-virtual helper method implementation
201 //===----------------------------------------------------------------------===//
203 AliasAnalysis::Location
AliasAnalysis::getLocation(const LoadInst
*LI
) {
204 return Location(LI
->getPointerOperand(),
205 getTypeStoreSize(LI
->getType()),
206 LI
->getMetadata(LLVMContext::MD_tbaa
));
209 AliasAnalysis::Location
AliasAnalysis::getLocation(const StoreInst
*SI
) {
210 return Location(SI
->getPointerOperand(),
211 getTypeStoreSize(SI
->getValueOperand()->getType()),
212 SI
->getMetadata(LLVMContext::MD_tbaa
));
215 AliasAnalysis::Location
AliasAnalysis::getLocation(const VAArgInst
*VI
) {
216 return Location(VI
->getPointerOperand(),
218 VI
->getMetadata(LLVMContext::MD_tbaa
));
222 AliasAnalysis::Location
223 AliasAnalysis::getLocationForSource(const MemTransferInst
*MTI
) {
224 uint64_t Size
= UnknownSize
;
225 if (ConstantInt
*C
= dyn_cast
<ConstantInt
>(MTI
->getLength()))
226 Size
= C
->getValue().getZExtValue();
228 // memcpy/memmove can have TBAA tags. For memcpy, they apply
229 // to both the source and the destination.
230 MDNode
*TBAATag
= MTI
->getMetadata(LLVMContext::MD_tbaa
);
232 return Location(MTI
->getRawSource(), Size
, TBAATag
);
235 AliasAnalysis::Location
236 AliasAnalysis::getLocationForDest(const MemIntrinsic
*MTI
) {
237 uint64_t Size
= UnknownSize
;
238 if (ConstantInt
*C
= dyn_cast
<ConstantInt
>(MTI
->getLength()))
239 Size
= C
->getValue().getZExtValue();
241 // memcpy/memmove can have TBAA tags. For memcpy, they apply
242 // to both the source and the destination.
243 MDNode
*TBAATag
= MTI
->getMetadata(LLVMContext::MD_tbaa
);
245 return Location(MTI
->getRawDest(), Size
, TBAATag
);
250 AliasAnalysis::ModRefResult
251 AliasAnalysis::getModRefInfo(const LoadInst
*L
, const Location
&Loc
) {
252 // Be conservative in the face of volatile.
256 // If the load address doesn't alias the given address, it doesn't read
257 // or write the specified memory.
258 if (!alias(getLocation(L
), Loc
))
261 // Otherwise, a load just reads.
265 AliasAnalysis::ModRefResult
266 AliasAnalysis::getModRefInfo(const StoreInst
*S
, const Location
&Loc
) {
267 // Be conservative in the face of volatile.
271 // If the store address cannot alias the pointer in question, then the
272 // specified memory cannot be modified by the store.
273 if (!alias(getLocation(S
), Loc
))
276 // If the pointer is a pointer to constant memory, then it could not have been
277 // modified by this store.
278 if (pointsToConstantMemory(Loc
))
281 // Otherwise, a store just writes.
285 AliasAnalysis::ModRefResult
286 AliasAnalysis::getModRefInfo(const VAArgInst
*V
, const Location
&Loc
) {
287 // If the va_arg address cannot alias the pointer in question, then the
288 // specified memory cannot be accessed by the va_arg.
289 if (!alias(getLocation(V
), Loc
))
292 // If the pointer is a pointer to constant memory, then it could not have been
293 // modified by this va_arg.
294 if (pointsToConstantMemory(Loc
))
297 // Otherwise, a va_arg reads and writes.
301 // AliasAnalysis destructor: DO NOT move this to the header file for
302 // AliasAnalysis or else clients of the AliasAnalysis class may not depend on
303 // the AliasAnalysis.o file in the current .a file, causing alias analysis
304 // support to not be included in the tool correctly!
306 AliasAnalysis::~AliasAnalysis() {}
308 /// InitializeAliasAnalysis - Subclasses must call this method to initialize the
309 /// AliasAnalysis interface before any other methods are called.
311 void AliasAnalysis::InitializeAliasAnalysis(Pass
*P
) {
312 TD
= P
->getAnalysisIfAvailable
<TargetData
>();
313 AA
= &P
->getAnalysis
<AliasAnalysis
>();
316 // getAnalysisUsage - All alias analysis implementations should invoke this
317 // directly (using AliasAnalysis::getAnalysisUsage(AU)).
318 void AliasAnalysis::getAnalysisUsage(AnalysisUsage
&AU
) const {
319 AU
.addRequired
<AliasAnalysis
>(); // All AA's chain
322 /// getTypeStoreSize - Return the TargetData store size for the given type,
323 /// if known, or a conservative value otherwise.
325 uint64_t AliasAnalysis::getTypeStoreSize(const Type
*Ty
) {
326 return TD
? TD
->getTypeStoreSize(Ty
) : UnknownSize
;
329 /// canBasicBlockModify - Return true if it is possible for execution of the
330 /// specified basic block to modify the value pointed to by Ptr.
332 bool AliasAnalysis::canBasicBlockModify(const BasicBlock
&BB
,
333 const Location
&Loc
) {
334 return canInstructionRangeModify(BB
.front(), BB
.back(), Loc
);
337 /// canInstructionRangeModify - Return true if it is possible for the execution
338 /// of the specified instructions to modify the value pointed to by Ptr. The
339 /// instructions to consider are all of the instructions in the range of [I1,I2]
340 /// INCLUSIVE. I1 and I2 must be in the same basic block.
342 bool AliasAnalysis::canInstructionRangeModify(const Instruction
&I1
,
343 const Instruction
&I2
,
344 const Location
&Loc
) {
345 assert(I1
.getParent() == I2
.getParent() &&
346 "Instructions not in same basic block!");
347 BasicBlock::const_iterator I
= &I1
;
348 BasicBlock::const_iterator E
= &I2
;
349 ++E
; // Convert from inclusive to exclusive range.
351 for (; I
!= E
; ++I
) // Check every instruction in range
352 if (getModRefInfo(I
, Loc
) & Mod
)
357 /// isNoAliasCall - Return true if this pointer is returned by a noalias
359 bool llvm::isNoAliasCall(const Value
*V
) {
360 if (isa
<CallInst
>(V
) || isa
<InvokeInst
>(V
))
361 return ImmutableCallSite(cast
<Instruction
>(V
))
362 .paramHasAttr(0, Attribute::NoAlias
);
366 /// isIdentifiedObject - Return true if this pointer refers to a distinct and
367 /// identifiable object. This returns true for:
368 /// Global Variables and Functions (but not Global Aliases)
369 /// Allocas and Mallocs
370 /// ByVal and NoAlias Arguments
373 bool llvm::isIdentifiedObject(const Value
*V
) {
374 if (isa
<AllocaInst
>(V
))
376 if (isa
<GlobalValue
>(V
) && !isa
<GlobalAlias
>(V
))
378 if (isNoAliasCall(V
))
380 if (const Argument
*A
= dyn_cast
<Argument
>(V
))
381 return A
->hasNoAliasAttr() || A
->hasByValAttr();