Skip to content

Commit bf69217

Browse files
[instcombine] Sunk instructions with invalid source location.
When the 'int Four = Two;' is sunk into the 'case 0:' block, the debug value for 'Three' is set incorrectly to 'poison'. Reviewed By: aprantl Differential Revision: https://reviews.llvm.org/D158171
1 parent 6f31908 commit bf69217

File tree

2 files changed

+103
-4
lines changed

2 files changed

+103
-4
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3857,10 +3857,18 @@ bool InstCombinerImpl::tryToSinkInstruction(Instruction *I,
38573857
// here, but that computation has been sunk.
38583858
SmallVector<DbgVariableIntrinsic *, 2> DbgUsers;
38593859
findDbgUsers(DbgUsers, I);
3860-
// Process the sinking DbgUsers in reverse order, as we only want to clone the
3861-
// last appearing debug intrinsic for each given variable.
3860+
3861+
// For all debug values in the destination block, the sunk instruction
3862+
// will still be available, so they do not need to be dropped.
3863+
SmallVector<DbgVariableIntrinsic *, 2> DbgUsersToSalvage;
3864+
for (auto &DbgUser : DbgUsers)
3865+
if (DbgUser->getParent() != DestBlock)
3866+
DbgUsersToSalvage.push_back(DbgUser);
3867+
3868+
// Process the sinking DbgUsersToSalvage in reverse order, as we only want
3869+
// to clone the last appearing debug intrinsic for each given variable.
38623870
SmallVector<DbgVariableIntrinsic *, 2> DbgUsersToSink;
3863-
for (DbgVariableIntrinsic *DVI : DbgUsers)
3871+
for (DbgVariableIntrinsic *DVI : DbgUsersToSalvage)
38643872
if (DVI->getParent() == SrcBlock)
38653873
DbgUsersToSink.push_back(DVI);
38663874
llvm::sort(DbgUsersToSink,
@@ -3896,7 +3904,7 @@ bool InstCombinerImpl::tryToSinkInstruction(Instruction *I,
38963904

38973905
// Perform salvaging without the clones, then sink the clones.
38983906
if (!DIIClones.empty()) {
3899-
salvageDebugInfoForDbgValues(*I, DbgUsers);
3907+
salvageDebugInfoForDbgValues(*I, DbgUsersToSalvage);
39003908
// The clones are in reverse order of original appearance, reverse again to
39013909
// maintain the original order.
39023910
for (auto &DIIClone : llvm::reverse(DIIClones)) {
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
; RUN: opt -passes=instcombine -S -o - < %s | FileCheck %s
2+
3+
; When the 'int Four = Two;' is sunk into the 'case 0:' block,
4+
; the debug value for 'Three' is set incorrectly to 'poison'.
5+
6+
; 1 int One = 0;
7+
; 2 int Two = 0;
8+
; 3 int test() {
9+
; 4 int Three = 0;
10+
; 5 int Four = Two;
11+
; 6 switch (One) {
12+
; 7 case 0:
13+
; 8 Three = Four;
14+
; 9 break;
15+
; 10 case 2:
16+
; 11 Three = 4;
17+
; 12 break;
18+
; 13 }
19+
; 14 return Three;
20+
; 15 }
21+
22+
; CHECK-LABEL: sw.bb:
23+
; CHECK: %[[REG:[0-9]+]] = load i32, ptr @"?Two{{.*}}
24+
; CHECK: call void @llvm.dbg.value(metadata i32 %[[REG]], metadata ![[DBG1:[0-9]+]], {{.*}}
25+
; CHECK: call void @llvm.dbg.value(metadata i32 %[[REG]], metadata ![[DBG2:[0-9]+]], {{.*}}
26+
; CHECK-DAG: ![[DBG1]] = !DILocalVariable(name: "Four"{{.*}})
27+
; CHECK-DAG: ![[DBG2]] = !DILocalVariable(name: "Three"{{.*}})
28+
29+
@"?One@@3HA" = dso_local global i32 0, align 4, !dbg !0
30+
@"?Two@@3HA" = dso_local global i32 0, align 4, !dbg !5
31+
32+
define dso_local noundef i32 @"?test@@YAHXZ"() !dbg !15 {
33+
entry:
34+
call void @llvm.dbg.value(metadata i32 0, metadata !19, metadata !DIExpression()), !dbg !20
35+
%0 = load i32, ptr @"?Two@@3HA", align 4, !dbg !21
36+
call void @llvm.dbg.value(metadata i32 %0, metadata !22, metadata !DIExpression()), !dbg !20
37+
%1 = load i32, ptr @"?One@@3HA", align 4, !dbg !23
38+
switch i32 %1, label %sw.epilog [
39+
i32 0, label %sw.bb
40+
i32 2, label %sw.bb1
41+
], !dbg !23
42+
43+
sw.bb: ; preds = %entry
44+
call void @llvm.dbg.value(metadata i32 %0, metadata !19, metadata !DIExpression()), !dbg !20
45+
br label %sw.epilog, !dbg !24
46+
47+
sw.bb1: ; preds = %entry
48+
call void @llvm.dbg.value(metadata i32 4, metadata !19, metadata !DIExpression()), !dbg !20
49+
br label %sw.epilog, !dbg !26
50+
51+
sw.epilog: ; preds = %sw.bb1, %sw.bb, %entry
52+
%Three.0 = phi i32 [ 0, %entry ], [ 4, %sw.bb1 ], [ %0, %sw.bb ], !dbg !20
53+
call void @llvm.dbg.value(metadata i32 %Three.0, metadata !19, metadata !DIExpression()), !dbg !20
54+
ret i32 %Three.0, !dbg !27
55+
}
56+
57+
declare void @llvm.dbg.declare(metadata, metadata, metadata)
58+
declare void @llvm.dbg.value(metadata, metadata, metadata)
59+
60+
!llvm.dbg.cu = !{!2}
61+
!llvm.module.flags = !{!8, !9, !10, !11, !12, !13}
62+
!llvm.ident = !{!14}
63+
64+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
65+
!1 = distinct !DIGlobalVariable(name: "One", linkageName: "?One@@3HA", scope: !2, file: !3, line: 1, type: !7, isLocal: false, isDefinition: true)
66+
!2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !3, producer: "clang version 18.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, globals: !4, splitDebugInlining: false, nameTableKind: None)
67+
!3 = !DIFile(filename: "test.cpp", directory: "")
68+
!4 = !{!0, !5}
69+
!5 = !DIGlobalVariableExpression(var: !6, expr: !DIExpression())
70+
!6 = distinct !DIGlobalVariable(name: "Two", linkageName: "?Two@@3HA", scope: !2, file: !3, line: 2, type: !7, isLocal: false, isDefinition: true)
71+
!7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
72+
!8 = !{i32 2, !"CodeView", i32 1}
73+
!9 = !{i32 2, !"Debug Info Version", i32 3}
74+
!10 = !{i32 1, !"wchar_size", i32 2}
75+
!11 = !{i32 8, !"PIC Level", i32 2}
76+
!12 = !{i32 7, !"uwtable", i32 2}
77+
!13 = !{i32 1, !"MaxTLSAlign", i32 65536}
78+
!14 = !{!"clang version 18.0.0"}
79+
!15 = distinct !DISubprogram(name: "test", linkageName: "?test@@YAHXZ", scope: !3, file: !3, line: 3, type: !16, scopeLine: 3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !18)
80+
!16 = !DISubroutineType(types: !17)
81+
!17 = !{!7}
82+
!18 = !{}
83+
!19 = !DILocalVariable(name: "Three", scope: !15, file: !3, line: 4, type: !7)
84+
!20 = !DILocation(line: 0, scope: !15)
85+
!21 = !DILocation(line: 5, scope: !15)
86+
!22 = !DILocalVariable(name: "Four", scope: !15, file: !3, line: 5, type: !7)
87+
!23 = !DILocation(line: 6, scope: !15)
88+
!24 = !DILocation(line: 9, scope: !25)
89+
!25 = distinct !DILexicalBlock(scope: !15, file: !3, line: 6)
90+
!26 = !DILocation(line: 12, scope: !25)
91+
!27 = !DILocation(line: 14, scope: !15)

0 commit comments

Comments
 (0)