@@ -836,20 +836,42 @@ bool VPInstruction::onlyFirstPartUsed(const VPValue *Op) const {
836
836
837
837
InstructionCost VPInstruction::computeCost (ElementCount VF,
838
838
VPCostContext &Ctx) const {
839
- Type *ValTy = Ctx.Types .inferScalarType (getOperand (0 ));
840
839
841
840
switch (getOpcode ()) {
842
841
case VPInstruction::BranchOnCount: {
843
- // BranchOnCount will genearte icmp_eq + br instructions and the
844
- // cost of branch will be calculated in VPRegionBlock.
845
- // If the vector loop only executed once, ignore the cost of the cmp.
842
+ Type *ValTy = Ctx.Types .inferScalarType (getOperand (0 ));
843
+
844
+ // If the vector loop only executed once, ignore the cost.
845
+ // TODO: We can remove this after hoist `unrollByUF` and
846
+ // `optimizeForVFandUF` which will should optimize BranchOnCount out.
846
847
auto TC = dyn_cast_if_present<ConstantInt>(
847
848
getParent ()->getPlan ()->getTripCount ()->getUnderlyingValue ());
848
849
if (TC && VF.isFixed () && TC->getSExtValue () == VF.getFixedValue ())
849
850
return 0 ;
851
+
852
+ // BranchOnCount will generate icmp_eq + br instructions and the
853
+ // cost of branch will be calculated in VPRegionBlock.
850
854
return Ctx.TTI .getCmpSelInstrCost (Instruction::ICmp, ValTy, nullptr ,
851
855
CmpInst::ICMP_EQ, Ctx.CostKind );
852
856
}
857
+ case VPInstruction::BranchOnCond: {
858
+ // BranchOnCond will generate `extractelement` when the condition is vector
859
+ // type.
860
+ VPValue *Op = getOperand (0 );
861
+ VPRecipeBase *R = Op->getDefiningRecipe ();
862
+ if (R &&
863
+ any_of (R->operands (), [&](VPValue *V) { return !R->usesScalars (V); }) &&
864
+ VF.isVector ())
865
+ return Ctx.TTI .getVectorInstrCost (
866
+ Instruction::ExtractElement,
867
+ cast<VectorType>(
868
+ toVectorTy (Ctx.Types .inferScalarType (getOperand (0 )), VF)),
869
+ Ctx.CostKind , 0 , nullptr , nullptr );
870
+
871
+ // Otherwise, BranchOnCond is free since the branch cost is already
872
+ // calculated by VPBB.
873
+ return 0 ;
874
+ }
853
875
default :
854
876
// TODO: Compute accurate cost after retiring the legacy cost model.
855
877
return 0 ;
0 commit comments