Skip to content

Commit 3773191

Browse files
authored
[DebugInfo][JumpThreading] Fix missing debug location updates (#91581)
1 parent 23fe1fc commit 3773191

File tree

4 files changed

+153
-5
lines changed

4 files changed

+153
-5
lines changed

llvm/lib/Transforms/Scalar/JumpThreading.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1279,9 +1279,11 @@ bool JumpThreadingPass::simplifyPartiallyRedundantLoad(LoadInst *LoadI) {
12791279
// only happen in dead loops.
12801280
if (AvailableVal == LoadI)
12811281
AvailableVal = PoisonValue::get(LoadI->getType());
1282-
if (AvailableVal->getType() != LoadI->getType())
1282+
if (AvailableVal->getType() != LoadI->getType()) {
12831283
AvailableVal = CastInst::CreateBitOrPointerCast(
12841284
AvailableVal, LoadI->getType(), "", LoadI->getIterator());
1285+
cast<Instruction>(AvailableVal)->setDebugLoc(LoadI->getDebugLoc());
1286+
}
12851287
LoadI->replaceAllUsesWith(AvailableVal);
12861288
LoadI->eraseFromParent();
12871289
return true;
@@ -2987,6 +2989,7 @@ bool JumpThreadingPass::tryToUnfoldSelectInCurrBB(BasicBlock *BB) {
29872989
PHINode *NewPN = PHINode::Create(SI->getType(), 2, "", SI->getIterator());
29882990
NewPN->addIncoming(SI->getTrueValue(), Term->getParent());
29892991
NewPN->addIncoming(SI->getFalseValue(), BB);
2992+
NewPN->setDebugLoc(SI->getDebugLoc());
29902993
SI->replaceAllUsesWith(NewPN);
29912994
SI->eraseFromParent();
29922995
// NewBB and SplitBB are newly created blocks which require insertion.
@@ -3124,6 +3127,7 @@ bool JumpThreadingPass::threadGuard(BasicBlock *BB, IntrinsicInst *Guard,
31243127
PHINode *NewPN = PHINode::Create(Inst->getType(), 2);
31253128
NewPN->addIncoming(UnguardedMapping[Inst], UnguardedBlock);
31263129
NewPN->addIncoming(GuardedMapping[Inst], GuardedBlock);
3130+
NewPN->setDebugLoc(Inst->getDebugLoc());
31273131
NewPN->insertBefore(InsertionPoint);
31283132
Inst->replaceAllUsesWith(NewPN);
31293133
}

llvm/test/Transforms/JumpThreading/guard-split-debuginfo.ll

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals
22
; RUN: opt -S -passes=jump-threading %s -o - -S | FileCheck %s
33
; RUN: opt -S -passes=jump-threading %s -o - -S --try-experimental-debuginfo-iterators | FileCheck %s
44

@@ -7,6 +7,9 @@
77
; parent blocks. And that ino jump-threading, the old dbg.value gets
88
; deleted.
99

10+
; Test that JumpThreading's threadGuard() propagates the debug location
11+
; to the `phi` from the instruction it replaces (`%retval`)
12+
1013
declare void @llvm.experimental.guard(i1, ...)
1114

1215
declare i32 @f1()
@@ -20,20 +23,20 @@ define i32 @branch_implies_guard(i32 %a) !dbg !7 {
2023
; CHECK-NEXT: br i1 [[COND]], label [[T1_SPLIT:%.*]], label [[F1_SPLIT:%.*]], !dbg [[DBG12:![0-9]+]]
2124
; CHECK: T1.split:
2225
; CHECK-NEXT: [[V1:%.*]] = call i32 @f1(), !dbg [[DBG12]]
23-
; CHECK-NEXT: call void @llvm.dbg.value(metadata i32 0, metadata [[META13:![0-9]+]], metadata !DIExpression()), !dbg [[DBG14:![0-9]+]]
26+
; CHECK-NEXT: tail call void @llvm.dbg.value(metadata i32 0, metadata [[META13:![0-9]+]], metadata !DIExpression()), !dbg [[DBG14:![0-9]+]]
2427
; CHECK-NEXT: [[RETVAL3:%.*]] = add i32 [[V1]], 10, !dbg [[DBG12]]
2528
; CHECK-NEXT: [[CONDGUARD4:%.*]] = icmp slt i32 [[A]], 20, !dbg [[DBG12]]
2629
; CHECK-NEXT: br label [[MERGE:%.*]]
2730
; CHECK: F1.split:
2831
; CHECK-NEXT: [[V2:%.*]] = call i32 @f2(), !dbg [[DBG12]]
29-
; CHECK-NEXT: call void @llvm.dbg.value(metadata i32 0, metadata [[META13]], metadata !DIExpression()), !dbg [[DBG14]]
32+
; CHECK-NEXT: tail call void @llvm.dbg.value(metadata i32 0, metadata [[META13]], metadata !DIExpression()), !dbg [[DBG14]]
3033
; CHECK-NEXT: [[RETVAL1:%.*]] = add i32 [[V2]], 10, !dbg [[DBG12]]
3134
; CHECK-NEXT: [[CONDGUARD2:%.*]] = icmp slt i32 [[A]], 20, !dbg [[DBG12]]
3235
; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 [[CONDGUARD2]]) [ "deopt"() ]
3336
; CHECK-NEXT: br label [[MERGE]]
3437
; CHECK: Merge:
3538
; CHECK-NEXT: [[RETPHI:%.*]] = phi i32 [ [[V1]], [[T1_SPLIT]] ], [ [[V2]], [[F1_SPLIT]] ]
36-
; CHECK-NEXT: [[TMP1:%.*]] = phi i32 [ [[RETVAL3]], [[T1_SPLIT]] ], [ [[RETVAL1]], [[F1_SPLIT]] ]
39+
; CHECK-NEXT: [[TMP1:%.*]] = phi i32 [ [[RETVAL3]], [[T1_SPLIT]] ], [ [[RETVAL1]], [[F1_SPLIT]] ], !dbg [[DBG12]]
3740
; CHECK-NEXT: ret i32 [[TMP1]], !dbg [[DBG12]]
3841
;
3942
%cond = icmp slt i32 %a, 10
@@ -78,3 +81,22 @@ Merge:
7881
!19 = distinct !DILexicalBlock(scope: !7, file: !1, line: 8, column: 7)
7982
!26 = !DILocation(line: 13, column: 3, scope: !7)
8083

84+
;.
85+
; CHECK: attributes #[[ATTR0:[0-9]+]] = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
86+
;.
87+
; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C99, file: [[META1:![0-9]+]], isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: [[META2:![0-9]+]], splitDebugInlining: false, nameTableKind: None)
88+
; CHECK: [[META1]] = !DIFile(filename: "test.c", directory: {{.*}})
89+
; CHECK: [[META2]] = !{}
90+
; CHECK: [[META3:![0-9]+]] = !{i32 7, !"Dwarf Version", i32 4}
91+
; CHECK: [[META4:![0-9]+]] = !{i32 2, !"Debug Info Version", i32 3}
92+
; CHECK: [[META5:![0-9]+]] = !{i32 1, !"wchar_size", i32 4}
93+
; CHECK: [[META6:![0-9]+]] = !{!""}
94+
; CHECK: [[META7:![0-9]+]] = distinct !DISubprogram(name: "foo", scope: [[META1]], file: [[META1]], line: 3, type: [[META8:![0-9]+]], scopeLine: 3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: [[META0]], retainedNodes: [[META2]])
95+
; CHECK: [[META8]] = !DISubroutineType(types: [[META9:![0-9]+]])
96+
; CHECK: [[META9]] = !{[[META10:![0-9]+]], [[META11:![0-9]+]], [[META11]]}
97+
; CHECK: [[META10]] = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
98+
; CHECK: [[META11]] = !DIBasicType(name: "long int", size: 64, encoding: DW_ATE_signed)
99+
; CHECK: [[DBG12]] = !DILocation(line: 13, column: 3, scope: [[META7]])
100+
; CHECK: [[META13]] = !DILocalVariable(name: "bar", arg: 1, scope: [[META7]], file: [[META1]], line: 3, type: [[META11]])
101+
; CHECK: [[DBG14]] = !DILocation(line: 0, scope: [[META7]])
102+
;.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
; RUN: opt < %s -S -passes=jump-threading | FileCheck %s
2+
3+
; Test that JumpThreading's `simplifyPartiallyRedundantLoad` propagates
4+
; the debug location to the `bitcast` from the LoadInst it replaces (`%b`).
5+
6+
declare void @f1(...)
7+
8+
define void @test8(ptr %0, ptr %1, ptr %2) !dbg !5 {
9+
; CHECK: @test8
10+
; CHECK: [[TMP4:%.*]] = bitcast float [[A:%.*]] to i32, !dbg [[DBG9:![0-9]+]]
11+
; CHECK: [[DBG9]] = !DILocation(line: 2,
12+
;
13+
%a = load float, ptr %0, align 4, !dbg !8
14+
%b = load i32, ptr %0, align 4, !dbg !9
15+
store float %a, ptr %1, align 4, !dbg !10
16+
%c = icmp eq i32 %b, 8, !dbg !11
17+
br i1 %c, label %ret1, label %ret2, !dbg !12
18+
19+
ret1: ; preds = %3
20+
ret void, !dbg !13
21+
22+
ret2: ; preds = %3
23+
%xxx = tail call i32 (...) @f1() #0, !dbg !14
24+
ret void, !dbg !15
25+
}
26+
27+
!llvm.dbg.cu = !{!0}
28+
!llvm.debugify = !{!2, !3}
29+
!llvm.module.flags = !{!4}
30+
31+
!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
32+
!1 = !DIFile(filename: "test.ll", directory: "/")
33+
!2 = !{i32 8}
34+
!3 = !{i32 0}
35+
!4 = !{i32 2, !"Debug Info Version", i32 3}
36+
!5 = distinct !DISubprogram(name: "test8", linkageName: "test8", scope: null, file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
37+
!6 = !DISubroutineType(types: !7)
38+
!7 = !{}
39+
!8 = !DILocation(line: 1, column: 1, scope: !5)
40+
!9 = !DILocation(line: 2, column: 1, scope: !5)
41+
!10 = !DILocation(line: 3, column: 1, scope: !5)
42+
!11 = !DILocation(line: 4, column: 1, scope: !5)
43+
!12 = !DILocation(line: 5, column: 1, scope: !5)
44+
!13 = !DILocation(line: 6, column: 1, scope: !5)
45+
!14 = !DILocation(line: 7, column: 1, scope: !5)
46+
!15 = !DILocation(line: 8, column: 1, scope: !5)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
; RUN: opt < %s -S -passes=jump-threading | FileCheck %s
2+
3+
; Test the debug location update of the newly created PHINode
4+
; which replaces the select instruction in .exit block.
5+
6+
define i32 @unfold3(i32 %u, i32 %v, i32 %w, i32 %x, i32 %y, i32 %z, i32 %j) !dbg !5 {
7+
; CHECK: .exit.thread4:
8+
; CHECK-NEXT: [[TMP0:%.*]] = phi i32 {{.*}}, !dbg [[DBG29:![0-9]+]]
9+
; CHECK-NEXT: ret i32 [[TMP0]], !dbg [[DBG30:![0-9]+]]
10+
;
11+
; CHECK: [[DBG29]] = !DILocation(line: 13,
12+
;
13+
entry:
14+
%add3 = add nsw i32 %j, 2, !dbg !19
15+
%cmp.i = icmp slt i32 %u, %v, !dbg !20
16+
br i1 %cmp.i, label %.exit, label %cond.false.i, !dbg !21
17+
18+
cond.false.i: ; preds = %entry
19+
%cmp4.i = icmp sgt i32 %u, %v, !dbg !22
20+
br i1 %cmp4.i, label %.exit, label %cond.false.6.i, !dbg !23
21+
22+
cond.false.6.i: ; preds = %cond.false.i
23+
%cmp8.i = icmp slt i32 %w, %x, !dbg !24
24+
br i1 %cmp8.i, label %.exit, label %cond.false.10.i, !dbg !25
25+
26+
cond.false.10.i: ; preds = %cond.false.6.i
27+
%cmp13.i = icmp sgt i32 %w, %x, !dbg !26
28+
br i1 %cmp13.i, label %.exit, label %cond.false.15.i, !dbg !27
29+
30+
cond.false.15.i: ; preds = %cond.false.10.i
31+
%phitmp = icmp sge i32 %y, %z, !dbg !28
32+
br label %.exit, !dbg !29
33+
34+
.exit: ; preds = %cond.false.15.i, %cond.false.10.i, %cond.false.6.i, %cond.false.i, %entry
35+
%cond23.i = phi i1 [ false, %entry ], [ true, %cond.false.i ], [ false, %cond.false.6.i ], [ %phitmp, %cond.false.15.i ], [ true, %cond.false.10.i ], !dbg !30
36+
%j.add3 = select i1 %cond23.i, i32 %j, i32 %add3, !dbg !31
37+
ret i32 %j.add3, !dbg !32
38+
}
39+
40+
!llvm.dbg.cu = !{!0}
41+
!llvm.debugify = !{!2, !3}
42+
!llvm.module.flags = !{!4}
43+
44+
!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
45+
!1 = !DIFile(filename: "preserving-debugloc-trytofoldselect.ll", directory: "/")
46+
!2 = !{i32 14}
47+
!3 = !{i32 8}
48+
!4 = !{i32 2, !"Debug Info Version", i32 3}
49+
!5 = distinct !DISubprogram(name: "unfold3", linkageName: "unfold3", scope: null, file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
50+
!6 = !DISubroutineType(types: !7)
51+
!7 = !{}
52+
!8 = !{!9, !11, !13, !14, !15, !16, !17, !18}
53+
!9 = !DILocalVariable(name: "1", scope: !5, file: !1, line: 1, type: !10)
54+
!10 = !DIBasicType(name: "ty32", size: 32, encoding: DW_ATE_unsigned)
55+
!11 = !DILocalVariable(name: "2", scope: !5, file: !1, line: 2, type: !12)
56+
!12 = !DIBasicType(name: "ty8", size: 8, encoding: DW_ATE_unsigned)
57+
!13 = !DILocalVariable(name: "3", scope: !5, file: !1, line: 4, type: !12)
58+
!14 = !DILocalVariable(name: "4", scope: !5, file: !1, line: 6, type: !12)
59+
!15 = !DILocalVariable(name: "5", scope: !5, file: !1, line: 8, type: !12)
60+
!16 = !DILocalVariable(name: "6", scope: !5, file: !1, line: 10, type: !12)
61+
!17 = !DILocalVariable(name: "7", scope: !5, file: !1, line: 12, type: !12)
62+
!18 = !DILocalVariable(name: "8", scope: !5, file: !1, line: 13, type: !10)
63+
!19 = !DILocation(line: 1, column: 1, scope: !5)
64+
!20 = !DILocation(line: 2, column: 1, scope: !5)
65+
!21 = !DILocation(line: 3, column: 1, scope: !5)
66+
!22 = !DILocation(line: 4, column: 1, scope: !5)
67+
!23 = !DILocation(line: 5, column: 1, scope: !5)
68+
!24 = !DILocation(line: 6, column: 1, scope: !5)
69+
!25 = !DILocation(line: 7, column: 1, scope: !5)
70+
!26 = !DILocation(line: 8, column: 1, scope: !5)
71+
!27 = !DILocation(line: 9, column: 1, scope: !5)
72+
!28 = !DILocation(line: 10, column: 1, scope: !5)
73+
!29 = !DILocation(line: 11, column: 1, scope: !5)
74+
!30 = !DILocation(line: 12, column: 1, scope: !5)
75+
!31 = !DILocation(line: 13, column: 1, scope: !5)
76+
!32 = !DILocation(line: 14, column: 1, scope: !5)

0 commit comments

Comments
 (0)