@@ -1587,10 +1587,12 @@ deleteDeadClonedBlocks(Loop &L, ArrayRef<BasicBlock *> ExitBlocks,
1587
1587
BB->eraseFromParent ();
1588
1588
}
1589
1589
1590
- static void deleteDeadBlocksFromLoop (Loop &L,
1591
- SmallVectorImpl<BasicBlock *> &ExitBlocks,
1592
- DominatorTree &DT, LoopInfo &LI,
1593
- MemorySSAUpdater *MSSAU) {
1590
+ static void
1591
+ deleteDeadBlocksFromLoop (Loop &L,
1592
+ SmallVectorImpl<BasicBlock *> &ExitBlocks,
1593
+ DominatorTree &DT, LoopInfo &LI,
1594
+ MemorySSAUpdater *MSSAU,
1595
+ function_ref<void (Loop &, StringRef)> DestroyLoopCB) {
1594
1596
// Find all the dead blocks tied to this loop, and remove them from their
1595
1597
// successors.
1596
1598
SmallSetVector<BasicBlock *, 8 > DeadBlockSet;
@@ -1640,6 +1642,7 @@ static void deleteDeadBlocksFromLoop(Loop &L,
1640
1642
}) &&
1641
1643
" If the child loop header is dead all blocks in the child loop must "
1642
1644
" be dead as well!" );
1645
+ DestroyLoopCB (*ChildL, ChildL->getName ());
1643
1646
LI.destroy (ChildL);
1644
1647
return true ;
1645
1648
});
@@ -1980,6 +1983,8 @@ static bool rebuildLoopAfterUnswitch(Loop &L, ArrayRef<BasicBlock *> ExitBlocks,
1980
1983
ParentL->removeChildLoop (llvm::find (*ParentL, &L));
1981
1984
else
1982
1985
LI.removeLoop (llvm::find (LI, &L));
1986
+ // markLoopAsDeleted for L should be triggered by the caller (it is typically
1987
+ // done by using the UnswitchCB callback).
1983
1988
LI.destroy (&L);
1984
1989
return false ;
1985
1990
}
@@ -2019,7 +2024,8 @@ static void unswitchNontrivialInvariants(
2019
2024
SmallVectorImpl<BasicBlock *> &ExitBlocks, IVConditionInfo &PartialIVInfo,
2020
2025
DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC,
2021
2026
function_ref<void (bool , bool , ArrayRef<Loop *>)> UnswitchCB,
2022
- ScalarEvolution *SE, MemorySSAUpdater *MSSAU) {
2027
+ ScalarEvolution *SE, MemorySSAUpdater *MSSAU,
2028
+ function_ref<void(Loop &, StringRef)> DestroyLoopCB) {
2023
2029
auto *ParentBB = TI.getParent ();
2024
2030
BranchInst *BI = dyn_cast<BranchInst>(&TI);
2025
2031
SwitchInst *SI = BI ? nullptr : cast<SwitchInst>(&TI);
@@ -2319,7 +2325,7 @@ static void unswitchNontrivialInvariants(
2319
2325
// Now that our cloned loops have been built, we can update the original loop.
2320
2326
// First we delete the dead blocks from it and then we rebuild the loop
2321
2327
// structure taking these deletions into account.
2322
- deleteDeadBlocksFromLoop (L, ExitBlocks, DT, LI, MSSAU);
2328
+ deleteDeadBlocksFromLoop (L, ExitBlocks, DT, LI, MSSAU, DestroyLoopCB );
2323
2329
2324
2330
if (MSSAU && VerifyMemorySSA)
2325
2331
MSSAU->getMemorySSA ()->verifyMemorySSA ();
@@ -2670,7 +2676,8 @@ static bool unswitchBestCondition(
2670
2676
Loop &L, DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC,
2671
2677
AAResults &AA, TargetTransformInfo &TTI,
2672
2678
function_ref<void (bool , bool , ArrayRef<Loop *>)> UnswitchCB,
2673
- ScalarEvolution *SE, MemorySSAUpdater *MSSAU) {
2679
+ ScalarEvolution *SE, MemorySSAUpdater *MSSAU,
2680
+ function_ref<void(Loop &, StringRef)> DestroyLoopCB) {
2674
2681
// Collect all invariant conditions within this loop (as opposed to an inner
2675
2682
// loop which would be handled when visiting that inner loop).
2676
2683
SmallVector<std::pair<Instruction *, TinyPtrVector<Value *>>, 4 >
@@ -2958,7 +2965,7 @@ static bool unswitchBestCondition(
2958
2965
<< " \n " );
2959
2966
unswitchNontrivialInvariants (L, *BestUnswitchTI, BestUnswitchInvariants,
2960
2967
ExitBlocks, PartialIVInfo, DT, LI, AC,
2961
- UnswitchCB, SE, MSSAU);
2968
+ UnswitchCB, SE, MSSAU, DestroyLoopCB );
2962
2969
return true ;
2963
2970
}
2964
2971
@@ -2988,7 +2995,8 @@ unswitchLoop(Loop &L, DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC,
2988
2995
AAResults &AA, TargetTransformInfo &TTI, bool Trivial,
2989
2996
bool NonTrivial,
2990
2997
function_ref<void (bool , bool , ArrayRef<Loop *>)> UnswitchCB,
2991
- ScalarEvolution *SE, MemorySSAUpdater *MSSAU) {
2998
+ ScalarEvolution *SE, MemorySSAUpdater *MSSAU,
2999
+ function_ref<void(Loop &, StringRef)> DestroyLoopCB) {
2992
3000
assert (L.isRecursivelyLCSSAForm (DT, LI) &&
2993
3001
" Loops must be in LCSSA form before unswitching." );
2994
3002
@@ -3036,7 +3044,8 @@ unswitchLoop(Loop &L, DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC,
3036
3044
3037
3045
// Try to unswitch the best invariant condition. We prefer this full unswitch to
3038
3046
// a partial unswitch when possible below the threshold.
3039
- if (unswitchBestCondition (L, DT, LI, AC, AA, TTI, UnswitchCB, SE, MSSAU))
3047
+ if (unswitchBestCondition (L, DT, LI, AC, AA, TTI, UnswitchCB, SE, MSSAU,
3048
+ DestroyLoopCB))
3040
3049
return true ;
3041
3050
3042
3051
// No other opportunities to unswitch.
@@ -3083,6 +3092,10 @@ PreservedAnalyses SimpleLoopUnswitchPass::run(Loop &L, LoopAnalysisManager &AM,
3083
3092
U.markLoopAsDeleted (L, LoopName);
3084
3093
};
3085
3094
3095
+ auto DestroyLoopCB = [&U](Loop &L, StringRef Name) {
3096
+ U.markLoopAsDeleted (L, Name);
3097
+ };
3098
+
3086
3099
Optional<MemorySSAUpdater> MSSAU;
3087
3100
if (AR.MSSA ) {
3088
3101
MSSAU = MemorySSAUpdater (AR.MSSA );
@@ -3091,7 +3104,8 @@ PreservedAnalyses SimpleLoopUnswitchPass::run(Loop &L, LoopAnalysisManager &AM,
3091
3104
}
3092
3105
if (!unswitchLoop (L, AR.DT , AR.LI , AR.AC , AR.AA , AR.TTI , Trivial, NonTrivial,
3093
3106
UnswitchCB, &AR.SE ,
3094
- MSSAU.hasValue () ? MSSAU.getPointer () : nullptr ))
3107
+ MSSAU.hasValue () ? MSSAU.getPointer () : nullptr ,
3108
+ DestroyLoopCB))
3095
3109
return PreservedAnalyses::all ();
3096
3110
3097
3111
if (AR.MSSA && VerifyMemorySSA)
@@ -3179,12 +3193,17 @@ bool SimpleLoopUnswitchLegacyPass::runOnLoop(Loop *L, LPPassManager &LPM) {
3179
3193
LPM.markLoopAsDeleted (*L);
3180
3194
};
3181
3195
3196
+ auto DestroyLoopCB = [&LPM](Loop &L, StringRef /* Name */ ) {
3197
+ LPM.markLoopAsDeleted (L);
3198
+ };
3199
+
3182
3200
if (MSSA && VerifyMemorySSA)
3183
3201
MSSA->verifyMemorySSA ();
3184
3202
3185
3203
bool Changed =
3186
3204
unswitchLoop (*L, DT, LI, AC, AA, TTI, true , NonTrivial, UnswitchCB, SE,
3187
- MSSAU.hasValue () ? MSSAU.getPointer () : nullptr );
3205
+ MSSAU.hasValue () ? MSSAU.getPointer () : nullptr ,
3206
+ DestroyLoopCB);
3188
3207
3189
3208
if (MSSA && VerifyMemorySSA)
3190
3209
MSSA->verifyMemorySSA ();
0 commit comments