@@ -97,7 +97,7 @@ static bool hoist(Instruction &I, const DominatorTree *DT, const Loop *CurLoop,
97
97
const LoopSafetyInfo *SafetyInfo,
98
98
OptimizationRemarkEmitter *ORE);
99
99
static bool sink (Instruction &I, LoopInfo *LI, DominatorTree *DT,
100
- const Loop *CurLoop, const LoopSafetyInfo *SafetyInfo,
100
+ const Loop *CurLoop, LoopSafetyInfo *SafetyInfo,
101
101
OptimizationRemarkEmitter *ORE, bool FreeInLoop);
102
102
static bool isSafeToExecuteUnconditionally (Instruction &Inst,
103
103
const DominatorTree *DT,
@@ -855,10 +855,16 @@ static Instruction *sinkThroughTriviallyReplacablePHI(
855
855
return New;
856
856
}
857
857
858
- static bool canSplitPredecessors (PHINode *PN) {
858
+ static bool canSplitPredecessors (PHINode *PN, LoopSafetyInfo *SafetyInfo ) {
859
859
BasicBlock *BB = PN->getParent ();
860
860
if (!BB->canSplitPredecessors ())
861
861
return false ;
862
+ // FIXME: it's not impossible to split LandingPad blocks, but if BlockColors
863
+ // already exist it require updating BlockColors for all offspring blocks
864
+ // accordingly. By skipping such corner case, we can make updating BlockColors
865
+ // after splitting predecessor fairly simple.
866
+ if (!SafetyInfo->BlockColors .empty () && BB->getFirstNonPHI ()->isEHPad ())
867
+ return false ;
862
868
for (pred_iterator PI = pred_begin (BB), E = pred_end (BB); PI != E; ++PI) {
863
869
BasicBlock *BBPred = *PI;
864
870
if (isa<IndirectBrInst>(BBPred->getTerminator ()))
@@ -868,7 +874,8 @@ static bool canSplitPredecessors(PHINode *PN) {
868
874
}
869
875
870
876
static void splitPredecessorsOfLoopExit (PHINode *PN, DominatorTree *DT,
871
- LoopInfo *LI, const Loop *CurLoop) {
877
+ LoopInfo *LI, const Loop *CurLoop,
878
+ LoopSafetyInfo *SafetyInfo) {
872
879
#ifndef NDEBUG
873
880
SmallVector<BasicBlock *, 32 > ExitBlocks;
874
881
CurLoop->getUniqueExitBlocks (ExitBlocks);
@@ -910,13 +917,21 @@ static void splitPredecessorsOfLoopExit(PHINode *PN, DominatorTree *DT,
910
917
// LE:
911
918
// %p = phi [%p1, %LE.split], [%p2, %LE.split2]
912
919
//
920
+ auto &BlockColors = SafetyInfo->BlockColors ;
913
921
SmallSetVector<BasicBlock *, 8 > PredBBs (pred_begin (ExitBB), pred_end (ExitBB));
914
922
while (!PredBBs.empty ()) {
915
923
BasicBlock *PredBB = *PredBBs.begin ();
916
924
assert (CurLoop->contains (PredBB) &&
917
925
" Expect all predecessors are in the loop" );
918
- if (PN->getBasicBlockIndex (PredBB) >= 0 )
919
- SplitBlockPredecessors (ExitBB, PredBB, " .split.loop.exit" , DT, LI, true );
926
+ if (PN->getBasicBlockIndex (PredBB) >= 0 ) {
927
+ BasicBlock *NewPred = SplitBlockPredecessors (
928
+ ExitBB, PredBB, " .split.loop.exit" , DT, LI, true );
929
+ // Since we do not allow splitting EH-block with BlockColors in
930
+ // canSplitPredecessors(), we can simply assign predecessor's color to
931
+ // the new block.
932
+ if (!BlockColors.empty ())
933
+ BlockColors[NewPred] = BlockColors[PredBB];
934
+ }
920
935
PredBBs.remove (PredBB);
921
936
}
922
937
}
@@ -927,7 +942,7 @@ static void splitPredecessorsOfLoopExit(PHINode *PN, DominatorTree *DT,
927
942
// / position, and may either delete it or move it to outside of the loop.
928
943
// /
929
944
static bool sink (Instruction &I, LoopInfo *LI, DominatorTree *DT,
930
- const Loop *CurLoop, const LoopSafetyInfo *SafetyInfo,
945
+ const Loop *CurLoop, LoopSafetyInfo *SafetyInfo,
931
946
OptimizationRemarkEmitter *ORE, bool FreeInLoop) {
932
947
DEBUG (dbgs () << " LICM sinking instruction: " << I << " \n " );
933
948
ORE->emit ([&]() {
@@ -975,12 +990,12 @@ static bool sink(Instruction &I, LoopInfo *LI, DominatorTree *DT,
975
990
if (isTriviallyReplacablePHI (*PN, I))
976
991
continue ;
977
992
978
- if (!canSplitPredecessors (PN))
993
+ if (!canSplitPredecessors (PN, SafetyInfo ))
979
994
return Changed;
980
995
981
996
// Split predecessors of the PHI so that we can make users trivially
982
997
// replacable.
983
- splitPredecessorsOfLoopExit (PN, DT, LI, CurLoop);
998
+ splitPredecessorsOfLoopExit (PN, DT, LI, CurLoop, SafetyInfo );
984
999
985
1000
// Should rebuild the iterators, as they may be invalidated by
986
1001
// splitPredecessorsOfLoopExit().
0 commit comments