blob: d30f106a939a3e77bceaddd26ab1d39208dbafa0 [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
66// Determine the relative position of the pieces described by P1 and P2.
67// Returns -1 if P1 is entirely before P2, 0 if P1 and P2 overlap,
68// 1 if P1 is entirely after P2.
69int DebugHandlerBase::pieceCmp(const DIExpression *P1, const DIExpression *P2) {
70 unsigned l1 = P1->getBitPieceOffset();
71 unsigned l2 = P2->getBitPieceOffset();
72 unsigned r1 = l1 + P1->getBitPieceSize();
73 unsigned r2 = l2 + P2->getBitPieceSize();
74 if (r1 <= l2)
75 return -1;
76 else if (r2 <= l1)
77 return 1;
78 else
79 return 0;
80}
81
82/// Determine whether two variable pieces overlap.
83bool DebugHandlerBase::piecesOverlap(const DIExpression *P1, const DIExpression *P2) {
84 if (!P1->isBitPiece() || !P2->isBitPiece())
85 return true;
86 return pieceCmp(P1, P2) == 0;
87}
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
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000118void DebugHandlerBase::beginFunction(const MachineFunction *MF) {
119 // Grab the lexical scopes for the function, if we don't have any of those
120 // then we're not going to be able to do anything.
121 LScopes.initialize(*MF);
122 if (LScopes.empty())
123 return;
124
125 // Make sure that each lexical scope will have a begin/end label.
126 identifyScopeMarkers();
127
128 // Calculate history for local variables.
129 assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
130 calculateDbgValueHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
131 DbgValues);
132
133 // Request labels for the full history.
134 for (const auto &I : DbgValues) {
135 const auto &Ranges = I.second;
136 if (Ranges.empty())
137 continue;
138
139 // The first mention of a function argument gets the CurrentFnBegin
140 // label, so arguments are visible when breaking at function entry.
141 const DILocalVariable *DIVar = Ranges.front().first->getDebugVariable();
142 if (DIVar->isParameter() &&
143 getDISubprogram(DIVar->getScope())->describes(MF->getFunction())) {
144 LabelsBeforeInsn[Ranges.front().first] = Asm->getFunctionBegin();
145 if (Ranges.front().first->getDebugExpression()->isBitPiece()) {
146 // Mark all non-overlapping initial pieces.
147 for (auto I = Ranges.begin(); I != Ranges.end(); ++I) {
148 const DIExpression *Piece = I->first->getDebugExpression();
149 if (std::all_of(Ranges.begin(), I,
150 [&](DbgValueHistoryMap::InstrRange Pred) {
151 return !piecesOverlap(Piece, Pred.first->getDebugExpression());
152 }))
153 LabelsBeforeInsn[I->first] = Asm->getFunctionBegin();
154 else
155 break;
156 }
157 }
158 }
159
160 for (const auto &Range : Ranges) {
161 requestLabelBeforeInsn(Range.first);
162 if (Range.second)
163 requestLabelAfterInsn(Range.second);
164 }
165 }
166
167 PrevInstLoc = DebugLoc();
168 PrevLabel = Asm->getFunctionBegin();
169}
170
171void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {
172 if (!MMI->hasDebugInfo())
173 return;
174
175 assert(CurMI == nullptr);
176 CurMI = MI;
177
178 // Insert labels where requested.
179 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
180 LabelsBeforeInsn.find(MI);
181
182 // No label needed.
183 if (I == LabelsBeforeInsn.end())
184 return;
185
186 // Label already assigned.
187 if (I->second)
188 return;
189
190 if (!PrevLabel) {
191 PrevLabel = MMI->getContext().createTempSymbol();
192 Asm->OutStreamer->EmitLabel(PrevLabel);
193 }
194 I->second = PrevLabel;
195}
196
197void DebugHandlerBase::endInstruction() {
198 if (!MMI->hasDebugInfo())
199 return;
200
201 assert(CurMI != nullptr);
202 // Don't create a new label after DBG_VALUE instructions.
203 // They don't generate code.
Kostya Serebryanyb66cb882016-12-01 02:06:56 +0000204 if (!CurMI->isDebugValue())
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000205 PrevLabel = nullptr;
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000206
207 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
208 LabelsAfterInsn.find(CurMI);
209 CurMI = nullptr;
210
211 // No label needed.
212 if (I == LabelsAfterInsn.end())
213 return;
214
215 // Label already assigned.
216 if (I->second)
217 return;
218
219 // We need a label after this instruction.
220 if (!PrevLabel) {
221 PrevLabel = MMI->getContext().createTempSymbol();
222 Asm->OutStreamer->EmitLabel(PrevLabel);
223 }
224 I->second = PrevLabel;
225}
226
227void DebugHandlerBase::endFunction(const MachineFunction *MF) {
228 DbgValues.clear();
229 LabelsBeforeInsn.clear();
230 LabelsAfterInsn.clear();
231}