blob: 21d816bc9973c4f1dcd6b408c262964d601d1a96 [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"
Reid Klecknerf6f04f82016-03-25 17:54:46 +000018#include "llvm/Target/TargetLowering.h"
Alexey Samsonov414b6fb2014-04-30 21:34:11 +000019#include "llvm/Target/TargetRegisterInfo.h"
Reid Klecknerf6f04f82016-03-25 17:54:46 +000020#include "llvm/Target/TargetSubtargetInfo.h"
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000021#include <algorithm>
22#include <map>
Benjamin Kramer6bf8af52014-10-06 15:31:04 +000023using namespace llvm;
Alexey Samsonov414b6fb2014-04-30 21:34:11 +000024
25#define DEBUG_TYPE "dwarfdebug"
26
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000027// \brief If @MI is a DBG_VALUE with debug value described by a
28// defined register, returns the number of this register.
29// In the other case, returns 0.
30static unsigned isDescribedByReg(const MachineInstr &MI) {
31 assert(MI.isDebugValue());
Adrian Prantl87b7eb92014-10-01 18:55:02 +000032 assert(MI.getNumOperands() == 4);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000033 // If location of variable is described using a register (directly or
34 // indirecltly), this register is always a first operand.
35 return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0;
36}
37
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +000038void DbgValueHistoryMap::startInstrRange(InlinedVariable Var,
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000039 const MachineInstr &MI) {
40 // Instruction range should start with a DBG_VALUE instruction for the
41 // variable.
Adrian Prantl87b7eb92014-10-01 18:55:02 +000042 assert(MI.isDebugValue() && "not a DBG_VALUE");
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000043 auto &Ranges = VarInstrRanges[Var];
44 if (!Ranges.empty() && Ranges.back().second == nullptr &&
Duncan P. N. Exon Smithfd8cc232016-02-27 20:01:33 +000045 Ranges.back().first->isIdenticalTo(MI)) {
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000046 DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
47 << "\t" << Ranges.back().first << "\t" << MI << "\n");
48 return;
49 }
50 Ranges.push_back(std::make_pair(&MI, nullptr));
51}
52
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +000053void DbgValueHistoryMap::endInstrRange(InlinedVariable Var,
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000054 const MachineInstr &MI) {
55 auto &Ranges = VarInstrRanges[Var];
56 // Verify that the current instruction range is not yet closed.
57 assert(!Ranges.empty() && Ranges.back().second == nullptr);
58 // For now, instruction ranges are not allowed to cross basic block
59 // boundaries.
60 assert(Ranges.back().first->getParent() == MI.getParent());
61 Ranges.back().second = &MI;
62}
63
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +000064unsigned DbgValueHistoryMap::getRegisterForVar(InlinedVariable Var) const {
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000065 const auto &I = VarInstrRanges.find(Var);
66 if (I == VarInstrRanges.end())
67 return 0;
68 const auto &Ranges = I->second;
69 if (Ranges.empty() || Ranges.back().second != nullptr)
70 return 0;
71 return isDescribedByReg(*Ranges.back().first);
72}
73
74namespace {
75// Maps physreg numbers to the variables they describe.
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +000076typedef DbgValueHistoryMap::InlinedVariable InlinedVariable;
77typedef std::map<unsigned, SmallVector<InlinedVariable, 1>> RegDescribedVarsMap;
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000078}
79
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000080// \brief Claim that @Var is not described by @RegNo anymore.
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +000081static void dropRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
82 InlinedVariable Var) {
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000083 const auto &I = RegVars.find(RegNo);
84 assert(RegNo != 0U && I != RegVars.end());
85 auto &VarSet = I->second;
86 const auto &VarPos = std::find(VarSet.begin(), VarSet.end(), Var);
87 assert(VarPos != VarSet.end());
88 VarSet.erase(VarPos);
89 // Don't keep empty sets in a map to keep it as small as possible.
90 if (VarSet.empty())
91 RegVars.erase(I);
92}
93
94// \brief Claim that @Var is now described by @RegNo.
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +000095static void addRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
96 InlinedVariable Var) {
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +000097 assert(RegNo != 0U);
Alexey Samsonovbb2990d2014-05-27 23:09:50 +000098 auto &VarSet = RegVars[RegNo];
99 assert(std::find(VarSet.begin(), VarSet.end(), Var) == VarSet.end());
100 VarSet.push_back(Var);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000101}
102
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000103// \brief Terminate the location range for variables described by register at
104// @I by inserting @ClobberingInstr to their history.
105static void clobberRegisterUses(RegDescribedVarsMap &RegVars,
106 RegDescribedVarsMap::iterator I,
107 DbgValueHistoryMap &HistMap,
108 const MachineInstr &ClobberingInstr) {
109 // Iterate over all variables described by this register and add this
110 // instruction to their history, clobbering it.
111 for (const auto &Var : I->second)
112 HistMap.endInstrRange(Var, ClobberingInstr);
113 RegVars.erase(I);
114}
115
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000116// \brief Terminate the location range for variables described by register
117// @RegNo by inserting @ClobberingInstr to their history.
118static void clobberRegisterUses(RegDescribedVarsMap &RegVars, unsigned RegNo,
119 DbgValueHistoryMap &HistMap,
120 const MachineInstr &ClobberingInstr) {
121 const auto &I = RegVars.find(RegNo);
122 if (I == RegVars.end())
123 return;
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000124 clobberRegisterUses(RegVars, I, HistMap, ClobberingInstr);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000125}
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;
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000137 for (MachineBasicBlock::const_reverse_iterator I(std::next(LastMI)),
138 E = MBB.rend();
139 I != E; ++I) {
Alexey Samsonov8000e272014-06-09 21:53:47 +0000140 if (I->getDebugLoc() != LastLoc)
141 return Res;
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000142 Res = &*I;
Alexey Samsonov8000e272014-06-09 21:53:47 +0000143 }
144 // If all instructions have the same debug location, assume whole MBB is
145 // an epilogue.
146 return MBB.begin();
147}
148
149// \brief Collect registers that are modified in the function body (their
Adrian Prantl364d1312014-08-06 18:41:24 +0000150// contents is changed outside of the prologue and epilogue).
Alexey Samsonov8000e272014-06-09 21:53:47 +0000151static void collectChangingRegs(const MachineFunction *MF,
152 const TargetRegisterInfo *TRI,
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000153 BitVector &Regs) {
Alexey Samsonov8000e272014-06-09 21:53:47 +0000154 for (const auto &MBB : *MF) {
155 auto FirstEpilogueInst = getFirstEpilogueInst(MBB);
Adrian Prantle2d63752014-08-06 18:41:19 +0000156
Alexey Samsonov8000e272014-06-09 21:53:47 +0000157 for (const auto &MI : MBB) {
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000158 // Avoid looking at prologue or epilogue instructions.
Adrian Prantle2d63752014-08-06 18:41:19 +0000159 if (&MI == FirstEpilogueInst)
160 break;
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000161 if (MI.getFlag(MachineInstr::FrameSetup))
162 continue;
163
164 // Look for register defs and register masks. Register masks are
165 // typically on calls and they clobber everything not in the mask.
166 for (const MachineOperand &MO : MI.operands()) {
167 if (MO.isReg() && MO.isDef() && MO.getReg()) {
168 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid();
169 ++AI)
170 Regs.set(*AI);
171 } else if (MO.isRegMask()) {
172 Regs.setBitsNotInMask(MO.getRegMask());
173 }
174 }
Alexey Samsonov8000e272014-06-09 21:53:47 +0000175 }
176 }
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000177}
178
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000179void llvm::calculateDbgValueHistory(const MachineFunction *MF,
180 const TargetRegisterInfo *TRI,
181 DbgValueHistoryMap &Result) {
182 BitVector ChangingRegs(TRI->getNumRegs());
Alexey Samsonov8000e272014-06-09 21:53:47 +0000183 collectChangingRegs(MF, TRI, ChangingRegs);
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000184
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000185 const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
186 unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
Alexey Samsonov8000e272014-06-09 21:53:47 +0000187 RegDescribedVarsMap RegVars;
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000188 for (const auto &MBB : *MF) {
189 for (const auto &MI : MBB) {
190 if (!MI.isDebugValue()) {
191 // Not a DBG_VALUE instruction. It may clobber registers which describe
192 // some variables.
Reid Klecknerf6f04f82016-03-25 17:54:46 +0000193 for (const MachineOperand &MO : MI.operands()) {
194 if (MO.isReg() && MO.isDef() && MO.getReg()) {
195 // If this is a register def operand, it may end a debug value
196 // range.
197 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid();
198 ++AI)
199 if (ChangingRegs.test(*AI))
200 clobberRegisterUses(RegVars, *AI, Result, MI);
201 } else if (MO.isRegMask()) {
202 // If this is a register mask operand, clobber all debug values in
203 // non-CSRs.
204 for (int I = ChangingRegs.find_first(); I != -1;
205 I = ChangingRegs.find_next(I)) {
206 // Don't consider SP to be clobbered by register masks.
207 if (unsigned(I) != SP && TRI->isPhysicalRegister(I) &&
208 MO.clobbersPhysReg(I)) {
209 clobberRegisterUses(RegVars, I, Result, MI);
210 }
211 }
212 }
213 }
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000214 continue;
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000215 }
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000216
Alexey Samsonovf0e0cca2014-05-27 22:35:00 +0000217 assert(MI.getNumOperands() > 1 && "Invalid DBG_VALUE instruction!");
Adrian Prantlb1416832014-08-01 22:11:58 +0000218 // Use the base variable (without any DW_OP_piece expressions)
219 // as index into History. The full variables including the
220 // piece expressions are attached to the MI.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000221 const DILocalVariable *RawVar = MI.getDebugVariable();
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000222 assert(RawVar->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
Duncan P. N. Exon Smith3bef6a32015-04-03 19:20:26 +0000223 "Expected inlined-at fields to agree");
Duncan P. N. Exon Smith78a95272015-04-16 22:12:59 +0000224 InlinedVariable Var(RawVar, MI.getDebugLoc()->getInlinedAt());
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000225
Alexey Samsonovbb2990d2014-05-27 23:09:50 +0000226 if (unsigned PrevReg = Result.getRegisterForVar(Var))
227 dropRegDescribedVar(RegVars, PrevReg, Var);
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000228
Alexey Samsonovbb2990d2014-05-27 23:09:50 +0000229 Result.startInstrRange(Var, MI);
230
231 if (unsigned NewReg = isDescribedByReg(MI))
232 addRegDescribedVar(RegVars, NewReg, Var);
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000233 }
Alexey Samsonovdfcaf9c2014-05-20 18:34:54 +0000234
235 // Make sure locations for register-described variables are valid only
236 // until the end of the basic block (unless it's the last basic block, in
237 // which case let their liveness run off to the end of the function).
Benjamin Kramer6bf8af52014-10-06 15:31:04 +0000238 if (!MBB.empty() && &MBB != &MF->back()) {
239 for (auto I = RegVars.begin(), E = RegVars.end(); I != E;) {
240 auto CurElem = I++; // CurElem can be erased below.
241 if (ChangingRegs.test(CurElem->first))
242 clobberRegisterUses(RegVars, CurElem, Result, MBB.back());
243 }
Alexey Samsonov8000e272014-06-09 21:53:47 +0000244 }
Alexey Samsonov414b6fb2014-04-30 21:34:11 +0000245 }
246}