1 ; RUN: opt < %s -analyze -branch-prob -enable-new-pm=0 | FileCheck %s
2 ; RUN: opt < %s -analyze -lazy-branch-prob -enable-new-pm=0 | 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 0x078780e3 / 0x80000000 = 5.88%
128 ; CHECK: edge entry -> else probability is 0x78787f1d / 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 define i32 @test_cold_loop(i32 %a, i32 %b) {
146 %cond1 = icmp eq i32 %a, 42
147 br i1 %cond1, label %header, label %exit
148 ; CHECK: edge entry -> header probability is 0x40000000 / 0x80000000 = 50.00%
149 ; CHECK: edge entry -> exit probability is 0x40000000 / 0x80000000 = 50.00%
154 %cond2 = icmp eq i32 %b, 42
155 br i1 %cond2, label %header, label %exit
156 ; CHECK: edge body -> header probability is 0x7fbe1203 / 0x80000000 = 99.80% [HOT edge]
157 ; CHECK: edge body -> exit probability is 0x0041edfd / 0x80000000 = 0.20%
159 call void @coldfunc()
163 declare i32 @regular_function(i32 %i)
165 define i32 @test_cold_call_sites_with_prof(i32 %a, i32 %b, i1 %flag, i1 %flag2) {
166 ; CHECK: Printing analysis {{.*}} for function 'test_cold_call_sites_with_prof'
168 br i1 %flag, label %then, label %else
169 ; CHECK: edge entry -> then probability is 0x078780e3 / 0x80000000 = 5.88%
170 ; CHECK: edge entry -> else probability is 0x78787f1d / 0x80000000 = 94.12% [HOT edge]
173 br i1 %flag2, label %then2, label %else2, !prof !3
174 ; CHECK: edge then -> then2 probability is 0x7ebb907a / 0x80000000 = 99.01% [HOT edge]
175 ; CHECK: edge then -> else2 probability is 0x01446f86 / 0x80000000 = 0.99%
179 ; CHECK: edge then2 -> join probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
183 ; CHECK: edge else2 -> join probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
186 %joinresult = phi i32 [ %a, %then2 ], [ %b, %else2 ]
187 call void @coldfunc()
189 ; CHECK: edge join -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
193 ; CHECK: edge else -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
196 %result = phi i32 [ %joinresult, %join ], [ %b, %else ]
200 !3 = !{!"branch_weights", i32 100, i32 1}
202 define i32 @test_cold_call_sites(i32* %a) {
203 ; Test that edges to blocks post-dominated by cold call sites
204 ; are marked as not expected to be taken.
205 ; TODO(dnovillo) The calls to regular_function should not be merged, but
206 ; they are currently being merged. Convert this into a code generation test
207 ; after that is fixed.
209 ; CHECK: Printing analysis {{.*}} for function 'test_cold_call_sites'
210 ; CHECK: edge entry -> then probability is 0x078780e3 / 0x80000000 = 5.88%
211 ; CHECK: edge entry -> else probability is 0x78787f1d / 0x80000000 = 94.12% [HOT edge]
214 %gep1 = getelementptr i32, i32* %a, i32 1
215 %val1 = load i32, i32* %gep1
216 %cond1 = icmp ugt i32 %val1, 1
217 br i1 %cond1, label %then, label %else
220 ; This function is not declared cold, but this call site is.
221 %val4 = call i32 @regular_function(i32 %val1) cold
225 %gep2 = getelementptr i32, i32* %a, i32 2
226 %val2 = load i32, i32* %gep2
227 %val3 = call i32 @regular_function(i32 %val2)
231 %ret = phi i32 [ %val4, %then ], [ %val3, %else ]
235 ; CHECK-LABEL: test_invoke_code_callsite1
236 define i32 @test_invoke_code_callsite1(i1 %c) personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
238 br i1 %c, label %if.then, label %if.end
239 ; Edge "entry->if.end" should have higher probability based on the cold call
240 ; heuristic which treat %if.then as a cold block because the normal destination
241 ; of the invoke instruction in %if.then is post-dominated by ColdFunc().
242 ; CHECK: edge entry -> if.then probability is 0x078780e3 / 0x80000000 = 5.88%
243 ; CHECK: edge entry -> if.end probability is 0x78787f1d / 0x80000000 = 94.12% [HOT edge]
246 invoke i32 @InvokeCall()
247 to label %invoke.cont unwind label %lpad
248 ; CHECK: edge if.then -> invoke.cont probability is 0x7fff8000 / 0x80000000 = 100.00% [HOT edge]
249 ; CHECK: edge if.then -> lpad probability is 0x00008000 / 0x80000000 = 0.00%
252 call void @ColdFunc() #0
256 %ll = landingpad { i8*, i32 }
264 ; CHECK-LABEL: test_invoke_code_callsite2
265 define i32 @test_invoke_code_callsite2(i1 %c) personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
267 br i1 %c, label %if.then, label %if.end
269 ; CHECK: edge entry -> if.then probability is 0x40000000 / 0x80000000 = 50.00%
270 ; CHECK: edge entry -> if.end probability is 0x40000000 / 0x80000000 = 50.00%
272 invoke i32 @InvokeCall()
273 to label %invoke.cont unwind label %lpad
274 ; The cold call heuristic should not kick in when the cold callsite is in EH path.
275 ; CHECK: edge if.then -> invoke.cont probability is 0x7ffff800 / 0x80000000 = 100.00% [HOT edge]
276 ; CHECK: edge if.then -> lpad probability is 0x00000800 / 0x80000000 = 0.00%
282 %ll = landingpad { i8*, i32 }
284 call void @ColdFunc() #0
291 ; CHECK-LABEL: test_invoke_code_callsite3
292 define i32 @test_invoke_code_callsite3(i1 %c) personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
294 br i1 %c, label %if.then, label %if.end
295 ; CHECK: edge entry -> if.then probability is 0x078780e3 / 0x80000000 = 5.88%
296 ; CHECK: edge entry -> if.end probability is 0x78787f1d / 0x80000000 = 94.12% [HOT edge]
299 invoke i32 @InvokeCall()
300 to label %invoke.cont unwind label %lpad
301 ; Regardless of cold calls, edge weights from a invoke instruction should be
302 ; determined by the invoke heuristic.
303 ; CHECK: edge if.then -> invoke.cont probability is 0x7fff8000 / 0x80000000 = 100.00% [HOT edge]
304 ; CHECK: edge if.then -> lpad probability is 0x00008000 / 0x80000000 = 0.00%
307 call void @ColdFunc() #0
311 %ll = landingpad { i8*, i32 }
313 call void @ColdFunc() #0
320 ; CHECK-LABEL: test_invoke_code_profiled
321 define void @test_invoke_code_profiled(i1 %c) personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
323 ; CHECK: edge entry -> invoke.to0 probability is 0x7ffff800 / 0x80000000 = 100.00% [HOT edge]
324 ; CHECK: edge entry -> lpad probability is 0x00000800 / 0x80000000 = 0.00%
325 invoke i32 @InvokeCall() to label %invoke.to0 unwind label %lpad
328 ; CHECK: edge invoke.to0 -> invoke.to1 probability is 0x7ffff800 / 0x80000000 = 100.00% [HOT edge]
329 ; CHECK: edge invoke.to0 -> lpad probability is 0x00000800 / 0x80000000 = 0.00%
330 invoke i32 @InvokeCall() to label %invoke.to1 unwind label %lpad,
331 !prof !{!"branch_weights", i32 444}
334 ; CHECK: invoke.to1 -> invoke.to2 probability is 0x55555555 / 0x80000000 = 66.67%
335 ; CHECK: invoke.to1 -> lpad probability is 0x2aaaaaab / 0x80000000 = 33.33%
336 invoke i32 @InvokeCall() to label %invoke.to2 unwind label %lpad,
337 !prof !{!"branch_weights", i32 222, i32 111}
344 %ll = landingpad { i8*, i32 }
349 declare i32 @__gxx_personality_v0(...)
350 declare void @ColdFunc()
351 declare i32 @InvokeCall()
353 attributes #0 = { cold }
356 define i32 @zero1(i32 %i, i32 %a, i32 %b) {
357 ; CHECK: Printing analysis {{.*}} for function 'zero1'
359 %cond = icmp eq i32 %i, 0
360 br i1 %cond, label %then, label %else
361 ; CHECK: edge entry -> then probability is 0x30000000 / 0x80000000 = 37.50%
362 ; CHECK: edge entry -> else probability is 0x50000000 / 0x80000000 = 62.50%
371 %result = phi i32 [ %a, %then ], [ %b, %else ]
375 define i32 @zero2(i32 %i, i32 %a, i32 %b) {
376 ; CHECK: Printing analysis {{.*}} for function 'zero2'
378 %cond = icmp ne i32 %i, -1
379 br i1 %cond, label %then, label %else
380 ; CHECK: edge entry -> then probability is 0x50000000 / 0x80000000 = 62.50%
381 ; CHECK: edge entry -> else probability is 0x30000000 / 0x80000000 = 37.50%
390 %result = phi i32 [ %a, %then ], [ %b, %else ]
394 define i32 @zero3(i32 %i, i32 %a, i32 %b) {
395 ; CHECK: Printing analysis {{.*}} for function 'zero3'
397 ; AND'ing with a single bit bitmask essentially leads to a bool comparison,
398 ; meaning we don't have probability information.
400 %tobool = icmp eq i32 %and, 0
401 br i1 %tobool, label %then, label %else
402 ; CHECK: edge entry -> then probability is 0x40000000 / 0x80000000 = 50.00%
403 ; CHECK: edge entry -> else probability is 0x40000000 / 0x80000000 = 50.00%
406 ; AND'ing with other bitmask might be something else, so we still assume the
407 ; usual probabilities.
408 %and2 = and i32 %i, 5
409 %tobool2 = icmp eq i32 %and2, 0
410 br i1 %tobool2, label %else, label %exit
411 ; CHECK: edge then -> else probability is 0x30000000 / 0x80000000 = 37.50%
412 ; CHECK: edge then -> exit probability is 0x50000000 / 0x80000000 = 62.50%
418 %result = phi i32 [ %a, %then ], [ %b, %else ]
422 define i32 @test_unreachable_with_prof_greater(i32 %a, i32 %b) {
423 ; CHECK: Printing analysis {{.*}} for function 'test_unreachable_with_prof_greater'
425 %cond = icmp eq i32 %a, 42
426 br i1 %cond, label %exit, label %unr, !prof !4
428 ; CHECK: edge entry -> exit probability is 0x7fffffff / 0x80000000 = 100.00% [HOT edge]
429 ; CHECK: edge entry -> unr probability is 0x00000001 / 0x80000000 = 0.00%
438 !4 = !{!"branch_weights", i32 0, i32 1}
440 define i32 @test_unreachable_with_prof_equal(i32 %a, i32 %b) {
441 ; CHECK: Printing analysis {{.*}} for function 'test_unreachable_with_prof_equal'
443 %cond = icmp eq i32 %a, 42
444 br i1 %cond, label %exit, label %unr, !prof !5
446 ; CHECK: edge entry -> exit probability is 0x7fffffff / 0x80000000 = 100.00% [HOT edge]
447 ; CHECK: edge entry -> unr probability is 0x00000001 / 0x80000000 = 0.00%
456 !5 = !{!"branch_weights", i32 2147483647, i32 1}
458 define i32 @test_unreachable_with_prof_zero(i32 %a, i32 %b) {
459 ; CHECK: Printing analysis {{.*}} for function 'test_unreachable_with_prof_zero'
461 %cond = icmp eq i32 %a, 42
462 br i1 %cond, label %exit, label %unr, !prof !6
464 ; CHECK: edge entry -> exit probability is 0x7fffffff / 0x80000000 = 100.00% [HOT edge]
465 ; CHECK: edge entry -> unr probability is 0x00000001 / 0x80000000 = 0.00%
474 !6 = !{!"branch_weights", i32 0, i32 0}
476 define i32 @test_unreachable_with_prof_less(i32 %a, i32 %b) {
477 ; CHECK: Printing analysis {{.*}} for function 'test_unreachable_with_prof_less'
479 %cond = icmp eq i32 %a, 42
480 br i1 %cond, label %exit, label %unr, !prof !7
482 ; CHECK: edge entry -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
483 ; CHECK: edge entry -> unr probability is 0x00000000 / 0x80000000 = 0.00%
492 !7 = !{!"branch_weights", i32 1, i32 0}
494 define i32 @test_unreachable_with_switch_prof1(i32 %i, i32 %a, i32 %b, i32 %c, i32 %d, i32 %e) {
495 ; CHECK: Printing analysis {{.*}} for function 'test_unreachable_with_switch_prof1'
497 switch i32 %i, label %case_a [ i32 1, label %case_b
500 i32 4, label %case_e ], !prof !8
501 ; Reachable probabilities keep their relation: 4/64/4/4 = 5.26% / 84.21% / 5.26% / 5.26%.
502 ; CHECK: edge entry -> case_a probability is 0x00000001 / 0x80000000 = 0.00%
503 ; CHECK: edge entry -> case_b probability is 0x06bca1af / 0x80000000 = 5.26%
504 ; CHECK: edge entry -> case_c probability is 0x6bca1af3 / 0x80000000 = 84.21% [HOT edge]
505 ; CHECK: edge entry -> case_d probability is 0x06bca1af / 0x80000000 = 5.26%
506 ; CHECK: edge entry -> case_e probability is 0x06bca1af / 0x80000000 = 5.26%
513 ; CHECK: edge case_b -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
517 ; CHECK: edge case_c -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
521 ; CHECK: edge case_d -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
525 ; CHECK: edge case_e -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
528 %result = phi i32 [ %b, %case_b ],
535 !8 = !{!"branch_weights", i32 4, i32 4, i32 64, i32 4, i32 4}
537 define i32 @test_unreachable_with_switch_prof2(i32 %i, i32 %a, i32 %b, i32 %c, i32 %d, i32 %e) {
538 ; CHECK: Printing analysis {{.*}} for function 'test_unreachable_with_switch_prof2'
540 switch i32 %i, label %case_a [ i32 1, label %case_b
543 i32 4, label %case_e ], !prof !9
544 ; Reachable probabilities keep their relation: 64/4/4 = 88.89% / 5.56% / 5.56%.
545 ; CHECK: edge entry -> case_a probability is 0x00000001 / 0x80000000 = 0.00%
546 ; CHECK: edge entry -> case_b probability is 0x00000001 / 0x80000000 = 0.00%
547 ; CHECK: edge entry -> case_c probability is 0x71c71c71 / 0x80000000 = 88.89% [HOT edge]
548 ; CHECK: edge entry -> case_d probability is 0x071c71c7 / 0x80000000 = 5.56%
549 ; CHECK: edge entry -> case_e probability is 0x071c71c7 / 0x80000000 = 5.56%
560 ; CHECK: edge case_c -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
564 ; CHECK: edge case_d -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
568 ; CHECK: edge case_e -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
571 %result = phi i32 [ %c, %case_c ],
577 !9 = !{!"branch_weights", i32 4, i32 4, i32 64, i32 4, i32 4}
579 define i32 @test_unreachable_with_switch_prof3(i32 %i, i32 %a, i32 %b, i32 %c, i32 %d, i32 %e) {
580 ; CHECK: Printing analysis {{.*}} for function 'test_unreachable_with_switch_prof3'
582 switch i32 %i, label %case_a [ i32 1, label %case_b
585 i32 4, label %case_e ], !prof !10
586 ; Reachable probabilities keep their relation: 64/4/4 = 88.89% / 5.56% / 5.56%.
587 ; CHECK: edge entry -> case_a probability is 0x00000000 / 0x80000000 = 0.00%
588 ; CHECK: edge entry -> case_b probability is 0x00000001 / 0x80000000 = 0.00%
589 ; CHECK: edge entry -> case_c probability is 0x71c71c71 / 0x80000000 = 88.89% [HOT edge]
590 ; CHECK: edge entry -> case_d probability is 0x071c71c7 / 0x80000000 = 5.56%
591 ; CHECK: edge entry -> case_e probability is 0x071c71c7 / 0x80000000 = 5.56%
601 ; CHECK: edge case_c -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
605 ; CHECK: edge case_d -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
609 ; CHECK: edge case_e -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
612 %result = phi i32 [ %c, %case_c ],
618 !10 = !{!"branch_weights", i32 0, i32 4, i32 64, i32 4, i32 4}
620 define i32 @test_unreachable_with_switch_prof4(i32 %i, i32 %a, i32 %b, i32 %c, i32 %d, i32 %e) {
621 ; CHECK: Printing analysis {{.*}} for function 'test_unreachable_with_switch_prof4'
623 switch i32 %i, label %case_a [ i32 1, label %case_b
626 i32 4, label %case_e ], !prof !11
627 ; CHECK: edge entry -> case_a probability is 0x1999999a / 0x80000000 = 20.00%
628 ; CHECK: edge entry -> case_b probability is 0x1999999a / 0x80000000 = 20.00%
629 ; CHECK: edge entry -> case_c probability is 0x1999999a / 0x80000000 = 20.00%
630 ; CHECK: edge entry -> case_d probability is 0x1999999a / 0x80000000 = 20.00%
631 ; CHECK: edge entry -> case_e probability is 0x1999999a / 0x80000000 = 20.00%
650 !11 = !{!"branch_weights", i32 0, i32 4, i32 64, i32 4, i32 4}