[llvm] [VPlan] Retain exit conditions and edges in initial VPlan (NFC). (PR #137709)
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Wed May 7 01:49:28 PDT 2025
================
@@ -425,21 +409,31 @@ static void createLoopRegion(VPlan &Plan, VPBlockBase *HeaderVPB) {
VPBlockUtils::disconnectBlocks(PreheaderVPBB, HeaderVPB);
VPBlockUtils::disconnectBlocks(LatchVPBB, HeaderVPB);
VPBlockBase *Succ = LatchVPBB->getSingleSuccessor();
- assert(LatchVPBB->getNumSuccessors() <= 1 &&
- "Latch has more than one successor");
- if (Succ)
- VPBlockUtils::disconnectBlocks(LatchVPBB, Succ);
+ assert(Succ && "Latch expected to be left with a single successor");
+
+ // Use a temporary placeholder between LatchVPBB and its successor, to
+ // preserve the original predecessor/successor order of the blocks.
+ auto *PlaceHolder = Plan.createVPBasicBlock("Region place holder");
+ VPBlockUtils::insertOnEdge(LatchVPBB, Succ, PlaceHolder);
+ VPBlockUtils::disconnectBlocks(LatchVPBB, PlaceHolder);
+ VPBlockUtils::connectBlocks(PreheaderVPBB, PlaceHolder);
auto *R = Plan.createVPRegionBlock(HeaderVPB, LatchVPBB, "",
false /*isReplicator*/);
- // All VPBB's reachable shallowly from HeaderVPB belong to top level loop,
- // because VPlan is expected to end at top level latch disconnected above.
+ // All VPBB's reachable shallowly from HeaderVPB belong to the current region,
+ // except the exit blocks reachable via non-latch exiting blocks,
+ SmallPtrSet<VPBlockBase *, 2> ExitBlocks(Plan.getExitBlocks().begin(),
+ Plan.getExitBlocks().end());
for (VPBlockBase *VPBB : vp_depth_first_shallow(HeaderVPB))
- VPBB->setParent(R);
+ if (!ExitBlocks.contains(VPBB))
+ VPBB->setParent(R);
VPBlockUtils::insertBlockAfter(R, PreheaderVPBB);
- if (Succ)
- VPBlockUtils::connectBlocks(R, Succ);
+ VPBlockUtils::insertOnEdge(PlaceHolder, Succ, R);
+
+ // Remove placeholder block.
+ VPBlockUtils::disconnectBlocks(R, PlaceHolder);
+ VPBlockUtils::disconnectBlocks(PlaceHolder, R);
----------------
fhahn wrote:
Actually there's no need to construct a placeholder region, we can simply create an empty main region first and then set entry/exiting after adjusting the CFG. Updated, thanks!
https://github.com/llvm/llvm-project/pull/137709
More information about the llvm-commits
mailing list