[llvm-branch-commits] [llvm] release/23.x: [RISCV] Specially handle multiply by negative power-of-two cases (#244731) (PR #244785)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Fri Nov 20 17:59:29 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-risc-v
Author: llvmbot
<details>
<summary>Changes</summary>
Backport d361f7b3f5946f69f9e9e54fb28feac5da45d7d0
Requested by: @<!-- -->lenary
---
Full diff: https://github.com/llvm/llvm-project/pull/244785.diff
2 Files Affected:
- (modified) llvm/lib/Target/RISCV/RISCVISelLowering.cpp (+20-1)
- (added) llvm/test/CodeGen/RISCV/mul-to-addsub-shl-xlen.ll (+116)
``````````diff
diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index 64ffee698c11b1..6c8150aa6bf3e4 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -18020,6 +18020,20 @@ static SDValue expandMulToNAFSequence(SDNode *N, SelectionDAG &DAG,
return Result;
}
+// Specially handle negative power of two cases as to avoid
+// overflow conditions.
+// X * (-2^N) -> sub (0, (shl X, C1))
+static SDValue expandMulByNegPowTwo(SDNode *N, SelectionDAG &DAG,
+ uint64_t MulAmt) {
+ uint64_t MulAmtLowBit = MulAmt & (-MulAmt);
+ SDValue X = N->getOperand(0);
+ EVT VT = N->getValueType(0);
+ SDLoc DL(N);
+ SDValue ShiftAmt = DAG.getShiftAmountConstant(Log2_64(MulAmtLowBit), VT, DL);
+ SDValue Shift = DAG.getNode(ISD::SHL, DL, VT, X, ShiftAmt);
+ return DAG.getNegative(Shift, DL, VT);
+}
+
// X * (2^N +/- 2^M) -> (add/sub (shl X, C1), (shl X, C2))
static SDValue expandMulToAddOrSubOfShl(SDNode *N, SelectionDAG &DAG,
uint64_t MulAmt) {
@@ -18218,8 +18232,13 @@ static SDValue expandMul(SDNode *N, SelectionDAG &DAG,
}
}
- if (SDValue V = expandMulToAddOrSubOfShl(N, DAG, MulAmt))
+ // Only attempt to call expandMulToAddOrSubOfShl() on non
+ // power-of-two cases to avoid overflow conditions.
+ if (CNode->getAPIntValue().isNegatedPowerOf2()) {
+ return expandMulByNegPowTwo(N, DAG, MulAmt);
+ } else if (SDValue V = expandMulToAddOrSubOfShl(N, DAG, MulAmt)) {
return V;
+ }
if (!Subtarget.hasStdExtZmmul())
return expandMulToNAFSequence(N, DAG, MulAmt);
diff --git a/llvm/test/CodeGen/RISCV/mul-to-addsub-shl-xlen.ll b/llvm/test/CodeGen/RISCV/mul-to-addsub-shl-xlen.ll
new file mode 100644
index 00000000000000..37896eb6e03af0
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/mul-to-addsub-shl-xlen.ll
@@ -0,0 +1,116 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=riscv32 -mattr=+m -verify-machineinstrs < %s | FileCheck -check-prefix=RV32 %s
+; RUN: llc -mtriple=riscv64 -mattr=+m -verify-machineinstrs < %s | FileCheck -check-prefix=RV64 %s
+
+; Tests from issue #244328
+define i32 @mul_opaque_32(i32 %x) {
+; RV32-LABEL: mul_opaque_32:
+; RV32: # %bb.0:
+; RV32-NEXT: slli a0, a0, 29
+; RV32-NEXT: neg a0, a0
+; RV32-NEXT: ret
+;
+; RV64-LABEL: mul_opaque_32:
+; RV64: # %bb.0:
+; RV64-NEXT: slli a0, a0, 29
+; RV64-NEXT: negw a0, a0
+; RV64-NEXT: ret
+ %k = bitcast i32 -536870912 to i32
+ %r = mul i32 %x, %k
+ ret i32 %r
+}
+
+define i32 @mul_constant_32(i32 %x) {
+; RV32-LABEL: mul_constant_32:
+; RV32: # %bb.0:
+; RV32-NEXT: slli a0, a0, 29
+; RV32-NEXT: neg a0, a0
+; RV32-NEXT: ret
+;
+; RV64-LABEL: mul_constant_32:
+; RV64: # %bb.0:
+; RV64-NEXT: slli a0, a0, 29
+; RV64-NEXT: negw a0, a0
+; RV64-NEXT: ret
+ %r = mul i32 %x, -536870912
+ ret i32 %r
+}
+
+define i64 @mul_opaque_64(i64 %x) {
+; RV32-LABEL: mul_opaque_64:
+; RV32: # %bb.0:
+; RV32-NEXT: lui a2, 524288
+; RV32-NEXT: mulhu a2, a0, a2
+; RV32-NEXT: slli a1, a1, 31
+; RV32-NEXT: add a1, a0, a1
+; RV32-NEXT: add a1, a2, a1
+; RV32-NEXT: slli a0, a0, 31
+; RV32-NEXT: ret
+;
+; RV64-LABEL: mul_opaque_64:
+; RV64: # %bb.0:
+; RV64-NEXT: slli a1, a0, 31
+; RV64-NEXT: slli a0, a0, 32
+; RV64-NEXT: add a0, a0, a1
+; RV64-NEXT: ret
+ %k = bitcast i64 6442450944 to i64
+ %r = mul i64 %x, %k
+ ret i64 %r
+}
+
+define i64 @mul_constant_64(i64 %x) {
+; RV32-LABEL: mul_constant_64:
+; RV32: # %bb.0:
+; RV32-NEXT: srli a2, a0, 1
+; RV32-NEXT: add a2, a2, a0
+; RV32-NEXT: slli a1, a1, 31
+; RV32-NEXT: add a1, a2, a1
+; RV32-NEXT: slli a0, a0, 31
+; RV32-NEXT: ret
+;
+; RV64-LABEL: mul_constant_64:
+; RV64: # %bb.0:
+; RV64-NEXT: slli a1, a0, 31
+; RV64-NEXT: slli a0, a0, 32
+; RV64-NEXT: add a0, a0, a1
+; RV64-NEXT: ret
+ %r = mul i64 %x, 6442450944
+ ret i64 %r
+}
+
+define i64 @mul_opaque_64_pt2(i64 %x) {
+; RV32-LABEL: mul_opaque_64_pt2:
+; RV32: # %bb.0:
+; RV32-NEXT: mv a1, a0
+; RV32-NEXT: li a0, 0
+; RV32-NEXT: slli a1, a1, 29
+; RV32-NEXT: neg a1, a1
+; RV32-NEXT: ret
+;
+; RV64-LABEL: mul_opaque_64_pt2:
+; RV64: # %bb.0:
+; RV64-NEXT: slli a0, a0, 61
+; RV64-NEXT: neg a0, a0
+; RV64-NEXT: ret
+ %k = bitcast i64 u0xE000000000000000 to i64
+ %r = mul i64 %x, %k
+ ret i64 %r
+}
+
+define i64 @mul_constant_64_pt2(i64 %x) {
+; RV32-LABEL: mul_constant_64_pt2:
+; RV32: # %bb.0:
+; RV32-NEXT: mv a1, a0
+; RV32-NEXT: li a0, 0
+; RV32-NEXT: slli a1, a1, 29
+; RV32-NEXT: neg a1, a1
+; RV32-NEXT: ret
+;
+; RV64-LABEL: mul_constant_64_pt2:
+; RV64: # %bb.0:
+; RV64-NEXT: slli a0, a0, 61
+; RV64-NEXT: neg a0, a0
+; RV64-NEXT: ret
+ %r = mul i64 %x, u0xE000000000000000
+ ret i64 %r
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/244785
More information about the llvm-branch-commits
mailing list