1; RUN: opt < %s -pgo-instr-gen -S | FileCheck %s --check-prefix=GEN
2; RUN: llvm-profdata merge %S/Inputs/criticaledge.proftext -o %t.profdata
3; RUN: opt < %s -pgo-instr-use -pgo-test-profile-file=%t.profdata -S | FileCheck %s --check-prefix=USE
4target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
5target triple = "x86_64-unknown-linux-gnu"
6
7; GEN: @__profn_test_criticalEdge = private constant [17 x i8] c"test_criticalEdge"
8; GEN: @__profn__stdin__bar = private constant [11 x i8] c"<stdin>:bar"
9
10define i32 @test_criticalEdge(i32 %i, i32 %j) {
11entry:
12; CHECK: entry:
13; GEN-NOT: call void @llvm.instrprof.increment
14  switch i32 %i, label %sw.default [
15    i32 1, label %sw.bb
16    i32 2, label %sw.bb1
17    i32 3, label %sw.bb2
18    i32 4, label %sw.bb2
19; CHECK:    i32 3, label %entry.sw.bb2_crit_edge
20; CHECK:    i32 4, label %entry.sw.bb2_crit_edge1
21    i32 5, label %sw.bb2
22  ]
23; USE: ]
24; USE-SAME: !prof ![[BW_SWITCH:[0-9]+]]
25
26; CHECK: entry.sw.bb2_crit_edge1:
27; GEN:   call void @llvm.instrprof.increment(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @__profn_test_criticalEdge, i32 0, i32 0), i64 82323253069, i32 8, i32 1)
28; CHECK:   br label %sw.bb2
29
30; CHECK: entry.sw.bb2_crit_edge:
31; GEN:   call void @llvm.instrprof.increment(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @__profn_test_criticalEdge, i32 0, i32 0), i64 82323253069, i32 8, i32 0)
32; CHECK:   br label %sw.bb2
33
34sw.bb:
35; GEN: sw.bb:
36; GEN: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @__profn_test_criticalEdge, i32 0, i32 0), i64 82323253069, i32 8, i32 5)
37  %call = call i32 @bar(i32 2)
38  br label %sw.epilog
39
40sw.bb1:
41; GEN: sw.bb1:
42; GEN: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @__profn_test_criticalEdge, i32 0, i32 0), i64 82323253069, i32 8, i32 4)
43  %call2 = call i32 @bar(i32 1024)
44  br label %sw.epilog
45
46sw.bb2:
47; GEN: sw.bb2:
48; GEN-NOT: call void @llvm.instrprof.increment
49  %cmp = icmp eq i32 %j, 2
50  br i1 %cmp, label %if.then, label %if.end
51; USE: br i1 %cmp, label %if.then, label %if.end
52; USE-SAME: !prof ![[BW_SW_BB2:[0-9]+]]
53
54if.then:
55; GEN: if.then:
56; GEN: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @__profn_test_criticalEdge, i32 0, i32 0), i64 82323253069, i32 8, i32 2)
57  %call4 = call i32 @bar(i32 4)
58  br label %return
59
60if.end:
61; GEN: if.end:
62; GEN: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @__profn_test_criticalEdge, i32 0, i32 0), i64 82323253069, i32 8, i32 3)
63  %call5 = call i32 @bar(i32 8)
64  br label %sw.epilog
65
66sw.default:
67; GEN: sw.default:
68; GEN-NOT: call void @llvm.instrprof.increment
69  %call6 = call i32 @bar(i32 32)
70  %cmp7 = icmp sgt i32 %j, 10
71  br i1 %cmp7, label %if.then8, label %if.end9
72; USE: br i1 %cmp7, label %if.then8, label %if.end9
73; USE-SAME: !prof ![[BW_SW_DEFAULT:[0-9]+]]
74
75if.then8:
76; GEN: if.then8:
77; GEN: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @__profn_test_criticalEdge, i32 0, i32 0), i64 82323253069, i32 8, i32 7)
78  %add = add nsw i32 %call6, 10
79  br label %if.end9
80
81if.end9:
82; GEN: if.end9:
83; GEN: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @__profn_test_criticalEdge, i32 0, i32 0), i64 82323253069, i32 8, i32 6)
84  %res.0 = phi i32 [ %add, %if.then8 ], [ %call6, %sw.default ]
85  br label %sw.epilog
86
87sw.epilog:
88; GEN: sw.epilog:
89; GEN-NOT: call void @llvm.instrprof.increment
90  %res.1 = phi i32 [ %res.0, %if.end9 ], [ %call5, %if.end ], [ %call2, %sw.bb1 ], [ %call, %sw.bb ]
91  br label %return
92
93return:
94; GEN: return:
95; GEN-NOT: call void @llvm.instrprof.increment
96  %retval = phi i32 [ %res.1, %sw.epilog ], [ %call4, %if.then ]
97  ret i32 %retval
98}
99
100define internal i32 @bar(i32 %i) {
101entry:
102; GEN: call void @llvm.instrprof.increment(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @__profn__stdin__bar, i32 0, i32 0), i64 12884901887, i32 1, i32 0)
103  ret i32 %i
104}
105
106; USE: ![[BW_SWITCH]] = !{!"branch_weights", i32 2, i32 1, i32 0, i32 2, i32 1, i32 1}
107; USE: ![[BW_SW_BB2]] = !{!"branch_weights", i32 2, i32 2}
108; USE: ![[BW_SW_DEFAULT]] = !{!"branch_weights", i32 1, i32 1}
109