@@ -969,12 +969,24 @@ void VPlan::execute(VPTransformState *State) {
969
969
setName (" Final VPlan" );
970
970
LLVM_DEBUG (dump ());
971
971
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.
975
972
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
+ }
978
990
979
991
ReversePostOrderTraversal<VPBlockShallowTraversalWrapper<VPBlockBase *>> RPOT (
980
992
Entry);
@@ -1648,14 +1660,18 @@ static void addRuntimeUnrollDisableMetaData(Loop *L) {
1648
1660
}
1649
1661
1650
1662
void 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) {
1654
1668
// 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});
1659
1675
if (RemainderLoopID) {
1660
1676
OrigLoop->setLoopID (*RemainderLoopID);
1661
1677
} else {
@@ -1670,15 +1686,15 @@ void LoopVectorizationPlanner::updateLoopMetadataAndProfileInfo(
1670
1686
if (!VectorLoop)
1671
1687
return ;
1672
1688
1673
- if (std::optional<MDNode *> VectorizedLoopID =
1674
- makeFollowupLoopID (LID , {LLVMLoopVectorizeFollowupAll,
1675
- LLVMLoopVectorizeFollowupVectorized})) {
1689
+ if (std::optional<MDNode *> VectorizedLoopID = makeFollowupLoopID (
1690
+ OrigLoopID , {LLVMLoopVectorizeFollowupAll,
1691
+ LLVMLoopVectorizeFollowupVectorized})) {
1676
1692
VectorLoop->setLoopID (*VectorizedLoopID);
1677
1693
} else {
1678
1694
// Keep all loop hints from the original loop on the vector loop (we'll
1679
1695
// replace the vectorizer-specific hints below).
1680
- if (LID )
1681
- VectorLoop->setLoopID (LID );
1696
+ if (OrigLoopID )
1697
+ VectorLoop->setLoopID (OrigLoopID );
1682
1698
1683
1699
if (!VectorizingEpilogue) {
1684
1700
LoopVectorizeHints Hints (VectorLoop, true , *ORE);
@@ -1723,7 +1739,21 @@ void LoopVectorizationPlanner::updateLoopMetadataAndProfileInfo(
1723
1739
// For scalable vectorization we can't know at compile time how many
1724
1740
// iterations of the loop are handled in one vector iteration, so instead
1725
1741
// 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
+ }
1727
1757
}
1728
1758
1729
1759
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
0 commit comments