18
18
19
19
#include " llvm/ADT/APInt.h"
20
20
#include " llvm/ADT/BitVector.h"
21
+ #include " llvm/ADT/STLExtras.h"
21
22
#include " llvm/ADT/SmallPtrSet.h"
22
23
#include " llvm/ADT/SmallVector.h"
23
24
#include " llvm/Analysis/LoopInfo.h"
@@ -347,6 +348,21 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
347
348
return Cost;
348
349
}
349
350
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
+
350
366
protected:
351
367
explicit BasicTTIImplBase (const TargetMachine *TM, const DataLayout &DL)
352
368
: BaseT(DL) {}
@@ -935,29 +951,21 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
935
951
CostKind);
936
952
}
937
953
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
940
956
// / argument are passes via Tys.
941
957
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 {
946
959
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) {
949
961
// Disregard things like metadata arguments.
950
- const Value *A = Args[I];
951
- Type *Ty = Tys[I];
952
962
if (!Ty->isIntOrIntVectorTy () && !Ty->isFPOrFPVectorTy () &&
953
963
!Ty->isPtrOrPtrVectorTy ())
954
964
continue ;
955
965
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);
961
969
}
962
970
963
971
return Cost;
@@ -974,7 +982,8 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
974
982
InstructionCost Cost = getScalarizationOverhead (
975
983
RetTy, /* Insert*/ true , /* Extract*/ false , CostKind);
976
984
if (!Args.empty ())
977
- Cost += getOperandsScalarizationOverhead (Args, Tys, CostKind);
985
+ Cost += getOperandsScalarizationOverhead (
986
+ filterConstantAndDuplicatedOperands (Args, Tys), CostKind);
978
987
else
979
988
// When no information on arguments is provided, we add the cost
980
989
// associated with one argument as a heuristic.
@@ -2170,8 +2179,9 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
2170
2179
/* Insert=*/ true , /* Extract=*/ false , CostKind);
2171
2180
}
2172
2181
}
2173
- ScalarizationCost +=
2174
- getOperandsScalarizationOverhead (Args, ICA.getArgTypes (), CostKind);
2182
+ ScalarizationCost += getOperandsScalarizationOverhead (
2183
+ filterConstantAndDuplicatedOperands (Args, ICA.getArgTypes ()),
2184
+ CostKind);
2175
2185
}
2176
2186
2177
2187
IntrinsicCostAttributes Attrs (IID, RetTy, ICA.getArgTypes (), FMF, I,
0 commit comments