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 = [HasStdExtMOrZmmul] in {
28 def MUL : ALU_rr<0b0000001, 0b000, "mul", Commutable=1>,
29 Sched<[WriteIMul, ReadIMul, ReadIMul]>;
30 def MULH : ALU_rr<0b0000001, 0b001, "mulh", Commutable=1>,
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", Commutable=1>,
35 Sched<[WriteIMul, ReadIMul, ReadIMul]>;
36 } // Predicates = [HasStdExtMOrZmmul]
38 let Predicates = [HasStdExtM] in {
39 def DIV : ALU_rr<0b0000001, 0b100, "div">,
40 Sched<[WriteIDiv, ReadIDiv, ReadIDiv]>;
41 def DIVU : ALU_rr<0b0000001, 0b101, "divu">,
42 Sched<[WriteIDiv, ReadIDiv, ReadIDiv]>;
43 def REM : ALU_rr<0b0000001, 0b110, "rem">,
44 Sched<[WriteIDiv, ReadIDiv, ReadIDiv]>;
45 def REMU : ALU_rr<0b0000001, 0b111, "remu">,
46 Sched<[WriteIDiv, ReadIDiv, ReadIDiv]>;
47 } // Predicates = [HasStdExtM]
49 let Predicates = [HasStdExtMOrZmmul, IsRV64], IsSignExtendingOpW = 1 in {
50 def MULW : ALUW_rr<0b0000001, 0b000, "mulw", Commutable=1>,
51 Sched<[WriteIMul32, ReadIMul32, ReadIMul32]>;
52 } // Predicates = [HasStdExtMOrZmmul, IsRV64]
54 let Predicates = [HasStdExtM, IsRV64], IsSignExtendingOpW = 1 in {
55 def DIVW : ALUW_rr<0b0000001, 0b100, "divw">,
56 Sched<[WriteIDiv32, ReadIDiv32, ReadIDiv32]>;
57 def DIVUW : ALUW_rr<0b0000001, 0b101, "divuw">,
58 Sched<[WriteIDiv32, ReadIDiv32, ReadIDiv32]>;
59 def REMW : ALUW_rr<0b0000001, 0b110, "remw">,
60 Sched<[WriteIDiv32, ReadIDiv32, ReadIDiv32]>;
61 def REMUW : ALUW_rr<0b0000001, 0b111, "remuw">,
62 Sched<[WriteIDiv32, ReadIDiv32, ReadIDiv32]>;
63 } // Predicates = [HasStdExtM, IsRV64]
65 //===----------------------------------------------------------------------===//
66 // Pseudo-instructions and codegen patterns
67 //===----------------------------------------------------------------------===//
69 let Predicates = [HasStdExtMOrZmmul] in {
70 def : PatGprGpr<mul, MUL>;
71 def : PatGprGpr<mulhs, MULH>;
72 def : PatGprGpr<mulhu, MULHU>;
73 def : PatGprGpr<riscv_mulhsu, MULHSU>;
74 } // Predicates = [HasStdExtMOrZmmul]
76 let Predicates = [HasStdExtM] in {
77 def : PatGprGpr<sdiv, DIV>;
78 def : PatGprGpr<udiv, DIVU>;
79 def : PatGprGpr<srem, REM>;
80 def : PatGprGpr<urem, REMU>;
81 } // Predicates = [HasStdExtM]
83 // Select W instructions if only the lower 32-bits of the result are used.
84 let Predicates = [HasStdExtMOrZmmul, IsRV64] in
85 def : PatGprGpr<binop_allwusers<mul>, MULW>;
87 let Predicates = [HasStdExtM, IsRV64] in {
88 def : PatGprGpr<riscv_divw, DIVW>;
89 def : PatGprGpr<riscv_divuw, DIVUW>;
90 def : PatGprGpr<riscv_remuw, REMUW>;
92 // Handle the specific cases where using DIVU/REMU would be correct and result
93 // in fewer instructions than emitting DIVUW/REMUW then zero-extending the
95 def : Pat<(and (riscv_divuw (assertzexti32 GPR:$rs1),
96 (assertzexti32 GPR:$rs2)), 0xffffffff),
97 (DIVU GPR:$rs1, GPR:$rs2)>;
98 def : Pat<(and (riscv_remuw (assertzexti32 GPR:$rs1),
99 (assertzexti32 GPR:$rs2)), 0xffffffff),
100 (REMU GPR:$rs1, GPR:$rs2)>;
102 // Although the sexti32 operands may not have originated from an i32 srem,
103 // this pattern is safe as it is impossible for two sign extended inputs to
104 // produce a result where res[63:32]=0 and res[31]=1.
105 def : Pat<(srem (sexti32 (i64 GPR:$rs1)), (sexti32 (i64 GPR:$rs2))),
106 (REMW GPR:$rs1, GPR:$rs2)>;
107 } // Predicates = [HasStdExtM, IsRV64]
109 let Predicates = [HasStdExtMOrZmmul, IsRV64, NotHasStdExtZba] in {
110 // Special case for calculating the full 64-bit product of a 32x32 unsigned
111 // multiply where the inputs aren't known to be zero extended. We can shift the
112 // inputs left by 32 and use a MULHU. This saves two SRLIs needed to finish
113 // zeroing the upper 32 bits.
114 def : Pat<(i64 (mul (and GPR:$rs1, 0xffffffff), (and GPR:$rs2, 0xffffffff))),
115 (MULHU (SLLI GPR:$rs1, 32), (SLLI GPR:$rs2, 32))>;
116 } // Predicates = [HasStdExtMOrZmmul, IsRV64, NotHasStdExtZba]
118 //===----------------------------------------------------------------------===//
119 // Experimental RV64 i32 legalization patterns.
120 //===----------------------------------------------------------------------===//
122 let Predicates = [HasStdExtMOrZmmul, IsRV64] in {
123 def : PatGprGpr<mul, MULW, i32, i32>;
126 let Predicates = [HasStdExtM, IsRV64] in {
127 def : PatGprGpr<sdiv, DIVW, i32, i32>;
128 def : PatGprGpr<udiv, DIVUW, i32, i32>;
129 def : PatGprGpr<srem, REMW, i32, i32>;
130 def : PatGprGpr<urem, REMUW, i32, i32>;