+; 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.
define float @replace_fabs_call_f32(float %x) {
; CHECK-LABEL: @replace_fabs_call_f32(
-; CHECK-NEXT: [[TMP1:%.*]] = call float @llvm.fabs.f32(float %x)
-; CHECK-NEXT: ret float [[TMP1]]
+; 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: [[TMP1:%.*]] = call double @llvm.fabs.f64(double %x)
-; CHECK-NEXT: ret double [[TMP1]]
+; 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: [[TMP1:%.*]] = call fp128 @llvm.fabs.f128(fp128 %x)
-; CHECK-NEXT: ret fp128 [[TMP1]]
+; 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: [[TMP1:%.*]] = call nnan float @llvm.fabs.f32(float %x)
-; CHECK-NEXT: ret float [[TMP1]]
+; 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
define float @square_fabs_intrinsic_f32(float %x) {
; CHECK-LABEL: @square_fabs_intrinsic_f32(
-; CHECK-NEXT: [[MUL:%.*]] = fmul float %x, %x
+; CHECK-NEXT: [[MUL:%.*]] = fmul float [[X:%.*]], [[X]]
; CHECK-NEXT: [[FABSF:%.*]] = tail call float @llvm.fabs.f32(float [[MUL]])
; CHECK-NEXT: 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: [[MUL:%.*]] = fmul double [[X:%.*]], [[X]]
; CHECK-NEXT: [[FABS:%.*]] = tail call double @llvm.fabs.f64(double [[MUL]])
; CHECK-NEXT: 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: [[MUL:%.*]] = fmul fp128 [[X:%.*]], [[X]]
; CHECK-NEXT: [[FABSL:%.*]] = tail call fp128 @llvm.fabs.f128(fp128 [[MUL]])
; CHECK-NEXT: 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: [[MUL:%.*]] = fmul nnan float [[X:%.*]], [[X]]
; CHECK-NEXT: ret float [[MUL]]
;
%mul = fmul nnan float %x, %x
define float @square_fabs_shrink_call1(float %x) {
; CHECK-LABEL: @square_fabs_shrink_call1(
-; CHECK-NEXT: [[TMP1:%.*]] = fmul float %x, %x
+; CHECK-NEXT: [[TMP1:%.*]] = fmul float [[X:%.*]], [[X]]
; CHECK-NEXT: [[TRUNC:%.*]] = call float @llvm.fabs.f32(float [[TMP1]])
; CHECK-NEXT: 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: [[SQ:%.*]] = fmul float [[X:%.*]], [[X]]
; CHECK-NEXT: [[TRUNC:%.*]] = call float @llvm.fabs.f32(float [[SQ]])
; CHECK-NEXT: 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: [[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]]
;
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: [[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]]
;
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: [[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]]
;
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: [[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]]
;
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: [[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]]
;
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: [[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)
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: [[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]]
;
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: [[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)
define double @multi_use_fabs_fpext(float %x) {
; CHECK-LABEL: @multi_use_fabs_fpext(
-; CHECK-NEXT: [[FPEXT:%.*]] = fpext float %x to double
+; 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]]
store volatile double %fpext, double* undef
ret double %fabs
}
+
+; X < 0.0 ? -X : X --> fabs(X)
+
+define double @select_fcmp_nsz_olt(double %x) {
+; CHECK-LABEL: @select_fcmp_nsz_olt(
+; CHECK-NEXT: [[LTZERO:%.*]] = fcmp nsz olt double [[X:%.*]], 0.000000e+00
+; CHECK-NEXT: [[NEGX:%.*]] = fsub double -0.000000e+00, [[X]]
+; CHECK-NEXT: [[FABS:%.*]] = select i1 [[LTZERO]], double [[NEGX]], double [[X]]
+; CHECK-NEXT: ret double [[FABS]]
+;
+ %ltzero = fcmp nsz olt double %x, 0.0
+ %negx = fsub double -0.0, %x
+ %fabs = select i1 %ltzero, double %negx, double %x
+ ret double %fabs
+}
+
+; X >= 0.0 ? X : -X --> fabs(X)
+; Also test for vector type and more FMF.
+
+define <2 x float> @select_fcmp_nsz_ogt(<2 x float> %x) {
+; CHECK-LABEL: @select_fcmp_nsz_ogt(
+; CHECK-NEXT: [[GEZERO:%.*]] = fcmp ninf nsz oge <2 x float> [[X:%.*]], zeroinitializer
+; CHECK-NEXT: [[NEGX:%.*]] = fsub <2 x float> <float -0.000000e+00, float -0.000000e+00>, [[X]]
+; CHECK-NEXT: [[FABS:%.*]] = select <2 x i1> [[GEZERO]], <2 x float> [[X]], <2 x float> [[NEGX]]
+; CHECK-NEXT: ret <2 x float> [[FABS]]
+;
+ %gezero = fcmp nsz ninf oge <2 x float> %x, zeroinitializer
+ %negx = fsub <2 x float> <float -0.0, float -0.0>, %x
+ %fabs = select <2 x i1> %gezero, <2 x float> %x, <2 x float> %negx
+ ret <2 x float> %fabs
+}
+