@@ -568,9 +568,6 @@ class InnerLoopVectorizer {
568568
569569 // --- Vectorization state ---
570570
571- // / The vector-loop preheader.
572- BasicBlock *LoopVectorPreHeader = nullptr ;
573-
574571 // / Trip count of the original loop.
575572 Value *TripCount = nullptr ;
576573
@@ -675,12 +672,14 @@ class EpilogueVectorizerMainLoop : public InnerLoopAndEpilogueVectorizer {
675672 void introduceCheckBlockInVPlan (BasicBlock *CheckIRBB);
676673
677674 // Create a check to see if the main vector loop should be executed
678- Value *createIterationCountCheck (ElementCount VF, unsigned UF) const ;
675+ Value *createIterationCountCheck (BasicBlock *VectorPH, ElementCount VF,
676+ unsigned UF) const ;
679677
680678 // / Emits an iteration count bypass check once for the main loop (when \p
681679 // / ForEpilogue is false) and once for the epilogue loop (when \p
682680 // / ForEpilogue is true).
683- BasicBlock *emitIterationCountCheck (BasicBlock *Bypass, bool ForEpilogue);
681+ BasicBlock *emitIterationCountCheck (BasicBlock *VectorPH, BasicBlock *Bypass,
682+ bool ForEpilogue);
684683 void printDebugTracesAtStart () override ;
685684 void printDebugTracesAtEnd () override ;
686685};
@@ -722,7 +721,7 @@ class EpilogueVectorizerEpilogueLoop : public InnerLoopAndEpilogueVectorizer {
722721 // / Emits an iteration count bypass check after the main vector loop has
723722 // / finished to see if there are any iterations left to execute by either
724723 // / the vector epilogue or the scalar epilogue.
725- BasicBlock *emitMinimumVectorEpilogueIterCountCheck (
724+ BasicBlock *emitMinimumVectorEpilogueIterCountCheck (BasicBlock *VectorPH,
726725 BasicBlock *Bypass,
727726 BasicBlock *Insert);
728727 void printDebugTracesAtStart () override ;
@@ -2270,9 +2269,8 @@ void EpilogueVectorizerMainLoop::introduceCheckBlockInVPlan(
22702269 }
22712270}
22722271
2273- Value *
2274- EpilogueVectorizerMainLoop::createIterationCountCheck (ElementCount VF,
2275- unsigned UF) const {
2272+ Value *EpilogueVectorizerMainLoop::createIterationCountCheck (
2273+ BasicBlock *VectorPH, ElementCount VF, unsigned UF) const {
22762274 // Generate code to check if the loop's trip count is less than VF * UF, or
22772275 // equal to it in case a scalar epilogue is required; this implies that the
22782276 // vector trip count is zero. This check also covers the case where adding one
@@ -2283,7 +2281,7 @@ EpilogueVectorizerMainLoop::createIterationCountCheck(ElementCount VF,
22832281
22842282 // Reuse existing vector loop preheader for TC checks.
22852283 // Note that new preheader block is generated for vector loop.
2286- BasicBlock *const TCCheckBlock = LoopVectorPreHeader ;
2284+ BasicBlock *const TCCheckBlock = VectorPH ;
22872285 IRBuilder<InstSimplifyFolder> Builder (
22882286 TCCheckBlock->getContext (),
22892287 InstSimplifyFolder (TCCheckBlock->getDataLayout ()));
@@ -2363,8 +2361,8 @@ static VPIRBasicBlock *replaceVPBBWithIRVPBB(VPBasicBlock *VPBB,
23632361}
23642362
23652363BasicBlock *InnerLoopVectorizer::createScalarPreheader (StringRef Prefix) {
2366- LoopVectorPreHeader = OrigLoop->getLoopPreheader ();
2367- assert (LoopVectorPreHeader && " Invalid loop structure" );
2364+ BasicBlock *VectorPH = OrigLoop->getLoopPreheader ();
2365+ assert (VectorPH && " Invalid loop structure" );
23682366 assert ((OrigLoop->getUniqueLatchExitBlock () ||
23692367 Cost->requiresScalarEpilogue (VF.isVector ())) &&
23702368 " loops not exiting via the latch without required epilogue?" );
@@ -2373,8 +2371,8 @@ BasicBlock *InnerLoopVectorizer::createScalarPreheader(StringRef Prefix) {
23732371 // wrapping the newly created scalar preheader here at the moment, because the
23742372 // Plan's scalar preheader may be unreachable at this point. Instead it is
23752373 // replaced in executePlan.
2376- return SplitBlock (LoopVectorPreHeader, LoopVectorPreHeader ->getTerminator (),
2377- DT, LI, nullptr , Twine (Prefix) + " scalar.ph" );
2374+ return SplitBlock (VectorPH, VectorPH ->getTerminator (), DT, LI, nullptr ,
2375+ Twine (Prefix) + " scalar.ph" );
23782376}
23792377
23802378// / Return the expanded step for \p ID using \p ExpandedSCEVs to look up SCEV
@@ -7276,21 +7274,27 @@ DenseMap<const SCEV *, Value *> LoopVectorizationPlanner::executePlan(
72767274// / depicted in https://llvm.org/docs/Vectorizers.html#epilogue-vectorization.
72777275BasicBlock *EpilogueVectorizerMainLoop::createVectorizedLoopSkeleton () {
72787276 BasicBlock *ScalarPH = createScalarPreheader (" " );
7277+ BasicBlock *VectorPH = ScalarPH->getSinglePredecessor ();
72797278
72807279 // Generate the code to check the minimum iteration count of the vector
72817280 // epilogue (see below).
7282- EPI.EpilogueIterationCountCheck = emitIterationCountCheck (ScalarPH, true );
7281+ EPI.EpilogueIterationCountCheck =
7282+ emitIterationCountCheck (VectorPH, ScalarPH, true );
72837283 EPI.EpilogueIterationCountCheck ->setName (" iter.check" );
72847284
7285+ VectorPH = cast<BranchInst>(EPI.EpilogueIterationCountCheck ->getTerminator ())
7286+ ->getSuccessor (1 );
72857287 // Generate the iteration count check for the main loop, *after* the check
72867288 // for the epilogue loop, so that the path-length is shorter for the case
72877289 // that goes directly through the vector epilogue. The longer-path length for
72887290 // the main loop is compensated for, by the gain from vectorizing the larger
72897291 // trip count. Note: the branch will get updated later on when we vectorize
72907292 // the epilogue.
7291- EPI.MainLoopIterationCountCheck = emitIterationCountCheck (ScalarPH, false );
7293+ EPI.MainLoopIterationCountCheck =
7294+ emitIterationCountCheck (VectorPH, ScalarPH, false );
72927295
7293- return LoopVectorPreHeader;
7296+ return cast<BranchInst>(EPI.MainLoopIterationCountCheck ->getTerminator ())
7297+ ->getSuccessor (1 );
72947298}
72957299
72967300void EpilogueVectorizerMainLoop::printDebugTracesAtStart () {
@@ -7310,35 +7314,33 @@ void EpilogueVectorizerMainLoop::printDebugTracesAtEnd() {
73107314 });
73117315}
73127316
7313- BasicBlock *
7314- EpilogueVectorizerMainLoop::emitIterationCountCheck (BasicBlock *Bypass,
7315- bool ForEpilogue) {
7317+ BasicBlock *EpilogueVectorizerMainLoop::emitIterationCountCheck (
7318+ BasicBlock *VectorPH, BasicBlock *Bypass, bool ForEpilogue) {
73167319 assert (Bypass && " Expected valid bypass basic block." );
73177320 Value *Count = getTripCount ();
73187321 MinProfitableTripCount = ElementCount::getFixed (0 );
7319- Value *CheckMinIters =
7320- createIterationCountCheck ( ForEpilogue ? EPI.EpilogueVF : EPI.MainLoopVF ,
7321- ForEpilogue ? EPI.EpilogueUF : EPI.MainLoopUF );
7322+ Value *CheckMinIters = createIterationCountCheck (
7323+ VectorPH, ForEpilogue ? EPI.EpilogueVF : EPI.MainLoopVF ,
7324+ ForEpilogue ? EPI.EpilogueUF : EPI.MainLoopUF );
73227325
7323- BasicBlock *const TCCheckBlock = LoopVectorPreHeader ;
7326+ BasicBlock *const TCCheckBlock = VectorPH ;
73247327 if (!ForEpilogue)
73257328 TCCheckBlock->setName (" vector.main.loop.iter.check" );
73267329
73277330 // Create new preheader for vector loop.
7328- LoopVectorPreHeader = SplitBlock (TCCheckBlock, TCCheckBlock->getTerminator (),
7329- static_cast <DominatorTree *>(nullptr ), LI,
7330- nullptr , " vector.ph" );
7331+ VectorPH = SplitBlock (TCCheckBlock, TCCheckBlock->getTerminator (),
7332+ static_cast <DominatorTree *>(nullptr ), LI, nullptr ,
7333+ " vector.ph" );
73317334 if (ForEpilogue) {
73327335 // Save the trip count so we don't have to regenerate it in the
73337336 // vec.epilog.iter.check. This is safe to do because the trip count
73347337 // generated here dominates the vector epilog iter check.
73357338 EPI.TripCount = Count;
73367339 } else {
7337- VectorPHVPBB = replaceVPBBWithIRVPBB (VectorPHVPBB, LoopVectorPreHeader );
7340+ VectorPHVPBB = replaceVPBBWithIRVPBB (VectorPHVPBB, VectorPH );
73387341 }
73397342
7340- BranchInst &BI =
7341- *BranchInst::Create (Bypass, LoopVectorPreHeader, CheckMinIters);
7343+ BranchInst &BI = *BranchInst::Create (Bypass, VectorPH, CheckMinIters);
73427344 if (hasBranchWeightMD (*OrigLoop->getLoopLatch ()->getTerminator ()))
73437345 setBranchWeights (BI, MinItersBypassWeights, /* IsExpected=*/ false );
73447346 ReplaceInstWithInst (TCCheckBlock->getTerminator (), &BI);
@@ -7360,16 +7362,16 @@ EpilogueVectorizerMainLoop::emitIterationCountCheck(BasicBlock *Bypass,
73607362// / depicted in https://llvm.org/docs/Vectorizers.html#epilogue-vectorization.
73617363BasicBlock *EpilogueVectorizerEpilogueLoop::createVectorizedLoopSkeleton () {
73627364 BasicBlock *ScalarPH = createScalarPreheader (" vec.epilog." );
7363-
7365+ BasicBlock *VectorPH = ScalarPH-> getSinglePredecessor ();
73647366 // Now, compare the remaining count and if there aren't enough iterations to
73657367 // execute the vectorized epilogue skip to the scalar part.
7366- LoopVectorPreHeader ->setName (" vec.epilog.ph" );
7368+ VectorPH ->setName (" vec.epilog.ph" );
73677369 BasicBlock *VecEpilogueIterationCountCheck =
7368- SplitBlock (LoopVectorPreHeader, LoopVectorPreHeader ->begin (), DT, LI,
7369- nullptr , " vec.epilog.iter.check" , true );
7370- VectorPHVPBB = replaceVPBBWithIRVPBB (VectorPHVPBB, LoopVectorPreHeader );
7370+ SplitBlock (VectorPH, VectorPH ->begin (), DT, LI, nullptr ,
7371+ " vec.epilog.iter.check" , true );
7372+ VectorPHVPBB = replaceVPBBWithIRVPBB (VectorPHVPBB, VectorPH );
73717373
7372- emitMinimumVectorEpilogueIterCountCheck (ScalarPH,
7374+ emitMinimumVectorEpilogueIterCountCheck (VectorPH, ScalarPH,
73737375 VecEpilogueIterationCountCheck);
73747376 AdditionalBypassBlock = VecEpilogueIterationCountCheck;
73757377
@@ -7378,7 +7380,7 @@ BasicBlock *EpilogueVectorizerEpilogueLoop::createVectorizedLoopSkeleton() {
73787380 assert (EPI.MainLoopIterationCountCheck && EPI.EpilogueIterationCountCheck &&
73797381 " expected this to be saved from the previous pass." );
73807382 EPI.MainLoopIterationCountCheck ->getTerminator ()->replaceUsesOfWith (
7381- VecEpilogueIterationCountCheck, LoopVectorPreHeader );
7383+ VecEpilogueIterationCountCheck, VectorPH );
73827384
73837385 EPI.EpilogueIterationCountCheck ->getTerminator ()->replaceUsesOfWith (
73847386 VecEpilogueIterationCountCheck, ScalarPH);
@@ -7402,7 +7404,7 @@ BasicBlock *EpilogueVectorizerEpilogueLoop::createVectorizedLoopSkeleton() {
74027404 llvm::make_pointer_range (VecEpilogueIterationCountCheck->phis ()));
74037405
74047406 for (PHINode *Phi : PhisInBlock) {
7405- Phi->moveBefore (LoopVectorPreHeader ->getFirstNonPHIIt ());
7407+ Phi->moveBefore (VectorPH ->getFirstNonPHIIt ());
74067408 Phi->replaceIncomingBlockWith (
74077409 VecEpilogueIterationCountCheck->getSinglePredecessor (),
74087410 VecEpilogueIterationCountCheck);
@@ -7422,12 +7424,12 @@ BasicBlock *EpilogueVectorizerEpilogueLoop::createVectorizedLoopSkeleton() {
74227424 Phi->removeIncomingValue (MemCheckBlock);
74237425 }
74247426
7425- return LoopVectorPreHeader ;
7427+ return VectorPH ;
74267428}
74277429
74287430BasicBlock *
74297431EpilogueVectorizerEpilogueLoop::emitMinimumVectorEpilogueIterCountCheck (
7430- BasicBlock *Bypass, BasicBlock *Insert) {
7432+ BasicBlock *VectorPH, BasicBlock * Bypass, BasicBlock *Insert) {
74317433
74327434 assert (EPI.TripCount &&
74337435 " Expected trip count to have been saved in the first pass." );
@@ -7447,8 +7449,7 @@ EpilogueVectorizerEpilogueLoop::emitMinimumVectorEpilogueIterCountCheck(
74477449 EPI.EpilogueVF , EPI.EpilogueUF ),
74487450 " min.epilog.iters.check" );
74497451
7450- BranchInst &BI =
7451- *BranchInst::Create (Bypass, LoopVectorPreHeader, CheckMinIters);
7452+ BranchInst &BI = *BranchInst::Create (Bypass, VectorPH, CheckMinIters);
74527453 auto VScale = Cost->getVScaleForTuning ();
74537454 unsigned MainLoopStep =
74547455 estimateElementCount (EPI.MainLoopVF * EPI.MainLoopUF , VScale);
0 commit comments