Skip to content

Commit a095d14

Browse files
committed
Fix an assertion failure in DwarfExpression's subregister composition
This patch fixes an assertion failure in DwarfExpression that is triggered when a complex fragment has exactly the size of a subregister of the register the DBG_VALUE points to *and* there is no DWARF encoding for the super-register. I took the opportunity to replace/document some magic values with static constructor functions to make this code less confusing to read. rdar://problem/58489125 Differential Revision: https://reviews.llvm.org/D72938
1 parent 7bca4a2 commit a095d14

File tree

3 files changed

+77
-13
lines changed

3 files changed

+77
-13
lines changed

llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ bool DwarfExpression::addMachineReg(const TargetRegisterInfo &TRI,
100100
unsigned MachineReg, unsigned MaxSize) {
101101
if (!llvm::Register::isPhysicalRegister(MachineReg)) {
102102
if (isFrameRegister(TRI, MachineReg)) {
103-
DwarfRegs.push_back({-1, 0, nullptr});
103+
DwarfRegs.push_back(Register::createRegister(-1, nullptr));
104104
return true;
105105
}
106106
return false;
@@ -110,7 +110,7 @@ bool DwarfExpression::addMachineReg(const TargetRegisterInfo &TRI,
110110

111111
// If this is a valid register number, emit it.
112112
if (Reg >= 0) {
113-
DwarfRegs.push_back({Reg, 0, nullptr});
113+
DwarfRegs.push_back(Register::createRegister(Reg, nullptr));
114114
return true;
115115
}
116116

@@ -122,7 +122,7 @@ bool DwarfExpression::addMachineReg(const TargetRegisterInfo &TRI,
122122
unsigned Idx = TRI.getSubRegIndex(*SR, MachineReg);
123123
unsigned Size = TRI.getSubRegIdxSize(Idx);
124124
unsigned RegOffset = TRI.getSubRegIdxOffset(Idx);
125-
DwarfRegs.push_back({Reg, 0, "super-register"});
125+
DwarfRegs.push_back(Register::createRegister(Reg, "super-register"));
126126
// Use a DW_OP_bit_piece to describe the sub-register.
127127
setSubRegisterPiece(Size, RegOffset);
128128
return true;
@@ -149,8 +149,8 @@ bool DwarfExpression::addMachineReg(const TargetRegisterInfo &TRI,
149149
if (Reg < 0)
150150
continue;
151151

152-
// Intersection between the bits we already emitted and the bits
153-
// covered by this subregister.
152+
// Used to build the intersection between the bits we already
153+
// emitted and the bits covered by this subregister.
154154
SmallBitVector CurSubReg(RegSize, false);
155155
CurSubReg.set(Offset, Offset + Size);
156156

@@ -159,10 +159,13 @@ bool DwarfExpression::addMachineReg(const TargetRegisterInfo &TRI,
159159
if (Offset < MaxSize && CurSubReg.test(Coverage)) {
160160
// Emit a piece for any gap in the coverage.
161161
if (Offset > CurPos)
162-
DwarfRegs.push_back(
163-
{-1, Offset - CurPos, "no DWARF register encoding"});
164-
DwarfRegs.push_back(
165-
{Reg, std::min<unsigned>(Size, MaxSize - Offset), "sub-register"});
162+
DwarfRegs.push_back(Register::createSubRegister(
163+
-1, Offset - CurPos, "no DWARF register encoding"));
164+
if (Offset == 0 && Size >= MaxSize)
165+
DwarfRegs.push_back(Register::createRegister(Reg, "sub-register"));
166+
else
167+
DwarfRegs.push_back(Register::createSubRegister(
168+
Reg, std::min<unsigned>(Size, MaxSize - Offset), "sub-register"));
166169
}
167170
// Mark it as emitted.
168171
Coverage.set(Offset, Offset + Size);
@@ -173,7 +176,8 @@ bool DwarfExpression::addMachineReg(const TargetRegisterInfo &TRI,
173176
return false;
174177
// Found a partial or complete DWARF encoding.
175178
if (CurPos < RegSize)
176-
DwarfRegs.push_back({-1, RegSize - CurPos, "no DWARF register encoding"});
179+
DwarfRegs.push_back(Register::createSubRegister(
180+
-1, RegSize - CurPos, "no DWARF register encoding"));
177181
return true;
178182
}
179183

@@ -249,7 +253,7 @@ bool DwarfExpression::addMachineRegExpression(const TargetRegisterInfo &TRI,
249253
for (auto &Reg : DwarfRegs) {
250254
if (Reg.DwarfRegNo >= 0)
251255
addReg(Reg.DwarfRegNo, Reg.Comment);
252-
addOpPiece(Reg.Size);
256+
addOpPiece(Reg.SubRegSize);
253257
}
254258

255259
if (isEntryValue())
@@ -276,7 +280,7 @@ bool DwarfExpression::addMachineRegExpression(const TargetRegisterInfo &TRI,
276280
auto Reg = DwarfRegs[0];
277281
bool FBReg = isFrameRegister(TRI, MachineReg);
278282
int SignedOffset = 0;
279-
assert(Reg.Size == 0 && "subregister has same size as superregister");
283+
assert(!Reg.isSubRegister() && "full register expected");
280284

281285
// Pattern-match combinations for which more efficient representations exist.
282286
// [Reg, DW_OP_plus_uconst, Offset] --> [DW_OP_breg, Offset].

llvm/lib/CodeGen/AsmPrinter/DwarfExpression.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,21 @@ class DwarfExpression {
107107
/// Holds information about all subregisters comprising a register location.
108108
struct Register {
109109
int DwarfRegNo;
110-
unsigned Size;
110+
unsigned SubRegSize;
111111
const char *Comment;
112+
113+
/// Create a full register, no extra DW_OP_piece operators necessary.
114+
static Register createRegister(int RegNo, const char *Comment) {
115+
return {RegNo, 0, Comment};
116+
}
117+
118+
/// Create a subregister that needs a DW_OP_piece operator with SizeInBits.
119+
static Register createSubRegister(int RegNo, unsigned SizeInBits,
120+
const char *Comment) {
121+
return {RegNo, SizeInBits, Comment};
122+
}
123+
124+
bool isSubRegister() const { return SubRegSize; }
112125
};
113126

114127
/// Whether we are currently emitting an entry value operation.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# RUN: llc -start-after=livedebugvalues -filetype=obj -o - %s | \
2+
# RUN: llvm-dwarfdump - | FileCheck %s
3+
#
4+
# This tests the edge-case where a complex fragment has exactly
5+
# the size of a subregister of the register the DBG_VALUE points to.
6+
#
7+
# CHECK: .debug_info contents:
8+
# CHECK: DW_TAG_variable
9+
# CHECK-NOT: DW_TAG
10+
# CHECK: DW_AT_location
11+
# Q8 = {D16, D17}
12+
# CHECK-NEXT: DW_OP_regx D16, DW_OP_piece 0x8)
13+
# CHECK-NOT: DW_TAG
14+
# CHECK: DW_AT_name ("q8")
15+
# CHECK: DW_TAG_variable
16+
# CHECK-NOT: DW_TAG
17+
# CHECK: DW_AT_location
18+
# Q9 = {D18, D19}
19+
# CHECK-NEXT: DW_OP_regx D18, DW_OP_piece 0x7)
20+
# CHECK-NOT: DW_TAG
21+
# CHECK: DW_AT_name ("q9")
22+
23+
--- |
24+
target triple = "thumbv7s-apple-ios"
25+
define hidden void @f() !dbg !5 {
26+
for.body:
27+
ret void, !dbg !20
28+
}
29+
!llvm.module.flags = !{!1, !2}
30+
!llvm.dbg.cu = !{!3}
31+
!1 = !{i32 7, !"Dwarf Version", i32 4}
32+
!2 = !{i32 2, !"Debug Info Version", i32 3}
33+
!3 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !4, isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
34+
!4 = !DIFile(filename: "t.cpp", directory: "/")
35+
!5 = distinct !DISubprogram(name: "f",scope: !4, file: !4, line: 1, type: !6, scopeLine: 1, unit: !3)
36+
!6 = !DISubroutineType(types: !{})
37+
!7 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "uint8x8x2_t", file: !4, line: 113, size: 128, flags: DIFlagTypePassByValue, elements: !{})
38+
!8 = !DILocalVariable(name: "q8", scope: !5, file: !4, line: 1, type: !7)
39+
!9 = !DILocalVariable(name: "q9", scope: !5, file: !4, line: 1, type: !7)
40+
!20 = !DILocation(line: 0, scope: !5)
41+
name: f
42+
body: |
43+
bb.2.for.body:
44+
t2Bcc %bb.2.for.body, 0, killed $cpsr, debug-location !20
45+
DBG_VALUE $q8, $noreg, !8, !DIExpression(DW_OP_LLVM_fragment, 0, 64), debug-location !20
46+
DBG_VALUE $q9, $noreg, !9, !DIExpression(DW_OP_LLVM_fragment, 0, 56), debug-location !20
47+
tB %bb.2.for.body, 14, $noreg

0 commit comments

Comments
 (0)