@@ -1022,22 +1022,26 @@ bool VectorCombine::scalarizeVPIntrinsic(Instruction &I) {
1022
1022
// / inserted scalar operand and convert to scalar op/cmp/intrinsic followed
1023
1023
// / by insertelement.
1024
1024
bool VectorCombine::scalarizeOpOrCmp (Instruction &I) {
1025
- if (!isa<UnaryOperator, BinaryOperator, CmpInst, IntrinsicInst>(I))
1025
+ auto *UO = dyn_cast<UnaryOperator>(&I);
1026
+ auto *BO = dyn_cast<BinaryOperator>(&I);
1027
+ auto *CI = dyn_cast<CmpInst>(&I);
1028
+ auto *II = dyn_cast<IntrinsicInst>(&I);
1029
+ if (!UO && !BO && !CI && !II)
1026
1030
return false ;
1027
1031
1028
1032
// TODO: Allow intrinsics with different argument types
1029
1033
// TODO: Allow intrinsics with scalar arguments
1030
- if (auto * II = dyn_cast<IntrinsicInst>(&I))
1031
- if (! isTriviallyVectorizable (II->getIntrinsicID ()) ||
1032
- ! all_of ( II->args (),
1033
- [&II](Value *Arg) { return Arg-> getType () == II-> getType (); } ))
1034
- return false ;
1034
+ if (II && (! isTriviallyVectorizable (II-> getIntrinsicID ()) ||
1035
+ ! all_of (II->args (), [&II](Value *Arg) {
1036
+ return Arg-> getType () == II->getType ();
1037
+ }) ))
1038
+ return false ;
1035
1039
1036
1040
// Do not convert the vector condition of a vector select into a scalar
1037
1041
// condition. That may cause problems for codegen because of differences in
1038
1042
// boolean formats and register-file transfers.
1039
1043
// TODO: Can we account for that in the cost model?
1040
- if (isa<CmpInst>(I) )
1044
+ if (CI )
1041
1045
for (User *U : I.users ())
1042
1046
if (match (U, m_Select (m_Specific (&I), m_Value (), m_Value ())))
1043
1047
return false ;
@@ -1048,8 +1052,7 @@ bool VectorCombine::scalarizeOpOrCmp(Instruction &I) {
1048
1052
SmallVector<Value *> ScalarOps;
1049
1053
std::optional<uint64_t > Index;
1050
1054
1051
- auto Ops = isa<IntrinsicInst>(I) ? cast<IntrinsicInst>(I).args ()
1052
- : I.operand_values ();
1055
+ auto Ops = II ? II->args () : I.operand_values ();
1053
1056
for (Value *Op : Ops) {
1054
1057
Constant *VecC;
1055
1058
Value *V;
@@ -1089,17 +1092,16 @@ bool VectorCombine::scalarizeOpOrCmp(Instruction &I) {
1089
1092
1090
1093
unsigned Opcode = I.getOpcode ();
1091
1094
InstructionCost ScalarOpCost, VectorOpCost;
1092
- if (isa<CmpInst>(I) ) {
1093
- CmpInst::Predicate Pred = cast<CmpInst>(I). getPredicate ();
1095
+ if (CI ) {
1096
+ CmpInst::Predicate Pred = CI-> getPredicate ();
1094
1097
ScalarOpCost = TTI.getCmpSelInstrCost (
1095
1098
Opcode, ScalarTy, CmpInst::makeCmpResultType (ScalarTy), Pred, CostKind);
1096
1099
VectorOpCost = TTI.getCmpSelInstrCost (
1097
1100
Opcode, VecTy, CmpInst::makeCmpResultType (VecTy), Pred, CostKind);
1098
- } else if (isa<UnaryOperator, BinaryOperator>(I) ) {
1101
+ } else if (UO || BO ) {
1099
1102
ScalarOpCost = TTI.getArithmeticInstrCost (Opcode, ScalarTy, CostKind);
1100
1103
VectorOpCost = TTI.getArithmeticInstrCost (Opcode, VecTy, CostKind);
1101
1104
} else {
1102
- auto *II = cast<IntrinsicInst>(&I);
1103
1105
IntrinsicCostAttributes ScalarICA (
1104
1106
II->getIntrinsicID (), ScalarTy,
1105
1107
SmallVector<Type *>(II->arg_size (), ScalarTy));
@@ -1113,20 +1115,16 @@ bool VectorCombine::scalarizeOpOrCmp(Instruction &I) {
1113
1115
// Fold the vector constants in the original vectors into a new base vector to
1114
1116
// get more accurate cost modelling.
1115
1117
Value *NewVecC = nullptr ;
1116
- if (auto *CI = dyn_cast<CmpInst>(&I) )
1118
+ if (CI )
1117
1119
NewVecC = ConstantFoldCompareInstOperands (CI->getPredicate (), VecCs[0 ],
1118
1120
VecCs[1 ], *DL);
1119
- else if (isa<UnaryOperator>(I))
1120
- NewVecC = ConstantFoldUnaryOpOperand ((Instruction::UnaryOps)Opcode,
1121
- VecCs[0 ], *DL);
1122
- else if (isa<BinaryOperator>(I))
1123
- NewVecC = ConstantFoldBinaryOpOperands ((Instruction::BinaryOps)Opcode,
1124
- VecCs[0 ], VecCs[1 ], *DL);
1125
- else if (auto *II = dyn_cast<IntrinsicInst>(&I)) {
1126
- if (II->arg_size () == 2 )
1127
- NewVecC = ConstantFoldBinaryIntrinsic (II->getIntrinsicID (), VecCs[0 ],
1128
- VecCs[1 ], II->getType (), II);
1129
- }
1121
+ else if (UO)
1122
+ NewVecC = ConstantFoldUnaryOpOperand (Opcode, VecCs[0 ], *DL);
1123
+ else if (BO)
1124
+ NewVecC = ConstantFoldBinaryOpOperands (Opcode, VecCs[0 ], VecCs[1 ], *DL);
1125
+ else if (II->arg_size () == 2 )
1126
+ NewVecC = ConstantFoldBinaryIntrinsic (II->getIntrinsicID (), VecCs[0 ],
1127
+ VecCs[1 ], II->getType (), II);
1130
1128
1131
1129
// Get cost estimate for the insert element. This cost will factor into
1132
1130
// both sequences.
@@ -1149,11 +1147,11 @@ bool VectorCombine::scalarizeOpOrCmp(Instruction &I) {
1149
1147
1150
1148
// vec_op (inselt VecC0, V0, Index), (inselt VecC1, V1, Index) -->
1151
1149
// inselt NewVecC, (scalar_op V0, V1), Index
1152
- if (isa<CmpInst>(I) )
1150
+ if (CI )
1153
1151
++NumScalarCmp;
1154
- else if (isa<UnaryOperator, BinaryOperator>(I) )
1152
+ else if (UO || BO )
1155
1153
++NumScalarOps;
1156
- else if (isa<IntrinsicInst>(I))
1154
+ else
1157
1155
++NumScalarIntrinsic;
1158
1156
1159
1157
// For constant cases, extract the scalar element, this should constant fold.
@@ -1163,13 +1161,12 @@ bool VectorCombine::scalarizeOpOrCmp(Instruction &I) {
1163
1161
cast<Constant>(VecC), Builder.getInt64 (*Index));
1164
1162
1165
1163
Value *Scalar;
1166
- if (auto *CI = dyn_cast<CmpInst>(&I) )
1164
+ if (CI )
1167
1165
Scalar = Builder.CreateCmp (CI->getPredicate (), ScalarOps[0 ], ScalarOps[1 ]);
1168
- else if (isa<UnaryOperator, BinaryOperator>(I) )
1166
+ else if (UO || BO )
1169
1167
Scalar = Builder.CreateNAryOp (Opcode, ScalarOps);
1170
1168
else
1171
- Scalar = Builder.CreateIntrinsic (
1172
- ScalarTy, cast<IntrinsicInst>(I).getIntrinsicID (), ScalarOps);
1169
+ Scalar = Builder.CreateIntrinsic (ScalarTy, II->getIntrinsicID (), ScalarOps);
1173
1170
1174
1171
Scalar->setName (I.getName () + " .scalar" );
1175
1172
@@ -1183,13 +1180,13 @@ bool VectorCombine::scalarizeOpOrCmp(Instruction &I) {
1183
1180
SmallVector<Value *> VecCValues;
1184
1181
VecCValues.reserve (VecCs.size ());
1185
1182
append_range (VecCValues, VecCs);
1186
- if (auto *CI = dyn_cast<CmpInst>(&I) )
1183
+ if (CI )
1187
1184
NewVecC = Builder.CreateCmp (CI->getPredicate (), VecCs[0 ], VecCs[1 ]);
1188
- else if (isa<UnaryOperator, BinaryOperator>(I) )
1185
+ else if (UO || BO )
1189
1186
NewVecC = Builder.CreateNAryOp (Opcode, VecCValues);
1190
1187
else
1191
- NewVecC = Builder. CreateIntrinsic (
1192
- VecTy, cast<IntrinsicInst>(I). getIntrinsicID (), VecCValues);
1188
+ NewVecC =
1189
+ Builder. CreateIntrinsic ( VecTy, II-> getIntrinsicID (), VecCValues);
1193
1190
}
1194
1191
Value *Insert = Builder.CreateInsertElement (NewVecC, Scalar, *Index);
1195
1192
replaceValue (I, *Insert);
0 commit comments