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/IR/DebugInfo.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Target/TargetRegisterInfo.h"
21 #define DEBUG_TYPE "dwarfdebug"
25 // \brief If @MI is a DBG_VALUE with debug value described by a
26 // defined register, returns the number of this register.
27 // In the other case, returns 0.
28 static unsigned isDescribedByReg(const MachineInstr &MI) {
29 assert(MI.isDebugValue());
30 assert(MI.getNumOperands() == 3);
31 // If location of variable is described using a register (directly or
32 // indirecltly), this register is always a first operand.
33 return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0;
36 void DbgValueHistoryMap::startInstrRange(const MDNode *Var,
37 const MachineInstr &MI) {
38 // Instruction range should start with a DBG_VALUE instruction for the
40 assert(MI.isDebugValue() && getEntireVariable(MI.getDebugVariable()) == Var);
41 auto &Ranges = VarInstrRanges[Var];
42 if (!Ranges.empty() && Ranges.back().second == nullptr &&
43 Ranges.back().first->isIdenticalTo(&MI)) {
44 DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
45 << "\t" << Ranges.back().first << "\t" << MI << "\n");
48 Ranges.push_back(std::make_pair(&MI, nullptr));
51 void DbgValueHistoryMap::endInstrRange(const MDNode *Var,
52 const MachineInstr &MI) {
53 auto &Ranges = VarInstrRanges[Var];
54 // Verify that the current instruction range is not yet closed.
55 assert(!Ranges.empty() && Ranges.back().second == nullptr);
56 // For now, instruction ranges are not allowed to cross basic block
58 assert(Ranges.back().first->getParent() == MI.getParent());
59 Ranges.back().second = &MI;
62 unsigned DbgValueHistoryMap::getRegisterForVar(const MDNode *Var) const {
63 const auto &I = VarInstrRanges.find(Var);
64 if (I == VarInstrRanges.end())
66 const auto &Ranges = I->second;
67 if (Ranges.empty() || Ranges.back().second != nullptr)
69 return isDescribedByReg(*Ranges.back().first);
73 // Maps physreg numbers to the variables they describe.
74 typedef std::map<unsigned, SmallVector<const MDNode *, 1>> RegDescribedVarsMap;
77 // \brief Claim that @Var is not described by @RegNo anymore.
78 static void dropRegDescribedVar(RegDescribedVarsMap &RegVars,
79 unsigned RegNo, const MDNode *Var) {
80 const auto &I = RegVars.find(RegNo);
81 assert(RegNo != 0U && I != RegVars.end());
82 auto &VarSet = I->second;
83 const auto &VarPos = std::find(VarSet.begin(), VarSet.end(), Var);
84 assert(VarPos != VarSet.end());
86 // Don't keep empty sets in a map to keep it as small as possible.
91 // \brief Claim that @Var is now described by @RegNo.
92 static void addRegDescribedVar(RegDescribedVarsMap &RegVars,
93 unsigned RegNo, const MDNode *Var) {
95 auto &VarSet = RegVars[RegNo];
96 assert(std::find(VarSet.begin(), VarSet.end(), Var) == VarSet.end());
97 VarSet.push_back(Var);
100 // \brief Terminate the location range for variables described by register
101 // @RegNo by inserting @ClobberingInstr to their history.
102 static void clobberRegisterUses(RegDescribedVarsMap &RegVars, unsigned RegNo,
103 DbgValueHistoryMap &HistMap,
104 const MachineInstr &ClobberingInstr) {
105 const auto &I = RegVars.find(RegNo);
106 if (I == RegVars.end())
108 // Iterate over all variables described by this register and add this
109 // instruction to their history, clobbering it.
110 for (const auto &Var : I->second)
111 HistMap.endInstrRange(Var, ClobberingInstr);
115 // \brief Collect all registers clobbered by @MI and insert them to @Regs.
116 static void collectClobberedRegisters(const MachineInstr &MI,
117 const TargetRegisterInfo *TRI,
118 std::set<unsigned> &Regs) {
119 for (const MachineOperand &MO : MI.operands()) {
120 if (!MO.isReg() || !MO.isDef() || !MO.getReg())
122 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid(); ++AI)
127 // \brief Returns the first instruction in @MBB which corresponds to
128 // the function epilogue, or nullptr if @MBB doesn't contain an epilogue.
129 static const MachineInstr *getFirstEpilogueInst(const MachineBasicBlock &MBB) {
130 auto LastMI = MBB.getLastNonDebugInstr();
131 if (LastMI == MBB.end() || !LastMI->isReturn())
133 // Assume that epilogue starts with instruction having the same debug location
134 // as the return instruction.
135 DebugLoc LastLoc = LastMI->getDebugLoc();
137 for (MachineBasicBlock::const_reverse_iterator I(std::next(LastMI)); I != MBB.rend();
139 if (I->getDebugLoc() != LastLoc)
141 Res = std::prev(I.base());
143 // If all instructions have the same debug location, assume whole MBB is
148 // \brief Collect registers that are modified in the function body (their
149 // contents is changed only in the prologue and epilogue).
150 static void collectChangingRegs(const MachineFunction *MF,
151 const TargetRegisterInfo *TRI,
152 std::set<unsigned> &Regs) {
153 for (const auto &MBB : *MF) {
154 auto FirstEpilogueInst = getFirstEpilogueInst(MBB);
156 for (const auto &MI : MBB) {
157 if (&MI == FirstEpilogueInst)
159 if (!MI.getFlag(MachineInstr::FrameSetup))
160 collectClobberedRegisters(MI, TRI, Regs);
165 void calculateDbgValueHistory(const MachineFunction *MF,
166 const TargetRegisterInfo *TRI,
167 DbgValueHistoryMap &Result) {
168 std::set<unsigned> ChangingRegs;
169 collectChangingRegs(MF, TRI, ChangingRegs);
171 RegDescribedVarsMap RegVars;
172 for (const auto &MBB : *MF) {
173 for (const auto &MI : MBB) {
174 if (!MI.isDebugValue()) {
175 // Not a DBG_VALUE instruction. It may clobber registers which describe
177 std::set<unsigned> MIClobberedRegs;
178 collectClobberedRegisters(MI, TRI, MIClobberedRegs);
179 for (unsigned RegNo : MIClobberedRegs) {
180 if (ChangingRegs.count(RegNo))
181 clobberRegisterUses(RegVars, RegNo, Result, MI);
186 assert(MI.getNumOperands() > 1 && "Invalid DBG_VALUE instruction!");
187 // Use the base variable (without any DW_OP_piece expressions)
188 // as index into History. The full variables including the
189 // piece expressions are attached to the MI.
190 DIVariable Var = getEntireVariable(MI.getDebugVariable());
192 if (unsigned PrevReg = Result.getRegisterForVar(Var))
193 dropRegDescribedVar(RegVars, PrevReg, Var);
195 Result.startInstrRange(Var, MI);
197 if (unsigned NewReg = isDescribedByReg(MI))
198 addRegDescribedVar(RegVars, NewReg, Var);
201 // Make sure locations for register-described variables are valid only
202 // until the end of the basic block (unless it's the last basic block, in
203 // which case let their liveness run off to the end of the function).
204 if (!MBB.empty() && &MBB != &MF->back()) {
205 for (unsigned RegNo : ChangingRegs)
206 clobberRegisterUses(RegVars, RegNo, Result, MBB.back());