LLVM 19.0.0git
SelectionDAGAddressAnalysis.cpp
Go to the documentation of this file.
1//==- llvm/CodeGen/SelectionDAGAddressAnalysis.cpp - DAG Address Analysis --==//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
17#include "llvm/IR/GlobalAlias.h"
19#include "llvm/Support/Debug.h"
20#include <cstdint>
21
22using namespace llvm;
23
25 const SelectionDAG &DAG,
26 int64_t &Off) const {
27 // Conservatively fail if we a match failed..
28 if (!Base.getNode() || !Other.Base.getNode())
29 return false;
30 if (!hasValidOffset() || !Other.hasValidOffset())
31 return false;
32 // Initial Offset difference.
33 Off = *Other.Offset - *Offset;
34
35 if ((Other.Index == Index) && (Other.IsIndexSignExt == IsIndexSignExt)) {
36 // Trivial match.
37 if (Other.Base == Base)
38 return true;
39
40 // Match GlobalAddresses
41 if (auto *A = dyn_cast<GlobalAddressSDNode>(Base)) {
42 if (auto *B = dyn_cast<GlobalAddressSDNode>(Other.Base))
43 if (A->getGlobal() == B->getGlobal()) {
44 Off += B->getOffset() - A->getOffset();
45 return true;
46 }
47
48 return false;
49 }
50
51 // Match Constants
52 if (auto *A = dyn_cast<ConstantPoolSDNode>(Base)) {
53 if (auto *B = dyn_cast<ConstantPoolSDNode>(Other.Base)) {
54 bool IsMatch =
55 A->isMachineConstantPoolEntry() == B->isMachineConstantPoolEntry();
56 if (IsMatch) {
57 if (A->isMachineConstantPoolEntry())
58 IsMatch = A->getMachineCPVal() == B->getMachineCPVal();
59 else
60 IsMatch = A->getConstVal() == B->getConstVal();
61 }
62 if (IsMatch) {
63 Off += B->getOffset() - A->getOffset();
64 return true;
65 }
66 }
67
68 return false;
69 }
70
71 // Match FrameIndexes.
72 if (auto *A = dyn_cast<FrameIndexSDNode>(Base))
73 if (auto *B = dyn_cast<FrameIndexSDNode>(Other.Base)) {
74 // Equal FrameIndexes - offsets are directly comparable.
75 if (A->getIndex() == B->getIndex())
76 return true;
77 // Non-equal FrameIndexes - If both frame indices are fixed
78 // we know their relative offsets and can compare them. Otherwise
79 // we must be conservative.
81 if (MFI.isFixedObjectIndex(A->getIndex()) &&
82 MFI.isFixedObjectIndex(B->getIndex())) {
83 Off += MFI.getObjectOffset(B->getIndex()) -
84 MFI.getObjectOffset(A->getIndex());
85 return true;
86 }
87 }
88 }
89
90 return false;
91}
92
94 const std::optional<int64_t> NumBytes0,
95 const SDNode *Op1,
96 const std::optional<int64_t> NumBytes1,
97 const SelectionDAG &DAG, bool &IsAlias) {
98
99 BaseIndexOffset BasePtr0 = match(Op0, DAG);
100 if (!BasePtr0.getBase().getNode())
101 return false;
102
103 BaseIndexOffset BasePtr1 = match(Op1, DAG);
104 if (!BasePtr1.getBase().getNode())
105 return false;
106
107 int64_t PtrDiff;
108 if (NumBytes0 && NumBytes1 &&
109 BasePtr0.equalBaseIndex(BasePtr1, DAG, PtrDiff)) {
110 // If the size of memory access is unknown, do not use it to analysis.
111 // One example of unknown size memory access is to load/store scalable
112 // vector objects on the stack.
113 // BasePtr1 is PtrDiff away from BasePtr0. They alias if none of the
114 // following situations arise:
115 if (PtrDiff >= 0 &&
116 *NumBytes0 != static_cast<int64_t>(MemoryLocation::UnknownSize)) {
117 // [----BasePtr0----]
118 // [---BasePtr1--]
119 // ========PtrDiff========>
120 IsAlias = !(*NumBytes0 <= PtrDiff);
121 return true;
122 }
123 if (PtrDiff < 0 &&
124 *NumBytes1 != static_cast<int64_t>(MemoryLocation::UnknownSize)) {
125 // [----BasePtr0----]
126 // [---BasePtr1--]
127 // =====(-PtrDiff)====>
128 IsAlias = !((PtrDiff + *NumBytes1) <= 0);
129 return true;
130 }
131 return false;
132 }
133 // If both BasePtr0 and BasePtr1 are FrameIndexes, we will not be
134 // able to calculate their relative offset if at least one arises
135 // from an alloca. However, these allocas cannot overlap and we
136 // can infer there is no alias.
137 if (auto *A = dyn_cast<FrameIndexSDNode>(BasePtr0.getBase()))
138 if (auto *B = dyn_cast<FrameIndexSDNode>(BasePtr1.getBase())) {
140 // If the base are the same frame index but the we couldn't find a
141 // constant offset, (indices are different) be conservative.
142 if (A->getIndex() != B->getIndex() && (!MFI.isFixedObjectIndex(A->getIndex()) ||
143 !MFI.isFixedObjectIndex(B->getIndex()))) {
144 IsAlias = false;
145 return true;
146 }
147 }
148
149 bool IsFI0 = isa<FrameIndexSDNode>(BasePtr0.getBase());
150 bool IsFI1 = isa<FrameIndexSDNode>(BasePtr1.getBase());
151 bool IsGV0 = isa<GlobalAddressSDNode>(BasePtr0.getBase());
152 bool IsGV1 = isa<GlobalAddressSDNode>(BasePtr1.getBase());
153 bool IsCV0 = isa<ConstantPoolSDNode>(BasePtr0.getBase());
154 bool IsCV1 = isa<ConstantPoolSDNode>(BasePtr1.getBase());
155
156 if ((IsFI0 || IsGV0 || IsCV0) && (IsFI1 || IsGV1 || IsCV1)) {
157 // We can derive NoAlias In case of mismatched base types.
158 if (IsFI0 != IsFI1 || IsGV0 != IsGV1 || IsCV0 != IsCV1) {
159 IsAlias = false;
160 return true;
161 }
162 if (IsGV0 && IsGV1) {
163 auto *GV0 = cast<GlobalAddressSDNode>(BasePtr0.getBase())->getGlobal();
164 auto *GV1 = cast<GlobalAddressSDNode>(BasePtr1.getBase())->getGlobal();
165 // It doesn't make sense to access one global value using another globals
166 // values address, so we can assume that there is no aliasing in case of
167 // two different globals (unless we have symbols that may indirectly point
168 // to each other).
169 // FIXME: This is perhaps a bit too defensive. We could try to follow the
170 // chain with aliasee information for GlobalAlias variables to find out if
171 // we indirect symbols may alias or not.
172 if (GV0 != GV1 && !isa<GlobalAlias>(GV0) && !isa<GlobalAlias>(GV1)) {
173 IsAlias = false;
174 return true;
175 }
176 }
177 }
178 return false; // Cannot determine whether the pointers alias.
179}
180
181bool BaseIndexOffset::contains(const SelectionDAG &DAG, int64_t BitSize,
182 const BaseIndexOffset &Other,
183 int64_t OtherBitSize, int64_t &BitOffset) const {
184 int64_t Offset;
185 if (!equalBaseIndex(Other, DAG, Offset))
186 return false;
187 if (Offset >= 0) {
188 // Other is after *this:
189 // [-------*this---------]
190 // [---Other--]
191 // ==Offset==>
192 BitOffset = 8 * Offset;
193 return BitOffset + OtherBitSize <= BitSize;
194 }
195 // Other starts strictly before *this, it cannot be fully contained.
196 // [-------*this---------]
197 // [--Other--]
198 return false;
199}
200
201/// Parses tree in Ptr for base, index, offset addresses.
203 const SelectionDAG &DAG) {
204 SDValue Ptr = N->getBasePtr();
205
206 // (((B + I*M) + c)) + c ...
209 int64_t Offset = 0;
210 bool IsIndexSignExt = false;
211
212 // pre-inc/pre-dec ops are components of EA.
213 if (N->getAddressingMode() == ISD::PRE_INC) {
214 if (auto *C = dyn_cast<ConstantSDNode>(N->getOffset()))
215 Offset += C->getSExtValue();
216 else // If unknown, give up now.
217 return BaseIndexOffset(SDValue(), SDValue(), 0, false);
218 } else if (N->getAddressingMode() == ISD::PRE_DEC) {
219 if (auto *C = dyn_cast<ConstantSDNode>(N->getOffset()))
220 Offset -= C->getSExtValue();
221 else // If unknown, give up now.
222 return BaseIndexOffset(SDValue(), SDValue(), 0, false);
223 }
224
225 // Consume constant adds & ors with appropriate masking.
226 while (true) {
227 switch (Base->getOpcode()) {
228 case ISD::OR:
229 // Only consider ORs which act as adds.
230 if (auto *C = dyn_cast<ConstantSDNode>(Base->getOperand(1)))
231 if (DAG.MaskedValueIsZero(Base->getOperand(0), C->getAPIntValue())) {
232 Offset += C->getSExtValue();
233 Base = DAG.getTargetLoweringInfo().unwrapAddress(Base->getOperand(0));
234 continue;
235 }
236 break;
237 case ISD::ADD:
238 if (auto *C = dyn_cast<ConstantSDNode>(Base->getOperand(1))) {
239 Offset += C->getSExtValue();
240 Base = DAG.getTargetLoweringInfo().unwrapAddress(Base->getOperand(0));
241 continue;
242 }
243 break;
244 case ISD::LOAD:
245 case ISD::STORE: {
246 auto *LSBase = cast<LSBaseSDNode>(Base.getNode());
247 unsigned int IndexResNo = (Base->getOpcode() == ISD::LOAD) ? 1 : 0;
248 if (LSBase->isIndexed() && Base.getResNo() == IndexResNo)
249 if (auto *C = dyn_cast<ConstantSDNode>(LSBase->getOffset())) {
250 auto Off = C->getSExtValue();
251 if (LSBase->getAddressingMode() == ISD::PRE_DEC ||
252 LSBase->getAddressingMode() == ISD::POST_DEC)
253 Offset -= Off;
254 else
255 Offset += Off;
256 Base = DAG.getTargetLoweringInfo().unwrapAddress(LSBase->getBasePtr());
257 continue;
258 }
259 break;
260 }
261 }
262 // If we get here break out of the loop.
263 break;
264 }
265
266 if (Base->getOpcode() == ISD::ADD) {
267 // TODO: The following code appears to be needless as it just
268 // bails on some Ptrs early, reducing the cases where we
269 // find equivalence. We should be able to remove this.
270 // Inside a loop the current BASE pointer is calculated using an ADD and a
271 // MUL instruction. In this case Base is the actual BASE pointer.
272 // (i64 add (i64 %array_ptr)
273 // (i64 mul (i64 %induction_var)
274 // (i64 %element_size)))
275 if (Base->getOperand(1)->getOpcode() == ISD::MUL)
276 return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt);
277
278 // Look at Base + Index + Offset cases.
279 Index = Base->getOperand(1);
280 SDValue PotentialBase = Base->getOperand(0);
281
282 // Skip signextends.
283 if (Index->getOpcode() == ISD::SIGN_EXTEND) {
284 Index = Index->getOperand(0);
285 IsIndexSignExt = true;
286 }
287
288 // Check if Index Offset pattern
289 if (Index->getOpcode() != ISD::ADD ||
290 !isa<ConstantSDNode>(Index->getOperand(1)))
291 return BaseIndexOffset(PotentialBase, Index, Offset, IsIndexSignExt);
292
293 Offset += cast<ConstantSDNode>(Index->getOperand(1))->getSExtValue();
294 Index = Index->getOperand(0);
295 if (Index->getOpcode() == ISD::SIGN_EXTEND) {
296 Index = Index->getOperand(0);
297 IsIndexSignExt = true;
298 } else
299 IsIndexSignExt = false;
300 Base = PotentialBase;
301 }
302 return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt);
303}
304
306 const SelectionDAG &DAG) {
307 if (const auto *LS0 = dyn_cast<LSBaseSDNode>(N))
308 return matchLSNode(LS0, DAG);
309 if (const auto *LN = dyn_cast<LifetimeSDNode>(N)) {
310 if (LN->hasOffset())
311 return BaseIndexOffset(LN->getOperand(1), SDValue(), LN->getOffset(),
312 false);
313 return BaseIndexOffset(LN->getOperand(1), SDValue(), false);
314 }
315 return BaseIndexOffset();
316}
317
318#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
319
321 print(dbgs());
322}
323
325 OS << "BaseIndexOffset base=[";
326 Base->print(OS);
327 OS << "] index=[";
328 if (Index)
329 Index->print(OS);
330 OS << "] offset=" << Offset;
331}
332
333#endif
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:529
std::optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1290
This file provides utility analysis objects describing memory locations.
raw_pwrite_stream & OS
static BaseIndexOffset matchLSNode(const LSBaseSDNode *N, const SelectionDAG &DAG)
Parses tree in Ptr for base, index, offset addresses.
This file describes how to lower LLVM code to machine code.
bool contains(const SelectionDAG &DAG, int64_t BitSize, const BaseIndexOffset &Other, int64_t OtherBitSize, int64_t &BitOffset) const
void print(raw_ostream &OS) const
static BaseIndexOffset match(const SDNode *N, const SelectionDAG &DAG)
Parses tree in N for base, index, offset addresses.
bool equalBaseIndex(const BaseIndexOffset &Other, const SelectionDAG &DAG, int64_t &Off) const
static bool computeAliasing(const SDNode *Op0, const std::optional< int64_t > NumBytes0, const SDNode *Op1, const std::optional< int64_t > NumBytes1, const SelectionDAG &DAG, bool &IsAlias)
Base class for LoadSDNode and StoreSDNode.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Represents one node in the SelectionDAG.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
Definition: SelectionDAG.h:225
const TargetLowering & getTargetLoweringInfo() const
Definition: SelectionDAG.h:478
MachineFunction & getMachineFunction() const
Definition: SelectionDAG.h:469
bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if 'Op & Mask' is known to be zero.
virtual SDValue unwrapAddress(SDValue N) const
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ ADD
Simple integer binary arithmetic operators.
Definition: ISDOpcodes.h:239
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
Definition: ISDOpcodes.h:1029
@ SIGN_EXTEND
Conversion operators.
Definition: ISDOpcodes.h:774
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
@ Other
Any other memory.
#define N
Helper struct to store a base, index and offset that forms an address.
Definition: LoadStoreOpt.h:38