llvm.org GIT mirror llvm / e0d9119
Merging r324916: ------------------------------------------------------------------------ r324916 | junbuml | 2018-02-12 18:56:55 +0100 (Mon, 12 Feb 2018) | 7 lines [LICM] update BlockColors after splitting predecessors Update BlockColors after splitting predecessors. Do not allow splitting EHPad for sinking when the BlockColors is not empty, so we can simply assign predecessor's color to the new block. Fixes PR36184 ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_60@325507 91177308-0d34-0410-b5e6-96231b3b80d8 Hans Wennborg 1 year, 6 months ago
2 changed file(s) with 84 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
9696 const LoopSafetyInfo *SafetyInfo,
9797 OptimizationRemarkEmitter *ORE);
9898 static bool sink(Instruction &I, LoopInfo *LI, DominatorTree *DT,
99 const Loop *CurLoop, const LoopSafetyInfo *SafetyInfo,
99 const Loop *CurLoop, LoopSafetyInfo *SafetyInfo,
100100 OptimizationRemarkEmitter *ORE, bool FreeInLoop);
101101 static bool isSafeToExecuteUnconditionally(Instruction &Inst,
102102 const DominatorTree *DT,
854854 return New;
855855 }
856856
857 static bool canSplitPredecessors(PHINode *PN) {
857 static bool canSplitPredecessors(PHINode *PN, LoopSafetyInfo *SafetyInfo) {
858858 BasicBlock *BB = PN->getParent();
859859 if (!BB->canSplitPredecessors())
860 return false;
861 // It's not impossible to split EHPad blocks, but if BlockColors already exist
862 // it require updating BlockColors for all offspring blocks accordingly. By
863 // skipping such corner case, we can make updating BlockColors after splitting
864 // predecessor fairly simple.
865 if (!SafetyInfo->BlockColors.empty() && BB->getFirstNonPHI()->isEHPad())
860866 return false;
861867 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
862868 BasicBlock *BBPred = *PI;
867873 }
868874
869875 static void splitPredecessorsOfLoopExit(PHINode *PN, DominatorTree *DT,
870 LoopInfo *LI, const Loop *CurLoop) {
876 LoopInfo *LI, const Loop *CurLoop,
877 LoopSafetyInfo *SafetyInfo) {
871878 #ifndef NDEBUG
872879 SmallVector ExitBlocks;
873880 CurLoop->getUniqueExitBlocks(ExitBlocks);
909916 // LE:
910917 // %p = phi [%p1, %LE.split], [%p2, %LE.split2]
911918 //
919 auto &BlockColors = SafetyInfo->BlockColors;
912920 SmallSetVector PredBBs(pred_begin(ExitBB), pred_end(ExitBB));
913921 while (!PredBBs.empty()) {
914922 BasicBlock *PredBB = *PredBBs.begin();
915923 assert(CurLoop->contains(PredBB) &&
916924 "Expect all predecessors are in the loop");
917 if (PN->getBasicBlockIndex(PredBB) >= 0)
918 SplitBlockPredecessors(ExitBB, PredBB, ".split.loop.exit", DT, LI, true);
925 if (PN->getBasicBlockIndex(PredBB) >= 0) {
926 BasicBlock *NewPred = SplitBlockPredecessors(
927 ExitBB, PredBB, ".split.loop.exit", DT, LI, true);
928 // Since we do not allow splitting EH-block with BlockColors in
929 // canSplitPredecessors(), we can simply assign predecessor's color to
930 // the new block.
931 if (!BlockColors.empty())
932 BlockColors[NewPred] = BlockColors[PredBB];
933 }
919934 PredBBs.remove(PredBB);
920935 }
921936 }
926941 /// position, and may either delete it or move it to outside of the loop.
927942 ///
928943 static bool sink(Instruction &I, LoopInfo *LI, DominatorTree *DT,
929 const Loop *CurLoop, const LoopSafetyInfo *SafetyInfo,
944 const Loop *CurLoop, LoopSafetyInfo *SafetyInfo,
930945 OptimizationRemarkEmitter *ORE, bool FreeInLoop) {
931946 DEBUG(dbgs() << "LICM sinking instruction: " << I << "\n");
932947 ORE->emit([&]() {
974989 if (isTriviallyReplacablePHI(*PN, I))
975990 continue;
976991
977 if (!canSplitPredecessors(PN))
992 if (!canSplitPredecessors(PN, SafetyInfo))
978993 return Changed;
979994
980995 // Split predecessors of the PHI so that we can make users trivially
981996 // replacable.
982 splitPredecessorsOfLoopExit(PN, DT, LI, CurLoop);
997 splitPredecessorsOfLoopExit(PN, DT, LI, CurLoop, SafetyInfo);
983998
984999 // Should rebuild the iterators, as they may be invalidated by
9851000 // splitPredecessorsOfLoopExit().
669669 ret void
670670 }
671671
672 ; The sinkable call should be sunk into an exit block split. After splitting
673 ; the exit block, BlockColor for new blocks should be added properly so
674 ; that we should be able to access valid ColorVector.
675 ;
676 ; CHECK-LABEL:@test21_pr36184
677 ; CHECK-LABEL: Loop
678 ; CHECK-NOT: %sinkableCall
679 ; CHECK-LABEL:Out.split.loop.exit
680 ; CHECK: %sinkableCall
681 define i32 @test21_pr36184(i8* %P) personality i32 (...)* @__CxxFrameHandler3 {
682 entry:
683 br label %loop.ph
684
685 loop.ph:
686 br label %Loop
687
688 Loop:
689 %sinkableCall = call i32 @strlen( i8* %P ) readonly
690 br i1 undef, label %ContLoop, label %Out
691
692 ContLoop:
693 br i1 undef, label %Loop, label %Out
694
695 Out:
696 %idx = phi i32 [ %sinkableCall, %Loop ], [0, %ContLoop ]
697 ret i32 %idx
698 }
699
700 ; We do not support splitting a landingpad block if BlockColors is not empty.
701 ; CHECK-LABEL: @test22
702 ; CHECK-LABEL: while.body2
703 ; CHECK-LABEL: %mul
704 ; CHECK-NOT: lpadBB.split{{.*}}
705 define void @test22(i1 %b, i32 %v1, i32 %v2) personality i32 (...)* @__CxxFrameHandler3 {
706 entry:
707 br label %while.cond
708 while.cond:
709 br i1 %b, label %try.cont, label %while.body
710
711 while.body:
712 invoke void @may_throw()
713 to label %while.body2 unwind label %lpadBB
714
715 while.body2:
716 %v = call i32 @getv()
717 %mul = mul i32 %v, %v2
718 invoke void @may_throw2()
719 to label %while.cond unwind label %lpadBB
720 lpadBB:
721 %.lcssa1 = phi i32 [ 0, %while.body ], [ %mul, %while.body2 ]
722 landingpad { i8*, i32 }
723 catch i8* null
724 br label %lpadBBSucc1
725
726 lpadBBSucc1:
727 ret void
728
729 try.cont:
730 ret void
731 }
732
672733 declare void @may_throw()
673734 declare void @may_throw2()
674735 declare i32 @__CxxFrameHandler3(...)