LLVM 24.0.0git
ARMSelectionDAGInfo.cpp
Go to the documentation of this file.
1//===-- ARMSelectionDAGInfo.cpp - ARM SelectionDAG Info -------------------===//
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//
9// This file implements the ARMSelectionDAGInfo class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "ARMSelectionDAGInfo.h"
14#include "ARMSubtarget.h"
16
17#define GET_SDNODE_DESC
18#include "ARMGenSDNodeInfo.inc"
19
20using namespace llvm;
21
22#define DEBUG_TYPE "arm-selectiondag-info"
23
26
70
71bool ARMSelectionDAGInfo::isTargetMemoryOpcode(unsigned Opcode) const {
72 // These nodes don't have corresponding entries in *.td files yet.
73 if (Opcode >= ARMISD::FIRST_MEMORY_OPCODE &&
75 return true;
76
78}
79
81 const SDNode *N) const {
82 switch (N->getOpcode()) {
83 default:
84 break;
85 case ARMISD::WIN__DBZCHK:
86 // invalid number of results; expected 2, got 1
87 case ARMISD::WIN__CHKSTK:
88 // invalid number of results; expected 1, got 2
89 case ARMISD::COPY_STRUCT_BYVAL:
90 // invalid number of operands; expected 6, got 5
91 case ARMISD::MEMCPY:
92 // invalid number of operands; expected 5, got 4
93 case ARMISD::VMOVRRD:
94 // operand #0 must have type f64, but has type v1i64/v4f16/v8i8
95 case ARMISD::VMOVIMM:
96 // operand #0 must have type i32, but has type i16
97 return;
98 }
99
101}
102
103// Emit, if possible, a specialized version of the given Libcall. Typically this
104// means selecting the appropriately aligned version, but we also convert memset
105// of 0 into memclr.
107 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
108 SDValue Size, unsigned Align, RTLIB::Libcall LC, bool &IsTailCall) const {
109 bool CanTailCall = IsTailCall;
110 IsTailCall = false;
111
112 const ARMSubtarget &Subtarget =
114 const ARMTargetLowering *TLI = Subtarget.getTargetLowering();
115
116 // Only use the AEABI functions if they are available.
117 //
118 // Translate RTLIB::Libcall to AEABILibcall. We only do this in order to be
119 // able to translate memset to memclr and use the value to index the function
120 // name array.
121 enum {
122 AEABI_MEMCPY = 0,
123 AEABI_MEMMOVE,
124 AEABI_MEMSET,
125 AEABI_MEMCLR
126 } AEABILibcall;
127 switch (LC) {
128 case RTLIB::MEMCPY:
129 if (DAG.getLibcalls().getLibcallImpl(RTLIB::AEABI_MEMCPY) ==
130 RTLIB::Unsupported)
131 return SDValue();
132
133 AEABILibcall = AEABI_MEMCPY;
134 break;
135 case RTLIB::MEMMOVE:
136 if (DAG.getLibcalls().getLibcallImpl(RTLIB::AEABI_MEMMOVE) ==
137 RTLIB::Unsupported)
138 return SDValue();
139
140 AEABILibcall = AEABI_MEMMOVE;
141 break;
142 case RTLIB::MEMSET:
143 if (DAG.getLibcalls().getLibcallImpl(RTLIB::AEABI_MEMSET) ==
144 RTLIB::Unsupported)
145 return SDValue();
146
147 AEABILibcall = AEABI_MEMSET;
148 if (isNullConstant(Src))
149 AEABILibcall = AEABI_MEMCLR;
150 break;
151 default:
152 return SDValue();
153 }
154
155 // Choose the most-aligned libcall variant that we can
156 enum {
157 ALIGN1 = 0,
158 ALIGN4,
159 ALIGN8
160 } AlignVariant;
161 if ((Align & 7) == 0)
162 AlignVariant = ALIGN8;
163 else if ((Align & 3) == 0)
164 AlignVariant = ALIGN4;
165 else
166 AlignVariant = ALIGN1;
167
170 Args.emplace_back(Dst, IntPtrTy);
171 if (AEABILibcall == AEABI_MEMCLR) {
172 Args.emplace_back(Size, IntPtrTy);
173 } else if (AEABILibcall == AEABI_MEMSET) {
174 // Adjust parameters for memset, EABI uses format (ptr, size, value),
175 // GNU library uses (ptr, value, size)
176 // See RTABI section 4.3.4
177 Args.emplace_back(Size, IntPtrTy);
178
179 // Extend or truncate the argument to be an i32 value for the call.
180 if (Src.getValueType().bitsGT(MVT::i32))
181 Src = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
182 else if (Src.getValueType().bitsLT(MVT::i32))
183 Src = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
184
187 Entry.IsSExt = false;
188 Args.push_back(Entry);
189 } else {
190 Args.emplace_back(Src, IntPtrTy);
191 Args.emplace_back(Size, IntPtrTy);
192 }
193
194 static const RTLIB::Libcall FunctionImpls[4][3] = {
195 {RTLIB::AEABI_MEMCPY, RTLIB::AEABI_MEMCPY4, RTLIB::AEABI_MEMCPY8},
196 {RTLIB::AEABI_MEMMOVE, RTLIB::AEABI_MEMMOVE4, RTLIB::AEABI_MEMMOVE8},
197 {RTLIB::AEABI_MEMSET, RTLIB::AEABI_MEMSET4, RTLIB::AEABI_MEMSET8},
198 {RTLIB::AEABI_MEMCLR, RTLIB::AEABI_MEMCLR4, RTLIB::AEABI_MEMCLR8}};
199
200 RTLIB::Libcall NewLC = FunctionImpls[AEABILibcall][AlignVariant];
201 RTLIB::LibcallImpl LCImpl = DAG.getLibcalls().getLibcallImpl(NewLC);
202 if (LCImpl == RTLIB::Unsupported)
203 return SDValue();
204
206 CLI.setDebugLoc(dl)
207 .setChain(Chain)
211 DAG.getExternalSymbol(LCImpl, TLI->getPointerTy(DAG.getDataLayout())),
212 std::move(Args))
214 .setTailCall(CanTailCall);
215 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
216 IsTailCall = CLI.IsTailCall;
217 return CallResult.second;
218}
219
221 const SelectionDAG &DAG,
222 ConstantSDNode *ConstantSize,
223 Align Alignment, bool IsMemcpy) {
224 auto &F = DAG.getMachineFunction().getFunction();
225 TPLoopMemTransfer Mode = Subtarget.getCLOpts().memtransfer_tploop;
227 return false;
229 return true;
230 // Do not generate inline TP loop if optimizations is disabled,
231 // or if optimization for size (-Os or -Oz) is on.
232 if (F.hasOptNone() || F.hasOptSize())
233 return false;
234 // If cli option is unset, for memset always generate inline TP.
235 // For memcpy, check some conditions
236 if (!IsMemcpy)
237 return true;
238 if (!ConstantSize && Alignment >= Align(4))
239 return true;
240 if (ConstantSize &&
241 ConstantSize->getZExtValue() > Subtarget.getMaxInlineSizeThreshold() &&
242 ConstantSize->getZExtValue() <
243 Subtarget.getMaxMemcpyTPInlineSizeThreshold())
244 return true;
245 return false;
246}
247
249 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
250 SDValue Size, Align DstAlign, Align SrcAlign, bool isVolatile,
251 bool AlwaysInline, bool &IsTailCall, MachinePointerInfo DstPtrInfo,
252 MachinePointerInfo SrcPtrInfo) const {
253 Align Alignment = std::min(DstAlign, SrcAlign);
254 const ARMSubtarget &Subtarget =
257
258 if (Subtarget.hasMVEIntegerOps() &&
259 shouldGenerateInlineTPLoop(Subtarget, DAG, ConstantSize, Alignment, true))
260 return DAG.getNode(ARMISD::MEMCPYLOOP, dl, MVT::Other, Chain, Dst, Src,
261 DAG.getZExtOrTrunc(Size, dl, MVT::i32));
262
263 // Do repeated 4-byte loads and stores. To be improved.
264 // This requires 4-byte alignment.
265 if (Alignment < Align(4)) {
266 if (AlwaysInline) {
267 IsTailCall = false;
268 return SDValue();
269 }
270
271 return EmitSpecializedLibcall(DAG, dl, Chain, Dst, Src, Size,
272 Alignment.value(), RTLIB::MEMCPY, IsTailCall);
273 }
274
275 // This requires the copy size to be a constant, preferably
276 // within a subtarget-specific limit.
277 if (!ConstantSize)
278 return EmitSpecializedLibcall(DAG, dl, Chain, Dst, Src, Size,
279 Alignment.value(), RTLIB::MEMCPY, IsTailCall);
280 uint64_t SizeVal = ConstantSize->getZExtValue();
281 if (!AlwaysInline && SizeVal > Subtarget.getMaxInlineSizeThreshold())
282 return EmitSpecializedLibcall(DAG, dl, Chain, Dst, Src, Size,
283 Alignment.value(), RTLIB::MEMCPY, IsTailCall);
284
285 unsigned BytesLeft = SizeVal & 3;
286 unsigned NumMemOps = SizeVal >> 2;
287 unsigned EmittedNumMemOps = 0;
288 EVT VT = MVT::i32;
289 unsigned VTSize = 4;
290 unsigned i = 0;
291 // Emit a maximum of 4 loads in Thumb1 since we have fewer registers
292 const unsigned MaxLoadsInLDM = Subtarget.isThumb1Only() ? 4 : 6;
293 SDValue TFOps[6];
294 SDValue Loads[6];
295 uint64_t SrcOff = 0, DstOff = 0;
296
297 // FIXME: We should invent a VMEMCPY pseudo-instruction that lowers to
298 // VLDM/VSTM and make this code emit it when appropriate. This would reduce
299 // pressure on the general purpose registers. However this seems harder to map
300 // onto the register allocator's view of the world.
301
302 // The number of MEMCPY pseudo-instructions to emit. We use up to
303 // MaxLoadsInLDM registers per mcopy, which will get lowered into ldm/stm
304 // later on. This is a lower bound on the number of MEMCPY operations we must
305 // emit.
306 unsigned NumMEMCPYs = (NumMemOps + MaxLoadsInLDM - 1) / MaxLoadsInLDM;
307
308 // Code size optimisation: do not inline memcpy if expansion results in
309 // more instructions than the library call.
310 if (NumMEMCPYs > 1 && Subtarget.hasMinSize()) {
311 return EmitSpecializedLibcall(DAG, dl, Chain, Dst, Src, Size,
312 Alignment.value(), RTLIB::MEMCPY, IsTailCall);
313 }
314
315 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other, MVT::Glue);
316
317 for (unsigned I = 0; I != NumMEMCPYs; ++I) {
318 // Evenly distribute registers among MEMCPY operations to reduce register
319 // pressure.
320 unsigned NextEmittedNumMemOps = NumMemOps * (I + 1) / NumMEMCPYs;
321 unsigned NumRegs = NextEmittedNumMemOps - EmittedNumMemOps;
322
323 Dst = DAG.getNode(ARMISD::MEMCPY, dl, VTs, Chain, Dst, Src,
324 DAG.getConstant(NumRegs, dl, MVT::i32));
325 Src = Dst.getValue(1);
326 Chain = Dst.getValue(2);
327
328 DstPtrInfo = DstPtrInfo.getWithOffset(NumRegs * VTSize);
329 SrcPtrInfo = SrcPtrInfo.getWithOffset(NumRegs * VTSize);
330
331 EmittedNumMemOps = NextEmittedNumMemOps;
332 }
333
334 if (BytesLeft == 0)
335 return Chain;
336
337 // Issue loads / stores for the trailing (1 - 3) bytes.
338 auto getRemainingValueType = [](unsigned BytesLeft) {
339 return (BytesLeft >= 2) ? MVT::i16 : MVT::i8;
340 };
341 auto getRemainingSize = [](unsigned BytesLeft) {
342 return (BytesLeft >= 2) ? 2 : 1;
343 };
344
345 unsigned BytesLeftSave = BytesLeft;
346 i = 0;
347 while (BytesLeft) {
348 VT = getRemainingValueType(BytesLeft);
349 VTSize = getRemainingSize(BytesLeft);
350 Loads[i] = DAG.getLoad(VT, dl, Chain,
351 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
352 DAG.getConstant(SrcOff, dl, MVT::i32)),
353 SrcPtrInfo.getWithOffset(SrcOff));
354 TFOps[i] = Loads[i].getValue(1);
355 ++i;
356 SrcOff += VTSize;
357 BytesLeft -= VTSize;
358 }
359 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, ArrayRef(TFOps, i));
360
361 i = 0;
362 BytesLeft = BytesLeftSave;
363 while (BytesLeft) {
364 VT = getRemainingValueType(BytesLeft);
365 VTSize = getRemainingSize(BytesLeft);
366 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
367 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
368 DAG.getConstant(DstOff, dl, MVT::i32)),
369 DstPtrInfo.getWithOffset(DstOff));
370 ++i;
371 DstOff += VTSize;
372 BytesLeft -= VTSize;
373 }
374 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, ArrayRef(TFOps, i));
375}
376
378 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
379 SDValue Size, Align DstAlign, Align SrcAlign, bool isVolatile,
380 bool &IsTailCall, MachinePointerInfo DstPtrInfo,
381 MachinePointerInfo SrcPtrInfo) const {
382 Align Alignment = std::min(DstAlign, SrcAlign);
383 return EmitSpecializedLibcall(DAG, dl, Chain, Dst, Src, Size,
384 Alignment.value(), RTLIB::MEMMOVE, IsTailCall);
385}
386
388 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
389 SDValue Size, Align Alignment, bool isVolatile, bool AlwaysInline,
390 bool &IsTailCall, MachinePointerInfo DstPtrInfo) const {
391
392 const ARMSubtarget &Subtarget =
394
396
397 // Generate TP loop for llvm.memset
398 if (Subtarget.hasMVEIntegerOps() &&
399 shouldGenerateInlineTPLoop(Subtarget, DAG, ConstantSize, Alignment,
400 false)) {
401 Src = DAG.getSplatBuildVector(MVT::v16i8, dl,
402 DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Src));
403 return DAG.getNode(ARMISD::MEMSETLOOP, dl, MVT::Other, Chain, Dst, Src,
404 DAG.getZExtOrTrunc(Size, dl, MVT::i32));
405 }
406
407 if (!AlwaysInline)
408 return EmitSpecializedLibcall(DAG, dl, Chain, Dst, Src, Size,
409 Alignment.value(), RTLIB::MEMSET, IsTailCall);
410
411 IsTailCall = false;
412 return SDValue();
413}
return SDValue()
const AArch64Subtarget & Subtarget
const TargetLowering & TLI
static bool shouldGenerateInlineTPLoop(const ARMSubtarget &Subtarget, const SelectionDAG &DAG, ConstantSDNode *ConstantSize, Align Alignment, bool IsMemcpy)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define MAKE_CASE(V)
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
const char * getTargetNodeName(unsigned Opcode) const override
Returns the name of the given target-specific opcode, suitable for debug printing.
SDValue EmitSpecializedLibcall(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, SDValue Size, unsigned Align, RTLIB::Libcall LC, bool &IsTailCall) const
SDValue EmitTargetCodeForMemmove(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, SDValue Size, Align DstAlign, Align SrcAlign, bool isVolatile, bool &IsTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo) const override
Emit target-specific code that performs a memmove.
SDValue EmitTargetCodeForMemcpy(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, SDValue Size, Align DstAlign, Align SrcAlign, bool isVolatile, bool AlwaysInline, bool &IsTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo) const override
Emit target-specific code that performs a memcpy.
void verifyTargetNode(const SelectionDAG &DAG, const SDNode *N) const override
Checks that the given target-specific node is valid. Aborts if it is not.
bool isTargetMemoryOpcode(unsigned Opcode) const override
Returns true if a node with the given target-specific opcode has a memory operand.
SDValue EmitTargetCodeForMemset(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, SDValue Op3, Align Alignment, bool isVolatile, bool AlwaysInline, bool &IsTailCall, MachinePointerInfo DstPtrInfo) const override
Emit target-specific code that performs a memset.
uint64_t getZExtValue() const
LLVM_ABI IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space.
CallingConv::ID getLibcallImplCallingConv(RTLIB::LibcallImpl Call) const
Get the CallingConv that should be used for the specified libcall.
RTLIB::LibcallImpl getLibcallImpl(RTLIB::Libcall Call) const
Return the lowering's selection of implementation call for Call.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Function & getFunction()
Return the LLVM function that this machine code represents.
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
Represents one node in the SelectionDAG.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
SDValue getValue(unsigned R) const
const char * getTargetNodeName(unsigned Opcode) const override
Returns the name of the given target-specific opcode, suitable for debug printing.
SelectionDAGGenTargetInfo(const SDNodeInfo &GenNodeInfo)
bool isTargetMemoryOpcode(unsigned Opcode) const override
Returns true if a node with the given target-specific opcode has a memory operand.
void verifyTargetNode(const SelectionDAG &DAG, const SDNode *N) const override
Checks that the given target-specific node is valid. Aborts if it is not.
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
LLVM_ABI SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
const DataLayout & getDataLayout() const
LLVM_ABI SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const MMOMetadata &Metadata=MMOMetadata())
Helper function to build ISD::STORE nodes.
LLVM_ABI SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
LLVM_ABI SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const MMOMetadata &Metadata=MMOMetadata())
Loads are not normal binary operators: their result type is not determined by their operands,...
LLVM_ABI SDValue getExternalSymbol(const char *Sym, EVT VT)
const LibcallLoweringInfo & getLibcalls() const
LLVM_ABI SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
MachineFunction & getMachineFunction() const
SDValue getSplatBuildVector(EVT VT, const SDLoc &DL, SDValue Op)
Return a splat ISD::BUILD_VECTOR node, consisting of Op splatted to all elements.
LLVM_ABI SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either zero-extending or trunca...
LLVMContext * getContext() const
std::vector< ArgListEntry > ArgListTy
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:294
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:267
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:266
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:882
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:55
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:888
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI bool isNullConstant(SDValue V)
Returns true if V is a constant integer zero.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
IntPtrTy
Definition InstrProf.h:82
ArrayRef(const T &OneElt) -> ArrayRef< T >
TPLoopMemTransfer
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Extended Value Type.
Definition ValueTypes.h:35
This class contains a discriminated union of information about pointers in memory operands,...
MachinePointerInfo getWithOffset(int64_t O) const
This represents a list of ValueType's that has been intern'd by a SelectionDAG.
This structure contains all information that is necessary for lowering calls.
CallLoweringInfo & setLibCallee(CallingConv::ID CC, Type *ResultType, SDValue Target, ArgListTy &&ArgsList)
CallLoweringInfo & setDiscardResult(bool Value=true)
CallLoweringInfo & setDebugLoc(const SDLoc &dl)
CallLoweringInfo & setTailCall(bool Value=true)
CallLoweringInfo & setChain(SDValue InChain)