1 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2 ; RUN: llc -mtriple=riscv64 -mattr=+m %s -o - | FileCheck %s
4 define zeroext i16 @overflow_add(i16 zeroext %a, i16 zeroext %b) {
5 ; CHECK-LABEL: overflow_add:
7 ; CHECK-NEXT: add a0, a1, a0
8 ; CHECK-NEXT: ori a0, a0, 1
9 ; CHECK-NEXT: slli a0, a0, 48
10 ; CHECK-NEXT: srli a1, a0, 48
11 ; CHECK-NEXT: li a2, 1024
12 ; CHECK-NEXT: li a0, 2
13 ; CHECK-NEXT: bltu a2, a1, .LBB0_2
14 ; CHECK-NEXT: # %bb.1:
15 ; CHECK-NEXT: li a0, 5
16 ; CHECK-NEXT: .LBB0_2:
20 %cmp = icmp ugt i16 %or, 1024
21 %res = select i1 %cmp, i16 2, i16 5
25 define zeroext i16 @overflow_sub(i16 zeroext %a, i16 zeroext %b) {
26 ; CHECK-LABEL: overflow_sub:
28 ; CHECK-NEXT: subw a0, a0, a1
29 ; CHECK-NEXT: ori a0, a0, 1
30 ; CHECK-NEXT: slli a0, a0, 48
31 ; CHECK-NEXT: srli a1, a0, 48
32 ; CHECK-NEXT: li a2, 1024
33 ; CHECK-NEXT: li a0, 2
34 ; CHECK-NEXT: bltu a2, a1, .LBB1_2
35 ; CHECK-NEXT: # %bb.1:
36 ; CHECK-NEXT: li a0, 5
37 ; CHECK-NEXT: .LBB1_2:
41 %cmp = icmp ugt i16 %or, 1024
42 %res = select i1 %cmp, i16 2, i16 5
46 define zeroext i16 @overflow_mul(i16 zeroext %a, i16 zeroext %b) {
47 ; CHECK-LABEL: overflow_mul:
49 ; CHECK-NEXT: mul a0, a1, a0
50 ; CHECK-NEXT: ori a0, a0, 1
51 ; CHECK-NEXT: slli a0, a0, 48
52 ; CHECK-NEXT: srli a1, a0, 48
53 ; CHECK-NEXT: li a2, 1024
54 ; CHECK-NEXT: li a0, 2
55 ; CHECK-NEXT: bltu a2, a1, .LBB2_2
56 ; CHECK-NEXT: # %bb.1:
57 ; CHECK-NEXT: li a0, 5
58 ; CHECK-NEXT: .LBB2_2:
62 %cmp = icmp ugt i16 %or, 1024
63 %res = select i1 %cmp, i16 2, i16 5
67 define zeroext i16 @overflow_shl(i16 zeroext %a, i16 zeroext %b) {
68 ; CHECK-LABEL: overflow_shl:
70 ; CHECK-NEXT: sll a0, a0, a1
71 ; CHECK-NEXT: ori a0, a0, 1
72 ; CHECK-NEXT: slli a0, a0, 48
73 ; CHECK-NEXT: srli a1, a0, 48
74 ; CHECK-NEXT: li a2, 1024
75 ; CHECK-NEXT: li a0, 2
76 ; CHECK-NEXT: bltu a2, a1, .LBB3_2
77 ; CHECK-NEXT: # %bb.1:
78 ; CHECK-NEXT: li a0, 5
79 ; CHECK-NEXT: .LBB3_2:
83 %cmp = icmp ugt i16 %or, 1024
84 %res = select i1 %cmp, i16 2, i16 5
88 define i32 @overflow_add_no_consts(i8 zeroext %a, i8 zeroext %b, i8 zeroext %limit) {
89 ; CHECK-LABEL: overflow_add_no_consts:
91 ; CHECK-NEXT: add a0, a1, a0
92 ; CHECK-NEXT: andi a1, a0, 255
93 ; CHECK-NEXT: li a0, 8
94 ; CHECK-NEXT: bltu a2, a1, .LBB4_2
95 ; CHECK-NEXT: # %bb.1:
96 ; CHECK-NEXT: li a0, 16
97 ; CHECK-NEXT: .LBB4_2:
100 %cmp = icmp ugt i8 %add, %limit
101 %res = select i1 %cmp, i32 8, i32 16
105 define i32 @overflow_add_const_limit(i8 zeroext %a, i8 zeroext %b) {
106 ; CHECK-LABEL: overflow_add_const_limit:
108 ; CHECK-NEXT: add a0, a1, a0
109 ; CHECK-NEXT: andi a1, a0, 255
110 ; CHECK-NEXT: li a2, 128
111 ; CHECK-NEXT: li a0, 8
112 ; CHECK-NEXT: bltu a2, a1, .LBB5_2
113 ; CHECK-NEXT: # %bb.1:
114 ; CHECK-NEXT: li a0, 16
115 ; CHECK-NEXT: .LBB5_2:
118 %cmp = icmp ugt i8 %add, -128
119 %res = select i1 %cmp, i32 8, i32 16
123 define i32 @overflow_add_positive_const_limit(i8 zeroext %a) {
124 ; CHECK-LABEL: overflow_add_positive_const_limit:
126 ; CHECK-NEXT: slli a0, a0, 56
127 ; CHECK-NEXT: srai a1, a0, 56
128 ; CHECK-NEXT: li a2, -1
129 ; CHECK-NEXT: li a0, 8
130 ; CHECK-NEXT: blt a1, a2, .LBB6_2
131 ; CHECK-NEXT: # %bb.1:
132 ; CHECK-NEXT: li a0, 16
133 ; CHECK-NEXT: .LBB6_2:
135 %cmp = icmp slt i8 %a, -1
136 %res = select i1 %cmp, i32 8, i32 16
140 define i32 @unsafe_add_underflow(i8 zeroext %a) {
141 ; CHECK-LABEL: unsafe_add_underflow:
143 ; CHECK-NEXT: mv a1, a0
144 ; CHECK-NEXT: li a2, 1
145 ; CHECK-NEXT: li a0, 8
146 ; CHECK-NEXT: beq a1, a2, .LBB7_2
147 ; CHECK-NEXT: # %bb.1:
148 ; CHECK-NEXT: li a0, 16
149 ; CHECK-NEXT: .LBB7_2:
151 %cmp = icmp eq i8 %a, 1
152 %res = select i1 %cmp, i32 8, i32 16
156 define i32 @safe_add_underflow(i8 zeroext %a) {
157 ; CHECK-LABEL: safe_add_underflow:
159 ; CHECK-NEXT: mv a1, a0
160 ; CHECK-NEXT: li a0, 8
161 ; CHECK-NEXT: beqz a1, .LBB8_2
162 ; CHECK-NEXT: # %bb.1:
163 ; CHECK-NEXT: li a0, 16
164 ; CHECK-NEXT: .LBB8_2:
166 %cmp = icmp eq i8 %a, 0
167 %res = select i1 %cmp, i32 8, i32 16
171 define i32 @safe_add_underflow_neg(i8 zeroext %a) {
172 ; CHECK-LABEL: safe_add_underflow_neg:
174 ; CHECK-NEXT: addi a1, a0, -2
175 ; CHECK-NEXT: li a2, 251
176 ; CHECK-NEXT: li a0, 8
177 ; CHECK-NEXT: bltu a1, a2, .LBB9_2
178 ; CHECK-NEXT: # %bb.1:
179 ; CHECK-NEXT: li a0, 16
180 ; CHECK-NEXT: .LBB9_2:
183 %cmp = icmp ult i8 %add, -5
184 %res = select i1 %cmp, i32 8, i32 16
188 define i32 @overflow_sub_negative_const_limit(i8 zeroext %a) {
189 ; CHECK-LABEL: overflow_sub_negative_const_limit:
191 ; CHECK-NEXT: slli a0, a0, 56
192 ; CHECK-NEXT: srai a1, a0, 56
193 ; CHECK-NEXT: li a2, -1
194 ; CHECK-NEXT: li a0, 8
195 ; CHECK-NEXT: blt a1, a2, .LBB10_2
196 ; CHECK-NEXT: # %bb.1:
197 ; CHECK-NEXT: li a0, 16
198 ; CHECK-NEXT: .LBB10_2:
200 %cmp = icmp slt i8 %a, -1
201 %res = select i1 %cmp, i32 8, i32 16
205 ; This is valid so long as the icmp immediate is sext.
206 define i32 @sext_sub_underflow(i8 zeroext %a) {
207 ; CHECK-LABEL: sext_sub_underflow:
209 ; CHECK-NEXT: addi a1, a0, -6
210 ; CHECK-NEXT: li a2, -6
211 ; CHECK-NEXT: li a0, 8
212 ; CHECK-NEXT: bltu a2, a1, .LBB11_2
213 ; CHECK-NEXT: # %bb.1:
214 ; CHECK-NEXT: li a0, 16
215 ; CHECK-NEXT: .LBB11_2:
218 %cmp = icmp ugt i8 %sub, -6
219 %res = select i1 %cmp, i32 8, i32 16
223 define i32 @safe_sub_underflow(i8 zeroext %a) {
224 ; CHECK-LABEL: safe_sub_underflow:
226 ; CHECK-NEXT: mv a1, a0
227 ; CHECK-NEXT: li a0, 16
228 ; CHECK-NEXT: beqz a1, .LBB12_2
229 ; CHECK-NEXT: # %bb.1:
230 ; CHECK-NEXT: li a0, 8
231 ; CHECK-NEXT: .LBB12_2:
233 %cmp.not = icmp eq i8 %a, 0
234 %res = select i1 %cmp.not, i32 16, i32 8
238 define i32 @safe_sub_underflow_neg(i8 zeroext %a) {
239 ; CHECK-LABEL: safe_sub_underflow_neg:
241 ; CHECK-NEXT: addi a1, a0, -4
242 ; CHECK-NEXT: li a2, 250
243 ; CHECK-NEXT: li a0, 8
244 ; CHECK-NEXT: bltu a2, a1, .LBB13_2
245 ; CHECK-NEXT: # %bb.1:
246 ; CHECK-NEXT: li a0, 16
247 ; CHECK-NEXT: .LBB13_2:
250 %cmp = icmp ugt i8 %sub, -6
251 %res = select i1 %cmp, i32 8, i32 16
255 ; This is valid so long as the icmp immediate is sext.
256 define i32 @sext_sub_underflow_neg(i8 zeroext %a) {
257 ; CHECK-LABEL: sext_sub_underflow_neg:
259 ; CHECK-NEXT: addi a1, a0, -4
260 ; CHECK-NEXT: li a2, -3
261 ; CHECK-NEXT: li a0, 8
262 ; CHECK-NEXT: bltu a1, a2, .LBB14_2
263 ; CHECK-NEXT: # %bb.1:
264 ; CHECK-NEXT: li a0, 16
265 ; CHECK-NEXT: .LBB14_2:
268 %cmp = icmp ult i8 %sub, -3
269 %res = select i1 %cmp, i32 8, i32 16
273 define i32 @safe_sub_imm_var(ptr nocapture readonly %b) local_unnamed_addr #1 {
274 ; CHECK-LABEL: safe_sub_imm_var:
275 ; CHECK: # %bb.0: # %entry
276 ; CHECK-NEXT: li a0, 0
282 define i32 @safe_sub_var_imm(ptr nocapture readonly %b) local_unnamed_addr #1 {
283 ; CHECK-LABEL: safe_sub_var_imm:
284 ; CHECK: # %bb.0: # %entry
285 ; CHECK-NEXT: lbu a0, 0(a0)
286 ; CHECK-NEXT: addi a0, a0, -248
287 ; CHECK-NEXT: sltiu a0, a0, -3
288 ; CHECK-NEXT: xori a0, a0, 1
291 %0 = load i8, ptr %b, align 1
292 %sub = add nsw i8 %0, 8
293 %cmp = icmp ugt i8 %sub, -4
294 %conv4 = zext i1 %cmp to i32
298 define i32 @safe_add_imm_var(ptr nocapture readnone %b) {
299 ; CHECK-LABEL: safe_add_imm_var:
300 ; CHECK: # %bb.0: # %entry
301 ; CHECK-NEXT: li a0, 1
307 define i32 @safe_add_var_imm(ptr nocapture readnone %b) {
308 ; CHECK-LABEL: safe_add_var_imm:
309 ; CHECK: # %bb.0: # %entry
310 ; CHECK-NEXT: li a0, 1
316 define i8 @convert_add_order(i8 zeroext %arg) {
317 ; CHECK-LABEL: convert_add_order:
319 ; CHECK-NEXT: ori a1, a0, 1
320 ; CHECK-NEXT: li a2, 50
321 ; CHECK-NEXT: bltu a1, a2, .LBB19_2
322 ; CHECK-NEXT: # %bb.1:
323 ; CHECK-NEXT: li a1, 255
324 ; CHECK-NEXT: and a0, a1, a0
326 ; CHECK-NEXT: .LBB19_2:
327 ; CHECK-NEXT: addi a1, a1, -40
328 ; CHECK-NEXT: sltiu a1, a1, 20
329 ; CHECK-NEXT: li a2, 2
330 ; CHECK-NEXT: sub a1, a2, a1
331 ; CHECK-NEXT: and a0, a1, a0
334 %cmp.0 = icmp ult i8 %shl, 50
335 %sub = add nsw i8 %shl, -40
336 %cmp.1 = icmp ult i8 %sub, 20
337 %mask.sel.v = select i1 %cmp.1, i8 1, i8 2
338 %mask.sel = select i1 %cmp.0, i8 %mask.sel.v, i8 -1
339 %res = and i8 %mask.sel, %arg
343 define i8 @underflow_if_sub(i32 %arg, i8 zeroext %arg1) {
344 ; CHECK-LABEL: underflow_if_sub:
346 ; CHECK-NEXT: sext.w a2, a0
347 ; CHECK-NEXT: sgtz a2, a2
348 ; CHECK-NEXT: and a0, a2, a0
349 ; CHECK-NEXT: addi a0, a0, 245
350 ; CHECK-NEXT: bltu a0, a1, .LBB20_2
351 ; CHECK-NEXT: # %bb.1:
352 ; CHECK-NEXT: li a0, 100
353 ; CHECK-NEXT: .LBB20_2:
355 %cmp = icmp sgt i32 %arg, 0
356 %conv = zext i1 %cmp to i32
357 %and = and i32 %conv, %arg
358 %trunc = trunc i32 %and to i8
359 %conv1 = add nuw nsw i8 %trunc, -11
360 %cmp.1 = icmp ult i8 %conv1, %arg1
361 %res = select i1 %cmp.1, i8 %conv1, i8 100
365 define i8 @underflow_if_sub_signext(i32 %arg, i8 signext %arg1) {
366 ; CHECK-LABEL: underflow_if_sub_signext:
368 ; CHECK-NEXT: sext.w a2, a0
369 ; CHECK-NEXT: andi a1, a1, 255
370 ; CHECK-NEXT: sgtz a2, a2
371 ; CHECK-NEXT: and a0, a2, a0
372 ; CHECK-NEXT: addi a0, a0, 245
373 ; CHECK-NEXT: bltu a0, a1, .LBB21_2
374 ; CHECK-NEXT: # %bb.1:
375 ; CHECK-NEXT: li a0, 100
376 ; CHECK-NEXT: .LBB21_2:
378 %cmp = icmp sgt i32 %arg, 0
379 %conv = zext i1 %cmp to i32
380 %and = and i32 %conv, %arg
381 %trunc = trunc i32 %and to i8
382 %conv1 = add nuw nsw i8 %trunc, -11
383 %cmp.1 = icmp ult i8 %conv1, %arg1
384 %res = select i1 %cmp.1, i8 %conv1, i8 100