[PATCH] D104436: [RISCV] Optimize mul-add in the zba extension with SH*ADD

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 17 16:49:53 PDT 2021


craig.topper added a comment.

There's a more generic optimization hiding here. Could we teach decomposeMulByConstant to emit (shl (sh1add X, X), C) to handle any constant of the form (3 << C). Similar for (shl (sh2add X, X)) to handle (5 << C), and (shl (sh3add X, X)) to handle (9 << C). If the multiply happens to be used by an add the existing patterns would combine the ADD and the SHL when possible.

If you want to try that as a followup. I'd suggest using the sequence you'd get from that instead. So your isel patterns would be

  def : Pat<(add (mul GPR:$rs1, (XLenVT 6)), GPR:$rs2),
            (SH1ADD (SH1ADD GPR:$rs1, GPR:$rs1), GPR:$rs2)>;
  def : Pat<(add (mul GPR:$rs1, (XLenVT 10)), GPR:$rs2),
            (SH1ADD (SH2ADD GPR:$rs1, GPR:$rs1), GPR:$rs2)>;
  def : Pat<(add (mul GPR:$rs1, (XLenVT 12)), GPR:$rs2),
            (SH2ADD (SH1ADD GPR:$rs1, GPR:$rs1), GPR:$rs2)>;

And you can add these additional cases.

  def : Pat<(add (mul GPR:$rs1, (XLenVT 24)), GPR:$rs2),
            (SH3ADD (SH1ADD GPR:$rs1, GPR:$rs1), GPR:$rs2)>;
  def : Pat<(add (mul GPR:$rs1, (XLenVT 20)), GPR:$rs2),
            (SH2ADD (SH2ADD GPR:$rs1, GPR:$rs1), GPR:$rs2)>;
  def : Pat<(add (mul GPR:$rs1, (XLenVT 40)), GPR:$rs2),
            (SH3ADD (SH2ADD GPR:$rs1, GPR:$rs1), GPR:$rs2)>;
  def : Pat<(add (mul GPR:$rs1, (XLenVT 18)), GPR:$rs2),
            (SH1ADD (SH3ADD GPR:$rs1, GPR:$rs1), GPR:$rs2)>;
  def : Pat<(add (mul GPR:$rs1, (XLenVT 36)), GPR:$rs2),
            (SH2ADD (SH3ADD GPR:$rs1, GPR:$rs1), GPR:$rs2)>;
  def : Pat<(add (mul GPR:$rs1, (XLenVT 72)), GPR:$rs2),
            (SH3ADD (SH3ADD GPR:$rs1, GPR:$rs1), GPR:$rs2)>;

X86 does basically the same optimization using LEA which is like our SHNADD. https://godbolt.org/z/e8PTT3oTo



================
Comment at: llvm/lib/Target/RISCV/RISCVInstrInfoB.td:974
+
+def : Pat<(add (mul GPR:$rs1, (XLenVT 6)), GPR:$rs2),
+          (SH1ADD GPR:$rs1, (SH2ADD GPR:$rs1, GPR:$rs2))>;
----------------
What if the multiply has an additional user that isn't the add?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D104436/new/

https://reviews.llvm.org/D104436



More information about the llvm-commits mailing list