Skip to content

Commit 6d6b395

Browse files
Ben MuddSLTozer
authored andcommitted
[DebugInfo][SelectionDAG] Add debug info salvaging for TRUNC nodes
This patch adds support for salvaging TRUNC nodes during SelectionDAG, fixing LLVM issue #63076: #63076 Reviewed in: #66922
1 parent 1556ddf commit 6d6b395

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13512,8 +13512,12 @@ SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
1351213512
N0.getScalarValueSizeInBits(),
1351313513
std::min(Op.getScalarValueSizeInBits(),
1351413514
VT.getScalarSizeInBits()));
13515-
if (TruncatedBits.isSubsetOf(Known.Zero))
13516-
return DAG.getZExtOrTrunc(Op, DL, VT);
13515+
if (TruncatedBits.isSubsetOf(Known.Zero)) {
13516+
SDValue ZExtOrTrunc = DAG.getZExtOrTrunc(Op, DL, VT);
13517+
DAG.salvageDebugInfo(*N0.getNode());
13518+
13519+
return ZExtOrTrunc;
13520+
}
1351713521
}
1351813522

1351913523
// fold (zext (truncate x)) -> (and x, mask)

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10778,7 +10778,7 @@ void SelectionDAG::salvageDebugInfo(SDNode &N) {
1077810778
switch (N.getOpcode()) {
1077910779
default:
1078010780
break;
10781-
case ISD::ADD:
10781+
case ISD::ADD: {
1078210782
SDValue N0 = N.getOperand(0);
1078310783
SDValue N1 = N.getOperand(1);
1078410784
if (!isa<ConstantSDNode>(N0) && isa<ConstantSDNode>(N1)) {
@@ -10819,6 +10819,40 @@ void SelectionDAG::salvageDebugInfo(SDNode &N) {
1081910819
N0.getNode()->dumprFull(this);
1082010820
dbgs() << " into " << *DIExpr << '\n');
1082110821
}
10822+
break;
10823+
}
10824+
case ISD::TRUNCATE: {
10825+
SDValue N0 = N.getOperand(0);
10826+
TypeSize FromSize = N0.getValueSizeInBits();
10827+
TypeSize ToSize = N.getValueSizeInBits(0);
10828+
10829+
DIExpression *DbgExpression = DV->getExpression();
10830+
auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
10831+
auto NewLocOps = DV->copyLocationOps();
10832+
bool Changed = false;
10833+
for (size_t i = 0; i < NewLocOps.size(); ++i) {
10834+
if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
10835+
NewLocOps[i].getSDNode() != &N)
10836+
continue;
10837+
10838+
NewLocOps[i] = SDDbgOperand::fromNode(N0.getNode(), N0.getResNo());
10839+
DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
10840+
Changed = true;
10841+
}
10842+
assert(Changed && "Salvage target doesn't use N");
10843+
10844+
SDDbgValue *Clone =
10845+
getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
10846+
DV->getAdditionalDependencies(), DV->isIndirect(),
10847+
DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
10848+
10849+
ClonedDVs.push_back(Clone);
10850+
DV->setIsInvalidated();
10851+
DV->setIsEmitted();
10852+
LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
10853+
dbgs() << " into " << *DbgExpression << '\n');
10854+
break;
10855+
}
1082210856
}
1082310857
}
1082410858

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
; RUN: llc --stop-after=finalize-isel < %s | FileCheck %s
2+
;
3+
; Verify that we can correctly salvage truncate expressions during SelectionDAG.
4+
; Fixes LLVM issue #63076.
5+
6+
; CHECK: body
7+
; CHECK: DBG_INSTR_REF !{{[0-9]+}}, !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_convert, 8, DW_ATE_unsigned, DW_OP_LLVM_convert, 1, DW_ATE_unsigned)
8+
9+
source_filename = "repro.cpp"
10+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
11+
target triple = "x86_64-unknown-linux-gnu"
12+
13+
define dso_local noundef i32 @_Z3funv() !dbg !10 {
14+
entry:
15+
%call = tail call noundef zeroext i1 @_Z3getv(), !dbg !17
16+
call void @llvm.dbg.value(metadata i1 %call, metadata !15, metadata !DIExpression()), !dbg !18
17+
%conv = zext i1 %call to i32, !dbg !19
18+
ret i32 %conv, !dbg !20
19+
}
20+
21+
declare !dbg !21 noundef zeroext i1 @_Z3getv()
22+
23+
declare void @llvm.dbg.value(metadata, metadata, metadata)
24+
25+
!llvm.dbg.cu = !{!0}
26+
!llvm.module.flags = !{!2, !3, !4, !5, !6, !7, !8}
27+
!llvm.ident = !{!9}
28+
29+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 18.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
30+
!1 = !DIFile(filename: "repro.cpp", directory: "/")
31+
!2 = !{i32 7, !"Dwarf Version", i32 5}
32+
!3 = !{i32 2, !"Debug Info Version", i32 3}
33+
!4 = !{i32 1, !"wchar_size", i32 4}
34+
!5 = !{i32 8, !"PIC Level", i32 2}
35+
!6 = !{i32 7, !"PIE Level", i32 2}
36+
!7 = !{i32 7, !"uwtable", i32 2}
37+
!8 = !{i32 7, !"debug-info-assignment-tracking", i1 true}
38+
!9 = !{!"clang version 18.0.0"}
39+
!10 = distinct !DISubprogram(name: "fun", linkageName: "_Z3funv", scope: !1, file: !1, line: 2, type: !11, scopeLine: 3, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !14)
40+
!11 = !DISubroutineType(types: !12)
41+
!12 = !{!13}
42+
!13 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
43+
!14 = !{!15}
44+
!15 = !DILocalVariable(name: "b", scope: !10, file: !1, line: 4, type: !16)
45+
!16 = !DIBasicType(name: "bool", size: 8, encoding: DW_ATE_boolean)
46+
!17 = !DILocation(line: 4, column: 11, scope: !10)
47+
!18 = !DILocation(line: 0, scope: !10)
48+
!19 = !DILocation(line: 5, column: 9, scope: !10)
49+
!20 = !DILocation(line: 5, column: 2, scope: !10)
50+
!21 = !DISubprogram(name: "get", linkageName: "_Z3getv", scope: !1, file: !1, line: 1, type: !22, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized)
51+
!22 = !DISubroutineType(types: !23)
52+
!23 = !{!16}

0 commit comments

Comments
 (0)