1 //===-- RISCVInstrInfoM.td - RISC-V 'M' instructions -------*- tablegen -*-===//
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 describes the RISC-V instructions from the standard 'M', Integer
10 // Multiplication and Division instruction set extension.
12 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
15 // RISC-V specific DAG Nodes.
16 //===----------------------------------------------------------------------===//
18 def riscv_divw : SDNode<"RISCVISD::DIVW", SDTIntBinOp>;
19 def riscv_divuw : SDNode<"RISCVISD::DIVUW", SDTIntBinOp>;
20 def riscv_remuw : SDNode<"RISCVISD::REMUW", SDTIntBinOp>;
22 //===----------------------------------------------------------------------===//
24 //===----------------------------------------------------------------------===//
26 let Predicates = [HasStdExtM] in {
27 def MUL : ALU_rr<0b0000001, 0b000, "mul">,
28 Sched<[WriteIMul, ReadIMul, ReadIMul]>;
29 def MULH : ALU_rr<0b0000001, 0b001, "mulh">,
30 Sched<[WriteIMul, ReadIMul, ReadIMul]>;
31 def MULHSU : ALU_rr<0b0000001, 0b010, "mulhsu">,
32 Sched<[WriteIMul, ReadIMul, ReadIMul]>;
33 def MULHU : ALU_rr<0b0000001, 0b011, "mulhu">,
34 Sched<[WriteIMul, ReadIMul, ReadIMul]>;
35 def DIV : ALU_rr<0b0000001, 0b100, "div">,
36 Sched<[WriteIDiv, ReadIDiv, ReadIDiv]>;
37 def DIVU : ALU_rr<0b0000001, 0b101, "divu">,
38 Sched<[WriteIDiv, ReadIDiv, ReadIDiv]>;
39 def REM : ALU_rr<0b0000001, 0b110, "rem">,
40 Sched<[WriteIDiv, ReadIDiv, ReadIDiv]>;
41 def REMU : ALU_rr<0b0000001, 0b111, "remu">,
42 Sched<[WriteIDiv, ReadIDiv, ReadIDiv]>;
43 } // Predicates = [HasStdExtM]
45 let Predicates = [HasStdExtM, IsRV64] in {
46 def MULW : ALUW_rr<0b0000001, 0b000, "mulw">,
47 Sched<[WriteIMul32, ReadIMul32, ReadIMul32]>;
48 def DIVW : ALUW_rr<0b0000001, 0b100, "divw">,
49 Sched<[WriteIDiv32, ReadIDiv32, ReadIDiv32]>;
50 def DIVUW : ALUW_rr<0b0000001, 0b101, "divuw">,
51 Sched<[WriteIDiv32, ReadIDiv32, ReadIDiv32]>;
52 def REMW : ALUW_rr<0b0000001, 0b110, "remw">,
53 Sched<[WriteIDiv32, ReadIDiv32, ReadIDiv32]>;
54 def REMUW : ALUW_rr<0b0000001, 0b111, "remuw">,
55 Sched<[WriteIDiv32, ReadIDiv32, ReadIDiv32]>;
56 } // Predicates = [HasStdExtM, IsRV64]
58 //===----------------------------------------------------------------------===//
59 // Pseudo-instructions and codegen patterns
60 //===----------------------------------------------------------------------===//
62 let Predicates = [HasStdExtM] in {
63 def : PatGprGpr<mul, MUL>;
64 def : PatGprGpr<mulhs, MULH>;
65 def : PatGprGpr<mulhu, MULHU>;
66 // No ISDOpcode for mulhsu
67 def : PatGprGpr<sdiv, DIV>;
68 def : PatGprGpr<udiv, DIVU>;
69 def : PatGprGpr<srem, REM>;
70 def : PatGprGpr<urem, REMU>;
71 } // Predicates = [HasStdExtM]
73 let Predicates = [HasStdExtM, IsRV64] in {
74 def : Pat<(sext_inreg (mul GPR:$rs1, GPR:$rs2), i32),
75 (MULW GPR:$rs1, GPR:$rs2)>;
77 def : PatGprGpr<riscv_divw, DIVW>;
78 def : PatGprGpr<riscv_divuw, DIVUW>;
79 def : PatGprGpr<riscv_remuw, REMUW>;
81 // Handle the specific cases where using DIVU/REMU would be correct and result
82 // in fewer instructions than emitting DIVUW/REMUW then zero-extending the
84 def : Pat<(zexti32 (riscv_divuw (zexti32 GPR:$rs1), (zexti32 GPR:$rs2))),
85 (DIVU GPR:$rs1, GPR:$rs2)>;
86 def : Pat<(zexti32 (riscv_remuw (zexti32 GPR:$rs1), (zexti32 GPR:$rs2))),
87 (REMU GPR:$rs1, GPR:$rs2)>;
89 // Although the sexti32 operands may not have originated from an i32 srem,
90 // this pattern is safe as it is impossible for two sign extended inputs to
91 // produce a result where res[63:32]=0 and res[31]=1.
92 def : Pat<(srem (sexti32 GPR:$rs1), (sexti32 GPR:$rs2)),
93 (REMW GPR:$rs1, GPR:$rs2)>;
94 def : Pat<(sext_inreg (srem (sexti32 GPR:$rs1),
95 (sexti32 GPR:$rs2)), i32),
96 (REMW GPR:$rs1, GPR:$rs2)>;
97 } // Predicates = [HasStdExtM, IsRV64]