@@ -212,14 +212,24 @@ bool VPBlockUtils::isHeader(const VPBlockBase *VPB,
212212 auto *VPBB = dyn_cast<VPBasicBlock>(VPB);
213213 if (!VPBB)
214214 return false ;
215+
216+ // If VPBB is in a region R, VPBB is a loop header if R is a loop region with
217+ // VPBB as its entry, i.e., free of predecessors.
215218 if (auto *R = VPBB->getParent ())
216219 return !R->isReplicator () && VPBB->getNumPredecessors () == 0 ;
217220
218- assert (!VPB->getParent () && " checking blocks in regions not implemented yet" );
221+ // A header dominates its second predecessor (the latch), with the other
222+ // predecessor being the preheader
219223 return VPB->getPredecessors ().size () == 2 &&
220224 VPDT.dominates (VPB, VPB->getPredecessors ()[1 ]);
221225}
222226
227+ bool VPBlockUtils::isLatch (const VPBlockBase *VPB,
228+ const VPDominatorTree &VPDT) {
229+ return VPB->getNumSuccessors () == 2 &&
230+ VPBlockUtils::isHeader (VPB->getSuccessors ()[1 ], VPDT);
231+ }
232+
223233VPBasicBlock::iterator VPBasicBlock::getFirstNonPhi () {
224234 iterator It = begin ();
225235 while (It != end () && It->isPhi ())
@@ -435,16 +445,17 @@ void VPBasicBlock::connectToPredecessors(VPTransformState &State) {
435445
436446 auto Preds = to_vector (getHierarchicalPredecessors ());
437447 if (VPBlockUtils::isHeader (this , State.VPDT )) {
438- // There's no block yet for the latch, don't try to connect it yet .
448+ // There's no block for the latch yet, connect to the preheader only .
439449 Preds = {Preds[0 ]};
440450 }
441451
442452 // Hook up the new basic block to its predecessors.
443453 for (VPBlockBase *PredVPBlock : Preds) {
444454 VPBasicBlock *PredVPBB = PredVPBlock->getExitingBasicBlock ();
445455 auto &PredVPSuccessors = PredVPBB->getHierarchicalSuccessors ();
456+ assert (CFG.VPBB2IRBB .contains (PredVPBB) &&
457+ " Predecessor basic-block not found building successor." );
446458 BasicBlock *PredBB = CFG.VPBB2IRBB .lookup (PredVPBB);
447- assert (PredBB && " Predecessor basic-block not found building successor." );
448459 auto *PredBBTerminator = PredBB->getTerminator ();
449460 LLVM_DEBUG (dbgs () << " LV: draw edge from" << PredBB->getName () << ' \n ' );
450461
@@ -548,8 +559,7 @@ void VPBasicBlock::execute(VPTransformState *State) {
548559 executeRecipes (State, NewBB);
549560
550561 // If this block is a latch, update CurrentParentLoop.
551- if (getNumSuccessors () == 2 &&
552- VPBlockUtils::isHeader (getSuccessors ()[1 ], State->VPDT ))
562+ if (VPBlockUtils::isLatch (this , State->VPDT ))
553563 State->CurrentParentLoop = State->CurrentParentLoop ->getParentLoop ();
554564}
555565
@@ -988,8 +998,7 @@ void VPlan::execute(VPTransformState *State) {
988998 for (VPBlockBase *Block : RPOT)
989999 Block->execute (State);
9901000
991- VPBasicBlock *Header =
992- vputils::getTopLevelVectorLoopHeader (*this , State->VPDT );
1001+ VPBasicBlock *Header = vputils::getFirstLoopHeader (*this , State->VPDT );
9931002 if (!Header)
9941003 return ;
9951004
0 commit comments