[PATCH] D44814: [CodeGenPrepare] Split huge basic blocks for faster compilation.

Michael Zolotukhin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 22 18:51:45 PDT 2018


mzolotukhin created this revision.
mzolotukhin added a reviewer: ab.
Herald added a subscriber: hiraditya.
mzolotukhin added a reviewer: davide.

Many passes stuggle with huge basic blocks. While we definitely should address
the issues in the passes in the first place, often we discover such places too
late when the compiler 'hangs'. This patch adds a 'fuse' to prevent this from
happening by splitting huge basic blocks. It shouldn't happen in usual
scenarios, but should help us in corner cases that occur here and there. This
does not apply for -O3, as at -O3 we're usually willing to sacrifice compile
time for the sake of any extra performance.


Repository:
  rL LLVM

https://reviews.llvm.org/D44814

Files:
  llvm/lib/CodeGen/CodeGenPrepare.cpp


Index: llvm/lib/CodeGen/CodeGenPrepare.cpp
===================================================================
--- llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -215,6 +215,10 @@
     "addr-sink-combine-scaled-reg", cl::Hidden, cl::init(true),
     cl::desc("Allow combining of ScaledReg field in Address sinking."));
 
+static cl::opt<unsigned> BasicBlockMaxSize(
+    "bb-max-size", cl::Hidden, cl::init(1000),
+    cl::desc("Split basic blocks bigger than this size when compiling for Os"));
+
 namespace {
 
 using SetOfInstrs = SmallPtrSet<Instruction *, 16>;
@@ -472,6 +476,31 @@
     EverMadeChange |= MadeChange;
   }
 
+  // Split big basic blocks. We're doing it to save compile time, which is not
+  // a concern on O3.
+  if (!OptSize && BasicBlockMaxSize != 0) {
+    SmallVector<BasicBlock*, 4> HugeBlocksToSplit;
+    for (BasicBlock &BB : F)
+      if (BB.size() > BasicBlockMaxSize)
+        HugeBlocksToSplit.push_back(&BB);
+    MadeChange = false;
+
+    for (BasicBlock *BB : HugeBlocksToSplit) {
+      while (BB->size() > BasicBlockMaxSize) {
+        unsigned n = 0;
+        for (Instruction &I : *BB) {
+          if (n == BasicBlockMaxSize) {
+            BB = SplitBlock(BB, &I);
+            MadeChange = true;
+            break;
+          }
+          n++;
+        }
+      }
+    }
+    EverMadeChange |= MadeChange;
+  }
+
   if (!DisableGCOpts) {
     SmallVector<Instruction *, 2> Statepoints;
     for (BasicBlock &BB : F)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D44814.139549.patch
Type: text/x-patch
Size: 1494 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180323/9ae2133f/attachment.bin>


More information about the llvm-commits mailing list