Skip to content

Commit 3a66c1c

Browse files
committed
[DebugInfo] Insert DW_OP_deref when spilling indirect DBG_VALUEs
Summary: This comes up in optimized debug info for C++ programs that pass and return objects indirectly by address. In these programs, llvm.dbg.declare survives optimization, which causes us to emit indirect DBG_VALUE instructions. The fast register allocator knows to insert DW_OP_deref when spilling indirect DBG_VALUE instructions, but the LiveDebugVariables did not until this change. This fixes part of PR34513. I need to look into why this doesn't work at -O0 and I'll send follow up patches to handle that. Reviewers: aprantl, dblaikie, probinson Subscribers: qcolombet, hiraditya, llvm-commits Differential Revision: https://reviews.llvm.org/D37911 llvm-svn: 313400
1 parent 9e6c309 commit 3a66c1c

File tree

4 files changed

+248
-38
lines changed

4 files changed

+248
-38
lines changed

llvm/lib/CodeGen/LiveDebugVariables.cpp

+53-35
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ class LDVImpl;
108108
/// held by the same virtual register. The equivalence class is the transitive
109109
/// closure of that relation.
110110
class UserValue {
111-
const MDNode *Variable; ///< The debug info variable we are part of.
112-
const MDNode *Expression; ///< Any complex address expression.
111+
const DILocalVariable *Variable; ///< The debug info variable we are part of.
112+
const DIExpression *Expression; ///< Any complex address expression.
113113
bool IsIndirect; ///< true if this is a register-indirect+offset value.
114114
DebugLoc dl; ///< The debug location for the variable. This is
115115
///< used by dwarf writer to find lexical scope.
@@ -132,8 +132,9 @@ class UserValue {
132132
void coalesceLocation(unsigned LocNo);
133133

134134
/// insertDebugValue - Insert a DBG_VALUE into MBB at Idx for LocNo.
135-
void insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx, unsigned LocNo,
136-
LiveIntervals &LIS, const TargetInstrInfo &TII);
135+
void insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx,
136+
unsigned LocNo, bool Spilled, LiveIntervals &LIS,
137+
const TargetInstrInfo &TII);
137138

138139
/// splitLocation - Replace OldLocNo ranges with NewRegs ranges where NewRegs
139140
/// is live. Returns true if any changes were made.
@@ -142,8 +143,8 @@ class UserValue {
142143

143144
public:
144145
/// UserValue - Create a new UserValue.
145-
UserValue(const MDNode *var, const MDNode *expr, bool i, DebugLoc L,
146-
LocMap::Allocator &alloc)
146+
UserValue(const DILocalVariable *var, const DIExpression *expr, bool i,
147+
DebugLoc L, LocMap::Allocator &alloc)
147148
: Variable(var), Expression(expr), IsIndirect(i), dl(std::move(L)),
148149
leader(this), locInts(alloc) {}
149150

@@ -159,8 +160,8 @@ class UserValue {
159160
UserValue *getNext() const { return next; }
160161

161162
/// match - Does this UserValue match the parameters?
162-
bool match(const MDNode *Var, const MDNode *Expr, const DILocation *IA,
163-
bool indirect) const {
163+
bool match(const DILocalVariable *Var, const DIExpression *Expr,
164+
const DILocation *IA, bool indirect) const {
164165
return Var == Variable && Expr == Expression && dl->getInlinedAt() == IA &&
165166
indirect == IsIndirect;
166167
}
@@ -262,12 +263,14 @@ class UserValue {
262263
LiveIntervals &LIS);
263264

264265
/// rewriteLocations - Rewrite virtual register locations according to the
265-
/// provided virtual register map.
266-
void rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI);
266+
/// provided virtual register map. Record which locations were spilled.
267+
void rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI,
268+
BitVector &SpilledLocations);
267269

268270
/// emitDebugValues - Recreate DBG_VALUE instruction from data structures.
269-
void emitDebugValues(VirtRegMap *VRM,
270-
LiveIntervals &LIS, const TargetInstrInfo &TRI);
271+
void emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
272+
const TargetInstrInfo &TRI,
273+
const BitVector &SpilledLocations);
271274

272275
/// getDebugLoc - Return DebugLoc of this UserValue.
273276
DebugLoc getDebugLoc() { return dl;}
@@ -297,11 +300,11 @@ class LDVImpl {
297300
VRMap virtRegToEqClass;
298301

299302
/// Map user variable to eq class leader.
300-
using UVMap = DenseMap<const MDNode *, UserValue *>;
303+
using UVMap = DenseMap<const DILocalVariable *, UserValue *>;
301304
UVMap userVarMap;
302305

303306
/// getUserValue - Find or create a UserValue.
304-
UserValue *getUserValue(const MDNode *Var, const MDNode *Expr,
307+
UserValue *getUserValue(const DILocalVariable *Var, const DIExpression *Expr,
305308
bool IsIndirect, const DebugLoc &DL);
306309

307310
/// lookupVirtReg - Find the EC leader for VirtReg or null.
@@ -454,8 +457,9 @@ void UserValue::mapVirtRegs(LDVImpl *LDV) {
454457
LDV->mapVirtReg(locations[i].getReg(), this);
455458
}
456459

457-
UserValue *LDVImpl::getUserValue(const MDNode *Var, const MDNode *Expr,
458-
bool IsIndirect, const DebugLoc &DL) {
460+
UserValue *LDVImpl::getUserValue(const DILocalVariable *Var,
461+
const DIExpression *Expr, bool IsIndirect,
462+
const DebugLoc &DL) {
459463
UserValue *&Leader = userVarMap[Var];
460464
if (Leader) {
461465
UserValue *UV = Leader->getLeader();
@@ -494,11 +498,11 @@ bool LDVImpl::handleDebugValue(MachineInstr &MI, SlotIndex Idx) {
494498
}
495499

496500
// Get or create the UserValue for (variable,offset).
497-
bool IsIndirect = MI.isIndirectDebugValue();
501+
bool IsIndirect = MI.getOperand(1).isImm();
498502
if (IsIndirect)
499503
assert(MI.getOperand(1).getImm() == 0 && "DBG_VALUE with nonzero offset");
500-
const MDNode *Var = MI.getDebugVariable();
501-
const MDNode *Expr = MI.getDebugExpression();
504+
const DILocalVariable *Var = MI.getDebugVariable();
505+
const DIExpression *Expr = MI.getDebugExpression();
502506
//here.
503507
UserValue *UV = getUserValue(Var, Expr, IsIndirect, MI.getDebugLoc());
504508
UV->addDef(Idx, MI.getOperand(0));
@@ -971,8 +975,10 @@ splitRegister(unsigned OldReg, ArrayRef<unsigned> NewRegs, LiveIntervals &LIS) {
971975
static_cast<LDVImpl*>(pImpl)->splitRegister(OldReg, NewRegs);
972976
}
973977

974-
void
975-
UserValue::rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI) {
978+
void UserValue::rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI,
979+
BitVector &SpilledLocations) {
980+
SpilledLocations.resize(locations.size());
981+
976982
// Iterate over locations in reverse makes it easier to handle coalescing.
977983
for (unsigned i = locations.size(); i ; --i) {
978984
unsigned LocNo = i-1;
@@ -991,6 +997,7 @@ UserValue::rewriteLocations(VirtRegMap &VRM, const TargetRegisterInfo &TRI) {
991997
} else if (VRM.getStackSlot(VirtReg) != VirtRegMap::NO_STACK_SLOT) {
992998
// FIXME: Translate SubIdx to a stackslot offset.
993999
Loc = MachineOperand::CreateFI(VRM.getStackSlot(VirtReg));
1000+
SpilledLocations.set(LocNo);
9941001
} else {
9951002
Loc.setReg(0);
9961003
Loc.setSubReg(0);
@@ -1024,7 +1031,7 @@ findInsertLocation(MachineBasicBlock *MBB, SlotIndex Idx,
10241031
}
10251032

10261033
void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx,
1027-
unsigned LocNo,
1034+
unsigned LocNo, bool Spilled,
10281035
LiveIntervals &LIS,
10291036
const TargetInstrInfo &TII) {
10301037
MachineBasicBlock::iterator I = findInsertLocation(MBB, Idx, LIS);
@@ -1034,25 +1041,35 @@ void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex Idx,
10341041
assert(cast<DILocalVariable>(Variable)
10351042
->isValidLocationForIntrinsic(getDebugLoc()) &&
10361043
"Expected inlined-at fields to agree");
1037-
if (Loc.isReg())
1038-
BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_VALUE),
1039-
IsIndirect, Loc.getReg(), Variable, Expression);
1044+
1045+
// If the location was spilled, the new DBG_VALUE will be indirect. If the
1046+
// original DBG_VALUE was indirect, we need to add DW_OP_deref to indicate
1047+
// that the original virtual register was a pointer.
1048+
bool NewIndirect = IsIndirect || Spilled;
1049+
const DIExpression *Expr = Expression;
1050+
if (Spilled && IsIndirect)
1051+
Expr = DIExpression::prepend(Expr, DIExpression::WithDeref);
1052+
1053+
MachineInstrBuilder MIB =
1054+
BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_VALUE))
1055+
.add(Loc);
1056+
if (NewIndirect)
1057+
MIB.addImm(0U);
10401058
else
1041-
BuildMI(*MBB, I, getDebugLoc(), TII.get(TargetOpcode::DBG_VALUE))
1042-
.add(Loc)
1043-
.addImm(0U)
1044-
.addMetadata(Variable)
1045-
.addMetadata(Expression);
1059+
MIB.addReg(0U, RegState::Debug);
1060+
MIB.addMetadata(Variable).addMetadata(Expr);
10461061
}
10471062

10481063
void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
1049-
const TargetInstrInfo &TII) {
1064+
const TargetInstrInfo &TII,
1065+
const BitVector &SpilledLocations) {
10501066
MachineFunction::iterator MFEnd = VRM->getMachineFunction().end();
10511067

10521068
for (LocMap::const_iterator I = locInts.begin(); I.valid();) {
10531069
SlotIndex Start = I.start();
10541070
SlotIndex Stop = I.stop();
10551071
unsigned LocNo = I.value();
1072+
bool Spilled = LocNo != ~0U ? SpilledLocations.test(LocNo) : false;
10561073

10571074
// If the interval start was trimmed to the lexical scope insert the
10581075
// DBG_VALUE at the previous index (otherwise it appears after the
@@ -1065,7 +1082,7 @@ void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
10651082
SlotIndex MBBEnd = LIS.getMBBEndIdx(&*MBB);
10661083

10671084
DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
1068-
insertDebugValue(&*MBB, Start, LocNo, LIS, TII);
1085+
insertDebugValue(&*MBB, Start, LocNo, Spilled, LIS, TII);
10691086
// This interval may span multiple basic blocks.
10701087
// Insert a DBG_VALUE into each one.
10711088
while(Stop > MBBEnd) {
@@ -1075,7 +1092,7 @@ void UserValue::emitDebugValues(VirtRegMap *VRM, LiveIntervals &LIS,
10751092
break;
10761093
MBBEnd = LIS.getMBBEndIdx(&*MBB);
10771094
DEBUG(dbgs() << " BB#" << MBB->getNumber() << '-' << MBBEnd);
1078-
insertDebugValue(&*MBB, Start, LocNo, LIS, TII);
1095+
insertDebugValue(&*MBB, Start, LocNo, Spilled, LIS, TII);
10791096
}
10801097
DEBUG(dbgs() << '\n');
10811098
if (MBB == MFEnd)
@@ -1090,10 +1107,11 @@ void LDVImpl::emitDebugValues(VirtRegMap *VRM) {
10901107
if (!MF)
10911108
return;
10921109
const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1110+
BitVector SpilledLocations;
10931111
for (unsigned i = 0, e = userValues.size(); i != e; ++i) {
10941112
DEBUG(userValues[i]->print(dbgs(), TRI));
1095-
userValues[i]->rewriteLocations(*VRM, *TRI);
1096-
userValues[i]->emitDebugValues(VRM, *LIS, *TII);
1113+
userValues[i]->rewriteLocations(*VRM, *TRI, SpilledLocations);
1114+
userValues[i]->emitDebugValues(VRM, *LIS, *TII, SpilledLocations);
10971115
}
10981116
EmitDone = true;
10991117
}

llvm/test/DebugInfo/X86/bbjoin.ll

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
; }
1212
; CHECK: ![[X:.*]] = !DILocalVariable(name: "x",
1313
; CHECK: bb.0.entry:
14-
; CHECK: DBG_VALUE 23, 0, ![[X]],
14+
; CHECK: DBG_VALUE 23, debug-use _, ![[X]],
1515
; CHECK: DBG_VALUE %rsp, 0, ![[X]], !DIExpression(DW_OP_plus_uconst, 4, DW_OP_deref),
1616
; CHECK: bb.1.if.then:
17-
; CHECK: DBG_VALUE 43, 0, ![[X]],
17+
; CHECK: DBG_VALUE 43, debug-use _, ![[X]],
1818
; CHECK: bb.2.if.end:
19-
; CHECK-NOT: DBG_VALUE 23, 0, ![[X]],
19+
; CHECK-NOT: DBG_VALUE 23, debug-use _, ![[X]],
2020
; CHECK: RETQ %eax
2121

2222
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
; RUN: llc < %s | FileCheck %s
2+
; RUN: llc -O0 < %s | FileCheck %s
3+
4+
; Make sure we insert DW_OP_deref when spilling indirect DBG_VALUE instructions.
5+
6+
; C++ source:
7+
; #define FORCE_SPILL() \
8+
; __asm volatile("" : : : \
9+
; "rax", "rbx", "rcx", "rdx", "rsi", "rdi", "rbp", "r8", \
10+
; "r9", "r10", "r11", "r12", "r13", "r14", "r15")
11+
; struct string {
12+
; string();
13+
; string(int i);
14+
; ~string();
15+
; int i = 0;
16+
; };
17+
; string get_string() {
18+
; string result = 3;
19+
; FORCE_SPILL();
20+
; return result;
21+
; }
22+
23+
; CHECK-LABEL: _Z10get_stringv:
24+
; CHECK: #DEBUG_VALUE: get_string:result <- [%RDI+0]
25+
; CHECK: movq %rdi, [[OFFS:[0-9]+]](%rsp) # 8-byte Spill
26+
; CHECK: #DEBUG_VALUE: get_string:result <- [DW_OP_plus_uconst [[OFFS]], DW_OP_deref] [%RSP+0]
27+
; CHECK: callq _ZN6stringC1Ei
28+
; CHECK: #APP
29+
; CHECK: #NO_APP
30+
31+
; ModuleID = 't.cpp'
32+
source_filename = "t.cpp"
33+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
34+
target triple = "x86_64--linux"
35+
36+
%struct.string = type { i32 }
37+
38+
; Function Attrs: uwtable
39+
define void @_Z10get_stringv(%struct.string* noalias sret %agg.result) #0 !dbg !7 {
40+
entry:
41+
%nrvo = alloca i1, align 1
42+
store i1 false, i1* %nrvo, align 1, !dbg !24
43+
call void @llvm.dbg.declare(metadata %struct.string* %agg.result, metadata !23, metadata !DIExpression()), !dbg !25
44+
call void @_ZN6stringC1Ei(%struct.string* %agg.result, i32 3), !dbg !26
45+
call void asm sideeffect "", "~{rax},~{rbx},~{rcx},~{rdx},~{rsi},~{rdi},~{rbp},~{r8},~{r9},~{r10},~{r11},~{r12},~{r13},~{r14},~{r15},~{dirflag},~{fpsr},~{flags}"() #3, !dbg !27, !srcloc !28
46+
store i1 true, i1* %nrvo, align 1, !dbg !29
47+
%nrvo.val = load i1, i1* %nrvo, align 1, !dbg !30
48+
br i1 %nrvo.val, label %nrvo.skipdtor, label %nrvo.unused, !dbg !30
49+
50+
nrvo.unused: ; preds = %entry
51+
call void @_ZN6stringD1Ev(%struct.string* %agg.result), !dbg !30
52+
br label %nrvo.skipdtor, !dbg !30
53+
54+
nrvo.skipdtor: ; preds = %nrvo.unused, %entry
55+
ret void, !dbg !30
56+
}
57+
58+
; Function Attrs: nounwind readnone speculatable
59+
declare void @llvm.dbg.declare(metadata, metadata, metadata) #1
60+
61+
declare void @_ZN6stringC1Ei(%struct.string*, i32) unnamed_addr
62+
63+
declare void @_ZN6stringD1Ev(%struct.string*) unnamed_addr
64+
65+
attributes #0 = { uwtable }
66+
attributes #1 = { nounwind readnone speculatable }
67+
attributes #3 = { nounwind }
68+
69+
!llvm.dbg.cu = !{!0}
70+
!llvm.module.flags = !{!3, !4, !5}
71+
!llvm.ident = !{!6}
72+
73+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 6.0.0 ", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
74+
!1 = !DIFile(filename: "t.cpp", directory: "C:\5Csrc\5Cllvm-project\5Cbuild")
75+
!2 = !{}
76+
!3 = !{i32 2, !"Dwarf Version", i32 4}
77+
!4 = !{i32 2, !"Debug Info Version", i32 3}
78+
!5 = !{i32 1, !"wchar_size", i32 4}
79+
!6 = !{!"clang version 6.0.0 "}
80+
!7 = distinct !DISubprogram(name: "get_string", linkageName: "_Z10get_stringv", scope: !1, file: !1, line: 13, type: !8, isLocal: false, isDefinition: true, scopeLine: 13, flags: DIFlagPrototyped, isOptimized: true, unit: !0, variables: !22)
81+
!8 = !DISubroutineType(types: !9)
82+
!9 = !{!10}
83+
!10 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "string", file: !1, line: 7, size: 32, elements: !11, identifier: "_ZTS6string")
84+
!11 = !{!12, !14, !18, !21}
85+
!12 = !DIDerivedType(tag: DW_TAG_member, name: "i", scope: !10, file: !1, line: 11, baseType: !13, size: 32)
86+
!13 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
87+
!14 = !DISubprogram(name: "string", scope: !10, file: !1, line: 8, type: !15, isLocal: false, isDefinition: false, scopeLine: 8, flags: DIFlagPrototyped, isOptimized: true)
88+
!15 = !DISubroutineType(types: !16)
89+
!16 = !{null, !17}
90+
!17 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !10, size: 64, flags: DIFlagArtificial | DIFlagObjectPointer)
91+
!18 = !DISubprogram(name: "string", scope: !10, file: !1, line: 9, type: !19, isLocal: false, isDefinition: false, scopeLine: 9, flags: DIFlagPrototyped, isOptimized: true)
92+
!19 = !DISubroutineType(types: !20)
93+
!20 = !{null, !17, !13}
94+
!21 = !DISubprogram(name: "~string", scope: !10, file: !1, line: 10, type: !15, isLocal: false, isDefinition: false, scopeLine: 10, flags: DIFlagPrototyped, isOptimized: true)
95+
!22 = !{!23}
96+
!23 = !DILocalVariable(name: "result", scope: !7, file: !1, line: 14, type: !10)
97+
!24 = !DILocation(line: 14, column: 3, scope: !7)
98+
!25 = !DILocation(line: 14, column: 10, scope: !7)
99+
!26 = !DILocation(line: 14, column: 19, scope: !7)
100+
!27 = !DILocation(line: 15, column: 3, scope: !7)
101+
!28 = !{i32 -2147471175}
102+
!29 = !DILocation(line: 16, column: 3, scope: !7)
103+
!30 = !DILocation(line: 17, column: 1, scope: !7)

0 commit comments

Comments
 (0)