1; RUN: llc -mtriple=i686-linux -pre-RA-sched=source < %s | FileCheck %s
2
3declare void @error(i32 %i, i32 %a, i32 %b)
4
5define 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
25
26entry:
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
31
32then1:
33  call void @error(i32 %i, i32 1, i32 %b)
34  br label %else1
35
36else1:
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
41
42then2:
43  call void @error(i32 %i, i32 1, i32 %b)
44  br label %else2
45
46else2:
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
51
52then3:
53  call void @error(i32 %i, i32 1, i32 %b)
54  br label %else3
55
56else3:
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
61
62then4:
63  call void @error(i32 %i, i32 1, i32 %b)
64  br label %else4
65
66else4:
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
71
72then5:
73  call void @error(i32 %i, i32 1, i32 %b)
74  br label %exit
75
76exit:
77  ret i32 %b
78}
79
80define 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
93
94entry:
95  br label %body1
96
97body1:
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
102
103unlikely1:
104  call void @error(i32 %i, i32 1, i32 %base)
105  br label %body2
106
107body2:
108  %unlikelycond2 = icmp sgt i32 %base, 21
109  br i1 %unlikelycond2, label %unlikely2, label %body3, !prof !0
110
111unlikely2:
112  call void @error(i32 %i, i32 2, i32 %base)
113  br label %body3
114
115body3:
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
122
123exit:
124  ret i32 %sum
125}
126
127!0 = !{!"branch_weights", i32 4, i32 64}
128
129define 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
141
142entry:
143  br label %body1
144
145body1:
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
150
151bail1:
152  ret i32 -1
153
154body2:
155  %bailcond2 = icmp eq i32 %base, 43
156  br i1 %bailcond2, label %bail2, label %body3
157
158bail2:
159  ret i32 -2
160
161body3:
162  %bailcond3 = icmp eq i32 %base, 44
163  br i1 %bailcond3, label %bail3, label %body4
164
165bail3:
166  ret i32 -3
167
168body4:
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
175
176exit:
177  ret i32 %sum
178}
179
180define i32 @test_loop_rotate(i32 %i, i32* %a) {
181; Check that we rotate conditional exits from the loop to the bottom of the
182; loop, eliminating unconditional branches to the top.
183; CHECK-LABEL: test_loop_rotate:
184; CHECK: %entry
185; CHECK: %body1
186; CHECK: %body0
187; CHECK: %exit
188
189entry:
190  br label %body0
191
192body0:
193  %iv = phi i32 [ 0, %entry ], [ %next, %body1 ]
194  %base = phi i32 [ 0, %entry ], [ %sum, %body1 ]
195  %next = add i32 %iv, 1
196  %exitcond = icmp eq i32 %next, %i
197  br i1 %exitcond, label %exit, label %body1
198
199body1:
200  %arrayidx = getelementptr inbounds i32, i32* %a, i32 %iv
201  %0 = load i32, i32* %arrayidx
202  %sum = add nsw i32 %0, %base
203  %bailcond1 = icmp eq i32 %sum, 42
204  br label %body0
205
206exit:
207  ret i32 %base
208}
209
210define i32 @test_no_loop_rotate(i32 %i, i32* %a) {
211; Check that we don't try to rotate a loop which is already laid out with
212; fallthrough opportunities into the top and out of the bottom.
213; CHECK-LABEL: test_no_loop_rotate:
214; CHECK: %entry
215; CHECK: %body0
216; CHECK: %body1
217; CHECK: %exit
218
219entry:
220  br label %body0
221
222body0:
223  %iv = phi i32 [ 0, %entry ], [ %next, %body1 ]
224  %base = phi i32 [ 0, %entry ], [ %sum, %body1 ]
225  %arrayidx = getelementptr inbounds i32, i32* %a, i32 %iv
226  %0 = load i32, i32* %arrayidx
227  %sum = add nsw i32 %0, %base
228  %bailcond1 = icmp eq i32 %sum, 42
229  br i1 %bailcond1, label %exit, label %body1
230
231body1:
232  %next = add i32 %iv, 1
233  %exitcond = icmp eq i32 %next, %i
234  br i1 %exitcond, label %exit, label %body0
235
236exit:
237  ret i32 %base
238}
239
240define i32 @test_loop_align(i32 %i, i32* %a) {
241; Check that we provide basic loop body alignment with the block placement
242; pass.
243; CHECK-LABEL: test_loop_align:
244; CHECK: %entry
245; CHECK: .p2align [[ALIGN:[0-9]+]],
246; CHECK-NEXT: %body
247; CHECK: %exit
248
249entry:
250  br label %body
251
252body:
253  %iv = phi i32 [ 0, %entry ], [ %next, %body ]
254  %base = phi i32 [ 0, %entry ], [ %sum, %body ]
255  %arrayidx = getelementptr inbounds i32, i32* %a, i32 %iv
256  %0 = load i32, i32* %arrayidx
257  %sum = add nsw i32 %0, %base
258  %next = add i32 %iv, 1
259  %exitcond = icmp eq i32 %next, %i
260  br i1 %exitcond, label %exit, label %body
261
262exit:
263  ret i32 %sum
264}
265
266define i32 @test_nested_loop_align(i32 %i, i32* %a, i32* %b) {
267; Check that we provide nested loop body alignment.
268; CHECK-LABEL: test_nested_loop_align:
269; CHECK: %entry
270; CHECK: .p2align [[ALIGN]],
271; CHECK-NEXT: %loop.body.1
272; CHECK: .p2align [[ALIGN]],
273; CHECK-NEXT: %inner.loop.body
274; CHECK-NOT: .p2align
275; CHECK: %exit
276
277entry:
278  br label %loop.body.1
279
280loop.body.1:
281  %iv = phi i32 [ 0, %entry ], [ %next, %loop.body.2 ]
282  %arrayidx = getelementptr inbounds i32, i32* %a, i32 %iv
283  %bidx = load i32, i32* %arrayidx
284  br label %inner.loop.body
285
286inner.loop.body:
287  %inner.iv = phi i32 [ 0, %loop.body.1 ], [ %inner.next, %inner.loop.body ]
288  %base = phi i32 [ 0, %loop.body.1 ], [ %sum, %inner.loop.body ]
289  %scaled_idx = mul i32 %bidx, %iv
290  %inner.arrayidx = getelementptr inbounds i32, i32* %b, i32 %scaled_idx
291  %0 = load i32, i32* %inner.arrayidx
292  %sum = add nsw i32 %0, %base
293  %inner.next = add i32 %iv, 1
294  %inner.exitcond = icmp eq i32 %inner.next, %i
295  br i1 %inner.exitcond, label %loop.body.2, label %inner.loop.body
296
297loop.body.2:
298  %next = add i32 %iv, 1
299  %exitcond = icmp eq i32 %next, %i
300  br i1 %exitcond, label %exit, label %loop.body.1
301
302exit:
303  ret i32 %sum
304}
305
306define void @unnatural_cfg1() {
307; Test that we can handle a loop with an inner unnatural loop at the end of
308; a function. This is a gross CFG reduced out of the single source GCC.
309; CHECK: unnatural_cfg1
310; CHECK: %entry
311; CHECK: %loop.body1
312; CHECK: %loop.body2
313; CHECK: %loop.body3
314
315entry:
316  br label %loop.header
317
318loop.header:
319  br label %loop.body1
320
321loop.body1:
322  br i1 undef, label %loop.body3, label %loop.body2
323
324loop.body2:
325  %ptr = load i32*, i32** undef, align 4
326  br label %loop.body3
327
328loop.body3:
329  %myptr = phi i32* [ %ptr2, %loop.body5 ], [ %ptr, %loop.body2 ], [ undef, %loop.body1 ]
330  %bcmyptr = bitcast i32* %myptr to i32*
331  %val = load i32, i32* %bcmyptr, align 4
332  %comp = icmp eq i32 %val, 48
333  br i1 %comp, label %loop.body4, label %loop.body5
334
335loop.body4:
336  br i1 undef, label %loop.header, label %loop.body5
337
338loop.body5:
339  %ptr2 = load i32*, i32** undef, align 4
340  br label %loop.body3
341}
342
343define void @unnatural_cfg2() {
344; Test that we can handle a loop with a nested natural loop *and* an unnatural
345; loop. This was reduced from a crash on block placement when run over
346; single-source GCC.
347; CHECK: unnatural_cfg2
348; CHECK: %entry
349; CHECK: %loop.body1
350; CHECK: %loop.body2
351; CHECK: %loop.body3
352; CHECK: %loop.inner1.begin
353; The end block is folded with %loop.body3...
354; CHECK-NOT: %loop.inner1.end
355; CHECK: %loop.body4
356; CHECK: %loop.inner2.begin
357; The loop.inner2.end block is folded
358; CHECK: %loop.header
359; CHECK: %bail
360
361entry:
362  br label %loop.header
363
364loop.header:
365  %comp0 = icmp eq i32* undef, null
366  br i1 %comp0, label %bail, label %loop.body1
367
368loop.body1:
369  %val0 = load i32*, i32** undef, align 4
370  br i1 undef, label %loop.body2, label %loop.inner1.begin
371
372loop.body2:
373  br i1 undef, label %loop.body4, label %loop.body3
374
375loop.body3:
376  %ptr1 = getelementptr inbounds i32, i32* %val0, i32 0
377  %castptr1 = bitcast i32* %ptr1 to i32**
378  %val1 = load i32*, i32** %castptr1, align 4
379  br label %loop.inner1.begin
380
381loop.inner1.begin:
382  %valphi = phi i32* [ %val2, %loop.inner1.end ], [ %val1, %loop.body3 ], [ %val0, %loop.body1 ]
383  %castval = bitcast i32* %valphi to i32*
384  %comp1 = icmp eq i32 undef, 48
385  br i1 %comp1, label %loop.inner1.end, label %loop.body4
386
387loop.inner1.end:
388  %ptr2 = getelementptr inbounds i32, i32* %valphi, i32 0
389  %castptr2 = bitcast i32* %ptr2 to i32**
390  %val2 = load i32*, i32** %castptr2, align 4
391  br label %loop.inner1.begin
392
393loop.body4.dead:
394  br label %loop.body4
395
396loop.body4:
397  %comp2 = icmp ult i32 undef, 3
398  br i1 %comp2, label %loop.inner2.begin, label %loop.end
399
400loop.inner2.begin:
401  br i1 false, label %loop.end, label %loop.inner2.end
402
403loop.inner2.end:
404  %comp3 = icmp eq i32 undef, 1769472
405  br i1 %comp3, label %loop.end, label %loop.inner2.begin
406
407loop.end:
408  br label %loop.header
409
410bail:
411  unreachable
412}
413
414define i32 @problematic_switch() {
415; This function's CFG caused overlow in the machine branch probability
416; calculation, triggering asserts. Make sure we don't crash on it.
417; CHECK: problematic_switch
418
419entry:
420  switch i32 undef, label %exit [
421    i32 879, label %bogus
422    i32 877, label %step
423    i32 876, label %step
424    i32 875, label %step
425    i32 874, label %step
426    i32 873, label %step
427    i32 872, label %step
428    i32 868, label %step
429    i32 867, label %step
430    i32 866, label %step
431    i32 861, label %step
432    i32 860, label %step
433    i32 856, label %step
434    i32 855, label %step
435    i32 854, label %step
436    i32 831, label %step
437    i32 830, label %step
438    i32 829, label %step
439    i32 828, label %step
440    i32 815, label %step
441    i32 814, label %step
442    i32 811, label %step
443    i32 806, label %step
444    i32 805, label %step
445    i32 804, label %step
446    i32 803, label %step
447    i32 802, label %step
448    i32 801, label %step
449    i32 800, label %step
450    i32 799, label %step
451    i32 798, label %step
452    i32 797, label %step
453    i32 796, label %step
454    i32 795, label %step
455  ]
456bogus:
457  unreachable
458step:
459  br label %exit
460exit:
461  %merge = phi i32 [ 3, %step ], [ 6, %entry ]
462  ret i32 %merge
463}
464
465define void @fpcmp_unanalyzable_branch(i1 %cond) {
466; This function's CFG contains an once-unanalyzable branch (une on floating
467; points). As now it becomes analyzable, we should get best layout in which each
468; edge in 'entry' -> 'entry.if.then_crit_edge' -> 'if.then' -> 'if.end' is
469; fall-through.
470; CHECK-LABEL: fpcmp_unanalyzable_branch:
471; CHECK:       # BB#0: # %entry
472; CHECK:       # BB#1: # %entry.if.then_crit_edge
473; CHECK:       .LBB10_4: # %if.then
474; CHECK:       .LBB10_5: # %if.end
475; CHECK:       # BB#3: # %exit
476; CHECK:       jne .LBB10_4
477; CHECK-NEXT:  jnp .LBB10_5
478; CHECK-NEXT:  jmp .LBB10_4
479
480entry:
481; Note that this branch must be strongly biased toward
482; 'entry.if.then_crit_edge' to ensure that we would try to form a chain for
483; 'entry' -> 'entry.if.then_crit_edge' -> 'if.then' -> 'if.end'.
484  br i1 %cond, label %entry.if.then_crit_edge, label %lor.lhs.false, !prof !1
485
486entry.if.then_crit_edge:
487  %.pre14 = load i8, i8* undef, align 1
488  br label %if.then
489
490lor.lhs.false:
491  br i1 undef, label %if.end, label %exit
492
493exit:
494  %cmp.i = fcmp une double 0.000000e+00, undef
495  br i1 %cmp.i, label %if.then, label %if.end, !prof !3
496
497if.then:
498  %0 = phi i8 [ %.pre14, %entry.if.then_crit_edge ], [ undef, %exit ]
499  %1 = and i8 %0, 1
500  store i8 %1, i8* undef, align 4
501  br label %if.end
502
503if.end:
504  ret void
505}
506
507!1 = !{!"branch_weights", i32 1000, i32 1}
508!3 = !{!"branch_weights", i32 1, i32 1000}
509
510declare i32 @f()
511declare i32 @g()
512declare i32 @h(i32 %x)
513
514define i32 @test_global_cfg_break_profitability() {
515; Check that our metrics for the profitability of a CFG break are global rather
516; than local. A successor may be very hot, but if the current block isn't, it
517; doesn't matter. Within this test the 'then' block is slightly warmer than the
518; 'else' block, but not nearly enough to merit merging it with the exit block
519; even though the probability of 'then' branching to the 'exit' block is very
520; high.
521; CHECK: test_global_cfg_break_profitability
522; CHECK: calll {{_?}}f
523; CHECK: calll {{_?}}g
524; CHECK: calll {{_?}}h
525; CHECK: ret
526
527entry:
528  br i1 undef, label %then, label %else, !prof !2
529
530then:
531  %then.result = call i32 @f()
532  br label %exit
533
534else:
535  %else.result = call i32 @g()
536  br label %exit
537
538exit:
539  %result = phi i32 [ %then.result, %then ], [ %else.result, %else ]
540  %result2 = call i32 @h(i32 %result)
541  ret i32 %result
542}
543
544!2 = !{!"branch_weights", i32 3, i32 1}
545
546declare i32 @__gxx_personality_v0(...)
547
548define void @test_eh_lpad_successor() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
549; Some times the landing pad ends up as the first successor of an invoke block.
550; When this happens, a strange result used to fall out of updateTerminators: we
551; didn't correctly locate the fallthrough successor, assuming blindly that the
552; first one was the fallthrough successor. As a result, we would add an
553; erroneous jump to the landing pad thinking *that* was the default successor.
554; CHECK: test_eh_lpad_successor
555; CHECK: %entry
556; CHECK-NOT: jmp
557; CHECK: %loop
558
559entry:
560  invoke i32 @f() to label %preheader unwind label %lpad
561
562preheader:
563  br label %loop
564
565lpad:
566  %lpad.val = landingpad { i8*, i32 }
567          cleanup
568  resume { i8*, i32 } %lpad.val
569
570loop:
571  br label %loop
572}
573
574declare void @fake_throw() noreturn
575
576define void @test_eh_throw() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
577; For blocks containing a 'throw' (or similar functionality), we have
578; a no-return invoke. In this case, only EH successors will exist, and
579; fallthrough simply won't occur. Make sure we don't crash trying to update
580; terminators for such constructs.
581;
582; CHECK: test_eh_throw
583; CHECK: %entry
584; CHECK: %cleanup
585
586entry:
587  invoke void @fake_throw() to label %continue unwind label %cleanup
588
589continue:
590  unreachable
591
592cleanup:
593  %0 = landingpad { i8*, i32 }
594          cleanup
595  unreachable
596}
597
598define void @test_unnatural_cfg_backwards_inner_loop() {
599; Test that when we encounter an unnatural CFG structure after having formed
600; a chain for an inner loop which happened to be laid out backwards we don't
601; attempt to merge onto the wrong end of the inner loop just because we find it
602; first. This was reduced from a crasher in GCC's single source.
603;
604; CHECK: test_unnatural_cfg_backwards_inner_loop
605; CHECK: %entry
606; CHECK: %loop2b
607; CHECK: %loop1
608
609entry:
610  br i1 undef, label %loop2a, label %body
611
612body:
613  br label %loop2a
614
615loop1:
616  %next.load = load i32*, i32** undef
617  br i1 %comp.a, label %loop2a, label %loop2b
618
619loop2a:
620  %var = phi i32* [ null, %entry ], [ null, %body ], [ %next.phi, %loop1 ]
621  %next.var = phi i32* [ null, %entry ], [ undef, %body ], [ %next.load, %loop1 ]
622  %comp.a = icmp eq i32* %var, null
623  br label %loop3
624
625loop2b:
626  %gep = getelementptr inbounds i32, i32* %var.phi, i32 0
627  %next.ptr = bitcast i32* %gep to i32**
628  store i32* %next.phi, i32** %next.ptr
629  br label %loop3
630
631loop3:
632  %var.phi = phi i32* [ %next.phi, %loop2b ], [ %var, %loop2a ]
633  %next.phi = phi i32* [ %next.load, %loop2b ], [ %next.var, %loop2a ]
634  br label %loop1
635}
636
637define void @unanalyzable_branch_to_loop_header() {
638; Ensure that we can handle unanalyzable branches into loop headers. We
639; pre-form chains for unanalyzable branches, and will find the tail end of that
640; at the start of the loop. This function uses floating point comparison
641; fallthrough because that happens to always produce unanalyzable branches on
642; x86.
643;
644; CHECK: unanalyzable_branch_to_loop_header
645; CHECK: %entry
646; CHECK: %loop
647; CHECK: %exit
648
649entry:
650  %cmp = fcmp une double 0.000000e+00, undef
651  br i1 %cmp, label %loop, label %exit
652
653loop:
654  %cond = icmp eq i8 undef, 42
655  br i1 %cond, label %exit, label %loop
656
657exit:
658  ret void
659}
660
661define void @unanalyzable_branch_to_best_succ(i1 %cond) {
662; Ensure that we can handle unanalyzable branches where the destination block
663; gets selected as the optimal successor to merge.
664;
665; This branch is now analyzable and hence the destination block becomes the
666; hotter one. The right order is entry->bar->exit->foo.
667;
668; CHECK: unanalyzable_branch_to_best_succ
669; CHECK: %entry
670; CHECK: %bar
671; CHECK: %exit
672; CHECK: %foo
673
674entry:
675  ; Bias this branch toward bar to ensure we form that chain.
676  br i1 %cond, label %bar, label %foo, !prof !1
677
678foo:
679  %cmp = fcmp une double 0.000000e+00, undef
680  br i1 %cmp, label %bar, label %exit
681
682bar:
683  call i32 @f()
684  br label %exit
685
686exit:
687  ret void
688}
689
690define void @unanalyzable_branch_to_free_block(float %x) {
691; Ensure that we can handle unanalyzable branches where the destination block
692; gets selected as the best free block in the CFG.
693;
694; CHECK: unanalyzable_branch_to_free_block
695; CHECK: %entry
696; CHECK: %a
697; CHECK: %b
698; CHECK: %c
699; CHECK: %exit
700
701entry:
702  br i1 undef, label %a, label %b
703
704a:
705  call i32 @f()
706  br label %c
707
708b:
709  %cmp = fcmp une float %x, undef
710  br i1 %cmp, label %c, label %exit
711
712c:
713  call i32 @g()
714  br label %exit
715
716exit:
717  ret void
718}
719
720define void @many_unanalyzable_branches() {
721; Ensure that we don't crash as we're building up many unanalyzable branches,
722; blocks, and loops.
723;
724; CHECK: many_unanalyzable_branches
725; CHECK: %entry
726; CHECK: %exit
727
728entry:
729  br label %0
730
731  %val0 = load volatile float, float* undef
732  %cmp0 = fcmp une float %val0, undef
733  br i1 %cmp0, label %1, label %0
734  %val1 = load volatile float, float* undef
735  %cmp1 = fcmp une float %val1, undef
736  br i1 %cmp1, label %2, label %1
737  %val2 = load volatile float, float* undef
738  %cmp2 = fcmp une float %val2, undef
739  br i1 %cmp2, label %3, label %2
740  %val3 = load volatile float, float* undef
741  %cmp3 = fcmp une float %val3, undef
742  br i1 %cmp3, label %4, label %3
743  %val4 = load volatile float, float* undef
744  %cmp4 = fcmp une float %val4, undef
745  br i1 %cmp4, label %5, label %4
746  %val5 = load volatile float, float* undef
747  %cmp5 = fcmp une float %val5, undef
748  br i1 %cmp5, label %6, label %5
749  %val6 = load volatile float, float* undef
750  %cmp6 = fcmp une float %val6, undef
751  br i1 %cmp6, label %7, label %6
752  %val7 = load volatile float, float* undef
753  %cmp7 = fcmp une float %val7, undef
754  br i1 %cmp7, label %8, label %7
755  %val8 = load volatile float, float* undef
756  %cmp8 = fcmp une float %val8, undef
757  br i1 %cmp8, label %9, label %8
758  %val9 = load volatile float, float* undef
759  %cmp9 = fcmp une float %val9, undef
760  br i1 %cmp9, label %10, label %9
761  %val10 = load volatile float, float* undef
762  %cmp10 = fcmp une float %val10, undef
763  br i1 %cmp10, label %11, label %10
764  %val11 = load volatile float, float* undef
765  %cmp11 = fcmp une float %val11, undef
766  br i1 %cmp11, label %12, label %11
767  %val12 = load volatile float, float* undef
768  %cmp12 = fcmp une float %val12, undef
769  br i1 %cmp12, label %13, label %12
770  %val13 = load volatile float, float* undef
771  %cmp13 = fcmp une float %val13, undef
772  br i1 %cmp13, label %14, label %13
773  %val14 = load volatile float, float* undef
774  %cmp14 = fcmp une float %val14, undef
775  br i1 %cmp14, label %15, label %14
776  %val15 = load volatile float, float* undef
777  %cmp15 = fcmp une float %val15, undef
778  br i1 %cmp15, label %16, label %15
779  %val16 = load volatile float, float* undef
780  %cmp16 = fcmp une float %val16, undef
781  br i1 %cmp16, label %17, label %16
782  %val17 = load volatile float, float* undef
783  %cmp17 = fcmp une float %val17, undef
784  br i1 %cmp17, label %18, label %17
785  %val18 = load volatile float, float* undef
786  %cmp18 = fcmp une float %val18, undef
787  br i1 %cmp18, label %19, label %18
788  %val19 = load volatile float, float* undef
789  %cmp19 = fcmp une float %val19, undef
790  br i1 %cmp19, label %20, label %19
791  %val20 = load volatile float, float* undef
792  %cmp20 = fcmp une float %val20, undef
793  br i1 %cmp20, label %21, label %20
794  %val21 = load volatile float, float* undef
795  %cmp21 = fcmp une float %val21, undef
796  br i1 %cmp21, label %22, label %21
797  %val22 = load volatile float, float* undef
798  %cmp22 = fcmp une float %val22, undef
799  br i1 %cmp22, label %23, label %22
800  %val23 = load volatile float, float* undef
801  %cmp23 = fcmp une float %val23, undef
802  br i1 %cmp23, label %24, label %23
803  %val24 = load volatile float, float* undef
804  %cmp24 = fcmp une float %val24, undef
805  br i1 %cmp24, label %25, label %24
806  %val25 = load volatile float, float* undef
807  %cmp25 = fcmp une float %val25, undef
808  br i1 %cmp25, label %26, label %25
809  %val26 = load volatile float, float* undef
810  %cmp26 = fcmp une float %val26, undef
811  br i1 %cmp26, label %27, label %26
812  %val27 = load volatile float, float* undef
813  %cmp27 = fcmp une float %val27, undef
814  br i1 %cmp27, label %28, label %27
815  %val28 = load volatile float, float* undef
816  %cmp28 = fcmp une float %val28, undef
817  br i1 %cmp28, label %29, label %28
818  %val29 = load volatile float, float* undef
819  %cmp29 = fcmp une float %val29, undef
820  br i1 %cmp29, label %30, label %29
821  %val30 = load volatile float, float* undef
822  %cmp30 = fcmp une float %val30, undef
823  br i1 %cmp30, label %31, label %30
824  %val31 = load volatile float, float* undef
825  %cmp31 = fcmp une float %val31, undef
826  br i1 %cmp31, label %32, label %31
827  %val32 = load volatile float, float* undef
828  %cmp32 = fcmp une float %val32, undef
829  br i1 %cmp32, label %33, label %32
830  %val33 = load volatile float, float* undef
831  %cmp33 = fcmp une float %val33, undef
832  br i1 %cmp33, label %34, label %33
833  %val34 = load volatile float, float* undef
834  %cmp34 = fcmp une float %val34, undef
835  br i1 %cmp34, label %35, label %34
836  %val35 = load volatile float, float* undef
837  %cmp35 = fcmp une float %val35, undef
838  br i1 %cmp35, label %36, label %35
839  %val36 = load volatile float, float* undef
840  %cmp36 = fcmp une float %val36, undef
841  br i1 %cmp36, label %37, label %36
842  %val37 = load volatile float, float* undef
843  %cmp37 = fcmp une float %val37, undef
844  br i1 %cmp37, label %38, label %37
845  %val38 = load volatile float, float* undef
846  %cmp38 = fcmp une float %val38, undef
847  br i1 %cmp38, label %39, label %38
848  %val39 = load volatile float, float* undef
849  %cmp39 = fcmp une float %val39, undef
850  br i1 %cmp39, label %40, label %39
851  %val40 = load volatile float, float* undef
852  %cmp40 = fcmp une float %val40, undef
853  br i1 %cmp40, label %41, label %40
854  %val41 = load volatile float, float* undef
855  %cmp41 = fcmp une float %val41, undef
856  br i1 %cmp41, label %42, label %41
857  %val42 = load volatile float, float* undef
858  %cmp42 = fcmp une float %val42, undef
859  br i1 %cmp42, label %43, label %42
860  %val43 = load volatile float, float* undef
861  %cmp43 = fcmp une float %val43, undef
862  br i1 %cmp43, label %44, label %43
863  %val44 = load volatile float, float* undef
864  %cmp44 = fcmp une float %val44, undef
865  br i1 %cmp44, label %45, label %44
866  %val45 = load volatile float, float* undef
867  %cmp45 = fcmp une float %val45, undef
868  br i1 %cmp45, label %46, label %45
869  %val46 = load volatile float, float* undef
870  %cmp46 = fcmp une float %val46, undef
871  br i1 %cmp46, label %47, label %46
872  %val47 = load volatile float, float* undef
873  %cmp47 = fcmp une float %val47, undef
874  br i1 %cmp47, label %48, label %47
875  %val48 = load volatile float, float* undef
876  %cmp48 = fcmp une float %val48, undef
877  br i1 %cmp48, label %49, label %48
878  %val49 = load volatile float, float* undef
879  %cmp49 = fcmp une float %val49, undef
880  br i1 %cmp49, label %50, label %49
881  %val50 = load volatile float, float* undef
882  %cmp50 = fcmp une float %val50, undef
883  br i1 %cmp50, label %51, label %50
884  %val51 = load volatile float, float* undef
885  %cmp51 = fcmp une float %val51, undef
886  br i1 %cmp51, label %52, label %51
887  %val52 = load volatile float, float* undef
888  %cmp52 = fcmp une float %val52, undef
889  br i1 %cmp52, label %53, label %52
890  %val53 = load volatile float, float* undef
891  %cmp53 = fcmp une float %val53, undef
892  br i1 %cmp53, label %54, label %53
893  %val54 = load volatile float, float* undef
894  %cmp54 = fcmp une float %val54, undef
895  br i1 %cmp54, label %55, label %54
896  %val55 = load volatile float, float* undef
897  %cmp55 = fcmp une float %val55, undef
898  br i1 %cmp55, label %56, label %55
899  %val56 = load volatile float, float* undef
900  %cmp56 = fcmp une float %val56, undef
901  br i1 %cmp56, label %57, label %56
902  %val57 = load volatile float, float* undef
903  %cmp57 = fcmp une float %val57, undef
904  br i1 %cmp57, label %58, label %57
905  %val58 = load volatile float, float* undef
906  %cmp58 = fcmp une float %val58, undef
907  br i1 %cmp58, label %59, label %58
908  %val59 = load volatile float, float* undef
909  %cmp59 = fcmp une float %val59, undef
910  br i1 %cmp59, label %60, label %59
911  %val60 = load volatile float, float* undef
912  %cmp60 = fcmp une float %val60, undef
913  br i1 %cmp60, label %61, label %60
914  %val61 = load volatile float, float* undef
915  %cmp61 = fcmp une float %val61, undef
916  br i1 %cmp61, label %62, label %61
917  %val62 = load volatile float, float* undef
918  %cmp62 = fcmp une float %val62, undef
919  br i1 %cmp62, label %63, label %62
920  %val63 = load volatile float, float* undef
921  %cmp63 = fcmp une float %val63, undef
922  br i1 %cmp63, label %64, label %63
923  %val64 = load volatile float, float* undef
924  %cmp64 = fcmp une float %val64, undef
925  br i1 %cmp64, label %65, label %64
926
927  br label %exit
928exit:
929  ret void
930}
931
932define void @benchmark_heapsort(i32 %n, double* nocapture %ra) {
933; This test case comes from the heapsort benchmark, and exemplifies several
934; important aspects to block placement in the presence of loops:
935; 1) Loop rotation needs to *ensure* that the desired exiting edge can be
936;    a fallthrough.
937; 2) The exiting edge from the loop which is rotated to be laid out at the
938;    bottom of the loop needs to be exiting into the nearest enclosing loop (to
939;    which there is an exit). Otherwise, we force that enclosing loop into
940;    strange layouts that are siginificantly less efficient, often times maing
941;    it discontiguous.
942;
943; CHECK: @benchmark_heapsort
944; CHECK: %entry
945; First rotated loop top.
946; CHECK: .p2align
947; CHECK: %while.end
948; CHECK: %for.cond
949; CHECK: %if.then
950; CHECK: %if.else
951; CHECK: %if.end10
952; Second rotated loop top
953; CHECK: .p2align
954; CHECK: %if.then24
955; CHECK: %while.cond.outer
956; Third rotated loop top
957; CHECK: .p2align
958; CHECK: %while.cond
959; CHECK: %while.body
960; CHECK: %land.lhs.true
961; CHECK: %if.then19
962; CHECK: %if.end20
963; CHECK: %if.then8
964; CHECK: ret
965
966entry:
967  %shr = ashr i32 %n, 1
968  %add = add nsw i32 %shr, 1
969  %arrayidx3 = getelementptr inbounds double, double* %ra, i64 1
970  br label %for.cond
971
972for.cond:
973  %ir.0 = phi i32 [ %n, %entry ], [ %ir.1, %while.end ]
974  %l.0 = phi i32 [ %add, %entry ], [ %l.1, %while.end ]
975  %cmp = icmp sgt i32 %l.0, 1
976  br i1 %cmp, label %if.then, label %if.else
977
978if.then:
979  %dec = add nsw i32 %l.0, -1
980  %idxprom = sext i32 %dec to i64
981  %arrayidx = getelementptr inbounds double, double* %ra, i64 %idxprom
982  %0 = load double, double* %arrayidx, align 8
983  br label %if.end10
984
985if.else:
986  %idxprom1 = sext i32 %ir.0 to i64
987  %arrayidx2 = getelementptr inbounds double, double* %ra, i64 %idxprom1
988  %1 = load double, double* %arrayidx2, align 8
989  %2 = load double, double* %arrayidx3, align 8
990  store double %2, double* %arrayidx2, align 8
991  %dec6 = add nsw i32 %ir.0, -1
992  %cmp7 = icmp eq i32 %dec6, 1
993  br i1 %cmp7, label %if.then8, label %if.end10
994
995if.then8:
996  store double %1, double* %arrayidx3, align 8
997  ret void
998
999if.end10:
1000  %ir.1 = phi i32 [ %ir.0, %if.then ], [ %dec6, %if.else ]
1001  %l.1 = phi i32 [ %dec, %if.then ], [ %l.0, %if.else ]
1002  %rra.0 = phi double [ %0, %if.then ], [ %1, %if.else ]
1003  %add31 = add nsw i32 %ir.1, 1
1004  br label %while.cond.outer
1005
1006while.cond.outer:
1007  %j.0.ph.in = phi i32 [ %l.1, %if.end10 ], [ %j.1, %if.then24 ]
1008  %j.0.ph = shl i32 %j.0.ph.in, 1
1009  br label %while.cond
1010
1011while.cond:
1012  %j.0 = phi i32 [ %add31, %if.end20 ], [ %j.0.ph, %while.cond.outer ]
1013  %cmp11 = icmp sgt i32 %j.0, %ir.1
1014  br i1 %cmp11, label %while.end, label %while.body
1015
1016while.body:
1017  %cmp12 = icmp slt i32 %j.0, %ir.1
1018  br i1 %cmp12, label %land.lhs.true, label %if.end20
1019
1020land.lhs.true:
1021  %idxprom13 = sext i32 %j.0 to i64
1022  %arrayidx14 = getelementptr inbounds double, double* %ra, i64 %idxprom13
1023  %3 = load double, double* %arrayidx14, align 8
1024  %add15 = add nsw i32 %j.0, 1
1025  %idxprom16 = sext i32 %add15 to i64
1026  %arrayidx17 = getelementptr inbounds double, double* %ra, i64 %idxprom16
1027  %4 = load double, double* %arrayidx17, align 8
1028  %cmp18 = fcmp olt double %3, %4
1029  br i1 %cmp18, label %if.then19, label %if.end20
1030
1031if.then19:
1032  br label %if.end20
1033
1034if.end20:
1035  %j.1 = phi i32 [ %add15, %if.then19 ], [ %j.0, %land.lhs.true ], [ %j.0, %while.body ]
1036  %idxprom21 = sext i32 %j.1 to i64
1037  %arrayidx22 = getelementptr inbounds double, double* %ra, i64 %idxprom21
1038  %5 = load double, double* %arrayidx22, align 8
1039  %cmp23 = fcmp olt double %rra.0, %5
1040  br i1 %cmp23, label %if.then24, label %while.cond
1041
1042if.then24:
1043  %idxprom27 = sext i32 %j.0.ph.in to i64
1044  %arrayidx28 = getelementptr inbounds double, double* %ra, i64 %idxprom27
1045  store double %5, double* %arrayidx28, align 8
1046  br label %while.cond.outer
1047
1048while.end:
1049  %idxprom33 = sext i32 %j.0.ph.in to i64
1050  %arrayidx34 = getelementptr inbounds double, double* %ra, i64 %idxprom33
1051  store double %rra.0, double* %arrayidx34, align 8
1052  br label %for.cond
1053}
1054
1055declare void @cold_function() cold
1056
1057define i32 @test_cold_calls(i32* %a) {
1058; Test that edges to blocks post-dominated by cold calls are
1059; marked as not expected to be taken.  They should be laid out
1060; at the bottom.
1061; CHECK-LABEL: test_cold_calls:
1062; CHECK: %entry
1063; CHECK: %else
1064; CHECK: %exit
1065; CHECK: %then
1066
1067entry:
1068  %gep1 = getelementptr i32, i32* %a, i32 1
1069  %val1 = load i32, i32* %gep1
1070  %cond1 = icmp ugt i32 %val1, 1
1071  br i1 %cond1, label %then, label %else
1072
1073then:
1074  call void @cold_function()
1075  br label %exit
1076
1077else:
1078  %gep2 = getelementptr i32, i32* %a, i32 2
1079  %val2 = load i32, i32* %gep2
1080  br label %exit
1081
1082exit:
1083  %ret = phi i32 [ %val1, %then ], [ %val2, %else ]
1084  ret i32 %ret
1085}
1086
1087; Make sure we put landingpads out of the way.
1088declare i32 @pers(...)
1089
1090declare i32 @foo();
1091
1092declare i32 @bar();
1093
1094define i32 @test_lp(i32 %a) personality i32 (...)* @pers {
1095; CHECK-LABEL: test_lp:
1096; CHECK: %entry
1097; CHECK: %hot
1098; CHECK: %then
1099; CHECK: %cold
1100; CHECK: %coldlp
1101; CHECK: %hotlp
1102; CHECK: %lpret
1103entry:
1104  %0 = icmp sgt i32 %a, 1
1105  br i1 %0, label %hot, label %cold, !prof !4
1106
1107hot:
1108  %1 = invoke i32 @foo()
1109          to label %then unwind label %hotlp
1110
1111cold:
1112  %2 = invoke i32 @bar()
1113          to label %then unwind label %coldlp
1114
1115then:
1116  %3 = phi i32 [ %1, %hot ], [ %2, %cold ]
1117  ret i32 %3
1118
1119hotlp:
1120  %4 = landingpad { i8*, i32 }
1121          cleanup
1122  br label %lpret
1123
1124coldlp:
1125  %5 = landingpad { i8*, i32 }
1126          cleanup
1127  br label %lpret
1128
1129lpret:
1130  %6 = phi i32 [-1, %hotlp], [-2, %coldlp]
1131  %7 = add i32 %6, 42
1132  ret i32 %7
1133}
1134
1135!4 = !{!"branch_weights", i32 65536, i32 0}
1136
1137; Make sure that ehpad are scheduled from the least probable one
1138; to the most probable one. See selectBestCandidateBlock as to why.
1139declare void @clean();
1140
1141define void @test_flow_unwind() personality i32 (...)* @pers {
1142; CHECK-LABEL: test_flow_unwind:
1143; CHECK: %entry
1144; CHECK: %then
1145; CHECK: %exit
1146; CHECK: %innerlp
1147; CHECK: %outerlp
1148; CHECK: %outercleanup
1149entry:
1150  %0 = invoke i32 @foo()
1151          to label %then unwind label %outerlp
1152
1153then:
1154  %1 = invoke i32 @bar()
1155          to label %exit unwind label %innerlp
1156
1157exit:
1158  ret void
1159
1160innerlp:
1161  %2 = landingpad { i8*, i32 }
1162          cleanup
1163  br label %innercleanup
1164
1165outerlp:
1166  %3 = landingpad { i8*, i32 }
1167          cleanup
1168  br label %outercleanup
1169
1170outercleanup:
1171  %4 = phi { i8*, i32 } [%2, %innercleanup], [%3, %outerlp]
1172  call void @clean()
1173  resume { i8*, i32 } %4
1174
1175innercleanup:
1176  call void @clean()
1177  br label %outercleanup
1178}
1179
1180declare void @hot_function()
1181
1182define void @test_hot_branch(i32* %a) {
1183; Test that a hot branch that has a probability a little larger than 80% will
1184; break CFG constrains when doing block placement.
1185; CHECK-LABEL: test_hot_branch:
1186; CHECK: %entry
1187; CHECK: %then
1188; CHECK: %exit
1189; CHECK: %else
1190
1191entry:
1192  %gep1 = getelementptr i32, i32* %a, i32 1
1193  %val1 = load i32, i32* %gep1
1194  %cond1 = icmp ugt i32 %val1, 1
1195  br i1 %cond1, label %then, label %else, !prof !5
1196
1197then:
1198  call void @hot_function()
1199  br label %exit
1200
1201else:
1202  call void @cold_function()
1203  br label %exit
1204
1205exit:
1206  call void @hot_function()
1207  ret void
1208}
1209
1210define void @test_hot_branch_profile(i32* %a) !prof !6 {
1211; Test that a hot branch that has a probability a little larger than 50% will
1212; break CFG constrains when doing block placement when profile is available.
1213; CHECK-LABEL: test_hot_branch_profile:
1214; CHECK: %entry
1215; CHECK: %then
1216; CHECK: %exit
1217; CHECK: %else
1218
1219entry:
1220  %gep1 = getelementptr i32, i32* %a, i32 1
1221  %val1 = load i32, i32* %gep1
1222  %cond1 = icmp ugt i32 %val1, 1
1223  br i1 %cond1, label %then, label %else, !prof !7
1224
1225then:
1226  call void @hot_function()
1227  br label %exit
1228
1229else:
1230  call void @cold_function()
1231  br label %exit
1232
1233exit:
1234  call void @hot_function()
1235  ret void
1236}
1237
1238define void @test_hot_branch_triangle_profile(i32* %a) !prof !6 {
1239; Test that a hot branch that has a probability a little larger than 80% will
1240; break triangle shaped CFG constrains when doing block placement if profile
1241; is present.
1242; CHECK-LABEL: test_hot_branch_triangle_profile:
1243; CHECK: %entry
1244; CHECK: %exit
1245; CHECK: %then
1246
1247entry:
1248  %gep1 = getelementptr i32, i32* %a, i32 1
1249  %val1 = load i32, i32* %gep1
1250  %cond1 = icmp ugt i32 %val1, 1
1251  br i1 %cond1, label %exit, label %then, !prof !5
1252
1253then:
1254  call void @hot_function()
1255  br label %exit
1256
1257exit:
1258  call void @hot_function()
1259  ret void
1260}
1261
1262define void @test_hot_branch_triangle_profile_topology(i32* %a) !prof !6 {
1263; Test that a hot branch that has a probability between 50% and 66% will not
1264; break triangle shaped CFG constrains when doing block placement if profile
1265; is present.
1266; CHECK-LABEL: test_hot_branch_triangle_profile_topology:
1267; CHECK: %entry
1268; CHECK: %then
1269; CHECK: %exit
1270
1271entry:
1272  %gep1 = getelementptr i32, i32* %a, i32 1
1273  %val1 = load i32, i32* %gep1
1274  %cond1 = icmp ugt i32 %val1, 1
1275  br i1 %cond1, label %exit, label %then, !prof !7
1276
1277then:
1278  call void @hot_function()
1279  br label %exit
1280
1281exit:
1282  call void @hot_function()
1283  ret void
1284}
1285
1286!5 = !{!"branch_weights", i32 84, i32 16}
1287!6 = !{!"function_entry_count", i32 10}
1288!7 = !{!"branch_weights", i32 60, i32 40}
1289