variable-signext-of-variable-high-bit-extraction.ll
29.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -instcombine -S | FileCheck %s
declare void @use16(i16)
declare void @use32(i32)
declare void @use64(i64)
define i32 @t0(i64 %data, i32 %nbits) {
; CHECK-LABEL: @t0(
; CHECK-NEXT: [[SKIP_HIGH:%.*]] = sub i32 64, [[NBITS:%.*]]
; CHECK-NEXT: call void @use32(i32 [[SKIP_HIGH]])
; CHECK-NEXT: [[SKIP_HIGH_WIDE:%.*]] = zext i32 [[SKIP_HIGH]] to i64
; CHECK-NEXT: call void @use64(i64 [[SKIP_HIGH_WIDE]])
; CHECK-NEXT: [[EXTRACTED:%.*]] = lshr i64 [[DATA:%.*]], [[SKIP_HIGH_WIDE]]
; CHECK-NEXT: call void @use64(i64 [[EXTRACTED]])
; CHECK-NEXT: [[EXTRACTED_NARROW:%.*]] = trunc i64 [[EXTRACTED]] to i32
; CHECK-NEXT: call void @use32(i32 [[EXTRACTED_NARROW]])
; CHECK-NEXT: [[NUM_HIGH_BITS_TO_SMEAR_NARROW:%.*]] = sub i32 32, [[NBITS]]
; CHECK-NEXT: call void @use32(i32 [[NUM_HIGH_BITS_TO_SMEAR_NARROW]])
; CHECK-NEXT: [[TMP1:%.*]] = ashr i64 [[DATA]], [[SKIP_HIGH_WIDE]]
; CHECK-NEXT: [[SIGNEXTENDED:%.*]] = trunc i64 [[TMP1]] to i32
; CHECK-NEXT: ret i32 [[SIGNEXTENDED]]
;
%skip_high = sub i32 64, %nbits
call void @use32(i32 %skip_high)
%skip_high_wide = zext i32 %skip_high to i64
call void @use64(i64 %skip_high_wide)
%extracted = lshr i64 %data, %skip_high_wide
call void @use64(i64 %extracted)
%extracted_narrow = trunc i64 %extracted to i32
call void @use32(i32 %extracted_narrow)
%num_high_bits_to_smear_narrow = sub i32 32, %nbits
call void @use32(i32 %num_high_bits_to_smear_narrow)
%signbit_positioned = shl i32 %extracted_narrow, %num_high_bits_to_smear_narrow
%signextended = ashr i32 %signbit_positioned, %num_high_bits_to_smear_narrow
ret i32 %signextended
}
define i32 @t0_zext_of_nbits(i64 %data, i8 %nbits_narrow) {
; CHECK-LABEL: @t0_zext_of_nbits(
; CHECK-NEXT: [[NBITS:%.*]] = zext i8 [[NBITS_NARROW:%.*]] to i16
; CHECK-NEXT: call void @use16(i16 [[NBITS]])
; CHECK-NEXT: [[SKIP_HIGH:%.*]] = sub nsw i16 64, [[NBITS]]
; CHECK-NEXT: call void @use16(i16 [[SKIP_HIGH]])
; CHECK-NEXT: [[SKIP_HIGH_WIDE:%.*]] = zext i16 [[SKIP_HIGH]] to i64
; CHECK-NEXT: call void @use64(i64 [[SKIP_HIGH_WIDE]])
; CHECK-NEXT: [[EXTRACTED:%.*]] = lshr i64 [[DATA:%.*]], [[SKIP_HIGH_WIDE]]
; CHECK-NEXT: call void @use64(i64 [[EXTRACTED]])
; CHECK-NEXT: [[EXTRACTED_NARROW:%.*]] = trunc i64 [[EXTRACTED]] to i32
; CHECK-NEXT: call void @use32(i32 [[EXTRACTED_NARROW]])
; CHECK-NEXT: [[NUM_HIGH_BITS_TO_SMEAR_NARROW_NARROW:%.*]] = sub nsw i16 32, [[NBITS]]
; CHECK-NEXT: call void @use16(i16 [[NUM_HIGH_BITS_TO_SMEAR_NARROW_NARROW]])
; CHECK-NEXT: [[NUM_HIGH_BITS_TO_SMEAR_NARROW:%.*]] = zext i16 [[NUM_HIGH_BITS_TO_SMEAR_NARROW_NARROW]] to i32
; CHECK-NEXT: call void @use32(i32 [[NUM_HIGH_BITS_TO_SMEAR_NARROW]])
; CHECK-NEXT: [[TMP1:%.*]] = ashr i64 [[DATA]], [[SKIP_HIGH_WIDE]]
; CHECK-NEXT: [[SIGNEXTENDED:%.*]] = trunc i64 [[TMP1]] to i32
; CHECK-NEXT: ret i32 [[SIGNEXTENDED]]
;
%nbits = zext i8 %nbits_narrow to i16
call void @use16(i16 %nbits)
%skip_high = sub i16 64, %nbits
call void @use16(i16 %skip_high)
%skip_high_wide = zext i16 %skip_high to i64
call void @use64(i64 %skip_high_wide)
%extracted = lshr i64 %data, %skip_high_wide
call void @use64(i64 %extracted)
%extracted_narrow = trunc i64 %extracted to i32
call void @use32(i32 %extracted_narrow)
%num_high_bits_to_smear_narrow_narrow = sub i16 32, %nbits
call void @use16(i16 %num_high_bits_to_smear_narrow_narrow)
%num_high_bits_to_smear_narrow = zext i16 %num_high_bits_to_smear_narrow_narrow to i32
call void @use32(i32 %num_high_bits_to_smear_narrow)
%signbit_positioned = shl i32 %extracted_narrow, %num_high_bits_to_smear_narrow
%signextended = ashr i32 %signbit_positioned, %num_high_bits_to_smear_narrow
ret i32 %signextended
}
define i32 @t0_exact(i64 %data, i32 %nbits) {
; CHECK-LABEL: @t0_exact(
; CHECK-NEXT: [[SKIP_HIGH:%.*]] = sub i32 64, [[NBITS:%.*]]
; CHECK-NEXT: call void @use32(i32 [[SKIP_HIGH]])
; CHECK-NEXT: [[SKIP_HIGH_WIDE:%.*]] = zext i32 [[SKIP_HIGH]] to i64
; CHECK-NEXT: call void @use64(i64 [[SKIP_HIGH_WIDE]])
; CHECK-NEXT: [[EXTRACTED:%.*]] = lshr exact i64 [[DATA:%.*]], [[SKIP_HIGH_WIDE]]
; CHECK-NEXT: call void @use64(i64 [[EXTRACTED]])
; CHECK-NEXT: [[EXTRACTED_NARROW:%.*]] = trunc i64 [[EXTRACTED]] to i32
; CHECK-NEXT: call void @use32(i32 [[EXTRACTED_NARROW]])
; CHECK-NEXT: [[NUM_HIGH_BITS_TO_SMEAR_NARROW:%.*]] = sub i32 32, [[NBITS]]
; CHECK-NEXT: call void @use32(i32 [[NUM_HIGH_BITS_TO_SMEAR_NARROW]])
; CHECK-NEXT: [[TMP1:%.*]] = ashr exact i64 [[DATA]], [[SKIP_HIGH_WIDE]]
; CHECK-NEXT: [[SIGNEXTENDED:%.*]] = trunc i64 [[TMP1]] to i32
; CHECK-NEXT: ret i32 [[SIGNEXTENDED]]
;
%skip_high = sub i32 64, %nbits
call void @use32(i32 %skip_high)
%skip_high_wide = zext i32 %skip_high to i64
call void @use64(i64 %skip_high_wide)
%extracted = lshr exact i64 %data, %skip_high_wide ; We can preserve `exact`-ness of the original shift.
call void @use64(i64 %extracted)
%extracted_narrow = trunc i64 %extracted to i32
call void @use32(i32 %extracted_narrow)
%num_high_bits_to_smear_narrow = sub i32 32, %nbits
call void @use32(i32 %num_high_bits_to_smear_narrow)
%signbit_positioned = shl i32 %extracted_narrow, %num_high_bits_to_smear_narrow
%signextended = ashr i32 %signbit_positioned, %num_high_bits_to_smear_narrow
ret i32 %signextended
}
define i32 @t1_redundant_sext(i64 %data, i32 %nbits) {
; CHECK-LABEL: @t1_redundant_sext(
; CHECK-NEXT: [[SKIP_HIGH:%.*]] = sub i32 64, [[NBITS:%.*]]
; CHECK-NEXT: call void @use32(i32 [[SKIP_HIGH]])
; CHECK-NEXT: [[SKIP_HIGH_WIDE:%.*]] = zext i32 [[SKIP_HIGH]] to i64
; CHECK-NEXT: call void @use64(i64 [[SKIP_HIGH_WIDE]])
; CHECK-NEXT: [[EXTRACTED_WITH_SIGNEXTENSION:%.*]] = ashr i64 [[DATA:%.*]], [[SKIP_HIGH_WIDE]]
; CHECK-NEXT: call void @use64(i64 [[EXTRACTED_WITH_SIGNEXTENSION]])
; CHECK-NEXT: [[EXTRACTED_WITH_SIGNEXTENSION_NARROW:%.*]] = trunc i64 [[EXTRACTED_WITH_SIGNEXTENSION]] to i32
; CHECK-NEXT: call void @use32(i32 [[EXTRACTED_WITH_SIGNEXTENSION_NARROW]])
; CHECK-NEXT: [[NUM_HIGH_BITS_TO_SMEAR_NARROW:%.*]] = sub i32 32, [[NBITS]]
; CHECK-NEXT: call void @use32(i32 [[NUM_HIGH_BITS_TO_SMEAR_NARROW]])
; CHECK-NEXT: [[SIGNBIT_POSITIONED:%.*]] = shl i32 [[EXTRACTED_WITH_SIGNEXTENSION_NARROW]], [[NUM_HIGH_BITS_TO_SMEAR_NARROW]]
; CHECK-NEXT: call void @use32(i32 [[SIGNBIT_POSITIONED]])
; CHECK-NEXT: ret i32 [[EXTRACTED_WITH_SIGNEXTENSION_NARROW]]
;
%skip_high = sub i32 64, %nbits
call void @use32(i32 %skip_high)
%skip_high_wide = zext i32 %skip_high to i64
call void @use64(i64 %skip_high_wide)
%extracted_with_signextension = ashr i64 %data, %skip_high_wide
call void @use64(i64 %extracted_with_signextension)
%extracted_with_signextension_narrow = trunc i64 %extracted_with_signextension to i32 ; this is already the answer.
call void @use32(i32 %extracted_with_signextension_narrow)
%num_high_bits_to_smear_narrow = sub i32 32, %nbits
call void @use32(i32 %num_high_bits_to_smear_narrow)
%signbit_positioned = shl i32 %extracted_with_signextension_narrow, %num_high_bits_to_smear_narrow
call void @use32(i32 %signbit_positioned)
%signextended = ashr i32 %signbit_positioned, %num_high_bits_to_smear_narrow
ret i32 %signextended
}
define i64 @t2_notrunc(i64 %data, i64 %nbits) {
; CHECK-LABEL: @t2_notrunc(
; CHECK-NEXT: [[SKIP_HIGH:%.*]] = sub i64 64, [[NBITS:%.*]]
; CHECK-NEXT: call void @use64(i64 [[SKIP_HIGH]])
; CHECK-NEXT: [[EXTRACTED:%.*]] = lshr i64 [[DATA:%.*]], [[SKIP_HIGH]]
; CHECK-NEXT: call void @use64(i64 [[EXTRACTED]])
; CHECK-NEXT: [[NUM_HIGH_BITS_TO_SMEAR:%.*]] = sub i64 64, [[NBITS]]
; CHECK-NEXT: call void @use64(i64 [[NUM_HIGH_BITS_TO_SMEAR]])
; CHECK-NEXT: [[SIGNBIT_POSITIONED:%.*]] = shl i64 [[EXTRACTED]], [[NUM_HIGH_BITS_TO_SMEAR]]
; CHECK-NEXT: call void @use64(i64 [[SIGNBIT_POSITIONED]])
; CHECK-NEXT: [[SIGNEXTENDED:%.*]] = ashr i64 [[DATA]], [[SKIP_HIGH]]
; CHECK-NEXT: ret i64 [[SIGNEXTENDED]]
;
%skip_high = sub i64 64, %nbits
call void @use64(i64 %skip_high)
%extracted = lshr i64 %data, %skip_high
call void @use64(i64 %extracted)
%num_high_bits_to_smear = sub i64 64, %nbits
call void @use64(i64 %num_high_bits_to_smear)
%signbit_positioned = shl i64 %extracted, %num_high_bits_to_smear ;
call void @use64(i64 %signbit_positioned)
%signextended = ashr i64 %signbit_positioned, %num_high_bits_to_smear ; can just shift %data itself.
ret i64 %signextended
}
define i64 @t3_notrunc_redundant_sext(i64 %data, i64 %nbits) {
; CHECK-LABEL: @t3_notrunc_redundant_sext(
; CHECK-NEXT: [[SKIP_HIGH:%.*]] = sub i64 64, [[NBITS:%.*]]
; CHECK-NEXT: call void @use64(i64 [[SKIP_HIGH]])
; CHECK-NEXT: [[EXTRACTED:%.*]] = ashr i64 [[DATA:%.*]], [[SKIP_HIGH]]
; CHECK-NEXT: call void @use64(i64 [[EXTRACTED]])
; CHECK-NEXT: [[NUM_HIGH_BITS_TO_SMEAR:%.*]] = sub i64 64, [[NBITS]]
; CHECK-NEXT: call void @use64(i64 [[NUM_HIGH_BITS_TO_SMEAR]])
; CHECK-NEXT: [[SIGNBIT_POSITIONED:%.*]] = shl i64 [[EXTRACTED]], [[NUM_HIGH_BITS_TO_SMEAR]]
; CHECK-NEXT: call void @use64(i64 [[SIGNBIT_POSITIONED]])
; CHECK-NEXT: ret i64 [[EXTRACTED]]
;
%skip_high = sub i64 64, %nbits
call void @use64(i64 %skip_high)
%extracted = ashr i64 %data, %skip_high ; this is already the answer.
call void @use64(i64 %extracted)
%num_high_bits_to_smear = sub i64 64, %nbits
call void @use64(i64 %num_high_bits_to_smear)
%signbit_positioned = shl i64 %extracted, %num_high_bits_to_smear
call void @use64(i64 %signbit_positioned)
%signextended = ashr i64 %signbit_positioned, %num_high_bits_to_smear
ret i64 %signextended
}
define <2 x i32> @t4_vec(<2 x i64> %data, <2 x i32> %nbits) {
; CHECK-LABEL: @t4_vec(
; CHECK-NEXT: [[SKIP_HIGH:%.*]] = sub <2 x i32> <i32 64, i32 64>, [[NBITS:%.*]]
; CHECK-NEXT: [[SKIP_HIGH_WIDE:%.*]] = zext <2 x i32> [[SKIP_HIGH]] to <2 x i64>
; CHECK-NEXT: [[TMP1:%.*]] = ashr <2 x i64> [[DATA:%.*]], [[SKIP_HIGH_WIDE]]
; CHECK-NEXT: [[SIGNEXTENDED:%.*]] = trunc <2 x i64> [[TMP1]] to <2 x i32>
; CHECK-NEXT: ret <2 x i32> [[SIGNEXTENDED]]
;
%skip_high = sub <2 x i32> <i32 64, i32 64>, %nbits
%skip_high_wide = zext <2 x i32> %skip_high to <2 x i64>
%extracted = lshr <2 x i64> %data, %skip_high_wide
%extracted_narrow = trunc <2 x i64> %extracted to <2 x i32>
%num_high_bits_to_smear_narrow = sub <2 x i32> <i32 32, i32 32>, %nbits
%signbit_positioned = shl <2 x i32> %extracted_narrow, %num_high_bits_to_smear_narrow
%signextended = ashr <2 x i32> %signbit_positioned, %num_high_bits_to_smear_narrow
ret <2 x i32> %signextended
}
define <3 x i32> @t5_vec_undef(<3 x i64> %data, <3 x i32> %nbits) {
; CHECK-LABEL: @t5_vec_undef(
; CHECK-NEXT: [[SKIP_HIGH:%.*]] = sub <3 x i32> <i32 64, i32 64, i32 undef>, [[NBITS:%.*]]
; CHECK-NEXT: [[SKIP_HIGH_WIDE:%.*]] = zext <3 x i32> [[SKIP_HIGH]] to <3 x i64>
; CHECK-NEXT: [[TMP1:%.*]] = ashr <3 x i64> [[DATA:%.*]], [[SKIP_HIGH_WIDE]]
; CHECK-NEXT: [[SIGNEXTENDED:%.*]] = trunc <3 x i64> [[TMP1]] to <3 x i32>
; CHECK-NEXT: ret <3 x i32> [[SIGNEXTENDED]]
;
%skip_high = sub <3 x i32> <i32 64, i32 64, i32 undef>, %nbits
%skip_high_wide = zext <3 x i32> %skip_high to <3 x i64>
%extracted = lshr <3 x i64> %data, %skip_high_wide
%extracted_narrow = trunc <3 x i64> %extracted to <3 x i32>
%num_high_bits_to_smear_narrow0 = sub <3 x i32> <i32 32, i32 32, i32 undef>, %nbits
%num_high_bits_to_smear_narrow1 = sub <3 x i32> <i32 undef, i32 32, i32 32>, %nbits
%signbit_positioned = shl <3 x i32> %extracted_narrow, %num_high_bits_to_smear_narrow0
%signextended = ashr <3 x i32> %signbit_positioned, %num_high_bits_to_smear_narrow1
ret <3 x i32> %signextended
}
; Extra-uses
define i32 @t6_extrause_good0(i64 %data, i32 %nbits) {
; CHECK-LABEL: @t6_extrause_good0(
; CHECK-NEXT: [[SKIP_HIGH:%.*]] = sub i32 64, [[NBITS:%.*]]
; CHECK-NEXT: call void @use32(i32 [[SKIP_HIGH]])
; CHECK-NEXT: [[SKIP_HIGH_WIDE:%.*]] = zext i32 [[SKIP_HIGH]] to i64
; CHECK-NEXT: call void @use64(i64 [[SKIP_HIGH_WIDE]])
; CHECK-NEXT: [[EXTRACTED:%.*]] = lshr i64 [[DATA:%.*]], [[SKIP_HIGH_WIDE]]
; CHECK-NEXT: call void @use64(i64 [[EXTRACTED]])
; CHECK-NEXT: [[EXTRACTED_NARROW:%.*]] = trunc i64 [[EXTRACTED]] to i32
; CHECK-NEXT: call void @use32(i32 [[EXTRACTED_NARROW]])
; CHECK-NEXT: [[NUM_HIGH_BITS_TO_SMEAR_NARROW:%.*]] = sub i32 32, [[NBITS]]
; CHECK-NEXT: call void @use32(i32 [[NUM_HIGH_BITS_TO_SMEAR_NARROW]])
; CHECK-NEXT: [[TMP1:%.*]] = ashr i64 [[DATA]], [[SKIP_HIGH_WIDE]]
; CHECK-NEXT: [[SIGNEXTENDED:%.*]] = trunc i64 [[TMP1]] to i32
; CHECK-NEXT: ret i32 [[SIGNEXTENDED]]
;
%skip_high = sub i32 64, %nbits
call void @use32(i32 %skip_high)
%skip_high_wide = zext i32 %skip_high to i64
call void @use64(i64 %skip_high_wide)
%extracted = lshr i64 %data, %skip_high_wide
call void @use64(i64 %extracted)
%extracted_narrow = trunc i64 %extracted to i32
call void @use32(i32 %extracted_narrow)
%num_high_bits_to_smear_narrow = sub i32 32, %nbits
call void @use32(i32 %num_high_bits_to_smear_narrow)
%signbit_positioned = shl i32 %extracted_narrow, %num_high_bits_to_smear_narrow ; will go away
%signextended = ashr i32 %signbit_positioned, %num_high_bits_to_smear_narrow
ret i32 %signextended
}
define i32 @t7_extrause_good1(i64 %data, i32 %nbits) {
; CHECK-LABEL: @t7_extrause_good1(
; CHECK-NEXT: [[SKIP_HIGH:%.*]] = sub i32 64, [[NBITS:%.*]]
; CHECK-NEXT: call void @use32(i32 [[SKIP_HIGH]])
; CHECK-NEXT: [[SKIP_HIGH_WIDE:%.*]] = zext i32 [[SKIP_HIGH]] to i64
; CHECK-NEXT: call void @use64(i64 [[SKIP_HIGH_WIDE]])
; CHECK-NEXT: [[EXTRACTED:%.*]] = lshr i64 [[DATA:%.*]], [[SKIP_HIGH_WIDE]]
; CHECK-NEXT: call void @use64(i64 [[EXTRACTED]])
; CHECK-NEXT: [[EXTRACTED_NARROW:%.*]] = trunc i64 [[EXTRACTED]] to i32
; CHECK-NEXT: call void @use32(i32 [[EXTRACTED_NARROW]])
; CHECK-NEXT: [[NUM_HIGH_BITS_TO_SMEAR_NARROW0:%.*]] = sub i32 32, [[NBITS]]
; CHECK-NEXT: call void @use32(i32 [[NUM_HIGH_BITS_TO_SMEAR_NARROW0]])
; CHECK-NEXT: [[SIGNBIT_POSITIONED:%.*]] = shl i32 [[EXTRACTED_NARROW]], [[NUM_HIGH_BITS_TO_SMEAR_NARROW0]]
; CHECK-NEXT: call void @use32(i32 [[SIGNBIT_POSITIONED]])
; CHECK-NEXT: [[TMP1:%.*]] = ashr i64 [[DATA]], [[SKIP_HIGH_WIDE]]
; CHECK-NEXT: [[SIGNEXTENDED:%.*]] = trunc i64 [[TMP1]] to i32
; CHECK-NEXT: ret i32 [[SIGNEXTENDED]]
;
%skip_high = sub i32 64, %nbits
call void @use32(i32 %skip_high)
%skip_high_wide = zext i32 %skip_high to i64
call void @use64(i64 %skip_high_wide)
%extracted = lshr i64 %data, %skip_high_wide
call void @use64(i64 %extracted)
%extracted_narrow = trunc i64 %extracted to i32
call void @use32(i32 %extracted_narrow)
%num_high_bits_to_smear_narrow0 = sub i32 32, %nbits
call void @use32(i32 %num_high_bits_to_smear_narrow0)
%num_high_bits_to_smear_narrow1 = sub i32 32, %nbits ; will go away.
%signbit_positioned = shl i32 %extracted_narrow, %num_high_bits_to_smear_narrow0
call void @use32(i32 %signbit_positioned)
%signextended = ashr i32 %signbit_positioned, %num_high_bits_to_smear_narrow1
ret i32 %signextended
}
define i32 @n8_extrause_bad(i64 %data, i32 %nbits) {
; CHECK-LABEL: @n8_extrause_bad(
; CHECK-NEXT: [[SKIP_HIGH:%.*]] = sub i32 64, [[NBITS:%.*]]
; CHECK-NEXT: call void @use32(i32 [[SKIP_HIGH]])
; CHECK-NEXT: [[SKIP_HIGH_WIDE:%.*]] = zext i32 [[SKIP_HIGH]] to i64
; CHECK-NEXT: call void @use64(i64 [[SKIP_HIGH_WIDE]])
; CHECK-NEXT: [[EXTRACTED:%.*]] = lshr i64 [[DATA:%.*]], [[SKIP_HIGH_WIDE]]
; CHECK-NEXT: call void @use64(i64 [[EXTRACTED]])
; CHECK-NEXT: [[EXTRACTED_NARROW:%.*]] = trunc i64 [[EXTRACTED]] to i32
; CHECK-NEXT: call void @use32(i32 [[EXTRACTED_NARROW]])
; CHECK-NEXT: [[NUM_HIGH_BITS_TO_SMEAR_NARROW:%.*]] = sub i32 32, [[NBITS]]
; CHECK-NEXT: call void @use32(i32 [[NUM_HIGH_BITS_TO_SMEAR_NARROW]])
; CHECK-NEXT: [[SIGNBIT_POSITIONED:%.*]] = shl i32 [[EXTRACTED_NARROW]], [[NUM_HIGH_BITS_TO_SMEAR_NARROW]]
; CHECK-NEXT: call void @use32(i32 [[SIGNBIT_POSITIONED]])
; CHECK-NEXT: [[SIGNEXTENDED:%.*]] = ashr i32 [[SIGNBIT_POSITIONED]], [[NUM_HIGH_BITS_TO_SMEAR_NARROW]]
; CHECK-NEXT: ret i32 [[SIGNEXTENDED]]
;
%skip_high = sub i32 64, %nbits
call void @use32(i32 %skip_high)
%skip_high_wide = zext i32 %skip_high to i64
call void @use64(i64 %skip_high_wide)
%extracted = lshr i64 %data, %skip_high_wide
call void @use64(i64 %extracted)
%extracted_narrow = trunc i64 %extracted to i32
call void @use32(i32 %extracted_narrow)
%num_high_bits_to_smear_narrow = sub i32 32, %nbits
call void @use32(i32 %num_high_bits_to_smear_narrow)
%signbit_positioned = shl i32 %extracted_narrow, %num_high_bits_to_smear_narrow
call void @use32(i32 %signbit_positioned)
%signextended = ashr i32 %signbit_positioned, %num_high_bits_to_smear_narrow ; neither of operands will go away.
ret i32 %signextended
}
; Negative tests
define i32 @n9(i64 %data, i32 %nbits) {
; CHECK-LABEL: @n9(
; CHECK-NEXT: [[SKIP_HIGH:%.*]] = sub i32 63, [[NBITS:%.*]]
; CHECK-NEXT: call void @use32(i32 [[SKIP_HIGH]])
; CHECK-NEXT: [[SKIP_HIGH_WIDE:%.*]] = zext i32 [[SKIP_HIGH]] to i64
; CHECK-NEXT: call void @use64(i64 [[SKIP_HIGH_WIDE]])
; CHECK-NEXT: [[EXTRACTED:%.*]] = lshr i64 [[DATA:%.*]], [[SKIP_HIGH_WIDE]]
; CHECK-NEXT: call void @use64(i64 [[EXTRACTED]])
; CHECK-NEXT: [[EXTRACTED_NARROW:%.*]] = trunc i64 [[EXTRACTED]] to i32
; CHECK-NEXT: call void @use32(i32 [[EXTRACTED_NARROW]])
; CHECK-NEXT: [[NUM_HIGH_BITS_TO_SMEAR_NARROW:%.*]] = sub i32 32, [[NBITS]]
; CHECK-NEXT: call void @use32(i32 [[NUM_HIGH_BITS_TO_SMEAR_NARROW]])
; CHECK-NEXT: [[SIGNBIT_POSITIONED:%.*]] = shl i32 [[EXTRACTED_NARROW]], [[NUM_HIGH_BITS_TO_SMEAR_NARROW]]
; CHECK-NEXT: [[SIGNEXTENDED:%.*]] = ashr i32 [[SIGNBIT_POSITIONED]], [[NUM_HIGH_BITS_TO_SMEAR_NARROW]]
; CHECK-NEXT: ret i32 [[SIGNEXTENDED]]
;
%skip_high = sub i32 63, %nbits ; not 64
call void @use32(i32 %skip_high)
%skip_high_wide = zext i32 %skip_high to i64
call void @use64(i64 %skip_high_wide)
%extracted = lshr i64 %data, %skip_high_wide
call void @use64(i64 %extracted)
%extracted_narrow = trunc i64 %extracted to i32
call void @use32(i32 %extracted_narrow)
%num_high_bits_to_smear_narrow = sub i32 32, %nbits
call void @use32(i32 %num_high_bits_to_smear_narrow)
%signbit_positioned = shl i32 %extracted_narrow, %num_high_bits_to_smear_narrow
%signextended = ashr i32 %signbit_positioned, %num_high_bits_to_smear_narrow
ret i32 %signextended
}
define i32 @n10(i64 %data, i32 %nbits) {
; CHECK-LABEL: @n10(
; CHECK-NEXT: [[SKIP_HIGH:%.*]] = sub i32 64, [[NBITS:%.*]]
; CHECK-NEXT: call void @use32(i32 [[SKIP_HIGH]])
; CHECK-NEXT: [[SKIP_HIGH_WIDE:%.*]] = zext i32 [[SKIP_HIGH]] to i64
; CHECK-NEXT: call void @use64(i64 [[SKIP_HIGH_WIDE]])
; CHECK-NEXT: [[EXTRACTED:%.*]] = lshr i64 [[DATA:%.*]], [[SKIP_HIGH_WIDE]]
; CHECK-NEXT: call void @use64(i64 [[EXTRACTED]])
; CHECK-NEXT: [[EXTRACTED_NARROW:%.*]] = trunc i64 [[EXTRACTED]] to i32
; CHECK-NEXT: call void @use32(i32 [[EXTRACTED_NARROW]])
; CHECK-NEXT: [[NUM_HIGH_BITS_TO_SMEAR_NARROW:%.*]] = sub i32 31, [[NBITS]]
; CHECK-NEXT: call void @use32(i32 [[NUM_HIGH_BITS_TO_SMEAR_NARROW]])
; CHECK-NEXT: [[SIGNBIT_POSITIONED:%.*]] = shl i32 [[EXTRACTED_NARROW]], [[NUM_HIGH_BITS_TO_SMEAR_NARROW]]
; CHECK-NEXT: [[SIGNEXTENDED:%.*]] = ashr i32 [[SIGNBIT_POSITIONED]], [[NUM_HIGH_BITS_TO_SMEAR_NARROW]]
; CHECK-NEXT: ret i32 [[SIGNEXTENDED]]
;
%skip_high = sub i32 64, %nbits
call void @use32(i32 %skip_high)
%skip_high_wide = zext i32 %skip_high to i64
call void @use64(i64 %skip_high_wide)
%extracted = lshr i64 %data, %skip_high_wide
call void @use64(i64 %extracted)
%extracted_narrow = trunc i64 %extracted to i32
call void @use32(i32 %extracted_narrow)
%num_high_bits_to_smear_narrow = sub i32 31, %nbits ; not 32
call void @use32(i32 %num_high_bits_to_smear_narrow)
%signbit_positioned = shl i32 %extracted_narrow, %num_high_bits_to_smear_narrow
%signextended = ashr i32 %signbit_positioned, %num_high_bits_to_smear_narrow
ret i32 %signextended
}
define i32 @n11(i64 %data, i32 %nbits1, i32 %nbits2) {
; CHECK-LABEL: @n11(
; CHECK-NEXT: [[SKIP_HIGH:%.*]] = sub i32 64, [[NBITS1:%.*]]
; CHECK-NEXT: call void @use32(i32 [[SKIP_HIGH]])
; CHECK-NEXT: [[SKIP_HIGH_WIDE:%.*]] = zext i32 [[SKIP_HIGH]] to i64
; CHECK-NEXT: call void @use64(i64 [[SKIP_HIGH_WIDE]])
; CHECK-NEXT: [[EXTRACTED:%.*]] = lshr i64 [[DATA:%.*]], [[SKIP_HIGH_WIDE]]
; CHECK-NEXT: call void @use64(i64 [[EXTRACTED]])
; CHECK-NEXT: [[EXTRACTED_NARROW:%.*]] = trunc i64 [[EXTRACTED]] to i32
; CHECK-NEXT: call void @use32(i32 [[EXTRACTED_NARROW]])
; CHECK-NEXT: [[NUM_HIGH_BITS_TO_SMEAR_NARROW:%.*]] = sub i32 32, [[NBITS2:%.*]]
; CHECK-NEXT: call void @use32(i32 [[NUM_HIGH_BITS_TO_SMEAR_NARROW]])
; CHECK-NEXT: [[SIGNBIT_POSITIONED:%.*]] = shl i32 [[EXTRACTED_NARROW]], [[NUM_HIGH_BITS_TO_SMEAR_NARROW]]
; CHECK-NEXT: [[SIGNEXTENDED:%.*]] = ashr i32 [[SIGNBIT_POSITIONED]], [[NUM_HIGH_BITS_TO_SMEAR_NARROW]]
; CHECK-NEXT: ret i32 [[SIGNEXTENDED]]
;
%skip_high = sub i32 64, %nbits1 ; not %nbits2
call void @use32(i32 %skip_high)
%skip_high_wide = zext i32 %skip_high to i64
call void @use64(i64 %skip_high_wide)
%extracted = lshr i64 %data, %skip_high_wide
call void @use64(i64 %extracted)
%extracted_narrow = trunc i64 %extracted to i32
call void @use32(i32 %extracted_narrow)
%num_high_bits_to_smear_narrow = sub i32 32, %nbits2 ; not %nbits1
call void @use32(i32 %num_high_bits_to_smear_narrow)
%signbit_positioned = shl i32 %extracted_narrow, %num_high_bits_to_smear_narrow
%signextended = ashr i32 %signbit_positioned, %num_high_bits_to_smear_narrow
ret i32 %signextended
}
define i32 @n12(i64 %data, i32 %nbits1, i32 %nbits2) {
; CHECK-LABEL: @n12(
; CHECK-NEXT: [[SKIP_HIGH:%.*]] = sub i32 64, [[NBITS1:%.*]]
; CHECK-NEXT: call void @use32(i32 [[SKIP_HIGH]])
; CHECK-NEXT: [[SKIP_HIGH_WIDE:%.*]] = zext i32 [[SKIP_HIGH]] to i64
; CHECK-NEXT: call void @use64(i64 [[SKIP_HIGH_WIDE]])
; CHECK-NEXT: [[EXTRACTED:%.*]] = lshr i64 [[DATA:%.*]], [[SKIP_HIGH_WIDE]]
; CHECK-NEXT: call void @use64(i64 [[EXTRACTED]])
; CHECK-NEXT: [[EXTRACTED_NARROW:%.*]] = trunc i64 [[EXTRACTED]] to i32
; CHECK-NEXT: call void @use32(i32 [[EXTRACTED_NARROW]])
; CHECK-NEXT: [[NUM_HIGH_BITS_TO_SMEAR_NARROW1:%.*]] = sub i32 32, [[NBITS1]]
; CHECK-NEXT: [[NUM_HIGH_BITS_TO_SMEAR_NARROW2:%.*]] = sub i32 32, [[NBITS2:%.*]]
; CHECK-NEXT: call void @use32(i32 [[NUM_HIGH_BITS_TO_SMEAR_NARROW1]])
; CHECK-NEXT: call void @use32(i32 [[NUM_HIGH_BITS_TO_SMEAR_NARROW2]])
; CHECK-NEXT: [[SIGNBIT_POSITIONED:%.*]] = shl i32 [[EXTRACTED_NARROW]], [[NUM_HIGH_BITS_TO_SMEAR_NARROW1]]
; CHECK-NEXT: [[SIGNEXTENDED:%.*]] = ashr i32 [[SIGNBIT_POSITIONED]], [[NUM_HIGH_BITS_TO_SMEAR_NARROW2]]
; CHECK-NEXT: ret i32 [[SIGNEXTENDED]]
;
%skip_high = sub i32 64, %nbits1
call void @use32(i32 %skip_high)
%skip_high_wide = zext i32 %skip_high to i64
call void @use64(i64 %skip_high_wide)
%extracted = lshr i64 %data, %skip_high_wide
call void @use64(i64 %extracted)
%extracted_narrow = trunc i64 %extracted to i32
call void @use32(i32 %extracted_narrow)
%num_high_bits_to_smear_narrow1 = sub i32 32, %nbits1 ; not %nbits2
%num_high_bits_to_smear_narrow2 = sub i32 32, %nbits2 ; not %nbits1
call void @use32(i32 %num_high_bits_to_smear_narrow1)
call void @use32(i32 %num_high_bits_to_smear_narrow2)
%signbit_positioned = shl i32 %extracted_narrow, %num_high_bits_to_smear_narrow1
%signextended = ashr i32 %signbit_positioned, %num_high_bits_to_smear_narrow2
ret i32 %signextended
}
define i32 @n13(i64 %data, i32 %nbits) {
; CHECK-LABEL: @n13(
; CHECK-NEXT: [[SKIP_HIGH:%.*]] = sub i32 64, [[NBITS:%.*]]
; CHECK-NEXT: call void @use32(i32 [[SKIP_HIGH]])
; CHECK-NEXT: [[SKIP_HIGH_WIDE:%.*]] = zext i32 [[SKIP_HIGH]] to i64
; CHECK-NEXT: call void @use64(i64 [[SKIP_HIGH_WIDE]])
; CHECK-NEXT: [[EXTRACTED:%.*]] = lshr i64 [[DATA:%.*]], [[SKIP_HIGH_WIDE]]
; CHECK-NEXT: call void @use64(i64 [[EXTRACTED]])
; CHECK-NEXT: [[EXTRACTED_NARROW:%.*]] = trunc i64 [[EXTRACTED]] to i32
; CHECK-NEXT: call void @use32(i32 [[EXTRACTED_NARROW]])
; CHECK-NEXT: [[NUM_HIGH_BITS_TO_SMEAR_NARROW:%.*]] = sub i32 32, [[NBITS]]
; CHECK-NEXT: call void @use32(i32 [[NUM_HIGH_BITS_TO_SMEAR_NARROW]])
; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 -1, [[NUM_HIGH_BITS_TO_SMEAR_NARROW]]
; CHECK-NEXT: [[RES:%.*]] = and i32 [[TMP1]], [[EXTRACTED_NARROW]]
; CHECK-NEXT: ret i32 [[RES]]
;
%skip_high = sub i32 64, %nbits
call void @use32(i32 %skip_high)
%skip_high_wide = zext i32 %skip_high to i64
call void @use64(i64 %skip_high_wide)
%extracted = lshr i64 %data, %skip_high_wide
call void @use64(i64 %extracted)
%extracted_narrow = trunc i64 %extracted to i32
call void @use32(i32 %extracted_narrow)
%num_high_bits_to_smear_narrow = sub i32 32, %nbits
call void @use32(i32 %num_high_bits_to_smear_narrow)
%highbits_cleaned = shl i32 %extracted_narrow, %num_high_bits_to_smear_narrow
%res = lshr i32 %highbits_cleaned, %num_high_bits_to_smear_narrow ; not ashr
ret i32 %res
}
define i32 @n13_extrause(i64 %data, i32 %nbits) {
; CHECK-LABEL: @n13_extrause(
; CHECK-NEXT: [[SKIP_HIGH:%.*]] = sub i32 64, [[NBITS:%.*]]
; CHECK-NEXT: call void @use32(i32 [[SKIP_HIGH]])
; CHECK-NEXT: [[SKIP_HIGH_WIDE:%.*]] = zext i32 [[SKIP_HIGH]] to i64
; CHECK-NEXT: call void @use64(i64 [[SKIP_HIGH_WIDE]])
; CHECK-NEXT: [[EXTRACTED:%.*]] = lshr i64 [[DATA:%.*]], [[SKIP_HIGH_WIDE]]
; CHECK-NEXT: call void @use64(i64 [[EXTRACTED]])
; CHECK-NEXT: [[EXTRACTED_NARROW:%.*]] = trunc i64 [[EXTRACTED]] to i32
; CHECK-NEXT: call void @use32(i32 [[EXTRACTED_NARROW]])
; CHECK-NEXT: [[NUM_HIGH_BITS_TO_SMEAR_NARROW:%.*]] = sub i32 32, [[NBITS]]
; CHECK-NEXT: call void @use32(i32 [[NUM_HIGH_BITS_TO_SMEAR_NARROW]])
; CHECK-NEXT: [[HIGHBITS_CLEANED:%.*]] = shl i32 [[EXTRACTED_NARROW]], [[NUM_HIGH_BITS_TO_SMEAR_NARROW]]
; CHECK-NEXT: call void @use32(i32 [[HIGHBITS_CLEANED]])
; CHECK-NEXT: [[RES:%.*]] = lshr i32 [[HIGHBITS_CLEANED]], [[NUM_HIGH_BITS_TO_SMEAR_NARROW]]
; CHECK-NEXT: ret i32 [[RES]]
;
%skip_high = sub i32 64, %nbits
call void @use32(i32 %skip_high)
%skip_high_wide = zext i32 %skip_high to i64
call void @use64(i64 %skip_high_wide)
%extracted = lshr i64 %data, %skip_high_wide
call void @use64(i64 %extracted)
%extracted_narrow = trunc i64 %extracted to i32
call void @use32(i32 %extracted_narrow)
%num_high_bits_to_smear_narrow = sub i32 32, %nbits
call void @use32(i32 %num_high_bits_to_smear_narrow)
%highbits_cleaned = shl i32 %extracted_narrow, %num_high_bits_to_smear_narrow
call void @use32(i32 %highbits_cleaned)
%res = lshr i32 %highbits_cleaned, %num_high_bits_to_smear_narrow ; not ashr
ret i32 %res
}
define i32 @n14(i64 %data, i32 %nbits) {
; CHECK-LABEL: @n14(
; CHECK-NEXT: [[SKIP_HIGH:%.*]] = sub i32 64, [[NBITS:%.*]]
; CHECK-NEXT: call void @use32(i32 [[SKIP_HIGH]])
; CHECK-NEXT: [[SKIP_HIGH_WIDE:%.*]] = zext i32 [[SKIP_HIGH]] to i64
; CHECK-NEXT: call void @use64(i64 [[SKIP_HIGH_WIDE]])
; CHECK-NEXT: [[EXTRACTED:%.*]] = ashr i64 [[DATA:%.*]], [[SKIP_HIGH_WIDE]]
; CHECK-NEXT: call void @use64(i64 [[EXTRACTED]])
; CHECK-NEXT: [[EXTRACTED_NARROW:%.*]] = trunc i64 [[EXTRACTED]] to i32
; CHECK-NEXT: call void @use32(i32 [[EXTRACTED_NARROW]])
; CHECK-NEXT: [[NUM_HIGH_BITS_TO_SMEAR_NARROW:%.*]] = sub i32 32, [[NBITS]]
; CHECK-NEXT: call void @use32(i32 [[NUM_HIGH_BITS_TO_SMEAR_NARROW]])
; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 -1, [[NUM_HIGH_BITS_TO_SMEAR_NARROW]]
; CHECK-NEXT: [[RES:%.*]] = and i32 [[TMP1]], [[EXTRACTED_NARROW]]
; CHECK-NEXT: ret i32 [[RES]]
;
%skip_high = sub i32 64, %nbits
call void @use32(i32 %skip_high)
%skip_high_wide = zext i32 %skip_high to i64
call void @use64(i64 %skip_high_wide)
%extracted = ashr i64 %data, %skip_high_wide
call void @use64(i64 %extracted)
%extracted_narrow = trunc i64 %extracted to i32
call void @use32(i32 %extracted_narrow)
%num_high_bits_to_smear_narrow = sub i32 32, %nbits
call void @use32(i32 %num_high_bits_to_smear_narrow)
%highbits_cleaned = shl i32 %extracted_narrow, %num_high_bits_to_smear_narrow
%res = lshr i32 %highbits_cleaned, %num_high_bits_to_smear_narrow ; not ashr
ret i32 %res
}
define i32 @n14_extrause(i64 %data, i32 %nbits) {
; CHECK-LABEL: @n14_extrause(
; CHECK-NEXT: [[SKIP_HIGH:%.*]] = sub i32 64, [[NBITS:%.*]]
; CHECK-NEXT: call void @use32(i32 [[SKIP_HIGH]])
; CHECK-NEXT: [[SKIP_HIGH_WIDE:%.*]] = zext i32 [[SKIP_HIGH]] to i64
; CHECK-NEXT: call void @use64(i64 [[SKIP_HIGH_WIDE]])
; CHECK-NEXT: [[EXTRACTED:%.*]] = ashr i64 [[DATA:%.*]], [[SKIP_HIGH_WIDE]]
; CHECK-NEXT: call void @use64(i64 [[EXTRACTED]])
; CHECK-NEXT: [[EXTRACTED_NARROW:%.*]] = trunc i64 [[EXTRACTED]] to i32
; CHECK-NEXT: call void @use32(i32 [[EXTRACTED_NARROW]])
; CHECK-NEXT: [[NUM_HIGH_BITS_TO_SMEAR_NARROW:%.*]] = sub i32 32, [[NBITS]]
; CHECK-NEXT: call void @use32(i32 [[NUM_HIGH_BITS_TO_SMEAR_NARROW]])
; CHECK-NEXT: [[HIGHBITS_CLEANED:%.*]] = shl i32 [[EXTRACTED_NARROW]], [[NUM_HIGH_BITS_TO_SMEAR_NARROW]]
; CHECK-NEXT: call void @use32(i32 [[HIGHBITS_CLEANED]])
; CHECK-NEXT: [[RES:%.*]] = lshr i32 [[HIGHBITS_CLEANED]], [[NUM_HIGH_BITS_TO_SMEAR_NARROW]]
; CHECK-NEXT: ret i32 [[RES]]
;
%skip_high = sub i32 64, %nbits
call void @use32(i32 %skip_high)
%skip_high_wide = zext i32 %skip_high to i64
call void @use64(i64 %skip_high_wide)
%extracted = ashr i64 %data, %skip_high_wide
call void @use64(i64 %extracted)
%extracted_narrow = trunc i64 %extracted to i32
call void @use32(i32 %extracted_narrow)
%num_high_bits_to_smear_narrow = sub i32 32, %nbits
call void @use32(i32 %num_high_bits_to_smear_narrow)
%highbits_cleaned = shl i32 %extracted_narrow, %num_high_bits_to_smear_narrow
call void @use32(i32 %highbits_cleaned)
%res = lshr i32 %highbits_cleaned, %num_high_bits_to_smear_narrow ; not ashr
ret i32 %res
}