blob: 16ffe2e15acde08dc80a5ab1eba8e30327c602a9 [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 Klecknerf9c275f2016-02-10 20:55:49 +000021#include "llvm/Target/TargetSubtargetInfo.h"
22
23using namespace llvm;
24
25DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {}
26
27// Each LexicalScope has first instruction and last instruction to mark
28// beginning and end of a scope respectively. Create an inverse map that list
29// scopes starts (and ends) with an instruction. One instruction may start (or
30// end) multiple scopes. Ignore scopes that are not reachable.
31void DebugHandlerBase::identifyScopeMarkers() {
32 SmallVector<LexicalScope *, 4> WorkList;
33 WorkList.push_back(LScopes.getCurrentFunctionScope());
34 while (!WorkList.empty()) {
35 LexicalScope *S = WorkList.pop_back_val();
36
37 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
38 if (!Children.empty())
39 WorkList.append(Children.begin(), Children.end());
40
41 if (S->isAbstractScope())
42 continue;
43
44 for (const InsnRange &R : S->getRanges()) {
45 assert(R.first && "InsnRange does not have first instruction!");
46 assert(R.second && "InsnRange does not have second instruction!");
47 requestLabelBeforeInsn(R.first);
48 requestLabelAfterInsn(R.second);
49 }
50 }
51}
52
53// Return Label preceding the instruction.
54MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) {
55 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
56 assert(Label && "Didn't insert label before instruction");
57 return Label;
58}
59
60// Return Label immediately following the instruction.
61MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) {
62 return LabelsAfterInsn.lookup(MI);
63}
64
65// Determine the relative position of the pieces described by P1 and P2.
66// Returns -1 if P1 is entirely before P2, 0 if P1 and P2 overlap,
67// 1 if P1 is entirely after P2.
68int DebugHandlerBase::pieceCmp(const DIExpression *P1, const DIExpression *P2) {
69 unsigned l1 = P1->getBitPieceOffset();
70 unsigned l2 = P2->getBitPieceOffset();
71 unsigned r1 = l1 + P1->getBitPieceSize();
72 unsigned r2 = l2 + P2->getBitPieceSize();
73 if (r1 <= l2)
74 return -1;
75 else if (r2 <= l1)
76 return 1;
77 else
78 return 0;
79}
80
81/// Determine whether two variable pieces overlap.
82bool DebugHandlerBase::piecesOverlap(const DIExpression *P1, const DIExpression *P2) {
83 if (!P1->isBitPiece() || !P2->isBitPiece())
84 return true;
85 return pieceCmp(P1, P2) == 0;
86}
87
Amjad Aboudacee5682016-07-12 12:06:34 +000088/// If this type is derived from a base type then return base type size.
89uint64_t DebugHandlerBase::getBaseTypeSize(const DITypeRef TyRef) {
90 DIType *Ty = TyRef.resolve();
91 assert(Ty);
92 DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty);
93 if (!DDTy)
94 return Ty->getSizeInBits();
95
96 unsigned Tag = DDTy->getTag();
97
98 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
99 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
100 Tag != dwarf::DW_TAG_restrict_type)
101 return DDTy->getSizeInBits();
102
103 DIType *BaseType = DDTy->getBaseType().resolve();
104
105 assert(BaseType && "Unexpected invalid base type");
106
107 // If this is a derived type, go ahead and get the base type, unless it's a
108 // reference then it's just the size of the field. Pointer types have no need
109 // of this since they're a different type of qualification on the type.
110 if (BaseType->getTag() == dwarf::DW_TAG_reference_type ||
111 BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type)
112 return Ty->getSizeInBits();
113
114 return getBaseTypeSize(BaseType);
115}
116
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000117void DebugHandlerBase::beginFunction(const MachineFunction *MF) {
118 // Grab the lexical scopes for the function, if we don't have any of those
119 // then we're not going to be able to do anything.
120 LScopes.initialize(*MF);
121 if (LScopes.empty())
122 return;
123
124 // Make sure that each lexical scope will have a begin/end label.
125 identifyScopeMarkers();
126
127 // Calculate history for local variables.
128 assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
129 calculateDbgValueHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
130 DbgValues);
131
132 // Request labels for the full history.
133 for (const auto &I : DbgValues) {
134 const auto &Ranges = I.second;
135 if (Ranges.empty())
136 continue;
137
138 // The first mention of a function argument gets the CurrentFnBegin
139 // label, so arguments are visible when breaking at function entry.
140 const DILocalVariable *DIVar = Ranges.front().first->getDebugVariable();
141 if (DIVar->isParameter() &&
142 getDISubprogram(DIVar->getScope())->describes(MF->getFunction())) {
143 LabelsBeforeInsn[Ranges.front().first] = Asm->getFunctionBegin();
144 if (Ranges.front().first->getDebugExpression()->isBitPiece()) {
145 // Mark all non-overlapping initial pieces.
146 for (auto I = Ranges.begin(); I != Ranges.end(); ++I) {
147 const DIExpression *Piece = I->first->getDebugExpression();
148 if (std::all_of(Ranges.begin(), I,
149 [&](DbgValueHistoryMap::InstrRange Pred) {
150 return !piecesOverlap(Piece, Pred.first->getDebugExpression());
151 }))
152 LabelsBeforeInsn[I->first] = Asm->getFunctionBegin();
153 else
154 break;
155 }
156 }
157 }
158
159 for (const auto &Range : Ranges) {
160 requestLabelBeforeInsn(Range.first);
161 if (Range.second)
162 requestLabelAfterInsn(Range.second);
163 }
164 }
165
166 PrevInstLoc = DebugLoc();
167 PrevLabel = Asm->getFunctionBegin();
168}
169
170void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {
171 if (!MMI->hasDebugInfo())
172 return;
173
174 assert(CurMI == nullptr);
175 CurMI = MI;
176
177 // Insert labels where requested.
178 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
179 LabelsBeforeInsn.find(MI);
180
181 // No label needed.
182 if (I == LabelsBeforeInsn.end())
183 return;
184
185 // Label already assigned.
186 if (I->second)
187 return;
188
189 if (!PrevLabel) {
190 PrevLabel = MMI->getContext().createTempSymbol();
191 Asm->OutStreamer->EmitLabel(PrevLabel);
192 }
193 I->second = PrevLabel;
194}
195
196void DebugHandlerBase::endInstruction() {
197 if (!MMI->hasDebugInfo())
198 return;
199
200 assert(CurMI != nullptr);
201 // Don't create a new label after DBG_VALUE instructions.
202 // They don't generate code.
203 if (!CurMI->isDebugValue())
204 PrevLabel = nullptr;
205
206 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
207 LabelsAfterInsn.find(CurMI);
208 CurMI = nullptr;
209
210 // No label needed.
211 if (I == LabelsAfterInsn.end())
212 return;
213
214 // Label already assigned.
215 if (I->second)
216 return;
217
218 // We need a label after this instruction.
219 if (!PrevLabel) {
220 PrevLabel = MMI->getContext().createTempSymbol();
221 Asm->OutStreamer->EmitLabel(PrevLabel);
222 }
223 I->second = PrevLabel;
224}
225
226void DebugHandlerBase::endFunction(const MachineFunction *MF) {
227 DbgValues.clear();
228 LabelsBeforeInsn.clear();
229 LabelsAfterInsn.clear();
230}