Skip to content

Commit 2931aa7

Browse files
committed
Simplify matching
1 parent 4702c1d commit 2931aa7

File tree

2 files changed

+36
-35
lines changed

2 files changed

+36
-35
lines changed

llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,12 @@ m_Broadcast(const Op0_t &Op0) {
313313
return m_VPInstruction<VPInstruction::Broadcast>(Op0);
314314
}
315315

316+
template <typename Op0_t>
317+
inline VPInstruction_match<VPInstruction::ExplicitVectorLength, Op0_t>
318+
m_EVL(const Op0_t &Op0) {
319+
return m_VPInstruction<VPInstruction::ExplicitVectorLength>(Op0);
320+
}
321+
316322
template <typename Op0_t, typename Op1_t>
317323
inline VPInstruction_match<VPInstruction::ActiveLaneMask, Op0_t, Op1_t>
318324
m_ActiveLaneMask(const Op0_t &Op0, const Op1_t &Op1) {
@@ -370,6 +376,12 @@ m_c_Binary(const Op0_t &Op0, const Op1_t &Op1) {
370376
return AllRecipe_commutative_match<Opcode, Op0_t, Op1_t>(Op0, Op1);
371377
}
372378

379+
template <typename Op0_t, typename Op1_t>
380+
inline AllRecipe_commutative_match<Instruction::Add, Op0_t, Op1_t>
381+
m_c_Add(const Op0_t &Op0, const Op1_t &Op1) {
382+
return m_c_Binary<Instruction::Add, Op0_t, Op1_t>(Op0, Op1);
383+
}
384+
373385
template <typename Op0_t, typename Op1_t>
374386
inline AllRecipe_match<Instruction::Sub, Op0_t, Op1_t> m_Sub(const Op0_t &Op0,
375387
const Op1_t &Op1) {

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -741,8 +741,7 @@ static VPWidenInductionRecipe *getOptimizableIVOf(VPValue *VPV) {
741741
VPValue *IVStep = WideIV->getStepValue();
742742
switch (ID.getInductionOpcode()) {
743743
case Instruction::Add:
744-
return match(VPV, m_c_Binary<Instruction::Add>(m_Specific(WideIV),
745-
m_Specific(IVStep)));
744+
return match(VPV, m_c_Add(m_Specific(WideIV), m_Specific(IVStep)));
746745
case Instruction::FAdd:
747746
return match(VPV, m_c_Binary<Instruction::FAdd>(m_Specific(WideIV),
748747
m_Specific(IVStep)));
@@ -2231,9 +2230,8 @@ static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) {
22312230

22322231
assert(all_of(Plan.getVFxUF().users(),
22332232
[&Plan](VPUser *U) {
2234-
return match(U, m_c_Binary<Instruction::Add>(
2235-
m_Specific(Plan.getCanonicalIV()),
2236-
m_Specific(&Plan.getVFxUF()))) ||
2233+
return match(U, m_c_Add(m_Specific(Plan.getCanonicalIV()),
2234+
m_Specific(&Plan.getVFxUF()))) ||
22372235
isa<VPWidenPointerInductionRecipe>(U);
22382236
}) &&
22392237
"Only users of VFxUF should be VPWidenPointerInductionRecipe and the "
@@ -2446,45 +2444,37 @@ void VPlanTransforms::canonicalizeEVLLoops(VPlan &Plan) {
24462444
// Find EVL loop entries by locating VPEVLBasedIVPHIRecipe.
24472445
// There should be only one EVL PHI in the entire plan.
24482446
VPEVLBasedIVPHIRecipe *EVLPhi = nullptr;
2449-
VPValue *AVLNext = nullptr;
24502447

24512448
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
24522449
vp_depth_first_shallow(Plan.getEntry())))
2453-
for (VPRecipeBase &R : VPBB->phis()) {
2454-
auto *PhiR = dyn_cast<VPSingleDefRecipe>(&R);
2455-
if (!PhiR)
2456-
continue;
2457-
VPValue *Backedge;
2458-
if (auto *EVL = dyn_cast<VPEVLBasedIVPHIRecipe>(PhiR)) {
2450+
for (VPRecipeBase &R : VPBB->phis())
2451+
if (auto *PhiR = dyn_cast<VPEVLBasedIVPHIRecipe>(&R)) {
24592452
assert(!EVLPhi && "Found multiple EVL PHIs. Only one expected");
2460-
EVLPhi = EVL;
2461-
continue;
2462-
}
2463-
if (match(PhiR,
2464-
m_VPInstruction<Instruction::PHI>(
2465-
m_Specific(Plan.getTripCount()), m_VPValue(Backedge))) &&
2466-
match(Backedge,
2467-
m_VPInstruction<Instruction::Sub>(
2468-
m_Specific(PhiR),
2469-
m_ZExtOrSelf(
2470-
m_VPInstruction<VPInstruction::ExplicitVectorLength>(
2471-
m_CombineOr(
2472-
m_Specific(PhiR),
2473-
// The AVL may be capped to a safe distance.
2474-
m_Select(m_VPValue(), m_Specific(PhiR),
2475-
m_VPValue()))))))) {
2476-
AVLNext = Backedge;
2453+
EVLPhi = PhiR;
24772454
}
2478-
}
24792455

24802456
// Early return if no EVL PHI is found.
24812457
if (!EVLPhi)
24822458
return;
24832459

2484-
assert(AVLNext && "Didn't find AVL backedge?");
2485-
24862460
VPBasicBlock *HeaderVPBB = EVLPhi->getParent();
24872461
VPValue *EVLIncrement = EVLPhi->getBackedgeValue();
2462+
VPValue *AVL, *EVL;
2463+
if (!match(EVLIncrement,
2464+
m_c_Add(m_ZExtOrSelf(m_VPValue(EVL)), m_Specific(EVLPhi))) ||
2465+
!match(EVL, m_EVL(m_VPValue(AVL))))
2466+
llvm_unreachable("Didn't find EVL?");
2467+
2468+
// The AVL may be capped to a safe distance.
2469+
VPValue *SafeAVL;
2470+
if (match(AVL, m_Select(m_VPValue(), m_VPValue(SafeAVL), m_VPValue())))
2471+
AVL = SafeAVL;
2472+
2473+
VPValue *AVLNext;
2474+
if (!match(AVL, m_VPInstruction<Instruction::PHI>(
2475+
m_Specific(Plan.getTripCount()), m_VPValue(AVLNext))) ||
2476+
!match(AVLNext, m_Sub(m_Specific(AVL), m_ZExtOrSelf(m_Specific(EVL)))))
2477+
llvm_unreachable("Didn't find AVL backedge?");
24882478

24892479
// Convert EVLPhi to concrete recipe.
24902480
auto *ScalarR =
@@ -2496,9 +2486,8 @@ void VPlanTransforms::canonicalizeEVLLoops(VPlan &Plan) {
24962486
// Replace CanonicalIVInc with EVL-PHI increment.
24972487
auto *CanonicalIV = cast<VPPhi>(&*HeaderVPBB->begin());
24982488
VPValue *Backedge = CanonicalIV->getIncomingValue(1);
2499-
assert(match(Backedge,
2500-
m_c_Binary<Instruction::Add>(m_Specific(CanonicalIV),
2501-
m_Specific(&Plan.getVFxUF()))) &&
2489+
assert(match(Backedge, m_c_Add(m_Specific(CanonicalIV),
2490+
m_Specific(&Plan.getVFxUF()))) &&
25022491
"Unexpected canonical iv");
25032492
Backedge->replaceAllUsesWith(EVLIncrement);
25042493

0 commit comments

Comments
 (0)