diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -2863,14 +2863,12 @@ } // Canonicalize select with fcmp to fabs(). -0.0 makes this tricky. We need - // fast-math-flags (nsz) or fsub with +0.0 (not fneg) for this to work. We - // also require nnan because we do not want to unintentionally change the - // sign of a NaN value. + // fast-math-flags (nsz) or fsub with +0.0 (not fneg) for this to work. // (X <= +/-0.0) ? (0.0 - X) : X --> fabs(X) Instruction *FSub; if (match(CondVal, m_FCmp(Pred, m_Specific(FalseVal), m_AnyZeroFP())) && match(TrueVal, m_FSub(m_PosZeroFP(), m_Specific(FalseVal))) && - match(TrueVal, m_Instruction(FSub)) && FSub->hasNoNaNs() && + match(TrueVal, m_Instruction(FSub)) && (Pred == FCmpInst::FCMP_OLE || Pred == FCmpInst::FCMP_ULE)) { Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FalseVal, &SI); return replaceInstUsesWith(SI, Fabs); @@ -2878,7 +2876,7 @@ // (X > +/-0.0) ? X : (0.0 - X) --> fabs(X) if (match(CondVal, m_FCmp(Pred, m_Specific(TrueVal), m_AnyZeroFP())) && match(FalseVal, m_FSub(m_PosZeroFP(), m_Specific(TrueVal))) && - match(FalseVal, m_Instruction(FSub)) && FSub->hasNoNaNs() && + match(FalseVal, m_Instruction(FSub)) && (Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_UGT)) { Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, TrueVal, &SI); return replaceInstUsesWith(SI, Fabs); @@ -2889,8 +2887,7 @@ Instruction *FNeg; if (match(CondVal, m_FCmp(Pred, m_Specific(FalseVal), m_AnyZeroFP())) && match(TrueVal, m_FNeg(m_Specific(FalseVal))) && - match(TrueVal, m_Instruction(FNeg)) && FNeg->hasNoNaNs() && - FNeg->hasNoSignedZeros() && SI.hasNoSignedZeros() && + match(TrueVal, m_Instruction(FNeg)) && SI.hasNoSignedZeros() && (Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_OLE || Pred == FCmpInst::FCMP_ULT || Pred == FCmpInst::FCMP_ULE)) { Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FalseVal, &SI); @@ -2901,8 +2898,7 @@ // (X >= +/-0.0) ? X : -X --> fabs(X) if (match(CondVal, m_FCmp(Pred, m_Specific(TrueVal), m_AnyZeroFP())) && match(FalseVal, m_FNeg(m_Specific(TrueVal))) && - match(FalseVal, m_Instruction(FNeg)) && FNeg->hasNoNaNs() && - FNeg->hasNoSignedZeros() && SI.hasNoSignedZeros() && + match(FalseVal, m_Instruction(FNeg)) && SI.hasNoSignedZeros() && (Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_OGE || Pred == FCmpInst::FCMP_UGT || Pred == FCmpInst::FCMP_UGE)) { Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, TrueVal, &SI); diff --git a/llvm/test/Transforms/InstCombine/fabs.ll b/llvm/test/Transforms/InstCombine/fabs.ll --- a/llvm/test/Transforms/InstCombine/fabs.ll +++ b/llvm/test/Transforms/InstCombine/fabs.ll @@ -254,10 +254,8 @@ 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]] +; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.fabs.f64(double [[X:%.*]]) +; CHECK-NEXT: ret double [[TMP1]] ; %lezero = fcmp ole double %x, 0.0 %negx = fsub double 0.0, %x @@ -289,6 +287,17 @@ ret double %fabs } +define double @select_nnan_fcmp_ole_zero(double %x) { +; CHECK-LABEL: @select_nnan_fcmp_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 double 0.0, %x + %fabs = select nnan 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) { @@ -341,6 +350,17 @@ ret <2 x float> %fabs } +define <2 x float> @select_nnan_fcmp_ole_negzero(<2 x float> %x) { +; CHECK-LABEL: @select_nnan_fcmp_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, + %negx = fsub <2 x float> , %x + %fabs = select nnan <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) { @@ -365,6 +385,17 @@ ret fp128 %fabs } +define fp128 @select_nnan_fcmp_ogt_zero(fp128 %x) { +; CHECK-LABEL: @select_nnan_fcmp_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 fp128 zeroinitializer, %x + %fabs = select nnan 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) { @@ -389,6 +420,17 @@ ret half %fabs } +define half @select_nnan_fcmp_ogt_negzero(half %x) { +; CHECK-LABEL: @select_nnan_fcmp_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 half 0.0, %x + %fabs = select nnan 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) { @@ -443,6 +485,17 @@ ret double %fabs } +define double @select_nnan_nsz_fcmp_olt_zero(double %x) { +; CHECK-LABEL: @select_nnan_nsz_fcmp_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 double -0.0, %x + %fabs = select nnan nsz 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) { @@ -512,6 +565,17 @@ ret float %fabs } +define float @select_nnan_ninf_nsz_fcmp_olt_negzero(float %x) { +; CHECK-LABEL: @select_nnan_ninf_nsz_fcmp_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 float -0.0, %x + %fabs = select nnan ninf nsz 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) { @@ -650,6 +714,17 @@ ret float %fabs } +define float @select_nnan_nsz_fcmp_ole_negzero(float %x) { +; CHECK-LABEL: @select_nnan_nsz_fcmp_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 float -0.0, %x + %fabs = select nnan nsz 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) {