[llvm] [VPlan] Retain exit conditions and edges in initial VPlan (NFC). (PR #137709)

via llvm-commits llvm-commits at lists.llvm.org
Tue May 6 07:46:57 PDT 2025


================
@@ -511,12 +490,33 @@ void VPlanTransforms::prepareForVectorization(VPlan &Plan, Type *InductionTy,
   VPBlockUtils::insertBlockAfter(VecPreheader, Plan.getEntry());
 
   VPBasicBlock *MiddleVPBB = Plan.createVPBasicBlock("middle.block");
-  VPBlockUtils::connectBlocks(LatchVPB, MiddleVPBB);
-  LatchVPB->swapSuccessors();
+  VPBlockBase *LatchExitVPB = LatchVPB->getNumSuccessors() == 2
+                                  ? LatchVPB->getSuccessors()[0]
+                                  : nullptr;
+  if (LatchExitVPB) {
+    LatchVPB->getSuccessors()[0] = MiddleVPBB;
+    MiddleVPBB->setPredecessors({LatchVPB});
+    MiddleVPBB->setSuccessors({LatchExitVPB});
+    LatchExitVPB->replacePredecessor(LatchVPB, MiddleVPBB);
----------------
ayalz wrote:

```suggestion
  // Canonical LatchVPB has header block as last successor. If it has another
  // successor, the latter is an exit block - insert middle block on its edge.
  // Otherwise, add middle block as another successor retaining header as last.
  if (LatchVPB->getNumSuccessors() == 2) {
    VPBlockBase *LatchExitVPB = LatchVPB->getSuccessors()[0];
    VPBlockUtils::insertOnEdge(LatchVPB, LatchExitVPB, MiddleVPBB);
```
?

https://github.com/llvm/llvm-project/pull/137709


More information about the llvm-commits mailing list