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 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; @@ -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 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 Statepoints; for (BasicBlock &BB : F)