1 //===-- llvm/CodeGen/AsmPrinter/DbgValueHistoryCalculator.cpp -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "DbgValueHistoryCalculator.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/CodeGen/MachineBasicBlock.h"
13 #include "llvm/CodeGen/MachineFunction.h"
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Target/TargetRegisterInfo.h"
19 #define DEBUG_TYPE "dwarfdebug"
24 // Maps physreg numbers to the variables they describe.
25 typedef std::map<unsigned, SmallVector<const MDNode *, 1>> RegDescribedVarsMap;
28 // \brief If @MI is a DBG_VALUE with debug value described by a
29 // defined register, returns the number of this register.
30 // In the other case, returns 0.
31 static unsigned isDescribedByReg(const MachineInstr &MI) {
32 assert(MI.isDebugValue());
33 assert(MI.getNumOperands() == 3);
34 // If location of variable is described using a register (directly or
35 // indirecltly), this register is always a first operand.
36 return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0;
39 // \brief Claim that @Var is not described by @RegNo anymore.
40 static void dropRegDescribedVar(RegDescribedVarsMap &RegVars,
41 unsigned RegNo, const MDNode *Var) {
42 const auto &I = RegVars.find(RegNo);
43 assert(RegNo != 0U && I != RegVars.end());
44 auto &VarSet = I->second;
45 const auto &VarPos = std::find(VarSet.begin(), VarSet.end(), Var);
46 assert(VarPos != VarSet.end());
48 // Don't keep empty sets in a map to keep it as small as possible.
53 // \brief Claim that @Var is now described by @RegNo.
54 static void addRegDescribedVar(RegDescribedVarsMap &RegVars,
55 unsigned RegNo, const MDNode *Var) {
57 RegVars[RegNo].push_back(Var);
60 static void clobberVariableLocation(SmallVectorImpl<const MachineInstr *> &VarHistory,
61 const MachineInstr &ClobberingInstr) {
62 assert(!VarHistory.empty());
63 // DBG_VALUE we're clobbering should belong to the same MBB.
64 assert(VarHistory.back()->isDebugValue());
65 assert(VarHistory.back()->getParent() == ClobberingInstr.getParent());
66 VarHistory.push_back(&ClobberingInstr);
69 // \brief Terminate the location range for variables described by register
70 // @RegNo by inserting @ClobberingInstr to their history.
71 static void clobberRegisterUses(RegDescribedVarsMap &RegVars, unsigned RegNo,
72 DbgValueHistoryMap &HistMap,
73 const MachineInstr &ClobberingInstr) {
74 const auto &I = RegVars.find(RegNo);
75 if (I == RegVars.end())
77 // Iterate over all variables described by this register and add this
78 // instruction to their history, clobbering it.
79 for (const auto &Var : I->second)
80 clobberVariableLocation(HistMap[Var], ClobberingInstr);
84 // \brief Terminate the location range for all variables, described by registers
86 static void clobberRegisterUses(RegDescribedVarsMap &RegVars,
87 const MachineInstr &MI,
88 const TargetRegisterInfo *TRI,
89 DbgValueHistoryMap &HistMap) {
90 for (const MachineOperand &MO : MI.operands()) {
91 if (!MO.isReg() || !MO.isDef() || !MO.getReg())
93 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid();
96 clobberRegisterUses(RegVars, RegNo, HistMap, MI);
101 // \brief Terminate the location range for all register-described variables
102 // by inserting @ClobberingInstr to their history.
103 static void clobberAllRegistersUses(RegDescribedVarsMap &RegVars,
104 DbgValueHistoryMap &HistMap,
105 const MachineInstr &ClobberingInstr) {
106 for (const auto &I : RegVars)
107 for (const auto &Var : I.second)
108 clobberVariableLocation(HistMap[Var], ClobberingInstr);
112 // \brief Update the register that describes location of @Var in @RegVars map.
114 updateRegForVariable(RegDescribedVarsMap &RegVars, const MDNode *Var,
115 const SmallVectorImpl<const MachineInstr *> &VarHistory,
116 const MachineInstr &MI) {
117 if (!VarHistory.empty()) {
118 const MachineInstr &Prev = *VarHistory.back();
119 // Check if Var is currently described by a register by instruction in the
121 if (Prev.isDebugValue() && Prev.getDebugVariable() == Var &&
122 Prev.getParent() == MI.getParent()) {
123 if (unsigned PrevReg = isDescribedByReg(Prev))
124 dropRegDescribedVar(RegVars, PrevReg, Var);
128 assert(MI.getDebugVariable() == Var);
129 if (unsigned MIReg = isDescribedByReg(MI))
130 addRegDescribedVar(RegVars, MIReg, Var);
133 void calculateDbgValueHistory(const MachineFunction *MF,
134 const TargetRegisterInfo *TRI,
135 DbgValueHistoryMap &Result) {
136 RegDescribedVarsMap RegVars;
138 for (const auto &MBB : *MF) {
139 for (const auto &MI : MBB) {
140 if (!MI.isDebugValue()) {
141 // Not a DBG_VALUE instruction. It may clobber registers which describe
143 clobberRegisterUses(RegVars, MI, TRI, Result);
147 assert(MI.getNumOperands() > 1 && "Invalid DBG_VALUE instruction!");
148 const MDNode *Var = MI.getDebugVariable();
149 auto &History = Result[Var];
151 if (!History.empty() && History.back()->isIdenticalTo(&MI)) {
152 DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
153 << "\t" << History.back() << "\t" << MI << "\n");
157 updateRegForVariable(RegVars, Var, History, MI);
158 History.push_back(&MI);
161 // Make sure locations for register-described variables are valid only
162 // until the end of the basic block (unless it's the last basic block, in
163 // which case let their liveness run off to the end of the function).
164 if (!MBB.empty() && &MBB != &MF->back())
165 clobberAllRegistersUses(RegVars, Result, MBB.back());