From d2e7df3a2251316d04e1759721b80a62d2b0ba48 Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Wed, 25 Sep 2019 19:33:26 +0000 Subject: [PATCH] [InstSimplify] Match 1.0 and 0.0 for both operands in SimplifyFMAMul Because we do not constant fold multiplications in SimplifyFMAMul, we match 1.0 and 0.0 for both operands, as multiplying by them is guaranteed to produce an exact result (if it is allowed to do so). Note that it is not enough to just swap the operands to ensure a constant is on the RHS, as we want to also cover the case with 2 constants. Reviewers: lebedev.ri, spatel, reames, scanon Reviewed By: lebedev.ri, reames Differential Revision: https://reviews.llvm.org/D67553 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@372915 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/InstructionSimplify.cpp | 8 ++++++++ test/Transforms/InstCombine/fma.ll | 5 ++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/Analysis/InstructionSimplify.cpp b/lib/Analysis/InstructionSimplify.cpp index 4ae052eb14b..dd477fe1c5d 100644 --- a/lib/Analysis/InstructionSimplify.cpp +++ b/lib/Analysis/InstructionSimplify.cpp @@ -4582,10 +4582,18 @@ static Value *SimplifyFMAFMul(Value *Op0, Value *Op1, FastMathFlags FMF, if (match(Op1, m_FPOne())) return Op0; + // fmul 1.0, X ==> X + if (match(Op0, m_FPOne())) + return Op1; + // fmul nnan nsz X, 0 ==> 0 if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op1, m_AnyZeroFP())) return ConstantFP::getNullValue(Op0->getType()); + // fmul nnan nsz 0, X ==> 0 + if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op0, m_AnyZeroFP())) + return ConstantFP::getNullValue(Op1->getType()); + // sqrt(X) * sqrt(X) --> X, if we can: // 1. Remove the intermediate rounding (reassociate). // 2. Ignore non-zero negative numbers because sqrt would produce NAN. diff --git a/test/Transforms/InstCombine/fma.ll b/test/Transforms/InstCombine/fma.ll index 29d372fca05..33dc8100b58 100644 --- a/test/Transforms/InstCombine/fma.ll +++ b/test/Transforms/InstCombine/fma.ll @@ -460,8 +460,7 @@ entry: define <2 x double> @fma_const_fmul_zero(<2 x double> %b) { ; CHECK-LABEL: @fma_const_fmul_zero( ; CHECK-NEXT: entry: -; CHECK-NEXT: [[RES:%.*]] = call nnan nsz <2 x double> @llvm.fma.v2f64(<2 x double> zeroinitializer, <2 x double> , <2 x double> [[B:%.*]]) -; CHECK-NEXT: ret <2 x double> [[RES]] +; CHECK-NEXT: ret <2 x double> [[B:%.*]] ; entry: %res = call nnan nsz <2 x double> @llvm.fma.v2f64(<2 x double> , <2 x double> , <2 x double> %b) @@ -481,7 +480,7 @@ entry: define <2 x double> @fma_const_fmul_one(<2 x double> %b) { ; CHECK-LABEL: @fma_const_fmul_one( ; CHECK-NEXT: entry: -; CHECK-NEXT: [[RES:%.*]] = call nnan nsz <2 x double> @llvm.fma.v2f64(<2 x double> , <2 x double> , <2 x double> [[B:%.*]]) +; CHECK-NEXT: [[RES:%.*]] = fadd nnan nsz <2 x double> [[B:%.*]], ; CHECK-NEXT: ret <2 x double> [[RES]] ; entry: -- 2.40.0