1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals
2 ; RUN: opt -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -switch-range-to-icmp -S -o - < %s | FileCheck %s
4 declare void @helper(i32)
7 ; CHECK: @max_regno = common global i32 0, align 4
9 define void @test1(i1 %a, i1 %b) {
10 ; CHECK-LABEL: @test1(
12 ; CHECK-NEXT: [[A_NOT:%.*]] = xor i1 [[A:%.*]], true
13 ; CHECK-NEXT: [[C:%.*]] = or i1 [[B:%.*]], false
14 ; CHECK-NEXT: [[OR_COND:%.*]] = select i1 [[A_NOT]], i1 [[C]], i1 false
15 ; CHECK-NEXT: br i1 [[OR_COND]], label [[Z:%.*]], label [[Y:%.*]], !prof [[PROF0:![0-9]+]]
17 ; CHECK-NEXT: ret void
19 ; CHECK-NEXT: call void @helper(i32 0)
20 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
22 ; CHECK-NEXT: call void @helper(i32 1)
23 ; CHECK-NEXT: br label [[COMMON_RET]]
26 br i1 %a, label %Y, label %X, !prof !0
30 br i1 %c, label %Z, label %Y, !prof !1
33 call void @helper(i32 0)
37 call void @helper(i32 1)
41 ; Make sure the metadata name string is "branch_weights" before propagating it.
43 define void @fake_weights(i1 %a, i1 %b) {
44 ; CHECK-LABEL: @fake_weights(
46 ; CHECK-NEXT: [[A_NOT:%.*]] = xor i1 [[A:%.*]], true
47 ; CHECK-NEXT: [[C:%.*]] = or i1 [[B:%.*]], false
48 ; CHECK-NEXT: [[OR_COND:%.*]] = select i1 [[A_NOT]], i1 [[C]], i1 false
49 ; CHECK-NEXT: br i1 [[OR_COND]], label [[Z:%.*]], label [[Y:%.*]], !prof [[PROF1:![0-9]+]]
51 ; CHECK-NEXT: ret void
53 ; CHECK-NEXT: call void @helper(i32 0)
54 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
56 ; CHECK-NEXT: call void @helper(i32 1)
57 ; CHECK-NEXT: br label [[COMMON_RET]]
60 br i1 %a, label %Y, label %X, !prof !12
63 br i1 %c, label %Z, label %Y, !prof !1
66 call void @helper(i32 0)
70 call void @helper(i32 1)
74 define void @test2(i1 %a, i1 %b) {
75 ; CHECK-LABEL: @test2(
77 ; CHECK-NEXT: [[C:%.*]] = or i1 [[B:%.*]], false
78 ; CHECK-NEXT: [[OR_COND:%.*]] = select i1 [[A:%.*]], i1 [[C]], i1 false
79 ; CHECK-NEXT: br i1 [[OR_COND]], label [[Z:%.*]], label [[Y:%.*]], !prof [[PROF2:![0-9]+]]
81 ; CHECK-NEXT: ret void
83 ; CHECK-NEXT: call void @helper(i32 0)
84 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
86 ; CHECK-NEXT: call void @helper(i32 1)
87 ; CHECK-NEXT: br label [[COMMON_RET]]
90 br i1 %a, label %X, label %Y, !prof !1
94 br i1 %c, label %Z, label %Y, !prof !2
97 call void @helper(i32 0)
101 call void @helper(i32 1)
105 define void @test3(i1 %a, i1 %b) {
106 ; CHECK-LABEL: @test3(
108 ; CHECK-NEXT: [[C:%.*]] = or i1 [[B:%.*]], false
109 ; CHECK-NEXT: [[OR_COND:%.*]] = select i1 [[A:%.*]], i1 [[C]], i1 false
110 ; CHECK-NEXT: br i1 [[OR_COND]], label [[Z:%.*]], label [[Y:%.*]], !prof [[PROF1]]
112 ; CHECK-NEXT: ret void
114 ; CHECK-NEXT: call void @helper(i32 0)
115 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
117 ; CHECK-NEXT: call void @helper(i32 1)
118 ; CHECK-NEXT: br label [[COMMON_RET]]
121 br i1 %a, label %X, label %Y, !prof !1
125 br i1 %c, label %Z, label %Y
128 call void @helper(i32 0)
132 call void @helper(i32 1)
136 define void @test4(i1 %a, i1 %b) {
137 ; CHECK-LABEL: @test4(
139 ; CHECK-NEXT: [[C:%.*]] = or i1 [[B:%.*]], false
140 ; CHECK-NEXT: [[OR_COND:%.*]] = select i1 [[A:%.*]], i1 [[C]], i1 false
141 ; CHECK-NEXT: br i1 [[OR_COND]], label [[Z:%.*]], label [[Y:%.*]], !prof [[PROF1]]
143 ; CHECK-NEXT: ret void
145 ; CHECK-NEXT: call void @helper(i32 0)
146 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
148 ; CHECK-NEXT: call void @helper(i32 1)
149 ; CHECK-NEXT: br label [[COMMON_RET]]
152 br i1 %a, label %X, label %Y
156 br i1 %c, label %Z, label %Y, !prof !1
159 call void @helper(i32 0)
163 call void @helper(i32 1)
167 ;; test5 - The case where it jumps to the default target will be removed.
168 define void @test5(i32 %M, i32 %N) nounwind uwtable {
169 ; CHECK-LABEL: @test5(
171 ; CHECK-NEXT: switch i32 [[N:%.*]], label [[SW2:%.*]] [
172 ; CHECK-NEXT: i32 3, label [[SW_BB1:%.*]]
173 ; CHECK-NEXT: i32 2, label [[SW_BB:%.*]]
174 ; CHECK-NEXT: ], !prof [[PROF3:![0-9]+]]
176 ; CHECK-NEXT: call void @helper(i32 0)
177 ; CHECK-NEXT: br label [[SW_EPILOG:%.*]]
179 ; CHECK-NEXT: call void @helper(i32 1)
180 ; CHECK-NEXT: br label [[SW_EPILOG]]
182 ; CHECK-NEXT: call void @helper(i32 2)
183 ; CHECK-NEXT: br label [[SW_EPILOG]]
185 ; CHECK-NEXT: ret void
188 switch i32 %N, label %sw2 [
195 call void @helper(i32 0)
199 call void @helper(i32 1)
203 call void @helper(i32 2)
210 ;; test6 - Some cases of the second switch are pruned during optimization.
211 ;; Then the second switch will be converted to a branch, finally, the first
212 ;; switch and the branch will be merged into a single switch.
213 define void @test6(i32 %M, i32 %N) nounwind uwtable {
214 ; CHECK-LABEL: @test6(
216 ; CHECK-NEXT: switch i32 [[N:%.*]], label [[SW_EPILOG:%.*]] [
217 ; CHECK-NEXT: i32 3, label [[SW_BB1:%.*]]
218 ; CHECK-NEXT: i32 2, label [[SW_BB:%.*]]
219 ; CHECK-NEXT: i32 4, label [[SW_BB5:%.*]]
220 ; CHECK-NEXT: ], !prof [[PROF4:![0-9]+]]
222 ; CHECK-NEXT: call void @helper(i32 0)
223 ; CHECK-NEXT: br label [[SW_EPILOG]]
225 ; CHECK-NEXT: call void @helper(i32 1)
226 ; CHECK-NEXT: br label [[SW_EPILOG]]
228 ; CHECK-NEXT: call void @helper(i32 3)
229 ; CHECK-NEXT: br label [[SW_EPILOG]]
231 ; CHECK-NEXT: ret void
234 switch i32 %N, label %sw2 [
241 call void @helper(i32 0)
245 call void @helper(i32 1)
249 ;; Here "case 2" is invalidated since the default case of the first switch
250 ;; does not include "case 2".
251 switch i32 %N, label %sw.epilog [
257 call void @helper(i32 2)
261 call void @helper(i32 3)
268 ;; This test is based on test1 but swapped the targets of the second branch.
269 define void @test1_swap(i1 %a, i1 %b) {
270 ; CHECK-LABEL: @test1_swap(
272 ; CHECK-NEXT: [[C:%.*]] = or i1 [[B:%.*]], false
273 ; CHECK-NEXT: [[OR_COND:%.*]] = select i1 [[A:%.*]], i1 true, i1 [[C]]
274 ; CHECK-NEXT: br i1 [[OR_COND]], label [[Y:%.*]], label [[Z:%.*]], !prof [[PROF5:![0-9]+]]
276 ; CHECK-NEXT: ret void
278 ; CHECK-NEXT: call void @helper(i32 0)
279 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
281 ; CHECK-NEXT: call void @helper(i32 1)
282 ; CHECK-NEXT: br label [[COMMON_RET]]
285 br i1 %a, label %Y, label %X, !prof !0
289 br i1 %c, label %Y, label %Z, !prof !1
292 call void @helper(i32 0)
296 call void @helper(i32 1)
300 define void @test7(i1 %a, i1 %b) {
301 ; CHECK-LABEL: @test7(
303 ; CHECK-NEXT: [[C:%.*]] = or i1 [[B:%.*]], false
304 ; CHECK-NEXT: [[BRMERGE:%.*]] = select i1 [[A:%.*]], i1 true, i1 [[C]]
305 ; CHECK-NEXT: br i1 [[BRMERGE]], label [[Y:%.*]], label [[Z:%.*]], !prof [[PROF6:![0-9]+]]
307 ; CHECK-NEXT: ret void
309 ; CHECK-NEXT: call void @helper(i32 0)
310 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
312 ; CHECK-NEXT: call void @helper(i32 1)
313 ; CHECK-NEXT: br label [[COMMON_RET]]
317 br i1 %a, label %Y, label %X, !prof !0
320 br i1 %c, label %Y, label %Z, !prof !6
323 call void @helper(i32 0)
327 call void @helper(i32 1)
331 ; Test basic folding to a conditional branch.
332 define void @test8(i64 %x, i64 %y) nounwind {
333 ; CHECK-LABEL: @test8(
335 ; CHECK-NEXT: [[LT:%.*]] = icmp slt i64 [[X:%.*]], [[Y:%.*]]
336 ; CHECK-NEXT: br i1 [[LT]], label [[A:%.*]], label [[B:%.*]], !prof [[PROF7:![0-9]+]]
338 ; CHECK-NEXT: ret void
340 ; CHECK-NEXT: call void @helper(i32 0) #[[ATTR1:[0-9]+]]
341 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
343 ; CHECK-NEXT: call void @helper(i32 1) #[[ATTR1]]
344 ; CHECK-NEXT: br label [[COMMON_RET]]
347 %lt = icmp slt i64 %x, %y
348 %qux = select i1 %lt, i32 0, i32 2
349 switch i32 %qux, label %bees [
355 call void @helper(i32 0) nounwind
358 call void @helper(i32 1) nounwind
361 call void @helper(i32 2) nounwind
365 ; Test edge splitting when the default target has icmp and unconditinal
367 define i1 @test9(i32 %x, i32 %y) nounwind {
368 ; CHECK-LABEL: @test9(
370 ; CHECK-NEXT: switch i32 [[X:%.*]], label [[BEES:%.*]] [
371 ; CHECK-NEXT: i32 0, label [[A:%.*]]
372 ; CHECK-NEXT: i32 1, label [[END:%.*]]
373 ; CHECK-NEXT: i32 2, label [[END]]
374 ; CHECK-NEXT: i32 92, label [[END]]
375 ; CHECK-NEXT: ], !prof [[PROF8:![0-9]+]]
377 ; CHECK-NEXT: [[COMMON_RET_OP:%.*]] = phi i1 [ [[RETA:%.*]], [[A]] ], [ [[RET:%.*]], [[END]] ]
378 ; CHECK-NEXT: ret i1 [[COMMON_RET_OP]]
380 ; CHECK-NEXT: call void @helper(i32 0) #[[ATTR1]]
381 ; CHECK-NEXT: [[RETA]] = icmp slt i32 [[X]], [[Y:%.*]]
382 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
384 ; CHECK-NEXT: br label [[END]]
386 ; CHECK-NEXT: [[RET]] = phi i1 [ true, [[ENTRY:%.*]] ], [ false, [[BEES]] ], [ true, [[ENTRY]] ], [ true, [[ENTRY]] ]
387 ; CHECK-NEXT: call void @helper(i32 2) #[[ATTR1]]
388 ; CHECK-NEXT: br label [[COMMON_RET]]
391 switch i32 %x, label %bees [
398 call void @helper(i32 0) nounwind
399 %reta = icmp slt i32 %x, %y
403 %tmp = icmp eq i32 %x, 92
407 %ret = phi i1 [ true, %entry ], [%tmp, %bees], [true, %entry]
408 call void @helper(i32 2) nounwind
412 define void @test10(i32 %x) nounwind readnone ssp noredzone {
413 ; CHECK-LABEL: @test10(
415 ; CHECK-NEXT: [[X_OFF:%.*]] = add i32 [[X:%.*]], -1
416 ; CHECK-NEXT: [[SWITCH:%.*]] = icmp ult i32 [[X_OFF]], 3
417 ; CHECK-NEXT: br i1 [[SWITCH]], label [[LOR_END:%.*]], label [[LOR_RHS:%.*]], !prof [[PROF9:![0-9]+]]
419 ; CHECK-NEXT: ret void
421 ; CHECK-NEXT: call void @helper(i32 1) #[[ATTR1]]
422 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
424 ; CHECK-NEXT: call void @helper(i32 0) #[[ATTR1]]
425 ; CHECK-NEXT: br label [[COMMON_RET]]
428 switch i32 %x, label %lor.rhs [
429 i32 2, label %lor.end
430 i32 1, label %lor.end
431 i32 3, label %lor.end
435 call void @helper(i32 1) nounwind
439 call void @helper(i32 0) nounwind
444 ; Remove dead cases from the switch.
445 define void @test11(i32 %x) nounwind {
446 ; CHECK-LABEL: @test11(
447 ; CHECK-NEXT: [[I:%.*]] = shl i32 [[X:%.*]], 1
448 ; CHECK-NEXT: [[COND:%.*]] = icmp eq i32 [[I]], 24
449 ; CHECK-NEXT: br i1 [[COND]], label [[C:%.*]], label [[A:%.*]], !prof [[PROF10:![0-9]+]]
451 ; CHECK-NEXT: ret void
453 ; CHECK-NEXT: call void @helper(i32 0) #[[ATTR1]]
454 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
456 ; CHECK-NEXT: call void @helper(i32 2) #[[ATTR1]]
457 ; CHECK-NEXT: br label [[COMMON_RET]]
460 switch i32 %i, label %a [
466 call void @helper(i32 0) nounwind
469 call void @helper(i32 1) nounwind
472 call void @helper(i32 2) nounwind
476 ;; test12 - Don't crash if the whole switch is removed
477 define void @test12(i32 %M, i32 %N) nounwind uwtable {
478 ; CHECK-LABEL: @test12(
480 ; CHECK-NEXT: call void @helper(i32 0)
481 ; CHECK-NEXT: ret void
484 switch i32 %N, label %sw.bb [
489 call void @helper(i32 0)
496 ;; If every case is dead, make sure they are all removed. This used to
497 ;; crash trying to merge the metadata.
498 define void @test13(i32 %x) nounwind {
499 ; CHECK-LABEL: @test13(
501 ; CHECK-NEXT: call void @helper(i32 0) #[[ATTR1]]
502 ; CHECK-NEXT: ret void
506 switch i32 %i, label %a [
512 call void @helper(i32 0) nounwind
515 call void @helper(i32 1) nounwind
518 call void @helper(i32 2) nounwind
522 ;; When folding branches to common destination, the updated branch weights
523 ;; can exceed uint32 by more than factor of 2. We should keep halving the
524 ;; weights until they can fit into uint32.
525 @max_regno = common global i32 0, align 4
526 define void @test14(ptr %old, i32 %final) {
527 ; CHECK-LABEL: @test14(
528 ; CHECK-NEXT: for.cond:
529 ; CHECK-NEXT: br label [[FOR_COND2:%.*]]
531 ; CHECK-NEXT: [[I_1:%.*]] = phi i32 [ [[INC19:%.*]], [[FOR_INC:%.*]] ], [ 0, [[FOR_COND:%.*]] ]
532 ; CHECK-NEXT: [[BIT_0:%.*]] = phi i32 [ [[SHL:%.*]], [[FOR_INC]] ], [ 1, [[FOR_COND]] ]
533 ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i32 [[BIT_0]], 0
534 ; CHECK-NEXT: [[V3:%.*]] = load i32, ptr @max_regno, align 4
535 ; CHECK-NEXT: [[CMP4:%.*]] = icmp eq i32 [[I_1]], [[V3]]
536 ; CHECK-NEXT: [[OR_COND:%.*]] = select i1 [[TOBOOL]], i1 true, i1 [[CMP4]]
537 ; CHECK-NEXT: br i1 [[OR_COND]], label [[FOR_EXIT:%.*]], label [[FOR_INC]], !prof [[PROF11:![0-9]+]]
539 ; CHECK-NEXT: [[SHL]] = shl i32 [[BIT_0]], 1
540 ; CHECK-NEXT: [[INC19]] = add nsw i32 [[I_1]], 1
541 ; CHECK-NEXT: br label [[FOR_COND2]]
543 ; CHECK-NEXT: ret void
548 %i.1 = phi i32 [ %inc19, %for.inc ], [ 0, %for.cond ]
549 %bit.0 = phi i32 [ %shl, %for.inc ], [ 1, %for.cond ]
550 %tobool = icmp eq i32 %bit.0, 0
551 br i1 %tobool, label %for.exit, label %for.body3, !prof !10
553 %v3 = load i32, ptr @max_regno, align 4
554 %cmp4 = icmp eq i32 %i.1, %v3
555 br i1 %cmp4, label %for.exit, label %for.inc, !prof !11
557 %shl = shl i32 %bit.0, 1
558 %inc19 = add nsw i32 %i.1, 1
564 ; Don't drop the metadata.
566 define i32 @HoistThenElseCodeToIf(i32 %n) {
567 ; CHECK-LABEL: @HoistThenElseCodeToIf(
569 ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i32 [[N:%.*]], 0
570 ; CHECK-NEXT: [[DOT:%.*]] = select i1 [[TOBOOL]], i32 1, i32 234, !prof [[PROF12:![0-9]+]]
571 ; CHECK-NEXT: ret i32 [[DOT]]
574 %tobool = icmp eq i32 %n, 0
575 br i1 %tobool, label %if, label %else, !prof !0
584 %retval.0 = phi i32 [ 1, %if ], [ 234, %else ]
588 ; The selects should have freshly calculated branch weights.
590 define i32 @SimplifyCondBranchToCondBranch(i1 %cmpa, i1 %cmpb) {
591 ; CHECK-LABEL: @SimplifyCondBranchToCondBranch(
592 ; CHECK-NEXT: block1:
593 ; CHECK-NEXT: [[BRMERGE:%.*]] = select i1 [[CMPA:%.*]], i1 true, i1 [[CMPB:%.*]]
594 ; CHECK-NEXT: [[DOTMUX:%.*]] = select i1 [[CMPA]], i32 0, i32 2, !prof [[PROF13:![0-9]+]]
595 ; CHECK-NEXT: [[OUTVAL:%.*]] = select i1 [[BRMERGE]], i32 [[DOTMUX]], i32 1, !prof [[PROF14:![0-9]+]]
596 ; CHECK-NEXT: ret i32 [[OUTVAL]]
599 br i1 %cmpa, label %block3, label %block2, !prof !13
602 br i1 %cmpb, label %block3, label %exit, !prof !14
605 %cowval = phi i32 [ 2, %block2 ], [ 0, %block1 ]
609 %outval = phi i32 [ %cowval, %block3 ], [ 1, %block2 ]
613 ; Swap the operands of the compares to verify that the weights update correctly.
615 define i32 @SimplifyCondBranchToCondBranchSwap(i1 %cmpa, i1 %cmpb) {
616 ; CHECK-LABEL: @SimplifyCondBranchToCondBranchSwap(
617 ; CHECK-NEXT: block1:
618 ; CHECK-NEXT: [[CMPA_NOT:%.*]] = xor i1 [[CMPA:%.*]], true
619 ; CHECK-NEXT: [[CMPB_NOT:%.*]] = xor i1 [[CMPB:%.*]], true
620 ; CHECK-NEXT: [[BRMERGE:%.*]] = select i1 [[CMPA_NOT]], i1 true, i1 [[CMPB_NOT]]
621 ; CHECK-NEXT: [[DOTMUX:%.*]] = select i1 [[CMPA_NOT]], i32 0, i32 2, !prof [[PROF15:![0-9]+]]
622 ; CHECK-NEXT: [[OUTVAL:%.*]] = select i1 [[BRMERGE]], i32 [[DOTMUX]], i32 1, !prof [[PROF16:![0-9]+]]
623 ; CHECK-NEXT: ret i32 [[OUTVAL]]
626 br i1 %cmpa, label %block2, label %block3, !prof !13
629 br i1 %cmpb, label %exit, label %block3, !prof !14
632 %cowval = phi i32 [ 2, %block2 ], [ 0, %block1 ]
636 %outval = phi i32 [ %cowval, %block3 ], [ 1, %block2 ]
640 define i32 @SimplifyCondBranchToCondBranchSwapMissingWeight(i1 %cmpa, i1 %cmpb) {
641 ; CHECK-LABEL: @SimplifyCondBranchToCondBranchSwapMissingWeight(
642 ; CHECK-NEXT: block1:
643 ; CHECK-NEXT: [[CMPA_NOT:%.*]] = xor i1 [[CMPA:%.*]], true
644 ; CHECK-NEXT: [[CMPB_NOT:%.*]] = xor i1 [[CMPB:%.*]], true
645 ; CHECK-NEXT: [[BRMERGE:%.*]] = select i1 [[CMPA_NOT]], i1 true, i1 [[CMPB_NOT]]
646 ; CHECK-NEXT: [[DOTMUX:%.*]] = select i1 [[CMPA_NOT]], i32 0, i32 2, !prof [[PROF17:![0-9]+]]
647 ; CHECK-NEXT: [[OUTVAL:%.*]] = select i1 [[BRMERGE]], i32 [[DOTMUX]], i32 1, !prof [[PROF18:![0-9]+]]
648 ; CHECK-NEXT: ret i32 [[OUTVAL]]
651 br i1 %cmpa, label %block2, label %block3, !prof !13
654 br i1 %cmpb, label %exit, label %block3
657 %cowval = phi i32 [ 2, %block2 ], [ 0, %block1 ]
661 %outval = phi i32 [ %cowval, %block3 ], [ 1, %block2 ]
665 ; Merging the icmps with logic-op defeats the purpose of the metadata.
666 ; We can't tell which condition is expensive if they are combined.
668 define void @or_icmps_harmful(i32 %x, i32 %y, ptr %p) {
669 ; CHECK-LABEL: @or_icmps_harmful(
671 ; CHECK-NEXT: [[EXPECTED_TRUE:%.*]] = icmp sgt i32 [[X:%.*]], -1
672 ; CHECK-NEXT: br i1 [[EXPECTED_TRUE]], label [[EXIT:%.*]], label [[RARE:%.*]], !prof [[PROF19:![0-9]+]]
674 ; CHECK-NEXT: [[EXPENSIVE:%.*]] = icmp eq i32 [[Y:%.*]], 0
675 ; CHECK-NEXT: br i1 [[EXPENSIVE]], label [[EXIT]], label [[FALSE:%.*]]
677 ; CHECK-NEXT: store i8 42, ptr [[P:%.*]], align 1
678 ; CHECK-NEXT: br label [[EXIT]]
680 ; CHECK-NEXT: ret void
683 %expected_true = icmp sgt i32 %x, -1
684 br i1 %expected_true, label %exit, label %rare, !prof !15
687 %expensive = icmp eq i32 %y, 0
688 br i1 %expensive, label %exit, label %false
691 store i8 42, ptr %p, align 1
698 ; Merging the icmps with logic-op defeats the purpose of the metadata.
699 ; We can't tell which condition is expensive if they are combined.
701 define void @or_icmps_harmful_inverted(i32 %x, i32 %y, ptr %p) {
702 ; CHECK-LABEL: @or_icmps_harmful_inverted(
704 ; CHECK-NEXT: [[EXPECTED_FALSE:%.*]] = icmp sgt i32 [[X:%.*]], -1
705 ; CHECK-NEXT: br i1 [[EXPECTED_FALSE]], label [[RARE:%.*]], label [[EXIT:%.*]], !prof [[PROF20:![0-9]+]]
707 ; CHECK-NEXT: [[EXPENSIVE:%.*]] = icmp eq i32 [[Y:%.*]], 0
708 ; CHECK-NEXT: br i1 [[EXPENSIVE]], label [[EXIT]], label [[FALSE:%.*]]
710 ; CHECK-NEXT: store i8 42, ptr [[P:%.*]], align 1
711 ; CHECK-NEXT: br label [[EXIT]]
713 ; CHECK-NEXT: ret void
716 %expected_false = icmp sgt i32 %x, -1
717 br i1 %expected_false, label %rare, label %exit, !prof !16
720 %expensive = icmp eq i32 %y, 0
721 br i1 %expensive, label %exit, label %false
724 store i8 42, ptr %p, align 1
732 define void @or_icmps_probably_not_harmful(i32 %x, i32 %y, ptr %p) {
733 ; CHECK-LABEL: @or_icmps_probably_not_harmful(
735 ; CHECK-NEXT: [[EXPECTED_TRUE:%.*]] = icmp sgt i32 [[X:%.*]], -1
736 ; CHECK-NEXT: [[EXPENSIVE:%.*]] = icmp eq i32 [[Y:%.*]], 0
737 ; CHECK-NEXT: [[OR_COND:%.*]] = select i1 [[EXPECTED_TRUE]], i1 true, i1 [[EXPENSIVE]]
738 ; CHECK-NEXT: br i1 [[OR_COND]], label [[EXIT:%.*]], label [[FALSE:%.*]], !prof [[PROF21:![0-9]+]], !unpredictable [[META22:![0-9]+]]
740 ; CHECK-NEXT: store i8 42, ptr [[P:%.*]], align 1
741 ; CHECK-NEXT: br label [[EXIT]]
743 ; CHECK-NEXT: ret void
746 %expected_true = icmp sgt i32 %x, -1
747 br i1 %expected_true, label %exit, label %rare, !prof !15, !unpredictable !20
750 %expensive = icmp eq i32 %y, 0
751 br i1 %expensive, label %exit, label %false
754 store i8 42, ptr %p, align 1
761 ; The probability threshold is determined by a TTI setting.
762 ; In this example, we are just short of strongly expected, so speculate.
764 define void @or_icmps_not_that_harmful(i32 %x, i32 %y, ptr %p) {
765 ; CHECK-LABEL: @or_icmps_not_that_harmful(
767 ; CHECK-NEXT: [[EXPECTED_TRUE:%.*]] = icmp sgt i32 [[X:%.*]], -1
768 ; CHECK-NEXT: [[EXPENSIVE:%.*]] = icmp eq i32 [[Y:%.*]], 0
769 ; CHECK-NEXT: [[OR_COND:%.*]] = select i1 [[EXPECTED_TRUE]], i1 true, i1 [[EXPENSIVE]]
770 ; CHECK-NEXT: br i1 [[OR_COND]], label [[EXIT:%.*]], label [[FALSE:%.*]], !prof [[PROF23:![0-9]+]]
772 ; CHECK-NEXT: store i8 42, ptr [[P:%.*]], align 1
773 ; CHECK-NEXT: br label [[EXIT]]
775 ; CHECK-NEXT: ret void
778 %expected_true = icmp sgt i32 %x, -1
779 br i1 %expected_true, label %exit, label %rare, !prof !17
782 %expensive = icmp eq i32 %y, 0
783 br i1 %expensive, label %exit, label %false
786 store i8 42, ptr %p, align 1
793 ; The probability threshold is determined by a TTI setting.
794 ; In this example, we are just short of strongly expected, so speculate.
796 define void @or_icmps_not_that_harmful_inverted(i32 %x, i32 %y, ptr %p) {
797 ; CHECK-LABEL: @or_icmps_not_that_harmful_inverted(
799 ; CHECK-NEXT: [[EXPECTED_TRUE:%.*]] = icmp sgt i32 [[X:%.*]], -1
800 ; CHECK-NEXT: [[EXPENSIVE:%.*]] = icmp eq i32 [[Y:%.*]], 0
801 ; CHECK-NEXT: [[OR_COND:%.*]] = select i1 [[EXPECTED_TRUE]], i1 true, i1 [[EXPENSIVE]]
802 ; CHECK-NEXT: br i1 [[OR_COND]], label [[EXIT:%.*]], label [[FALSE:%.*]], !prof [[PROF24:![0-9]+]]
804 ; CHECK-NEXT: store i8 42, ptr [[P:%.*]], align 1
805 ; CHECK-NEXT: br label [[EXIT]]
807 ; CHECK-NEXT: ret void
810 %expected_true = icmp sgt i32 %x, -1
811 br i1 %expected_true, label %exit, label %rare, !prof !18
814 %expensive = icmp eq i32 %y, 0
815 br i1 %expensive, label %exit, label %false
818 store i8 42, ptr %p, align 1
825 ; The 1st cmp is probably true, so speculating the 2nd is probably a win.
827 define void @or_icmps_useful(i32 %x, i32 %y, ptr %p) {
828 ; CHECK-LABEL: @or_icmps_useful(
830 ; CHECK-NEXT: [[EXPECTED_TRUE:%.*]] = icmp sle i32 [[X:%.*]], -1
831 ; CHECK-NEXT: [[EXPENSIVE:%.*]] = icmp eq i32 [[Y:%.*]], 0
832 ; CHECK-NEXT: [[OR_COND:%.*]] = select i1 [[EXPECTED_TRUE]], i1 true, i1 [[EXPENSIVE]]
833 ; CHECK-NEXT: br i1 [[OR_COND]], label [[EXIT:%.*]], label [[FALSE:%.*]], !prof [[PROF25:![0-9]+]]
835 ; CHECK-NEXT: store i8 42, ptr [[P:%.*]], align 1
836 ; CHECK-NEXT: br label [[EXIT]]
838 ; CHECK-NEXT: ret void
841 %expected_true = icmp sgt i32 %x, -1
842 br i1 %expected_true, label %likely, label %exit, !prof !15
845 %expensive = icmp eq i32 %y, 0
846 br i1 %expensive, label %exit, label %false
849 store i8 42, ptr %p, align 1
856 ; The 1st cmp is probably false, so speculating the 2nd is probably a win.
858 define void @or_icmps_useful_inverted(i32 %x, i32 %y, ptr %p) {
859 ; CHECK-LABEL: @or_icmps_useful_inverted(
861 ; CHECK-NEXT: [[EXPECTED_FALSE:%.*]] = icmp sgt i32 [[X:%.*]], -1
862 ; CHECK-NEXT: [[EXPENSIVE:%.*]] = icmp eq i32 [[Y:%.*]], 0
863 ; CHECK-NEXT: [[OR_COND:%.*]] = select i1 [[EXPECTED_FALSE]], i1 true, i1 [[EXPENSIVE]]
864 ; CHECK-NEXT: br i1 [[OR_COND]], label [[EXIT:%.*]], label [[FALSE:%.*]], !prof [[PROF25]]
866 ; CHECK-NEXT: store i8 42, ptr [[P:%.*]], align 1
867 ; CHECK-NEXT: br label [[EXIT]]
869 ; CHECK-NEXT: ret void
872 %expected_false = icmp sgt i32 %x, -1
873 br i1 %expected_false, label %exit, label %likely, !prof !16
876 %expensive = icmp eq i32 %y, 0
877 br i1 %expensive, label %exit, label %false
880 store i8 42, ptr %p, align 1
887 ; Don't crash processing degenerate metadata.
889 define void @or_icmps_empty_metadata(i32 %x, i32 %y, ptr %p) {
890 ; CHECK-LABEL: @or_icmps_empty_metadata(
892 ; CHECK-NEXT: [[EXPECTED_TRUE:%.*]] = icmp sgt i32 [[X:%.*]], -1
893 ; CHECK-NEXT: [[EXPENSIVE:%.*]] = icmp eq i32 [[Y:%.*]], 0
894 ; CHECK-NEXT: [[OR_COND:%.*]] = select i1 [[EXPECTED_TRUE]], i1 true, i1 [[EXPENSIVE]]
895 ; CHECK-NEXT: br i1 [[OR_COND]], label [[EXIT:%.*]], label [[MORE_RARE:%.*]]
897 ; CHECK-NEXT: store i8 42, ptr [[P:%.*]], align 1
898 ; CHECK-NEXT: br label [[EXIT]]
900 ; CHECK-NEXT: ret void
903 %expected_true = icmp sgt i32 %x, -1
904 br i1 %expected_true, label %exit, label %rare, !prof !19
907 %expensive = icmp eq i32 %y, 0
908 br i1 %expensive, label %exit, label %more_rare
911 store i8 42, ptr %p, align 1
918 ; Merging the icmps with logic-op defeats the purpose of the metadata.
919 ; We can't tell which condition is expensive if they are combined.
921 define void @and_icmps_harmful(i32 %x, i32 %y, ptr %p) {
922 ; CHECK-LABEL: @and_icmps_harmful(
924 ; CHECK-NEXT: [[EXPECTED_FALSE:%.*]] = icmp sgt i32 [[X:%.*]], -1
925 ; CHECK-NEXT: br i1 [[EXPECTED_FALSE]], label [[RARE:%.*]], label [[EXIT:%.*]], !prof [[PROF20]]
927 ; CHECK-NEXT: [[EXPENSIVE:%.*]] = icmp eq i32 [[Y:%.*]], 0
928 ; CHECK-NEXT: br i1 [[EXPENSIVE]], label [[FALSE:%.*]], label [[EXIT]]
930 ; CHECK-NEXT: store i8 42, ptr [[P:%.*]], align 1
931 ; CHECK-NEXT: br label [[EXIT]]
933 ; CHECK-NEXT: ret void
936 %expected_false = icmp sgt i32 %x, -1
937 br i1 %expected_false, label %rare, label %exit, !prof !16
940 %expensive = icmp eq i32 %y, 0
941 br i1 %expensive, label %false, label %exit
944 store i8 42, ptr %p, align 1
951 ; Merging the icmps with logic-op defeats the purpose of the metadata.
952 ; We can't tell which condition is expensive if they are combined.
954 define void @and_icmps_harmful_inverted(i32 %x, i32 %y, ptr %p) {
955 ; CHECK-LABEL: @and_icmps_harmful_inverted(
957 ; CHECK-NEXT: [[EXPECTED_TRUE:%.*]] = icmp sgt i32 [[X:%.*]], -1
958 ; CHECK-NEXT: br i1 [[EXPECTED_TRUE]], label [[EXIT:%.*]], label [[RARE:%.*]], !prof [[PROF19]]
960 ; CHECK-NEXT: [[EXPENSIVE:%.*]] = icmp eq i32 [[Y:%.*]], 0
961 ; CHECK-NEXT: br i1 [[EXPENSIVE]], label [[FALSE:%.*]], label [[EXIT]]
963 ; CHECK-NEXT: store i8 42, ptr [[P:%.*]], align 1
964 ; CHECK-NEXT: br label [[EXIT]]
966 ; CHECK-NEXT: ret void
969 %expected_true = icmp sgt i32 %x, -1
970 br i1 %expected_true, label %exit, label %rare, !prof !15
973 %expensive = icmp eq i32 %y, 0
974 br i1 %expensive, label %false, label %exit
977 store i8 42, ptr %p, align 1
984 ; The probability threshold is determined by a TTI setting.
985 ; In this example, we are just short of strongly expected, so speculate.
987 define void @and_icmps_not_that_harmful(i32 %x, i32 %y, ptr %p) {
988 ; CHECK-LABEL: @and_icmps_not_that_harmful(
990 ; CHECK-NEXT: [[EXPECTED_FALSE:%.*]] = icmp sgt i32 [[X:%.*]], -1
991 ; CHECK-NEXT: [[EXPENSIVE:%.*]] = icmp eq i32 [[Y:%.*]], 0
992 ; CHECK-NEXT: [[OR_COND:%.*]] = select i1 [[EXPECTED_FALSE]], i1 [[EXPENSIVE]], i1 false
993 ; CHECK-NEXT: br i1 [[OR_COND]], label [[FALSE:%.*]], label [[EXIT:%.*]], !prof [[PROF26:![0-9]+]]
995 ; CHECK-NEXT: store i8 42, ptr [[P:%.*]], align 1
996 ; CHECK-NEXT: br label [[EXIT]]
998 ; CHECK-NEXT: ret void
1001 %expected_false = icmp sgt i32 %x, -1
1002 br i1 %expected_false, label %rare, label %exit, !prof !18
1005 %expensive = icmp eq i32 %y, 0
1006 br i1 %expensive, label %false, label %exit
1009 store i8 42, ptr %p, align 1
1016 ; The probability threshold is determined by a TTI setting.
1017 ; In this example, we are just short of strongly expected, so speculate.
1019 define void @and_icmps_not_that_harmful_inverted(i32 %x, i32 %y, ptr %p) {
1020 ; CHECK-LABEL: @and_icmps_not_that_harmful_inverted(
1021 ; CHECK-NEXT: entry:
1022 ; CHECK-NEXT: [[EXPECTED_TRUE:%.*]] = icmp sle i32 [[X:%.*]], -1
1023 ; CHECK-NEXT: [[EXPENSIVE:%.*]] = icmp eq i32 [[Y:%.*]], 0
1024 ; CHECK-NEXT: [[OR_COND:%.*]] = select i1 [[EXPECTED_TRUE]], i1 [[EXPENSIVE]], i1 false
1025 ; CHECK-NEXT: br i1 [[OR_COND]], label [[FALSE:%.*]], label [[EXIT:%.*]], !prof [[PROF26]]
1027 ; CHECK-NEXT: store i8 42, ptr [[P:%.*]], align 1
1028 ; CHECK-NEXT: br label [[EXIT]]
1030 ; CHECK-NEXT: ret void
1033 %expected_true = icmp sgt i32 %x, -1
1034 br i1 %expected_true, label %exit, label %rare, !prof !17
1037 %expensive = icmp eq i32 %y, 0
1038 br i1 %expensive, label %false, label %exit
1041 store i8 42, ptr %p, align 1
1048 ; The 1st cmp is probably true, so speculating the 2nd is probably a win.
1050 define void @and_icmps_useful(i32 %x, i32 %y, ptr %p) {
1051 ; CHECK-LABEL: @and_icmps_useful(
1052 ; CHECK-NEXT: entry:
1053 ; CHECK-NEXT: [[EXPECTED_TRUE:%.*]] = icmp sgt i32 [[X:%.*]], -1
1054 ; CHECK-NEXT: [[EXPENSIVE:%.*]] = icmp eq i32 [[Y:%.*]], 0
1055 ; CHECK-NEXT: [[OR_COND:%.*]] = select i1 [[EXPECTED_TRUE]], i1 [[EXPENSIVE]], i1 false
1056 ; CHECK-NEXT: br i1 [[OR_COND]], label [[FALSE:%.*]], label [[EXIT:%.*]], !prof [[PROF27:![0-9]+]]
1058 ; CHECK-NEXT: store i8 42, ptr [[P:%.*]], align 1
1059 ; CHECK-NEXT: br label [[EXIT]]
1061 ; CHECK-NEXT: ret void
1064 %expected_true = icmp sgt i32 %x, -1
1065 br i1 %expected_true, label %likely, label %exit, !prof !15
1068 %expensive = icmp eq i32 %y, 0
1069 br i1 %expensive, label %false, label %exit
1072 store i8 42, ptr %p, align 1
1079 ; The 1st cmp is probably false, so speculating the 2nd is probably a win.
1081 define void @and_icmps_useful_inverted(i32 %x, i32 %y, ptr %p) {
1082 ; CHECK-LABEL: @and_icmps_useful_inverted(
1083 ; CHECK-NEXT: entry:
1084 ; CHECK-NEXT: [[EXPECTED_FALSE:%.*]] = icmp sle i32 [[X:%.*]], -1
1085 ; CHECK-NEXT: [[EXPENSIVE:%.*]] = icmp eq i32 [[Y:%.*]], 0
1086 ; CHECK-NEXT: [[OR_COND:%.*]] = select i1 [[EXPECTED_FALSE]], i1 [[EXPENSIVE]], i1 false
1087 ; CHECK-NEXT: br i1 [[OR_COND]], label [[FALSE:%.*]], label [[EXIT:%.*]], !prof [[PROF27]]
1089 ; CHECK-NEXT: store i8 42, ptr [[P:%.*]], align 1
1090 ; CHECK-NEXT: br label [[EXIT]]
1092 ; CHECK-NEXT: ret void
1095 %expected_false = icmp sgt i32 %x, -1
1096 br i1 %expected_false, label %exit, label %likely, !prof !16
1099 %expensive = icmp eq i32 %y, 0
1100 br i1 %expensive, label %false, label %exit
1103 store i8 42, ptr %p, align 1
1111 !0 = !{!"branch_weights", i32 3, i32 5}
1112 !1 = !{!"branch_weights", i32 1, i32 1}
1113 !2 = !{!"branch_weights", i32 1, i32 2}
1114 !3 = !{!"branch_weights", i32 4, i32 3, i32 2, i32 1}
1115 !4 = !{!"branch_weights", i32 4, i32 3, i32 2, i32 1}
1116 !5 = !{!"branch_weights", i32 7, i32 6, i32 5}
1117 !6 = !{!"branch_weights", i32 1, i32 3}
1118 !7 = !{!"branch_weights", i32 33, i32 9, i32 8, i32 7}
1119 !8 = !{!"branch_weights", i32 33, i32 9, i32 8}
1120 !9 = !{!"branch_weights", i32 7, i32 6}
1121 !10 = !{!"branch_weights", i32 672646, i32 21604207}
1122 !11 = !{!"branch_weights", i32 6960, i32 21597248}
1123 !12 = !{!"these_are_not_the_branch_weights_you_are_looking_for", i32 3, i32 5}
1124 !13 = !{!"branch_weights", i32 2, i32 3}
1125 !14 = !{!"branch_weights", i32 4, i32 7}
1126 !15 = !{!"branch_weights", i32 99, i32 1}
1127 !16 = !{!"branch_weights", i32 1, i32 99}
1128 !17 = !{!"branch_weights", i32 98, i32 1}
1129 !18 = !{!"branch_weights", i32 1, i32 98}
1130 !19 = !{!"branch_weights", i32 0, i32 0}
1134 ; CHECK: attributes #[[ATTR0:[0-9]+]] = { nounwind uwtable }
1135 ; CHECK: attributes #[[ATTR1]] = { nounwind }
1136 ; CHECK: attributes #[[ATTR2:[0-9]+]] = { noredzone nounwind ssp memory(none) }
1138 ; CHECK: [[PROF0]] = !{!"branch_weights", i32 5, i32 11}
1139 ; CHECK: [[PROF1]] = !{!"branch_weights", i32 1, i32 3}
1140 ; CHECK: [[PROF2]] = !{!"branch_weights", i32 1, i32 5}
1141 ; CHECK: [[PROF3]] = !{!"branch_weights", i32 7, i32 1, i32 2}
1142 ; CHECK: [[PROF4]] = !{!"branch_weights", i32 49, i32 12, i32 24, i32 35}
1143 ; CHECK: [[PROF5]] = !{!"branch_weights", i32 11, i32 5}
1144 ; CHECK: [[PROF6]] = !{!"branch_weights", i32 17, i32 15}
1145 ; CHECK: [[PROF7]] = !{!"branch_weights", i32 9, i32 7}
1146 ; CHECK: [[PROF8]] = !{!"branch_weights", i32 17, i32 9, i32 8, i32 7, i32 17}
1147 ; CHECK: [[PROF9]] = !{!"branch_weights", i32 24, i32 33}
1148 ; CHECK: [[PROF10]] = !{!"branch_weights", i32 8, i32 33}
1149 ; CHECK: [[PROF11]] = !{!"branch_weights", i32 112017436, i32 -735157296}
1150 ; CHECK: [[PROF12]] = !{!"branch_weights", i32 3, i32 5}
1151 ; CHECK: [[PROF13]] = !{!"branch_weights", i32 22, i32 12}
1152 ; CHECK: [[PROF14]] = !{!"branch_weights", i32 34, i32 21}
1153 ; CHECK: [[PROF15]] = !{!"branch_weights", i32 33, i32 14}
1154 ; CHECK: [[PROF16]] = !{!"branch_weights", i32 47, i32 8}
1155 ; CHECK: [[PROF17]] = !{!"branch_weights", i32 6, i32 2}
1156 ; CHECK: [[PROF18]] = !{!"branch_weights", i32 8, i32 2}
1157 ; CHECK: [[PROF19]] = !{!"branch_weights", i32 99, i32 1}
1158 ; CHECK: [[PROF20]] = !{!"branch_weights", i32 1, i32 99}
1159 ; CHECK: [[PROF21]] = !{!"branch_weights", i32 199, i32 1}
1160 ; CHECK: [[META22]] = !{}
1161 ; CHECK: [[PROF23]] = !{!"branch_weights", i32 197, i32 1}
1162 ; CHECK: [[PROF24]] = !{!"branch_weights", i32 100, i32 98}
1163 ; CHECK: [[PROF25]] = !{!"branch_weights", i32 101, i32 99}
1164 ; CHECK: [[PROF26]] = !{!"branch_weights", i32 1, i32 197}
1165 ; CHECK: [[PROF27]] = !{!"branch_weights", i32 99, i32 101}