Skip to content

Commit d7dcc05

Browse files
committed
[InstCombine] Add test coverage for (a % b) lt/le/ge/gt (b(-/+)1) (NFC).
1 parent 4b3ea33 commit d7dcc05

File tree

1 file changed

+349
-0
lines changed
  • llvm/test/Transforms/InstCombine

1 file changed

+349
-0
lines changed

llvm/test/Transforms/InstCombine/icmp.ll

Lines changed: 349 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,311 @@ declare void @use_i8(i8)
1010
declare void @use_i32(i32)
1111
declare void @use_i64(i64)
1212

13+
; tests for (x % c) >=/ < (c - 1), where c >= 0
14+
define i1 @srem_sgt_test1(i64 %x) {
15+
; CHECK-LABEL: @srem_sgt_test1(
16+
; CHECK-NEXT: [[Y:%.*]] = srem i64 [[X:%.*]], 34360750831
17+
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i64 [[Y]], 34360750829
18+
; CHECK-NEXT: ret i1 [[CMP]]
19+
;
20+
%y = srem i64 %x, 34360750831
21+
%cmp = icmp sgt i64 %y, 34360750829
22+
ret i1 %cmp
23+
}
24+
25+
define i1 @srem_slt_test1(i64 %x) {
26+
; CHECK-LABEL: @srem_slt_test1(
27+
; CHECK-NEXT: [[Y:%.*]] = srem i64 [[X:%.*]], 34360750831
28+
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i64 [[Y]], 34360750830
29+
; CHECK-NEXT: ret i1 [[CMP]]
30+
;
31+
%y = srem i64 %x, 34360750831
32+
%cmp = icmp slt i64 %y, 34360750830
33+
ret i1 %cmp
34+
}
35+
36+
define i1 @srem_sgt_test2(i32 %x) {
37+
; CHECK-LABEL: @srem_sgt_test2(
38+
; CHECK-NEXT: [[Y:%.*]] = srem i32 [[X:%.*]], 1074977277
39+
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[Y]], 1074977275
40+
; CHECK-NEXT: ret i1 [[CMP]]
41+
;
42+
%y = srem i32 %x, 1074977277
43+
%cmp = icmp sgt i32 %y, 1074977275
44+
ret i1 %cmp
45+
}
46+
47+
define i1 @srem_slt_test2(i32 %x) {
48+
; CHECK-LABEL: @srem_slt_test2(
49+
; CHECK-NEXT: [[Y:%.*]] = srem i32 [[X:%.*]], 1074977277
50+
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[Y]], 1074977276
51+
; CHECK-NEXT: ret i1 [[CMP]]
52+
;
53+
%y = srem i32 %x, 1074977277
54+
%cmp = icmp slt i32 %y, 1074977276
55+
ret i1 %cmp
56+
}
57+
58+
define i1 @srem_sgt_test3(i16 %x) {
59+
; CHECK-LABEL: @srem_sgt_test3(
60+
; CHECK-NEXT: [[Y:%.*]] = srem i16 [[X:%.*]], 2259
61+
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i16 [[Y]], 2257
62+
; CHECK-NEXT: ret i1 [[CMP]]
63+
;
64+
%y = srem i16 %x, 2259
65+
%cmp = icmp sgt i16 %y, 2257
66+
ret i1 %cmp
67+
}
68+
69+
define i1 @srem_slt_test3(i16 %x) {
70+
; CHECK-LABEL: @srem_slt_test3(
71+
; CHECK-NEXT: [[Y:%.*]] = srem i16 [[X:%.*]], 2259
72+
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i16 [[Y]], 2258
73+
; CHECK-NEXT: ret i1 [[CMP]]
74+
;
75+
%y = srem i16 %x, 2259
76+
%cmp = icmp slt i16 %y, 2258
77+
ret i1 %cmp
78+
}
79+
80+
define i1 @srem_sgt_test4(i8 %x) {
81+
; CHECK-LABEL: @srem_sgt_test4(
82+
; CHECK-NEXT: [[Y:%.*]] = srem i8 [[X:%.*]], 50
83+
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[Y]], 48
84+
; CHECK-NEXT: ret i1 [[CMP]]
85+
;
86+
%y = srem i8 %x, 50
87+
%cmp = icmp sgt i8 %y, 48
88+
ret i1 %cmp
89+
}
90+
91+
define i1 @srem_slt_test4(i8 %x) {
92+
; CHECK-LABEL: @srem_slt_test4(
93+
; CHECK-NEXT: [[Y:%.*]] = srem i8 [[X:%.*]], 50
94+
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[Y]], 49
95+
; CHECK-NEXT: ret i1 [[CMP]]
96+
;
97+
%y = srem i8 %x, 50
98+
%cmp = icmp slt i8 %y, 49
99+
ret i1 %cmp
100+
}
101+
102+
define i1 @test_srem_slt_constant(i32 %a) {
103+
; CHECK-LABEL: @test_srem_slt_constant(
104+
; CHECK-NEXT: [[Y:%.*]] = srem i32 [[A:%.*]], 512
105+
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[Y]], 511
106+
; CHECK-NEXT: ret i1 [[CMP]]
107+
;
108+
%y = srem i32 %a, 512
109+
%cmp = icmp slt i32 %y, 511
110+
ret i1 %cmp
111+
}
112+
113+
define i1 @test_srem_sgt_constant(i32 %a) {
114+
; CHECK-LABEL: @test_srem_sgt_constant(
115+
; CHECK-NEXT: [[Y:%.*]] = srem i32 [[A:%.*]], 512
116+
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[Y]], 510
117+
; CHECK-NEXT: ret i1 [[CMP]]
118+
;
119+
%y = srem i32 %a, 512
120+
%cmp = icmp sgt i32 %y, 510
121+
ret i1 %cmp
122+
}
123+
124+
define <2 x i1> @test_srem_slt_constant_splat(<2 x i32> %a) {
125+
; CHECK-LABEL: @test_srem_slt_constant_splat(
126+
; CHECK-NEXT: [[Y:%.*]] = srem <2 x i32> [[A:%.*]], <i32 512, i32 512>
127+
; CHECK-NEXT: [[CMP:%.*]] = icmp slt <2 x i32> [[Y]], <i32 511, i32 511>
128+
; CHECK-NEXT: ret <2 x i1> [[CMP]]
129+
;
130+
%y = srem <2 x i32> %a, <i32 512, i32 512>
131+
%cmp = icmp slt <2 x i32> %y, <i32 511, i32 511>
132+
ret <2 x i1> %cmp
133+
}
134+
135+
define <2 x i1> @test_srem_sgt_constant_splat(<2 x i32> %a) {
136+
; CHECK-LABEL: @test_srem_sgt_constant_splat(
137+
; CHECK-NEXT: [[Y:%.*]] = srem <2 x i32> [[A:%.*]], <i32 512, i32 512>
138+
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <2 x i32> [[Y]], <i32 510, i32 510>
139+
; CHECK-NEXT: ret <2 x i1> [[CMP]]
140+
;
141+
%y = srem <2 x i32> %a, <i32 512, i32 512>
142+
%cmp = icmp sgt <2 x i32> %y, <i32 510, i32 510>
143+
ret <2 x i1> %cmp
144+
}
145+
146+
; tests for (x % c) <=/> (c + 1), where y < 0
147+
define i1 @srem_sgt_test(i16 %x) {
148+
; CHECK-LABEL: @srem_sgt_test(
149+
; CHECK-NEXT: [[Y:%.*]] = srem i16 [[X:%.*]], 2259
150+
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i16 [[Y]], -2258
151+
; CHECK-NEXT: ret i1 [[CMP]]
152+
;
153+
%y = srem i16 %x, -2259
154+
%cmp = icmp sgt i16 %y, -2258
155+
ret i1 %cmp
156+
}
157+
158+
define i1 @srem_sle_test(i16 %x) {
159+
; CHECK-LABEL: @srem_sle_test(
160+
; CHECK-NEXT: [[Y:%.*]] = srem i16 [[X:%.*]], 2259
161+
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i16 [[Y]], -2257
162+
; CHECK-NEXT: ret i1 [[CMP]]
163+
;
164+
%y = srem i16 %x, -2259
165+
%cmp = icmp slt i16 %y, -2257
166+
ret i1 %cmp
167+
}
168+
169+
define <2 x i1> @test_srem_sgt_constant_splat_neg(<2 x i32> %a) {
170+
; CHECK-LABEL: @test_srem_sgt_constant_splat_neg(
171+
; CHECK-NEXT: [[Y:%.*]] = srem <2 x i32> [[A:%.*]], <i32 512, i32 512>
172+
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <2 x i32> [[Y]], <i32 -511, i32 -511>
173+
; CHECK-NEXT: ret <2 x i1> [[CMP]]
174+
;
175+
%y = srem <2 x i32> %a, <i32 -512, i32 -512>
176+
%cmp = icmp sgt <2 x i32> %y, <i32 -511, i32 -511>
177+
ret <2 x i1> %cmp
178+
}
179+
180+
define <2 x i1> @test_srem_slt_constant_splat_neg(<2 x i32> %a) {
181+
; CHECK-LABEL: @test_srem_slt_constant_splat_neg(
182+
; CHECK-NEXT: [[Y:%.*]] = srem <2 x i32> [[A:%.*]], <i32 512, i32 512>
183+
; CHECK-NEXT: [[CMP:%.*]] = icmp slt <2 x i32> [[Y]], <i32 -510, i32 -510>
184+
; CHECK-NEXT: ret <2 x i1> [[CMP]]
185+
;
186+
%y = srem <2 x i32> %a, <i32 -512, i32 -512>
187+
%cmp = icmp slt <2 x i32> %y, <i32 -510, i32 -510>
188+
ret <2 x i1> %cmp
189+
}
190+
191+
; tests for handling urem w/ slt/sge/uge/ult
192+
define i1 @test_urem_slt(i32 %x) {
193+
; CHECK-LABEL: @test_urem_slt(
194+
; CHECK-NEXT: [[Y:%.*]] = urem i32 [[X:%.*]], 12235
195+
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[Y]], 12234
196+
; CHECK-NEXT: ret i1 [[CMP]]
197+
;
198+
%y = urem i32 %x, 12235
199+
%cmp = icmp slt i32 %y, 12234
200+
ret i1 %cmp
201+
}
202+
203+
define i1 @test_urem_sge(i32 %x) {
204+
; CHECK-LABEL: @test_urem_sge(
205+
; CHECK-NEXT: [[Y:%.*]] = urem i32 [[X:%.*]], 13546
206+
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32 [[Y]], 13544
207+
; CHECK-NEXT: ret i1 [[CMP]]
208+
;
209+
%y = urem i32 %x, 13546
210+
%cmp = icmp sge i32 %y, 13545
211+
ret i1 %cmp
212+
}
213+
214+
define i1 @test_urem_uge(i32 %x) {
215+
; CHECK-LABEL: @test_urem_uge(
216+
; CHECK-NEXT: [[Y:%.*]] = urem i32 [[X:%.*]], 18642
217+
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32 [[Y]], 18640
218+
; CHECK-NEXT: ret i1 [[CMP]]
219+
;
220+
%y = urem i32 %x, 18642
221+
%cmp = icmp ugt i32 %y, 18640
222+
ret i1 %cmp
223+
}
224+
225+
define i1 @test_urem_ult(i32 %x) {
226+
; CHECK-LABEL: @test_urem_ult(
227+
; CHECK-NEXT: [[Y:%.*]] = urem i32 [[X:%.*]], 15344
228+
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[Y]], 15343
229+
; CHECK-NEXT: ret i1 [[CMP]]
230+
;
231+
%y = urem i32 %x, 15344
232+
%cmp = icmp ult i32 %y, 15343
233+
ret i1 %cmp
234+
}
235+
236+
define <2 x i1> @test_urem_slt_constant_splat(<2 x i32> %a) {
237+
; CHECK-LABEL: @test_urem_slt_constant_splat(
238+
; CHECK-NEXT: [[Y:%.*]] = srem <2 x i32> [[A:%.*]], <i32 512, i32 512>
239+
; CHECK-NEXT: [[CMP:%.*]] = icmp slt <2 x i32> [[Y]], <i32 511, i32 511>
240+
; CHECK-NEXT: ret <2 x i1> [[CMP]]
241+
;
242+
%y = srem <2 x i32> %a, <i32 512, i32 512>
243+
%cmp = icmp slt <2 x i32> %y, <i32 511, i32 511>
244+
ret <2 x i1> %cmp
245+
}
246+
247+
define <2 x i1> @test_urem_sgt_constant_splat(<2 x i32> %a) {
248+
; CHECK-LABEL: @test_urem_sgt_constant_splat(
249+
; CHECK-NEXT: [[Y:%.*]] = srem <2 x i32> [[A:%.*]], <i32 512, i32 512>
250+
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <2 x i32> [[Y]], <i32 510, i32 510>
251+
; CHECK-NEXT: ret <2 x i1> [[CMP]]
252+
;
253+
%y = srem <2 x i32> %a, <i32 512, i32 512>
254+
%cmp = icmp sgt <2 x i32> %y, <i32 510, i32 510>
255+
ret <2 x i1> %cmp
256+
}
257+
258+
define <2 x i1> @test_urem_ugt_constant_splat(<2 x i32> %a) {
259+
; CHECK-LABEL: @test_urem_ugt_constant_splat(
260+
; CHECK-NEXT: [[Y:%.*]] = srem <2 x i32> [[A:%.*]], <i32 512, i32 512>
261+
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt <2 x i32> [[Y]], <i32 510, i32 510>
262+
; CHECK-NEXT: ret <2 x i1> [[CMP]]
263+
;
264+
%y = srem <2 x i32> %a, <i32 512, i32 512>
265+
%cmp = icmp ugt <2 x i32> %y, <i32 510, i32 510>
266+
ret <2 x i1> %cmp
267+
}
268+
269+
define <2 x i1> @test_urem_ult_constant_splat(<2 x i32> %a) {
270+
; CHECK-LABEL: @test_urem_ult_constant_splat(
271+
; CHECK-NEXT: [[Y:%.*]] = srem <2 x i32> [[A:%.*]], <i32 512, i32 512>
272+
; CHECK-NEXT: [[CMP:%.*]] = icmp ult <2 x i32> [[Y]], <i32 511, i32 511>
273+
; CHECK-NEXT: ret <2 x i1> [[CMP]]
274+
;
275+
%y = srem <2 x i32> %a, <i32 512, i32 512>
276+
%cmp = icmp ult <2 x i32> %y, <i32 511, i32 511>
277+
ret <2 x i1> %cmp
278+
}
279+
280+
; negative tests
281+
define i1 @srem_slt_neg_test(i8 %x, i8 %C) {
282+
; CHECK-LABEL: @srem_slt_neg_test(
283+
; CHECK-NEXT: [[CMINUS1:%.*]] = add i8 [[C:%.*]], -1
284+
; CHECK-NEXT: [[Y:%.*]] = srem i8 [[X:%.*]], [[C]]
285+
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[Y]], [[CMINUS1]]
286+
; CHECK-NEXT: ret i1 [[CMP]]
287+
;
288+
%Cminus1 = add i8 %C, -1
289+
%y = srem i8 %x, %C
290+
%cmp = icmp slt i8 %y, %Cminus1
291+
ret i1 %cmp
292+
}
293+
294+
define i1 @srem_sge_neg_test(i8 %x, i8 %C) {
295+
; CHECK-LABEL: @srem_sge_neg_test(
296+
; CHECK-NEXT: [[CMINUS1:%.*]] = add i8 [[C:%.*]], -1
297+
; CHECK-NEXT: [[Y:%.*]] = srem i8 [[X:%.*]], [[C]]
298+
; CHECK-NEXT: [[CMP:%.*]] = icmp sge i8 [[Y]], [[CMINUS1]]
299+
; CHECK-NEXT: ret i1 [[CMP]]
300+
;
301+
%Cminus1 = add i8 %C, -1
302+
%y = srem i8 %x, %C
303+
%cmp = icmp sge i8 %y, %Cminus1
304+
ret i1 %cmp
305+
}
306+
307+
; negative tests for urem
308+
; if C <= 1 in ugt
309+
define i1 @test_neg_ult_1(i32 %x) {
310+
; CHECK-LABEL: @test_neg_ult_1(
311+
; CHECK-NEXT: ret i1 true
312+
;
313+
%y = urem i32 %x, 1
314+
%cmp = icmp ult i32 %y, -1
315+
ret i1 %cmp
316+
}
317+
13318
define i32 @test1(i32 %X) {
14319
; CHECK-LABEL: @test1(
15320
; CHECK-NEXT: [[X_LOBIT:%.*]] = lshr i32 [[X:%.*]], 31
@@ -20,6 +325,50 @@ define i32 @test1(i32 %X) {
20325
ret i32 %b
21326
}
22327

328+
; negative tests off by one
329+
define i1 @test_urem_ult_neg(i32 %x) {
330+
; CHECK-LABEL: @test_urem_ult_neg(
331+
; CHECK-NEXT: [[Y:%.*]] = urem i32 [[X:%.*]], 15344
332+
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[Y]], 15342
333+
; CHECK-NEXT: ret i1 [[CMP]]
334+
;
335+
%y = urem i32 %x, 15344
336+
%cmp = icmp ult i32 %y, 15342
337+
ret i1 %cmp
338+
}
339+
340+
define i1 @srem_slt_neg(i64 %x) {
341+
; CHECK-LABEL: @srem_slt_neg(
342+
; CHECK-NEXT: [[Y:%.*]] = srem i64 [[X:%.*]], 34360750831
343+
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i64 [[Y]], 34360750829
344+
; CHECK-NEXT: ret i1 [[CMP]]
345+
;
346+
%y = srem i64 %x, 34360750831
347+
%cmp = icmp slt i64 %y, 34360750829
348+
ret i1 %cmp
349+
}
350+
351+
define i1 @srem_sgt_neg(i32 %x) {
352+
; CHECK-LABEL: @srem_sgt_neg(
353+
; CHECK-NEXT: [[Y:%.*]] = srem i32 [[X:%.*]], 1074977277
354+
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[Y]], 1074977274
355+
; CHECK-NEXT: ret i1 [[CMP]]
356+
;
357+
%y = srem i32 %x, 1074977277
358+
%cmp = icmp sgt i32 %y, 1074977274
359+
ret i1 %cmp
360+
}
361+
362+
363+
define i1 @srem_sle_neg_neg(i16 %x) {
364+
; CHECK-LABEL: @srem_sle_neg_neg(
365+
; CHECK-NEXT: ret i1 false
366+
;
367+
%y = srem i16 %x, -2259
368+
%cmp = icmp slt i16 %y, -2258
369+
ret i1 %cmp
370+
}
371+
23372
define <2 x i32> @test1vec(<2 x i32> %X) {
24373
; CHECK-LABEL: @test1vec(
25374
; CHECK-NEXT: [[X_LOBIT:%.*]] = lshr <2 x i32> [[X:%.*]], <i32 31, i32 31>

0 commit comments

Comments
 (0)