blob: 2e5c224479362d6780f63c7addce7f32c4abd454 [file] [log] [blame]
Reid Klecknerf9c275f2016-02-10 20:55:49 +00001//===-- llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp -------*- C++ -*--===//
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// Common functionality for different debug information format backends.
11// LLVM currently supports DWARF and CodeView.
12//
13//===----------------------------------------------------------------------===//
14
15#include "DebugHandlerBase.h"
Bob Haarman1a4cbbe2017-08-30 17:50:21 +000016#include "llvm/ADT/Optional.h"
17#include "llvm/ADT/Twine.h"
Reid Klecknerf9c275f2016-02-10 20:55:49 +000018#include "llvm/CodeGen/AsmPrinter.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/MachineInstr.h"
21#include "llvm/CodeGen/MachineModuleInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000022#include "llvm/CodeGen/TargetSubtargetInfo.h"
Reid Kleckner28865802016-04-14 18:29:59 +000023#include "llvm/IR/DebugInfo.h"
Reid Klecknera5b1eef2016-08-26 17:58:37 +000024#include "llvm/MC/MCStreamer.h"
Reid Klecknerf9c275f2016-02-10 20:55:49 +000025
26using namespace llvm;
27
Bob Haarman1a4cbbe2017-08-30 17:50:21 +000028Optional<DbgVariableLocation>
29DbgVariableLocation::extractFromMachineInstruction(
30 const MachineInstr &Instruction) {
31 DbgVariableLocation Location;
Bob Haarman223303c2017-08-29 20:59:25 +000032 if (!Instruction.isDebugValue())
Bob Haarman1a4cbbe2017-08-30 17:50:21 +000033 return None;
Bob Haarman223303c2017-08-29 20:59:25 +000034 if (!Instruction.getOperand(0).isReg())
Bob Haarman1a4cbbe2017-08-30 17:50:21 +000035 return None;
Bob Haarman223303c2017-08-29 20:59:25 +000036 Location.Register = Instruction.getOperand(0).getReg();
Bob Haarman223303c2017-08-29 20:59:25 +000037 Location.FragmentInfo.reset();
38 // We only handle expressions generated by DIExpression::appendOffset,
39 // which doesn't require a full stack machine.
40 int64_t Offset = 0;
41 const DIExpression *DIExpr = Instruction.getDebugExpression();
42 auto Op = DIExpr->expr_op_begin();
43 while (Op != DIExpr->expr_op_end()) {
44 switch (Op->getOp()) {
45 case dwarf::DW_OP_constu: {
46 int Value = Op->getArg(0);
47 ++Op;
48 if (Op != DIExpr->expr_op_end()) {
49 switch (Op->getOp()) {
50 case dwarf::DW_OP_minus:
51 Offset -= Value;
52 break;
53 case dwarf::DW_OP_plus:
54 Offset += Value;
Bob Haarman68e46012017-08-29 22:54:31 +000055 break;
Bob Haarman223303c2017-08-29 20:59:25 +000056 default:
57 continue;
58 }
59 }
60 } break;
61 case dwarf::DW_OP_plus_uconst:
62 Offset += Op->getArg(0);
63 break;
64 case dwarf::DW_OP_LLVM_fragment:
65 Location.FragmentInfo = {Op->getArg(1), Op->getArg(0)};
66 break;
67 case dwarf::DW_OP_deref:
Reid Kleckner08f5fd52017-08-31 15:56:49 +000068 Location.LoadChain.push_back(Offset);
69 Offset = 0;
Bob Haarman223303c2017-08-29 20:59:25 +000070 break;
71 default:
Bob Haarman1a4cbbe2017-08-30 17:50:21 +000072 return None;
Bob Haarman223303c2017-08-29 20:59:25 +000073 }
74 ++Op;
75 }
76
Reid Kleckner08f5fd52017-08-31 15:56:49 +000077 // Do one final implicit DW_OP_deref if this was an indirect DBG_VALUE
78 // instruction.
79 // FIXME: Replace these with DIExpression.
80 if (Instruction.isIndirectDebugValue())
81 Location.LoadChain.push_back(Offset);
82
Bob Haarman1a4cbbe2017-08-30 17:50:21 +000083 return Location;
Bob Haarman223303c2017-08-29 20:59:25 +000084}
85
Reid Klecknerf9c275f2016-02-10 20:55:49 +000086DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {}
87
88// Each LexicalScope has first instruction and last instruction to mark
89// beginning and end of a scope respectively. Create an inverse map that list
90// scopes starts (and ends) with an instruction. One instruction may start (or
91// end) multiple scopes. Ignore scopes that are not reachable.
92void DebugHandlerBase::identifyScopeMarkers() {
93 SmallVector<LexicalScope *, 4> WorkList;
94 WorkList.push_back(LScopes.getCurrentFunctionScope());
95 while (!WorkList.empty()) {
96 LexicalScope *S = WorkList.pop_back_val();
97
98 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
99 if (!Children.empty())
100 WorkList.append(Children.begin(), Children.end());
101
102 if (S->isAbstractScope())
103 continue;
104
105 for (const InsnRange &R : S->getRanges()) {
106 assert(R.first && "InsnRange does not have first instruction!");
107 assert(R.second && "InsnRange does not have second instruction!");
108 requestLabelBeforeInsn(R.first);
109 requestLabelAfterInsn(R.second);
110 }
111 }
112}
113
114// Return Label preceding the instruction.
115MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) {
116 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
117 assert(Label && "Didn't insert label before instruction");
118 return Label;
119}
120
121// Return Label immediately following the instruction.
122MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) {
123 return LabelsAfterInsn.lookup(MI);
124}
125
Adrian Prantl941fa752016-12-05 18:04:47 +0000126int DebugHandlerBase::fragmentCmp(const DIExpression *P1,
127 const DIExpression *P2) {
Adrian Prantl49797ca2016-12-22 05:27:12 +0000128 auto Fragment1 = *P1->getFragmentInfo();
129 auto Fragment2 = *P2->getFragmentInfo();
130 unsigned l1 = Fragment1.OffsetInBits;
131 unsigned l2 = Fragment2.OffsetInBits;
132 unsigned r1 = l1 + Fragment1.SizeInBits;
133 unsigned r2 = l2 + Fragment2.SizeInBits;
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000134 if (r1 <= l2)
135 return -1;
136 else if (r2 <= l1)
137 return 1;
138 else
139 return 0;
140}
141
Adrian Prantl941fa752016-12-05 18:04:47 +0000142bool DebugHandlerBase::fragmentsOverlap(const DIExpression *P1,
143 const DIExpression *P2) {
144 if (!P1->isFragment() || !P2->isFragment())
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000145 return true;
Adrian Prantl941fa752016-12-05 18:04:47 +0000146 return fragmentCmp(P1, P2) == 0;
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000147}
148
Amjad Aboudacee5682016-07-12 12:06:34 +0000149/// If this type is derived from a base type then return base type size.
150uint64_t DebugHandlerBase::getBaseTypeSize(const DITypeRef TyRef) {
151 DIType *Ty = TyRef.resolve();
152 assert(Ty);
153 DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty);
154 if (!DDTy)
155 return Ty->getSizeInBits();
156
157 unsigned Tag = DDTy->getTag();
158
159 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
160 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
Victor Leschuke1156c22016-10-31 19:09:38 +0000161 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type)
Amjad Aboudacee5682016-07-12 12:06:34 +0000162 return DDTy->getSizeInBits();
163
164 DIType *BaseType = DDTy->getBaseType().resolve();
165
Adrian McCarthy74bfafa2018-01-05 23:01:04 +0000166 if (!BaseType)
167 return 0;
Amjad Aboudacee5682016-07-12 12:06:34 +0000168
169 // If this is a derived type, go ahead and get the base type, unless it's a
170 // reference then it's just the size of the field. Pointer types have no need
171 // of this since they're a different type of qualification on the type.
172 if (BaseType->getTag() == dwarf::DW_TAG_reference_type ||
173 BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type)
174 return Ty->getSizeInBits();
175
176 return getBaseTypeSize(BaseType);
177}
178
Benjamin Kramerdebb3c32017-05-26 20:09:00 +0000179static bool hasDebugInfo(const MachineModuleInfo *MMI,
180 const MachineFunction *MF) {
David Blaikieb2fbb4b2017-02-16 18:48:33 +0000181 if (!MMI->hasDebugInfo())
182 return false;
Matthias Braunf1caa282017-12-15 22:22:58 +0000183 auto *SP = MF->getFunction().getSubprogram();
David Blaikieb2fbb4b2017-02-16 18:48:33 +0000184 if (!SP)
185 return false;
186 assert(SP->getUnit());
187 auto EK = SP->getUnit()->getEmissionKind();
188 if (EK == DICompileUnit::NoDebug)
189 return false;
190 return true;
191}
192
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000193void DebugHandlerBase::beginFunction(const MachineFunction *MF) {
Adrian Prantl8f333792017-03-23 20:23:42 +0000194 PrevInstBB = nullptr;
David Blaikieb2fbb4b2017-02-16 18:48:33 +0000195
Reid Kleckner5bc85432017-05-12 17:02:40 +0000196 if (!Asm || !hasDebugInfo(MMI, MF)) {
David Blaikieb2fbb4b2017-02-16 18:48:33 +0000197 skippedNonDebugFunction();
198 return;
199 }
200
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000201 // Grab the lexical scopes for the function, if we don't have any of those
202 // then we're not going to be able to do anything.
203 LScopes.initialize(*MF);
David Blaikieb2fbb4b2017-02-16 18:48:33 +0000204 if (LScopes.empty()) {
205 beginFunctionImpl(MF);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000206 return;
David Blaikieb2fbb4b2017-02-16 18:48:33 +0000207 }
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000208
209 // Make sure that each lexical scope will have a begin/end label.
210 identifyScopeMarkers();
211
212 // Calculate history for local variables.
213 assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
214 calculateDbgValueHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
215 DbgValues);
216
217 // Request labels for the full history.
218 for (const auto &I : DbgValues) {
219 const auto &Ranges = I.second;
220 if (Ranges.empty())
221 continue;
222
223 // The first mention of a function argument gets the CurrentFnBegin
224 // label, so arguments are visible when breaking at function entry.
225 const DILocalVariable *DIVar = Ranges.front().first->getDebugVariable();
226 if (DIVar->isParameter() &&
Matthias Braunf1caa282017-12-15 22:22:58 +0000227 getDISubprogram(DIVar->getScope())->describes(&MF->getFunction())) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000228 LabelsBeforeInsn[Ranges.front().first] = Asm->getFunctionBegin();
Adrian Prantl941fa752016-12-05 18:04:47 +0000229 if (Ranges.front().first->getDebugExpression()->isFragment()) {
230 // Mark all non-overlapping initial fragments.
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000231 for (auto I = Ranges.begin(); I != Ranges.end(); ++I) {
Adrian Prantl941fa752016-12-05 18:04:47 +0000232 const DIExpression *Fragment = I->first->getDebugExpression();
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000233 if (std::all_of(Ranges.begin(), I,
234 [&](DbgValueHistoryMap::InstrRange Pred) {
Adrian Prantl941fa752016-12-05 18:04:47 +0000235 return !fragmentsOverlap(
236 Fragment, Pred.first->getDebugExpression());
237 }))
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000238 LabelsBeforeInsn[I->first] = Asm->getFunctionBegin();
239 else
240 break;
241 }
242 }
243 }
244
245 for (const auto &Range : Ranges) {
246 requestLabelBeforeInsn(Range.first);
247 if (Range.second)
248 requestLabelAfterInsn(Range.second);
249 }
250 }
251
252 PrevInstLoc = DebugLoc();
253 PrevLabel = Asm->getFunctionBegin();
David Blaikieb2fbb4b2017-02-16 18:48:33 +0000254 beginFunctionImpl(MF);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000255}
256
257void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {
258 if (!MMI->hasDebugInfo())
259 return;
260
261 assert(CurMI == nullptr);
262 CurMI = MI;
263
264 // Insert labels where requested.
265 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
266 LabelsBeforeInsn.find(MI);
267
268 // No label needed.
269 if (I == LabelsBeforeInsn.end())
270 return;
271
272 // Label already assigned.
273 if (I->second)
274 return;
275
276 if (!PrevLabel) {
277 PrevLabel = MMI->getContext().createTempSymbol();
278 Asm->OutStreamer->EmitLabel(PrevLabel);
279 }
280 I->second = PrevLabel;
281}
282
283void DebugHandlerBase::endInstruction() {
284 if (!MMI->hasDebugInfo())
285 return;
286
287 assert(CurMI != nullptr);
Adrian Prantlfb31da12017-05-22 20:47:09 +0000288 // Don't create a new label after DBG_VALUE and other instructions that don't
289 // generate code.
290 if (!CurMI->isMetaInstruction()) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000291 PrevLabel = nullptr;
Paul Robinsonac7fe5e2016-12-12 20:49:11 +0000292 PrevInstBB = CurMI->getParent();
293 }
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000294
295 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
296 LabelsAfterInsn.find(CurMI);
297 CurMI = nullptr;
298
299 // No label needed.
300 if (I == LabelsAfterInsn.end())
301 return;
302
303 // Label already assigned.
304 if (I->second)
305 return;
306
307 // We need a label after this instruction.
308 if (!PrevLabel) {
309 PrevLabel = MMI->getContext().createTempSymbol();
310 Asm->OutStreamer->EmitLabel(PrevLabel);
311 }
312 I->second = PrevLabel;
313}
314
315void DebugHandlerBase::endFunction(const MachineFunction *MF) {
David Blaikieb2fbb4b2017-02-16 18:48:33 +0000316 if (hasDebugInfo(MMI, MF))
317 endFunctionImpl(MF);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000318 DbgValues.clear();
319 LabelsBeforeInsn.clear();
320 LabelsAfterInsn.clear();
321}