@@ -2357,9 +2357,9 @@ EpilogueVectorizerMainLoop::createIterationCountCheck(ElementCount VF,
23572357// / VPBB are moved to the end of the newly created VPIRBasicBlock. VPBB must
23582358// / have a single predecessor, which is rewired to the new VPIRBasicBlock. All
23592359// / successors of VPBB, if any, are rewired to the new VPIRBasicBlock.
2360- static VPIRBasicBlock *replaceVPBBWithIRVPBB (VPBasicBlock *VPBB,
2360+ static VPIRBasicBlock *replaceVPBBWithIRVPBB (VPlan &Plan, VPBasicBlock *VPBB,
23612361 BasicBlock *IRBB) {
2362- VPIRBasicBlock *IRVPBB = VPBB-> getPlan ()-> createVPIRBasicBlock (IRBB);
2362+ VPIRBasicBlock *IRVPBB = Plan. createVPIRBasicBlock (IRBB);
23632363 auto IP = IRVPBB->begin ();
23642364 for (auto &R : make_early_inc_range (VPBB->phis ()))
23652365 R.moveBefore (*IRVPBB, IP);
@@ -2571,6 +2571,9 @@ void InnerLoopVectorizer::fixVectorizedLoop(VPTransformState &State) {
25712571 // Remove redundant induction instructions.
25722572 cse (HeaderBB);
25732573
2574+ if (Plan.getScalarPreheader ()->getNumPredecessors () == 0 )
2575+ return ;
2576+
25742577 // Set/update profile weights for the vector and remainder loops as original
25752578 // loop iterations are now distributed among them. Note that original loop
25762579 // becomes the scalar remainder loop after vectorization.
@@ -7226,6 +7229,12 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
72267229 VPlanTransforms::optimizeForVFAndUF (BestVPlan, BestVF, BestUF, PSE);
72277230 VPlanTransforms::simplifyRecipes (BestVPlan);
72287231 VPlanTransforms::removeBranchOnConst (BestVPlan);
7232+ if (BestVPlan.getEntry ()->getSingleSuccessor () ==
7233+ BestVPlan.getScalarPreheader ()) {
7234+ // TODO: Should not even try to vectorize.
7235+ return DenseMap<const SCEV *, Value *>();
7236+ }
7237+
72297238 VPlanTransforms::narrowInterleaveGroups (
72307239 BestVPlan, BestVF,
72317240 TTI.getRegisterBitWidth (TargetTransformInfo::RGK_FixedWidthVector));
@@ -7268,7 +7277,7 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
72687277 BasicBlock *EntryBB =
72697278 cast<VPIRBasicBlock>(BestVPlan.getEntry ())->getIRBasicBlock ();
72707279 State.CFG .PrevBB = ILV.createVectorizedLoopSkeleton ();
7271- replaceVPBBWithIRVPBB (BestVPlan.getScalarPreheader (),
7280+ replaceVPBBWithIRVPBB (BestVPlan, BestVPlan .getScalarPreheader (),
72727281 State.CFG .PrevBB ->getSingleSuccessor ());
72737282 VPlanTransforms::removeDeadRecipes (BestVPlan);
72747283
@@ -7351,8 +7360,9 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
73517360 } else {
73527361 // Keep all loop hints from the original loop on the vector loop (we'll
73537362 // replace the vectorizer-specific hints below).
7354- if (MDNode *LID = OrigLoop->getLoopID ())
7355- L->setLoopID (LID);
7363+ if (BestVPlan.getScalarPreheader ()->getNumPredecessors () > 0 )
7364+ if (MDNode *LID = OrigLoop->getLoopID ())
7365+ L->setLoopID (LID);
73567366
73577367 LoopVectorizeHints Hints (L, true , *ORE);
73587368 Hints.setAlreadyVectorized ();
@@ -7383,6 +7393,16 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
73837393 addRuntimeUnrollDisableMetaData (L);
73847394 }
73857395
7396+ if (BestVPlan.getScalarPreheader ()->getNumPredecessors () == 0 ) {
7397+ // If the original loop became unreachable, we need to delete it.
7398+ auto Blocks = OrigLoop->getBlocksVector ();
7399+ Blocks.push_back (cast<VPIRBasicBlock>(BestVPlan.getScalarPreheader ())
7400+ ->getIRBasicBlock ());
7401+ for (auto *BB : Blocks)
7402+ LI->removeBlock (BB);
7403+ LI->erase (OrigLoop);
7404+ }
7405+
73867406 // 3. Fix the vectorized code: take care of header phi's, live-outs,
73877407 // predication, updating analyses.
73887408 ILV.fixVectorizedLoop (State);
@@ -7460,7 +7480,8 @@ EpilogueVectorizerMainLoop::emitIterationCountCheck(BasicBlock *Bypass,
74607480 // generated here dominates the vector epilog iter check.
74617481 EPI.TripCount = Count;
74627482 } else {
7463- VectorPHVPBB = replaceVPBBWithIRVPBB (VectorPHVPBB, LoopVectorPreHeader);
7483+ VectorPHVPBB =
7484+ replaceVPBBWithIRVPBB (Plan, VectorPHVPBB, LoopVectorPreHeader);
74647485 }
74657486
74667487 BranchInst &BI =
@@ -7493,7 +7514,7 @@ BasicBlock *EpilogueVectorizerEpilogueLoop::createVectorizedLoopSkeleton() {
74937514 BasicBlock *VecEpilogueIterationCountCheck =
74947515 SplitBlock (LoopVectorPreHeader, LoopVectorPreHeader->begin (), DT, LI,
74957516 nullptr , " vec.epilog.iter.check" , true );
7496- VectorPHVPBB = replaceVPBBWithIRVPBB (VectorPHVPBB, LoopVectorPreHeader);
7517+ VectorPHVPBB = replaceVPBBWithIRVPBB (Plan, VectorPHVPBB, LoopVectorPreHeader);
74977518
74987519 emitMinimumVectorEpilogueIterCountCheck (LoopScalarPreHeader,
74997520 VecEpilogueIterationCountCheck);
@@ -10213,11 +10234,22 @@ bool LoopVectorizePass::processLoop(Loop *L) {
1021310234 LLVM_DEBUG (dbgs () << " LV: Interleave Count is " << IC << ' \n ' );
1021410235 }
1021510236
10237+ if (ORE->allowExtraAnalysis (LV_NAME))
10238+ checkMixedPrecision (L, ORE);
10239+
1021610240 bool DisableRuntimeUnroll = false ;
1021710241 MDNode *OrigLoopID = L->getLoopID ();
10242+ bool LoopRemoved = false ;
1021810243 {
1021910244 using namespace ore ;
1022010245 if (!VectorizeLoop) {
10246+ ORE->emit ([&]() {
10247+ return OptimizationRemark (LV_NAME, " Interleaved" , L->getStartLoc (),
10248+ L->getHeader ())
10249+ << " interleaved loop (interleaved count: "
10250+ << NV (" InterleaveCount" , IC) << " )" ;
10251+ });
10252+
1022110253 assert (IC > 1 && " interleave count should not be 1 or 0" );
1022210254 // If we decided that it is not legal to vectorize the loop, then
1022310255 // interleave it.
@@ -10234,14 +10266,11 @@ bool LoopVectorizePass::processLoop(Loop *L) {
1023410266 LVP.addMinimumIterationCheck (BestPlan, VF.Width , IC,
1023510267 VF.MinProfitableTripCount );
1023610268 LVP.executePlan (VF.Width , IC, BestPlan, Unroller, DT, false );
10237-
10238- ORE->emit ([&]() {
10239- return OptimizationRemark (LV_NAME, " Interleaved" , L->getStartLoc (),
10240- L->getHeader ())
10241- << " interleaved loop (interleaved count: "
10242- << NV (" InterleaveCount" , IC) << " )" ;
10243- });
10269+ LoopRemoved = BestPlan.getScalarPreheader ()->getNumPredecessors () == 0 ;
1024410270 } else {
10271+ // Report the vectorization decision.
10272+ reportVectorization (ORE, L, VF, IC);
10273+
1024510274 // If we decided that it is *legal* to vectorize the loop, then do it.
1024610275
1024710276 VPlan &BestPlan = LVP.getPlanFor (VF.Width );
@@ -10311,23 +10340,23 @@ bool LoopVectorizePass::processLoop(Loop *L) {
1031110340 // rarely used is not worth unrolling.
1031210341 if (!Checks.hasChecks ())
1031310342 DisableRuntimeUnroll = true ;
10343+ LoopRemoved = BestPlan.getScalarPreheader ()->getNumPredecessors () == 0 ;
1031410344 }
10315- // Report the vectorization decision.
10316- reportVectorization (ORE, L, VF, IC);
1031710345 }
10318-
10319- if (ORE->allowExtraAnalysis (LV_NAME))
10320- checkMixedPrecision (L, ORE);
1032110346 }
1032210347
1032310348 assert (DT->verify (DominatorTree::VerificationLevel::Fast) &&
1032410349 " DT not preserved correctly" );
1032510350
10351+ if (LoopRemoved)
10352+ return true ;
10353+
1032610354 std::optional<MDNode *> RemainderLoopID =
1032710355 makeFollowupLoopID (OrigLoopID, {LLVMLoopVectorizeFollowupAll,
1032810356 LLVMLoopVectorizeFollowupEpilogue});
1032910357 if (RemainderLoopID) {
10330- L->setLoopID (*RemainderLoopID);
10358+ if (!LoopRemoved)
10359+ L->setLoopID (*RemainderLoopID);
1033110360 } else {
1033210361 if (DisableRuntimeUnroll)
1033310362 addRuntimeUnrollDisableMetaData (L);
0 commit comments