Index: llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp @@ -2520,17 +2520,35 @@ return nullptr; } -static Instruction *hoistFNegAboveFMulFDiv(Value *FNegOp, - Instruction &FMFSource, - InstCombiner::BuilderTy &Builder) { +Instruction *InstCombinerImpl::hoistFNegAboveFMulFDiv(Value *FNegOp, + Instruction &FMFSource) { Value *X, *Y; - if (match(FNegOp, m_FMul(m_Value(X), m_Value(Y)))) - return BinaryOperator::CreateFMulFMF(Builder.CreateFNegFMF(X, &FMFSource), - Y, &FMFSource); + if (match(FNegOp, m_FMul(m_Value(X), m_Value(Y)))) { + return cast(Builder.CreateFMulFMF( + Builder.CreateFNegFMF(X, &FMFSource), Y, &FMFSource)); + } + + if (match(FNegOp, m_FDiv(m_Value(X), m_Value(Y)))) { + return cast(Builder.CreateFDivFMF( + Builder.CreateFNegFMF(X, &FMFSource), Y, &FMFSource)); + } + + if (IntrinsicInst *II = dyn_cast(FNegOp)) { + // Make sure to preserve flags and metadata on the call. + if (II->getIntrinsicID() == Intrinsic::ldexp) { + FastMathFlags FMF = FMFSource.getFastMathFlags(); + FMF |= II->getFastMathFlags(); - if (match(FNegOp, m_FDiv(m_Value(X), m_Value(Y)))) - return BinaryOperator::CreateFDivFMF(Builder.CreateFNegFMF(X, &FMFSource), - Y, &FMFSource); + 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 New; + } + } return nullptr; } @@ -2556,8 +2574,8 @@ if (!match(Op, m_OneUse(m_Value(OneUse)))) return nullptr; - if (Instruction *R = hoistFNegAboveFMulFDiv(OneUse, I, Builder)) - return R; + if (Instruction *R = hoistFNegAboveFMulFDiv(OneUse, I)) + return replaceInstUsesWith(I, R); // Try to eliminate fneg if at least 1 arm of the select is negated. Value *Cond; Index: llvm/lib/Transforms/InstCombine/InstCombineInternal.h =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineInternal.h +++ llvm/lib/Transforms/InstCombine/InstCombineInternal.h @@ -392,6 +392,8 @@ Instruction *foldAndOrOfSelectUsingImpliedCond(Value *Op, SelectInst &SI, bool IsAnd); + Instruction *hoistFNegAboveFMulFDiv(Value *FNegOp, Instruction &FMFSource); + 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: [[LDEXP:%.*]] = call float @llvm.ldexp.f32.i32(float [[X:%.*]], i32 [[N:%.*]]) +; CHECK-NEXT: [[NEG:%.*]] = fneg float [[LDEXP]] +; 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: [[LDEXP:%.*]] = call float @llvm.ldexp.f32.i32(float [[X:%.*]], i32 [[N:%.*]]) +; CHECK-NEXT: [[NEG:%.*]] = fneg float [[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 float @fsub_fneg_ldexp_nsz(float %x, i32 %n) { +; CHECK-LABEL: @fsub_fneg_ldexp_nsz( +; CHECK-NEXT: [[LDEXP:%.*]] = call float @llvm.ldexp.f32.i32(float [[X:%.*]], i32 [[N:%.*]]) +; CHECK-NEXT: [[NEG:%.*]] = fneg nsz float [[LDEXP]] +; 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: [[LDEXP:%.*]] = call float @llvm.ldexp.f32.i32(float [[X:%.*]], i32 [[N:%.*]]) +; CHECK-NEXT: [[NEG:%.*]] = fneg nsz float [[LDEXP]] +; 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: [[LDEXP:%.*]] = call <2 x float> @llvm.ldexp.v2f32.v2i32(<2 x float> [[X:%.*]], <2 x i32> [[N:%.*]]) +; CHECK-NEXT: [[NEG:%.*]] = fneg <2 x float> [[LDEXP]] +; 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: [[LDEXP:%.*]] = call nnan float @llvm.ldexp.f32.i32(float [[X:%.*]], i32 [[N:%.*]]) +; CHECK-NEXT: [[NEG:%.*]] = fneg float [[LDEXP]] +; 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: [[LDEXP:%.*]] = call float @llvm.ldexp.f32.i32(float [[X:%.*]], i32 [[N:%.*]]) +; CHECK-NEXT: [[NEG:%.*]] = fneg nnan float [[LDEXP]] +; 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: [[LDEXP:%.*]] = call ninf float @llvm.ldexp.f32.i32(float [[X:%.*]], i32 [[N:%.*]]) +; CHECK-NEXT: [[NEG:%.*]] = fneg nnan float [[LDEXP]] +; 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: [[LDEXP:%.*]] = call contract float @llvm.ldexp.f32.i32(float [[X:%.*]], i32 [[N:%.*]]) +; CHECK-NEXT: [[NEG:%.*]] = fneg float [[LDEXP]] +; 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: [[LDEXP:%.*]] = call float @llvm.ldexp.f32.i32(float [[X:%.*]], i32 [[N:%.*]]) +; CHECK-NEXT: [[NEG:%.*]] = fneg contract float [[LDEXP]] +; 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: [[LDEXP:%.*]] = call contract float @llvm.ldexp.f32.i32(float [[X:%.*]], i32 [[N:%.*]]) +; CHECK-NEXT: [[NEG:%.*]] = fneg contract float [[LDEXP]] +; 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: [[LDEXP:%.*]] = call float @llvm.ldexp.f32.i32(float [[X:%.*]], i32 [[N:%.*]]), !arst !0 +; CHECK-NEXT: [[NEG:%.*]] = fneg float [[LDEXP]] +; 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 = !{}