1//===-- llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp -------*- C++ -*--===// 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 7//===----------------------------------------------------------------------===// 9// Common functionality for different debug information format backends. 10// LLVM currently supports DWARF and CodeView. 12//===----------------------------------------------------------------------===// 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. 33std::optional<DbgVariableLocation>
37// Variables calculated from multiple locations can't be represented here. 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. 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. 60case dwarf::DW_OP_constu: {
65case dwarf::DW_OP_minus:
68case dwarf::DW_OP_plus:
76case dwarf::DW_OP_plus_uconst:
80 Location.FragmentInfo = {
Op->getArg(1),
Op->getArg(0)};
82case dwarf::DW_OP_deref:
83 Location.LoadChain.push_back(
Offset);
92// Do one final implicit DW_OP_deref if this was an indirect DBG_VALUE 94// FIXME: Replace these with DIExpression. 96 Location.LoadChain.push_back(
Offset);
106if (M->debug_compile_units().empty())
110// Each LexicalScope has first instruction and last instruction to mark 111// beginning and end of a scope respectively. Create an inverse map that list 112// scopes starts (and ends) with an instruction. One instruction may start (or 113// end) multiple scopes. Ignore scopes that are not reachable. 117while (!WorkList.
empty()) {
121if (!Children.empty())
122 WorkList.
append(Children.begin(), Children.end());
128assert(R.first &&
"InsnRange does not have first instruction!");
129assert(R.second &&
"InsnRange does not have second instruction!");
136// Return Label preceding the instruction. 139assert(Label &&
"Didn't insert label before instruction");
143// Return Label immediately following the instruction. 148/// If this type is derived from a base type then return base type size. 157if (
Tag != dwarf::DW_TAG_member &&
Tag != dwarf::DW_TAG_typedef &&
158Tag != dwarf::DW_TAG_const_type &&
Tag != dwarf::DW_TAG_volatile_type &&
159Tag != dwarf::DW_TAG_restrict_type &&
Tag != dwarf::DW_TAG_atomic_type &&
160Tag != dwarf::DW_TAG_immutable_type &&
161Tag != dwarf::DW_TAG_template_alias)
169// If this is a derived type, go ahead and get the base type, unless it's a 170// reference then it's just the size of the field. Pointer types have no need 171// of this since they're a different type of qualification on the type. 172if (
BaseType->getTag() == dwarf::DW_TAG_reference_type ||
173BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type)
180if (isa<DIStringType>(Ty)) {
181// Some transformations (e.g. instcombine) may decide to turn a Fortran 182// character object into an integer, and later ones (e.g. SROA) may 183// further inject a constant integer in a llvm.dbg.value call to track 184// the object's value. Here we trust the transformations are doing the 185// right thing, and treat the constant as unsigned to preserve that value 186// (i.e. avoid sign extension). 190if (
auto *CTy = dyn_cast<DICompositeType>(Ty)) {
191if (CTy->getTag() == dwarf::DW_TAG_enumeration_type) {
192if (!(Ty = CTy->getBaseType()))
193// FIXME: Enums without a fixed underlying type have unknown signedness 194// here, leading to incorrectly emitted constants. 197// (Pieces of) aggregate types that get hacked apart by SROA may be 198// represented by a constant. Encode them as unsigned bytes. 202if (
auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
204// Encode pointer constants as unsigned bytes. This is used at least for 205// null pointer constant emission. 206// FIXME: reference and rvalue_reference /probably/ shouldn't be allowed 207// here, but accept them for now due to a bug in SROA producing bogus 209if (
T == dwarf::DW_TAG_pointer_type ||
210T == dwarf::DW_TAG_ptr_to_member_type ||
211T == dwarf::DW_TAG_reference_type ||
212T == dwarf::DW_TAG_rvalue_reference_type)
214assert(
T == dwarf::DW_TAG_typedef ||
T == dwarf::DW_TAG_const_type ||
215T == dwarf::DW_TAG_volatile_type ||
216T == dwarf::DW_TAG_restrict_type ||
T == dwarf::DW_TAG_atomic_type ||
217T == dwarf::DW_TAG_immutable_type ||
218T == dwarf::DW_TAG_template_alias);
219assert(DTy->getBaseType() &&
"Expected valid base type");
223auto *BTy = cast<DIBasicType>(Ty);
224unsigned Encoding = BTy->getEncoding();
225assert((Encoding == dwarf::DW_ATE_unsigned ||
226 Encoding == dwarf::DW_ATE_unsigned_char ||
227 Encoding == dwarf::DW_ATE_signed ||
228 Encoding == dwarf::DW_ATE_signed_char ||
229 Encoding == dwarf::DW_ATE_float || Encoding == dwarf::DW_ATE_UTF ||
230 Encoding == dwarf::DW_ATE_boolean ||
231 Encoding == dwarf::DW_ATE_complex_float ||
232 Encoding == dwarf::DW_ATE_signed_fixed ||
233 Encoding == dwarf::DW_ATE_unsigned_fixed ||
234 (Ty->
getTag() == dwarf::DW_TAG_unspecified_type &&
235 Ty->
getName() ==
"decltype(nullptr)")) &&
236"Unsupported encoding");
237return Encoding == dwarf::DW_ATE_unsigned ||
238 Encoding == dwarf::DW_ATE_unsigned_char ||
239 Encoding == dwarf::DW_ATE_UTF || Encoding == dwarf::DW_ATE_boolean ||
240 Encoding == llvm::dwarf::DW_ATE_unsigned_fixed ||
241 Ty->
getTag() == dwarf::DW_TAG_unspecified_type;
249auto EK = SP->getUnit()->getEmissionKind();
263// Grab the lexical scopes for the function, if we don't have any of those 264// then we're not going to be able to do anything. 271// Make sure that each lexical scope will have a begin/end label. 274// Calculate history for local variables. 284// Request labels for the full history. 286constauto &Entries =
I.second;
292 [](
auto &MO) { return MO.isReg() && MO.getReg(); });
295// The first mention of a function argument gets the CurrentFnBegin label, 296// so arguments are visible when breaking at function entry. 298// We do not change the label for values that are described by registers, 299// as that could place them above their defining instructions. We should 300// ideally not change the labels for constant debug values either, since 301// doing that violates the ranges that are calculated in the history map. 302// However, we currently do not emit debug values for constant arguments 303// directly at the start of the function, so this code is still useful. 305 Entries.front().getInstr()->getDebugVariable();
308if (!IsDescribedByReg(Entries.front().getInstr()))
310if (Entries.front().getInstr()->getDebugExpression()->isFragment()) {
311// Mark all non-overlapping initial fragments. 312for (
constauto *
I = Entries.begin();
I != Entries.end(); ++
I) {
315constDIExpression *Fragment =
I->getInstr()->getDebugExpression();
316if (std::any_of(Entries.begin(),
I,
318 return Pred.isDbgValue() &&
319 Fragment->fragmentsOverlap(
320 Pred.getInstr()->getDebugExpression());
323// The code that generates location lists for DWARF assumes that the 324// entries' start labels are monotonically increasing, and since we 325// don't change the label for fragments that are described by 326// registers, we must bail out when encountering such a fragment. 327if (IsDescribedByReg(
I->getInstr()))
334for (
constauto &Entry : Entries) {
335if (Entry.isDbgValue())
342// Ensure there is a symbol before DBG_LABEL. 360// Insert labels where requested. 368// Label already assigned. 384// Don't create a new label after DBG_VALUE and other instructions that don't 394// No label needed or label already assigned. 400// We need a label after this instruction. With basic block sections, just 401// use the end symbol of the section if this is the last instruction of the 402// section. This reduces the need for an additional label and also helps 421 InstOrdering.
clear();
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static cl::opt< bool > TrimVarLocs("trim-var-locs", cl::Hidden, cl::init(true))
If true, we drop variable location ranges which exist entirely outside the variable's lexical scope i...
static bool hasDebugInfo(const MachineFunction *MF)
Module.h This file contains the declarations for the Module class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class is intended to be used as a driving class for all asm writers.
MCSymbol * getFunctionBegin() const
MachineFunction * MF
The current machine function.
bool hasDebugInfo() const
Returns true if valid debug info is present.
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
expr_op_iterator expr_op_begin() const
Visit the elements via ExprOperand wrappers.
expr_op_iterator expr_op_end() const
DILocalScope * getScope() const
Get the local scope for this variable.
dwarf::Tag getTag() const
StringRef getName() const
uint64_t getSizeInBits() const
This class represents an Operation in the Expression.
Specifies a change in a variable's debug value history.
void trimLocationRanges(const MachineFunction &MF, LexicalScopes &LScopes, const InstructionOrdering &Ordering)
Drop location ranges which exist entirely outside each variable's scope.
LLVM_DUMP_METHOD void dump(StringRef FuncName) const
static bool isUnsignedDIType(const DIType *Ty)
Return true if type encoding is unsigned.
const MachineInstr * CurMI
If nonnull, stores the current machine instruction we're processing.
AsmPrinter * Asm
Target of debug info emission.
virtual void endFunctionImpl(const MachineFunction *MF)=0
MCSymbol * getLabelBeforeInsn(const MachineInstr *MI)
Return Label preceding the instruction.
MachineModuleInfo * MMI
Collected machine module information.
void endBasicBlockSection(const MachineBasicBlock &MBB) override
Process the end of a basic-block-section within a function.
void identifyScopeMarkers()
Indentify instructions that are marking the beginning of or ending of a scope.
virtual void skippedNonDebugFunction()
void endFunction(const MachineFunction *MF) override
Gather post-function debug information.
DebugLoc PrevInstLoc
Previous instruction's location information.
void beginFunction(const MachineFunction *MF) override
Gather pre-function debug information.
void endInstruction() override
Process end of an instruction.
virtual ~DebugHandlerBase() override
MCSymbol * getLabelAfterInsn(const MachineInstr *MI)
Return Label immediately following the instruction.
DebugHandlerBase(AsmPrinter *A)
void beginInstruction(const MachineInstr *MI) override
Process beginning of an instruction.
const MachineBasicBlock * PrevInstBB
virtual void beginFunctionImpl(const MachineFunction *MF)=0
void requestLabelAfterInsn(const MachineInstr *MI)
Ensure that a label will be emitted after MI.
void beginBasicBlockSection(const MachineBasicBlock &MBB) override
Process the beginning of a new basic-block-section within a function.
DbgValueHistoryMap DbgValues
History of DBG_VALUE and clobber instructions for each user variable.
DbgLabelInstrMap DbgLabels
Mapping of inlined labels and DBG_LABEL machine instruction.
DenseMap< const MachineInstr *, MCSymbol * > LabelsBeforeInsn
Maps instruction with label emitted before instruction.
void beginModule(Module *M) override
DenseMap< const MachineInstr *, MCSymbol * > LabelsAfterInsn
Maps instruction with label emitted after instruction.
void requestLabelBeforeInsn(const MachineInstr *MI)
Ensure that a label will be emitted before MI.
const MachineBasicBlock * EpilogBeginBlock
This block includes epilogue instructions.
static uint64_t getBaseTypeSize(const DIType *Ty)
If this type is derived from a base type then return base type size.
DISubprogram * getSubprogram() const
Get the attached subprogram.
void initialize(const MachineFunction &MF)
LexicalScope - This class is used to track scope information.
SmallVectorImpl< LexicalScope * > & getChildren()
SmallVectorImpl< InsnRange > & getRanges()
bool isAbstractScope() const
void initialize(const MachineFunction &)
initialize - Scan machine function and constuct lexical scope nest, resets the instance if necessary.
bool empty()
empty - Return true if there is any lexical scope information available.
LexicalScope * getCurrentFunctionScope() const
getCurrentFunctionScope - Return lexical scope for the current function.
MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
MCSymbol * getEndSymbol() const
Returns the MCSymbol marking the end of this basic block.
bool isEntryBlock() const
Returns true if this is the entry block of the function.
bool isEndSection() const
Returns true if this block ends any section.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
const MachineBasicBlock * getParent() const
bool isMetaInstruction(QueryType Type=IgnoreBundle) const
Return true if this instruction doesn't produce any output in the form of executable instructions.
const MCContext & getContext() const
A Module instance is used to store all the information related to an LLVM module.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
LLVM Value Representation.
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
initializer< Ty > init(const Ty &Val)
@ DW_OP_LLVM_fragment
Only used in LLVM metadata.
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
This is an optimization pass for GlobalISel generic memory operations.
void calculateDbgEntityHistory(const MachineFunction *MF, const TargetRegisterInfo *TRI, DbgValueHistoryMap &DbgValues, DbgLabelInstrMap &DbgLabels)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
DWARFExpression::Operation Op
std::pair< const MachineInstr *, const MachineInstr * > InsnRange
InsnRange - This is used to track range of instructions with identical lexical scope.
DISubprogram * getDISubprogram(const MDNode *Scope)
Find subprogram that is enclosing this scope.
Represents the location at which a variable is stored.
static std::optional< DbgVariableLocation > extractFromMachineInstruction(const MachineInstr &Instruction)
Extract a VariableLocation from a MachineInstr.