[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / llvm / lib / CodeGen / AsmPrinter / DebugHandlerBase.cpp
blob8ebbed974abbd549f0ff25dd7a4dfb6fad5dfef1
1 //===-- llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp -------*- C++ -*--===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Common functionality for different debug information format backends.
10 // LLVM currently supports DWARF and CodeView.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/DebugHandlerBase.h"
15 #include "llvm/ADT/Optional.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/CodeGen/TargetSubtargetInfo.h"
21 #include "llvm/IR/DebugInfo.h"
22 #include "llvm/MC/MCStreamer.h"
23 #include "llvm/Support/CommandLine.h"
25 using namespace llvm;
27 #define DEBUG_TYPE "dwarfdebug"
29 /// If true, we drop variable location ranges which exist entirely outside the
30 /// variable's lexical scope instruction ranges.
31 static cl::opt<bool> TrimVarLocs("trim-var-locs", cl::Hidden, cl::init(true));
33 Optional<DbgVariableLocation>
34 DbgVariableLocation::extractFromMachineInstruction(
35 const MachineInstr &Instruction) {
36 DbgVariableLocation Location;
37 // Variables calculated from multiple locations can't be represented here.
38 if (Instruction.getNumDebugOperands() != 1)
39 return None;
40 if (!Instruction.getDebugOperand(0).isReg())
41 return None;
42 Location.Register = Instruction.getDebugOperand(0).getReg();
43 Location.FragmentInfo.reset();
44 // We only handle expressions generated by DIExpression::appendOffset,
45 // which doesn't require a full stack machine.
46 int64_t Offset = 0;
47 const DIExpression *DIExpr = Instruction.getDebugExpression();
48 auto Op = DIExpr->expr_op_begin();
49 // We can handle a DBG_VALUE_LIST iff it has exactly one location operand that
50 // appears exactly once at the start of the expression.
51 if (Instruction.isDebugValueList()) {
52 if (Instruction.getNumDebugOperands() == 1 &&
53 Op->getOp() == dwarf::DW_OP_LLVM_arg)
54 ++Op;
55 else
56 return None;
58 while (Op != DIExpr->expr_op_end()) {
59 switch (Op->getOp()) {
60 case dwarf::DW_OP_constu: {
61 int Value = Op->getArg(0);
62 ++Op;
63 if (Op != DIExpr->expr_op_end()) {
64 switch (Op->getOp()) {
65 case dwarf::DW_OP_minus:
66 Offset -= Value;
67 break;
68 case dwarf::DW_OP_plus:
69 Offset += Value;
70 break;
71 default:
72 continue;
75 } break;
76 case dwarf::DW_OP_plus_uconst:
77 Offset += Op->getArg(0);
78 break;
79 case dwarf::DW_OP_LLVM_fragment:
80 Location.FragmentInfo = {Op->getArg(1), Op->getArg(0)};
81 break;
82 case dwarf::DW_OP_deref:
83 Location.LoadChain.push_back(Offset);
84 Offset = 0;
85 break;
86 default:
87 return None;
89 ++Op;
92 // Do one final implicit DW_OP_deref if this was an indirect DBG_VALUE
93 // instruction.
94 // FIXME: Replace these with DIExpression.
95 if (Instruction.isIndirectDebugValue())
96 Location.LoadChain.push_back(Offset);
98 return Location;
101 DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {}
103 void DebugHandlerBase::beginModule(Module *M) {
104 if (M->debug_compile_units().empty())
105 Asm = nullptr;
108 // Each LexicalScope has first instruction and last instruction to mark
109 // beginning and end of a scope respectively. Create an inverse map that list
110 // scopes starts (and ends) with an instruction. One instruction may start (or
111 // end) multiple scopes. Ignore scopes that are not reachable.
112 void DebugHandlerBase::identifyScopeMarkers() {
113 SmallVector<LexicalScope *, 4> WorkList;
114 WorkList.push_back(LScopes.getCurrentFunctionScope());
115 while (!WorkList.empty()) {
116 LexicalScope *S = WorkList.pop_back_val();
118 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
119 if (!Children.empty())
120 WorkList.append(Children.begin(), Children.end());
122 if (S->isAbstractScope())
123 continue;
125 for (const InsnRange &R : S->getRanges()) {
126 assert(R.first && "InsnRange does not have first instruction!");
127 assert(R.second && "InsnRange does not have second instruction!");
128 requestLabelBeforeInsn(R.first);
129 requestLabelAfterInsn(R.second);
134 // Return Label preceding the instruction.
135 MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) {
136 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
137 assert(Label && "Didn't insert label before instruction");
138 return Label;
141 // Return Label immediately following the instruction.
142 MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) {
143 return LabelsAfterInsn.lookup(MI);
146 /// If this type is derived from a base type then return base type size.
147 uint64_t DebugHandlerBase::getBaseTypeSize(const DIType *Ty) {
148 assert(Ty);
149 const DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty);
150 if (!DDTy)
151 return Ty->getSizeInBits();
153 unsigned Tag = DDTy->getTag();
155 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
156 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
157 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type &&
158 Tag != dwarf::DW_TAG_immutable_type)
159 return DDTy->getSizeInBits();
161 DIType *BaseType = DDTy->getBaseType();
163 if (!BaseType)
164 return 0;
166 // If this is a derived type, go ahead and get the base type, unless it's a
167 // reference then it's just the size of the field. Pointer types have no need
168 // of this since they're a different type of qualification on the type.
169 if (BaseType->getTag() == dwarf::DW_TAG_reference_type ||
170 BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type)
171 return Ty->getSizeInBits();
173 return getBaseTypeSize(BaseType);
176 bool DebugHandlerBase::isUnsignedDIType(const DIType *Ty) {
177 if (isa<DIStringType>(Ty)) {
178 // Some transformations (e.g. instcombine) may decide to turn a Fortran
179 // character object into an integer, and later ones (e.g. SROA) may
180 // further inject a constant integer in a llvm.dbg.value call to track
181 // the object's value. Here we trust the transformations are doing the
182 // right thing, and treat the constant as unsigned to preserve that value
183 // (i.e. avoid sign extension).
184 return true;
187 if (auto *CTy = dyn_cast<DICompositeType>(Ty)) {
188 if (CTy->getTag() == dwarf::DW_TAG_enumeration_type) {
189 if (!(Ty = CTy->getBaseType()))
190 // FIXME: Enums without a fixed underlying type have unknown signedness
191 // here, leading to incorrectly emitted constants.
192 return false;
193 } else
194 // (Pieces of) aggregate types that get hacked apart by SROA may be
195 // represented by a constant. Encode them as unsigned bytes.
196 return true;
199 if (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
200 dwarf::Tag T = (dwarf::Tag)Ty->getTag();
201 // Encode pointer constants as unsigned bytes. This is used at least for
202 // null pointer constant emission.
203 // FIXME: reference and rvalue_reference /probably/ shouldn't be allowed
204 // here, but accept them for now due to a bug in SROA producing bogus
205 // dbg.values.
206 if (T == dwarf::DW_TAG_pointer_type ||
207 T == dwarf::DW_TAG_ptr_to_member_type ||
208 T == dwarf::DW_TAG_reference_type ||
209 T == dwarf::DW_TAG_rvalue_reference_type)
210 return true;
211 assert(T == dwarf::DW_TAG_typedef || T == dwarf::DW_TAG_const_type ||
212 T == dwarf::DW_TAG_volatile_type ||
213 T == dwarf::DW_TAG_restrict_type || T == dwarf::DW_TAG_atomic_type ||
214 T == dwarf::DW_TAG_immutable_type);
215 assert(DTy->getBaseType() && "Expected valid base type");
216 return isUnsignedDIType(DTy->getBaseType());
219 auto *BTy = cast<DIBasicType>(Ty);
220 unsigned Encoding = BTy->getEncoding();
221 assert((Encoding == dwarf::DW_ATE_unsigned ||
222 Encoding == dwarf::DW_ATE_unsigned_char ||
223 Encoding == dwarf::DW_ATE_signed ||
224 Encoding == dwarf::DW_ATE_signed_char ||
225 Encoding == dwarf::DW_ATE_float || Encoding == dwarf::DW_ATE_UTF ||
226 Encoding == dwarf::DW_ATE_boolean ||
227 (Ty->getTag() == dwarf::DW_TAG_unspecified_type &&
228 Ty->getName() == "decltype(nullptr)")) &&
229 "Unsupported encoding");
230 return Encoding == dwarf::DW_ATE_unsigned ||
231 Encoding == dwarf::DW_ATE_unsigned_char ||
232 Encoding == dwarf::DW_ATE_UTF || Encoding == dwarf::DW_ATE_boolean ||
233 Ty->getTag() == dwarf::DW_TAG_unspecified_type;
236 static bool hasDebugInfo(const MachineModuleInfo *MMI,
237 const MachineFunction *MF) {
238 if (!MMI->hasDebugInfo())
239 return false;
240 auto *SP = MF->getFunction().getSubprogram();
241 if (!SP)
242 return false;
243 assert(SP->getUnit());
244 auto EK = SP->getUnit()->getEmissionKind();
245 if (EK == DICompileUnit::NoDebug)
246 return false;
247 return true;
250 void DebugHandlerBase::beginFunction(const MachineFunction *MF) {
251 PrevInstBB = nullptr;
253 if (!Asm || !hasDebugInfo(MMI, MF)) {
254 skippedNonDebugFunction();
255 return;
258 // Grab the lexical scopes for the function, if we don't have any of those
259 // then we're not going to be able to do anything.
260 LScopes.initialize(*MF);
261 if (LScopes.empty()) {
262 beginFunctionImpl(MF);
263 return;
266 // Make sure that each lexical scope will have a begin/end label.
267 identifyScopeMarkers();
269 // Calculate history for local variables.
270 assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
271 assert(DbgLabels.empty() && "DbgLabels map wasn't cleaned!");
272 calculateDbgEntityHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
273 DbgValues, DbgLabels);
274 InstOrdering.initialize(*MF);
275 if (TrimVarLocs)
276 DbgValues.trimLocationRanges(*MF, LScopes, InstOrdering);
277 LLVM_DEBUG(DbgValues.dump());
279 // Request labels for the full history.
280 for (const auto &I : DbgValues) {
281 const auto &Entries = I.second;
282 if (Entries.empty())
283 continue;
285 auto IsDescribedByReg = [](const MachineInstr *MI) {
286 return any_of(MI->debug_operands(),
287 [](auto &MO) { return MO.isReg() && MO.getReg(); });
290 // The first mention of a function argument gets the CurrentFnBegin label,
291 // so arguments are visible when breaking at function entry.
293 // We do not change the label for values that are described by registers,
294 // as that could place them above their defining instructions. We should
295 // ideally not change the labels for constant debug values either, since
296 // doing that violates the ranges that are calculated in the history map.
297 // However, we currently do not emit debug values for constant arguments
298 // directly at the start of the function, so this code is still useful.
299 const DILocalVariable *DIVar =
300 Entries.front().getInstr()->getDebugVariable();
301 if (DIVar->isParameter() &&
302 getDISubprogram(DIVar->getScope())->describes(&MF->getFunction())) {
303 if (!IsDescribedByReg(Entries.front().getInstr()))
304 LabelsBeforeInsn[Entries.front().getInstr()] = Asm->getFunctionBegin();
305 if (Entries.front().getInstr()->getDebugExpression()->isFragment()) {
306 // Mark all non-overlapping initial fragments.
307 for (const auto *I = Entries.begin(); I != Entries.end(); ++I) {
308 if (!I->isDbgValue())
309 continue;
310 const DIExpression *Fragment = I->getInstr()->getDebugExpression();
311 if (std::any_of(Entries.begin(), I,
312 [&](DbgValueHistoryMap::Entry Pred) {
313 return Pred.isDbgValue() &&
314 Fragment->fragmentsOverlap(
315 Pred.getInstr()->getDebugExpression());
317 break;
318 // The code that generates location lists for DWARF assumes that the
319 // entries' start labels are monotonically increasing, and since we
320 // don't change the label for fragments that are described by
321 // registers, we must bail out when encountering such a fragment.
322 if (IsDescribedByReg(I->getInstr()))
323 break;
324 LabelsBeforeInsn[I->getInstr()] = Asm->getFunctionBegin();
329 for (const auto &Entry : Entries) {
330 if (Entry.isDbgValue())
331 requestLabelBeforeInsn(Entry.getInstr());
332 else
333 requestLabelAfterInsn(Entry.getInstr());
337 // Ensure there is a symbol before DBG_LABEL.
338 for (const auto &I : DbgLabels) {
339 const MachineInstr *MI = I.second;
340 requestLabelBeforeInsn(MI);
343 PrevInstLoc = DebugLoc();
344 PrevLabel = Asm->getFunctionBegin();
345 beginFunctionImpl(MF);
348 void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {
349 if (!Asm || !MMI->hasDebugInfo())
350 return;
352 assert(CurMI == nullptr);
353 CurMI = MI;
355 // Insert labels where requested.
356 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
357 LabelsBeforeInsn.find(MI);
359 // No label needed.
360 if (I == LabelsBeforeInsn.end())
361 return;
363 // Label already assigned.
364 if (I->second)
365 return;
367 if (!PrevLabel) {
368 PrevLabel = MMI->getContext().createTempSymbol();
369 Asm->OutStreamer->emitLabel(PrevLabel);
371 I->second = PrevLabel;
374 void DebugHandlerBase::endInstruction() {
375 if (!Asm || !MMI->hasDebugInfo())
376 return;
378 assert(CurMI != nullptr);
379 // Don't create a new label after DBG_VALUE and other instructions that don't
380 // generate code.
381 if (!CurMI->isMetaInstruction()) {
382 PrevLabel = nullptr;
383 PrevInstBB = CurMI->getParent();
386 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
387 LabelsAfterInsn.find(CurMI);
389 // No label needed or label already assigned.
390 if (I == LabelsAfterInsn.end() || I->second) {
391 CurMI = nullptr;
392 return;
395 // We need a label after this instruction. With basic block sections, just
396 // use the end symbol of the section if this is the last instruction of the
397 // section. This reduces the need for an additional label and also helps
398 // merging ranges.
399 if (CurMI->getParent()->isEndSection() && CurMI->getNextNode() == nullptr) {
400 PrevLabel = CurMI->getParent()->getEndSymbol();
401 } else if (!PrevLabel) {
402 PrevLabel = MMI->getContext().createTempSymbol();
403 Asm->OutStreamer->emitLabel(PrevLabel);
405 I->second = PrevLabel;
406 CurMI = nullptr;
409 void DebugHandlerBase::endFunction(const MachineFunction *MF) {
410 if (Asm && hasDebugInfo(MMI, MF))
411 endFunctionImpl(MF);
412 DbgValues.clear();
413 DbgLabels.clear();
414 LabelsBeforeInsn.clear();
415 LabelsAfterInsn.clear();
416 InstOrdering.clear();
419 void DebugHandlerBase::beginBasicBlock(const MachineBasicBlock &MBB) {
420 if (!MBB.isBeginSection())
421 return;
423 PrevLabel = MBB.getSymbol();
426 void DebugHandlerBase::endBasicBlock(const MachineBasicBlock &MBB) {
427 if (!MBB.isEndSection())
428 return;
430 PrevLabel = nullptr;