blob: 82e14dc13cb1620f5d05cbfac2e42a629053c8b9 [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
Vedant Kumar7224c082018-06-01 22:33:15 +000028#define DEBUG_TYPE "dwarfdebug"
29
Bob Haarman1a4cbbe2017-08-30 17:50:21 +000030Optional<DbgVariableLocation>
31DbgVariableLocation::extractFromMachineInstruction(
32 const MachineInstr &Instruction) {
33 DbgVariableLocation Location;
Bob Haarman223303c2017-08-29 20:59:25 +000034 if (!Instruction.isDebugValue())
Bob Haarman1a4cbbe2017-08-30 17:50:21 +000035 return None;
Bob Haarman223303c2017-08-29 20:59:25 +000036 if (!Instruction.getOperand(0).isReg())
Bob Haarman1a4cbbe2017-08-30 17:50:21 +000037 return None;
Bob Haarman223303c2017-08-29 20:59:25 +000038 Location.Register = Instruction.getOperand(0).getReg();
Bob Haarman223303c2017-08-29 20:59:25 +000039 Location.FragmentInfo.reset();
40 // We only handle expressions generated by DIExpression::appendOffset,
41 // which doesn't require a full stack machine.
42 int64_t Offset = 0;
43 const DIExpression *DIExpr = Instruction.getDebugExpression();
44 auto Op = DIExpr->expr_op_begin();
45 while (Op != DIExpr->expr_op_end()) {
46 switch (Op->getOp()) {
47 case dwarf::DW_OP_constu: {
48 int Value = Op->getArg(0);
49 ++Op;
50 if (Op != DIExpr->expr_op_end()) {
51 switch (Op->getOp()) {
52 case dwarf::DW_OP_minus:
53 Offset -= Value;
54 break;
55 case dwarf::DW_OP_plus:
56 Offset += Value;
Bob Haarman68e46012017-08-29 22:54:31 +000057 break;
Bob Haarman223303c2017-08-29 20:59:25 +000058 default:
59 continue;
60 }
61 }
62 } break;
63 case dwarf::DW_OP_plus_uconst:
64 Offset += Op->getArg(0);
65 break;
66 case dwarf::DW_OP_LLVM_fragment:
67 Location.FragmentInfo = {Op->getArg(1), Op->getArg(0)};
68 break;
69 case dwarf::DW_OP_deref:
Reid Kleckner08f5fd52017-08-31 15:56:49 +000070 Location.LoadChain.push_back(Offset);
71 Offset = 0;
Bob Haarman223303c2017-08-29 20:59:25 +000072 break;
73 default:
Bob Haarman1a4cbbe2017-08-30 17:50:21 +000074 return None;
Bob Haarman223303c2017-08-29 20:59:25 +000075 }
76 ++Op;
77 }
78
Reid Kleckner08f5fd52017-08-31 15:56:49 +000079 // Do one final implicit DW_OP_deref if this was an indirect DBG_VALUE
80 // instruction.
81 // FIXME: Replace these with DIExpression.
82 if (Instruction.isIndirectDebugValue())
83 Location.LoadChain.push_back(Offset);
84
Bob Haarman1a4cbbe2017-08-30 17:50:21 +000085 return Location;
Bob Haarman223303c2017-08-29 20:59:25 +000086}
87
Reid Klecknerf9c275f2016-02-10 20:55:49 +000088DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {}
89
90// Each LexicalScope has first instruction and last instruction to mark
91// beginning and end of a scope respectively. Create an inverse map that list
92// scopes starts (and ends) with an instruction. One instruction may start (or
93// end) multiple scopes. Ignore scopes that are not reachable.
94void DebugHandlerBase::identifyScopeMarkers() {
95 SmallVector<LexicalScope *, 4> WorkList;
96 WorkList.push_back(LScopes.getCurrentFunctionScope());
97 while (!WorkList.empty()) {
98 LexicalScope *S = WorkList.pop_back_val();
99
100 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
101 if (!Children.empty())
102 WorkList.append(Children.begin(), Children.end());
103
104 if (S->isAbstractScope())
105 continue;
106
107 for (const InsnRange &R : S->getRanges()) {
108 assert(R.first && "InsnRange does not have first instruction!");
109 assert(R.second && "InsnRange does not have second instruction!");
110 requestLabelBeforeInsn(R.first);
111 requestLabelAfterInsn(R.second);
112 }
113 }
114}
115
116// Return Label preceding the instruction.
117MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) {
118 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
119 assert(Label && "Didn't insert label before instruction");
120 return Label;
121}
122
123// Return Label immediately following the instruction.
124MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) {
125 return LabelsAfterInsn.lookup(MI);
126}
127
Amjad Aboudacee5682016-07-12 12:06:34 +0000128/// If this type is derived from a base type then return base type size.
129uint64_t DebugHandlerBase::getBaseTypeSize(const DITypeRef TyRef) {
130 DIType *Ty = TyRef.resolve();
131 assert(Ty);
132 DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty);
133 if (!DDTy)
134 return Ty->getSizeInBits();
135
136 unsigned Tag = DDTy->getTag();
137
138 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
139 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
Victor Leschuke1156c22016-10-31 19:09:38 +0000140 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type)
Amjad Aboudacee5682016-07-12 12:06:34 +0000141 return DDTy->getSizeInBits();
142
143 DIType *BaseType = DDTy->getBaseType().resolve();
144
Adrian McCarthy74bfafa2018-01-05 23:01:04 +0000145 if (!BaseType)
146 return 0;
Amjad Aboudacee5682016-07-12 12:06:34 +0000147
148 // If this is a derived type, go ahead and get the base type, unless it's a
149 // reference then it's just the size of the field. Pointer types have no need
150 // of this since they're a different type of qualification on the type.
151 if (BaseType->getTag() == dwarf::DW_TAG_reference_type ||
152 BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type)
153 return Ty->getSizeInBits();
154
155 return getBaseTypeSize(BaseType);
156}
157
Benjamin Kramerdebb3c32017-05-26 20:09:00 +0000158static bool hasDebugInfo(const MachineModuleInfo *MMI,
159 const MachineFunction *MF) {
David Blaikieb2fbb4b2017-02-16 18:48:33 +0000160 if (!MMI->hasDebugInfo())
161 return false;
Matthias Braunf1caa282017-12-15 22:22:58 +0000162 auto *SP = MF->getFunction().getSubprogram();
David Blaikieb2fbb4b2017-02-16 18:48:33 +0000163 if (!SP)
164 return false;
165 assert(SP->getUnit());
166 auto EK = SP->getUnit()->getEmissionKind();
167 if (EK == DICompileUnit::NoDebug)
168 return false;
169 return true;
170}
171
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000172void DebugHandlerBase::beginFunction(const MachineFunction *MF) {
Adrian Prantl8f333792017-03-23 20:23:42 +0000173 PrevInstBB = nullptr;
David Blaikieb2fbb4b2017-02-16 18:48:33 +0000174
Reid Kleckner5bc85432017-05-12 17:02:40 +0000175 if (!Asm || !hasDebugInfo(MMI, MF)) {
David Blaikieb2fbb4b2017-02-16 18:48:33 +0000176 skippedNonDebugFunction();
177 return;
178 }
179
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000180 // Grab the lexical scopes for the function, if we don't have any of those
181 // then we're not going to be able to do anything.
182 LScopes.initialize(*MF);
David Blaikieb2fbb4b2017-02-16 18:48:33 +0000183 if (LScopes.empty()) {
184 beginFunctionImpl(MF);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000185 return;
David Blaikieb2fbb4b2017-02-16 18:48:33 +0000186 }
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000187
188 // Make sure that each lexical scope will have a begin/end label.
189 identifyScopeMarkers();
190
191 // Calculate history for local variables.
192 assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
193 calculateDbgValueHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
194 DbgValues);
Vedant Kumar7224c082018-06-01 22:33:15 +0000195 LLVM_DEBUG(DbgValues.dump());
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000196
197 // Request labels for the full history.
198 for (const auto &I : DbgValues) {
199 const auto &Ranges = I.second;
200 if (Ranges.empty())
201 continue;
202
203 // The first mention of a function argument gets the CurrentFnBegin
204 // label, so arguments are visible when breaking at function entry.
205 const DILocalVariable *DIVar = Ranges.front().first->getDebugVariable();
206 if (DIVar->isParameter() &&
Matthias Braunf1caa282017-12-15 22:22:58 +0000207 getDISubprogram(DIVar->getScope())->describes(&MF->getFunction())) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000208 LabelsBeforeInsn[Ranges.front().first] = Asm->getFunctionBegin();
Adrian Prantl941fa752016-12-05 18:04:47 +0000209 if (Ranges.front().first->getDebugExpression()->isFragment()) {
210 // Mark all non-overlapping initial fragments.
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000211 for (auto I = Ranges.begin(); I != Ranges.end(); ++I) {
Adrian Prantl941fa752016-12-05 18:04:47 +0000212 const DIExpression *Fragment = I->first->getDebugExpression();
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000213 if (std::all_of(Ranges.begin(), I,
214 [&](DbgValueHistoryMap::InstrRange Pred) {
Bjorn Petterssona223f8152018-03-12 18:02:39 +0000215 return !Fragment->fragmentsOverlap(
216 Pred.first->getDebugExpression());
Adrian Prantl941fa752016-12-05 18:04:47 +0000217 }))
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000218 LabelsBeforeInsn[I->first] = Asm->getFunctionBegin();
219 else
220 break;
221 }
222 }
223 }
224
225 for (const auto &Range : Ranges) {
226 requestLabelBeforeInsn(Range.first);
227 if (Range.second)
228 requestLabelAfterInsn(Range.second);
229 }
230 }
231
232 PrevInstLoc = DebugLoc();
233 PrevLabel = Asm->getFunctionBegin();
David Blaikieb2fbb4b2017-02-16 18:48:33 +0000234 beginFunctionImpl(MF);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000235}
236
237void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {
238 if (!MMI->hasDebugInfo())
239 return;
240
241 assert(CurMI == nullptr);
242 CurMI = MI;
243
244 // Insert labels where requested.
245 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
246 LabelsBeforeInsn.find(MI);
247
248 // No label needed.
249 if (I == LabelsBeforeInsn.end())
250 return;
251
252 // Label already assigned.
253 if (I->second)
254 return;
255
256 if (!PrevLabel) {
257 PrevLabel = MMI->getContext().createTempSymbol();
258 Asm->OutStreamer->EmitLabel(PrevLabel);
259 }
260 I->second = PrevLabel;
261}
262
263void DebugHandlerBase::endInstruction() {
264 if (!MMI->hasDebugInfo())
265 return;
266
267 assert(CurMI != nullptr);
Adrian Prantlfb31da12017-05-22 20:47:09 +0000268 // Don't create a new label after DBG_VALUE and other instructions that don't
269 // generate code.
270 if (!CurMI->isMetaInstruction()) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000271 PrevLabel = nullptr;
Paul Robinsonac7fe5e2016-12-12 20:49:11 +0000272 PrevInstBB = CurMI->getParent();
273 }
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000274
275 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
276 LabelsAfterInsn.find(CurMI);
277 CurMI = nullptr;
278
279 // No label needed.
280 if (I == LabelsAfterInsn.end())
281 return;
282
283 // Label already assigned.
284 if (I->second)
285 return;
286
287 // We need a label after this instruction.
288 if (!PrevLabel) {
289 PrevLabel = MMI->getContext().createTempSymbol();
290 Asm->OutStreamer->EmitLabel(PrevLabel);
291 }
292 I->second = PrevLabel;
293}
294
295void DebugHandlerBase::endFunction(const MachineFunction *MF) {
David Blaikieb2fbb4b2017-02-16 18:48:33 +0000296 if (hasDebugInfo(MMI, MF))
297 endFunctionImpl(MF);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000298 DbgValues.clear();
299 LabelsBeforeInsn.clear();
300 LabelsAfterInsn.clear();
301}