Skip to content

Commit a487616

Browse files
authored
[DebugInfo][GVNSink] Merging debug locations of sinked instructions (llvm#92859)
Fix llvm#85069 .
1 parent 6af4118 commit a487616

File tree

3 files changed

+120
-3
lines changed

3 files changed

+120
-3
lines changed

llvm/lib/Transforms/Scalar/GVNSink.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,8 +921,10 @@ void GVNSink::sinkLastInstruction(ArrayRef<BasicBlock *> Blocks,
921921
}
922922

923923
for (auto *I : Insts)
924-
if (I != I0)
924+
if (I != I0) {
925925
I->replaceAllUsesWith(I0);
926+
I0->applyMergedLocation(I0->getDebugLoc(), I->getDebugLoc());
927+
}
926928
foldPointlessPHINodes(BBEnd);
927929

928930
// Finally nuke all instructions apart from the common instruction.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
; RUN: opt < %s -passes=gvn-sink -S | FileCheck %s
2+
3+
; Test that GVNSink correctly merges the debug locations of sinked instruction
4+
5+
define zeroext i1 @test18(i32 %flag, i32 %blksA, i32 %blksB, i32 %nblks) !dbg !5 {
6+
; CHECK: if.end:
7+
; CHECK-NEXT: [[CMP2_SINK:%.*]] = phi i1 [ %cmp2, %if.then2 ], [ %cmp, %if.then ], [ %cmp3, %if.then3 ]
8+
; CHECK-NEXT: [[FROMBOOL4:%.*]] = zext i1 [[CMP2_SINK]] to i8, !dbg [[DBG17:![0-9]+]]
9+
;
10+
entry:
11+
switch i32 %flag, label %if.then3 [
12+
i32 0, label %if.then
13+
i32 1, label %if.then2
14+
], !dbg !8
15+
16+
if.then: ; preds = %entry
17+
%cmp = icmp uge i32 %blksA, %nblks, !dbg !9
18+
%frombool1 = zext i1 %cmp to i8, !dbg !10
19+
br label %if.end, !dbg !11
20+
21+
if.then2: ; preds = %entry
22+
%add = add i32 %nblks, %blksB, !dbg !12
23+
%cmp2 = icmp ule i32 %add, %blksA, !dbg !13
24+
%frombool3 = zext i1 %cmp2 to i8, !dbg !14
25+
br label %if.end, !dbg !15
26+
27+
if.then3: ; preds = %entry
28+
%add2 = add i32 %nblks, %blksA, !dbg !16
29+
%cmp3 = icmp ule i32 %add2, %blksA, !dbg !17
30+
%frombool4 = zext i1 %cmp3 to i8, !dbg !18
31+
br label %if.end, !dbg !19
32+
33+
if.end: ; preds = %if.then3, %if.then2, %if.then
34+
%obeys.0 = phi i8 [ %frombool1, %if.then ], [ %frombool3, %if.then2 ], [ %frombool4, %if.then3 ], !dbg !20
35+
%tobool4 = icmp ne i8 %obeys.0, 0, !dbg !21
36+
ret i1 %tobool4, !dbg !22
37+
}
38+
39+
define zeroext i1 @test_pr30244(i1 zeroext %flag, i1 zeroext %flag2, i32 %blksA, i32 %blksB, i32 %nblks) !dbg !23 {
40+
; CHECK: if.end.gvnsink.split:
41+
; CHECK-NEXT: [[CMP2_SINK:%.*]] = phi i1 [ %cmp2, %if.then2 ], [ %cmp, %if.then ]
42+
; CHECK-NEXT: [[FROMBOOL1:%.*]] = zext i1 [[CMP2_SINK]] to i8, !dbg [[DBG29:![0-9]+]]
43+
;
44+
entry:
45+
%p = alloca i8, align 1, !dbg !24
46+
br i1 %flag, label %if.then, label %if.else, !dbg !25
47+
48+
if.then: ; preds = %entry
49+
%cmp = icmp uge i32 %blksA, %nblks, !dbg !26
50+
%frombool1 = zext i1 %cmp to i8, !dbg !27
51+
store i8 %frombool1, ptr %p, align 1, !dbg !28
52+
br label %if.end, !dbg !29
53+
54+
if.else: ; preds = %entry
55+
br i1 %flag2, label %if.then2, label %if.end, !dbg !30
56+
57+
if.then2: ; preds = %if.else
58+
%add = add i32 %nblks, %blksB, !dbg !31
59+
%cmp2 = icmp ule i32 %add, %blksA, !dbg !32
60+
%frombool3 = zext i1 %cmp2 to i8, !dbg !33
61+
store i8 %frombool3, ptr %p, align 1, !dbg !34
62+
br label %if.end, !dbg !35
63+
64+
if.end: ; preds = %if.then2, %if.else, %if.then
65+
ret i1 true, !dbg !36
66+
}
67+
68+
!llvm.dbg.cu = !{!0}
69+
!llvm.debugify = !{!2, !3}
70+
!llvm.module.flags = !{!4}
71+
72+
!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
73+
!1 = !DIFile(filename: "sink-common-code.ll", directory: "/")
74+
!2 = !{i32 28}
75+
!3 = !{i32 0}
76+
!4 = !{i32 2, !"Debug Info Version", i32 3}
77+
!5 = distinct !DISubprogram(name: "test18", linkageName: "test18", scope: null, file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
78+
!6 = !DISubroutineType(types: !7)
79+
!7 = !{}
80+
!8 = !DILocation(line: 1, column: 1, scope: !5)
81+
!9 = !DILocation(line: 2, column: 1, scope: !5)
82+
!10 = !DILocation(line: 3, column: 1, scope: !5)
83+
!11 = !DILocation(line: 4, column: 1, scope: !5)
84+
!12 = !DILocation(line: 5, column: 1, scope: !5)
85+
!13 = !DILocation(line: 6, column: 1, scope: !5)
86+
!14 = !DILocation(line: 7, column: 1, scope: !5)
87+
!15 = !DILocation(line: 8, column: 1, scope: !5)
88+
!16 = !DILocation(line: 9, column: 1, scope: !5)
89+
!17 = !DILocation(line: 10, column: 1, scope: !5)
90+
!18 = !DILocation(line: 11, column: 1, scope: !5)
91+
!19 = !DILocation(line: 12, column: 1, scope: !5)
92+
!20 = !DILocation(line: 13, column: 1, scope: !5)
93+
!21 = !DILocation(line: 14, column: 1, scope: !5)
94+
!22 = !DILocation(line: 15, column: 1, scope: !5)
95+
!23 = distinct !DISubprogram(name: "test_pr30244", linkageName: "test_pr30244", scope: null, file: !1, line: 16, type: !6, scopeLine: 16, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
96+
!24 = !DILocation(line: 16, column: 1, scope: !23)
97+
!25 = !DILocation(line: 17, column: 1, scope: !23)
98+
!26 = !DILocation(line: 18, column: 1, scope: !23)
99+
!27 = !DILocation(line: 19, column: 1, scope: !23)
100+
!28 = !DILocation(line: 20, column: 1, scope: !23)
101+
!29 = !DILocation(line: 21, column: 1, scope: !23)
102+
!30 = !DILocation(line: 22, column: 1, scope: !23)
103+
!31 = !DILocation(line: 23, column: 1, scope: !23)
104+
!32 = !DILocation(line: 24, column: 1, scope: !23)
105+
!33 = !DILocation(line: 25, column: 1, scope: !23)
106+
!34 = !DILocation(line: 26, column: 1, scope: !23)
107+
!35 = !DILocation(line: 27, column: 1, scope: !23)
108+
!36 = !DILocation(line: 28, column: 1, scope: !23)
109+
;.
110+
; CHECK: [[DBG17]] = !DILocation(line: 0
111+
; CHECK: [[DBG29]] = !DILocation(line: 0
112+
;.

llvm/test/Transforms/GVNSink/sink-ignore-dbg-intrinsics.ll

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
; RUN: opt < %s -passes=gvn-sink -S | FileCheck %s
22

33
; Test that GVNSink correctly performs the sink optimization in the presence of debug information
4+
; Test that GVNSink correctly merges the debug locations of sinked instruction, eg, propagating
5+
; the merged debug location of `%add` and `%add1` to the sinked add instruction.
46

57
; Function Attrs: noinline nounwind uwtable
68
define dso_local i32 @fun(i32 noundef %a, i32 noundef %b) #0 !dbg !10 {
79
; CHECK-LABEL: define dso_local i32 @fun(
810
; CHECK-SAME: i32 noundef [[A:%.*]], i32 noundef [[B:%.*]])
911
; CHECK: if.end:
1012
; CHECK: [[B_SINK:%.*]] = phi i32 [ [[B]], %if.else ], [ [[A]], %if.then ]
11-
; CHECK: [[ADD1:%.*]] = add nsw i32 [[B_SINK]], 1
12-
; CHECK: [[XOR2:%.*]] = xor i32 [[ADD1]], 1
13+
; CHECK: [[ADD1:%.*]] = add nsw i32 [[B_SINK]], 1, !dbg [[DBG:![0-9]+]]
14+
; CHECK: [[XOR2:%.*]] = xor i32 [[ADD1]], 1, !dbg [[DBG:![0-9]+]]
15+
; CHECK: [[DBG]] = !DILocation(line: 0,
1316
;
1417
entry:
1518
tail call void @llvm.dbg.value(metadata i32 %a, metadata !15, metadata !DIExpression()), !dbg !16

0 commit comments

Comments
 (0)