Home | History | Annotate | Download | only in RISCV
      1 //===-- RISCVInstrInfoF.td - RISC-V 'F' instructions -------*- tablegen -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file describes the RISC-V instructions from the standard 'F',
     11 // Single-Precision Floating-Point instruction set extension.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 //===----------------------------------------------------------------------===//
     16 // Operand and SDNode transformation definitions.
     17 //===----------------------------------------------------------------------===//
     18 
     19 // Floating-point rounding mode
     20 
     21 def FRMArg : AsmOperandClass {
     22   let Name = "FRMArg";
     23   let RenderMethod = "addFRMArgOperands";
     24   let DiagnosticType = "InvalidFRMArg";
     25 }
     26 
     27 def frmarg : Operand<XLenVT> {
     28   let ParserMatchClass = FRMArg;
     29   let PrintMethod = "printFRMArg";
     30   let DecoderMethod = "decodeUImmOperand<3>";
     31 }
     32 
     33 //===----------------------------------------------------------------------===//
     34 // Instruction class templates
     35 //===----------------------------------------------------------------------===//
     36 
     37 let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
     38 class FPFMAS_rrr_frm<RISCVOpcode opcode, string opcodestr>
     39     : RVInstR4<0b00, opcode, (outs FPR32:$rd),
     40                (ins FPR32:$rs1, FPR32:$rs2, FPR32:$rs3, frmarg:$funct3),
     41                 opcodestr, "$rd, $rs1, $rs2, $rs3, $funct3">;
     42 
     43 class FPFMASDynFrmAlias<FPFMAS_rrr_frm Inst, string OpcodeStr>
     44     : InstAlias<OpcodeStr#" $rd, $rs1, $rs2, $rs3",
     45                 (Inst FPR32:$rd, FPR32:$rs1, FPR32:$rs2, FPR32:$rs3, 0b111)>;
     46 
     47 let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
     48 class FPALUS_rr<bits<7> funct7, bits<3> funct3, string opcodestr>
     49     : RVInstR<funct7, funct3, OPC_OP_FP, (outs FPR32:$rd),
     50               (ins FPR32:$rs1, FPR32:$rs2), opcodestr, "$rd, $rs1, $rs2">;
     51 
     52 let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
     53 class FPALUS_rr_frm<bits<7> funct7, string opcodestr>
     54     : RVInstRFrm<funct7, OPC_OP_FP, (outs FPR32:$rd),
     55                  (ins FPR32:$rs1, FPR32:$rs2, frmarg:$funct3), opcodestr,
     56                   "$rd, $rs1, $rs2, $funct3">;
     57 
     58 class FPALUSDynFrmAlias<FPALUS_rr_frm Inst, string OpcodeStr>
     59     : InstAlias<OpcodeStr#" $rd, $rs1, $rs2",
     60                 (Inst FPR32:$rd, FPR32:$rs1, FPR32:$rs2, 0b111)>;
     61 
     62 let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
     63 class FPUnaryOp_r<bits<7> funct7, bits<3> funct3, RegisterClass rdty,
     64                 RegisterClass rs1ty, string opcodestr>
     65     : RVInstR<funct7, funct3, OPC_OP_FP, (outs rdty:$rd), (ins rs1ty:$rs1),
     66               opcodestr, "$rd, $rs1">;
     67 
     68 let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
     69 class FPUnaryOp_r_frm<bits<7> funct7, RegisterClass rdty, RegisterClass rs1ty,
     70                       string opcodestr>
     71     : RVInstRFrm<funct7, OPC_OP_FP, (outs rdty:$rd),
     72                  (ins rs1ty:$rs1, frmarg:$funct3), opcodestr,
     73                   "$rd, $rs1, $funct3">;
     74 
     75 class FPUnaryOpDynFrmAlias<FPUnaryOp_r_frm Inst, string OpcodeStr,
     76                            RegisterClass rdty, RegisterClass rs1ty>
     77     : InstAlias<OpcodeStr#" $rd, $rs1",
     78                 (Inst rdty:$rd, rs1ty:$rs1, 0b111)>;
     79 
     80 let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
     81 class FPCmpS_rr<bits<3> funct3, string opcodestr>
     82     : RVInstR<0b1010000, funct3, OPC_OP_FP, (outs GPR:$rd),
     83               (ins FPR32:$rs1, FPR32:$rs2), opcodestr, "$rd, $rs1, $rs2">;
     84 
     85 //===----------------------------------------------------------------------===//
     86 // Instructions
     87 //===----------------------------------------------------------------------===//
     88 
     89 let Predicates = [HasStdExtF] in {
     90 let hasSideEffects = 0, mayLoad = 1, mayStore = 0 in
     91 def FLW : RVInstI<0b010, OPC_LOAD_FP, (outs FPR32:$rd),
     92                   (ins GPR:$rs1, simm12:$imm12),
     93                    "flw", "$rd, ${imm12}(${rs1})">;
     94 
     95 // Operands for stores are in the order srcreg, base, offset rather than
     96 // reflecting the order these fields are specified in the instruction
     97 // encoding.
     98 let hasSideEffects = 0, mayLoad = 0, mayStore = 1 in
     99 def FSW : RVInstS<0b010, OPC_STORE_FP, (outs),
    100                   (ins FPR32:$rs2, GPR:$rs1, simm12:$imm12),
    101                    "fsw", "$rs2, ${imm12}(${rs1})">;
    102 
    103 def FMADD_S  : FPFMAS_rrr_frm<OPC_MADD, "fmadd.s">;
    104 def          : FPFMASDynFrmAlias<FMADD_S, "fmadd.s">;
    105 def FMSUB_S  : FPFMAS_rrr_frm<OPC_MSUB, "fmsub.s">;
    106 def          : FPFMASDynFrmAlias<FMSUB_S, "fmsub.s">;
    107 def FNMSUB_S : FPFMAS_rrr_frm<OPC_NMSUB, "fnmsub.s">;
    108 def          : FPFMASDynFrmAlias<FNMSUB_S, "fnmsub.s">;
    109 def FNMADD_S : FPFMAS_rrr_frm<OPC_NMADD, "fnmadd.s">;
    110 def          : FPFMASDynFrmAlias<FNMADD_S, "fnmadd.s">;
    111 
    112 def FADD_S : FPALUS_rr_frm<0b0000000, "fadd.s">;
    113 def        : FPALUSDynFrmAlias<FADD_S, "fadd.s">;
    114 def FSUB_S : FPALUS_rr_frm<0b0000100, "fsub.s">;
    115 def        : FPALUSDynFrmAlias<FSUB_S, "fsub.s">;
    116 def FMUL_S : FPALUS_rr_frm<0b0001000, "fmul.s">;
    117 def        : FPALUSDynFrmAlias<FMUL_S, "fmul.s">;
    118 def FDIV_S : FPALUS_rr_frm<0b0001100, "fdiv.s">;
    119 def        : FPALUSDynFrmAlias<FDIV_S, "fdiv.s">;
    120 
    121 def FSQRT_S : FPUnaryOp_r_frm<0b0101100, FPR32, FPR32, "fsqrt.s"> {
    122   let rs2 = 0b00000;
    123 }
    124 def         : FPUnaryOpDynFrmAlias<FSQRT_S, "fsqrt.s", FPR32, FPR32>;
    125 
    126 def FSGNJ_S  : FPALUS_rr<0b0010000, 0b000, "fsgnj.s">;
    127 def FSGNJN_S : FPALUS_rr<0b0010000, 0b001, "fsgnjn.s">;
    128 def FSGNJX_S : FPALUS_rr<0b0010000, 0b010, "fsgnjx.s">;
    129 def FMIN_S   : FPALUS_rr<0b0010100, 0b000, "fmin.s">;
    130 def FMAX_S   : FPALUS_rr<0b0010100, 0b001, "fmax.s">;
    131 
    132 def FCVT_W_S : FPUnaryOp_r_frm<0b1100000, GPR, FPR32, "fcvt.w.s"> {
    133   let rs2 = 0b00000;
    134 }
    135 def          : FPUnaryOpDynFrmAlias<FCVT_W_S, "fcvt.w.s", GPR, FPR32>;
    136 
    137 def FCVT_WU_S : FPUnaryOp_r_frm<0b1100000, GPR, FPR32, "fcvt.wu.s"> {
    138   let rs2 = 0b00001;
    139 }
    140 def           : FPUnaryOpDynFrmAlias<FCVT_WU_S, "fcvt.wu.s", GPR, FPR32>;
    141 
    142 def FMV_X_W : FPUnaryOp_r<0b1110000, 0b000, GPR, FPR32, "fmv.x.w"> {
    143   let rs2 = 0b00000;
    144 }
    145 
    146 def FEQ_S : FPCmpS_rr<0b010, "feq.s">;
    147 def FLT_S : FPCmpS_rr<0b001, "flt.s">;
    148 def FLE_S : FPCmpS_rr<0b000, "fle.s">;
    149 
    150 def FCLASS_S : FPUnaryOp_r<0b1110000, 0b001, GPR, FPR32, "fclass.s"> {
    151   let rs2 = 0b00000;
    152 }
    153 
    154 def FCVT_S_W : FPUnaryOp_r_frm<0b1101000, FPR32, GPR, "fcvt.s.w"> {
    155   let rs2 = 0b00000;
    156 }
    157 def          : FPUnaryOpDynFrmAlias<FCVT_S_W, "fcvt.s.w", FPR32, GPR>;
    158 
    159 def FCVT_S_WU : FPUnaryOp_r_frm<0b1101000, FPR32, GPR, "fcvt.s.wu"> {
    160   let rs2 = 0b00001;
    161 }
    162 def           : FPUnaryOpDynFrmAlias<FCVT_S_WU, "fcvt.s.wu", FPR32, GPR>;
    163 
    164 def FMV_W_X : FPUnaryOp_r<0b1111000, 0b000, FPR32, GPR, "fmv.w.x"> {
    165   let rs2 = 0b00000;
    166 }
    167 } // Predicates = [HasStdExtF]
    168 
    169 let Predicates = [HasStdExtF, IsRV64] in {
    170 def FCVT_L_S  : FPUnaryOp_r_frm<0b1100000, GPR, FPR32, "fcvt.l.s"> {
    171   let rs2 = 0b00010;
    172 }
    173 def           : FPUnaryOpDynFrmAlias<FCVT_L_S, "fcvt.l.s", GPR, FPR32>;
    174 
    175 def FCVT_LU_S  : FPUnaryOp_r_frm<0b1100000, GPR, FPR32, "fcvt.lu.s"> {
    176   let rs2 = 0b00011;
    177 }
    178 def            : FPUnaryOpDynFrmAlias<FCVT_LU_S, "fcvt.lu.s", GPR, FPR32>;
    179 
    180 def FCVT_S_L : FPUnaryOp_r_frm<0b1101000, FPR32, GPR, "fcvt.s.l"> {
    181   let rs2 = 0b00010;
    182 }
    183 def          : FPUnaryOpDynFrmAlias<FCVT_S_L, "fcvt.s.l", FPR32, GPR>;
    184 
    185 def FCVT_S_LU : FPUnaryOp_r_frm<0b1101000, FPR32, GPR, "fcvt.s.lu"> {
    186   let rs2 = 0b00011;
    187 }
    188 def           : FPUnaryOpDynFrmAlias<FCVT_S_LU, "fcvt.s.lu", FPR32, GPR>;
    189 } // Predicates = [HasStdExtF, IsRV64]
    190 
    191 //===----------------------------------------------------------------------===//
    192 // Assembler Pseudo Instructions (User-Level ISA, Version 2.2, Chapter 20)
    193 //===----------------------------------------------------------------------===//
    194 
    195 let Predicates = [HasStdExtF] in {
    196 // TODO flw
    197 // TODO fsw
    198 
    199 def : InstAlias<"fmv.s $rd, $rs",  (FSGNJ_S  FPR32:$rd, FPR32:$rs, FPR32:$rs)>;
    200 def : InstAlias<"fabs.s $rd, $rs", (FSGNJX_S FPR32:$rd, FPR32:$rs, FPR32:$rs)>;
    201 def : InstAlias<"fneg.s $rd, $rs", (FSGNJN_S FPR32:$rd, FPR32:$rs, FPR32:$rs)>;
    202 
    203 // fgt.s/fge.s are recognised by the GNU assembler but the canonical
    204 // flt.s/fle.s forms will always be printed. Therefore, set a zero weight.
    205 def : InstAlias<"fgt.s $rd, $rs, $rt",
    206                 (FLT_S GPR:$rd, FPR32:$rt, FPR32:$rs), 0>;
    207 def : InstAlias<"fge.s $rd, $rs, $rt",
    208                 (FLE_S GPR:$rd, FPR32:$rt, FPR32:$rs), 0>;
    209 
    210 // The following csr instructions actually alias instructions from the base ISA.
    211 // However, it only makes sense to support them when the F extension is enabled.
    212 // CSR Addresses: 0x003 == fcsr, 0x002 == frm, 0x001 == fflags
    213 // NOTE: "frcsr", "frrm", and "frflags" are more specialized version of "csrr".
    214 def : InstAlias<"frcsr $rd",      (CSRRS GPR:$rd, 0x003, X0), 2>;
    215 def : InstAlias<"fscsr $rd, $rs", (CSRRW GPR:$rd, 0x003, GPR:$rs)>;
    216 def : InstAlias<"fscsr $rs",      (CSRRW      X0, 0x003, GPR:$rs), 2>;
    217 
    218 def : InstAlias<"frrm $rd",        (CSRRS  GPR:$rd, 0x002, X0), 2>;
    219 def : InstAlias<"fsrm $rd, $rs",   (CSRRW  GPR:$rd, 0x002, GPR:$rs)>;
    220 def : InstAlias<"fsrm $rs",        (CSRRW       X0, 0x002, GPR:$rs), 2>;
    221 def : InstAlias<"fsrmi $rd, $imm", (CSRRWI GPR:$rd, 0x002, uimm5:$imm)>;
    222 def : InstAlias<"fsrmi $imm",      (CSRRWI      X0, 0x002, uimm5:$imm), 2>;
    223 
    224 def : InstAlias<"frflags $rd",        (CSRRS  GPR:$rd, 0x001, X0), 2>;
    225 def : InstAlias<"fsflags $rd, $rs",   (CSRRW  GPR:$rd, 0x001, GPR:$rs)>;
    226 def : InstAlias<"fsflags $rs",        (CSRRW       X0, 0x001, GPR:$rs), 2>;
    227 def : InstAlias<"fsflagsi $rd, $imm", (CSRRWI GPR:$rd, 0x001, uimm5:$imm)>;
    228 def : InstAlias<"fsflagsi $imm",      (CSRRWI      X0, 0x001, uimm5:$imm), 2>;
    229 
    230 // fmv.w.x and fmv.x.w were previously known as fmv.s.x and fmv.x.s. Both
    231 // spellings should be supported by standard tools.
    232 def : MnemonicAlias<"fmv.s.x", "fmv.w.x">;
    233 def : MnemonicAlias<"fmv.x.s", "fmv.x.w">;
    234 } // Predicates = [HasStdExtF]
    235 
    236 //===----------------------------------------------------------------------===//
    237 // Pseudo-instructions and codegen patterns
    238 //===----------------------------------------------------------------------===//
    239 
    240 /// Generic pattern classes
    241 class PatFpr32Fpr32<SDPatternOperator OpNode, RVInstR Inst>
    242     : Pat<(OpNode FPR32:$rs1, FPR32:$rs2), (Inst $rs1, $rs2)>;
    243 
    244 class PatFpr32Fpr32DynFrm<SDPatternOperator OpNode, RVInstRFrm Inst>
    245     : Pat<(OpNode FPR32:$rs1, FPR32:$rs2), (Inst $rs1, $rs2, 0b111)>;
    246 
    247 let Predicates = [HasStdExtF] in {
    248 
    249 /// Float conversion operations
    250 
    251 // Moves (no conversion)
    252 def : Pat<(bitconvert GPR:$rs1), (FMV_W_X GPR:$rs1)>;
    253 def : Pat<(bitconvert FPR32:$rs1), (FMV_X_W FPR32:$rs1)>;
    254 
    255 // FP->[u]int. Round-to-zero must be used
    256 def : Pat<(fp_to_sint FPR32:$rs1), (FCVT_W_S $rs1, 0b001)>;
    257 def : Pat<(fp_to_uint FPR32:$rs1), (FCVT_WU_S $rs1, 0b001)>;
    258 
    259 // [u]int->fp. Match GCC and default to using dynamic rounding mode.
    260 def : Pat<(sint_to_fp GPR:$rs1), (FCVT_S_W $rs1, 0b111)>;
    261 def : Pat<(uint_to_fp GPR:$rs1), (FCVT_S_WU $rs1, 0b111)>;
    262 
    263 /// Float arithmetic operations
    264 
    265 def : PatFpr32Fpr32DynFrm<fadd, FADD_S>;
    266 def : PatFpr32Fpr32DynFrm<fsub, FSUB_S>;
    267 def : PatFpr32Fpr32DynFrm<fmul, FMUL_S>;
    268 def : PatFpr32Fpr32DynFrm<fdiv, FDIV_S>;
    269 
    270 def : Pat<(fsqrt FPR32:$rs1), (FSQRT_S FPR32:$rs1, 0b111)>;
    271 
    272 def : Pat<(fneg FPR32:$rs1), (FSGNJN_S $rs1, $rs1)>;
    273 def : Pat<(fabs FPR32:$rs1), (FSGNJX_S $rs1, $rs1)>;
    274 
    275 def : PatFpr32Fpr32<fcopysign, FSGNJ_S>;
    276 def : Pat<(fcopysign FPR32:$rs1, (fneg FPR32:$rs2)), (FSGNJN_S $rs1, $rs2)>;
    277 
    278 // The RISC-V 2.2 user-level ISA spec defines fmin and fmax as returning the
    279 // canonical NaN when given a signaling NaN. This doesn't match the LLVM
    280 // behaviour (see https://bugs.llvm.org/show_bug.cgi?id=27363). However, the
    281 // draft 2.3 ISA spec changes the definition of fmin and fmax in a way that
    282 // matches LLVM's fminnum and fmaxnum
    283 // <https://github.com/riscv/riscv-isa-manual/commit/cd20cee7efd9bac7c5aa127ec3b451749d2b3cce>.
    284 def : PatFpr32Fpr32<fminnum, FMIN_S>;
    285 def : PatFpr32Fpr32<fmaxnum, FMAX_S>;
    286 
    287 /// Setcc
    288 
    289 def : PatFpr32Fpr32<seteq, FEQ_S>;
    290 def : PatFpr32Fpr32<setoeq, FEQ_S>;
    291 def : PatFpr32Fpr32<setlt, FLT_S>;
    292 def : PatFpr32Fpr32<setolt, FLT_S>;
    293 def : PatFpr32Fpr32<setle, FLE_S>;
    294 def : PatFpr32Fpr32<setole, FLE_S>;
    295 
    296 // Define pattern expansions for setcc operations which aren't directly
    297 // handled by a RISC-V instruction and aren't expanded in the SelectionDAG
    298 // Legalizer.
    299 
    300 def : Pat<(setuo FPR32:$rs1, FPR32:$rs2),
    301           (SLTIU (AND (FEQ_S FPR32:$rs1, FPR32:$rs1),
    302                       (FEQ_S FPR32:$rs2, FPR32:$rs2)),
    303                  1)>;
    304 
    305 def Select_FPR32_Using_CC_GPR : SelectCC_rrirr<FPR32, GPR>;
    306 
    307 /// Loads
    308 
    309 defm : LdPat<load, FLW>;
    310 
    311 /// Stores
    312 
    313 defm : StPat<store, FSW, FPR32>;
    314 
    315 } // Predicates = [HasStdExtF]
    316