[llvm] 30f44c9 - [VPlan] Set values for non-header phis at construction. (NFC)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 22 09:27:27 PST 2025


Author: Florian Hahn
Date: 2025-02-22T17:27:10Z
New Revision: 30f44c96277e9d68926b9d6e27f232da6302c955

URL: https://github.com/llvm/llvm-project/commit/30f44c96277e9d68926b9d6e27f232da6302c955
DIFF: https://github.com/llvm/llvm-project/commit/30f44c96277e9d68926b9d6e27f232da6302c955.diff

LOG: [VPlan] Set values for non-header phis at construction. (NFC)

Update HCFG builder to set the incoming values directly at construction
for non-header phis.

Simplification/clarification as suggested independently in
https://github.com/llvm/llvm-project/pull/126388.

Added: 
    

Modified: 
    llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp b/llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp
index 934202f27eb73..dcf1057b991ee 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp
@@ -62,7 +62,7 @@ class PlainCFGBuilder {
   // Utility functions.
   void setVPBBPredsFromBB(VPBasicBlock *VPBB, BasicBlock *BB);
   void setRegionPredsFromBB(VPRegionBlock *VPBB, BasicBlock *BB);
-  void fixPhiNodes();
+  void fixHeaderPhis();
   VPBasicBlock *getOrCreateVPBB(BasicBlock *BB);
 #ifndef NDEBUG
   bool isExternalDef(Value *Val);
@@ -120,7 +120,7 @@ void PlainCFGBuilder::setRegionPredsFromBB(VPRegionBlock *Region,
 }
 
 // Add operands to VPInstructions representing phi nodes from the input IR.
-void PlainCFGBuilder::fixPhiNodes() {
+void PlainCFGBuilder::fixHeaderPhis() {
   for (auto *Phi : PhisToFix) {
     assert(IRDef2VPValue.count(Phi) && "Missing VPInstruction for PHINode.");
     VPValue *VPVal = IRDef2VPValue[Phi];
@@ -131,29 +131,17 @@ void PlainCFGBuilder::fixPhiNodes() {
            "Expected VPInstruction with no operands.");
 
     Loop *L = LI->getLoopFor(Phi->getParent());
-    if (isHeaderBB(Phi->getParent(), L)) {
-      // For header phis, make sure the incoming value from the loop
-      // predecessor is the first operand of the recipe.
-      assert(Phi->getNumOperands() == 2 &&
-             "header phi must have exactly 2 operands");
-      BasicBlock *LoopPred = L->getLoopPredecessor();
-      VPPhi->addOperand(
-          getOrCreateVPOperand(Phi->getIncomingValueForBlock(LoopPred)));
-      BasicBlock *LoopLatch = L->getLoopLatch();
-      VPPhi->addOperand(
-          getOrCreateVPOperand(Phi->getIncomingValueForBlock(LoopLatch)));
-      continue;
-    }
-
-    // Add operands for VPPhi in the order matching its predecessors in VPlan.
-    DenseMap<const VPBasicBlock *, VPValue *> VPPredToIncomingValue;
-    for (unsigned I = 0; I != Phi->getNumOperands(); ++I) {
-      VPPredToIncomingValue[BB2VPBB[Phi->getIncomingBlock(I)]] =
-          getOrCreateVPOperand(Phi->getIncomingValue(I));
-    }
-    for (VPBlockBase *Pred : VPPhi->getParent()->getPredecessors())
-      VPPhi->addOperand(
-          VPPredToIncomingValue.lookup(Pred->getExitingBasicBlock()));
+    assert(isHeaderBB(Phi->getParent(), L));
+    // For header phis, make sure the incoming value from the loop
+    // predecessor is the first operand of the recipe.
+    assert(Phi->getNumOperands() == 2 &&
+           "header phi must have exactly 2 operands");
+    BasicBlock *LoopPred = L->getLoopPredecessor();
+    VPPhi->addOperand(
+        getOrCreateVPOperand(Phi->getIncomingValueForBlock(LoopPred)));
+    BasicBlock *LoopLatch = L->getLoopLatch();
+    VPPhi->addOperand(
+        getOrCreateVPOperand(Phi->getIncomingValueForBlock(LoopLatch)));
   }
 }
 
@@ -329,7 +317,22 @@ void PlainCFGBuilder::createVPInstructionsForVPBB(VPBasicBlock *VPBB,
       // been built.
       NewR = new VPWidenPHIRecipe(Phi, nullptr, Phi->getDebugLoc());
       VPBB->appendRecipe(NewR);
-      PhisToFix.push_back(Phi);
+      if (isHeaderBB(Phi->getParent(), LI->getLoopFor(Phi->getParent()))) {
+        // Header phis need to be fixed after the VPBB for the latch has been
+        // created.
+        PhisToFix.push_back(Phi);
+      } else {
+        // Add operands for VPPhi in the order matching its predecessors in
+        // VPlan.
+        DenseMap<const VPBasicBlock *, VPValue *> VPPredToIncomingValue;
+        for (unsigned I = 0; I != Phi->getNumOperands(); ++I) {
+          VPPredToIncomingValue[BB2VPBB[Phi->getIncomingBlock(I)]] =
+              getOrCreateVPOperand(Phi->getIncomingValue(I));
+        }
+        for (VPBlockBase *Pred : VPBB->getPredecessors())
+          NewR->addOperand(
+              VPPredToIncomingValue.lookup(Pred->getExitingBasicBlock()));
+      }
     } else {
       // Translate LLVM-IR operands into VPValue operands and set them in the
       // new VPInstruction.
@@ -475,9 +478,9 @@ void PlainCFGBuilder::buildPlainCFG(
   }
 
   // 2. The whole CFG has been built at this point so all the input Values must
-  // have a VPlan couterpart. Fix VPlan phi nodes by adding their corresponding
-  // VPlan operands.
-  fixPhiNodes();
+  // have a VPlan counterpart. Fix VPlan header phi by adding their
+  // corresponding VPlan operands.
+  fixHeaderPhis();
 
   for (const auto &[IRBB, VPB] : BB2VPBB)
     VPB2IRBB[VPB] = IRBB;


        


More information about the llvm-commits mailing list