diff --git a/llvm/include/llvm/CodeGen/BasicTTIImpl.h b/llvm/include/llvm/CodeGen/BasicTTIImpl.h --- a/llvm/include/llvm/CodeGen/BasicTTIImpl.h +++ b/llvm/include/llvm/CodeGen/BasicTTIImpl.h @@ -84,13 +84,17 @@ /// Estimate a cost of Broadcast as an extract and sequence of insert /// operations. - InstructionCost getBroadcastShuffleOverhead(FixedVectorType *VTy) { + InstructionCost getBroadcastShuffleOverhead(VectorType *VTy) { + // Don't know how many lanes scalable vector has, so return invalid. + if (isa(VTy)) + return InstructionCost::getInvalid(); + InstructionCost Cost = 0; // Broadcast cost is equal to the cost of extracting the zero'th element // plus the cost of inserting it into every element of the result vector. Cost += thisT()->getVectorInstrCost(Instruction::ExtractElement, VTy, 0); - for (int i = 0, e = VTy->getNumElements(); i < e; ++i) { + for (int i = 0, e = VTy->getElementCount().getKnownMinValue(); i < e; ++i) { Cost += thisT()->getVectorInstrCost(Instruction::InsertElement, VTy, i); } return Cost; @@ -872,10 +876,9 @@ InstructionCost getShuffleCost(TTI::ShuffleKind Kind, VectorType *Tp, ArrayRef Mask, int Index, VectorType *SubTp) { - switch (improveShuffleKindFromMask(Kind, Mask)) { case TTI::SK_Broadcast: - return getBroadcastShuffleOverhead(cast(Tp)); + return getBroadcastShuffleOverhead(Tp); case TTI::SK_Select: case TTI::SK_Splice: case TTI::SK_Reverse: diff --git a/llvm/test/Analysis/CostModel/RISCV/rvv-shuffle-broadcast.ll b/llvm/test/Analysis/CostModel/RISCV/rvv-shuffle-broadcast.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Analysis/CostModel/RISCV/rvv-shuffle-broadcast.ll @@ -0,0 +1,36 @@ +; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py +; Check getShuffleCost for SK_BroadCast with scalable vector + +; RUN: opt -cost-model -analyze -mtriple=riscv64 -mattr=+m,+experimental-v -scalable-vectorization=on < %s | FileCheck %s + +define void @broadcast() { +; CHECK-LABEL: 'broadcast' +; CHECK-NEXT: Cost Model: Invalid cost for instruction: %zero = shufflevector undef, undef, zeroinitializer +; CHECK-NEXT: Cost Model: Invalid cost for instruction: %1 = shufflevector undef, undef, zeroinitializer +; CHECK-NEXT: Cost Model: Invalid cost for instruction: %2 = shufflevector undef, undef, zeroinitializer +; CHECK-NEXT: Cost Model: Invalid cost for instruction: %3 = shufflevector undef, undef, zeroinitializer +; CHECK-NEXT: Cost Model: Invalid cost for instruction: %4 = shufflevector undef, undef, zeroinitializer +; CHECK-NEXT: Cost Model: Invalid cost for instruction: %5 = shufflevector undef, undef, zeroinitializer +; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %6 = shufflevector undef, undef, zeroinitializer +; CHECK-NEXT: Cost Model: Invalid cost for instruction: %7 = shufflevector undef, undef, zeroinitializer +; CHECK-NEXT: Cost Model: Invalid cost for instruction: %8 = shufflevector undef, undef, zeroinitializer +; CHECK-NEXT: Cost Model: Invalid cost for instruction: %9 = shufflevector undef, undef, zeroinitializer +; CHECK-NEXT: Cost Model: Invalid cost for instruction: %10 = shufflevector undef, undef, zeroinitializer +; CHECK-NEXT: Cost Model: Invalid cost for instruction: %11 = shufflevector undef, undef, zeroinitializer +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret void +; + %zero = shufflevector undef, undef, zeroinitializer + %1 = shufflevector undef, undef, zeroinitializer + %2 = shufflevector undef, undef, zeroinitializer + %3 = shufflevector undef, undef, zeroinitializer + %4 = shufflevector undef, undef, zeroinitializer + %5 = shufflevector undef, undef, zeroinitializer + %6 = shufflevector undef, undef, zeroinitializer + %7 = shufflevector undef, undef, zeroinitializer + %8 = shufflevector undef, undef, zeroinitializer + %9 = shufflevector undef, undef, zeroinitializer + %10 = shufflevector undef, undef, zeroinitializer + %11 = shufflevector undef, undef, zeroinitializer + ret void +} +