[PATCH] D107711: [RISCV] Optimize (add (mul x, c0), c1)

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 9 09:22:57 PDT 2021


craig.topper added subscribers: spatel, lebedev.ri.
craig.topper added a comment.

In D107711#2934246 <https://reviews.llvm.org/D107711#2934246>, @benshi001 wrote:

> Is there any better way to implement this optimization other than ISelDagToDag?
>
> 1. It seems hard to calculate c1/c0 by writing TD mapping rules.
>
> 2. I also tried DAG transform in `RISCVTargetLowering::ReplaceNodeResults`, it is also not easy, since it involves more code about legalization.

It should probably be a DAG combine and the transform that's turning (mul (add X, C1), C2) into (add (mul X, C2), C1 * C2) should ask the target if it is profitable, or at least call isLegalAddImmediate for C1*C2. @spatel or @lebedev.ri, what do you think?



================
Comment at: llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp:18
 #include "RISCVMachineFunctionInfo.h"
+#include "set"
 #include "llvm/CodeGen/MachineFrameInfo.h"
----------------
#include <set>. and it should be after all llvm headers


================
Comment at: llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp:534
+    if (Subtarget->hasStdExtZba()) {
+      int64_t Arr[] = {6,  10, 18, 12, 20, 36, 24, 40, 72, 11,
+                       19, 13, 21, 37, 25, 41, 73, 27, 45, 81};
----------------
Use std::array so you can use begin()/end() on the std::set.

Though you could sort the array and use std::binary_search and avoid the set completely.


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

https://reviews.llvm.org/D107711



More information about the llvm-commits mailing list