aboutsummaryrefslogtreecommitdiff
path: root/llvm/test/Transforms/VectorCombine/X86/bitop-of-castops.ll
blob: 79e72aaed608217a0066fdabec5ba192d635d363 (plain)
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
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -passes=vector-combine -S -mtriple=x86_64-- | FileCheck %s

; Negative test: bitcast from float to int (optimization should not apply)
define <4 x i32> @and_bitcast_v4f32_to_v4i32(<4 x float> %a, <4 x float> %b) {
; CHECK-LABEL: @and_bitcast_v4f32_to_v4i32(
; CHECK-NEXT:    [[BC1:%.*]] = bitcast <4 x float> [[A:%.*]] to <4 x i32>
; CHECK-NEXT:    [[BC2:%.*]] = bitcast <4 x float> [[B:%.*]] to <4 x i32>
; CHECK-NEXT:    [[AND:%.*]] = and <4 x i32> [[BC1]], [[BC2]]
; CHECK-NEXT:    ret <4 x i32> [[AND]]
;
  %bc1 = bitcast <4 x float> %a to <4 x i32>
  %bc2 = bitcast <4 x float> %b to <4 x i32>
  %and = and <4 x i32> %bc1, %bc2
  ret <4 x i32> %and
}

; Test bitwise operations with integer-to-integer bitcast
define <2 x i32> @or_bitcast_v4i16_to_v2i32(<4 x i16> %a, <4 x i16> %b) {
; CHECK-LABEL: @or_bitcast_v4i16_to_v2i32(
; CHECK-NEXT:    [[B:%.*]] = or <4 x i16> [[A:%.*]], [[B1:%.*]]
; CHECK-NEXT:    [[BC2:%.*]] = bitcast <4 x i16> [[B]] to <2 x i32>
; CHECK-NEXT:    ret <2 x i32> [[BC2]]
;
  %bc1 = bitcast <4 x i16> %a to <2 x i32>
  %bc2 = bitcast <4 x i16> %b to <2 x i32>
  %or = or <2 x i32> %bc1, %bc2
  ret <2 x i32> %or
}

define <16 x i8> @xor_bitcast_v2i64_to_v16i8(<2 x i64> %a, <2 x i64> %b) {
; CHECK-LABEL: @xor_bitcast_v2i64_to_v16i8(
; CHECK-NEXT:    [[B:%.*]] = xor <2 x i64> [[A:%.*]], [[B1:%.*]]
; CHECK-NEXT:    [[BC2:%.*]] = bitcast <2 x i64> [[B]] to <16 x i8>
; CHECK-NEXT:    ret <16 x i8> [[BC2]]
;
  %bc1 = bitcast <2 x i64> %a to <16 x i8>
  %bc2 = bitcast <2 x i64> %b to <16 x i8>
  %xor = xor <16 x i8> %bc1, %bc2
  ret <16 x i8> %xor
}

; Test bitwise operations with truncate
define <4 x i16> @and_trunc_v4i32_to_v4i16(<4 x i32> %a, <4 x i32> %b) {
; CHECK-LABEL: @and_trunc_v4i32_to_v4i16(
; CHECK-NEXT:    [[AND_INNER:%.*]] = and <4 x i32> [[A:%.*]], [[B:%.*]]
; CHECK-NEXT:    [[AND:%.*]] = trunc <4 x i32> [[AND_INNER]] to <4 x i16>
; CHECK-NEXT:    ret <4 x i16> [[AND]]
;
  %t1 = trunc <4 x i32> %a to <4 x i16>
  %t2 = trunc <4 x i32> %b to <4 x i16>
  %and = and <4 x i16> %t1, %t2
  ret <4 x i16> %and
}

define <8 x i8> @or_trunc_v8i16_to_v8i8(<8 x i16> %a, <8 x i16> %b) {
; CHECK-LABEL: @or_trunc_v8i16_to_v8i8(
; CHECK-NEXT:    [[OR_INNER:%.*]] = or <8 x i16> [[A:%.*]], [[B:%.*]]
; CHECK-NEXT:    [[OR:%.*]] = trunc <8 x i16> [[OR_INNER]] to <8 x i8>
; CHECK-NEXT:    ret <8 x i8> [[OR]]
;
  %t1 = trunc <8 x i16> %a to <8 x i8>
  %t2 = trunc <8 x i16> %b to <8 x i8>
  %or = or <8 x i8> %t1, %t2
  ret <8 x i8> %or
}

define <2 x i32> @xor_trunc_v2i64_to_v2i32(<2 x i64> %a, <2 x i64> %b) {
; CHECK-LABEL: @xor_trunc_v2i64_to_v2i32(
; CHECK-NEXT:    [[XOR_INNER:%.*]] = xor <2 x i64> [[A:%.*]], [[B:%.*]]
; CHECK-NEXT:    [[XOR:%.*]] = trunc <2 x i64> [[XOR_INNER]] to <2 x i32>
; CHECK-NEXT:    ret <2 x i32> [[XOR]]
;
  %t1 = trunc <2 x i64> %a to <2 x i32>
  %t2 = trunc <2 x i64> %b to <2 x i32>
  %xor = xor <2 x i32> %t1, %t2
  ret <2 x i32> %xor
}

; Test bitwise operations with zero extend
define <4 x i32> @and_zext_v4i16_to_v4i32(<4 x i16> %a, <4 x i16> %b) {
; CHECK-LABEL: @and_zext_v4i16_to_v4i32(
; CHECK-NEXT:    [[AND_INNER:%.*]] = and <4 x i16> [[A:%.*]], [[B:%.*]]
; CHECK-NEXT:    [[AND:%.*]] = zext <4 x i16> [[AND_INNER]] to <4 x i32>
; CHECK-NEXT:    ret <4 x i32> [[AND]]
;
  %z1 = zext <4 x i16> %a to <4 x i32>
  %z2 = zext <4 x i16> %b to <4 x i32>
  %and = and <4 x i32> %z1, %z2
  ret <4 x i32> %and
}

define <8 x i16> @or_zext_v8i8_to_v8i16(<8 x i8> %a, <8 x i8> %b) {
; CHECK-LABEL: @or_zext_v8i8_to_v8i16(
; CHECK-NEXT:    [[OR_INNER:%.*]] = or <8 x i8> [[A:%.*]], [[B:%.*]]
; CHECK-NEXT:    [[OR:%.*]] = zext <8 x i8> [[OR_INNER]] to <8 x i16>
; CHECK-NEXT:    ret <8 x i16> [[OR]]
;
  %z1 = zext <8 x i8> %a to <8 x i16>
  %z2 = zext <8 x i8> %b to <8 x i16>
  %or = or <8 x i16> %z1, %z2
  ret <8 x i16> %or
}

define <2 x i64> @xor_zext_v2i32_to_v2i64(<2 x i32> %a, <2 x i32> %b) {
; CHECK-LABEL: @xor_zext_v2i32_to_v2i64(
; CHECK-NEXT:    [[XOR_INNER:%.*]] = xor <2 x i32> [[A:%.*]], [[B:%.*]]
; CHECK-NEXT:    [[XOR:%.*]] = zext <2 x i32> [[XOR_INNER]] to <2 x i64>
; CHECK-NEXT:    ret <2 x i64> [[XOR]]
;
  %z1 = zext <2 x i32> %a to <2 x i64>
  %z2 = zext <2 x i32> %b to <2 x i64>
  %xor = xor <2 x i64> %z1, %z2
  ret <2 x i64> %xor
}

; Test bitwise operations with sign extend
define <4 x i32> @and_sext_v4i16_to_v4i32(<4 x i16> %a, <4 x i16> %b) {
; CHECK-LABEL: @and_sext_v4i16_to_v4i32(
; CHECK-NEXT:    [[AND_INNER:%.*]] = and <4 x i16> [[A:%.*]], [[B:%.*]]
; CHECK-NEXT:    [[AND:%.*]] = sext <4 x i16> [[AND_INNER]] to <4 x i32>
; CHECK-NEXT:    ret <4 x i32> [[AND]]
;
  %s1 = sext <4 x i16> %a to <4 x i32>
  %s2 = sext <4 x i16> %b to <4 x i32>
  %and = and <4 x i32> %s1, %s2
  ret <4 x i32> %and
}

define <8 x i16> @or_sext_v8i8_to_v8i16(<8 x i8> %a, <8 x i8> %b) {
; CHECK-LABEL: @or_sext_v8i8_to_v8i16(
; CHECK-NEXT:    [[OR_INNER:%.*]] = or <8 x i8> [[A:%.*]], [[B:%.*]]
; CHECK-NEXT:    [[OR:%.*]] = sext <8 x i8> [[OR_INNER]] to <8 x i16>
; CHECK-NEXT:    ret <8 x i16> [[OR]]
;
  %s1 = sext <8 x i8> %a to <8 x i16>
  %s2 = sext <8 x i8> %b to <8 x i16>
  %or = or <8 x i16> %s1, %s2
  ret <8 x i16> %or
}

define <2 x i64> @xor_sext_v2i32_to_v2i64(<2 x i32> %a, <2 x i32> %b) {
; CHECK-LABEL: @xor_sext_v2i32_to_v2i64(
; CHECK-NEXT:    [[XOR_INNER:%.*]] = xor <2 x i32> [[A:%.*]], [[B:%.*]]
; CHECK-NEXT:    [[XOR:%.*]] = sext <2 x i32> [[XOR_INNER]] to <2 x i64>
; CHECK-NEXT:    ret <2 x i64> [[XOR]]
;
  %s1 = sext <2 x i32> %a to <2 x i64>
  %s2 = sext <2 x i32> %b to <2 x i64>
  %xor = xor <2 x i64> %s1, %s2
  ret <2 x i64> %xor
}

; Negative test: mismatched cast types (zext and sext)
define <4 x i32> @and_zext_sext_mismatch(<4 x i16> %a, <4 x i16> %b) {
; CHECK-LABEL: @and_zext_sext_mismatch(
; CHECK-NEXT:    [[Z1:%.*]] = zext <4 x i16> [[A:%.*]] to <4 x i32>
; CHECK-NEXT:    [[S2:%.*]] = sext <4 x i16> [[B:%.*]] to <4 x i32>
; CHECK-NEXT:    [[AND:%.*]] = and <4 x i32> [[Z1]], [[S2]]
; CHECK-NEXT:    ret <4 x i32> [[AND]]
;
  %z1 = zext <4 x i16> %a to <4 x i32>
  %s2 = sext <4 x i16> %b to <4 x i32>
  %and = and <4 x i32> %z1, %s2
  ret <4 x i32> %and
}

; Negative test: mismatched source types
define <4 x i32> @or_zext_different_src_types(<4 x i16> %a, <4 x i8> %b) {
; CHECK-LABEL: @or_zext_different_src_types(
; CHECK-NEXT:    [[Z1:%.*]] = zext <4 x i16> [[A:%.*]] to <4 x i32>
; CHECK-NEXT:    [[Z2:%.*]] = zext <4 x i8> [[B:%.*]] to <4 x i32>
; CHECK-NEXT:    [[OR:%.*]] = or <4 x i32> [[Z1]], [[Z2]]
; CHECK-NEXT:    ret <4 x i32> [[OR]]
;
  %z1 = zext <4 x i16> %a to <4 x i32>
  %z2 = zext <4 x i8> %b to <4 x i32>
  %or = or <4 x i32> %z1, %z2
  ret <4 x i32> %or
}

; Negative test: scalar types (not vectors)
define i32 @xor_zext_scalar(i16 %a, i16 %b) {
; CHECK-LABEL: @xor_zext_scalar(
; CHECK-NEXT:    [[Z1:%.*]] = zext i16 [[A:%.*]] to i32
; CHECK-NEXT:    [[Z2:%.*]] = zext i16 [[B:%.*]] to i32
; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[Z1]], [[Z2]]
; CHECK-NEXT:    ret i32 [[XOR]]
;
  %z1 = zext i16 %a to i32
  %z2 = zext i16 %b to i32
  %xor = xor i32 %z1, %z2
  ret i32 %xor
}

; Test multi-use: one cast has multiple uses
define <4 x i32> @and_zext_multiuse(<4 x i16> %a, <4 x i16> %b) {
; CHECK-LABEL: @and_zext_multiuse(
; CHECK-NEXT:    [[Z1:%.*]] = zext <4 x i16> [[A:%.*]] to <4 x i32>
; CHECK-NEXT:    [[AND_INNER:%.*]] = and <4 x i16> [[A]], [[B:%.*]]
; CHECK-NEXT:    [[AND:%.*]] = zext <4 x i16> [[AND_INNER]] to <4 x i32>
; CHECK-NEXT:    [[ADD:%.*]] = add <4 x i32> [[Z1]], [[AND]]
; CHECK-NEXT:    ret <4 x i32> [[ADD]]
;
  %z1 = zext <4 x i16> %a to <4 x i32>
  %z2 = zext <4 x i16> %b to <4 x i32>
  %and = and <4 x i32> %z1, %z2
  %add = add <4 x i32> %z1, %and  ; z1 has multiple uses
  ret <4 x i32> %add
}

; Test with different vector sizes
define <16 x i16> @or_zext_v16i8_to_v16i16(<16 x i8> %a, <16 x i8> %b) {
; CHECK-LABEL: @or_zext_v16i8_to_v16i16(
; CHECK-NEXT:    [[OR_INNER:%.*]] = or <16 x i8> [[A:%.*]], [[B:%.*]]
; CHECK-NEXT:    [[OR:%.*]] = zext <16 x i8> [[OR_INNER]] to <16 x i16>
; CHECK-NEXT:    ret <16 x i16> [[OR]]
;
  %z1 = zext <16 x i8> %a to <16 x i16>
  %z2 = zext <16 x i8> %b to <16 x i16>
  %or = or <16 x i16> %z1, %z2
  ret <16 x i16> %or
}

; Test bitcast with different element counts
define <8 x i16> @xor_bitcast_v4i32_to_v8i16(<4 x i32> %a, <4 x i32> %b) {
; CHECK-LABEL: @xor_bitcast_v4i32_to_v8i16(
; CHECK-NEXT:    [[XOR_INNER:%.*]] = xor <4 x i32> [[A:%.*]], [[B:%.*]]
; CHECK-NEXT:    [[XOR:%.*]] = bitcast <4 x i32> [[XOR_INNER]] to <8 x i16>
; CHECK-NEXT:    ret <8 x i16> [[XOR]]
;
  %bc1 = bitcast <4 x i32> %a to <8 x i16>
  %bc2 = bitcast <4 x i32> %b to <8 x i16>
  %xor = xor <8 x i16> %bc1, %bc2
  ret <8 x i16> %xor
}

; Test truncate with flag preservation
define <4 x i16> @and_trunc_nuw_nsw(<4 x i32> %a, <4 x i32> %b) {
; CHECK-LABEL: @and_trunc_nuw_nsw(
; CHECK-NEXT:    [[AND_INNER:%.*]] = and <4 x i32> [[A:%.*]], [[B:%.*]]
; CHECK-NEXT:    [[AND:%.*]] = trunc nuw nsw <4 x i32> [[AND_INNER]] to <4 x i16>
; CHECK-NEXT:    ret <4 x i16> [[AND]]
;
  %t1 = trunc nuw nsw <4 x i32> %a to <4 x i16>
  %t2 = trunc nuw nsw <4 x i32> %b to <4 x i16>
  %and = and <4 x i16> %t1, %t2
  ret <4 x i16> %and
}

; Test sign extend with nneg flag
define <4 x i32> @or_zext_nneg(<4 x i16> %a, <4 x i16> %b) {
; CHECK-LABEL: @or_zext_nneg(
; CHECK-NEXT:    [[OR_INNER:%.*]] = or <4 x i16> [[A:%.*]], [[B:%.*]]
; CHECK-NEXT:    [[OR:%.*]] = zext nneg <4 x i16> [[OR_INNER]] to <4 x i32>
; CHECK-NEXT:    ret <4 x i32> [[OR]]
;
  %z1 = zext nneg <4 x i16> %a to <4 x i32>
  %z2 = zext nneg <4 x i16> %b to <4 x i32>
  %or = or <4 x i32> %z1, %z2
  ret <4 x i32> %or
}

; Test bitwise operations with integer-to-integer bitcast with one constant
define <2 x i32> @or_bitcast_v4i16_to_v2i32_constant(<4 x i16> %a) {
; CHECK-LABEL: @or_bitcast_v4i16_to_v2i32_constant(
; CHECK-NEXT:    [[A:%.*]] = or <4 x i16> [[A1:%.*]], <i16 16960, i16 15, i16 -31616, i16 30>
; CHECK-NEXT:    [[BC1:%.*]] = bitcast <4 x i16> [[A]] to <2 x i32>
; CHECK-NEXT:    ret <2 x i32> [[BC1]]
;
  %bc1 = bitcast <4 x i16> %a to <2 x i32>
  %or = or <2 x i32> %bc1, <i32 1000000, i32 2000000>
  ret <2 x i32> %or
}

define <2 x i32> @or_bitcast_v4i16_to_v2i32_constant_commuted(<4 x i16> %a) {
; CHECK-LABEL: @or_bitcast_v4i16_to_v2i32_constant_commuted(
; CHECK-NEXT:    [[A:%.*]] = or <4 x i16> [[A1:%.*]], <i16 16960, i16 15, i16 -31616, i16 30>
; CHECK-NEXT:    [[BC1:%.*]] = bitcast <4 x i16> [[A]] to <2 x i32>
; CHECK-NEXT:    ret <2 x i32> [[BC1]]
;
  %bc1 = bitcast <4 x i16> %a to <2 x i32>
  %or = or <2 x i32> <i32 1000000, i32 2000000>, %bc1
  ret <2 x i32> %or
}

; Test bitwise operations with truncate and one constant
define <4 x i16> @or_trunc_v4i32_to_v4i16_constant(<4 x i32> %a) {
; CHECK-LABEL: @or_trunc_v4i32_to_v4i16_constant(
; CHECK-NEXT:    [[A:%.*]] = or <4 x i32> [[A1:%.*]], <i32 1, i32 2, i32 3, i32 4>
; CHECK-NEXT:    [[T1:%.*]] = trunc <4 x i32> [[A]] to <4 x i16>
; CHECK-NEXT:    ret <4 x i16> [[T1]]
;
  %t1 = trunc <4 x i32> %a to <4 x i16>
  %or = or <4 x i16> %t1, <i16 1, i16 2, i16 3, i16 4>
  ret <4 x i16> %or
}

; Test bitwise operations with zero extend and one constant
define <4 x i32> @or_zext_v4i16_to_v4i32_constant(<4 x i16> %a) {
; CHECK-LABEL: @or_zext_v4i16_to_v4i32_constant(
; CHECK-NEXT:    [[A:%.*]] = or <4 x i16> [[A1:%.*]], <i16 1, i16 2, i16 3, i16 4>
; CHECK-NEXT:    [[Z1:%.*]] = zext <4 x i16> [[A]] to <4 x i32>
; CHECK-NEXT:    ret <4 x i32> [[Z1]]
;
  %z1 = zext <4 x i16> %a to <4 x i32>
  %or = or <4 x i32> %z1, <i32 1, i32 2, i32 3, i32 4>
  ret <4 x i32> %or
}

define <4 x i32> @or_zext_v4i8_to_v4i32_constant_with_loss(<4 x i8> %a) {
; CHECK-LABEL: @or_zext_v4i8_to_v4i32_constant_with_loss(
; CHECK-NEXT:    [[Z1:%.*]] = zext <4 x i8> [[A:%.*]] to <4 x i32>
; CHECK-NEXT:    [[OR:%.*]] = or <4 x i32> [[Z1]], <i32 1024, i32 129, i32 3, i32 4>
; CHECK-NEXT:    ret <4 x i32> [[OR]]
;
  %z1 = zext <4 x i8> %a to <4 x i32>
  %or = or <4 x i32> %z1, <i32 1024, i32 129, i32 3, i32 4>
  ret <4 x i32> %or
}

; Test bitwise operations with sign extend and one constant
define <4 x i32> @or_sext_v4i8_to_v4i32_positive_constant(<4 x i8> %a) {
; CHECK-LABEL: @or_sext_v4i8_to_v4i32_positive_constant(
; CHECK-NEXT:    [[A:%.*]] = or <4 x i8> [[A1:%.*]], <i8 1, i8 2, i8 3, i8 4>
; CHECK-NEXT:    [[S1:%.*]] = sext <4 x i8> [[A]] to <4 x i32>
; CHECK-NEXT:    ret <4 x i32> [[S1]]
;
  %s1 = sext <4 x i8> %a to <4 x i32>
  %or = or <4 x i32> %s1, <i32 1, i32 2, i32 3, i32 4>
  ret <4 x i32> %or
}

define <4 x i32> @or_sext_v4i8_to_v4i32_minus_constant(<4 x i8> %a) {
; CHECK-LABEL: @or_sext_v4i8_to_v4i32_minus_constant(
; CHECK-NEXT:    [[A:%.*]] = or <4 x i8> [[A1:%.*]], <i8 -1, i8 -2, i8 -3, i8 -4>
; CHECK-NEXT:    [[S1:%.*]] = sext <4 x i8> [[A]] to <4 x i32>
; CHECK-NEXT:    ret <4 x i32> [[S1]]
;
  %s1 = sext <4 x i8> %a to <4 x i32>
  %or = or <4 x i32> %s1, <i32 -1, i32 -2, i32 -3, i32 -4>
  ret <4 x i32> %or
}

define <4 x i32> @or_sext_v4i8_to_v4i32_constant_with_loss(<4 x i8> %a) {
; CHECK-LABEL: @or_sext_v4i8_to_v4i32_constant_with_loss(
; CHECK-NEXT:    [[Z1:%.*]] = sext <4 x i8> [[A:%.*]] to <4 x i32>
; CHECK-NEXT:    [[OR:%.*]] = or <4 x i32> [[Z1]], <i32 -10000, i32 2, i32 3, i32 4>
; CHECK-NEXT:    ret <4 x i32> [[OR]]
;
  %z1 = sext <4 x i8> %a to <4 x i32>
  %or = or <4 x i32> %z1, <i32 -10000, i32 2, i32 3, i32 4>
  ret <4 x i32> %or
}

; Test truncate with flag preservation and one constant
define <4 x i16> @and_trunc_nuw_nsw_constant(<4 x i32> %a) {
; CHECK-LABEL: @and_trunc_nuw_nsw_constant(
; CHECK-NEXT:    [[AND_INNER:%.*]] = and <4 x i32> [[A:%.*]], <i32 1, i32 2, i32 3, i32 4>
; CHECK-NEXT:    [[AND:%.*]] = trunc <4 x i32> [[AND_INNER]] to <4 x i16>
; CHECK-NEXT:    ret <4 x i16> [[AND]]
;
  %t1 = trunc nuw nsw <4 x i32> %a to <4 x i16>
  %and = and <4 x i16> %t1, <i16 1, i16 2, i16 3, i16 4>
  ret <4 x i16> %and
}

define <4 x i8> @and_trunc_nuw_nsw_minus_constant(<4 x i32> %a) {
; CHECK-LABEL: @and_trunc_nuw_nsw_minus_constant(
; CHECK-NEXT:    [[AND_INNER:%.*]] = and <4 x i32> [[A:%.*]], <i32 240, i32 241, i32 242, i32 243>
; CHECK-NEXT:    [[AND:%.*]] = trunc <4 x i32> [[AND_INNER]] to <4 x i8>
; CHECK-NEXT:    ret <4 x i8> [[AND]]
;
  %t1 = trunc nuw nsw <4 x i32> %a to <4 x i8>
  %and = and <4 x i8> %t1, <i8 240, i8 241, i8 242, i8 243>
  ret <4 x i8> %and
}

define <4 x i8> @and_trunc_nuw_nsw_multiconstant(<4 x i32> %a) {
; CHECK-LABEL: @and_trunc_nuw_nsw_multiconstant(
; CHECK-NEXT:    [[AND_INNER:%.*]] = and <4 x i32> [[A:%.*]], <i32 240, i32 1, i32 242, i32 3>
; CHECK-NEXT:    [[AND:%.*]] = trunc <4 x i32> [[AND_INNER]] to <4 x i8>
; CHECK-NEXT:    ret <4 x i8> [[AND]]
;
  %t1 = trunc nuw nsw <4 x i32> %a to <4 x i8>
  %and = and <4 x i8> %t1, <i8 240, i8 1, i8 242, i8 3>
  ret <4 x i8> %and
}

; Test sign extend with nneg flag and one constant
define <4 x i32> @or_zext_nneg_constant(<4 x i16> %a) {
; CHECK-LABEL: @or_zext_nneg_constant(
; CHECK-NEXT:    [[OR_INNER:%.*]] = or <4 x i16> [[A:%.*]], <i16 1, i16 2, i16 3, i16 4>
; CHECK-NEXT:    [[OR:%.*]] = zext <4 x i16> [[OR_INNER]] to <4 x i32>
; CHECK-NEXT:    ret <4 x i32> [[OR]]
;
  %z1 = zext nneg <4 x i16> %a to <4 x i32>
  %or = or <4 x i32> %z1, <i32 1, i32 2, i32 3, i32 4>
  ret <4 x i32> %or
}

define <4 x i32> @or_zext_nneg_minus_constant(<4 x i8> %a) {
; CHECK-LABEL: @or_zext_nneg_minus_constant(
; CHECK-NEXT:    [[OR_INNER:%.*]] = or <4 x i8> [[A:%.*]], <i8 -16, i8 -15, i8 -14, i8 -13>
; CHECK-NEXT:    [[OR:%.*]] = zext <4 x i8> [[OR_INNER]] to <4 x i32>
; CHECK-NEXT:    ret <4 x i32> [[OR]]
;
  %z1 = zext nneg <4 x i8> %a to <4 x i32>
  %or = or <4 x i32> %z1, <i32 240, i32 241, i32 242, i32 243>
  ret <4 x i32> %or
}

define <4 x i32> @or_zext_nneg_multiconstant(<4 x i8> %a) {
; CHECK-LABEL: @or_zext_nneg_multiconstant(
; CHECK-NEXT:    [[OR_INNER:%.*]] = or <4 x i8> [[A:%.*]], <i8 -16, i8 1, i8 -14, i8 3>
; CHECK-NEXT:    [[OR:%.*]] = zext <4 x i8> [[OR_INNER]] to <4 x i32>
; CHECK-NEXT:    ret <4 x i32> [[OR]]
;
  %z1 = zext nneg <4 x i8> %a to <4 x i32>
  %or = or <4 x i32> %z1, <i32 240, i32 1, i32 242, i32 3>
  ret <4 x i32> %or
}

; Negative test: bitcast from scalar float to vector int (optimization should not apply)
define <2 x i16> @and_bitcast_f32_to_v2i16_constant(float %a) {
; CHECK-LABEL: @and_bitcast_f32_to_v2i16_constant(
; CHECK-NEXT:    [[BC2:%.*]] = bitcast float [[B:%.*]] to <2 x i16>
; CHECK-NEXT:    [[AND:%.*]] = and <2 x i16> <i16 0, i16 1>, [[BC2]]
; CHECK-NEXT:    ret <2 x i16> [[AND]]
;
  %bc = bitcast float %a to <2 x i16>
  %and = and <2 x i16> <i16 0, i16 1>, %bc
  ret <2 x i16> %and
}

define <2 x i16> @and_bitcast_f32_to_v2i16(float %a, float %b) {
; CHECK-LABEL: @and_bitcast_f32_to_v2i16(
; CHECK-NEXT:    [[BC1:%.*]] = bitcast float [[A:%.*]] to <2 x i16>
; CHECK-NEXT:    [[BC2:%.*]] = bitcast float [[B:%.*]] to <2 x i16>
; CHECK-NEXT:    [[AND:%.*]] = and <2 x i16> [[BC1]], [[BC2]]
; CHECK-NEXT:    ret <2 x i16> [[AND]]
;
  %bc1 = bitcast float %a to <2 x i16>
  %bc2 = bitcast float %b to <2 x i16>
  %and = and <2 x i16> %bc1, %bc2
  ret <2 x i16> %and
}

; Negative test: bitcast from vector float to scalar int (optimization should not apply)
define i64 @and_bitcast_v2f32_to_i64_constant(<2 x float> %a) {
; CHECK-LABEL: @and_bitcast_v2f32_to_i64_constant(
; CHECK-NEXT:    [[BC2:%.*]] = bitcast <2 x float> [[B:%.*]] to i64
; CHECK-NEXT:    [[AND:%.*]] = and i64 123, [[BC2]]
; CHECK-NEXT:    ret i64 [[AND]]
;
  %bc = bitcast <2 x float> %a to i64
  %and = and i64 123, %bc
  ret i64 %and
}

define i64 @and_bitcast_v2f32_to_i64(<2 x float> %a, <2 x float> %b) {
; CHECK-LABEL: @and_bitcast_v2f32_to_i64(
; CHECK-NEXT:    [[BC1:%.*]] = bitcast <2 x float> [[A:%.*]] to i64
; CHECK-NEXT:    [[BC2:%.*]] = bitcast <2 x float> [[B:%.*]] to i64
; CHECK-NEXT:    [[AND:%.*]] = and i64 [[BC1]], [[BC2]]
; CHECK-NEXT:    ret i64 [[AND]]
;
  %bc1 = bitcast <2 x float> %a to i64
  %bc2 = bitcast <2 x float> %b to i64
  %and = and i64 %bc1, %bc2
  ret i64 %and
}

; Test no-op bitcast
define i16 @xor_bitcast_i16_to_i16_constant(i16 %a) {
; CHECK-LABEL: @xor_bitcast_i16_to_i16_constant(
; CHECK-NEXT:    [[BC2:%.*]] = bitcast i16 [[B:%.*]] to i16
; CHECK-NEXT:    [[OR:%.*]] = xor i16 123, [[BC2]]
; CHECK-NEXT:    ret i16 [[OR]]
;
  %bc = bitcast i16 %a to i16
  %or = xor i16 123, %bc
  ret i16 %or
}

define i16 @xor_bitcast_i16_to_i16(i16 %a, i16 %b) {
; CHECK-LABEL: @xor_bitcast_i16_to_i16(
; CHECK-NEXT:    [[BC1:%.*]] = bitcast i16 [[A:%.*]] to i16
; CHECK-NEXT:    [[BC2:%.*]] = bitcast i16 [[B:%.*]] to i16
; CHECK-NEXT:    [[OR:%.*]] = xor i16 [[BC1]], [[BC2]]
; CHECK-NEXT:    ret i16 [[OR]]
;
  %bc1 = bitcast i16 %a to i16
  %bc2 = bitcast i16 %b to i16
  %or = xor i16 %bc1, %bc2
  ret i16 %or
}

; Test bitwise operations with integer vector to integer bitcast
define <16 x i1> @xor_bitcast_i16_to_v16i1_constant(i16 %a) {
; CHECK-LABEL: @xor_bitcast_i16_to_v16i1_constant(
; CHECK-NEXT:    [[B:%.*]] = xor i16 [[A:%.*]], -1
; CHECK-NEXT:    [[BC2:%.*]] = bitcast i16 [[B]] to <16 x i1>
; CHECK-NEXT:    ret <16 x i1> [[BC2]]
;
  %bc = bitcast i16 %a to <16 x i1>
  %or = xor <16 x i1> %bc, splat (i1 true)
  ret <16 x i1> %or
}

define <16 x i1> @xor_bitcast_i16_to_v16i1(i16 %a, i16 %b) {
; CHECK-LABEL: @xor_bitcast_i16_to_v16i1(
; CHECK-NEXT:    [[B1:%.*]] = xor i16 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT:    [[BC3:%.*]] = bitcast i16 [[B1]] to <16 x i1>
; CHECK-NEXT:    ret <16 x i1> [[BC3]]
;
  %bc1 = bitcast i16 %a to <16 x i1>
  %bc2 = bitcast i16 %b to <16 x i1>
  %or = xor <16 x i1> %bc1, %bc2
  ret <16 x i1> %or
}

; Test bitwise operations with integer vector to integer bitcast
define i16 @or_bitcast_v16i1_to_i16_constant(<16 x i1> %a) {
; CHECK-LABEL: @or_bitcast_v16i1_to_i16_constant(
; CHECK-NEXT:    [[BC2:%.*]] = bitcast <16 x i1> [[B:%.*]] to i16
; CHECK-NEXT:    [[OR:%.*]] = or i16 [[BC2]], 3
; CHECK-NEXT:    ret i16 [[OR]]
;
  %bc = bitcast <16 x i1> %a to i16
  %or = or i16 %bc, 3
  ret i16 %or
}

define i16 @or_bitcast_v16i1_to_i16(<16 x i1> %a, <16 x i1> %b) {
; CHECK-LABEL: @or_bitcast_v16i1_to_i16(
; CHECK-NEXT:    [[BC1:%.*]] = bitcast <16 x i1> [[A:%.*]] to i16
; CHECK-NEXT:    [[BC2:%.*]] = bitcast <16 x i1> [[B:%.*]] to i16
; CHECK-NEXT:    [[OR:%.*]] = or i16 [[BC1]], [[BC2]]
; CHECK-NEXT:    ret i16 [[OR]]
;
  %bc1 = bitcast <16 x i1> %a to i16
  %bc2 = bitcast <16 x i1> %b to i16
  %or = or i16 %bc1, %bc2
  ret i16 %or
}