Index: llvm/lib/Transforms/Vectorize/VectorCombine.cpp =================================================================== --- llvm/lib/Transforms/Vectorize/VectorCombine.cpp +++ llvm/lib/Transforms/Vectorize/VectorCombine.cpp @@ -34,6 +34,7 @@ #define DEBUG_TYPE "vector-combine" STATISTIC(NumVecCmp, "Number of vector compares formed"); STATISTIC(NumVecBO, "Number of vector binops formed"); +STATISTIC(NumScalarBO, "Number of scalar binops formed"); static cl::opt DisableVectorCombine( "disable-vector-combine", cl::init(false), cl::Hidden, @@ -308,6 +309,60 @@ return true; } +/// Match a vector binop instruction with inserted scalar operands and convert +/// to scalar binop followed by insertelement. +static bool scalarizeBinop(Instruction &I, const TargetTransformInfo &TTI) { + Instruction *Ins0, *Ins1; + if (!match(&I, m_BinOp(m_Instruction(Ins0), m_Instruction(Ins1)))) + return false; + + Value *V0, *V1; + uint64_t Index; + if (!match(Ins0, m_OneUse(m_InsertElement(m_Undef(), m_Value(V0), + m_ConstantInt(Index)))) || + !match(Ins1, m_OneUse(m_InsertElement(m_Undef(), m_Value(V1), + m_SpecificInt(Index))))) + return false; + + Type *ScalarTy = V0->getType(); + Type *VecTy = I.getType(); + assert(VecTy->isVectorTy() && ScalarTy == V1->getType() && + (ScalarTy->isIntegerTy() || ScalarTy->isFloatingPointTy()) && + "Unexpected types for insert into binop"); + + Instruction::BinaryOps Opcode = cast(&I)->getOpcode(); + int ScalarOpCost = TTI.getArithmeticInstrCost(Opcode, ScalarTy); + int VectorOpCost = TTI.getArithmeticInstrCost(Opcode, VecTy); + + // Get cost estimate for the insert element. This cost will factor into + // both sequences. + int InsertCost = + TTI.getVectorInstrCost(Instruction::InsertElement, VecTy, Index); + int OldCost = InsertCost + InsertCost + VectorOpCost; + int NewCost = ScalarOpCost + InsertCost; + + // We want to scalarize unless the vector variant actually has lower cost. + if (OldCost < NewCost) + return false; + + // bo (ins undef, V0, Index), (ins undef, V1, Index) --> + // ins undef, (bo V0, V1), Index + ++NumScalarBO; + IRBuilder<> Builder(&I); + Value *Scalar = Builder.CreateBinOp(Opcode, V0, V1, I.getName() + ".scalar"); + + // All IR flags are safe to back-propagate. There is no potential for extra + // poison to be created by the scalar instruction. + if (auto *ScalarInst = dyn_cast(Scalar)) + ScalarInst->copyIRFlags(&I); + + Value *Insert = + Builder.CreateInsertElement(UndefValue::get(VecTy), Scalar, Index); + I.replaceAllUsesWith(Insert); + Insert->takeName(&I); + return true; +} + /// This is the entry point for all transforms. Pass manager differences are /// handled in the callers of this function. static bool runImpl(Function &F, const TargetTransformInfo &TTI, @@ -330,6 +385,7 @@ continue; MadeChange |= foldExtractExtract(I, TTI); MadeChange |= foldBitcastShuf(I, TTI); + MadeChange |= scalarizeBinop(I, TTI); } } Index: llvm/test/Transforms/VectorCombine/X86/insert-binop.ll =================================================================== --- llvm/test/Transforms/VectorCombine/X86/insert-binop.ll +++ llvm/test/Transforms/VectorCombine/X86/insert-binop.ll @@ -8,9 +8,8 @@ define <16 x i8> @ins0_ins0_add(i8 %x, i8 %y) { ; CHECK-LABEL: @ins0_ins0_add( -; CHECK-NEXT: [[I0:%.*]] = insertelement <16 x i8> undef, i8 [[X:%.*]], i32 0 -; CHECK-NEXT: [[I1:%.*]] = insertelement <16 x i8> undef, i8 [[Y:%.*]], i32 0 -; CHECK-NEXT: [[R:%.*]] = add <16 x i8> [[I0]], [[I1]] +; CHECK-NEXT: [[R_SCALAR:%.*]] = add i8 [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: [[R:%.*]] = insertelement <16 x i8> undef, i8 [[R_SCALAR]], i64 0 ; CHECK-NEXT: ret <16 x i8> [[R]] ; %i0 = insertelement <16 x i8> undef, i8 %x, i32 0 @@ -23,9 +22,8 @@ define <8 x i16> @ins0_ins0_sub_flags(i16 %x, i16 %y) { ; CHECK-LABEL: @ins0_ins0_sub_flags( -; CHECK-NEXT: [[I0:%.*]] = insertelement <8 x i16> undef, i16 [[X:%.*]], i8 5 -; CHECK-NEXT: [[I1:%.*]] = insertelement <8 x i16> undef, i16 [[Y:%.*]], i32 5 -; CHECK-NEXT: [[R:%.*]] = sub nuw nsw <8 x i16> [[I0]], [[I1]] +; CHECK-NEXT: [[R_SCALAR:%.*]] = sub nuw nsw i16 [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: [[R:%.*]] = insertelement <8 x i16> undef, i16 [[R_SCALAR]], i64 5 ; CHECK-NEXT: ret <8 x i16> [[R]] ; %i0 = insertelement <8 x i16> undef, i16 %x, i8 5 @@ -38,9 +36,8 @@ define <2 x double> @ins0_ins0_fadd(double %x, double %y) { ; CHECK-LABEL: @ins0_ins0_fadd( -; CHECK-NEXT: [[I0:%.*]] = insertelement <2 x double> undef, double [[X:%.*]], i32 0 -; CHECK-NEXT: [[I1:%.*]] = insertelement <2 x double> undef, double [[Y:%.*]], i32 0 -; CHECK-NEXT: [[R:%.*]] = fadd reassoc nsz <2 x double> [[I0]], [[I1]] +; CHECK-NEXT: [[R_SCALAR:%.*]] = fadd reassoc nsz double [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: [[R:%.*]] = insertelement <2 x double> undef, double [[R_SCALAR]], i64 0 ; CHECK-NEXT: ret <2 x double> [[R]] ; %i0 = insertelement <2 x double> undef, double %x, i32 0