Index: lib/Transforms/InstCombine/InstCombineCalls.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineCalls.cpp +++ lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -2114,6 +2114,54 @@ break; } + case Intrinsic::exp: { + if (II->isFast()) { + Value *SrcSrc = nullptr; + Value *Src = II->getArgOperand(0); + // exp(log(x)) -> x + if (match(Src, m_Intrinsic(m_Value(SrcSrc)))) { + return replaceInstUsesWith(CI, SrcSrc); + } + } + + break; + } + case Intrinsic::exp2: { + if (II->isFast()) { + Value *SrcSrc = nullptr; + Value *Src = II->getArgOperand(0); + // exp2(log2(x)) -> x + if (match(Src, m_Intrinsic(m_Value(SrcSrc)))) { + return replaceInstUsesWith(CI, SrcSrc); + } + } + + break; + } + case Intrinsic::log: { + if (II->isFast()) { + Value *SrcSrc = nullptr; + Value *Src = II->getArgOperand(0); + // log(exp(x)) -> x + if (match(Src, m_Intrinsic(m_Value(SrcSrc)))) { + return replaceInstUsesWith(CI, SrcSrc); + } + } + + break; + } + case Intrinsic::log2: { + if (II->isFast()) { + Value *SrcSrc = nullptr; + Value *Src = II->getArgOperand(0); + // log2(exp2(x)) -> x + if (match(Src, m_Intrinsic(m_Value(SrcSrc)))) { + return replaceInstUsesWith(CI, SrcSrc); + } + } + + break; + } case Intrinsic::ppc_altivec_lvx: case Intrinsic::ppc_altivec_lvxl: // Turn PPC lvx -> load if the pointer is known aligned. Index: test/Transforms/InstCombine/exp-intrinsic.ll =================================================================== --- /dev/null +++ test/Transforms/InstCombine/exp-intrinsic.ll @@ -0,0 +1,46 @@ +; RUN: opt < %s -instcombine -S | FileCheck %s + +declare double @llvm.exp.f64(double) +declare double @llvm.log.f64(double) + +; CHECK-LABEL: @exp_log( +; CHECK-NEXT: %1 = call double @llvm.log.f64(double %a) +; CHECK-NEXT: %2 = call double @llvm.exp.f64(double %1) +; CHECK-NEXT: ret double %2 +define double @exp_log(double %a) { + %1 = call double @llvm.log.f64(double %a) + %2 = call double @llvm.exp.f64(double %1) + ret double %2 +} + +; CHECK-LABEL: @exp_log_fast( +; CHECK-NEXT: ret double %a +define double @exp_log_fast(double %a) { + %1 = call fast double @llvm.log.f64(double %a) + %2 = call fast double @llvm.exp.f64(double %1) + ret double %2 +} + +; CHECK-LABEL: @exp_log_exp_log( +; CHECK-NEXT: %1 = call double @llvm.log.f64(double %a) +; CHECK-NEXT: %2 = call double @llvm.exp.f64(double %1) +; CHECK-NEXT: %3 = call double @llvm.log.f64(double %2) +; CHECK-NEXT: %4 = call double @llvm.exp.f64(double %3) +; CHECK-NEXT: ret double %4 +define double @exp_log_exp_log(double %a) { + %1 = call double @llvm.log.f64(double %a) + %2 = call double @llvm.exp.f64(double %1) + %3 = call double @llvm.log.f64(double %2) + %4 = call double @llvm.exp.f64(double %3) + ret double %4 +} + +; CHECK-LABEL: @exp_log_exp_log_fast( +; CHECK-NEXT: ret double %a +define double @exp_log_exp_log_fast(double %a) { + %1 = call fast double @llvm.log.f64(double %a) + %2 = call fast double @llvm.exp.f64(double %1) + %3 = call fast double @llvm.log.f64(double %2) + %4 = call fast double @llvm.exp.f64(double %3) + ret double %4 +} \ No newline at end of file Index: test/Transforms/InstCombine/exp2-intrinsic.ll =================================================================== --- /dev/null +++ test/Transforms/InstCombine/exp2-intrinsic.ll @@ -0,0 +1,46 @@ +; RUN: opt < %s -instcombine -S | FileCheck %s + +declare double @llvm.exp2.f64(double) +declare double @llvm.log2.f64(double) + +; CHECK-LABEL: @exp2_log2( +; CHECK-NEXT: %1 = call double @llvm.log2.f64(double %a) +; CHECK-NEXT: %2 = call double @llvm.exp2.f64(double %1) +; CHECK-NEXT: ret double %2 +define double @exp2_log2(double %a) { + %1 = call double @llvm.log2.f64(double %a) + %2 = call double @llvm.exp2.f64(double %1) + ret double %2 +} + +; CHECK-LABEL: @exp2_log2_fast( +; CHECK-NEXT: ret double %a +define double @exp2_log2_fast(double %a) { + %1 = call fast double @llvm.log2.f64(double %a) + %2 = call fast double @llvm.exp2.f64(double %1) + ret double %2 +} + +; CHECK-LABEL: @exp2_log2_exp2_log2( +; CHECK-NEXT: %1 = call double @llvm.log2.f64(double %a) +; CHECK-NEXT: %2 = call double @llvm.exp2.f64(double %1) +; CHECK-NEXT: %3 = call double @llvm.log2.f64(double %2) +; CHECK-NEXT: %4 = call double @llvm.exp2.f64(double %3) +; CHECK-NEXT: ret double %4 +define double @exp2_log2_exp2_log2(double %a) { + %1 = call double @llvm.log2.f64(double %a) + %2 = call double @llvm.exp2.f64(double %1) + %3 = call double @llvm.log2.f64(double %2) + %4 = call double @llvm.exp2.f64(double %3) + ret double %4 +} + +; CHECK-LABEL: @exp2_log2_exp2_log2_fast( +; CHECK-NEXT: ret double %a +define double @exp2_log2_exp2_log2_fast(double %a) { + %1 = call fast double @llvm.log2.f64(double %a) + %2 = call fast double @llvm.exp2.f64(double %1) + %3 = call fast double @llvm.log2.f64(double %2) + %4 = call fast double @llvm.exp2.f64(double %3) + ret double %4 +} \ No newline at end of file Index: test/Transforms/InstCombine/log-intrinsic.ll =================================================================== --- /dev/null +++ test/Transforms/InstCombine/log-intrinsic.ll @@ -0,0 +1,46 @@ +; RUN: opt < %s -instcombine -S | FileCheck %s + +declare double @llvm.log.f64(double) +declare double @llvm.exp.f64(double) + +; CHECK-LABEL: @log_exp( +; CHECK-NEXT: %1 = call double @llvm.exp.f64(double %a) +; CHECK-NEXT: %2 = call double @llvm.log.f64(double %1) +; CHECK-NEXT: ret double %2 +define double @log_exp(double %a) { + %1 = call double @llvm.exp.f64(double %a) + %2 = call double @llvm.log.f64(double %1) + ret double %2 +} + +; CHECK-LABEL: @log_exp_fast( +; CHECK-NEXT: ret double %a +define double @log_exp_fast(double %a) { + %1 = call fast double @llvm.exp.f64(double %a) + %2 = call fast double @llvm.log.f64(double %1) + ret double %2 +} + +; CHECK-LABEL: @log_exp_log_exp( +; CHECK-NEXT: %1 = call double @llvm.exp.f64(double %a) +; CHECK-NEXT: %2 = call double @llvm.log.f64(double %1) +; CHECK-NEXT: %3 = call double @llvm.exp.f64(double %2) +; CHECK-NEXT: %4 = call double @llvm.log.f64(double %3) +; CHECK-NEXT: ret double %4 +define double @log_exp_log_exp(double %a) { + %1 = call double @llvm.exp.f64(double %a) + %2 = call double @llvm.log.f64(double %1) + %3 = call double @llvm.exp.f64(double %2) + %4 = call double @llvm.log.f64(double %3) + ret double %4 +} + +; CHECK-LABEL: @log_exp_log_exp_fast( +; CHECK-NEXT: ret double %a +define double @log_exp_log_exp_fast(double %a) { + %1 = call fast double @llvm.exp.f64(double %a) + %2 = call fast double @llvm.log.f64(double %1) + %3 = call fast double @llvm.exp.f64(double %2) + %4 = call fast double @llvm.log.f64(double %3) + ret double %4 +} \ No newline at end of file Index: test/Transforms/InstCombine/log2-intrinsic.ll =================================================================== --- /dev/null +++ test/Transforms/InstCombine/log2-intrinsic.ll @@ -0,0 +1,46 @@ +; RUN: opt < %s -instcombine -S | FileCheck %s + +declare double @llvm.log2.f64(double) +declare double @llvm.exp2.f64(double) + +; CHECK-LABEL: @log2_exp2( +; CHECK-NEXT: %1 = call double @llvm.exp2.f64(double %a) +; CHECK-NEXT: %2 = call double @llvm.log2.f64(double %1) +; CHECK-NEXT: ret double %2 +define double @log2_exp2(double %a) { + %1 = call double @llvm.exp2.f64(double %a) + %2 = call double @llvm.log2.f64(double %1) + ret double %2 +} + +; CHECK-LABEL: @log2_exp2_fast( +; CHECK-NEXT: ret double %a +define double @log2_exp2_fast(double %a) { + %1 = call fast double @llvm.exp2.f64(double %a) + %2 = call fast double @llvm.log2.f64(double %1) + ret double %2 +} + +; CHECK-LABEL: @log2_exp2_log2_exp2( +; CHECK-NEXT: %1 = call double @llvm.exp2.f64(double %a) +; CHECK-NEXT: %2 = call double @llvm.log2.f64(double %1) +; CHECK-NEXT: %3 = call double @llvm.exp2.f64(double %2) +; CHECK-NEXT: %4 = call double @llvm.log2.f64(double %3) +; CHECK-NEXT: ret double %4 +define double @log2_exp2_log2_exp2(double %a) { + %1 = call double @llvm.exp2.f64(double %a) + %2 = call double @llvm.log2.f64(double %1) + %3 = call double @llvm.exp2.f64(double %2) + %4 = call double @llvm.log2.f64(double %3) + ret double %4 +} + +; CHECK-LABEL: @log2_exp2_log2_exp2_fast( +; CHECK-NEXT: ret double %a +define double @log2_exp2_log2_exp2_fast(double %a) { + %1 = call fast double @llvm.exp2.f64(double %a) + %2 = call fast double @llvm.log2.f64(double %1) + %3 = call fast double @llvm.exp2.f64(double %2) + %4 = call fast double @llvm.log2.f64(double %3) + ret double %4 +} \ No newline at end of file