[llvm] [VPlan] Invert condition if needed when creating inner regions. (PR #132292)

via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 25 14:03:14 PDT 2025


================
@@ -420,6 +420,17 @@ static void createLoopRegion(VPlan &Plan, VPBlockBase *HeaderVPB) {
   auto *PreheaderVPBB = HeaderVPB->getPredecessors()[0];
   auto *LatchVPBB = HeaderVPB->getPredecessors()[1];
 
+  // We are canonicalizing the successors of the latch when introducing the
+  // region. We will exit the region of the latch condition is true; invert the
+  // original condition if the original CFG branches to the header on true.
+  if (!LatchVPBB->getSingleSuccessor() &&
+      LatchVPBB->getSuccessors()[0] == HeaderVPB) {
+    auto *Term = cast<VPBasicBlock>(LatchVPBB)->getTerminator();
+    auto *Not = new VPInstruction(VPInstruction::Not, {Term->getOperand(0)});
+    Not->insertBefore(Term);
+    Term->setOperand(0, Not);
+  }
+
----------------
ayalz wrote:

Better to perform this canonicalization in a separate canonicalizeLatch(), analogous to canonical[ize]Header()?
E.g., something like
```
    if (canonicalizeHeader(HeaderVPB, VPDT)) {
      canonicalizeLatchOfHeader(HeaderVPB);
      createLoopRegion(Plan, HeaderVPB);
    }
```
or possibly fused into `canonicalizeHeaderAndLatch(HeaderVPB, VPDT)`?

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


More information about the llvm-commits mailing list