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
5 define i32 @test1(i32 %i, i32* %a) {
6 ; CHECK: Printing analysis {{.*}} for function 'test1'
9 ; CHECK: edge entry -> body probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
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]
27 define i32 @test2(i32 %i, i32 %a, i32 %b) {
28 ; CHECK: Printing analysis {{.*}} for function 'test2'
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%
37 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
41 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
44 %result = phi i32 [ %a, %then ], [ %b, %else ]
48 !0 = !{!"branch_weights", i32 64, i32 4}
50 define i32 @test3(i32 %i, i32 %a, i32 %b, i32 %c, i32 %d, i32 %e) {
51 ; CHECK: Printing analysis {{.*}} for function 'test3'
53 switch i32 %i, label %case_a [ i32 1, label %case_b
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%
65 ; CHECK: edge case_a -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
69 ; CHECK: edge case_b -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
73 ; CHECK: edge case_c -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
77 ; CHECK: edge case_d -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
81 ; CHECK: edge case_e -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
84 %result = phi i32 [ %a, %case_a ],
92 !1 = !{!"branch_weights", i32 4, i32 4, i32 64, i32 4, i32 4}
94 define i32 @test4(i32 %x) nounwind uwtable readnone ssp {
95 ; CHECK: Printing analysis {{.*}} for function 'test4'
97 %conv = sext i32 %x to i64
98 switch i64 %conv, label %return [
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%
115 %retval.0 = phi i32 [ 5, %sw.bb1 ], [ 1, %sw.bb ], [ 0, %entry ]
119 !2 = !{!"branch_weights", i32 7, i32 6, i32 4, i32 4, i32 64}
121 declare void @coldfunc() cold
123 define i32 @test5(i32 %a, i32 %b, i1 %flag) {
124 ; CHECK: Printing analysis {{.*}} for function 'test5'
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]
131 call void @coldfunc()
133 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
137 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
140 %result = phi i32 [ %a, %then ], [ %b, %else ]
144 declare i32 @regular_function(i32 %i)
146 define i32 @test_cold_call_sites_with_prof(i32 %a, i32 %b, i1 %flag, i1 %flag2) {
147 ; CHECK: Printing analysis {{.*}} for function 'test_cold_call_sites_with_prof'
149 br i1 %flag, label %then, label %else
150 ; CHECK: edge entry -> then probability is 0x07878788 / 0x80000000 = 5.88%
151 ; CHECK: edge entry -> else probability is 0x78787878 / 0x80000000 = 94.12% [HOT edge]
154 br i1 %flag2, label %then2, label %else2, !prof !3
155 ; CHECK: edge then -> then2 probability is 0x7ebb907a / 0x80000000 = 99.01% [HOT edge]
156 ; CHECK: edge then -> else2 probability is 0x01446f86 / 0x80000000 = 0.99%
160 ; CHECK: edge then2 -> join probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
164 ; CHECK: edge else2 -> join probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
167 %joinresult = phi i32 [ %a, %then2 ], [ %b, %else2 ]
168 call void @coldfunc()
170 ; CHECK: edge join -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
174 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
177 %result = phi i32 [ %joinresult, %join ], [ %b, %else ]
181 !3 = !{!"branch_weights", i32 100, i32 1}
183 define i32 @test_cold_call_sites(i32* %a) {
184 ; Test that edges to blocks post-dominated by cold call sites
185 ; are marked as not expected to be taken.
186 ; TODO(dnovillo) The calls to regular_function should not be merged, but
187 ; they are currently being merged. Convert this into a code generation test
188 ; after that is fixed.
190 ; CHECK: Printing analysis {{.*}} for function 'test_cold_call_sites'
191 ; CHECK: edge entry -> then probability is 0x07878788 / 0x80000000 = 5.88%
192 ; CHECK: edge entry -> else probability is 0x78787878 / 0x80000000 = 94.12% [HOT edge]
195 %gep1 = getelementptr i32, i32* %a, i32 1
196 %val1 = load i32, i32* %gep1
197 %cond1 = icmp ugt i32 %val1, 1
198 br i1 %cond1, label %then, label %else
201 ; This function is not declared cold, but this call site is.
202 %val4 = call i32 @regular_function(i32 %val1) cold
206 %gep2 = getelementptr i32, i32* %a, i32 2
207 %val2 = load i32, i32* %gep2
208 %val3 = call i32 @regular_function(i32 %val2)
212 %ret = phi i32 [ %val4, %then ], [ %val3, %else ]
216 ; CHECK-LABEL: test_invoke_code_callsite1
217 define i32 @test_invoke_code_callsite1(i1 %c) personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
219 br i1 %c, label %if.then, label %if.end
220 ; Edge "entry->if.end" should have higher probability based on the cold call
221 ; heuristic which treat %if.then as a cold block because the normal destination
222 ; of the invoke instruction in %if.then is post-dominated by ColdFunc().
223 ; CHECK: edge entry -> if.then probability is 0x07878788 / 0x80000000 = 5.88%
224 ; CHECK: edge entry -> if.end probability is 0x78787878 / 0x80000000 = 94.12% [HOT edge]
227 invoke i32 @InvokeCall()
228 to label %invoke.cont unwind label %lpad
229 ; CHECK: edge if.then -> invoke.cont probability is 0x7ffff800 / 0x80000000 = 100.00% [HOT edge]
230 ; CHECK: edge if.then -> lpad probability is 0x00000800 / 0x80000000 = 0.00%
233 call void @ColdFunc() #0
237 %ll = landingpad { i8*, i32 }
245 ; CHECK-LABEL: test_invoke_code_callsite2
246 define i32 @test_invoke_code_callsite2(i1 %c) personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
248 br i1 %c, label %if.then, label %if.end
250 ; CHECK: edge entry -> if.then probability is 0x40000000 / 0x80000000 = 50.00%
251 ; CHECK: edge entry -> if.end probability is 0x40000000 / 0x80000000 = 50.00%
254 invoke i32 @InvokeCall()
255 to label %invoke.cont unwind label %lpad
256 ; The cold call heuristic should not kick in when the cold callsite is in EH path.
257 ; CHECK: edge if.then -> invoke.cont probability is 0x7ffff800 / 0x80000000 = 100.00% [HOT edge]
258 ; CHECK: edge if.then -> lpad probability is 0x00000800 / 0x80000000 = 0.00%
264 %ll = landingpad { i8*, i32 }
266 call void @ColdFunc() #0
273 ; CHECK-LABEL: test_invoke_code_callsite3
274 define i32 @test_invoke_code_callsite3(i1 %c) personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
276 br i1 %c, label %if.then, label %if.end
277 ; CHECK: edge entry -> if.then probability is 0x07878788 / 0x80000000 = 5.88%
278 ; CHECK: edge entry -> if.end probability is 0x78787878 / 0x80000000 = 94.12% [HOT edge]
281 invoke i32 @InvokeCall()
282 to label %invoke.cont unwind label %lpad
283 ; Regardless of cold calls, edge weights from a invoke instruction should be
284 ; determined by the invoke heuristic.
285 ; CHECK: edge if.then -> invoke.cont probability is 0x7ffff800 / 0x80000000 = 100.00% [HOT edge]
286 ; CHECK: edge if.then -> lpad probability is 0x00000800 / 0x80000000 = 0.00%
289 call void @ColdFunc() #0
293 %ll = landingpad { i8*, i32 }
295 call void @ColdFunc() #0
302 declare i32 @__gxx_personality_v0(...)
303 declare void @ColdFunc()
304 declare i32 @InvokeCall()
306 attributes #0 = { cold }
309 define i32 @zero1(i32 %i, i32 %a, i32 %b) {
310 ; CHECK: Printing analysis {{.*}} for function 'zero1'
312 %cond = icmp eq i32 %i, 0
313 br i1 %cond, label %then, label %else
314 ; CHECK: edge entry -> then probability is 0x30000000 / 0x80000000 = 37.50%
315 ; CHECK: edge entry -> else probability is 0x50000000 / 0x80000000 = 62.50%
324 %result = phi i32 [ %a, %then ], [ %b, %else ]
328 define i32 @zero2(i32 %i, i32 %a, i32 %b) {
329 ; CHECK: Printing analysis {{.*}} for function 'zero2'
331 %cond = icmp ne i32 %i, -1
332 br i1 %cond, label %then, label %else
333 ; CHECK: edge entry -> then probability is 0x50000000 / 0x80000000 = 62.50%
334 ; CHECK: edge entry -> else probability is 0x30000000 / 0x80000000 = 37.50%
343 %result = phi i32 [ %a, %then ], [ %b, %else ]
347 define i32 @zero3(i32 %i, i32 %a, i32 %b) {
348 ; CHECK: Printing analysis {{.*}} for function 'zero3'
350 ; AND'ing with a single bit bitmask essentially leads to a bool comparison,
351 ; meaning we don't have probability information.
353 %tobool = icmp eq i32 %and, 0
354 br i1 %tobool, label %then, label %else
355 ; CHECK: edge entry -> then probability is 0x40000000 / 0x80000000 = 50.00%
356 ; CHECK: edge entry -> else probability is 0x40000000 / 0x80000000 = 50.00%
359 ; AND'ing with other bitmask might be something else, so we still assume the
360 ; usual probabilities.
361 %and2 = and i32 %i, 5
362 %tobool2 = icmp eq i32 %and2, 0
363 br i1 %tobool2, label %else, label %exit
364 ; CHECK: edge then -> else probability is 0x30000000 / 0x80000000 = 37.50%
365 ; CHECK: edge then -> exit probability is 0x50000000 / 0x80000000 = 62.50%
371 %result = phi i32 [ %a, %then ], [ %b, %else ]
375 define i32 @test_unreachable_with_prof_greater(i32 %a, i32 %b) {
376 ; CHECK: Printing analysis {{.*}} for function 'test_unreachable_with_prof_greater'
378 %cond = icmp eq i32 %a, 42
379 br i1 %cond, label %exit, label %unr, !prof !4
381 ; CHECK: edge entry -> exit probability is 0x7fffffff / 0x80000000 = 100.00% [HOT edge]
382 ; CHECK: edge entry -> unr probability is 0x00000001 / 0x80000000 = 0.00%
391 !4 = !{!"branch_weights", i32 0, i32 1}
393 define i32 @test_unreachable_with_prof_equal(i32 %a, i32 %b) {
394 ; CHECK: Printing analysis {{.*}} for function 'test_unreachable_with_prof_equal'
396 %cond = icmp eq i32 %a, 42
397 br i1 %cond, label %exit, label %unr, !prof !5
399 ; CHECK: edge entry -> exit probability is 0x7fffffff / 0x80000000 = 100.00% [HOT edge]
400 ; CHECK: edge entry -> unr probability is 0x00000001 / 0x80000000 = 0.00%
409 !5 = !{!"branch_weights", i32 2147483647, i32 1}
411 define i32 @test_unreachable_with_prof_zero(i32 %a, i32 %b) {
412 ; CHECK: Printing analysis {{.*}} for function 'test_unreachable_with_prof_zero'
414 %cond = icmp eq i32 %a, 42
415 br i1 %cond, label %exit, label %unr, !prof !6
417 ; CHECK: edge entry -> exit probability is 0x7fffffff / 0x80000000 = 100.00% [HOT edge]
418 ; CHECK: edge entry -> unr probability is 0x00000001 / 0x80000000 = 0.00%
427 !6 = !{!"branch_weights", i32 0, i32 0}
429 define i32 @test_unreachable_with_prof_less(i32 %a, i32 %b) {
430 ; CHECK: Printing analysis {{.*}} for function 'test_unreachable_with_prof_less'
432 %cond = icmp eq i32 %a, 42
433 br i1 %cond, label %exit, label %unr, !prof !7
435 ; CHECK: edge entry -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
436 ; CHECK: edge entry -> unr probability is 0x00000000 / 0x80000000 = 0.00%
445 !7 = !{!"branch_weights", i32 1, i32 0}
447 define i32 @test_unreachable_with_switch_prof1(i32 %i, i32 %a, i32 %b, i32 %c, i32 %d, i32 %e) {
448 ; CHECK: Printing analysis {{.*}} for function 'test_unreachable_with_switch_prof1'
450 switch i32 %i, label %case_a [ i32 1, label %case_b
453 i32 4, label %case_e ], !prof !8
454 ; CHECK: edge entry -> case_a probability is 0x00000001 / 0x80000000 = 0.00%
455 ; CHECK: edge entry -> case_b probability is 0x07ffffff / 0x80000000 = 6.25%
456 ; CHECK: edge entry -> case_c probability is 0x67ffffff / 0x80000000 = 81.25% [HOT edge]
457 ; CHECK: edge entry -> case_d probability is 0x07ffffff / 0x80000000 = 6.25%
458 ; CHECK: edge entry -> case_e probability is 0x07ffffff / 0x80000000 = 6.25%
465 ; CHECK: edge case_b -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
469 ; CHECK: edge case_c -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
473 ; CHECK: edge case_d -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
477 ; CHECK: edge case_e -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
480 %result = phi i32 [ %b, %case_b ],
487 !8 = !{!"branch_weights", i32 4, i32 4, i32 64, i32 4, i32 4}
489 define i32 @test_unreachable_with_switch_prof2(i32 %i, i32 %a, i32 %b, i32 %c, i32 %d, i32 %e) {
490 ; CHECK: Printing analysis {{.*}} for function 'test_unreachable_with_switch_prof2'
492 switch i32 %i, label %case_a [ i32 1, label %case_b
495 i32 4, label %case_e ], !prof !9
496 ; CHECK: edge entry -> case_a probability is 0x00000001 / 0x80000000 = 0.00%
497 ; CHECK: edge entry -> case_b probability is 0x00000001 / 0x80000000 = 0.00%
498 ; CHECK: edge entry -> case_c probability is 0x6aaaaaa9 / 0x80000000 = 83.33% [HOT edge]
499 ; CHECK: edge entry -> case_d probability is 0x0aaaaaa9 / 0x80000000 = 8.33%
500 ; CHECK: edge entry -> case_e probability is 0x0aaaaaa9 / 0x80000000 = 8.33%
510 ; CHECK: edge case_c -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
514 ; CHECK: edge case_d -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
518 ; CHECK: edge case_e -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
521 %result = phi i32 [ %c, %case_c ],
527 !9 = !{!"branch_weights", i32 4, i32 4, i32 64, i32 4, i32 4}
529 define i32 @test_unreachable_with_switch_prof3(i32 %i, i32 %a, i32 %b, i32 %c, i32 %d, i32 %e) {
530 ; CHECK: Printing analysis {{.*}} for function 'test_unreachable_with_switch_prof3'
532 switch i32 %i, label %case_a [ i32 1, label %case_b
535 i32 4, label %case_e ], !prof !10
536 ; CHECK: edge entry -> case_a probability is 0x00000000 / 0x80000000 = 0.00%
537 ; CHECK: edge entry -> case_b probability is 0x00000001 / 0x80000000 = 0.00%
538 ; CHECK: edge entry -> case_c probability is 0x6e08fb82 / 0x80000000 = 85.96% [HOT edge]
539 ; CHECK: edge entry -> case_d probability is 0x08fb823e / 0x80000000 = 7.02%
540 ; CHECK: edge entry -> case_e probability is 0x08fb823e / 0x80000000 = 7.02%
550 ; CHECK: edge case_c -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
554 ; CHECK: edge case_d -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
558 ; CHECK: edge case_e -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
561 %result = phi i32 [ %c, %case_c ],
567 !10 = !{!"branch_weights", i32 0, i32 4, i32 64, i32 4, i32 4}
569 define i32 @test_unreachable_with_switch_prof4(i32 %i, i32 %a, i32 %b, i32 %c, i32 %d, i32 %e) {
570 ; CHECK: Printing analysis {{.*}} for function 'test_unreachable_with_switch_prof4'
572 switch i32 %i, label %case_a [ i32 1, label %case_b
575 i32 4, label %case_e ], !prof !11
576 ; CHECK: edge entry -> case_a probability is 0x1999999a / 0x80000000 = 20.00%
577 ; CHECK: edge entry -> case_b probability is 0x1999999a / 0x80000000 = 20.00%
578 ; CHECK: edge entry -> case_c probability is 0x1999999a / 0x80000000 = 20.00%
579 ; CHECK: edge entry -> case_d probability is 0x1999999a / 0x80000000 = 20.00%
580 ; CHECK: edge entry -> case_e probability is 0x1999999a / 0x80000000 = 20.00%
599 !11 = !{!"branch_weights", i32 0, i32 4, i32 64, i32 4, i32 4}