Index: lib/Transforms/InstCombine/InstCombineMulDivRem.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineMulDivRem.cpp +++ lib/Transforms/InstCombine/InstCombineMulDivRem.cpp @@ -441,6 +441,26 @@ return replaceInstUsesWith(I, Sqrt); } + // exp(X) * exp(Y) -> exp(X + Y) + // Match as long as at least one of exp has only one use. + if (match(Op0, m_Intrinsic(m_Value(X))) && + match(Op1, m_Intrinsic(m_Value(Y))) && + (Op0->hasOneUse() || Op1->hasOneUse())) { + Value *XY = Builder.CreateFAddFMF(X, Y, &I); + Value *Exp = Builder.CreateUnaryIntrinsic(Intrinsic::exp, XY, &I); + return replaceInstUsesWith(I, Exp); + } + + // exp2(X) * exp2(Y) -> exp2(X + Y) + // Match as long as at least one of exp2 has only one use. + if (match(Op0, m_Intrinsic(m_Value(X))) && + match(Op1, m_Intrinsic(m_Value(Y))) && + (Op0->hasOneUse() || Op1->hasOneUse())) { + Value *XY = Builder.CreateFAddFMF(X, Y, &I); + Value *Exp2 = Builder.CreateUnaryIntrinsic(Intrinsic::exp2, XY, &I); + return replaceInstUsesWith(I, Exp2); + } + // (X*Y) * X => (X*X) * Y where Y != X // The purpose is two-fold: // 1) to form a power expression (of X). Index: test/Transforms/InstCombine/fmul-exp.ll =================================================================== --- test/Transforms/InstCombine/fmul-exp.ll +++ test/Transforms/InstCombine/fmul-exp.ll @@ -21,11 +21,11 @@ ; exp(a) * exp(b) reassoc, multiple uses define double @exp_a_exp_b_multiple_uses(double %a, double %b) { ; CHECK-LABEL: @exp_a_exp_b_multiple_uses( -; CHECK-NEXT: [[TMP:%.*]] = call double @llvm.exp.f64(double [[A:%.*]]) ; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.exp.f64(double [[B:%.*]]) -; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc double [[TMP]], [[TMP1]] +; CHECK-NEXT: [[TMP:%.*]] = fadd reassoc double [[A:%.*]], [[B]] +; CHECK-NEXT: [[TMP2:%.*]] = call reassoc double @llvm.exp.f64(double [[TMP]]) ; CHECK-NEXT: call void @use(double [[TMP1]]) -; CHECK-NEXT: ret double [[MUL]] +; CHECK-NEXT: ret double [[TMP2]] ; %tmp = call double @llvm.exp.f64(double %a) %tmp1 = call double @llvm.exp.f64(double %b) @@ -55,10 +55,9 @@ ; exp(a) * exp(b) => exp(a+b) with reassoc define double @exp_a_exp_b_reassoc(double %a, double %b) { ; CHECK-LABEL: @exp_a_exp_b_reassoc( -; CHECK-NEXT: [[TMP:%.*]] = call double @llvm.exp.f64(double [[A:%.*]]) -; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.exp.f64(double [[B:%.*]]) -; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc double [[TMP]], [[TMP1]] -; CHECK-NEXT: ret double [[MUL]] +; CHECK-NEXT: [[TMP:%.*]] = fadd reassoc double [[A:%.*]], [[B:%.*]] +; CHECK-NEXT: [[TMP1:%.*]] = call reassoc double @llvm.exp.f64(double [[TMP]]) +; CHECK-NEXT: ret double [[TMP1]] ; %tmp = call double @llvm.exp.f64(double %a) %tmp1 = call double @llvm.exp.f64(double %b) @@ -69,14 +68,11 @@ ; exp(a) * exp(b) * exp(c) * exp(d) => exp(a+b+c+d) with reassoc define double @exp_a_exp_b_exp_c_exp_d_fast(double %a, double %b, double %c, double %d) { ; CHECK-LABEL: @exp_a_exp_b_exp_c_exp_d_fast( -; CHECK-NEXT: [[TMP:%.*]] = call double @llvm.exp.f64(double [[A:%.*]]) -; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.exp.f64(double [[B:%.*]]) -; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc double [[TMP]], [[TMP1]] -; CHECK-NEXT: [[TMP2:%.*]] = call double @llvm.exp.f64(double [[C:%.*]]) -; CHECK-NEXT: [[MUL1:%.*]] = fmul reassoc double [[MUL]], [[TMP2]] -; CHECK-NEXT: [[TMP3:%.*]] = call double @llvm.exp.f64(double [[D:%.*]]) -; CHECK-NEXT: [[MUL2:%.*]] = fmul reassoc double [[MUL1]], [[TMP3]] -; CHECK-NEXT: ret double [[MUL2]] +; CHECK-NEXT: [[TMP:%.*]] = fadd reassoc double [[A:%.*]], [[B:%.*]] +; CHECK-NEXT: [[TMP1:%.*]] = fadd reassoc double [[TMP]], [[C:%.*]] +; CHECK-NEXT: [[TMP2:%.*]] = fadd reassoc double [[TMP1]], [[D:%.*]] +; CHECK-NEXT: [[TMP3:%.*]] = call reassoc double @llvm.exp.f64(double [[TMP2]]) +; CHECK-NEXT: ret double [[TMP3]] ; %tmp = call double @llvm.exp.f64(double %a) %tmp1 = call double @llvm.exp.f64(double %b) Index: test/Transforms/InstCombine/fmul-exp2.ll =================================================================== --- test/Transforms/InstCombine/fmul-exp2.ll +++ test/Transforms/InstCombine/fmul-exp2.ll @@ -21,11 +21,11 @@ ; exp2(a) * exp2(b) reassoc, multiple uses define double @exp2_a_exp2_b_multiple_uses(double %a, double %b) { ; CHECK-LABEL: @exp2_a_exp2_b_multiple_uses( -; CHECK-NEXT: [[TMP:%.*]] = call double @llvm.exp2.f64(double [[A:%.*]]) ; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.exp2.f64(double [[B:%.*]]) -; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc double [[TMP]], [[TMP1]] +; CHECK-NEXT: [[TMP:%.*]] = fadd reassoc double [[A:%.*]], [[B]] +; CHECK-NEXT: [[TMP2:%.*]] = call reassoc double @llvm.exp2.f64(double [[TMP]]) ; CHECK-NEXT: call void @use(double [[TMP1]]) -; CHECK-NEXT: ret double [[MUL]] +; CHECK-NEXT: ret double [[TMP2]] ; %tmp = call double @llvm.exp2.f64(double %a) %tmp1 = call double @llvm.exp2.f64(double %b) @@ -55,10 +55,9 @@ ; exp2(a) * exp2(b) => exp2(a+b) with reassoc define double @exp2_a_exp2_b_reassoc(double %a, double %b) { ; CHECK-LABEL: @exp2_a_exp2_b_reassoc( -; CHECK-NEXT: [[TMP:%.*]] = call double @llvm.exp2.f64(double [[A:%.*]]) -; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.exp2.f64(double [[B:%.*]]) -; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc double [[TMP]], [[TMP1]] -; CHECK-NEXT: ret double [[MUL]] +; CHECK-NEXT: [[TMP:%.*]] = fadd reassoc double [[A:%.*]], [[B:%.*]] +; CHECK-NEXT: [[TMP1:%.*]] = call reassoc double @llvm.exp2.f64(double [[TMP]]) +; CHECK-NEXT: ret double [[TMP1]] ; %tmp = call double @llvm.exp2.f64(double %a) %tmp1 = call double @llvm.exp2.f64(double %b) @@ -69,14 +68,11 @@ ; exp2(a) * exp2(b) * exp2(c) * exp2(d) => exp2(a+b+c+d) with reassoc define double @exp2_a_exp2_b_exp2_c_exp2_d(double %a, double %b, double %c, double %d) { ; CHECK-LABEL: @exp2_a_exp2_b_exp2_c_exp2_d( -; CHECK-NEXT: [[TMP:%.*]] = call double @llvm.exp2.f64(double [[A:%.*]]) -; CHECK-NEXT: [[TMP1:%.*]] = call double @llvm.exp2.f64(double [[B:%.*]]) -; CHECK-NEXT: [[MUL:%.*]] = fmul reassoc double [[TMP]], [[TMP1]] -; CHECK-NEXT: [[TMP2:%.*]] = call double @llvm.exp2.f64(double [[C:%.*]]) -; CHECK-NEXT: [[MUL1:%.*]] = fmul reassoc double [[MUL]], [[TMP2]] -; CHECK-NEXT: [[TMP3:%.*]] = call double @llvm.exp2.f64(double [[D:%.*]]) -; CHECK-NEXT: [[MUL2:%.*]] = fmul reassoc double [[MUL1]], [[TMP3]] -; CHECK-NEXT: ret double [[MUL2]] +; CHECK-NEXT: [[TMP:%.*]] = fadd reassoc double [[A:%.*]], [[B:%.*]] +; CHECK-NEXT: [[TMP1:%.*]] = fadd reassoc double [[TMP]], [[C:%.*]] +; CHECK-NEXT: [[TMP2:%.*]] = fadd reassoc double [[TMP1]], [[D:%.*]] +; CHECK-NEXT: [[TMP3:%.*]] = call reassoc double @llvm.exp2.f64(double [[TMP2]]) +; CHECK-NEXT: ret double [[TMP3]] ; %tmp = call double @llvm.exp2.f64(double %a) %tmp1 = call double @llvm.exp2.f64(double %b)