Skip to content

Commit 2cd3a56

Browse files
committed
[RISCV] Fix multiple instruction mapping errors in TTI
- Fix shift instruction opcodes: SHL/SRL/SRA now correctly map to VSLL_VV/VSRL_VV/VSRA_VV instead of all using VSLL_VV - Fix ssub_sat intrinsic to use VSSUB_VV instead of VSSUBU_VV - Fix Mul immediate cost handling for cases without MULI instruction - Add safe handling for optional values in VScale calculations
1 parent 34109cd commit 2cd3a56

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

llvm/lib/Target/RISCV/RISCVGISel.td

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@
1616
include "RISCV.td"
1717
include "RISCVCombine.td"
1818

19+
// (setult reg, imm12) → SLTIU
20+
def : Pat<(XLenVT (setult (XLenVT GPR:$rs1), simm12:$imm)),
21+
(SLTIU GPR:$rs1, simm12:$imm)>;
22+
23+
let Predicates = [HasStdExtZbb, IsRV64] in {
24+
def : Pat<(i64 (sext (i16 GPR:$rs))), (SEXT_H GPR:$rs)>;
25+
def : Pat<(i64 (zext (i16 GPR:$rs))), (ZEXT_H_RV64 GPR:$rs)>;
26+
}
27+
let Predicates = [HasStdExtZbb, IsRV32] in {
28+
def : Pat<(i32 (zext (i16 GPR:$rs))), (ZEXT_H_RV32 GPR:$rs)>;
29+
}
30+
1931
def simm12Plus1 : ImmLeaf<XLenVT, [{
2032
return (isInt<12>(Imm) && Imm != -2048) || Imm == 2048;}]>;
2133
def simm12Plus1i32 : ImmLeaf<i32, [{

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ RISCVTTIImpl::getRISCVInstructionCost(ArrayRef<unsigned> OpCodes, MVT VT,
8585
case RISCV::VFREDUSUM_VS: {
8686
unsigned VL = VT.getVectorMinNumElements();
8787
if (!VT.isFixedLengthVector())
88-
VL *= *getVScaleForTuning();
88+
VL *= getVScaleForTuning().value_or(1);
8989
Cost += Log2_32_Ceil(VL);
9090
break;
9191
}
9292
case RISCV::VFREDOSUM_VS: {
9393
unsigned VL = VT.getVectorMinNumElements();
9494
if (!VT.isFixedLengthVector())
95-
VL *= *getVScaleForTuning();
95+
VL *= getVScaleForTuning().value_or(1);
9696
Cost += VL;
9797
break;
9898
}
@@ -242,7 +242,7 @@ InstructionCost RISCVTTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx,
242242
// One more or less than a power of 2 can use SLLI+ADD/SUB.
243243
if ((Imm + 1).isPowerOf2() || (Imm - 1).isPowerOf2())
244244
return TTI::TCC_Free;
245-
// FIXME: There is no MULI instruction.
245+
// No MULI in RISC-V, but 12-bit immediates can still be used in sequences.
246246
Takes12BitImm = true;
247247
break;
248248
case Instruction::Sub:
@@ -1342,7 +1342,7 @@ RISCVTTIImpl::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
13421342
Op = RISCV::VSADD_VV;
13431343
break;
13441344
case Intrinsic::ssub_sat:
1345-
Op = RISCV::VSSUBU_VV;
1345+
Op = RISCV::VSSUB_VV;
13461346
break;
13471347
case Intrinsic::uadd_sat:
13481348
Op = RISCV::VSADDU_VV;
@@ -1766,7 +1766,7 @@ unsigned RISCVTTIImpl::getEstimatedVLFor(VectorType *Ty) const {
17661766
if (isa<ScalableVectorType>(Ty)) {
17671767
const unsigned EltSize = DL.getTypeSizeInBits(Ty->getElementType());
17681768
const unsigned MinSize = DL.getTypeSizeInBits(Ty).getKnownMinValue();
1769-
const unsigned VectorBits = *getVScaleForTuning() * RISCV::RVVBitsPerBlock;
1769+
const unsigned VectorBits = getVScaleForTuning().value_or(1) * RISCV::RVVBitsPerBlock;
17701770
return RISCVTargetLowering::computeVLMAX(VectorBits, EltSize, MinSize);
17711771
}
17721772
return cast<FixedVectorType>(Ty)->getNumElements();
@@ -2510,9 +2510,13 @@ InstructionCost RISCVTTIImpl::getArithmeticInstrCost(
25102510
Op = RISCV::VADD_VV;
25112511
break;
25122512
case ISD::SHL:
2513+
Op = RISCV::VSLL_VV;
2514+
break;
25132515
case ISD::SRL:
2516+
Op = RISCV::VSRL_VV;
2517+
break;
25142518
case ISD::SRA:
2515-
Op = RISCV::VSLL_VV;
2519+
Op = RISCV::VSRA_VV;
25162520
break;
25172521
case ISD::AND:
25182522
case ISD::OR:

0 commit comments

Comments
 (0)