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 @@ -2846,15 +2846,13 @@ // 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. - // FIXME: These folds should test/propagate FMF from the select, not the - // fsub or fneg. // (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() && (Pred == FCmpInst::FCMP_OLE || Pred == FCmpInst::FCMP_ULE)) { - Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FalseVal, FSub); + Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FalseVal, &SI); return replaceInstUsesWith(SI, Fabs); } // (X > +/-0.0) ? X : (0.0 - X) --> fabs(X) 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 @@ -266,10 +266,11 @@ } ; X <= 0.0 ? (0.0 - X) : X --> fabs(X) +; This transformation drops the fast math flags, here, nnan flag. 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: [[TMP1:%.*]] = call double @llvm.fabs.f64(double [[X:%.*]]) ; CHECK-NEXT: ret double [[TMP1]] ; %lezero = fcmp ole double %x, 0.0 @@ -278,11 +279,24 @@ ret double %fabs } +; An Example with fast math flag in select instruction. + +define double @select_nnan_fcmp_nnan_ole_zero(double %x) { +; CHECK-LABEL: @select_nnan_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 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) { ; CHECK-LABEL: @select_fcmp_nnan_ule_zero( -; CHECK-NEXT: [[TMP1:%.*]] = call nnan double @llvm.fabs.f64(double [[X:%.*]]) +; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.fabs.f64(double [[X:%.*]]) ; CHECK-NEXT: ret double [[TMP1]] ; %lezero = fcmp ule double %x, 0.0 @@ -310,7 +324,7 @@ 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: [[TMP1:%.*]] = call <2 x float> @llvm.fabs.v2f32(<2 x float> [[X:%.*]]) ; CHECK-NEXT: ret <2 x float> [[TMP1]] ; %lezero = fcmp ole <2 x float> %x,