1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
4 define i32 @sdiv1(i32 %x) {
6 ; CHECK-NEXT: [[Y:%.*]] = sdiv i32 [[X:%.*]], 8
7 ; CHECK-NEXT: ret i32 [[Y]]
13 define i32 @sdiv2(i32 %x) {
14 ; CHECK-LABEL: @sdiv2(
15 ; CHECK-NEXT: [[Y:%.*]] = ashr exact i32 [[X:%.*]], 3
16 ; CHECK-NEXT: ret i32 [[Y]]
18 %y = sdiv exact i32 %x, 8
22 define <2 x i32> @sdiv2_vec(<2 x i32> %x) {
23 ; CHECK-LABEL: @sdiv2_vec(
24 ; CHECK-NEXT: [[Y:%.*]] = ashr exact <2 x i32> [[X:%.*]], splat (i32 7)
25 ; CHECK-NEXT: ret <2 x i32> [[Y]]
27 %y = sdiv exact <2 x i32> %x, <i32 128, i32 128>
31 define i32 @sdiv3(i32 %x) {
32 ; CHECK-LABEL: @sdiv3(
33 ; CHECK-NEXT: [[X_FR:%.*]] = freeze i32 [[X:%.*]]
34 ; CHECK-NEXT: [[TMP1:%.*]] = srem i32 [[X_FR]], 3
35 ; CHECK-NEXT: [[Z:%.*]] = sub nsw i32 [[X_FR]], [[TMP1]]
36 ; CHECK-NEXT: ret i32 [[Z]]
43 define i32 @sdiv4(i32 %x) {
44 ; CHECK-LABEL: @sdiv4(
45 ; CHECK-NEXT: ret i32 [[X:%.*]]
47 %y = sdiv exact i32 %x, 3
52 define i32 @sdiv5(i32 %x) {
53 ; CHECK-LABEL: @sdiv5(
54 ; CHECK-NEXT: [[X_FR:%.*]] = freeze i32 [[X:%.*]]
55 ; CHECK-NEXT: [[TMP1:%.*]] = srem i32 [[X_FR]], 3
56 ; CHECK-NEXT: [[Z:%.*]] = sub i32 [[TMP1]], [[X_FR]]
57 ; CHECK-NEXT: ret i32 [[Z]]
64 define i32 @sdiv6(i32 %x) {
65 ; CHECK-LABEL: @sdiv6(
66 ; CHECK-NEXT: [[Z:%.*]] = sub i32 0, [[X:%.*]]
67 ; CHECK-NEXT: ret i32 [[Z]]
69 %y = sdiv exact i32 %x, 3
74 define i32 @udiv1(i32 %x, i32 %w) {
75 ; CHECK-LABEL: @udiv1(
76 ; CHECK-NEXT: ret i32 [[X:%.*]]
78 %y = udiv exact i32 %x, %w
83 define i32 @udiv2(i32 %x, i32 %w) {
84 ; CHECK-LABEL: @udiv2(
85 ; CHECK-NEXT: [[Z1:%.*]] = lshr exact i32 [[X:%.*]], [[W:%.*]]
86 ; CHECK-NEXT: ret i32 [[Z1]]
89 %z = udiv exact i32 %x, %y
93 define i64 @ashr1(i64 %X) {
94 ; CHECK-LABEL: @ashr1(
95 ; CHECK-NEXT: [[A:%.*]] = shl i64 [[X:%.*]], 8
96 ; CHECK-NEXT: [[B:%.*]] = ashr exact i64 [[A]], 2
97 ; CHECK-NEXT: ret i64 [[B]]
104 ; The vector ashr should be exact (like it is in the preceding test).
106 define <2 x i64> @ashr1_vec(<2 x i64> %X) {
107 ; CHECK-LABEL: @ashr1_vec(
108 ; CHECK-NEXT: [[A:%.*]] = shl <2 x i64> [[X:%.*]], splat (i64 8)
109 ; CHECK-NEXT: [[B:%.*]] = ashr exact <2 x i64> [[A]], splat (i64 2)
110 ; CHECK-NEXT: ret <2 x i64> [[B]]
112 %A = shl <2 x i64> %X, <i64 8, i64 8>
113 %B = ashr <2 x i64> %A, <i64 2, i64 2>
118 define i1 @ashr_icmp1(i64 %X) {
119 ; CHECK-LABEL: @ashr_icmp1(
120 ; CHECK-NEXT: [[B:%.*]] = icmp eq i64 [[X:%.*]], 0
121 ; CHECK-NEXT: ret i1 [[B]]
123 %A = ashr exact i64 %X, 2 ; X/4
124 %B = icmp eq i64 %A, 0
128 define i1 @ashr_icmp2(i64 %X) {
129 ; CHECK-LABEL: @ashr_icmp2(
130 ; CHECK-NEXT: [[Z:%.*]] = icmp slt i64 [[X:%.*]], 16
131 ; CHECK-NEXT: ret i1 [[Z]]
133 %Y = ashr exact i64 %X, 2 ; x / 4
134 %Z = icmp slt i64 %Y, 4 ; x < 16
138 define <2 x i1> @ashr_icmp2_vec(<2 x i64> %X) {
139 ; CHECK-LABEL: @ashr_icmp2_vec(
140 ; CHECK-NEXT: [[Z:%.*]] = icmp slt <2 x i64> [[X:%.*]], splat (i64 16)
141 ; CHECK-NEXT: ret <2 x i1> [[Z]]
143 %Y = ashr exact <2 x i64> %X, <i64 2, i64 2>
144 %Z = icmp slt <2 x i64> %Y, <i64 4, i64 4>
149 ; Make sure we don't transform the ashr here into an sdiv
150 define i1 @pr9998(i32 %V) {
151 ; CHECK-LABEL: @pr9998(
152 ; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[V:%.*]], 1
153 ; CHECK-NEXT: [[Z:%.*]] = icmp ne i32 [[TMP1]], 0
154 ; CHECK-NEXT: ret i1 [[Z]]
157 %X = ashr exact i32 %W, 31
158 %Y = sext i32 %X to i64
159 %Z = icmp ugt i64 %Y, 7297771788697658747
163 ; FIXME: Vectors should fold the same way.
164 define <2 x i1> @pr9998vec(<2 x i32> %V) {
165 ; CHECK-LABEL: @pr9998vec(
166 ; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i32> [[V:%.*]], splat (i32 1)
167 ; CHECK-NEXT: [[X:%.*]] = sub nsw <2 x i32> zeroinitializer, [[TMP1]]
168 ; CHECK-NEXT: [[Y:%.*]] = sext <2 x i32> [[X]] to <2 x i64>
169 ; CHECK-NEXT: [[Z:%.*]] = icmp ugt <2 x i64> [[Y]], splat (i64 7297771788697658747)
170 ; CHECK-NEXT: ret <2 x i1> [[Z]]
172 %W = shl <2 x i32> %V, <i32 31, i32 31>
173 %X = ashr exact <2 x i32> %W, <i32 31, i32 31>
174 %Y = sext <2 x i32> %X to <2 x i64>
175 %Z = icmp ugt <2 x i64> %Y, <i64 7297771788697658747, i64 7297771788697658747>
179 define i1 @udiv_icmp1(i64 %X) {
180 ; CHECK-LABEL: @udiv_icmp1(
181 ; CHECK-NEXT: [[B:%.*]] = icmp ne i64 [[X:%.*]], 0
182 ; CHECK-NEXT: ret i1 [[B]]
184 %A = udiv exact i64 %X, 5 ; X/5
185 %B = icmp ne i64 %A, 0
189 define <2 x i1> @udiv_icmp1_vec(<2 x i64> %X) {
190 ; CHECK-LABEL: @udiv_icmp1_vec(
191 ; CHECK-NEXT: [[B:%.*]] = icmp ne <2 x i64> [[X:%.*]], zeroinitializer
192 ; CHECK-NEXT: ret <2 x i1> [[B]]
194 %A = udiv exact <2 x i64> %X, <i64 5, i64 5>
195 %B = icmp ne <2 x i64> %A, zeroinitializer
199 define i1 @udiv_icmp2(i64 %X) {
200 ; CHECK-LABEL: @udiv_icmp2(
201 ; CHECK-NEXT: [[B:%.*]] = icmp eq i64 [[X:%.*]], 0
202 ; CHECK-NEXT: ret i1 [[B]]
204 %A = udiv exact i64 %X, 5 ; X/5 == 0 --> x == 0
205 %B = icmp eq i64 %A, 0
209 define <2 x i1> @udiv_icmp2_vec(<2 x i64> %X) {
210 ; CHECK-LABEL: @udiv_icmp2_vec(
211 ; CHECK-NEXT: [[B:%.*]] = icmp eq <2 x i64> [[X:%.*]], zeroinitializer
212 ; CHECK-NEXT: ret <2 x i1> [[B]]
214 %A = udiv exact <2 x i64> %X, <i64 5, i64 5>
215 %B = icmp eq <2 x i64> %A, zeroinitializer
219 define i1 @sdiv_icmp1(i64 %X) {
220 ; CHECK-LABEL: @sdiv_icmp1(
221 ; CHECK-NEXT: [[B:%.*]] = icmp eq i64 [[X:%.*]], 0
222 ; CHECK-NEXT: ret i1 [[B]]
224 %A = sdiv exact i64 %X, 5 ; X/5 == 0 --> x == 0
225 %B = icmp eq i64 %A, 0
229 define <2 x i1> @sdiv_icmp1_vec(<2 x i64> %X) {
230 ; CHECK-LABEL: @sdiv_icmp1_vec(
231 ; CHECK-NEXT: [[B:%.*]] = icmp eq <2 x i64> [[X:%.*]], zeroinitializer
232 ; CHECK-NEXT: ret <2 x i1> [[B]]
234 %A = sdiv exact <2 x i64> %X, <i64 5, i64 5>
235 %B = icmp eq <2 x i64> %A, zeroinitializer
239 define i1 @sdiv_icmp2(i64 %X) {
240 ; CHECK-LABEL: @sdiv_icmp2(
241 ; CHECK-NEXT: [[B:%.*]] = icmp eq i64 [[X:%.*]], 5
242 ; CHECK-NEXT: ret i1 [[B]]
244 %A = sdiv exact i64 %X, 5 ; X/5 == 1 --> x == 5
245 %B = icmp eq i64 %A, 1
249 define <2 x i1> @sdiv_icmp2_vec(<2 x i64> %X) {
250 ; CHECK-LABEL: @sdiv_icmp2_vec(
251 ; CHECK-NEXT: [[B:%.*]] = icmp eq <2 x i64> [[X:%.*]], splat (i64 5)
252 ; CHECK-NEXT: ret <2 x i1> [[B]]
254 %A = sdiv exact <2 x i64> %X, <i64 5, i64 5>
255 %B = icmp eq <2 x i64> %A, <i64 1, i64 1>
259 define i1 @sdiv_icmp3(i64 %X) {
260 ; CHECK-LABEL: @sdiv_icmp3(
261 ; CHECK-NEXT: [[B:%.*]] = icmp eq i64 [[X:%.*]], -5
262 ; CHECK-NEXT: ret i1 [[B]]
264 %A = sdiv exact i64 %X, 5 ; X/5 == -1 --> x == -5
265 %B = icmp eq i64 %A, -1
269 define <2 x i1> @sdiv_icmp3_vec(<2 x i64> %X) {
270 ; CHECK-LABEL: @sdiv_icmp3_vec(
271 ; CHECK-NEXT: [[B:%.*]] = icmp eq <2 x i64> [[X:%.*]], splat (i64 -5)
272 ; CHECK-NEXT: ret <2 x i1> [[B]]
274 %A = sdiv exact <2 x i64> %X, <i64 5, i64 5>
275 %B = icmp eq <2 x i64> %A, <i64 -1, i64 -1>
279 define i1 @sdiv_icmp4(i64 %X) {
280 ; CHECK-LABEL: @sdiv_icmp4(
281 ; CHECK-NEXT: [[B:%.*]] = icmp eq i64 [[X:%.*]], 0
282 ; CHECK-NEXT: ret i1 [[B]]
284 %A = sdiv exact i64 %X, -5 ; X/-5 == 0 --> x == 0
285 %B = icmp eq i64 %A, 0
289 define <2 x i1> @sdiv_icmp4_vec(<2 x i64> %X) {
290 ; CHECK-LABEL: @sdiv_icmp4_vec(
291 ; CHECK-NEXT: [[B:%.*]] = icmp eq <2 x i64> [[X:%.*]], zeroinitializer
292 ; CHECK-NEXT: ret <2 x i1> [[B]]
294 %A = sdiv exact <2 x i64> %X, <i64 -5, i64 -5>
295 %B = icmp eq <2 x i64> %A, zeroinitializer
299 define i1 @sdiv_icmp5(i64 %X) {
300 ; CHECK-LABEL: @sdiv_icmp5(
301 ; CHECK-NEXT: [[B:%.*]] = icmp eq i64 [[X:%.*]], -5
302 ; CHECK-NEXT: ret i1 [[B]]
304 %A = sdiv exact i64 %X, -5 ; X/-5 == 1 --> x == -5
305 %B = icmp eq i64 %A, 1
309 define <2 x i1> @sdiv_icmp5_vec(<2 x i64> %X) {
310 ; CHECK-LABEL: @sdiv_icmp5_vec(
311 ; CHECK-NEXT: [[B:%.*]] = icmp eq <2 x i64> [[X:%.*]], splat (i64 -5)
312 ; CHECK-NEXT: ret <2 x i1> [[B]]
314 %A = sdiv exact <2 x i64> %X, <i64 -5, i64 -5>
315 %B = icmp eq <2 x i64> %A, <i64 1, i64 1>
319 define i1 @sdiv_icmp6(i64 %X) {
320 ; CHECK-LABEL: @sdiv_icmp6(
321 ; CHECK-NEXT: [[B:%.*]] = icmp eq i64 [[X:%.*]], 5
322 ; CHECK-NEXT: ret i1 [[B]]
324 %A = sdiv exact i64 %X, -5 ; X/-5 == -1 --> x == 5
325 %B = icmp eq i64 %A, -1
329 define <2 x i1> @sdiv_icmp6_vec(<2 x i64> %X) {
330 ; CHECK-LABEL: @sdiv_icmp6_vec(
331 ; CHECK-NEXT: [[B:%.*]] = icmp eq <2 x i64> [[X:%.*]], splat (i64 5)
332 ; CHECK-NEXT: ret <2 x i1> [[B]]
334 %A = sdiv exact <2 x i64> %X, <i64 -5, i64 -5>
335 %B = icmp eq <2 x i64> %A, <i64 -1, i64 -1>
339 define i8 @mul_of_udiv(i8 %x) {
340 ; CHECK-LABEL: @mul_of_udiv(
341 ; CHECK-NEXT: [[MUL1:%.*]] = lshr exact i8 [[X:%.*]], 1
342 ; CHECK-NEXT: ret i8 [[MUL1]]
344 %div = udiv exact i8 %x, 12
345 %mul = mul i8 %div, 6
349 define i8 @mul_of_sdiv(i8 %x) {
350 ; CHECK-LABEL: @mul_of_sdiv(
351 ; CHECK-NEXT: [[MUL_NEG:%.*]] = ashr exact i8 [[X:%.*]], 1
352 ; CHECK-NEXT: [[MUL:%.*]] = sub nsw i8 0, [[MUL_NEG]]
353 ; CHECK-NEXT: ret i8 [[MUL]]
355 %div = sdiv exact i8 %x, 12
356 %mul = mul i8 %div, -6
360 define <2 x i8> @mul_of_sdiv_non_splat(<2 x i8> %x) {
361 ; CHECK-LABEL: @mul_of_sdiv_non_splat(
362 ; CHECK-NEXT: [[MUL:%.*]] = sdiv exact <2 x i8> [[X:%.*]], <i8 1, i8 -2>
363 ; CHECK-NEXT: ret <2 x i8> [[MUL]]
365 %div = sdiv exact <2 x i8> %x, <i8 6, i8 -12>
366 %mul = mul <2 x i8> %div, <i8 6, i8 6>
370 define i8 @mul_of_sdiv_fail_missing_exact(i8 %x) {
371 ; CHECK-LABEL: @mul_of_sdiv_fail_missing_exact(
372 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i8 [[X:%.*]], 12
373 ; CHECK-NEXT: [[MUL:%.*]] = mul i8 [[DIV]], -6
374 ; CHECK-NEXT: ret i8 [[MUL]]
376 %div = sdiv i8 %x, 12
377 %mul = mul i8 %div, -6
381 define i8 @mul_of_udiv_fail_bad_remainder(i8 %x) {
382 ; CHECK-LABEL: @mul_of_udiv_fail_bad_remainder(
383 ; CHECK-NEXT: [[DIV:%.*]] = udiv exact i8 [[X:%.*]], 11
384 ; CHECK-NEXT: [[MUL:%.*]] = mul nuw i8 [[DIV]], 6
385 ; CHECK-NEXT: ret i8 [[MUL]]
387 %div = udiv exact i8 %x, 11
388 %mul = mul i8 %div, 6
392 define i8 @mul_of_sdiv_fail_ub(i8 %x) {
393 ; CHECK-LABEL: @mul_of_sdiv_fail_ub(
394 ; CHECK-NEXT: [[MUL:%.*]] = sub i8 0, [[X:%.*]]
395 ; CHECK-NEXT: ret i8 [[MUL]]
397 %div = sdiv exact i8 %x, 6
398 %mul = mul i8 %div, -6
402 define <2 x i8> @mul_of_sdiv_fail_ub_non_splat(<2 x i8> %x) {
403 ; CHECK-LABEL: @mul_of_sdiv_fail_ub_non_splat(
404 ; CHECK-NEXT: [[DIV:%.*]] = sdiv exact <2 x i8> [[X:%.*]], <i8 -6, i8 -12>
405 ; CHECK-NEXT: [[MUL:%.*]] = mul <2 x i8> [[DIV]], splat (i8 6)
406 ; CHECK-NEXT: ret <2 x i8> [[MUL]]
408 %div = sdiv exact <2 x i8> %x, <i8 -6, i8 -12>
409 %mul = mul <2 x i8> %div, <i8 6, i8 6>