1 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2 ; RUN: llc < %s -mtriple=riscv32 \
3 ; RUN: | FileCheck %s --check-prefixes=CHECK,RV32,RV32I
4 ; RUN: llc < %s -mtriple=riscv64 \
5 ; RUN: | FileCheck %s --check-prefixes=CHECK,RV64,RV64I
6 ; RUN: llc < %s -mtriple=riscv32 -mattr=+zbb \
7 ; RUN: | FileCheck %s --check-prefixes=CHECK,RV32,RV32ZBB
8 ; RUN: llc < %s -mtriple=riscv64 -mattr=+zbb \
9 ; RUN: | FileCheck %s --check-prefixes=CHECK,RV64,RV64ZBB
11 ; Compare if negative and select of constants where one constant is zero.
12 define i32 @neg_sel_constants(i32 signext %a) {
13 ; RV32-LABEL: neg_sel_constants:
15 ; RV32-NEXT: srai a0, a0, 31
16 ; RV32-NEXT: andi a0, a0, 5
19 ; RV64-LABEL: neg_sel_constants:
21 ; RV64-NEXT: srai a0, a0, 63
22 ; RV64-NEXT: andi a0, a0, 5
24 %tmp.1 = icmp slt i32 %a, 0
25 %retval = select i1 %tmp.1, i32 5, i32 0
29 ; Compare if negative and select of constants where one constant is zero and the
30 ; other is a single bit.
31 define i32 @neg_sel_special_constant(i32 signext %a) {
32 ; RV32-LABEL: neg_sel_special_constant:
34 ; RV32-NEXT: srli a0, a0, 31
35 ; RV32-NEXT: slli a0, a0, 9
38 ; RV64-LABEL: neg_sel_special_constant:
40 ; RV64-NEXT: srliw a0, a0, 31
41 ; RV64-NEXT: slli a0, a0, 9
43 %tmp.1 = icmp slt i32 %a, 0
44 %retval = select i1 %tmp.1, i32 512, i32 0
48 ; Compare if negative and select variable or zero.
49 define i32 @neg_sel_variable_and_zero(i32 signext %a, i32 signext %b) {
50 ; CHECK-LABEL: neg_sel_variable_and_zero:
52 ; CHECK-NEXT: srai a0, a0, 31
53 ; CHECK-NEXT: and a0, a0, a1
55 %tmp.1 = icmp slt i32 %a, 0
56 %retval = select i1 %tmp.1, i32 %b, i32 0
60 ; Compare if not positive and select the same variable as being compared:
62 define i32 @not_pos_sel_same_variable(i32 signext %a) {
63 ; CHECK-LABEL: not_pos_sel_same_variable:
65 ; CHECK-NEXT: srai a1, a0, 31
66 ; CHECK-NEXT: and a0, a1, a0
68 %tmp = icmp slt i32 %a, 1
69 %min = select i1 %tmp, i32 %a, i32 0
73 ; Flipping the comparison condition can be handled by getting the bitwise not of
75 ; TODO: We aren't doing a good job of this.
77 ; Compare if positive and select of constants where one constant is zero.
78 define i32 @pos_sel_constants(i32 signext %a) {
79 ; CHECK-LABEL: pos_sel_constants:
81 ; CHECK-NEXT: slti a0, a0, 0
82 ; CHECK-NEXT: addi a0, a0, -1
83 ; CHECK-NEXT: andi a0, a0, 5
85 %tmp.1 = icmp sgt i32 %a, -1
86 %retval = select i1 %tmp.1, i32 5, i32 0
90 ; Compare if positive and select of constants where one constant is zero and the
91 ; other is a single bit.
92 ; TODO: Why do RV32 and RV64 generate different code? RV64 uses more registers,
93 ; but the addi isn't part of the dependency chain of %a so may be faster.
94 define i32 @pos_sel_special_constant(i32 signext %a) {
95 ; RV32-LABEL: pos_sel_special_constant:
97 ; RV32-NEXT: not a0, a0
98 ; RV32-NEXT: srli a0, a0, 31
99 ; RV32-NEXT: slli a0, a0, 9
102 ; RV64-LABEL: pos_sel_special_constant:
104 ; RV64-NEXT: slti a0, a0, 0
105 ; RV64-NEXT: xori a0, a0, 1
106 ; RV64-NEXT: slli a0, a0, 9
108 %tmp.1 = icmp sgt i32 %a, -1
109 %retval = select i1 %tmp.1, i32 512, i32 0
113 ; Compare if positive and select variable or zero.
114 define i32 @pos_sel_variable_and_zero(i32 signext %a, i32 signext %b) {
115 ; RV32I-LABEL: pos_sel_variable_and_zero:
117 ; RV32I-NEXT: slti a0, a0, 0
118 ; RV32I-NEXT: addi a0, a0, -1
119 ; RV32I-NEXT: and a0, a0, a1
122 ; RV64I-LABEL: pos_sel_variable_and_zero:
124 ; RV64I-NEXT: slti a0, a0, 0
125 ; RV64I-NEXT: addi a0, a0, -1
126 ; RV64I-NEXT: and a0, a0, a1
129 ; RV32ZBB-LABEL: pos_sel_variable_and_zero:
131 ; RV32ZBB-NEXT: srai a0, a0, 31
132 ; RV32ZBB-NEXT: andn a0, a1, a0
135 ; RV64ZBB-LABEL: pos_sel_variable_and_zero:
137 ; RV64ZBB-NEXT: srai a0, a0, 31
138 ; RV64ZBB-NEXT: andn a0, a1, a0
140 %tmp.1 = icmp sgt i32 %a, -1
141 %retval = select i1 %tmp.1, i32 %b, i32 0
145 ; Compare if not negative or zero and select the same variable as being
146 ; compared: smax(a, 0).
147 define i32 @not_neg_sel_same_variable(i32 signext %a) {
148 ; RV32I-LABEL: not_neg_sel_same_variable:
150 ; RV32I-NEXT: sgtz a1, a0
151 ; RV32I-NEXT: neg a1, a1
152 ; RV32I-NEXT: and a0, a1, a0
155 ; RV64I-LABEL: not_neg_sel_same_variable:
157 ; RV64I-NEXT: sgtz a1, a0
158 ; RV64I-NEXT: neg a1, a1
159 ; RV64I-NEXT: and a0, a1, a0
162 ; RV32ZBB-LABEL: not_neg_sel_same_variable:
164 ; RV32ZBB-NEXT: max a0, a0, zero
167 ; RV64ZBB-LABEL: not_neg_sel_same_variable:
169 ; RV64ZBB-NEXT: max a0, a0, zero
171 %tmp = icmp sgt i32 %a, 0
172 %min = select i1 %tmp, i32 %a, i32 0
176 ; ret = (x-y) > 0 ? x-y : 0
177 define i32 @sub_clamp_zero(i32 signext %x, i32 signext %y) {
178 ; RV32I-LABEL: sub_clamp_zero:
180 ; RV32I-NEXT: sub a0, a0, a1
181 ; RV32I-NEXT: sgtz a1, a0
182 ; RV32I-NEXT: neg a1, a1
183 ; RV32I-NEXT: and a0, a1, a0
186 ; RV64I-LABEL: sub_clamp_zero:
188 ; RV64I-NEXT: subw a0, a0, a1
189 ; RV64I-NEXT: sgtz a1, a0
190 ; RV64I-NEXT: neg a1, a1
191 ; RV64I-NEXT: and a0, a1, a0
194 ; RV32ZBB-LABEL: sub_clamp_zero:
196 ; RV32ZBB-NEXT: sub a0, a0, a1
197 ; RV32ZBB-NEXT: max a0, a0, zero
200 ; RV64ZBB-LABEL: sub_clamp_zero:
202 ; RV64ZBB-NEXT: subw a0, a0, a1
203 ; RV64ZBB-NEXT: max a0, a0, zero
205 %sub = sub nsw i32 %x, %y
206 %cmp = icmp sgt i32 %sub, 0
207 %sel = select i1 %cmp, i32 %sub, i32 0
211 define i8 @sel_shift_bool_i8(i1 %t) {
212 ; RV32-LABEL: sel_shift_bool_i8:
214 ; RV32-NEXT: slli a0, a0, 31
215 ; RV32-NEXT: srai a0, a0, 31
216 ; RV32-NEXT: andi a0, a0, -128
219 ; RV64-LABEL: sel_shift_bool_i8:
221 ; RV64-NEXT: slli a0, a0, 63
222 ; RV64-NEXT: srai a0, a0, 63
223 ; RV64-NEXT: andi a0, a0, -128
225 %shl = select i1 %t, i8 128, i8 0
229 define i16 @sel_shift_bool_i16(i1 %t) {
230 ; CHECK-LABEL: sel_shift_bool_i16:
232 ; CHECK-NEXT: andi a0, a0, 1
233 ; CHECK-NEXT: slli a0, a0, 7
235 %shl = select i1 %t, i16 128, i16 0
239 define i32 @sel_shift_bool_i32(i1 %t) {
240 ; CHECK-LABEL: sel_shift_bool_i32:
242 ; CHECK-NEXT: andi a0, a0, 1
243 ; CHECK-NEXT: slli a0, a0, 6
245 %shl = select i1 %t, i32 64, i32 0
249 define i64 @sel_shift_bool_i64(i1 %t) {
250 ; RV32-LABEL: sel_shift_bool_i64:
252 ; RV32-NEXT: andi a0, a0, 1
253 ; RV32-NEXT: slli a0, a0, 16
254 ; RV32-NEXT: li a1, 0
257 ; RV64-LABEL: sel_shift_bool_i64:
259 ; RV64-NEXT: andi a0, a0, 1
260 ; RV64-NEXT: slli a0, a0, 16
262 %shl = select i1 %t, i64 65536, i64 0