@@ -62,7 +62,7 @@ class PlainCFGBuilder {
62
62
// Utility functions.
63
63
void setVPBBPredsFromBB (VPBasicBlock *VPBB, BasicBlock *BB);
64
64
void setRegionPredsFromBB (VPRegionBlock *VPBB, BasicBlock *BB);
65
- void fixPhiNodes ();
65
+ void fixHeaderPhis ();
66
66
VPBasicBlock *getOrCreateVPBB (BasicBlock *BB);
67
67
#ifndef NDEBUG
68
68
bool isExternalDef (Value *Val);
@@ -120,7 +120,7 @@ void PlainCFGBuilder::setRegionPredsFromBB(VPRegionBlock *Region,
120
120
}
121
121
122
122
// Add operands to VPInstructions representing phi nodes from the input IR.
123
- void PlainCFGBuilder::fixPhiNodes () {
123
+ void PlainCFGBuilder::fixHeaderPhis () {
124
124
for (auto *Phi : PhisToFix) {
125
125
assert (IRDef2VPValue.count (Phi) && " Missing VPInstruction for PHINode." );
126
126
VPValue *VPVal = IRDef2VPValue[Phi];
@@ -131,29 +131,17 @@ void PlainCFGBuilder::fixPhiNodes() {
131
131
" Expected VPInstruction with no operands." );
132
132
133
133
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)));
157
145
}
158
146
}
159
147
@@ -329,7 +317,22 @@ void PlainCFGBuilder::createVPInstructionsForVPBB(VPBasicBlock *VPBB,
329
317
// been built.
330
318
NewR = new VPWidenPHIRecipe (Phi, nullptr , Phi->getDebugLoc ());
331
319
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
+ }
333
336
} else {
334
337
// Translate LLVM-IR operands into VPValue operands and set them in the
335
338
// new VPInstruction.
@@ -475,9 +478,9 @@ void PlainCFGBuilder::buildPlainCFG(
475
478
}
476
479
477
480
// 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 ();
481
484
482
485
for (const auto &[IRBB, VPB] : BB2VPBB)
483
486
VPB2IRBB[VPB] = IRBB;
0 commit comments