blob: aa8b8fbb86981211e5c3bb9bf8d2a8805e7df8a4 [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"
16#include "llvm/CodeGen/AsmPrinter.h"
17#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/CodeGen/MachineInstr.h"
19#include "llvm/CodeGen/MachineModuleInfo.h"
Reid Kleckner28865802016-04-14 18:29:59 +000020#include "llvm/IR/DebugInfo.h"
Reid Klecknera5b1eef2016-08-26 17:58:37 +000021#include "llvm/MC/MCStreamer.h"
Reid Klecknerf9c275f2016-02-10 20:55:49 +000022#include "llvm/Target/TargetSubtargetInfo.h"
23
24using namespace llvm;
25
26DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {}
27
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.
32void DebugHandlerBase::identifyScopeMarkers() {
33 SmallVector<LexicalScope *, 4> WorkList;
34 WorkList.push_back(LScopes.getCurrentFunctionScope());
35 while (!WorkList.empty()) {
36 LexicalScope *S = WorkList.pop_back_val();
37
38 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
39 if (!Children.empty())
40 WorkList.append(Children.begin(), Children.end());
41
42 if (S->isAbstractScope())
43 continue;
44
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);
50 }
51 }
52}
53
54// Return Label preceding the instruction.
55MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) {
56 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
57 assert(Label && "Didn't insert label before instruction");
58 return Label;
59}
60
61// Return Label immediately following the instruction.
62MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) {
63 return LabelsAfterInsn.lookup(MI);
64}
65
Adrian Prantl941fa752016-12-05 18:04:47 +000066int DebugHandlerBase::fragmentCmp(const DIExpression *P1,
67 const DIExpression *P2) {
Adrian Prantl49797ca2016-12-22 05:27:12 +000068 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;
Reid Klecknerf9c275f2016-02-10 20:55:49 +000074 if (r1 <= l2)
75 return -1;
76 else if (r2 <= l1)
77 return 1;
78 else
79 return 0;
80}
81
Adrian Prantl941fa752016-12-05 18:04:47 +000082bool DebugHandlerBase::fragmentsOverlap(const DIExpression *P1,
83 const DIExpression *P2) {
84 if (!P1->isFragment() || !P2->isFragment())
Reid Klecknerf9c275f2016-02-10 20:55:49 +000085 return true;
Adrian Prantl941fa752016-12-05 18:04:47 +000086 return fragmentCmp(P1, P2) == 0;
Reid Klecknerf9c275f2016-02-10 20:55:49 +000087}
88
Amjad Aboudacee5682016-07-12 12:06:34 +000089/// If this type is derived from a base type then return base type size.
90uint64_t DebugHandlerBase::getBaseTypeSize(const DITypeRef TyRef) {
91 DIType *Ty = TyRef.resolve();
92 assert(Ty);
93 DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty);
94 if (!DDTy)
95 return Ty->getSizeInBits();
96
97 unsigned Tag = DDTy->getTag();
98
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 &&
Victor Leschuke1156c22016-10-31 19:09:38 +0000101 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type)
Amjad Aboudacee5682016-07-12 12:06:34 +0000102 return DDTy->getSizeInBits();
103
104 DIType *BaseType = DDTy->getBaseType().resolve();
105
106 assert(BaseType && "Unexpected invalid base type");
107
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();
114
115 return getBaseTypeSize(BaseType);
116}
117
David Blaikieb2fbb4b2017-02-16 18:48:33 +0000118bool hasDebugInfo(const MachineModuleInfo *MMI, const MachineFunction *MF) {
119 if (!MMI->hasDebugInfo())
120 return false;
121 auto *SP = MF->getFunction()->getSubprogram();
122 if (!SP)
123 return false;
124 assert(SP->getUnit());
125 auto EK = SP->getUnit()->getEmissionKind();
126 if (EK == DICompileUnit::NoDebug)
127 return false;
128 return true;
129}
130
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000131void DebugHandlerBase::beginFunction(const MachineFunction *MF) {
David Blaikieb2fbb4b2017-02-16 18:48:33 +0000132 assert(Asm);
133
134 if (!hasDebugInfo(MMI, MF)) {
135 skippedNonDebugFunction();
136 return;
137 }
138
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000139 // Grab the lexical scopes for the function, if we don't have any of those
140 // then we're not going to be able to do anything.
141 LScopes.initialize(*MF);
David Blaikieb2fbb4b2017-02-16 18:48:33 +0000142 if (LScopes.empty()) {
143 beginFunctionImpl(MF);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000144 return;
David Blaikieb2fbb4b2017-02-16 18:48:33 +0000145 }
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000146
147 // Make sure that each lexical scope will have a begin/end label.
148 identifyScopeMarkers();
149
150 // Calculate history for local variables.
151 assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
152 calculateDbgValueHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
153 DbgValues);
154
155 // Request labels for the full history.
156 for (const auto &I : DbgValues) {
157 const auto &Ranges = I.second;
158 if (Ranges.empty())
159 continue;
160
161 // The first mention of a function argument gets the CurrentFnBegin
162 // label, so arguments are visible when breaking at function entry.
163 const DILocalVariable *DIVar = Ranges.front().first->getDebugVariable();
164 if (DIVar->isParameter() &&
165 getDISubprogram(DIVar->getScope())->describes(MF->getFunction())) {
166 LabelsBeforeInsn[Ranges.front().first] = Asm->getFunctionBegin();
Adrian Prantl941fa752016-12-05 18:04:47 +0000167 if (Ranges.front().first->getDebugExpression()->isFragment()) {
168 // Mark all non-overlapping initial fragments.
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000169 for (auto I = Ranges.begin(); I != Ranges.end(); ++I) {
Adrian Prantl941fa752016-12-05 18:04:47 +0000170 const DIExpression *Fragment = I->first->getDebugExpression();
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000171 if (std::all_of(Ranges.begin(), I,
172 [&](DbgValueHistoryMap::InstrRange Pred) {
Adrian Prantl941fa752016-12-05 18:04:47 +0000173 return !fragmentsOverlap(
174 Fragment, Pred.first->getDebugExpression());
175 }))
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000176 LabelsBeforeInsn[I->first] = Asm->getFunctionBegin();
177 else
178 break;
179 }
180 }
181 }
182
183 for (const auto &Range : Ranges) {
184 requestLabelBeforeInsn(Range.first);
185 if (Range.second)
186 requestLabelAfterInsn(Range.second);
187 }
188 }
189
190 PrevInstLoc = DebugLoc();
191 PrevLabel = Asm->getFunctionBegin();
David Blaikieb2fbb4b2017-02-16 18:48:33 +0000192 beginFunctionImpl(MF);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000193}
194
195void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {
196 if (!MMI->hasDebugInfo())
197 return;
198
199 assert(CurMI == nullptr);
200 CurMI = MI;
201
202 // Insert labels where requested.
203 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
204 LabelsBeforeInsn.find(MI);
205
206 // No label needed.
207 if (I == LabelsBeforeInsn.end())
208 return;
209
210 // Label already assigned.
211 if (I->second)
212 return;
213
214 if (!PrevLabel) {
215 PrevLabel = MMI->getContext().createTempSymbol();
216 Asm->OutStreamer->EmitLabel(PrevLabel);
217 }
218 I->second = PrevLabel;
219}
220
221void DebugHandlerBase::endInstruction() {
222 if (!MMI->hasDebugInfo())
223 return;
224
225 assert(CurMI != nullptr);
226 // Don't create a new label after DBG_VALUE instructions.
227 // They don't generate code.
Paul Robinsonac7fe5e2016-12-12 20:49:11 +0000228 if (!CurMI->isDebugValue()) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000229 PrevLabel = nullptr;
Paul Robinsonac7fe5e2016-12-12 20:49:11 +0000230 PrevInstBB = CurMI->getParent();
231 }
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000232
233 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
234 LabelsAfterInsn.find(CurMI);
235 CurMI = nullptr;
236
237 // No label needed.
238 if (I == LabelsAfterInsn.end())
239 return;
240
241 // Label already assigned.
242 if (I->second)
243 return;
244
245 // We need a label after this instruction.
246 if (!PrevLabel) {
247 PrevLabel = MMI->getContext().createTempSymbol();
248 Asm->OutStreamer->EmitLabel(PrevLabel);
249 }
250 I->second = PrevLabel;
251}
252
253void DebugHandlerBase::endFunction(const MachineFunction *MF) {
David Blaikieb2fbb4b2017-02-16 18:48:33 +0000254 if (hasDebugInfo(MMI, MF))
255 endFunctionImpl(MF);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000256 DbgValues.clear();
257 LabelsBeforeInsn.clear();
258 LabelsAfterInsn.clear();
259}