1818
1919#include " llvm/ADT/APInt.h"
2020#include " llvm/ADT/BitVector.h"
21+ #include " llvm/ADT/STLExtras.h"
2122#include " llvm/ADT/SmallPtrSet.h"
2223#include " llvm/ADT/SmallVector.h"
2324#include " llvm/Analysis/LoopInfo.h"
@@ -347,6 +348,21 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
347348 return Cost;
348349 }
349350
351+ // / Filter out constant and duplicated entries in \p Ops and return a vector
352+ // / containing the types from \p Tys corresponding to the remaining operands.
353+ static SmallVector<Type *, 4 >
354+ filterConstantAndDuplicatedOperands (ArrayRef<const Value *> Ops,
355+ ArrayRef<Type *> Tys) {
356+ SmallPtrSet<const Value *, 4 > UniqueOperands;
357+ SmallVector<Type *, 4 > FilteredTys;
358+ for (const auto &[Op, Ty] : zip_equal (Ops, Tys)) {
359+ if (isa<Constant>(Op) || !UniqueOperands.insert (Op).second )
360+ continue ;
361+ FilteredTys.push_back (Ty);
362+ }
363+ return FilteredTys;
364+ }
365+
350366protected:
351367 explicit BasicTTIImplBase (const TargetMachine *TM, const DataLayout &DL)
352368 : BaseT(DL) {}
@@ -935,29 +951,21 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
935951 CostKind);
936952 }
937953
938- // / Estimate the overhead of scalarizing an instructions unique
939- // / non-constant operands. The (potentially vector) types to use for each of
954+ // / Estimate the overhead of scalarizing an instruction's
955+ // / operands. The (potentially vector) types to use for each of
940956 // / argument are passes via Tys.
941957 InstructionCost getOperandsScalarizationOverhead (
942- ArrayRef<const Value *> Args, ArrayRef<Type *> Tys,
943- TTI::TargetCostKind CostKind) const override {
944- assert (Args.size () == Tys.size () && " Expected matching Args and Tys" );
945-
958+ ArrayRef<Type *> Tys, TTI::TargetCostKind CostKind) const override {
946959 InstructionCost Cost = 0 ;
947- SmallPtrSet<const Value*, 4 > UniqueOperands;
948- for (int I = 0 , E = Args.size (); I != E; I++) {
960+ for (Type *Ty : Tys) {
949961 // Disregard things like metadata arguments.
950- const Value *A = Args[I];
951- Type *Ty = Tys[I];
952962 if (!Ty->isIntOrIntVectorTy () && !Ty->isFPOrFPVectorTy () &&
953963 !Ty->isPtrOrPtrVectorTy ())
954964 continue ;
955965
956- if (!isa<Constant>(A) && UniqueOperands.insert (A).second ) {
957- if (auto *VecTy = dyn_cast<VectorType>(Ty))
958- Cost += getScalarizationOverhead (VecTy, /* Insert*/ false ,
959- /* Extract*/ true , CostKind);
960- }
966+ if (auto *VecTy = dyn_cast<VectorType>(Ty))
967+ Cost += getScalarizationOverhead (VecTy, /* Insert*/ false ,
968+ /* Extract*/ true , CostKind);
961969 }
962970
963971 return Cost;
@@ -974,7 +982,8 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
974982 InstructionCost Cost = getScalarizationOverhead (
975983 RetTy, /* Insert*/ true , /* Extract*/ false , CostKind);
976984 if (!Args.empty ())
977- Cost += getOperandsScalarizationOverhead (Args, Tys, CostKind);
985+ Cost += getOperandsScalarizationOverhead (
986+ filterConstantAndDuplicatedOperands (Args, Tys), CostKind);
978987 else
979988 // When no information on arguments is provided, we add the cost
980989 // associated with one argument as a heuristic.
@@ -2170,8 +2179,9 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
21702179 /* Insert=*/ true , /* Extract=*/ false , CostKind);
21712180 }
21722181 }
2173- ScalarizationCost +=
2174- getOperandsScalarizationOverhead (Args, ICA.getArgTypes (), CostKind);
2182+ ScalarizationCost += getOperandsScalarizationOverhead (
2183+ filterConstantAndDuplicatedOperands (Args, ICA.getArgTypes ()),
2184+ CostKind);
21752185 }
21762186
21772187 IntrinsicCostAttributes Attrs (IID, RetTy, ICA.getArgTypes (), FMF, I,
0 commit comments