LLVM API Documentation

DbgValueHistoryCalculator.cpp
Go to the documentation of this file.
00001 //===-- llvm/CodeGen/AsmPrinter/DbgValueHistoryCalculator.cpp -------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "DbgValueHistoryCalculator.h"
00011 #include "llvm/ADT/SmallVector.h"
00012 #include "llvm/CodeGen/MachineBasicBlock.h"
00013 #include "llvm/CodeGen/MachineFunction.h"
00014 #include "llvm/IR/DebugInfo.h"
00015 #include "llvm/Support/Debug.h"
00016 #include "llvm/Target/TargetRegisterInfo.h"
00017 #include <algorithm>
00018 #include <map>
00019 #include <set>
00020 
00021 #define DEBUG_TYPE "dwarfdebug"
00022 
00023 namespace llvm {
00024 
00025 // \brief If @MI is a DBG_VALUE with debug value described by a
00026 // defined register, returns the number of this register.
00027 // In the other case, returns 0.
00028 static unsigned isDescribedByReg(const MachineInstr &MI) {
00029   assert(MI.isDebugValue());
00030   assert(MI.getNumOperands() == 3);
00031   // If location of variable is described using a register (directly or
00032   // indirecltly), this register is always a first operand.
00033   return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0;
00034 }
00035 
00036 void DbgValueHistoryMap::startInstrRange(const MDNode *Var,
00037                                          const MachineInstr &MI) {
00038   // Instruction range should start with a DBG_VALUE instruction for the
00039   // variable.
00040   assert(MI.isDebugValue() && getEntireVariable(MI.getDebugVariable()) == Var);
00041   auto &Ranges = VarInstrRanges[Var];
00042   if (!Ranges.empty() && Ranges.back().second == nullptr &&
00043       Ranges.back().first->isIdenticalTo(&MI)) {
00044     DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
00045                  << "\t" << Ranges.back().first << "\t" << MI << "\n");
00046     return;
00047   }
00048   Ranges.push_back(std::make_pair(&MI, nullptr));
00049 }
00050 
00051 void DbgValueHistoryMap::endInstrRange(const MDNode *Var,
00052                                        const MachineInstr &MI) {
00053   auto &Ranges = VarInstrRanges[Var];
00054   // Verify that the current instruction range is not yet closed.
00055   assert(!Ranges.empty() && Ranges.back().second == nullptr);
00056   // For now, instruction ranges are not allowed to cross basic block
00057   // boundaries.
00058   assert(Ranges.back().first->getParent() == MI.getParent());
00059   Ranges.back().second = &MI;
00060 }
00061 
00062 unsigned DbgValueHistoryMap::getRegisterForVar(const MDNode *Var) const {
00063   const auto &I = VarInstrRanges.find(Var);
00064   if (I == VarInstrRanges.end())
00065     return 0;
00066   const auto &Ranges = I->second;
00067   if (Ranges.empty() || Ranges.back().second != nullptr)
00068     return 0;
00069   return isDescribedByReg(*Ranges.back().first);
00070 }
00071 
00072 namespace {
00073 // Maps physreg numbers to the variables they describe.
00074 typedef std::map<unsigned, SmallVector<const MDNode *, 1>> RegDescribedVarsMap;
00075 }
00076 
00077 // \brief Claim that @Var is not described by @RegNo anymore.
00078 static void dropRegDescribedVar(RegDescribedVarsMap &RegVars,
00079                                 unsigned RegNo, const MDNode *Var) {
00080   const auto &I = RegVars.find(RegNo);
00081   assert(RegNo != 0U && I != RegVars.end());
00082   auto &VarSet = I->second;
00083   const auto &VarPos = std::find(VarSet.begin(), VarSet.end(), Var);
00084   assert(VarPos != VarSet.end());
00085   VarSet.erase(VarPos);
00086   // Don't keep empty sets in a map to keep it as small as possible.
00087   if (VarSet.empty())
00088     RegVars.erase(I);
00089 }
00090 
00091 // \brief Claim that @Var is now described by @RegNo.
00092 static void addRegDescribedVar(RegDescribedVarsMap &RegVars,
00093                                unsigned RegNo, const MDNode *Var) {
00094   assert(RegNo != 0U);
00095   auto &VarSet = RegVars[RegNo];
00096   assert(std::find(VarSet.begin(), VarSet.end(), Var) == VarSet.end());
00097   VarSet.push_back(Var);
00098 }
00099 
00100 // \brief Terminate the location range for variables described by register
00101 // @RegNo by inserting @ClobberingInstr to their history.
00102 static void clobberRegisterUses(RegDescribedVarsMap &RegVars, unsigned RegNo,
00103                                 DbgValueHistoryMap &HistMap,
00104                                 const MachineInstr &ClobberingInstr) {
00105   const auto &I = RegVars.find(RegNo);
00106   if (I == RegVars.end())
00107     return;
00108   // Iterate over all variables described by this register and add this
00109   // instruction to their history, clobbering it.
00110   for (const auto &Var : I->second)
00111     HistMap.endInstrRange(Var, ClobberingInstr);
00112   RegVars.erase(I);
00113 }
00114 
00115 // \brief Collect all registers clobbered by @MI and apply the functor
00116 // @Func to their RegNo.
00117 // @Func should be a functor with a void(unsigned) signature. We're
00118 // not using std::function here for performance reasons. It has a
00119 // small but measurable impact. By using a functor instead of a
00120 // std::set& here, we can avoid the overhead of constructing
00121 // temporaries in calculateDbgValueHistory, which has a significant
00122 // performance impact.
00123 template<typename Callable>
00124 static void applyToClobberedRegisters(const MachineInstr &MI,
00125                                       const TargetRegisterInfo *TRI,
00126                                       Callable Func) {
00127   for (const MachineOperand &MO : MI.operands()) {
00128     if (!MO.isReg() || !MO.isDef() || !MO.getReg())
00129       continue;
00130     for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid(); ++AI)
00131       Func(*AI);
00132   }
00133 }
00134 
00135 // \brief Returns the first instruction in @MBB which corresponds to
00136 // the function epilogue, or nullptr if @MBB doesn't contain an epilogue.
00137 static const MachineInstr *getFirstEpilogueInst(const MachineBasicBlock &MBB) {
00138   auto LastMI = MBB.getLastNonDebugInstr();
00139   if (LastMI == MBB.end() || !LastMI->isReturn())
00140     return nullptr;
00141   // Assume that epilogue starts with instruction having the same debug location
00142   // as the return instruction.
00143   DebugLoc LastLoc = LastMI->getDebugLoc();
00144   auto Res = LastMI;
00145   for (MachineBasicBlock::const_reverse_iterator I(std::next(LastMI)); I != MBB.rend();
00146        ++I) {
00147     if (I->getDebugLoc() != LastLoc)
00148       return Res;
00149     Res = std::prev(I.base());
00150   }
00151   // If all instructions have the same debug location, assume whole MBB is
00152   // an epilogue.
00153   return MBB.begin();
00154 }
00155 
00156 // \brief Collect registers that are modified in the function body (their
00157 // contents is changed outside of the prologue and epilogue).
00158 static void collectChangingRegs(const MachineFunction *MF,
00159                                 const TargetRegisterInfo *TRI,
00160                                 std::set<unsigned> &Regs) {
00161   for (const auto &MBB : *MF) {
00162     auto FirstEpilogueInst = getFirstEpilogueInst(MBB);
00163 
00164     for (const auto &MI : MBB) {
00165       if (&MI == FirstEpilogueInst)
00166         break;
00167       if (!MI.getFlag(MachineInstr::FrameSetup))
00168         applyToClobberedRegisters(MI, TRI, [&](unsigned r) { Regs.insert(r); });
00169     }
00170   }
00171 }
00172 
00173 void calculateDbgValueHistory(const MachineFunction *MF,
00174                               const TargetRegisterInfo *TRI,
00175                               DbgValueHistoryMap &Result) {
00176   std::set<unsigned> ChangingRegs;
00177   collectChangingRegs(MF, TRI, ChangingRegs);
00178 
00179   RegDescribedVarsMap RegVars;
00180   for (const auto &MBB : *MF) {
00181     for (const auto &MI : MBB) {
00182       if (!MI.isDebugValue()) {
00183         // Not a DBG_VALUE instruction. It may clobber registers which describe
00184         // some variables.
00185         applyToClobberedRegisters(MI, TRI, [&](unsigned RegNo) {
00186           if (ChangingRegs.count(RegNo))
00187             clobberRegisterUses(RegVars, RegNo, Result, MI);
00188         });
00189         continue;
00190       }
00191 
00192       assert(MI.getNumOperands() > 1 && "Invalid DBG_VALUE instruction!");
00193       // Use the base variable (without any DW_OP_piece expressions)
00194       // as index into History. The full variables including the
00195       // piece expressions are attached to the MI.
00196       DIVariable Var = getEntireVariable(MI.getDebugVariable());
00197 
00198       if (unsigned PrevReg = Result.getRegisterForVar(Var))
00199         dropRegDescribedVar(RegVars, PrevReg, Var);
00200 
00201       Result.startInstrRange(Var, MI);
00202 
00203       if (unsigned NewReg = isDescribedByReg(MI))
00204         addRegDescribedVar(RegVars, NewReg, Var);
00205     }
00206 
00207     // Make sure locations for register-described variables are valid only
00208     // until the end of the basic block (unless it's the last basic block, in
00209     // which case let their liveness run off to the end of the function).
00210     if (!MBB.empty() &&  &MBB != &MF->back()) {
00211       for (unsigned RegNo : ChangingRegs)
00212         clobberRegisterUses(RegVars, RegNo, Result, MBB.back());
00213     }
00214   }
00215 }
00216 
00217 }