1; RUN: opt -simplifycfg -sink-common-insts -S < %s | FileCheck %s
2
3target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
4target triple = "x86_64-unknown-linux-gnu"
5
6; Simplify CFG will try to sink the last instruction in a series of basic
7; blocks, creating a "common" instruction in the successor block.  If the
8; debug locations of the commoned instructions have different file/line
9; numbers the debug location of the common instruction should be a merged
10; location.
11
12; Generated from source:
13
14; extern int foo(void);
15; extern int bar(void);
16;
17; int test(int a, int b) {
18;   if(a)
19;     b -= foo();
20;   else
21;     b -= bar();
22;   return b;
23; }
24
25; CHECK: define i32 @test
26; CHECK-LABEL: if.end:
27; CHECK: %[[PHI:.*]] = phi i32 [ %call1, %if.else ], [ %call, %if.then ]
28; CHECK: sub nsw i32 %b, %[[PHI]], !dbg [[testMergedLoc:![0-9]+]]
29; CHECK: ret i32
30
31define i32 @test(i32 %a, i32 %b) !dbg !6 {
32entry:
33  %tobool = icmp ne i32 %a, 0, !dbg !8
34  br i1 %tobool, label %if.then, label %if.else, !dbg !8
35
36if.then:                                          ; preds = %entry
37  %call = call i32 @foo(), !dbg !9
38  %sub = sub nsw i32 %b, %call, !dbg !10
39  br label %if.end, !dbg !11
40
41if.else:                                          ; preds = %entry
42  %call1 = call i32 @bar(), !dbg !12
43  %sub2 = sub nsw i32 %b, %call1, !dbg !13
44  br label %if.end
45
46if.end:                                           ; preds = %if.else, %if.then
47  %b.addr.0 = phi i32 [ %sub, %if.then ], [ %sub2, %if.else ]
48  ret i32 %b.addr.0, !dbg !14
49}
50
51; When the commoned instructions have the same debug location, this location
52; should be used as the location of the common instruction.
53
54; Generated from source (with -mllvm -no-discriminators and -gno-column-info):
55
56; int test2(int a, int b) {
57;   if(a) b -= foo(); else b -= bar();
58;   return b;
59; }
60
61; CHECK: define i32 @test2
62; CHECK-LABEL: if.end:
63; CHECK: %[[PHI:.*]] = phi i32 [ %call1, %if.else ], [ %call, %if.then ]
64; CHECK: sub nsw i32 %b, %[[PHI]], !dbg [[test2Loc:![0-9]+]]
65; CHECK: ret i32
66
67define i32 @test2(i32 %a, i32 %b) !dbg !15 {
68entry:
69  %tobool = icmp ne i32 %a, 0, !dbg !16
70  br i1 %tobool, label %if.then, label %if.else, !dbg !16
71
72if.then:                                          ; preds = %entry
73  %call = call i32 @foo(), !dbg !16
74  %sub = sub nsw i32 %b, %call, !dbg !16
75  br label %if.end, !dbg !16
76
77if.else:                                          ; preds = %entry
78  %call1 = call i32 @bar(), !dbg !16
79  %sub2 = sub nsw i32 %b, %call1, !dbg !16
80  br label %if.end
81
82if.end:                                           ; preds = %if.else, %if.then
83  %b.addr.0 = phi i32 [ %sub, %if.then ], [ %sub2, %if.else ]
84  ret i32 %b.addr.0, !dbg !17
85}
86
87; CHECK: [[testMergedLoc]] = !DILocation(line: 0
88; CHECK: [[test2Loc]] = !DILocation(line: 17
89
90declare i32 @foo()
91declare i32 @bar()
92
93!llvm.dbg.cu = !{!0}
94!llvm.module.flags = !{!3, !4}
95
96!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "", isOptimized: false, runtimeVersion: 0, emissionKind: LineTablesOnly, enums: !2)
97!1 = !DIFile(filename: "test.c", directory: "")
98!2 = !{}
99!3 = !{i32 2, !"Dwarf Version", i32 4}
100!4 = !{i32 2, !"Debug Info Version", i32 3}
101!6 = distinct !DISubprogram(name: "test", scope: !1, file: !1, line: 8, type: !7, isLocal: false, isDefinition: true, scopeLine: 8, flags: DIFlagPrototyped, isOptimized: false, unit: !0, retainedNodes: !2)
102!7 = !DISubroutineType(types: !2)
103!8 = !DILocation(line: 9, column: 6, scope: !6)
104!9 = !DILocation(line: 10, column: 10, scope: !6)
105!10 = !DILocation(line: 10, column: 7, scope: !6)
106!11 = !DILocation(line: 10, column: 5, scope: !6)
107!12 = !DILocation(line: 12, column: 10, scope: !6)
108!13 = !DILocation(line: 12, column: 7, scope: !6)
109!14 = !DILocation(line: 13, column: 3, scope: !6)
110!15 = distinct !DISubprogram(name: "test2", scope: !1, file: !1, line: 16, type: !7, isLocal: false, isDefinition: true, scopeLine: 16, flags: DIFlagPrototyped, isOptimized: false, unit: !0, retainedNodes: !2)
111!16 = !DILocation(line: 17, scope: !15)
112!17 = !DILocation(line: 18, scope: !15)
113