@@ -113,6 +113,9 @@ VPBasicBlock *PlainCFGBuilder::getOrCreateVPBB(BasicBlock *BB) {
113
113
return VPBB;
114
114
}
115
115
116
+ if (!TheLoop->contains (BB))
117
+ return Plan->getExitBlock (BB);
118
+
116
119
// Create new VPBB.
117
120
StringRef Name = BB->getName ();
118
121
LLVM_DEBUG (dbgs () << " Creating VPBasicBlock for " << Name << " \n " );
@@ -146,14 +149,6 @@ bool PlainCFGBuilder::isExternalDef(Value *Val) {
146
149
// Instruction definition is in outermost loop PH.
147
150
return false ;
148
151
149
- // Check whether Instruction definition is in a loop exit.
150
- SmallVector<BasicBlock *> ExitBlocks;
151
- TheLoop->getExitBlocks (ExitBlocks);
152
- if (is_contained (ExitBlocks, InstParent)) {
153
- // Instruction definition is in outermost loop exit.
154
- return false ;
155
- }
156
-
157
152
// Check whether Instruction definition is in loop body.
158
153
return !TheLoop->contains (Inst);
159
154
}
@@ -202,11 +197,6 @@ void PlainCFGBuilder::createVPInstructionsForVPBB(VPBasicBlock *VPBB,
202
197
" Instruction shouldn't have been visited." );
203
198
204
199
if (auto *Br = dyn_cast<BranchInst>(Inst)) {
205
- if (TheLoop->getLoopLatch () == BB ||
206
- any_of (successors (BB),
207
- [this ](BasicBlock *Succ) { return !TheLoop->contains (Succ); }))
208
- continue ;
209
-
210
200
// Conditional branch instruction are represented using BranchOnCond
211
201
// recipes.
212
202
if (Br->isConditional ()) {
@@ -296,7 +286,6 @@ std::unique_ptr<VPlan> PlainCFGBuilder::buildPlainCFG(
296
286
for (BasicBlock *BB : RPO) {
297
287
// Create or retrieve the VPBasicBlock for this BB.
298
288
VPBasicBlock *VPBB = getOrCreateVPBB (BB);
299
- Loop *LoopForBB = LI->getLoopFor (BB);
300
289
// Set VPBB predecessors in the same order as they are in the incoming BB.
301
290
setVPBBPredsFromBB (VPBB, BB);
302
291
@@ -327,24 +316,12 @@ std::unique_ptr<VPlan> PlainCFGBuilder::buildPlainCFG(
327
316
BasicBlock *IRSucc1 = BI->getSuccessor (1 );
328
317
VPBasicBlock *Successor0 = getOrCreateVPBB (IRSucc0);
329
318
VPBasicBlock *Successor1 = getOrCreateVPBB (IRSucc1);
330
-
331
- // Don't connect any blocks outside the current loop except the latches for
332
- // inner loops.
333
- // TODO: Also connect exit blocks during initial VPlan construction.
334
- if (LoopForBB == TheLoop || BB != LoopForBB->getLoopLatch ()) {
335
- if (!LoopForBB->contains (IRSucc0)) {
336
- VPBB->setOneSuccessor (Successor1);
337
- continue ;
338
- }
339
- if (!LoopForBB->contains (IRSucc1)) {
340
- VPBB->setOneSuccessor (Successor0);
341
- continue ;
342
- }
343
- }
344
-
345
319
VPBB->setTwoSuccessors (Successor0, Successor1);
346
320
}
347
321
322
+ for (auto *EB : Plan->getExitBlocks ())
323
+ setVPBBPredsFromBB (EB, EB->getIRBasicBlock ());
324
+
348
325
// 2. The whole CFG has been built at this point so all the input Values must
349
326
// have a VPlan counterpart. Fix VPlan header phi by adding their
350
327
// corresponding VPlan operands.
@@ -447,19 +424,21 @@ static void createLoopRegion(VPlan &Plan, VPBlockBase *HeaderVPB) {
447
424
VPBlockBase *Succ = LatchVPBB->getSingleSuccessor ();
448
425
assert (LatchVPBB->getNumSuccessors () <= 1 &&
449
426
" Latch has more than one successor" );
450
- if (Succ)
451
- VPBlockUtils::disconnectBlocks (LatchVPBB, Succ);
427
+ LatchVPBB->removeSuccessor (Succ);
452
428
453
429
auto *R = Plan.createVPRegionBlock (HeaderVPB, LatchVPBB, " " ,
454
430
false /* isReplicator*/ );
455
431
// All VPBB's reachable shallowly from HeaderVPB belong to top level loop,
456
432
// because VPlan is expected to end at top level latch disconnected above.
433
+ SmallPtrSet<VPBlockBase *, 2 > ExitBlocks (Plan.getExitBlocks ().begin (),
434
+ Plan.getExitBlocks ().end ());
457
435
for (VPBlockBase *VPBB : vp_depth_first_shallow (HeaderVPB))
458
- VPBB->setParent (R);
436
+ if (!ExitBlocks.contains (VPBB))
437
+ VPBB->setParent (R);
459
438
460
439
VPBlockUtils::insertBlockAfter (R, PreheaderVPBB);
461
- if (Succ)
462
- VPBlockUtils::connectBlocks (R, Succ );
440
+ R-> setOneSuccessor (Succ);
441
+ Succ-> replacePredecessor (LatchVPBB, R );
463
442
}
464
443
465
444
// Add the necessary canonical IV and branch recipes required to control the
@@ -511,12 +490,33 @@ void VPlanTransforms::prepareForVectorization(VPlan &Plan, Type *InductionTy,
511
490
VPBlockUtils::insertBlockAfter (VecPreheader, Plan.getEntry ());
512
491
513
492
VPBasicBlock *MiddleVPBB = Plan.createVPBasicBlock (" middle.block" );
514
- VPBlockUtils::connectBlocks (LatchVPB, MiddleVPBB);
515
- LatchVPB->swapSuccessors ();
493
+ VPBlockBase *LatchExitVPB = LatchVPB->getNumSuccessors () == 2
494
+ ? LatchVPB->getSuccessors ()[0 ]
495
+ : nullptr ;
496
+ if (LatchExitVPB) {
497
+ LatchVPB->getSuccessors ()[0 ] = MiddleVPBB;
498
+ MiddleVPBB->setPredecessors ({LatchVPB});
499
+ MiddleVPBB->setSuccessors ({LatchExitVPB});
500
+ LatchExitVPB->replacePredecessor (LatchVPB, MiddleVPBB);
501
+ } else {
502
+ VPBlockUtils::connectBlocks (LatchVPB, MiddleVPBB);
503
+ LatchVPB->swapSuccessors ();
504
+ }
516
505
517
506
addCanonicalIVRecipes (Plan, cast<VPBasicBlock>(HeaderVPB),
518
507
cast<VPBasicBlock>(LatchVPB), InductionTy, IVDL);
519
508
509
+ // Disconnect all edges between exit blocks other than from the latch.
510
+ // TODO: Uncountable exit blocks should be handled here.
511
+ for (VPBlockBase *EB : to_vector (Plan.getExitBlocks ())) {
512
+ for (VPBlockBase *Pred : to_vector (EB->getPredecessors ())) {
513
+ if (Pred == MiddleVPBB)
514
+ continue ;
515
+ cast<VPBasicBlock>(Pred)->getTerminator ()->eraseFromParent ();
516
+ VPBlockUtils::disconnectBlocks (Pred, EB);
517
+ }
518
+ }
519
+
520
520
// Create SCEV and VPValue for the trip count.
521
521
// We use the symbolic max backedge-taken-count, which works also when
522
522
// vectorizing loops with uncountable early exits.
@@ -541,8 +541,9 @@ void VPlanTransforms::prepareForVectorization(VPlan &Plan, Type *InductionTy,
541
541
// Thus if tail is to be folded, we know we don't need to run the
542
542
// remainder and we can set the condition to true.
543
543
// 3) Otherwise, construct a runtime check.
544
-
545
544
if (!RequiresScalarEpilogueCheck) {
545
+ if (LatchExitVPB)
546
+ VPBlockUtils::disconnectBlocks (MiddleVPBB, LatchExitVPB);
546
547
VPBlockUtils::connectBlocks (MiddleVPBB, ScalarPH);
547
548
// The exit blocks are unreachable, remove their recipes to make sure no
548
549
// users remain that may pessimize transforms.
@@ -554,9 +555,6 @@ void VPlanTransforms::prepareForVectorization(VPlan &Plan, Type *InductionTy,
554
555
}
555
556
556
557
// The connection order corresponds to the operands of the conditional branch.
557
- BasicBlock *IRExitBlock = TheLoop->getUniqueLatchExitBlock ();
558
- auto *VPExitBlock = Plan.getExitBlock (IRExitBlock);
559
- VPBlockUtils::connectBlocks (MiddleVPBB, VPExitBlock);
560
558
VPBlockUtils::connectBlocks (MiddleVPBB, ScalarPH);
561
559
562
560
auto *ScalarLatchTerm = TheLoop->getLoopLatch ()->getTerminator ();
0 commit comments