[PowerPC] Recommit r314244 with refactoring and off by default
[llvm-core.git] / test / CodeGen / X86 / block-placement.ll
blobd4948892a31da24554369bc6d1258dcb548fc55b
1 ; RUN: llc -mtriple=i686-linux -pre-RA-sched=source < %s | FileCheck %s
3 declare void @error(i32 %i, i32 %a, i32 %b)
5 define i32 @test_ifchains(i32 %i, i32* %a, i32 %b) {
6 ; Test a chain of ifs, where the block guarded by the if is error handling code
7 ; that is not expected to run.
8 ; CHECK-LABEL: test_ifchains:
9 ; CHECK: %entry
10 ; CHECK-NOT: .p2align
11 ; CHECK: %else1
12 ; CHECK-NOT: .p2align
13 ; CHECK: %else2
14 ; CHECK-NOT: .p2align
15 ; CHECK: %else3
16 ; CHECK-NOT: .p2align
17 ; CHECK: %else4
18 ; CHECK-NOT: .p2align
19 ; CHECK: %exit
20 ; CHECK: %then1
21 ; CHECK: %then2
22 ; CHECK: %then3
23 ; CHECK: %then4
24 ; CHECK: %then5
26 entry:
27   %gep1 = getelementptr i32, i32* %a, i32 1
28   %val1 = load i32, i32* %gep1
29   %cond1 = icmp ugt i32 %val1, 1
30   br i1 %cond1, label %then1, label %else1, !prof !0
32 then1:
33   call void @error(i32 %i, i32 1, i32 %b)
34   br label %else1
36 else1:
37   %gep2 = getelementptr i32, i32* %a, i32 2
38   %val2 = load i32, i32* %gep2
39   %cond2 = icmp ugt i32 %val2, 2
40   br i1 %cond2, label %then2, label %else2, !prof !0
42 then2:
43   call void @error(i32 %i, i32 1, i32 %b)
44   br label %else2
46 else2:
47   %gep3 = getelementptr i32, i32* %a, i32 3
48   %val3 = load i32, i32* %gep3
49   %cond3 = icmp ugt i32 %val3, 3
50   br i1 %cond3, label %then3, label %else3, !prof !0
52 then3:
53   call void @error(i32 %i, i32 1, i32 %b)
54   br label %else3
56 else3:
57   %gep4 = getelementptr i32, i32* %a, i32 4
58   %val4 = load i32, i32* %gep4
59   %cond4 = icmp ugt i32 %val4, 4
60   br i1 %cond4, label %then4, label %else4, !prof !0
62 then4:
63   call void @error(i32 %i, i32 1, i32 %b)
64   br label %else4
66 else4:
67   %gep5 = getelementptr i32, i32* %a, i32 3
68   %val5 = load i32, i32* %gep5
69   %cond5 = icmp ugt i32 %val5, 3
70   br i1 %cond5, label %then5, label %exit, !prof !0
72 then5:
73   call void @error(i32 %i, i32 1, i32 %b)
74   br label %exit
76 exit:
77   ret i32 %b
80 define i32 @test_loop_cold_blocks(i32 %i, i32* %a) {
81 ; Check that we sink cold loop blocks after the hot loop body.
82 ; CHECK-LABEL: test_loop_cold_blocks:
83 ; CHECK: %entry
84 ; CHECK-NOT: .p2align
85 ; CHECK: %unlikely1
86 ; CHECK-NOT: .p2align
87 ; CHECK: %unlikely2
88 ; CHECK: .p2align
89 ; CHECK: %body1
90 ; CHECK: %body2
91 ; CHECK: %body3
92 ; CHECK: %exit
94 entry:
95   br label %body1
97 body1:
98   %iv = phi i32 [ 0, %entry ], [ %next, %body3 ]
99   %base = phi i32 [ 0, %entry ], [ %sum, %body3 ]
100   %unlikelycond1 = icmp slt i32 %base, 42
101   br i1 %unlikelycond1, label %unlikely1, label %body2, !prof !0
103 unlikely1:
104   call void @error(i32 %i, i32 1, i32 %base)
105   br label %body2
107 body2:
108   %unlikelycond2 = icmp sgt i32 %base, 21
109   br i1 %unlikelycond2, label %unlikely2, label %body3, !prof !0
111 unlikely2:
112   call void @error(i32 %i, i32 2, i32 %base)
113   br label %body3
115 body3:
116   %arrayidx = getelementptr inbounds i32, i32* %a, i32 %iv
117   %0 = load i32, i32* %arrayidx
118   %sum = add nsw i32 %0, %base
119   %next = add i32 %iv, 1
120   %exitcond = icmp eq i32 %next, %i
121   br i1 %exitcond, label %exit, label %body1
123 exit:
124   ret i32 %sum
127 !0 = !{!"branch_weights", i32 4, i32 64}
129 define i32 @test_loop_early_exits(i32 %i, i32* %a) {
130 ; Check that we sink early exit blocks out of loop bodies.
131 ; CHECK-LABEL: test_loop_early_exits:
132 ; CHECK: %entry
133 ; CHECK: %body1
134 ; CHECK: %body2
135 ; CHECK: %body3
136 ; CHECK: %body4
137 ; CHECK: %exit
138 ; CHECK: %bail1
139 ; CHECK: %bail2
140 ; CHECK: %bail3
142 entry:
143   br label %body1
145 body1:
146   %iv = phi i32 [ 0, %entry ], [ %next, %body4 ]
147   %base = phi i32 [ 0, %entry ], [ %sum, %body4 ]
148   %bailcond1 = icmp eq i32 %base, 42
149   br i1 %bailcond1, label %bail1, label %body2
151 bail1:
152   ret i32 -1
154 body2:
155   %bailcond2 = icmp eq i32 %base, 43
156   br i1 %bailcond2, label %bail2, label %body3
158 bail2:
159   ret i32 -2
161 body3:
162   %bailcond3 = icmp eq i32 %base, 44
163   br i1 %bailcond3, label %bail3, label %body4
165 bail3:
166   ret i32 -3
168 body4:
169   %arrayidx = getelementptr inbounds i32, i32* %a, i32 %iv
170   %0 = load i32, i32* %arrayidx
171   %sum = add nsw i32 %0, %base
172   %next = add i32 %iv, 1
173   %exitcond = icmp eq i32 %next, %i
174   br i1 %exitcond, label %exit, label %body1
176 exit:
177   ret i32 %sum
180 ; Tail duplication during layout can entirely remove body0 by duplicating it
181 ; into the entry block and into body1. This is a good thing but it isn't what
182 ; this test is looking for. So to make the blocks longer so they don't get
183 ; duplicated, we add some calls to dummy.
184 declare void @dummy()
186 define i32 @test_loop_rotate(i32 %i, i32* %a) {
187 ; Check that we rotate conditional exits from the loop to the bottom of the
188 ; loop, eliminating unconditional branches to the top.
189 ; CHECK-LABEL: test_loop_rotate:
190 ; CHECK: %entry
191 ; CHECK: %body1
192 ; CHECK: %body0
193 ; CHECK: %exit
195 entry:
196   br label %body0
198 body0:
199   %iv = phi i32 [ 0, %entry ], [ %next, %body1 ]
200   %base = phi i32 [ 0, %entry ], [ %sum, %body1 ]
201   %next = add i32 %iv, 1
202   %exitcond = icmp eq i32 %next, %i
203   call void @dummy()
204   call void @dummy()
205   br i1 %exitcond, label %exit, label %body1
207 body1:
208   %arrayidx = getelementptr inbounds i32, i32* %a, i32 %iv
209   %0 = load i32, i32* %arrayidx
210   %sum = add nsw i32 %0, %base
211   %bailcond1 = icmp eq i32 %sum, 42
212   br label %body0
214 exit:
215   ret i32 %base
218 define i32 @test_no_loop_rotate(i32 %i, i32* %a) {
219 ; Check that we don't try to rotate a loop which is already laid out with
220 ; fallthrough opportunities into the top and out of the bottom.
221 ; CHECK-LABEL: test_no_loop_rotate:
222 ; CHECK: %entry
223 ; CHECK: %body0
224 ; CHECK: %body1
225 ; CHECK: %exit
227 entry:
228   br label %body0
230 body0:
231   %iv = phi i32 [ 0, %entry ], [ %next, %body1 ]
232   %base = phi i32 [ 0, %entry ], [ %sum, %body1 ]
233   %arrayidx = getelementptr inbounds i32, i32* %a, i32 %iv
234   %0 = load i32, i32* %arrayidx
235   %sum = add nsw i32 %0, %base
236   %bailcond1 = icmp eq i32 %sum, 42
237   br i1 %bailcond1, label %exit, label %body1
239 body1:
240   %next = add i32 %iv, 1
241   %exitcond = icmp eq i32 %next, %i
242   br i1 %exitcond, label %exit, label %body0
244 exit:
245   ret i32 %base
248 define i32 @test_loop_align(i32 %i, i32* %a) {
249 ; Check that we provide basic loop body alignment with the block placement
250 ; pass.
251 ; CHECK-LABEL: test_loop_align:
252 ; CHECK: %entry
253 ; CHECK: .p2align [[ALIGN:[0-9]+]],
254 ; CHECK-NEXT: %body
255 ; CHECK: %exit
257 entry:
258   br label %body
260 body:
261   %iv = phi i32 [ 0, %entry ], [ %next, %body ]
262   %base = phi i32 [ 0, %entry ], [ %sum, %body ]
263   %arrayidx = getelementptr inbounds i32, i32* %a, i32 %iv
264   %0 = load i32, i32* %arrayidx
265   %sum = add nsw i32 %0, %base
266   %next = add i32 %iv, 1
267   %exitcond = icmp eq i32 %next, %i
268   br i1 %exitcond, label %exit, label %body
270 exit:
271   ret i32 %sum
274 define i32 @test_nested_loop_align(i32 %i, i32* %a, i32* %b) {
275 ; Check that we provide nested loop body alignment.
276 ; CHECK-LABEL: test_nested_loop_align:
277 ; CHECK: %entry
278 ; CHECK: .p2align [[ALIGN]],
279 ; CHECK-NEXT: %loop.body.1
280 ; CHECK: .p2align [[ALIGN]],
281 ; CHECK-NEXT: %inner.loop.body
282 ; CHECK-NOT: .p2align
283 ; CHECK: %exit
285 entry:
286   br label %loop.body.1
288 loop.body.1:
289   %iv = phi i32 [ 0, %entry ], [ %next, %loop.body.2 ]
290   %arrayidx = getelementptr inbounds i32, i32* %a, i32 %iv
291   %bidx = load i32, i32* %arrayidx
292   br label %inner.loop.body
294 inner.loop.body:
295   %inner.iv = phi i32 [ 0, %loop.body.1 ], [ %inner.next, %inner.loop.body ]
296   %base = phi i32 [ 0, %loop.body.1 ], [ %sum, %inner.loop.body ]
297   %scaled_idx = mul i32 %bidx, %iv
298   %inner.arrayidx = getelementptr inbounds i32, i32* %b, i32 %scaled_idx
299   %0 = load i32, i32* %inner.arrayidx
300   %sum = add nsw i32 %0, %base
301   %inner.next = add i32 %iv, 1
302   %inner.exitcond = icmp eq i32 %inner.next, %i
303   br i1 %inner.exitcond, label %loop.body.2, label %inner.loop.body
305 loop.body.2:
306   %next = add i32 %iv, 1
307   %exitcond = icmp eq i32 %next, %i
308   br i1 %exitcond, label %exit, label %loop.body.1
310 exit:
311   ret i32 %sum
314 define void @unnatural_cfg1() {
315 ; Test that we can handle a loop with an inner unnatural loop at the end of
316 ; a function. This is a gross CFG reduced out of the single source GCC.
317 ; CHECK-LABEL: unnatural_cfg1
318 ; CHECK: %entry
319 ; CHECK: %loop.body1
320 ; CHECK: %loop.body2
321 ; CHECK: %loop.body3
323 entry:
324   br label %loop.header
326 loop.header:
327   br label %loop.body1
329 loop.body1:
330   br i1 undef, label %loop.body3, label %loop.body2
332 loop.body2:
333   %ptr = load i32*, i32** undef, align 4
334   br label %loop.body3
336 loop.body3:
337   %myptr = phi i32* [ %ptr2, %loop.body5 ], [ %ptr, %loop.body2 ], [ undef, %loop.body1 ]
338   %bcmyptr = bitcast i32* %myptr to i32*
339   %val = load i32, i32* %bcmyptr, align 4
340   %comp = icmp eq i32 %val, 48
341   br i1 %comp, label %loop.body4, label %loop.body5
343 loop.body4:
344   br i1 undef, label %loop.header, label %loop.body5
346 loop.body5:
347   %ptr2 = load i32*, i32** undef, align 4
348   br label %loop.body3
351 define void @unnatural_cfg2() {
352 ; Test that we can handle a loop with a nested natural loop *and* an unnatural
353 ; loop. This was reduced from a crash on block placement when run over
354 ; single-source GCC.
355 ; CHECK-LABEL: unnatural_cfg2
356 ; CHECK: %entry
357 ; CHECK: %loop.header
358 ; CHECK: %loop.body1
359 ; CHECK: %loop.body2
360 ; CHECK: %loop.body4
361 ; CHECK: %loop.inner2.begin
362 ; CHECK: %loop.inner2.begin
363 ; CHECK: %loop.body3
364 ; CHECK: %loop.inner1.begin
365 ; CHECK: %bail
367 entry:
368   br label %loop.header
370 loop.header:
371   %comp0 = icmp eq i32* undef, null
372   br i1 %comp0, label %bail, label %loop.body1
374 loop.body1:
375   %val0 = load i32*, i32** undef, align 4
376   br i1 undef, label %loop.body2, label %loop.inner1.begin
378 loop.body2:
379   br i1 undef, label %loop.body4, label %loop.body3
381 loop.body3:
382   %ptr1 = getelementptr inbounds i32, i32* %val0, i32 0
383   %castptr1 = bitcast i32* %ptr1 to i32**
384   %val1 = load i32*, i32** %castptr1, align 4
385   br label %loop.inner1.begin
387 loop.inner1.begin:
388   %valphi = phi i32* [ %val2, %loop.inner1.end ], [ %val1, %loop.body3 ], [ %val0, %loop.body1 ]
389   %castval = bitcast i32* %valphi to i32*
390   %comp1 = icmp eq i32 undef, 48
391   br i1 %comp1, label %loop.inner1.end, label %loop.body4
393 loop.inner1.end:
394   %ptr2 = getelementptr inbounds i32, i32* %valphi, i32 0
395   %castptr2 = bitcast i32* %ptr2 to i32**
396   %val2 = load i32*, i32** %castptr2, align 4
397   br label %loop.inner1.begin
399 loop.body4.dead:
400   br label %loop.body4
402 loop.body4:
403   %comp2 = icmp ult i32 undef, 3
404   br i1 %comp2, label %loop.inner2.begin, label %loop.end
406 loop.inner2.begin:
407   br i1 false, label %loop.end, label %loop.inner2.end
409 loop.inner2.end:
410   %comp3 = icmp eq i32 undef, 1769472
411   br i1 %comp3, label %loop.end, label %loop.inner2.begin
413 loop.end:
414   br label %loop.header
416 bail:
417   unreachable
420 define i32 @problematic_switch() {
421 ; This function's CFG caused overlow in the machine branch probability
422 ; calculation, triggering asserts. Make sure we don't crash on it.
423 ; CHECK: problematic_switch
425 entry:
426   switch i32 undef, label %exit [
427     i32 879, label %bogus
428     i32 877, label %step
429     i32 876, label %step
430     i32 875, label %step
431     i32 874, label %step
432     i32 873, label %step
433     i32 872, label %step
434     i32 868, label %step
435     i32 867, label %step
436     i32 866, label %step
437     i32 861, label %step
438     i32 860, label %step
439     i32 856, label %step
440     i32 855, label %step
441     i32 854, label %step
442     i32 831, label %step
443     i32 830, label %step
444     i32 829, label %step
445     i32 828, label %step
446     i32 815, label %step
447     i32 814, label %step
448     i32 811, label %step
449     i32 806, label %step
450     i32 805, label %step
451     i32 804, label %step
452     i32 803, label %step
453     i32 802, label %step
454     i32 801, label %step
455     i32 800, label %step
456     i32 799, label %step
457     i32 798, label %step
458     i32 797, label %step
459     i32 796, label %step
460     i32 795, label %step
461   ]
462 bogus:
463   unreachable
464 step:
465   br label %exit
466 exit:
467   %merge = phi i32 [ 3, %step ], [ 6, %entry ]
468   ret i32 %merge
471 define void @fpcmp_unanalyzable_branch(i1 %cond) {
472 ; This function's CFG contains an once-unanalyzable branch (une on floating
473 ; points). As now it becomes analyzable, we should get best layout in which each
474 ; edge in 'entry' -> 'entry.if.then_crit_edge' -> 'if.then' -> 'if.end' is
475 ; fall-through.
476 ; CHECK-LABEL: fpcmp_unanalyzable_branch:
477 ; CHECK:       # BB#0: # %entry
478 ; CHECK:       # BB#1: # %entry.if.then_crit_edge
479 ; CHECK:       .LBB10_5: # %if.then
480 ; CHECK:       .LBB10_6: # %if.end
481 ; CHECK:       # BB#3: # %exit
482 ; CHECK:       jne .LBB10_4
483 ; CHECK-NEXT:  jnp .LBB10_6
484 ; CHECK:       jmp .LBB10_5
486 entry:
487 ; Note that this branch must be strongly biased toward
488 ; 'entry.if.then_crit_edge' to ensure that we would try to form a chain for
489 ; 'entry' -> 'entry.if.then_crit_edge' -> 'if.then' -> 'if.end'.
490   br i1 %cond, label %entry.if.then_crit_edge, label %lor.lhs.false, !prof !1
492 entry.if.then_crit_edge:
493   %.pre14 = load i8, i8* undef, align 1
494   br label %if.then
496 lor.lhs.false:
497   br i1 undef, label %if.end, label %exit
499 exit:
500   %cmp.i = fcmp une double 0.000000e+00, undef
501   br i1 %cmp.i, label %if.then, label %if.end, !prof !3
503 if.then:
504   %0 = phi i8 [ %.pre14, %entry.if.then_crit_edge ], [ undef, %exit ]
505   %1 = and i8 %0, 1
506   store i8 %1, i8* undef, align 4
507   br label %if.end
509 if.end:
510   ret void
513 !1 = !{!"branch_weights", i32 1000, i32 1}
514 !3 = !{!"branch_weights", i32 1, i32 1000}
516 declare i32 @f()
517 declare i32 @g()
518 declare i32 @h(i32 %x)
520 define i32 @test_global_cfg_break_profitability() {
521 ; Check that our metrics for the profitability of a CFG break are global rather
522 ; than local. A successor may be very hot, but if the current block isn't, it
523 ; doesn't matter. Within this test the 'then' block is slightly warmer than the
524 ; 'else' block, but not nearly enough to merit merging it with the exit block
525 ; even though the probability of 'then' branching to the 'exit' block is very
526 ; high.
527 ; CHECK: test_global_cfg_break_profitability
528 ; CHECK: calll {{_?}}f
529 ; CHECK: calll {{_?}}g
530 ; CHECK: calll {{_?}}h
531 ; CHECK: ret
533 entry:
534   br i1 undef, label %then, label %else, !prof !2
536 then:
537   %then.result = call i32 @f()
538   br label %exit
540 else:
541   %else.result = call i32 @g()
542   br label %exit
544 exit:
545   %result = phi i32 [ %then.result, %then ], [ %else.result, %else ]
546   %result2 = call i32 @h(i32 %result)
547   ret i32 %result
550 !2 = !{!"branch_weights", i32 3, i32 1}
552 declare i32 @__gxx_personality_v0(...)
554 define void @test_eh_lpad_successor() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
555 ; Some times the landing pad ends up as the first successor of an invoke block.
556 ; When this happens, a strange result used to fall out of updateTerminators: we
557 ; didn't correctly locate the fallthrough successor, assuming blindly that the
558 ; first one was the fallthrough successor. As a result, we would add an
559 ; erroneous jump to the landing pad thinking *that* was the default successor.
560 ; CHECK-LABEL: test_eh_lpad_successor
561 ; CHECK: %entry
562 ; CHECK-NOT: jmp
563 ; CHECK: %loop
565 entry:
566   invoke i32 @f() to label %preheader unwind label %lpad
568 preheader:
569   br label %loop
571 lpad:
572   %lpad.val = landingpad { i8*, i32 }
573           cleanup
574   resume { i8*, i32 } %lpad.val
576 loop:
577   br label %loop
580 declare void @fake_throw() noreturn
582 define void @test_eh_throw() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
583 ; For blocks containing a 'throw' (or similar functionality), we have
584 ; a no-return invoke. In this case, only EH successors will exist, and
585 ; fallthrough simply won't occur. Make sure we don't crash trying to update
586 ; terminators for such constructs.
588 ; CHECK-LABEL: test_eh_throw
589 ; CHECK: %entry
590 ; CHECK: %cleanup
592 entry:
593   invoke void @fake_throw() to label %continue unwind label %cleanup
595 continue:
596   unreachable
598 cleanup:
599   %0 = landingpad { i8*, i32 }
600           cleanup
601   unreachable
604 define void @test_unnatural_cfg_backwards_inner_loop() {
605 ; Test that when we encounter an unnatural CFG structure after having formed
606 ; a chain for an inner loop which happened to be laid out backwards we don't
607 ; attempt to merge onto the wrong end of the inner loop just because we find it
608 ; first. This was reduced from a crasher in GCC's single source.
610 ; CHECK-LABEL: test_unnatural_cfg_backwards_inner_loop
611 ; CHECK: %entry
612 ; CHECK: %loop2b
613 ; CHECK: %loop1
615 entry:
616   br i1 undef, label %loop2a, label %body
618 body:
619   br label %loop2a
621 loop1:
622   %next.load = load i32*, i32** undef
623   br i1 %comp.a, label %loop2a, label %loop2b
625 loop2a:
626   %var = phi i32* [ null, %entry ], [ null, %body ], [ %next.phi, %loop1 ]
627   %next.var = phi i32* [ null, %entry ], [ undef, %body ], [ %next.load, %loop1 ]
628   %comp.a = icmp eq i32* %var, null
629   br label %loop3
631 loop2b:
632   %gep = getelementptr inbounds i32, i32* %var.phi, i32 0
633   %next.ptr = bitcast i32* %gep to i32**
634   store i32* %next.phi, i32** %next.ptr
635   br label %loop3
637 loop3:
638   %var.phi = phi i32* [ %next.phi, %loop2b ], [ %var, %loop2a ]
639   %next.phi = phi i32* [ %next.load, %loop2b ], [ %next.var, %loop2a ]
640   br label %loop1
643 define void @unanalyzable_branch_to_loop_header() {
644 ; Ensure that we can handle unanalyzable branches into loop headers. We
645 ; pre-form chains for unanalyzable branches, and will find the tail end of that
646 ; at the start of the loop. This function uses floating point comparison
647 ; fallthrough because that happens to always produce unanalyzable branches on
648 ; x86.
650 ; CHECK-LABEL: unanalyzable_branch_to_loop_header
651 ; CHECK: %entry
652 ; CHECK: %loop
653 ; CHECK: %exit
655 entry:
656   %cmp = fcmp une double 0.000000e+00, undef
657   br i1 %cmp, label %loop, label %exit
659 loop:
660   %cond = icmp eq i8 undef, 42
661   br i1 %cond, label %exit, label %loop
663 exit:
664   ret void
667 define void @unanalyzable_branch_to_best_succ(i1 %cond) {
668 ; Ensure that we can handle unanalyzable branches where the destination block
669 ; gets selected as the optimal successor to merge.
671 ; This branch is now analyzable and hence the destination block becomes the
672 ; hotter one. The right order is entry->bar->exit->foo.
674 ; CHECK-LABEL: unanalyzable_branch_to_best_succ
675 ; CHECK: %entry
676 ; CHECK: %bar
677 ; CHECK: %exit
678 ; CHECK: %foo
680 entry:
681   ; Bias this branch toward bar to ensure we form that chain.
682   br i1 %cond, label %bar, label %foo, !prof !1
684 foo:
685   %cmp = fcmp une double 0.000000e+00, undef
686   br i1 %cmp, label %bar, label %exit
688 bar:
689   call i32 @f()
690   br label %exit
692 exit:
693   ret void
696 define void @unanalyzable_branch_to_free_block(float %x) {
697 ; Ensure that we can handle unanalyzable branches where the destination block
698 ; gets selected as the best free block in the CFG.
700 ; CHECK-LABEL: unanalyzable_branch_to_free_block
701 ; CHECK: %entry
702 ; CHECK: %a
703 ; CHECK: %b
704 ; CHECK: %c
705 ; CHECK: %exit
707 entry:
708   br i1 undef, label %a, label %b
711   call i32 @f()
712   br label %c
715   %cmp = fcmp une float %x, undef
716   br i1 %cmp, label %c, label %exit
719   call i32 @g()
720   br label %exit
722 exit:
723   ret void
726 define void @many_unanalyzable_branches() {
727 ; Ensure that we don't crash as we're building up many unanalyzable branches,
728 ; blocks, and loops.
730 ; CHECK-LABEL: many_unanalyzable_branches
731 ; CHECK: %entry
732 ; CHECK: %exit
734 entry:
735   br label %0
737   %val0 = load volatile float, float* undef
738   %cmp0 = fcmp une float %val0, undef
739   br i1 %cmp0, label %1, label %0
740   %val1 = load volatile float, float* undef
741   %cmp1 = fcmp une float %val1, undef
742   br i1 %cmp1, label %2, label %1
743   %val2 = load volatile float, float* undef
744   %cmp2 = fcmp une float %val2, undef
745   br i1 %cmp2, label %3, label %2
746   %val3 = load volatile float, float* undef
747   %cmp3 = fcmp une float %val3, undef
748   br i1 %cmp3, label %4, label %3
749   %val4 = load volatile float, float* undef
750   %cmp4 = fcmp une float %val4, undef
751   br i1 %cmp4, label %5, label %4
752   %val5 = load volatile float, float* undef
753   %cmp5 = fcmp une float %val5, undef
754   br i1 %cmp5, label %6, label %5
755   %val6 = load volatile float, float* undef
756   %cmp6 = fcmp une float %val6, undef
757   br i1 %cmp6, label %7, label %6
758   %val7 = load volatile float, float* undef
759   %cmp7 = fcmp une float %val7, undef
760   br i1 %cmp7, label %8, label %7
761   %val8 = load volatile float, float* undef
762   %cmp8 = fcmp une float %val8, undef
763   br i1 %cmp8, label %9, label %8
764   %val9 = load volatile float, float* undef
765   %cmp9 = fcmp une float %val9, undef
766   br i1 %cmp9, label %10, label %9
767   %val10 = load volatile float, float* undef
768   %cmp10 = fcmp une float %val10, undef
769   br i1 %cmp10, label %11, label %10
770   %val11 = load volatile float, float* undef
771   %cmp11 = fcmp une float %val11, undef
772   br i1 %cmp11, label %12, label %11
773   %val12 = load volatile float, float* undef
774   %cmp12 = fcmp une float %val12, undef
775   br i1 %cmp12, label %13, label %12
776   %val13 = load volatile float, float* undef
777   %cmp13 = fcmp une float %val13, undef
778   br i1 %cmp13, label %14, label %13
779   %val14 = load volatile float, float* undef
780   %cmp14 = fcmp une float %val14, undef
781   br i1 %cmp14, label %15, label %14
782   %val15 = load volatile float, float* undef
783   %cmp15 = fcmp une float %val15, undef
784   br i1 %cmp15, label %16, label %15
785   %val16 = load volatile float, float* undef
786   %cmp16 = fcmp une float %val16, undef
787   br i1 %cmp16, label %17, label %16
788   %val17 = load volatile float, float* undef
789   %cmp17 = fcmp une float %val17, undef
790   br i1 %cmp17, label %18, label %17
791   %val18 = load volatile float, float* undef
792   %cmp18 = fcmp une float %val18, undef
793   br i1 %cmp18, label %19, label %18
794   %val19 = load volatile float, float* undef
795   %cmp19 = fcmp une float %val19, undef
796   br i1 %cmp19, label %20, label %19
797   %val20 = load volatile float, float* undef
798   %cmp20 = fcmp une float %val20, undef
799   br i1 %cmp20, label %21, label %20
800   %val21 = load volatile float, float* undef
801   %cmp21 = fcmp une float %val21, undef
802   br i1 %cmp21, label %22, label %21
803   %val22 = load volatile float, float* undef
804   %cmp22 = fcmp une float %val22, undef
805   br i1 %cmp22, label %23, label %22
806   %val23 = load volatile float, float* undef
807   %cmp23 = fcmp une float %val23, undef
808   br i1 %cmp23, label %24, label %23
809   %val24 = load volatile float, float* undef
810   %cmp24 = fcmp une float %val24, undef
811   br i1 %cmp24, label %25, label %24
812   %val25 = load volatile float, float* undef
813   %cmp25 = fcmp une float %val25, undef
814   br i1 %cmp25, label %26, label %25
815   %val26 = load volatile float, float* undef
816   %cmp26 = fcmp une float %val26, undef
817   br i1 %cmp26, label %27, label %26
818   %val27 = load volatile float, float* undef
819   %cmp27 = fcmp une float %val27, undef
820   br i1 %cmp27, label %28, label %27
821   %val28 = load volatile float, float* undef
822   %cmp28 = fcmp une float %val28, undef
823   br i1 %cmp28, label %29, label %28
824   %val29 = load volatile float, float* undef
825   %cmp29 = fcmp une float %val29, undef
826   br i1 %cmp29, label %30, label %29
827   %val30 = load volatile float, float* undef
828   %cmp30 = fcmp une float %val30, undef
829   br i1 %cmp30, label %31, label %30
830   %val31 = load volatile float, float* undef
831   %cmp31 = fcmp une float %val31, undef
832   br i1 %cmp31, label %32, label %31
833   %val32 = load volatile float, float* undef
834   %cmp32 = fcmp une float %val32, undef
835   br i1 %cmp32, label %33, label %32
836   %val33 = load volatile float, float* undef
837   %cmp33 = fcmp une float %val33, undef
838   br i1 %cmp33, label %34, label %33
839   %val34 = load volatile float, float* undef
840   %cmp34 = fcmp une float %val34, undef
841   br i1 %cmp34, label %35, label %34
842   %val35 = load volatile float, float* undef
843   %cmp35 = fcmp une float %val35, undef
844   br i1 %cmp35, label %36, label %35
845   %val36 = load volatile float, float* undef
846   %cmp36 = fcmp une float %val36, undef
847   br i1 %cmp36, label %37, label %36
848   %val37 = load volatile float, float* undef
849   %cmp37 = fcmp une float %val37, undef
850   br i1 %cmp37, label %38, label %37
851   %val38 = load volatile float, float* undef
852   %cmp38 = fcmp une float %val38, undef
853   br i1 %cmp38, label %39, label %38
854   %val39 = load volatile float, float* undef
855   %cmp39 = fcmp une float %val39, undef
856   br i1 %cmp39, label %40, label %39
857   %val40 = load volatile float, float* undef
858   %cmp40 = fcmp une float %val40, undef
859   br i1 %cmp40, label %41, label %40
860   %val41 = load volatile float, float* undef
861   %cmp41 = fcmp une float %val41, undef
862   br i1 %cmp41, label %42, label %41
863   %val42 = load volatile float, float* undef
864   %cmp42 = fcmp une float %val42, undef
865   br i1 %cmp42, label %43, label %42
866   %val43 = load volatile float, float* undef
867   %cmp43 = fcmp une float %val43, undef
868   br i1 %cmp43, label %44, label %43
869   %val44 = load volatile float, float* undef
870   %cmp44 = fcmp une float %val44, undef
871   br i1 %cmp44, label %45, label %44
872   %val45 = load volatile float, float* undef
873   %cmp45 = fcmp une float %val45, undef
874   br i1 %cmp45, label %46, label %45
875   %val46 = load volatile float, float* undef
876   %cmp46 = fcmp une float %val46, undef
877   br i1 %cmp46, label %47, label %46
878   %val47 = load volatile float, float* undef
879   %cmp47 = fcmp une float %val47, undef
880   br i1 %cmp47, label %48, label %47
881   %val48 = load volatile float, float* undef
882   %cmp48 = fcmp une float %val48, undef
883   br i1 %cmp48, label %49, label %48
884   %val49 = load volatile float, float* undef
885   %cmp49 = fcmp une float %val49, undef
886   br i1 %cmp49, label %50, label %49
887   %val50 = load volatile float, float* undef
888   %cmp50 = fcmp une float %val50, undef
889   br i1 %cmp50, label %51, label %50
890   %val51 = load volatile float, float* undef
891   %cmp51 = fcmp une float %val51, undef
892   br i1 %cmp51, label %52, label %51
893   %val52 = load volatile float, float* undef
894   %cmp52 = fcmp une float %val52, undef
895   br i1 %cmp52, label %53, label %52
896   %val53 = load volatile float, float* undef
897   %cmp53 = fcmp une float %val53, undef
898   br i1 %cmp53, label %54, label %53
899   %val54 = load volatile float, float* undef
900   %cmp54 = fcmp une float %val54, undef
901   br i1 %cmp54, label %55, label %54
902   %val55 = load volatile float, float* undef
903   %cmp55 = fcmp une float %val55, undef
904   br i1 %cmp55, label %56, label %55
905   %val56 = load volatile float, float* undef
906   %cmp56 = fcmp une float %val56, undef
907   br i1 %cmp56, label %57, label %56
908   %val57 = load volatile float, float* undef
909   %cmp57 = fcmp une float %val57, undef
910   br i1 %cmp57, label %58, label %57
911   %val58 = load volatile float, float* undef
912   %cmp58 = fcmp une float %val58, undef
913   br i1 %cmp58, label %59, label %58
914   %val59 = load volatile float, float* undef
915   %cmp59 = fcmp une float %val59, undef
916   br i1 %cmp59, label %60, label %59
917   %val60 = load volatile float, float* undef
918   %cmp60 = fcmp une float %val60, undef
919   br i1 %cmp60, label %61, label %60
920   %val61 = load volatile float, float* undef
921   %cmp61 = fcmp une float %val61, undef
922   br i1 %cmp61, label %62, label %61
923   %val62 = load volatile float, float* undef
924   %cmp62 = fcmp une float %val62, undef
925   br i1 %cmp62, label %63, label %62
926   %val63 = load volatile float, float* undef
927   %cmp63 = fcmp une float %val63, undef
928   br i1 %cmp63, label %64, label %63
929   %val64 = load volatile float, float* undef
930   %cmp64 = fcmp une float %val64, undef
931   br i1 %cmp64, label %65, label %64
933   br label %exit
934 exit:
935   ret void
938 define void @benchmark_heapsort(i32 %n, double* nocapture %ra) {
939 ; This test case comes from the heapsort benchmark, and exemplifies several
940 ; important aspects to block placement in the presence of loops:
941 ; 1) Loop rotation needs to *ensure* that the desired exiting edge can be
942 ;    a fallthrough.
943 ; 2) The exiting edge from the loop which is rotated to be laid out at the
944 ;    bottom of the loop needs to be exiting into the nearest enclosing loop (to
945 ;    which there is an exit). Otherwise, we force that enclosing loop into
946 ;    strange layouts that are siginificantly less efficient, often times making
947 ;    it discontiguous.
949 ; CHECK-LABEL: @benchmark_heapsort
950 ; CHECK: %entry
951 ; First rotated loop top.
952 ; CHECK: .p2align
953 ; CHECK: %while.end
954 ; %for.cond gets completely tail-duplicated away.
955 ; CHECK: %if.then
956 ; CHECK: %if.else
957 ; CHECK: %if.end10
958 ; Second rotated loop top
959 ; CHECK: .p2align
960 ; CHECK: %if.then24
961 ; CHECK: %while.cond.outer
962 ; Third rotated loop top
963 ; CHECK: .p2align
964 ; CHECK: %while.cond
965 ; CHECK: %while.body
966 ; CHECK: %land.lhs.true
967 ; CHECK: %if.then19
968 ; CHECK: %if.end20
969 ; CHECK: %if.then8
970 ; CHECK: ret
972 entry:
973   %shr = ashr i32 %n, 1
974   %add = add nsw i32 %shr, 1
975   %arrayidx3 = getelementptr inbounds double, double* %ra, i64 1
976   br label %for.cond
978 for.cond:
979   %ir.0 = phi i32 [ %n, %entry ], [ %ir.1, %while.end ]
980   %l.0 = phi i32 [ %add, %entry ], [ %l.1, %while.end ]
981   %cmp = icmp sgt i32 %l.0, 1
982   br i1 %cmp, label %if.then, label %if.else
984 if.then:
985   %dec = add nsw i32 %l.0, -1
986   %idxprom = sext i32 %dec to i64
987   %arrayidx = getelementptr inbounds double, double* %ra, i64 %idxprom
988   %0 = load double, double* %arrayidx, align 8
989   br label %if.end10
991 if.else:
992   %idxprom1 = sext i32 %ir.0 to i64
993   %arrayidx2 = getelementptr inbounds double, double* %ra, i64 %idxprom1
994   %1 = load double, double* %arrayidx2, align 8
995   %2 = load double, double* %arrayidx3, align 8
996   store double %2, double* %arrayidx2, align 8
997   %dec6 = add nsw i32 %ir.0, -1
998   %cmp7 = icmp eq i32 %dec6, 1
999   br i1 %cmp7, label %if.then8, label %if.end10
1001 if.then8:
1002   store double %1, double* %arrayidx3, align 8
1003   ret void
1005 if.end10:
1006   %ir.1 = phi i32 [ %ir.0, %if.then ], [ %dec6, %if.else ]
1007   %l.1 = phi i32 [ %dec, %if.then ], [ %l.0, %if.else ]
1008   %rra.0 = phi double [ %0, %if.then ], [ %1, %if.else ]
1009   %add31 = add nsw i32 %ir.1, 1
1010   br label %while.cond.outer
1012 while.cond.outer:
1013   %j.0.ph.in = phi i32 [ %l.1, %if.end10 ], [ %j.1, %if.then24 ]
1014   %j.0.ph = shl i32 %j.0.ph.in, 1
1015   br label %while.cond
1017 while.cond:
1018   %j.0 = phi i32 [ %add31, %if.end20 ], [ %j.0.ph, %while.cond.outer ]
1019   %cmp11 = icmp sgt i32 %j.0, %ir.1
1020   br i1 %cmp11, label %while.end, label %while.body
1022 while.body:
1023   %cmp12 = icmp slt i32 %j.0, %ir.1
1024   br i1 %cmp12, label %land.lhs.true, label %if.end20
1026 land.lhs.true:
1027   %idxprom13 = sext i32 %j.0 to i64
1028   %arrayidx14 = getelementptr inbounds double, double* %ra, i64 %idxprom13
1029   %3 = load double, double* %arrayidx14, align 8
1030   %add15 = add nsw i32 %j.0, 1
1031   %idxprom16 = sext i32 %add15 to i64
1032   %arrayidx17 = getelementptr inbounds double, double* %ra, i64 %idxprom16
1033   %4 = load double, double* %arrayidx17, align 8
1034   %cmp18 = fcmp olt double %3, %4
1035   br i1 %cmp18, label %if.then19, label %if.end20
1037 if.then19:
1038   br label %if.end20
1040 if.end20:
1041   %j.1 = phi i32 [ %add15, %if.then19 ], [ %j.0, %land.lhs.true ], [ %j.0, %while.body ]
1042   %idxprom21 = sext i32 %j.1 to i64
1043   %arrayidx22 = getelementptr inbounds double, double* %ra, i64 %idxprom21
1044   %5 = load double, double* %arrayidx22, align 8
1045   %cmp23 = fcmp olt double %rra.0, %5
1046   br i1 %cmp23, label %if.then24, label %while.cond
1048 if.then24:
1049   %idxprom27 = sext i32 %j.0.ph.in to i64
1050   %arrayidx28 = getelementptr inbounds double, double* %ra, i64 %idxprom27
1051   store double %5, double* %arrayidx28, align 8
1052   br label %while.cond.outer
1054 while.end:
1055   %idxprom33 = sext i32 %j.0.ph.in to i64
1056   %arrayidx34 = getelementptr inbounds double, double* %ra, i64 %idxprom33
1057   store double %rra.0, double* %arrayidx34, align 8
1058   br label %for.cond
1061 declare void @cold_function() cold
1063 define i32 @test_cold_calls(i32* %a) {
1064 ; Test that edges to blocks post-dominated by cold calls are
1065 ; marked as not expected to be taken.  They should be laid out
1066 ; at the bottom.
1067 ; CHECK-LABEL: test_cold_calls:
1068 ; CHECK: %entry
1069 ; CHECK: %else
1070 ; CHECK: %exit
1071 ; CHECK: %then
1073 entry:
1074   %gep1 = getelementptr i32, i32* %a, i32 1
1075   %val1 = load i32, i32* %gep1
1076   %cond1 = icmp ugt i32 %val1, 1
1077   br i1 %cond1, label %then, label %else
1079 then:
1080   call void @cold_function()
1081   br label %exit
1083 else:
1084   %gep2 = getelementptr i32, i32* %a, i32 2
1085   %val2 = load i32, i32* %gep2
1086   br label %exit
1088 exit:
1089   %ret = phi i32 [ %val1, %then ], [ %val2, %else ]
1090   ret i32 %ret
1093 ; Make sure we put landingpads out of the way.
1094 declare i32 @pers(...)
1096 declare i32 @foo();
1098 declare i32 @bar();
1100 define i32 @test_lp(i32 %a) personality i32 (...)* @pers {
1101 ; CHECK-LABEL: test_lp:
1102 ; CHECK: %entry
1103 ; CHECK: %hot
1104 ; CHECK: %then
1105 ; CHECK: %cold
1106 ; CHECK: %coldlp
1107 ; CHECK: %hotlp
1108 ; CHECK: %lpret
1109 entry:
1110   %0 = icmp sgt i32 %a, 1
1111   br i1 %0, label %hot, label %cold, !prof !4
1113 hot:
1114   %1 = invoke i32 @foo()
1115           to label %then unwind label %hotlp
1117 cold:
1118   %2 = invoke i32 @bar()
1119           to label %then unwind label %coldlp
1121 then:
1122   %3 = phi i32 [ %1, %hot ], [ %2, %cold ]
1123   ret i32 %3
1125 hotlp:
1126   %4 = landingpad { i8*, i32 }
1127           cleanup
1128   br label %lpret
1130 coldlp:
1131   %5 = landingpad { i8*, i32 }
1132           cleanup
1133   br label %lpret
1135 lpret:
1136   %6 = phi i32 [-1, %hotlp], [-2, %coldlp]
1137   %7 = add i32 %6, 42
1138   ret i32 %7
1141 !4 = !{!"branch_weights", i32 65536, i32 0}
1143 ; Make sure that ehpad are scheduled from the least probable one
1144 ; to the most probable one. See selectBestCandidateBlock as to why.
1145 declare void @clean();
1147 define void @test_flow_unwind() personality i32 (...)* @pers {
1148 ; CHECK-LABEL: test_flow_unwind:
1149 ; CHECK: %entry
1150 ; CHECK: %then
1151 ; CHECK: %exit
1152 ; CHECK: %innerlp
1153 ; CHECK: %outerlp
1154 ; CHECK: %outercleanup
1155 entry:
1156   %0 = invoke i32 @foo()
1157           to label %then unwind label %outerlp
1159 then:
1160   %1 = invoke i32 @bar()
1161           to label %exit unwind label %innerlp
1163 exit:
1164   ret void
1166 innerlp:
1167   %2 = landingpad { i8*, i32 }
1168           cleanup
1169   br label %innercleanup
1171 outerlp:
1172   %3 = landingpad { i8*, i32 }
1173           cleanup
1174   br label %outercleanup
1176 outercleanup:
1177   %4 = phi { i8*, i32 } [%2, %innercleanup], [%3, %outerlp]
1178   call void @clean()
1179   resume { i8*, i32 } %4
1181 innercleanup:
1182   call void @clean()
1183   br label %outercleanup
1186 declare void @hot_function()
1188 define void @test_hot_branch(i32* %a) {
1189 ; Test that a hot branch that has a probability a little larger than 80% will
1190 ; break CFG constrains when doing block placement.
1191 ; CHECK-LABEL: test_hot_branch:
1192 ; CHECK: %entry
1193 ; CHECK: %then
1194 ; CHECK: %exit
1195 ; CHECK: %else
1197 entry:
1198   %gep1 = getelementptr i32, i32* %a, i32 1
1199   %val1 = load i32, i32* %gep1
1200   %cond1 = icmp ugt i32 %val1, 1
1201   br i1 %cond1, label %then, label %else, !prof !5
1203 then:
1204   call void @hot_function()
1205   br label %exit
1207 else:
1208   call void @cold_function()
1209   br label %exit
1211 exit:
1212   call void @hot_function()
1213   ret void
1216 define void @test_hot_branch_profile(i32* %a) !prof !6 {
1217 ; Test that a hot branch that has a probability a little larger than 50% will
1218 ; break CFG constrains when doing block placement when profile is available.
1219 ; CHECK-LABEL: test_hot_branch_profile:
1220 ; CHECK: %entry
1221 ; CHECK: %then
1222 ; CHECK: %exit
1223 ; CHECK: %else
1225 entry:
1226   %gep1 = getelementptr i32, i32* %a, i32 1
1227   %val1 = load i32, i32* %gep1
1228   %cond1 = icmp ugt i32 %val1, 1
1229   br i1 %cond1, label %then, label %else, !prof !7
1231 then:
1232   call void @hot_function()
1233   br label %exit
1235 else:
1236   call void @cold_function()
1237   br label %exit
1239 exit:
1240   call void @hot_function()
1241   ret void
1244 define void @test_hot_branch_triangle_profile(i32* %a) !prof !6 {
1245 ; Test that a hot branch that has a probability a little larger than 80% will
1246 ; break triangle shaped CFG constrains when doing block placement if profile
1247 ; is present.
1248 ; CHECK-LABEL: test_hot_branch_triangle_profile:
1249 ; CHECK: %entry
1250 ; CHECK: %exit
1251 ; CHECK: %then
1253 entry:
1254   %gep1 = getelementptr i32, i32* %a, i32 1
1255   %val1 = load i32, i32* %gep1
1256   %cond1 = icmp ugt i32 %val1, 1
1257   br i1 %cond1, label %exit, label %then, !prof !5
1259 then:
1260   call void @hot_function()
1261   br label %exit
1263 exit:
1264   call void @hot_function()
1265   ret void
1268 define void @test_hot_branch_triangle_profile_topology(i32* %a) !prof !6 {
1269 ; Test that a hot branch that has a probability between 50% and 66% will not
1270 ; break triangle shaped CFG constrains when doing block placement if profile
1271 ; is present.
1272 ; CHECK-LABEL: test_hot_branch_triangle_profile_topology:
1273 ; CHECK: %entry
1274 ; CHECK: %then
1275 ; CHECK: %exit
1277 entry:
1278   %gep1 = getelementptr i32, i32* %a, i32 1
1279   %val1 = load i32, i32* %gep1
1280   %cond1 = icmp ugt i32 %val1, 1
1281   br i1 %cond1, label %exit, label %then, !prof !7
1283 then:
1284   call void @hot_function()
1285   br label %exit
1287 exit:
1288   call void @hot_function()
1289   ret void
1292 declare void @a()
1293 declare void @b()
1295 define void @test_forked_hot_diamond(i32* %a) {
1296 ; Test that a hot-branch with probability > 80% followed by a 50/50 branch
1297 ; will not place the cold predecessor if the probability for the fallthrough
1298 ; remains above 80%
1299 ; CHECK-LABEL: test_forked_hot_diamond
1300 ; CHECK: %entry
1301 ; CHECK: %then
1302 ; CHECK: %fork1
1303 ; CHECK: %else
1304 ; CHECK: %fork2
1305 ; CHECK: %exit
1306 entry:
1307   %gep1 = getelementptr i32, i32* %a, i32 1
1308   %val1 = load i32, i32* %gep1
1309   %cond1 = icmp ugt i32 %val1, 1
1310   br i1 %cond1, label %then, label %else, !prof !5
1312 then:
1313   call void @hot_function()
1314   %gep2 = getelementptr i32, i32* %a, i32 2
1315   %val2 = load i32, i32* %gep2
1316   %cond2 = icmp ugt i32 %val2, 2
1317   br i1 %cond2, label %fork1, label %fork2, !prof !8
1319 else:
1320   call void @cold_function()
1321   %gep3 = getelementptr i32, i32* %a, i32 3
1322   %val3 = load i32, i32* %gep3
1323   %cond3 = icmp ugt i32 %val3, 3
1324   br i1 %cond3, label %fork1, label %fork2, !prof !8
1326 fork1:
1327   call void @a()
1328   br label %exit
1330 fork2:
1331   call void @b()
1332   br label %exit
1334 exit:
1335   call void @hot_function()
1336   ret void
1339 define void @test_forked_hot_diamond_gets_cold(i32* %a) {
1340 ; Test that a hot-branch with probability > 80% followed by a 50/50 branch
1341 ; will place the cold predecessor if the probability for the fallthrough
1342 ; falls below 80%
1343 ; The probability for both branches is 85%. For then2 vs else1
1344 ; this results in a compounded probability of 83%.
1345 ; Neither then2->fork1 nor then2->fork2 has a large enough relative
1346 ; probability to break the CFG.
1347 ; Relative probs:
1348 ; then2 -> fork1 vs else1 -> fork1 = 71%
1349 ; then2 -> fork2 vs else2 -> fork2 = 74%
1350 ; CHECK-LABEL: test_forked_hot_diamond_gets_cold
1351 ; CHECK: %entry
1352 ; CHECK: %then1
1353 ; CHECK: %then2
1354 ; CHECK: %else1
1355 ; CHECK: %fork1
1356 ; CHECK: %else2
1357 ; CHECK: %fork2
1358 ; CHECK: %exit
1359 entry:
1360   %gep1 = getelementptr i32, i32* %a, i32 1
1361   %val1 = load i32, i32* %gep1
1362   %cond1 = icmp ugt i32 %val1, 1
1363   br i1 %cond1, label %then1, label %else1, !prof !9
1365 then1:
1366   call void @hot_function()
1367   %gep2 = getelementptr i32, i32* %a, i32 2
1368   %val2 = load i32, i32* %gep2
1369   %cond2 = icmp ugt i32 %val2, 2
1370   br i1 %cond2, label %then2, label %else2, !prof !9
1372 else1:
1373   call void @cold_function()
1374   br label %fork1
1376 then2:
1377   call void @hot_function()
1378   %gep3 = getelementptr i32, i32* %a, i32 3
1379   %val3 = load i32, i32* %gep2
1380   %cond3 = icmp ugt i32 %val2, 3
1381   br i1 %cond3, label %fork1, label %fork2, !prof !8
1383 else2:
1384   call void @cold_function()
1385   br label %fork2
1387 fork1:
1388   call void @a()
1389   br label %exit
1391 fork2:
1392   call void @b()
1393   br label %exit
1395 exit:
1396   call void @hot_function()
1397   ret void
1400 define void @test_forked_hot_diamond_stays_hot(i32* %a) {
1401 ; Test that a hot-branch with probability > 88.88% (1:8) followed by a 50/50
1402 ; branch will not place the cold predecessor as the probability for the
1403 ; fallthrough stays above 80%
1404 ; (1:8) followed by (1:1) is still (1:4)
1405 ; Here we use 90% probability because two in a row
1406 ; have a 89 % probability vs the original branch.
1407 ; CHECK-LABEL: test_forked_hot_diamond_stays_hot
1408 ; CHECK: %entry
1409 ; CHECK: %then1
1410 ; CHECK: %then2
1411 ; CHECK: %fork1
1412 ; CHECK: %else1
1413 ; CHECK: %else2
1414 ; CHECK: %fork2
1415 ; CHECK: %exit
1416 entry:
1417   %gep1 = getelementptr i32, i32* %a, i32 1
1418   %val1 = load i32, i32* %gep1
1419   %cond1 = icmp ugt i32 %val1, 1
1420   br i1 %cond1, label %then1, label %else1, !prof !10
1422 then1:
1423   call void @hot_function()
1424   %gep2 = getelementptr i32, i32* %a, i32 2
1425   %val2 = load i32, i32* %gep2
1426   %cond2 = icmp ugt i32 %val2, 2
1427   br i1 %cond2, label %then2, label %else2, !prof !10
1429 else1:
1430   call void @cold_function()
1431   br label %fork1
1433 then2:
1434   call void @hot_function()
1435   %gep3 = getelementptr i32, i32* %a, i32 3
1436   %val3 = load i32, i32* %gep2
1437   %cond3 = icmp ugt i32 %val2, 3
1438   br i1 %cond3, label %fork1, label %fork2, !prof !8
1440 else2:
1441   call void @cold_function()
1442   br label %fork2
1444 fork1:
1445   call void @a()
1446   br label %exit
1448 fork2:
1449   call void @b()
1450   br label %exit
1452 exit:
1453   call void @hot_function()
1454   ret void
1457 ; Because %endif has a higher frequency than %if, the calculations show we
1458 ; shouldn't tail-duplicate %endif so that we can place it after %if. We were
1459 ; previously undercounting the cost by ignoring execution frequency that didn't
1460 ; come from the %if->%endif path.
1461 ; CHECK-LABEL: higher_frequency_succ_tail_dup
1462 ; CHECK: %entry
1463 ; CHECK: %elseif
1464 ; CHECK: %else
1465 ; CHECK: %endif
1466 ; CHECK: %then
1467 ; CHECK: %ret
1468 define void @higher_frequency_succ_tail_dup(i1 %a, i1 %b, i1 %c) {
1469 entry:
1470   br label %if
1471 if:                                               ; preds = %entry
1472   call void @effect(i32 0)
1473   br i1 %a, label %elseif, label %endif, !prof !11 ; even
1475 elseif:                                           ; preds = %if
1476   call void @effect(i32 1)
1477   br i1 %b, label %else, label %endif, !prof !11 ; even
1479 else:                                             ; preds = %elseif
1480   call void @effect(i32 2)
1481   br label %endif
1483 endif:                                            ; preds = %if, %elseif, %else
1484   br i1 %c, label %then, label %ret, !prof !12 ; 5 to 3
1486 then:                                             ; preds = %endif
1487   call void @effect(i32 3)
1488   br label %ret
1490 ret:                                              ; preds = %endif, %then
1491   ret void
1494 define i32 @not_rotate_if_extra_branch(i32 %count) {
1495 ; Test checks that there is no loop rotation
1496 ; if it introduces extra branch.
1497 ; Specifically in this case because best exit is .header
1498 ; but it has fallthrough to .middle block and last block in
1499 ; loop chain .slow does not have afallthrough to .header.
1500 ; CHECK-LABEL: not_rotate_if_extra_branch
1501 ; CHECK: %.entry
1502 ; CHECK: %.header
1503 ; CHECK: %.middle
1504 ; CHECK: %.backedge
1505 ; CHECK: %.slow
1506 ; CHECK: %.bailout
1507 ; CHECK: %.stop
1508 .entry:
1509   %sum.0 = shl nsw i32 %count, 1
1510   br label %.header
1512 .header:
1513   %i = phi i32 [ %i.1, %.backedge ], [ 0, %.entry ]
1514   %sum = phi i32 [ %sum.1, %.backedge ], [ %sum.0, %.entry ]
1515   %is_exc = icmp sgt i32 %i, 9000000
1516   br i1 %is_exc, label %.bailout, label %.middle, !prof !13
1518 .bailout:
1519   %sum.2 = add nsw i32 %count, 1
1520   br label %.stop
1522 .middle:
1523   %pr.1 = and i32 %i, 1023
1524   %pr.2 = icmp eq i32 %pr.1, 0
1525   br i1 %pr.2, label %.slow, label %.backedge, !prof !14
1527 .slow:
1528   tail call void @effect(i32 %sum)
1529   br label %.backedge
1531 .backedge:
1532   %sum.1 = add nsw i32 %i, %sum
1533   %i.1 = add nsw i32 %i, 1
1534   %end = icmp slt i32 %i.1, %count
1535   br i1 %end, label %.header, label %.stop, !prof !15
1537 .stop:
1538   %sum.phi = phi i32 [ %sum.1, %.backedge ], [ %sum.2, %.bailout ]
1539   ret i32 %sum.phi
1542 define i32 @not_rotate_if_extra_branch_regression(i32 %count, i32 %init) {
1543 ; This is a regression test against patch avoid loop rotation if
1544 ; it introduce an extra btanch.
1545 ; CHECK-LABEL: not_rotate_if_extra_branch_regression
1546 ; CHECK: %.entry
1547 ; CHECK: %.first_backedge
1548 ; CHECK: %.slow
1549 ; CHECK: %.second_header
1550 .entry:
1551   %sum.0 = shl nsw i32 %count, 1
1552   br label %.first_header
1554 .first_header:
1555   %i = phi i32 [ %i.1, %.first_backedge ], [ 0, %.entry ]
1556   %is_bo1 = icmp sgt i32 %i, 9000000
1557   br i1 %is_bo1, label %.bailout, label %.first_backedge, !prof !14
1559 .first_backedge:
1560   %i.1 = add nsw i32 %i, 1
1561   %end = icmp slt i32 %i.1, %count
1562   br i1 %end, label %.first_header, label %.second_header, !prof !13
1564 .second_header:
1565   %j = phi i32 [ %j.1, %.second_backedge ], [ %init, %.first_backedge ]
1566   %end.2 = icmp sgt i32 %j, %count
1567   br i1 %end.2, label %.stop, label %.second_middle, !prof !14
1569 .second_middle:
1570   %is_slow = icmp sgt i32 %j, 9000000
1571   br i1 %is_slow, label %.slow, label %.second_backedge, !prof !14
1573 .slow:
1574   tail call void @effect(i32 %j)
1575   br label %.second_backedge
1577 .second_backedge:
1578   %j.1 = add nsw i32 %j, 1
1579   %end.3 = icmp slt i32 %j, 10000000
1580   br i1 %end.3, label %.second_header, label %.stop, !prof !13
1582 .stop:
1583   %res = add nsw i32 %j, %i.1
1584   ret i32 %res
1586 .bailout:
1587   ret i32 0
1590 declare void @effect(i32)
1592 !5 = !{!"branch_weights", i32 84, i32 16}
1593 !6 = !{!"function_entry_count", i32 10}
1594 !7 = !{!"branch_weights", i32 60, i32 40}
1595 !8 = !{!"branch_weights", i32 5001, i32 4999}
1596 !9 = !{!"branch_weights", i32 85, i32 15}
1597 !10 = !{!"branch_weights", i32 90, i32 10}
1598 !11 = !{!"branch_weights", i32 1, i32 1}
1599 !12 = !{!"branch_weights", i32 5, i32 3}
1600 !13 = !{!"branch_weights", i32 1, i32 1}
1601 !14 = !{!"branch_weights", i32 1, i32 1023}
1602 !15 = !{!"branch_weights", i32 4095, i32 1}