@@ -62,7 +62,7 @@ class PlainCFGBuilder {
6262 // Utility functions.
6363 void setVPBBPredsFromBB (VPBasicBlock *VPBB, BasicBlock *BB);
6464 void setRegionPredsFromBB (VPRegionBlock *VPBB, BasicBlock *BB);
65- void fixPhiNodes ();
65+ void fixHeaderPhis ();
6666 VPBasicBlock *getOrCreateVPBB (BasicBlock *BB);
6767#ifndef NDEBUG
6868 bool isExternalDef (Value *Val);
@@ -120,7 +120,7 @@ void PlainCFGBuilder::setRegionPredsFromBB(VPRegionBlock *Region,
120120}
121121
122122// Add operands to VPInstructions representing phi nodes from the input IR.
123- void PlainCFGBuilder::fixPhiNodes () {
123+ void PlainCFGBuilder::fixHeaderPhis () {
124124 for (auto *Phi : PhisToFix) {
125125 assert (IRDef2VPValue.count (Phi) && " Missing VPInstruction for PHINode." );
126126 VPValue *VPVal = IRDef2VPValue[Phi];
@@ -131,29 +131,17 @@ void PlainCFGBuilder::fixPhiNodes() {
131131 " Expected VPInstruction with no operands." );
132132
133133 Loop *L = LI->getLoopFor (Phi->getParent ());
134- if (isHeaderBB (Phi->getParent (), L)) {
135- // For header phis, make sure the incoming value from the loop
136- // predecessor is the first operand of the recipe.
137- assert (Phi->getNumOperands () == 2 &&
138- " header phi must have exactly 2 operands" );
139- BasicBlock *LoopPred = L->getLoopPredecessor ();
140- VPPhi->addOperand (
141- getOrCreateVPOperand (Phi->getIncomingValueForBlock (LoopPred)));
142- BasicBlock *LoopLatch = L->getLoopLatch ();
143- VPPhi->addOperand (
144- getOrCreateVPOperand (Phi->getIncomingValueForBlock (LoopLatch)));
145- continue ;
146- }
147-
148- // Add operands for VPPhi in the order matching its predecessors in VPlan.
149- DenseMap<const VPBasicBlock *, VPValue *> VPPredToIncomingValue;
150- for (unsigned I = 0 ; I != Phi->getNumOperands (); ++I) {
151- VPPredToIncomingValue[BB2VPBB[Phi->getIncomingBlock (I)]] =
152- getOrCreateVPOperand (Phi->getIncomingValue (I));
153- }
154- for (VPBlockBase *Pred : VPPhi->getParent ()->getPredecessors ())
155- VPPhi->addOperand (
156- VPPredToIncomingValue.lookup (Pred->getExitingBasicBlock ()));
134+ assert (isHeaderBB (Phi->getParent (), L));
135+ // For header phis, make sure the incoming value from the loop
136+ // predecessor is the first operand of the recipe.
137+ assert (Phi->getNumOperands () == 2 &&
138+ " header phi must have exactly 2 operands" );
139+ BasicBlock *LoopPred = L->getLoopPredecessor ();
140+ VPPhi->addOperand (
141+ getOrCreateVPOperand (Phi->getIncomingValueForBlock (LoopPred)));
142+ BasicBlock *LoopLatch = L->getLoopLatch ();
143+ VPPhi->addOperand (
144+ getOrCreateVPOperand (Phi->getIncomingValueForBlock (LoopLatch)));
157145 }
158146}
159147
@@ -329,7 +317,22 @@ void PlainCFGBuilder::createVPInstructionsForVPBB(VPBasicBlock *VPBB,
329317 // been built.
330318 NewR = new VPWidenPHIRecipe (Phi, nullptr , Phi->getDebugLoc ());
331319 VPBB->appendRecipe (NewR);
332- PhisToFix.push_back (Phi);
320+ if (isHeaderBB (Phi->getParent (), LI->getLoopFor (Phi->getParent ()))) {
321+ // Header phis need to be fixed after the VPBB for the latch has been
322+ // created.
323+ PhisToFix.push_back (Phi);
324+ } else {
325+ // Add operands for VPPhi in the order matching its predecessors in
326+ // VPlan.
327+ DenseMap<const VPBasicBlock *, VPValue *> VPPredToIncomingValue;
328+ for (unsigned I = 0 ; I != Phi->getNumOperands (); ++I) {
329+ VPPredToIncomingValue[BB2VPBB[Phi->getIncomingBlock (I)]] =
330+ getOrCreateVPOperand (Phi->getIncomingValue (I));
331+ }
332+ for (VPBlockBase *Pred : VPBB->getPredecessors ())
333+ NewR->addOperand (
334+ VPPredToIncomingValue.lookup (Pred->getExitingBasicBlock ()));
335+ }
333336 } else {
334337 // Translate LLVM-IR operands into VPValue operands and set them in the
335338 // new VPInstruction.
@@ -475,9 +478,9 @@ void PlainCFGBuilder::buildPlainCFG(
475478 }
476479
477480 // 2. The whole CFG has been built at this point so all the input Values must
478- // have a VPlan couterpart . Fix VPlan phi nodes by adding their corresponding
479- // VPlan operands.
480- fixPhiNodes ();
481+ // have a VPlan counterpart . Fix VPlan header phi by adding their
482+ // corresponding VPlan operands.
483+ fixHeaderPhis ();
481484
482485 for (const auto &[IRBB, VPB] : BB2VPBB)
483486 VPB2IRBB[VPB] = IRBB;
0 commit comments