llvm-project/llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp

433 lines
15 KiB
C++
Raw Normal View History

//===-- llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp -------*- C++ -*--===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Common functionality for different debug information format backends.
// LLVM currently supports DWARF and CodeView.
//
//===----------------------------------------------------------------------===//
#include "llvm/CodeGen/DebugHandlerBase.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/Twine.h"
#include "llvm/CodeGen/AsmPrinter.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/TargetSubtargetInfo.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/MC/MCStreamer.h"
[DebugInfo] Drop location ranges for variables which exist entirely outside the variable's scope Summary: This patch reduces file size in debug builds by dropping variable locations a debugger user will not see. After building the debug entity history map we loop through it. For each variable we look at each entry. If the entry opens a location range which does not intersect any of the variable's scope's ranges then we mark it for removal. After visiting the entries for each variable we also mark any clobbering entries which will no longer be referenced for removal, and then finally erase the marked entries. This all requires the ability to query the order of instructions, so before this runs we number them. Tests: Added llvm/test/DebugInfo/X86/trim-var-locs.mir Modified llvm/test/DebugInfo/COFF/register-variables.ll Branch folding merges the tails of if.then and if.else into if.else. Each blocks' debug-locations point to different scopes so when they're merged we can't use either. Because of this the variable 'c' ends up with a location range which doesn't cover any instructions in its scope; with the patch applied the location range is dropped and its flag changes to IsOptimizedOut. Modified llvm/test/DebugInfo/X86/live-debug-variables.ll Modified llvm/test/DebugInfo/ARM/PR26163.ll In both tests an out of scope location is now removed. The remaining location covers the entire scope of the variable allowing us to emit it as a single location. Reviewed By: aprantl Differential Revision: https://reviews.llvm.org/D82129
2020-07-22 16:25:14 +08:00
#include "llvm/Support/CommandLine.h"
using namespace llvm;
#define DEBUG_TYPE "dwarfdebug"
[DebugInfo] Drop location ranges for variables which exist entirely outside the variable's scope Summary: This patch reduces file size in debug builds by dropping variable locations a debugger user will not see. After building the debug entity history map we loop through it. For each variable we look at each entry. If the entry opens a location range which does not intersect any of the variable's scope's ranges then we mark it for removal. After visiting the entries for each variable we also mark any clobbering entries which will no longer be referenced for removal, and then finally erase the marked entries. This all requires the ability to query the order of instructions, so before this runs we number them. Tests: Added llvm/test/DebugInfo/X86/trim-var-locs.mir Modified llvm/test/DebugInfo/COFF/register-variables.ll Branch folding merges the tails of if.then and if.else into if.else. Each blocks' debug-locations point to different scopes so when they're merged we can't use either. Because of this the variable 'c' ends up with a location range which doesn't cover any instructions in its scope; with the patch applied the location range is dropped and its flag changes to IsOptimizedOut. Modified llvm/test/DebugInfo/X86/live-debug-variables.ll Modified llvm/test/DebugInfo/ARM/PR26163.ll In both tests an out of scope location is now removed. The remaining location covers the entire scope of the variable allowing us to emit it as a single location. Reviewed By: aprantl Differential Revision: https://reviews.llvm.org/D82129
2020-07-22 16:25:14 +08:00
/// If true, we drop variable location ranges which exist entirely outside the
/// variable's lexical scope instruction ranges.
static cl::opt<bool> TrimVarLocs("trim-var-locs", cl::Hidden, cl::init(true));
Optional<DbgVariableLocation>
DbgVariableLocation::extractFromMachineInstruction(
const MachineInstr &Instruction) {
DbgVariableLocation Location;
// Variables calculated from multiple locations can't be represented here.
if (Instruction.getNumDebugOperands() != 1)
return None;
if (!Instruction.getDebugOperand(0).isReg())
return None;
Location.Register = Instruction.getDebugOperand(0).getReg();
Location.FragmentInfo.reset();
// We only handle expressions generated by DIExpression::appendOffset,
// which doesn't require a full stack machine.
int64_t Offset = 0;
const DIExpression *DIExpr = Instruction.getDebugExpression();
auto Op = DIExpr->expr_op_begin();
// We can handle a DBG_VALUE_LIST iff it has exactly one location operand that
// appears exactly once at the start of the expression.
if (Instruction.isDebugValueList()) {
if (Instruction.getNumDebugOperands() == 1 &&
Op->getOp() == dwarf::DW_OP_LLVM_arg)
++Op;
else
return None;
}
while (Op != DIExpr->expr_op_end()) {
switch (Op->getOp()) {
case dwarf::DW_OP_constu: {
int Value = Op->getArg(0);
++Op;
if (Op != DIExpr->expr_op_end()) {
switch (Op->getOp()) {
case dwarf::DW_OP_minus:
Offset -= Value;
break;
case dwarf::DW_OP_plus:
Offset += Value;
break;
default:
continue;
}
}
} break;
case dwarf::DW_OP_plus_uconst:
Offset += Op->getArg(0);
break;
case dwarf::DW_OP_LLVM_fragment:
Location.FragmentInfo = {Op->getArg(1), Op->getArg(0)};
break;
case dwarf::DW_OP_deref:
Location.LoadChain.push_back(Offset);
Offset = 0;
break;
default:
return None;
}
++Op;
}
// Do one final implicit DW_OP_deref if this was an indirect DBG_VALUE
// instruction.
// FIXME: Replace these with DIExpression.
if (Instruction.isIndirectDebugValue())
Location.LoadChain.push_back(Offset);
return Location;
}
DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {}
void DebugHandlerBase::beginModule(Module *M) {
if (M->debug_compile_units().empty())
Asm = nullptr;
}
// Each LexicalScope has first instruction and last instruction to mark
// beginning and end of a scope respectively. Create an inverse map that list
// scopes starts (and ends) with an instruction. One instruction may start (or
// end) multiple scopes. Ignore scopes that are not reachable.
void DebugHandlerBase::identifyScopeMarkers() {
SmallVector<LexicalScope *, 4> WorkList;
WorkList.push_back(LScopes.getCurrentFunctionScope());
while (!WorkList.empty()) {
LexicalScope *S = WorkList.pop_back_val();
const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
if (!Children.empty())
WorkList.append(Children.begin(), Children.end());
if (S->isAbstractScope())
continue;
for (const InsnRange &R : S->getRanges()) {
assert(R.first && "InsnRange does not have first instruction!");
assert(R.second && "InsnRange does not have second instruction!");
requestLabelBeforeInsn(R.first);
requestLabelAfterInsn(R.second);
}
}
}
// Return Label preceding the instruction.
MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) {
MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
assert(Label && "Didn't insert label before instruction");
return Label;
}
// Return Label immediately following the instruction.
MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) {
return LabelsAfterInsn.lookup(MI);
}
/// If this type is derived from a base type then return base type size.
uint64_t DebugHandlerBase::getBaseTypeSize(const DIType *Ty) {
assert(Ty);
const DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty);
if (!DDTy)
return Ty->getSizeInBits();
unsigned Tag = DDTy->getTag();
if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type &&
Tag != dwarf::DW_TAG_immutable_type)
return DDTy->getSizeInBits();
DIType *BaseType = DDTy->getBaseType();
if (!BaseType)
return 0;
// If this is a derived type, go ahead and get the base type, unless it's a
// reference then it's just the size of the field. Pointer types have no need
// of this since they're a different type of qualification on the type.
if (BaseType->getTag() == dwarf::DW_TAG_reference_type ||
BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type)
return Ty->getSizeInBits();
return getBaseTypeSize(BaseType);
}
bool DebugHandlerBase::isUnsignedDIType(const DIType *Ty) {
if (isa<DIStringType>(Ty)) {
// Some transformations (e.g. instcombine) may decide to turn a Fortran
// character object into an integer, and later ones (e.g. SROA) may
// further inject a constant integer in a llvm.dbg.value call to track
// the object's value. Here we trust the transformations are doing the
// right thing, and treat the constant as unsigned to preserve that value
// (i.e. avoid sign extension).
return true;
}
if (auto *CTy = dyn_cast<DICompositeType>(Ty)) {
if (CTy->getTag() == dwarf::DW_TAG_enumeration_type) {
if (!(Ty = CTy->getBaseType()))
// FIXME: Enums without a fixed underlying type have unknown signedness
// here, leading to incorrectly emitted constants.
return false;
} else
// (Pieces of) aggregate types that get hacked apart by SROA may be
// represented by a constant. Encode them as unsigned bytes.
return true;
}
if (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
dwarf::Tag T = (dwarf::Tag)Ty->getTag();
// Encode pointer constants as unsigned bytes. This is used at least for
// null pointer constant emission.
// FIXME: reference and rvalue_reference /probably/ shouldn't be allowed
// here, but accept them for now due to a bug in SROA producing bogus
// dbg.values.
if (T == dwarf::DW_TAG_pointer_type ||
T == dwarf::DW_TAG_ptr_to_member_type ||
T == dwarf::DW_TAG_reference_type ||
T == dwarf::DW_TAG_rvalue_reference_type)
return true;
assert(T == dwarf::DW_TAG_typedef || T == dwarf::DW_TAG_const_type ||
T == dwarf::DW_TAG_volatile_type ||
T == dwarf::DW_TAG_restrict_type || T == dwarf::DW_TAG_atomic_type ||
T == dwarf::DW_TAG_immutable_type);
assert(DTy->getBaseType() && "Expected valid base type");
return isUnsignedDIType(DTy->getBaseType());
}
auto *BTy = cast<DIBasicType>(Ty);
unsigned Encoding = BTy->getEncoding();
assert((Encoding == dwarf::DW_ATE_unsigned ||
Encoding == dwarf::DW_ATE_unsigned_char ||
Encoding == dwarf::DW_ATE_signed ||
Encoding == dwarf::DW_ATE_signed_char ||
Encoding == dwarf::DW_ATE_float || Encoding == dwarf::DW_ATE_UTF ||
Encoding == dwarf::DW_ATE_boolean ||
(Ty->getTag() == dwarf::DW_TAG_unspecified_type &&
Ty->getName() == "decltype(nullptr)")) &&
"Unsupported encoding");
return Encoding == dwarf::DW_ATE_unsigned ||
Encoding == dwarf::DW_ATE_unsigned_char ||
Encoding == dwarf::DW_ATE_UTF || Encoding == dwarf::DW_ATE_boolean ||
Ty->getTag() == dwarf::DW_TAG_unspecified_type;
}
static bool hasDebugInfo(const MachineModuleInfo *MMI,
const MachineFunction *MF) {
if (!MMI->hasDebugInfo())
return false;
auto *SP = MF->getFunction().getSubprogram();
if (!SP)
return false;
assert(SP->getUnit());
auto EK = SP->getUnit()->getEmissionKind();
if (EK == DICompileUnit::NoDebug)
return false;
return true;
}
void DebugHandlerBase::beginFunction(const MachineFunction *MF) {
PrevInstBB = nullptr;
if (!Asm || !hasDebugInfo(MMI, MF)) {
skippedNonDebugFunction();
return;
}
// Grab the lexical scopes for the function, if we don't have any of those
// then we're not going to be able to do anything.
LScopes.initialize(*MF);
if (LScopes.empty()) {
beginFunctionImpl(MF);
return;
}
// Make sure that each lexical scope will have a begin/end label.
identifyScopeMarkers();
// Calculate history for local variables.
assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
assert(DbgLabels.empty() && "DbgLabels map wasn't cleaned!");
calculateDbgEntityHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
DbgValues, DbgLabels);
InstOrdering.initialize(*MF);
[DebugInfo] Drop location ranges for variables which exist entirely outside the variable's scope Summary: This patch reduces file size in debug builds by dropping variable locations a debugger user will not see. After building the debug entity history map we loop through it. For each variable we look at each entry. If the entry opens a location range which does not intersect any of the variable's scope's ranges then we mark it for removal. After visiting the entries for each variable we also mark any clobbering entries which will no longer be referenced for removal, and then finally erase the marked entries. This all requires the ability to query the order of instructions, so before this runs we number them. Tests: Added llvm/test/DebugInfo/X86/trim-var-locs.mir Modified llvm/test/DebugInfo/COFF/register-variables.ll Branch folding merges the tails of if.then and if.else into if.else. Each blocks' debug-locations point to different scopes so when they're merged we can't use either. Because of this the variable 'c' ends up with a location range which doesn't cover any instructions in its scope; with the patch applied the location range is dropped and its flag changes to IsOptimizedOut. Modified llvm/test/DebugInfo/X86/live-debug-variables.ll Modified llvm/test/DebugInfo/ARM/PR26163.ll In both tests an out of scope location is now removed. The remaining location covers the entire scope of the variable allowing us to emit it as a single location. Reviewed By: aprantl Differential Revision: https://reviews.llvm.org/D82129
2020-07-22 16:25:14 +08:00
if (TrimVarLocs)
DbgValues.trimLocationRanges(*MF, LScopes, InstOrdering);
LLVM_DEBUG(DbgValues.dump());
// Request labels for the full history.
for (const auto &I : DbgValues) {
const auto &Entries = I.second;
if (Entries.empty())
continue;
[DebugInfo] Stop changing labels for register-described parameter DBG_VALUEs Summary: This is a follow-up to D57510. This patch stops DebugHandlerBase from changing the starting label for the first non-overlapping, register-described parameter DBG_VALUEs to the beginning of the function. That code did not consider what defined the registers, which could result in the ranges for the debug values starting before their defining instructions. We currently do not emit debug values for constant values directly at the start of the function, so this code is still useful for such values, but my intention is to remove the code from DebugHandlerBase completely when we get there. One reason for removing it is that the code violates the history map's ranges, which I think can make it quite confusing when troubleshooting. In D57510, PrologEpilogInserter was amended so that parameter DBG_VALUEs now are kept at the start of the entry block, even after emission of prologue code. That was done to reduce the degradation of debug completeness from this patch. PR40638 is another example, where the lexical-scope trimming that LDV does, in combination with scheduling, results in instructions after the prologue being left without locations. There might be other cases where the DBG_VALUEs are pushed further down, for which the DebugHandlerBase code may be helpful, but as it now quite often result in incorrect locations, even after the prologue, it seems better to remove that code, and try to work our way up with accurate locations. In the long run we should maybe not aim to provide accurate locations inside the prologue. Some single location descriptions, at least those referring to stack values, generate inaccurate values inside the epilogue, so we maybe should not aim to achieve accuracy for location lists. However, it seems that we now emit line number programs that can result in GDB and LLDB stopping inside the prologue when doing line number stepping into functions. See PR40188 for more information. A summary of some of the changed test cases is available in PR40188#c2. Reviewers: aprantl, dblaikie, rnk, jmorse Reviewed By: aprantl Subscribers: jdoerfert, jholewinski, jvesely, javed.absar, llvm-commits Tags: #debug-info, #llvm Differential Revision: https://reviews.llvm.org/D57511 llvm-svn: 353928
2019-02-13 17:34:07 +08:00
auto IsDescribedByReg = [](const MachineInstr *MI) {
return any_of(MI->debug_operands(),
[](auto &MO) { return MO.isReg() && MO.getReg(); });
[DebugInfo] Stop changing labels for register-described parameter DBG_VALUEs Summary: This is a follow-up to D57510. This patch stops DebugHandlerBase from changing the starting label for the first non-overlapping, register-described parameter DBG_VALUEs to the beginning of the function. That code did not consider what defined the registers, which could result in the ranges for the debug values starting before their defining instructions. We currently do not emit debug values for constant values directly at the start of the function, so this code is still useful for such values, but my intention is to remove the code from DebugHandlerBase completely when we get there. One reason for removing it is that the code violates the history map's ranges, which I think can make it quite confusing when troubleshooting. In D57510, PrologEpilogInserter was amended so that parameter DBG_VALUEs now are kept at the start of the entry block, even after emission of prologue code. That was done to reduce the degradation of debug completeness from this patch. PR40638 is another example, where the lexical-scope trimming that LDV does, in combination with scheduling, results in instructions after the prologue being left without locations. There might be other cases where the DBG_VALUEs are pushed further down, for which the DebugHandlerBase code may be helpful, but as it now quite often result in incorrect locations, even after the prologue, it seems better to remove that code, and try to work our way up with accurate locations. In the long run we should maybe not aim to provide accurate locations inside the prologue. Some single location descriptions, at least those referring to stack values, generate inaccurate values inside the epilogue, so we maybe should not aim to achieve accuracy for location lists. However, it seems that we now emit line number programs that can result in GDB and LLDB stopping inside the prologue when doing line number stepping into functions. See PR40188 for more information. A summary of some of the changed test cases is available in PR40188#c2. Reviewers: aprantl, dblaikie, rnk, jmorse Reviewed By: aprantl Subscribers: jdoerfert, jholewinski, jvesely, javed.absar, llvm-commits Tags: #debug-info, #llvm Differential Revision: https://reviews.llvm.org/D57511 llvm-svn: 353928
2019-02-13 17:34:07 +08:00
};
// The first mention of a function argument gets the CurrentFnBegin label,
// so arguments are visible when breaking at function entry.
//
// We do not change the label for values that are described by registers,
// as that could place them above their defining instructions. We should
// ideally not change the labels for constant debug values either, since
// doing that violates the ranges that are calculated in the history map.
// However, we currently do not emit debug values for constant arguments
// directly at the start of the function, so this code is still useful.
const DILocalVariable *DIVar =
[DebugInfo] Improve handling of clobbered fragments Summary: Currently the DbgValueHistorymap only keeps track of clobbered registers for the last debug value that it has encountered. This could lead to preceding register-described debug values living on longer in the location lists than they should. See PR40283 for an example. This patch does not introduce tracking of multiple registers, but changes the DbgValueHistoryMap structure to allow for that in a follow-up patch. This patch is not NFC, as it at least fixes two bugs in DwarfDebug (both are covered in the new clobbered-fragments.mir test): * If a debug value was clobbered (its End pointer set), the value would still be added to OpenRanges, meaning that the succeeding location list entries could potentially contain stale values. * If a debug value was clobbered, and there were non-overlapping fragments that were still live after the clobbering, DwarfDebug would not create a location list entry starting directly after the clobbering instruction. This meant that the location list could have a gap until the next debug value for the variable was encountered. Before this patch, the history map was represented by <Begin, End> pairs, where a new pair was created for each new debug value. When dealing with partially overlapping register-described debug values, such as in the following example: DBG_VALUE $reg2, $noreg, !1, !DIExpression(DW_OP_LLVM_fragment, 32, 32) [...] DBG_VALUE $reg3, $noreg, !1, !DIExpression(DW_OP_LLVM_fragment, 64, 32) [...] $reg2 = insn1 [...] $reg3 = insn2 the history map would then contain the entries `[<DV1, insn1>, [<DV2, insn2>]`. This would leave it up to the users of the map to be aware of the relative order of the instructions, which e.g. could make DwarfDebug::buildLocationList() needlessly complex. Instead, this patch makes the history map structure monotonically increasing by dropping the End pointer, and replacing that with explicit clobbering entries in the vector. Each debug value has an "end index", which if set, points to the entry in the vector that ends the debug value. The ending entry can either be an overlapping debug value, or an instruction which clobbers the register that the debug value is described by. The ending entry's instruction can thus either be excluded or included in the debug value's range. If the end index is not set, the debug value that the entry introduces is valid until the end of the function. Changes to test cases: * DebugInfo/X86/pieces-3.ll: The range of the first DBG_VALUE, which describes that the fragment (0, 64) is located in RDI, was incorrectly ended by the clobbering of RAX, which the second (non-overlapping) DBG_VALUE was described by. With this patch we get a second entry that only describes RDI after that clobbering. * DebugInfo/ARM/partial-subreg.ll: This test seems to indiciate a bug in LiveDebugValues that is caused by it not being aware of fragments. I have added some comments in the test case about that. Also, before this patch DwarfDebug would incorrectly include a register-described debug value from a preceding block in a location list entry. Reviewers: aprantl, probinson, dblaikie, rnk, bjope Reviewed By: aprantl Subscribers: javed.absar, kristof.beyls, jdoerfert, llvm-commits Tags: #debug-info, #llvm Differential Revision: https://reviews.llvm.org/D59941 llvm-svn: 358072
2019-04-10 19:28:20 +08:00
Entries.front().getInstr()->getDebugVariable();
if (DIVar->isParameter() &&
getDISubprogram(DIVar->getScope())->describes(&MF->getFunction())) {
[DebugInfo] Improve handling of clobbered fragments Summary: Currently the DbgValueHistorymap only keeps track of clobbered registers for the last debug value that it has encountered. This could lead to preceding register-described debug values living on longer in the location lists than they should. See PR40283 for an example. This patch does not introduce tracking of multiple registers, but changes the DbgValueHistoryMap structure to allow for that in a follow-up patch. This patch is not NFC, as it at least fixes two bugs in DwarfDebug (both are covered in the new clobbered-fragments.mir test): * If a debug value was clobbered (its End pointer set), the value would still be added to OpenRanges, meaning that the succeeding location list entries could potentially contain stale values. * If a debug value was clobbered, and there were non-overlapping fragments that were still live after the clobbering, DwarfDebug would not create a location list entry starting directly after the clobbering instruction. This meant that the location list could have a gap until the next debug value for the variable was encountered. Before this patch, the history map was represented by <Begin, End> pairs, where a new pair was created for each new debug value. When dealing with partially overlapping register-described debug values, such as in the following example: DBG_VALUE $reg2, $noreg, !1, !DIExpression(DW_OP_LLVM_fragment, 32, 32) [...] DBG_VALUE $reg3, $noreg, !1, !DIExpression(DW_OP_LLVM_fragment, 64, 32) [...] $reg2 = insn1 [...] $reg3 = insn2 the history map would then contain the entries `[<DV1, insn1>, [<DV2, insn2>]`. This would leave it up to the users of the map to be aware of the relative order of the instructions, which e.g. could make DwarfDebug::buildLocationList() needlessly complex. Instead, this patch makes the history map structure monotonically increasing by dropping the End pointer, and replacing that with explicit clobbering entries in the vector. Each debug value has an "end index", which if set, points to the entry in the vector that ends the debug value. The ending entry can either be an overlapping debug value, or an instruction which clobbers the register that the debug value is described by. The ending entry's instruction can thus either be excluded or included in the debug value's range. If the end index is not set, the debug value that the entry introduces is valid until the end of the function. Changes to test cases: * DebugInfo/X86/pieces-3.ll: The range of the first DBG_VALUE, which describes that the fragment (0, 64) is located in RDI, was incorrectly ended by the clobbering of RAX, which the second (non-overlapping) DBG_VALUE was described by. With this patch we get a second entry that only describes RDI after that clobbering. * DebugInfo/ARM/partial-subreg.ll: This test seems to indiciate a bug in LiveDebugValues that is caused by it not being aware of fragments. I have added some comments in the test case about that. Also, before this patch DwarfDebug would incorrectly include a register-described debug value from a preceding block in a location list entry. Reviewers: aprantl, probinson, dblaikie, rnk, bjope Reviewed By: aprantl Subscribers: javed.absar, kristof.beyls, jdoerfert, llvm-commits Tags: #debug-info, #llvm Differential Revision: https://reviews.llvm.org/D59941 llvm-svn: 358072
2019-04-10 19:28:20 +08:00
if (!IsDescribedByReg(Entries.front().getInstr()))
LabelsBeforeInsn[Entries.front().getInstr()] = Asm->getFunctionBegin();
if (Entries.front().getInstr()->getDebugExpression()->isFragment()) {
// Mark all non-overlapping initial fragments.
for (auto I = Entries.begin(); I != Entries.end(); ++I) {
[DebugInfo] Improve handling of clobbered fragments Summary: Currently the DbgValueHistorymap only keeps track of clobbered registers for the last debug value that it has encountered. This could lead to preceding register-described debug values living on longer in the location lists than they should. See PR40283 for an example. This patch does not introduce tracking of multiple registers, but changes the DbgValueHistoryMap structure to allow for that in a follow-up patch. This patch is not NFC, as it at least fixes two bugs in DwarfDebug (both are covered in the new clobbered-fragments.mir test): * If a debug value was clobbered (its End pointer set), the value would still be added to OpenRanges, meaning that the succeeding location list entries could potentially contain stale values. * If a debug value was clobbered, and there were non-overlapping fragments that were still live after the clobbering, DwarfDebug would not create a location list entry starting directly after the clobbering instruction. This meant that the location list could have a gap until the next debug value for the variable was encountered. Before this patch, the history map was represented by <Begin, End> pairs, where a new pair was created for each new debug value. When dealing with partially overlapping register-described debug values, such as in the following example: DBG_VALUE $reg2, $noreg, !1, !DIExpression(DW_OP_LLVM_fragment, 32, 32) [...] DBG_VALUE $reg3, $noreg, !1, !DIExpression(DW_OP_LLVM_fragment, 64, 32) [...] $reg2 = insn1 [...] $reg3 = insn2 the history map would then contain the entries `[<DV1, insn1>, [<DV2, insn2>]`. This would leave it up to the users of the map to be aware of the relative order of the instructions, which e.g. could make DwarfDebug::buildLocationList() needlessly complex. Instead, this patch makes the history map structure monotonically increasing by dropping the End pointer, and replacing that with explicit clobbering entries in the vector. Each debug value has an "end index", which if set, points to the entry in the vector that ends the debug value. The ending entry can either be an overlapping debug value, or an instruction which clobbers the register that the debug value is described by. The ending entry's instruction can thus either be excluded or included in the debug value's range. If the end index is not set, the debug value that the entry introduces is valid until the end of the function. Changes to test cases: * DebugInfo/X86/pieces-3.ll: The range of the first DBG_VALUE, which describes that the fragment (0, 64) is located in RDI, was incorrectly ended by the clobbering of RAX, which the second (non-overlapping) DBG_VALUE was described by. With this patch we get a second entry that only describes RDI after that clobbering. * DebugInfo/ARM/partial-subreg.ll: This test seems to indiciate a bug in LiveDebugValues that is caused by it not being aware of fragments. I have added some comments in the test case about that. Also, before this patch DwarfDebug would incorrectly include a register-described debug value from a preceding block in a location list entry. Reviewers: aprantl, probinson, dblaikie, rnk, bjope Reviewed By: aprantl Subscribers: javed.absar, kristof.beyls, jdoerfert, llvm-commits Tags: #debug-info, #llvm Differential Revision: https://reviews.llvm.org/D59941 llvm-svn: 358072
2019-04-10 19:28:20 +08:00
if (!I->isDbgValue())
continue;
const DIExpression *Fragment = I->getInstr()->getDebugExpression();
if (std::any_of(Entries.begin(), I,
[&](DbgValueHistoryMap::Entry Pred) {
[DebugInfo] Improve handling of clobbered fragments Summary: Currently the DbgValueHistorymap only keeps track of clobbered registers for the last debug value that it has encountered. This could lead to preceding register-described debug values living on longer in the location lists than they should. See PR40283 for an example. This patch does not introduce tracking of multiple registers, but changes the DbgValueHistoryMap structure to allow for that in a follow-up patch. This patch is not NFC, as it at least fixes two bugs in DwarfDebug (both are covered in the new clobbered-fragments.mir test): * If a debug value was clobbered (its End pointer set), the value would still be added to OpenRanges, meaning that the succeeding location list entries could potentially contain stale values. * If a debug value was clobbered, and there were non-overlapping fragments that were still live after the clobbering, DwarfDebug would not create a location list entry starting directly after the clobbering instruction. This meant that the location list could have a gap until the next debug value for the variable was encountered. Before this patch, the history map was represented by <Begin, End> pairs, where a new pair was created for each new debug value. When dealing with partially overlapping register-described debug values, such as in the following example: DBG_VALUE $reg2, $noreg, !1, !DIExpression(DW_OP_LLVM_fragment, 32, 32) [...] DBG_VALUE $reg3, $noreg, !1, !DIExpression(DW_OP_LLVM_fragment, 64, 32) [...] $reg2 = insn1 [...] $reg3 = insn2 the history map would then contain the entries `[<DV1, insn1>, [<DV2, insn2>]`. This would leave it up to the users of the map to be aware of the relative order of the instructions, which e.g. could make DwarfDebug::buildLocationList() needlessly complex. Instead, this patch makes the history map structure monotonically increasing by dropping the End pointer, and replacing that with explicit clobbering entries in the vector. Each debug value has an "end index", which if set, points to the entry in the vector that ends the debug value. The ending entry can either be an overlapping debug value, or an instruction which clobbers the register that the debug value is described by. The ending entry's instruction can thus either be excluded or included in the debug value's range. If the end index is not set, the debug value that the entry introduces is valid until the end of the function. Changes to test cases: * DebugInfo/X86/pieces-3.ll: The range of the first DBG_VALUE, which describes that the fragment (0, 64) is located in RDI, was incorrectly ended by the clobbering of RAX, which the second (non-overlapping) DBG_VALUE was described by. With this patch we get a second entry that only describes RDI after that clobbering. * DebugInfo/ARM/partial-subreg.ll: This test seems to indiciate a bug in LiveDebugValues that is caused by it not being aware of fragments. I have added some comments in the test case about that. Also, before this patch DwarfDebug would incorrectly include a register-described debug value from a preceding block in a location list entry. Reviewers: aprantl, probinson, dblaikie, rnk, bjope Reviewed By: aprantl Subscribers: javed.absar, kristof.beyls, jdoerfert, llvm-commits Tags: #debug-info, #llvm Differential Revision: https://reviews.llvm.org/D59941 llvm-svn: 358072
2019-04-10 19:28:20 +08:00
return Pred.isDbgValue() &&
Fragment->fragmentsOverlap(
Pred.getInstr()->getDebugExpression());
}))
break;
// The code that generates location lists for DWARF assumes that the
// entries' start labels are monotonically increasing, and since we
// don't change the label for fragments that are described by
// registers, we must bail out when encountering such a fragment.
if (IsDescribedByReg(I->getInstr()))
break;
LabelsBeforeInsn[I->getInstr()] = Asm->getFunctionBegin();
}
}
}
for (const auto &Entry : Entries) {
[DebugInfo] Improve handling of clobbered fragments Summary: Currently the DbgValueHistorymap only keeps track of clobbered registers for the last debug value that it has encountered. This could lead to preceding register-described debug values living on longer in the location lists than they should. See PR40283 for an example. This patch does not introduce tracking of multiple registers, but changes the DbgValueHistoryMap structure to allow for that in a follow-up patch. This patch is not NFC, as it at least fixes two bugs in DwarfDebug (both are covered in the new clobbered-fragments.mir test): * If a debug value was clobbered (its End pointer set), the value would still be added to OpenRanges, meaning that the succeeding location list entries could potentially contain stale values. * If a debug value was clobbered, and there were non-overlapping fragments that were still live after the clobbering, DwarfDebug would not create a location list entry starting directly after the clobbering instruction. This meant that the location list could have a gap until the next debug value for the variable was encountered. Before this patch, the history map was represented by <Begin, End> pairs, where a new pair was created for each new debug value. When dealing with partially overlapping register-described debug values, such as in the following example: DBG_VALUE $reg2, $noreg, !1, !DIExpression(DW_OP_LLVM_fragment, 32, 32) [...] DBG_VALUE $reg3, $noreg, !1, !DIExpression(DW_OP_LLVM_fragment, 64, 32) [...] $reg2 = insn1 [...] $reg3 = insn2 the history map would then contain the entries `[<DV1, insn1>, [<DV2, insn2>]`. This would leave it up to the users of the map to be aware of the relative order of the instructions, which e.g. could make DwarfDebug::buildLocationList() needlessly complex. Instead, this patch makes the history map structure monotonically increasing by dropping the End pointer, and replacing that with explicit clobbering entries in the vector. Each debug value has an "end index", which if set, points to the entry in the vector that ends the debug value. The ending entry can either be an overlapping debug value, or an instruction which clobbers the register that the debug value is described by. The ending entry's instruction can thus either be excluded or included in the debug value's range. If the end index is not set, the debug value that the entry introduces is valid until the end of the function. Changes to test cases: * DebugInfo/X86/pieces-3.ll: The range of the first DBG_VALUE, which describes that the fragment (0, 64) is located in RDI, was incorrectly ended by the clobbering of RAX, which the second (non-overlapping) DBG_VALUE was described by. With this patch we get a second entry that only describes RDI after that clobbering. * DebugInfo/ARM/partial-subreg.ll: This test seems to indiciate a bug in LiveDebugValues that is caused by it not being aware of fragments. I have added some comments in the test case about that. Also, before this patch DwarfDebug would incorrectly include a register-described debug value from a preceding block in a location list entry. Reviewers: aprantl, probinson, dblaikie, rnk, bjope Reviewed By: aprantl Subscribers: javed.absar, kristof.beyls, jdoerfert, llvm-commits Tags: #debug-info, #llvm Differential Revision: https://reviews.llvm.org/D59941 llvm-svn: 358072
2019-04-10 19:28:20 +08:00
if (Entry.isDbgValue())
requestLabelBeforeInsn(Entry.getInstr());
else
requestLabelAfterInsn(Entry.getInstr());
}
}
// Ensure there is a symbol before DBG_LABEL.
for (const auto &I : DbgLabels) {
const MachineInstr *MI = I.second;
requestLabelBeforeInsn(MI);
}
PrevInstLoc = DebugLoc();
PrevLabel = Asm->getFunctionBegin();
beginFunctionImpl(MF);
}
void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {
if (!Asm || !MMI->hasDebugInfo())
return;
assert(CurMI == nullptr);
CurMI = MI;
// Insert labels where requested.
DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
LabelsBeforeInsn.find(MI);
// No label needed.
if (I == LabelsBeforeInsn.end())
return;
// Label already assigned.
if (I->second)
return;
if (!PrevLabel) {
PrevLabel = MMI->getContext().createTempSymbol();
Asm->OutStreamer->emitLabel(PrevLabel);
}
I->second = PrevLabel;
}
void DebugHandlerBase::endInstruction() {
if (!Asm || !MMI->hasDebugInfo())
return;
assert(CurMI != nullptr);
// Don't create a new label after DBG_VALUE and other instructions that don't
// generate code.
if (!CurMI->isMetaInstruction()) {
PrevLabel = nullptr;
PrevInstBB = CurMI->getParent();
}
DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
LabelsAfterInsn.find(CurMI);
// No label needed or label already assigned.
if (I == LabelsAfterInsn.end() || I->second) {
CurMI = nullptr;
return;
}
// We need a label after this instruction. With basic block sections, just
// use the end symbol of the section if this is the last instruction of the
// section. This reduces the need for an additional label and also helps
// merging ranges.
if (CurMI->getParent()->isEndSection() && CurMI->getNextNode() == nullptr) {
PrevLabel = CurMI->getParent()->getEndSymbol();
} else if (!PrevLabel) {
PrevLabel = MMI->getContext().createTempSymbol();
Asm->OutStreamer->emitLabel(PrevLabel);
}
I->second = PrevLabel;
CurMI = nullptr;
}
void DebugHandlerBase::endFunction(const MachineFunction *MF) {
if (Asm && hasDebugInfo(MMI, MF))
endFunctionImpl(MF);
DbgValues.clear();
DbgLabels.clear();
LabelsBeforeInsn.clear();
LabelsAfterInsn.clear();
InstOrdering.clear();
}
void DebugHandlerBase::beginBasicBlock(const MachineBasicBlock &MBB) {
if (!MBB.isBeginSection())
return;
PrevLabel = MBB.getSymbol();
}
void DebugHandlerBase::endBasicBlock(const MachineBasicBlock &MBB) {
if (!MBB.isEndSection())
return;
PrevLabel = nullptr;
}