diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp --- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp +++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp @@ -6437,24 +6437,36 @@ auto Op2Info = getOperandInfo(VL, OpIdx); SmallVector Operands(VL0->operand_values()); - InstructionCost ScalarEltCost = - TTI->getArithmeticInstrCost(E->getOpcode(), ScalarTy, CostKind, - Op1Info, Op2Info, - Operands, VL0); + InstructionCost ScalarCost = 0; + for (const Value *V : VL) { + ScalarCost += TTI->getArithmeticInstrCost( + E->getOpcode(), ScalarTy, CostKind, Op1Info, Op2Info, Operands, + isa(V) ? cast(V) : nullptr); + } + if (NeedToShuffleReuses) { - CommonCost -= (EntryVF - VL.size()) * ScalarEltCost; + CommonCost -= + (EntryVF - VL.size()) * + TTI->getArithmeticInstrCost(E->getOpcode(), ScalarTy, CostKind, + Op1Info, Op2Info, Operands); } - InstructionCost ScalarCost = VecTy->getNumElements() * ScalarEltCost; + for (unsigned I = 0, Num = VL0->getNumOperands(); I < Num; ++I) { if (all_of(VL, [I](Value *V) { return isConstant(cast(V)->getOperand(I)); })) Operands[I] = ConstantVector::getNullValue(VecTy); } + + // Populate vector with context instructions. + SmallVector OpsForVector = {VL0}; + for (const Value *V : VL.drop_front()) + OpsForVector.push_back(cast(V)); + InstructionCost VecCost = - TTI->getArithmeticInstrCost(E->getOpcode(), VecTy, CostKind, - Op1Info, Op2Info, - Operands, VL0); + TTI->getArithmeticInstrCost(E->getOpcode(), VecTy, CostKind, Op1Info, + Op2Info, Operands, OpsForVector); + LLVM_DEBUG(dumpTreeCosts(E, CommonCost, VecCost, ScalarCost)); return CommonCost + VecCost - ScalarCost; }