xref: /llvm-project/llvm/test/Transforms/AddDiscriminators/memcpy-discriminator.ll (revision 7850ab21128875611dead7d312ffbe4c8b47a5f2)
1; RUN: opt < %s -passes='add-discriminators,sroa' -S | FileCheck %s
2
3target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
4
5; Test case obtained from the following C code:
6
7; struct A {
8;  int field1;
9;  short field2;
10; };
11;
12; struct B {
13;   struct A field1;
14;   int field2;
15; };
16;
17;
18; extern struct B g_b;
19; extern int bar(struct B b, int c);
20;
21; int foo(int cond) {
22;   int result = cond ? bar(g_b, 33) : 42;
23;   return result;
24; }
25
26; In this test, global variable g_b is passed by copy to function bar. That
27; copy is located on the stack (see alloca %g_b.coerce), and it is initialized
28; by a memcpy call.
29;
30; SROA would split alloca %g_b.coerce into two (smaller disjoint) slices:
31; slice [0,8) and slice [8, 12). Users of the original alloca are rewritten
32; as users of the new alloca slices.
33; In particular, the memcpy is rewritten by SROA as two load/store pairs.
34;
35; Later on, mem2reg successfully promotes the new alloca slices to registers,
36; and loads %3 and %5 are made redundant by the loads obtained from the memcpy
37; intrinsic expansion.
38;
39; If pass AddDiscriminators doesn't assign a discriminator to the intrinsic
40; memcpy call, then the loads obtained from the memcpy expansion would not have
41; a correct discriminator.
42;
43; This test checks that the two new loads inserted by SROA in %cond.true
44; correctly reference a debug location with a non-zero discriminator. This test
45; also checks that the same discriminator is used by all instructions from
46; basic block %cond.true.
47
48%struct.B = type { %struct.A, i32 }
49%struct.A = type { i32, i16 }
50
51@g_b = external global %struct.B, align 4
52
53define i32 @foo(i32 %cond) #0 !dbg !5 {
54entry:
55  %g_b.coerce = alloca { i64, i32 }, align 4
56  %tobool = icmp ne i32 %cond, 0, !dbg !7
57  br i1 %tobool, label %cond.true, label %cond.end, !dbg !7
58
59cond.true:
60; CHECK-LABEL: cond.true:
61; CHECK:       load i64, {{.*}}, !dbg ![[LOC:[0-9]+]]
62; CHECK-NEXT:  load i32, {{.*}}, !dbg ![[LOC]]
63; CHECK-NEXT:  %call = call i32 @bar({{.*}}), !dbg ![[LOC]]
64; CHECK-NEXT:  br label %cond.end, !dbg ![[BR_LOC:[0-9]+]]
65
66; CHECK-DAG: ![[LOC]] = !DILocation(line: 16, column: 23, scope: ![[SCOPE:[0-9]+]])
67; CHECK-DAG: ![[SCOPE]] = !DILexicalBlockFile({{.*}}, discriminator: 2)
68; CHECK-DAG: ![[BR_LOC]] = !DILocation(line: 16, column: 16, scope: ![[SCOPE]])
69
70  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %g_b.coerce, ptr align 4 @g_b, i64 12, i1 false), !dbg !8
71  %p1 = getelementptr inbounds { i64, i32 }, ptr %g_b.coerce, i32 0, i32 0, !dbg !8
72  %v1 = load i64, ptr %p1, align 4, !dbg !8
73  %p2 = getelementptr inbounds { i64, i32 }, ptr %g_b.coerce, i32 0, i32 1, !dbg !8
74  %v2 = load i32, ptr %p2, align 4, !dbg !8
75  %call = call i32 @bar(i64 %v1, i32 %v2, i32 33), !dbg !8
76  br label %cond.end, !dbg !7
77
78cond.end:                                         ; preds = %entry, %cond.true
79  %cond1 = phi i32 [ %call, %cond.true ], [ 42, %entry ], !dbg !7
80  ret i32 %cond1, !dbg !9
81}
82
83declare i32 @bar(i64, i32, i32)
84
85declare void @llvm.memcpy.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1) #1
86
87attributes #0 = { noinline nounwind uwtable }
88attributes #1 = { argmemonly nounwind }
89
90!llvm.dbg.cu = !{!0}
91!llvm.module.flags = !{!3, !4}
92
93!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, isOptimized: false, runtimeVersion: 0, emissionKind: LineTablesOnly, enums: !2)
94!1 = !DIFile(filename: "test.c", directory: ".")
95!2 = !{}
96!3 = !{i32 2, !"Dwarf Version", i32 4}
97!4 = !{i32 2, !"Debug Info Version", i32 3}
98!5 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 15, type: !6, isLocal: false, isDefinition: true, scopeLine: 15, flags: DIFlagPrototyped, isOptimized: false, unit: !0, retainedNodes: !2)
99!6 = !DISubroutineType(types: !2)
100!7 = !DILocation(line: 16, column: 16, scope: !5)
101!8 = !DILocation(line: 16, column: 23, scope: !5)
102!9 = !DILocation(line: 17, column: 3, scope: !5)
103