blob: bbdf2378f6ea0bd88150e089fde962bbabaa0087 [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"
Benjamin Kramer16132e62015-03-23 18:07:13 +000017#include "llvm/Support/raw_ostream.h"
Alexey Samsonov414b6fb2014-04-30 21:34:11 +000018#include "llvm/Target/TargetRegisterInfo.h"
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000019#include <algorithm>
20#include <map>
Benjamin Kramer6bf8af52014-10-06 15:31:04 +000021using namespace llvm;
Alexey Samsonov414b6fb2014-04-30 21:34:11 +000022
23#define DEBUG_TYPE "dwarfdebug"
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());
Adrian Prantl87b7eb92014-10-01 18:55:02 +000030 assert(MI.getNumOperands() == 4);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000031 // 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 Prantl87b7eb92014-10-01 18:55:02 +000040 assert(MI.isDebugValue() && "not a DBG_VALUE");
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
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000100// \brief Terminate the location range for variables described by register at
101// @I by inserting @ClobberingInstr to their history.
102static void clobberRegisterUses(RegDescribedVarsMap &RegVars,
103 RegDescribedVarsMap::iterator I,
104 DbgValueHistoryMap &HistMap,
105 const MachineInstr &ClobberingInstr) {
106 // Iterate over all variables described by this register and add this
107 // instruction to their history, clobbering it.
108 for (const auto &Var : I->second)
109 HistMap.endInstrRange(Var, ClobberingInstr);
110 RegVars.erase(I);
111}
112
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000113// \brief Terminate the location range for variables described by register
114// @RegNo by inserting @ClobberingInstr to their history.
115static void clobberRegisterUses(RegDescribedVarsMap &RegVars, unsigned RegNo,
116 DbgValueHistoryMap &HistMap,
117 const MachineInstr &ClobberingInstr) {
118 const auto &I = RegVars.find(RegNo);
119 if (I == RegVars.end())
120 return;
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000121 clobberRegisterUses(RegVars, I, HistMap, ClobberingInstr);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000122}
123
Adrian Prantl364d1312014-08-06 18:41:24 +0000124// \brief Collect all registers clobbered by @MI and apply the functor
125// @Func to their RegNo.
126// @Func should be a functor with a void(unsigned) signature. We're
127// not using std::function here for performance reasons. It has a
128// small but measurable impact. By using a functor instead of a
129// std::set& here, we can avoid the overhead of constructing
130// temporaries in calculateDbgValueHistory, which has a significant
131// performance impact.
132template<typename Callable>
133static void applyToClobberedRegisters(const MachineInstr &MI,
Alexey Samsonov8000e272014-06-09 21:53:47 +0000134 const TargetRegisterInfo *TRI,
Adrian Prantl364d1312014-08-06 18:41:24 +0000135 Callable Func) {
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000136 for (const MachineOperand &MO : MI.operands()) {
137 if (!MO.isReg() || !MO.isDef() || !MO.getReg())
138 continue;
Alexey Samsonov8000e272014-06-09 21:53:47 +0000139 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid(); ++AI)
Adrian Prantl364d1312014-08-06 18:41:24 +0000140 Func(*AI);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000141 }
142}
143
Alexey Samsonov8000e272014-06-09 21:53:47 +0000144// \brief Returns the first instruction in @MBB which corresponds to
145// the function epilogue, or nullptr if @MBB doesn't contain an epilogue.
146static const MachineInstr *getFirstEpilogueInst(const MachineBasicBlock &MBB) {
147 auto LastMI = MBB.getLastNonDebugInstr();
148 if (LastMI == MBB.end() || !LastMI->isReturn())
149 return nullptr;
150 // Assume that epilogue starts with instruction having the same debug location
151 // as the return instruction.
152 DebugLoc LastLoc = LastMI->getDebugLoc();
153 auto Res = LastMI;
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000154 for (MachineBasicBlock::const_reverse_iterator I(std::next(LastMI)),
155 E = MBB.rend();
156 I != E; ++I) {
Alexey Samsonov8000e272014-06-09 21:53:47 +0000157 if (I->getDebugLoc() != LastLoc)
158 return Res;
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000159 Res = &*I;
Alexey Samsonov8000e272014-06-09 21:53:47 +0000160 }
161 // If all instructions have the same debug location, assume whole MBB is
162 // an epilogue.
163 return MBB.begin();
164}
165
166// \brief Collect registers that are modified in the function body (their
Adrian Prantl364d1312014-08-06 18:41:24 +0000167// contents is changed outside of the prologue and epilogue).
Alexey Samsonov8000e272014-06-09 21:53:47 +0000168static void collectChangingRegs(const MachineFunction *MF,
169 const TargetRegisterInfo *TRI,
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000170 BitVector &Regs) {
Alexey Samsonov8000e272014-06-09 21:53:47 +0000171 for (const auto &MBB : *MF) {
172 auto FirstEpilogueInst = getFirstEpilogueInst(MBB);
Adrian Prantle2d63752014-08-06 18:41:19 +0000173
Alexey Samsonov8000e272014-06-09 21:53:47 +0000174 for (const auto &MI : MBB) {
Adrian Prantle2d63752014-08-06 18:41:19 +0000175 if (&MI == FirstEpilogueInst)
176 break;
177 if (!MI.getFlag(MachineInstr::FrameSetup))
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000178 applyToClobberedRegisters(MI, TRI, [&](unsigned r) { Regs.set(r); });
Alexey Samsonov8000e272014-06-09 21:53:47 +0000179 }
180 }
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000181}
182
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000183void llvm::calculateDbgValueHistory(const MachineFunction *MF,
184 const TargetRegisterInfo *TRI,
185 DbgValueHistoryMap &Result) {
186 BitVector ChangingRegs(TRI->getNumRegs());
Alexey Samsonov8000e272014-06-09 21:53:47 +0000187 collectChangingRegs(MF, TRI, ChangingRegs);
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000188
Alexey Samsonov8000e272014-06-09 21:53:47 +0000189 RegDescribedVarsMap RegVars;
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000190 for (const auto &MBB : *MF) {
191 for (const auto &MI : MBB) {
192 if (!MI.isDebugValue()) {
193 // Not a DBG_VALUE instruction. It may clobber registers which describe
194 // some variables.
Adrian Prantl364d1312014-08-06 18:41:24 +0000195 applyToClobberedRegisters(MI, TRI, [&](unsigned RegNo) {
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000196 if (ChangingRegs.test(RegNo))
Alexey Samsonov8000e272014-06-09 21:53:47 +0000197 clobberRegisterUses(RegVars, RegNo, Result, MI);
Adrian Prantl364d1312014-08-06 18:41:24 +0000198 });
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000199 continue;
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000200 }
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000201
Alexey Samsonovf0e0cca2014-05-27 22:35:00 +0000202 assert(MI.getNumOperands() > 1 && "Invalid DBG_VALUE instruction!");
Adrian Prantlb1416832014-08-01 22:11:58 +0000203 // Use the base variable (without any DW_OP_piece expressions)
204 // as index into History. The full variables including the
205 // piece expressions are attached to the MI.
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000206 DIVariable Var = MI.getDebugVariable();
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000207
Alexey Samsonovbb2990d2014-05-27 23:09:50 +0000208 if (unsigned PrevReg = Result.getRegisterForVar(Var))
209 dropRegDescribedVar(RegVars, PrevReg, Var);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000210
Alexey Samsonovbb2990d2014-05-27 23:09:50 +0000211 Result.startInstrRange(Var, MI);
212
213 if (unsigned NewReg = isDescribedByReg(MI))
214 addRegDescribedVar(RegVars, NewReg, Var);
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000215 }
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000216
217 // Make sure locations for register-described variables are valid only
218 // until the end of the basic block (unless it's the last basic block, in
219 // which case let their liveness run off to the end of the function).
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000220 if (!MBB.empty() && &MBB != &MF->back()) {
221 for (auto I = RegVars.begin(), E = RegVars.end(); I != E;) {
222 auto CurElem = I++; // CurElem can be erased below.
223 if (ChangingRegs.test(CurElem->first))
224 clobberRegisterUses(RegVars, CurElem, Result, MBB.back());
225 }
Alexey Samsonov8000e272014-06-09 21:53:47 +0000226 }
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000227 }
228}