2004-11-27-SetCCForCastLargerAndConstant.ll
12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -instcombine -S | FileCheck %s
; This tests the InstructionCombining optimization that reduces things like:
; %Y = sext i8 %X to i32
; %C = icmp ult i32 %Y, 1024
; to
; %C = i1 true
; It includes test cases for different constant values, signedness of the
; cast operands, and types of setCC operators. In all cases, the cast should
; be eliminated. In many cases the setCC is also eliminated based on the
; constant value and the range of the casted value.
;
define i1 @lt_signed_to_large_unsigned(i8 %SB) {
; CHECK-LABEL: @lt_signed_to_large_unsigned(
; CHECK-NEXT: [[C:%.*]] = icmp sgt i8 [[SB:%.*]], -1
; CHECK-NEXT: ret i1 [[C]]
;
%Y = sext i8 %SB to i32
%C = icmp ult i32 %Y, 1024
ret i1 %C
}
; PR28011 - https://llvm.org/bugs/show_bug.cgi?id=28011
; The above transform only applies to scalar integers; it shouldn't be attempted for constant expressions or vectors.
@a = common global i32** null
@b = common global [1 x i32] zeroinitializer
define i1 @PR28011(i16 %a) {
; CHECK-LABEL: @PR28011(
; CHECK-NEXT: [[CONV:%.*]] = sext i16 [[A:%.*]] to i32
; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[CONV]], or (i32 zext (i1 icmp ne (i32*** bitcast ([1 x i32]* @b to i32***), i32*** @a) to i32), i32 1)
; CHECK-NEXT: ret i1 [[CMP]]
;
%conv = sext i16 %a to i32
%cmp = icmp ne i32 %conv, or (i32 zext (i1 icmp ne (i32*** bitcast ([1 x i32]* @b to i32***), i32*** @a) to i32), i32 1)
ret i1 %cmp
}
define <2 x i1> @lt_signed_to_large_unsigned_vec(<2 x i8> %SB) {
; CHECK-LABEL: @lt_signed_to_large_unsigned_vec(
; CHECK-NEXT: [[Y:%.*]] = sext <2 x i8> [[SB:%.*]] to <2 x i32>
; CHECK-NEXT: [[C:%.*]] = icmp ult <2 x i32> [[Y]], <i32 1024, i32 2>
; CHECK-NEXT: ret <2 x i1> [[C]]
;
%Y = sext <2 x i8> %SB to <2 x i32>
%C = icmp ult <2 x i32> %Y, <i32 1024, i32 2>
ret <2 x i1> %C
}
define i1 @lt_signed_to_large_signed(i8 %SB) {
; CHECK-LABEL: @lt_signed_to_large_signed(
; CHECK-NEXT: ret i1 true
;
%Y = sext i8 %SB to i32
%C = icmp slt i32 %Y, 1024
ret i1 %C
}
define i1 @lt_signed_to_large_negative(i8 %SB) {
; CHECK-LABEL: @lt_signed_to_large_negative(
; CHECK-NEXT: ret i1 false
;
%Y = sext i8 %SB to i32
%C = icmp slt i32 %Y, -1024
ret i1 %C
}
define i1 @lt_signed_to_small_unsigned(i8 %SB) {
; CHECK-LABEL: @lt_signed_to_small_unsigned(
; CHECK-NEXT: [[C:%.*]] = icmp ult i8 [[SB:%.*]], 17
; CHECK-NEXT: ret i1 [[C]]
;
%Y = sext i8 %SB to i32
%C = icmp ult i32 %Y, 17
ret i1 %C
}
define i1 @lt_signed_to_small_signed(i8 %SB) {
; CHECK-LABEL: @lt_signed_to_small_signed(
; CHECK-NEXT: [[C:%.*]] = icmp slt i8 [[SB:%.*]], 17
; CHECK-NEXT: ret i1 [[C]]
;
%Y = sext i8 %SB to i32
%C = icmp slt i32 %Y, 17
ret i1 %C
}
define i1 @lt_signed_to_small_negative(i8 %SB) {
; CHECK-LABEL: @lt_signed_to_small_negative(
; CHECK-NEXT: [[C:%.*]] = icmp slt i8 [[SB:%.*]], -17
; CHECK-NEXT: ret i1 [[C]]
;
%Y = sext i8 %SB to i32
%C = icmp slt i32 %Y, -17
ret i1 %C
}
define i1 @lt_unsigned_to_large_unsigned(i8 %SB) {
; CHECK-LABEL: @lt_unsigned_to_large_unsigned(
; CHECK-NEXT: ret i1 true
;
%Y = zext i8 %SB to i32
%C = icmp ult i32 %Y, 1024
ret i1 %C
}
define i1 @lt_unsigned_to_large_signed(i8 %SB) {
; CHECK-LABEL: @lt_unsigned_to_large_signed(
; CHECK-NEXT: ret i1 true
;
%Y = zext i8 %SB to i32
%C = icmp slt i32 %Y, 1024
ret i1 %C
}
define i1 @lt_unsigned_to_large_negative(i8 %SB) {
; CHECK-LABEL: @lt_unsigned_to_large_negative(
; CHECK-NEXT: ret i1 false
;
%Y = zext i8 %SB to i32
%C = icmp slt i32 %Y, -1024
ret i1 %C
}
define i1 @lt_unsigned_to_small_unsigned(i8 %SB) {
; CHECK-LABEL: @lt_unsigned_to_small_unsigned(
; CHECK-NEXT: [[C:%.*]] = icmp ult i8 [[SB:%.*]], 17
; CHECK-NEXT: ret i1 [[C]]
;
%Y = zext i8 %SB to i32
%C = icmp ult i32 %Y, 17
ret i1 %C
}
define i1 @lt_unsigned_to_small_signed(i8 %SB) {
; CHECK-LABEL: @lt_unsigned_to_small_signed(
; CHECK-NEXT: [[C:%.*]] = icmp ult i8 [[SB:%.*]], 17
; CHECK-NEXT: ret i1 [[C]]
;
%Y = zext i8 %SB to i32
%C = icmp slt i32 %Y, 17
ret i1 %C
}
define i1 @lt_unsigned_to_small_negative(i8 %SB) {
; CHECK-LABEL: @lt_unsigned_to_small_negative(
; CHECK-NEXT: ret i1 false
;
%Y = zext i8 %SB to i32
%C = icmp slt i32 %Y, -17
ret i1 %C
}
define i1 @gt_signed_to_large_unsigned(i8 %SB) {
; CHECK-LABEL: @gt_signed_to_large_unsigned(
; CHECK-NEXT: [[C:%.*]] = icmp slt i8 [[SB:%.*]], 0
; CHECK-NEXT: ret i1 [[C]]
;
%Y = sext i8 %SB to i32
%C = icmp ugt i32 %Y, 1024
ret i1 %C
}
define i1 @gt_signed_to_large_signed(i8 %SB) {
; CHECK-LABEL: @gt_signed_to_large_signed(
; CHECK-NEXT: ret i1 false
;
%Y = sext i8 %SB to i32
%C = icmp sgt i32 %Y, 1024
ret i1 %C
}
define i1 @gt_signed_to_large_negative(i8 %SB) {
; CHECK-LABEL: @gt_signed_to_large_negative(
; CHECK-NEXT: ret i1 true
;
%Y = sext i8 %SB to i32
%C = icmp sgt i32 %Y, -1024
ret i1 %C
}
define i1 @gt_signed_to_small_unsigned(i8 %SB) {
; CHECK-LABEL: @gt_signed_to_small_unsigned(
; CHECK-NEXT: [[C:%.*]] = icmp ugt i8 [[SB:%.*]], 17
; CHECK-NEXT: ret i1 [[C]]
;
%Y = sext i8 %SB to i32
%C = icmp ugt i32 %Y, 17
ret i1 %C
}
define i1 @gt_signed_to_small_signed(i8 %SB) {
; CHECK-LABEL: @gt_signed_to_small_signed(
; CHECK-NEXT: [[C:%.*]] = icmp sgt i8 [[SB:%.*]], 17
; CHECK-NEXT: ret i1 [[C]]
;
%Y = sext i8 %SB to i32
%C = icmp sgt i32 %Y, 17
ret i1 %C
}
define i1 @gt_signed_to_small_negative(i8 %SB) {
; CHECK-LABEL: @gt_signed_to_small_negative(
; CHECK-NEXT: [[C:%.*]] = icmp sgt i8 [[SB:%.*]], -17
; CHECK-NEXT: ret i1 [[C]]
;
%Y = sext i8 %SB to i32
%C = icmp sgt i32 %Y, -17
ret i1 %C
}
define i1 @gt_unsigned_to_large_unsigned(i8 %SB) {
; CHECK-LABEL: @gt_unsigned_to_large_unsigned(
; CHECK-NEXT: ret i1 false
;
%Y = zext i8 %SB to i32
%C = icmp ugt i32 %Y, 1024
ret i1 %C
}
define i1 @gt_unsigned_to_large_signed(i8 %SB) {
; CHECK-LABEL: @gt_unsigned_to_large_signed(
; CHECK-NEXT: ret i1 false
;
%Y = zext i8 %SB to i32
%C = icmp sgt i32 %Y, 1024
ret i1 %C
}
define i1 @gt_unsigned_to_large_negative(i8 %SB) {
; CHECK-LABEL: @gt_unsigned_to_large_negative(
; CHECK-NEXT: ret i1 true
;
%Y = zext i8 %SB to i32
%C = icmp sgt i32 %Y, -1024
ret i1 %C
}
define i1 @gt_unsigned_to_small_unsigned(i8 %SB) {
; CHECK-LABEL: @gt_unsigned_to_small_unsigned(
; CHECK-NEXT: [[C:%.*]] = icmp ugt i8 [[SB:%.*]], 17
; CHECK-NEXT: ret i1 [[C]]
;
%Y = zext i8 %SB to i32
%C = icmp ugt i32 %Y, 17
ret i1 %C
}
define i1 @gt_unsigned_to_small_signed(i8 %SB) {
; CHECK-LABEL: @gt_unsigned_to_small_signed(
; CHECK-NEXT: [[C:%.*]] = icmp ugt i8 [[SB:%.*]], 17
; CHECK-NEXT: ret i1 [[C]]
;
%Y = zext i8 %SB to i32
%C = icmp sgt i32 %Y, 17
ret i1 %C
}
define i1 @gt_unsigned_to_small_negative(i8 %SB) {
; CHECK-LABEL: @gt_unsigned_to_small_negative(
; CHECK-NEXT: ret i1 true
;
%Y = zext i8 %SB to i32
%C = icmp sgt i32 %Y, -17
ret i1 %C
}
define i1 @different_size_zext_zext_ugt(i7 %x, i4 %y) {
; CHECK-LABEL: @different_size_zext_zext_ugt(
; CHECK-NEXT: [[TMP1:%.*]] = zext i4 [[Y:%.*]] to i7
; CHECK-NEXT: [[R:%.*]] = icmp ult i7 [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret i1 [[R]]
;
%zx = zext i7 %x to i25
%zy = zext i4 %y to i25
%r = icmp ugt i25 %zx, %zy
ret i1 %r
}
define <2 x i1> @different_size_zext_zext_ugt_commute(<2 x i4> %x, <2 x i7> %y) {
; CHECK-LABEL: @different_size_zext_zext_ugt_commute(
; CHECK-NEXT: [[TMP1:%.*]] = zext <2 x i4> [[X:%.*]] to <2 x i7>
; CHECK-NEXT: [[R:%.*]] = icmp ugt <2 x i7> [[TMP1]], [[Y:%.*]]
; CHECK-NEXT: ret <2 x i1> [[R]]
;
%zx = zext <2 x i4> %x to <2 x i25>
%zy = zext <2 x i7> %y to <2 x i25>
%r = icmp ugt <2 x i25> %zx, %zy
ret <2 x i1> %r
}
define i1 @different_size_zext_zext_ult(i4 %x, i7 %y) {
; CHECK-LABEL: @different_size_zext_zext_ult(
; CHECK-NEXT: [[TMP1:%.*]] = zext i4 [[X:%.*]] to i7
; CHECK-NEXT: [[R:%.*]] = icmp ult i7 [[TMP1]], [[Y:%.*]]
; CHECK-NEXT: ret i1 [[R]]
;
%zx = zext i4 %x to i25
%zy = zext i7 %y to i25
%r = icmp ult i25 %zx, %zy
ret i1 %r
}
define i1 @different_size_zext_zext_eq(i4 %x, i7 %y) {
; CHECK-LABEL: @different_size_zext_zext_eq(
; CHECK-NEXT: [[TMP1:%.*]] = zext i4 [[X:%.*]] to i7
; CHECK-NEXT: [[R:%.*]] = icmp eq i7 [[TMP1]], [[Y:%.*]]
; CHECK-NEXT: ret i1 [[R]]
;
%zx = zext i4 %x to i25
%zy = zext i7 %y to i25
%r = icmp eq i25 %zx, %zy
ret i1 %r
}
define i1 @different_size_zext_zext_ne_commute(i7 %x, i4 %y) {
; CHECK-LABEL: @different_size_zext_zext_ne_commute(
; CHECK-NEXT: [[TMP1:%.*]] = zext i4 [[Y:%.*]] to i7
; CHECK-NEXT: [[R:%.*]] = icmp ne i7 [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret i1 [[R]]
;
%zx = zext i7 %x to i25
%zy = zext i4 %y to i25
%r = icmp ne i25 %zx, %zy
ret i1 %r
}
define i1 @different_size_zext_zext_slt(i7 %x, i4 %y) {
; CHECK-LABEL: @different_size_zext_zext_slt(
; CHECK-NEXT: [[TMP1:%.*]] = zext i4 [[Y:%.*]] to i7
; CHECK-NEXT: [[R:%.*]] = icmp ugt i7 [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret i1 [[R]]
;
%zx = zext i7 %x to i25
%zy = zext i4 %y to i25
%r = icmp slt i25 %zx, %zy
ret i1 %r
}
define i1 @different_size_zext_zext_sgt(i7 %x, i4 %y) {
; CHECK-LABEL: @different_size_zext_zext_sgt(
; CHECK-NEXT: [[TMP1:%.*]] = zext i4 [[Y:%.*]] to i7
; CHECK-NEXT: [[R:%.*]] = icmp ult i7 [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret i1 [[R]]
;
%zx = zext i7 %x to i25
%zy = zext i4 %y to i25
%r = icmp sgt i25 %zx, %zy
ret i1 %r
}
define i1 @different_size_sext_sext_sgt(i7 %x, i4 %y) {
; CHECK-LABEL: @different_size_sext_sext_sgt(
; CHECK-NEXT: [[TMP1:%.*]] = sext i4 [[Y:%.*]] to i7
; CHECK-NEXT: [[R:%.*]] = icmp slt i7 [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret i1 [[R]]
;
%sx = sext i7 %x to i25
%sy = sext i4 %y to i25
%r = icmp sgt i25 %sx, %sy
ret i1 %r
}
define i1 @different_size_sext_sext_sle(i7 %x, i4 %y) {
; CHECK-LABEL: @different_size_sext_sext_sle(
; CHECK-NEXT: [[TMP1:%.*]] = sext i4 [[Y:%.*]] to i7
; CHECK-NEXT: [[R:%.*]] = icmp sge i7 [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret i1 [[R]]
;
%sx = sext i7 %x to i25
%sy = sext i4 %y to i25
%r = icmp sle i25 %sx, %sy
ret i1 %r
}
define i1 @different_size_sext_sext_eq(i7 %x, i4 %y) {
; CHECK-LABEL: @different_size_sext_sext_eq(
; CHECK-NEXT: [[TMP1:%.*]] = sext i4 [[Y:%.*]] to i7
; CHECK-NEXT: [[R:%.*]] = icmp eq i7 [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret i1 [[R]]
;
%sx = sext i7 %x to i25
%sy = sext i4 %y to i25
%r = icmp eq i25 %sx, %sy
ret i1 %r
}
define i1 @different_size_sext_sext_ule(i7 %x, i4 %y) {
; CHECK-LABEL: @different_size_sext_sext_ule(
; CHECK-NEXT: [[TMP1:%.*]] = sext i4 [[Y:%.*]] to i7
; CHECK-NEXT: [[R:%.*]] = icmp uge i7 [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret i1 [[R]]
;
%sx = sext i7 %x to i25
%sy = sext i4 %y to i25
%r = icmp ule i25 %sx, %sy
ret i1 %r
}
; TODO: This can be reduced.
define i1 @different_size_sext_zext_ne(i7 %x, i4 %y) {
; CHECK-LABEL: @different_size_sext_zext_ne(
; CHECK-NEXT: [[SX:%.*]] = sext i7 [[X:%.*]] to i25
; CHECK-NEXT: [[ZY:%.*]] = zext i4 [[Y:%.*]] to i25
; CHECK-NEXT: [[R:%.*]] = icmp ne i25 [[SX]], [[ZY]]
; CHECK-NEXT: ret i1 [[R]]
;
%sx = sext i7 %x to i25
%zy = zext i4 %y to i25
%r = icmp ne i25 %sx, %zy
ret i1 %r
}
declare void @use(i25)
define i1 @different_size_sext_sext_ule_extra_use1(i7 %x, i4 %y) {
; CHECK-LABEL: @different_size_sext_sext_ule_extra_use1(
; CHECK-NEXT: [[SY:%.*]] = sext i4 [[Y:%.*]] to i25
; CHECK-NEXT: call void @use(i25 [[SY]])
; CHECK-NEXT: [[TMP1:%.*]] = sext i4 [[Y]] to i7
; CHECK-NEXT: [[R:%.*]] = icmp uge i7 [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret i1 [[R]]
;
%sx = sext i7 %x to i25
%sy = sext i4 %y to i25
call void @use(i25 %sy)
%r = icmp ule i25 %sx, %sy
ret i1 %r
}
define i1 @different_size_sext_sext_ule_extra_use2(i7 %x, i4 %y) {
; CHECK-LABEL: @different_size_sext_sext_ule_extra_use2(
; CHECK-NEXT: [[SX:%.*]] = sext i7 [[X:%.*]] to i25
; CHECK-NEXT: call void @use(i25 [[SX]])
; CHECK-NEXT: [[TMP1:%.*]] = sext i4 [[Y:%.*]] to i7
; CHECK-NEXT: [[R:%.*]] = icmp uge i7 [[TMP1]], [[X]]
; CHECK-NEXT: ret i1 [[R]]
;
%sx = sext i7 %x to i25
call void @use(i25 %sx)
%sy = sext i4 %y to i25
%r = icmp ule i25 %sx, %sy
ret i1 %r
}
; Negative test - extra uses on both casts is too much.
define i1 @different_size_sext_sext_ule_extra_use3(i7 %x, i4 %y) {
; CHECK-LABEL: @different_size_sext_sext_ule_extra_use3(
; CHECK-NEXT: [[SX:%.*]] = sext i7 [[X:%.*]] to i25
; CHECK-NEXT: call void @use(i25 [[SX]])
; CHECK-NEXT: [[SY:%.*]] = sext i4 [[Y:%.*]] to i25
; CHECK-NEXT: call void @use(i25 [[SY]])
; CHECK-NEXT: [[R:%.*]] = icmp ule i25 [[SX]], [[SY]]
; CHECK-NEXT: ret i1 [[R]]
;
%sx = sext i7 %x to i25
call void @use(i25 %sx)
%sy = sext i4 %y to i25
call void @use(i25 %sy)
%r = icmp ule i25 %sx, %sy
ret i1 %r
}