Skip to content

Commit 5971b41

Browse files
authored
[DebugInfo][LICM] Salvage dbg_values for the dead instructions to erase (#138796)
fix #138684 .
1 parent 86ceed7 commit 5971b41

File tree

5 files changed

+255
-3
lines changed

5 files changed

+255
-3
lines changed

llvm/lib/Transforms/Scalar/LICM.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2538,6 +2538,7 @@ static bool hoistGEP(Instruction &I, Loop &L, ICFLoopSafetyInfo &SafetyInfo,
25382538
IsInBounds);
25392539
GEP->replaceAllUsesWith(NewGEP);
25402540
eraseInstruction(*GEP, SafetyInfo, MSSAU);
2541+
salvageDebugInfo(*Src);
25412542
eraseInstruction(*Src, SafetyInfo, MSSAU);
25422543
return true;
25432544
}
@@ -2592,7 +2593,10 @@ static bool hoistAdd(ICmpInst::Predicate Pred, Value *VariantLHS,
25922593
ICmp.setPredicate(Pred);
25932594
ICmp.setOperand(0, VariantOp);
25942595
ICmp.setOperand(1, NewCmpOp);
2595-
eraseInstruction(cast<Instruction>(*VariantLHS), SafetyInfo, MSSAU);
2596+
2597+
Instruction &DeadI = cast<Instruction>(*VariantLHS);
2598+
salvageDebugInfo(DeadI);
2599+
eraseInstruction(DeadI, SafetyInfo, MSSAU);
25962600
return true;
25972601
}
25982602

@@ -2670,7 +2674,10 @@ static bool hoistSub(ICmpInst::Predicate Pred, Value *VariantLHS,
26702674
ICmp.setPredicate(Pred);
26712675
ICmp.setOperand(0, VariantOp);
26722676
ICmp.setOperand(1, NewCmpOp);
2673-
eraseInstruction(cast<Instruction>(*VariantLHS), SafetyInfo, MSSAU);
2677+
2678+
Instruction &DeadI = cast<Instruction>(*VariantLHS);
2679+
salvageDebugInfo(DeadI);
2680+
eraseInstruction(DeadI, SafetyInfo, MSSAU);
26742681
return true;
26752682
}
26762683

@@ -2877,8 +2884,10 @@ static bool hoistBOAssociation(Instruction &I, Loop &L,
28772884

28782885
// (LV op C1) might not be erased if it has more uses than the one we just
28792886
// replaced.
2880-
if (BO0->use_empty())
2887+
if (BO0->use_empty()) {
2888+
salvageDebugInfo(*BO0);
28812889
eraseInstruction(*BO0, SafetyInfo, MSSAU);
2890+
}
28822891

28832892
return true;
28842893
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
; RUN: opt -S -passes=licm %s | FileCheck %s
2+
3+
; Check that hoistAdd() in LICM salvages the dbg_value for the hoisted add
4+
; instruction.
5+
6+
define i32 @hoist_add(ptr %p, ptr %x_p, ptr %length_p) !dbg !5 {
7+
; CHECK-LABEL: define i32 @hoist_add(
8+
; CHECK-LABEL: loop:
9+
; CHECK: #dbg_value(!DIArgList(i32 [[X:%.*]], i32 [[IV:%.*]]), [[META9:![0-9]+]], !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_plus, DW_OP_stack_value), [[META16:![0-9]+]])
10+
;
11+
entry:
12+
%x = load i32, ptr %x_p, align 4, !dbg !20, !range !21
13+
%length = load i32, ptr %length_p, align 4, !dbg !22, !range !21
14+
br label %loop, !dbg !23
15+
16+
loop: ; preds = %backedge, %entry
17+
%iv = phi i32 [ 0, %entry ], [ %iv.next, %backedge ], !dbg !24
18+
%arith = add nsw i32 %x, %iv, !dbg !25
19+
#dbg_value(i32 %arith, !13, !DIExpression(), !25)
20+
%x_check = icmp slt i32 %arith, 4, !dbg !26
21+
br i1 %x_check, label %out_of_bounds, label %backedge, !dbg !27
22+
23+
backedge: ; preds = %loop
24+
%el.ptr = getelementptr i32, ptr %p, i32 %iv, !dbg !28
25+
store i32 1, ptr %el.ptr, align 4, !dbg !29
26+
%iv.next = add nuw nsw i32 %iv, 4, !dbg !30
27+
%loop_cond = icmp slt i32 %iv.next, %length, !dbg !31
28+
br i1 %loop_cond, label %loop, label %exit, !dbg !32
29+
30+
exit: ; preds = %backedge
31+
ret i32 %iv.next, !dbg !33
32+
33+
out_of_bounds: ; preds = %loop
34+
ret i32 -1, !dbg !34
35+
}
36+
37+
!llvm.dbg.cu = !{!0}
38+
!llvm.debugify = !{!2, !3}
39+
!llvm.module.flags = !{!4}
40+
41+
!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
42+
!1 = !DIFile(filename: "salvage-hoisted-add", directory: "/")
43+
!2 = !{i32 14}
44+
!3 = !{i32 8}
45+
!4 = !{i32 2, !"Debug Info Version", i32 3}
46+
!5 = distinct !DISubprogram(name: "hoist_add", linkageName: "hoist_add", scope: null, file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
47+
!6 = !DISubroutineType(types: !7)
48+
!7 = !{}
49+
!8 = !{!13}
50+
!10 = !DIBasicType(name: "ty32", size: 32, encoding: DW_ATE_unsigned)
51+
!13 = !DILocalVariable(name: "4", scope: !5, file: !1, line: 5, type: !10)
52+
!20 = !DILocation(line: 1, column: 1, scope: !5)
53+
!21 = !{i32 0, i32 -2147483648}
54+
!22 = !DILocation(line: 2, column: 1, scope: !5)
55+
!23 = !DILocation(line: 3, column: 1, scope: !5)
56+
!24 = !DILocation(line: 4, column: 1, scope: !5)
57+
!25 = !DILocation(line: 5, column: 1, scope: !5)
58+
!26 = !DILocation(line: 6, column: 1, scope: !5)
59+
!27 = !DILocation(line: 7, column: 1, scope: !5)
60+
!28 = !DILocation(line: 8, column: 1, scope: !5)
61+
!29 = !DILocation(line: 9, column: 1, scope: !5)
62+
!30 = !DILocation(line: 10, column: 1, scope: !5)
63+
!31 = !DILocation(line: 11, column: 1, scope: !5)
64+
!32 = !DILocation(line: 12, column: 1, scope: !5)
65+
!33 = !DILocation(line: 13, column: 1, scope: !5)
66+
!34 = !DILocation(line: 14, column: 1, scope: !5)
67+
;.
68+
; CHECK: [[META9]] = !DILocalVariable(name: "4",
69+
; CHECK: [[META16]] = !DILocation(line: 5, column: 1,
70+
;.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
; RUN: opt -S -passes=licm %s | FileCheck %s
2+
3+
; Check that hoistBOAssociation() in LICM salvages the dbg_value for the
4+
; hoisted binary operation.
5+
6+
define void @hoist_binop(i64 %c1, i64 %c2) !dbg !5 {
7+
; CHECK-LABEL: define void @hoist_binop(
8+
; CHECK-LABEL: loop:
9+
; CHECK: #dbg_value(!DIArgList(i64 [[INDEX:%.*]], i64 [[C1:%.*]]), [[META9:![0-9]+]], !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_plus, DW_OP_stack_value), [[META13:![0-9]+]])
10+
;
11+
entry:
12+
br label %loop, !dbg !13
13+
14+
loop: ; preds = %loop, %entry
15+
%index = phi i64 [ 0, %entry ], [ %index.next, %loop ], !dbg !14
16+
%step.add = add i64 %index, %c1, !dbg !15
17+
#dbg_value(i64 %step.add, !11, !DIExpression(), !15)
18+
%index.next = add i64 %step.add, %c2, !dbg !16
19+
br label %loop, !dbg !17
20+
}
21+
22+
!llvm.dbg.cu = !{!0}
23+
!llvm.debugify = !{!2, !3}
24+
!llvm.module.flags = !{!4}
25+
26+
!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
27+
!1 = !DIFile(filename: "salvage-hoist-binop.ll", directory: "/")
28+
!2 = !{i32 5}
29+
!3 = !{i32 3}
30+
!4 = !{i32 2, !"Debug Info Version", i32 3}
31+
!5 = distinct !DISubprogram(name: "hoist_binop", linkageName: "hoist_binop", scope: null, file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
32+
!6 = !DISubroutineType(types: !7)
33+
!7 = !{}
34+
!8 = !{!11}
35+
!10 = !DIBasicType(name: "ty64", size: 64, encoding: DW_ATE_unsigned)
36+
!11 = !DILocalVariable(name: "2", scope: !5, file: !1, line: 3, type: !10)
37+
!13 = !DILocation(line: 1, column: 1, scope: !5)
38+
!14 = !DILocation(line: 2, column: 1, scope: !5)
39+
!15 = !DILocation(line: 3, column: 1, scope: !5)
40+
!16 = !DILocation(line: 4, column: 1, scope: !5)
41+
!17 = !DILocation(line: 5, column: 1, scope: !5)
42+
;.
43+
; CHECK: [[META9]] = !DILocalVariable(name: "2",
44+
; CHECK: [[META13]] = !DILocation(line: 3, column: 1,
45+
;.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
; RUN: opt -S -passes=licm %s | FileCheck %s
2+
3+
; Check that hoistGEP() in LICM salvages the dbg_value for the hoisted
4+
; getelementptr instruction.
5+
6+
define void @hoist_gep(ptr %ptr, i1 %c, i32 %arg) !dbg !5 {
7+
; CHECK-LABEL: define void @hoist_gep(
8+
; CHECK-LABEL: loop:
9+
; CHECK: #dbg_value(!DIArgList(ptr [[PTR:%.*]], i64 [[VAL_EXT:%.*]]), [[META9:![0-9]+]], !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_plus, DW_OP_stack_value), [[META15:![0-9]+]])
10+
;
11+
entry:
12+
%arg.ext = zext i32 %arg to i64, !dbg !16
13+
br label %loop, !dbg !17
14+
15+
loop: ; preds = %loop, %entry
16+
%val = call i32 @get.i32(), !dbg !18
17+
%val.ext = zext i32 %val to i64, !dbg !19
18+
%ptr2 = getelementptr inbounds i8, ptr %ptr, i64 %val.ext, !dbg !20
19+
#dbg_value(ptr %ptr2, !14, !DIExpression(), !20)
20+
%ptr3 = getelementptr i8, ptr %ptr2, i64 %arg.ext, !dbg !21
21+
call void @use(ptr %ptr3), !dbg !22
22+
br i1 %c, label %loop, label %exit, !dbg !23
23+
24+
exit: ; preds = %loop
25+
ret void, !dbg !24
26+
}
27+
28+
declare i32 @get.i32()
29+
declare void @use(ptr)
30+
31+
!llvm.dbg.cu = !{!0}
32+
!llvm.debugify = !{!2, !3}
33+
!llvm.module.flags = !{!4}
34+
35+
!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
36+
!1 = !DIFile(filename: "salvage-hoisted-gep.ll", directory: "/")
37+
!2 = !{i32 9}
38+
!3 = !{i32 5}
39+
!4 = !{i32 2, !"Debug Info Version", i32 3}
40+
!5 = distinct !DISubprogram(name: "hoist_gep", linkageName: "hoist_gep", scope: null, file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
41+
!6 = !DISubroutineType(types: !7)
42+
!7 = !{}
43+
!8 = !{!14}
44+
!10 = !DIBasicType(name: "ty64", size: 64, encoding: DW_ATE_unsigned)
45+
!14 = !DILocalVariable(name: "4", scope: !5, file: !1, line: 5, type: !10)
46+
!16 = !DILocation(line: 1, column: 1, scope: !5)
47+
!17 = !DILocation(line: 2, column: 1, scope: !5)
48+
!18 = !DILocation(line: 3, column: 1, scope: !5)
49+
!19 = !DILocation(line: 4, column: 1, scope: !5)
50+
!20 = !DILocation(line: 5, column: 1, scope: !5)
51+
!21 = !DILocation(line: 6, column: 1, scope: !5)
52+
!22 = !DILocation(line: 7, column: 1, scope: !5)
53+
!23 = !DILocation(line: 8, column: 1, scope: !5)
54+
!24 = !DILocation(line: 9, column: 1, scope: !5)
55+
;.
56+
; CHECK: [[META9]] = !DILocalVariable(name: "4",
57+
; CHECK: [[META15]] = !DILocation(line: 5,
58+
;.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
; RUN: opt -S -passes=licm %s | FileCheck %s
2+
3+
; Check that hoistSub() in LICM salvages the dbg_value for the hoisted sub
4+
; instruction.
5+
6+
define i32 @hoist_sub(ptr %p, ptr %x_p, ptr %length_p) !dbg !5 {
7+
; CHECK-LABEL: define i32 @hoist_sub(
8+
; CHECK-LABEL: loop:
9+
; CHECK: #dbg_value(!DIArgList(i32 [[X:%.*]], i32 [[IV:%.*]]), [[META9:![0-9]+]], !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_minus, DW_OP_stack_value), [[META16:![0-9]+]])
10+
;
11+
entry:
12+
%x = load i32, ptr %x_p, align 4, !dbg !20, !range !21
13+
%length = load i32, ptr %length_p, align 4, !dbg !22, !range !21
14+
br label %loop, !dbg !23
15+
16+
loop: ; preds = %backedge, %entry
17+
%iv = phi i32 [ 0, %entry ], [ %iv.next, %backedge ], !dbg !24
18+
%arith = sub nsw i32 %x, %iv, !dbg !25
19+
#dbg_value(i32 %arith, !13, !DIExpression(), !25)
20+
%x_check = icmp slt i32 %arith, 4, !dbg !26
21+
br i1 %x_check, label %out_of_bounds, label %backedge, !dbg !27
22+
23+
backedge: ; preds = %loop
24+
%el.ptr = getelementptr i32, ptr %p, i32 %iv, !dbg !28
25+
store i32 1, ptr %el.ptr, align 4, !dbg !29
26+
%iv.next = add nuw nsw i32 %iv, 4, !dbg !30
27+
%loop_cond = icmp slt i32 %iv.next, %length, !dbg !31
28+
br i1 %loop_cond, label %loop, label %exit, !dbg !32
29+
30+
exit: ; preds = %backedge
31+
ret i32 %iv.next, !dbg !33
32+
33+
out_of_bounds: ; preds = %loop
34+
ret i32 -1, !dbg !34
35+
}
36+
37+
!llvm.dbg.cu = !{!0}
38+
!llvm.debugify = !{!2, !3}
39+
!llvm.module.flags = !{!4}
40+
41+
!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
42+
!1 = !DIFile(filename: "salvage-hoisted-sub.ll", directory: "/")
43+
!2 = !{i32 14}
44+
!3 = !{i32 8}
45+
!4 = !{i32 2, !"Debug Info Version", i32 3}
46+
!5 = distinct !DISubprogram(name: "hoist_sub", linkageName: "hoist_sub", scope: null, file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
47+
!6 = !DISubroutineType(types: !7)
48+
!7 = !{}
49+
!8 = !{!13}
50+
!10 = !DIBasicType(name: "ty32", size: 32, encoding: DW_ATE_unsigned)
51+
!13 = !DILocalVariable(name: "4", scope: !5, file: !1, line: 5, type: !10)
52+
!20 = !DILocation(line: 1, column: 1, scope: !5)
53+
!21 = !{i32 0, i32 -2147483648}
54+
!22 = !DILocation(line: 2, column: 1, scope: !5)
55+
!23 = !DILocation(line: 3, column: 1, scope: !5)
56+
!24 = !DILocation(line: 4, column: 1, scope: !5)
57+
!25 = !DILocation(line: 5, column: 1, scope: !5)
58+
!26 = !DILocation(line: 6, column: 1, scope: !5)
59+
!27 = !DILocation(line: 7, column: 1, scope: !5)
60+
!28 = !DILocation(line: 8, column: 1, scope: !5)
61+
!29 = !DILocation(line: 9, column: 1, scope: !5)
62+
!30 = !DILocation(line: 10, column: 1, scope: !5)
63+
!31 = !DILocation(line: 11, column: 1, scope: !5)
64+
!32 = !DILocation(line: 12, column: 1, scope: !5)
65+
!33 = !DILocation(line: 13, column: 1, scope: !5)
66+
!34 = !DILocation(line: 14, column: 1, scope: !5)
67+
;.
68+
; CHECK: [[META9]] = !DILocalVariable(name: "4",
69+
; CHECK: [[META16]] = !DILocation(line: 5, column: 1,
70+
;.

0 commit comments

Comments
 (0)