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 @@ -2809,7 +2809,7 @@ 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 @@ -269,7 +269,7 @@ 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 +278,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 +323,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,