1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
6 define i1 @test1(i32 %a) {
8 ; CHECK-NEXT: [[C:%.*]] = icmp ugt i32 [[A:%.*]], -5
9 ; CHECK-NEXT: ret i1 [[C]]
12 %c = icmp ult i32 %b, 4
16 define <2 x i1> @test1vec(<2 x i32> %a) {
17 ; CHECK-LABEL: @test1vec(
18 ; CHECK-NEXT: [[C:%.*]] = icmp ugt <2 x i32> [[A:%.*]], <i32 -5, i32 -5>
19 ; CHECK-NEXT: ret <2 x i1> [[C]]
21 %b = add <2 x i32> %a, <i32 4, i32 4>
22 %c = icmp ult <2 x i32> %b, <i32 4, i32 4>
26 define i1 @test2(i32 %a) {
27 ; CHECK-LABEL: @test2(
28 ; CHECK-NEXT: [[C:%.*]] = icmp ult i32 [[A:%.*]], 4
29 ; CHECK-NEXT: ret i1 [[C]]
32 %c = icmp ugt i32 %b, -5
36 define <2 x i1> @test2vec(<2 x i32> %a) {
37 ; CHECK-LABEL: @test2vec(
38 ; CHECK-NEXT: [[C:%.*]] = icmp ult <2 x i32> [[A:%.*]], <i32 4, i32 4>
39 ; CHECK-NEXT: ret <2 x i1> [[C]]
41 %b = sub <2 x i32> %a, <i32 4, i32 4>
42 %c = icmp ugt <2 x i32> %b, <i32 -5, i32 -5>
46 define i1 @test3(i32 %a) {
47 ; CHECK-LABEL: @test3(
48 ; CHECK-NEXT: [[C:%.*]] = icmp sgt i32 [[A:%.*]], 2147483643
49 ; CHECK-NEXT: ret i1 [[C]]
52 %c = icmp slt i32 %b, 2147483652
56 define <2 x i1> @test3vec(<2 x i32> %a) {
57 ; CHECK-LABEL: @test3vec(
58 ; CHECK-NEXT: [[C:%.*]] = icmp sgt <2 x i32> [[A:%.*]], <i32 2147483643, i32 2147483643>
59 ; CHECK-NEXT: ret <2 x i1> [[C]]
61 %b = add <2 x i32> %a, <i32 4, i32 4>
62 %c = icmp slt <2 x i32> %b, <i32 2147483652, i32 2147483652>
66 define i1 @test4(i32 %a) {
67 ; CHECK-LABEL: @test4(
68 ; CHECK-NEXT: [[C:%.*]] = icmp slt i32 [[A:%.*]], -4
69 ; CHECK-NEXT: ret i1 [[C]]
71 %b = add i32 %a, 2147483652
72 %c = icmp sge i32 %b, 4
76 define { i32, i1 } @test4multiuse(i32 %a) {
77 ; CHECK-LABEL: @test4multiuse(
78 ; CHECK-NEXT: [[B:%.*]] = add i32 [[A:%.*]], -2147483644
79 ; CHECK-NEXT: [[C:%.*]] = icmp slt i32 [[B]], -4
80 ; CHECK-NEXT: [[TMP:%.*]] = insertvalue { i32, i1 } undef, i32 [[B]], 0
81 ; CHECK-NEXT: [[RES:%.*]] = insertvalue { i32, i1 } [[TMP]], i1 [[C]], 1
82 ; CHECK-NEXT: ret { i32, i1 } [[RES]]
85 %b = add i32 %a, -2147483644
86 %c = icmp slt i32 %b, -4
88 %tmp = insertvalue { i32, i1 } undef, i32 %b, 0
89 %res = insertvalue { i32, i1 } %tmp, i1 %c, 1
94 define <2 x i1> @test4vec(<2 x i32> %a) {
95 ; CHECK-LABEL: @test4vec(
96 ; CHECK-NEXT: [[C:%.*]] = icmp slt <2 x i32> [[A:%.*]], <i32 -4, i32 -4>
97 ; CHECK-NEXT: ret <2 x i1> [[C]]
99 %b = add <2 x i32> %a, <i32 2147483652, i32 2147483652>
100 %c = icmp sge <2 x i32> %b, <i32 4, i32 4>
104 ; icmp Pred (add nsw X, C2), C --> icmp Pred X, (C - C2), when C - C2 does not overflow.
105 ; This becomes equality because it's at the limit.
107 define i1 @nsw_slt1(i8 %a) {
108 ; CHECK-LABEL: @nsw_slt1(
109 ; CHECK-NEXT: [[C:%.*]] = icmp eq i8 [[A:%.*]], -128
110 ; CHECK-NEXT: ret i1 [[C]]
112 %b = add nsw i8 %a, 100
113 %c = icmp slt i8 %b, -27
117 define <2 x i1> @nsw_slt1_splat_vec(<2 x i8> %a) {
118 ; CHECK-LABEL: @nsw_slt1_splat_vec(
119 ; CHECK-NEXT: [[C:%.*]] = icmp eq <2 x i8> [[A:%.*]], <i8 -128, i8 -128>
120 ; CHECK-NEXT: ret <2 x i1> [[C]]
122 %b = add nsw <2 x i8> %a, <i8 100, i8 100>
123 %c = icmp slt <2 x i8> %b, <i8 -27, i8 -27>
127 ; icmp Pred (add nsw X, C2), C --> icmp Pred X, (C - C2), when C - C2 does not overflow.
128 ; This becomes equality because it's at the limit.
130 define i1 @nsw_slt2(i8 %a) {
131 ; CHECK-LABEL: @nsw_slt2(
132 ; CHECK-NEXT: [[C:%.*]] = icmp ne i8 [[A:%.*]], 127
133 ; CHECK-NEXT: ret i1 [[C]]
135 %b = add nsw i8 %a, -100
136 %c = icmp slt i8 %b, 27
140 define <2 x i1> @nsw_slt2_splat_vec(<2 x i8> %a) {
141 ; CHECK-LABEL: @nsw_slt2_splat_vec(
142 ; CHECK-NEXT: [[C:%.*]] = icmp ne <2 x i8> [[A:%.*]], <i8 127, i8 127>
143 ; CHECK-NEXT: ret <2 x i1> [[C]]
145 %b = add nsw <2 x i8> %a, <i8 -100, i8 -100>
146 %c = icmp slt <2 x i8> %b, <i8 27, i8 27>
150 ; icmp Pred (add nsw X, C2), C --> icmp Pred X, (C - C2), when C - C2 does not overflow.
151 ; Less than the limit, so the predicate doesn't change.
153 define i1 @nsw_slt3(i8 %a) {
154 ; CHECK-LABEL: @nsw_slt3(
155 ; CHECK-NEXT: [[C:%.*]] = icmp slt i8 [[A:%.*]], -126
156 ; CHECK-NEXT: ret i1 [[C]]
158 %b = add nsw i8 %a, 100
159 %c = icmp slt i8 %b, -26
163 ; icmp Pred (add nsw X, C2), C --> icmp Pred X, (C - C2), when C - C2 does not overflow.
164 ; Less than the limit, so the predicate doesn't change.
166 define i1 @nsw_slt4(i8 %a) {
167 ; CHECK-LABEL: @nsw_slt4(
168 ; CHECK-NEXT: [[C:%.*]] = icmp slt i8 [[A:%.*]], 126
169 ; CHECK-NEXT: ret i1 [[C]]
171 %b = add nsw i8 %a, -100
172 %c = icmp slt i8 %b, 26
176 ; icmp Pred (add nsw X, C2), C --> icmp Pred X, (C - C2), when C - C2 does not overflow.
177 ; Try sgt to make sure that works too.
179 define i1 @nsw_sgt1(i8 %a) {
180 ; CHECK-LABEL: @nsw_sgt1(
181 ; CHECK-NEXT: [[C:%.*]] = icmp eq i8 [[A:%.*]], 127
182 ; CHECK-NEXT: ret i1 [[C]]
184 %b = add nsw i8 %a, -100
185 %c = icmp sgt i8 %b, 26
189 define <2 x i1> @nsw_sgt1_splat_vec(<2 x i8> %a) {
190 ; CHECK-LABEL: @nsw_sgt1_splat_vec(
191 ; CHECK-NEXT: [[C:%.*]] = icmp eq <2 x i8> [[A:%.*]], <i8 127, i8 127>
192 ; CHECK-NEXT: ret <2 x i1> [[C]]
194 %b = add nsw <2 x i8> %a, <i8 -100, i8 -100>
195 %c = icmp sgt <2 x i8> %b, <i8 26, i8 26>
199 define i1 @nsw_sgt2(i8 %a) {
200 ; CHECK-LABEL: @nsw_sgt2(
201 ; CHECK-NEXT: [[C:%.*]] = icmp sgt i8 [[A:%.*]], -126
202 ; CHECK-NEXT: ret i1 [[C]]
204 %b = add nsw i8 %a, 100
205 %c = icmp sgt i8 %b, -26
209 define <2 x i1> @nsw_sgt2_splat_vec(<2 x i8> %a) {
210 ; CHECK-LABEL: @nsw_sgt2_splat_vec(
211 ; CHECK-NEXT: [[C:%.*]] = icmp sgt <2 x i8> [[A:%.*]], <i8 -126, i8 -126>
212 ; CHECK-NEXT: ret <2 x i1> [[C]]
214 %b = add nsw <2 x i8> %a, <i8 100, i8 100>
215 %c = icmp sgt <2 x i8> %b, <i8 -26, i8 -26>
219 ; icmp Pred (add nsw X, C2), C --> icmp Pred X, (C - C2), when C - C2 does not overflow.
220 ; Comparison with 0 doesn't need special-casing.
222 define i1 @slt_zero_add_nsw(i32 %a) {
223 ; CHECK-LABEL: @slt_zero_add_nsw(
224 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[A:%.*]], -1
225 ; CHECK-NEXT: ret i1 [[CMP]]
227 %add = add nsw i32 %a, 1
228 %cmp = icmp slt i32 %add, 0
232 ; The same fold should work with vectors.
234 define <2 x i1> @slt_zero_add_nsw_splat_vec(<2 x i8> %a) {
235 ; CHECK-LABEL: @slt_zero_add_nsw_splat_vec(
236 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt <2 x i8> [[A:%.*]], <i8 -1, i8 -1>
237 ; CHECK-NEXT: ret <2 x i1> [[CMP]]
239 %add = add nsw <2 x i8> %a, <i8 1, i8 1>
240 %cmp = icmp slt <2 x i8> %add, zeroinitializer
244 ; Test the edges - instcombine should not interfere with simplification to constants.
245 ; Constant subtraction does not overflow, but this is false.
247 define i1 @nsw_slt3_ov_no(i8 %a) {
248 ; CHECK-LABEL: @nsw_slt3_ov_no(
249 ; CHECK-NEXT: ret i1 false
251 %b = add nsw i8 %a, 100
252 %c = icmp slt i8 %b, -28
256 ; Test the edges - instcombine should not interfere with simplification to constants.
257 ; Constant subtraction overflows. This is false.
259 define i1 @nsw_slt4_ov(i8 %a) {
260 ; CHECK-LABEL: @nsw_slt4_ov(
261 ; CHECK-NEXT: ret i1 false
263 %b = add nsw i8 %a, 100
264 %c = icmp slt i8 %b, -29
268 ; Test the edges - instcombine should not interfere with simplification to constants.
269 ; Constant subtraction overflows. This is true.
271 define i1 @nsw_slt5_ov(i8 %a) {
272 ; CHECK-LABEL: @nsw_slt5_ov(
273 ; CHECK-NEXT: ret i1 true
275 %b = add nsw i8 %a, -100
276 %c = icmp slt i8 %b, 28
280 ; InstCombine should not thwart this opportunity to simplify completely.
282 define i1 @slt_zero_add_nsw_signbit(i8 %x) {
283 ; CHECK-LABEL: @slt_zero_add_nsw_signbit(
284 ; CHECK-NEXT: ret i1 true
286 %y = add nsw i8 %x, -128
287 %z = icmp slt i8 %y, 0
291 ; InstCombine should not thwart this opportunity to simplify completely.
293 define i1 @slt_zero_add_nuw_signbit(i8 %x) {
294 ; CHECK-LABEL: @slt_zero_add_nuw_signbit(
295 ; CHECK-NEXT: ret i1 true
297 %y = add nuw i8 %x, 128
298 %z = icmp slt i8 %y, 0
302 define i1 @reduce_add_ult(i32 %in) {
303 ; CHECK-LABEL: @reduce_add_ult(
304 ; CHECK-NEXT: [[A18:%.*]] = icmp ult i32 [[IN:%.*]], 9
305 ; CHECK-NEXT: ret i1 [[A18]]
307 %a6 = add nuw i32 %in, 3
308 %a18 = icmp ult i32 %a6, 12
312 define i1 @reduce_add_ugt(i32 %in) {
313 ; CHECK-LABEL: @reduce_add_ugt(
314 ; CHECK-NEXT: [[A18:%.*]] = icmp ugt i32 [[IN:%.*]], 9
315 ; CHECK-NEXT: ret i1 [[A18]]
317 %a6 = add nuw i32 %in, 3
318 %a18 = icmp ugt i32 %a6, 12
322 define i1 @reduce_add_ule(i32 %in) {
323 ; CHECK-LABEL: @reduce_add_ule(
324 ; CHECK-NEXT: [[A18:%.*]] = icmp ult i32 [[IN:%.*]], 10
325 ; CHECK-NEXT: ret i1 [[A18]]
327 %a6 = add nuw i32 %in, 3
328 %a18 = icmp ule i32 %a6, 12
332 define i1 @reduce_add_uge(i32 %in) {
333 ; CHECK-LABEL: @reduce_add_uge(
334 ; CHECK-NEXT: [[A18:%.*]] = icmp ugt i32 [[IN:%.*]], 8
335 ; CHECK-NEXT: ret i1 [[A18]]
337 %a6 = add nuw i32 %in, 3
338 %a18 = icmp uge i32 %a6, 12
342 define i1 @ult_add_ssubov(i32 %in) {
343 ; CHECK-LABEL: @ult_add_ssubov(
344 ; CHECK-NEXT: ret i1 false
346 %a6 = add nuw i32 %in, 71
347 %a18 = icmp ult i32 %a6, 3
351 define i1 @ult_add_nonuw(i8 %in) {
352 ; CHECK-LABEL: @ult_add_nonuw(
353 ; CHECK-NEXT: [[A6:%.*]] = add i8 [[IN:%.*]], 71
354 ; CHECK-NEXT: [[A18:%.*]] = icmp ult i8 [[A6]], 12
355 ; CHECK-NEXT: ret i1 [[A18]]
358 %a18 = icmp ult i8 %a6, 12
362 define i1 @uge_add_nonuw(i32 %in) {
363 ; CHECK-LABEL: @uge_add_nonuw(
364 ; CHECK-NEXT: [[A6:%.*]] = add i32 [[IN:%.*]], 3
365 ; CHECK-NEXT: [[A18:%.*]] = icmp ugt i32 [[A6]], 11
366 ; CHECK-NEXT: ret i1 [[A18]]
369 %a18 = icmp uge i32 %a6, 12
373 ; Test unsigned add overflow patterns. The div ops are only here to
374 ; thwart complexity based canonicalization of the operand order.
376 define i1 @op_ugt_sum_commute1(i8 %p1, i8 %p2) {
377 ; CHECK-LABEL: @op_ugt_sum_commute1(
378 ; CHECK-NEXT: [[X:%.*]] = sdiv i8 42, [[P1:%.*]]
379 ; CHECK-NEXT: [[Y:%.*]] = sdiv i8 42, [[P2:%.*]]
380 ; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[X]], -1
381 ; CHECK-NEXT: [[C:%.*]] = icmp ugt i8 [[Y]], [[TMP1]]
382 ; CHECK-NEXT: ret i1 [[C]]
387 %c = icmp ugt i8 %x, %a
391 define <2 x i1> @op_ugt_sum_vec_commute2(<2 x i8> %p1, <2 x i8> %p2) {
392 ; CHECK-LABEL: @op_ugt_sum_vec_commute2(
393 ; CHECK-NEXT: [[X:%.*]] = sdiv <2 x i8> <i8 42, i8 -42>, [[P1:%.*]]
394 ; CHECK-NEXT: [[Y:%.*]] = sdiv <2 x i8> <i8 42, i8 -42>, [[P2:%.*]]
395 ; CHECK-NEXT: [[TMP1:%.*]] = xor <2 x i8> [[X]], <i8 -1, i8 -1>
396 ; CHECK-NEXT: [[C:%.*]] = icmp ugt <2 x i8> [[Y]], [[TMP1]]
397 ; CHECK-NEXT: ret <2 x i1> [[C]]
399 %x = sdiv <2 x i8> <i8 42, i8 -42>, %p1
400 %y = sdiv <2 x i8> <i8 42, i8 -42>, %p2
401 %a = add <2 x i8> %y, %x
402 %c = icmp ugt <2 x i8> %x, %a
406 define i1 @sum_ugt_op_uses(i8 %p1, i8 %p2, i8* %p3) {
407 ; CHECK-LABEL: @sum_ugt_op_uses(
408 ; CHECK-NEXT: [[X:%.*]] = sdiv i8 42, [[P1:%.*]]
409 ; CHECK-NEXT: [[Y:%.*]] = sdiv i8 42, [[P2:%.*]]
410 ; CHECK-NEXT: [[A:%.*]] = add nsw i8 [[X]], [[Y]]
411 ; CHECK-NEXT: store i8 [[A]], i8* [[P3:%.*]], align 1
412 ; CHECK-NEXT: [[C:%.*]] = icmp ugt i8 [[X]], [[A]]
413 ; CHECK-NEXT: ret i1 [[C]]
419 %c = icmp ugt i8 %x, %a
423 define <2 x i1> @sum_ult_op_vec_commute1(<2 x i8> %p1, <2 x i8> %p2) {
424 ; CHECK-LABEL: @sum_ult_op_vec_commute1(
425 ; CHECK-NEXT: [[X:%.*]] = sdiv <2 x i8> <i8 42, i8 -42>, [[P1:%.*]]
426 ; CHECK-NEXT: [[Y:%.*]] = sdiv <2 x i8> <i8 -42, i8 42>, [[P2:%.*]]
427 ; CHECK-NEXT: [[TMP1:%.*]] = xor <2 x i8> [[X]], <i8 -1, i8 -1>
428 ; CHECK-NEXT: [[C:%.*]] = icmp ugt <2 x i8> [[Y]], [[TMP1]]
429 ; CHECK-NEXT: ret <2 x i1> [[C]]
431 %x = sdiv <2 x i8> <i8 42, i8 -42>, %p1
432 %y = sdiv <2 x i8> <i8 -42, i8 42>, %p2
433 %a = add <2 x i8> %x, %y
434 %c = icmp ult <2 x i8> %a, %x
438 define i1 @sum_ult_op_commute2(i8 %p1, i8 %p2) {
439 ; CHECK-LABEL: @sum_ult_op_commute2(
440 ; CHECK-NEXT: [[X:%.*]] = sdiv i8 42, [[P1:%.*]]
441 ; CHECK-NEXT: [[Y:%.*]] = sdiv i8 42, [[P2:%.*]]
442 ; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[X]], -1
443 ; CHECK-NEXT: [[C:%.*]] = icmp ugt i8 [[Y]], [[TMP1]]
444 ; CHECK-NEXT: ret i1 [[C]]
449 %c = icmp ult i8 %a, %x
453 define i1 @sum_ult_op_uses(i8 %x, i8 %y, i8* %p) {
454 ; CHECK-LABEL: @sum_ult_op_uses(
455 ; CHECK-NEXT: [[A:%.*]] = add i8 [[Y:%.*]], [[X:%.*]]
456 ; CHECK-NEXT: store i8 [[A]], i8* [[P:%.*]], align 1
457 ; CHECK-NEXT: [[C:%.*]] = icmp ult i8 [[A]], [[X]]
458 ; CHECK-NEXT: ret i1 [[C]]
462 %c = icmp ult i8 %a, %x