Skip to content

Commit 441115b

Browse files
committed
[RISCV] Expand constant multiplication for targets without M extension
1 parent 690a30f commit 441115b

12 files changed

+1491
-700
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 110 additions & 7 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"
@@ -15452,6 +15453,105 @@ static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
1545215453
return combineSelectAndUseCommutative(N, DAG, /*AllOnes*/ false, Subtarget);
1545315454
}
1545415455

15456+
static SDValue expandMulToNAFSequence(SDNode *N, SelectionDAG &DAG,
15457+
const SDLoc &DL, uint64_t MulAmt) {
15458+
EVT VT = N->getValueType(0);
15459+
const uint64_t BitWidth = VT.getFixedSizeInBits();
15460+
15461+
// Find the Non-adjacent form of the multiplier.
15462+
llvm::SmallVector<std::pair<bool, uint64_t>> Sequence; // {isAdd, shamt}
15463+
for (uint64_t E = MulAmt, I = 0; E && I < BitWidth; ++I, E >>= 1) {
15464+
if (E & 1) {
15465+
bool IsAdd = (E & 3) == 1;
15466+
Sequence.push_back({IsAdd, I});
15467+
E -= IsAdd ? 1 : -1;
15468+
}
15469+
}
15470+
15471+
SDValue Result = DAG.getConstant(0, DL, N->getValueType(0));
15472+
SDValue N0 = N->getOperand(0);
15473+
15474+
for (const auto &Op : Sequence) {
15475+
SDValue ShiftVal;
15476+
if (Op.second > 0)
15477+
ShiftVal =
15478+
DAG.getNode(ISD::SHL, DL, VT, N0, DAG.getConstant(Op.second, DL, VT));
15479+
else
15480+
ShiftVal = N0;
15481+
15482+
ISD::NodeType AddSubOp = Op.first ? ISD::ADD : ISD::SUB;
15483+
Result = DAG.getNode(AddSubOp, DL, VT, Result, ShiftVal);
15484+
}
15485+
return Result;
15486+
}
15487+
// Try to expand a multiply to a sequence of shifts and add/subs,
15488+
// for a machine without native mul instruction.
15489+
static SDValue expandMulToBasicOps(SDNode *N, SelectionDAG &DAG,
15490+
uint64_t MulAmt) {
15491+
EVT VT = N->getValueType(0);
15492+
const uint64_t BitWidth = VT.getFixedSizeInBits();
15493+
SDLoc DL(N);
15494+
15495+
if (MulAmt == 0)
15496+
return DAG.getConstant(0, DL, N->getValueType(0));
15497+
15498+
// Try to factorize into (2^N) * (2^M_1 +/- 1) * (2^M_2 +/- 1) * ...
15499+
uint64_t TrailingZeros = llvm::countr_zero(MulAmt);
15500+
uint64_t E = MulAmt >> TrailingZeros;
15501+
15502+
llvm::SmallVector<std::pair<bool, uint64_t>> Factors; // {is_2^M+1, M}
15503+
15504+
while (E > 1) {
15505+
bool Found = false;
15506+
for (int64_t I = BitWidth - 1; I >= 2; --I) {
15507+
uint64_t Factor = 1ULL << I;
15508+
15509+
if (E % (Factor + 1) == 0) {
15510+
Factors.push_back({true, I});
15511+
E /= Factor + 1;
15512+
Found = true;
15513+
break;
15514+
}
15515+
if (E % (Factor - 1) == 0) {
15516+
Factors.push_back({false, I});
15517+
E /= Factor - 1;
15518+
Found = true;
15519+
break;
15520+
}
15521+
}
15522+
if (!Found)
15523+
break;
15524+
}
15525+
15526+
SDValue Result;
15527+
SDValue N0 = N->getOperand(0);
15528+
15529+
bool UseFactorization = !Factors.empty() && (Factors.size() < 5);
15530+
15531+
if (UseFactorization) {
15532+
if (E == 1)
15533+
Result = N0;
15534+
else
15535+
Result = expandMulToNAFSequence(N, DAG, DL, E);
15536+
15537+
for (const auto &F : Factors) {
15538+
SDValue ShiftVal = DAG.getNode(ISD::SHL, DL, VT, Result,
15539+
DAG.getConstant(F.second, DL, VT));
15540+
15541+
ISD::NodeType AddSubOp = F.first ? ISD::ADD : ISD::SUB;
15542+
Result = DAG.getNode(AddSubOp, DL, N->getValueType(0), ShiftVal, Result);
15543+
}
15544+
15545+
if (TrailingZeros > 0)
15546+
Result = DAG.getNode(ISD::SHL, DL, VT, Result,
15547+
DAG.getConstant(TrailingZeros, DL, VT));
15548+
15549+
return Result;
15550+
}
15551+
15552+
return expandMulToNAFSequence(N, DAG, DL, MulAmt);
15553+
}
15554+
1545515555
// X * (2^N +/- 2^M) -> (add/sub (shl X, C1), (shl X, C2))
1545615556
static SDValue expandMulToAddOrSubOfShl(SDNode *N, SelectionDAG &DAG,
1545715557
uint64_t MulAmt) {
@@ -15487,21 +15587,24 @@ static SDValue expandMul(SDNode *N, SelectionDAG &DAG,
1548715587
if (DAG.getMachineFunction().getFunction().hasMinSize())
1548815588
return SDValue();
1548915589

15490-
if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
15491-
return SDValue();
15492-
1549315590
if (VT != Subtarget.getXLenVT())
1549415591
return SDValue();
1549515592

15496-
const bool HasShlAdd = Subtarget.hasStdExtZba() ||
15497-
Subtarget.hasVendorXTHeadBa() ||
15498-
Subtarget.hasVendorXAndesPerf();
15499-
1550015593
ConstantSDNode *CNode = dyn_cast<ConstantSDNode>(N->getOperand(1));
1550115594
if (!CNode)
1550215595
return SDValue();
1550315596
uint64_t MulAmt = CNode->getZExtValue();
1550415597

15598+
if (!Subtarget.hasStdExtM() && !Subtarget.hasStdExtZmmul())
15599+
return expandMulToBasicOps(N, DAG, MulAmt);
15600+
15601+
if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
15602+
return SDValue();
15603+
15604+
const bool HasShlAdd = Subtarget.hasStdExtZba() ||
15605+
Subtarget.hasVendorXTHeadBa() ||
15606+
Subtarget.hasVendorXAndesPerf();
15607+
1550515608
// WARNING: The code below is knowingly incorrect with regards to undef semantics.
1550615609
// We're adding additional uses of X here, and in principle, we should be freezing
1550715610
// X before doing so. However, adding freeze here causes real regressions, and no

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)