Skip to content

Commit dae5c4e

Browse files
authored
[RISCV] Expand constant multiplication for targets without M extension (llvm#137195)
Closes llvm#137023 On RISC-V machines without a native multiply instruction (e.g., `rv32i` base), multiplying a variable by a constant integer often compiles to a call to a library routine like `__mul{s,d}i3`. ```assembly .globl __mulxi3 .type __mulxi3, @function __mulxi3: mv a2, a0 mv a0, zero .L1: andi a3, a1, 1 beqz a3, .L2 add a0, a0, a2 .L2: srli a1, a1, 1 slli a2, a2, 1 bnez a1, .L1 ret ``` This library function implements multiplication in software using a loop of shifts and adds, processing the constant bit by bit. On rv32i, it requires a minimum of 8 instructions (for multiply by `0`) and up to about 200 instructions (by `0xffffffff`), involves heavy branching and function call overhead. When not optimizing for size, we could expand the constant multiplication into a sequence of shift and add/sub instructions. For now we use non-adjacent form for the shift and add/sub sequence, which could save 1/2 - 2/3 instructions compared to a shl+add-only sequence.
1 parent 8d63afb commit dae5c4e

12 files changed

+1537
-669
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "RISCVSelectionDAGInfo.h"
2121
#include "RISCVSubtarget.h"
2222
#include "llvm/ADT/SmallSet.h"
23+
#include "llvm/ADT/SmallVector.h"
2324
#include "llvm/ADT/Statistic.h"
2425
#include "llvm/Analysis/MemoryLocation.h"
2526
#include "llvm/Analysis/ValueTracking.h"
@@ -15502,6 +15503,32 @@ static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
1550215503
return combineSelectAndUseCommutative(N, DAG, /*AllOnes*/ false, Subtarget);
1550315504
}
1550415505

15506+
// Try to expand a multiply to a sequence of shifts and add/subs,
15507+
// for a machine without native mul instruction.
15508+
static SDValue expandMulToNAFSequence(SDNode *N, SelectionDAG &DAG,
15509+
uint64_t MulAmt) {
15510+
SDLoc DL(N);
15511+
EVT VT = N->getValueType(0);
15512+
const uint64_t BitWidth = VT.getFixedSizeInBits();
15513+
15514+
SDValue Result = DAG.getConstant(0, DL, N->getValueType(0));
15515+
SDValue N0 = N->getOperand(0);
15516+
15517+
// Find the Non-adjacent form of the multiplier.
15518+
for (uint64_t E = MulAmt, I = 0; E && I < BitWidth; ++I, E >>= 1) {
15519+
if (E & 1) {
15520+
bool IsAdd = (E & 3) == 1;
15521+
E -= IsAdd ? 1 : -1;
15522+
SDValue ShiftVal = DAG.getNode(ISD::SHL, DL, VT, N0,
15523+
DAG.getShiftAmountConstant(I, VT, DL));
15524+
ISD::NodeType AddSubOp = IsAdd ? ISD::ADD : ISD::SUB;
15525+
Result = DAG.getNode(AddSubOp, DL, VT, Result, ShiftVal);
15526+
}
15527+
}
15528+
15529+
return Result;
15530+
}
15531+
1550515532
// X * (2^N +/- 2^M) -> (add/sub (shl X, C1), (shl X, C2))
1550615533
static SDValue expandMulToAddOrSubOfShl(SDNode *N, SelectionDAG &DAG,
1550715534
uint64_t MulAmt) {
@@ -15537,21 +15564,24 @@ static SDValue expandMul(SDNode *N, SelectionDAG &DAG,
1553715564
if (DAG.getMachineFunction().getFunction().hasMinSize())
1553815565
return SDValue();
1553915566

15540-
if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
15541-
return SDValue();
15542-
1554315567
if (VT != Subtarget.getXLenVT())
1554415568
return SDValue();
1554515569

15546-
const bool HasShlAdd = Subtarget.hasStdExtZba() ||
15547-
Subtarget.hasVendorXTHeadBa() ||
15548-
Subtarget.hasVendorXAndesPerf();
15570+
bool ShouldExpandMul =
15571+
(!DCI.isBeforeLegalize() && !DCI.isCalledByLegalizer()) ||
15572+
!Subtarget.hasStdExtZmmul();
15573+
if (!ShouldExpandMul)
15574+
return SDValue();
1554915575

1555015576
ConstantSDNode *CNode = dyn_cast<ConstantSDNode>(N->getOperand(1));
1555115577
if (!CNode)
1555215578
return SDValue();
1555315579
uint64_t MulAmt = CNode->getZExtValue();
1555415580

15581+
const bool HasShlAdd = Subtarget.hasStdExtZba() ||
15582+
Subtarget.hasVendorXTHeadBa() ||
15583+
Subtarget.hasVendorXAndesPerf();
15584+
1555515585
// WARNING: The code below is knowingly incorrect with regards to undef semantics.
1555615586
// We're adding additional uses of X here, and in principle, we should be freezing
1555715587
// X before doing so. However, adding freeze here causes real regressions, and no
@@ -15689,6 +15719,9 @@ static SDValue expandMul(SDNode *N, SelectionDAG &DAG,
1568915719
if (SDValue V = expandMulToAddOrSubOfShl(N, DAG, MulAmt))
1569015720
return V;
1569115721

15722+
if (!Subtarget.hasStdExtZmmul())
15723+
return expandMulToNAFSequence(N, DAG, MulAmt);
15724+
1569215725
return SDValue();
1569315726
}
1569415727

llvm/test/CodeGen/RISCV/ctlz-cttz-ctpop.ll

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -262,20 +262,33 @@ define i32 @test_cttz_i32(i32 %a) nounwind {
262262
; RV64I-NEXT: sext.w a1, a0
263263
; RV64I-NEXT: beqz a1, .LBB2_2
264264
; RV64I-NEXT: # %bb.1: # %cond.false
265-
; RV64I-NEXT: addi sp, sp, -16
266-
; RV64I-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
267-
; RV64I-NEXT: neg a1, a0
265+
; RV64I-NEXT: negw a1, a0
268266
; RV64I-NEXT: and a0, a0, a1
269-
; RV64I-NEXT: lui a1, 30667
270-
; RV64I-NEXT: addiw a1, a1, 1329
271-
; RV64I-NEXT: call __muldi3
267+
; RV64I-NEXT: slli a1, a0, 6
268+
; RV64I-NEXT: slli a2, a0, 8
269+
; RV64I-NEXT: slli a3, a0, 10
270+
; RV64I-NEXT: slli a4, a0, 12
271+
; RV64I-NEXT: add a1, a1, a2
272+
; RV64I-NEXT: slli a2, a0, 16
273+
; RV64I-NEXT: subw a3, a3, a4
274+
; RV64I-NEXT: slli a4, a0, 18
275+
; RV64I-NEXT: subw a2, a2, a4
276+
; RV64I-NEXT: slli a4, a0, 4
277+
; RV64I-NEXT: subw a4, a0, a4
278+
; RV64I-NEXT: add a1, a4, a1
279+
; RV64I-NEXT: slli a4, a0, 14
280+
; RV64I-NEXT: subw a3, a3, a4
281+
; RV64I-NEXT: slli a4, a0, 23
282+
; RV64I-NEXT: subw a2, a2, a4
283+
; RV64I-NEXT: slli a0, a0, 27
284+
; RV64I-NEXT: add a1, a1, a3
285+
; RV64I-NEXT: add a0, a2, a0
286+
; RV64I-NEXT: add a0, a1, a0
272287
; RV64I-NEXT: srliw a0, a0, 27
273288
; RV64I-NEXT: lui a1, %hi(.LCPI2_0)
274289
; RV64I-NEXT: addi a1, a1, %lo(.LCPI2_0)
275290
; RV64I-NEXT: add a0, a1, a0
276291
; RV64I-NEXT: lbu a0, 0(a0)
277-
; RV64I-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
278-
; RV64I-NEXT: addi sp, sp, 16
279292
; RV64I-NEXT: ret
280293
; RV64I-NEXT: .LBB2_2:
281294
; RV64I-NEXT: li a0, 32
@@ -730,20 +743,33 @@ define i32 @test_cttz_i32_zero_undef(i32 %a) nounwind {
730743
;
731744
; RV64I-LABEL: test_cttz_i32_zero_undef:
732745
; RV64I: # %bb.0:
733-
; RV64I-NEXT: addi sp, sp, -16
734-
; RV64I-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
735-
; RV64I-NEXT: neg a1, a0
746+
; RV64I-NEXT: negw a1, a0
736747
; RV64I-NEXT: and a0, a0, a1
737-
; RV64I-NEXT: lui a1, 30667
738-
; RV64I-NEXT: addiw a1, a1, 1329
739-
; RV64I-NEXT: call __muldi3
748+
; RV64I-NEXT: slli a1, a0, 6
749+
; RV64I-NEXT: slli a2, a0, 8
750+
; RV64I-NEXT: slli a3, a0, 10
751+
; RV64I-NEXT: slli a4, a0, 12
752+
; RV64I-NEXT: add a1, a1, a2
753+
; RV64I-NEXT: slli a2, a0, 16
754+
; RV64I-NEXT: subw a3, a3, a4
755+
; RV64I-NEXT: slli a4, a0, 18
756+
; RV64I-NEXT: subw a2, a2, a4
757+
; RV64I-NEXT: slli a4, a0, 4
758+
; RV64I-NEXT: subw a4, a0, a4
759+
; RV64I-NEXT: add a1, a4, a1
760+
; RV64I-NEXT: slli a4, a0, 14
761+
; RV64I-NEXT: subw a3, a3, a4
762+
; RV64I-NEXT: slli a4, a0, 23
763+
; RV64I-NEXT: subw a2, a2, a4
764+
; RV64I-NEXT: slli a0, a0, 27
765+
; RV64I-NEXT: add a1, a1, a3
766+
; RV64I-NEXT: add a0, a2, a0
767+
; RV64I-NEXT: add a0, a1, a0
740768
; RV64I-NEXT: srliw a0, a0, 27
741769
; RV64I-NEXT: lui a1, %hi(.LCPI6_0)
742770
; RV64I-NEXT: addi a1, a1, %lo(.LCPI6_0)
743771
; RV64I-NEXT: add a0, a1, a0
744772
; RV64I-NEXT: lbu a0, 0(a0)
745-
; RV64I-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
746-
; RV64I-NEXT: addi sp, sp, 16
747773
; RV64I-NEXT: ret
748774
;
749775
; RV32M-LABEL: test_cttz_i32_zero_undef:

0 commit comments

Comments
 (0)