[PATCH] D42833: [LICM] update BlockColors after splitting predecessors

Jun Bum Lim via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 1 22:50:29 PST 2018


junbuml created this revision.
junbuml added reviewers: efriedma, majnemer, alexcrichton.
Herald added a subscriber: mcrosier.

Update BlockColors after splitting predecessors. Do not allow splitting
LandingPad for sinking as well, so we can simply assign predecessor's color to
the new block.

Fixes PR36184


https://reviews.llvm.org/D42833

Files:
  lib/Transforms/Scalar/LICM.cpp


Index: lib/Transforms/Scalar/LICM.cpp
===================================================================
--- lib/Transforms/Scalar/LICM.cpp
+++ lib/Transforms/Scalar/LICM.cpp
@@ -97,7 +97,7 @@
                   const LoopSafetyInfo *SafetyInfo,
                   OptimizationRemarkEmitter *ORE);
 static bool sink(Instruction &I, LoopInfo *LI, DominatorTree *DT,
-                 const Loop *CurLoop, const LoopSafetyInfo *SafetyInfo,
+                 const Loop *CurLoop, LoopSafetyInfo *SafetyInfo,
                  OptimizationRemarkEmitter *ORE, bool FreeInLoop);
 static bool isSafeToExecuteUnconditionally(Instruction &Inst,
                                            const DominatorTree *DT,
@@ -857,8 +857,13 @@
 
 static bool canSplitPredecessors(PHINode *PN) {
   BasicBlock *BB = PN->getParent();
-  if (!BB->canSplitPredecessors())
+  // FIXME: it's not impossible to split LandingPad blocks, but it require
+  // updating BlockColors for all offspring blocks. By skipping such corner
+  // case, we can make updating BlockColors after splitting predecessor fairly
+  // simple.
+  if (BB->getFirstNonPHI()->isEHPad())
     return false;
+
   for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
     BasicBlock *BBPred = *PI;
     if (isa<IndirectBrInst>(BBPred->getTerminator()))
@@ -868,7 +873,8 @@
 }
 
 static void splitPredecessorsOfLoopExit(PHINode *PN, DominatorTree *DT,
-                                        LoopInfo *LI, const Loop *CurLoop) {
+                                        LoopInfo *LI, const Loop *CurLoop,
+                                        LoopSafetyInfo *SafetyInfo) {
 #ifndef NDEBUG
   SmallVector<BasicBlock *, 32> ExitBlocks;
   CurLoop->getUniqueExitBlocks(ExitBlocks);
@@ -910,13 +916,27 @@
   // LE:
   //   %p = phi [%p1, %LE.split], [%p2, %LE.split2]
   //
+
+  auto &BlockColors = SafetyInfo->BlockColors;
   SmallSetVector<BasicBlock *, 8> PredBBs(pred_begin(ExitBB), pred_end(ExitBB));
+
+  assert(!ExitBB->getFirstNonPHI()->isEHPad() &&
+         "Unexpected exit block split.");
+
   while (!PredBBs.empty()) {
     BasicBlock *PredBB = *PredBBs.begin();
     assert(CurLoop->contains(PredBB) &&
            "Expect all predecessors are in the loop");
-    if (PN->getBasicBlockIndex(PredBB) >= 0)
-      SplitBlockPredecessors(ExitBB, PredBB, ".split.loop.exit", DT, LI, true);
+    if (PN->getBasicBlockIndex(PredBB) >= 0) {
+      BasicBlock *NewPred = SplitBlockPredecessors(
+          ExitBB, PredBB, ".split.loop.exit", DT, LI, true);
+
+      // Sine we don't allow splitting EH-blocks, we can simply assign
+      // predecessor's color to the new block.
+      if (!BlockColors.empty())
+        BlockColors[NewPred] = BlockColors[PredBB];
+    }
+
     PredBBs.remove(PredBB);
   }
 }
@@ -927,7 +947,7 @@
 /// position, and may either delete it or move it to outside of the loop.
 ///
 static bool sink(Instruction &I, LoopInfo *LI, DominatorTree *DT,
-                 const Loop *CurLoop, const LoopSafetyInfo *SafetyInfo,
+                 const Loop *CurLoop, LoopSafetyInfo *SafetyInfo,
                  OptimizationRemarkEmitter *ORE, bool FreeInLoop) {
   DEBUG(dbgs() << "LICM sinking instruction: " << I << "\n");
   ORE->emit([&]() {
@@ -980,7 +1000,7 @@
 
     // Split predecessors of the PHI so that we can make users trivially
     // replacable.
-    splitPredecessorsOfLoopExit(PN, DT, LI, CurLoop);
+    splitPredecessorsOfLoopExit(PN, DT, LI, CurLoop, SafetyInfo);
 
     // Should rebuild the iterators, as they may be invalidated by
     // splitPredecessorsOfLoopExit().


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D42833.132537.patch
Type: text/x-patch
Size: 3595 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180202/8ae1836c/attachment.bin>


More information about the llvm-commits mailing list