blob: 0c2a5e551ad07109cd8739bd2c4f3f34146bcfa2 [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"
Benjamin Kramer6bf8af52014-10-06 15:31:04 +000011#include "llvm/ADT/BitVector.h"
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000012#include "llvm/ADT/SmallVector.h"
Alexey Samsonov414b6fb2014-04-30 21:34:11 +000013#include "llvm/CodeGen/MachineBasicBlock.h"
14#include "llvm/CodeGen/MachineFunction.h"
Adrian Prantlb1416832014-08-01 22:11:58 +000015#include "llvm/IR/DebugInfo.h"
Alexey Samsonov414b6fb2014-04-30 21:34:11 +000016#include "llvm/Support/Debug.h"
17#include "llvm/Target/TargetRegisterInfo.h"
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000018#include <algorithm>
19#include <map>
Benjamin Kramer6bf8af52014-10-06 15:31:04 +000020using namespace llvm;
Alexey Samsonov414b6fb2014-04-30 21:34:11 +000021
22#define DEBUG_TYPE "dwarfdebug"
23
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000024// \brief If @MI is a DBG_VALUE with debug value described by a
25// defined register, returns the number of this register.
26// In the other case, returns 0.
27static unsigned isDescribedByReg(const MachineInstr &MI) {
28 assert(MI.isDebugValue());
Adrian Prantl87b7eb92014-10-01 18:55:02 +000029 assert(MI.getNumOperands() == 4);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000030 // If location of variable is described using a register (directly or
31 // indirecltly), this register is always a first operand.
32 return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0;
33}
34
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000035void DbgValueHistoryMap::startInstrRange(const MDNode *Var,
36 const MachineInstr &MI) {
37 // Instruction range should start with a DBG_VALUE instruction for the
38 // variable.
Adrian Prantl87b7eb92014-10-01 18:55:02 +000039 assert(MI.isDebugValue() && "not a DBG_VALUE");
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000040 auto &Ranges = VarInstrRanges[Var];
41 if (!Ranges.empty() && Ranges.back().second == nullptr &&
42 Ranges.back().first->isIdenticalTo(&MI)) {
43 DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
44 << "\t" << Ranges.back().first << "\t" << MI << "\n");
45 return;
46 }
47 Ranges.push_back(std::make_pair(&MI, nullptr));
48}
49
50void DbgValueHistoryMap::endInstrRange(const MDNode *Var,
51 const MachineInstr &MI) {
52 auto &Ranges = VarInstrRanges[Var];
53 // Verify that the current instruction range is not yet closed.
54 assert(!Ranges.empty() && Ranges.back().second == nullptr);
55 // For now, instruction ranges are not allowed to cross basic block
56 // boundaries.
57 assert(Ranges.back().first->getParent() == MI.getParent());
58 Ranges.back().second = &MI;
59}
60
61unsigned DbgValueHistoryMap::getRegisterForVar(const MDNode *Var) const {
62 const auto &I = VarInstrRanges.find(Var);
63 if (I == VarInstrRanges.end())
64 return 0;
65 const auto &Ranges = I->second;
66 if (Ranges.empty() || Ranges.back().second != nullptr)
67 return 0;
68 return isDescribedByReg(*Ranges.back().first);
69}
70
71namespace {
72// Maps physreg numbers to the variables they describe.
73typedef std::map<unsigned, SmallVector<const MDNode *, 1>> RegDescribedVarsMap;
74}
75
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000076// \brief Claim that @Var is not described by @RegNo anymore.
77static void dropRegDescribedVar(RegDescribedVarsMap &RegVars,
78 unsigned RegNo, const MDNode *Var) {
79 const auto &I = RegVars.find(RegNo);
80 assert(RegNo != 0U && I != RegVars.end());
81 auto &VarSet = I->second;
82 const auto &VarPos = std::find(VarSet.begin(), VarSet.end(), Var);
83 assert(VarPos != VarSet.end());
84 VarSet.erase(VarPos);
85 // Don't keep empty sets in a map to keep it as small as possible.
86 if (VarSet.empty())
87 RegVars.erase(I);
88}
89
90// \brief Claim that @Var is now described by @RegNo.
91static void addRegDescribedVar(RegDescribedVarsMap &RegVars,
92 unsigned RegNo, const MDNode *Var) {
93 assert(RegNo != 0U);
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000094 auto &VarSet = RegVars[RegNo];
95 assert(std::find(VarSet.begin(), VarSet.end(), Var) == VarSet.end());
96 VarSet.push_back(Var);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000097}
98
Benjamin Kramer6bf8af52014-10-06 15:31:04 +000099// \brief Terminate the location range for variables described by register at
100// @I by inserting @ClobberingInstr to their history.
101static void clobberRegisterUses(RegDescribedVarsMap &RegVars,
102 RegDescribedVarsMap::iterator I,
103 DbgValueHistoryMap &HistMap,
104 const MachineInstr &ClobberingInstr) {
105 // Iterate over all variables described by this register and add this
106 // instruction to their history, clobbering it.
107 for (const auto &Var : I->second)
108 HistMap.endInstrRange(Var, ClobberingInstr);
109 RegVars.erase(I);
110}
111
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000112// \brief Terminate the location range for variables described by register
113// @RegNo by inserting @ClobberingInstr to their history.
114static void clobberRegisterUses(RegDescribedVarsMap &RegVars, unsigned RegNo,
115 DbgValueHistoryMap &HistMap,
116 const MachineInstr &ClobberingInstr) {
117 const auto &I = RegVars.find(RegNo);
118 if (I == RegVars.end())
119 return;
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000120 clobberRegisterUses(RegVars, I, HistMap, ClobberingInstr);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000121}
122
Adrian Prantl364d1312014-08-06 18:41:24 +0000123// \brief Collect all registers clobbered by @MI and apply the functor
124// @Func to their RegNo.
125// @Func should be a functor with a void(unsigned) signature. We're
126// not using std::function here for performance reasons. It has a
127// small but measurable impact. By using a functor instead of a
128// std::set& here, we can avoid the overhead of constructing
129// temporaries in calculateDbgValueHistory, which has a significant
130// performance impact.
131template<typename Callable>
132static void applyToClobberedRegisters(const MachineInstr &MI,
Alexey Samsonov8000e272014-06-09 21:53:47 +0000133 const TargetRegisterInfo *TRI,
Adrian Prantl364d1312014-08-06 18:41:24 +0000134 Callable Func) {
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000135 for (const MachineOperand &MO : MI.operands()) {
136 if (!MO.isReg() || !MO.isDef() || !MO.getReg())
137 continue;
Alexey Samsonov8000e272014-06-09 21:53:47 +0000138 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid(); ++AI)
Adrian Prantl364d1312014-08-06 18:41:24 +0000139 Func(*AI);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000140 }
141}
142
Alexey Samsonov8000e272014-06-09 21:53:47 +0000143// \brief Returns the first instruction in @MBB which corresponds to
144// the function epilogue, or nullptr if @MBB doesn't contain an epilogue.
145static const MachineInstr *getFirstEpilogueInst(const MachineBasicBlock &MBB) {
146 auto LastMI = MBB.getLastNonDebugInstr();
147 if (LastMI == MBB.end() || !LastMI->isReturn())
148 return nullptr;
149 // Assume that epilogue starts with instruction having the same debug location
150 // as the return instruction.
151 DebugLoc LastLoc = LastMI->getDebugLoc();
152 auto Res = LastMI;
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000153 for (MachineBasicBlock::const_reverse_iterator I(std::next(LastMI)),
154 E = MBB.rend();
155 I != E; ++I) {
Alexey Samsonov8000e272014-06-09 21:53:47 +0000156 if (I->getDebugLoc() != LastLoc)
157 return Res;
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000158 Res = &*I;
Alexey Samsonov8000e272014-06-09 21:53:47 +0000159 }
160 // If all instructions have the same debug location, assume whole MBB is
161 // an epilogue.
162 return MBB.begin();
163}
164
165// \brief Collect registers that are modified in the function body (their
Adrian Prantl364d1312014-08-06 18:41:24 +0000166// contents is changed outside of the prologue and epilogue).
Alexey Samsonov8000e272014-06-09 21:53:47 +0000167static void collectChangingRegs(const MachineFunction *MF,
168 const TargetRegisterInfo *TRI,
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000169 BitVector &Regs) {
Alexey Samsonov8000e272014-06-09 21:53:47 +0000170 for (const auto &MBB : *MF) {
171 auto FirstEpilogueInst = getFirstEpilogueInst(MBB);
Adrian Prantle2d63752014-08-06 18:41:19 +0000172
Alexey Samsonov8000e272014-06-09 21:53:47 +0000173 for (const auto &MI : MBB) {
Adrian Prantle2d63752014-08-06 18:41:19 +0000174 if (&MI == FirstEpilogueInst)
175 break;
176 if (!MI.getFlag(MachineInstr::FrameSetup))
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000177 applyToClobberedRegisters(MI, TRI, [&](unsigned r) { Regs.set(r); });
Alexey Samsonov8000e272014-06-09 21:53:47 +0000178 }
179 }
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000180}
181
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000182void llvm::calculateDbgValueHistory(const MachineFunction *MF,
183 const TargetRegisterInfo *TRI,
184 DbgValueHistoryMap &Result) {
185 BitVector ChangingRegs(TRI->getNumRegs());
Alexey Samsonov8000e272014-06-09 21:53:47 +0000186 collectChangingRegs(MF, TRI, ChangingRegs);
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000187
Alexey Samsonov8000e272014-06-09 21:53:47 +0000188 RegDescribedVarsMap RegVars;
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000189 for (const auto &MBB : *MF) {
190 for (const auto &MI : MBB) {
191 if (!MI.isDebugValue()) {
192 // Not a DBG_VALUE instruction. It may clobber registers which describe
193 // some variables.
Adrian Prantl364d1312014-08-06 18:41:24 +0000194 applyToClobberedRegisters(MI, TRI, [&](unsigned RegNo) {
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000195 if (ChangingRegs.test(RegNo))
Alexey Samsonov8000e272014-06-09 21:53:47 +0000196 clobberRegisterUses(RegVars, RegNo, Result, MI);
Adrian Prantl364d1312014-08-06 18:41:24 +0000197 });
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000198 continue;
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000199 }
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000200
Alexey Samsonovf0e0cca2014-05-27 22:35:00 +0000201 assert(MI.getNumOperands() > 1 && "Invalid DBG_VALUE instruction!");
Adrian Prantlb1416832014-08-01 22:11:58 +0000202 // Use the base variable (without any DW_OP_piece expressions)
203 // as index into History. The full variables including the
204 // piece expressions are attached to the MI.
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000205 DIVariable Var = MI.getDebugVariable();
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000206
Alexey Samsonovbb2990d2014-05-27 23:09:50 +0000207 if (unsigned PrevReg = Result.getRegisterForVar(Var))
208 dropRegDescribedVar(RegVars, PrevReg, Var);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000209
Alexey Samsonovbb2990d2014-05-27 23:09:50 +0000210 Result.startInstrRange(Var, MI);
211
212 if (unsigned NewReg = isDescribedByReg(MI))
213 addRegDescribedVar(RegVars, NewReg, Var);
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000214 }
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000215
216 // Make sure locations for register-described variables are valid only
217 // until the end of the basic block (unless it's the last basic block, in
218 // which case let their liveness run off to the end of the function).
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000219 if (!MBB.empty() && &MBB != &MF->back()) {
220 for (auto I = RegVars.begin(), E = RegVars.end(); I != E;) {
221 auto CurElem = I++; // CurElem can be erased below.
222 if (ChangingRegs.test(CurElem->first))
223 clobberRegisterUses(RegVars, CurElem, Result, MBB.back());
224 }
Alexey Samsonov8000e272014-06-09 21:53:47 +0000225 }
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000226 }
227}