Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 9f81bea

Browse files
committed
Apply fix from bug 36184
Review is at https://reviews.llvm.org/D42833 and hasn't landed yet, but let's test it out! https://bugs.llvm.org/show_bug.cgi?id=36184
1 parent 36c8f31 commit 9f81bea

File tree

2 files changed

+84
-8
lines changed

2 files changed

+84
-8
lines changed

lib/Transforms/Scalar/LICM.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static bool hoist(Instruction &I, const DominatorTree *DT, const Loop *CurLoop,
9797
const LoopSafetyInfo *SafetyInfo,
9898
OptimizationRemarkEmitter *ORE);
9999
static bool sink(Instruction &I, LoopInfo *LI, DominatorTree *DT,
100-
const Loop *CurLoop, const LoopSafetyInfo *SafetyInfo,
100+
const Loop *CurLoop, LoopSafetyInfo *SafetyInfo,
101101
OptimizationRemarkEmitter *ORE, bool FreeInLoop);
102102
static bool isSafeToExecuteUnconditionally(Instruction &Inst,
103103
const DominatorTree *DT,
@@ -855,10 +855,16 @@ static Instruction *sinkThroughTriviallyReplacablePHI(
855855
return New;
856856
}
857857

858-
static bool canSplitPredecessors(PHINode *PN) {
858+
static bool canSplitPredecessors(PHINode *PN, LoopSafetyInfo *SafetyInfo) {
859859
BasicBlock *BB = PN->getParent();
860860
if (!BB->canSplitPredecessors())
861861
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;
862868
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
863869
BasicBlock *BBPred = *PI;
864870
if (isa<IndirectBrInst>(BBPred->getTerminator()))
@@ -868,7 +874,8 @@ static bool canSplitPredecessors(PHINode *PN) {
868874
}
869875

870876
static void splitPredecessorsOfLoopExit(PHINode *PN, DominatorTree *DT,
871-
LoopInfo *LI, const Loop *CurLoop) {
877+
LoopInfo *LI, const Loop *CurLoop,
878+
LoopSafetyInfo *SafetyInfo) {
872879
#ifndef NDEBUG
873880
SmallVector<BasicBlock *, 32> ExitBlocks;
874881
CurLoop->getUniqueExitBlocks(ExitBlocks);
@@ -910,13 +917,21 @@ static void splitPredecessorsOfLoopExit(PHINode *PN, DominatorTree *DT,
910917
// LE:
911918
// %p = phi [%p1, %LE.split], [%p2, %LE.split2]
912919
//
920+
auto &BlockColors = SafetyInfo->BlockColors;
913921
SmallSetVector<BasicBlock *, 8> PredBBs(pred_begin(ExitBB), pred_end(ExitBB));
914922
while (!PredBBs.empty()) {
915923
BasicBlock *PredBB = *PredBBs.begin();
916924
assert(CurLoop->contains(PredBB) &&
917925
"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+
}
920935
PredBBs.remove(PredBB);
921936
}
922937
}
@@ -927,7 +942,7 @@ static void splitPredecessorsOfLoopExit(PHINode *PN, DominatorTree *DT,
927942
/// position, and may either delete it or move it to outside of the loop.
928943
///
929944
static bool sink(Instruction &I, LoopInfo *LI, DominatorTree *DT,
930-
const Loop *CurLoop, const LoopSafetyInfo *SafetyInfo,
945+
const Loop *CurLoop, LoopSafetyInfo *SafetyInfo,
931946
OptimizationRemarkEmitter *ORE, bool FreeInLoop) {
932947
DEBUG(dbgs() << "LICM sinking instruction: " << I << "\n");
933948
ORE->emit([&]() {
@@ -975,12 +990,12 @@ static bool sink(Instruction &I, LoopInfo *LI, DominatorTree *DT,
975990
if (isTriviallyReplacablePHI(*PN, I))
976991
continue;
977992

978-
if (!canSplitPredecessors(PN))
993+
if (!canSplitPredecessors(PN, SafetyInfo))
979994
return Changed;
980995

981996
// Split predecessors of the PHI so that we can make users trivially
982997
// replacable.
983-
splitPredecessorsOfLoopExit(PN, DT, LI, CurLoop);
998+
splitPredecessorsOfLoopExit(PN, DT, LI, CurLoop, SafetyInfo);
984999

9851000
// Should rebuild the iterators, as they may be invalidated by
9861001
// splitPredecessorsOfLoopExit().

test/Transforms/LICM/sinking.ll

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,67 @@ try.cont:
670670
ret void
671671
}
672672

673+
; The sinkable call should be sunk into an exit block split. After splitting
674+
; the exit block, BlockColor for new blocks should be added properly so
675+
; that we should be able to access valid ColorVector.
676+
;
677+
; CHECK-LABEL:@test21_pr36184
678+
; CHECK-LABEL: Loop
679+
; CHECK-NOT: %sinkableCall
680+
; CHECK-LABEL:Out.split.loop.exit
681+
; CHECK: %sinkableCall
682+
define i32 @test21_pr36184(i8* %P) personality i32 (...)* @__CxxFrameHandler3 {
683+
entry:
684+
br label %loop.ph
685+
686+
loop.ph:
687+
br label %Loop
688+
689+
Loop:
690+
%sinkableCall = call i32 @strlen( i8* %P ) readonly
691+
br i1 undef, label %ContLoop, label %Out
692+
693+
ContLoop:
694+
br i1 undef, label %Loop, label %Out
695+
696+
Out:
697+
%idx = phi i32 [ %sinkableCall, %Loop ], [0, %ContLoop ]
698+
ret i32 %idx
699+
}
700+
701+
; We do not support splitting a landingpad block if BlockColors is not empty.
702+
; CHECK-LABEL: @test22
703+
; CHECK-LABEL: while.body2
704+
; CHECK-LABEL: %mul
705+
; CHECK-NOT: lpadBB.split{{.*}}
706+
define void @test22(i1 %b, i32 %v1, i32 %v2) personality i32 (...)* @__CxxFrameHandler3 {
707+
entry:
708+
br label %while.cond
709+
while.cond:
710+
br i1 %b, label %try.cont, label %while.body
711+
712+
while.body:
713+
invoke void @may_throw()
714+
to label %while.body2 unwind label %lpadBB
715+
716+
while.body2:
717+
%v = call i32 @getv()
718+
%mul = mul i32 %v, %v2
719+
invoke void @may_throw2()
720+
to label %while.cond unwind label %lpadBB
721+
lpadBB:
722+
%.lcssa1 = phi i32 [ 0, %while.body ], [ %mul, %while.body2 ]
723+
landingpad { i8*, i32 }
724+
catch i8* null
725+
br label %lpadBBSucc1
726+
727+
lpadBBSucc1:
728+
ret void
729+
730+
try.cont:
731+
ret void
732+
}
733+
673734
declare void @may_throw()
674735
declare void @may_throw2()
675736
declare i32 @__CxxFrameHandler3(...)

0 commit comments

Comments
 (0)