From 8ea5b7120f539067383c02cf47777c81f664508f Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Mon, 6 Oct 2014 15:31:04 +0000 Subject: [PATCH] DbgValueHistoryCalculator: Store modified registers in a BitVector instead of std::set. And iterate over the smaller map instead of the larger set first. Reduces the time spent in calculateDbgValueHistory by 30-40%. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@219123 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../AsmPrinter/DbgValueHistoryCalculator.cpp | 56 +++++++++++-------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/lib/CodeGen/AsmPrinter/DbgValueHistoryCalculator.cpp b/lib/CodeGen/AsmPrinter/DbgValueHistoryCalculator.cpp index a258d621a6a..0c2a5e551ad 100644 --- a/lib/CodeGen/AsmPrinter/DbgValueHistoryCalculator.cpp +++ b/lib/CodeGen/AsmPrinter/DbgValueHistoryCalculator.cpp @@ -8,6 +8,7 @@ //===----------------------------------------------------------------------===// #include "DbgValueHistoryCalculator.h" +#include "llvm/ADT/BitVector.h" #include "llvm/ADT/SmallVector.h" #include "llvm/CodeGen/MachineBasicBlock.h" #include "llvm/CodeGen/MachineFunction.h" @@ -16,12 +17,10 @@ #include "llvm/Target/TargetRegisterInfo.h" #include #include -#include +using namespace llvm; #define DEBUG_TYPE "dwarfdebug" -namespace llvm { - // \brief If @MI is a DBG_VALUE with debug value described by a // defined register, returns the number of this register. // In the other case, returns 0. @@ -97,6 +96,19 @@ static void addRegDescribedVar(RegDescribedVarsMap &RegVars, VarSet.push_back(Var); } +// \brief Terminate the location range for variables described by register at +// @I by inserting @ClobberingInstr to their history. +static void clobberRegisterUses(RegDescribedVarsMap &RegVars, + RegDescribedVarsMap::iterator I, + DbgValueHistoryMap &HistMap, + const MachineInstr &ClobberingInstr) { + // Iterate over all variables described by this register and add this + // instruction to their history, clobbering it. + for (const auto &Var : I->second) + HistMap.endInstrRange(Var, ClobberingInstr); + RegVars.erase(I); +} + // \brief Terminate the location range for variables described by register // @RegNo by inserting @ClobberingInstr to their history. static void clobberRegisterUses(RegDescribedVarsMap &RegVars, unsigned RegNo, @@ -105,11 +117,7 @@ static void clobberRegisterUses(RegDescribedVarsMap &RegVars, unsigned RegNo, const auto &I = RegVars.find(RegNo); if (I == RegVars.end()) return; - // Iterate over all variables described by this register and add this - // instruction to their history, clobbering it. - for (const auto &Var : I->second) - HistMap.endInstrRange(Var, ClobberingInstr); - RegVars.erase(I); + clobberRegisterUses(RegVars, I, HistMap, ClobberingInstr); } // \brief Collect all registers clobbered by @MI and apply the functor @@ -142,11 +150,12 @@ static const MachineInstr *getFirstEpilogueInst(const MachineBasicBlock &MBB) { // as the return instruction. DebugLoc LastLoc = LastMI->getDebugLoc(); auto Res = LastMI; - for (MachineBasicBlock::const_reverse_iterator I(std::next(LastMI)); I != MBB.rend(); - ++I) { + for (MachineBasicBlock::const_reverse_iterator I(std::next(LastMI)), + E = MBB.rend(); + I != E; ++I) { if (I->getDebugLoc() != LastLoc) return Res; - Res = std::prev(I.base()); + Res = &*I; } // If all instructions have the same debug location, assume whole MBB is // an epilogue. @@ -157,7 +166,7 @@ static const MachineInstr *getFirstEpilogueInst(const MachineBasicBlock &MBB) { // contents is changed outside of the prologue and epilogue). static void collectChangingRegs(const MachineFunction *MF, const TargetRegisterInfo *TRI, - std::set &Regs) { + BitVector &Regs) { for (const auto &MBB : *MF) { auto FirstEpilogueInst = getFirstEpilogueInst(MBB); @@ -165,15 +174,15 @@ static void collectChangingRegs(const MachineFunction *MF, if (&MI == FirstEpilogueInst) break; if (!MI.getFlag(MachineInstr::FrameSetup)) - applyToClobberedRegisters(MI, TRI, [&](unsigned r) { Regs.insert(r); }); + applyToClobberedRegisters(MI, TRI, [&](unsigned r) { Regs.set(r); }); } } } -void calculateDbgValueHistory(const MachineFunction *MF, - const TargetRegisterInfo *TRI, - DbgValueHistoryMap &Result) { - std::set ChangingRegs; +void llvm::calculateDbgValueHistory(const MachineFunction *MF, + const TargetRegisterInfo *TRI, + DbgValueHistoryMap &Result) { + BitVector ChangingRegs(TRI->getNumRegs()); collectChangingRegs(MF, TRI, ChangingRegs); RegDescribedVarsMap RegVars; @@ -183,7 +192,7 @@ void calculateDbgValueHistory(const MachineFunction *MF, // Not a DBG_VALUE instruction. It may clobber registers which describe // some variables. applyToClobberedRegisters(MI, TRI, [&](unsigned RegNo) { - if (ChangingRegs.count(RegNo)) + if (ChangingRegs.test(RegNo)) clobberRegisterUses(RegVars, RegNo, Result, MI); }); continue; @@ -207,11 +216,12 @@ void calculateDbgValueHistory(const MachineFunction *MF, // Make sure locations for register-described variables are valid only // until the end of the basic block (unless it's the last basic block, in // which case let their liveness run off to the end of the function). - if (!MBB.empty() && &MBB != &MF->back()) { - for (unsigned RegNo : ChangingRegs) - clobberRegisterUses(RegVars, RegNo, Result, MBB.back()); + if (!MBB.empty() && &MBB != &MF->back()) { + for (auto I = RegVars.begin(), E = RegVars.end(); I != E;) { + auto CurElem = I++; // CurElem can be erased below. + if (ChangingRegs.test(CurElem->first)) + clobberRegisterUses(RegVars, CurElem, Result, MBB.back()); + } } } } - -}