Index: llvm/lib/CodeGen/CodeGenPrepare.cpp =================================================================== --- llvm/lib/CodeGen/CodeGenPrepare.cpp +++ llvm/lib/CodeGen/CodeGenPrepare.cpp @@ -339,6 +339,10 @@ /// lazily and update it when required. std::unique_ptr DT; + /// If there are too many BBs, we need to consider the build time. + bool BuildTimeLimit = false; + SmallDenseSet VisitedBBs; + public: static char ID; // Pass identification, replacement for typeid @@ -474,6 +478,7 @@ // Clear per function information. InsertedInsts.clear(); PromotedInsts.clear(); + VisitedBBs.clear(); TM = &getAnalysis().getTM(); SubtargetInfo = TM->getSubtargetImpl(F); @@ -545,11 +550,22 @@ EverMadeChange |= SplitIndirectBrCriticalEdges(F, /*IgnoreBlocksWithoutPHI=*/true); + // Refine Me: We may use better condition to consider the build time. + BuildTimeLimit = F.size() > 10000; + bool MadeChange = true; while (MadeChange) { MadeChange = false; DT.reset(); for (BasicBlock &BB : llvm::make_early_inc_range(F)) { + // This is very time consuming when there are big number BBs. + // We encounter somes big tests (> 20000 BBs) will cost 30 mins here. + // Though some optimizations may affect the dominate relation, usually + // split or merge BBs, it just fine tuning in the whole tree. + // Most optimized BBs will not be optimized again. + if (BuildTimeLimit && VisitedBBs.count(&BB)) + continue; + bool ModifiedDTOnIteration = false; MadeChange |= optimizeBlock(BB, ModifiedDTOnIteration); @@ -8061,6 +8077,9 @@ return true; } + if (BuildTimeLimit) + VisitedBBs.insert(&BB); + bool MadeBitReverse = true; while (MadeBitReverse) { MadeBitReverse = false;