Skip to content

Commit 6d7a485

Browse files
committed
[RISCV] Expand constant multiplication for targets without M extension
1 parent d915355 commit 6d7a485

12 files changed

+1486
-708
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 109 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"
@@ -15456,6 +15457,105 @@ static SDValue performXORCombine(SDNode *N, SelectionDAG &DAG,
1545615457
return combineSelectAndUseCommutative(N, DAG, /*AllOnes*/ false, Subtarget);
1545715458
}
1545815459

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

15470-
if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
15471-
return SDValue();
15472-
1547315570
if (VT != Subtarget.getXLenVT())
1547415571
return SDValue();
1547515572

15476-
const bool HasShlAdd =
15477-
Subtarget.hasStdExtZba() || Subtarget.hasVendorXTHeadBa();
15478-
1547915573
ConstantSDNode *CNode = dyn_cast<ConstantSDNode>(N->getOperand(1));
1548015574
if (!CNode)
1548115575
return SDValue();
1548215576
uint64_t MulAmt = CNode->getZExtValue();
1548315577

15578+
if (!Subtarget.hasStdExtM() && !Subtarget.hasStdExtZmmul())
15579+
return expandMulToBasicOps(N, DAG, MulAmt);
15580+
15581+
if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
15582+
return SDValue();
15583+
15584+
const bool HasShlAdd =
15585+
Subtarget.hasStdExtZba() || Subtarget.hasVendorXTHeadBa();
15586+
1548415587
// WARNING: The code below is knowingly incorrect with regards to undef semantics.
1548515588
// We're adding additional uses of X here, and in principle, we should be freezing
1548615589
// 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)