From: Owen Anderson Date: Thu, 16 Jan 2014 20:36:42 +0000 (+0000) Subject: Teach InstCombine that (fmul X, -1.0) can be simplified to (fneg X), which LLVM expre... X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=a2a8bbb30f271843dd0678f563a62154919e09b4;p=oota-llvm.git Teach InstCombine that (fmul X, -1.0) can be simplified to (fneg X), which LLVM expresses as (fsub -0.0, X). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@199420 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp index a7595482eed..6c34b1bf566 100644 --- a/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp +++ b/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp @@ -426,6 +426,16 @@ Instruction *InstCombiner::visitFMul(BinaryOperator &I) { return NV; ConstantFP *C = dyn_cast(Op1); + + // (fmul X, -1.0) --> (fsub -0.0, X) + if (C && C->isExactlyValue(-1.0)) { + Instruction *RI = BinaryOperator::CreateFSub( + ConstantFP::getNegativeZero(C->getType()), + Op0); + RI->copyFastMathFlags(&I); + return RI; + } + if (C && AllowReassociate && C->getValueAPF().isFiniteNonZero()) { // Let MDC denote an expression in one of these forms: // X * C, C/X, X/C, where C is a constant. diff --git a/test/Transforms/InstCombine/fmul.ll b/test/Transforms/InstCombine/fmul.ll index 402ee52e624..71b7138b644 100644 --- a/test/Transforms/InstCombine/fmul.ll +++ b/test/Transforms/InstCombine/fmul.ll @@ -93,3 +93,15 @@ for.body: ; preds = %for.cond for.end: ; preds = %for.cond ret void } + +; X * -1.0 => -0.0 - X +define float @test9(float %x) { + %mul = fmul float %x, -1.0 + ret float %mul + +; CHECK-LABEL: @test9( +; CHECK-NOT: fmul +; CHECK: fsub +} + +