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/CodeGen/MachineBasicBlock.h"
12 #include "llvm/CodeGen/MachineFunction.h"
13 #include "llvm/Support/Debug.h"
14 #include "llvm/Target/TargetRegisterInfo.h"
16 #define DEBUG_TYPE "dwarfdebug"
20 // Return true if debug value, encoded by DBG_VALUE instruction, is in a
22 static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
23 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
24 return MI->getNumOperands() == 3 && MI->getOperand(0).isReg() &&
25 MI->getOperand(0).getReg() &&
26 (MI->getOperand(1).isImm() ||
27 (MI->getOperand(1).isReg() && MI->getOperand(1).getReg() == 0U));
30 void calculateDbgValueHistory(const MachineFunction *MF,
31 const TargetRegisterInfo *TRI,
32 DbgValueHistoryMap &Result) {
33 // LiveUserVar - Map physreg numbers to the MDNode they contain.
34 std::vector<const MDNode *> LiveUserVar(TRI->getNumRegs());
36 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E;
38 bool AtBlockEntry = true;
39 for (const auto &MI : *I) {
40 if (MI.isDebugValue()) {
41 assert(MI.getNumOperands() > 1 && "Invalid machine instruction!");
43 // Keep track of user variables.
44 const MDNode *Var = MI.getDebugVariable();
46 // Variable is in a register, we need to check for clobbers.
47 if (isDbgValueInDefinedReg(&MI))
48 LiveUserVar[MI.getOperand(0).getReg()] = Var;
50 // Check the history of this variable.
51 SmallVectorImpl<const MachineInstr *> &History = Result[Var];
52 if (!History.empty()) {
53 // We have seen this variable before. Try to coalesce DBG_VALUEs.
54 const MachineInstr *Prev = History.back();
55 if (Prev->isDebugValue()) {
56 // Coalesce identical entries at the end of History.
57 if (History.size() >= 2 &&
58 Prev->isIdenticalTo(History[History.size() - 2])) {
59 DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
60 << "\t" << *Prev << "\t"
61 << *History[History.size() - 2] << "\n");
65 // Terminate old register assignments that don't reach MI;
66 MachineFunction::const_iterator PrevMBB = Prev->getParent();
67 if (PrevMBB != I && (!AtBlockEntry || std::next(PrevMBB) != I) &&
68 isDbgValueInDefinedReg(Prev)) {
69 // Previous register assignment needs to terminate at the end of
71 MachineBasicBlock::const_iterator LastMI =
72 PrevMBB->getLastNonDebugInstr();
73 if (LastMI == PrevMBB->end()) {
74 // Drop DBG_VALUE for empty range.
75 DEBUG(dbgs() << "Dropping DBG_VALUE for empty range:\n"
76 << "\t" << *Prev << "\n");
78 } else if (std::next(PrevMBB) != PrevMBB->getParent()->end())
79 // Terminate after LastMI.
80 History.push_back(LastMI);
84 History.push_back(&MI);
86 // Not a DBG_VALUE instruction.
90 // Check if the instruction clobbers any registers with debug vars.
91 for (const MachineOperand &MO : MI.operands()) {
92 if (!MO.isReg() || !MO.isDef() || !MO.getReg())
94 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid();
97 const MDNode *Var = LiveUserVar[Reg];
100 // Reg is now clobbered.
101 LiveUserVar[Reg] = nullptr;
103 // Was MD last defined by a DBG_VALUE referring to Reg?
104 auto HistI = Result.find(Var);
105 if (HistI == Result.end())
107 SmallVectorImpl<const MachineInstr *> &History = HistI->second;
110 const MachineInstr *Prev = History.back();
111 // Sanity-check: Register assignments are terminated at the end of
113 if (!Prev->isDebugValue() || Prev->getParent() != MI.getParent())
115 // Is the variable still in Reg?
116 if (!isDbgValueInDefinedReg(Prev) ||
117 Prev->getOperand(0).getReg() != Reg)
119 // Var is clobbered. Make sure the next instruction gets a label.
120 History.push_back(&MI);
127 // Make sure the final register assignments are terminated.
128 for (auto &I : Result) {
129 SmallVectorImpl<const MachineInstr *> &History = I.second;
133 const MachineInstr *Prev = History.back();
134 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
135 const MachineBasicBlock *PrevMBB = Prev->getParent();
136 MachineBasicBlock::const_iterator LastMI =
137 PrevMBB->getLastNonDebugInstr();
138 if (LastMI == PrevMBB->end())
139 // Drop DBG_VALUE for empty range.
141 else if (PrevMBB != &PrevMBB->getParent()->back()) {
142 // Terminate after LastMI.
143 History.push_back(LastMI);