@@ -122,6 +122,9 @@ VPBasicBlock *PlainCFGBuilder::getOrCreateVPBB(BasicBlock *BB) {
122122 return VPBB;
123123 }
124124
125+ if (!TheLoop->contains (BB))
126+ return Plan.getExitBlock (BB);
127+
125128 // Create new VPBB.
126129 StringRef Name = isHeaderBB (BB, TheLoop) ? " vector.body" : BB->getName ();
127130 LLVM_DEBUG (dbgs () << " Creating VPBasicBlock for " << Name << " \n " );
@@ -155,14 +158,6 @@ bool PlainCFGBuilder::isExternalDef(Value *Val) {
155158 // Instruction definition is in outermost loop PH.
156159 return false ;
157160
158- // Check whether Instruction definition is in a loop exit.
159- SmallVector<BasicBlock *> ExitBlocks;
160- TheLoop->getExitBlocks (ExitBlocks);
161- if (is_contained (ExitBlocks, InstParent)) {
162- // Instruction definition is in outermost loop exit.
163- return false ;
164- }
165-
166161 // Check whether Instruction definition is in loop body.
167162 return !TheLoop->contains (Inst);
168163}
@@ -211,11 +206,8 @@ void PlainCFGBuilder::createVPInstructionsForVPBB(VPBasicBlock *VPBB,
211206 " Instruction shouldn't have been visited." );
212207
213208 if (auto *Br = dyn_cast<BranchInst>(Inst)) {
214- if (TheLoop->getLoopLatch () == BB ||
215- any_of (successors (BB),
216- [this ](BasicBlock *Succ) { return !TheLoop->contains (Succ); }))
209+ if (TheLoop->getLoopLatch () == BB)
217210 continue ;
218-
219211 // Conditional branch instruction are represented using BranchOnCond
220212 // recipes.
221213 if (Br->isConditional ()) {
@@ -305,7 +297,6 @@ void PlainCFGBuilder::buildPlainCFG(
305297 for (BasicBlock *BB : RPO) {
306298 // Create or retrieve the VPBasicBlock for this BB.
307299 VPBasicBlock *VPBB = getOrCreateVPBB (BB);
308- Loop *LoopForBB = LI.getLoopFor (BB);
309300 // Set VPBB predecessors in the same order as they are in the incoming BB.
310301 setVPBBPredsFromBB (VPBB, BB);
311302
@@ -339,24 +330,12 @@ void PlainCFGBuilder::buildPlainCFG(
339330 BasicBlock *IRSucc1 = BI->getSuccessor (1 );
340331 VPBasicBlock *Successor0 = getOrCreateVPBB (IRSucc0);
341332 VPBasicBlock *Successor1 = getOrCreateVPBB (IRSucc1);
342-
343- // Don't connect any blocks outside the current loop except the latch, which
344- // is handled below.
345- if (LoopForBB &&
346- (LoopForBB == TheLoop || BB != LoopForBB->getLoopLatch ())) {
347- if (!LoopForBB->contains (IRSucc0)) {
348- VPBB->setOneSuccessor (Successor1);
349- continue ;
350- }
351- if (!LoopForBB->contains (IRSucc1)) {
352- VPBB->setOneSuccessor (Successor0);
353- continue ;
354- }
355- }
356-
357333 VPBB->setTwoSuccessors (Successor0, Successor1);
358334 }
359335
336+ for (auto *EB : Plan.getExitBlocks ()) {
337+ setVPBBPredsFromBB (EB, EB->getIRBasicBlock ());
338+ }
360339 // 2. The whole CFG has been built at this point so all the input Values must
361340 // have a VPlan counterpart. Fix VPlan header phi by adding their
362341 // corresponding VPlan operands.
@@ -413,10 +392,15 @@ static VPRegionBlock *introduceRegion(VPlan &Plan, VPBasicBlock *PreheaderVPBB,
413392 auto *R = Plan.createVPRegionBlock (HeaderVPBB, LatchVPBB, " " ,
414393 false /* isReplicator*/ );
415394 R->setParent (HeaderVPBB->getParent ());
395+
416396 // All VPBB's reachable shallowly from HeaderVPBB belong to top level loop,
417397 // because VPlan is expected to end at top level latch disconnected above.
418- for (VPBlockBase *VPBB : vp_depth_first_shallow (HeaderVPBB))
419- VPBB->setParent (R);
398+ SmallPtrSet<VPBlockBase *, 2 > ExitBlocks (Plan.getExitBlocks ().begin (),
399+ Plan.getExitBlocks ().end ());
400+ for (VPBlockBase *VPBB : vp_depth_first_shallow (HeaderVPBB)) {
401+ if (!ExitBlocks.contains (VPBB))
402+ VPBB->setParent (R);
403+ }
420404
421405 VPBlockUtils::insertBlockAfter (R, PreheaderVPBB);
422406 if (Succ)
@@ -466,7 +450,11 @@ void VPlanTransforms::introduceRegions(VPlan &Plan, Type *InductionTy,
466450
467451 VPBasicBlock *ScalarPH = Plan.createVPBasicBlock (" scalar.ph" );
468452 VPBlockUtils::connectBlocks (ScalarPH, Plan.getScalarHeader ());
453+ BasicBlock *IRExitBlock = TheLoop->getUniqueLatchExitBlock ();
454+ auto *VPExitBlock = IRExitBlock ? Plan.getExitBlock (IRExitBlock) : nullptr ;
469455 if (!RequiresScalarEpilogueCheck) {
456+ if (VPExitBlock)
457+ VPBlockUtils::disconnectBlocks (MiddleVPBB, VPExitBlock);
470458 VPBlockUtils::connectBlocks (MiddleVPBB, ScalarPH);
471459 for (auto *EB : Plan.getExitBlocks ()) {
472460 for (VPRecipeBase &R : *EB)
@@ -484,10 +472,7 @@ void VPlanTransforms::introduceRegions(VPlan &Plan, Type *InductionTy,
484472 // 2) If we require a scalar epilogue, there is no conditional branch as
485473 // we unconditionally branch to the scalar preheader. Do nothing.
486474 // 3) Otherwise, construct a runtime check.
487- BasicBlock *IRExitBlock = TheLoop->getUniqueLatchExitBlock ();
488- auto *VPExitBlock = Plan.getExitBlock (IRExitBlock);
489475 // The connection order corresponds to the operands of the conditional branch.
490- VPBlockUtils::insertBlockAfter (VPExitBlock, MiddleVPBB);
491476 VPBlockUtils::connectBlocks (MiddleVPBB, ScalarPH);
492477
493478 auto *ScalarLatchTerm = TheLoop->getLoopLatch ()->getTerminator ();
0 commit comments