Skip to content

Commit c008647

Browse files
committed
[VPlan] Introduce isHeaderMask helper (NFCI).
Split off from #92555 and slightly generalized to more precisely check for a header mask. Use it to replace manual checks in collectHeaderMasks.
1 parent 936bc9b commit c008647

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,3 +1459,23 @@ VPValue *vputils::getOrCreateVPValueForSCEVExpr(VPlan &Plan, const SCEV *Expr,
14591459
Plan.addSCEVExpansion(Expr, Expanded);
14601460
return Expanded;
14611461
}
1462+
1463+
bool vputils::isHeaderMask(VPValue *V, VPlan &Plan) {
1464+
if (isa<VPActiveLaneMaskPHIRecipe>(V))
1465+
return true;
1466+
1467+
auto IsWideCanonicalIV = [](VPValue *A) {
1468+
return isa<VPWidenCanonicalIVRecipe>(A) ||
1469+
(isa<VPWidenIntOrFpInductionRecipe>(A) &&
1470+
cast<VPWidenIntOrFpInductionRecipe>(A)->isCanonical());
1471+
};
1472+
1473+
VPValue *A, *B;
1474+
if (match(V, m_ActiveLaneMask(m_VPValue(A), m_VPValue(B))))
1475+
return B == Plan.getTripCount() &&
1476+
(match(A, m_ScalarIVSteps(m_CanonicalIV(), m_SpecificInt(1))) ||
1477+
IsWideCanonicalIV(A));
1478+
1479+
return match(V, m_Binary<Instruction::ICmp>(m_VPValue(A), m_VPValue(B))) &&
1480+
IsWideCanonicalIV(A) && B == Plan.getOrCreateBackedgeTakenCount();
1481+
}

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3665,6 +3665,10 @@ inline bool isUniformAfterVectorization(VPValue *VPV) {
36653665
return VPI->isVectorToScalar();
36663666
return false;
36673667
}
3668+
3669+
/// Return true if \p V is a header mask in \p Plan.
3670+
bool isHeaderMask(VPValue *V, VPlan &Plan);
3671+
36683672
} // end namespace vputils
36693673

36703674
} // end namespace llvm

llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,46 @@ inline BinaryVPInstruction_match<Op0_t, Op1_t, VPInstruction::LogicalAnd>
303303
m_LogicalAnd(const Op0_t &Op0, const Op1_t &Op1) {
304304
return m_VPInstruction<VPInstruction::LogicalAnd, Op0_t, Op1_t>(Op0, Op1);
305305
}
306+
307+
struct VPCanonicalIVPHI_match {
308+
bool match(const VPValue *V) {
309+
auto *DefR = V->getDefiningRecipe();
310+
return DefR && match(DefR);
311+
}
312+
313+
bool match(const VPRecipeBase *R) { return isa<VPCanonicalIVPHIRecipe>(R); }
314+
};
315+
316+
inline VPCanonicalIVPHI_match m_CanonicalIV() {
317+
return VPCanonicalIVPHI_match();
318+
}
319+
320+
template <typename Op0_t, typename Op1_t> struct VPScalarIVSteps_match {
321+
Op0_t Op0;
322+
Op1_t Op1;
323+
324+
VPScalarIVSteps_match(Op0_t Op0, Op1_t Op1) : Op0(Op0), Op1(Op1) {}
325+
326+
bool match(const VPValue *V) {
327+
auto *DefR = V->getDefiningRecipe();
328+
return DefR && match(DefR);
329+
}
330+
331+
bool match(const VPRecipeBase *R) {
332+
if (!isa<VPScalarIVStepsRecipe>(R))
333+
return false;
334+
assert(R->getNumOperands() == 2 &&
335+
"VPScalarIVSteps must have exactly 2 operands");
336+
return Op0.match(R->getOperand(0)) && Op1.match(R->getOperand(1));
337+
}
338+
};
339+
340+
template <typename Op0_t, typename Op1_t>
341+
inline VPScalarIVSteps_match<Op0_t, Op1_t> m_ScalarIVSteps(const Op0_t &Op0,
342+
const Op1_t &Op1) {
343+
return VPScalarIVSteps_match<Op0_t, Op1_t>(Op0, Op1);
344+
}
345+
306346
} // namespace VPlanPatternMatch
307347
} // namespace llvm
308348

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,13 +1334,10 @@ static SmallVector<VPValue *> collectAllHeaderMasks(VPlan &Plan) {
13341334
// Walk users of wide canonical IVs and collect to all compares of the form
13351335
// (ICMP_ULE, WideCanonicalIV, backedge-taken-count).
13361336
SmallVector<VPValue *> HeaderMasks;
1337-
VPValue *BTC = Plan.getOrCreateBackedgeTakenCount();
13381337
for (auto *Wide : WideCanonicalIVs) {
13391338
for (VPUser *U : SmallVector<VPUser *>(Wide->users())) {
13401339
auto *HeaderMask = dyn_cast<VPInstruction>(U);
1341-
if (!HeaderMask || HeaderMask->getOpcode() != Instruction::ICmp ||
1342-
HeaderMask->getPredicate() != CmpInst::ICMP_ULE ||
1343-
HeaderMask->getOperand(1) != BTC)
1340+
if (!HeaderMask || !vputils::isHeaderMask(HeaderMask, Plan))
13441341
continue;
13451342

13461343
assert(HeaderMask->getOperand(0) == Wide &&

0 commit comments

Comments
 (0)