[llvm] [RISCV] Add 2^N + 2^M expanding pattern for mul (PR #137954)

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Fri May 9 07:53:12 PDT 2025


================
@@ -502,24 +502,23 @@ define i32 @muli32_p18(i32 %a) nounwind {
 ;
 ; RV32IM-LABEL: muli32_p18:
 ; RV32IM:       # %bb.0:
-; RV32IM-NEXT:    li a1, 18
-; RV32IM-NEXT:    mul a0, a0, a1
+; RV32IM-NEXT:    slli a1, a0, 1
+; RV32IM-NEXT:    slli a0, a0, 4
+; RV32IM-NEXT:    add a0, a0, a1
 ; RV32IM-NEXT:    ret
 ;
 ; RV64I-LABEL: muli32_p18:
 ; RV64I:       # %bb.0:
-; RV64I-NEXT:    addi sp, sp, -16
-; RV64I-NEXT:    sd ra, 8(sp) # 8-byte Folded Spill
-; RV64I-NEXT:    li a1, 18
-; RV64I-NEXT:    call __muldi3
-; RV64I-NEXT:    ld ra, 8(sp) # 8-byte Folded Reload
-; RV64I-NEXT:    addi sp, sp, 16
+; RV64I-NEXT:    slli a1, a0, 1
+; RV64I-NEXT:    slli a0, a0, 4
+; RV64I-NEXT:    add a0, a0, a1
----------------
preames wrote:

There's an oddity here I don't understand on first glance.  The RV64I configuration uses an "add" here, but the RV64M configuration uses an "addw".  This would seem like a difference in sign extension behavior, why is this correct?  And why does the existence of the extend depend on the M extension being enabled?

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


More information about the llvm-commits mailing list