1 //==- llvm/CodeGen/SelectionDAGAddressAnalysis.cpp - DAG Address Analysis --==//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/CodeGen/SelectionDAGAddressAnalysis.h"
10 #include "llvm/CodeGen/ISDOpcodes.h"
11 #include "llvm/CodeGen/MachineFrameInfo.h"
12 #include "llvm/CodeGen/MachineFunction.h"
13 #include "llvm/CodeGen/SelectionDAG.h"
14 #include "llvm/CodeGen/SelectionDAGNodes.h"
15 #include "llvm/CodeGen/TargetLowering.h"
16 #include "llvm/Support/Casting.h"
17 #include "llvm/Support/Debug.h"
22 bool BaseIndexOffset::equalBaseIndex(const BaseIndexOffset
&Other
,
23 const SelectionDAG
&DAG
,
25 // Conservatively fail if we a match failed..
26 if (!Base
.getNode() || !Other
.Base
.getNode())
28 if (!hasValidOffset() || !Other
.hasValidOffset())
30 // Initial Offset difference.
31 Off
= *Other
.Offset
- *Offset
;
33 if ((Other
.Index
== Index
) && (Other
.IsIndexSignExt
== IsIndexSignExt
)) {
35 if (Other
.Base
== Base
)
38 // Match GlobalAddresses
39 if (auto *A
= dyn_cast
<GlobalAddressSDNode
>(Base
))
40 if (auto *B
= dyn_cast
<GlobalAddressSDNode
>(Other
.Base
))
41 if (A
->getGlobal() == B
->getGlobal()) {
42 Off
+= B
->getOffset() - A
->getOffset();
47 if (auto *A
= dyn_cast
<ConstantPoolSDNode
>(Base
))
48 if (auto *B
= dyn_cast
<ConstantPoolSDNode
>(Other
.Base
)) {
50 A
->isMachineConstantPoolEntry() == B
->isMachineConstantPoolEntry();
52 if (A
->isMachineConstantPoolEntry())
53 IsMatch
= A
->getMachineCPVal() == B
->getMachineCPVal();
55 IsMatch
= A
->getConstVal() == B
->getConstVal();
58 Off
+= B
->getOffset() - A
->getOffset();
63 const MachineFrameInfo
&MFI
= DAG
.getMachineFunction().getFrameInfo();
65 // Match FrameIndexes.
66 if (auto *A
= dyn_cast
<FrameIndexSDNode
>(Base
))
67 if (auto *B
= dyn_cast
<FrameIndexSDNode
>(Other
.Base
)) {
68 // Equal FrameIndexes - offsets are directly comparable.
69 if (A
->getIndex() == B
->getIndex())
71 // Non-equal FrameIndexes - If both frame indices are fixed
72 // we know their relative offsets and can compare them. Otherwise
73 // we must be conservative.
74 if (MFI
.isFixedObjectIndex(A
->getIndex()) &&
75 MFI
.isFixedObjectIndex(B
->getIndex())) {
76 Off
+= MFI
.getObjectOffset(B
->getIndex()) -
77 MFI
.getObjectOffset(A
->getIndex());
85 bool BaseIndexOffset::computeAliasing(const SDNode
*Op0
,
86 const Optional
<int64_t> NumBytes0
,
88 const Optional
<int64_t> NumBytes1
,
89 const SelectionDAG
&DAG
, bool &IsAlias
) {
91 BaseIndexOffset BasePtr0
= match(Op0
, DAG
);
92 BaseIndexOffset BasePtr1
= match(Op1
, DAG
);
94 if (!(BasePtr0
.getBase().getNode() && BasePtr1
.getBase().getNode()))
97 if (NumBytes0
.hasValue() && NumBytes1
.hasValue() &&
98 BasePtr0
.equalBaseIndex(BasePtr1
, DAG
, PtrDiff
)) {
99 // BasePtr1 is PtrDiff away from BasePtr0. They alias if none of the
100 // following situations arise:
102 // [----BasePtr0----]
104 // ========PtrDiff========>
105 (*NumBytes0
<= PtrDiff
) ||
106 // [----BasePtr0----]
108 // =====(-PtrDiff)====>
109 (PtrDiff
+ *NumBytes1
<= 0)); // i.e. *NumBytes1 < -PtrDiff.
112 // If both BasePtr0 and BasePtr1 are FrameIndexes, we will not be
113 // able to calculate their relative offset if at least one arises
114 // from an alloca. However, these allocas cannot overlap and we
115 // can infer there is no alias.
116 if (auto *A
= dyn_cast
<FrameIndexSDNode
>(BasePtr0
.getBase()))
117 if (auto *B
= dyn_cast
<FrameIndexSDNode
>(BasePtr1
.getBase())) {
118 MachineFrameInfo
&MFI
= DAG
.getMachineFunction().getFrameInfo();
119 // If the base are the same frame index but the we couldn't find a
120 // constant offset, (indices are different) be conservative.
121 if (A
!= B
&& (!MFI
.isFixedObjectIndex(A
->getIndex()) ||
122 !MFI
.isFixedObjectIndex(B
->getIndex()))) {
128 bool IsFI0
= isa
<FrameIndexSDNode
>(BasePtr0
.getBase());
129 bool IsFI1
= isa
<FrameIndexSDNode
>(BasePtr1
.getBase());
130 bool IsGV0
= isa
<GlobalAddressSDNode
>(BasePtr0
.getBase());
131 bool IsGV1
= isa
<GlobalAddressSDNode
>(BasePtr1
.getBase());
132 bool IsCV0
= isa
<ConstantPoolSDNode
>(BasePtr0
.getBase());
133 bool IsCV1
= isa
<ConstantPoolSDNode
>(BasePtr1
.getBase());
135 // If of mismatched base types or checkable indices we can check
136 // they do not alias.
137 if ((BasePtr0
.getIndex() == BasePtr1
.getIndex() || (IsFI0
!= IsFI1
) ||
138 (IsGV0
!= IsGV1
) || (IsCV0
!= IsCV1
)) &&
139 (IsFI0
|| IsGV0
|| IsCV0
) && (IsFI1
|| IsGV1
|| IsCV1
)) {
143 return false; // Cannot determine whether the pointers alias.
146 bool BaseIndexOffset::contains(const SelectionDAG
&DAG
, int64_t BitSize
,
147 const BaseIndexOffset
&Other
,
148 int64_t OtherBitSize
, int64_t &BitOffset
) const {
150 if (!equalBaseIndex(Other
, DAG
, Offset
))
153 // Other is after *this:
154 // [-------*this---------]
157 BitOffset
= 8 * Offset
;
158 return BitOffset
+ OtherBitSize
<= BitSize
;
160 // Other starts strictly before *this, it cannot be fully contained.
161 // [-------*this---------]
166 /// Parses tree in Ptr for base, index, offset addresses.
167 static BaseIndexOffset
matchLSNode(const LSBaseSDNode
*N
,
168 const SelectionDAG
&DAG
) {
169 SDValue Ptr
= N
->getBasePtr();
171 // (((B + I*M) + c)) + c ...
172 SDValue Base
= DAG
.getTargetLoweringInfo().unwrapAddress(Ptr
);
173 SDValue Index
= SDValue();
175 bool IsIndexSignExt
= false;
177 // pre-inc/pre-dec ops are components of EA.
178 if (N
->getAddressingMode() == ISD::PRE_INC
) {
179 if (auto *C
= dyn_cast
<ConstantSDNode
>(N
->getOffset()))
180 Offset
+= C
->getSExtValue();
181 else // If unknown, give up now.
182 return BaseIndexOffset(SDValue(), SDValue(), 0, false);
183 } else if (N
->getAddressingMode() == ISD::PRE_DEC
) {
184 if (auto *C
= dyn_cast
<ConstantSDNode
>(N
->getOffset()))
185 Offset
-= C
->getSExtValue();
186 else // If unknown, give up now.
187 return BaseIndexOffset(SDValue(), SDValue(), 0, false);
190 // Consume constant adds & ors with appropriate masking.
192 switch (Base
->getOpcode()) {
194 // Only consider ORs which act as adds.
195 if (auto *C
= dyn_cast
<ConstantSDNode
>(Base
->getOperand(1)))
196 if (DAG
.MaskedValueIsZero(Base
->getOperand(0), C
->getAPIntValue())) {
197 Offset
+= C
->getSExtValue();
198 Base
= DAG
.getTargetLoweringInfo().unwrapAddress(Base
->getOperand(0));
203 if (auto *C
= dyn_cast
<ConstantSDNode
>(Base
->getOperand(1))) {
204 Offset
+= C
->getSExtValue();
205 Base
= DAG
.getTargetLoweringInfo().unwrapAddress(Base
->getOperand(0));
211 auto *LSBase
= cast
<LSBaseSDNode
>(Base
.getNode());
212 unsigned int IndexResNo
= (Base
->getOpcode() == ISD::LOAD
) ? 1 : 0;
213 if (LSBase
->isIndexed() && Base
.getResNo() == IndexResNo
)
214 if (auto *C
= dyn_cast
<ConstantSDNode
>(LSBase
->getOffset())) {
215 auto Off
= C
->getSExtValue();
216 if (LSBase
->getAddressingMode() == ISD::PRE_DEC
||
217 LSBase
->getAddressingMode() == ISD::POST_DEC
)
221 Base
= DAG
.getTargetLoweringInfo().unwrapAddress(LSBase
->getBasePtr());
227 // If we get here break out of the loop.
231 if (Base
->getOpcode() == ISD::ADD
) {
232 // TODO: The following code appears to be needless as it just
233 // bails on some Ptrs early, reducing the cases where we
234 // find equivalence. We should be able to remove this.
235 // Inside a loop the current BASE pointer is calculated using an ADD and a
236 // MUL instruction. In this case Base is the actual BASE pointer.
237 // (i64 add (i64 %array_ptr)
238 // (i64 mul (i64 %induction_var)
239 // (i64 %element_size)))
240 if (Base
->getOperand(1)->getOpcode() == ISD::MUL
)
241 return BaseIndexOffset(Base
, Index
, Offset
, IsIndexSignExt
);
243 // Look at Base + Index + Offset cases.
244 Index
= Base
->getOperand(1);
245 SDValue PotentialBase
= Base
->getOperand(0);
248 if (Index
->getOpcode() == ISD::SIGN_EXTEND
) {
249 Index
= Index
->getOperand(0);
250 IsIndexSignExt
= true;
253 // Check if Index Offset pattern
254 if (Index
->getOpcode() != ISD::ADD
||
255 !isa
<ConstantSDNode
>(Index
->getOperand(1)))
256 return BaseIndexOffset(PotentialBase
, Index
, Offset
, IsIndexSignExt
);
258 Offset
+= cast
<ConstantSDNode
>(Index
->getOperand(1))->getSExtValue();
259 Index
= Index
->getOperand(0);
260 if (Index
->getOpcode() == ISD::SIGN_EXTEND
) {
261 Index
= Index
->getOperand(0);
262 IsIndexSignExt
= true;
264 IsIndexSignExt
= false;
265 Base
= PotentialBase
;
267 return BaseIndexOffset(Base
, Index
, Offset
, IsIndexSignExt
);
270 BaseIndexOffset
BaseIndexOffset::match(const SDNode
*N
,
271 const SelectionDAG
&DAG
) {
272 if (const auto *LS0
= dyn_cast
<LSBaseSDNode
>(N
))
273 return matchLSNode(LS0
, DAG
);
274 if (const auto *LN
= dyn_cast
<LifetimeSDNode
>(N
)) {
276 return BaseIndexOffset(LN
->getOperand(1), SDValue(), LN
->getOffset(),
278 return BaseIndexOffset(LN
->getOperand(1), SDValue(), false);
280 return BaseIndexOffset();
283 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
285 LLVM_DUMP_METHOD
void BaseIndexOffset::dump() const {
289 void BaseIndexOffset::print(raw_ostream
& OS
) const {
290 OS
<< "BaseIndexOffset base=[";
295 OS
<< "] offset=" << Offset
;