1 ; RUN: llc < %s -mtriple=aarch64-unknown-unknown | FileCheck %s 2 3 ; Compare if negative and select of constants where one constant is zero. 4 5 define i32 @neg_sel_constants(i32 %a) { 6 ; CHECK-LABEL: neg_sel_constants: 7 ; CHECK: // %bb.0: 8 ; CHECK-NEXT: mov w8, #5 9 ; CHECK-NEXT: and w0, w8, w0, asr #31 10 ; CHECK-NEXT: ret 11 ; 12 %tmp.1 = icmp slt i32 %a, 0 13 %retval = select i1 %tmp.1, i32 5, i32 0 14 ret i32 %retval 15 } 16 17 ; Compare if negative and select of constants where one constant is zero and the other is a single bit. 18 19 define i32 @neg_sel_special_constant(i32 %a) { 20 ; CHECK-LABEL: neg_sel_special_constant: 21 ; CHECK: // %bb.0: 22 ; CHECK-NEXT: lsr w8, w0, #22 23 ; CHECK-NEXT: and w0, w8, #0x200 24 ; CHECK-NEXT: ret 25 ; 26 %tmp.1 = icmp slt i32 %a, 0 27 %retval = select i1 %tmp.1, i32 512, i32 0 28 ret i32 %retval 29 } 30 31 ; Compare if negative and select variable or zero. 32 33 define i32 @neg_sel_variable_and_zero(i32 %a, i32 %b) { 34 ; CHECK-LABEL: neg_sel_variable_and_zero: 35 ; CHECK: // %bb.0: 36 ; CHECK-NEXT: and w0, w1, w0, asr #31 37 ; CHECK-NEXT: ret 38 ; 39 %tmp.1 = icmp slt i32 %a, 0 40 %retval = select i1 %tmp.1, i32 %b, i32 0 41 ret i32 %retval 42 } 43 44 ; Compare if not positive and select the same variable as being compared: smin(a, 0). 45 46 define i32 @not_pos_sel_same_variable(i32 %a) { 47 ; CHECK-LABEL: not_pos_sel_same_variable: 48 ; CHECK: // %bb.0: 49 ; CHECK-NEXT: and w0, w0, w0, asr #31 50 ; CHECK-NEXT: ret 51 ; 52 %tmp = icmp slt i32 %a, 1 53 %min = select i1 %tmp, i32 %a, i32 0 54 ret i32 %min 55 } 56 57 ; Flipping the comparison condition can be handled by getting the bitwise not of the sign mask. 58 59 ; Compare if positive and select of constants where one constant is zero. 60 61 define i32 @pos_sel_constants(i32 %a) { 62 ; CHECK-LABEL: pos_sel_constants: 63 ; CHECK: // %bb.0: 64 ; CHECK-NEXT: mov w8, #5 65 ; CHECK-NEXT: bic w0, w8, w0, asr #31 66 ; CHECK-NEXT: ret 67 ; 68 %tmp.1 = icmp sgt i32 %a, -1 69 %retval = select i1 %tmp.1, i32 5, i32 0 70 ret i32 %retval 71 } 72 73 ; Compare if positive and select of constants where one constant is zero and the other is a single bit. 74 75 define i32 @pos_sel_special_constant(i32 %a) { 76 ; CHECK-LABEL: pos_sel_special_constant: 77 ; CHECK: // %bb.0: 78 ; CHECK-NEXT: orr w8, wzr, #0x200 79 ; CHECK-NEXT: bic w0, w8, w0, lsr #22 80 ; CHECK-NEXT: ret 81 ; 82 %tmp.1 = icmp sgt i32 %a, -1 83 %retval = select i1 %tmp.1, i32 512, i32 0 84 ret i32 %retval 85 } 86 87 ; Compare if positive and select variable or zero. 88 89 define i32 @pos_sel_variable_and_zero(i32 %a, i32 %b) { 90 ; CHECK-LABEL: pos_sel_variable_and_zero: 91 ; CHECK: // %bb.0: 92 ; CHECK-NEXT: bic w0, w1, w0, asr #31 93 ; CHECK-NEXT: ret 94 ; 95 %tmp.1 = icmp sgt i32 %a, -1 96 %retval = select i1 %tmp.1, i32 %b, i32 0 97 ret i32 %retval 98 } 99 100 ; Compare if not negative or zero and select the same variable as being compared: smax(a, 0). 101 102 define i32 @not_neg_sel_same_variable(i32 %a) { 103 ; CHECK-LABEL: not_neg_sel_same_variable: 104 ; CHECK: // %bb.0: 105 ; CHECK-NEXT: bic w0, w0, w0, asr #31 106 ; CHECK-NEXT: ret 107 ; 108 %tmp = icmp sgt i32 %a, 0 109 %min = select i1 %tmp, i32 %a, i32 0 110 ret i32 %min 111 } 112 113 ; https://llvm.org/bugs/show_bug.cgi?id=31175 114 115 ; ret = (x-y) > 0 ? x-y : 0 116 define i32 @PR31175(i32 %x, i32 %y) { 117 ; CHECK-LABEL: PR31175: 118 ; CHECK: // %bb.0: 119 ; CHECK-NEXT: sub w8, w0, w1 120 ; CHECK-NEXT: bic w0, w8, w8, asr #31 121 ; CHECK-NEXT: ret 122 ; 123 %sub = sub nsw i32 %x, %y 124 %cmp = icmp sgt i32 %sub, 0 125 %sel = select i1 %cmp, i32 %sub, i32 0 126 ret i32 %sel 127 } 128 129