Index: llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp @@ -2520,19 +2520,35 @@ return nullptr; } -static Instruction *hoistFNegAboveFMulFDiv(Instruction &I, - InstCombiner::BuilderTy &Builder) { +Instruction *InstCombinerImpl::hoistFNegAboveFMulFDiv(Instruction &I) { Value *FNeg; - if (!match(&I, m_FNeg(m_Value(FNeg)))) + if (!match(&I, m_FNeg(m_OneUse(m_Value(FNeg))))) return nullptr; Value *X, *Y; - if (match(FNeg, m_OneUse(m_FMul(m_Value(X), m_Value(Y))))) + if (match(FNeg, m_FMul(m_Value(X), m_Value(Y)))) return BinaryOperator::CreateFMulFMF(Builder.CreateFNegFMF(X, &I), Y, &I); - if (match(FNeg, m_OneUse(m_FDiv(m_Value(X), m_Value(Y))))) + if (match(FNeg, m_FDiv(m_Value(X), m_Value(Y)))) return BinaryOperator::CreateFDivFMF(Builder.CreateFNegFMF(X, &I), Y, &I); + if (IntrinsicInst *II = dyn_cast(FNeg)) { + // Make sure to preserve flags and metadata on the call. + if (II->getIntrinsicID() == Intrinsic::ldexp) { + FastMathFlags FMF = I.getFastMathFlags(); + FMF |= II->getFastMathFlags(); + + IRBuilder<>::FastMathFlagGuard FMFGuard(Builder); + Builder.setFastMathFlags(FMF); + + CallInst *New = Builder.CreateCall( + II->getCalledFunction(), + {Builder.CreateFNeg(II->getArgOperand(0)), II->getArgOperand(1)}); + New->copyMetadata(*II); + return replaceInstUsesWith(I, New); + } + } + return nullptr; } @@ -2553,7 +2569,7 @@ match(Op, m_OneUse(m_FSub(m_Value(X), m_Value(Y))))) return BinaryOperator::CreateFSubFMF(Y, X, &I); - if (Instruction *R = hoistFNegAboveFMulFDiv(I, Builder)) + if (Instruction *R = hoistFNegAboveFMulFDiv(I)) return R; Value *OneUse; @@ -2638,7 +2654,7 @@ if (Instruction *X = foldFNegIntoConstant(I, DL)) return X; - if (Instruction *R = hoistFNegAboveFMulFDiv(I, Builder)) + if (Instruction *R = hoistFNegAboveFMulFDiv(I)) return R; Value *X, *Y; Index: llvm/lib/Transforms/InstCombine/InstCombineInternal.h =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineInternal.h +++ llvm/lib/Transforms/InstCombine/InstCombineInternal.h @@ -387,6 +387,8 @@ Instruction *foldAndOrOfSelectUsingImpliedCond(Value *Op, SelectInst &SI, bool IsAnd); + Instruction *hoistFNegAboveFMulFDiv(Instruction &I); + public: /// Create and insert the idiom we use to indicate a block is unreachable /// without having to rewrite the CFG from within InstCombine. Index: llvm/test/Transforms/InstCombine/fneg.ll =================================================================== --- llvm/test/Transforms/InstCombine/fneg.ll +++ llvm/test/Transforms/InstCombine/fneg.ll @@ -1,6 +1,8 @@ ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py ; RUN: opt < %s -passes=instcombine -S | FileCheck %s +declare float @llvm.ldexp.f32.i32(float, i32) +declare <2 x float> @llvm.ldexp.v2f32.v2i32(<2 x float>, <2 x i32>) declare void @use(float) define float @fneg_fneg(float %a) { @@ -829,3 +831,161 @@ %r = fneg float %s ret float %r } + +define float @fneg_ldexp(float %x, i32 %n) { +; CHECK-LABEL: @fneg_ldexp( +; CHECK-NEXT: [[TMP1:%.*]] = fneg float [[X:%.*]] +; CHECK-NEXT: [[NEG:%.*]] = call float @llvm.ldexp.f32.i32(float [[TMP1]], i32 [[N:%.*]]) +; CHECK-NEXT: ret float [[NEG]] +; + %ldexp = call float @llvm.ldexp.f32.i32(float %x, i32 %n) + %neg = fneg float %ldexp + ret float %neg +} + +define float @fsub_fneg_ldexp(float %x, i32 %n) { +; CHECK-LABEL: @fsub_fneg_ldexp( +; CHECK-NEXT: [[TMP1:%.*]] = fneg float [[X:%.*]] +; CHECK-NEXT: [[NEG:%.*]] = call float @llvm.ldexp.f32.i32(float [[TMP1]], i32 [[N:%.*]]) +; CHECK-NEXT: ret float [[NEG]] +; + %ldexp = call float @llvm.ldexp.f32.i32(float %x, i32 %n) + %neg = fsub float -0.0, %ldexp + ret float %neg +} + +define float @fsub_fneg_ldexp_nsz(float %x, i32 %n) { +; CHECK-LABEL: @fsub_fneg_ldexp_nsz( +; CHECK-NEXT: [[TMP1:%.*]] = fneg nsz float [[X:%.*]] +; CHECK-NEXT: [[NEG:%.*]] = call nsz float @llvm.ldexp.f32.i32(float [[TMP1]], i32 [[N:%.*]]) +; CHECK-NEXT: ret float [[NEG]] +; + %ldexp = call float @llvm.ldexp.f32.i32(float %x, i32 %n) + %neg = fsub nsz float -0.0, %ldexp + ret float %neg +} + +define float @fsub_fneg_ldexp_p0_nsz(float %x, i32 %n) { +; CHECK-LABEL: @fsub_fneg_ldexp_p0_nsz( +; CHECK-NEXT: [[TMP1:%.*]] = fneg nsz float [[X:%.*]] +; CHECK-NEXT: [[NEG:%.*]] = call nsz float @llvm.ldexp.f32.i32(float [[TMP1]], i32 [[N:%.*]]) +; CHECK-NEXT: ret float [[NEG]] +; + %ldexp = call float @llvm.ldexp.f32.i32(float %x, i32 %n) + %neg = fsub nsz float 0.0, %ldexp + ret float %neg +} + +define float @fsub_fneg_ldexp_p0(float %x, i32 %n) { +; CHECK-LABEL: @fsub_fneg_ldexp_p0( +; CHECK-NEXT: [[LDEXP:%.*]] = call float @llvm.ldexp.f32.i32(float [[X:%.*]], i32 [[N:%.*]]) +; CHECK-NEXT: [[NEG:%.*]] = fsub float 0.000000e+00, [[LDEXP]] +; CHECK-NEXT: ret float [[NEG]] +; + %ldexp = call float @llvm.ldexp.f32.i32(float %x, i32 %n) + %neg = fsub float 0.0, %ldexp + ret float %neg +} + +define <2 x float> @fneg_ldexp_vector(<2 x float> %x, <2 x i32> %n) { +; CHECK-LABEL: @fneg_ldexp_vector( +; CHECK-NEXT: [[TMP1:%.*]] = fneg <2 x float> [[X:%.*]] +; CHECK-NEXT: [[NEG:%.*]] = call <2 x float> @llvm.ldexp.v2f32.v2i32(<2 x float> [[TMP1]], <2 x i32> [[N:%.*]]) +; CHECK-NEXT: ret <2 x float> [[NEG]] +; + %ldexp = call <2 x float> @llvm.ldexp.v2f32.v2i32(<2 x float> %x, <2 x i32> %n) + %neg = fneg <2 x float> %ldexp + ret <2 x float> %neg +} + +define float @fneg_ldexp_multiuse(float %x, i32 %n, ptr %ptr) { +; CHECK-LABEL: @fneg_ldexp_multiuse( +; CHECK-NEXT: [[LDEXP:%.*]] = call float @llvm.ldexp.f32.i32(float [[X:%.*]], i32 [[N:%.*]]) +; CHECK-NEXT: store float [[LDEXP]], ptr [[PTR:%.*]], align 4 +; CHECK-NEXT: [[NEG:%.*]] = fneg float [[LDEXP]] +; CHECK-NEXT: ret float [[NEG]] +; + %ldexp = call float @llvm.ldexp.f32.i32(float %x, i32 %n) + store float %ldexp, ptr %ptr + %neg = fneg float %ldexp + ret float %neg +} + +define float @fneg_ldexp_fmf_ldexp(float %x, i32 %n) { +; CHECK-LABEL: @fneg_ldexp_fmf_ldexp( +; CHECK-NEXT: [[TMP1:%.*]] = fneg nnan float [[X:%.*]] +; CHECK-NEXT: [[NEG:%.*]] = call nnan float @llvm.ldexp.f32.i32(float [[TMP1]], i32 [[N:%.*]]) +; CHECK-NEXT: ret float [[NEG]] +; + %ldexp = call nnan float @llvm.ldexp.f32.i32(float %x, i32 %n) + %neg = fneg float %ldexp + ret float %neg +} + +define float @fneg_ldexp_fmf_neg(float %x, i32 %n) { +; CHECK-LABEL: @fneg_ldexp_fmf_neg( +; CHECK-NEXT: [[TMP1:%.*]] = fneg nnan float [[X:%.*]] +; CHECK-NEXT: [[NEG:%.*]] = call nnan float @llvm.ldexp.f32.i32(float [[TMP1]], i32 [[N:%.*]]) +; CHECK-NEXT: ret float [[NEG]] +; + %ldexp = call float @llvm.ldexp.f32.i32(float %x, i32 %n) + %neg = fneg nnan float %ldexp + ret float %neg +} + +define float @fneg_ldexp_fmf(float %x, i32 %n) { +; CHECK-LABEL: @fneg_ldexp_fmf( +; CHECK-NEXT: [[TMP1:%.*]] = fneg nnan ninf float [[X:%.*]] +; CHECK-NEXT: [[NEG:%.*]] = call nnan ninf float @llvm.ldexp.f32.i32(float [[TMP1]], i32 [[N:%.*]]) +; CHECK-NEXT: ret float [[NEG]] +; + %ldexp = call ninf float @llvm.ldexp.f32.i32(float %x, i32 %n) + %neg = fneg nnan float %ldexp + ret float %neg +} + +define float @fneg_ldexp_contract0(float %x, i32 %n) { +; CHECK-LABEL: @fneg_ldexp_contract0( +; CHECK-NEXT: [[TMP1:%.*]] = fneg contract float [[X:%.*]] +; CHECK-NEXT: [[NEG:%.*]] = call contract float @llvm.ldexp.f32.i32(float [[TMP1]], i32 [[N:%.*]]) +; CHECK-NEXT: ret float [[NEG]] +; + %ldexp = call contract float @llvm.ldexp.f32.i32(float %x, i32 %n) + %neg = fneg float %ldexp + ret float %neg +} + +define float @fneg_ldexp_contract1(float %x, i32 %n) { +; CHECK-LABEL: @fneg_ldexp_contract1( +; CHECK-NEXT: [[TMP1:%.*]] = fneg contract float [[X:%.*]] +; CHECK-NEXT: [[NEG:%.*]] = call contract float @llvm.ldexp.f32.i32(float [[TMP1]], i32 [[N:%.*]]) +; CHECK-NEXT: ret float [[NEG]] +; + %ldexp = call float @llvm.ldexp.f32.i32(float %x, i32 %n) + %neg = fneg contract float %ldexp + ret float %neg +} + +define float @fneg_ldexp_contract(float %x, i32 %n) { +; CHECK-LABEL: @fneg_ldexp_contract( +; CHECK-NEXT: [[TMP1:%.*]] = fneg contract float [[X:%.*]] +; CHECK-NEXT: [[NEG:%.*]] = call contract float @llvm.ldexp.f32.i32(float [[TMP1]], i32 [[N:%.*]]) +; CHECK-NEXT: ret float [[NEG]] +; + %ldexp = call contract float @llvm.ldexp.f32.i32(float %x, i32 %n) + %neg = fneg contract float %ldexp + ret float %neg +} + +define float @fneg_ldexp_metadata(float %x, i32 %n) { +; CHECK-LABEL: @fneg_ldexp_metadata( +; CHECK-NEXT: [[TMP1:%.*]] = fneg float [[X:%.*]] +; CHECK-NEXT: [[NEG:%.*]] = call float @llvm.ldexp.f32.i32(float [[TMP1]], i32 [[N:%.*]]), !arst !0 +; CHECK-NEXT: ret float [[NEG]] +; + %ldexp = call float @llvm.ldexp.f32.i32(float %x, i32 %n), !arst !0 + %neg = fneg float %ldexp + ret float %neg +} + +!0 = !{}