1 //===-- llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp -------*- C++ -*--===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Common functionality for different debug information format backends.
10 // LLVM currently supports DWARF and CodeView.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/DebugHandlerBase.h"
15 #include "llvm/ADT/Optional.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/CodeGen/AsmPrinter.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineInstr.h"
20 #include "llvm/CodeGen/MachineModuleInfo.h"
21 #include "llvm/CodeGen/TargetSubtargetInfo.h"
22 #include "llvm/IR/DebugInfo.h"
23 #include "llvm/MC/MCStreamer.h"
27 #define DEBUG_TYPE "dwarfdebug"
29 Optional
<DbgVariableLocation
>
30 DbgVariableLocation::extractFromMachineInstruction(
31 const MachineInstr
&Instruction
) {
32 DbgVariableLocation Location
;
33 if (!Instruction
.isDebugValue())
35 if (!Instruction
.getOperand(0).isReg())
37 Location
.Register
= Instruction
.getOperand(0).getReg();
38 Location
.FragmentInfo
.reset();
39 // We only handle expressions generated by DIExpression::appendOffset,
40 // which doesn't require a full stack machine.
42 const DIExpression
*DIExpr
= Instruction
.getDebugExpression();
43 auto Op
= DIExpr
->expr_op_begin();
44 while (Op
!= DIExpr
->expr_op_end()) {
45 switch (Op
->getOp()) {
46 case dwarf::DW_OP_constu
: {
47 int Value
= Op
->getArg(0);
49 if (Op
!= DIExpr
->expr_op_end()) {
50 switch (Op
->getOp()) {
51 case dwarf::DW_OP_minus
:
54 case dwarf::DW_OP_plus
:
62 case dwarf::DW_OP_plus_uconst
:
63 Offset
+= Op
->getArg(0);
65 case dwarf::DW_OP_LLVM_fragment
:
66 Location
.FragmentInfo
= {Op
->getArg(1), Op
->getArg(0)};
68 case dwarf::DW_OP_deref
:
69 Location
.LoadChain
.push_back(Offset
);
78 // Do one final implicit DW_OP_deref if this was an indirect DBG_VALUE
80 // FIXME: Replace these with DIExpression.
81 if (Instruction
.isIndirectDebugValue())
82 Location
.LoadChain
.push_back(Offset
);
87 DebugHandlerBase::DebugHandlerBase(AsmPrinter
*A
) : Asm(A
), MMI(Asm
->MMI
) {}
89 // Each LexicalScope has first instruction and last instruction to mark
90 // beginning and end of a scope respectively. Create an inverse map that list
91 // scopes starts (and ends) with an instruction. One instruction may start (or
92 // end) multiple scopes. Ignore scopes that are not reachable.
93 void DebugHandlerBase::identifyScopeMarkers() {
94 SmallVector
<LexicalScope
*, 4> WorkList
;
95 WorkList
.push_back(LScopes
.getCurrentFunctionScope());
96 while (!WorkList
.empty()) {
97 LexicalScope
*S
= WorkList
.pop_back_val();
99 const SmallVectorImpl
<LexicalScope
*> &Children
= S
->getChildren();
100 if (!Children
.empty())
101 WorkList
.append(Children
.begin(), Children
.end());
103 if (S
->isAbstractScope())
106 for (const InsnRange
&R
: S
->getRanges()) {
107 assert(R
.first
&& "InsnRange does not have first instruction!");
108 assert(R
.second
&& "InsnRange does not have second instruction!");
109 requestLabelBeforeInsn(R
.first
);
110 requestLabelAfterInsn(R
.second
);
115 // Return Label preceding the instruction.
116 MCSymbol
*DebugHandlerBase::getLabelBeforeInsn(const MachineInstr
*MI
) {
117 MCSymbol
*Label
= LabelsBeforeInsn
.lookup(MI
);
118 assert(Label
&& "Didn't insert label before instruction");
122 // Return Label immediately following the instruction.
123 MCSymbol
*DebugHandlerBase::getLabelAfterInsn(const MachineInstr
*MI
) {
124 return LabelsAfterInsn
.lookup(MI
);
127 // Return the function-local offset of an instruction.
129 DebugHandlerBase::getFunctionLocalOffsetAfterInsn(const MachineInstr
*MI
) {
130 MCContext
&MC
= Asm
->OutContext
;
132 MCSymbol
*Start
= Asm
->getFunctionBegin();
133 const auto *StartRef
= MCSymbolRefExpr::create(Start
, MC
);
135 MCSymbol
*AfterInsn
= getLabelAfterInsn(MI
);
136 assert(AfterInsn
&& "Expected label after instruction");
137 const auto *AfterRef
= MCSymbolRefExpr::create(AfterInsn
, MC
);
139 return MCBinaryExpr::createSub(AfterRef
, StartRef
, MC
);
142 /// If this type is derived from a base type then return base type size.
143 uint64_t DebugHandlerBase::getBaseTypeSize(const DITypeRef TyRef
) {
144 DIType
*Ty
= TyRef
.resolve();
146 DIDerivedType
*DDTy
= dyn_cast
<DIDerivedType
>(Ty
);
148 return Ty
->getSizeInBits();
150 unsigned Tag
= DDTy
->getTag();
152 if (Tag
!= dwarf::DW_TAG_member
&& Tag
!= dwarf::DW_TAG_typedef
&&
153 Tag
!= dwarf::DW_TAG_const_type
&& Tag
!= dwarf::DW_TAG_volatile_type
&&
154 Tag
!= dwarf::DW_TAG_restrict_type
&& Tag
!= dwarf::DW_TAG_atomic_type
)
155 return DDTy
->getSizeInBits();
157 DIType
*BaseType
= DDTy
->getBaseType().resolve();
162 // If this is a derived type, go ahead and get the base type, unless it's a
163 // reference then it's just the size of the field. Pointer types have no need
164 // of this since they're a different type of qualification on the type.
165 if (BaseType
->getTag() == dwarf::DW_TAG_reference_type
||
166 BaseType
->getTag() == dwarf::DW_TAG_rvalue_reference_type
)
167 return Ty
->getSizeInBits();
169 return getBaseTypeSize(BaseType
);
172 static bool hasDebugInfo(const MachineModuleInfo
*MMI
,
173 const MachineFunction
*MF
) {
174 if (!MMI
->hasDebugInfo())
176 auto *SP
= MF
->getFunction().getSubprogram();
179 assert(SP
->getUnit());
180 auto EK
= SP
->getUnit()->getEmissionKind();
181 if (EK
== DICompileUnit::NoDebug
)
186 void DebugHandlerBase::beginFunction(const MachineFunction
*MF
) {
187 PrevInstBB
= nullptr;
189 if (!Asm
|| !hasDebugInfo(MMI
, MF
)) {
190 skippedNonDebugFunction();
194 // Grab the lexical scopes for the function, if we don't have any of those
195 // then we're not going to be able to do anything.
196 LScopes
.initialize(*MF
);
197 if (LScopes
.empty()) {
198 beginFunctionImpl(MF
);
202 // Make sure that each lexical scope will have a begin/end label.
203 identifyScopeMarkers();
205 // Calculate history for local variables.
206 assert(DbgValues
.empty() && "DbgValues map wasn't cleaned!");
207 assert(DbgLabels
.empty() && "DbgLabels map wasn't cleaned!");
208 calculateDbgEntityHistory(MF
, Asm
->MF
->getSubtarget().getRegisterInfo(),
209 DbgValues
, DbgLabels
);
210 LLVM_DEBUG(DbgValues
.dump());
212 // Request labels for the full history.
213 for (const auto &I
: DbgValues
) {
214 const auto &Ranges
= I
.second
;
218 auto IsDescribedByReg
= [](const MachineInstr
*MI
) {
219 return MI
->getOperand(0).isReg() && MI
->getOperand(0).getReg();
222 // The first mention of a function argument gets the CurrentFnBegin label,
223 // so arguments are visible when breaking at function entry.
225 // We do not change the label for values that are described by registers,
226 // as that could place them above their defining instructions. We should
227 // ideally not change the labels for constant debug values either, since
228 // doing that violates the ranges that are calculated in the history map.
229 // However, we currently do not emit debug values for constant arguments
230 // directly at the start of the function, so this code is still useful.
231 const DILocalVariable
*DIVar
= Ranges
.front().first
->getDebugVariable();
232 if (DIVar
->isParameter() &&
233 getDISubprogram(DIVar
->getScope())->describes(&MF
->getFunction())) {
234 if (!IsDescribedByReg(Ranges
.front().first
))
235 LabelsBeforeInsn
[Ranges
.front().first
] = Asm
->getFunctionBegin();
236 if (Ranges
.front().first
->getDebugExpression()->isFragment()) {
237 // Mark all non-overlapping initial fragments.
238 for (auto I
= Ranges
.begin(); I
!= Ranges
.end(); ++I
) {
239 const DIExpression
*Fragment
= I
->first
->getDebugExpression();
240 if (std::any_of(Ranges
.begin(), I
,
241 [&](DbgValueHistoryMap::InstrRange Pred
) {
242 return Fragment
->fragmentsOverlap(
243 Pred
.first
->getDebugExpression());
246 if (!IsDescribedByReg(I
->first
))
247 LabelsBeforeInsn
[I
->first
] = Asm
->getFunctionBegin();
252 for (const auto &Range
: Ranges
) {
253 requestLabelBeforeInsn(Range
.first
);
255 requestLabelAfterInsn(Range
.second
);
259 // Ensure there is a symbol before DBG_LABEL.
260 for (const auto &I
: DbgLabels
) {
261 const MachineInstr
*MI
= I
.second
;
262 requestLabelBeforeInsn(MI
);
265 PrevInstLoc
= DebugLoc();
266 PrevLabel
= Asm
->getFunctionBegin();
267 beginFunctionImpl(MF
);
270 void DebugHandlerBase::beginInstruction(const MachineInstr
*MI
) {
271 if (!MMI
->hasDebugInfo())
274 assert(CurMI
== nullptr);
277 // Insert labels where requested.
278 DenseMap
<const MachineInstr
*, MCSymbol
*>::iterator I
=
279 LabelsBeforeInsn
.find(MI
);
282 if (I
== LabelsBeforeInsn
.end())
285 // Label already assigned.
290 PrevLabel
= MMI
->getContext().createTempSymbol();
291 Asm
->OutStreamer
->EmitLabel(PrevLabel
);
293 I
->second
= PrevLabel
;
296 void DebugHandlerBase::endInstruction() {
297 if (!MMI
->hasDebugInfo())
300 assert(CurMI
!= nullptr);
301 // Don't create a new label after DBG_VALUE and other instructions that don't
303 if (!CurMI
->isMetaInstruction()) {
305 PrevInstBB
= CurMI
->getParent();
308 DenseMap
<const MachineInstr
*, MCSymbol
*>::iterator I
=
309 LabelsAfterInsn
.find(CurMI
);
313 if (I
== LabelsAfterInsn
.end())
316 // Label already assigned.
320 // We need a label after this instruction.
322 PrevLabel
= MMI
->getContext().createTempSymbol();
323 Asm
->OutStreamer
->EmitLabel(PrevLabel
);
325 I
->second
= PrevLabel
;
328 void DebugHandlerBase::endFunction(const MachineFunction
*MF
) {
329 if (hasDebugInfo(MMI
, MF
))
333 LabelsBeforeInsn
.clear();
334 LabelsAfterInsn
.clear();