From 59365f33e27b24b6ecc971c53889c8ac3b34b15d Mon Sep 17 00:00:00 2001 From: Pengxuan Zheng Date: Thu, 15 Sep 2022 17:52:46 -0700 Subject: [PATCH] [MachineCSE] Add a threshold to avoid spending too much time in isProfitableToCSE Currently, it can become extremely costly to compute MayIncreasePressure if the size of CSUses turns out to be very large. In that case, it's no longer cost effective to keep computing MayIncreasePressure. Therefore, to limit the amount of time spent in isProfitableToCSE, we simply conservatively assume MayIncreasePressure if the size of CSUses is too large. This can reduce overall compile time by 30% for some benchmarks. Reviewed By: arsenm Differential Revision: https://reviews.llvm.org/D134003 --- llvm/lib/CodeGen/MachineCSE.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/llvm/lib/CodeGen/MachineCSE.cpp b/llvm/lib/CodeGen/MachineCSE.cpp index da2fd66..2de5879 100644 --- a/llvm/lib/CodeGen/MachineCSE.cpp +++ b/llvm/lib/CodeGen/MachineCSE.cpp @@ -60,6 +60,11 @@ STATISTIC(NumCrossBBCSEs, "Number of cross-MBB physreg referencing CS eliminated"); STATISTIC(NumCommutes, "Number of copies coalesced after commuting"); +// Threshold to avoid excessive cost to compute isProfitableToCSE. +static cl::opt + CSUsesThreshold("csuses-threshold", cl::Hidden, cl::init(1024), + cl::desc("Threshold for the size of CSUses")); + namespace { class MachineCSE : public MachineFunctionPass { @@ -443,15 +448,23 @@ bool MachineCSE::isProfitableToCSE(Register CSReg, Register Reg, if (Register::isVirtualRegister(CSReg) && Register::isVirtualRegister(Reg)) { MayIncreasePressure = false; SmallPtrSet CSUses; + int NumOfUses = 0; for (MachineInstr &MI : MRI->use_nodbg_instructions(CSReg)) { CSUses.insert(&MI); - } - for (MachineInstr &MI : MRI->use_nodbg_instructions(Reg)) { - if (!CSUses.count(&MI)) { + // Too costly to compute if NumOfUses is very large. Conservatively assume + // MayIncreasePressure to avoid spending too much time here. + if (++NumOfUses > CSUsesThreshold) { MayIncreasePressure = true; break; } } + if (!MayIncreasePressure) + for (MachineInstr &MI : MRI->use_nodbg_instructions(Reg)) { + if (!CSUses.count(&MI)) { + MayIncreasePressure = true; + break; + } + } } if (!MayIncreasePressure) return true; -- 2.7.4