blob: f557ccddb47dce36380f60cffc7e83fca9c8cf93 [file] [log] [blame]
Alexey Samsonov414b6fb2014-04-30 21:34:11 +00001//===-- llvm/CodeGen/AsmPrinter/DbgValueHistoryCalculator.cpp -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "DbgValueHistoryCalculator.h"
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000011#include "llvm/ADT/SmallVector.h"
Alexey Samsonov414b6fb2014-04-30 21:34:11 +000012#include "llvm/CodeGen/MachineBasicBlock.h"
13#include "llvm/CodeGen/MachineFunction.h"
Adrian Prantlb1416832014-08-01 22:11:58 +000014#include "llvm/IR/DebugInfo.h"
Alexey Samsonov414b6fb2014-04-30 21:34:11 +000015#include "llvm/Support/Debug.h"
16#include "llvm/Target/TargetRegisterInfo.h"
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000017#include <algorithm>
18#include <map>
Alexey Samsonov8000e272014-06-09 21:53:47 +000019#include <set>
Alexey Samsonov414b6fb2014-04-30 21:34:11 +000020
21#define DEBUG_TYPE "dwarfdebug"
22
23namespace llvm {
24
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000025// \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.
28static 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;
34}
35
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000036void DbgValueHistoryMap::startInstrRange(const MDNode *Var,
37 const MachineInstr &MI) {
38 // Instruction range should start with a DBG_VALUE instruction for the
39 // variable.
Adrian Prantlb1416832014-08-01 22:11:58 +000040 assert(MI.isDebugValue() && getEntireVariable(MI.getDebugVariable()) == Var);
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000041 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");
46 return;
47 }
48 Ranges.push_back(std::make_pair(&MI, nullptr));
49}
50
51void 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
57 // boundaries.
58 assert(Ranges.back().first->getParent() == MI.getParent());
59 Ranges.back().second = &MI;
60}
61
62unsigned DbgValueHistoryMap::getRegisterForVar(const MDNode *Var) const {
63 const auto &I = VarInstrRanges.find(Var);
64 if (I == VarInstrRanges.end())
65 return 0;
66 const auto &Ranges = I->second;
67 if (Ranges.empty() || Ranges.back().second != nullptr)
68 return 0;
69 return isDescribedByReg(*Ranges.back().first);
70}
71
72namespace {
73// Maps physreg numbers to the variables they describe.
74typedef std::map<unsigned, SmallVector<const MDNode *, 1>> RegDescribedVarsMap;
75}
76
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000077// \brief Claim that @Var is not described by @RegNo anymore.
78static 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());
85 VarSet.erase(VarPos);
86 // Don't keep empty sets in a map to keep it as small as possible.
87 if (VarSet.empty())
88 RegVars.erase(I);
89}
90
91// \brief Claim that @Var is now described by @RegNo.
92static void addRegDescribedVar(RegDescribedVarsMap &RegVars,
93 unsigned RegNo, const MDNode *Var) {
94 assert(RegNo != 0U);
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000095 auto &VarSet = RegVars[RegNo];
96 assert(std::find(VarSet.begin(), VarSet.end(), Var) == VarSet.end());
97 VarSet.push_back(Var);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000098}
99
100// \brief Terminate the location range for variables described by register
101// @RegNo by inserting @ClobberingInstr to their history.
102static 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())
107 return;
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)
Alexey Samsonovbb2990d2014-05-27 23:09:50 +0000111 HistMap.endInstrRange(Var, ClobberingInstr);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000112 RegVars.erase(I);
113}
114
Alexey Samsonov8000e272014-06-09 21:53:47 +0000115// \brief Collect all registers clobbered by @MI and insert them to @Regs.
116static void collectClobberedRegisters(const MachineInstr &MI,
117 const TargetRegisterInfo *TRI,
118 std::set<unsigned> &Regs) {
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000119 for (const MachineOperand &MO : MI.operands()) {
120 if (!MO.isReg() || !MO.isDef() || !MO.getReg())
121 continue;
Alexey Samsonov8000e272014-06-09 21:53:47 +0000122 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid(); ++AI)
123 Regs.insert(*AI);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000124 }
125}
126
Alexey Samsonov8000e272014-06-09 21:53:47 +0000127// \brief Returns the first instruction in @MBB which corresponds to
128// the function epilogue, or nullptr if @MBB doesn't contain an epilogue.
129static const MachineInstr *getFirstEpilogueInst(const MachineBasicBlock &MBB) {
130 auto LastMI = MBB.getLastNonDebugInstr();
131 if (LastMI == MBB.end() || !LastMI->isReturn())
132 return nullptr;
133 // Assume that epilogue starts with instruction having the same debug location
134 // as the return instruction.
135 DebugLoc LastLoc = LastMI->getDebugLoc();
136 auto Res = LastMI;
137 for (MachineBasicBlock::const_reverse_iterator I(std::next(LastMI)); I != MBB.rend();
138 ++I) {
139 if (I->getDebugLoc() != LastLoc)
140 return Res;
141 Res = std::prev(I.base());
142 }
143 // If all instructions have the same debug location, assume whole MBB is
144 // an epilogue.
145 return MBB.begin();
146}
147
148// \brief Collect registers that are modified in the function body (their
149// contents is changed only in the prologue and epilogue).
150static 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);
155 bool IsInEpilogue = false;
156 for (const auto &MI : MBB) {
157 IsInEpilogue |= &MI == FirstEpilogueInst;
158 if (!MI.getFlag(MachineInstr::FrameSetup) && !IsInEpilogue)
159 collectClobberedRegisters(MI, TRI, Regs);
160 }
161 }
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000162}
163
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000164void calculateDbgValueHistory(const MachineFunction *MF,
165 const TargetRegisterInfo *TRI,
166 DbgValueHistoryMap &Result) {
Alexey Samsonov8000e272014-06-09 21:53:47 +0000167 std::set<unsigned> ChangingRegs;
168 collectChangingRegs(MF, TRI, ChangingRegs);
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000169
Alexey Samsonov8000e272014-06-09 21:53:47 +0000170 RegDescribedVarsMap RegVars;
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000171 for (const auto &MBB : *MF) {
172 for (const auto &MI : MBB) {
173 if (!MI.isDebugValue()) {
174 // Not a DBG_VALUE instruction. It may clobber registers which describe
175 // some variables.
Alexey Samsonov8000e272014-06-09 21:53:47 +0000176 std::set<unsigned> MIClobberedRegs;
177 collectClobberedRegisters(MI, TRI, MIClobberedRegs);
178 for (unsigned RegNo : MIClobberedRegs) {
179 if (ChangingRegs.count(RegNo))
180 clobberRegisterUses(RegVars, RegNo, Result, MI);
181 }
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000182 continue;
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000183 }
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000184
Alexey Samsonovf0e0cca2014-05-27 22:35:00 +0000185 assert(MI.getNumOperands() > 1 && "Invalid DBG_VALUE instruction!");
Adrian Prantlb1416832014-08-01 22:11:58 +0000186 // Use the base variable (without any DW_OP_piece expressions)
187 // as index into History. The full variables including the
188 // piece expressions are attached to the MI.
189 DIVariable Var = getEntireVariable(MI.getDebugVariable());
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000190
Alexey Samsonovbb2990d2014-05-27 23:09:50 +0000191 if (unsigned PrevReg = Result.getRegisterForVar(Var))
192 dropRegDescribedVar(RegVars, PrevReg, Var);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000193
Alexey Samsonovbb2990d2014-05-27 23:09:50 +0000194 Result.startInstrRange(Var, MI);
195
196 if (unsigned NewReg = isDescribedByReg(MI))
197 addRegDescribedVar(RegVars, NewReg, Var);
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000198 }
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000199
200 // Make sure locations for register-described variables are valid only
201 // until the end of the basic block (unless it's the last basic block, in
202 // which case let their liveness run off to the end of the function).
Alexey Samsonov8000e272014-06-09 21:53:47 +0000203 if (!MBB.empty() && &MBB != &MF->back()) {
204 for (unsigned RegNo : ChangingRegs)
205 clobberRegisterUses(RegVars, RegNo, Result, MBB.back());
206 }
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000207 }
208}
209
210}