blob: 3c46a99d0845df99dbdb0d60888f146dd6bb4b26 [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
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +000036void DbgValueHistoryMap::startInstrRange(InlinedVariable Var,
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000037 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
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +000051void DbgValueHistoryMap::endInstrRange(InlinedVariable Var,
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000052 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
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +000062unsigned DbgValueHistoryMap::getRegisterForVar(InlinedVariable Var) const {
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000063 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.
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +000074typedef DbgValueHistoryMap::InlinedVariable InlinedVariable;
75typedef std::map<unsigned, SmallVector<InlinedVariable, 1>> RegDescribedVarsMap;
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000076}
77
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000078// \brief Claim that @Var is not described by @RegNo anymore.
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +000079static void dropRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
80 InlinedVariable Var) {
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000081 const auto &I = RegVars.find(RegNo);
82 assert(RegNo != 0U && I != RegVars.end());
83 auto &VarSet = I->second;
84 const auto &VarPos = std::find(VarSet.begin(), VarSet.end(), Var);
85 assert(VarPos != VarSet.end());
86 VarSet.erase(VarPos);
87 // Don't keep empty sets in a map to keep it as small as possible.
88 if (VarSet.empty())
89 RegVars.erase(I);
90}
91
92// \brief Claim that @Var is now described by @RegNo.
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +000093static void addRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
94 InlinedVariable Var) {
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000095 assert(RegNo != 0U);
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000096 auto &VarSet = RegVars[RegNo];
97 assert(std::find(VarSet.begin(), VarSet.end(), Var) == VarSet.end());
98 VarSet.push_back(Var);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000099}
100
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000101// \brief Terminate the location range for variables described by register at
102// @I by inserting @ClobberingInstr to their history.
103static void clobberRegisterUses(RegDescribedVarsMap &RegVars,
104 RegDescribedVarsMap::iterator I,
105 DbgValueHistoryMap &HistMap,
106 const MachineInstr &ClobberingInstr) {
107 // Iterate over all variables described by this register and add this
108 // instruction to their history, clobbering it.
109 for (const auto &Var : I->second)
110 HistMap.endInstrRange(Var, ClobberingInstr);
111 RegVars.erase(I);
112}
113
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000114// \brief Terminate the location range for variables described by register
115// @RegNo by inserting @ClobberingInstr to their history.
116static void clobberRegisterUses(RegDescribedVarsMap &RegVars, unsigned RegNo,
117 DbgValueHistoryMap &HistMap,
118 const MachineInstr &ClobberingInstr) {
119 const auto &I = RegVars.find(RegNo);
120 if (I == RegVars.end())
121 return;
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000122 clobberRegisterUses(RegVars, I, HistMap, ClobberingInstr);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000123}
124
Adrian Prantl364d1312014-08-06 18:41:24 +0000125// \brief Collect all registers clobbered by @MI and apply the functor
126// @Func to their RegNo.
127// @Func should be a functor with a void(unsigned) signature. We're
128// not using std::function here for performance reasons. It has a
129// small but measurable impact. By using a functor instead of a
130// std::set& here, we can avoid the overhead of constructing
131// temporaries in calculateDbgValueHistory, which has a significant
132// performance impact.
133template<typename Callable>
134static void applyToClobberedRegisters(const MachineInstr &MI,
Alexey Samsonov8000e272014-06-09 21:53:47 +0000135 const TargetRegisterInfo *TRI,
Adrian Prantl364d1312014-08-06 18:41:24 +0000136 Callable Func) {
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000137 for (const MachineOperand &MO : MI.operands()) {
138 if (!MO.isReg() || !MO.isDef() || !MO.getReg())
139 continue;
Alexey Samsonov8000e272014-06-09 21:53:47 +0000140 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid(); ++AI)
Adrian Prantl364d1312014-08-06 18:41:24 +0000141 Func(*AI);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000142 }
143}
144
Alexey Samsonov8000e272014-06-09 21:53:47 +0000145// \brief Returns the first instruction in @MBB which corresponds to
146// the function epilogue, or nullptr if @MBB doesn't contain an epilogue.
147static const MachineInstr *getFirstEpilogueInst(const MachineBasicBlock &MBB) {
148 auto LastMI = MBB.getLastNonDebugInstr();
149 if (LastMI == MBB.end() || !LastMI->isReturn())
150 return nullptr;
151 // Assume that epilogue starts with instruction having the same debug location
152 // as the return instruction.
153 DebugLoc LastLoc = LastMI->getDebugLoc();
154 auto Res = LastMI;
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000155 for (MachineBasicBlock::const_reverse_iterator I(std::next(LastMI)),
156 E = MBB.rend();
157 I != E; ++I) {
Alexey Samsonov8000e272014-06-09 21:53:47 +0000158 if (I->getDebugLoc() != LastLoc)
159 return Res;
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000160 Res = &*I;
Alexey Samsonov8000e272014-06-09 21:53:47 +0000161 }
162 // If all instructions have the same debug location, assume whole MBB is
163 // an epilogue.
164 return MBB.begin();
165}
166
167// \brief Collect registers that are modified in the function body (their
Adrian Prantl364d1312014-08-06 18:41:24 +0000168// contents is changed outside of the prologue and epilogue).
Alexey Samsonov8000e272014-06-09 21:53:47 +0000169static void collectChangingRegs(const MachineFunction *MF,
170 const TargetRegisterInfo *TRI,
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000171 BitVector &Regs) {
Alexey Samsonov8000e272014-06-09 21:53:47 +0000172 for (const auto &MBB : *MF) {
173 auto FirstEpilogueInst = getFirstEpilogueInst(MBB);
Adrian Prantle2d63752014-08-06 18:41:19 +0000174
Alexey Samsonov8000e272014-06-09 21:53:47 +0000175 for (const auto &MI : MBB) {
Adrian Prantle2d63752014-08-06 18:41:19 +0000176 if (&MI == FirstEpilogueInst)
177 break;
178 if (!MI.getFlag(MachineInstr::FrameSetup))
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000179 applyToClobberedRegisters(MI, TRI, [&](unsigned r) { Regs.set(r); });
Alexey Samsonov8000e272014-06-09 21:53:47 +0000180 }
181 }
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000182}
183
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000184void llvm::calculateDbgValueHistory(const MachineFunction *MF,
185 const TargetRegisterInfo *TRI,
186 DbgValueHistoryMap &Result) {
187 BitVector ChangingRegs(TRI->getNumRegs());
Alexey Samsonov8000e272014-06-09 21:53:47 +0000188 collectChangingRegs(MF, TRI, ChangingRegs);
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000189
Alexey Samsonov8000e272014-06-09 21:53:47 +0000190 RegDescribedVarsMap RegVars;
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000191 for (const auto &MBB : *MF) {
192 for (const auto &MI : MBB) {
193 if (!MI.isDebugValue()) {
194 // Not a DBG_VALUE instruction. It may clobber registers which describe
195 // some variables.
Adrian Prantl364d1312014-08-06 18:41:24 +0000196 applyToClobberedRegisters(MI, TRI, [&](unsigned RegNo) {
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000197 if (ChangingRegs.test(RegNo))
Alexey Samsonov8000e272014-06-09 21:53:47 +0000198 clobberRegisterUses(RegVars, RegNo, Result, MI);
Adrian Prantl364d1312014-08-06 18:41:24 +0000199 });
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000200 continue;
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000201 }
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000202
Alexey Samsonovf0e0cca2014-05-27 22:35:00 +0000203 assert(MI.getNumOperands() > 1 && "Invalid DBG_VALUE instruction!");
Adrian Prantlb1416832014-08-01 22:11:58 +0000204 // Use the base variable (without any DW_OP_piece expressions)
205 // as index into History. The full variables including the
206 // piece expressions are attached to the MI.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000207 const DILocalVariable *RawVar = MI.getDebugVariable();
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000208 assert(RawVar->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +0000209 "Expected inlined-at fields to agree");
Duncan P. N. Exon Smith78a95272015-04-16 22:12:59 +0000210 InlinedVariable Var(RawVar, MI.getDebugLoc()->getInlinedAt());
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000211
Alexey Samsonovbb2990d2014-05-27 23:09:50 +0000212 if (unsigned PrevReg = Result.getRegisterForVar(Var))
213 dropRegDescribedVar(RegVars, PrevReg, Var);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000214
Alexey Samsonovbb2990d2014-05-27 23:09:50 +0000215 Result.startInstrRange(Var, MI);
216
217 if (unsigned NewReg = isDescribedByReg(MI))
218 addRegDescribedVar(RegVars, NewReg, Var);
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000219 }
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000220
221 // Make sure locations for register-described variables are valid only
222 // until the end of the basic block (unless it's the last basic block, in
223 // which case let their liveness run off to the end of the function).
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000224 if (!MBB.empty() && &MBB != &MF->back()) {
225 for (auto I = RegVars.begin(), E = RegVars.end(); I != E;) {
226 auto CurElem = I++; // CurElem can be erased below.
227 if (ChangingRegs.test(CurElem->first))
228 clobberRegisterUses(RegVars, CurElem, Result, MBB.back());
229 }
Alexey Samsonov8000e272014-06-09 21:53:47 +0000230 }
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000231 }
232}