Skip to content

Commit 2b0ab05

Browse files
authored
[SLP][NFC] Simplify type checks with isa predicates (#87182)
For more context on isa predicates, see: #83753.
1 parent d2b63ed commit 2b0ab05

File tree

1 file changed

+33
-62
lines changed

1 file changed

+33
-62
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 33 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,7 @@ static SmallBitVector isUndefVector(const Value *V,
458458
/// ShuffleVectorInst/getShuffleCost?
459459
static std::optional<TargetTransformInfo::ShuffleKind>
460460
isFixedVectorShuffle(ArrayRef<Value *> VL, SmallVectorImpl<int> &Mask) {
461-
const auto *It =
462-
find_if(VL, [](Value *V) { return isa<ExtractElementInst>(V); });
461+
const auto *It = find_if(VL, IsaPred<ExtractElementInst>);
463462
if (It == VL.end())
464463
return std::nullopt;
465464
auto *EI0 = cast<ExtractElementInst>(*It);
@@ -4695,12 +4694,8 @@ BoUpSLP::getReorderingData(const TreeEntry &TE, bool TopToBottom) {
46954694
// TODO: add analysis of other gather nodes with extractelement
46964695
// instructions and other values/instructions, not only undefs.
46974696
if ((TE.getOpcode() == Instruction::ExtractElement ||
4698-
(all_of(TE.Scalars,
4699-
[](Value *V) {
4700-
return isa<UndefValue, ExtractElementInst>(V);
4701-
}) &&
4702-
any_of(TE.Scalars,
4703-
[](Value *V) { return isa<ExtractElementInst>(V); }))) &&
4697+
(all_of(TE.Scalars, IsaPred<UndefValue, ExtractElementInst>) &&
4698+
any_of(TE.Scalars, IsaPred<ExtractElementInst>))) &&
47044699
all_of(TE.Scalars, [](Value *V) {
47054700
auto *EE = dyn_cast<ExtractElementInst>(V);
47064701
return !EE || isa<FixedVectorType>(EE->getVectorOperandType());
@@ -4721,7 +4716,7 @@ BoUpSLP::getReorderingData(const TreeEntry &TE, bool TopToBottom) {
47214716
// might be transformed.
47224717
int Sz = TE.Scalars.size();
47234718
if (isSplat(TE.Scalars) && !allConstant(TE.Scalars) &&
4724-
count_if(TE.Scalars, UndefValue::classof) == Sz - 1) {
4719+
count_if(TE.Scalars, IsaPred<UndefValue>) == Sz - 1) {
47254720
const auto *It =
47264721
find_if(TE.Scalars, [](Value *V) { return !isConstant(V); });
47274722
if (It == TE.Scalars.begin())
@@ -6345,11 +6340,10 @@ void BoUpSLP::buildTree_rec(ArrayRef<Value *> VL, unsigned Depth,
63456340
UserTreeIdx.UserTE->State == TreeEntry::ScatterVectorize &&
63466341
!(S.getOpcode() && allSameBlock(VL))) {
63476342
assert(S.OpValue->getType()->isPointerTy() &&
6348-
count_if(VL, [](Value *V) { return isa<GetElementPtrInst>(V); }) >=
6349-
2 &&
6343+
count_if(VL, IsaPred<GetElementPtrInst>) >= 2 &&
63506344
"Expected pointers only.");
63516345
// Reset S to make it GetElementPtr kind of node.
6352-
const auto *It = find_if(VL, [](Value *V) { return isa<GetElementPtrInst>(V); });
6346+
const auto *It = find_if(VL, IsaPred<GetElementPtrInst>);
63536347
assert(It != VL.end() && "Expected at least one GEP.");
63546348
S = getSameOpcode(*It, *TLI);
63556349
}
@@ -6893,17 +6887,12 @@ unsigned BoUpSLP::canMapToVector(Type *T) const {
68936887
bool BoUpSLP::canReuseExtract(ArrayRef<Value *> VL, Value *OpValue,
68946888
SmallVectorImpl<unsigned> &CurrentOrder,
68956889
bool ResizeAllowed) const {
6896-
const auto *It = find_if(VL, [](Value *V) {
6897-
return isa<ExtractElementInst, ExtractValueInst>(V);
6898-
});
6890+
const auto *It = find_if(VL, IsaPred<ExtractElementInst, ExtractValueInst>);
68996891
assert(It != VL.end() && "Expected at least one extract instruction.");
69006892
auto *E0 = cast<Instruction>(*It);
6901-
assert(all_of(VL,
6902-
[](Value *V) {
6903-
return isa<UndefValue, ExtractElementInst, ExtractValueInst>(
6904-
V);
6905-
}) &&
6906-
"Invalid opcode");
6893+
assert(
6894+
all_of(VL, IsaPred<UndefValue, ExtractElementInst, ExtractValueInst>) &&
6895+
"Invalid opcode");
69076896
// Check if all of the extracts come from the same vector and from the
69086897
// correct offset.
69096898
Value *Vec = E0->getOperand(0);
@@ -7575,7 +7564,7 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
75757564
}
75767565

75777566
InstructionCost getBuildVectorCost(ArrayRef<Value *> VL, Value *Root) {
7578-
if ((!Root && allConstant(VL)) || all_of(VL, UndefValue::classof))
7567+
if ((!Root && allConstant(VL)) || all_of(VL, IsaPred<UndefValue>))
75797568
return TTI::TCC_Free;
75807569
auto *VecTy = FixedVectorType::get(VL.front()->getType(), VL.size());
75817570
InstructionCost GatherCost = 0;
@@ -7743,21 +7732,20 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
77437732
} else if (!Root && isSplat(VL)) {
77447733
// Found the broadcasting of the single scalar, calculate the cost as
77457734
// the broadcast.
7746-
const auto *It =
7747-
find_if(VL, [](Value *V) { return !isa<UndefValue>(V); });
7735+
const auto *It = find_if_not(VL, IsaPred<UndefValue>);
77487736
assert(It != VL.end() && "Expected at least one non-undef value.");
77497737
// Add broadcast for non-identity shuffle only.
77507738
bool NeedShuffle =
77517739
count(VL, *It) > 1 &&
7752-
(VL.front() != *It || !all_of(VL.drop_front(), UndefValue::classof));
7740+
(VL.front() != *It || !all_of(VL.drop_front(), IsaPred<UndefValue>));
77537741
if (!NeedShuffle)
77547742
return TTI.getVectorInstrCost(Instruction::InsertElement, VecTy,
77557743
CostKind, std::distance(VL.begin(), It),
77567744
PoisonValue::get(VecTy), *It);
77577745

77587746
SmallVector<int> ShuffleMask(VL.size(), PoisonMaskElem);
77597747
transform(VL, ShuffleMask.begin(), [](Value *V) {
7760-
return isa<PoisonValue>(V) ? PoisonMaskElem : 0;
7748+
return isa<PoisonValue>(V) ? PoisonMaskElem : 0;
77617749
});
77627750
InstructionCost InsertCost = TTI.getVectorInstrCost(
77637751
Instruction::InsertElement, VecTy, CostKind, 0,
@@ -7768,7 +7756,7 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
77687756
/*SubTp=*/nullptr, /*Args=*/*It);
77697757
}
77707758
return GatherCost +
7771-
(all_of(Gathers, UndefValue::classof)
7759+
(all_of(Gathers, IsaPred<UndefValue>)
77727760
? TTI::TCC_Free
77737761
: R.getGatherCost(Gathers, !Root && VL.equals(Gathers)));
77747762
};
@@ -8178,9 +8166,8 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
81788166
// Take credit for instruction that will become dead.
81798167
if (EE->hasOneUse() || !PrevNodeFound) {
81808168
Instruction *Ext = EE->user_back();
8181-
if (isa<SExtInst, ZExtInst>(Ext) && all_of(Ext->users(), [](User *U) {
8182-
return isa<GetElementPtrInst>(U);
8183-
})) {
8169+
if (isa<SExtInst, ZExtInst>(Ext) &&
8170+
all_of(Ext->users(), IsaPred<GetElementPtrInst>)) {
81848171
// Use getExtractWithExtendCost() to calculate the cost of
81858172
// extractelement/ext pair.
81868173
Cost -=
@@ -8645,8 +8632,7 @@ BoUpSLP::getEntryCost(const TreeEntry *E, ArrayRef<Value *> VectorizedVals,
86458632
if (I->hasOneUse()) {
86468633
Instruction *Ext = I->user_back();
86478634
if ((isa<SExtInst>(Ext) || isa<ZExtInst>(Ext)) &&
8648-
all_of(Ext->users(),
8649-
[](User *U) { return isa<GetElementPtrInst>(U); })) {
8635+
all_of(Ext->users(), IsaPred<GetElementPtrInst>)) {
86508636
// Use getExtractWithExtendCost() to calculate the cost of
86518637
// extractelement/ext pair.
86528638
InstructionCost Cost = TTI->getExtractWithExtendCost(
@@ -9130,10 +9116,7 @@ bool BoUpSLP::isFullyVectorizableTinyTree(bool ForReduction) const {
91309116
(allConstant(TE->Scalars) || isSplat(TE->Scalars) ||
91319117
TE->Scalars.size() < Limit ||
91329118
((TE->getOpcode() == Instruction::ExtractElement ||
9133-
all_of(TE->Scalars,
9134-
[](Value *V) {
9135-
return isa<ExtractElementInst, UndefValue>(V);
9136-
})) &&
9119+
all_of(TE->Scalars, IsaPred<ExtractElementInst, UndefValue>)) &&
91379120
isFixedVectorShuffle(TE->Scalars, Mask)) ||
91389121
(TE->State == TreeEntry::NeedToGather &&
91399122
TE->getOpcode() == Instruction::Load && !TE->isAltShuffle()));
@@ -9254,9 +9237,7 @@ bool BoUpSLP::isTreeTinyAndNotFullyVectorizable(bool ForReduction) const {
92549237
all_of(VectorizableTree, [&](const std::unique_ptr<TreeEntry> &TE) {
92559238
return (TE->State == TreeEntry::NeedToGather &&
92569239
TE->getOpcode() != Instruction::ExtractElement &&
9257-
count_if(TE->Scalars,
9258-
[](Value *V) { return isa<ExtractElementInst>(V); }) <=
9259-
Limit) ||
9240+
count_if(TE->Scalars, IsaPred<ExtractElementInst>) <= Limit) ||
92609241
TE->getOpcode() == Instruction::PHI;
92619242
}))
92629243
return true;
@@ -9285,9 +9266,7 @@ bool BoUpSLP::isTreeTinyAndNotFullyVectorizable(bool ForReduction) const {
92859266
return isa<ExtractElementInst, UndefValue>(V) ||
92869267
(IsAllowedSingleBVNode &&
92879268
!V->hasNUsesOrMore(UsesLimit) &&
9288-
any_of(V->users(), [](User *U) {
9289-
return isa<InsertElementInst>(U);
9290-
}));
9269+
any_of(V->users(), IsaPred<InsertElementInst>));
92919270
});
92929271
}))
92939272
return false;
@@ -10284,7 +10263,7 @@ BoUpSLP::isGatherShuffledSingleRegisterEntry(
1028410263
}
1028510264
}
1028610265

10287-
bool IsSplatOrUndefs = isSplat(VL) || all_of(VL, UndefValue::classof);
10266+
bool IsSplatOrUndefs = isSplat(VL) || all_of(VL, IsaPred<UndefValue>);
1028810267
// Checks if the 2 PHIs are compatible in terms of high possibility to be
1028910268
// vectorized.
1029010269
auto AreCompatiblePHIs = [&](Value *V, Value *V1) {
@@ -11261,8 +11240,7 @@ Value *BoUpSLP::vectorizeOperand(TreeEntry *E, unsigned NodeIdx,
1126111240
InstructionsState S = getSameOpcode(VL, *TLI);
1126211241
// Special processing for GEPs bundle, which may include non-gep values.
1126311242
if (!S.getOpcode() && VL.front()->getType()->isPointerTy()) {
11264-
const auto *It =
11265-
find_if(VL, [](Value *V) { return isa<GetElementPtrInst>(V); });
11243+
const auto *It = find_if(VL, IsaPred<GetElementPtrInst>);
1126611244
if (It != VL.end())
1126711245
S = getSameOpcode(*It, *TLI);
1126811246
}
@@ -11432,7 +11410,7 @@ ResTy BoUpSLP::processBuildVector(const TreeEntry *E, Args &...Params) {
1143211410
unsigned NumParts = TTI->getNumberOfParts(VecTy);
1143311411
if (NumParts == 0 || NumParts >= GatheredScalars.size())
1143411412
NumParts = 1;
11435-
if (!all_of(GatheredScalars, UndefValue::classof)) {
11413+
if (!all_of(GatheredScalars, IsaPred<UndefValue>)) {
1143611414
// Check for gathered extracts.
1143711415
bool Resized = false;
1143811416
ExtractShuffles =
@@ -11757,7 +11735,7 @@ ResTy BoUpSLP::processBuildVector(const TreeEntry *E, Args &...Params) {
1175711735
GatheredScalars[I] = PoisonValue::get(ScalarTy);
1175811736
}
1175911737
// Generate constants for final shuffle and build a mask for them.
11760-
if (!all_of(GatheredScalars, PoisonValue::classof)) {
11738+
if (!all_of(GatheredScalars, IsaPred<PoisonValue>)) {
1176111739
SmallVector<int> BVMask(GatheredScalars.size(), PoisonMaskElem);
1176211740
TryPackScalars(GatheredScalars, BVMask, /*IsRootPoison=*/true);
1176311741
Value *BV = ShuffleBuilder.gather(GatheredScalars, BVMask.size());
@@ -14509,7 +14487,7 @@ void BoUpSLP::computeMinimumValueSizes() {
1450914487
return SIt != DemotedConsts.end() &&
1451014488
is_contained(SIt->getSecond(), Idx);
1451114489
}) ||
14512-
all_of(CTE->Scalars, Constant::classof))
14490+
all_of(CTE->Scalars, IsaPred<Constant>))
1451314491
MinBWs.try_emplace(CTE, MaxBitWidth, IsSigned);
1451414492
}
1451514493
}
@@ -15257,12 +15235,10 @@ class HorizontalReduction {
1525715235
static Value *createOp(IRBuilderBase &Builder, RecurKind RdxKind, Value *LHS,
1525815236
Value *RHS, const Twine &Name,
1525915237
const ReductionOpsListType &ReductionOps) {
15260-
bool UseSelect =
15261-
ReductionOps.size() == 2 ||
15262-
// Logical or/and.
15263-
(ReductionOps.size() == 1 && any_of(ReductionOps.front(), [](Value *V) {
15264-
return isa<SelectInst>(V);
15265-
}));
15238+
bool UseSelect = ReductionOps.size() == 2 ||
15239+
// Logical or/and.
15240+
(ReductionOps.size() == 1 &&
15241+
any_of(ReductionOps.front(), IsaPred<SelectInst>));
1526615242
assert((!UseSelect || ReductionOps.size() != 2 ||
1526715243
isa<SelectInst>(ReductionOps[1][0])) &&
1526815244
"Expected cmp + select pairs for reduction");
@@ -15501,7 +15477,7 @@ class HorizontalReduction {
1550115477
!hasRequiredNumberOfUses(IsCmpSelMinMax, EdgeInst) ||
1550215478
!isVectorizable(RdxKind, EdgeInst) ||
1550315479
(R.isAnalyzedReductionRoot(EdgeInst) &&
15504-
all_of(EdgeInst->operands(), Constant::classof))) {
15480+
all_of(EdgeInst->operands(), IsaPred<Constant>))) {
1550515481
PossibleReducedVals.push_back(EdgeVal);
1550615482
continue;
1550715483
}
@@ -16857,9 +16833,7 @@ bool SLPVectorizerPass::vectorizeInsertElementInst(InsertElementInst *IEI,
1685716833
SmallVector<Value *, 16> BuildVectorOpds;
1685816834
SmallVector<int> Mask;
1685916835
if (!findBuildAggregate(IEI, TTI, BuildVectorOpds, BuildVectorInsts) ||
16860-
(llvm::all_of(
16861-
BuildVectorOpds,
16862-
[](Value *V) { return isa<ExtractElementInst, UndefValue>(V); }) &&
16836+
(llvm::all_of(BuildVectorOpds, IsaPred<ExtractElementInst, UndefValue>) &&
1686316837
isFixedVectorShuffle(BuildVectorOpds, Mask)))
1686416838
return false;
1686516839

@@ -17080,10 +17054,7 @@ bool SLPVectorizerPass::vectorizeCmpInsts(iterator_range<ItT> CmpInsts,
1708017054

1708117055
bool SLPVectorizerPass::vectorizeInserts(InstSetVector &Instructions,
1708217056
BasicBlock *BB, BoUpSLP &R) {
17083-
assert(all_of(Instructions,
17084-
[](auto *I) {
17085-
return isa<InsertElementInst, InsertValueInst>(I);
17086-
}) &&
17057+
assert(all_of(Instructions, IsaPred<InsertElementInst, InsertValueInst>) &&
1708717058
"This function only accepts Insert instructions");
1708817059
bool OpsChanged = false;
1708917060
SmallVector<WeakTrackingVH> PostponedInsts;

0 commit comments

Comments
 (0)