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/Analysis/MemoryLocation.h"
11 #include "llvm/CodeGen/ISDOpcodes.h"
12 #include "llvm/CodeGen/MachineFrameInfo.h"
13 #include "llvm/CodeGen/MachineFunction.h"
14 #include "llvm/CodeGen/SelectionDAG.h"
15 #include "llvm/CodeGen/SelectionDAGNodes.h"
16 #include "llvm/CodeGen/TargetLowering.h"
17 #include "llvm/Support/Casting.h"
18 #include "llvm/Support/Debug.h"
23 bool BaseIndexOffset::equalBaseIndex(const BaseIndexOffset
&Other
,
24 const SelectionDAG
&DAG
,
26 // Conservatively fail if we a match failed..
27 if (!Base
.getNode() || !Other
.Base
.getNode())
29 if (!hasValidOffset() || !Other
.hasValidOffset())
31 // Initial Offset difference.
32 Off
= *Other
.Offset
- *Offset
;
34 if ((Other
.Index
== Index
) && (Other
.IsIndexSignExt
== IsIndexSignExt
)) {
36 if (Other
.Base
== Base
)
39 // Match GlobalAddresses
40 if (auto *A
= dyn_cast
<GlobalAddressSDNode
>(Base
))
41 if (auto *B
= dyn_cast
<GlobalAddressSDNode
>(Other
.Base
))
42 if (A
->getGlobal() == B
->getGlobal()) {
43 Off
+= B
->getOffset() - A
->getOffset();
48 if (auto *A
= dyn_cast
<ConstantPoolSDNode
>(Base
))
49 if (auto *B
= dyn_cast
<ConstantPoolSDNode
>(Other
.Base
)) {
51 A
->isMachineConstantPoolEntry() == B
->isMachineConstantPoolEntry();
53 if (A
->isMachineConstantPoolEntry())
54 IsMatch
= A
->getMachineCPVal() == B
->getMachineCPVal();
56 IsMatch
= A
->getConstVal() == B
->getConstVal();
59 Off
+= B
->getOffset() - A
->getOffset();
64 const MachineFrameInfo
&MFI
= DAG
.getMachineFunction().getFrameInfo();
66 // Match FrameIndexes.
67 if (auto *A
= dyn_cast
<FrameIndexSDNode
>(Base
))
68 if (auto *B
= dyn_cast
<FrameIndexSDNode
>(Other
.Base
)) {
69 // Equal FrameIndexes - offsets are directly comparable.
70 if (A
->getIndex() == B
->getIndex())
72 // Non-equal FrameIndexes - If both frame indices are fixed
73 // we know their relative offsets and can compare them. Otherwise
74 // we must be conservative.
75 if (MFI
.isFixedObjectIndex(A
->getIndex()) &&
76 MFI
.isFixedObjectIndex(B
->getIndex())) {
77 Off
+= MFI
.getObjectOffset(B
->getIndex()) -
78 MFI
.getObjectOffset(A
->getIndex());
86 bool BaseIndexOffset::computeAliasing(const SDNode
*Op0
,
87 const Optional
<int64_t> NumBytes0
,
89 const Optional
<int64_t> NumBytes1
,
90 const SelectionDAG
&DAG
, bool &IsAlias
) {
92 BaseIndexOffset BasePtr0
= match(Op0
, DAG
);
93 BaseIndexOffset BasePtr1
= match(Op1
, DAG
);
95 if (!(BasePtr0
.getBase().getNode() && BasePtr1
.getBase().getNode()))
98 if (NumBytes0
.hasValue() && NumBytes1
.hasValue() &&
99 BasePtr0
.equalBaseIndex(BasePtr1
, DAG
, PtrDiff
)) {
100 // If the size of memory access is unknown, do not use it to analysis.
101 // One example of unknown size memory access is to load/store scalable
102 // vector objects on the stack.
103 // BasePtr1 is PtrDiff away from BasePtr0. They alias if none of the
104 // following situations arise:
106 *NumBytes0
!= static_cast<int64_t>(MemoryLocation::UnknownSize
)) {
107 // [----BasePtr0----]
109 // ========PtrDiff========>
110 IsAlias
= !(*NumBytes0
<= PtrDiff
);
114 *NumBytes1
!= static_cast<int64_t>(MemoryLocation::UnknownSize
)) {
115 // [----BasePtr0----]
117 // =====(-PtrDiff)====>
118 IsAlias
= !((PtrDiff
+ *NumBytes1
) <= 0);
123 // If both BasePtr0 and BasePtr1 are FrameIndexes, we will not be
124 // able to calculate their relative offset if at least one arises
125 // from an alloca. However, these allocas cannot overlap and we
126 // can infer there is no alias.
127 if (auto *A
= dyn_cast
<FrameIndexSDNode
>(BasePtr0
.getBase()))
128 if (auto *B
= dyn_cast
<FrameIndexSDNode
>(BasePtr1
.getBase())) {
129 MachineFrameInfo
&MFI
= DAG
.getMachineFunction().getFrameInfo();
130 // If the base are the same frame index but the we couldn't find a
131 // constant offset, (indices are different) be conservative.
132 if (A
!= B
&& (!MFI
.isFixedObjectIndex(A
->getIndex()) ||
133 !MFI
.isFixedObjectIndex(B
->getIndex()))) {
139 bool IsFI0
= isa
<FrameIndexSDNode
>(BasePtr0
.getBase());
140 bool IsFI1
= isa
<FrameIndexSDNode
>(BasePtr1
.getBase());
141 bool IsGV0
= isa
<GlobalAddressSDNode
>(BasePtr0
.getBase());
142 bool IsGV1
= isa
<GlobalAddressSDNode
>(BasePtr1
.getBase());
143 bool IsCV0
= isa
<ConstantPoolSDNode
>(BasePtr0
.getBase());
144 bool IsCV1
= isa
<ConstantPoolSDNode
>(BasePtr1
.getBase());
146 // If of mismatched base types or checkable indices we can check
147 // they do not alias.
148 if ((BasePtr0
.getIndex() == BasePtr1
.getIndex() || (IsFI0
!= IsFI1
) ||
149 (IsGV0
!= IsGV1
) || (IsCV0
!= IsCV1
)) &&
150 (IsFI0
|| IsGV0
|| IsCV0
) && (IsFI1
|| IsGV1
|| IsCV1
)) {
154 return false; // Cannot determine whether the pointers alias.
157 bool BaseIndexOffset::contains(const SelectionDAG
&DAG
, int64_t BitSize
,
158 const BaseIndexOffset
&Other
,
159 int64_t OtherBitSize
, int64_t &BitOffset
) const {
161 if (!equalBaseIndex(Other
, DAG
, Offset
))
164 // Other is after *this:
165 // [-------*this---------]
168 BitOffset
= 8 * Offset
;
169 return BitOffset
+ OtherBitSize
<= BitSize
;
171 // Other starts strictly before *this, it cannot be fully contained.
172 // [-------*this---------]
177 /// Parses tree in Ptr for base, index, offset addresses.
178 static BaseIndexOffset
matchLSNode(const LSBaseSDNode
*N
,
179 const SelectionDAG
&DAG
) {
180 SDValue Ptr
= N
->getBasePtr();
182 // (((B + I*M) + c)) + c ...
183 SDValue Base
= DAG
.getTargetLoweringInfo().unwrapAddress(Ptr
);
184 SDValue Index
= SDValue();
186 bool IsIndexSignExt
= false;
188 // pre-inc/pre-dec ops are components of EA.
189 if (N
->getAddressingMode() == ISD::PRE_INC
) {
190 if (auto *C
= dyn_cast
<ConstantSDNode
>(N
->getOffset()))
191 Offset
+= C
->getSExtValue();
192 else // If unknown, give up now.
193 return BaseIndexOffset(SDValue(), SDValue(), 0, false);
194 } else if (N
->getAddressingMode() == ISD::PRE_DEC
) {
195 if (auto *C
= dyn_cast
<ConstantSDNode
>(N
->getOffset()))
196 Offset
-= C
->getSExtValue();
197 else // If unknown, give up now.
198 return BaseIndexOffset(SDValue(), SDValue(), 0, false);
201 // Consume constant adds & ors with appropriate masking.
203 switch (Base
->getOpcode()) {
205 // Only consider ORs which act as adds.
206 if (auto *C
= dyn_cast
<ConstantSDNode
>(Base
->getOperand(1)))
207 if (DAG
.MaskedValueIsZero(Base
->getOperand(0), C
->getAPIntValue())) {
208 Offset
+= C
->getSExtValue();
209 Base
= DAG
.getTargetLoweringInfo().unwrapAddress(Base
->getOperand(0));
214 if (auto *C
= dyn_cast
<ConstantSDNode
>(Base
->getOperand(1))) {
215 Offset
+= C
->getSExtValue();
216 Base
= DAG
.getTargetLoweringInfo().unwrapAddress(Base
->getOperand(0));
222 auto *LSBase
= cast
<LSBaseSDNode
>(Base
.getNode());
223 unsigned int IndexResNo
= (Base
->getOpcode() == ISD::LOAD
) ? 1 : 0;
224 if (LSBase
->isIndexed() && Base
.getResNo() == IndexResNo
)
225 if (auto *C
= dyn_cast
<ConstantSDNode
>(LSBase
->getOffset())) {
226 auto Off
= C
->getSExtValue();
227 if (LSBase
->getAddressingMode() == ISD::PRE_DEC
||
228 LSBase
->getAddressingMode() == ISD::POST_DEC
)
232 Base
= DAG
.getTargetLoweringInfo().unwrapAddress(LSBase
->getBasePtr());
238 // If we get here break out of the loop.
242 if (Base
->getOpcode() == ISD::ADD
) {
243 // TODO: The following code appears to be needless as it just
244 // bails on some Ptrs early, reducing the cases where we
245 // find equivalence. We should be able to remove this.
246 // Inside a loop the current BASE pointer is calculated using an ADD and a
247 // MUL instruction. In this case Base is the actual BASE pointer.
248 // (i64 add (i64 %array_ptr)
249 // (i64 mul (i64 %induction_var)
250 // (i64 %element_size)))
251 if (Base
->getOperand(1)->getOpcode() == ISD::MUL
)
252 return BaseIndexOffset(Base
, Index
, Offset
, IsIndexSignExt
);
254 // Look at Base + Index + Offset cases.
255 Index
= Base
->getOperand(1);
256 SDValue PotentialBase
= Base
->getOperand(0);
259 if (Index
->getOpcode() == ISD::SIGN_EXTEND
) {
260 Index
= Index
->getOperand(0);
261 IsIndexSignExt
= true;
264 // Check if Index Offset pattern
265 if (Index
->getOpcode() != ISD::ADD
||
266 !isa
<ConstantSDNode
>(Index
->getOperand(1)))
267 return BaseIndexOffset(PotentialBase
, Index
, Offset
, IsIndexSignExt
);
269 Offset
+= cast
<ConstantSDNode
>(Index
->getOperand(1))->getSExtValue();
270 Index
= Index
->getOperand(0);
271 if (Index
->getOpcode() == ISD::SIGN_EXTEND
) {
272 Index
= Index
->getOperand(0);
273 IsIndexSignExt
= true;
275 IsIndexSignExt
= false;
276 Base
= PotentialBase
;
278 return BaseIndexOffset(Base
, Index
, Offset
, IsIndexSignExt
);
281 BaseIndexOffset
BaseIndexOffset::match(const SDNode
*N
,
282 const SelectionDAG
&DAG
) {
283 if (const auto *LS0
= dyn_cast
<LSBaseSDNode
>(N
))
284 return matchLSNode(LS0
, DAG
);
285 if (const auto *LN
= dyn_cast
<LifetimeSDNode
>(N
)) {
287 return BaseIndexOffset(LN
->getOperand(1), SDValue(), LN
->getOffset(),
289 return BaseIndexOffset(LN
->getOperand(1), SDValue(), false);
291 return BaseIndexOffset();
294 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
296 LLVM_DUMP_METHOD
void BaseIndexOffset::dump() const {
300 void BaseIndexOffset::print(raw_ostream
& OS
) const {
301 OS
<< "BaseIndexOffset base=[";
306 OS
<< "] offset=" << Offset
;