1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -instcombine -S < %s | FileCheck %s
4 declare { i8, i1 } @llvm.uadd.with.overflow.i8(i8, i8) nounwind readnone
5 declare { i8, i1 } @llvm.sadd.with.overflow.i8(i8, i8) nounwind readnone
6 declare { i8, i1 } @llvm.usub.with.overflow.i8(i8, i8) nounwind readnone
7 declare { i8, i1 } @llvm.ssub.with.overflow.i8(i8, i8) nounwind readnone
8 declare { i8, i1 } @llvm.umul.with.overflow.i8(i8, i8) nounwind readnone
9 declare { i8, i1 } @llvm.smul.with.overflow.i8(i8, i8) nounwind readnone
10 declare { i32, i1 } @llvm.sadd.with.overflow.i32(i32, i32) nounwind readnone
11 declare { i32, i1 } @llvm.uadd.with.overflow.i32(i32, i32) nounwind readnone
12 declare { i32, i1 } @llvm.ssub.with.overflow.i32(i32, i32) nounwind readnone
13 declare { i32, i1 } @llvm.usub.with.overflow.i32(i32, i32) nounwind readnone
14 declare { i32, i1 } @llvm.smul.with.overflow.i32(i32, i32) nounwind readnone
15 declare { i32, i1 } @llvm.umul.with.overflow.i32(i32, i32) nounwind readnone
17 define i8 @uaddtest1(i8 %A, i8 %B) {
18 ; CHECK-LABEL: @uaddtest1(
19 ; CHECK-NEXT: [[Y:%.*]] = add i8 [[A:%.*]], [[B:%.*]]
20 ; CHECK-NEXT: ret i8 [[Y]]
22 %x = call { i8, i1 } @llvm.uadd.with.overflow.i8(i8 %A, i8 %B)
23 %y = extractvalue { i8, i1 } %x, 0
27 define i8 @uaddtest2(i8 %A, i8 %B, i1* %overflowPtr) {
28 ; CHECK-LABEL: @uaddtest2(
29 ; CHECK-NEXT: [[AND_A:%.*]] = and i8 [[A:%.*]], 127
30 ; CHECK-NEXT: [[AND_B:%.*]] = and i8 [[B:%.*]], 127
31 ; CHECK-NEXT: [[X:%.*]] = add nuw i8 [[AND_A]], [[AND_B]]
32 ; CHECK-NEXT: store i1 false, i1* [[OVERFLOWPTR:%.*]], align 1
33 ; CHECK-NEXT: ret i8 [[X]]
35 %and.A = and i8 %A, 127
36 %and.B = and i8 %B, 127
37 %x = call { i8, i1 } @llvm.uadd.with.overflow.i8(i8 %and.A, i8 %and.B)
38 %y = extractvalue { i8, i1 } %x, 0
39 %z = extractvalue { i8, i1 } %x, 1
40 store i1 %z, i1* %overflowPtr
44 define i8 @uaddtest3(i8 %A, i8 %B, i1* %overflowPtr) {
45 ; CHECK-LABEL: @uaddtest3(
46 ; CHECK-NEXT: [[OR_A:%.*]] = or i8 [[A:%.*]], -128
47 ; CHECK-NEXT: [[OR_B:%.*]] = or i8 [[B:%.*]], -128
48 ; CHECK-NEXT: [[X:%.*]] = add i8 [[OR_A]], [[OR_B]]
49 ; CHECK-NEXT: store i1 true, i1* [[OVERFLOWPTR:%.*]], align 1
50 ; CHECK-NEXT: ret i8 [[X]]
52 %or.A = or i8 %A, -128
53 %or.B = or i8 %B, -128
54 %x = call { i8, i1 } @llvm.uadd.with.overflow.i8(i8 %or.A, i8 %or.B)
55 %y = extractvalue { i8, i1 } %x, 0
56 %z = extractvalue { i8, i1 } %x, 1
57 store i1 %z, i1* %overflowPtr
61 define i8 @uaddtest4(i8 %A, i1* %overflowPtr) {
62 ; CHECK-LABEL: @uaddtest4(
63 ; CHECK-NEXT: store i1 false, i1* [[OVERFLOWPTR:%.*]], align 1
64 ; CHECK-NEXT: ret i8 undef
66 %x = call { i8, i1 } @llvm.uadd.with.overflow.i8(i8 undef, i8 %A)
67 %y = extractvalue { i8, i1 } %x, 0
68 %z = extractvalue { i8, i1 } %x, 1
69 store i1 %z, i1* %overflowPtr
73 define i8 @uaddtest5(i8 %A, i1* %overflowPtr) {
74 ; CHECK-LABEL: @uaddtest5(
75 ; CHECK-NEXT: store i1 false, i1* [[OVERFLOWPTR:%.*]], align 1
76 ; CHECK-NEXT: ret i8 [[A:%.*]]
78 %x = call { i8, i1 } @llvm.uadd.with.overflow.i8(i8 0, i8 %A)
79 %y = extractvalue { i8, i1 } %x, 0
80 %z = extractvalue { i8, i1 } %x, 1
81 store i1 %z, i1* %overflowPtr
85 define i1 @uaddtest6(i8 %A, i8 %B) {
86 ; CHECK-LABEL: @uaddtest6(
87 ; CHECK-NEXT: [[Z:%.*]] = icmp ugt i8 [[A:%.*]], 3
88 ; CHECK-NEXT: ret i1 [[Z]]
90 %x = call { i8, i1 } @llvm.uadd.with.overflow.i8(i8 %A, i8 -4)
91 %z = extractvalue { i8, i1 } %x, 1
95 define i8 @uaddtest7(i8 %A, i8 %B) {
96 ; CHECK-LABEL: @uaddtest7(
97 ; CHECK-NEXT: [[Z:%.*]] = add i8 [[A:%.*]], [[B:%.*]]
98 ; CHECK-NEXT: ret i8 [[Z]]
100 %x = call { i8, i1 } @llvm.uadd.with.overflow.i8(i8 %A, i8 %B)
101 %z = extractvalue { i8, i1 } %x, 0
106 define { i32, i1 } @saddtest_nsw(i8 %a, i8 %b) {
107 ; CHECK-LABEL: @saddtest_nsw(
108 ; CHECK-NEXT: [[AA:%.*]] = sext i8 [[A:%.*]] to i32
109 ; CHECK-NEXT: [[BB:%.*]] = sext i8 [[B:%.*]] to i32
110 ; CHECK-NEXT: [[X:%.*]] = add nsw i32 [[AA]], [[BB]]
111 ; CHECK-NEXT: [[TMP1:%.*]] = insertvalue { i32, i1 } { i32 undef, i1 false }, i32 [[X]], 0
112 ; CHECK-NEXT: ret { i32, i1 } [[TMP1]]
114 %aa = sext i8 %a to i32
115 %bb = sext i8 %b to i32
116 %x = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %aa, i32 %bb)
120 define { i32, i1 } @uaddtest_nuw(i32 %a, i32 %b) {
121 ; CHECK-LABEL: @uaddtest_nuw(
122 ; CHECK-NEXT: [[AA:%.*]] = and i32 [[A:%.*]], 2147483647
123 ; CHECK-NEXT: [[BB:%.*]] = and i32 [[B:%.*]], 2147483647
124 ; CHECK-NEXT: [[X:%.*]] = add nuw i32 [[AA]], [[BB]]
125 ; CHECK-NEXT: [[TMP1:%.*]] = insertvalue { i32, i1 } { i32 undef, i1 false }, i32 [[X]], 0
126 ; CHECK-NEXT: ret { i32, i1 } [[TMP1]]
128 %aa = and i32 %a, 2147483647
129 %bb = and i32 %b, 2147483647
130 %x = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 %aa, i32 %bb)
134 define { i32, i1 } @ssubtest_nsw(i8 %a, i8 %b) {
135 ; CHECK-LABEL: @ssubtest_nsw(
136 ; CHECK-NEXT: [[AA:%.*]] = sext i8 [[A:%.*]] to i32
137 ; CHECK-NEXT: [[BB:%.*]] = sext i8 [[B:%.*]] to i32
138 ; CHECK-NEXT: [[X:%.*]] = sub nsw i32 [[AA]], [[BB]]
139 ; CHECK-NEXT: [[TMP1:%.*]] = insertvalue { i32, i1 } { i32 undef, i1 false }, i32 [[X]], 0
140 ; CHECK-NEXT: ret { i32, i1 } [[TMP1]]
142 %aa = sext i8 %a to i32
143 %bb = sext i8 %b to i32
144 %x = call { i32, i1 } @llvm.ssub.with.overflow.i32(i32 %aa, i32 %bb)
148 define { i32, i1 } @usubtest_nuw(i32 %a, i32 %b) {
149 ; CHECK-LABEL: @usubtest_nuw(
150 ; CHECK-NEXT: [[AA:%.*]] = or i32 [[A:%.*]], -2147483648
151 ; CHECK-NEXT: [[BB:%.*]] = and i32 [[B:%.*]], 2147483647
152 ; CHECK-NEXT: [[X:%.*]] = sub nuw i32 [[AA]], [[BB]]
153 ; CHECK-NEXT: [[TMP1:%.*]] = insertvalue { i32, i1 } { i32 undef, i1 false }, i32 [[X]], 0
154 ; CHECK-NEXT: ret { i32, i1 } [[TMP1]]
156 %aa = or i32 %a, 2147483648
157 %bb = and i32 %b, 2147483647
158 %x = call { i32, i1 } @llvm.usub.with.overflow.i32(i32 %aa, i32 %bb)
162 define { i32, i1 } @smultest1_nsw(i32 %a, i32 %b) {
163 ; CHECK-LABEL: @smultest1_nsw(
164 ; CHECK-NEXT: [[AA:%.*]] = and i32 [[A:%.*]], 4095
165 ; CHECK-NEXT: [[BB:%.*]] = and i32 [[B:%.*]], 524287
166 ; CHECK-NEXT: [[X:%.*]] = mul nuw nsw i32 [[AA]], [[BB]]
167 ; CHECK-NEXT: [[TMP1:%.*]] = insertvalue { i32, i1 } { i32 undef, i1 false }, i32 [[X]], 0
168 ; CHECK-NEXT: ret { i32, i1 } [[TMP1]]
170 %aa = and i32 %a, 4095 ; 0xfff
171 %bb = and i32 %b, 524287; 0x7ffff
172 %x = call { i32, i1 } @llvm.smul.with.overflow.i32(i32 %aa, i32 %bb)
176 define { i32, i1 } @smultest2_nsw(i32 %a, i32 %b) {
177 ; CHECK-LABEL: @smultest2_nsw(
178 ; CHECK-NEXT: [[AA:%.*]] = ashr i32 [[A:%.*]], 16
179 ; CHECK-NEXT: [[BB:%.*]] = ashr i32 [[B:%.*]], 16
180 ; CHECK-NEXT: [[X:%.*]] = mul nsw i32 [[AA]], [[BB]]
181 ; CHECK-NEXT: [[TMP1:%.*]] = insertvalue { i32, i1 } { i32 undef, i1 false }, i32 [[X]], 0
182 ; CHECK-NEXT: ret { i32, i1 } [[TMP1]]
184 %aa = ashr i32 %a, 16
185 %bb = ashr i32 %b, 16
186 %x = call { i32, i1 } @llvm.smul.with.overflow.i32(i32 %aa, i32 %bb)
190 define { i32, i1 } @smultest3_sw(i32 %a, i32 %b) {
191 ; CHECK-LABEL: @smultest3_sw(
192 ; CHECK-NEXT: [[AA:%.*]] = ashr i32 [[A:%.*]], 16
193 ; CHECK-NEXT: [[BB:%.*]] = ashr i32 [[B:%.*]], 15
194 ; CHECK-NEXT: [[X:%.*]] = call { i32, i1 } @llvm.smul.with.overflow.i32(i32 [[AA]], i32 [[BB]])
195 ; CHECK-NEXT: ret { i32, i1 } [[X]]
197 %aa = ashr i32 %a, 16
198 %bb = ashr i32 %b, 15
199 %x = call { i32, i1 } @llvm.smul.with.overflow.i32(i32 %aa, i32 %bb)
203 define { i32, i1 } @umultest_nuw(i32 %a, i32 %b) {
204 ; CHECK-LABEL: @umultest_nuw(
205 ; CHECK-NEXT: [[AA:%.*]] = and i32 [[A:%.*]], 65535
206 ; CHECK-NEXT: [[BB:%.*]] = and i32 [[B:%.*]], 65535
207 ; CHECK-NEXT: [[X:%.*]] = mul nuw i32 [[AA]], [[BB]]
208 ; CHECK-NEXT: [[TMP1:%.*]] = insertvalue { i32, i1 } { i32 undef, i1 false }, i32 [[X]], 0
209 ; CHECK-NEXT: ret { i32, i1 } [[TMP1]]
211 %aa = and i32 %a, 65535 ; 0xffff
212 %bb = and i32 %b, 65535 ; 0xffff
213 %x = call { i32, i1 } @llvm.umul.with.overflow.i32(i32 %aa, i32 %bb)
217 define i8 @umultest1(i8 %A, i1* %overflowPtr) {
218 ; CHECK-LABEL: @umultest1(
219 ; CHECK-NEXT: store i1 false, i1* [[OVERFLOWPTR:%.*]], align 1
220 ; CHECK-NEXT: ret i8 0
222 %x = call { i8, i1 } @llvm.umul.with.overflow.i8(i8 0, i8 %A)
223 %y = extractvalue { i8, i1 } %x, 0
224 %z = extractvalue { i8, i1 } %x, 1
225 store i1 %z, i1* %overflowPtr
229 define i8 @umultest2(i8 %A, i1* %overflowPtr) {
230 ; CHECK-LABEL: @umultest2(
231 ; CHECK-NEXT: store i1 false, i1* [[OVERFLOWPTR:%.*]], align 1
232 ; CHECK-NEXT: ret i8 [[A:%.*]]
234 %x = call { i8, i1 } @llvm.umul.with.overflow.i8(i8 1, i8 %A)
235 %y = extractvalue { i8, i1 } %x, 0
236 %z = extractvalue { i8, i1 } %x, 1
237 store i1 %z, i1* %overflowPtr
241 define i32 @umultest3(i32 %n) nounwind {
242 ; CHECK-LABEL: @umultest3(
243 ; CHECK-NEXT: [[SHR:%.*]] = lshr i32 [[N:%.*]], 2
244 ; CHECK-NEXT: [[MUL:%.*]] = mul nuw i32 [[SHR]], 3
245 ; CHECK-NEXT: ret i32 [[MUL]]
247 %shr = lshr i32 %n, 2
248 %mul = call { i32, i1 } @llvm.umul.with.overflow.i32(i32 %shr, i32 3)
249 %ov = extractvalue { i32, i1 } %mul, 1
250 %res = extractvalue { i32, i1 } %mul, 0
251 %ret = select i1 %ov, i32 -1, i32 %res
255 define i32 @umultest4(i32 %n) nounwind {
256 ; CHECK-LABEL: @umultest4(
257 ; CHECK-NEXT: [[SHR:%.*]] = lshr i32 [[N:%.*]], 1
258 ; CHECK-NEXT: [[MUL:%.*]] = call { i32, i1 } @llvm.umul.with.overflow.i32(i32 [[SHR]], i32 4)
259 ; CHECK-NEXT: [[OV:%.*]] = extractvalue { i32, i1 } [[MUL]], 1
260 ; CHECK-NEXT: [[RES:%.*]] = extractvalue { i32, i1 } [[MUL]], 0
261 ; CHECK-NEXT: [[RET:%.*]] = select i1 [[OV]], i32 -1, i32 [[RES]]
262 ; CHECK-NEXT: ret i32 [[RET]]
264 %shr = lshr i32 %n, 1
265 %mul = call { i32, i1 } @llvm.umul.with.overflow.i32(i32 %shr, i32 4)
266 %ov = extractvalue { i32, i1 } %mul, 1
267 %res = extractvalue { i32, i1 } %mul, 0
268 %ret = select i1 %ov, i32 -1, i32 %res
272 define { i32, i1 } @umultest5(i32 %x, i32 %y) nounwind {
273 ; CHECK-LABEL: @umultest5(
274 ; CHECK-NEXT: [[OR_X:%.*]] = or i32 [[X:%.*]], -2147483648
275 ; CHECK-NEXT: [[OR_Y:%.*]] = or i32 [[Y:%.*]], -2147483648
276 ; CHECK-NEXT: [[MUL:%.*]] = mul i32 [[OR_X]], [[OR_Y]]
277 ; CHECK-NEXT: [[TMP1:%.*]] = insertvalue { i32, i1 } { i32 undef, i1 true }, i32 [[MUL]], 0
278 ; CHECK-NEXT: ret { i32, i1 } [[TMP1]]
280 %or_x = or i32 %x, 2147483648
281 %or_y = or i32 %y, 2147483648
282 %mul = call { i32, i1 } @llvm.umul.with.overflow.i32(i32 %or_x, i32 %or_y)
286 define i1 @overflow_div_add(i32 %v1, i32 %v2) nounwind {
287 ; CHECK-LABEL: @overflow_div_add(
288 ; CHECK-NEXT: ret i1 false
290 %div = sdiv i32 %v1, 2
291 %t = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %div, i32 1)
292 %obit = extractvalue { i32, i1 } %t, 1
296 define i1 @overflow_div_sub(i32 %v1, i32 %v2) nounwind {
297 ; Check cases where the known sign bits are larger than the word size.
298 ; CHECK-LABEL: @overflow_div_sub(
299 ; CHECK-NEXT: ret i1 false
301 %a = ashr i32 %v1, 18
302 %div = sdiv i32 %a, 65536
303 %t = call { i32, i1 } @llvm.ssub.with.overflow.i32(i32 %div, i32 1)
304 %obit = extractvalue { i32, i1 } %t, 1
308 define i1 @overflow_mod_mul(i32 %v1, i32 %v2) nounwind {
309 ; CHECK-LABEL: @overflow_mod_mul(
310 ; CHECK-NEXT: ret i1 false
312 %rem = srem i32 %v1, 1000
313 %t = call { i32, i1 } @llvm.smul.with.overflow.i32(i32 %rem, i32 %rem)
314 %obit = extractvalue { i32, i1 } %t, 1
318 define i1 @overflow_mod_overflow_mul(i32 %v1, i32 %v2) nounwind {
319 ; CHECK-LABEL: @overflow_mod_overflow_mul(
320 ; CHECK-NEXT: [[REM:%.*]] = srem i32 [[V1:%.*]], 65537
321 ; CHECK-NEXT: [[T:%.*]] = call { i32, i1 } @llvm.smul.with.overflow.i32(i32 [[REM]], i32 [[REM]])
322 ; CHECK-NEXT: [[OBIT:%.*]] = extractvalue { i32, i1 } [[T]], 1
323 ; CHECK-NEXT: ret i1 [[OBIT]]
325 %rem = srem i32 %v1, 65537
326 ; This may overflow because the result of the mul operands may be greater than 16bits
327 ; and the result greater than 32.
328 %t = call { i32, i1 } @llvm.smul.with.overflow.i32(i32 %rem, i32 %rem)
329 %obit = extractvalue { i32, i1 } %t, 1
333 define { i32, i1 } @ssubtest_reorder(i8 %a) {
334 ; CHECK-LABEL: @ssubtest_reorder(
335 ; CHECK-NEXT: [[AA:%.*]] = sext i8 [[A:%.*]] to i32
336 ; CHECK-NEXT: [[X:%.*]] = sub nsw i32 0, [[AA]]
337 ; CHECK-NEXT: [[TMP1:%.*]] = insertvalue { i32, i1 } { i32 undef, i1 false }, i32 [[X]], 0
338 ; CHECK-NEXT: ret { i32, i1 } [[TMP1]]
340 %aa = sext i8 %a to i32
341 %x = call { i32, i1 } @llvm.ssub.with.overflow.i32(i32 0, i32 %aa)
345 define { i32, i1 } @never_overflows_ssub_test0(i32 %a) {
346 ; CHECK-LABEL: @never_overflows_ssub_test0(
347 ; CHECK-NEXT: [[X:%.*]] = insertvalue { i32, i1 } { i32 undef, i1 false }, i32 [[A:%.*]], 0
348 ; CHECK-NEXT: ret { i32, i1 } [[X]]
350 %x = call { i32, i1 } @llvm.ssub.with.overflow.i32(i32 %a, i32 0)
354 define i1 @uadd_res_ult_x(i32 %x, i32 %y, i1* %p) nounwind {
355 ; CHECK-LABEL: @uadd_res_ult_x(
356 ; CHECK-NEXT: [[A:%.*]] = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[X:%.*]], i32 [[Y:%.*]])
357 ; CHECK-NEXT: [[B:%.*]] = extractvalue { i32, i1 } [[A]], 1
358 ; CHECK-NEXT: store i1 [[B]], i1* [[P:%.*]], align 1
359 ; CHECK-NEXT: [[C:%.*]] = extractvalue { i32, i1 } [[A]], 0
360 ; CHECK-NEXT: [[D:%.*]] = icmp ult i32 [[C]], [[X]]
361 ; CHECK-NEXT: ret i1 [[D]]
363 %a = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 %x, i32 %y)
364 %b = extractvalue { i32, i1 } %a, 1
366 %c = extractvalue { i32, i1 } %a, 0
367 %d = icmp ult i32 %c, %x
371 define i1 @uadd_res_ult_y(i32 %x, i32 %y, i1* %p) nounwind {
372 ; CHECK-LABEL: @uadd_res_ult_y(
373 ; CHECK-NEXT: [[A:%.*]] = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[X:%.*]], i32 [[Y:%.*]])
374 ; CHECK-NEXT: [[B:%.*]] = extractvalue { i32, i1 } [[A]], 1
375 ; CHECK-NEXT: store i1 [[B]], i1* [[P:%.*]], align 1
376 ; CHECK-NEXT: [[C:%.*]] = extractvalue { i32, i1 } [[A]], 0
377 ; CHECK-NEXT: [[D:%.*]] = icmp ult i32 [[C]], [[Y]]
378 ; CHECK-NEXT: ret i1 [[D]]
380 %a = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 %x, i32 %y)
381 %b = extractvalue { i32, i1 } %a, 1
383 %c = extractvalue { i32, i1 } %a, 0
384 %d = icmp ult i32 %c, %y
388 define i1 @uadd_res_ugt_x(i32 %xx, i32 %y, i1* %p) nounwind {
389 ; CHECK-LABEL: @uadd_res_ugt_x(
390 ; CHECK-NEXT: [[X:%.*]] = urem i32 42, [[XX:%.*]]
391 ; CHECK-NEXT: [[A:%.*]] = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[X]], i32 [[Y:%.*]])
392 ; CHECK-NEXT: [[B:%.*]] = extractvalue { i32, i1 } [[A]], 1
393 ; CHECK-NEXT: store i1 [[B]], i1* [[P:%.*]], align 1
394 ; CHECK-NEXT: [[C:%.*]] = extractvalue { i32, i1 } [[A]], 0
395 ; CHECK-NEXT: [[D:%.*]] = icmp ugt i32 [[X]], [[C]]
396 ; CHECK-NEXT: ret i1 [[D]]
398 %x = urem i32 42, %xx ; Thwart complexity-based canonicalization
399 %a = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 %x, i32 %y)
400 %b = extractvalue { i32, i1 } %a, 1
402 %c = extractvalue { i32, i1 } %a, 0
403 %d = icmp ugt i32 %x, %c
407 define i1 @uadd_res_ugt_y(i32 %x, i32 %yy, i1* %p) nounwind {
408 ; CHECK-LABEL: @uadd_res_ugt_y(
409 ; CHECK-NEXT: [[Y:%.*]] = urem i32 42, [[YY:%.*]]
410 ; CHECK-NEXT: [[A:%.*]] = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[X:%.*]], i32 [[Y]])
411 ; CHECK-NEXT: [[B:%.*]] = extractvalue { i32, i1 } [[A]], 1
412 ; CHECK-NEXT: store i1 [[B]], i1* [[P:%.*]], align 1
413 ; CHECK-NEXT: [[C:%.*]] = extractvalue { i32, i1 } [[A]], 0
414 ; CHECK-NEXT: [[D:%.*]] = icmp ugt i32 [[Y]], [[C]]
415 ; CHECK-NEXT: ret i1 [[D]]
417 %y = urem i32 42, %yy ; Thwart complexity-based canonicalization
418 %a = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 %x, i32 %y)
419 %b = extractvalue { i32, i1 } %a, 1
421 %c = extractvalue { i32, i1 } %a, 0
422 %d = icmp ugt i32 %y, %c
426 define i1 @uadd_res_ult_const(i32 %x, i1* %p) nounwind {
427 ; CHECK-LABEL: @uadd_res_ult_const(
428 ; CHECK-NEXT: [[A:%.*]] = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[X:%.*]], i32 42)
429 ; CHECK-NEXT: [[B:%.*]] = extractvalue { i32, i1 } [[A]], 1
430 ; CHECK-NEXT: store i1 [[B]], i1* [[P:%.*]], align 1
431 ; CHECK-NEXT: [[C:%.*]] = extractvalue { i32, i1 } [[A]], 0
432 ; CHECK-NEXT: [[D:%.*]] = icmp ult i32 [[C]], 42
433 ; CHECK-NEXT: ret i1 [[D]]
435 %a = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 %x, i32 42)
436 %b = extractvalue { i32, i1 } %a, 1
438 %c = extractvalue { i32, i1 } %a, 0
439 %d = icmp ult i32 %c, 42
443 define i1 @uadd_res_ult_const_one(i32 %x, i1* %p) nounwind {
444 ; CHECK-LABEL: @uadd_res_ult_const_one(
445 ; CHECK-NEXT: [[A:%.*]] = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[X:%.*]], i32 1)
446 ; CHECK-NEXT: [[B:%.*]] = extractvalue { i32, i1 } [[A]], 1
447 ; CHECK-NEXT: store i1 [[B]], i1* [[P:%.*]], align 1
448 ; CHECK-NEXT: [[C:%.*]] = extractvalue { i32, i1 } [[A]], 0
449 ; CHECK-NEXT: [[D:%.*]] = icmp eq i32 [[C]], 0
450 ; CHECK-NEXT: ret i1 [[D]]
452 %a = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 %x, i32 1)
453 %b = extractvalue { i32, i1 } %a, 1
455 %c = extractvalue { i32, i1 } %a, 0
456 %d = icmp ult i32 %c, 1
460 define i1 @uadd_res_ult_const_minus_one(i32 %x, i1* %p) nounwind {
461 ; CHECK-LABEL: @uadd_res_ult_const_minus_one(
462 ; CHECK-NEXT: [[A:%.*]] = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[X:%.*]], i32 -1)
463 ; CHECK-NEXT: [[B:%.*]] = extractvalue { i32, i1 } [[A]], 1
464 ; CHECK-NEXT: store i1 [[B]], i1* [[P:%.*]], align 1
465 ; CHECK-NEXT: [[C:%.*]] = extractvalue { i32, i1 } [[A]], 0
466 ; CHECK-NEXT: [[D:%.*]] = icmp ne i32 [[C]], -1
467 ; CHECK-NEXT: ret i1 [[D]]
469 %a = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 %x, i32 -1)
470 %b = extractvalue { i32, i1 } %a, 1
472 %c = extractvalue { i32, i1 } %a, 0
473 %d = icmp ult i32 %c, -1
477 define { i32, i1 } @sadd_canonicalize_constant_arg0(i32 %x) nounwind {
478 ; CHECK-LABEL: @sadd_canonicalize_constant_arg0(
479 ; CHECK-NEXT: [[A:%.*]] = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 [[X:%.*]], i32 42)
480 ; CHECK-NEXT: ret { i32, i1 } [[A]]
482 %a = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 42, i32 %x)
486 define { i32, i1 } @uadd_canonicalize_constant_arg0(i32 %x) nounwind {
487 ; CHECK-LABEL: @uadd_canonicalize_constant_arg0(
488 ; CHECK-NEXT: [[A:%.*]] = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[X:%.*]], i32 42)
489 ; CHECK-NEXT: ret { i32, i1 } [[A]]
491 %a = call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 42, i32 %x)
495 define { i32, i1 } @ssub_no_canonicalize_constant_arg0(i32 %x) nounwind {
496 ; CHECK-LABEL: @ssub_no_canonicalize_constant_arg0(
497 ; CHECK-NEXT: [[A:%.*]] = call { i32, i1 } @llvm.ssub.with.overflow.i32(i32 42, i32 [[X:%.*]])
498 ; CHECK-NEXT: ret { i32, i1 } [[A]]
500 %a = call { i32, i1 } @llvm.ssub.with.overflow.i32(i32 42, i32 %x)
504 define { i32, i1 } @usub_no_canonicalize_constant_arg0(i32 %x) nounwind {
505 ; CHECK-LABEL: @usub_no_canonicalize_constant_arg0(
506 ; CHECK-NEXT: [[A:%.*]] = call { i32, i1 } @llvm.usub.with.overflow.i32(i32 42, i32 [[X:%.*]])
507 ; CHECK-NEXT: ret { i32, i1 } [[A]]
509 %a = call { i32, i1 } @llvm.usub.with.overflow.i32(i32 42, i32 %x)
513 define { i32, i1 } @smul_canonicalize_constant_arg0(i32 %x) nounwind {
514 ; CHECK-LABEL: @smul_canonicalize_constant_arg0(
515 ; CHECK-NEXT: [[A:%.*]] = call { i32, i1 } @llvm.smul.with.overflow.i32(i32 [[X:%.*]], i32 42)
516 ; CHECK-NEXT: ret { i32, i1 } [[A]]
518 %a = call { i32, i1 } @llvm.smul.with.overflow.i32(i32 42, i32 %x)
522 define { i32, i1 } @umul_canonicalize_constant_arg0(i32 %x) nounwind {
523 ; CHECK-LABEL: @umul_canonicalize_constant_arg0(
524 ; CHECK-NEXT: [[A:%.*]] = call { i32, i1 } @llvm.umul.with.overflow.i32(i32 [[X:%.*]], i32 42)
525 ; CHECK-NEXT: ret { i32, i1 } [[A]]
527 %a = call { i32, i1 } @llvm.umul.with.overflow.i32(i32 42, i32 %x)
531 ; Always overflow tests
533 define { i8, i1 } @uadd_always_overflow(i8 %x) nounwind {
534 ; CHECK-LABEL: @uadd_always_overflow(
535 ; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X:%.*]], 63
536 ; CHECK-NEXT: [[TMP2:%.*]] = insertvalue { i8, i1 } { i8 undef, i1 true }, i8 [[TMP1]], 0
537 ; CHECK-NEXT: ret { i8, i1 } [[TMP2]]
540 %a = call { i8, i1 } @llvm.uadd.with.overflow.i8(i8 %y, i8 64)
544 define { i8, i1 } @usub_always_overflow(i8 %x) nounwind {
545 ; CHECK-LABEL: @usub_always_overflow(
546 ; CHECK-NEXT: [[Y:%.*]] = or i8 [[X:%.*]], 64
547 ; CHECK-NEXT: [[A:%.*]] = sub nsw i8 63, [[Y]]
548 ; CHECK-NEXT: [[TMP1:%.*]] = insertvalue { i8, i1 } { i8 undef, i1 true }, i8 [[A]], 0
549 ; CHECK-NEXT: ret { i8, i1 } [[TMP1]]
552 %a = call { i8, i1 } @llvm.usub.with.overflow.i8(i8 63, i8 %y)
556 define { i8, i1 } @umul_always_overflow(i8 %x) nounwind {
557 ; CHECK-LABEL: @umul_always_overflow(
558 ; CHECK-NEXT: [[A:%.*]] = shl i8 [[X:%.*]], 1
559 ; CHECK-NEXT: [[TMP1:%.*]] = insertvalue { i8, i1 } { i8 undef, i1 true }, i8 [[A]], 0
560 ; CHECK-NEXT: ret { i8, i1 } [[TMP1]]
563 %a = call { i8, i1 } @llvm.umul.with.overflow.i8(i8 %y, i8 2)
567 define { i8, i1 } @sadd_always_overflow(i8 %x) nounwind {
568 ; CHECK-LABEL: @sadd_always_overflow(
569 ; CHECK-NEXT: [[C:%.*]] = icmp sgt i8 [[X:%.*]], 100
570 ; CHECK-NEXT: [[Y:%.*]] = select i1 [[C]], i8 [[X]], i8 100
571 ; CHECK-NEXT: [[A:%.*]] = add nuw i8 [[Y]], 28
572 ; CHECK-NEXT: [[TMP1:%.*]] = insertvalue { i8, i1 } { i8 undef, i1 true }, i8 [[A]], 0
573 ; CHECK-NEXT: ret { i8, i1 } [[TMP1]]
575 %c = icmp sgt i8 %x, 100
576 %y = select i1 %c, i8 %x, i8 100
577 %a = call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 %y, i8 28)
581 define { i8, i1 } @ssub_always_overflow(i8 %x) nounwind {
582 ; CHECK-LABEL: @ssub_always_overflow(
583 ; CHECK-NEXT: [[C:%.*]] = icmp sgt i8 [[X:%.*]], 29
584 ; CHECK-NEXT: [[Y:%.*]] = select i1 [[C]], i8 [[X]], i8 29
585 ; CHECK-NEXT: [[A:%.*]] = sub nuw i8 -100, [[Y]]
586 ; CHECK-NEXT: [[TMP1:%.*]] = insertvalue { i8, i1 } { i8 undef, i1 true }, i8 [[A]], 0
587 ; CHECK-NEXT: ret { i8, i1 } [[TMP1]]
589 %c = icmp sgt i8 %x, 29
590 %y = select i1 %c, i8 %x, i8 29
591 %a = call { i8, i1 } @llvm.ssub.with.overflow.i8(i8 -100, i8 %y)
595 define { i8, i1 } @smul_always_overflow(i8 %x) nounwind {
596 ; CHECK-LABEL: @smul_always_overflow(
597 ; CHECK-NEXT: [[C:%.*]] = icmp sgt i8 [[X:%.*]], 100
598 ; CHECK-NEXT: [[Y:%.*]] = select i1 [[C]], i8 [[X]], i8 100
599 ; CHECK-NEXT: [[A:%.*]] = call { i8, i1 } @llvm.smul.with.overflow.i8(i8 [[Y]], i8 2)
600 ; CHECK-NEXT: ret { i8, i1 } [[A]]
602 %c = icmp sgt i8 %x, 100
603 %y = select i1 %c, i8 %x, i8 100
604 %a = call { i8, i1 } @llvm.smul.with.overflow.i8(i8 %y, i8 2)