Sanjay Patel | 183f90a | 2016-11-29 18:35:04 +0000 | [diff] [blame] | 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: |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 7 | ; CHECK: // %bb.0: |
Sanjay Patel | 183f90a | 2016-11-29 18:35:04 +0000 | [diff] [blame] | 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: |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 21 | ; CHECK: // %bb.0: |
Sanjay Patel | 183f90a | 2016-11-29 18:35:04 +0000 | [diff] [blame] | 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: |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 35 | ; CHECK: // %bb.0: |
Sanjay Patel | 183f90a | 2016-11-29 18:35:04 +0000 | [diff] [blame] | 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: |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 48 | ; CHECK: // %bb.0: |
Sanjay Patel | 183f90a | 2016-11-29 18:35:04 +0000 | [diff] [blame] | 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 | |
Sanjay Patel | afee21a | 2016-12-14 22:59:14 +0000 | [diff] [blame] | 57 | ; Flipping the comparison condition can be handled by getting the bitwise not of the sign mask. |
Sanjay Patel | 183f90a | 2016-11-29 18:35:04 +0000 | [diff] [blame] | 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: |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 63 | ; CHECK: // %bb.0: |
Sanjay Patel | 183f90a | 2016-11-29 18:35:04 +0000 | [diff] [blame] | 64 | ; CHECK-NEXT: mov w8, #5 |
Sanjay Patel | afee21a | 2016-12-14 22:59:14 +0000 | [diff] [blame] | 65 | ; CHECK-NEXT: bic w0, w8, w0, asr #31 |
Sanjay Patel | 183f90a | 2016-11-29 18:35:04 +0000 | [diff] [blame] | 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: |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 77 | ; CHECK: // %bb.0: |
Sanjay Patel | afee21a | 2016-12-14 22:59:14 +0000 | [diff] [blame] | 78 | ; CHECK-NEXT: orr w8, wzr, #0x200 |
| 79 | ; CHECK-NEXT: bic w0, w8, w0, lsr #22 |
Sanjay Patel | 183f90a | 2016-11-29 18:35:04 +0000 | [diff] [blame] | 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: |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 91 | ; CHECK: // %bb.0: |
Sanjay Patel | afee21a | 2016-12-14 22:59:14 +0000 | [diff] [blame] | 92 | ; CHECK-NEXT: bic w0, w1, w0, asr #31 |
Sanjay Patel | 183f90a | 2016-11-29 18:35:04 +0000 | [diff] [blame] | 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: |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 104 | ; CHECK: // %bb.0: |
Sanjay Patel | afee21a | 2016-12-14 22:59:14 +0000 | [diff] [blame] | 105 | ; CHECK-NEXT: bic w0, w0, w0, asr #31 |
Sanjay Patel | 183f90a | 2016-11-29 18:35:04 +0000 | [diff] [blame] | 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: |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 118 | ; CHECK: // %bb.0: |
Sanjay Patel | 183f90a | 2016-11-29 18:35:04 +0000 | [diff] [blame] | 119 | ; CHECK-NEXT: sub w8, w0, w1 |
Sanjay Patel | afee21a | 2016-12-14 22:59:14 +0000 | [diff] [blame] | 120 | ; CHECK-NEXT: bic w0, w8, w8, asr #31 |
Sanjay Patel | 183f90a | 2016-11-29 18:35:04 +0000 | [diff] [blame] | 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 | |