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