Skip to content

Commit 3ab03ad

Browse files
dstenbmikaelholmen
andauthored
[SelectionDAG] Salvage debug info for non-constant ADDs (#68981)
Teach SelectionDAG::salvageDebugInfo() to salvage debug information for ADD nodes where the RHS is non-constant. Co-authored-by: Mikael Holmen <[email protected]> - [DebugInfo] Precommit testcase for pointer addition with unknown offset - [SelectionDAG] Salvage debug info for non-constant ADDs --------- Co-authored-by: Mikael Holmen <[email protected]>
1 parent 5efa84c commit 3ab03ad

File tree

2 files changed

+93
-10
lines changed

2 files changed

+93
-10
lines changed

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "llvm/Analysis/MemoryLocation.h"
2828
#include "llvm/Analysis/ValueTracking.h"
2929
#include "llvm/Analysis/VectorUtils.h"
30+
#include "llvm/BinaryFormat/Dwarf.h"
3031
#include "llvm/CodeGen/Analysis.h"
3132
#include "llvm/CodeGen/FunctionLoweringInfo.h"
3233
#include "llvm/CodeGen/ISDOpcodes.h"
@@ -10843,8 +10844,11 @@ void SelectionDAG::salvageDebugInfo(SDNode &N) {
1084310844
case ISD::ADD: {
1084410845
SDValue N0 = N.getOperand(0);
1084510846
SDValue N1 = N.getOperand(1);
10846-
if (!isa<ConstantSDNode>(N0) && isa<ConstantSDNode>(N1)) {
10847-
uint64_t Offset = N.getConstantOperandVal(1);
10847+
if (!isa<ConstantSDNode>(N0)) {
10848+
bool RHSConstant = isa<ConstantSDNode>(N1);
10849+
uint64_t Offset;
10850+
if (RHSConstant)
10851+
Offset = N.getConstantOperandVal(1);
1084810852

1084910853
// Rewrite an ADD constant node into a DIExpression. Since we are
1085010854
// performing arithmetic to compute the variable's *value* in the
@@ -10853,27 +10857,46 @@ void SelectionDAG::salvageDebugInfo(SDNode &N) {
1085310857
auto *DIExpr = DV->getExpression();
1085410858
auto NewLocOps = DV->copyLocationOps();
1085510859
bool Changed = false;
10856-
for (size_t i = 0; i < NewLocOps.size(); ++i) {
10860+
size_t OrigLocOpsSize = NewLocOps.size();
10861+
for (size_t i = 0; i < OrigLocOpsSize; ++i) {
1085710862
// We're not given a ResNo to compare against because the whole
1085810863
// node is going away. We know that any ISD::ADD only has one
1085910864
// result, so we can assume any node match is using the result.
1086010865
if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
1086110866
NewLocOps[i].getSDNode() != &N)
1086210867
continue;
1086310868
NewLocOps[i] = SDDbgOperand::fromNode(N0.getNode(), N0.getResNo());
10864-
SmallVector<uint64_t, 3> ExprOps;
10865-
DIExpression::appendOffset(ExprOps, Offset);
10866-
DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
10869+
if (RHSConstant) {
10870+
SmallVector<uint64_t, 3> ExprOps;
10871+
DIExpression::appendOffset(ExprOps, Offset);
10872+
DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
10873+
} else {
10874+
// Convert to a variadic expression (if not already).
10875+
// convertToVariadicExpression() returns a const pointer, so we use
10876+
// a temporary const variable here.
10877+
const auto *TmpDIExpr =
10878+
DIExpression::convertToVariadicExpression(DIExpr);
10879+
SmallVector<uint64_t, 3> ExprOps;
10880+
ExprOps.push_back(dwarf::DW_OP_LLVM_arg);
10881+
ExprOps.push_back(NewLocOps.size());
10882+
ExprOps.push_back(dwarf::DW_OP_plus);
10883+
SDDbgOperand RHS =
10884+
SDDbgOperand::fromNode(N1.getNode(), N1.getResNo());
10885+
NewLocOps.push_back(RHS);
10886+
DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
10887+
}
1086710888
Changed = true;
1086810889
}
1086910890
(void)Changed;
1087010891
assert(Changed && "Salvage target doesn't use N");
1087110892

10893+
bool IsVariadic =
10894+
DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
10895+
1087210896
auto AdditionalDependencies = DV->getAdditionalDependencies();
10873-
SDDbgValue *Clone = getDbgValueList(DV->getVariable(), DIExpr,
10874-
NewLocOps, AdditionalDependencies,
10875-
DV->isIndirect(), DV->getDebugLoc(),
10876-
DV->getOrder(), DV->isVariadic());
10897+
SDDbgValue *Clone = getDbgValueList(
10898+
DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
10899+
DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
1087710900
ClonedDVs.push_back(Clone);
1087810901
DV->setIsInvalidated();
1087910902
DV->setIsEmitted();
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 3
2+
# RUN: llc -mtriple=sparcv9 %s -start-before=sparc-isel -o - -stop-after=sparc-isel | FileCheck %s
3+
--- |
4+
target datalayout = "E-m:e-i64:64-n32:64-S128"
5+
target triple = "sparcv9"
6+
7+
define void @pointer_add_unknown_offset(ptr %base, i32 %offset) !dbg !7 {
8+
entry:
9+
%idx.ext = sext i32 %offset to i64
10+
%add.ptr = getelementptr inbounds i32, ptr %base, i64 %idx.ext
11+
call void @llvm.dbg.value(metadata ptr %add.ptr, metadata !13, metadata !DIExpression()), !dbg !16
12+
call void @llvm.dbg.value(metadata ptr %add.ptr, metadata !14, metadata !DIExpression(DW_OP_plus_uconst, 3, DW_OP_stack_value)), !dbg !16
13+
call void @llvm.dbg.value(metadata !DIArgList(ptr %add.ptr, ptr %add.ptr), metadata !15, metadata !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_plus)), !dbg !16
14+
store i32 42, ptr %add.ptr, align 4
15+
ret void
16+
}
17+
18+
declare void @llvm.dbg.value(metadata, metadata, metadata)
19+
20+
!llvm.dbg.cu = !{!0}
21+
!llvm.module.flags = !{!2, !3, !4, !5}
22+
!llvm.ident = !{!6}
23+
24+
!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
25+
!1 = !DIFile(filename: "test.c", directory: "/tmp", checksumkind: CSK_MD5, checksum: "4321633e52cefeee6e307c697a82574f")
26+
!2 = !{i32 7, !"Dwarf Version", i32 5}
27+
!3 = !{i32 2, !"Debug Info Version", i32 3}
28+
!4 = !{i32 1, !"wchar_size", i32 4}
29+
!5 = !{i32 7, !"debug-info-assignment-tracking", i1 true}
30+
!6 = !{!"clang"}
31+
!7 = distinct !DISubprogram(name: "pointer_add_unknown_offset", scope: !1, file: !1, line: 1, type: !8, scopeLine: 1, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !12)
32+
!8 = !DISubroutineType(types: !9)
33+
!9 = !{null, !10, !11}
34+
!10 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !11, size: 64)
35+
!11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
36+
!12 = !{!13, !14, !15}
37+
!13 = !DILocalVariable(name: "p", scope: !7, file: !1, line: 2, type: !10)
38+
!14 = !DILocalVariable(name: "q", scope: !7, file: !1, line: 2, type: !10)
39+
!15 = !DILocalVariable(name: "r", scope: !7, file: !1, line: 2, type: !10)
40+
!16 = !DILocation(line: 0, scope: !7)
41+
...
42+
---
43+
name: pointer_add_unknown_offset
44+
alignment: 4
45+
tracksRegLiveness: true
46+
body: |
47+
; CHECK-LABEL: name: pointer_add_unknown_offset
48+
; CHECK: bb.0.entry:
49+
; CHECK-NEXT: liveins: $i0, $i1
50+
; CHECK-NEXT: {{ $}}
51+
; CHECK-NEXT: [[COPY:%[0-9]+]]:i64regs = COPY $i1
52+
; CHECK-NEXT: [[COPY1:%[0-9]+]]:i64regs = COPY $i0
53+
; CHECK-NEXT: [[SRAri:%[0-9]+]]:i64regs = SRAri [[COPY]], 0
54+
; CHECK-NEXT: [[SLLXri:%[0-9]+]]:i64regs = SLLXri killed [[SRAri]], 2
55+
; CHECK-NEXT: DBG_VALUE_LIST !13, !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_plus, DW_OP_stack_value), [[COPY1]], [[SLLXri]], debug-location !16
56+
; CHECK-NEXT: DBG_VALUE_LIST !14, !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_plus, DW_OP_plus_uconst, 3, DW_OP_stack_value), [[COPY1]], [[SLLXri]], debug-location !16
57+
; CHECK-NEXT: DBG_VALUE_LIST !15, !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 2, DW_OP_plus, DW_OP_LLVM_arg, 1, DW_OP_LLVM_arg, 3, DW_OP_plus, DW_OP_plus, DW_OP_stack_value), [[COPY1]], [[COPY1]], [[SLLXri]], [[SLLXri]], debug-location !16
58+
; CHECK-NEXT: [[ORri:%[0-9]+]]:intregs = ORri $g0, 42
59+
; CHECK-NEXT: STrr [[COPY1]], killed [[SLLXri]], killed [[ORri]] :: (store (s32) into %ir.add.ptr)
60+
; CHECK-NEXT: RETL 8

0 commit comments

Comments
 (0)