@@ -969,12 +969,24 @@ void VPlan::execute(VPTransformState *State) {
969969 setName("Final VPlan");
970970 LLVM_DEBUG(dump());
971971
972- // Disconnect scalar preheader and scalar header, as the dominator tree edge
973- // will be updated as part of VPlan execution. This allows keeping the DTU
974- // logic generic during VPlan execution.
975972 BasicBlock *ScalarPh = State->CFG.ExitBB;
976- State->CFG.DTU.applyUpdates(
977- {{DominatorTree::Delete, ScalarPh, ScalarPh->getSingleSuccessor()}});
973+ VPBasicBlock *ScalarPhVPBB = getScalarPreheader();
974+ if (ScalarPhVPBB->hasPredecessors()) {
975+ // Disconnect scalar preheader and scalar header, as the dominator tree edge
976+ // will be updated as part of VPlan execution. This allows keeping the DTU
977+ // logic generic during VPlan execution.
978+ State->CFG.DTU.applyUpdates(
979+ {{DominatorTree::Delete, ScalarPh, ScalarPh->getSingleSuccessor()}});
980+ } else {
981+ Loop *OrigLoop =
982+ State->LI->getLoopFor(getScalarHeader()->getIRBasicBlock());
983+ // If the original loop is unreachable, we need to delete it.
984+ auto Blocks = OrigLoop->getBlocksVector();
985+ Blocks.push_back(cast<VPIRBasicBlock>(ScalarPhVPBB)->getIRBasicBlock());
986+ for (auto *BB : Blocks)
987+ State->LI->removeBlock(BB);
988+ State->LI->erase(OrigLoop);
989+ }
978990
979991 ReversePostOrderTraversal<VPBlockShallowTraversalWrapper<VPBlockBase *>> RPOT(
980992 Entry);
@@ -1648,14 +1660,18 @@ static void addRuntimeUnrollDisableMetaData(Loop *L) {
16481660}
16491661
16501662void LoopVectorizationPlanner::updateLoopMetadataAndProfileInfo(
1651- Loop *VectorLoop, VPBasicBlock *HeaderVPBB, bool VectorizingEpilogue,
1652- unsigned EstimatedVFxUF, bool DisableRuntimeUnroll) {
1653- MDNode *LID = OrigLoop->getLoopID();
1663+ Loop *VectorLoop, VPBasicBlock *HeaderVPBB, const VPlan &Plan,
1664+ bool VectorizingEpilogue, MDNode *OrigLoopID,
1665+ std::optional<unsigned> OrigAverageTripCount,
1666+ unsigned OrigLoopInvocationWeight, unsigned EstimatedVFxUF,
1667+ bool DisableRuntimeUnroll) {
16541668 // Update the metadata of the scalar loop. Skip the update when vectorizing
1655- // the epilogue loop, to ensure it is only updated once.
1656- if (!VectorizingEpilogue) {
1657- std::optional<MDNode *> RemainderLoopID = makeFollowupLoopID(
1658- LID, {LLVMLoopVectorizeFollowupAll, LLVMLoopVectorizeFollowupEpilogue});
1669+ // the epilogue loop to ensure it is updated only once. Also skip the update
1670+ // when the scalar loop became unreachable.
1671+ if (Plan.getScalarPreheader()->hasPredecessors() && !VectorizingEpilogue) {
1672+ std::optional<MDNode *> RemainderLoopID =
1673+ makeFollowupLoopID(OrigLoopID, {LLVMLoopVectorizeFollowupAll,
1674+ LLVMLoopVectorizeFollowupEpilogue});
16591675 if (RemainderLoopID) {
16601676 OrigLoop->setLoopID(*RemainderLoopID);
16611677 } else {
@@ -1670,15 +1686,15 @@ void LoopVectorizationPlanner::updateLoopMetadataAndProfileInfo(
16701686 if (!VectorLoop)
16711687 return;
16721688
1673- if (std::optional<MDNode *> VectorizedLoopID =
1674- makeFollowupLoopID(LID , {LLVMLoopVectorizeFollowupAll,
1675- LLVMLoopVectorizeFollowupVectorized})) {
1689+ if (std::optional<MDNode *> VectorizedLoopID = makeFollowupLoopID(
1690+ OrigLoopID , {LLVMLoopVectorizeFollowupAll,
1691+ LLVMLoopVectorizeFollowupVectorized})) {
16761692 VectorLoop->setLoopID(*VectorizedLoopID);
16771693 } else {
16781694 // Keep all loop hints from the original loop on the vector loop (we'll
16791695 // replace the vectorizer-specific hints below).
1680- if (LID )
1681- VectorLoop->setLoopID(LID );
1696+ if (OrigLoopID )
1697+ VectorLoop->setLoopID(OrigLoopID );
16821698
16831699 if (!VectorizingEpilogue) {
16841700 LoopVectorizeHints Hints(VectorLoop, true, *ORE);
@@ -1723,7 +1739,21 @@ void LoopVectorizationPlanner::updateLoopMetadataAndProfileInfo(
17231739 // For scalable vectorization we can't know at compile time how many
17241740 // iterations of the loop are handled in one vector iteration, so instead
17251741 // use the value of vscale used for tuning.
1726- setProfileInfoAfterUnrolling(OrigLoop, VectorLoop, OrigLoop, EstimatedVFxUF);
1742+ if (!OrigAverageTripCount)
1743+ return;
1744+ // Calculate number of iterations in unrolled loop.
1745+ unsigned AverageVectorTripCount = *OrigAverageTripCount / EstimatedVFxUF;
1746+ // Calculate number of iterations for remainder loop.
1747+ unsigned RemainderAverageTripCount = *OrigAverageTripCount % EstimatedVFxUF;
1748+
1749+ if (HeaderVPBB) {
1750+ setLoopEstimatedTripCount(VectorLoop, AverageVectorTripCount,
1751+ OrigLoopInvocationWeight);
1752+ }
1753+ if (Plan.getScalarPreheader()->hasPredecessors()) {
1754+ setLoopEstimatedTripCount(OrigLoop, RemainderAverageTripCount,
1755+ OrigLoopInvocationWeight);
1756+ }
17271757}
17281758
17291759#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
0 commit comments