1 //===-- llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp -------*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Common functionality for different debug information format backends.
11 // LLVM currently supports DWARF and CodeView.
13 //===----------------------------------------------------------------------===//
15 #include "DebugHandlerBase.h"
16 #include "llvm/ADT/Optional.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/CodeGen/AsmPrinter.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineInstr.h"
21 #include "llvm/CodeGen/MachineModuleInfo.h"
22 #include "llvm/IR/DebugInfo.h"
23 #include "llvm/MC/MCStreamer.h"
24 #include "llvm/Target/TargetSubtargetInfo.h"
28 Optional
<DbgVariableLocation
>
29 DbgVariableLocation::extractFromMachineInstruction(
30 const MachineInstr
&Instruction
) {
31 DbgVariableLocation Location
;
32 if (!Instruction
.isDebugValue())
34 if (!Instruction
.getOperand(0).isReg())
36 Location
.Register
= Instruction
.getOperand(0).getReg();
37 Location
.FragmentInfo
.reset();
38 // We only handle expressions generated by DIExpression::appendOffset,
39 // which doesn't require a full stack machine.
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);
48 if (Op
!= DIExpr
->expr_op_end()) {
49 switch (Op
->getOp()) {
50 case dwarf::DW_OP_minus
:
53 case dwarf::DW_OP_plus
:
61 case dwarf::DW_OP_plus_uconst
:
62 Offset
+= Op
->getArg(0);
64 case dwarf::DW_OP_LLVM_fragment
:
65 Location
.FragmentInfo
= {Op
->getArg(1), Op
->getArg(0)};
67 case dwarf::DW_OP_deref
:
68 Location
.LoadChain
.push_back(Offset
);
77 // Do one final implicit DW_OP_deref if this was an indirect DBG_VALUE
79 // FIXME: Replace these with DIExpression.
80 if (Instruction
.isIndirectDebugValue())
81 Location
.LoadChain
.push_back(Offset
);
86 DebugHandlerBase::DebugHandlerBase(AsmPrinter
*A
) : Asm(A
), MMI(Asm
->MMI
) {}
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.
92 void DebugHandlerBase::identifyScopeMarkers() {
93 SmallVector
<LexicalScope
*, 4> WorkList
;
94 WorkList
.push_back(LScopes
.getCurrentFunctionScope());
95 while (!WorkList
.empty()) {
96 LexicalScope
*S
= WorkList
.pop_back_val();
98 const SmallVectorImpl
<LexicalScope
*> &Children
= S
->getChildren();
99 if (!Children
.empty())
100 WorkList
.append(Children
.begin(), Children
.end());
102 if (S
->isAbstractScope())
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
);
114 // Return Label preceding the instruction.
115 MCSymbol
*DebugHandlerBase::getLabelBeforeInsn(const MachineInstr
*MI
) {
116 MCSymbol
*Label
= LabelsBeforeInsn
.lookup(MI
);
117 assert(Label
&& "Didn't insert label before instruction");
121 // Return Label immediately following the instruction.
122 MCSymbol
*DebugHandlerBase::getLabelAfterInsn(const MachineInstr
*MI
) {
123 return LabelsAfterInsn
.lookup(MI
);
126 int DebugHandlerBase::fragmentCmp(const DIExpression
*P1
,
127 const DIExpression
*P2
) {
128 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
;
142 bool DebugHandlerBase::fragmentsOverlap(const DIExpression
*P1
,
143 const DIExpression
*P2
) {
144 if (!P1
->isFragment() || !P2
->isFragment())
146 return fragmentCmp(P1
, P2
) == 0;
149 /// If this type is derived from a base type then return base type size.
150 uint64_t DebugHandlerBase::getBaseTypeSize(const DITypeRef TyRef
) {
151 DIType
*Ty
= TyRef
.resolve();
153 DIDerivedType
*DDTy
= dyn_cast
<DIDerivedType
>(Ty
);
155 return Ty
->getSizeInBits();
157 unsigned Tag
= DDTy
->getTag();
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
&&
161 Tag
!= dwarf::DW_TAG_restrict_type
&& Tag
!= dwarf::DW_TAG_atomic_type
)
162 return DDTy
->getSizeInBits();
164 DIType
*BaseType
= DDTy
->getBaseType().resolve();
166 assert(BaseType
&& "Unexpected invalid base type");
168 // If this is a derived type, go ahead and get the base type, unless it's a
169 // reference then it's just the size of the field. Pointer types have no need
170 // of this since they're a different type of qualification on the type.
171 if (BaseType
->getTag() == dwarf::DW_TAG_reference_type
||
172 BaseType
->getTag() == dwarf::DW_TAG_rvalue_reference_type
)
173 return Ty
->getSizeInBits();
175 return getBaseTypeSize(BaseType
);
178 static bool hasDebugInfo(const MachineModuleInfo
*MMI
,
179 const MachineFunction
*MF
) {
180 if (!MMI
->hasDebugInfo())
182 auto *SP
= MF
->getFunction()->getSubprogram();
185 assert(SP
->getUnit());
186 auto EK
= SP
->getUnit()->getEmissionKind();
187 if (EK
== DICompileUnit::NoDebug
)
192 void DebugHandlerBase::beginFunction(const MachineFunction
*MF
) {
193 PrevInstBB
= nullptr;
195 if (!Asm
|| !hasDebugInfo(MMI
, MF
)) {
196 skippedNonDebugFunction();
200 // Grab the lexical scopes for the function, if we don't have any of those
201 // then we're not going to be able to do anything.
202 LScopes
.initialize(*MF
);
203 if (LScopes
.empty()) {
204 beginFunctionImpl(MF
);
208 // Make sure that each lexical scope will have a begin/end label.
209 identifyScopeMarkers();
211 // Calculate history for local variables.
212 assert(DbgValues
.empty() && "DbgValues map wasn't cleaned!");
213 calculateDbgValueHistory(MF
, Asm
->MF
->getSubtarget().getRegisterInfo(),
216 // Request labels for the full history.
217 for (const auto &I
: DbgValues
) {
218 const auto &Ranges
= I
.second
;
222 // The first mention of a function argument gets the CurrentFnBegin
223 // label, so arguments are visible when breaking at function entry.
224 const DILocalVariable
*DIVar
= Ranges
.front().first
->getDebugVariable();
225 if (DIVar
->isParameter() &&
226 getDISubprogram(DIVar
->getScope())->describes(MF
->getFunction())) {
227 LabelsBeforeInsn
[Ranges
.front().first
] = Asm
->getFunctionBegin();
228 if (Ranges
.front().first
->getDebugExpression()->isFragment()) {
229 // Mark all non-overlapping initial fragments.
230 for (auto I
= Ranges
.begin(); I
!= Ranges
.end(); ++I
) {
231 const DIExpression
*Fragment
= I
->first
->getDebugExpression();
232 if (std::all_of(Ranges
.begin(), I
,
233 [&](DbgValueHistoryMap::InstrRange Pred
) {
234 return !fragmentsOverlap(
235 Fragment
, Pred
.first
->getDebugExpression());
237 LabelsBeforeInsn
[I
->first
] = Asm
->getFunctionBegin();
244 for (const auto &Range
: Ranges
) {
245 requestLabelBeforeInsn(Range
.first
);
247 requestLabelAfterInsn(Range
.second
);
251 PrevInstLoc
= DebugLoc();
252 PrevLabel
= Asm
->getFunctionBegin();
253 beginFunctionImpl(MF
);
256 void DebugHandlerBase::beginInstruction(const MachineInstr
*MI
) {
257 if (!MMI
->hasDebugInfo())
260 assert(CurMI
== nullptr);
263 // Insert labels where requested.
264 DenseMap
<const MachineInstr
*, MCSymbol
*>::iterator I
=
265 LabelsBeforeInsn
.find(MI
);
268 if (I
== LabelsBeforeInsn
.end())
271 // Label already assigned.
276 PrevLabel
= MMI
->getContext().createTempSymbol();
277 Asm
->OutStreamer
->EmitLabel(PrevLabel
);
279 I
->second
= PrevLabel
;
282 void DebugHandlerBase::endInstruction() {
283 if (!MMI
->hasDebugInfo())
286 assert(CurMI
!= nullptr);
287 // Don't create a new label after DBG_VALUE and other instructions that don't
289 if (!CurMI
->isMetaInstruction()) {
291 PrevInstBB
= CurMI
->getParent();
294 DenseMap
<const MachineInstr
*, MCSymbol
*>::iterator I
=
295 LabelsAfterInsn
.find(CurMI
);
299 if (I
== LabelsAfterInsn
.end())
302 // Label already assigned.
306 // We need a label after this instruction.
308 PrevLabel
= MMI
->getContext().createTempSymbol();
309 Asm
->OutStreamer
->EmitLabel(PrevLabel
);
311 I
->second
= PrevLabel
;
314 void DebugHandlerBase::endFunction(const MachineFunction
*MF
) {
315 if (hasDebugInfo(MMI
, MF
))
318 LabelsBeforeInsn
.clear();
319 LabelsAfterInsn
.clear();