1 //===- Loads.cpp - Local load analysis ------------------------------------===//
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 defines simple local analyses for load instructions.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Analysis/Loads.h"
15 #include "llvm/Analysis/AliasAnalysis.h"
16 #include "llvm/Target/TargetData.h"
17 #include "llvm/GlobalAlias.h"
18 #include "llvm/GlobalVariable.h"
19 #include "llvm/IntrinsicInst.h"
20 #include "llvm/Operator.h"
23 /// AreEquivalentAddressValues - Test if A and B will obviously have the same
24 /// value. This includes recognizing that %t0 and %t1 will have the same
25 /// value in code like this:
26 /// %t0 = getelementptr \@a, 0, 3
27 /// store i32 0, i32* %t0
28 /// %t1 = getelementptr \@a, 0, 3
29 /// %t2 = load i32* %t1
31 static bool AreEquivalentAddressValues(const Value
*A
, const Value
*B
) {
32 // Test if the values are trivially equivalent.
33 if (A
== B
) return true;
35 // Test if the values come from identical arithmetic instructions.
36 // Use isIdenticalToWhenDefined instead of isIdenticalTo because
37 // this function is only used when one address use dominates the
38 // other, which means that they'll always either have the same
39 // value or one of them will have an undefined value.
40 if (isa
<BinaryOperator
>(A
) || isa
<CastInst
>(A
) ||
41 isa
<PHINode
>(A
) || isa
<GetElementPtrInst
>(A
))
42 if (const Instruction
*BI
= dyn_cast
<Instruction
>(B
))
43 if (cast
<Instruction
>(A
)->isIdenticalToWhenDefined(BI
))
46 // Otherwise they may not be equivalent.
50 /// getUnderlyingObjectWithOffset - Strip off up to MaxLookup GEPs and
51 /// bitcasts to get back to the underlying object being addressed, keeping
52 /// track of the offset in bytes from the GEPs relative to the result.
53 /// This is closely related to GetUnderlyingObject but is located
54 /// here to avoid making VMCore depend on TargetData.
55 static Value
*getUnderlyingObjectWithOffset(Value
*V
, const TargetData
*TD
,
57 unsigned MaxLookup
= 6) {
58 if (!V
->getType()->isPointerTy())
60 for (unsigned Count
= 0; MaxLookup
== 0 || Count
< MaxLookup
; ++Count
) {
61 if (GEPOperator
*GEP
= dyn_cast
<GEPOperator
>(V
)) {
62 if (!GEP
->hasAllConstantIndices())
64 SmallVector
<Value
*, 8> Indices(GEP
->op_begin() + 1, GEP
->op_end());
65 ByteOffset
+= TD
->getIndexedOffset(GEP
->getPointerOperandType(),
66 &Indices
[0], Indices
.size());
67 V
= GEP
->getPointerOperand();
68 } else if (Operator::getOpcode(V
) == Instruction::BitCast
) {
69 V
= cast
<Operator
>(V
)->getOperand(0);
70 } else if (GlobalAlias
*GA
= dyn_cast
<GlobalAlias
>(V
)) {
71 if (GA
->mayBeOverridden())
77 assert(V
->getType()->isPointerTy() && "Unexpected operand type!");
82 /// isSafeToLoadUnconditionally - Return true if we know that executing a load
83 /// from this value cannot trap. If it is not obviously safe to load from the
84 /// specified pointer, we do a quick local scan of the basic block containing
85 /// ScanFrom, to determine if the address is already accessed.
86 bool llvm::isSafeToLoadUnconditionally(Value
*V
, Instruction
*ScanFrom
,
87 unsigned Align
, const TargetData
*TD
) {
88 uint64_t ByteOffset
= 0;
91 Base
= getUnderlyingObjectWithOffset(V
, TD
, ByteOffset
);
93 const Type
*BaseType
= 0;
94 unsigned BaseAlign
= 0;
95 if (const AllocaInst
*AI
= dyn_cast
<AllocaInst
>(Base
)) {
96 // An alloca is safe to load from as load as it is suitably aligned.
97 BaseType
= AI
->getAllocatedType();
98 BaseAlign
= AI
->getAlignment();
99 } else if (const GlobalValue
*GV
= dyn_cast
<GlobalValue
>(Base
)) {
100 // Global variables are safe to load from but their size cannot be
101 // guaranteed if they are overridden.
102 if (!isa
<GlobalAlias
>(GV
) && !GV
->mayBeOverridden()) {
103 BaseType
= GV
->getType()->getElementType();
104 BaseAlign
= GV
->getAlignment();
108 if (BaseType
&& BaseType
->isSized()) {
109 if (TD
&& BaseAlign
== 0)
110 BaseAlign
= TD
->getPrefTypeAlignment(BaseType
);
112 if (Align
<= BaseAlign
) {
114 return true; // Loading directly from an alloca or global is OK.
116 // Check if the load is within the bounds of the underlying object.
117 const PointerType
*AddrTy
= cast
<PointerType
>(V
->getType());
118 uint64_t LoadSize
= TD
->getTypeStoreSize(AddrTy
->getElementType());
119 if (ByteOffset
+ LoadSize
<= TD
->getTypeAllocSize(BaseType
) &&
120 (Align
== 0 || (ByteOffset
% Align
) == 0))
125 // Otherwise, be a little bit aggressive by scanning the local block where we
126 // want to check to see if the pointer is already being loaded or stored
127 // from/to. If so, the previous load or store would have already trapped,
128 // so there is no harm doing an extra load (also, CSE will later eliminate
129 // the load entirely).
130 BasicBlock::iterator BBI
= ScanFrom
, E
= ScanFrom
->getParent()->begin();
135 // If we see a free or a call which may write to memory (i.e. which might do
136 // a free) the pointer could be marked invalid.
137 if (isa
<CallInst
>(BBI
) && BBI
->mayWriteToMemory() &&
138 !isa
<DbgInfoIntrinsic
>(BBI
))
141 if (LoadInst
*LI
= dyn_cast
<LoadInst
>(BBI
)) {
142 if (AreEquivalentAddressValues(LI
->getOperand(0), V
)) return true;
143 } else if (StoreInst
*SI
= dyn_cast
<StoreInst
>(BBI
)) {
144 if (AreEquivalentAddressValues(SI
->getOperand(1), V
)) return true;
150 /// FindAvailableLoadedValue - Scan the ScanBB block backwards (starting at the
151 /// instruction before ScanFrom) checking to see if we have the value at the
152 /// memory address *Ptr locally available within a small number of instructions.
153 /// If the value is available, return it.
155 /// If not, return the iterator for the last validated instruction that the
156 /// value would be live through. If we scanned the entire block and didn't find
157 /// something that invalidates *Ptr or provides it, ScanFrom would be left at
158 /// begin() and this returns null. ScanFrom could also be left
160 /// MaxInstsToScan specifies the maximum instructions to scan in the block. If
161 /// it is set to 0, it will scan the whole block. You can also optionally
162 /// specify an alias analysis implementation, which makes this more precise.
163 Value
*llvm::FindAvailableLoadedValue(Value
*Ptr
, BasicBlock
*ScanBB
,
164 BasicBlock::iterator
&ScanFrom
,
165 unsigned MaxInstsToScan
,
167 if (MaxInstsToScan
== 0) MaxInstsToScan
= ~0U;
169 // If we're using alias analysis to disambiguate get the size of *Ptr.
170 uint64_t AccessSize
= 0;
172 const Type
*AccessTy
= cast
<PointerType
>(Ptr
->getType())->getElementType();
173 AccessSize
= AA
->getTypeStoreSize(AccessTy
);
176 while (ScanFrom
!= ScanBB
->begin()) {
177 // We must ignore debug info directives when counting (otherwise they
178 // would affect codegen).
179 Instruction
*Inst
= --ScanFrom
;
180 if (isa
<DbgInfoIntrinsic
>(Inst
))
183 // Restore ScanFrom to expected value in case next test succeeds
186 // Don't scan huge blocks.
187 if (MaxInstsToScan
-- == 0) return 0;
190 // If this is a load of Ptr, the loaded value is available.
191 if (LoadInst
*LI
= dyn_cast
<LoadInst
>(Inst
))
192 if (AreEquivalentAddressValues(LI
->getOperand(0), Ptr
))
195 if (StoreInst
*SI
= dyn_cast
<StoreInst
>(Inst
)) {
196 // If this is a store through Ptr, the value is available!
197 if (AreEquivalentAddressValues(SI
->getOperand(1), Ptr
))
198 return SI
->getOperand(0);
200 // If Ptr is an alloca and this is a store to a different alloca, ignore
201 // the store. This is a trivial form of alias analysis that is important
202 // for reg2mem'd code.
203 if ((isa
<AllocaInst
>(Ptr
) || isa
<GlobalVariable
>(Ptr
)) &&
204 (isa
<AllocaInst
>(SI
->getOperand(1)) ||
205 isa
<GlobalVariable
>(SI
->getOperand(1))))
208 // If we have alias analysis and it says the store won't modify the loaded
209 // value, ignore the store.
211 (AA
->getModRefInfo(SI
, Ptr
, AccessSize
) & AliasAnalysis::Mod
) == 0)
214 // Otherwise the store that may or may not alias the pointer, bail out.
219 // If this is some other instruction that may clobber Ptr, bail out.
220 if (Inst
->mayWriteToMemory()) {
221 // If alias analysis claims that it really won't modify the load,
224 (AA
->getModRefInfo(Inst
, Ptr
, AccessSize
) & AliasAnalysis::Mod
) == 0)
227 // May modify the pointer, bail out.
233 // Got to the start of the block, we didn't find it, but are done for this