Index: lib/Transforms/InstCombine/InstCombineMulDivRem.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineMulDivRem.cpp +++ lib/Transforms/InstCombine/InstCombineMulDivRem.cpp @@ -728,6 +728,20 @@ } } + // exp(a) * exp(b) -> exp(a + b) + if (AllowReassociate) { + Value *Opnd0 = nullptr; + Value *Opnd1 = nullptr; + if (match(Op0, m_Intrinsic(m_Value(Opnd0))) && + match(Op1, m_Intrinsic(m_Value(Opnd1)))) { + Builder.setFastMathFlags(I.getFastMathFlags()); + Value *FAddVal = Builder.CreateFAdd(Opnd0, Opnd1); + Value *Exp = Intrinsic::getDeclaration(I.getModule(), Intrinsic::exp, I.getType()); + Value *ExpCall = Builder.CreateCall(Exp, FAddVal); + return replaceInstUsesWith(I, ExpCall); + } + } + // Handle symmetric situation in a 2-iteration loop Value *Opnd0 = Op0; Value *Opnd1 = Op1; Index: test/Transforms/InstCombine/fmul.ll =================================================================== --- test/Transforms/InstCombine/fmul.ll +++ test/Transforms/InstCombine/fmul.ll @@ -181,3 +181,31 @@ %mul = fmul float %x.fabs, %y.fabs ret float %mul } + +; CHECK-LABEL @exp_a_exp_b( +; CHECK: fadd fast double %a, %b +; CHECK: call fast double @llvm.exp.f64(double %1) +define double @exp_a_exp_b(double %a, double %b) { + %1 = call fast double @llvm.exp.f64(double %a) + %2 = call fast double @llvm.exp.f64(double %b) + %mul = fmul fast double %1, %2 + ret double %mul +} + +declare double @llvm.exp.f64(double) nounwind readnone speculatable + +; CHECK-LABEL @exp_a_exp_b_exp_c_exp_d( +; CHECK: fadd fast double %a, %b +; CHECK: fadd fast double %1, %c +; CHECK: fadd fast double %2, %d +; CHECK: call fast double @llvm.exp.f64(double %3) +define double @exp_a_exp_b_exp_c_exp_d(double %a, double %b, double %c, double %d) { + %1 = call fast double @llvm.exp.f64(double %a) + %2 = call fast double @llvm.exp.f64(double %b) + %mul = fmul fast double %1, %2 + %3 = call fast double @llvm.exp.f64(double %c) + %mul1 = fmul fast double %mul, %3 + %4 = call fast double @llvm.exp.f64(double %d) + %mul2 = fmul fast double %mul1, %4 + ret double %mul2 +}