[llvm] 12c8027 - [NFC][SimplifyCFG] Common code sinking: check profitability once

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 28 15:01:21 PDT 2021


Author: Roman Lebedev
Date: 2021-04-29T01:01:00+03:00
New Revision: 12c8027ce39f776a5cc1e322b0f125362384b8aa

URL: https://github.com/llvm/llvm-project/commit/12c8027ce39f776a5cc1e322b0f125362384b8aa
DIFF: https://github.com/llvm/llvm-project/commit/12c8027ce39f776a5cc1e322b0f125362384b8aa.diff

LOG: [NFC][SimplifyCFG] Common code sinking: check profitability once

We can just eagerly pre-check all the instructions that we *could*
sink that we'd actually want to sink them, clamping the number of
instructions that we'll sink to stop just before the first unprofitable one.

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 6f19453aba03..89f6273c31f1 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -2024,8 +2024,8 @@ static bool SinkCommonCodeFromPredecessors(BasicBlock *BB,
   if (ScanIdx == 0)
     return false;
 
-  bool Changed = false;
-
+  // Okay, we *could* sink last ScanIdx instructions. But how many can we
+  // actually sink before encountering instruction that is unprofitable to sink?
   auto ProfitableToSinkInstruction = [&](LockstepReverseIterator &LRI) {
     unsigned NumPHIdValues = 0;
     for (auto *I : *LRI)
@@ -2039,6 +2039,25 @@ static bool SinkCommonCodeFromPredecessors(BasicBlock *BB,
 
     return NumPHIInsts <= 1;
   };
+  LRI.reset();
+  unsigned Idx = 0;
+  while (Idx < ScanIdx) {
+    if (!ProfitableToSinkInstruction(LRI)) {
+      // Too many PHIs would be created.
+      LLVM_DEBUG(
+          dbgs() << "SINK: stopping here, too many PHIs would be created!\n");
+      break;
+    }
+    --LRI;
+    ++Idx;
+  }
+  ScanIdx = Idx;
+
+  // If no instructions can be sunk, early-return.
+  if (ScanIdx == 0)
+    return false;
+
+  bool Changed = false;
 
   if (Cond) {
     // It is always legal to sink common instructions from unconditional
@@ -2049,7 +2068,7 @@ static bool SinkCommonCodeFromPredecessors(BasicBlock *BB,
     LRI.reset();
     unsigned Idx = 0;
     bool Profitable = false;
-    while (ProfitableToSinkInstruction(LRI) && Idx < ScanIdx) {
+    while (Idx < ScanIdx) {
       if (!isSafeToSpeculativelyExecute((*LRI)[0])) {
         Profitable = true;
         break;
@@ -2090,12 +2109,6 @@ static bool SinkCommonCodeFromPredecessors(BasicBlock *BB,
     // Because we've sunk every instruction in turn, the current instruction to
     // sink is always at index 0.
     LRI.reset();
-    if (!ProfitableToSinkInstruction(LRI)) {
-      // Too many PHIs would be created.
-      LLVM_DEBUG(
-          dbgs() << "SINK: stopping here, too many PHIs would be created!\n");
-      break;
-    }
 
     if (!sinkLastInstruction(UnconditionalPreds)) {
       LLVM_DEBUG(


        


More information about the llvm-commits mailing list