1 ; RUN: opt < %s -passes='print<branch-prob>' -disable-output 2>&1 | FileCheck %s
3 define i32 @test1(i32 %i, ptr %a) {
4 ; CHECK: Printing analysis {{.*}} for function 'test1'
7 ; CHECK: edge entry -> body probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
10 %iv = phi i32 [ 0, %entry ], [ %next, %body ]
11 %base = phi i32 [ 0, %entry ], [ %sum, %body ]
12 %arrayidx = getelementptr inbounds i32, ptr %a, i32 %iv
13 %0 = load i32, ptr %arrayidx
14 %sum = add nsw i32 %0, %base
15 %next = add i32 %iv, 1
16 %exitcond = icmp eq i32 %next, %i
17 br i1 %exitcond, label %exit, label %body
18 ; CHECK: edge body -> exit probability is 0x04000000 / 0x80000000 = 3.12%
19 ; CHECK: edge body -> body probability is 0x7c000000 / 0x80000000 = 96.88% [HOT edge]
25 define i32 @test2(i32 %i, i32 %a, i32 %b) {
26 ; CHECK: Printing analysis {{.*}} for function 'test2'
28 %cond = icmp ult i32 %i, 42
29 br i1 %cond, label %then, label %else, !prof !0
30 ; CHECK: edge entry -> then probability is 0x78787878 / 0x80000000 = 94.12% [HOT edge]
31 ; CHECK: edge entry -> else probability is 0x07878788 / 0x80000000 = 5.88%
35 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
39 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
42 %result = phi i32 [ %a, %then ], [ %b, %else ]
46 !0 = !{!"branch_weights", i32 64, i32 4}
48 define i32 @test3(i32 %i, i32 %a, i32 %b, i32 %c, i32 %d, i32 %e) {
49 ; CHECK: Printing analysis {{.*}} for function 'test3'
51 switch i32 %i, label %case_a [ i32 1, label %case_b
54 i32 4, label %case_e ], !prof !1
55 ; CHECK: edge entry -> case_a probability is 0x06666666 / 0x80000000 = 5.00%
56 ; CHECK: edge entry -> case_b probability is 0x06666666 / 0x80000000 = 5.00%
57 ; CHECK: edge entry -> case_c probability is 0x66666666 / 0x80000000 = 80.00%
58 ; CHECK: edge entry -> case_d probability is 0x06666666 / 0x80000000 = 5.00%
59 ; CHECK: edge entry -> case_e probability is 0x06666666 / 0x80000000 = 5.00%
63 ; CHECK: edge case_a -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
67 ; CHECK: edge case_b -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
71 ; CHECK: edge case_c -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
75 ; CHECK: edge case_d -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
79 ; CHECK: edge case_e -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
82 %result = phi i32 [ %a, %case_a ],
90 !1 = !{!"branch_weights", i32 4, i32 4, i32 64, i32 4, i32 4}
92 define i32 @test4(i32 %x) nounwind uwtable readnone ssp {
93 ; CHECK: Printing analysis {{.*}} for function 'test4'
95 %conv = sext i32 %x to i64
96 switch i64 %conv, label %return [
102 ; CHECK: edge entry -> return probability is 0x0a8a8a8b / 0x80000000 = 8.24%
103 ; CHECK: edge entry -> sw.bb probability is 0x15151515 / 0x80000000 = 16.47%
104 ; CHECK: edge entry -> sw.bb1 probability is 0x60606060 / 0x80000000 = 75.29%
113 %retval.0 = phi i32 [ 5, %sw.bb1 ], [ 1, %sw.bb ], [ 0, %entry ]
117 !2 = !{!"branch_weights", i32 7, i32 6, i32 4, i32 4, i32 64}
119 declare void @coldfunc() cold
121 define i32 @test5(i32 %a, i32 %b, i1 %flag) {
122 ; CHECK: Printing analysis {{.*}} for function 'test5'
124 br i1 %flag, label %then, label %else
125 ; CHECK: edge entry -> then probability is 0x078780e3 / 0x80000000 = 5.88%
126 ; CHECK: edge entry -> else probability is 0x78787f1d / 0x80000000 = 94.12% [HOT edge]
129 call void @coldfunc()
131 ; CHECK: edge then -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
135 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
138 %result = phi i32 [ %a, %then ], [ %b, %else ]
142 define i32 @test_cold_loop(i32 %a, i32 %b) {
144 %cond1 = icmp eq i32 %a, 42
145 br i1 %cond1, label %header, label %exit
146 ; CHECK: edge entry -> header probability is 0x40000000 / 0x80000000 = 50.00%
147 ; CHECK: edge entry -> exit probability is 0x40000000 / 0x80000000 = 50.00%
152 %cond2 = icmp eq i32 %b, 42
153 br i1 %cond2, label %header, label %exit
154 ; CHECK: edge body -> header probability is 0x7fbe1203 / 0x80000000 = 99.80% [HOT edge]
155 ; CHECK: edge body -> exit probability is 0x0041edfd / 0x80000000 = 0.20%
157 call void @coldfunc()
161 declare i32 @regular_function(i32 %i)
163 define i32 @test_cold_call_sites_with_prof(i32 %a, i32 %b, i1 %flag, i1 %flag2) {
164 ; CHECK: Printing analysis {{.*}} for function 'test_cold_call_sites_with_prof'
166 br i1 %flag, label %then, label %else
167 ; CHECK: edge entry -> then probability is 0x078780e3 / 0x80000000 = 5.88%
168 ; CHECK: edge entry -> else probability is 0x78787f1d / 0x80000000 = 94.12% [HOT edge]
171 br i1 %flag2, label %then2, label %else2, !prof !3
172 ; CHECK: edge then -> then2 probability is 0x7ebb907a / 0x80000000 = 99.01% [HOT edge]
173 ; CHECK: edge then -> else2 probability is 0x01446f86 / 0x80000000 = 0.99%
177 ; CHECK: edge then2 -> join probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
181 ; CHECK: edge else2 -> join probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
184 %joinresult = phi i32 [ %a, %then2 ], [ %b, %else2 ]
185 call void @coldfunc()
187 ; CHECK: edge join -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
191 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
194 %result = phi i32 [ %joinresult, %join ], [ %b, %else ]
198 !3 = !{!"branch_weights", i32 100, i32 1}
200 define i32 @test_cold_call_sites(ptr %a) {
201 ; Test that edges to blocks post-dominated by cold call sites
202 ; are marked as not expected to be taken.
203 ; TODO(dnovillo) The calls to regular_function should not be merged, but
204 ; they are currently being merged. Convert this into a code generation test
205 ; after that is fixed.
207 ; CHECK: Printing analysis {{.*}} for function 'test_cold_call_sites'
208 ; CHECK: edge entry -> then probability is 0x078780e3 / 0x80000000 = 5.88%
209 ; CHECK: edge entry -> else probability is 0x78787f1d / 0x80000000 = 94.12% [HOT edge]
212 %gep1 = getelementptr i32, ptr %a, i32 1
213 %val1 = load i32, ptr %gep1
214 %cond1 = icmp ugt i32 %val1, 1
215 br i1 %cond1, label %then, label %else
218 ; This function is not declared cold, but this call site is.
219 %val4 = call i32 @regular_function(i32 %val1) cold
223 %gep2 = getelementptr i32, ptr %a, i32 2
224 %val2 = load i32, ptr %gep2
225 %val3 = call i32 @regular_function(i32 %val2)
229 %ret = phi i32 [ %val4, %then ], [ %val3, %else ]
233 ; CHECK-LABEL: test_invoke_code_callsite1
234 define i32 @test_invoke_code_callsite1(i1 %c) personality ptr @__gxx_personality_v0 {
236 br i1 %c, label %if.then, label %if.end
237 ; Edge "entry->if.end" should have higher probability based on the cold call
238 ; heuristic which treat %if.then as a cold block because the normal destination
239 ; of the invoke instruction in %if.then is post-dominated by ColdFunc().
240 ; CHECK: edge entry -> if.then probability is 0x078780e3 / 0x80000000 = 5.88%
241 ; CHECK: edge entry -> if.end probability is 0x78787f1d / 0x80000000 = 94.12% [HOT edge]
244 invoke i32 @InvokeCall()
245 to label %invoke.cont unwind label %lpad
246 ; CHECK: edge if.then -> invoke.cont probability is 0x7fff8000 / 0x80000000 = 100.00% [HOT edge]
247 ; CHECK: edge if.then -> lpad probability is 0x00008000 / 0x80000000 = 0.00%
250 call void @ColdFunc() #0
254 %ll = landingpad { ptr, i32 }
262 ; CHECK-LABEL: test_invoke_code_callsite2
263 define i32 @test_invoke_code_callsite2(i1 %c) personality ptr @__gxx_personality_v0 {
265 br i1 %c, label %if.then, label %if.end
267 ; CHECK: edge entry -> if.then probability is 0x40000000 / 0x80000000 = 50.00%
268 ; CHECK: edge entry -> if.end probability is 0x40000000 / 0x80000000 = 50.00%
270 invoke i32 @InvokeCall()
271 to label %invoke.cont unwind label %lpad
272 ; The cold call heuristic should not kick in when the cold callsite is in EH path.
273 ; CHECK: edge if.then -> invoke.cont probability is 0x7ffff800 / 0x80000000 = 100.00% [HOT edge]
274 ; CHECK: edge if.then -> lpad probability is 0x00000800 / 0x80000000 = 0.00%
280 %ll = landingpad { ptr, i32 }
282 call void @ColdFunc() #0
289 ; CHECK-LABEL: test_invoke_code_callsite3
290 define i32 @test_invoke_code_callsite3(i1 %c) personality ptr @__gxx_personality_v0 {
292 br i1 %c, label %if.then, label %if.end
293 ; CHECK: edge entry -> if.then probability is 0x078780e3 / 0x80000000 = 5.88%
294 ; CHECK: edge entry -> if.end probability is 0x78787f1d / 0x80000000 = 94.12% [HOT edge]
297 invoke i32 @InvokeCall()
298 to label %invoke.cont unwind label %lpad
299 ; Regardless of cold calls, edge weights from a invoke instruction should be
300 ; determined by the invoke heuristic.
301 ; CHECK: edge if.then -> invoke.cont probability is 0x7fff8000 / 0x80000000 = 100.00% [HOT edge]
302 ; CHECK: edge if.then -> lpad probability is 0x00008000 / 0x80000000 = 0.00%
305 call void @ColdFunc() #0
309 %ll = landingpad { ptr, i32 }
311 call void @ColdFunc() #0
318 ; CHECK-LABEL: test_invoke_code_profiled
319 define void @test_invoke_code_profiled(i1 %c) personality ptr @__gxx_personality_v0 {
321 ; CHECK: edge entry -> invoke.to0 probability is 0x7ffff800 / 0x80000000 = 100.00% [HOT edge]
322 ; CHECK: edge entry -> lpad probability is 0x00000800 / 0x80000000 = 0.00%
323 invoke i32 @InvokeCall() to label %invoke.to0 unwind label %lpad
326 ; CHECK: edge invoke.to0 -> invoke.to1 probability is 0x7ffff800 / 0x80000000 = 100.00% [HOT edge]
327 ; CHECK: edge invoke.to0 -> lpad probability is 0x00000800 / 0x80000000 = 0.00%
328 invoke i32 @InvokeCall() to label %invoke.to1 unwind label %lpad,
329 !prof !{!"branch_weights", i32 444}
332 ; CHECK: invoke.to1 -> invoke.to2 probability is 0x55555555 / 0x80000000 = 66.67%
333 ; CHECK: invoke.to1 -> lpad probability is 0x2aaaaaab / 0x80000000 = 33.33%
334 invoke i32 @InvokeCall() to label %invoke.to2 unwind label %lpad,
335 !prof !{!"branch_weights", i32 222, i32 111}
342 %ll = landingpad { ptr, i32 }
347 declare i32 @__gxx_personality_v0(...)
348 declare void @ColdFunc()
349 declare i32 @InvokeCall()
351 attributes #0 = { cold }
354 define i32 @zero1(i32 %i, i32 %a, i32 %b) {
355 ; CHECK: Printing analysis {{.*}} for function 'zero1'
357 %cond = icmp eq i32 %i, 0
358 br i1 %cond, label %then, label %else
359 ; CHECK: edge entry -> then probability is 0x30000000 / 0x80000000 = 37.50%
360 ; CHECK: edge entry -> else probability is 0x50000000 / 0x80000000 = 62.50%
369 %result = phi i32 [ %a, %then ], [ %b, %else ]
373 define i32 @zero2(i32 %i, i32 %a, i32 %b) {
374 ; CHECK: Printing analysis {{.*}} for function 'zero2'
376 %cond = icmp ne i32 %i, -1
377 br i1 %cond, label %then, label %else
378 ; CHECK: edge entry -> then probability is 0x50000000 / 0x80000000 = 62.50%
379 ; CHECK: edge entry -> else probability is 0x30000000 / 0x80000000 = 37.50%
388 %result = phi i32 [ %a, %then ], [ %b, %else ]
392 define i32 @zero3(i32 %i, i32 %a, i32 %b) {
393 ; CHECK: Printing analysis {{.*}} for function 'zero3'
395 ; AND'ing with a single bit bitmask essentially leads to a bool comparison,
396 ; meaning we don't have probability information.
398 %tobool = icmp eq i32 %and, 0
399 br i1 %tobool, label %then, label %else
400 ; CHECK: edge entry -> then probability is 0x40000000 / 0x80000000 = 50.00%
401 ; CHECK: edge entry -> else probability is 0x40000000 / 0x80000000 = 50.00%
404 ; AND'ing with other bitmask might be something else, so we still assume the
405 ; usual probabilities.
406 %and2 = and i32 %i, 5
407 %tobool2 = icmp eq i32 %and2, 0
408 br i1 %tobool2, label %else, label %exit
409 ; CHECK: edge then -> else probability is 0x30000000 / 0x80000000 = 37.50%
410 ; CHECK: edge then -> exit probability is 0x50000000 / 0x80000000 = 62.50%
416 %result = phi i32 [ %a, %then ], [ %b, %else ]
420 define i32 @test_unreachable_with_prof_greater(i32 %a, i32 %b) {
421 ; CHECK: Printing analysis {{.*}} for function 'test_unreachable_with_prof_greater'
423 %cond = icmp eq i32 %a, 42
424 br i1 %cond, label %exit, label %unr, !prof !4
426 ; CHECK: edge entry -> exit probability is 0x7fffffff / 0x80000000 = 100.00% [HOT edge]
427 ; CHECK: edge entry -> unr probability is 0x00000001 / 0x80000000 = 0.00%
436 !4 = !{!"branch_weights", i32 0, i32 1}
438 define i32 @test_unreachable_with_prof_equal(i32 %a, i32 %b) {
439 ; CHECK: Printing analysis {{.*}} for function 'test_unreachable_with_prof_equal'
441 %cond = icmp eq i32 %a, 42
442 br i1 %cond, label %exit, label %unr, !prof !5
444 ; CHECK: edge entry -> exit probability is 0x7fffffff / 0x80000000 = 100.00% [HOT edge]
445 ; CHECK: edge entry -> unr probability is 0x00000001 / 0x80000000 = 0.00%
454 !5 = !{!"branch_weights", i32 2147483647, i32 1}
456 define i32 @test_unreachable_with_prof_zero(i32 %a, i32 %b) {
457 ; CHECK: Printing analysis {{.*}} for function 'test_unreachable_with_prof_zero'
459 %cond = icmp eq i32 %a, 42
460 br i1 %cond, label %exit, label %unr, !prof !6
462 ; CHECK: edge entry -> exit probability is 0x7fffffff / 0x80000000 = 100.00% [HOT edge]
463 ; CHECK: edge entry -> unr probability is 0x00000001 / 0x80000000 = 0.00%
472 !6 = !{!"branch_weights", i32 0, i32 0}
474 define i32 @test_unreachable_with_prof_less(i32 %a, i32 %b) {
475 ; CHECK: Printing analysis {{.*}} for function 'test_unreachable_with_prof_less'
477 %cond = icmp eq i32 %a, 42
478 br i1 %cond, label %exit, label %unr, !prof !7
480 ; CHECK: edge entry -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
481 ; CHECK: edge entry -> unr probability is 0x00000000 / 0x80000000 = 0.00%
490 !7 = !{!"branch_weights", i32 1, i32 0}
492 define i32 @test_unreachable_with_switch_prof1(i32 %i, i32 %a, i32 %b, i32 %c, i32 %d, i32 %e) {
493 ; CHECK: Printing analysis {{.*}} for function 'test_unreachable_with_switch_prof1'
495 switch i32 %i, label %case_a [ i32 1, label %case_b
498 i32 4, label %case_e ], !prof !8
499 ; Reachable probabilities keep their relation: 4/64/4/4 = 5.26% / 84.21% / 5.26% / 5.26%.
500 ; CHECK: edge entry -> case_a probability is 0x00000001 / 0x80000000 = 0.00%
501 ; CHECK: edge entry -> case_b probability is 0x06bca1af / 0x80000000 = 5.26%
502 ; CHECK: edge entry -> case_c probability is 0x6bca1af3 / 0x80000000 = 84.21% [HOT edge]
503 ; CHECK: edge entry -> case_d probability is 0x06bca1af / 0x80000000 = 5.26%
504 ; CHECK: edge entry -> case_e probability is 0x06bca1af / 0x80000000 = 5.26%
511 ; CHECK: edge case_b -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
515 ; CHECK: edge case_c -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
519 ; CHECK: edge case_d -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
523 ; CHECK: edge case_e -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
526 %result = phi i32 [ %b, %case_b ],
533 !8 = !{!"branch_weights", i32 4, i32 4, i32 64, i32 4, i32 4}
535 define i32 @test_unreachable_with_switch_prof2(i32 %i, i32 %a, i32 %b, i32 %c, i32 %d, i32 %e) {
536 ; CHECK: Printing analysis {{.*}} for function 'test_unreachable_with_switch_prof2'
538 switch i32 %i, label %case_a [ i32 1, label %case_b
541 i32 4, label %case_e ], !prof !9
542 ; Reachable probabilities keep their relation: 64/4/4 = 88.89% / 5.56% / 5.56%.
543 ; CHECK: edge entry -> case_a probability is 0x00000001 / 0x80000000 = 0.00%
544 ; CHECK: edge entry -> case_b probability is 0x00000001 / 0x80000000 = 0.00%
545 ; CHECK: edge entry -> case_c probability is 0x71c71c71 / 0x80000000 = 88.89% [HOT edge]
546 ; CHECK: edge entry -> case_d probability is 0x071c71c7 / 0x80000000 = 5.56%
547 ; CHECK: edge entry -> case_e probability is 0x071c71c7 / 0x80000000 = 5.56%
558 ; CHECK: edge case_c -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
562 ; CHECK: edge case_d -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
566 ; CHECK: edge case_e -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
569 %result = phi i32 [ %c, %case_c ],
575 !9 = !{!"branch_weights", i32 4, i32 4, i32 64, i32 4, i32 4}
577 define i32 @test_unreachable_with_switch_prof3(i32 %i, i32 %a, i32 %b, i32 %c, i32 %d, i32 %e) {
578 ; CHECK: Printing analysis {{.*}} for function 'test_unreachable_with_switch_prof3'
580 switch i32 %i, label %case_a [ i32 1, label %case_b
583 i32 4, label %case_e ], !prof !10
584 ; Reachable probabilities keep their relation: 64/4/4 = 88.89% / 5.56% / 5.56%.
585 ; CHECK: edge entry -> case_a probability is 0x00000000 / 0x80000000 = 0.00%
586 ; CHECK: edge entry -> case_b probability is 0x00000001 / 0x80000000 = 0.00%
587 ; CHECK: edge entry -> case_c probability is 0x71c71c71 / 0x80000000 = 88.89% [HOT edge]
588 ; CHECK: edge entry -> case_d probability is 0x071c71c7 / 0x80000000 = 5.56%
589 ; CHECK: edge entry -> case_e probability is 0x071c71c7 / 0x80000000 = 5.56%
599 ; CHECK: edge case_c -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
603 ; CHECK: edge case_d -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
607 ; CHECK: edge case_e -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
610 %result = phi i32 [ %c, %case_c ],
616 !10 = !{!"branch_weights", i32 0, i32 4, i32 64, i32 4, i32 4}
618 define i32 @test_unreachable_with_switch_prof4(i32 %i, i32 %a, i32 %b, i32 %c, i32 %d, i32 %e) {
619 ; CHECK: Printing analysis {{.*}} for function 'test_unreachable_with_switch_prof4'
621 switch i32 %i, label %case_a [ i32 1, label %case_b
624 i32 4, label %case_e ], !prof !11
625 ; CHECK: edge entry -> case_a probability is 0x1999999a / 0x80000000 = 20.00%
626 ; CHECK: edge entry -> case_b probability is 0x1999999a / 0x80000000 = 20.00%
627 ; CHECK: edge entry -> case_c probability is 0x1999999a / 0x80000000 = 20.00%
628 ; CHECK: edge entry -> case_d probability is 0x1999999a / 0x80000000 = 20.00%
629 ; CHECK: edge entry -> case_e probability is 0x1999999a / 0x80000000 = 20.00%
648 !11 = !{!"branch_weights", i32 0, i32 4, i32 64, i32 4, i32 4}