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

Ben Shi via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 17 18:59:05 PDT 2021


benshi001 added a comment.

In D104436#2825904 <https://reviews.llvm.org/D104436#2825904>, @craig.topper wrote:

> 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

Thanks for teaching me so much skills. It seems I should submit another patch first, which contains tests for all above ways.


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

https://reviews.llvm.org/D104436



More information about the llvm-commits mailing list