[llvm] [InstCombine] Optimize `sinh` and `cosh` divisions (PR #81433)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Wed May 8 08:40:45 PDT 2024


================
@@ -0,0 +1,114 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -passes=instcombine < %s | FileCheck %s
+
+define double @fdiv_cosh_sinh(double %a) {
+; CHECK-LABEL: @fdiv_cosh_sinh(
+; CHECK-NEXT:    [[TMP1:%.*]] = call double @cosh(double [[A:%.*]])
+; CHECK-NEXT:    [[TMP2:%.*]] = call double @sinh(double [[A]])
+; CHECK-NEXT:    [[DIV:%.*]] = fdiv double [[TMP1]], [[TMP2]]
+; CHECK-NEXT:    ret double [[DIV]]
+;
+  %cosh = call double @cosh(double %a)
+  %sinh = call double @sinh(double %a)
+  %div = fdiv double %cosh, %sinh
+  ret double %div
+}
+
+define double @fdiv_strict_cosh_strict_sinh_reassoc(double %a) {
+; CHECK-LABEL: @fdiv_strict_cosh_strict_sinh_reassoc(
+; CHECK-NEXT:    [[TMP1:%.*]] = call double @cosh(double [[A:%.*]])
+; CHECK-NEXT:    [[TMP2:%.*]] = call reassoc double @sinh(double [[A]])
+; CHECK-NEXT:    [[DIV:%.*]] = fdiv double [[TMP1]], [[TMP2]]
+; CHECK-NEXT:    ret double [[DIV]]
+;
+  %cosh = call double @cosh(double %a)
+  %sinh = call reassoc double @sinh(double %a)
+  %div = fdiv double %cosh, %sinh
+  ret double %div
+}
+
+define double @fdiv_reassoc_cosh_strict_sinh_strict(double %a, ptr dereferenceable(2) %dummy) {
+; CHECK-LABEL: @fdiv_reassoc_cosh_strict_sinh_strict(
+; CHECK-NEXT:    [[TANH:%.*]] = call reassoc double @tanh(double [[A]])
+; CHECK-NEXT:    [[DIV:%.*]] = fdiv reassoc double 1.000000e+00, [[TANH]]
+; CHECK-NEXT:    ret double [[DIV]]
+;
+  %cosh = call double @cosh(double %a)
+  %sinh = call double @sinh(double %a)
+  %div = fdiv reassoc double %cosh, %sinh
+  ret double %div
+}
+
+define double @fdiv_reassoc_cosh_reassoc_sinh_strict(double %a) {
+; CHECK-LABEL: @fdiv_reassoc_cosh_reassoc_sinh_strict(
+; CHECK-NEXT:    [[TANH:%.*]] = call reassoc double @tanh(double [[A]])
+; CHECK-NEXT:    [[DIV:%.*]] = fdiv reassoc double 1.000000e+00, [[TANH]]
+; CHECK-NEXT:    ret double [[DIV]]
+;
+  %cosh = call reassoc double @cosh(double %a)
+  %sinh = call double @sinh(double %a)
+  %div = fdiv reassoc double %cosh, %sinh
+  ret double %div
+}
+
+define double @fdiv_cosh_sinh_reassoc_multiple_uses(double %a) {
+; CHECK-LABEL: @fdiv_cosh_sinh_reassoc_multiple_uses(
+; CHECK-NEXT:    [[TMP1:%.*]] = call reassoc double @cosh(double [[A:%.*]])
+; CHECK-NEXT:    [[TMP2:%.*]] = call reassoc double @sinh(double [[A]])
+; CHECK-NEXT:    [[DIV:%.*]] = fdiv reassoc double [[TMP1]], [[TMP2]]
+; CHECK-NEXT:    call void @use(double [[TMP2]])
+; CHECK-NEXT:    ret double [[DIV]]
+;
+  %cosh = call reassoc double @cosh(double %a)
+  %sinh = call reassoc double @sinh(double %a)
+  %div = fdiv reassoc double %cosh, %sinh
+  call void @use(double %sinh)
+  ret double %div
+}
+
+define double @fdiv_cosh_sinh_reassoc(double %a){
+; CHECK-LABEL: @fdiv_cosh_sinh_reassoc(
+; CHECK-NEXT:    [[TANH:%.*]] = call reassoc double @tanh(double [[A]])
+; CHECK-NEXT:    [[DIV:%.*]] = fdiv reassoc double 1.000000e+00, [[TANH]]
+; CHECK-NEXT:    ret double [[DIV]]
+;
+  %cosh = call reassoc double @cosh(double %a)
+  %sinh = call reassoc double @sinh(double %a)
+  %div = fdiv reassoc double %cosh, %sinh
+  ret double %div
+}
+
+define fp128 @fdiv_coshl_sinhl_reassoc(fp128 %a){
+; CHECK-LABEL: @fdiv_coshl_sinhl_reassoc(
+; CHECK-NEXT:    [[TANH:%.*]] = call reassoc fp128 @tanhl(fp128 [[A]])
+; CHECK-NEXT:    [[DIV:%.*]] = fdiv reassoc fp128 0xL00000000000000003FFF000000000000, [[TANH]]
+; CHECK-NEXT:    ret fp128 [[DIV]]
+;
+  %cosh = call reassoc fp128 @coshl(fp128 %a)
+  %sinh = call reassoc fp128 @sinhl(fp128 %a)
+  %div = fdiv reassoc fp128 %cosh, %sinh
+  ret fp128 %div
+}
+
+
+define float @fdiv_coshf_sinhf_reassoc(float %a){
+; CHECK-LABEL: @fdiv_coshf_sinhf_reassoc(
+; CHECK-NEXT:    [[TANH:%.*]] = call reassoc float @tanhf(float [[A]])
+; CHECK-NEXT:    [[DIV:%.*]] = fdiv reassoc float 1.000000e+00, [[TANH]]
+; CHECK-NEXT:    ret float [[DIV]]
+;
+  %coshf = call reassoc float @coshf(float %a)
+  %sinhf = call reassoc float @sinhf(float %a)
+  %div = fdiv reassoc float %coshf, %sinhf
+  ret float %div
+}
+
----------------
arsenm wrote:

Test preservation of other flags? 

https://github.com/llvm/llvm-project/pull/81433


More information about the llvm-commits mailing list