commute.ll
26.2 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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -S -early-cse -earlycse-debug-hash | FileCheck %s
; RUN: opt < %s -S -basicaa -early-cse-memssa | FileCheck %s
define void @test1(float %A, float %B, float* %PA, float* %PB) {
; CHECK-LABEL: @test1(
; CHECK-NEXT: [[C:%.*]] = fadd float [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: store float [[C]], float* [[PA:%.*]]
; CHECK-NEXT: store float [[C]], float* [[PB:%.*]]
; CHECK-NEXT: ret void
;
%C = fadd float %A, %B
store float %C, float* %PA
%D = fadd float %B, %A
store float %D, float* %PB
ret void
}
define void @test2(float %A, float %B, i1* %PA, i1* %PB) {
; CHECK-LABEL: @test2(
; CHECK-NEXT: [[C:%.*]] = fcmp oeq float [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: store i1 [[C]], i1* [[PA:%.*]]
; CHECK-NEXT: store i1 [[C]], i1* [[PB:%.*]]
; CHECK-NEXT: ret void
;
%C = fcmp oeq float %A, %B
store i1 %C, i1* %PA
%D = fcmp oeq float %B, %A
store i1 %D, i1* %PB
ret void
}
define void @test3(float %A, float %B, i1* %PA, i1* %PB) {
; CHECK-LABEL: @test3(
; CHECK-NEXT: [[C:%.*]] = fcmp uge float [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: store i1 [[C]], i1* [[PA:%.*]]
; CHECK-NEXT: store i1 [[C]], i1* [[PB:%.*]]
; CHECK-NEXT: ret void
;
%C = fcmp uge float %A, %B
store i1 %C, i1* %PA
%D = fcmp ule float %B, %A
store i1 %D, i1* %PB
ret void
}
define void @test4(i32 %A, i32 %B, i1* %PA, i1* %PB) {
; CHECK-LABEL: @test4(
; CHECK-NEXT: [[C:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: store i1 [[C]], i1* [[PA:%.*]]
; CHECK-NEXT: store i1 [[C]], i1* [[PB:%.*]]
; CHECK-NEXT: ret void
;
%C = icmp eq i32 %A, %B
store i1 %C, i1* %PA
%D = icmp eq i32 %B, %A
store i1 %D, i1* %PB
ret void
}
define void @test5(i32 %A, i32 %B, i1* %PA, i1* %PB) {
; CHECK-LABEL: @test5(
; CHECK-NEXT: [[C:%.*]] = icmp sgt i32 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: store i1 [[C]], i1* [[PA:%.*]]
; CHECK-NEXT: store i1 [[C]], i1* [[PB:%.*]]
; CHECK-NEXT: ret void
;
%C = icmp sgt i32 %A, %B
store i1 %C, i1* %PA
%D = icmp slt i32 %B, %A
store i1 %D, i1* %PB
ret void
}
; Test degenerate case of commuted compare of identical comparands.
define void @test6(float %f, i1* %p1, i1* %p2) {
; CHECK-LABEL: @test6(
; CHECK-NEXT: [[C1:%.*]] = fcmp ult float [[F:%.*]], [[F]]
; CHECK-NEXT: store i1 [[C1]], i1* [[P1:%.*]]
; CHECK-NEXT: store i1 [[C1]], i1* [[P2:%.*]]
; CHECK-NEXT: ret void
;
%c1 = fcmp ult float %f, %f
%c2 = fcmp ugt float %f, %f
store i1 %c1, i1* %p1
store i1 %c2, i1* %p2
ret void
}
; Min/max operands may be commuted in the compare and select.
define i8 @smin_commute(i8 %a, i8 %b) {
; CHECK-LABEL: @smin_commute(
; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i8 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i8 [[B]], [[A]]
; CHECK-NEXT: [[M1:%.*]] = select i1 [[CMP1]], i8 [[A]], i8 [[B]]
; CHECK-NEXT: [[R:%.*]] = mul i8 [[M1]], [[M1]]
; CHECK-NEXT: ret i8 [[R]]
;
%cmp1 = icmp slt i8 %a, %b
%cmp2 = icmp slt i8 %b, %a
%m1 = select i1 %cmp1, i8 %a, i8 %b
%m2 = select i1 %cmp2, i8 %b, i8 %a
%r = mul i8 %m1, %m2
ret i8 %r
}
; Min/max can also have a swapped predicate and select operands.
define i1 @smin_swapped(i8 %a, i8 %b) {
; CHECK-LABEL: @smin_swapped(
; CHECK-NEXT: [[CMP1:%.*]] = icmp sgt i8 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i8 [[A]], [[B]]
; CHECK-NEXT: [[M1:%.*]] = select i1 [[CMP1]], i8 [[B]], i8 [[A]]
; CHECK-NEXT: ret i1 true
;
%cmp1 = icmp sgt i8 %a, %b
%cmp2 = icmp slt i8 %a, %b
%m1 = select i1 %cmp1, i8 %b, i8 %a
%m2 = select i1 %cmp2, i8 %a, i8 %b
%r = icmp eq i8 %m2, %m1
ret i1 %r
}
; Min/max can also have an inverted predicate and select operands.
define i1 @smin_inverted(i8 %a, i8 %b) {
; CHECK-LABEL: @smin_inverted(
; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i8 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[CMP2:%.*]] = xor i1 [[CMP1]], true
; CHECK-NEXT: [[M1:%.*]] = select i1 [[CMP1]], i8 [[A]], i8 [[B]]
; CHECK-NEXT: ret i1 true
;
%cmp1 = icmp slt i8 %a, %b
%cmp2 = xor i1 %cmp1, -1
%m1 = select i1 %cmp1, i8 %a, i8 %b
%m2 = select i1 %cmp2, i8 %b, i8 %a
%r = icmp eq i8 %m1, %m2
ret i1 %r
}
define i8 @smax_commute(i8 %a, i8 %b) {
; CHECK-LABEL: @smax_commute(
; CHECK-NEXT: [[CMP1:%.*]] = icmp sgt i8 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i8 [[B]], [[A]]
; CHECK-NEXT: [[M1:%.*]] = select i1 [[CMP1]], i8 [[A]], i8 [[B]]
; CHECK-NEXT: ret i8 0
;
%cmp1 = icmp sgt i8 %a, %b
%cmp2 = icmp sgt i8 %b, %a
%m1 = select i1 %cmp1, i8 %a, i8 %b
%m2 = select i1 %cmp2, i8 %b, i8 %a
%r = urem i8 %m2, %m1
ret i8 %r
}
define i8 @smax_swapped(i8 %a, i8 %b) {
; CHECK-LABEL: @smax_swapped(
; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i8 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i8 [[A]], [[B]]
; CHECK-NEXT: [[M1:%.*]] = select i1 [[CMP1]], i8 [[B]], i8 [[A]]
; CHECK-NEXT: ret i8 1
;
%cmp1 = icmp slt i8 %a, %b
%cmp2 = icmp sgt i8 %a, %b
%m1 = select i1 %cmp1, i8 %b, i8 %a
%m2 = select i1 %cmp2, i8 %a, i8 %b
%r = sdiv i8 %m1, %m2
ret i8 %r
}
define i1 @smax_inverted(i8 %a, i8 %b) {
; CHECK-LABEL: @smax_inverted(
; CHECK-NEXT: [[CMP1:%.*]] = icmp sgt i8 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[CMP2:%.*]] = xor i1 [[CMP1]], true
; CHECK-NEXT: [[M1:%.*]] = select i1 [[CMP1]], i8 [[A]], i8 [[B]]
; CHECK-NEXT: ret i1 true
;
%cmp1 = icmp sgt i8 %a, %b
%cmp2 = xor i1 %cmp1, -1
%m1 = select i1 %cmp1, i8 %a, i8 %b
%m2 = select i1 %cmp2, i8 %b, i8 %a
%r = icmp eq i8 %m1, %m2
ret i1 %r
}
define i8 @umin_commute(i8 %a, i8 %b) {
; CHECK-LABEL: @umin_commute(
; CHECK-NEXT: [[CMP1:%.*]] = icmp ult i8 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[CMP2:%.*]] = icmp ult i8 [[B]], [[A]]
; CHECK-NEXT: [[M1:%.*]] = select i1 [[CMP1]], i8 [[A]], i8 [[B]]
; CHECK-NEXT: ret i8 0
;
%cmp1 = icmp ult i8 %a, %b
%cmp2 = icmp ult i8 %b, %a
%m1 = select i1 %cmp1, i8 %a, i8 %b
%m2 = select i1 %cmp2, i8 %b, i8 %a
%r = sub i8 %m2, %m1
ret i8 %r
}
; Choose a vector type just to show that works.
define <2 x i8> @umin_swapped(<2 x i8> %a, <2 x i8> %b) {
; CHECK-LABEL: @umin_swapped(
; CHECK-NEXT: [[CMP1:%.*]] = icmp ugt <2 x i8> [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[CMP2:%.*]] = icmp ult <2 x i8> [[A]], [[B]]
; CHECK-NEXT: [[M1:%.*]] = select <2 x i1> [[CMP1]], <2 x i8> [[B]], <2 x i8> [[A]]
; CHECK-NEXT: ret <2 x i8> zeroinitializer
;
%cmp1 = icmp ugt <2 x i8> %a, %b
%cmp2 = icmp ult <2 x i8> %a, %b
%m1 = select <2 x i1> %cmp1, <2 x i8> %b, <2 x i8> %a
%m2 = select <2 x i1> %cmp2, <2 x i8> %a, <2 x i8> %b
%r = sub <2 x i8> %m2, %m1
ret <2 x i8> %r
}
define i1 @umin_inverted(i8 %a, i8 %b) {
; CHECK-LABEL: @umin_inverted(
; CHECK-NEXT: [[CMP1:%.*]] = icmp ult i8 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[CMP2:%.*]] = xor i1 [[CMP1]], true
; CHECK-NEXT: [[M1:%.*]] = select i1 [[CMP1]], i8 [[A]], i8 [[B]]
; CHECK-NEXT: ret i1 true
;
%cmp1 = icmp ult i8 %a, %b
%cmp2 = xor i1 %cmp1, -1
%m1 = select i1 %cmp1, i8 %a, i8 %b
%m2 = select i1 %cmp2, i8 %b, i8 %a
%r = icmp eq i8 %m1, %m2
ret i1 %r
}
define i8 @umax_commute(i8 %a, i8 %b) {
; CHECK-LABEL: @umax_commute(
; CHECK-NEXT: [[CMP1:%.*]] = icmp ugt i8 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[CMP2:%.*]] = icmp ugt i8 [[B]], [[A]]
; CHECK-NEXT: [[M1:%.*]] = select i1 [[CMP1]], i8 [[A]], i8 [[B]]
; CHECK-NEXT: ret i8 1
;
%cmp1 = icmp ugt i8 %a, %b
%cmp2 = icmp ugt i8 %b, %a
%m1 = select i1 %cmp1, i8 %a, i8 %b
%m2 = select i1 %cmp2, i8 %b, i8 %a
%r = udiv i8 %m1, %m2
ret i8 %r
}
define i8 @umax_swapped(i8 %a, i8 %b) {
; CHECK-LABEL: @umax_swapped(
; CHECK-NEXT: [[CMP1:%.*]] = icmp ult i8 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[CMP2:%.*]] = icmp ugt i8 [[A]], [[B]]
; CHECK-NEXT: [[M1:%.*]] = select i1 [[CMP1]], i8 [[B]], i8 [[A]]
; CHECK-NEXT: [[R:%.*]] = add i8 [[M1]], [[M1]]
; CHECK-NEXT: ret i8 [[R]]
;
%cmp1 = icmp ult i8 %a, %b
%cmp2 = icmp ugt i8 %a, %b
%m1 = select i1 %cmp1, i8 %b, i8 %a
%m2 = select i1 %cmp2, i8 %a, i8 %b
%r = add i8 %m2, %m1
ret i8 %r
}
define i1 @umax_inverted(i8 %a, i8 %b) {
; CHECK-LABEL: @umax_inverted(
; CHECK-NEXT: [[CMP1:%.*]] = icmp ugt i8 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[CMP2:%.*]] = xor i1 [[CMP1]], true
; CHECK-NEXT: [[M1:%.*]] = select i1 [[CMP1]], i8 [[A]], i8 [[B]]
; CHECK-NEXT: ret i1 true
;
%cmp1 = icmp ugt i8 %a, %b
%cmp2 = xor i1 %cmp1, -1
%m1 = select i1 %cmp1, i8 %a, i8 %b
%m2 = select i1 %cmp2, i8 %b, i8 %a
%r = icmp eq i8 %m1, %m2
ret i1 %r
}
; Min/max may exist with non-canonical operands. Value tracking can match those.
; But we do not use value tracking, so we expect instcombine will canonicalize
; this code to a form that allows CSE.
define i8 @smax_nsw(i8 %a, i8 %b) {
; CHECK-LABEL: @smax_nsw(
; CHECK-NEXT: [[SUB:%.*]] = sub nsw i8 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i8 [[A]], [[B]]
; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i8 [[SUB]], 0
; CHECK-NEXT: [[M1:%.*]] = select i1 [[CMP1]], i8 0, i8 [[SUB]]
; CHECK-NEXT: [[M2:%.*]] = select i1 [[CMP2]], i8 [[SUB]], i8 0
; CHECK-NEXT: [[R:%.*]] = sub i8 [[M2]], [[M1]]
; CHECK-NEXT: ret i8 [[R]]
;
%sub = sub nsw i8 %a, %b
%cmp1 = icmp slt i8 %a, %b
%cmp2 = icmp sgt i8 %sub, 0
%m1 = select i1 %cmp1, i8 0, i8 %sub
%m2 = select i1 %cmp2, i8 %sub, i8 0
%r = sub i8 %m2, %m1
ret i8 %r
}
; Abs/nabs may exist with non-canonical operands. Value tracking can match those.
; But we do not use value tracking, so we expect instcombine will canonicalize
; this code to a form that allows CSE.
define i8 @abs_swapped(i8 %a) {
; CHECK-LABEL: @abs_swapped(
; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[A:%.*]]
; CHECK-NEXT: [[CMP1:%.*]] = icmp sgt i8 [[A]], 0
; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i8 [[A]], 0
; CHECK-NEXT: [[M1:%.*]] = select i1 [[CMP1]], i8 [[A]], i8 [[NEG]]
; CHECK-NEXT: [[M2:%.*]] = select i1 [[CMP2]], i8 [[NEG]], i8 [[A]]
; CHECK-NEXT: [[R:%.*]] = or i8 [[M2]], [[M1]]
; CHECK-NEXT: ret i8 [[R]]
;
%neg = sub i8 0, %a
%cmp1 = icmp sgt i8 %a, 0
%cmp2 = icmp slt i8 %a, 0
%m1 = select i1 %cmp1, i8 %a, i8 %neg
%m2 = select i1 %cmp2, i8 %neg, i8 %a
%r = or i8 %m2, %m1
ret i8 %r
}
define i8 @abs_inverted(i8 %a) {
; CHECK-LABEL: @abs_inverted(
; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[A:%.*]]
; CHECK-NEXT: [[CMP1:%.*]] = icmp sgt i8 [[A]], 0
; CHECK-NEXT: [[CMP2:%.*]] = xor i1 [[CMP1]], true
; CHECK-NEXT: [[M1:%.*]] = select i1 [[CMP1]], i8 [[A]], i8 [[NEG]]
; CHECK-NEXT: ret i8 [[M1]]
;
%neg = sub i8 0, %a
%cmp1 = icmp sgt i8 %a, 0
%cmp2 = xor i1 %cmp1, -1
%m1 = select i1 %cmp1, i8 %a, i8 %neg
%m2 = select i1 %cmp2, i8 %neg, i8 %a
%r = or i8 %m2, %m1
ret i8 %r
}
; Abs/nabs may exist with non-canonical operands. Value tracking can match those.
; But we do not use value tracking, so we expect instcombine will canonicalize
; this code to a form that allows CSE.
define i8 @nabs_swapped(i8 %a) {
; CHECK-LABEL: @nabs_swapped(
; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[A:%.*]]
; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i8 [[A]], 0
; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i8 [[A]], 0
; CHECK-NEXT: [[M1:%.*]] = select i1 [[CMP1]], i8 [[A]], i8 [[NEG]]
; CHECK-NEXT: [[M2:%.*]] = select i1 [[CMP2]], i8 [[NEG]], i8 [[A]]
; CHECK-NEXT: [[R:%.*]] = xor i8 [[M2]], [[M1]]
; CHECK-NEXT: ret i8 [[R]]
;
%neg = sub i8 0, %a
%cmp1 = icmp slt i8 %a, 0
%cmp2 = icmp sgt i8 %a, 0
%m1 = select i1 %cmp1, i8 %a, i8 %neg
%m2 = select i1 %cmp2, i8 %neg, i8 %a
%r = xor i8 %m2, %m1
ret i8 %r
}
define i8 @nabs_inverted(i8 %a) {
; CHECK-LABEL: @nabs_inverted(
; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[A:%.*]]
; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i8 [[A]], 0
; CHECK-NEXT: [[CMP2:%.*]] = xor i1 [[CMP1]], true
; CHECK-NEXT: [[M1:%.*]] = select i1 [[CMP1]], i8 [[A]], i8 [[NEG]]
; CHECK-NEXT: ret i8 0
;
%neg = sub i8 0, %a
%cmp1 = icmp slt i8 %a, 0
%cmp2 = xor i1 %cmp1, -1
%m1 = select i1 %cmp1, i8 %a, i8 %neg
%m2 = select i1 %cmp2, i8 %neg, i8 %a
%r = xor i8 %m2, %m1
ret i8 %r
}
; Abs/nabs may exist with non-canonical operands. Value tracking can match those.
; But we do not use value tracking, so we expect instcombine will canonicalize
; this code to a form that allows CSE.
; compares are different.
define i8 @abs_different_constants(i8 %a) {
; CHECK-LABEL: @abs_different_constants(
; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[A:%.*]]
; CHECK-NEXT: [[CMP1:%.*]] = icmp sgt i8 [[A]], -1
; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i8 [[A]], 0
; CHECK-NEXT: [[M1:%.*]] = select i1 [[CMP1]], i8 [[A]], i8 [[NEG]]
; CHECK-NEXT: [[M2:%.*]] = select i1 [[CMP2]], i8 [[NEG]], i8 [[A]]
; CHECK-NEXT: [[R:%.*]] = or i8 [[M2]], [[M1]]
; CHECK-NEXT: ret i8 [[R]]
;
%neg = sub i8 0, %a
%cmp1 = icmp sgt i8 %a, -1
%cmp2 = icmp slt i8 %a, 0
%m1 = select i1 %cmp1, i8 %a, i8 %neg
%m2 = select i1 %cmp2, i8 %neg, i8 %a
%r = or i8 %m2, %m1
ret i8 %r
}
; Abs/nabs may exist with non-canonical operands. Value tracking can match those.
; But we do not use value tracking, so we expect instcombine will canonicalize
; this code to a form that allows CSE.
define i8 @nabs_different_constants(i8 %a) {
; CHECK-LABEL: @nabs_different_constants(
; CHECK-NEXT: [[NEG:%.*]] = sub i8 0, [[A:%.*]]
; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i8 [[A]], 0
; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i8 [[A]], -1
; CHECK-NEXT: [[M1:%.*]] = select i1 [[CMP1]], i8 [[A]], i8 [[NEG]]
; CHECK-NEXT: [[M2:%.*]] = select i1 [[CMP2]], i8 [[NEG]], i8 [[A]]
; CHECK-NEXT: [[R:%.*]] = xor i8 [[M2]], [[M1]]
; CHECK-NEXT: ret i8 [[R]]
;
%neg = sub i8 0, %a
%cmp1 = icmp slt i8 %a, 0
%cmp2 = icmp sgt i8 %a, -1
%m1 = select i1 %cmp1, i8 %a, i8 %neg
%m2 = select i1 %cmp2, i8 %neg, i8 %a
%r = xor i8 %m2, %m1
ret i8 %r
}
; https://bugs.llvm.org/show_bug.cgi?id=41101
; Detect equivalence of selects with commuted operands: 'not' cond.
define i32 @select_not_cond(i1 %cond, i32 %t, i32 %f) {
; CHECK-LABEL: @select_not_cond(
; CHECK-NEXT: [[NOT:%.*]] = xor i1 [[COND:%.*]], true
; CHECK-NEXT: [[M1:%.*]] = select i1 [[COND]], i32 [[T:%.*]], i32 [[F:%.*]]
; CHECK-NEXT: ret i32 0
;
%not = xor i1 %cond, -1
%m1 = select i1 %cond, i32 %t, i32 %f
%m2 = select i1 %not, i32 %f, i32 %t
%r = xor i32 %m2, %m1
ret i32 %r
}
; Detect equivalence of selects with commuted operands: 'not' cond with vector select.
define <2 x double> @select_not_cond_commute_vec(<2 x i1> %cond, <2 x double> %t, <2 x double> %f) {
; CHECK-LABEL: @select_not_cond_commute_vec(
; CHECK-NEXT: [[NOT:%.*]] = xor <2 x i1> [[COND:%.*]], <i1 true, i1 true>
; CHECK-NEXT: [[M1:%.*]] = select <2 x i1> [[COND]], <2 x double> [[T:%.*]], <2 x double> [[F:%.*]]
; CHECK-NEXT: ret <2 x double> <double 1.000000e+00, double 1.000000e+00>
;
%not = xor <2 x i1> %cond, <i1 -1, i1 -1>
%m1 = select <2 x i1> %cond, <2 x double> %t, <2 x double> %f
%m2 = select <2 x i1> %not, <2 x double> %f, <2 x double> %t
%r = fdiv nnan <2 x double> %m1, %m2
ret <2 x double> %r
}
; Negative test - select ops must be commuted.
define i32 @select_not_cond_wrong_select_ops(i1 %cond, i32 %t, i32 %f) {
; CHECK-LABEL: @select_not_cond_wrong_select_ops(
; CHECK-NEXT: [[NOT:%.*]] = xor i1 [[COND:%.*]], true
; CHECK-NEXT: [[M1:%.*]] = select i1 [[COND]], i32 [[T:%.*]], i32 [[F:%.*]]
; CHECK-NEXT: [[M2:%.*]] = select i1 [[NOT]], i32 [[T]], i32 [[F]]
; CHECK-NEXT: [[R:%.*]] = xor i32 [[M2]], [[M1]]
; CHECK-NEXT: ret i32 [[R]]
;
%not = xor i1 %cond, -1
%m1 = select i1 %cond, i32 %t, i32 %f
%m2 = select i1 %not, i32 %t, i32 %f
%r = xor i32 %m2, %m1
ret i32 %r
}
; Negative test - not a 'not'.
define i32 @select_not_cond_wrong_cond(i1 %cond, i32 %t, i32 %f) {
; CHECK-LABEL: @select_not_cond_wrong_cond(
; CHECK-NEXT: [[M1:%.*]] = select i1 [[COND:%.*]], i32 [[T:%.*]], i32 [[F:%.*]]
; CHECK-NEXT: [[M2:%.*]] = select i1 [[COND]], i32 [[F]], i32 [[T]]
; CHECK-NEXT: [[R:%.*]] = xor i32 [[M2]], [[M1]]
; CHECK-NEXT: ret i32 [[R]]
;
%not = xor i1 %cond, -2
%m1 = select i1 %cond, i32 %t, i32 %f
%m2 = select i1 %not, i32 %f, i32 %t
%r = xor i32 %m2, %m1
ret i32 %r
}
; Detect equivalence of selects with commuted operands: inverted pred with fcmps.
define i32 @select_invert_pred_cond(float %x, i32 %t, i32 %f) {
; CHECK-LABEL: @select_invert_pred_cond(
; CHECK-NEXT: [[COND:%.*]] = fcmp ueq float [[X:%.*]], 4.200000e+01
; CHECK-NEXT: [[INVCOND:%.*]] = fcmp one float [[X]], 4.200000e+01
; CHECK-NEXT: [[M1:%.*]] = select i1 [[COND]], i32 [[T:%.*]], i32 [[F:%.*]]
; CHECK-NEXT: ret i32 0
;
%cond = fcmp ueq float %x, 42.0
%invcond = fcmp one float %x, 42.0
%m1 = select i1 %cond, i32 %t, i32 %f
%m2 = select i1 %invcond, i32 %f, i32 %t
%r = xor i32 %m2, %m1
ret i32 %r
}
; Detect equivalence of selects with commuted operands: inverted pred with icmps and vectors.
define <2 x i32> @select_invert_pred_cond_commute_vec(<2 x i8> %x, <2 x i32> %t, <2 x i32> %f) {
; CHECK-LABEL: @select_invert_pred_cond_commute_vec(
; CHECK-NEXT: [[COND:%.*]] = icmp sgt <2 x i8> [[X:%.*]], <i8 42, i8 -1>
; CHECK-NEXT: [[INVCOND:%.*]] = icmp sle <2 x i8> [[X]], <i8 42, i8 -1>
; CHECK-NEXT: [[M1:%.*]] = select <2 x i1> [[COND]], <2 x i32> [[T:%.*]], <2 x i32> [[F:%.*]]
; CHECK-NEXT: ret <2 x i32> zeroinitializer
;
%cond = icmp sgt <2 x i8> %x, <i8 42, i8 -1>
%invcond = icmp sle <2 x i8> %x, <i8 42, i8 -1>
%m1 = select <2 x i1> %cond, <2 x i32> %t, <2 x i32> %f
%m2 = select <2 x i1> %invcond, <2 x i32> %f, <2 x i32> %t
%r = xor <2 x i32> %m1, %m2
ret <2 x i32> %r
}
; Negative test - select ops must be commuted.
define i32 @select_invert_pred_wrong_select_ops(float %x, i32 %t, i32 %f) {
; CHECK-LABEL: @select_invert_pred_wrong_select_ops(
; CHECK-NEXT: [[COND:%.*]] = fcmp ueq float [[X:%.*]], 4.200000e+01
; CHECK-NEXT: [[INVCOND:%.*]] = fcmp one float [[X]], 4.200000e+01
; CHECK-NEXT: [[M1:%.*]] = select i1 [[COND]], i32 [[F:%.*]], i32 [[T:%.*]]
; CHECK-NEXT: [[M2:%.*]] = select i1 [[INVCOND]], i32 [[F]], i32 [[T]]
; CHECK-NEXT: [[R:%.*]] = xor i32 [[M2]], [[M1]]
; CHECK-NEXT: ret i32 [[R]]
;
%cond = fcmp ueq float %x, 42.0
%invcond = fcmp one float %x, 42.0
%m1 = select i1 %cond, i32 %f, i32 %t
%m2 = select i1 %invcond, i32 %f, i32 %t
%r = xor i32 %m2, %m1
ret i32 %r
}
; Negative test - not an inverted predicate.
define i32 @select_invert_pred_wrong_cond(float %x, i32 %t, i32 %f) {
; CHECK-LABEL: @select_invert_pred_wrong_cond(
; CHECK-NEXT: [[COND:%.*]] = fcmp ueq float [[X:%.*]], 4.200000e+01
; CHECK-NEXT: [[INVCOND:%.*]] = fcmp une float [[X]], 4.200000e+01
; CHECK-NEXT: [[M1:%.*]] = select i1 [[COND]], i32 [[T:%.*]], i32 [[F:%.*]]
; CHECK-NEXT: [[M2:%.*]] = select i1 [[INVCOND]], i32 [[F]], i32 [[T]]
; CHECK-NEXT: [[R:%.*]] = xor i32 [[M2]], [[M1]]
; CHECK-NEXT: ret i32 [[R]]
;
%cond = fcmp ueq float %x, 42.0
%invcond = fcmp une float %x, 42.0
%m1 = select i1 %cond, i32 %t, i32 %f
%m2 = select i1 %invcond, i32 %f, i32 %t
%r = xor i32 %m2, %m1
ret i32 %r
}
; Negative test - cmp ops must match.
define i32 @select_invert_pred_wrong_cmp_ops(float %x, i32 %t, i32 %f) {
; CHECK-LABEL: @select_invert_pred_wrong_cmp_ops(
; CHECK-NEXT: [[COND:%.*]] = fcmp ueq float [[X:%.*]], 4.200000e+01
; CHECK-NEXT: [[INVCOND:%.*]] = fcmp one float [[X]], 4.300000e+01
; CHECK-NEXT: [[M1:%.*]] = select i1 [[COND]], i32 [[T:%.*]], i32 [[F:%.*]]
; CHECK-NEXT: [[M2:%.*]] = select i1 [[INVCOND]], i32 [[F]], i32 [[T]]
; CHECK-NEXT: [[R:%.*]] = xor i32 [[M2]], [[M1]]
; CHECK-NEXT: ret i32 [[R]]
;
%cond = fcmp ueq float %x, 42.0
%invcond = fcmp one float %x, 43.0
%m1 = select i1 %cond, i32 %t, i32 %f
%m2 = select i1 %invcond, i32 %f, i32 %t
%r = xor i32 %m2, %m1
ret i32 %r
}
; If we have both an inverted predicate and a 'not' op, recognize the double-negation.
define i32 @select_not_invert_pred_cond(i8 %x, i32 %t, i32 %f) {
; CHECK-LABEL: @select_not_invert_pred_cond(
; CHECK-NEXT: [[COND:%.*]] = icmp ugt i8 [[X:%.*]], 42
; CHECK-NEXT: [[INVCOND:%.*]] = icmp ule i8 [[X]], 42
; CHECK-NEXT: [[NOT:%.*]] = xor i1 [[INVCOND]], true
; CHECK-NEXT: [[M1:%.*]] = select i1 [[COND]], i32 [[T:%.*]], i32 [[F:%.*]]
; CHECK-NEXT: ret i32 0
;
%cond = icmp ugt i8 %x, 42
%invcond = icmp ule i8 %x, 42
%not = xor i1 %invcond, -1
%m1 = select i1 %cond, i32 %t, i32 %f
%m2 = select i1 %not, i32 %t, i32 %f
%r = sub i32 %m1, %m2
ret i32 %r
}
; If we have both an inverted predicate and a 'not' op, recognize the double-negation.
define i32 @select_not_invert_pred_cond_commute(i8 %x, i8 %y, i32 %t, i32 %f) {
; CHECK-LABEL: @select_not_invert_pred_cond_commute(
; CHECK-NEXT: [[INVCOND:%.*]] = icmp ule i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[NOT:%.*]] = xor i1 [[INVCOND]], true
; CHECK-NEXT: [[M2:%.*]] = select i1 [[NOT]], i32 [[T:%.*]], i32 [[F:%.*]]
; CHECK-NEXT: [[COND:%.*]] = icmp ugt i8 [[X]], [[Y]]
; CHECK-NEXT: ret i32 0
;
%invcond = icmp ule i8 %x, %y
%not = xor i1 %invcond, -1
%m2 = select i1 %not, i32 %t, i32 %f
%cond = icmp ugt i8 %x, %y
%m1 = select i1 %cond, i32 %t, i32 %f
%r = sub i32 %m2, %m1
ret i32 %r
}
; Negative test - not an inverted predicate.
define i32 @select_not_invert_pred_cond_wrong_pred(i8 %x, i8 %y, i32 %t, i32 %f) {
; CHECK-LABEL: @select_not_invert_pred_cond_wrong_pred(
; CHECK-NEXT: [[INVCOND:%.*]] = icmp ult i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[NOT:%.*]] = xor i1 [[INVCOND]], true
; CHECK-NEXT: [[M2:%.*]] = select i1 [[NOT]], i32 [[T:%.*]], i32 [[F:%.*]]
; CHECK-NEXT: [[COND:%.*]] = icmp ugt i8 [[X]], [[Y]]
; CHECK-NEXT: [[M1:%.*]] = select i1 [[COND]], i32 [[T]], i32 [[F]]
; CHECK-NEXT: [[R:%.*]] = sub i32 [[M2]], [[M1]]
; CHECK-NEXT: ret i32 [[R]]
;
%invcond = icmp ult i8 %x, %y
%not = xor i1 %invcond, -1
%m2 = select i1 %not, i32 %t, i32 %f
%cond = icmp ugt i8 %x, %y
%m1 = select i1 %cond, i32 %t, i32 %f
%r = sub i32 %m2, %m1
ret i32 %r
}
; Negative test - cmp ops must match.
define i32 @select_not_invert_pred_cond_wrong_cmp_op(i8 %x, i8 %y, i32 %t, i32 %f) {
; CHECK-LABEL: @select_not_invert_pred_cond_wrong_cmp_op(
; CHECK-NEXT: [[INVCOND:%.*]] = icmp ule i8 [[X:%.*]], 42
; CHECK-NEXT: [[NOT:%.*]] = xor i1 [[INVCOND]], true
; CHECK-NEXT: [[M2:%.*]] = select i1 [[NOT]], i32 [[T:%.*]], i32 [[F:%.*]]
; CHECK-NEXT: [[COND:%.*]] = icmp ugt i8 [[X]], [[Y:%.*]]
; CHECK-NEXT: [[M1:%.*]] = select i1 [[COND]], i32 [[T]], i32 [[F]]
; CHECK-NEXT: [[R:%.*]] = sub i32 [[M2]], [[M1]]
; CHECK-NEXT: ret i32 [[R]]
;
%invcond = icmp ule i8 %x, 42
%not = xor i1 %invcond, -1
%m2 = select i1 %not, i32 %t, i32 %f
%cond = icmp ugt i8 %x, %y
%m1 = select i1 %cond, i32 %t, i32 %f
%r = sub i32 %m2, %m1
ret i32 %r
}
; Negative test - select ops must be same (and not commuted).
define i32 @select_not_invert_pred_cond_wrong_select_op(i8 %x, i8 %y, i32 %t, i32 %f) {
; CHECK-LABEL: @select_not_invert_pred_cond_wrong_select_op(
; CHECK-NEXT: [[INVCOND:%.*]] = icmp ule i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[NOT:%.*]] = xor i1 [[INVCOND]], true
; CHECK-NEXT: [[M2:%.*]] = select i1 [[NOT]], i32 [[T:%.*]], i32 [[F:%.*]]
; CHECK-NEXT: [[COND:%.*]] = icmp ugt i8 [[X]], [[Y]]
; CHECK-NEXT: [[M1:%.*]] = select i1 [[COND]], i32 [[F]], i32 [[T]]
; CHECK-NEXT: [[R:%.*]] = sub i32 [[M2]], [[M1]]
; CHECK-NEXT: ret i32 [[R]]
;
%invcond = icmp ule i8 %x, %y
%not = xor i1 %invcond, -1
%m2 = select i1 %not, i32 %t, i32 %f
%cond = icmp ugt i8 %x, %y
%m1 = select i1 %cond, i32 %f, i32 %t
%r = sub i32 %m2, %m1
ret i32 %r
}
; This test is a reproducer for a bug involving inverted min/max selects
; hashing differently but comparing as equal. It exhibits such a pair of
; values, and we run this test with -earlycse-debug-hash which would catch
; the disagreement and fail if it regressed. This test also includes a
; negation of each negation to check for the same issue one level deeper.
define void @not_not_min(i32* %px, i32* %py, i32* %pout) {
; CHECK-LABEL: @not_not_min(
; CHECK-NEXT: [[X:%.*]] = load volatile i32, i32* [[PX:%.*]]
; CHECK-NEXT: [[Y:%.*]] = load volatile i32, i32* [[PY:%.*]]
; CHECK-NEXT: [[CMPA:%.*]] = icmp slt i32 [[X]], [[Y]]
; CHECK-NEXT: [[CMPB:%.*]] = xor i1 [[CMPA]], true
; CHECK-NEXT: [[RA:%.*]] = select i1 [[CMPA]], i32 [[X]], i32 [[Y]]
; CHECK-NEXT: store volatile i32 [[RA]], i32* [[POUT:%.*]]
; CHECK-NEXT: store volatile i32 [[RA]], i32* [[POUT]]
; CHECK-NEXT: store volatile i32 [[RA]], i32* [[POUT]]
; CHECK-NEXT: ret void
;
%x = load volatile i32, i32* %px
%y = load volatile i32, i32* %py
%cmpa = icmp slt i32 %x, %y
%cmpb = xor i1 %cmpa, -1
%cmpc = xor i1 %cmpb, -1
%ra = select i1 %cmpa, i32 %x, i32 %y
%rb = select i1 %cmpb, i32 %y, i32 %x
%rc = select i1 %cmpc, i32 %x, i32 %y
store volatile i32 %ra, i32* %pout
store volatile i32 %rb, i32* %pout
store volatile i32 %rc, i32* %pout
ret void
}
; This would cause an assert/crash because we matched
; a ValueTracking select pattern that required 'nsw'
; on an operand, but we remove that flag as part of
; CSE matching/hashing.
define void @PR41083_1(i32 %span_left, i32 %clip_left) {
; CHECK-LABEL: @PR41083_1(
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[CLIP_LEFT:%.*]], [[SPAN_LEFT:%.*]]
; CHECK-NEXT: [[SUB:%.*]] = sub i32 [[CLIP_LEFT]], [[SPAN_LEFT]]
; CHECK-NEXT: [[COND:%.*]] = select i1 [[CMP]], i32 [[SUB]], i32 0
; CHECK-NEXT: ret void
;
%cmp = icmp sgt i32 %clip_left, %span_left
%sub = sub nsw i32 %clip_left, %span_left
%cond = select i1 %cmp, i32 %sub, i32 0
%cmp83292 = icmp slt i32 %cond, undef
%sub2 = sub i32 %clip_left, %span_left
%sel2 = select i1 %cmp, i32 %sub2, i32 0
ret void
}
; This would cause an assert/crash because we matched
; a ValueTracking select pattern that required 'nsw'
; on an operand, but we remove that flag as part of
; CSE matching/hashing.
define i32 @PR41083_2(i32 %p) {
; CHECK-LABEL: @PR41083_2(
; CHECK-NEXT: [[S:%.*]] = sub i32 0, [[P:%.*]]
; CHECK-NEXT: [[A:%.*]] = ashr exact i32 [[S]], 2
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 0, [[A]]
; CHECK-NEXT: [[SUB:%.*]] = sub i32 0, [[A]]
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32 [[SUB]], i32 0
; CHECK-NEXT: [[M:%.*]] = mul i32 [[SEL]], [[SUB]]
; CHECK-NEXT: ret i32 [[M]]
;
%s = sub i32 0, %p
%a = ashr exact i32 %s, 2
%cmp = icmp sgt i32 0, %a
%sub = sub nsw i32 0, %a
%sel = select i1 %cmp, i32 %sub, i32 0
%s2 = sub i32 0, %a
%m = mul i32 %sel, %s2
ret i32 %m
}