1 //===-- RISCVISelLowering.cpp - RISCV DAG Lowering Implementation --------===//
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 // This file defines the interfaces that RISCV uses to lower LLVM code into a
12 //===----------------------------------------------------------------------===//
14 #include "RISCVISelLowering.h"
16 #include "RISCVMachineFunctionInfo.h"
17 #include "RISCVRegisterInfo.h"
18 #include "RISCVSubtarget.h"
19 #include "RISCVTargetMachine.h"
20 #include "Utils/RISCVMatInt.h"
21 #include "llvm/ADT/SmallSet.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/CodeGen/CallingConvLower.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineFunction.h"
26 #include "llvm/CodeGen/MachineInstrBuilder.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/CodeGen/SelectionDAGISel.h"
29 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
30 #include "llvm/CodeGen/ValueTypes.h"
31 #include "llvm/IR/DiagnosticInfo.h"
32 #include "llvm/IR/DiagnosticPrinter.h"
33 #include "llvm/IR/IntrinsicsRISCV.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/raw_ostream.h"
40 #define DEBUG_TYPE "riscv-lower"
42 STATISTIC(NumTailCalls, "Number of tail calls");
44 RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
45 const RISCVSubtarget &STI)
46 : TargetLowering(TM), Subtarget(STI) {
48 if (Subtarget.isRV32E())
49 report_fatal_error("Codegen not yet implemented for RV32E");
51 RISCVABI::ABI ABI = Subtarget.getTargetABI();
52 assert(ABI != RISCVABI::ABI_Unknown && "Improperly initialised target ABI");
54 if ((ABI == RISCVABI::ABI_ILP32F || ABI == RISCVABI::ABI_LP64F) &&
55 !Subtarget.hasStdExtF()) {
56 errs() << "Hard-float 'f' ABI can't be used for a target that "
57 "doesn't support the F instruction set extension (ignoring "
59 ABI = Subtarget.is64Bit() ? RISCVABI::ABI_LP64 : RISCVABI::ABI_ILP32;
60 } else if ((ABI == RISCVABI::ABI_ILP32D || ABI == RISCVABI::ABI_LP64D) &&
61 !Subtarget.hasStdExtD()) {
62 errs() << "Hard-float 'd' ABI can't be used for a target that "
63 "doesn't support the D instruction set extension (ignoring "
65 ABI = Subtarget.is64Bit() ? RISCVABI::ABI_LP64 : RISCVABI::ABI_ILP32;
70 report_fatal_error("Don't know how to lower this ABI");
71 case RISCVABI::ABI_ILP32:
72 case RISCVABI::ABI_ILP32F:
73 case RISCVABI::ABI_ILP32D:
74 case RISCVABI::ABI_LP64:
75 case RISCVABI::ABI_LP64F:
76 case RISCVABI::ABI_LP64D:
80 MVT XLenVT = Subtarget.getXLenVT();
82 // Set up the register classes.
83 addRegisterClass(XLenVT, &RISCV::GPRRegClass);
85 if (Subtarget.hasStdExtF())
86 addRegisterClass(MVT::f32, &RISCV::FPR32RegClass);
87 if (Subtarget.hasStdExtD())
88 addRegisterClass(MVT::f64, &RISCV::FPR64RegClass);
90 // Compute derived properties from the register classes.
91 computeRegisterProperties(STI.getRegisterInfo());
93 setStackPointerRegisterToSaveRestore(RISCV::X2);
95 for (auto N : {ISD::EXTLOAD, ISD::SEXTLOAD, ISD::ZEXTLOAD})
96 setLoadExtAction(N, XLenVT, MVT::i1, Promote);
98 // TODO: add all necessary setOperationAction calls.
99 setOperationAction(ISD::DYNAMIC_STACKALLOC, XLenVT, Expand);
101 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
102 setOperationAction(ISD::BR_CC, XLenVT, Expand);
103 setOperationAction(ISD::SELECT, XLenVT, Custom);
104 setOperationAction(ISD::SELECT_CC, XLenVT, Expand);
106 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
107 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
109 setOperationAction(ISD::VASTART, MVT::Other, Custom);
110 setOperationAction(ISD::VAARG, MVT::Other, Expand);
111 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
112 setOperationAction(ISD::VAEND, MVT::Other, Expand);
114 for (auto VT : {MVT::i1, MVT::i8, MVT::i16})
115 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
117 if (Subtarget.is64Bit()) {
118 setOperationAction(ISD::ADD, MVT::i32, Custom);
119 setOperationAction(ISD::SUB, MVT::i32, Custom);
120 setOperationAction(ISD::SHL, MVT::i32, Custom);
121 setOperationAction(ISD::SRA, MVT::i32, Custom);
122 setOperationAction(ISD::SRL, MVT::i32, Custom);
125 if (!Subtarget.hasStdExtM()) {
126 setOperationAction(ISD::MUL, XLenVT, Expand);
127 setOperationAction(ISD::MULHS, XLenVT, Expand);
128 setOperationAction(ISD::MULHU, XLenVT, Expand);
129 setOperationAction(ISD::SDIV, XLenVT, Expand);
130 setOperationAction(ISD::UDIV, XLenVT, Expand);
131 setOperationAction(ISD::SREM, XLenVT, Expand);
132 setOperationAction(ISD::UREM, XLenVT, Expand);
135 if (Subtarget.is64Bit() && Subtarget.hasStdExtM()) {
136 setOperationAction(ISD::MUL, MVT::i32, Custom);
137 setOperationAction(ISD::SDIV, MVT::i32, Custom);
138 setOperationAction(ISD::UDIV, MVT::i32, Custom);
139 setOperationAction(ISD::UREM, MVT::i32, Custom);
142 setOperationAction(ISD::SDIVREM, XLenVT, Expand);
143 setOperationAction(ISD::UDIVREM, XLenVT, Expand);
144 setOperationAction(ISD::SMUL_LOHI, XLenVT, Expand);
145 setOperationAction(ISD::UMUL_LOHI, XLenVT, Expand);
147 setOperationAction(ISD::SHL_PARTS, XLenVT, Custom);
148 setOperationAction(ISD::SRL_PARTS, XLenVT, Custom);
149 setOperationAction(ISD::SRA_PARTS, XLenVT, Custom);
151 setOperationAction(ISD::ROTL, XLenVT, Expand);
152 setOperationAction(ISD::ROTR, XLenVT, Expand);
153 setOperationAction(ISD::BSWAP, XLenVT, Expand);
154 setOperationAction(ISD::CTTZ, XLenVT, Expand);
155 setOperationAction(ISD::CTLZ, XLenVT, Expand);
156 setOperationAction(ISD::CTPOP, XLenVT, Expand);
158 ISD::CondCode FPCCToExtend[] = {
159 ISD::SETOGT, ISD::SETOGE, ISD::SETONE, ISD::SETUEQ, ISD::SETUGT,
160 ISD::SETUGE, ISD::SETULT, ISD::SETULE, ISD::SETUNE, ISD::SETGT,
161 ISD::SETGE, ISD::SETNE};
163 ISD::NodeType FPOpToExtend[] = {
164 ISD::FSIN, ISD::FCOS, ISD::FSINCOS, ISD::FPOW, ISD::FREM, ISD::FP16_TO_FP,
167 if (Subtarget.hasStdExtF()) {
168 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
169 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
170 for (auto CC : FPCCToExtend)
171 setCondCodeAction(CC, MVT::f32, Expand);
172 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
173 setOperationAction(ISD::SELECT, MVT::f32, Custom);
174 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
175 for (auto Op : FPOpToExtend)
176 setOperationAction(Op, MVT::f32, Expand);
177 setLoadExtAction(ISD::EXTLOAD, MVT::f32, MVT::f16, Expand);
178 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
181 if (Subtarget.hasStdExtF() && Subtarget.is64Bit())
182 setOperationAction(ISD::BITCAST, MVT::i32, Custom);
184 if (Subtarget.hasStdExtD()) {
185 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
186 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
187 for (auto CC : FPCCToExtend)
188 setCondCodeAction(CC, MVT::f64, Expand);
189 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
190 setOperationAction(ISD::SELECT, MVT::f64, Custom);
191 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
192 setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand);
193 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
194 for (auto Op : FPOpToExtend)
195 setOperationAction(Op, MVT::f64, Expand);
196 setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f16, Expand);
197 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
200 if (Subtarget.is64Bit() &&
201 !(Subtarget.hasStdExtD() || Subtarget.hasStdExtF())) {
202 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
203 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
204 setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::i32, Custom);
205 setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::i32, Custom);
208 setOperationAction(ISD::GlobalAddress, XLenVT, Custom);
209 setOperationAction(ISD::BlockAddress, XLenVT, Custom);
210 setOperationAction(ISD::ConstantPool, XLenVT, Custom);
212 setOperationAction(ISD::GlobalTLSAddress, XLenVT, Custom);
214 // TODO: On M-mode only targets, the cycle[h] CSR may not be present.
215 // Unfortunately this can't be determined just from the ISA naming string.
216 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64,
217 Subtarget.is64Bit() ? Legal : Custom);
219 setOperationAction(ISD::TRAP, MVT::Other, Legal);
220 setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal);
222 if (Subtarget.hasStdExtA()) {
223 setMaxAtomicSizeInBitsSupported(Subtarget.getXLen());
224 setMinCmpXchgSizeInBits(32);
226 setMaxAtomicSizeInBitsSupported(0);
229 setBooleanContents(ZeroOrOneBooleanContent);
231 // Function alignments.
232 const Align FunctionAlignment(Subtarget.hasStdExtC() ? 2 : 4);
233 setMinFunctionAlignment(FunctionAlignment);
234 setPrefFunctionAlignment(FunctionAlignment);
236 // Effectively disable jump table generation.
237 setMinimumJumpTableEntries(INT_MAX);
240 EVT RISCVTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
243 return getPointerTy(DL);
244 return VT.changeVectorElementTypeToInteger();
247 bool RISCVTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
250 unsigned Intrinsic) const {
254 case Intrinsic::riscv_masked_atomicrmw_xchg_i32:
255 case Intrinsic::riscv_masked_atomicrmw_add_i32:
256 case Intrinsic::riscv_masked_atomicrmw_sub_i32:
257 case Intrinsic::riscv_masked_atomicrmw_nand_i32:
258 case Intrinsic::riscv_masked_atomicrmw_max_i32:
259 case Intrinsic::riscv_masked_atomicrmw_min_i32:
260 case Intrinsic::riscv_masked_atomicrmw_umax_i32:
261 case Intrinsic::riscv_masked_atomicrmw_umin_i32:
262 case Intrinsic::riscv_masked_cmpxchg_i32:
263 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
264 Info.opc = ISD::INTRINSIC_W_CHAIN;
265 Info.memVT = MVT::getVT(PtrTy->getElementType());
266 Info.ptrVal = I.getArgOperand(0);
268 Info.align = Align(4);
269 Info.flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore |
270 MachineMemOperand::MOVolatile;
275 bool RISCVTargetLowering::isLegalAddressingMode(const DataLayout &DL,
276 const AddrMode &AM, Type *Ty,
278 Instruction *I) const {
279 // No global is ever allowed as a base.
283 // Require a 12-bit signed offset.
284 if (!isInt<12>(AM.BaseOffs))
288 case 0: // "r+i" or just "i", depending on HasBaseReg.
291 if (!AM.HasBaseReg) // allow "r+i".
293 return false; // disallow "r+r" or "r+r+i".
301 bool RISCVTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
302 return isInt<12>(Imm);
305 bool RISCVTargetLowering::isLegalAddImmediate(int64_t Imm) const {
306 return isInt<12>(Imm);
309 // On RV32, 64-bit integers are split into their high and low parts and held
310 // in two different registers, so the trunc is free since the low register can
312 bool RISCVTargetLowering::isTruncateFree(Type *SrcTy, Type *DstTy) const {
313 if (Subtarget.is64Bit() || !SrcTy->isIntegerTy() || !DstTy->isIntegerTy())
315 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits();
316 unsigned DestBits = DstTy->getPrimitiveSizeInBits();
317 return (SrcBits == 64 && DestBits == 32);
320 bool RISCVTargetLowering::isTruncateFree(EVT SrcVT, EVT DstVT) const {
321 if (Subtarget.is64Bit() || SrcVT.isVector() || DstVT.isVector() ||
322 !SrcVT.isInteger() || !DstVT.isInteger())
324 unsigned SrcBits = SrcVT.getSizeInBits();
325 unsigned DestBits = DstVT.getSizeInBits();
326 return (SrcBits == 64 && DestBits == 32);
329 bool RISCVTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
330 // Zexts are free if they can be combined with a load.
331 if (auto *LD = dyn_cast<LoadSDNode>(Val)) {
332 EVT MemVT = LD->getMemoryVT();
333 if ((MemVT == MVT::i8 || MemVT == MVT::i16 ||
334 (Subtarget.is64Bit() && MemVT == MVT::i32)) &&
335 (LD->getExtensionType() == ISD::NON_EXTLOAD ||
336 LD->getExtensionType() == ISD::ZEXTLOAD))
340 return TargetLowering::isZExtFree(Val, VT2);
343 bool RISCVTargetLowering::isSExtCheaperThanZExt(EVT SrcVT, EVT DstVT) const {
344 return Subtarget.is64Bit() && SrcVT == MVT::i32 && DstVT == MVT::i64;
347 bool RISCVTargetLowering::hasBitPreservingFPLogic(EVT VT) const {
348 return (VT == MVT::f32 && Subtarget.hasStdExtF()) ||
349 (VT == MVT::f64 && Subtarget.hasStdExtD());
352 // Changes the condition code and swaps operands if necessary, so the SetCC
353 // operation matches one of the comparisons supported directly in the RISC-V
355 static void normaliseSetCC(SDValue &LHS, SDValue &RHS, ISD::CondCode &CC) {
363 CC = ISD::getSetCCSwappedOperands(CC);
369 // Return the RISC-V branch opcode that matches the given DAG integer
370 // condition code. The CondCode must be one of those supported by the RISC-V
371 // ISA (see normaliseSetCC).
372 static unsigned getBranchOpcodeForIntCondCode(ISD::CondCode CC) {
375 llvm_unreachable("Unsupported CondCode");
391 SDValue RISCVTargetLowering::LowerOperation(SDValue Op,
392 SelectionDAG &DAG) const {
393 switch (Op.getOpcode()) {
395 report_fatal_error("unimplemented operand");
396 case ISD::GlobalAddress:
397 return lowerGlobalAddress(Op, DAG);
398 case ISD::BlockAddress:
399 return lowerBlockAddress(Op, DAG);
400 case ISD::ConstantPool:
401 return lowerConstantPool(Op, DAG);
402 case ISD::GlobalTLSAddress:
403 return lowerGlobalTLSAddress(Op, DAG);
405 return lowerSELECT(Op, DAG);
407 return lowerVASTART(Op, DAG);
409 return lowerFRAMEADDR(Op, DAG);
410 case ISD::RETURNADDR:
411 return lowerRETURNADDR(Op, DAG);
413 return lowerShiftLeftParts(Op, DAG);
415 return lowerShiftRightParts(Op, DAG, true);
417 return lowerShiftRightParts(Op, DAG, false);
419 assert(Subtarget.is64Bit() && Subtarget.hasStdExtF() &&
420 "Unexpected custom legalisation");
422 SDValue Op0 = Op.getOperand(0);
423 if (Op.getValueType() != MVT::f32 || Op0.getValueType() != MVT::i32)
425 SDValue NewOp0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
426 SDValue FPConv = DAG.getNode(RISCVISD::FMV_W_X_RV64, DL, MVT::f32, NewOp0);
432 static SDValue getTargetNode(GlobalAddressSDNode *N, SDLoc DL, EVT Ty,
433 SelectionDAG &DAG, unsigned Flags) {
434 return DAG.getTargetGlobalAddress(N->getGlobal(), DL, Ty, 0, Flags);
437 static SDValue getTargetNode(BlockAddressSDNode *N, SDLoc DL, EVT Ty,
438 SelectionDAG &DAG, unsigned Flags) {
439 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, N->getOffset(),
443 static SDValue getTargetNode(ConstantPoolSDNode *N, SDLoc DL, EVT Ty,
444 SelectionDAG &DAG, unsigned Flags) {
445 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
446 N->getOffset(), Flags);
449 template <class NodeTy>
450 SDValue RISCVTargetLowering::getAddr(NodeTy *N, SelectionDAG &DAG,
451 bool IsLocal) const {
453 EVT Ty = getPointerTy(DAG.getDataLayout());
455 if (isPositionIndependent()) {
456 SDValue Addr = getTargetNode(N, DL, Ty, DAG, 0);
458 // Use PC-relative addressing to access the symbol. This generates the
459 // pattern (PseudoLLA sym), which expands to (addi (auipc %pcrel_hi(sym))
460 // %pcrel_lo(auipc)).
461 return SDValue(DAG.getMachineNode(RISCV::PseudoLLA, DL, Ty, Addr), 0);
463 // Use PC-relative addressing to access the GOT for this symbol, then load
464 // the address from the GOT. This generates the pattern (PseudoLA sym),
465 // which expands to (ld (addi (auipc %got_pcrel_hi(sym)) %pcrel_lo(auipc))).
466 return SDValue(DAG.getMachineNode(RISCV::PseudoLA, DL, Ty, Addr), 0);
469 switch (getTargetMachine().getCodeModel()) {
471 report_fatal_error("Unsupported code model for lowering");
472 case CodeModel::Small: {
473 // Generate a sequence for accessing addresses within the first 2 GiB of
474 // address space. This generates the pattern (addi (lui %hi(sym)) %lo(sym)).
475 SDValue AddrHi = getTargetNode(N, DL, Ty, DAG, RISCVII::MO_HI);
476 SDValue AddrLo = getTargetNode(N, DL, Ty, DAG, RISCVII::MO_LO);
477 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, AddrHi), 0);
478 return SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNHi, AddrLo), 0);
480 case CodeModel::Medium: {
481 // Generate a sequence for accessing addresses within any 2GiB range within
482 // the address space. This generates the pattern (PseudoLLA sym), which
483 // expands to (addi (auipc %pcrel_hi(sym)) %pcrel_lo(auipc)).
484 SDValue Addr = getTargetNode(N, DL, Ty, DAG, 0);
485 return SDValue(DAG.getMachineNode(RISCV::PseudoLLA, DL, Ty, Addr), 0);
490 SDValue RISCVTargetLowering::lowerGlobalAddress(SDValue Op,
491 SelectionDAG &DAG) const {
493 EVT Ty = Op.getValueType();
494 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
495 int64_t Offset = N->getOffset();
496 MVT XLenVT = Subtarget.getXLenVT();
498 const GlobalValue *GV = N->getGlobal();
499 bool IsLocal = getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
500 SDValue Addr = getAddr(N, DAG, IsLocal);
502 // In order to maximise the opportunity for common subexpression elimination,
503 // emit a separate ADD node for the global address offset instead of folding
504 // it in the global address node. Later peephole optimisations may choose to
505 // fold it back in when profitable.
507 return DAG.getNode(ISD::ADD, DL, Ty, Addr,
508 DAG.getConstant(Offset, DL, XLenVT));
512 SDValue RISCVTargetLowering::lowerBlockAddress(SDValue Op,
513 SelectionDAG &DAG) const {
514 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
516 return getAddr(N, DAG);
519 SDValue RISCVTargetLowering::lowerConstantPool(SDValue Op,
520 SelectionDAG &DAG) const {
521 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
523 return getAddr(N, DAG);
526 SDValue RISCVTargetLowering::getStaticTLSAddr(GlobalAddressSDNode *N,
530 EVT Ty = getPointerTy(DAG.getDataLayout());
531 const GlobalValue *GV = N->getGlobal();
532 MVT XLenVT = Subtarget.getXLenVT();
535 // Use PC-relative addressing to access the GOT for this TLS symbol, then
536 // load the address from the GOT and add the thread pointer. This generates
537 // the pattern (PseudoLA_TLS_IE sym), which expands to
538 // (ld (auipc %tls_ie_pcrel_hi(sym)) %pcrel_lo(auipc)).
539 SDValue Addr = DAG.getTargetGlobalAddress(GV, DL, Ty, 0, 0);
541 SDValue(DAG.getMachineNode(RISCV::PseudoLA_TLS_IE, DL, Ty, Addr), 0);
543 // Add the thread pointer.
544 SDValue TPReg = DAG.getRegister(RISCV::X4, XLenVT);
545 return DAG.getNode(ISD::ADD, DL, Ty, Load, TPReg);
548 // Generate a sequence for accessing the address relative to the thread
549 // pointer, with the appropriate adjustment for the thread pointer offset.
550 // This generates the pattern
551 // (add (add_tprel (lui %tprel_hi(sym)) tp %tprel_add(sym)) %tprel_lo(sym))
553 DAG.getTargetGlobalAddress(GV, DL, Ty, 0, RISCVII::MO_TPREL_HI);
555 DAG.getTargetGlobalAddress(GV, DL, Ty, 0, RISCVII::MO_TPREL_ADD);
557 DAG.getTargetGlobalAddress(GV, DL, Ty, 0, RISCVII::MO_TPREL_LO);
559 SDValue MNHi = SDValue(DAG.getMachineNode(RISCV::LUI, DL, Ty, AddrHi), 0);
560 SDValue TPReg = DAG.getRegister(RISCV::X4, XLenVT);
561 SDValue MNAdd = SDValue(
562 DAG.getMachineNode(RISCV::PseudoAddTPRel, DL, Ty, MNHi, TPReg, AddrAdd),
564 return SDValue(DAG.getMachineNode(RISCV::ADDI, DL, Ty, MNAdd, AddrLo), 0);
567 SDValue RISCVTargetLowering::getDynamicTLSAddr(GlobalAddressSDNode *N,
568 SelectionDAG &DAG) const {
570 EVT Ty = getPointerTy(DAG.getDataLayout());
571 IntegerType *CallTy = Type::getIntNTy(*DAG.getContext(), Ty.getSizeInBits());
572 const GlobalValue *GV = N->getGlobal();
574 // Use a PC-relative addressing mode to access the global dynamic GOT address.
575 // This generates the pattern (PseudoLA_TLS_GD sym), which expands to
576 // (addi (auipc %tls_gd_pcrel_hi(sym)) %pcrel_lo(auipc)).
577 SDValue Addr = DAG.getTargetGlobalAddress(GV, DL, Ty, 0, 0);
579 SDValue(DAG.getMachineNode(RISCV::PseudoLA_TLS_GD, DL, Ty, Addr), 0);
581 // Prepare argument list to generate call.
586 Args.push_back(Entry);
588 // Setup call to __tls_get_addr.
589 TargetLowering::CallLoweringInfo CLI(DAG);
591 .setChain(DAG.getEntryNode())
592 .setLibCallee(CallingConv::C, CallTy,
593 DAG.getExternalSymbol("__tls_get_addr", Ty),
596 return LowerCallTo(CLI).first;
599 SDValue RISCVTargetLowering::lowerGlobalTLSAddress(SDValue Op,
600 SelectionDAG &DAG) const {
602 EVT Ty = Op.getValueType();
603 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
604 int64_t Offset = N->getOffset();
605 MVT XLenVT = Subtarget.getXLenVT();
607 TLSModel::Model Model = getTargetMachine().getTLSModel(N->getGlobal());
611 case TLSModel::LocalExec:
612 Addr = getStaticTLSAddr(N, DAG, /*UseGOT=*/false);
614 case TLSModel::InitialExec:
615 Addr = getStaticTLSAddr(N, DAG, /*UseGOT=*/true);
617 case TLSModel::LocalDynamic:
618 case TLSModel::GeneralDynamic:
619 Addr = getDynamicTLSAddr(N, DAG);
623 // In order to maximise the opportunity for common subexpression elimination,
624 // emit a separate ADD node for the global address offset instead of folding
625 // it in the global address node. Later peephole optimisations may choose to
626 // fold it back in when profitable.
628 return DAG.getNode(ISD::ADD, DL, Ty, Addr,
629 DAG.getConstant(Offset, DL, XLenVT));
633 SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
634 SDValue CondV = Op.getOperand(0);
635 SDValue TrueV = Op.getOperand(1);
636 SDValue FalseV = Op.getOperand(2);
638 MVT XLenVT = Subtarget.getXLenVT();
640 // If the result type is XLenVT and CondV is the output of a SETCC node
641 // which also operated on XLenVT inputs, then merge the SETCC node into the
642 // lowered RISCVISD::SELECT_CC to take advantage of the integer
643 // compare+branch instructions. i.e.:
644 // (select (setcc lhs, rhs, cc), truev, falsev)
645 // -> (riscvisd::select_cc lhs, rhs, cc, truev, falsev)
646 if (Op.getSimpleValueType() == XLenVT && CondV.getOpcode() == ISD::SETCC &&
647 CondV.getOperand(0).getSimpleValueType() == XLenVT) {
648 SDValue LHS = CondV.getOperand(0);
649 SDValue RHS = CondV.getOperand(1);
650 auto CC = cast<CondCodeSDNode>(CondV.getOperand(2));
651 ISD::CondCode CCVal = CC->get();
653 normaliseSetCC(LHS, RHS, CCVal);
655 SDValue TargetCC = DAG.getConstant(CCVal, DL, XLenVT);
656 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
657 SDValue Ops[] = {LHS, RHS, TargetCC, TrueV, FalseV};
658 return DAG.getNode(RISCVISD::SELECT_CC, DL, VTs, Ops);
662 // (select condv, truev, falsev)
663 // -> (riscvisd::select_cc condv, zero, setne, truev, falsev)
664 SDValue Zero = DAG.getConstant(0, DL, XLenVT);
665 SDValue SetNE = DAG.getConstant(ISD::SETNE, DL, XLenVT);
667 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
668 SDValue Ops[] = {CondV, Zero, SetNE, TrueV, FalseV};
670 return DAG.getNode(RISCVISD::SELECT_CC, DL, VTs, Ops);
673 SDValue RISCVTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
674 MachineFunction &MF = DAG.getMachineFunction();
675 RISCVMachineFunctionInfo *FuncInfo = MF.getInfo<RISCVMachineFunctionInfo>();
678 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
679 getPointerTy(MF.getDataLayout()));
681 // vastart just stores the address of the VarArgsFrameIndex slot into the
682 // memory location argument.
683 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
684 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
685 MachinePointerInfo(SV));
688 SDValue RISCVTargetLowering::lowerFRAMEADDR(SDValue Op,
689 SelectionDAG &DAG) const {
690 const RISCVRegisterInfo &RI = *Subtarget.getRegisterInfo();
691 MachineFunction &MF = DAG.getMachineFunction();
692 MachineFrameInfo &MFI = MF.getFrameInfo();
693 MFI.setFrameAddressIsTaken(true);
694 Register FrameReg = RI.getFrameRegister(MF);
695 int XLenInBytes = Subtarget.getXLen() / 8;
697 EVT VT = Op.getValueType();
699 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL, FrameReg, VT);
700 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
702 int Offset = -(XLenInBytes * 2);
703 SDValue Ptr = DAG.getNode(ISD::ADD, DL, VT, FrameAddr,
704 DAG.getIntPtrConstant(Offset, DL));
706 DAG.getLoad(VT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo());
711 SDValue RISCVTargetLowering::lowerRETURNADDR(SDValue Op,
712 SelectionDAG &DAG) const {
713 const RISCVRegisterInfo &RI = *Subtarget.getRegisterInfo();
714 MachineFunction &MF = DAG.getMachineFunction();
715 MachineFrameInfo &MFI = MF.getFrameInfo();
716 MFI.setReturnAddressIsTaken(true);
717 MVT XLenVT = Subtarget.getXLenVT();
718 int XLenInBytes = Subtarget.getXLen() / 8;
720 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
723 EVT VT = Op.getValueType();
725 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
727 int Off = -XLenInBytes;
728 SDValue FrameAddr = lowerFRAMEADDR(Op, DAG);
729 SDValue Offset = DAG.getConstant(Off, DL, VT);
730 return DAG.getLoad(VT, DL, DAG.getEntryNode(),
731 DAG.getNode(ISD::ADD, DL, VT, FrameAddr, Offset),
732 MachinePointerInfo());
735 // Return the value of the return address register, marking it an implicit
737 Register Reg = MF.addLiveIn(RI.getRARegister(), getRegClassFor(XLenVT));
738 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, Reg, XLenVT);
741 SDValue RISCVTargetLowering::lowerShiftLeftParts(SDValue Op,
742 SelectionDAG &DAG) const {
744 SDValue Lo = Op.getOperand(0);
745 SDValue Hi = Op.getOperand(1);
746 SDValue Shamt = Op.getOperand(2);
747 EVT VT = Lo.getValueType();
749 // if Shamt-XLEN < 0: // Shamt < XLEN
751 // Hi = (Hi << Shamt) | ((Lo >>u 1) >>u (XLEN-1 - Shamt))
754 // Hi = Lo << (Shamt-XLEN)
756 SDValue Zero = DAG.getConstant(0, DL, VT);
757 SDValue One = DAG.getConstant(1, DL, VT);
758 SDValue MinusXLen = DAG.getConstant(-(int)Subtarget.getXLen(), DL, VT);
759 SDValue XLenMinus1 = DAG.getConstant(Subtarget.getXLen() - 1, DL, VT);
760 SDValue ShamtMinusXLen = DAG.getNode(ISD::ADD, DL, VT, Shamt, MinusXLen);
761 SDValue XLenMinus1Shamt = DAG.getNode(ISD::SUB, DL, VT, XLenMinus1, Shamt);
763 SDValue LoTrue = DAG.getNode(ISD::SHL, DL, VT, Lo, Shamt);
764 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, VT, Lo, One);
765 SDValue ShiftRightLo =
766 DAG.getNode(ISD::SRL, DL, VT, ShiftRight1Lo, XLenMinus1Shamt);
767 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, VT, Hi, Shamt);
768 SDValue HiTrue = DAG.getNode(ISD::OR, DL, VT, ShiftLeftHi, ShiftRightLo);
769 SDValue HiFalse = DAG.getNode(ISD::SHL, DL, VT, Lo, ShamtMinusXLen);
771 SDValue CC = DAG.getSetCC(DL, VT, ShamtMinusXLen, Zero, ISD::SETLT);
773 Lo = DAG.getNode(ISD::SELECT, DL, VT, CC, LoTrue, Zero);
774 Hi = DAG.getNode(ISD::SELECT, DL, VT, CC, HiTrue, HiFalse);
776 SDValue Parts[2] = {Lo, Hi};
777 return DAG.getMergeValues(Parts, DL);
780 SDValue RISCVTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
783 SDValue Lo = Op.getOperand(0);
784 SDValue Hi = Op.getOperand(1);
785 SDValue Shamt = Op.getOperand(2);
786 EVT VT = Lo.getValueType();
789 // if Shamt-XLEN < 0: // Shamt < XLEN
790 // Lo = (Lo >>u Shamt) | ((Hi << 1) << (XLEN-1 - Shamt))
793 // Lo = Hi >>s (Shamt-XLEN);
794 // Hi = Hi >>s (XLEN-1)
797 // if Shamt-XLEN < 0: // Shamt < XLEN
798 // Lo = (Lo >>u Shamt) | ((Hi << 1) << (XLEN-1 - Shamt))
801 // Lo = Hi >>u (Shamt-XLEN);
804 unsigned ShiftRightOp = IsSRA ? ISD::SRA : ISD::SRL;
806 SDValue Zero = DAG.getConstant(0, DL, VT);
807 SDValue One = DAG.getConstant(1, DL, VT);
808 SDValue MinusXLen = DAG.getConstant(-(int)Subtarget.getXLen(), DL, VT);
809 SDValue XLenMinus1 = DAG.getConstant(Subtarget.getXLen() - 1, DL, VT);
810 SDValue ShamtMinusXLen = DAG.getNode(ISD::ADD, DL, VT, Shamt, MinusXLen);
811 SDValue XLenMinus1Shamt = DAG.getNode(ISD::SUB, DL, VT, XLenMinus1, Shamt);
813 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, VT, Lo, Shamt);
814 SDValue ShiftLeftHi1 = DAG.getNode(ISD::SHL, DL, VT, Hi, One);
815 SDValue ShiftLeftHi =
816 DAG.getNode(ISD::SHL, DL, VT, ShiftLeftHi1, XLenMinus1Shamt);
817 SDValue LoTrue = DAG.getNode(ISD::OR, DL, VT, ShiftRightLo, ShiftLeftHi);
818 SDValue HiTrue = DAG.getNode(ShiftRightOp, DL, VT, Hi, Shamt);
819 SDValue LoFalse = DAG.getNode(ShiftRightOp, DL, VT, Hi, ShamtMinusXLen);
821 IsSRA ? DAG.getNode(ISD::SRA, DL, VT, Hi, XLenMinus1) : Zero;
823 SDValue CC = DAG.getSetCC(DL, VT, ShamtMinusXLen, Zero, ISD::SETLT);
825 Lo = DAG.getNode(ISD::SELECT, DL, VT, CC, LoTrue, LoFalse);
826 Hi = DAG.getNode(ISD::SELECT, DL, VT, CC, HiTrue, HiFalse);
828 SDValue Parts[2] = {Lo, Hi};
829 return DAG.getMergeValues(Parts, DL);
832 // Returns the opcode of the target-specific SDNode that implements the 32-bit
833 // form of the given Opcode.
834 static RISCVISD::NodeType getRISCVWOpcode(unsigned Opcode) {
837 llvm_unreachable("Unexpected opcode");
839 return RISCVISD::SLLW;
841 return RISCVISD::SRAW;
843 return RISCVISD::SRLW;
845 return RISCVISD::DIVW;
847 return RISCVISD::DIVUW;
849 return RISCVISD::REMUW;
853 // Converts the given 32-bit operation to a target-specific SelectionDAG node.
854 // Because i32 isn't a legal type for RV64, these operations would otherwise
855 // be promoted to i64, making it difficult to select the SLLW/DIVUW/.../*W
856 // later one because the fact the operation was originally of type i32 is
858 static SDValue customLegalizeToWOp(SDNode *N, SelectionDAG &DAG) {
860 RISCVISD::NodeType WOpcode = getRISCVWOpcode(N->getOpcode());
861 SDValue NewOp0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(0));
862 SDValue NewOp1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(1));
863 SDValue NewRes = DAG.getNode(WOpcode, DL, MVT::i64, NewOp0, NewOp1);
864 // ReplaceNodeResults requires we maintain the same type for the return value.
865 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, NewRes);
868 // Converts the given 32-bit operation to a i64 operation with signed extension
869 // semantic to reduce the signed extension instructions.
870 static SDValue customLegalizeToWOpWithSExt(SDNode *N, SelectionDAG &DAG) {
872 SDValue NewOp0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(0));
873 SDValue NewOp1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, N->getOperand(1));
874 SDValue NewWOp = DAG.getNode(N->getOpcode(), DL, MVT::i64, NewOp0, NewOp1);
875 SDValue NewRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i64, NewWOp,
876 DAG.getValueType(MVT::i32));
877 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, NewRes);
880 void RISCVTargetLowering::ReplaceNodeResults(SDNode *N,
881 SmallVectorImpl<SDValue> &Results,
882 SelectionDAG &DAG) const {
884 switch (N->getOpcode()) {
886 llvm_unreachable("Don't know how to custom type legalize this operation!");
887 case ISD::STRICT_FP_TO_SINT:
888 case ISD::STRICT_FP_TO_UINT:
889 case ISD::FP_TO_SINT:
890 case ISD::FP_TO_UINT: {
891 bool IsStrict = N->isStrictFPOpcode();
892 assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
893 "Unexpected custom legalisation");
894 SDValue Op0 = IsStrict ? N->getOperand(1) : N->getOperand(0);
896 if (N->getOpcode() == ISD::FP_TO_SINT ||
897 N->getOpcode() == ISD::STRICT_FP_TO_SINT)
898 LC = RTLIB::getFPTOSINT(Op0.getValueType(), N->getValueType(0));
900 LC = RTLIB::getFPTOUINT(Op0.getValueType(), N->getValueType(0));
901 MakeLibCallOptions CallOptions;
902 EVT OpVT = Op0.getValueType();
903 CallOptions.setTypeListBeforeSoften(OpVT, N->getValueType(0), true);
904 SDValue Chain = IsStrict ? N->getOperand(0) : SDValue();
906 std::tie(Result, Chain) =
907 makeLibCall(DAG, LC, N->getValueType(0), Op0, CallOptions, DL, Chain);
908 Results.push_back(Result);
910 Results.push_back(Chain);
913 case ISD::READCYCLECOUNTER: {
914 assert(!Subtarget.is64Bit() &&
915 "READCYCLECOUNTER only has custom type legalization on riscv32");
917 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
919 DAG.getNode(RISCVISD::READ_CYCLE_WIDE, DL, VTs, N->getOperand(0));
921 Results.push_back(RCW);
922 Results.push_back(RCW.getValue(1));
923 Results.push_back(RCW.getValue(2));
929 assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
930 "Unexpected custom legalisation");
931 if (N->getOperand(1).getOpcode() == ISD::Constant)
933 Results.push_back(customLegalizeToWOpWithSExt(N, DAG));
938 assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
939 "Unexpected custom legalisation");
940 if (N->getOperand(1).getOpcode() == ISD::Constant)
942 Results.push_back(customLegalizeToWOp(N, DAG));
947 assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
948 Subtarget.hasStdExtM() && "Unexpected custom legalisation");
949 if (N->getOperand(0).getOpcode() == ISD::Constant ||
950 N->getOperand(1).getOpcode() == ISD::Constant)
952 Results.push_back(customLegalizeToWOp(N, DAG));
955 assert(N->getValueType(0) == MVT::i32 && Subtarget.is64Bit() &&
956 Subtarget.hasStdExtF() && "Unexpected custom legalisation");
958 SDValue Op0 = N->getOperand(0);
959 if (Op0.getValueType() != MVT::f32)
962 DAG.getNode(RISCVISD::FMV_X_ANYEXTW_RV64, DL, MVT::i64, Op0);
963 Results.push_back(DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, FPConv));
969 SDValue RISCVTargetLowering::PerformDAGCombine(SDNode *N,
970 DAGCombinerInfo &DCI) const {
971 SelectionDAG &DAG = DCI.DAG;
973 switch (N->getOpcode()) {
976 case RISCVISD::SplitF64: {
977 SDValue Op0 = N->getOperand(0);
978 // If the input to SplitF64 is just BuildPairF64 then the operation is
979 // redundant. Instead, use BuildPairF64's operands directly.
980 if (Op0->getOpcode() == RISCVISD::BuildPairF64)
981 return DCI.CombineTo(N, Op0.getOperand(0), Op0.getOperand(1));
985 // It's cheaper to materialise two 32-bit integers than to load a double
986 // from the constant pool and transfer it to integer registers through the
988 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op0)) {
989 APInt V = C->getValueAPF().bitcastToAPInt();
990 SDValue Lo = DAG.getConstant(V.trunc(32), DL, MVT::i32);
991 SDValue Hi = DAG.getConstant(V.lshr(32).trunc(32), DL, MVT::i32);
992 return DCI.CombineTo(N, Lo, Hi);
995 // This is a target-specific version of a DAGCombine performed in
996 // DAGCombiner::visitBITCAST. It performs the equivalent of:
997 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
998 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
999 if (!(Op0.getOpcode() == ISD::FNEG || Op0.getOpcode() == ISD::FABS) ||
1000 !Op0.getNode()->hasOneUse())
1002 SDValue NewSplitF64 =
1003 DAG.getNode(RISCVISD::SplitF64, DL, DAG.getVTList(MVT::i32, MVT::i32),
1005 SDValue Lo = NewSplitF64.getValue(0);
1006 SDValue Hi = NewSplitF64.getValue(1);
1007 APInt SignBit = APInt::getSignMask(32);
1008 if (Op0.getOpcode() == ISD::FNEG) {
1009 SDValue NewHi = DAG.getNode(ISD::XOR, DL, MVT::i32, Hi,
1010 DAG.getConstant(SignBit, DL, MVT::i32));
1011 return DCI.CombineTo(N, Lo, NewHi);
1013 assert(Op0.getOpcode() == ISD::FABS);
1014 SDValue NewHi = DAG.getNode(ISD::AND, DL, MVT::i32, Hi,
1015 DAG.getConstant(~SignBit, DL, MVT::i32));
1016 return DCI.CombineTo(N, Lo, NewHi);
1018 case RISCVISD::SLLW:
1019 case RISCVISD::SRAW:
1020 case RISCVISD::SRLW: {
1021 // Only the lower 32 bits of LHS and lower 5 bits of RHS are read.
1022 SDValue LHS = N->getOperand(0);
1023 SDValue RHS = N->getOperand(1);
1024 APInt LHSMask = APInt::getLowBitsSet(LHS.getValueSizeInBits(), 32);
1025 APInt RHSMask = APInt::getLowBitsSet(RHS.getValueSizeInBits(), 5);
1026 if ((SimplifyDemandedBits(N->getOperand(0), LHSMask, DCI)) ||
1027 (SimplifyDemandedBits(N->getOperand(1), RHSMask, DCI)))
1031 case RISCVISD::FMV_X_ANYEXTW_RV64: {
1033 SDValue Op0 = N->getOperand(0);
1034 // If the input to FMV_X_ANYEXTW_RV64 is just FMV_W_X_RV64 then the
1035 // conversion is unnecessary and can be replaced with an ANY_EXTEND
1036 // of the FMV_W_X_RV64 operand.
1037 if (Op0->getOpcode() == RISCVISD::FMV_W_X_RV64) {
1039 DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0.getOperand(0));
1040 return DCI.CombineTo(N, AExtOp);
1043 // This is a target-specific version of a DAGCombine performed in
1044 // DAGCombiner::visitBITCAST. It performs the equivalent of:
1045 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
1046 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
1047 if (!(Op0.getOpcode() == ISD::FNEG || Op0.getOpcode() == ISD::FABS) ||
1048 !Op0.getNode()->hasOneUse())
1050 SDValue NewFMV = DAG.getNode(RISCVISD::FMV_X_ANYEXTW_RV64, DL, MVT::i64,
1052 APInt SignBit = APInt::getSignMask(32).sext(64);
1053 if (Op0.getOpcode() == ISD::FNEG) {
1054 return DCI.CombineTo(N,
1055 DAG.getNode(ISD::XOR, DL, MVT::i64, NewFMV,
1056 DAG.getConstant(SignBit, DL, MVT::i64)));
1058 assert(Op0.getOpcode() == ISD::FABS);
1059 return DCI.CombineTo(N,
1060 DAG.getNode(ISD::AND, DL, MVT::i64, NewFMV,
1061 DAG.getConstant(~SignBit, DL, MVT::i64)));
1068 bool RISCVTargetLowering::isDesirableToCommuteWithShift(
1069 const SDNode *N, CombineLevel Level) const {
1070 // The following folds are only desirable if `(OP _, c1 << c2)` can be
1071 // materialised in fewer instructions than `(OP _, c1)`:
1073 // (shl (add x, c1), c2) -> (add (shl x, c2), c1 << c2)
1074 // (shl (or x, c1), c2) -> (or (shl x, c2), c1 << c2)
1075 SDValue N0 = N->getOperand(0);
1076 EVT Ty = N0.getValueType();
1077 if (Ty.isScalarInteger() &&
1078 (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::OR)) {
1079 auto *C1 = dyn_cast<ConstantSDNode>(N0->getOperand(1));
1080 auto *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1));
1082 APInt C1Int = C1->getAPIntValue();
1083 APInt ShiftedC1Int = C1Int << C2->getAPIntValue();
1085 // We can materialise `c1 << c2` into an add immediate, so it's "free",
1086 // and the combine should happen, to potentially allow further combines
1088 if (ShiftedC1Int.getMinSignedBits() <= 64 &&
1089 isLegalAddImmediate(ShiftedC1Int.getSExtValue()))
1092 // We can materialise `c1` in an add immediate, so it's "free", and the
1093 // combine should be prevented.
1094 if (C1Int.getMinSignedBits() <= 64 &&
1095 isLegalAddImmediate(C1Int.getSExtValue()))
1098 // Neither constant will fit into an immediate, so find materialisation
1100 int C1Cost = RISCVMatInt::getIntMatCost(C1Int, Ty.getSizeInBits(),
1101 Subtarget.is64Bit());
1102 int ShiftedC1Cost = RISCVMatInt::getIntMatCost(
1103 ShiftedC1Int, Ty.getSizeInBits(), Subtarget.is64Bit());
1105 // Materialising `c1` is cheaper than materialising `c1 << c2`, so the
1106 // combine should be prevented.
1107 if (C1Cost < ShiftedC1Cost)
1114 unsigned RISCVTargetLowering::ComputeNumSignBitsForTargetNode(
1115 SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG,
1116 unsigned Depth) const {
1117 switch (Op.getOpcode()) {
1120 case RISCVISD::SLLW:
1121 case RISCVISD::SRAW:
1122 case RISCVISD::SRLW:
1123 case RISCVISD::DIVW:
1124 case RISCVISD::DIVUW:
1125 case RISCVISD::REMUW:
1126 // TODO: As the result is sign-extended, this is conservatively correct. A
1127 // more precise answer could be calculated for SRAW depending on known
1128 // bits in the shift amount.
1135 static MachineBasicBlock *emitReadCycleWidePseudo(MachineInstr &MI,
1136 MachineBasicBlock *BB) {
1137 assert(MI.getOpcode() == RISCV::ReadCycleWide && "Unexpected instruction");
1139 // To read the 64-bit cycle CSR on a 32-bit target, we read the two halves.
1140 // Should the count have wrapped while it was being read, we need to try
1144 // rdcycleh x3 # load high word of cycle
1145 // rdcycle x2 # load low word of cycle
1146 // rdcycleh x4 # load high word of cycle
1147 // bne x3, x4, read # check if high word reads match, otherwise try again
1150 MachineFunction &MF = *BB->getParent();
1151 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1152 MachineFunction::iterator It = ++BB->getIterator();
1154 MachineBasicBlock *LoopMBB = MF.CreateMachineBasicBlock(LLVM_BB);
1155 MF.insert(It, LoopMBB);
1157 MachineBasicBlock *DoneMBB = MF.CreateMachineBasicBlock(LLVM_BB);
1158 MF.insert(It, DoneMBB);
1160 // Transfer the remainder of BB and its successor edges to DoneMBB.
1161 DoneMBB->splice(DoneMBB->begin(), BB,
1162 std::next(MachineBasicBlock::iterator(MI)), BB->end());
1163 DoneMBB->transferSuccessorsAndUpdatePHIs(BB);
1165 BB->addSuccessor(LoopMBB);
1167 MachineRegisterInfo &RegInfo = MF.getRegInfo();
1168 Register ReadAgainReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
1169 Register LoReg = MI.getOperand(0).getReg();
1170 Register HiReg = MI.getOperand(1).getReg();
1171 DebugLoc DL = MI.getDebugLoc();
1173 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
1174 BuildMI(LoopMBB, DL, TII->get(RISCV::CSRRS), HiReg)
1175 .addImm(RISCVSysReg::lookupSysRegByName("CYCLEH")->Encoding)
1177 BuildMI(LoopMBB, DL, TII->get(RISCV::CSRRS), LoReg)
1178 .addImm(RISCVSysReg::lookupSysRegByName("CYCLE")->Encoding)
1180 BuildMI(LoopMBB, DL, TII->get(RISCV::CSRRS), ReadAgainReg)
1181 .addImm(RISCVSysReg::lookupSysRegByName("CYCLEH")->Encoding)
1184 BuildMI(LoopMBB, DL, TII->get(RISCV::BNE))
1186 .addReg(ReadAgainReg)
1189 LoopMBB->addSuccessor(LoopMBB);
1190 LoopMBB->addSuccessor(DoneMBB);
1192 MI.eraseFromParent();
1197 static MachineBasicBlock *emitSplitF64Pseudo(MachineInstr &MI,
1198 MachineBasicBlock *BB) {
1199 assert(MI.getOpcode() == RISCV::SplitF64Pseudo && "Unexpected instruction");
1201 MachineFunction &MF = *BB->getParent();
1202 DebugLoc DL = MI.getDebugLoc();
1203 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
1204 const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
1205 Register LoReg = MI.getOperand(0).getReg();
1206 Register HiReg = MI.getOperand(1).getReg();
1207 Register SrcReg = MI.getOperand(2).getReg();
1208 const TargetRegisterClass *SrcRC = &RISCV::FPR64RegClass;
1209 int FI = MF.getInfo<RISCVMachineFunctionInfo>()->getMoveF64FrameIndex();
1211 TII.storeRegToStackSlot(*BB, MI, SrcReg, MI.getOperand(2).isKill(), FI, SrcRC,
1213 MachineMemOperand *MMO =
1214 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF, FI),
1215 MachineMemOperand::MOLoad, 8, 8);
1216 BuildMI(*BB, MI, DL, TII.get(RISCV::LW), LoReg)
1219 .addMemOperand(MMO);
1220 BuildMI(*BB, MI, DL, TII.get(RISCV::LW), HiReg)
1223 .addMemOperand(MMO);
1224 MI.eraseFromParent(); // The pseudo instruction is gone now.
1228 static MachineBasicBlock *emitBuildPairF64Pseudo(MachineInstr &MI,
1229 MachineBasicBlock *BB) {
1230 assert(MI.getOpcode() == RISCV::BuildPairF64Pseudo &&
1231 "Unexpected instruction");
1233 MachineFunction &MF = *BB->getParent();
1234 DebugLoc DL = MI.getDebugLoc();
1235 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
1236 const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
1237 Register DstReg = MI.getOperand(0).getReg();
1238 Register LoReg = MI.getOperand(1).getReg();
1239 Register HiReg = MI.getOperand(2).getReg();
1240 const TargetRegisterClass *DstRC = &RISCV::FPR64RegClass;
1241 int FI = MF.getInfo<RISCVMachineFunctionInfo>()->getMoveF64FrameIndex();
1243 MachineMemOperand *MMO =
1244 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF, FI),
1245 MachineMemOperand::MOStore, 8, 8);
1246 BuildMI(*BB, MI, DL, TII.get(RISCV::SW))
1247 .addReg(LoReg, getKillRegState(MI.getOperand(1).isKill()))
1250 .addMemOperand(MMO);
1251 BuildMI(*BB, MI, DL, TII.get(RISCV::SW))
1252 .addReg(HiReg, getKillRegState(MI.getOperand(2).isKill()))
1255 .addMemOperand(MMO);
1256 TII.loadRegFromStackSlot(*BB, MI, DstReg, FI, DstRC, RI);
1257 MI.eraseFromParent(); // The pseudo instruction is gone now.
1261 static bool isSelectPseudo(MachineInstr &MI) {
1262 switch (MI.getOpcode()) {
1265 case RISCV::Select_GPR_Using_CC_GPR:
1266 case RISCV::Select_FPR32_Using_CC_GPR:
1267 case RISCV::Select_FPR64_Using_CC_GPR:
1272 static MachineBasicBlock *emitSelectPseudo(MachineInstr &MI,
1273 MachineBasicBlock *BB) {
1274 // To "insert" Select_* instructions, we actually have to insert the triangle
1275 // control-flow pattern. The incoming instructions know the destination vreg
1276 // to set, the condition code register to branch on, the true/false values to
1277 // select between, and the condcode to use to select the appropriate branch.
1279 // We produce the following control flow:
1286 // When we find a sequence of selects we attempt to optimize their emission
1287 // by sharing the control flow. Currently we only handle cases where we have
1288 // multiple selects with the exact same condition (same LHS, RHS and CC).
1289 // The selects may be interleaved with other instructions if the other
1290 // instructions meet some requirements we deem safe:
1291 // - They are debug instructions. Otherwise,
1292 // - They do not have side-effects, do not access memory and their inputs do
1293 // not depend on the results of the select pseudo-instructions.
1294 // The TrueV/FalseV operands of the selects cannot depend on the result of
1295 // previous selects in the sequence.
1296 // These conditions could be further relaxed. See the X86 target for a
1297 // related approach and more information.
1298 Register LHS = MI.getOperand(1).getReg();
1299 Register RHS = MI.getOperand(2).getReg();
1300 auto CC = static_cast<ISD::CondCode>(MI.getOperand(3).getImm());
1302 SmallVector<MachineInstr *, 4> SelectDebugValues;
1303 SmallSet<Register, 4> SelectDests;
1304 SelectDests.insert(MI.getOperand(0).getReg());
1306 MachineInstr *LastSelectPseudo = &MI;
1308 for (auto E = BB->end(), SequenceMBBI = MachineBasicBlock::iterator(MI);
1309 SequenceMBBI != E; ++SequenceMBBI) {
1310 if (SequenceMBBI->isDebugInstr())
1312 else if (isSelectPseudo(*SequenceMBBI)) {
1313 if (SequenceMBBI->getOperand(1).getReg() != LHS ||
1314 SequenceMBBI->getOperand(2).getReg() != RHS ||
1315 SequenceMBBI->getOperand(3).getImm() != CC ||
1316 SelectDests.count(SequenceMBBI->getOperand(4).getReg()) ||
1317 SelectDests.count(SequenceMBBI->getOperand(5).getReg()))
1319 LastSelectPseudo = &*SequenceMBBI;
1320 SequenceMBBI->collectDebugValues(SelectDebugValues);
1321 SelectDests.insert(SequenceMBBI->getOperand(0).getReg());
1323 if (SequenceMBBI->hasUnmodeledSideEffects() ||
1324 SequenceMBBI->mayLoadOrStore())
1326 if (llvm::any_of(SequenceMBBI->operands(), [&](MachineOperand &MO) {
1327 return MO.isReg() && MO.isUse() && SelectDests.count(MO.getReg());
1333 const TargetInstrInfo &TII = *BB->getParent()->getSubtarget().getInstrInfo();
1334 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1335 DebugLoc DL = MI.getDebugLoc();
1336 MachineFunction::iterator I = ++BB->getIterator();
1338 MachineBasicBlock *HeadMBB = BB;
1339 MachineFunction *F = BB->getParent();
1340 MachineBasicBlock *TailMBB = F->CreateMachineBasicBlock(LLVM_BB);
1341 MachineBasicBlock *IfFalseMBB = F->CreateMachineBasicBlock(LLVM_BB);
1343 F->insert(I, IfFalseMBB);
1344 F->insert(I, TailMBB);
1346 // Transfer debug instructions associated with the selects to TailMBB.
1347 for (MachineInstr *DebugInstr : SelectDebugValues) {
1348 TailMBB->push_back(DebugInstr->removeFromParent());
1351 // Move all instructions after the sequence to TailMBB.
1352 TailMBB->splice(TailMBB->end(), HeadMBB,
1353 std::next(LastSelectPseudo->getIterator()), HeadMBB->end());
1354 // Update machine-CFG edges by transferring all successors of the current
1355 // block to the new block which will contain the Phi nodes for the selects.
1356 TailMBB->transferSuccessorsAndUpdatePHIs(HeadMBB);
1357 // Set the successors for HeadMBB.
1358 HeadMBB->addSuccessor(IfFalseMBB);
1359 HeadMBB->addSuccessor(TailMBB);
1361 // Insert appropriate branch.
1362 unsigned Opcode = getBranchOpcodeForIntCondCode(CC);
1364 BuildMI(HeadMBB, DL, TII.get(Opcode))
1369 // IfFalseMBB just falls through to TailMBB.
1370 IfFalseMBB->addSuccessor(TailMBB);
1372 // Create PHIs for all of the select pseudo-instructions.
1373 auto SelectMBBI = MI.getIterator();
1374 auto SelectEnd = std::next(LastSelectPseudo->getIterator());
1375 auto InsertionPoint = TailMBB->begin();
1376 while (SelectMBBI != SelectEnd) {
1377 auto Next = std::next(SelectMBBI);
1378 if (isSelectPseudo(*SelectMBBI)) {
1379 // %Result = phi [ %TrueValue, HeadMBB ], [ %FalseValue, IfFalseMBB ]
1380 BuildMI(*TailMBB, InsertionPoint, SelectMBBI->getDebugLoc(),
1381 TII.get(RISCV::PHI), SelectMBBI->getOperand(0).getReg())
1382 .addReg(SelectMBBI->getOperand(4).getReg())
1384 .addReg(SelectMBBI->getOperand(5).getReg())
1385 .addMBB(IfFalseMBB);
1386 SelectMBBI->eraseFromParent();
1391 F->getProperties().reset(MachineFunctionProperties::Property::NoPHIs);
1396 RISCVTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
1397 MachineBasicBlock *BB) const {
1398 switch (MI.getOpcode()) {
1400 llvm_unreachable("Unexpected instr type to insert");
1401 case RISCV::ReadCycleWide:
1402 assert(!Subtarget.is64Bit() &&
1403 "ReadCycleWrite is only to be used on riscv32");
1404 return emitReadCycleWidePseudo(MI, BB);
1405 case RISCV::Select_GPR_Using_CC_GPR:
1406 case RISCV::Select_FPR32_Using_CC_GPR:
1407 case RISCV::Select_FPR64_Using_CC_GPR:
1408 return emitSelectPseudo(MI, BB);
1409 case RISCV::BuildPairF64Pseudo:
1410 return emitBuildPairF64Pseudo(MI, BB);
1411 case RISCV::SplitF64Pseudo:
1412 return emitSplitF64Pseudo(MI, BB);
1416 // Calling Convention Implementation.
1417 // The expectations for frontend ABI lowering vary from target to target.
1418 // Ideally, an LLVM frontend would be able to avoid worrying about many ABI
1419 // details, but this is a longer term goal. For now, we simply try to keep the
1420 // role of the frontend as simple and well-defined as possible. The rules can
1421 // be summarised as:
1422 // * Never split up large scalar arguments. We handle them here.
1423 // * If a hardfloat calling convention is being used, and the struct may be
1424 // passed in a pair of registers (fp+fp, int+fp), and both registers are
1425 // available, then pass as two separate arguments. If either the GPRs or FPRs
1426 // are exhausted, then pass according to the rule below.
1427 // * If a struct could never be passed in registers or directly in a stack
1428 // slot (as it is larger than 2*XLEN and the floating point rules don't
1429 // apply), then pass it using a pointer with the byval attribute.
1430 // * If a struct is less than 2*XLEN, then coerce to either a two-element
1431 // word-sized array or a 2*XLEN scalar (depending on alignment).
1432 // * The frontend can determine whether a struct is returned by reference or
1433 // not based on its size and fields. If it will be returned by reference, the
1434 // frontend must modify the prototype so a pointer with the sret annotation is
1435 // passed as the first argument. This is not necessary for large scalar
1437 // * Struct return values and varargs should be coerced to structs containing
1438 // register-size fields in the same situations they would be for fixed
1441 static const MCPhysReg ArgGPRs[] = {
1442 RISCV::X10, RISCV::X11, RISCV::X12, RISCV::X13,
1443 RISCV::X14, RISCV::X15, RISCV::X16, RISCV::X17
1445 static const MCPhysReg ArgFPR32s[] = {
1446 RISCV::F10_F, RISCV::F11_F, RISCV::F12_F, RISCV::F13_F,
1447 RISCV::F14_F, RISCV::F15_F, RISCV::F16_F, RISCV::F17_F
1449 static const MCPhysReg ArgFPR64s[] = {
1450 RISCV::F10_D, RISCV::F11_D, RISCV::F12_D, RISCV::F13_D,
1451 RISCV::F14_D, RISCV::F15_D, RISCV::F16_D, RISCV::F17_D
1454 // Pass a 2*XLEN argument that has been split into two XLEN values through
1455 // registers or the stack as necessary.
1456 static bool CC_RISCVAssign2XLen(unsigned XLen, CCState &State, CCValAssign VA1,
1457 ISD::ArgFlagsTy ArgFlags1, unsigned ValNo2,
1458 MVT ValVT2, MVT LocVT2,
1459 ISD::ArgFlagsTy ArgFlags2) {
1460 unsigned XLenInBytes = XLen / 8;
1461 if (Register Reg = State.AllocateReg(ArgGPRs)) {
1462 // At least one half can be passed via register.
1463 State.addLoc(CCValAssign::getReg(VA1.getValNo(), VA1.getValVT(), Reg,
1464 VA1.getLocVT(), CCValAssign::Full));
1466 // Both halves must be passed on the stack, with proper alignment.
1467 unsigned StackAlign = std::max(XLenInBytes, ArgFlags1.getOrigAlign());
1469 CCValAssign::getMem(VA1.getValNo(), VA1.getValVT(),
1470 State.AllocateStack(XLenInBytes, StackAlign),
1471 VA1.getLocVT(), CCValAssign::Full));
1472 State.addLoc(CCValAssign::getMem(
1473 ValNo2, ValVT2, State.AllocateStack(XLenInBytes, XLenInBytes), LocVT2,
1474 CCValAssign::Full));
1478 if (Register Reg = State.AllocateReg(ArgGPRs)) {
1479 // The second half can also be passed via register.
1481 CCValAssign::getReg(ValNo2, ValVT2, Reg, LocVT2, CCValAssign::Full));
1483 // The second half is passed via the stack, without additional alignment.
1484 State.addLoc(CCValAssign::getMem(
1485 ValNo2, ValVT2, State.AllocateStack(XLenInBytes, XLenInBytes), LocVT2,
1486 CCValAssign::Full));
1492 // Implements the RISC-V calling convention. Returns true upon failure.
1493 static bool CC_RISCV(const DataLayout &DL, RISCVABI::ABI ABI, unsigned ValNo,
1494 MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo,
1495 ISD::ArgFlagsTy ArgFlags, CCState &State, bool IsFixed,
1496 bool IsRet, Type *OrigTy) {
1497 unsigned XLen = DL.getLargestLegalIntTypeSizeInBits();
1498 assert(XLen == 32 || XLen == 64);
1499 MVT XLenVT = XLen == 32 ? MVT::i32 : MVT::i64;
1501 // Any return value split in to more than two values can't be returned
1503 if (IsRet && ValNo > 1)
1506 // UseGPRForF32 if targeting one of the soft-float ABIs, if passing a
1507 // variadic argument, or if no F32 argument registers are available.
1508 bool UseGPRForF32 = true;
1509 // UseGPRForF64 if targeting soft-float ABIs or an FLEN=32 ABI, if passing a
1510 // variadic argument, or if no F64 argument registers are available.
1511 bool UseGPRForF64 = true;
1515 llvm_unreachable("Unexpected ABI");
1516 case RISCVABI::ABI_ILP32:
1517 case RISCVABI::ABI_LP64:
1519 case RISCVABI::ABI_ILP32F:
1520 case RISCVABI::ABI_LP64F:
1521 UseGPRForF32 = !IsFixed;
1523 case RISCVABI::ABI_ILP32D:
1524 case RISCVABI::ABI_LP64D:
1525 UseGPRForF32 = !IsFixed;
1526 UseGPRForF64 = !IsFixed;
1530 if (State.getFirstUnallocated(ArgFPR32s) == array_lengthof(ArgFPR32s))
1531 UseGPRForF32 = true;
1532 if (State.getFirstUnallocated(ArgFPR64s) == array_lengthof(ArgFPR64s))
1533 UseGPRForF64 = true;
1535 // From this point on, rely on UseGPRForF32, UseGPRForF64 and similar local
1536 // variables rather than directly checking against the target ABI.
1538 if (UseGPRForF32 && ValVT == MVT::f32) {
1540 LocInfo = CCValAssign::BCvt;
1541 } else if (UseGPRForF64 && XLen == 64 && ValVT == MVT::f64) {
1543 LocInfo = CCValAssign::BCvt;
1546 // If this is a variadic argument, the RISC-V calling convention requires
1547 // that it is assigned an 'even' or 'aligned' register if it has 8-byte
1548 // alignment (RV32) or 16-byte alignment (RV64). An aligned register should
1549 // be used regardless of whether the original argument was split during
1550 // legalisation or not. The argument will not be passed by registers if the
1551 // original type is larger than 2*XLEN, so the register alignment rule does
1553 unsigned TwoXLenInBytes = (2 * XLen) / 8;
1554 if (!IsFixed && ArgFlags.getOrigAlign() == TwoXLenInBytes &&
1555 DL.getTypeAllocSize(OrigTy) == TwoXLenInBytes) {
1556 unsigned RegIdx = State.getFirstUnallocated(ArgGPRs);
1557 // Skip 'odd' register if necessary.
1558 if (RegIdx != array_lengthof(ArgGPRs) && RegIdx % 2 == 1)
1559 State.AllocateReg(ArgGPRs);
1562 SmallVectorImpl<CCValAssign> &PendingLocs = State.getPendingLocs();
1563 SmallVectorImpl<ISD::ArgFlagsTy> &PendingArgFlags =
1564 State.getPendingArgFlags();
1566 assert(PendingLocs.size() == PendingArgFlags.size() &&
1567 "PendingLocs and PendingArgFlags out of sync");
1569 // Handle passing f64 on RV32D with a soft float ABI or when floating point
1570 // registers are exhausted.
1571 if (UseGPRForF64 && XLen == 32 && ValVT == MVT::f64) {
1572 assert(!ArgFlags.isSplit() && PendingLocs.empty() &&
1573 "Can't lower f64 if it is split");
1574 // Depending on available argument GPRS, f64 may be passed in a pair of
1575 // GPRs, split between a GPR and the stack, or passed completely on the
1576 // stack. LowerCall/LowerFormalArguments/LowerReturn must recognise these
1578 Register Reg = State.AllocateReg(ArgGPRs);
1581 unsigned StackOffset = State.AllocateStack(8, 8);
1583 CCValAssign::getMem(ValNo, ValVT, StackOffset, LocVT, LocInfo));
1586 if (!State.AllocateReg(ArgGPRs))
1587 State.AllocateStack(4, 4);
1588 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
1592 // Split arguments might be passed indirectly, so keep track of the pending
1594 if (ArgFlags.isSplit() || !PendingLocs.empty()) {
1596 LocInfo = CCValAssign::Indirect;
1597 PendingLocs.push_back(
1598 CCValAssign::getPending(ValNo, ValVT, LocVT, LocInfo));
1599 PendingArgFlags.push_back(ArgFlags);
1600 if (!ArgFlags.isSplitEnd()) {
1605 // If the split argument only had two elements, it should be passed directly
1606 // in registers or on the stack.
1607 if (ArgFlags.isSplitEnd() && PendingLocs.size() <= 2) {
1608 assert(PendingLocs.size() == 2 && "Unexpected PendingLocs.size()");
1609 // Apply the normal calling convention rules to the first half of the
1611 CCValAssign VA = PendingLocs[0];
1612 ISD::ArgFlagsTy AF = PendingArgFlags[0];
1613 PendingLocs.clear();
1614 PendingArgFlags.clear();
1615 return CC_RISCVAssign2XLen(XLen, State, VA, AF, ValNo, ValVT, LocVT,
1619 // Allocate to a register if possible, or else a stack slot.
1621 if (ValVT == MVT::f32 && !UseGPRForF32)
1622 Reg = State.AllocateReg(ArgFPR32s, ArgFPR64s);
1623 else if (ValVT == MVT::f64 && !UseGPRForF64)
1624 Reg = State.AllocateReg(ArgFPR64s, ArgFPR32s);
1626 Reg = State.AllocateReg(ArgGPRs);
1627 unsigned StackOffset = Reg ? 0 : State.AllocateStack(XLen / 8, XLen / 8);
1629 // If we reach this point and PendingLocs is non-empty, we must be at the
1630 // end of a split argument that must be passed indirectly.
1631 if (!PendingLocs.empty()) {
1632 assert(ArgFlags.isSplitEnd() && "Expected ArgFlags.isSplitEnd()");
1633 assert(PendingLocs.size() > 2 && "Unexpected PendingLocs.size()");
1635 for (auto &It : PendingLocs) {
1637 It.convertToReg(Reg);
1639 It.convertToMem(StackOffset);
1642 PendingLocs.clear();
1643 PendingArgFlags.clear();
1647 assert((!UseGPRForF32 || !UseGPRForF64 || LocVT == XLenVT) &&
1648 "Expected an XLenVT at this stage");
1651 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
1655 // When an f32 or f64 is passed on the stack, no bit-conversion is needed.
1656 if (ValVT == MVT::f32 || ValVT == MVT::f64) {
1658 LocInfo = CCValAssign::Full;
1660 State.addLoc(CCValAssign::getMem(ValNo, ValVT, StackOffset, LocVT, LocInfo));
1664 void RISCVTargetLowering::analyzeInputArgs(
1665 MachineFunction &MF, CCState &CCInfo,
1666 const SmallVectorImpl<ISD::InputArg> &Ins, bool IsRet) const {
1667 unsigned NumArgs = Ins.size();
1668 FunctionType *FType = MF.getFunction().getFunctionType();
1670 for (unsigned i = 0; i != NumArgs; ++i) {
1671 MVT ArgVT = Ins[i].VT;
1672 ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
1674 Type *ArgTy = nullptr;
1676 ArgTy = FType->getReturnType();
1677 else if (Ins[i].isOrigArg())
1678 ArgTy = FType->getParamType(Ins[i].getOrigArgIndex());
1680 RISCVABI::ABI ABI = MF.getSubtarget<RISCVSubtarget>().getTargetABI();
1681 if (CC_RISCV(MF.getDataLayout(), ABI, i, ArgVT, ArgVT, CCValAssign::Full,
1682 ArgFlags, CCInfo, /*IsRet=*/true, IsRet, ArgTy)) {
1683 LLVM_DEBUG(dbgs() << "InputArg #" << i << " has unhandled type "
1684 << EVT(ArgVT).getEVTString() << '\n');
1685 llvm_unreachable(nullptr);
1690 void RISCVTargetLowering::analyzeOutputArgs(
1691 MachineFunction &MF, CCState &CCInfo,
1692 const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsRet,
1693 CallLoweringInfo *CLI) const {
1694 unsigned NumArgs = Outs.size();
1696 for (unsigned i = 0; i != NumArgs; i++) {
1697 MVT ArgVT = Outs[i].VT;
1698 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
1699 Type *OrigTy = CLI ? CLI->getArgs()[Outs[i].OrigArgIndex].Ty : nullptr;
1701 RISCVABI::ABI ABI = MF.getSubtarget<RISCVSubtarget>().getTargetABI();
1702 if (CC_RISCV(MF.getDataLayout(), ABI, i, ArgVT, ArgVT, CCValAssign::Full,
1703 ArgFlags, CCInfo, Outs[i].IsFixed, IsRet, OrigTy)) {
1704 LLVM_DEBUG(dbgs() << "OutputArg #" << i << " has unhandled type "
1705 << EVT(ArgVT).getEVTString() << "\n");
1706 llvm_unreachable(nullptr);
1711 // Convert Val to a ValVT. Should not be called for CCValAssign::Indirect
1713 static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDValue Val,
1714 const CCValAssign &VA, const SDLoc &DL) {
1715 switch (VA.getLocInfo()) {
1717 llvm_unreachable("Unexpected CCValAssign::LocInfo");
1718 case CCValAssign::Full:
1720 case CCValAssign::BCvt:
1721 if (VA.getLocVT() == MVT::i64 && VA.getValVT() == MVT::f32) {
1722 Val = DAG.getNode(RISCVISD::FMV_W_X_RV64, DL, MVT::f32, Val);
1725 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val);
1731 // The caller is responsible for loading the full value if the argument is
1732 // passed with CCValAssign::Indirect.
1733 static SDValue unpackFromRegLoc(SelectionDAG &DAG, SDValue Chain,
1734 const CCValAssign &VA, const SDLoc &DL) {
1735 MachineFunction &MF = DAG.getMachineFunction();
1736 MachineRegisterInfo &RegInfo = MF.getRegInfo();
1737 EVT LocVT = VA.getLocVT();
1739 const TargetRegisterClass *RC;
1741 switch (LocVT.getSimpleVT().SimpleTy) {
1743 llvm_unreachable("Unexpected register type");
1746 RC = &RISCV::GPRRegClass;
1749 RC = &RISCV::FPR32RegClass;
1752 RC = &RISCV::FPR64RegClass;
1756 Register VReg = RegInfo.createVirtualRegister(RC);
1757 RegInfo.addLiveIn(VA.getLocReg(), VReg);
1758 Val = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
1760 if (VA.getLocInfo() == CCValAssign::Indirect)
1763 return convertLocVTToValVT(DAG, Val, VA, DL);
1766 static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDValue Val,
1767 const CCValAssign &VA, const SDLoc &DL) {
1768 EVT LocVT = VA.getLocVT();
1770 switch (VA.getLocInfo()) {
1772 llvm_unreachable("Unexpected CCValAssign::LocInfo");
1773 case CCValAssign::Full:
1775 case CCValAssign::BCvt:
1776 if (VA.getLocVT() == MVT::i64 && VA.getValVT() == MVT::f32) {
1777 Val = DAG.getNode(RISCVISD::FMV_X_ANYEXTW_RV64, DL, MVT::i64, Val);
1780 Val = DAG.getNode(ISD::BITCAST, DL, LocVT, Val);
1786 // The caller is responsible for loading the full value if the argument is
1787 // passed with CCValAssign::Indirect.
1788 static SDValue unpackFromMemLoc(SelectionDAG &DAG, SDValue Chain,
1789 const CCValAssign &VA, const SDLoc &DL) {
1790 MachineFunction &MF = DAG.getMachineFunction();
1791 MachineFrameInfo &MFI = MF.getFrameInfo();
1792 EVT LocVT = VA.getLocVT();
1793 EVT ValVT = VA.getValVT();
1794 EVT PtrVT = MVT::getIntegerVT(DAG.getDataLayout().getPointerSizeInBits(0));
1795 int FI = MFI.CreateFixedObject(ValVT.getSizeInBits() / 8,
1796 VA.getLocMemOffset(), /*Immutable=*/true);
1797 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
1800 ISD::LoadExtType ExtType;
1801 switch (VA.getLocInfo()) {
1803 llvm_unreachable("Unexpected CCValAssign::LocInfo");
1804 case CCValAssign::Full:
1805 case CCValAssign::Indirect:
1806 case CCValAssign::BCvt:
1807 ExtType = ISD::NON_EXTLOAD;
1810 Val = DAG.getExtLoad(
1811 ExtType, DL, LocVT, Chain, FIN,
1812 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), ValVT);
1816 static SDValue unpackF64OnRV32DSoftABI(SelectionDAG &DAG, SDValue Chain,
1817 const CCValAssign &VA, const SDLoc &DL) {
1818 assert(VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64 &&
1820 MachineFunction &MF = DAG.getMachineFunction();
1821 MachineFrameInfo &MFI = MF.getFrameInfo();
1822 MachineRegisterInfo &RegInfo = MF.getRegInfo();
1824 if (VA.isMemLoc()) {
1825 // f64 is passed on the stack.
1826 int FI = MFI.CreateFixedObject(8, VA.getLocMemOffset(), /*Immutable=*/true);
1827 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
1828 return DAG.getLoad(MVT::f64, DL, Chain, FIN,
1829 MachinePointerInfo::getFixedStack(MF, FI));
1832 assert(VA.isRegLoc() && "Expected register VA assignment");
1834 Register LoVReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
1835 RegInfo.addLiveIn(VA.getLocReg(), LoVReg);
1836 SDValue Lo = DAG.getCopyFromReg(Chain, DL, LoVReg, MVT::i32);
1838 if (VA.getLocReg() == RISCV::X17) {
1839 // Second half of f64 is passed on the stack.
1840 int FI = MFI.CreateFixedObject(4, 0, /*Immutable=*/true);
1841 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
1842 Hi = DAG.getLoad(MVT::i32, DL, Chain, FIN,
1843 MachinePointerInfo::getFixedStack(MF, FI));
1845 // Second half of f64 is passed in another GPR.
1846 Register HiVReg = RegInfo.createVirtualRegister(&RISCV::GPRRegClass);
1847 RegInfo.addLiveIn(VA.getLocReg() + 1, HiVReg);
1848 Hi = DAG.getCopyFromReg(Chain, DL, HiVReg, MVT::i32);
1850 return DAG.getNode(RISCVISD::BuildPairF64, DL, MVT::f64, Lo, Hi);
1853 // FastCC has less than 1% performance improvement for some particular
1854 // benchmark. But theoretically, it may has benenfit for some cases.
1855 static bool CC_RISCV_FastCC(unsigned ValNo, MVT ValVT, MVT LocVT,
1856 CCValAssign::LocInfo LocInfo,
1857 ISD::ArgFlagsTy ArgFlags, CCState &State) {
1859 if (LocVT == MVT::i32 || LocVT == MVT::i64) {
1860 // X5 and X6 might be used for save-restore libcall.
1861 static const MCPhysReg GPRList[] = {
1862 RISCV::X10, RISCV::X11, RISCV::X12, RISCV::X13, RISCV::X14,
1863 RISCV::X15, RISCV::X16, RISCV::X17, RISCV::X7, RISCV::X28,
1864 RISCV::X29, RISCV::X30, RISCV::X31};
1865 if (unsigned Reg = State.AllocateReg(GPRList)) {
1866 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
1871 if (LocVT == MVT::f32) {
1872 static const MCPhysReg FPR32List[] = {
1873 RISCV::F10_F, RISCV::F11_F, RISCV::F12_F, RISCV::F13_F, RISCV::F14_F,
1874 RISCV::F15_F, RISCV::F16_F, RISCV::F17_F, RISCV::F0_F, RISCV::F1_F,
1875 RISCV::F2_F, RISCV::F3_F, RISCV::F4_F, RISCV::F5_F, RISCV::F6_F,
1876 RISCV::F7_F, RISCV::F28_F, RISCV::F29_F, RISCV::F30_F, RISCV::F31_F};
1877 if (unsigned Reg = State.AllocateReg(FPR32List)) {
1878 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
1883 if (LocVT == MVT::f64) {
1884 static const MCPhysReg FPR64List[] = {
1885 RISCV::F10_D, RISCV::F11_D, RISCV::F12_D, RISCV::F13_D, RISCV::F14_D,
1886 RISCV::F15_D, RISCV::F16_D, RISCV::F17_D, RISCV::F0_D, RISCV::F1_D,
1887 RISCV::F2_D, RISCV::F3_D, RISCV::F4_D, RISCV::F5_D, RISCV::F6_D,
1888 RISCV::F7_D, RISCV::F28_D, RISCV::F29_D, RISCV::F30_D, RISCV::F31_D};
1889 if (unsigned Reg = State.AllocateReg(FPR64List)) {
1890 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
1895 if (LocVT == MVT::i32 || LocVT == MVT::f32) {
1896 unsigned Offset4 = State.AllocateStack(4, 4);
1897 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset4, LocVT, LocInfo));
1901 if (LocVT == MVT::i64 || LocVT == MVT::f64) {
1902 unsigned Offset5 = State.AllocateStack(8, 8);
1903 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset5, LocVT, LocInfo));
1907 return true; // CC didn't match.
1910 // Transform physical registers into virtual registers.
1911 SDValue RISCVTargetLowering::LowerFormalArguments(
1912 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
1913 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
1914 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
1918 report_fatal_error("Unsupported calling convention");
1919 case CallingConv::C:
1920 case CallingConv::Fast:
1924 MachineFunction &MF = DAG.getMachineFunction();
1926 const Function &Func = MF.getFunction();
1927 if (Func.hasFnAttribute("interrupt")) {
1928 if (!Func.arg_empty())
1930 "Functions with the interrupt attribute cannot have arguments!");
1933 MF.getFunction().getFnAttribute("interrupt").getValueAsString();
1935 if (!(Kind == "user" || Kind == "supervisor" || Kind == "machine"))
1937 "Function interrupt attribute argument not supported!");
1940 EVT PtrVT = getPointerTy(DAG.getDataLayout());
1941 MVT XLenVT = Subtarget.getXLenVT();
1942 unsigned XLenInBytes = Subtarget.getXLen() / 8;
1943 // Used with vargs to acumulate store chains.
1944 std::vector<SDValue> OutChains;
1946 // Assign locations to all of the incoming arguments.
1947 SmallVector<CCValAssign, 16> ArgLocs;
1948 CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
1950 if (CallConv == CallingConv::Fast)
1951 CCInfo.AnalyzeFormalArguments(Ins, CC_RISCV_FastCC);
1953 analyzeInputArgs(MF, CCInfo, Ins, /*IsRet=*/false);
1955 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1956 CCValAssign &VA = ArgLocs[i];
1958 // Passing f64 on RV32D with a soft float ABI must be handled as a special
1960 if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64)
1961 ArgValue = unpackF64OnRV32DSoftABI(DAG, Chain, VA, DL);
1962 else if (VA.isRegLoc())
1963 ArgValue = unpackFromRegLoc(DAG, Chain, VA, DL);
1965 ArgValue = unpackFromMemLoc(DAG, Chain, VA, DL);
1967 if (VA.getLocInfo() == CCValAssign::Indirect) {
1968 // If the original argument was split and passed by reference (e.g. i128
1969 // on RV32), we need to load all parts of it here (using the same
1971 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain, ArgValue,
1972 MachinePointerInfo()));
1973 unsigned ArgIndex = Ins[i].OrigArgIndex;
1974 assert(Ins[i].PartOffset == 0);
1975 while (i + 1 != e && Ins[i + 1].OrigArgIndex == ArgIndex) {
1976 CCValAssign &PartVA = ArgLocs[i + 1];
1977 unsigned PartOffset = Ins[i + 1].PartOffset;
1978 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue,
1979 DAG.getIntPtrConstant(PartOffset, DL));
1980 InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain, Address,
1981 MachinePointerInfo()));
1986 InVals.push_back(ArgValue);
1990 ArrayRef<MCPhysReg> ArgRegs = makeArrayRef(ArgGPRs);
1991 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs);
1992 const TargetRegisterClass *RC = &RISCV::GPRRegClass;
1993 MachineFrameInfo &MFI = MF.getFrameInfo();
1994 MachineRegisterInfo &RegInfo = MF.getRegInfo();
1995 RISCVMachineFunctionInfo *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
1997 // Offset of the first variable argument from stack pointer, and size of
1998 // the vararg save area. For now, the varargs save area is either zero or
1999 // large enough to hold a0-a7.
2000 int VaArgOffset, VarArgsSaveSize;
2002 // If all registers are allocated, then all varargs must be passed on the
2003 // stack and we don't need to save any argregs.
2004 if (ArgRegs.size() == Idx) {
2005 VaArgOffset = CCInfo.getNextStackOffset();
2006 VarArgsSaveSize = 0;
2008 VarArgsSaveSize = XLenInBytes * (ArgRegs.size() - Idx);
2009 VaArgOffset = -VarArgsSaveSize;
2012 // Record the frame index of the first variable argument
2013 // which is a value necessary to VASTART.
2014 int FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset, true);
2015 RVFI->setVarArgsFrameIndex(FI);
2017 // If saving an odd number of registers then create an extra stack slot to
2018 // ensure that the frame pointer is 2*XLEN-aligned, which in turn ensures
2019 // offsets to even-numbered registered remain 2*XLEN-aligned.
2021 MFI.CreateFixedObject(XLenInBytes, VaArgOffset - (int)XLenInBytes, true);
2022 VarArgsSaveSize += XLenInBytes;
2025 // Copy the integer registers that may have been used for passing varargs
2026 // to the vararg save area.
2027 for (unsigned I = Idx; I < ArgRegs.size();
2028 ++I, VaArgOffset += XLenInBytes) {
2029 const Register Reg = RegInfo.createVirtualRegister(RC);
2030 RegInfo.addLiveIn(ArgRegs[I], Reg);
2031 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, XLenVT);
2032 FI = MFI.CreateFixedObject(XLenInBytes, VaArgOffset, true);
2033 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
2034 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
2035 MachinePointerInfo::getFixedStack(MF, FI));
2036 cast<StoreSDNode>(Store.getNode())
2038 ->setValue((Value *)nullptr);
2039 OutChains.push_back(Store);
2041 RVFI->setVarArgsSaveSize(VarArgsSaveSize);
2044 // All stores are grouped in one node to allow the matching between
2045 // the size of Ins and InVals. This only happens for vararg functions.
2046 if (!OutChains.empty()) {
2047 OutChains.push_back(Chain);
2048 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
2054 /// isEligibleForTailCallOptimization - Check whether the call is eligible
2055 /// for tail call optimization.
2056 /// Note: This is modelled after ARM's IsEligibleForTailCallOptimization.
2057 bool RISCVTargetLowering::isEligibleForTailCallOptimization(
2058 CCState &CCInfo, CallLoweringInfo &CLI, MachineFunction &MF,
2059 const SmallVector<CCValAssign, 16> &ArgLocs) const {
2061 auto &Callee = CLI.Callee;
2062 auto CalleeCC = CLI.CallConv;
2063 auto &Outs = CLI.Outs;
2064 auto &Caller = MF.getFunction();
2065 auto CallerCC = Caller.getCallingConv();
2067 // Exception-handling functions need a special set of instructions to
2068 // indicate a return to the hardware. Tail-calling another function would
2069 // probably break this.
2070 // TODO: The "interrupt" attribute isn't currently defined by RISC-V. This
2071 // should be expanded as new function attributes are introduced.
2072 if (Caller.hasFnAttribute("interrupt"))
2075 // Do not tail call opt if the stack is used to pass parameters.
2076 if (CCInfo.getNextStackOffset() != 0)
2079 // Do not tail call opt if any parameters need to be passed indirectly.
2080 // Since long doubles (fp128) and i128 are larger than 2*XLEN, they are
2081 // passed indirectly. So the address of the value will be passed in a
2082 // register, or if not available, then the address is put on the stack. In
2083 // order to pass indirectly, space on the stack often needs to be allocated
2084 // in order to store the value. In this case the CCInfo.getNextStackOffset()
2085 // != 0 check is not enough and we need to check if any CCValAssign ArgsLocs
2086 // are passed CCValAssign::Indirect.
2087 for (auto &VA : ArgLocs)
2088 if (VA.getLocInfo() == CCValAssign::Indirect)
2091 // Do not tail call opt if either caller or callee uses struct return
2093 auto IsCallerStructRet = Caller.hasStructRetAttr();
2094 auto IsCalleeStructRet = Outs.empty() ? false : Outs[0].Flags.isSRet();
2095 if (IsCallerStructRet || IsCalleeStructRet)
2098 // Externally-defined functions with weak linkage should not be
2099 // tail-called. The behaviour of branch instructions in this situation (as
2100 // used for tail calls) is implementation-defined, so we cannot rely on the
2101 // linker replacing the tail call with a return.
2102 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2103 const GlobalValue *GV = G->getGlobal();
2104 if (GV->hasExternalWeakLinkage())
2108 // The callee has to preserve all registers the caller needs to preserve.
2109 const RISCVRegisterInfo *TRI = Subtarget.getRegisterInfo();
2110 const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC);
2111 if (CalleeCC != CallerCC) {
2112 const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC);
2113 if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved))
2117 // Byval parameters hand the function a pointer directly into the stack area
2118 // we want to reuse during a tail call. Working around this *is* possible
2119 // but less efficient and uglier in LowerCall.
2120 for (auto &Arg : Outs)
2121 if (Arg.Flags.isByVal())
2127 // Lower a call to a callseq_start + CALL + callseq_end chain, and add input
2128 // and output parameter nodes.
2129 SDValue RISCVTargetLowering::LowerCall(CallLoweringInfo &CLI,
2130 SmallVectorImpl<SDValue> &InVals) const {
2131 SelectionDAG &DAG = CLI.DAG;
2133 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2134 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2135 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
2136 SDValue Chain = CLI.Chain;
2137 SDValue Callee = CLI.Callee;
2138 bool &IsTailCall = CLI.IsTailCall;
2139 CallingConv::ID CallConv = CLI.CallConv;
2140 bool IsVarArg = CLI.IsVarArg;
2141 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2142 MVT XLenVT = Subtarget.getXLenVT();
2144 MachineFunction &MF = DAG.getMachineFunction();
2146 // Analyze the operands of the call, assigning locations to each operand.
2147 SmallVector<CCValAssign, 16> ArgLocs;
2148 CCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
2150 if (CallConv == CallingConv::Fast)
2151 ArgCCInfo.AnalyzeCallOperands(Outs, CC_RISCV_FastCC);
2153 analyzeOutputArgs(MF, ArgCCInfo, Outs, /*IsRet=*/false, &CLI);
2155 // Check if it's really possible to do a tail call.
2157 IsTailCall = isEligibleForTailCallOptimization(ArgCCInfo, CLI, MF, ArgLocs);
2161 else if (CLI.CS && CLI.CS.isMustTailCall())
2162 report_fatal_error("failed to perform tail call elimination on a call "
2163 "site marked musttail");
2165 // Get a count of how many bytes are to be pushed on the stack.
2166 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
2168 // Create local copies for byval args
2169 SmallVector<SDValue, 8> ByValArgs;
2170 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
2171 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2172 if (!Flags.isByVal())
2175 SDValue Arg = OutVals[i];
2176 unsigned Size = Flags.getByValSize();
2177 unsigned Align = Flags.getByValAlign();
2179 int FI = MF.getFrameInfo().CreateStackObject(Size, Align, /*isSS=*/false);
2180 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
2181 SDValue SizeNode = DAG.getConstant(Size, DL, XLenVT);
2183 Chain = DAG.getMemcpy(Chain, DL, FIPtr, Arg, SizeNode, Align,
2184 /*IsVolatile=*/false,
2185 /*AlwaysInline=*/false,
2186 IsTailCall, MachinePointerInfo(),
2187 MachinePointerInfo());
2188 ByValArgs.push_back(FIPtr);
2192 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, CLI.DL);
2194 // Copy argument values to their designated locations.
2195 SmallVector<std::pair<Register, SDValue>, 8> RegsToPass;
2196 SmallVector<SDValue, 8> MemOpChains;
2198 for (unsigned i = 0, j = 0, e = ArgLocs.size(); i != e; ++i) {
2199 CCValAssign &VA = ArgLocs[i];
2200 SDValue ArgValue = OutVals[i];
2201 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2203 // Handle passing f64 on RV32D with a soft float ABI as a special case.
2204 bool IsF64OnRV32DSoftABI =
2205 VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64;
2206 if (IsF64OnRV32DSoftABI && VA.isRegLoc()) {
2207 SDValue SplitF64 = DAG.getNode(
2208 RISCVISD::SplitF64, DL, DAG.getVTList(MVT::i32, MVT::i32), ArgValue);
2209 SDValue Lo = SplitF64.getValue(0);
2210 SDValue Hi = SplitF64.getValue(1);
2212 Register RegLo = VA.getLocReg();
2213 RegsToPass.push_back(std::make_pair(RegLo, Lo));
2215 if (RegLo == RISCV::X17) {
2216 // Second half of f64 is passed on the stack.
2217 // Work out the address of the stack slot.
2218 if (!StackPtr.getNode())
2219 StackPtr = DAG.getCopyFromReg(Chain, DL, RISCV::X2, PtrVT);
2221 MemOpChains.push_back(
2222 DAG.getStore(Chain, DL, Hi, StackPtr, MachinePointerInfo()));
2224 // Second half of f64 is passed in another GPR.
2225 assert(RegLo < RISCV::X31 && "Invalid register pair");
2226 Register RegHigh = RegLo + 1;
2227 RegsToPass.push_back(std::make_pair(RegHigh, Hi));
2232 // IsF64OnRV32DSoftABI && VA.isMemLoc() is handled below in the same way
2233 // as any other MemLoc.
2235 // Promote the value if needed.
2236 // For now, only handle fully promoted and indirect arguments.
2237 if (VA.getLocInfo() == CCValAssign::Indirect) {
2238 // Store the argument in a stack slot and pass its address.
2239 SDValue SpillSlot = DAG.CreateStackTemporary(Outs[i].ArgVT);
2240 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
2241 MemOpChains.push_back(
2242 DAG.getStore(Chain, DL, ArgValue, SpillSlot,
2243 MachinePointerInfo::getFixedStack(MF, FI)));
2244 // If the original argument was split (e.g. i128), we need
2245 // to store all parts of it here (and pass just one address).
2246 unsigned ArgIndex = Outs[i].OrigArgIndex;
2247 assert(Outs[i].PartOffset == 0);
2248 while (i + 1 != e && Outs[i + 1].OrigArgIndex == ArgIndex) {
2249 SDValue PartValue = OutVals[i + 1];
2250 unsigned PartOffset = Outs[i + 1].PartOffset;
2251 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, SpillSlot,
2252 DAG.getIntPtrConstant(PartOffset, DL));
2253 MemOpChains.push_back(
2254 DAG.getStore(Chain, DL, PartValue, Address,
2255 MachinePointerInfo::getFixedStack(MF, FI)));
2258 ArgValue = SpillSlot;
2260 ArgValue = convertValVTToLocVT(DAG, ArgValue, VA, DL);
2263 // Use local copy if it is a byval arg.
2264 if (Flags.isByVal())
2265 ArgValue = ByValArgs[j++];
2267 if (VA.isRegLoc()) {
2268 // Queue up the argument copies and emit them at the end.
2269 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
2271 assert(VA.isMemLoc() && "Argument not register or memory");
2272 assert(!IsTailCall && "Tail call not allowed if stack is used "
2273 "for passing parameters");
2275 // Work out the address of the stack slot.
2276 if (!StackPtr.getNode())
2277 StackPtr = DAG.getCopyFromReg(Chain, DL, RISCV::X2, PtrVT);
2279 DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
2280 DAG.getIntPtrConstant(VA.getLocMemOffset(), DL));
2283 MemOpChains.push_back(
2284 DAG.getStore(Chain, DL, ArgValue, Address, MachinePointerInfo()));
2288 // Join the stores, which are independent of one another.
2289 if (!MemOpChains.empty())
2290 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
2294 // Build a sequence of copy-to-reg nodes, chained and glued together.
2295 for (auto &Reg : RegsToPass) {
2296 Chain = DAG.getCopyToReg(Chain, DL, Reg.first, Reg.second, Glue);
2297 Glue = Chain.getValue(1);
2300 // Validate that none of the argument registers have been marked as
2301 // reserved, if so report an error. Do the same for the return address if this
2302 // is not a tailcall.
2303 validateCCReservedRegs(RegsToPass, MF);
2305 MF.getSubtarget<RISCVSubtarget>().isRegisterReservedByUser(RISCV::X1))
2306 MF.getFunction().getContext().diagnose(DiagnosticInfoUnsupported{
2308 "Return address register required, but has been reserved."});
2310 // If the callee is a GlobalAddress/ExternalSymbol node, turn it into a
2311 // TargetGlobalAddress/TargetExternalSymbol node so that legalize won't
2312 // split it and then direct call can be matched by PseudoCALL.
2313 if (GlobalAddressSDNode *S = dyn_cast<GlobalAddressSDNode>(Callee)) {
2314 const GlobalValue *GV = S->getGlobal();
2316 unsigned OpFlags = RISCVII::MO_CALL;
2317 if (!getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV))
2318 OpFlags = RISCVII::MO_PLT;
2320 Callee = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, OpFlags);
2321 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
2322 unsigned OpFlags = RISCVII::MO_CALL;
2324 if (!getTargetMachine().shouldAssumeDSOLocal(*MF.getFunction().getParent(),
2326 OpFlags = RISCVII::MO_PLT;
2328 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), PtrVT, OpFlags);
2331 // The first call operand is the chain and the second is the target address.
2332 SmallVector<SDValue, 8> Ops;
2333 Ops.push_back(Chain);
2334 Ops.push_back(Callee);
2336 // Add argument registers to the end of the list so that they are
2337 // known live into the call.
2338 for (auto &Reg : RegsToPass)
2339 Ops.push_back(DAG.getRegister(Reg.first, Reg.second.getValueType()));
2342 // Add a register mask operand representing the call-preserved registers.
2343 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
2344 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
2345 assert(Mask && "Missing call preserved mask for calling convention");
2346 Ops.push_back(DAG.getRegisterMask(Mask));
2349 // Glue the call to the argument copies, if any.
2351 Ops.push_back(Glue);
2354 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2357 MF.getFrameInfo().setHasTailCall();
2358 return DAG.getNode(RISCVISD::TAIL, DL, NodeTys, Ops);
2361 Chain = DAG.getNode(RISCVISD::CALL, DL, NodeTys, Ops);
2362 Glue = Chain.getValue(1);
2364 // Mark the end of the call, which is glued to the call itself.
2365 Chain = DAG.getCALLSEQ_END(Chain,
2366 DAG.getConstant(NumBytes, DL, PtrVT, true),
2367 DAG.getConstant(0, DL, PtrVT, true),
2369 Glue = Chain.getValue(1);
2371 // Assign locations to each value returned by this call.
2372 SmallVector<CCValAssign, 16> RVLocs;
2373 CCState RetCCInfo(CallConv, IsVarArg, MF, RVLocs, *DAG.getContext());
2374 analyzeInputArgs(MF, RetCCInfo, Ins, /*IsRet=*/true);
2376 // Copy all of the result registers out of their specified physreg.
2377 for (auto &VA : RVLocs) {
2378 // Copy the value out
2380 DAG.getCopyFromReg(Chain, DL, VA.getLocReg(), VA.getLocVT(), Glue);
2381 // Glue the RetValue to the end of the call sequence
2382 Chain = RetValue.getValue(1);
2383 Glue = RetValue.getValue(2);
2385 if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64) {
2386 assert(VA.getLocReg() == ArgGPRs[0] && "Unexpected reg assignment");
2388 DAG.getCopyFromReg(Chain, DL, ArgGPRs[1], MVT::i32, Glue);
2389 Chain = RetValue2.getValue(1);
2390 Glue = RetValue2.getValue(2);
2391 RetValue = DAG.getNode(RISCVISD::BuildPairF64, DL, MVT::f64, RetValue,
2395 RetValue = convertLocVTToValVT(DAG, RetValue, VA, DL);
2397 InVals.push_back(RetValue);
2403 bool RISCVTargetLowering::CanLowerReturn(
2404 CallingConv::ID CallConv, MachineFunction &MF, bool IsVarArg,
2405 const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context) const {
2406 SmallVector<CCValAssign, 16> RVLocs;
2407 CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context);
2408 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
2409 MVT VT = Outs[i].VT;
2410 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
2411 RISCVABI::ABI ABI = MF.getSubtarget<RISCVSubtarget>().getTargetABI();
2412 if (CC_RISCV(MF.getDataLayout(), ABI, i, VT, VT, CCValAssign::Full,
2413 ArgFlags, CCInfo, /*IsFixed=*/true, /*IsRet=*/true, nullptr))
2420 RISCVTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
2422 const SmallVectorImpl<ISD::OutputArg> &Outs,
2423 const SmallVectorImpl<SDValue> &OutVals,
2424 const SDLoc &DL, SelectionDAG &DAG) const {
2425 const MachineFunction &MF = DAG.getMachineFunction();
2426 const RISCVSubtarget &STI = MF.getSubtarget<RISCVSubtarget>();
2428 // Stores the assignment of the return value to a location.
2429 SmallVector<CCValAssign, 16> RVLocs;
2431 // Info about the registers and stack slot.
2432 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs,
2435 analyzeOutputArgs(DAG.getMachineFunction(), CCInfo, Outs, /*IsRet=*/true,
2439 SmallVector<SDValue, 4> RetOps(1, Chain);
2441 // Copy the result values into the output registers.
2442 for (unsigned i = 0, e = RVLocs.size(); i < e; ++i) {
2443 SDValue Val = OutVals[i];
2444 CCValAssign &VA = RVLocs[i];
2445 assert(VA.isRegLoc() && "Can only return in registers!");
2447 if (VA.getLocVT() == MVT::i32 && VA.getValVT() == MVT::f64) {
2448 // Handle returning f64 on RV32D with a soft float ABI.
2449 assert(VA.isRegLoc() && "Expected return via registers");
2450 SDValue SplitF64 = DAG.getNode(RISCVISD::SplitF64, DL,
2451 DAG.getVTList(MVT::i32, MVT::i32), Val);
2452 SDValue Lo = SplitF64.getValue(0);
2453 SDValue Hi = SplitF64.getValue(1);
2454 Register RegLo = VA.getLocReg();
2455 assert(RegLo < RISCV::X31 && "Invalid register pair");
2456 Register RegHi = RegLo + 1;
2458 if (STI.isRegisterReservedByUser(RegLo) ||
2459 STI.isRegisterReservedByUser(RegHi))
2460 MF.getFunction().getContext().diagnose(DiagnosticInfoUnsupported{
2462 "Return value register required, but has been reserved."});
2464 Chain = DAG.getCopyToReg(Chain, DL, RegLo, Lo, Glue);
2465 Glue = Chain.getValue(1);
2466 RetOps.push_back(DAG.getRegister(RegLo, MVT::i32));
2467 Chain = DAG.getCopyToReg(Chain, DL, RegHi, Hi, Glue);
2468 Glue = Chain.getValue(1);
2469 RetOps.push_back(DAG.getRegister(RegHi, MVT::i32));
2471 // Handle a 'normal' return.
2472 Val = convertValVTToLocVT(DAG, Val, VA, DL);
2473 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Glue);
2475 if (STI.isRegisterReservedByUser(VA.getLocReg()))
2476 MF.getFunction().getContext().diagnose(DiagnosticInfoUnsupported{
2478 "Return value register required, but has been reserved."});
2480 // Guarantee that all emitted copies are stuck together.
2481 Glue = Chain.getValue(1);
2482 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
2486 RetOps[0] = Chain; // Update chain.
2488 // Add the glue node if we have it.
2489 if (Glue.getNode()) {
2490 RetOps.push_back(Glue);
2493 // Interrupt service routines use different return instructions.
2494 const Function &Func = DAG.getMachineFunction().getFunction();
2495 if (Func.hasFnAttribute("interrupt")) {
2496 if (!Func.getReturnType()->isVoidTy())
2498 "Functions with the interrupt attribute must have void return type!");
2500 MachineFunction &MF = DAG.getMachineFunction();
2502 MF.getFunction().getFnAttribute("interrupt").getValueAsString();
2506 RetOpc = RISCVISD::URET_FLAG;
2507 else if (Kind == "supervisor")
2508 RetOpc = RISCVISD::SRET_FLAG;
2510 RetOpc = RISCVISD::MRET_FLAG;
2512 return DAG.getNode(RetOpc, DL, MVT::Other, RetOps);
2515 return DAG.getNode(RISCVISD::RET_FLAG, DL, MVT::Other, RetOps);
2518 void RISCVTargetLowering::validateCCReservedRegs(
2519 const SmallVectorImpl<std::pair<llvm::Register, llvm::SDValue>> &Regs,
2520 MachineFunction &MF) const {
2521 const Function &F = MF.getFunction();
2522 const RISCVSubtarget &STI = MF.getSubtarget<RISCVSubtarget>();
2524 if (std::any_of(std::begin(Regs), std::end(Regs), [&STI](auto Reg) {
2525 return STI.isRegisterReservedByUser(Reg.first);
2527 F.getContext().diagnose(DiagnosticInfoUnsupported{
2528 F, "Argument register required, but has been reserved."});
2531 const char *RISCVTargetLowering::getTargetNodeName(unsigned Opcode) const {
2532 switch ((RISCVISD::NodeType)Opcode) {
2533 case RISCVISD::FIRST_NUMBER:
2535 case RISCVISD::RET_FLAG:
2536 return "RISCVISD::RET_FLAG";
2537 case RISCVISD::URET_FLAG:
2538 return "RISCVISD::URET_FLAG";
2539 case RISCVISD::SRET_FLAG:
2540 return "RISCVISD::SRET_FLAG";
2541 case RISCVISD::MRET_FLAG:
2542 return "RISCVISD::MRET_FLAG";
2543 case RISCVISD::CALL:
2544 return "RISCVISD::CALL";
2545 case RISCVISD::SELECT_CC:
2546 return "RISCVISD::SELECT_CC";
2547 case RISCVISD::BuildPairF64:
2548 return "RISCVISD::BuildPairF64";
2549 case RISCVISD::SplitF64:
2550 return "RISCVISD::SplitF64";
2551 case RISCVISD::TAIL:
2552 return "RISCVISD::TAIL";
2553 case RISCVISD::SLLW:
2554 return "RISCVISD::SLLW";
2555 case RISCVISD::SRAW:
2556 return "RISCVISD::SRAW";
2557 case RISCVISD::SRLW:
2558 return "RISCVISD::SRLW";
2559 case RISCVISD::DIVW:
2560 return "RISCVISD::DIVW";
2561 case RISCVISD::DIVUW:
2562 return "RISCVISD::DIVUW";
2563 case RISCVISD::REMUW:
2564 return "RISCVISD::REMUW";
2565 case RISCVISD::FMV_W_X_RV64:
2566 return "RISCVISD::FMV_W_X_RV64";
2567 case RISCVISD::FMV_X_ANYEXTW_RV64:
2568 return "RISCVISD::FMV_X_ANYEXTW_RV64";
2569 case RISCVISD::READ_CYCLE_WIDE:
2570 return "RISCVISD::READ_CYCLE_WIDE";
2575 /// getConstraintType - Given a constraint letter, return the type of
2576 /// constraint it is for this target.
2577 RISCVTargetLowering::ConstraintType
2578 RISCVTargetLowering::getConstraintType(StringRef Constraint) const {
2579 if (Constraint.size() == 1) {
2580 switch (Constraint[0]) {
2584 return C_RegisterClass;
2593 return TargetLowering::getConstraintType(Constraint);
2596 std::pair<unsigned, const TargetRegisterClass *>
2597 RISCVTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
2598 StringRef Constraint,
2600 // First, see if this is a constraint that directly corresponds to a
2601 // RISCV register class.
2602 if (Constraint.size() == 1) {
2603 switch (Constraint[0]) {
2605 return std::make_pair(0U, &RISCV::GPRRegClass);
2607 if (Subtarget.hasStdExtF() && VT == MVT::f32)
2608 return std::make_pair(0U, &RISCV::FPR32RegClass);
2609 if (Subtarget.hasStdExtD() && VT == MVT::f64)
2610 return std::make_pair(0U, &RISCV::FPR64RegClass);
2617 // Clang will correctly decode the usage of register name aliases into their
2618 // official names. However, other frontends like `rustc` do not. This allows
2619 // users of these frontends to use the ABI names for registers in LLVM-style
2620 // register constraints.
2621 Register XRegFromAlias = StringSwitch<Register>(Constraint.lower())
2622 .Case("{zero}", RISCV::X0)
2623 .Case("{ra}", RISCV::X1)
2624 .Case("{sp}", RISCV::X2)
2625 .Case("{gp}", RISCV::X3)
2626 .Case("{tp}", RISCV::X4)
2627 .Case("{t0}", RISCV::X5)
2628 .Case("{t1}", RISCV::X6)
2629 .Case("{t2}", RISCV::X7)
2630 .Cases("{s0}", "{fp}", RISCV::X8)
2631 .Case("{s1}", RISCV::X9)
2632 .Case("{a0}", RISCV::X10)
2633 .Case("{a1}", RISCV::X11)
2634 .Case("{a2}", RISCV::X12)
2635 .Case("{a3}", RISCV::X13)
2636 .Case("{a4}", RISCV::X14)
2637 .Case("{a5}", RISCV::X15)
2638 .Case("{a6}", RISCV::X16)
2639 .Case("{a7}", RISCV::X17)
2640 .Case("{s2}", RISCV::X18)
2641 .Case("{s3}", RISCV::X19)
2642 .Case("{s4}", RISCV::X20)
2643 .Case("{s5}", RISCV::X21)
2644 .Case("{s6}", RISCV::X22)
2645 .Case("{s7}", RISCV::X23)
2646 .Case("{s8}", RISCV::X24)
2647 .Case("{s9}", RISCV::X25)
2648 .Case("{s10}", RISCV::X26)
2649 .Case("{s11}", RISCV::X27)
2650 .Case("{t3}", RISCV::X28)
2651 .Case("{t4}", RISCV::X29)
2652 .Case("{t5}", RISCV::X30)
2653 .Case("{t6}", RISCV::X31)
2654 .Default(RISCV::NoRegister);
2655 if (XRegFromAlias != RISCV::NoRegister)
2656 return std::make_pair(XRegFromAlias, &RISCV::GPRRegClass);
2658 // Since TargetLowering::getRegForInlineAsmConstraint uses the name of the
2659 // TableGen record rather than the AsmName to choose registers for InlineAsm
2660 // constraints, plus we want to match those names to the widest floating point
2661 // register type available, manually select floating point registers here.
2663 // The second case is the ABI name of the register, so that frontends can also
2664 // use the ABI names in register constraint lists.
2665 if (Subtarget.hasStdExtF() || Subtarget.hasStdExtD()) {
2666 std::pair<Register, Register> FReg =
2667 StringSwitch<std::pair<Register, Register>>(Constraint.lower())
2668 .Cases("{f0}", "{ft0}", {RISCV::F0_F, RISCV::F0_D})
2669 .Cases("{f1}", "{ft1}", {RISCV::F1_F, RISCV::F1_D})
2670 .Cases("{f2}", "{ft2}", {RISCV::F2_F, RISCV::F2_D})
2671 .Cases("{f3}", "{ft3}", {RISCV::F3_F, RISCV::F3_D})
2672 .Cases("{f4}", "{ft4}", {RISCV::F4_F, RISCV::F4_D})
2673 .Cases("{f5}", "{ft5}", {RISCV::F5_F, RISCV::F5_D})
2674 .Cases("{f6}", "{ft6}", {RISCV::F6_F, RISCV::F6_D})
2675 .Cases("{f7}", "{ft7}", {RISCV::F7_F, RISCV::F7_D})
2676 .Cases("{f8}", "{fs0}", {RISCV::F8_F, RISCV::F8_D})
2677 .Cases("{f9}", "{fs1}", {RISCV::F9_F, RISCV::F9_D})
2678 .Cases("{f10}", "{fa0}", {RISCV::F10_F, RISCV::F10_D})
2679 .Cases("{f11}", "{fa1}", {RISCV::F11_F, RISCV::F11_D})
2680 .Cases("{f12}", "{fa2}", {RISCV::F12_F, RISCV::F12_D})
2681 .Cases("{f13}", "{fa3}", {RISCV::F13_F, RISCV::F13_D})
2682 .Cases("{f14}", "{fa4}", {RISCV::F14_F, RISCV::F14_D})
2683 .Cases("{f15}", "{fa5}", {RISCV::F15_F, RISCV::F15_D})
2684 .Cases("{f16}", "{fa6}", {RISCV::F16_F, RISCV::F16_D})
2685 .Cases("{f17}", "{fa7}", {RISCV::F17_F, RISCV::F17_D})
2686 .Cases("{f18}", "{fs2}", {RISCV::F18_F, RISCV::F18_D})
2687 .Cases("{f19}", "{fs3}", {RISCV::F19_F, RISCV::F19_D})
2688 .Cases("{f20}", "{fs4}", {RISCV::F20_F, RISCV::F20_D})
2689 .Cases("{f21}", "{fs5}", {RISCV::F21_F, RISCV::F21_D})
2690 .Cases("{f22}", "{fs6}", {RISCV::F22_F, RISCV::F22_D})
2691 .Cases("{f23}", "{fs7}", {RISCV::F23_F, RISCV::F23_D})
2692 .Cases("{f24}", "{fs8}", {RISCV::F24_F, RISCV::F24_D})
2693 .Cases("{f25}", "{fs9}", {RISCV::F25_F, RISCV::F25_D})
2694 .Cases("{f26}", "{fs10}", {RISCV::F26_F, RISCV::F26_D})
2695 .Cases("{f27}", "{fs11}", {RISCV::F27_F, RISCV::F27_D})
2696 .Cases("{f28}", "{ft8}", {RISCV::F28_F, RISCV::F28_D})
2697 .Cases("{f29}", "{ft9}", {RISCV::F29_F, RISCV::F29_D})
2698 .Cases("{f30}", "{ft10}", {RISCV::F30_F, RISCV::F30_D})
2699 .Cases("{f31}", "{ft11}", {RISCV::F31_F, RISCV::F31_D})
2700 .Default({RISCV::NoRegister, RISCV::NoRegister});
2701 if (FReg.first != RISCV::NoRegister)
2702 return Subtarget.hasStdExtD()
2703 ? std::make_pair(FReg.second, &RISCV::FPR64RegClass)
2704 : std::make_pair(FReg.first, &RISCV::FPR32RegClass);
2707 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
2711 RISCVTargetLowering::getInlineAsmMemConstraint(StringRef ConstraintCode) const {
2712 // Currently only support length 1 constraints.
2713 if (ConstraintCode.size() == 1) {
2714 switch (ConstraintCode[0]) {
2716 return InlineAsm::Constraint_A;
2722 return TargetLowering::getInlineAsmMemConstraint(ConstraintCode);
2725 void RISCVTargetLowering::LowerAsmOperandForConstraint(
2726 SDValue Op, std::string &Constraint, std::vector<SDValue> &Ops,
2727 SelectionDAG &DAG) const {
2728 // Currently only support length 1 constraints.
2729 if (Constraint.length() == 1) {
2730 switch (Constraint[0]) {
2732 // Validate & create a 12-bit signed immediate operand.
2733 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
2734 uint64_t CVal = C->getSExtValue();
2735 if (isInt<12>(CVal))
2737 DAG.getTargetConstant(CVal, SDLoc(Op), Subtarget.getXLenVT()));
2741 // Validate & create an integer zero operand.
2742 if (auto *C = dyn_cast<ConstantSDNode>(Op))
2743 if (C->getZExtValue() == 0)
2745 DAG.getTargetConstant(0, SDLoc(Op), Subtarget.getXLenVT()));
2748 // Validate & create a 5-bit unsigned immediate operand.
2749 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
2750 uint64_t CVal = C->getZExtValue();
2751 if (isUInt<5>(CVal))
2753 DAG.getTargetConstant(CVal, SDLoc(Op), Subtarget.getXLenVT()));
2760 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
2763 Instruction *RISCVTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
2765 AtomicOrdering Ord) const {
2766 if (isa<LoadInst>(Inst) && Ord == AtomicOrdering::SequentiallyConsistent)
2767 return Builder.CreateFence(Ord);
2768 if (isa<StoreInst>(Inst) && isReleaseOrStronger(Ord))
2769 return Builder.CreateFence(AtomicOrdering::Release);
2773 Instruction *RISCVTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
2775 AtomicOrdering Ord) const {
2776 if (isa<LoadInst>(Inst) && isAcquireOrStronger(Ord))
2777 return Builder.CreateFence(AtomicOrdering::Acquire);
2781 TargetLowering::AtomicExpansionKind
2782 RISCVTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
2783 // atomicrmw {fadd,fsub} must be expanded to use compare-exchange, as floating
2784 // point operations can't be used in an lr/sc sequence without breaking the
2785 // forward-progress guarantee.
2786 if (AI->isFloatingPointOperation())
2787 return AtomicExpansionKind::CmpXChg;
2789 unsigned Size = AI->getType()->getPrimitiveSizeInBits();
2790 if (Size == 8 || Size == 16)
2791 return AtomicExpansionKind::MaskedIntrinsic;
2792 return AtomicExpansionKind::None;
2795 static Intrinsic::ID
2796 getIntrinsicForMaskedAtomicRMWBinOp(unsigned XLen, AtomicRMWInst::BinOp BinOp) {
2800 llvm_unreachable("Unexpected AtomicRMW BinOp");
2801 case AtomicRMWInst::Xchg:
2802 return Intrinsic::riscv_masked_atomicrmw_xchg_i32;
2803 case AtomicRMWInst::Add:
2804 return Intrinsic::riscv_masked_atomicrmw_add_i32;
2805 case AtomicRMWInst::Sub:
2806 return Intrinsic::riscv_masked_atomicrmw_sub_i32;
2807 case AtomicRMWInst::Nand:
2808 return Intrinsic::riscv_masked_atomicrmw_nand_i32;
2809 case AtomicRMWInst::Max:
2810 return Intrinsic::riscv_masked_atomicrmw_max_i32;
2811 case AtomicRMWInst::Min:
2812 return Intrinsic::riscv_masked_atomicrmw_min_i32;
2813 case AtomicRMWInst::UMax:
2814 return Intrinsic::riscv_masked_atomicrmw_umax_i32;
2815 case AtomicRMWInst::UMin:
2816 return Intrinsic::riscv_masked_atomicrmw_umin_i32;
2823 llvm_unreachable("Unexpected AtomicRMW BinOp");
2824 case AtomicRMWInst::Xchg:
2825 return Intrinsic::riscv_masked_atomicrmw_xchg_i64;
2826 case AtomicRMWInst::Add:
2827 return Intrinsic::riscv_masked_atomicrmw_add_i64;
2828 case AtomicRMWInst::Sub:
2829 return Intrinsic::riscv_masked_atomicrmw_sub_i64;
2830 case AtomicRMWInst::Nand:
2831 return Intrinsic::riscv_masked_atomicrmw_nand_i64;
2832 case AtomicRMWInst::Max:
2833 return Intrinsic::riscv_masked_atomicrmw_max_i64;
2834 case AtomicRMWInst::Min:
2835 return Intrinsic::riscv_masked_atomicrmw_min_i64;
2836 case AtomicRMWInst::UMax:
2837 return Intrinsic::riscv_masked_atomicrmw_umax_i64;
2838 case AtomicRMWInst::UMin:
2839 return Intrinsic::riscv_masked_atomicrmw_umin_i64;
2843 llvm_unreachable("Unexpected XLen\n");
2846 Value *RISCVTargetLowering::emitMaskedAtomicRMWIntrinsic(
2847 IRBuilder<> &Builder, AtomicRMWInst *AI, Value *AlignedAddr, Value *Incr,
2848 Value *Mask, Value *ShiftAmt, AtomicOrdering Ord) const {
2849 unsigned XLen = Subtarget.getXLen();
2851 Builder.getIntN(XLen, static_cast<uint64_t>(AI->getOrdering()));
2852 Type *Tys[] = {AlignedAddr->getType()};
2853 Function *LrwOpScwLoop = Intrinsic::getDeclaration(
2855 getIntrinsicForMaskedAtomicRMWBinOp(XLen, AI->getOperation()), Tys);
2858 Incr = Builder.CreateSExt(Incr, Builder.getInt64Ty());
2859 Mask = Builder.CreateSExt(Mask, Builder.getInt64Ty());
2860 ShiftAmt = Builder.CreateSExt(ShiftAmt, Builder.getInt64Ty());
2865 // Must pass the shift amount needed to sign extend the loaded value prior
2866 // to performing a signed comparison for min/max. ShiftAmt is the number of
2867 // bits to shift the value into position. Pass XLen-ShiftAmt-ValWidth, which
2868 // is the number of bits to left+right shift the value in order to
2870 if (AI->getOperation() == AtomicRMWInst::Min ||
2871 AI->getOperation() == AtomicRMWInst::Max) {
2872 const DataLayout &DL = AI->getModule()->getDataLayout();
2874 DL.getTypeStoreSizeInBits(AI->getValOperand()->getType());
2876 Builder.CreateSub(Builder.getIntN(XLen, XLen - ValWidth), ShiftAmt);
2877 Result = Builder.CreateCall(LrwOpScwLoop,
2878 {AlignedAddr, Incr, Mask, SextShamt, Ordering});
2881 Builder.CreateCall(LrwOpScwLoop, {AlignedAddr, Incr, Mask, Ordering});
2885 Result = Builder.CreateTrunc(Result, Builder.getInt32Ty());
2889 TargetLowering::AtomicExpansionKind
2890 RISCVTargetLowering::shouldExpandAtomicCmpXchgInIR(
2891 AtomicCmpXchgInst *CI) const {
2892 unsigned Size = CI->getCompareOperand()->getType()->getPrimitiveSizeInBits();
2893 if (Size == 8 || Size == 16)
2894 return AtomicExpansionKind::MaskedIntrinsic;
2895 return AtomicExpansionKind::None;
2898 Value *RISCVTargetLowering::emitMaskedAtomicCmpXchgIntrinsic(
2899 IRBuilder<> &Builder, AtomicCmpXchgInst *CI, Value *AlignedAddr,
2900 Value *CmpVal, Value *NewVal, Value *Mask, AtomicOrdering Ord) const {
2901 unsigned XLen = Subtarget.getXLen();
2902 Value *Ordering = Builder.getIntN(XLen, static_cast<uint64_t>(Ord));
2903 Intrinsic::ID CmpXchgIntrID = Intrinsic::riscv_masked_cmpxchg_i32;
2905 CmpVal = Builder.CreateSExt(CmpVal, Builder.getInt64Ty());
2906 NewVal = Builder.CreateSExt(NewVal, Builder.getInt64Ty());
2907 Mask = Builder.CreateSExt(Mask, Builder.getInt64Ty());
2908 CmpXchgIntrID = Intrinsic::riscv_masked_cmpxchg_i64;
2910 Type *Tys[] = {AlignedAddr->getType()};
2911 Function *MaskedCmpXchg =
2912 Intrinsic::getDeclaration(CI->getModule(), CmpXchgIntrID, Tys);
2913 Value *Result = Builder.CreateCall(
2914 MaskedCmpXchg, {AlignedAddr, CmpVal, NewVal, Mask, Ordering});
2916 Result = Builder.CreateTrunc(Result, Builder.getInt32Ty());
2920 unsigned RISCVTargetLowering::getExceptionPointerRegister(
2921 const Constant *PersonalityFn) const {
2925 unsigned RISCVTargetLowering::getExceptionSelectorRegister(
2926 const Constant *PersonalityFn) const {
2930 bool RISCVTargetLowering::shouldExtendTypeInLibCall(EVT Type) const {
2931 // Return false to suppress the unnecessary extensions if the LibCall
2932 // arguments or return value is f32 type for LP64 ABI.
2933 RISCVABI::ABI ABI = Subtarget.getTargetABI();
2934 if (ABI == RISCVABI::ABI_LP64 && (Type == MVT::f32))
2940 #define GET_REGISTER_MATCHER
2941 #include "RISCVGenAsmMatcher.inc"
2944 RISCVTargetLowering::getRegisterByName(const char *RegName, LLT VT,
2945 const MachineFunction &MF) const {
2946 Register Reg = MatchRegisterAltName(RegName);
2947 if (Reg == RISCV::NoRegister)
2948 Reg = MatchRegisterName(RegName);
2949 if (Reg == RISCV::NoRegister)
2951 Twine("Invalid register name \"" + StringRef(RegName) + "\"."));
2952 BitVector ReservedRegs = Subtarget.getRegisterInfo()->getReservedRegs(MF);
2953 if (!ReservedRegs.test(Reg) && !Subtarget.isRegisterReservedByUser(Reg))
2954 report_fatal_error(Twine("Trying to obtain non-reserved register \"" +
2955 StringRef(RegName) + "\"."));