1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -simplifycfg -S -o - < %s | FileCheck %s
4 declare void @helper(i32)
6 define void @test1(i1 %a, i1 %b) {
9 ; CHECK-NEXT: [[A_NOT:%.*]] = xor i1 [[A:%.*]], true
10 ; CHECK-NEXT: [[C:%.*]] = or i1 [[B:%.*]], false
11 ; CHECK-NEXT: [[OR_COND:%.*]] = and i1 [[A_NOT]], [[C]]
12 ; CHECK-NEXT: br i1 [[OR_COND]], label [[Z:%.*]], label [[Y:%.*]], !prof !0
14 ; CHECK-NEXT: call void @helper(i32 0)
15 ; CHECK-NEXT: ret void
17 ; CHECK-NEXT: call void @helper(i32 1)
18 ; CHECK-NEXT: ret void
21 br i1 %a, label %Y, label %X, !prof !0
25 br i1 %c, label %Z, label %Y, !prof !1
28 call void @helper(i32 0)
32 call void @helper(i32 1)
36 ; Make sure the metadata name string is "branch_weights" before propagating it.
38 define void @fake_weights(i1 %a, i1 %b) {
39 ; CHECK-LABEL: @fake_weights(
41 ; CHECK-NEXT: [[A_NOT:%.*]] = xor i1 [[A:%.*]], true
42 ; CHECK-NEXT: [[C:%.*]] = or i1 [[B:%.*]], false
43 ; CHECK-NEXT: [[OR_COND:%.*]] = and i1 [[A_NOT]], [[C]]
44 ; CHECK-NEXT: br i1 [[OR_COND]], label [[Z:%.*]], label [[Y:%.*]], !prof !1
46 ; CHECK-NEXT: call void @helper(i32 0)
47 ; CHECK-NEXT: ret void
49 ; CHECK-NEXT: call void @helper(i32 1)
50 ; CHECK-NEXT: ret void
53 br i1 %a, label %Y, label %X, !prof !12
56 br i1 %c, label %Z, label %Y, !prof !1
59 call void @helper(i32 0)
63 call void @helper(i32 1)
67 define void @test2(i1 %a, i1 %b) {
68 ; CHECK-LABEL: @test2(
70 ; CHECK-NEXT: [[C:%.*]] = or i1 [[B:%.*]], false
71 ; CHECK-NEXT: [[OR_COND:%.*]] = and i1 [[A:%.*]], [[C]]
72 ; CHECK-NEXT: br i1 [[OR_COND]], label [[Z:%.*]], label [[Y:%.*]], !prof !2
74 ; CHECK-NEXT: call void @helper(i32 0)
75 ; CHECK-NEXT: ret void
77 ; CHECK-NEXT: call void @helper(i32 1)
78 ; CHECK-NEXT: ret void
81 br i1 %a, label %X, label %Y, !prof !1
85 br i1 %c, label %Z, label %Y, !prof !2
88 call void @helper(i32 0)
92 call void @helper(i32 1)
96 define void @test3(i1 %a, i1 %b) {
97 ; CHECK-LABEL: @test3(
99 ; CHECK-NEXT: [[C:%.*]] = or i1 [[B:%.*]], false
100 ; CHECK-NEXT: [[OR_COND:%.*]] = and i1 [[A:%.*]], [[C]]
101 ; CHECK-NEXT: br i1 [[OR_COND]], label [[Z:%.*]], label [[Y:%.*]], !prof !1
103 ; CHECK-NEXT: call void @helper(i32 0)
104 ; CHECK-NEXT: ret void
106 ; CHECK-NEXT: call void @helper(i32 1)
107 ; CHECK-NEXT: ret void
110 br i1 %a, label %X, label %Y, !prof !1
114 br i1 %c, label %Z, label %Y
117 call void @helper(i32 0)
121 call void @helper(i32 1)
125 define void @test4(i1 %a, i1 %b) {
126 ; CHECK-LABEL: @test4(
128 ; CHECK-NEXT: [[C:%.*]] = or i1 [[B:%.*]], false
129 ; CHECK-NEXT: [[OR_COND:%.*]] = and i1 [[A:%.*]], [[C]]
130 ; CHECK-NEXT: br i1 [[OR_COND]], label [[Z:%.*]], label [[Y:%.*]], !prof !1
132 ; CHECK-NEXT: call void @helper(i32 0)
133 ; CHECK-NEXT: ret void
135 ; CHECK-NEXT: call void @helper(i32 1)
136 ; CHECK-NEXT: ret void
139 br i1 %a, label %X, label %Y
143 br i1 %c, label %Z, label %Y, !prof !1
146 call void @helper(i32 0)
150 call void @helper(i32 1)
154 ;; test5 - The case where it jumps to the default target will be removed.
155 define void @test5(i32 %M, i32 %N) nounwind uwtable {
156 ; CHECK-LABEL: @test5(
158 ; CHECK-NEXT: switch i32 [[N:%.*]], label [[SW2:%.*]] [
159 ; CHECK-NEXT: i32 3, label [[SW_BB1:%.*]]
160 ; CHECK-NEXT: i32 2, label [[SW_BB:%.*]]
161 ; CHECK-NEXT: ], !prof !3
163 ; CHECK-NEXT: call void @helper(i32 0)
164 ; CHECK-NEXT: br label [[SW_EPILOG:%.*]]
166 ; CHECK-NEXT: call void @helper(i32 1)
167 ; CHECK-NEXT: br label [[SW_EPILOG]]
169 ; CHECK-NEXT: call void @helper(i32 2)
170 ; CHECK-NEXT: br label [[SW_EPILOG]]
172 ; CHECK-NEXT: ret void
175 switch i32 %N, label %sw2 [
182 call void @helper(i32 0)
186 call void @helper(i32 1)
190 call void @helper(i32 2)
197 ;; test6 - Some cases of the second switch are pruned during optimization.
198 ;; Then the second switch will be converted to a branch, finally, the first
199 ;; switch and the branch will be merged into a single switch.
200 define void @test6(i32 %M, i32 %N) nounwind uwtable {
201 ; CHECK-LABEL: @test6(
203 ; CHECK-NEXT: switch i32 [[N:%.*]], label [[SW_EPILOG:%.*]] [
204 ; CHECK-NEXT: i32 3, label [[SW_BB1:%.*]]
205 ; CHECK-NEXT: i32 2, label [[SW_BB:%.*]]
206 ; CHECK-NEXT: i32 4, label [[SW_BB5:%.*]]
207 ; CHECK-NEXT: ], !prof !4
209 ; CHECK-NEXT: call void @helper(i32 0)
210 ; CHECK-NEXT: br label [[SW_EPILOG]]
212 ; CHECK-NEXT: call void @helper(i32 1)
213 ; CHECK-NEXT: br label [[SW_EPILOG]]
215 ; CHECK-NEXT: call void @helper(i32 3)
216 ; CHECK-NEXT: br label [[SW_EPILOG]]
218 ; CHECK-NEXT: ret void
221 switch i32 %N, label %sw2 [
228 call void @helper(i32 0)
232 call void @helper(i32 1)
236 ;; Here "case 2" is invalidated since the default case of the first switch
237 ;; does not include "case 2".
238 switch i32 %N, label %sw.epilog [
244 call void @helper(i32 2)
248 call void @helper(i32 3)
255 ;; This test is based on test1 but swapped the targets of the second branch.
256 define void @test1_swap(i1 %a, i1 %b) {
257 ; CHECK-LABEL: @test1_swap(
259 ; CHECK-NEXT: [[C:%.*]] = or i1 [[B:%.*]], false
260 ; CHECK-NEXT: [[OR_COND:%.*]] = or i1 [[A:%.*]], [[C]]
261 ; CHECK-NEXT: br i1 [[OR_COND]], label [[Y:%.*]], label [[Z:%.*]], !prof !5
263 ; CHECK-NEXT: call void @helper(i32 0)
264 ; CHECK-NEXT: ret void
266 ; CHECK-NEXT: call void @helper(i32 1)
267 ; CHECK-NEXT: ret void
270 br i1 %a, label %Y, label %X, !prof !0
274 br i1 %c, label %Y, label %Z, !prof !1
277 call void @helper(i32 0)
281 call void @helper(i32 1)
285 define void @test7(i1 %a, i1 %b) {
286 ; CHECK-LABEL: @test7(
288 ; CHECK-NEXT: [[C:%.*]] = or i1 [[B:%.*]], false
289 ; CHECK-NEXT: [[BRMERGE:%.*]] = or i1 [[A:%.*]], [[C]]
290 ; CHECK-NEXT: br i1 [[BRMERGE]], label [[Y:%.*]], label [[Z:%.*]], !prof !6
292 ; CHECK-NEXT: call void @helper(i32 0)
293 ; CHECK-NEXT: ret void
295 ; CHECK-NEXT: call void @helper(i32 1)
296 ; CHECK-NEXT: ret void
300 br i1 %a, label %Y, label %X, !prof !0
303 br i1 %c, label %Y, label %Z, !prof !6
306 call void @helper(i32 0)
310 call void @helper(i32 1)
314 ; Test basic folding to a conditional branch.
315 define void @test8(i64 %x, i64 %y) nounwind {
316 ; CHECK-LABEL: @test8(
318 ; CHECK-NEXT: [[LT:%.*]] = icmp slt i64 [[X:%.*]], [[Y:%.*]]
319 ; CHECK-NEXT: br i1 [[LT]], label [[A:%.*]], label [[B:%.*]], !prof !7
321 ; CHECK-NEXT: call void @helper(i32 0) #1
322 ; CHECK-NEXT: ret void
324 ; CHECK-NEXT: call void @helper(i32 1) #1
325 ; CHECK-NEXT: ret void
328 %lt = icmp slt i64 %x, %y
329 %qux = select i1 %lt, i32 0, i32 2
330 switch i32 %qux, label %bees [
336 call void @helper(i32 0) nounwind
339 call void @helper(i32 1) nounwind
342 call void @helper(i32 2) nounwind
346 ; Test edge splitting when the default target has icmp and unconditinal
348 define i1 @test9(i32 %x, i32 %y) nounwind {
349 ; CHECK-LABEL: @test9(
351 ; CHECK-NEXT: switch i32 [[X:%.*]], label [[BEES:%.*]] [
352 ; CHECK-NEXT: i32 0, label [[A:%.*]]
353 ; CHECK-NEXT: i32 1, label [[END:%.*]]
354 ; CHECK-NEXT: i32 2, label [[END]]
355 ; CHECK-NEXT: i32 92, label [[END]]
356 ; CHECK-NEXT: ], !prof !8
358 ; CHECK-NEXT: call void @helper(i32 0) #1
359 ; CHECK-NEXT: [[RETA:%.*]] = icmp slt i32 [[X]], [[Y:%.*]]
360 ; CHECK-NEXT: ret i1 [[RETA]]
362 ; CHECK-NEXT: br label [[END]]
364 ; CHECK-NEXT: [[RET:%.*]] = phi i1 [ true, [[ENTRY:%.*]] ], [ false, [[BEES]] ], [ true, [[ENTRY]] ], [ true, [[ENTRY]] ]
365 ; CHECK-NEXT: call void @helper(i32 2) #1
366 ; CHECK-NEXT: ret i1 [[RET]]
369 switch i32 %x, label %bees [
376 call void @helper(i32 0) nounwind
377 %reta = icmp slt i32 %x, %y
381 %tmp = icmp eq i32 %x, 92
385 %ret = phi i1 [ true, %entry ], [%tmp, %bees], [true, %entry]
386 call void @helper(i32 2) nounwind
390 define void @test10(i32 %x) nounwind readnone ssp noredzone {
391 ; CHECK-LABEL: @test10(
393 ; CHECK-NEXT: [[X_OFF:%.*]] = add i32 [[X:%.*]], -1
394 ; CHECK-NEXT: [[SWITCH:%.*]] = icmp ult i32 [[X_OFF]], 3
395 ; CHECK-NEXT: br i1 [[SWITCH]], label [[LOR_END:%.*]], label [[LOR_RHS:%.*]], !prof !9
397 ; CHECK-NEXT: call void @helper(i32 1) #1
398 ; CHECK-NEXT: ret void
400 ; CHECK-NEXT: call void @helper(i32 0) #1
401 ; CHECK-NEXT: ret void
404 switch i32 %x, label %lor.rhs [
405 i32 2, label %lor.end
406 i32 1, label %lor.end
407 i32 3, label %lor.end
411 call void @helper(i32 1) nounwind
415 call void @helper(i32 0) nounwind
420 ; Remove dead cases from the switch.
421 define void @test11(i32 %x) nounwind {
422 ; CHECK-LABEL: @test11(
423 ; CHECK-NEXT: [[I:%.*]] = shl i32 [[X:%.*]], 1
424 ; CHECK-NEXT: [[COND:%.*]] = icmp eq i32 [[I]], 24
425 ; CHECK-NEXT: br i1 [[COND]], label [[C:%.*]], label [[A:%.*]], !prof !10
427 ; CHECK-NEXT: call void @helper(i32 0) #1
428 ; CHECK-NEXT: ret void
430 ; CHECK-NEXT: call void @helper(i32 2) #1
431 ; CHECK-NEXT: ret void
434 switch i32 %i, label %a [
440 call void @helper(i32 0) nounwind
443 call void @helper(i32 1) nounwind
446 call void @helper(i32 2) nounwind
450 ;; test12 - Don't crash if the whole switch is removed
451 define void @test12(i32 %M, i32 %N) nounwind uwtable {
452 ; CHECK-LABEL: @test12(
454 ; CHECK-NEXT: call void @helper(i32 0)
455 ; CHECK-NEXT: ret void
458 switch i32 %N, label %sw.bb [
463 call void @helper(i32 0)
470 ;; If every case is dead, make sure they are all removed. This used to
471 ;; crash trying to merge the metadata.
472 define void @test13(i32 %x) nounwind {
473 ; CHECK-LABEL: @test13(
475 ; CHECK-NEXT: call void @helper(i32 0) #1
476 ; CHECK-NEXT: ret void
480 switch i32 %i, label %a [
486 call void @helper(i32 0) nounwind
489 call void @helper(i32 1) nounwind
492 call void @helper(i32 2) nounwind
496 ;; When folding branches to common destination, the updated branch weights
497 ;; can exceed uint32 by more than factor of 2. We should keep halving the
498 ;; weights until they can fit into uint32.
499 @max_regno = common global i32 0, align 4
500 define void @test14(i32* %old, i32 %final) {
501 ; CHECK-LABEL: @test14(
502 ; CHECK-NEXT: for.cond:
503 ; CHECK-NEXT: br label [[FOR_COND2:%.*]]
505 ; CHECK-NEXT: [[I_1:%.*]] = phi i32 [ [[INC19:%.*]], [[FOR_INC:%.*]] ], [ 0, [[FOR_COND:%.*]] ]
506 ; CHECK-NEXT: [[BIT_0:%.*]] = phi i32 [ [[SHL:%.*]], [[FOR_INC]] ], [ 1, [[FOR_COND]] ]
507 ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i32 [[BIT_0]], 0
508 ; CHECK-NEXT: [[V3:%.*]] = load i32, i32* @max_regno, align 4
509 ; CHECK-NEXT: [[CMP4:%.*]] = icmp eq i32 [[I_1]], [[V3]]
510 ; CHECK-NEXT: [[OR_COND:%.*]] = or i1 [[TOBOOL]], [[CMP4]]
511 ; CHECK-NEXT: br i1 [[OR_COND]], label [[FOR_EXIT:%.*]], label [[FOR_INC]], !prof !11
513 ; CHECK-NEXT: [[SHL]] = shl i32 [[BIT_0]], 1
514 ; CHECK-NEXT: [[INC19]] = add nsw i32 [[I_1]], 1
515 ; CHECK-NEXT: br label [[FOR_COND2]]
517 ; CHECK-NEXT: ret void
522 %i.1 = phi i32 [ %inc19, %for.inc ], [ 0, %for.cond ]
523 %bit.0 = phi i32 [ %shl, %for.inc ], [ 1, %for.cond ]
524 %tobool = icmp eq i32 %bit.0, 0
525 br i1 %tobool, label %for.exit, label %for.body3, !prof !10
527 %v3 = load i32, i32* @max_regno, align 4
528 %cmp4 = icmp eq i32 %i.1, %v3
529 br i1 %cmp4, label %for.exit, label %for.inc, !prof !11
531 %shl = shl i32 %bit.0, 1
532 %inc19 = add nsw i32 %i.1, 1
538 ; Don't drop the metadata.
540 define i32 @HoistThenElseCodeToIf(i32 %n) {
541 ; CHECK-LABEL: @HoistThenElseCodeToIf(
543 ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i32 [[N:%.*]], 0
544 ; CHECK-NEXT: [[DOT:%.*]] = select i1 [[TOBOOL]], i32 1, i32 234, !prof !12
545 ; CHECK-NEXT: ret i32 [[DOT]]
548 %tobool = icmp eq i32 %n, 0
549 br i1 %tobool, label %if, label %else, !prof !0
558 %retval.0 = phi i32 [ 1, %if ], [ 234, %else ]
562 ; The selects should have freshly calculated branch weights.
564 define i32 @SimplifyCondBranchToCondBranch(i1 %cmpa, i1 %cmpb) {
565 ; CHECK-LABEL: @SimplifyCondBranchToCondBranch(
566 ; CHECK-NEXT: block1:
567 ; CHECK-NEXT: [[BRMERGE:%.*]] = or i1 [[CMPA:%.*]], [[CMPB:%.*]]
568 ; CHECK-NEXT: [[DOTMUX:%.*]] = select i1 [[CMPA]], i32 0, i32 2, !prof !13
569 ; CHECK-NEXT: [[OUTVAL:%.*]] = select i1 [[BRMERGE]], i32 [[DOTMUX]], i32 1, !prof !14
570 ; CHECK-NEXT: ret i32 [[OUTVAL]]
573 br i1 %cmpa, label %block3, label %block2, !prof !13
576 br i1 %cmpb, label %block3, label %exit, !prof !14
579 %cowval = phi i32 [ 2, %block2 ], [ 0, %block1 ]
583 %outval = phi i32 [ %cowval, %block3 ], [ 1, %block2 ]
587 ; Swap the operands of the compares to verify that the weights update correctly.
589 define i32 @SimplifyCondBranchToCondBranchSwap(i1 %cmpa, i1 %cmpb) {
590 ; CHECK-LABEL: @SimplifyCondBranchToCondBranchSwap(
591 ; CHECK-NEXT: block1:
592 ; CHECK-NEXT: [[CMPA_NOT:%.*]] = xor i1 [[CMPA:%.*]], true
593 ; CHECK-NEXT: [[CMPB_NOT:%.*]] = xor i1 [[CMPB:%.*]], true
594 ; CHECK-NEXT: [[BRMERGE:%.*]] = or i1 [[CMPA_NOT]], [[CMPB_NOT]]
595 ; CHECK-NEXT: [[DOTMUX:%.*]] = select i1 [[CMPA_NOT]], i32 0, i32 2, !prof !15
596 ; CHECK-NEXT: [[OUTVAL:%.*]] = select i1 [[BRMERGE]], i32 [[DOTMUX]], i32 1, !prof !16
597 ; CHECK-NEXT: ret i32 [[OUTVAL]]
600 br i1 %cmpa, label %block2, label %block3, !prof !13
603 br i1 %cmpb, label %exit, label %block3, !prof !14
606 %cowval = phi i32 [ 2, %block2 ], [ 0, %block1 ]
610 %outval = phi i32 [ %cowval, %block3 ], [ 1, %block2 ]
614 define i32 @SimplifyCondBranchToCondBranchSwapMissingWeight(i1 %cmpa, i1 %cmpb) {
615 ; CHECK-LABEL: @SimplifyCondBranchToCondBranchSwapMissingWeight(
616 ; CHECK-NEXT: block1:
617 ; CHECK-NEXT: [[CMPA_NOT:%.*]] = xor i1 [[CMPA:%.*]], true
618 ; CHECK-NEXT: [[CMPB_NOT:%.*]] = xor i1 [[CMPB:%.*]], true
619 ; CHECK-NEXT: [[BRMERGE:%.*]] = or i1 [[CMPA_NOT]], [[CMPB_NOT]]
620 ; CHECK-NEXT: [[DOTMUX:%.*]] = select i1 [[CMPA_NOT]], i32 0, i32 2, !prof !17
621 ; CHECK-NEXT: [[OUTVAL:%.*]] = select i1 [[BRMERGE]], i32 [[DOTMUX]], i32 1, !prof !18
622 ; CHECK-NEXT: ret i32 [[OUTVAL]]
625 br i1 %cmpa, label %block2, label %block3, !prof !13
628 br i1 %cmpb, label %exit, label %block3
631 %cowval = phi i32 [ 2, %block2 ], [ 0, %block1 ]
635 %outval = phi i32 [ %cowval, %block3 ], [ 1, %block2 ]
639 !0 = !{!"branch_weights", i32 3, i32 5}
640 !1 = !{!"branch_weights", i32 1, i32 1}
641 !2 = !{!"branch_weights", i32 1, i32 2}
642 !3 = !{!"branch_weights", i32 4, i32 3, i32 2, i32 1}
643 !4 = !{!"branch_weights", i32 4, i32 3, i32 2, i32 1}
644 !5 = !{!"branch_weights", i32 7, i32 6, i32 5}
645 !6 = !{!"branch_weights", i32 1, i32 3}
646 !7 = !{!"branch_weights", i32 33, i32 9, i32 8, i32 7}
647 !8 = !{!"branch_weights", i32 33, i32 9, i32 8}
648 !9 = !{!"branch_weights", i32 7, i32 6}
649 !10 = !{!"branch_weights", i32 672646, i32 21604207}
650 !11 = !{!"branch_weights", i32 6960, i32 21597248}
651 !12 = !{!"these_are_not_the_branch_weights_you_are_looking_for", i32 3, i32 5}
652 !13 = !{!"branch_weights", i32 2, i32 3}
653 !14 = !{!"branch_weights", i32 4, i32 7}
655 ; CHECK: !0 = !{!"branch_weights", i32 5, i32 11}
656 ; CHECK: !1 = !{!"branch_weights", i32 1, i32 3}
657 ; CHECK: !2 = !{!"branch_weights", i32 1, i32 5}
658 ; CHECK: !3 = !{!"branch_weights", i32 7, i32 1, i32 2}
659 ; CHECK: !4 = !{!"branch_weights", i32 49, i32 12, i32 24, i32 35}
660 ; CHECK: !5 = !{!"branch_weights", i32 11, i32 5}
661 ; CHECK: !6 = !{!"branch_weights", i32 17, i32 15}
662 ; CHECK: !7 = !{!"branch_weights", i32 9, i32 7}
663 ; CHECK: !8 = !{!"branch_weights", i32 17, i32 9, i32 8, i32 7, i32 17}
664 ; CHECK: !9 = !{!"branch_weights", i32 24, i32 33}
665 ; CHECK: !10 = !{!"branch_weights", i32 8, i32 33}
666 ;; The false weight prints out as a negative integer here, but inside llvm, we
667 ;; treat the weight as an unsigned integer.
668 ; CHECK: !11 = !{!"branch_weights", i32 112017436, i32 -735157296}
669 ; CHECK: !12 = !{!"branch_weights", i32 3, i32 5}
670 ; CHECK: !13 = !{!"branch_weights", i32 22, i32 12}
671 ; CHECK: !14 = !{!"branch_weights", i32 34, i32 21}
672 ; CHECK: !15 = !{!"branch_weights", i32 33, i32 14}
673 ; CHECK: !16 = !{!"branch_weights", i32 47, i32 8}
674 ; CHECK: !17 = !{!"branch_weights", i32 6, i32 2}
675 ; CHECK: !18 = !{!"branch_weights", i32 8, i32 2}