Skip to content

Commit e99fed6

Browse files
committed
Perform all dyn_casts at the start
1 parent 5f57fb6 commit e99fed6

File tree

1 file changed

+33
-36
lines changed

1 file changed

+33
-36
lines changed

llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,22 +1022,26 @@ bool VectorCombine::scalarizeVPIntrinsic(Instruction &I) {
10221022
/// inserted scalar operand and convert to scalar op/cmp/intrinsic followed
10231023
/// by insertelement.
10241024
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)
10261030
return false;
10271031

10281032
// TODO: Allow intrinsics with different argument types
10291033
// 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;
10351039

10361040
// Do not convert the vector condition of a vector select into a scalar
10371041
// condition. That may cause problems for codegen because of differences in
10381042
// boolean formats and register-file transfers.
10391043
// TODO: Can we account for that in the cost model?
1040-
if (isa<CmpInst>(I))
1044+
if (CI)
10411045
for (User *U : I.users())
10421046
if (match(U, m_Select(m_Specific(&I), m_Value(), m_Value())))
10431047
return false;
@@ -1048,8 +1052,7 @@ bool VectorCombine::scalarizeOpOrCmp(Instruction &I) {
10481052
SmallVector<Value *> ScalarOps;
10491053
std::optional<uint64_t> Index;
10501054

1051-
auto Ops = isa<IntrinsicInst>(I) ? cast<IntrinsicInst>(I).args()
1052-
: I.operand_values();
1055+
auto Ops = II ? II->args() : I.operand_values();
10531056
for (Value *Op : Ops) {
10541057
Constant *VecC;
10551058
Value *V;
@@ -1089,17 +1092,16 @@ bool VectorCombine::scalarizeOpOrCmp(Instruction &I) {
10891092

10901093
unsigned Opcode = I.getOpcode();
10911094
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();
10941097
ScalarOpCost = TTI.getCmpSelInstrCost(
10951098
Opcode, ScalarTy, CmpInst::makeCmpResultType(ScalarTy), Pred, CostKind);
10961099
VectorOpCost = TTI.getCmpSelInstrCost(
10971100
Opcode, VecTy, CmpInst::makeCmpResultType(VecTy), Pred, CostKind);
1098-
} else if (isa<UnaryOperator, BinaryOperator>(I)) {
1101+
} else if (UO || BO) {
10991102
ScalarOpCost = TTI.getArithmeticInstrCost(Opcode, ScalarTy, CostKind);
11001103
VectorOpCost = TTI.getArithmeticInstrCost(Opcode, VecTy, CostKind);
11011104
} else {
1102-
auto *II = cast<IntrinsicInst>(&I);
11031105
IntrinsicCostAttributes ScalarICA(
11041106
II->getIntrinsicID(), ScalarTy,
11051107
SmallVector<Type *>(II->arg_size(), ScalarTy));
@@ -1113,20 +1115,16 @@ bool VectorCombine::scalarizeOpOrCmp(Instruction &I) {
11131115
// Fold the vector constants in the original vectors into a new base vector to
11141116
// get more accurate cost modelling.
11151117
Value *NewVecC = nullptr;
1116-
if (auto *CI = dyn_cast<CmpInst>(&I))
1118+
if (CI)
11171119
NewVecC = ConstantFoldCompareInstOperands(CI->getPredicate(), VecCs[0],
11181120
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);
11301128

11311129
// Get cost estimate for the insert element. This cost will factor into
11321130
// both sequences.
@@ -1149,11 +1147,11 @@ bool VectorCombine::scalarizeOpOrCmp(Instruction &I) {
11491147

11501148
// vec_op (inselt VecC0, V0, Index), (inselt VecC1, V1, Index) -->
11511149
// inselt NewVecC, (scalar_op V0, V1), Index
1152-
if (isa<CmpInst>(I))
1150+
if (CI)
11531151
++NumScalarCmp;
1154-
else if (isa<UnaryOperator, BinaryOperator>(I))
1152+
else if (UO || BO)
11551153
++NumScalarOps;
1156-
else if (isa<IntrinsicInst>(I))
1154+
else
11571155
++NumScalarIntrinsic;
11581156

11591157
// For constant cases, extract the scalar element, this should constant fold.
@@ -1163,13 +1161,12 @@ bool VectorCombine::scalarizeOpOrCmp(Instruction &I) {
11631161
cast<Constant>(VecC), Builder.getInt64(*Index));
11641162

11651163
Value *Scalar;
1166-
if (auto *CI = dyn_cast<CmpInst>(&I))
1164+
if (CI)
11671165
Scalar = Builder.CreateCmp(CI->getPredicate(), ScalarOps[0], ScalarOps[1]);
1168-
else if (isa<UnaryOperator, BinaryOperator>(I))
1166+
else if (UO || BO)
11691167
Scalar = Builder.CreateNAryOp(Opcode, ScalarOps);
11701168
else
1171-
Scalar = Builder.CreateIntrinsic(
1172-
ScalarTy, cast<IntrinsicInst>(I).getIntrinsicID(), ScalarOps);
1169+
Scalar = Builder.CreateIntrinsic(ScalarTy, II->getIntrinsicID(), ScalarOps);
11731170

11741171
Scalar->setName(I.getName() + ".scalar");
11751172

@@ -1183,13 +1180,13 @@ bool VectorCombine::scalarizeOpOrCmp(Instruction &I) {
11831180
SmallVector<Value *> VecCValues;
11841181
VecCValues.reserve(VecCs.size());
11851182
append_range(VecCValues, VecCs);
1186-
if (auto *CI = dyn_cast<CmpInst>(&I))
1183+
if (CI)
11871184
NewVecC = Builder.CreateCmp(CI->getPredicate(), VecCs[0], VecCs[1]);
1188-
else if (isa<UnaryOperator, BinaryOperator>(I))
1185+
else if (UO || BO)
11891186
NewVecC = Builder.CreateNAryOp(Opcode, VecCValues);
11901187
else
1191-
NewVecC = Builder.CreateIntrinsic(
1192-
VecTy, cast<IntrinsicInst>(I).getIntrinsicID(), VecCValues);
1188+
NewVecC =
1189+
Builder.CreateIntrinsic(VecTy, II->getIntrinsicID(), VecCValues);
11931190
}
11941191
Value *Insert = Builder.CreateInsertElement(NewVecC, Scalar, *Index);
11951192
replaceValue(I, *Insert);

0 commit comments

Comments
 (0)