Skip to content

Commit 0384f6c

Browse files
artagnonlukel97
andauthored
[VPlanPatternMatch] Introduce match functor (NFC) (#159521)
Follow up on 7fb3a91 ([PatternMatch] Introduce match functor) to introduce the VPlanPatternMatch version of the match functor to shorten some idioms. Co-authored-by: Luke Lau <[email protected]>
1 parent 8552760 commit 0384f6c

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6901,11 +6901,12 @@ static bool planContainsAdditionalSimplifications(VPlan &Plan,
69016901
// Unused FOR splices are removed by VPlan transforms, so the VPlan-based
69026902
// cost model won't cost it whilst the legacy will.
69036903
if (auto *FOR = dyn_cast<VPFirstOrderRecurrencePHIRecipe>(&R)) {
6904-
if (none_of(FOR->users(), [](VPUser *U) {
6905-
auto *VPI = dyn_cast<VPInstruction>(U);
6906-
return VPI && VPI->getOpcode() ==
6907-
VPInstruction::FirstOrderRecurrenceSplice;
6908-
}))
6904+
using namespace VPlanPatternMatch;
6905+
if (none_of(
6906+
FOR->users(),
6907+
match_fn(
6908+
m_VPInstruction<VPInstruction::FirstOrderRecurrenceSplice>(
6909+
m_VPValue(), m_VPValue()))))
69096910
return true;
69106911
}
69116912
// The VPlan-based cost model is more accurate for partial reduction and

llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ template <typename Pattern> bool match(VPUser *U, const Pattern &P) {
2929
return R && match(R, P);
3030
}
3131

32+
template <typename Val, typename Pattern> struct VPMatchFunctor {
33+
const Pattern &P;
34+
VPMatchFunctor(const Pattern &P) : P(P) {}
35+
bool operator()(Val *V) const { return match(V, P); }
36+
};
37+
38+
/// A match functor that can be used as a UnaryPredicate in functional
39+
/// algorithms like all_of.
40+
template <typename Val = VPUser, typename Pattern>
41+
VPMatchFunctor<Val, Pattern> match_fn(const Pattern &P) {
42+
return P;
43+
}
44+
3245
template <typename Class> struct class_match {
3346
template <typename ITy> bool match(ITy *V) const { return isa<Class>(V); }
3447
};

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,11 +1149,10 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
11491149
CmpPredicate Pred;
11501150
if (match(A, m_Cmp(Pred, m_VPValue(), m_VPValue()))) {
11511151
auto *Cmp = cast<VPRecipeWithIRFlags>(A);
1152-
if (all_of(Cmp->users(), [&Cmp](VPUser *U) {
1153-
return match(U, m_CombineOr(m_Not(m_Specific(Cmp)),
1154-
m_Select(m_Specific(Cmp), m_VPValue(),
1155-
m_VPValue())));
1156-
})) {
1152+
if (all_of(Cmp->users(),
1153+
match_fn(m_CombineOr(
1154+
m_Not(m_Specific(Cmp)),
1155+
m_Select(m_Specific(Cmp), m_VPValue(), m_VPValue()))))) {
11571156
Cmp->setPredicate(CmpInst::getInversePredicate(Pred));
11581157
for (VPUser *U : to_vector(Cmp->users())) {
11591158
auto *R = cast<VPSingleDefRecipe>(U);

llvm/lib/Transforms/Vectorize/VPlanVerifier.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,11 @@ bool VPlanVerifier::verifyEVLRecipe(const VPInstruction &EVL) const {
198198
}
199199
// EVLIVIncrement is only used by EVLIV & BranchOnCount.
200200
// Having more than two users is unexpected.
201+
using namespace llvm::VPlanPatternMatch;
201202
if ((I->getNumUsers() != 1) &&
202-
(I->getNumUsers() != 2 || none_of(I->users(), [&I](VPUser *U) {
203-
using namespace llvm::VPlanPatternMatch;
204-
return match(U, m_BranchOnCount(m_Specific(I), m_VPValue()));
205-
}))) {
203+
(I->getNumUsers() != 2 ||
204+
none_of(I->users(), match_fn(m_BranchOnCount(m_Specific(I),
205+
m_VPValue()))))) {
206206
errs() << "EVL is used in VPInstruction with multiple users\n";
207207
return false;
208208
}

0 commit comments

Comments
 (0)