RISCVInstrInfoM.td 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //===-- RISCVInstrInfoM.td - RISC-V 'M' instructions -------*- tablegen -*-===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file describes the RISC-V instructions from the standard 'M', Integer
  10. // Multiplication and Division instruction set extension.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //===----------------------------------------------------------------------===//
  14. // RISC-V specific DAG Nodes.
  15. //===----------------------------------------------------------------------===//
  16. def riscv_mulhsu : SDNode<"RISCVISD::MULHSU", SDTIntBinOp>;
  17. def riscv_divw : SDNode<"RISCVISD::DIVW", SDT_RISCVIntBinOpW>;
  18. def riscv_divuw : SDNode<"RISCVISD::DIVUW", SDT_RISCVIntBinOpW>;
  19. def riscv_remuw : SDNode<"RISCVISD::REMUW", SDT_RISCVIntBinOpW>;
  20. //===----------------------------------------------------------------------===//
  21. // Instructions
  22. //===----------------------------------------------------------------------===//
  23. let Predicates = [HasStdExtMOrZmmul] in {
  24. def MUL : ALU_rr<0b0000001, 0b000, "mul", /*Commutable*/1>,
  25. Sched<[WriteIMul, ReadIMul, ReadIMul]>;
  26. def MULH : ALU_rr<0b0000001, 0b001, "mulh", /*Commutable*/1>,
  27. Sched<[WriteIMul, ReadIMul, ReadIMul]>;
  28. def MULHSU : ALU_rr<0b0000001, 0b010, "mulhsu">,
  29. Sched<[WriteIMul, ReadIMul, ReadIMul]>;
  30. def MULHU : ALU_rr<0b0000001, 0b011, "mulhu", /*Commutable*/1>,
  31. Sched<[WriteIMul, ReadIMul, ReadIMul]>;
  32. } // Predicates = [HasStdExtMOrZmmul]
  33. let Predicates = [HasStdExtM] in {
  34. def DIV : ALU_rr<0b0000001, 0b100, "div">,
  35. Sched<[WriteIDiv, ReadIDiv, ReadIDiv]>;
  36. def DIVU : ALU_rr<0b0000001, 0b101, "divu">,
  37. Sched<[WriteIDiv, ReadIDiv, ReadIDiv]>;
  38. def REM : ALU_rr<0b0000001, 0b110, "rem">,
  39. Sched<[WriteIDiv, ReadIDiv, ReadIDiv]>;
  40. def REMU : ALU_rr<0b0000001, 0b111, "remu">,
  41. Sched<[WriteIDiv, ReadIDiv, ReadIDiv]>;
  42. } // Predicates = [HasStdExtM]
  43. let Predicates = [HasStdExtMOrZmmul, IsRV64], IsSignExtendingOpW = 1 in {
  44. def MULW : ALUW_rr<0b0000001, 0b000, "mulw", /*Commutable*/1>,
  45. Sched<[WriteIMul32, ReadIMul32, ReadIMul32]>;
  46. } // Predicates = [HasStdExtMOrZmmul, IsRV64]
  47. let Predicates = [HasStdExtM, IsRV64], IsSignExtendingOpW = 1 in {
  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]
  57. //===----------------------------------------------------------------------===//
  58. // Pseudo-instructions and codegen patterns
  59. //===----------------------------------------------------------------------===//
  60. let Predicates = [HasStdExtMOrZmmul] in {
  61. def : PatGprGpr<mul, MUL>;
  62. def : PatGprGpr<mulhs, MULH>;
  63. def : PatGprGpr<mulhu, MULHU>;
  64. def : PatGprGpr<riscv_mulhsu, MULHSU>;
  65. } // Predicates = [HasStdExtMOrZmmul]
  66. let Predicates = [HasStdExtM] in {
  67. def : PatGprGpr<sdiv, DIV>;
  68. def : PatGprGpr<udiv, DIVU>;
  69. def : PatGprGpr<srem, REM>;
  70. def : PatGprGpr<urem, REMU>;
  71. } // Predicates = [HasStdExtM]
  72. // Select W instructions if only the lower 32-bits of the result are used.
  73. let Predicates = [HasStdExtMOrZmmul, IsRV64] in
  74. def : PatGprGpr<binop_allwusers<mul>, MULW>;
  75. let Predicates = [HasStdExtM, IsRV64] in {
  76. def : PatGprGpr<riscv_divw, DIVW>;
  77. def : PatGprGpr<riscv_divuw, DIVUW>;
  78. def : PatGprGpr<riscv_remuw, REMUW>;
  79. // Handle the specific cases where using DIVU/REMU would be correct and result
  80. // in fewer instructions than emitting DIVUW/REMUW then zero-extending the
  81. // result.
  82. def : Pat<(and (riscv_divuw (assertzexti32 GPR:$rs1),
  83. (assertzexti32 GPR:$rs2)), 0xffffffff),
  84. (DIVU GPR:$rs1, GPR:$rs2)>;
  85. def : Pat<(and (riscv_remuw (assertzexti32 GPR:$rs1),
  86. (assertzexti32 GPR:$rs2)), 0xffffffff),
  87. (REMU GPR:$rs1, GPR:$rs2)>;
  88. // Although the sexti32 operands may not have originated from an i32 srem,
  89. // this pattern is safe as it is impossible for two sign extended inputs to
  90. // produce a result where res[63:32]=0 and res[31]=1.
  91. def : Pat<(srem (sexti32 (i64 GPR:$rs1)), (sexti32 (i64 GPR:$rs2))),
  92. (REMW GPR:$rs1, GPR:$rs2)>;
  93. } // Predicates = [HasStdExtM, IsRV64]
  94. let Predicates = [HasStdExtMOrZmmul, IsRV64, NotHasStdExtZba] in {
  95. // Special case for calculating the full 64-bit product of a 32x32 unsigned
  96. // multiply where the inputs aren't known to be zero extended. We can shift the
  97. // inputs left by 32 and use a MULHU. This saves two SRLIs needed to finish
  98. // zeroing the upper 32 bits.
  99. def : Pat<(i64 (mul (and GPR:$rs1, 0xffffffff), (and GPR:$rs2, 0xffffffff))),
  100. (MULHU (SLLI GPR:$rs1, 32), (SLLI GPR:$rs2, 32))>;
  101. } // Predicates = [HasStdExtMOrZmmul, IsRV64, NotHasStdExtZba]