xref: /llvm-project/llvm/test/DebugInfo/Generic/assignment-tracking/licm/merge.ll (revision 094572701dce4aaf36f4521d6cf750420d39f206)
1; RUN: opt -passes=licm %s -S | FileCheck %s
2; RUN: opt --try-experimental-debuginfo-iterators -passes=licm %s -S | FileCheck %s
3
4;; Ensure that we correctly merge the DIAssignID's from the sunk stores, add it
5;; to the new new store instruction, and update the dbg.assign intrinsics using
6;; them to use it instead.
7
8;; Generated from the following, with some changes to the IR by hand:
9;; $ cat test.c
10;; void b(int c) {
11;;   esc(&c);
12;;   for (; c;  c++) // NOTE: I've added another store to c in the loop by hand.
13;;   ;
14;; }
15;; $ clang -O2 -Xclang -disable-llvm-passes -g -emit-llvm -S -o a.ll
16;; $ opt -passes=declare-to-assign,sroa,instcombine,simplifycfg,loop-simplify,lcssa,loop-rotate a.ll
17
18; CHECK-LABEL: for.inc:
19;; Check that the stores have actually been removed from this block, otherwise
20;; this test is useless.
21; CHECK-NOT: store i32 %inc, ptr %c.addr
22;; Check that the two dbg.assigns now have the same (merged) !DIAssingID ID.
23; CHECK: #dbg_assign(i32 %inc, ![[VAR_C:[0-9]+]], !DIExpression(), ![[ID:[0-9]+]], ptr %c.addr, !DIExpression(),
24; CHECK-NOT: store i32 %inc, ptr %c.addr
25; CHECK: #dbg_assign(i32 %inc, ![[VAR_C]], !DIExpression(), ![[ID]], ptr %c.addr, !DIExpression(),
26
27; CHECK-LABEL: for.cond.for.end_crit_edge:
28; CHECK-NEXT: %[[PHI:.*]] = phi i32 [ %inc, %for.inc ]
29; CHECK-NEXT: store i32 %[[PHI]], ptr %c.addr{{.*}}, !DIAssignID ![[ID]]
30; CHECK-NOT:  {{.*}}llvm.dbg{{.*}}
31; CHECK-NEXT: br label %for.end
32
33; CHECK: ![[VAR_C]] = !DILocalVariable(name: "c",
34
35define dso_local void @b(i32 %c) !dbg !7 {
36entry:
37  %c.addr = alloca i32, align 4
38  store i32 %c, ptr %c.addr, align 4, !DIAssignID !36
39  call void @llvm.dbg.assign(metadata i32 %c, metadata !12, metadata !DIExpression(), metadata !36, metadata ptr %c.addr, metadata !DIExpression()), !dbg !13
40  call void @esc(ptr nonnull %c.addr), !dbg !18
41  %0 = load i32, ptr %c.addr, align 4, !dbg !19
42  %tobool.not1 = icmp eq i32 %0, 0, !dbg !22
43  br i1 %tobool.not1, label %for.end, label %for.inc.lr.ph, !dbg !22
44
45for.inc.lr.ph:                                    ; preds = %entry
46  br label %for.inc, !dbg !22
47
48for.inc:                                          ; preds = %for.inc.lr.ph, %for.inc
49  %1 = load i32, ptr %c.addr, align 4, !dbg !23
50  %inc = add nsw i32 %1, 1, !dbg !23
51  store i32 %inc, ptr %c.addr, align 4, !dbg !23, !DIAssignID !38
52  call void @llvm.dbg.assign(metadata i32 %inc, metadata !12, metadata !DIExpression(), metadata !38, metadata ptr %c.addr, metadata !DIExpression()), !dbg !13
53  ;; The following store and dbg.assign intrinsics are copies of those above,
54  ;; with a new DIAssignID.
55  store i32 %inc, ptr %c.addr, align 4, !dbg !23, !DIAssignID !37
56  call void @llvm.dbg.assign(metadata i32 %inc, metadata !12, metadata !DIExpression(), metadata !37, metadata ptr %c.addr, metadata !DIExpression()), !dbg !13
57  %2 = load i32, ptr %c.addr, align 4, !dbg !19
58  %tobool.not = icmp eq i32 %2, 0, !dbg !22
59  br i1 %tobool.not, label %for.cond.for.end_crit_edge, label %for.inc, !dbg !22, !llvm.loop !24
60
61for.cond.for.end_crit_edge:                       ; preds = %for.inc
62  br label %for.end, !dbg !22
63
64for.end:                                          ; preds = %for.cond.for.end_crit_edge, %entry
65  ret void, !dbg !27
66}
67
68
69declare void @llvm.dbg.assign(metadata, metadata, metadata, metadata, metadata, metadata)
70declare !dbg !28 dso_local void @esc(ptr)
71
72!llvm.dbg.cu = !{!0}
73!llvm.module.flags = !{!3, !4, !5, !1000}
74!llvm.ident = !{!6}
75
76!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 12.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None)
77!1 = !DIFile(filename: "test.c", directory: "/")
78!2 = !{}
79!3 = !{i32 7, !"Dwarf Version", i32 4}
80!4 = !{i32 2, !"Debug Info Version", i32 3}
81!5 = !{i32 1, !"wchar_size", i32 4}
82!6 = !{!"clang version 12.0.0"}
83!7 = distinct !DISubprogram(name: "b", scope: !1, file: !1, line: 2, type: !8, scopeLine: 2, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !11)
84!8 = !DISubroutineType(types: !9)
85!9 = !{null, !10}
86!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
87!11 = !{!12}
88!12 = !DILocalVariable(name: "c", arg: 1, scope: !7, file: !1, line: 2, type: !10)
89!13 = !DILocation(line: 0, scope: !7)
90!18 = !DILocation(line: 3, column: 3, scope: !7)
91!19 = !DILocation(line: 4, column: 10, scope: !20)
92!20 = distinct !DILexicalBlock(scope: !21, file: !1, line: 4, column: 3)
93!21 = distinct !DILexicalBlock(scope: !7, file: !1, line: 4, column: 3)
94!22 = !DILocation(line: 4, column: 3, scope: !21)
95!23 = !DILocation(line: 4, column: 15, scope: !20)
96!24 = distinct !{!24, !22, !25, !26}
97!25 = !DILocation(line: 5, column: 3, scope: !21)
98!26 = !{!"llvm.loop.mustprogress"}
99!27 = !DILocation(line: 6, column: 1, scope: !7)
100!28 = !DISubprogram(name: "esc", scope: !1, file: !1, line: 1, type: !29, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, retainedNodes: !2)
101!29 = !DISubroutineType(types: !30)
102!30 = !{null, !31}
103!31 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !10, size: 64)
104!36 = distinct !DIAssignID()
105!37 = distinct !DIAssignID()
106!38 = distinct !DIAssignID()
107!1000 = !{i32 7, !"debug-info-assignment-tracking", i1 true}
108