fabs.ll
28.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
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -mtriple=x86_64-unknown-linux-gnu < %s -instcombine -S | FileCheck %s
; Make sure libcalls are replaced with intrinsic calls.
declare float @llvm.fabs.f32(float)
declare <2 x float> @llvm.fabs.v2f32(<2 x float>)
declare double @llvm.fabs.f64(double)
declare fp128 @llvm.fabs.f128(fp128)
declare float @fabsf(float)
declare double @fabs(double)
declare fp128 @fabsl(fp128)
declare float @llvm.fma.f32(float, float, float)
declare float @llvm.fmuladd.f32(float, float, float)
declare void @use(float)
define float @replace_fabs_call_f32(float %x) {
; CHECK-LABEL: @replace_fabs_call_f32(
; CHECK-NEXT: [[FABSF:%.*]] = call float @llvm.fabs.f32(float [[X:%.*]])
; CHECK-NEXT: ret float [[FABSF]]
;
%fabsf = tail call float @fabsf(float %x)
ret float %fabsf
}
define double @replace_fabs_call_f64(double %x) {
; CHECK-LABEL: @replace_fabs_call_f64(
; CHECK-NEXT: [[FABS:%.*]] = call double @llvm.fabs.f64(double [[X:%.*]])
; CHECK-NEXT: ret double [[FABS]]
;
%fabs = tail call double @fabs(double %x)
ret double %fabs
}
define fp128 @replace_fabs_call_f128(fp128 %x) {
; CHECK-LABEL: @replace_fabs_call_f128(
; CHECK-NEXT: [[FABSL:%.*]] = call fp128 @llvm.fabs.f128(fp128 [[X:%.*]])
; CHECK-NEXT: ret fp128 [[FABSL]]
;
%fabsl = tail call fp128 @fabsl(fp128 %x)
ret fp128 %fabsl
}
; Make sure fast math flags are preserved when replacing the libcall.
define float @fmf_replace_fabs_call_f32(float %x) {
; CHECK-LABEL: @fmf_replace_fabs_call_f32(
; CHECK-NEXT: [[FABSF:%.*]] = call nnan float @llvm.fabs.f32(float [[X:%.*]])
; CHECK-NEXT: ret float [[FABSF]]
;
%fabsf = tail call nnan float @fabsf(float %x)
ret float %fabsf
}
; Make sure all intrinsic calls are eliminated when the input is known
; positive.
; The fabs cannot be eliminated because %x may be a NaN
define float @square_fabs_intrinsic_f32(float %x) {
; CHECK-LABEL: @square_fabs_intrinsic_f32(
; CHECK-NEXT: [[MUL:%.*]] = fmul float [[X:%.*]], [[X]]
; CHECK-NEXT: [[FABSF:%.*]] = tail call float @llvm.fabs.f32(float [[MUL]])
; CHECK-NEXT: ret float [[FABSF]]
;
%mul = fmul float %x, %x
%fabsf = tail call float @llvm.fabs.f32(float %mul)
ret float %fabsf
}
define double @square_fabs_intrinsic_f64(double %x) {
; CHECK-LABEL: @square_fabs_intrinsic_f64(
; CHECK-NEXT: [[MUL:%.*]] = fmul double [[X:%.*]], [[X]]
; CHECK-NEXT: [[FABS:%.*]] = tail call double @llvm.fabs.f64(double [[MUL]])
; CHECK-NEXT: ret double [[FABS]]
;
%mul = fmul double %x, %x
%fabs = tail call double @llvm.fabs.f64(double %mul)
ret double %fabs
}
define fp128 @square_fabs_intrinsic_f128(fp128 %x) {
; CHECK-LABEL: @square_fabs_intrinsic_f128(
; CHECK-NEXT: [[MUL:%.*]] = fmul fp128 [[X:%.*]], [[X]]
; CHECK-NEXT: [[FABSL:%.*]] = tail call fp128 @llvm.fabs.f128(fp128 [[MUL]])
; CHECK-NEXT: ret fp128 [[FABSL]]
;
%mul = fmul fp128 %x, %x
%fabsl = tail call fp128 @llvm.fabs.f128(fp128 %mul)
ret fp128 %fabsl
}
define float @square_nnan_fabs_intrinsic_f32(float %x) {
; CHECK-LABEL: @square_nnan_fabs_intrinsic_f32(
; CHECK-NEXT: [[MUL:%.*]] = fmul nnan float [[X:%.*]], [[X]]
; CHECK-NEXT: ret float [[MUL]]
;
%mul = fmul nnan float %x, %x
%fabsf = call float @llvm.fabs.f32(float %mul)
ret float %fabsf
}
; Shrinking a library call to a smaller type should not be inhibited by nor inhibit the square optimization.
define float @square_fabs_shrink_call1(float %x) {
; CHECK-LABEL: @square_fabs_shrink_call1(
; CHECK-NEXT: [[TMP1:%.*]] = fmul float [[X:%.*]], [[X]]
; CHECK-NEXT: [[TRUNC:%.*]] = call float @llvm.fabs.f32(float [[TMP1]])
; CHECK-NEXT: ret float [[TRUNC]]
;
%ext = fpext float %x to double
%sq = fmul double %ext, %ext
%fabs = call double @fabs(double %sq)
%trunc = fptrunc double %fabs to float
ret float %trunc
}
define float @square_fabs_shrink_call2(float %x) {
; CHECK-LABEL: @square_fabs_shrink_call2(
; CHECK-NEXT: [[SQ:%.*]] = fmul float [[X:%.*]], [[X]]
; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.fabs.f32(float [[SQ]])
; CHECK-NEXT: ret float [[TMP1]]
;
%sq = fmul float %x, %x
%ext = fpext float %sq to double
%fabs = call double @fabs(double %ext)
%trunc = fptrunc double %fabs to float
ret float %trunc
}
define float @fabs_select_constant_negative_positive(i32 %c) {
; CHECK-LABEL: @fabs_select_constant_negative_positive(
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[C:%.*]], 0
; CHECK-NEXT: [[FABS:%.*]] = select i1 [[CMP]], float 1.000000e+00, float 2.000000e+00
; CHECK-NEXT: ret float [[FABS]]
;
%cmp = icmp eq i32 %c, 0
%select = select i1 %cmp, float -1.0, float 2.0
%fabs = call float @llvm.fabs.f32(float %select)
ret float %fabs
}
define float @fabs_select_constant_positive_negative(i32 %c) {
; CHECK-LABEL: @fabs_select_constant_positive_negative(
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[C:%.*]], 0
; CHECK-NEXT: [[FABS:%.*]] = select i1 [[CMP]], float 1.000000e+00, float 2.000000e+00
; CHECK-NEXT: ret float [[FABS]]
;
%cmp = icmp eq i32 %c, 0
%select = select i1 %cmp, float 1.0, float -2.0
%fabs = call float @llvm.fabs.f32(float %select)
ret float %fabs
}
define float @fabs_select_constant_negative_negative(i32 %c) {
; CHECK-LABEL: @fabs_select_constant_negative_negative(
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[C:%.*]], 0
; CHECK-NEXT: [[FABS:%.*]] = select i1 [[CMP]], float 1.000000e+00, float 2.000000e+00
; CHECK-NEXT: ret float [[FABS]]
;
%cmp = icmp eq i32 %c, 0
%select = select i1 %cmp, float -1.0, float -2.0
%fabs = call float @llvm.fabs.f32(float %select)
ret float %fabs
}
define float @fabs_select_constant_neg0(i32 %c) {
; CHECK-LABEL: @fabs_select_constant_neg0(
; CHECK-NEXT: ret float 0.000000e+00
;
%cmp = icmp eq i32 %c, 0
%select = select i1 %cmp, float -0.0, float 0.0
%fabs = call float @llvm.fabs.f32(float %select)
ret float %fabs
}
define float @fabs_select_var_constant_negative(i32 %c, float %x) {
; CHECK-LABEL: @fabs_select_var_constant_negative(
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[C:%.*]], 0
; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], float [[X:%.*]], float -1.000000e+00
; CHECK-NEXT: [[FABS:%.*]] = call float @llvm.fabs.f32(float [[SELECT]])
; CHECK-NEXT: ret float [[FABS]]
;
%cmp = icmp eq i32 %c, 0
%select = select i1 %cmp, float %x, float -1.0
%fabs = call float @llvm.fabs.f32(float %select)
ret float %fabs
}
; The fabs cannot be eliminated because %x may be a NaN
define float @square_fma_fabs_intrinsic_f32(float %x) {
; CHECK-LABEL: @square_fma_fabs_intrinsic_f32(
; CHECK-NEXT: [[FMA:%.*]] = call float @llvm.fma.f32(float [[X:%.*]], float [[X]], float 1.000000e+00)
; CHECK-NEXT: [[FABSF:%.*]] = call float @llvm.fabs.f32(float [[FMA]])
; CHECK-NEXT: ret float [[FABSF]]
;
%fma = call float @llvm.fma.f32(float %x, float %x, float 1.0)
%fabsf = call float @llvm.fabs.f32(float %fma)
ret float %fabsf
}
; The fabs cannot be eliminated because %x may be a NaN
define float @square_nnan_fma_fabs_intrinsic_f32(float %x) {
; CHECK-LABEL: @square_nnan_fma_fabs_intrinsic_f32(
; CHECK-NEXT: [[FMA:%.*]] = call nnan float @llvm.fma.f32(float [[X:%.*]], float [[X]], float 1.000000e+00)
; CHECK-NEXT: ret float [[FMA]]
;
%fma = call nnan float @llvm.fma.f32(float %x, float %x, float 1.0)
%fabsf = call float @llvm.fabs.f32(float %fma)
ret float %fabsf
}
define float @square_fmuladd_fabs_intrinsic_f32(float %x) {
; CHECK-LABEL: @square_fmuladd_fabs_intrinsic_f32(
; CHECK-NEXT: [[FMULADD:%.*]] = call float @llvm.fmuladd.f32(float [[X:%.*]], float [[X]], float 1.000000e+00)
; CHECK-NEXT: [[FABSF:%.*]] = call float @llvm.fabs.f32(float [[FMULADD]])
; CHECK-NEXT: ret float [[FABSF]]
;
%fmuladd = call float @llvm.fmuladd.f32(float %x, float %x, float 1.0)
%fabsf = call float @llvm.fabs.f32(float %fmuladd)
ret float %fabsf
}
define float @square_nnan_fmuladd_fabs_intrinsic_f32(float %x) {
; CHECK-LABEL: @square_nnan_fmuladd_fabs_intrinsic_f32(
; CHECK-NEXT: [[FMULADD:%.*]] = call nnan float @llvm.fmuladd.f32(float [[X:%.*]], float [[X]], float 1.000000e+00)
; CHECK-NEXT: ret float [[FMULADD]]
;
%fmuladd = call nnan float @llvm.fmuladd.f32(float %x, float %x, float 1.0)
%fabsf = call float @llvm.fabs.f32(float %fmuladd)
ret float %fabsf
}
; Don't introduce a second fpext
define double @multi_use_fabs_fpext(float %x) {
; CHECK-LABEL: @multi_use_fabs_fpext(
; CHECK-NEXT: [[FPEXT:%.*]] = fpext float [[X:%.*]] to double
; CHECK-NEXT: [[FABS:%.*]] = call double @llvm.fabs.f64(double [[FPEXT]])
; CHECK-NEXT: store volatile double [[FPEXT]], double* undef, align 8
; CHECK-NEXT: ret double [[FABS]]
;
%fpext = fpext float %x to double
%fabs = call double @llvm.fabs.f64(double %fpext)
store volatile double %fpext, double* undef
ret double %fabs
}
; Negative test for the fabs folds below: we require nnan, so
; we won't always clear the sign bit of a NaN value.
define double @select_fcmp_ole_zero(double %x) {
; CHECK-LABEL: @select_fcmp_ole_zero(
; CHECK-NEXT: [[LEZERO:%.*]] = fcmp ole double [[X:%.*]], 0.000000e+00
; CHECK-NEXT: [[NEGX:%.*]] = fsub double 0.000000e+00, [[X]]
; CHECK-NEXT: [[FABS:%.*]] = select i1 [[LEZERO]], double [[NEGX]], double [[X]]
; CHECK-NEXT: ret double [[FABS]]
;
%lezero = fcmp ole double %x, 0.0
%negx = fsub double 0.0, %x
%fabs = select i1 %lezero, double %negx, double %x
ret double %fabs
}
; X <= 0.0 ? (0.0 - X) : X --> fabs(X)
define double @select_fcmp_nnan_ole_zero(double %x) {
; CHECK-LABEL: @select_fcmp_nnan_ole_zero(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan double @llvm.fabs.f64(double [[X:%.*]])
; CHECK-NEXT: ret double [[TMP1]]
;
%lezero = fcmp ole double %x, 0.0
%negx = fsub nnan double 0.0, %x
%fabs = select i1 %lezero, double %negx, double %x
ret double %fabs
}
; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
define double @select_fcmp_nnan_ule_zero(double %x) {
; CHECK-LABEL: @select_fcmp_nnan_ule_zero(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan double @llvm.fabs.f64(double [[X:%.*]])
; CHECK-NEXT: ret double [[TMP1]]
;
%lezero = fcmp ule double %x, 0.0
%negx = fsub nnan double 0.0, %x
%fabs = select i1 %lezero, double %negx, double %x
ret double %fabs
}
; Negative test - wrong predicate.
define double @select_fcmp_nnan_olt_zero(double %x) {
; CHECK-LABEL: @select_fcmp_nnan_olt_zero(
; CHECK-NEXT: [[LEZERO:%.*]] = fcmp olt double [[X:%.*]], 0.000000e+00
; CHECK-NEXT: [[NEGX:%.*]] = fsub nnan double 0.000000e+00, [[X]]
; CHECK-NEXT: [[FABS:%.*]] = select i1 [[LEZERO]], double [[NEGX]], double [[X]]
; CHECK-NEXT: ret double [[FABS]]
;
%lezero = fcmp olt double %x, 0.0
%negx = fsub nnan double 0.0, %x
%fabs = select i1 %lezero, double %negx, double %x
ret double %fabs
}
; X <= -0.0 ? (0.0 - X) : X --> fabs(X)
define <2 x float> @select_fcmp_nnan_ole_negzero(<2 x float> %x) {
; CHECK-LABEL: @select_fcmp_nnan_ole_negzero(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan <2 x float> @llvm.fabs.v2f32(<2 x float> [[X:%.*]])
; CHECK-NEXT: ret <2 x float> [[TMP1]]
;
%lezero = fcmp ole <2 x float> %x, <float -0.0, float -0.0>
%negx = fsub nnan <2 x float> <float 0.0, float undef>, %x
%fabs = select <2 x i1> %lezero, <2 x float> %negx, <2 x float> %x
ret <2 x float> %fabs
}
; X > 0.0 ? X : (0.0 - X) --> fabs(X)
define fp128 @select_fcmp_nnan_ogt_zero(fp128 %x) {
; CHECK-LABEL: @select_fcmp_nnan_ogt_zero(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan fp128 @llvm.fabs.f128(fp128 [[X:%.*]])
; CHECK-NEXT: ret fp128 [[TMP1]]
;
%gtzero = fcmp ogt fp128 %x, zeroinitializer
%negx = fsub nnan fp128 zeroinitializer, %x
%fabs = select i1 %gtzero, fp128 %x, fp128 %negx
ret fp128 %fabs
}
; X > -0.0 ? X : (0.0 - X) --> fabs(X)
define half @select_fcmp_nnan_ogt_negzero(half %x) {
; CHECK-LABEL: @select_fcmp_nnan_ogt_negzero(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan half @llvm.fabs.f16(half [[X:%.*]])
; CHECK-NEXT: ret half [[TMP1]]
;
%gtzero = fcmp ogt half %x, -0.0
%negx = fsub nnan half 0.0, %x
%fabs = select i1 %gtzero, half %x, half %negx
ret half %fabs
}
; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
define half @select_fcmp_nnan_ugt_negzero(half %x) {
; CHECK-LABEL: @select_fcmp_nnan_ugt_negzero(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan half @llvm.fabs.f16(half [[X:%.*]])
; CHECK-NEXT: ret half [[TMP1]]
;
%gtzero = fcmp ugt half %x, -0.0
%negx = fsub nnan half 0.0, %x
%fabs = select i1 %gtzero, half %x, half %negx
ret half %fabs
}
; Negative test - wrong predicate.
define half @select_fcmp_nnan_oge_negzero(half %x) {
; CHECK-LABEL: @select_fcmp_nnan_oge_negzero(
; CHECK-NEXT: [[GTZERO:%.*]] = fcmp oge half [[X:%.*]], 0xH0000
; CHECK-NEXT: [[NEGX:%.*]] = fsub nnan half 0xH0000, [[X]]
; CHECK-NEXT: [[FABS:%.*]] = select i1 [[GTZERO]], half [[X]], half [[NEGX]]
; CHECK-NEXT: ret half [[FABS]]
;
%gtzero = fcmp oge half %x, -0.0
%negx = fsub nnan half 0.0, %x
%fabs = select i1 %gtzero, half %x, half %negx
ret half %fabs
}
; X < 0.0 ? -X : X --> fabs(X)
define double @select_fcmp_nnan_nsz_olt_zero(double %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_olt_zero(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz double @llvm.fabs.f64(double [[X:%.*]])
; CHECK-NEXT: ret double [[TMP1]]
;
%ltzero = fcmp olt double %x, 0.0
%negx = fsub nnan nsz double -0.0, %x
%fabs = select i1 %ltzero, double %negx, double %x
ret double %fabs
}
; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
define double @select_fcmp_nnan_nsz_ult_zero(double %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_ult_zero(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz double @llvm.fabs.f64(double [[X:%.*]])
; CHECK-NEXT: ret double [[TMP1]]
;
%ltzero = fcmp ult double %x, 0.0
%negx = fsub nnan nsz double -0.0, %x
%fabs = select i1 %ltzero, double %negx, double %x
ret double %fabs
}
define double @select_fcmp_nnan_nsz_olt_zero_unary_fneg(double %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_olt_zero_unary_fneg(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz double @llvm.fabs.f64(double [[X:%.*]])
; CHECK-NEXT: ret double [[TMP1]]
;
%ltzero = fcmp olt double %x, 0.0
%negx = fneg nnan nsz double %x
%fabs = select i1 %ltzero, double %negx, double %x
ret double %fabs
}
; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
define double @select_fcmp_nnan_nsz_ult_zero_unary_fneg(double %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_ult_zero_unary_fneg(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz double @llvm.fabs.f64(double [[X:%.*]])
; CHECK-NEXT: ret double [[TMP1]]
;
%ltzero = fcmp ult double %x, 0.0
%negx = fneg nnan nsz double %x
%fabs = select i1 %ltzero, double %negx, double %x
ret double %fabs
}
; X < -0.0 ? -X : X --> fabs(X)
define float @select_fcmp_nnan_nsz_olt_negzero(float %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_olt_negzero(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan ninf nsz float @llvm.fabs.f32(float [[X:%.*]])
; CHECK-NEXT: ret float [[TMP1]]
;
%ltzero = fcmp olt float %x, -0.0
%negx = fsub nnan ninf nsz float -0.0, %x
%fabs = select i1 %ltzero, float %negx, float %x
ret float %fabs
}
; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
define float @select_fcmp_nnan_nsz_ult_negzero(float %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_ult_negzero(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan ninf nsz float @llvm.fabs.f32(float [[X:%.*]])
; CHECK-NEXT: ret float [[TMP1]]
;
%ltzero = fcmp ult float %x, -0.0
%negx = fsub nnan ninf nsz float -0.0, %x
%fabs = select i1 %ltzero, float %negx, float %x
ret float %fabs
}
define float @select_fcmp_nnan_nsz_olt_negzero_unary_fneg(float %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_olt_negzero_unary_fneg(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan ninf nsz float @llvm.fabs.f32(float [[X:%.*]])
; CHECK-NEXT: ret float [[TMP1]]
;
%ltzero = fcmp olt float %x, -0.0
%negx = fneg nnan ninf nsz float %x
%fabs = select i1 %ltzero, float %negx, float %x
ret float %fabs
}
; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
define float @select_fcmp_nnan_nsz_ult_negzero_unary_fneg(float %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_ult_negzero_unary_fneg(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan ninf nsz float @llvm.fabs.f32(float [[X:%.*]])
; CHECK-NEXT: ret float [[TMP1]]
;
%ltzero = fcmp ult float %x, -0.0
%negx = fneg nnan ninf nsz float %x
%fabs = select i1 %ltzero, float %negx, float %x
ret float %fabs
}
; X <= 0.0 ? -X : X --> fabs(X)
define double @select_fcmp_nnan_nsz_ole_zero(double %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_ole_zero(
; CHECK-NEXT: [[TMP1:%.*]] = call fast double @llvm.fabs.f64(double [[X:%.*]])
; CHECK-NEXT: ret double [[TMP1]]
;
%lezero = fcmp ole double %x, 0.0
%negx = fsub fast double -0.0, %x
%fabs = select i1 %lezero, double %negx, double %x
ret double %fabs
}
; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
define double @select_fcmp_nnan_nsz_ule_zero(double %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_ule_zero(
; CHECK-NEXT: [[TMP1:%.*]] = call fast double @llvm.fabs.f64(double [[X:%.*]])
; CHECK-NEXT: ret double [[TMP1]]
;
%lezero = fcmp ule double %x, 0.0
%negx = fsub fast double -0.0, %x
%fabs = select i1 %lezero, double %negx, double %x
ret double %fabs
}
define double @select_fcmp_nnan_nsz_ole_zero_unary_fneg(double %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_ole_zero_unary_fneg(
; CHECK-NEXT: [[TMP1:%.*]] = call fast double @llvm.fabs.f64(double [[X:%.*]])
; CHECK-NEXT: ret double [[TMP1]]
;
%lezero = fcmp ole double %x, 0.0
%negx = fneg fast double %x
%fabs = select i1 %lezero, double %negx, double %x
ret double %fabs
}
; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
define double @select_fcmp_nnan_nsz_ule_zero_unary_fneg(double %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_ule_zero_unary_fneg(
; CHECK-NEXT: [[TMP1:%.*]] = call fast double @llvm.fabs.f64(double [[X:%.*]])
; CHECK-NEXT: ret double [[TMP1]]
;
%lezero = fcmp ule double %x, 0.0
%negx = fneg fast double %x
%fabs = select i1 %lezero, double %negx, double %x
ret double %fabs
}
; X <= -0.0 ? -X : X --> fabs(X)
define float @select_fcmp_nnan_nsz_ole_negzero(float %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_ole_negzero(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz float @llvm.fabs.f32(float [[X:%.*]])
; CHECK-NEXT: ret float [[TMP1]]
;
%lezero = fcmp ole float %x, -0.0
%negx = fsub nnan nsz float -0.0, %x
%fabs = select i1 %lezero, float %negx, float %x
ret float %fabs
}
; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
define float @select_fcmp_nnan_nsz_ule_negzero(float %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_ule_negzero(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz float @llvm.fabs.f32(float [[X:%.*]])
; CHECK-NEXT: ret float [[TMP1]]
;
%lezero = fcmp ule float %x, -0.0
%negx = fsub nnan nsz float -0.0, %x
%fabs = select i1 %lezero, float %negx, float %x
ret float %fabs
}
define float @select_fcmp_nnan_nsz_ole_negzero_unary_fneg(float %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_ole_negzero_unary_fneg(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz float @llvm.fabs.f32(float [[X:%.*]])
; CHECK-NEXT: ret float [[TMP1]]
;
%lezero = fcmp ole float %x, -0.0
%negx = fneg nnan nsz float %x
%fabs = select i1 %lezero, float %negx, float %x
ret float %fabs
}
; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
define float @select_fcmp_nnan_nsz_ule_negzero_unary_fneg(float %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_ule_negzero_unary_fneg(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz float @llvm.fabs.f32(float [[X:%.*]])
; CHECK-NEXT: ret float [[TMP1]]
;
%lezero = fcmp ule float %x, -0.0
%negx = fneg nnan nsz float %x
%fabs = select i1 %lezero, float %negx, float %x
ret float %fabs
}
; X > 0.0 ? X : (0.0 - X) --> fabs(X)
define <2 x float> @select_fcmp_nnan_nsz_ogt_zero(<2 x float> %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_ogt_zero(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz arcp <2 x float> @llvm.fabs.v2f32(<2 x float> [[X:%.*]])
; CHECK-NEXT: ret <2 x float> [[TMP1]]
;
%gtzero = fcmp ogt <2 x float> %x, zeroinitializer
%negx = fsub nnan nsz arcp <2 x float> <float -0.0, float -0.0>, %x
%fabs = select <2 x i1> %gtzero, <2 x float> %x, <2 x float> %negx
ret <2 x float> %fabs
}
; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
define <2 x float> @select_fcmp_nnan_nsz_ugt_zero(<2 x float> %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_ugt_zero(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz arcp <2 x float> @llvm.fabs.v2f32(<2 x float> [[X:%.*]])
; CHECK-NEXT: ret <2 x float> [[TMP1]]
;
%gtzero = fcmp ugt <2 x float> %x, zeroinitializer
%negx = fsub nnan nsz arcp <2 x float> <float -0.0, float -0.0>, %x
%fabs = select <2 x i1> %gtzero, <2 x float> %x, <2 x float> %negx
ret <2 x float> %fabs
}
define <2 x float> @select_fcmp_nnan_nsz_ogt_zero_unary_fneg(<2 x float> %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_ogt_zero_unary_fneg(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz arcp <2 x float> @llvm.fabs.v2f32(<2 x float> [[X:%.*]])
; CHECK-NEXT: ret <2 x float> [[TMP1]]
;
%gtzero = fcmp ogt <2 x float> %x, zeroinitializer
%negx = fneg nnan nsz arcp <2 x float> %x
%fabs = select <2 x i1> %gtzero, <2 x float> %x, <2 x float> %negx
ret <2 x float> %fabs
}
; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
define <2 x float> @select_fcmp_nnan_nsz_ugt_zero_unary_fneg(<2 x float> %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_ugt_zero_unary_fneg(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz arcp <2 x float> @llvm.fabs.v2f32(<2 x float> [[X:%.*]])
; CHECK-NEXT: ret <2 x float> [[TMP1]]
;
%gtzero = fcmp ugt <2 x float> %x, zeroinitializer
%negx = fneg nnan nsz arcp <2 x float> %x
%fabs = select <2 x i1> %gtzero, <2 x float> %x, <2 x float> %negx
ret <2 x float> %fabs
}
; X > -0.0 ? X : (0.0 - X) --> fabs(X)
define half @select_fcmp_nnan_nsz_ogt_negzero(half %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_ogt_negzero(
; CHECK-NEXT: [[TMP1:%.*]] = call fast half @llvm.fabs.f16(half [[X:%.*]])
; CHECK-NEXT: ret half [[TMP1]]
;
%gtzero = fcmp ogt half %x, -0.0
%negx = fsub fast half 0.0, %x
%fabs = select i1 %gtzero, half %x, half %negx
ret half %fabs
}
; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
define half @select_fcmp_nnan_nsz_ugt_negzero(half %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_ugt_negzero(
; CHECK-NEXT: [[TMP1:%.*]] = call fast half @llvm.fabs.f16(half [[X:%.*]])
; CHECK-NEXT: ret half [[TMP1]]
;
%gtzero = fcmp ugt half %x, -0.0
%negx = fsub fast half 0.0, %x
%fabs = select i1 %gtzero, half %x, half %negx
ret half %fabs
}
; X > 0.0 ? X : (0.0 - X) --> fabs(X)
define <2 x double> @select_fcmp_nnan_nsz_oge_zero(<2 x double> %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_oge_zero(
; CHECK-NEXT: [[TMP1:%.*]] = call reassoc nnan nsz <2 x double> @llvm.fabs.v2f64(<2 x double> [[X:%.*]])
; CHECK-NEXT: ret <2 x double> [[TMP1]]
;
%gezero = fcmp oge <2 x double> %x, zeroinitializer
%negx = fsub nnan nsz reassoc <2 x double> <double -0.0, double -0.0>, %x
%fabs = select <2 x i1> %gezero, <2 x double> %x, <2 x double> %negx
ret <2 x double> %fabs
}
; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
define <2 x double> @select_fcmp_nnan_nsz_uge_zero(<2 x double> %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_uge_zero(
; CHECK-NEXT: [[TMP1:%.*]] = call reassoc nnan nsz <2 x double> @llvm.fabs.v2f64(<2 x double> [[X:%.*]])
; CHECK-NEXT: ret <2 x double> [[TMP1]]
;
%gezero = fcmp uge <2 x double> %x, zeroinitializer
%negx = fsub nnan nsz reassoc <2 x double> <double -0.0, double -0.0>, %x
%fabs = select <2 x i1> %gezero, <2 x double> %x, <2 x double> %negx
ret <2 x double> %fabs
}
define <2 x double> @select_fcmp_nnan_nsz_oge_zero_unary_fneg(<2 x double> %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_oge_zero_unary_fneg(
; CHECK-NEXT: [[TMP1:%.*]] = call reassoc nnan nsz <2 x double> @llvm.fabs.v2f64(<2 x double> [[X:%.*]])
; CHECK-NEXT: ret <2 x double> [[TMP1]]
;
%gezero = fcmp oge <2 x double> %x, zeroinitializer
%negx = fneg nnan nsz reassoc <2 x double> %x
%fabs = select <2 x i1> %gezero, <2 x double> %x, <2 x double> %negx
ret <2 x double> %fabs
}
; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
define <2 x double> @select_fcmp_nnan_nsz_uge_zero_unary_fneg(<2 x double> %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_uge_zero_unary_fneg(
; CHECK-NEXT: [[TMP1:%.*]] = call reassoc nnan nsz <2 x double> @llvm.fabs.v2f64(<2 x double> [[X:%.*]])
; CHECK-NEXT: ret <2 x double> [[TMP1]]
;
%gezero = fcmp uge <2 x double> %x, zeroinitializer
%negx = fneg nnan nsz reassoc <2 x double> %x
%fabs = select <2 x i1> %gezero, <2 x double> %x, <2 x double> %negx
ret <2 x double> %fabs
}
; X > -0.0 ? X : (0.0 - X) --> fabs(X)
define half @select_fcmp_nnan_nsz_oge_negzero(half %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_oge_negzero(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz half @llvm.fabs.f16(half [[X:%.*]])
; CHECK-NEXT: ret half [[TMP1]]
;
%gezero = fcmp oge half %x, -0.0
%negx = fsub nnan nsz half -0.0, %x
%fabs = select i1 %gezero, half %x, half %negx
ret half %fabs
}
; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
define half @select_fcmp_nnan_nsz_uge_negzero(half %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_uge_negzero(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz half @llvm.fabs.f16(half [[X:%.*]])
; CHECK-NEXT: ret half [[TMP1]]
;
%gezero = fcmp uge half %x, -0.0
%negx = fsub nnan nsz half -0.0, %x
%fabs = select i1 %gezero, half %x, half %negx
ret half %fabs
}
define half @select_fcmp_nnan_nsz_oge_negzero_unary_fneg(half %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_oge_negzero_unary_fneg(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz half @llvm.fabs.f16(half [[X:%.*]])
; CHECK-NEXT: ret half [[TMP1]]
;
%gezero = fcmp oge half %x, -0.0
%negx = fneg nnan nsz half %x
%fabs = select i1 %gezero, half %x, half %negx
ret half %fabs
}
; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
define half @select_fcmp_nnan_nsz_uge_negzero_unary_fneg(half %x) {
; CHECK-LABEL: @select_fcmp_nnan_nsz_uge_negzero_unary_fneg(
; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz half @llvm.fabs.f16(half [[X:%.*]])
; CHECK-NEXT: ret half [[TMP1]]
;
%gezero = fcmp uge half %x, -0.0
%negx = fneg nnan nsz half %x
%fabs = select i1 %gezero, half %x, half %negx
ret half %fabs
}
define float @select_fneg(i1 %c, float %x) {
; CHECK-LABEL: @select_fneg(
; CHECK-NEXT: [[FABS:%.*]] = call float @llvm.fabs.f32(float [[X:%.*]])
; CHECK-NEXT: ret float [[FABS]]
;
%n = fneg float %x
%s = select i1 %c, float %n, float %x
%fabs = call float @llvm.fabs.f32(float %s)
ret float %fabs
}
define float @select_fneg_use1(i1 %c, float %x) {
; CHECK-LABEL: @select_fneg_use1(
; CHECK-NEXT: [[N:%.*]] = fneg float [[X:%.*]]
; CHECK-NEXT: call void @use(float [[N]])
; CHECK-NEXT: [[FABS:%.*]] = call fast float @llvm.fabs.f32(float [[X]])
; CHECK-NEXT: ret float [[FABS]]
;
%n = fneg float %x
call void @use(float %n)
%s = select i1 %c, float %x, float %n
%fabs = call fast float @llvm.fabs.f32(float %s)
ret float %fabs
}
define float @select_fneg_use2(i1 %c, float %x) {
; CHECK-LABEL: @select_fneg_use2(
; CHECK-NEXT: [[N:%.*]] = fneg arcp float [[X:%.*]]
; CHECK-NEXT: [[S:%.*]] = select i1 [[C:%.*]], float [[N]], float [[X]]
; CHECK-NEXT: call void @use(float [[S]])
; CHECK-NEXT: [[FABS:%.*]] = call nnan nsz float @llvm.fabs.f32(float [[X]])
; CHECK-NEXT: ret float [[FABS]]
;
%n = fneg arcp float %x
%s = select i1 %c, float %n, float %x
call void @use(float %s)
%fabs = call nnan nsz float @llvm.fabs.f32(float %s)
ret float %fabs
}
define <2 x float> @select_fneg_vec(<2 x i1> %c, <2 x float> %x) {
; CHECK-LABEL: @select_fneg_vec(
; CHECK-NEXT: [[FABS:%.*]] = call <2 x float> @llvm.fabs.v2f32(<2 x float> [[X:%.*]])
; CHECK-NEXT: ret <2 x float> [[FABS]]
;
%n = fneg <2 x float> %x
%s = select fast <2 x i1> %c, <2 x float> %x, <2 x float> %n
%fabs = call <2 x float> @llvm.fabs.v2f32(<2 x float> %s)
ret <2 x float> %fabs
}