1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -mtriple=x86_64-unknown-linux-gnu < %s -instcombine -S | FileCheck %s
4 ; Make sure libcalls are replaced with intrinsic calls.
6 declare float @llvm.fabs.f32(float)
7 declare double @llvm.fabs.f64(double)
8 declare fp128 @llvm.fabs.f128(fp128)
10 declare float @fabsf(float)
11 declare double @fabs(double)
12 declare fp128 @fabsl(fp128)
13 declare float @llvm.fma.f32(float, float, float)
14 declare float @llvm.fmuladd.f32(float, float, float)
16 define float @replace_fabs_call_f32(float %x) {
17 ; CHECK-LABEL: @replace_fabs_call_f32(
18 ; CHECK-NEXT: [[FABSF:%.*]] = call float @llvm.fabs.f32(float [[X:%.*]])
19 ; CHECK-NEXT: ret float [[FABSF]]
21 %fabsf = tail call float @fabsf(float %x)
25 define double @replace_fabs_call_f64(double %x) {
26 ; CHECK-LABEL: @replace_fabs_call_f64(
27 ; CHECK-NEXT: [[FABS:%.*]] = call double @llvm.fabs.f64(double [[X:%.*]])
28 ; CHECK-NEXT: ret double [[FABS]]
30 %fabs = tail call double @fabs(double %x)
34 define fp128 @replace_fabs_call_f128(fp128 %x) {
35 ; CHECK-LABEL: @replace_fabs_call_f128(
36 ; CHECK-NEXT: [[FABSL:%.*]] = call fp128 @llvm.fabs.f128(fp128 [[X:%.*]])
37 ; CHECK-NEXT: ret fp128 [[FABSL]]
39 %fabsl = tail call fp128 @fabsl(fp128 %x)
43 ; Make sure fast math flags are preserved when replacing the libcall.
44 define float @fmf_replace_fabs_call_f32(float %x) {
45 ; CHECK-LABEL: @fmf_replace_fabs_call_f32(
46 ; CHECK-NEXT: [[FABSF:%.*]] = call nnan float @llvm.fabs.f32(float [[X:%.*]])
47 ; CHECK-NEXT: ret float [[FABSF]]
49 %fabsf = tail call nnan float @fabsf(float %x)
53 ; Make sure all intrinsic calls are eliminated when the input is known
56 ; The fabs cannot be eliminated because %x may be a NaN
58 define float @square_fabs_intrinsic_f32(float %x) {
59 ; CHECK-LABEL: @square_fabs_intrinsic_f32(
60 ; CHECK-NEXT: [[MUL:%.*]] = fmul float [[X:%.*]], [[X]]
61 ; CHECK-NEXT: [[FABSF:%.*]] = tail call float @llvm.fabs.f32(float [[MUL]])
62 ; CHECK-NEXT: ret float [[FABSF]]
64 %mul = fmul float %x, %x
65 %fabsf = tail call float @llvm.fabs.f32(float %mul)
69 define double @square_fabs_intrinsic_f64(double %x) {
70 ; CHECK-LABEL: @square_fabs_intrinsic_f64(
71 ; CHECK-NEXT: [[MUL:%.*]] = fmul double [[X:%.*]], [[X]]
72 ; CHECK-NEXT: [[FABS:%.*]] = tail call double @llvm.fabs.f64(double [[MUL]])
73 ; CHECK-NEXT: ret double [[FABS]]
75 %mul = fmul double %x, %x
76 %fabs = tail call double @llvm.fabs.f64(double %mul)
80 define fp128 @square_fabs_intrinsic_f128(fp128 %x) {
81 ; CHECK-LABEL: @square_fabs_intrinsic_f128(
82 ; CHECK-NEXT: [[MUL:%.*]] = fmul fp128 [[X:%.*]], [[X]]
83 ; CHECK-NEXT: [[FABSL:%.*]] = tail call fp128 @llvm.fabs.f128(fp128 [[MUL]])
84 ; CHECK-NEXT: ret fp128 [[FABSL]]
86 %mul = fmul fp128 %x, %x
87 %fabsl = tail call fp128 @llvm.fabs.f128(fp128 %mul)
91 define float @square_nnan_fabs_intrinsic_f32(float %x) {
92 ; CHECK-LABEL: @square_nnan_fabs_intrinsic_f32(
93 ; CHECK-NEXT: [[MUL:%.*]] = fmul nnan float [[X:%.*]], [[X]]
94 ; CHECK-NEXT: ret float [[MUL]]
96 %mul = fmul nnan float %x, %x
97 %fabsf = call float @llvm.fabs.f32(float %mul)
101 ; Shrinking a library call to a smaller type should not be inhibited by nor inhibit the square optimization.
103 define float @square_fabs_shrink_call1(float %x) {
104 ; CHECK-LABEL: @square_fabs_shrink_call1(
105 ; CHECK-NEXT: [[TMP1:%.*]] = fmul float [[X:%.*]], [[X]]
106 ; CHECK-NEXT: [[TRUNC:%.*]] = call float @llvm.fabs.f32(float [[TMP1]])
107 ; CHECK-NEXT: ret float [[TRUNC]]
109 %ext = fpext float %x to double
110 %sq = fmul double %ext, %ext
111 %fabs = call double @fabs(double %sq)
112 %trunc = fptrunc double %fabs to float
116 define float @square_fabs_shrink_call2(float %x) {
117 ; CHECK-LABEL: @square_fabs_shrink_call2(
118 ; CHECK-NEXT: [[SQ:%.*]] = fmul float [[X:%.*]], [[X]]
119 ; CHECK-NEXT: [[TRUNC:%.*]] = call float @llvm.fabs.f32(float [[SQ]])
120 ; CHECK-NEXT: ret float [[TRUNC]]
122 %sq = fmul float %x, %x
123 %ext = fpext float %sq to double
124 %fabs = call double @fabs(double %ext)
125 %trunc = fptrunc double %fabs to float
129 define float @fabs_select_constant_negative_positive(i32 %c) {
130 ; CHECK-LABEL: @fabs_select_constant_negative_positive(
131 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[C:%.*]], 0
132 ; CHECK-NEXT: [[FABS:%.*]] = select i1 [[CMP]], float 1.000000e+00, float 2.000000e+00
133 ; CHECK-NEXT: ret float [[FABS]]
135 %cmp = icmp eq i32 %c, 0
136 %select = select i1 %cmp, float -1.0, float 2.0
137 %fabs = call float @llvm.fabs.f32(float %select)
141 define float @fabs_select_constant_positive_negative(i32 %c) {
142 ; CHECK-LABEL: @fabs_select_constant_positive_negative(
143 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[C:%.*]], 0
144 ; CHECK-NEXT: [[FABS:%.*]] = select i1 [[CMP]], float 1.000000e+00, float 2.000000e+00
145 ; CHECK-NEXT: ret float [[FABS]]
147 %cmp = icmp eq i32 %c, 0
148 %select = select i1 %cmp, float 1.0, float -2.0
149 %fabs = call float @llvm.fabs.f32(float %select)
153 define float @fabs_select_constant_negative_negative(i32 %c) {
154 ; CHECK-LABEL: @fabs_select_constant_negative_negative(
155 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[C:%.*]], 0
156 ; CHECK-NEXT: [[FABS:%.*]] = select i1 [[CMP]], float 1.000000e+00, float 2.000000e+00
157 ; CHECK-NEXT: ret float [[FABS]]
159 %cmp = icmp eq i32 %c, 0
160 %select = select i1 %cmp, float -1.0, float -2.0
161 %fabs = call float @llvm.fabs.f32(float %select)
165 define float @fabs_select_constant_neg0(i32 %c) {
166 ; CHECK-LABEL: @fabs_select_constant_neg0(
167 ; CHECK-NEXT: ret float 0.000000e+00
169 %cmp = icmp eq i32 %c, 0
170 %select = select i1 %cmp, float -0.0, float 0.0
171 %fabs = call float @llvm.fabs.f32(float %select)
175 define float @fabs_select_var_constant_negative(i32 %c, float %x) {
176 ; CHECK-LABEL: @fabs_select_var_constant_negative(
177 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[C:%.*]], 0
178 ; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], float [[X:%.*]], float -1.000000e+00
179 ; CHECK-NEXT: [[FABS:%.*]] = call float @llvm.fabs.f32(float [[SELECT]])
180 ; CHECK-NEXT: ret float [[FABS]]
182 %cmp = icmp eq i32 %c, 0
183 %select = select i1 %cmp, float %x, float -1.0
184 %fabs = call float @llvm.fabs.f32(float %select)
188 ; The fabs cannot be eliminated because %x may be a NaN
190 define float @square_fma_fabs_intrinsic_f32(float %x) {
191 ; CHECK-LABEL: @square_fma_fabs_intrinsic_f32(
192 ; CHECK-NEXT: [[FMA:%.*]] = call float @llvm.fma.f32(float [[X:%.*]], float [[X]], float 1.000000e+00)
193 ; CHECK-NEXT: [[FABSF:%.*]] = call float @llvm.fabs.f32(float [[FMA]])
194 ; CHECK-NEXT: ret float [[FABSF]]
196 %fma = call float @llvm.fma.f32(float %x, float %x, float 1.0)
197 %fabsf = call float @llvm.fabs.f32(float %fma)
201 ; The fabs cannot be eliminated because %x may be a NaN
203 define float @square_nnan_fma_fabs_intrinsic_f32(float %x) {
204 ; CHECK-LABEL: @square_nnan_fma_fabs_intrinsic_f32(
205 ; CHECK-NEXT: [[FMA:%.*]] = call nnan float @llvm.fma.f32(float [[X:%.*]], float [[X]], float 1.000000e+00)
206 ; CHECK-NEXT: ret float [[FMA]]
208 %fma = call nnan float @llvm.fma.f32(float %x, float %x, float 1.0)
209 %fabsf = call float @llvm.fabs.f32(float %fma)
213 define float @square_fmuladd_fabs_intrinsic_f32(float %x) {
214 ; CHECK-LABEL: @square_fmuladd_fabs_intrinsic_f32(
215 ; CHECK-NEXT: [[FMULADD:%.*]] = call float @llvm.fmuladd.f32(float [[X:%.*]], float [[X]], float 1.000000e+00)
216 ; CHECK-NEXT: [[FABSF:%.*]] = call float @llvm.fabs.f32(float [[FMULADD]])
217 ; CHECK-NEXT: ret float [[FABSF]]
219 %fmuladd = call float @llvm.fmuladd.f32(float %x, float %x, float 1.0)
220 %fabsf = call float @llvm.fabs.f32(float %fmuladd)
224 define float @square_nnan_fmuladd_fabs_intrinsic_f32(float %x) {
225 ; CHECK-LABEL: @square_nnan_fmuladd_fabs_intrinsic_f32(
226 ; CHECK-NEXT: [[FMULADD:%.*]] = call nnan float @llvm.fmuladd.f32(float [[X:%.*]], float [[X]], float 1.000000e+00)
227 ; CHECK-NEXT: ret float [[FMULADD]]
229 %fmuladd = call nnan float @llvm.fmuladd.f32(float %x, float %x, float 1.0)
230 %fabsf = call float @llvm.fabs.f32(float %fmuladd)
234 ; Don't introduce a second fpext
236 define double @multi_use_fabs_fpext(float %x) {
237 ; CHECK-LABEL: @multi_use_fabs_fpext(
238 ; CHECK-NEXT: [[FPEXT:%.*]] = fpext float [[X:%.*]] to double
239 ; CHECK-NEXT: [[FABS:%.*]] = call double @llvm.fabs.f64(double [[FPEXT]])
240 ; CHECK-NEXT: store volatile double [[FPEXT]], double* undef, align 8
241 ; CHECK-NEXT: ret double [[FABS]]
243 %fpext = fpext float %x to double
244 %fabs = call double @llvm.fabs.f64(double %fpext)
245 store volatile double %fpext, double* undef
249 ; Negative test for the fabs folds below: we require nnan, so
250 ; we won't always clear the sign bit of a NaN value.
252 define double @select_fcmp_ole_zero(double %x) {
253 ; CHECK-LABEL: @select_fcmp_ole_zero(
254 ; CHECK-NEXT: [[LEZERO:%.*]] = fcmp ole double [[X:%.*]], 0.000000e+00
255 ; CHECK-NEXT: [[NEGX:%.*]] = fsub double 0.000000e+00, [[X]]
256 ; CHECK-NEXT: [[FABS:%.*]] = select i1 [[LEZERO]], double [[NEGX]], double [[X]]
257 ; CHECK-NEXT: ret double [[FABS]]
259 %lezero = fcmp ole double %x, 0.0
260 %negx = fsub double 0.0, %x
261 %fabs = select i1 %lezero, double %negx, double %x
265 ; X <= 0.0 ? (0.0 - X) : X --> fabs(X)
267 define double @select_fcmp_nnan_ole_zero(double %x) {
268 ; CHECK-LABEL: @select_fcmp_nnan_ole_zero(
269 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan double @llvm.fabs.f64(double [[X:%.*]])
270 ; CHECK-NEXT: ret double [[TMP1]]
272 %lezero = fcmp ole double %x, 0.0
273 %negx = fsub nnan double 0.0, %x
274 %fabs = select i1 %lezero, double %negx, double %x
278 ; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
280 define double @select_fcmp_nnan_ule_zero(double %x) {
281 ; CHECK-LABEL: @select_fcmp_nnan_ule_zero(
282 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan double @llvm.fabs.f64(double [[X:%.*]])
283 ; CHECK-NEXT: ret double [[TMP1]]
285 %lezero = fcmp ule double %x, 0.0
286 %negx = fsub nnan double 0.0, %x
287 %fabs = select i1 %lezero, double %negx, double %x
291 ; Negative test - wrong predicate.
293 define double @select_fcmp_nnan_olt_zero(double %x) {
294 ; CHECK-LABEL: @select_fcmp_nnan_olt_zero(
295 ; CHECK-NEXT: [[LEZERO:%.*]] = fcmp olt double [[X:%.*]], 0.000000e+00
296 ; CHECK-NEXT: [[NEGX:%.*]] = fsub nnan double 0.000000e+00, [[X]]
297 ; CHECK-NEXT: [[FABS:%.*]] = select i1 [[LEZERO]], double [[NEGX]], double [[X]]
298 ; CHECK-NEXT: ret double [[FABS]]
300 %lezero = fcmp olt double %x, 0.0
301 %negx = fsub nnan double 0.0, %x
302 %fabs = select i1 %lezero, double %negx, double %x
306 ; X <= -0.0 ? (0.0 - X) : X --> fabs(X)
308 define <2 x float> @select_fcmp_nnan_ole_negzero(<2 x float> %x) {
309 ; CHECK-LABEL: @select_fcmp_nnan_ole_negzero(
310 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan <2 x float> @llvm.fabs.v2f32(<2 x float> [[X:%.*]])
311 ; CHECK-NEXT: ret <2 x float> [[TMP1]]
313 %lezero = fcmp ole <2 x float> %x, <float -0.0, float -0.0>
314 %negx = fsub nnan <2 x float> <float 0.0, float undef>, %x
315 %fabs = select <2 x i1> %lezero, <2 x float> %negx, <2 x float> %x
316 ret <2 x float> %fabs
319 ; X > 0.0 ? X : (0.0 - X) --> fabs(X)
321 define fp128 @select_fcmp_nnan_ogt_zero(fp128 %x) {
322 ; CHECK-LABEL: @select_fcmp_nnan_ogt_zero(
323 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan fp128 @llvm.fabs.f128(fp128 [[X:%.*]])
324 ; CHECK-NEXT: ret fp128 [[TMP1]]
326 %gtzero = fcmp ogt fp128 %x, zeroinitializer
327 %negx = fsub nnan fp128 zeroinitializer, %x
328 %fabs = select i1 %gtzero, fp128 %x, fp128 %negx
332 ; X > -0.0 ? X : (0.0 - X) --> fabs(X)
334 define half @select_fcmp_nnan_ogt_negzero(half %x) {
335 ; CHECK-LABEL: @select_fcmp_nnan_ogt_negzero(
336 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan half @llvm.fabs.f16(half [[X:%.*]])
337 ; CHECK-NEXT: ret half [[TMP1]]
339 %gtzero = fcmp ogt half %x, -0.0
340 %negx = fsub nnan half 0.0, %x
341 %fabs = select i1 %gtzero, half %x, half %negx
345 ; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
347 define half @select_fcmp_nnan_ugt_negzero(half %x) {
348 ; CHECK-LABEL: @select_fcmp_nnan_ugt_negzero(
349 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan half @llvm.fabs.f16(half [[X:%.*]])
350 ; CHECK-NEXT: ret half [[TMP1]]
352 %gtzero = fcmp ugt half %x, -0.0
353 %negx = fsub nnan half 0.0, %x
354 %fabs = select i1 %gtzero, half %x, half %negx
358 ; Negative test - wrong predicate.
360 define half @select_fcmp_nnan_oge_negzero(half %x) {
361 ; CHECK-LABEL: @select_fcmp_nnan_oge_negzero(
362 ; CHECK-NEXT: [[GTZERO:%.*]] = fcmp oge half [[X:%.*]], 0xH0000
363 ; CHECK-NEXT: [[NEGX:%.*]] = fsub nnan half 0xH0000, [[X]]
364 ; CHECK-NEXT: [[FABS:%.*]] = select i1 [[GTZERO]], half [[X]], half [[NEGX]]
365 ; CHECK-NEXT: ret half [[FABS]]
367 %gtzero = fcmp oge half %x, -0.0
368 %negx = fsub nnan half 0.0, %x
369 %fabs = select i1 %gtzero, half %x, half %negx
373 ; X < 0.0 ? -X : X --> fabs(X)
375 define double @select_fcmp_nnan_nsz_olt_zero(double %x) {
376 ; CHECK-LABEL: @select_fcmp_nnan_nsz_olt_zero(
377 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz double @llvm.fabs.f64(double [[X:%.*]])
378 ; CHECK-NEXT: ret double [[TMP1]]
380 %ltzero = fcmp olt double %x, 0.0
381 %negx = fsub nnan nsz double -0.0, %x
382 %fabs = select i1 %ltzero, double %negx, double %x
386 ; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
388 define double @select_fcmp_nnan_nsz_ult_zero(double %x) {
389 ; CHECK-LABEL: @select_fcmp_nnan_nsz_ult_zero(
390 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz double @llvm.fabs.f64(double [[X:%.*]])
391 ; CHECK-NEXT: ret double [[TMP1]]
393 %ltzero = fcmp ult double %x, 0.0
394 %negx = fsub nnan nsz double -0.0, %x
395 %fabs = select i1 %ltzero, double %negx, double %x
399 define double @select_fcmp_nnan_nsz_olt_zero_unary_fneg(double %x) {
400 ; CHECK-LABEL: @select_fcmp_nnan_nsz_olt_zero_unary_fneg(
401 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz double @llvm.fabs.f64(double [[X:%.*]])
402 ; CHECK-NEXT: ret double [[TMP1]]
404 %ltzero = fcmp olt double %x, 0.0
405 %negx = fneg nnan nsz double %x
406 %fabs = select i1 %ltzero, double %negx, double %x
410 ; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
412 define double @select_fcmp_nnan_nsz_ult_zero_unary_fneg(double %x) {
413 ; CHECK-LABEL: @select_fcmp_nnan_nsz_ult_zero_unary_fneg(
414 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz double @llvm.fabs.f64(double [[X:%.*]])
415 ; CHECK-NEXT: ret double [[TMP1]]
417 %ltzero = fcmp ult double %x, 0.0
418 %negx = fneg nnan nsz double %x
419 %fabs = select i1 %ltzero, double %negx, double %x
423 ; X < -0.0 ? -X : X --> fabs(X)
425 define float @select_fcmp_nnan_nsz_olt_negzero(float %x) {
426 ; CHECK-LABEL: @select_fcmp_nnan_nsz_olt_negzero(
427 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan ninf nsz float @llvm.fabs.f32(float [[X:%.*]])
428 ; CHECK-NEXT: ret float [[TMP1]]
430 %ltzero = fcmp olt float %x, -0.0
431 %negx = fsub nnan ninf nsz float -0.0, %x
432 %fabs = select i1 %ltzero, float %negx, float %x
436 ; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
438 define float @select_fcmp_nnan_nsz_ult_negzero(float %x) {
439 ; CHECK-LABEL: @select_fcmp_nnan_nsz_ult_negzero(
440 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan ninf nsz float @llvm.fabs.f32(float [[X:%.*]])
441 ; CHECK-NEXT: ret float [[TMP1]]
443 %ltzero = fcmp ult float %x, -0.0
444 %negx = fsub nnan ninf nsz float -0.0, %x
445 %fabs = select i1 %ltzero, float %negx, float %x
449 define float @select_fcmp_nnan_nsz_olt_negzero_unary_fneg(float %x) {
450 ; CHECK-LABEL: @select_fcmp_nnan_nsz_olt_negzero_unary_fneg(
451 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan ninf nsz float @llvm.fabs.f32(float [[X:%.*]])
452 ; CHECK-NEXT: ret float [[TMP1]]
454 %ltzero = fcmp olt float %x, -0.0
455 %negx = fneg nnan ninf nsz float %x
456 %fabs = select i1 %ltzero, float %negx, float %x
460 ; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
462 define float @select_fcmp_nnan_nsz_ult_negzero_unary_fneg(float %x) {
463 ; CHECK-LABEL: @select_fcmp_nnan_nsz_ult_negzero_unary_fneg(
464 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan ninf nsz float @llvm.fabs.f32(float [[X:%.*]])
465 ; CHECK-NEXT: ret float [[TMP1]]
467 %ltzero = fcmp ult float %x, -0.0
468 %negx = fneg nnan ninf nsz float %x
469 %fabs = select i1 %ltzero, float %negx, float %x
473 ; X <= 0.0 ? -X : X --> fabs(X)
475 define double @select_fcmp_nnan_nsz_ole_zero(double %x) {
476 ; CHECK-LABEL: @select_fcmp_nnan_nsz_ole_zero(
477 ; CHECK-NEXT: [[TMP1:%.*]] = call fast double @llvm.fabs.f64(double [[X:%.*]])
478 ; CHECK-NEXT: ret double [[TMP1]]
480 %lezero = fcmp ole double %x, 0.0
481 %negx = fsub fast double -0.0, %x
482 %fabs = select i1 %lezero, double %negx, double %x
486 ; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
488 define double @select_fcmp_nnan_nsz_ule_zero(double %x) {
489 ; CHECK-LABEL: @select_fcmp_nnan_nsz_ule_zero(
490 ; CHECK-NEXT: [[TMP1:%.*]] = call fast double @llvm.fabs.f64(double [[X:%.*]])
491 ; CHECK-NEXT: ret double [[TMP1]]
493 %lezero = fcmp ule double %x, 0.0
494 %negx = fsub fast double -0.0, %x
495 %fabs = select i1 %lezero, double %negx, double %x
499 define double @select_fcmp_nnan_nsz_ole_zero_unary_fneg(double %x) {
500 ; CHECK-LABEL: @select_fcmp_nnan_nsz_ole_zero_unary_fneg(
501 ; CHECK-NEXT: [[TMP1:%.*]] = call fast double @llvm.fabs.f64(double [[X:%.*]])
502 ; CHECK-NEXT: ret double [[TMP1]]
504 %lezero = fcmp ole double %x, 0.0
505 %negx = fneg fast double %x
506 %fabs = select i1 %lezero, double %negx, double %x
510 ; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
512 define double @select_fcmp_nnan_nsz_ule_zero_unary_fneg(double %x) {
513 ; CHECK-LABEL: @select_fcmp_nnan_nsz_ule_zero_unary_fneg(
514 ; CHECK-NEXT: [[TMP1:%.*]] = call fast double @llvm.fabs.f64(double [[X:%.*]])
515 ; CHECK-NEXT: ret double [[TMP1]]
517 %lezero = fcmp ule double %x, 0.0
518 %negx = fneg fast double %x
519 %fabs = select i1 %lezero, double %negx, double %x
523 ; X <= -0.0 ? -X : X --> fabs(X)
525 define float @select_fcmp_nnan_nsz_ole_negzero(float %x) {
526 ; CHECK-LABEL: @select_fcmp_nnan_nsz_ole_negzero(
527 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz float @llvm.fabs.f32(float [[X:%.*]])
528 ; CHECK-NEXT: ret float [[TMP1]]
530 %lezero = fcmp ole float %x, -0.0
531 %negx = fsub nnan nsz float -0.0, %x
532 %fabs = select i1 %lezero, float %negx, float %x
536 ; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
538 define float @select_fcmp_nnan_nsz_ule_negzero(float %x) {
539 ; CHECK-LABEL: @select_fcmp_nnan_nsz_ule_negzero(
540 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz float @llvm.fabs.f32(float [[X:%.*]])
541 ; CHECK-NEXT: ret float [[TMP1]]
543 %lezero = fcmp ule float %x, -0.0
544 %negx = fsub nnan nsz float -0.0, %x
545 %fabs = select i1 %lezero, float %negx, float %x
549 define float @select_fcmp_nnan_nsz_ole_negzero_unary_fneg(float %x) {
550 ; CHECK-LABEL: @select_fcmp_nnan_nsz_ole_negzero_unary_fneg(
551 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz float @llvm.fabs.f32(float [[X:%.*]])
552 ; CHECK-NEXT: ret float [[TMP1]]
554 %lezero = fcmp ole float %x, -0.0
555 %negx = fneg nnan nsz float %x
556 %fabs = select i1 %lezero, float %negx, float %x
560 ; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
562 define float @select_fcmp_nnan_nsz_ule_negzero_unary_fneg(float %x) {
563 ; CHECK-LABEL: @select_fcmp_nnan_nsz_ule_negzero_unary_fneg(
564 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz float @llvm.fabs.f32(float [[X:%.*]])
565 ; CHECK-NEXT: ret float [[TMP1]]
567 %lezero = fcmp ule float %x, -0.0
568 %negx = fneg nnan nsz float %x
569 %fabs = select i1 %lezero, float %negx, float %x
573 ; X > 0.0 ? X : (0.0 - X) --> fabs(X)
575 define <2 x float> @select_fcmp_nnan_nsz_ogt_zero(<2 x float> %x) {
576 ; CHECK-LABEL: @select_fcmp_nnan_nsz_ogt_zero(
577 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz arcp <2 x float> @llvm.fabs.v2f32(<2 x float> [[X:%.*]])
578 ; CHECK-NEXT: ret <2 x float> [[TMP1]]
580 %gtzero = fcmp ogt <2 x float> %x, zeroinitializer
581 %negx = fsub nnan nsz arcp <2 x float> <float -0.0, float -0.0>, %x
582 %fabs = select <2 x i1> %gtzero, <2 x float> %x, <2 x float> %negx
583 ret <2 x float> %fabs
586 ; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
588 define <2 x float> @select_fcmp_nnan_nsz_ugt_zero(<2 x float> %x) {
589 ; CHECK-LABEL: @select_fcmp_nnan_nsz_ugt_zero(
590 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz arcp <2 x float> @llvm.fabs.v2f32(<2 x float> [[X:%.*]])
591 ; CHECK-NEXT: ret <2 x float> [[TMP1]]
593 %gtzero = fcmp ugt <2 x float> %x, zeroinitializer
594 %negx = fsub nnan nsz arcp <2 x float> <float -0.0, float -0.0>, %x
595 %fabs = select <2 x i1> %gtzero, <2 x float> %x, <2 x float> %negx
596 ret <2 x float> %fabs
599 define <2 x float> @select_fcmp_nnan_nsz_ogt_zero_unary_fneg(<2 x float> %x) {
600 ; CHECK-LABEL: @select_fcmp_nnan_nsz_ogt_zero_unary_fneg(
601 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz arcp <2 x float> @llvm.fabs.v2f32(<2 x float> [[X:%.*]])
602 ; CHECK-NEXT: ret <2 x float> [[TMP1]]
604 %gtzero = fcmp ogt <2 x float> %x, zeroinitializer
605 %negx = fneg nnan nsz arcp <2 x float> %x
606 %fabs = select <2 x i1> %gtzero, <2 x float> %x, <2 x float> %negx
607 ret <2 x float> %fabs
610 ; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
612 define <2 x float> @select_fcmp_nnan_nsz_ugt_zero_unary_fneg(<2 x float> %x) {
613 ; CHECK-LABEL: @select_fcmp_nnan_nsz_ugt_zero_unary_fneg(
614 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz arcp <2 x float> @llvm.fabs.v2f32(<2 x float> [[X:%.*]])
615 ; CHECK-NEXT: ret <2 x float> [[TMP1]]
617 %gtzero = fcmp ugt <2 x float> %x, zeroinitializer
618 %negx = fneg nnan nsz arcp <2 x float> %x
619 %fabs = select <2 x i1> %gtzero, <2 x float> %x, <2 x float> %negx
620 ret <2 x float> %fabs
623 ; X > -0.0 ? X : (0.0 - X) --> fabs(X)
625 define half @select_fcmp_nnan_nsz_ogt_negzero(half %x) {
626 ; CHECK-LABEL: @select_fcmp_nnan_nsz_ogt_negzero(
627 ; CHECK-NEXT: [[TMP1:%.*]] = call fast half @llvm.fabs.f16(half [[X:%.*]])
628 ; CHECK-NEXT: ret half [[TMP1]]
630 %gtzero = fcmp ogt half %x, -0.0
631 %negx = fsub fast half 0.0, %x
632 %fabs = select i1 %gtzero, half %x, half %negx
636 ; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
638 define half @select_fcmp_nnan_nsz_ugt_negzero(half %x) {
639 ; CHECK-LABEL: @select_fcmp_nnan_nsz_ugt_negzero(
640 ; CHECK-NEXT: [[TMP1:%.*]] = call fast half @llvm.fabs.f16(half [[X:%.*]])
641 ; CHECK-NEXT: ret half [[TMP1]]
643 %gtzero = fcmp ugt half %x, -0.0
644 %negx = fsub fast half 0.0, %x
645 %fabs = select i1 %gtzero, half %x, half %negx
649 ; X > 0.0 ? X : (0.0 - X) --> fabs(X)
651 define <2 x double> @select_fcmp_nnan_nsz_oge_zero(<2 x double> %x) {
652 ; CHECK-LABEL: @select_fcmp_nnan_nsz_oge_zero(
653 ; CHECK-NEXT: [[TMP1:%.*]] = call reassoc nnan nsz <2 x double> @llvm.fabs.v2f64(<2 x double> [[X:%.*]])
654 ; CHECK-NEXT: ret <2 x double> [[TMP1]]
656 %gezero = fcmp oge <2 x double> %x, zeroinitializer
657 %negx = fsub nnan nsz reassoc <2 x double> <double -0.0, double -0.0>, %x
658 %fabs = select <2 x i1> %gezero, <2 x double> %x, <2 x double> %negx
659 ret <2 x double> %fabs
662 ; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
664 define <2 x double> @select_fcmp_nnan_nsz_uge_zero(<2 x double> %x) {
665 ; CHECK-LABEL: @select_fcmp_nnan_nsz_uge_zero(
666 ; CHECK-NEXT: [[TMP1:%.*]] = call reassoc nnan nsz <2 x double> @llvm.fabs.v2f64(<2 x double> [[X:%.*]])
667 ; CHECK-NEXT: ret <2 x double> [[TMP1]]
669 %gezero = fcmp uge <2 x double> %x, zeroinitializer
670 %negx = fsub nnan nsz reassoc <2 x double> <double -0.0, double -0.0>, %x
671 %fabs = select <2 x i1> %gezero, <2 x double> %x, <2 x double> %negx
672 ret <2 x double> %fabs
675 define <2 x double> @select_fcmp_nnan_nsz_oge_zero_unary_fneg(<2 x double> %x) {
676 ; CHECK-LABEL: @select_fcmp_nnan_nsz_oge_zero_unary_fneg(
677 ; CHECK-NEXT: [[TMP1:%.*]] = call reassoc nnan nsz <2 x double> @llvm.fabs.v2f64(<2 x double> [[X:%.*]])
678 ; CHECK-NEXT: ret <2 x double> [[TMP1]]
680 %gezero = fcmp oge <2 x double> %x, zeroinitializer
681 %negx = fneg nnan nsz reassoc <2 x double> %x
682 %fabs = select <2 x i1> %gezero, <2 x double> %x, <2 x double> %negx
683 ret <2 x double> %fabs
686 ; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
688 define <2 x double> @select_fcmp_nnan_nsz_uge_zero_unary_fneg(<2 x double> %x) {
689 ; CHECK-LABEL: @select_fcmp_nnan_nsz_uge_zero_unary_fneg(
690 ; CHECK-NEXT: [[TMP1:%.*]] = call reassoc nnan nsz <2 x double> @llvm.fabs.v2f64(<2 x double> [[X:%.*]])
691 ; CHECK-NEXT: ret <2 x double> [[TMP1]]
693 %gezero = fcmp uge <2 x double> %x, zeroinitializer
694 %negx = fneg nnan nsz reassoc <2 x double> %x
695 %fabs = select <2 x i1> %gezero, <2 x double> %x, <2 x double> %negx
696 ret <2 x double> %fabs
699 ; X > -0.0 ? X : (0.0 - X) --> fabs(X)
701 define half @select_fcmp_nnan_nsz_oge_negzero(half %x) {
702 ; CHECK-LABEL: @select_fcmp_nnan_nsz_oge_negzero(
703 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz half @llvm.fabs.f16(half [[X:%.*]])
704 ; CHECK-NEXT: ret half [[TMP1]]
706 %gezero = fcmp oge half %x, -0.0
707 %negx = fsub nnan nsz half -0.0, %x
708 %fabs = select i1 %gezero, half %x, half %negx
712 ; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
714 define half @select_fcmp_nnan_nsz_uge_negzero(half %x) {
715 ; CHECK-LABEL: @select_fcmp_nnan_nsz_uge_negzero(
716 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz half @llvm.fabs.f16(half [[X:%.*]])
717 ; CHECK-NEXT: ret half [[TMP1]]
719 %gezero = fcmp uge half %x, -0.0
720 %negx = fsub nnan nsz half -0.0, %x
721 %fabs = select i1 %gezero, half %x, half %negx
725 define half @select_fcmp_nnan_nsz_oge_negzero_unary_fneg(half %x) {
726 ; CHECK-LABEL: @select_fcmp_nnan_nsz_oge_negzero_unary_fneg(
727 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz half @llvm.fabs.f16(half [[X:%.*]])
728 ; CHECK-NEXT: ret half [[TMP1]]
730 %gezero = fcmp oge half %x, -0.0
731 %negx = fneg nnan nsz half %x
732 %fabs = select i1 %gezero, half %x, half %negx
736 ; Repeat with unordered predicate - nnan allows us to treat ordered/unordered identically.
738 define half @select_fcmp_nnan_nsz_uge_negzero_unary_fneg(half %x) {
739 ; CHECK-LABEL: @select_fcmp_nnan_nsz_uge_negzero_unary_fneg(
740 ; CHECK-NEXT: [[TMP1:%.*]] = call nnan nsz half @llvm.fabs.f16(half [[X:%.*]])
741 ; CHECK-NEXT: ret half [[TMP1]]
743 %gezero = fcmp uge half %x, -0.0
744 %negx = fneg nnan nsz half %x
745 %fabs = select i1 %gezero, half %x, half %negx