Skip to content

Commit 0b98c35

Browse files
committed
[RISCV] Restructure i1 insert/extract element costing [nfc-ish]
These get expanded through i8, so let's just model that directly instead of looking at the expanded code and trying to fit that into the flow. This results in slightly better costs for constant indices today (we can remove the add), but is mostly focused on making future changes easier. Differential Revision: https://reviews.llvm.org/D158770
1 parent 8ca614a commit 0b98c35

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,6 +1505,31 @@ InstructionCost RISCVTTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val,
15051505
if (!isTypeLegal(Val))
15061506
return BaseT::getVectorInstrCost(Opcode, Val, CostKind, Index, Op0, Op1);
15071507

1508+
// Mask vector extract/insert is expanded via e8.
1509+
if (Val->getScalarSizeInBits() == 1) {
1510+
VectorType *WideTy =
1511+
VectorType::get(IntegerType::get(Val->getContext(), 8),
1512+
cast<VectorType>(Val)->getElementCount());
1513+
if (Opcode == Instruction::ExtractElement) {
1514+
InstructionCost ExtendCost
1515+
= getCastInstrCost(Instruction::ZExt, WideTy, Val,
1516+
TTI::CastContextHint::None, CostKind);
1517+
InstructionCost ExtractCost
1518+
= getVectorInstrCost(Opcode, WideTy, CostKind, Index, nullptr, nullptr);
1519+
return ExtendCost + ExtractCost;
1520+
}
1521+
InstructionCost ExtendCost
1522+
= getCastInstrCost(Instruction::ZExt, WideTy, Val,
1523+
TTI::CastContextHint::None, CostKind);
1524+
InstructionCost InsertCost
1525+
= getVectorInstrCost(Opcode, WideTy, CostKind, Index, nullptr, nullptr);
1526+
InstructionCost TruncCost
1527+
= getCastInstrCost(Instruction::Trunc, Val, WideTy,
1528+
TTI::CastContextHint::None, CostKind);
1529+
return ExtendCost + InsertCost + TruncCost;
1530+
}
1531+
1532+
15081533
// In RVV, we could use vslidedown + vmv.x.s to extract element from vector
15091534
// and vslideup + vmv.s.x to insert element to vector.
15101535
unsigned BaseCost = 1;
@@ -1526,30 +1551,6 @@ InstructionCost RISCVTTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val,
15261551
SlideCost = 1; // With a constant index, we do not need to use addi.
15271552
}
15281553

1529-
// Mask vector extract/insert element is different from normal case.
1530-
if (Val->getScalarSizeInBits() == 1) {
1531-
// For extractelement, we need the following instructions:
1532-
// vmv.v.i v8, 0
1533-
// vmerge.vim v8, v8, 1, v0
1534-
// vsetivli zero, 1, e8, m2, ta, mu (not count)
1535-
// vslidedown.vx v8, v8, a0
1536-
// vmv.x.s a0, v8
1537-
1538-
// For insertelement, we need the following instructions:
1539-
// vsetvli a2, zero, e8, m1, ta, mu (not count)
1540-
// vmv.s.x v8, a0
1541-
// vmv.v.i v9, 0
1542-
// vmerge.vim v9, v9, 1, v0
1543-
// addi a0, a1, 1
1544-
// vsetvli zero, a0, e8, m1, tu, mu (not count)
1545-
// vslideup.vx v9, v8, a1
1546-
// vsetvli a0, zero, e8, m1, ta, mu (not count)
1547-
// vand.vi v8, v9, 1
1548-
// vmsne.vi v0, v8, 0
1549-
1550-
// TODO: should we count these special vsetvlis?
1551-
BaseCost = Opcode == Instruction::InsertElement ? 5 : 3;
1552-
}
15531554
// Extract i64 in the target that has XLEN=32 need more instruction.
15541555
if (Val->getScalarType()->isIntegerTy() &&
15551556
ST->getXLen() < Val->getScalarSizeInBits()) {

0 commit comments

Comments
 (0)