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_mulhsu : SDNode<"RISCVISD::MULHSU", SDTIntBinOp>;
19 def riscv_divw : SDNode<"RISCVISD::DIVW", SDT_RISCVIntBinOpW>;
20 def riscv_divuw : SDNode<"RISCVISD::DIVUW", SDT_RISCVIntBinOpW>;
21 def riscv_remuw : SDNode<"RISCVISD::REMUW", SDT_RISCVIntBinOpW>;
23 //===----------------------------------------------------------------------===//
25 //===----------------------------------------------------------------------===//
27 let Predicates = [HasStdExtM] in {
28 def MUL : ALU_rr<0b0000001, 0b000, "mul">,
29 Sched<[WriteIMul, ReadIMul, ReadIMul]>;
30 def MULH : ALU_rr<0b0000001, 0b001, "mulh">,
31 Sched<[WriteIMul, ReadIMul, ReadIMul]>;
32 def MULHSU : ALU_rr<0b0000001, 0b010, "mulhsu">,
33 Sched<[WriteIMul, ReadIMul, ReadIMul]>;
34 def MULHU : ALU_rr<0b0000001, 0b011, "mulhu">,
35 Sched<[WriteIMul, ReadIMul, ReadIMul]>;
36 def DIV : ALU_rr<0b0000001, 0b100, "div">,
37 Sched<[WriteIDiv, ReadIDiv, ReadIDiv]>;
38 def DIVU : ALU_rr<0b0000001, 0b101, "divu">,
39 Sched<[WriteIDiv, ReadIDiv, ReadIDiv]>;
40 def REM : ALU_rr<0b0000001, 0b110, "rem">,
41 Sched<[WriteIDiv, ReadIDiv, ReadIDiv]>;
42 def REMU : ALU_rr<0b0000001, 0b111, "remu">,
43 Sched<[WriteIDiv, ReadIDiv, ReadIDiv]>;
44 } // Predicates = [HasStdExtM]
46 let Predicates = [HasStdExtM, IsRV64] in {
47 def MULW : ALUW_rr<0b0000001, 0b000, "mulw">,
48 Sched<[WriteIMul32, ReadIMul32, ReadIMul32]>;
49 def DIVW : ALUW_rr<0b0000001, 0b100, "divw">,
50 Sched<[WriteIDiv32, ReadIDiv32, ReadIDiv32]>;
51 def DIVUW : ALUW_rr<0b0000001, 0b101, "divuw">,
52 Sched<[WriteIDiv32, ReadIDiv32, ReadIDiv32]>;
53 def REMW : ALUW_rr<0b0000001, 0b110, "remw">,
54 Sched<[WriteIDiv32, ReadIDiv32, ReadIDiv32]>;
55 def REMUW : ALUW_rr<0b0000001, 0b111, "remuw">,
56 Sched<[WriteIDiv32, ReadIDiv32, ReadIDiv32]>;
57 } // Predicates = [HasStdExtM, IsRV64]
59 //===----------------------------------------------------------------------===//
60 // Pseudo-instructions and codegen patterns
61 //===----------------------------------------------------------------------===//
63 let Predicates = [HasStdExtM] in {
64 def : PatGprGpr<mul, MUL>;
65 def : PatGprGpr<mulhs, MULH>;
66 def : PatGprGpr<mulhu, MULHU>;
67 def : PatGprGpr<riscv_mulhsu, MULHSU>;
68 def : PatGprGpr<sdiv, DIV>;
69 def : PatGprGpr<udiv, DIVU>;
70 def : PatGprGpr<srem, REM>;
71 def : PatGprGpr<urem, REMU>;
72 } // Predicates = [HasStdExtM]
74 let Predicates = [HasStdExtM, IsRV64] in {
75 // Select W instructions without sext_inreg if only the lower 32-bits of the
77 def : PatGprGpr<overflowingbinopw<mul>, MULW>;
79 def : PatGprGpr<riscv_divw, DIVW>;
80 def : PatGprGpr<riscv_divuw, DIVUW>;
81 def : PatGprGpr<riscv_remuw, REMUW>;
83 // Handle the specific cases where using DIVU/REMU would be correct and result
84 // in fewer instructions than emitting DIVUW/REMUW then zero-extending the
86 def : Pat<(and (riscv_divuw (assertzexti32 GPR:$rs1),
87 (assertzexti32 GPR:$rs2)), 0xffffffff),
88 (DIVU GPR:$rs1, GPR:$rs2)>;
89 def : Pat<(and (riscv_remuw (assertzexti32 GPR:$rs1),
90 (assertzexti32 GPR:$rs2)), 0xffffffff),
91 (REMU GPR:$rs1, GPR:$rs2)>;
93 // Although the sexti32 operands may not have originated from an i32 srem,
94 // this pattern is safe as it is impossible for two sign extended inputs to
95 // produce a result where res[63:32]=0 and res[31]=1.
96 def : Pat<(srem (sexti32 (i64 GPR:$rs1)), (sexti32 (i64 GPR:$rs2))),
97 (REMW GPR:$rs1, GPR:$rs2)>;
98 } // Predicates = [HasStdExtM, IsRV64]
100 let Predicates = [HasStdExtM, IsRV64, NotHasStdExtZba] in {
101 // Special case for calculating the full 64-bit product of a 32x32 unsigned
102 // multiply where the inputs aren't known to be zero extended. We can shift the
103 // inputs left by 32 and use a MULHU. This saves two SRLIs needed to finish
104 // zeroing the upper 32 bits.
105 // TODO: If one of the operands is zero extended and the other isn't, we might
106 // still be better off shifting both left by 32.
107 def : Pat<(i64 (mul (and GPR:$rs1, 0xffffffff), (and GPR:$rs2, 0xffffffff))),
108 (MULHU (SLLI GPR:$rs1, 32), (SLLI GPR:$rs2, 32))>;
109 } // Predicates = [HasStdExtM, IsRV64, NotHasStdExtZba]