Skip to content

Commit b6476e5

Browse files
committed
[LV] Retain branch in middle block when scalar epilogue is needed (NFC)
splitBlock will create a unconditional branch between the middle block and scalar preheader. Instead of creating and replacing the same branch again when scalar epilogue is needed, simply add an early exit. As suggested by @ayalz in #92651 to clarify the existing code.
1 parent 13c6638 commit b6476e5

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2971,32 +2971,33 @@ void InnerLoopVectorizer::createVectorLoopSkeleton(StringRef Prefix) {
29712971
SplitBlock(LoopMiddleBlock, LoopMiddleBlock->getTerminator(), DT, LI,
29722972
nullptr, Twine(Prefix) + "scalar.ph");
29732973

2974-
auto *ScalarLatchTerm = OrigLoop->getLoopLatch()->getTerminator();
2975-
29762974
// Set up the middle block terminator. Two cases:
2977-
// 1) If we know that we must execute the scalar epilogue, emit an
2978-
// unconditional branch.
2975+
// 1) If we know that we must execute the scalar epilogue, retain the existing
2976+
// unconditional branch from the middle block to the scalar preheader. In that
2977+
// case, there's no edge from the middle block to exit blocks and thus no
2978+
// need to update the immediate dominator of the exit blocks.
2979+
if (Cost->requiresScalarEpilogue(VF.isVector())) {
2980+
assert(
2981+
LoopMiddleBlock->getSingleSuccessor() == LoopScalarPreHeader &&
2982+
" middle block should have the scalar preheader as single successor");
2983+
return;
2984+
}
2985+
29792986
// 2) Otherwise, we must have a single unique exit block (due to how we
29802987
// implement the multiple exit case). In this case, set up a conditional
29812988
// branch from the middle block to the loop scalar preheader, and the
29822989
// exit block. completeLoopSkeleton will update the condition to use an
29832990
// iteration check, if required to decide whether to execute the remainder.
29842991
BranchInst *BrInst =
2985-
Cost->requiresScalarEpilogue(VF.isVector())
2986-
? BranchInst::Create(LoopScalarPreHeader)
2987-
: BranchInst::Create(LoopExitBlock, LoopScalarPreHeader,
2988-
Builder.getTrue());
2992+
BranchInst::Create(LoopExitBlock, LoopScalarPreHeader, Builder.getTrue());
2993+
auto *ScalarLatchTerm = OrigLoop->getLoopLatch()->getTerminator();
29892994
BrInst->setDebugLoc(ScalarLatchTerm->getDebugLoc());
29902995
ReplaceInstWithInst(LoopMiddleBlock->getTerminator(), BrInst);
29912996

29922997
// Update dominator for loop exit. During skeleton creation, only the vector
29932998
// pre-header and the middle block are created. The vector loop is entirely
29942999
// created during VPlan exection.
2995-
if (!Cost->requiresScalarEpilogue(VF.isVector()))
2996-
// If there is an epilogue which must run, there's no edge from the
2997-
// middle block to exit blocks and thus no need to update the immediate
2998-
// dominator of the exit blocks.
2999-
DT->changeImmediateDominator(LoopExitBlock, LoopMiddleBlock);
3000+
DT->changeImmediateDominator(LoopExitBlock, LoopMiddleBlock);
30003001
}
30013002

30023003
PHINode *InnerLoopVectorizer::createInductionResumeValue(

0 commit comments

Comments
 (0)