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/CodeGen/AsmPrinter.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineInstr.h"
19 #include "llvm/CodeGen/MachineModuleInfo.h"
20 #include "llvm/IR/DebugInfo.h"
21 #include "llvm/MC/MCStreamer.h"
22 #include "llvm/Target/TargetSubtargetInfo.h"
26 DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {}
28 // Each LexicalScope has first instruction and last instruction to mark
29 // beginning and end of a scope respectively. Create an inverse map that list
30 // scopes starts (and ends) with an instruction. One instruction may start (or
31 // end) multiple scopes. Ignore scopes that are not reachable.
32 void DebugHandlerBase::identifyScopeMarkers() {
33 SmallVector<LexicalScope *, 4> WorkList;
34 WorkList.push_back(LScopes.getCurrentFunctionScope());
35 while (!WorkList.empty()) {
36 LexicalScope *S = WorkList.pop_back_val();
38 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
39 if (!Children.empty())
40 WorkList.append(Children.begin(), Children.end());
42 if (S->isAbstractScope())
45 for (const InsnRange &R : S->getRanges()) {
46 assert(R.first && "InsnRange does not have first instruction!");
47 assert(R.second && "InsnRange does not have second instruction!");
48 requestLabelBeforeInsn(R.first);
49 requestLabelAfterInsn(R.second);
54 // Return Label preceding the instruction.
55 MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) {
56 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
57 assert(Label && "Didn't insert label before instruction");
61 // Return Label immediately following the instruction.
62 MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) {
63 return LabelsAfterInsn.lookup(MI);
66 int DebugHandlerBase::fragmentCmp(const DIExpression *P1,
67 const DIExpression *P2) {
68 auto Fragment1 = *P1->getFragmentInfo();
69 auto Fragment2 = *P2->getFragmentInfo();
70 unsigned l1 = Fragment1.OffsetInBits;
71 unsigned l2 = Fragment2.OffsetInBits;
72 unsigned r1 = l1 + Fragment1.SizeInBits;
73 unsigned r2 = l2 + Fragment2.SizeInBits;
82 bool DebugHandlerBase::fragmentsOverlap(const DIExpression *P1,
83 const DIExpression *P2) {
84 if (!P1->isFragment() || !P2->isFragment())
86 return fragmentCmp(P1, P2) == 0;
89 /// If this type is derived from a base type then return base type size.
90 uint64_t DebugHandlerBase::getBaseTypeSize(const DITypeRef TyRef) {
91 DIType *Ty = TyRef.resolve();
93 DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty);
95 return Ty->getSizeInBits();
97 unsigned Tag = DDTy->getTag();
99 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
100 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
101 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type)
102 return DDTy->getSizeInBits();
104 DIType *BaseType = DDTy->getBaseType().resolve();
106 assert(BaseType && "Unexpected invalid base type");
108 // If this is a derived type, go ahead and get the base type, unless it's a
109 // reference then it's just the size of the field. Pointer types have no need
110 // of this since they're a different type of qualification on the type.
111 if (BaseType->getTag() == dwarf::DW_TAG_reference_type ||
112 BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type)
113 return Ty->getSizeInBits();
115 return getBaseTypeSize(BaseType);
118 static bool hasDebugInfo(const MachineModuleInfo *MMI,
119 const MachineFunction *MF) {
120 if (!MMI->hasDebugInfo())
122 auto *SP = MF->getFunction()->getSubprogram();
125 assert(SP->getUnit());
126 auto EK = SP->getUnit()->getEmissionKind();
127 if (EK == DICompileUnit::NoDebug)
132 void DebugHandlerBase::beginFunction(const MachineFunction *MF) {
133 PrevInstBB = nullptr;
135 if (!Asm || !hasDebugInfo(MMI, MF)) {
136 skippedNonDebugFunction();
140 // Grab the lexical scopes for the function, if we don't have any of those
141 // then we're not going to be able to do anything.
142 LScopes.initialize(*MF);
143 if (LScopes.empty()) {
144 beginFunctionImpl(MF);
148 // Make sure that each lexical scope will have a begin/end label.
149 identifyScopeMarkers();
151 // Calculate history for local variables.
152 assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
153 calculateDbgValueHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
156 // Request labels for the full history.
157 for (const auto &I : DbgValues) {
158 const auto &Ranges = I.second;
162 // The first mention of a function argument gets the CurrentFnBegin
163 // label, so arguments are visible when breaking at function entry.
164 const DILocalVariable *DIVar = Ranges.front().first->getDebugVariable();
165 if (DIVar->isParameter() &&
166 getDISubprogram(DIVar->getScope())->describes(MF->getFunction())) {
167 LabelsBeforeInsn[Ranges.front().first] = Asm->getFunctionBegin();
168 if (Ranges.front().first->getDebugExpression()->isFragment()) {
169 // Mark all non-overlapping initial fragments.
170 for (auto I = Ranges.begin(); I != Ranges.end(); ++I) {
171 const DIExpression *Fragment = I->first->getDebugExpression();
172 if (std::all_of(Ranges.begin(), I,
173 [&](DbgValueHistoryMap::InstrRange Pred) {
174 return !fragmentsOverlap(
175 Fragment, Pred.first->getDebugExpression());
177 LabelsBeforeInsn[I->first] = Asm->getFunctionBegin();
184 for (const auto &Range : Ranges) {
185 requestLabelBeforeInsn(Range.first);
187 requestLabelAfterInsn(Range.second);
191 PrevInstLoc = DebugLoc();
192 PrevLabel = Asm->getFunctionBegin();
193 beginFunctionImpl(MF);
196 void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {
197 if (!MMI->hasDebugInfo())
200 assert(CurMI == nullptr);
203 // Insert labels where requested.
204 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
205 LabelsBeforeInsn.find(MI);
208 if (I == LabelsBeforeInsn.end())
211 // Label already assigned.
216 PrevLabel = MMI->getContext().createTempSymbol();
217 Asm->OutStreamer->EmitLabel(PrevLabel);
219 I->second = PrevLabel;
222 void DebugHandlerBase::endInstruction() {
223 if (!MMI->hasDebugInfo())
226 assert(CurMI != nullptr);
227 // Don't create a new label after DBG_VALUE and other instructions that don't
229 if (!CurMI->isMetaInstruction()) {
231 PrevInstBB = CurMI->getParent();
234 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
235 LabelsAfterInsn.find(CurMI);
239 if (I == LabelsAfterInsn.end())
242 // Label already assigned.
246 // We need a label after this instruction.
248 PrevLabel = MMI->getContext().createTempSymbol();
249 Asm->OutStreamer->EmitLabel(PrevLabel);
251 I->second = PrevLabel;
254 void DebugHandlerBase::endFunction(const MachineFunction *MF) {
255 if (hasDebugInfo(MMI, MF))
258 LabelsBeforeInsn.clear();
259 LabelsAfterInsn.clear();