[llvm] [RISCV] Expand multiplication by `2^N * 3/5/9 + 1` with SHL_ADD (PR #166933)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 10 08:54:55 PST 2025


================
@@ -944,6 +944,58 @@ define i64 @addmul146(i64 %a, i64 %b) {
   ret i64 %d
 }
 
+define i64 @mul49(i64 %a) {
+; RV64I-LABEL: mul49:
+; RV64I:       # %bb.0:
+; RV64I-NEXT:    li a1, 49
+; RV64I-NEXT:    mul a0, a0, a1
+; RV64I-NEXT:    ret
+;
+; RV64ZBA-LABEL: mul49:
+; RV64ZBA:       # %bb.0:
+; RV64ZBA-NEXT:    slli a1, a0, 4
+; RV64ZBA-NEXT:    sh1add a1, a1, a1
+; RV64ZBA-NEXT:    add a0, a1, a0
----------------
topperc wrote:

Ok, I agree with that.

Some of the transforms have ILP between 2 of the 3 instructions. This lowering is serial. There are probably other serial lowerings. On a core with more shift/add resources than multiply, a serial implementation this could still be beneficial if it prevents the multiplier from being a bottleneck.

On a single issue in order CPU with a pipelined multiplier, this may just be increasing the code size for no benefit. Freeing up the multiplier doesn't help if you can't issue any more instructions.

https://github.com/llvm/llvm-project/pull/166933


More information about the llvm-commits mailing list