1; RUN: opt -passes=instcombine -S -o - < %s | FileCheck %s
2; RUN: opt -passes=instcombine -S -o - < %s --try-experimental-debuginfo-iterators | FileCheck %s
3
4; When the 'int Four = Two;' is sunk into the 'case 0:' block,
5; the debug value for 'Three' is set incorrectly to 'poison'.
6
7;  1 int One = 0;
8;  2 int Two = 0;
9;  3 int test() {
10;  4   int Three = 0;
11;  5   int Four = Two;
12;  6   switch (One) {
13;  7   case 0:
14;  8     Three = Four;
15;  9     break;
16; 10   case 2:
17; 11     Three = 4;
18; 12     break;
19; 13   }
20; 14   return Three;
21; 15 }
22
23; CHECK-LABEL: sw.bb:
24; CHECK: %[[REG:[0-9]+]] = load i32, ptr @"?Two{{.*}}
25; CHECK: #dbg_value(i32 %[[REG]], ![[DBG1:[0-9]+]], {{.*}}
26; CHECK: #dbg_value(i32 %[[REG]], ![[DBG2:[0-9]+]], {{.*}}
27; CHECK-DAG: ![[DBG1]] = !DILocalVariable(name: "Four"{{.*}})
28; CHECK-DAG: ![[DBG2]] = !DILocalVariable(name: "Three"{{.*}})
29
30@"?One@@3HA" = dso_local global i32 0, align 4, !dbg !0
31@"?Two@@3HA" = dso_local global i32 0, align 4, !dbg !5
32
33define dso_local noundef i32 @"?test@@YAHXZ"() !dbg !15 {
34entry:
35  call void @llvm.dbg.value(metadata i32 0, metadata !19, metadata !DIExpression()), !dbg !20
36  %0 = load i32, ptr @"?Two@@3HA", align 4, !dbg !21
37  call void @llvm.dbg.value(metadata i32 %0, metadata !22, metadata !DIExpression()), !dbg !20
38  %1 = load i32, ptr @"?One@@3HA", align 4, !dbg !23
39  switch i32 %1, label %sw.epilog [
40    i32 0, label %sw.bb
41    i32 2, label %sw.bb1
42  ], !dbg !23
43
44sw.bb:                                            ; preds = %entry
45  call void @llvm.dbg.value(metadata i32 %0, metadata !19, metadata !DIExpression()), !dbg !20
46  br label %sw.epilog, !dbg !24
47
48sw.bb1:                                           ; preds = %entry
49  call void @llvm.dbg.value(metadata i32 4, metadata !19, metadata !DIExpression()), !dbg !20
50  br label %sw.epilog, !dbg !26
51
52sw.epilog:                                        ; preds = %sw.bb1, %sw.bb, %entry
53  %Three.0 = phi i32 [ 0, %entry ], [ 4, %sw.bb1 ], [ %0, %sw.bb ], !dbg !20
54  call void @llvm.dbg.value(metadata i32 %Three.0, metadata !19, metadata !DIExpression()), !dbg !20
55  ret i32 %Three.0, !dbg !27
56}
57
58declare void @llvm.dbg.declare(metadata, metadata, metadata)
59declare void @llvm.dbg.value(metadata, metadata, metadata)
60
61!llvm.dbg.cu = !{!2}
62!llvm.module.flags = !{!8, !9, !10, !11, !12, !13}
63!llvm.ident = !{!14}
64
65!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
66!1 = distinct !DIGlobalVariable(name: "One", linkageName: "?One@@3HA", scope: !2, file: !3, line: 1, type: !7, isLocal: false, isDefinition: true)
67!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)
68!3 = !DIFile(filename: "test.cpp", directory: "")
69!4 = !{!0, !5}
70!5 = !DIGlobalVariableExpression(var: !6, expr: !DIExpression())
71!6 = distinct !DIGlobalVariable(name: "Two", linkageName: "?Two@@3HA", scope: !2, file: !3, line: 2, type: !7, isLocal: false, isDefinition: true)
72!7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
73!8 = !{i32 2, !"CodeView", i32 1}
74!9 = !{i32 2, !"Debug Info Version", i32 3}
75!10 = !{i32 1, !"wchar_size", i32 2}
76!11 = !{i32 8, !"PIC Level", i32 2}
77!12 = !{i32 7, !"uwtable", i32 2}
78!13 = !{i32 1, !"MaxTLSAlign", i32 65536}
79!14 = !{!"clang version 18.0.0"}
80!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)
81!16 = !DISubroutineType(types: !17)
82!17 = !{!7}
83!18 = !{}
84!19 = !DILocalVariable(name: "Three", scope: !15, file: !3, line: 4, type: !7)
85!20 = !DILocation(line: 0, scope: !15)
86!21 = !DILocation(line: 5, scope: !15)
87!22 = !DILocalVariable(name: "Four", scope: !15, file: !3, line: 5, type: !7)
88!23 = !DILocation(line: 6, scope: !15)
89!24 = !DILocation(line: 9, scope: !25)
90!25 = distinct !DILexicalBlock(scope: !15, file: !3, line: 6)
91!26 = !DILocation(line: 12, scope: !25)
92!27 = !DILocation(line: 14, scope: !15)
93