Skip to content

Commit cc81801

Browse files
committed
!fixup don't special case header/latch predecessor order.
1 parent 2a500dd commit cc81801

File tree

4 files changed

+29
-30
lines changed

4 files changed

+29
-30
lines changed

llvm/lib/Transforms/Vectorize/VPlan.cpp

+14-9
Original file line numberDiff line numberDiff line change
@@ -648,15 +648,20 @@ bool VPBasicBlock::isExiting() const {
648648
return getParent() && getParent()->getExitingBasicBlock() == this;
649649
}
650650

651-
bool VPBasicBlock::isHeader(const VPDominatorTree &VPDT) const {
652-
if (getNumPredecessors() != 2)
653-
return false;
654-
VPBlockBase *LatchVPBB = getPredecessors()[1];
655-
if (!VPDT.dominates(this, LatchVPBB))
656-
return false;
657-
assert(VPDT.dominates(getPredecessors()[0], this) &&
658-
"preheader must dominate header");
659-
return true;
651+
std::optional<std::pair<VPBasicBlock *, VPBasicBlock *>>
652+
VPBasicBlock::isHeader(const VPDominatorTree &VPDT) const {
653+
ArrayRef<VPBlockBase *> Preds = getPredecessors();
654+
if (Preds.size() != 2)
655+
return std::nullopt;
656+
657+
for (unsigned Idx : {0, 1}) {
658+
auto *PreheaderVPBB = cast<VPBasicBlock>(Preds[Idx]);
659+
auto *LatchVPBB = cast<VPBasicBlock>(Preds[1 - Idx]);
660+
if (VPDT.dominates(PreheaderVPBB, this) && VPDT.dominates(this, LatchVPBB))
661+
return {std::make_pair(PreheaderVPBB, LatchVPBB)};
662+
}
663+
664+
return std::nullopt;
660665
}
661666

662667
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)

llvm/lib/Transforms/Vectorize/VPlan.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -3255,10 +3255,13 @@ class VPBasicBlock : public VPBlockBase {
32553255
/// Returns true if the block is exiting it's parent region.
32563256
bool isExiting() const;
32573257

3258-
/// Returns true if the block is a loop header block in the plain CFG; that
3259-
/// is, it has exactly 2 predecessors (preheader and latch), where the block
3260-
/// dominates the latch and the preheader dominates the block.
3261-
bool isHeader(const VPDominatorTree &VPDT) const;
3258+
/// Checks if the block is a loop header block in the plain CFG; that is, it
3259+
/// has exactly 2 predecessors (preheader and latch), where the block
3260+
/// dominates the latch and the preheader dominates the block. If it is a
3261+
/// header block, returns a pair with the corresponding preheader and latch
3262+
/// blocks. Otherwise return std::nullopt.
3263+
std::optional<std::pair<VPBasicBlock *, VPBasicBlock *>>
3264+
isHeader(const VPDominatorTree &VPDT) const;
32623265

32633266
/// Clone the current block and it's recipes, without updating the operands of
32643267
/// the cloned recipes.

llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ using namespace llvm;
2323

2424
/// Create and return a new VPRegionBlock for loop starting at \p HeaderVPBB and
2525
/// return it.
26-
static VPRegionBlock *introduceRegion(VPlan &Plan, VPBlockBase *HeaderVPBB) {
27-
VPBlockBase *PreheaderVPBB = HeaderVPBB->getPredecessors()[0];
28-
VPBlockBase *LatchVPBB = HeaderVPBB->getPredecessors()[1];
26+
static VPRegionBlock *introduceRegion(VPlan &Plan, VPBasicBlock *PreheaderVPBB,
27+
VPBasicBlock *HeaderVPBB,
28+
VPBasicBlock *LatchVPBB) {
2929
VPBlockUtils::disconnectBlocks(PreheaderVPBB, HeaderVPBB);
3030
VPBlockUtils::disconnectBlocks(LatchVPBB, HeaderVPBB);
3131
VPBlockBase *Succ = LatchVPBB->getSingleSuccessor();
@@ -56,9 +56,11 @@ void VPlanTransforms::introduceRegions(VPlan &Plan, Type *InductionTy,
5656
VPDT.recalculate(Plan);
5757
for (VPBasicBlock *HeaderVPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
5858
vp_depth_first_shallow(Plan.getEntry()))) {
59-
if (!HeaderVPBB->isHeader(VPDT))
59+
auto Res = HeaderVPBB->isHeader(VPDT);
60+
if (!Res)
6061
continue;
61-
introduceRegion(Plan, HeaderVPBB);
62+
const auto &[PreheaderVPBB, LatchVPBB] = *Res;
63+
introduceRegion(Plan, PreheaderVPBB, HeaderVPBB, LatchVPBB);
6264
}
6365

6466
VPRegionBlock *TopRegion = Plan.getVectorLoopRegion();

llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp

+1-12
Original file line numberDiff line numberDiff line change
@@ -313,22 +313,11 @@ void PlainCFGBuilder::buildPlainCFG(
313313
VPBasicBlock *VPBB = getOrCreateVPBB(BB);
314314
Loop *LoopForBB = LI->getLoopFor(BB);
315315
// Set VPBB predecessors in the same order as they are in the incoming BB.
316-
if (!isHeaderBB(BB, LoopForBB)) {
317-
setVPBBPredsFromBB(VPBB, BB);
318-
} else {
319-
VPBB->setPredecessors({getOrCreateVPBB(LoopForBB->getLoopPredecessor()),
320-
getOrCreateVPBB(LoopForBB->getLoopLatch())});
321-
}
316+
setVPBBPredsFromBB(VPBB, BB);
322317

323318
// Create VPInstructions for BB.
324319
createVPInstructionsForVPBB(VPBB, BB);
325320

326-
if (BB == TheLoop->getLoopLatch()) {
327-
VPBasicBlock *HeaderVPBB = getOrCreateVPBB(LoopForBB->getHeader());
328-
VPBB->setOneSuccessor(HeaderVPBB);
329-
continue;
330-
}
331-
332321
// Set VPBB successors. We create empty VPBBs for successors if they don't
333322
// exist already. Recipes will be created when the successor is visited
334323
// during the RPO traversal.

0 commit comments

Comments
 (0)