1; RUN: opt < %s -analyze -branch-prob | FileCheck %s
2; RUN: opt < %s -analyze -lazy-branch-prob | FileCheck %s
3; RUN: opt < %s -passes='print<branch-prob>' -disable-output 2>&1 | FileCheck %s
4
5define i32 @test1(i32 %i, i32* %a) {
6; CHECK: Printing analysis {{.*}} for function 'test1'
7entry:
8  br label %body
9; CHECK: edge entry -> body probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
10
11body:
12  %iv = phi i32 [ 0, %entry ], [ %next, %body ]
13  %base = phi i32 [ 0, %entry ], [ %sum, %body ]
14  %arrayidx = getelementptr inbounds i32, i32* %a, i32 %iv
15  %0 = load i32, i32* %arrayidx
16  %sum = add nsw i32 %0, %base
17  %next = add i32 %iv, 1
18  %exitcond = icmp eq i32 %next, %i
19  br i1 %exitcond, label %exit, label %body
20; CHECK: edge body -> exit probability is 0x04000000 / 0x80000000 = 3.12%
21; CHECK: edge body -> body probability is 0x7c000000 / 0x80000000 = 96.88% [HOT edge]
22
23exit:
24  ret i32 %sum
25}
26
27define i32 @test2(i32 %i, i32 %a, i32 %b) {
28; CHECK: Printing analysis {{.*}} for function 'test2'
29entry:
30  %cond = icmp ult i32 %i, 42
31  br i1 %cond, label %then, label %else, !prof !0
32; CHECK: edge entry -> then probability is 0x78787878 / 0x80000000 = 94.12% [HOT edge]
33; CHECK: edge entry -> else probability is 0x07878788 / 0x80000000 = 5.88%
34
35then:
36  br label %exit
37; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
38
39else:
40  br label %exit
41; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
42
43exit:
44  %result = phi i32 [ %a, %then ], [ %b, %else ]
45  ret i32 %result
46}
47
48!0 = !{!"branch_weights", i32 64, i32 4}
49
50define i32 @test3(i32 %i, i32 %a, i32 %b, i32 %c, i32 %d, i32 %e) {
51; CHECK: Printing analysis {{.*}} for function 'test3'
52entry:
53  switch i32 %i, label %case_a [ i32 1, label %case_b
54                                 i32 2, label %case_c
55                                 i32 3, label %case_d
56                                 i32 4, label %case_e ], !prof !1
57; CHECK: edge entry -> case_a probability is 0x06666666 / 0x80000000 = 5.00%
58; CHECK: edge entry -> case_b probability is 0x06666666 / 0x80000000 = 5.00%
59; CHECK: edge entry -> case_c probability is 0x66666666 / 0x80000000 = 80.00%
60; CHECK: edge entry -> case_d probability is 0x06666666 / 0x80000000 = 5.00%
61; CHECK: edge entry -> case_e probability is 0x06666666 / 0x80000000 = 5.00%
62
63case_a:
64  br label %exit
65; CHECK: edge case_a -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
66
67case_b:
68  br label %exit
69; CHECK: edge case_b -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
70
71case_c:
72  br label %exit
73; CHECK: edge case_c -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
74
75case_d:
76  br label %exit
77; CHECK: edge case_d -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
78
79case_e:
80  br label %exit
81; CHECK: edge case_e -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
82
83exit:
84  %result = phi i32 [ %a, %case_a ],
85                    [ %b, %case_b ],
86                    [ %c, %case_c ],
87                    [ %d, %case_d ],
88                    [ %e, %case_e ]
89  ret i32 %result
90}
91
92!1 = !{!"branch_weights", i32 4, i32 4, i32 64, i32 4, i32 4}
93
94define i32 @test4(i32 %x) nounwind uwtable readnone ssp {
95; CHECK: Printing analysis {{.*}} for function 'test4'
96entry:
97  %conv = sext i32 %x to i64
98  switch i64 %conv, label %return [
99    i64 0, label %sw.bb
100    i64 1, label %sw.bb
101    i64 2, label %sw.bb
102    i64 5, label %sw.bb1
103  ], !prof !2
104; CHECK: edge entry -> return probability is 0x0a8a8a8b / 0x80000000 = 8.24%
105; CHECK: edge entry -> sw.bb probability is 0x15151515 / 0x80000000 = 16.47%
106; CHECK: edge entry -> sw.bb1 probability is 0x60606060 / 0x80000000 = 75.29%
107
108sw.bb:
109  br label %return
110
111sw.bb1:
112  br label %return
113
114return:
115  %retval.0 = phi i32 [ 5, %sw.bb1 ], [ 1, %sw.bb ], [ 0, %entry ]
116  ret i32 %retval.0
117}
118
119!2 = !{!"branch_weights", i32 7, i32 6, i32 4, i32 4, i32 64}
120
121declare void @coldfunc() cold
122
123define i32 @test5(i32 %a, i32 %b, i1 %flag) {
124; CHECK: Printing analysis {{.*}} for function 'test5'
125entry:
126  br i1 %flag, label %then, label %else
127; CHECK: edge entry -> then probability is 0x07878788 / 0x80000000 = 5.88%
128; CHECK: edge entry -> else probability is 0x78787878 / 0x80000000 = 94.12% [HOT edge]
129
130then:
131  call void @coldfunc()
132  br label %exit
133; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
134
135else:
136  br label %exit
137; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
138
139exit:
140  %result = phi i32 [ %a, %then ], [ %b, %else ]
141  ret i32 %result
142}
143
144declare i32 @regular_function(i32 %i)
145
146define i32 @test_cold_call_sites(i32* %a) {
147; Test that edges to blocks post-dominated by cold call sites
148; are marked as not expected to be taken.
149; TODO(dnovillo) The calls to regular_function should not be merged, but
150; they are currently being merged. Convert this into a code generation test
151; after that is fixed.
152
153; CHECK: Printing analysis {{.*}} for function 'test_cold_call_sites'
154; CHECK: edge entry -> then probability is 0x07878788 / 0x80000000 = 5.88%
155; CHECK: edge entry -> else probability is 0x78787878 / 0x80000000 = 94.12% [HOT edge]
156
157entry:
158  %gep1 = getelementptr i32, i32* %a, i32 1
159  %val1 = load i32, i32* %gep1
160  %cond1 = icmp ugt i32 %val1, 1
161  br i1 %cond1, label %then, label %else
162
163then:
164  ; This function is not declared cold, but this call site is.
165  %val4 = call i32 @regular_function(i32 %val1) cold
166  br label %exit
167
168else:
169  %gep2 = getelementptr i32, i32* %a, i32 2
170  %val2 = load i32, i32* %gep2
171  %val3 = call i32 @regular_function(i32 %val2)
172  br label %exit
173
174exit:
175  %ret = phi i32 [ %val4, %then ], [ %val3, %else ]
176  ret i32 %ret
177}
178
179; CHECK-LABEL: test_invoke_code_callsite1
180define i32 @test_invoke_code_callsite1(i1 %c) personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
181entry:
182  br i1 %c, label %if.then, label %if.end
183; Edge "entry->if.end" should have higher probability based on the cold call
184; heuristic which treat %if.then as a cold block because the normal destination
185; of the invoke instruction in %if.then is post-dominated by ColdFunc().
186; CHECK:  edge entry -> if.then probability is 0x07878788 / 0x80000000 = 5.88%
187; CHECK:  edge entry -> if.end probability is 0x78787878 / 0x80000000 = 94.12% [HOT edge]
188
189if.then:
190  invoke i32 @InvokeCall()
191          to label %invoke.cont unwind label %lpad
192; CHECK:  edge if.then -> invoke.cont probability is 0x7ffff800 / 0x80000000 = 100.00% [HOT edge]
193; CHECK:  edge if.then -> lpad probability is 0x00000800 / 0x80000000 = 0.00%
194
195invoke.cont:
196  call void @ColdFunc() #0
197  br label %if.end
198
199lpad:
200  %ll = landingpad { i8*, i32 }
201          cleanup
202  br label %if.end
203
204if.end:
205  ret i32 0
206}
207
208; CHECK-LABEL: test_invoke_code_callsite2
209define i32 @test_invoke_code_callsite2(i1 %c) personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
210entry:
211  br i1 %c, label %if.then, label %if.end
212
213; CHECK:  edge entry -> if.then probability is 0x40000000 / 0x80000000 = 50.00%
214; CHECK:  edge entry -> if.end probability is 0x40000000 / 0x80000000 = 50.00%
215
216if.then:
217  invoke i32 @InvokeCall()
218          to label %invoke.cont unwind label %lpad
219; The cold call heuristic should not kick in when the cold callsite is in EH path.
220; CHECK:  edge if.then -> invoke.cont probability is 0x7ffff800 / 0x80000000 = 100.00% [HOT edge]
221; CHECK:  edge if.then -> lpad probability is 0x00000800 / 0x80000000 = 0.00%
222
223invoke.cont:
224  br label %if.end
225
226lpad:
227  %ll = landingpad { i8*, i32 }
228          cleanup
229  call void @ColdFunc() #0
230  br label %if.end
231
232if.end:
233  ret i32 0
234}
235
236; CHECK-LABEL: test_invoke_code_callsite3
237define i32 @test_invoke_code_callsite3(i1 %c) personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
238entry:
239  br i1 %c, label %if.then, label %if.end
240; CHECK: edge entry -> if.then probability is 0x07878788 / 0x80000000 = 5.88%
241; CHECK: edge entry -> if.end probability is 0x78787878 / 0x80000000 = 94.12% [HOT edge]
242
243if.then:
244  invoke i32 @InvokeCall()
245          to label %invoke.cont unwind label %lpad
246; Regardless of cold calls, edge weights from a invoke instruction should be
247; determined by the invoke heuristic.
248; CHECK: edge if.then -> invoke.cont probability is 0x7ffff800 / 0x80000000 = 100.00% [HOT edge]
249; CHECK: edge if.then -> lpad probability is 0x00000800 / 0x80000000 = 0.00%
250
251invoke.cont:
252  call void @ColdFunc() #0
253  br label %if.end
254
255lpad:
256  %ll = landingpad { i8*, i32 }
257          cleanup
258  call void @ColdFunc() #0
259  br label %if.end
260
261if.end:
262  ret i32 0
263}
264
265declare i32 @__gxx_personality_v0(...)
266declare void  @ColdFunc()
267declare i32 @InvokeCall()
268
269attributes #0 = { cold }
270
271
272define i32 @zero1(i32 %i, i32 %a, i32 %b) {
273; CHECK: Printing analysis {{.*}} for function 'zero1'
274entry:
275  %cond = icmp eq i32 %i, 0
276  br i1 %cond, label %then, label %else
277; CHECK: edge entry -> then probability is 0x30000000 / 0x80000000 = 37.50%
278; CHECK: edge entry -> else probability is 0x50000000 / 0x80000000 = 62.50%
279
280then:
281  br label %exit
282
283else:
284  br label %exit
285
286exit:
287  %result = phi i32 [ %a, %then ], [ %b, %else ]
288  ret i32 %result
289}
290
291define i32 @zero2(i32 %i, i32 %a, i32 %b) {
292; CHECK: Printing analysis {{.*}} for function 'zero2'
293entry:
294  %cond = icmp ne i32 %i, -1
295  br i1 %cond, label %then, label %else
296; CHECK: edge entry -> then probability is 0x50000000 / 0x80000000 = 62.50%
297; CHECK: edge entry -> else probability is 0x30000000 / 0x80000000 = 37.50%
298
299then:
300  br label %exit
301
302else:
303  br label %exit
304
305exit:
306  %result = phi i32 [ %a, %then ], [ %b, %else ]
307  ret i32 %result
308}
309
310define i32 @zero3(i32 %i, i32 %a, i32 %b) {
311; CHECK: Printing analysis {{.*}} for function 'zero3'
312entry:
313; AND'ing with a single bit bitmask essentially leads to a bool comparison,
314; meaning we don't have probability information.
315  %and = and i32 %i, 2
316  %tobool = icmp eq i32 %and, 0
317  br i1 %tobool, label %then, label %else
318; CHECK: edge entry -> then probability is 0x40000000 / 0x80000000 = 50.00%
319; CHECK: edge entry -> else probability is 0x40000000 / 0x80000000 = 50.00%
320
321then:
322; AND'ing with other bitmask might be something else, so we still assume the
323; usual probabilities.
324  %and2 = and i32 %i, 5
325  %tobool2 = icmp eq i32 %and2, 0
326  br i1 %tobool2, label %else, label %exit
327; CHECK: edge then -> else probability is 0x30000000 / 0x80000000 = 37.50%
328; CHECK: edge then -> exit probability is 0x50000000 / 0x80000000 = 62.50%
329
330else:
331  br label %exit
332
333exit:
334  %result = phi i32 [ %a, %then ], [ %b, %else ]
335  ret i32 %result
336}
337
338