• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
21def FRMArg : AsmOperandClass {
22  let Name = "FRMArg";
23  let RenderMethod = "addFRMArgOperands";
24  let DiagnosticType = "InvalidFRMArg";
25}
26
27def 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
37let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
38class 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
43class 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
47let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
48class 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
52let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
53class 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
58class FPALUSDynFrmAlias<FPALUS_rr_frm Inst, string OpcodeStr>
59    : InstAlias<OpcodeStr#" $rd, $rs1, $rs2",
60                (Inst FPR32:$rd, FPR32:$rs1, FPR32:$rs2, 0b111)>;
61
62let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
63class 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
68let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
69class 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
75class 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
80let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
81class 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
89let Predicates = [HasStdExtF] in {
90let hasSideEffects = 0, mayLoad = 1, mayStore = 0 in
91def 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.
98let hasSideEffects = 0, mayLoad = 0, mayStore = 1 in
99def FSW : RVInstS<0b010, OPC_STORE_FP, (outs),
100                  (ins FPR32:$rs2, GPR:$rs1, simm12:$imm12),
101                   "fsw", "$rs2, ${imm12}(${rs1})">;
102
103def FMADD_S  : FPFMAS_rrr_frm<OPC_MADD, "fmadd.s">;
104def          : FPFMASDynFrmAlias<FMADD_S, "fmadd.s">;
105def FMSUB_S  : FPFMAS_rrr_frm<OPC_MSUB, "fmsub.s">;
106def          : FPFMASDynFrmAlias<FMSUB_S, "fmsub.s">;
107def FNMSUB_S : FPFMAS_rrr_frm<OPC_NMSUB, "fnmsub.s">;
108def          : FPFMASDynFrmAlias<FNMSUB_S, "fnmsub.s">;
109def FNMADD_S : FPFMAS_rrr_frm<OPC_NMADD, "fnmadd.s">;
110def          : FPFMASDynFrmAlias<FNMADD_S, "fnmadd.s">;
111
112def FADD_S : FPALUS_rr_frm<0b0000000, "fadd.s">;
113def        : FPALUSDynFrmAlias<FADD_S, "fadd.s">;
114def FSUB_S : FPALUS_rr_frm<0b0000100, "fsub.s">;
115def        : FPALUSDynFrmAlias<FSUB_S, "fsub.s">;
116def FMUL_S : FPALUS_rr_frm<0b0001000, "fmul.s">;
117def        : FPALUSDynFrmAlias<FMUL_S, "fmul.s">;
118def FDIV_S : FPALUS_rr_frm<0b0001100, "fdiv.s">;
119def        : FPALUSDynFrmAlias<FDIV_S, "fdiv.s">;
120
121def FSQRT_S : FPUnaryOp_r_frm<0b0101100, FPR32, FPR32, "fsqrt.s"> {
122  let rs2 = 0b00000;
123}
124def         : FPUnaryOpDynFrmAlias<FSQRT_S, "fsqrt.s", FPR32, FPR32>;
125
126def FSGNJ_S  : FPALUS_rr<0b0010000, 0b000, "fsgnj.s">;
127def FSGNJN_S : FPALUS_rr<0b0010000, 0b001, "fsgnjn.s">;
128def FSGNJX_S : FPALUS_rr<0b0010000, 0b010, "fsgnjx.s">;
129def FMIN_S   : FPALUS_rr<0b0010100, 0b000, "fmin.s">;
130def FMAX_S   : FPALUS_rr<0b0010100, 0b001, "fmax.s">;
131
132def FCVT_W_S : FPUnaryOp_r_frm<0b1100000, GPR, FPR32, "fcvt.w.s"> {
133  let rs2 = 0b00000;
134}
135def          : FPUnaryOpDynFrmAlias<FCVT_W_S, "fcvt.w.s", GPR, FPR32>;
136
137def FCVT_WU_S : FPUnaryOp_r_frm<0b1100000, GPR, FPR32, "fcvt.wu.s"> {
138  let rs2 = 0b00001;
139}
140def           : FPUnaryOpDynFrmAlias<FCVT_WU_S, "fcvt.wu.s", GPR, FPR32>;
141
142def FMV_X_W : FPUnaryOp_r<0b1110000, 0b000, GPR, FPR32, "fmv.x.w"> {
143  let rs2 = 0b00000;
144}
145
146def FEQ_S : FPCmpS_rr<0b010, "feq.s">;
147def FLT_S : FPCmpS_rr<0b001, "flt.s">;
148def FLE_S : FPCmpS_rr<0b000, "fle.s">;
149
150def FCLASS_S : FPUnaryOp_r<0b1110000, 0b001, GPR, FPR32, "fclass.s"> {
151  let rs2 = 0b00000;
152}
153
154def FCVT_S_W : FPUnaryOp_r_frm<0b1101000, FPR32, GPR, "fcvt.s.w"> {
155  let rs2 = 0b00000;
156}
157def          : FPUnaryOpDynFrmAlias<FCVT_S_W, "fcvt.s.w", FPR32, GPR>;
158
159def FCVT_S_WU : FPUnaryOp_r_frm<0b1101000, FPR32, GPR, "fcvt.s.wu"> {
160  let rs2 = 0b00001;
161}
162def           : FPUnaryOpDynFrmAlias<FCVT_S_WU, "fcvt.s.wu", FPR32, GPR>;
163
164def FMV_W_X : FPUnaryOp_r<0b1111000, 0b000, FPR32, GPR, "fmv.w.x"> {
165  let rs2 = 0b00000;
166}
167} // Predicates = [HasStdExtF]
168
169let Predicates = [HasStdExtF, IsRV64] in {
170def FCVT_L_S  : FPUnaryOp_r_frm<0b1100000, GPR, FPR32, "fcvt.l.s"> {
171  let rs2 = 0b00010;
172}
173def           : FPUnaryOpDynFrmAlias<FCVT_L_S, "fcvt.l.s", GPR, FPR32>;
174
175def FCVT_LU_S  : FPUnaryOp_r_frm<0b1100000, GPR, FPR32, "fcvt.lu.s"> {
176  let rs2 = 0b00011;
177}
178def            : FPUnaryOpDynFrmAlias<FCVT_LU_S, "fcvt.lu.s", GPR, FPR32>;
179
180def FCVT_S_L : FPUnaryOp_r_frm<0b1101000, FPR32, GPR, "fcvt.s.l"> {
181  let rs2 = 0b00010;
182}
183def          : FPUnaryOpDynFrmAlias<FCVT_S_L, "fcvt.s.l", FPR32, GPR>;
184
185def FCVT_S_LU : FPUnaryOp_r_frm<0b1101000, FPR32, GPR, "fcvt.s.lu"> {
186  let rs2 = 0b00011;
187}
188def           : 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
195let Predicates = [HasStdExtF] in {
196// TODO flw
197// TODO fsw
198
199def : InstAlias<"fmv.s $rd, $rs",  (FSGNJ_S  FPR32:$rd, FPR32:$rs, FPR32:$rs)>;
200def : InstAlias<"fabs.s $rd, $rs", (FSGNJX_S FPR32:$rd, FPR32:$rs, FPR32:$rs)>;
201def : 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.
205def : InstAlias<"fgt.s $rd, $rs, $rt",
206                (FLT_S GPR:$rd, FPR32:$rt, FPR32:$rs), 0>;
207def : 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".
214def : InstAlias<"frcsr $rd",      (CSRRS GPR:$rd, 0x003, X0), 2>;
215def : InstAlias<"fscsr $rd, $rs", (CSRRW GPR:$rd, 0x003, GPR:$rs)>;
216def : InstAlias<"fscsr $rs",      (CSRRW      X0, 0x003, GPR:$rs), 2>;
217
218def : InstAlias<"frrm $rd",        (CSRRS  GPR:$rd, 0x002, X0), 2>;
219def : InstAlias<"fsrm $rd, $rs",   (CSRRW  GPR:$rd, 0x002, GPR:$rs)>;
220def : InstAlias<"fsrm $rs",        (CSRRW       X0, 0x002, GPR:$rs), 2>;
221def : InstAlias<"fsrmi $rd, $imm", (CSRRWI GPR:$rd, 0x002, uimm5:$imm)>;
222def : InstAlias<"fsrmi $imm",      (CSRRWI      X0, 0x002, uimm5:$imm), 2>;
223
224def : InstAlias<"frflags $rd",        (CSRRS  GPR:$rd, 0x001, X0), 2>;
225def : InstAlias<"fsflags $rd, $rs",   (CSRRW  GPR:$rd, 0x001, GPR:$rs)>;
226def : InstAlias<"fsflags $rs",        (CSRRW       X0, 0x001, GPR:$rs), 2>;
227def : InstAlias<"fsflagsi $rd, $imm", (CSRRWI GPR:$rd, 0x001, uimm5:$imm)>;
228def : 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.
232def : MnemonicAlias<"fmv.s.x", "fmv.w.x">;
233def : MnemonicAlias<"fmv.x.s", "fmv.x.w">;
234} // Predicates = [HasStdExtF]
235
236//===----------------------------------------------------------------------===//
237// Pseudo-instructions and codegen patterns
238//===----------------------------------------------------------------------===//
239
240/// Generic pattern classes
241class PatFpr32Fpr32<SDPatternOperator OpNode, RVInstR Inst>
242    : Pat<(OpNode FPR32:$rs1, FPR32:$rs2), (Inst $rs1, $rs2)>;
243
244class PatFpr32Fpr32DynFrm<SDPatternOperator OpNode, RVInstRFrm Inst>
245    : Pat<(OpNode FPR32:$rs1, FPR32:$rs2), (Inst $rs1, $rs2, 0b111)>;
246
247let Predicates = [HasStdExtF] in {
248
249/// Float conversion operations
250
251// Moves (no conversion)
252def : Pat<(bitconvert GPR:$rs1), (FMV_W_X GPR:$rs1)>;
253def : Pat<(bitconvert FPR32:$rs1), (FMV_X_W FPR32:$rs1)>;
254
255// FP->[u]int. Round-to-zero must be used
256def : Pat<(fp_to_sint FPR32:$rs1), (FCVT_W_S $rs1, 0b001)>;
257def : 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.
260def : Pat<(sint_to_fp GPR:$rs1), (FCVT_S_W $rs1, 0b111)>;
261def : Pat<(uint_to_fp GPR:$rs1), (FCVT_S_WU $rs1, 0b111)>;
262
263/// Float arithmetic operations
264
265def : PatFpr32Fpr32DynFrm<fadd, FADD_S>;
266def : PatFpr32Fpr32DynFrm<fsub, FSUB_S>;
267def : PatFpr32Fpr32DynFrm<fmul, FMUL_S>;
268def : PatFpr32Fpr32DynFrm<fdiv, FDIV_S>;
269
270def : Pat<(fsqrt FPR32:$rs1), (FSQRT_S FPR32:$rs1, 0b111)>;
271
272def : Pat<(fneg FPR32:$rs1), (FSGNJN_S $rs1, $rs1)>;
273def : Pat<(fabs FPR32:$rs1), (FSGNJX_S $rs1, $rs1)>;
274
275def : PatFpr32Fpr32<fcopysign, FSGNJ_S>;
276def : 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>.
284def : PatFpr32Fpr32<fminnum, FMIN_S>;
285def : PatFpr32Fpr32<fmaxnum, FMAX_S>;
286
287/// Setcc
288
289def : PatFpr32Fpr32<seteq, FEQ_S>;
290def : PatFpr32Fpr32<setoeq, FEQ_S>;
291def : PatFpr32Fpr32<setlt, FLT_S>;
292def : PatFpr32Fpr32<setolt, FLT_S>;
293def : PatFpr32Fpr32<setle, FLE_S>;
294def : 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
300def : 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
305def Select_FPR32_Using_CC_GPR : SelectCC_rrirr<FPR32, GPR>;
306
307/// Loads
308
309defm : LdPat<load, FLW>;
310
311/// Stores
312
313defm : StPat<store, FSW, FPR32>;
314
315} // Predicates = [HasStdExtF]
316