[llvm] r321710 - [InstSimplify] Missed optimization in math expression: squashing exp(log), log(exp)

Dmitry Venikov via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 3 06:37:42 PST 2018


Author: quolyk
Date: Wed Jan  3 06:37:42 2018
New Revision: 321710

URL: http://llvm.org/viewvc/llvm-project?rev=321710&view=rev
Log:
[InstSimplify] Missed optimization in math expression: squashing exp(log), log(exp)

Summary: This patch enables folding following expressions under -ffast-math flag: exp(log(x)) -> x, exp2(log2(x)) -> x, log(exp(x)) -> x, log2(exp2(x)) -> x

Reviewers: spatel, hfinkel, davide

Reviewed By: spatel, hfinkel, davide

Subscribers: scanon, llvm-commits

Differential Revision: https://reviews.llvm.org/D41381

Added:
    llvm/trunk/test/Transforms/InstSimplify/exp-intrinsic.ll
    llvm/trunk/test/Transforms/InstSimplify/exp2-intrinsic.ll
    llvm/trunk/test/Transforms/InstSimplify/log-intrinsic.ll
    llvm/trunk/test/Transforms/InstSimplify/log2-intrinsic.ll
Modified:
    llvm/trunk/lib/Analysis/InstructionSimplify.cpp

Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=321710&r1=321709&r2=321710&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
+++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Wed Jan  3 06:37:42 2018
@@ -4493,28 +4493,55 @@ static Value *SimplifyIntrinsic(Function
       }
     }
 
+    Value *IIOperand = *ArgBegin;
+    Value *X;
     switch (IID) {
     case Intrinsic::fabs: {
-      if (SignBitMustBeZero(*ArgBegin, Q.TLI))
-        return *ArgBegin;
+      if (SignBitMustBeZero(IIOperand, Q.TLI))
+        return IIOperand;
       return nullptr;
     }
     case Intrinsic::bswap: {
-      Value *IIOperand = *ArgBegin;
-      Value *X = nullptr;
       // bswap(bswap(x)) -> x
       if (match(IIOperand, m_BSwap(m_Value(X))))
         return X;
       return nullptr;
     }
     case Intrinsic::bitreverse: {
-      Value *IIOperand = *ArgBegin;
-      Value *X = nullptr;
       // bitreverse(bitreverse(x)) -> x
       if (match(IIOperand, m_BitReverse(m_Value(X))))
         return X;
       return nullptr;
     }
+    case Intrinsic::exp: {
+      // exp(log(x)) -> x
+      if (Q.CxtI->isFast() &&
+          match(IIOperand, m_Intrinsic<Intrinsic::log>(m_Value(X))))
+        return X;
+      return nullptr;
+    }
+    case Intrinsic::exp2: {
+      // exp2(log2(x)) -> x
+      if (Q.CxtI->isFast() &&
+          match(IIOperand, m_Intrinsic<Intrinsic::log2>(m_Value(X))))
+        return X;
+      return nullptr;
+    }
+    case Intrinsic::log: {
+      // log(exp(x)) -> x
+      if (Q.CxtI->isFast() &&
+          match(IIOperand, m_Intrinsic<Intrinsic::exp>(m_Value(X))))
+        return X;
+      return nullptr;
+    }
+    case Intrinsic::log2: {
+      // log2(exp2(x)) -> x
+      if (Q.CxtI->isFast() &&
+          match(IIOperand, m_Intrinsic<Intrinsic::exp2>(m_Value(X)))) {
+        return X;
+      }
+      return nullptr;
+    }
     default:
       return nullptr;
     }

Added: llvm/trunk/test/Transforms/InstSimplify/exp-intrinsic.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/exp-intrinsic.ll?rev=321710&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/exp-intrinsic.ll (added)
+++ llvm/trunk/test/Transforms/InstSimplify/exp-intrinsic.ll Wed Jan  3 06:37:42 2018
@@ -0,0 +1,71 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -instsimplify -S | FileCheck %s
+
+declare double @llvm.exp.f64(double)
+declare double @llvm.log.f64(double)
+
+define double @exp_log(double %a) {
+; CHECK-LABEL: @exp_log(
+; CHECK-NEXT:    [[TMP1:%.*]] = call double @llvm.log.f64(double [[A:%.*]])
+; CHECK-NEXT:    [[TMP2:%.*]] = call double @llvm.exp.f64(double [[TMP1]])
+; CHECK-NEXT:    ret double [[TMP2]]
+;
+  %1 = call double @llvm.log.f64(double %a)
+  %2 = call double @llvm.exp.f64(double %1)
+  ret double %2
+}
+
+define double @exp_log_fast(double %a) {
+; CHECK-LABEL: @exp_log_fast(
+; CHECK-NEXT:    ret double [[A:%.*]]
+;
+  %1 = call fast double @llvm.log.f64(double %a)
+  %2 = call fast double @llvm.exp.f64(double %1)
+  ret double %2
+}
+
+define double @exp_fast_log_strict(double %a) {
+; CHECK-LABEL: @exp_fast_log_strict(
+; CHECK-NEXT:    ret double [[A:%.*]]
+;
+  %1 = call double @llvm.log.f64(double %a)
+  %2 = call fast double @llvm.exp.f64(double %1)
+  ret double %2
+}
+
+define double @exp_strict_log_fast(double %a) {
+; CHECK-LABEL: @exp_strict_log_fast(
+; CHECK-NEXT:    [[TMP1:%.*]] = call fast double @llvm.log.f64(double [[A:%.*]])
+; CHECK-NEXT:    [[TMP2:%.*]] = call double @llvm.exp.f64(double [[TMP1]])
+; CHECK-NEXT:    ret double [[TMP2]]
+;
+  %1 = call fast double @llvm.log.f64(double %a)
+  %2 = call double @llvm.exp.f64(double %1)
+  ret double %2
+}
+
+define double @exp_log_exp_log(double %a) {
+; CHECK-LABEL: @exp_log_exp_log(
+; CHECK-NEXT:    [[TMP1:%.*]] = call double @llvm.log.f64(double [[A:%.*]])
+; CHECK-NEXT:    [[TMP2:%.*]] = call double @llvm.exp.f64(double [[TMP1]])
+; CHECK-NEXT:    [[TMP3:%.*]] = call double @llvm.log.f64(double [[TMP2]])
+; CHECK-NEXT:    [[TMP4:%.*]] = call double @llvm.exp.f64(double [[TMP3]])
+; CHECK-NEXT:    ret double [[TMP4]]
+;
+  %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
+}
+
+define double @exp_log_exp_log_fast(double %a) {
+; CHECK-LABEL: @exp_log_exp_log_fast(
+; CHECK-NEXT:    ret 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
+}

Added: llvm/trunk/test/Transforms/InstSimplify/exp2-intrinsic.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/exp2-intrinsic.ll?rev=321710&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/exp2-intrinsic.ll (added)
+++ llvm/trunk/test/Transforms/InstSimplify/exp2-intrinsic.ll Wed Jan  3 06:37:42 2018
@@ -0,0 +1,71 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -instsimplify -S | FileCheck %s
+
+declare double @llvm.exp2.f64(double)
+declare double @llvm.log2.f64(double)
+
+define double @exp2_log2(double %a) {
+; CHECK-LABEL: @exp2_log2(
+; CHECK-NEXT:    [[TMP1:%.*]] = call double @llvm.log2.f64(double [[A:%.*]])
+; CHECK-NEXT:    [[TMP2:%.*]] = call double @llvm.exp2.f64(double [[TMP1]])
+; CHECK-NEXT:    ret double [[TMP2]]
+;
+  %1 = call double @llvm.log2.f64(double %a)
+  %2 = call double @llvm.exp2.f64(double %1)
+  ret double %2
+}
+
+define double @exp2_log2_fast(double %a) {
+; CHECK-LABEL: @exp2_log2_fast(
+; CHECK-NEXT:    ret double [[A:%.*]]
+;
+  %1 = call fast double @llvm.log2.f64(double %a)
+  %2 = call fast double @llvm.exp2.f64(double %1)
+  ret double %2
+}
+
+define double @exp2_fast_log2_strict(double %a) {
+; CHECK-LABEL: @exp2_fast_log2_strict(
+; CHECK-NEXT:    ret double [[A:%.*]]
+;
+  %1 = call double @llvm.log2.f64(double %a)
+  %2 = call fast double @llvm.exp2.f64(double %1)
+  ret double %2
+}
+
+define double @exp2_strict_log2_fast(double %a) {
+; CHECK-LABEL: @exp2_strict_log2_fast(
+; CHECK-NEXT:    [[TMP1:%.*]] = call fast double @llvm.log2.f64(double [[A:%.*]])
+; CHECK-NEXT:    [[TMP2:%.*]] = call double @llvm.exp2.f64(double [[TMP1]])
+; CHECK-NEXT:    ret double [[TMP2]]
+;
+  %1 = call fast double @llvm.log2.f64(double %a)
+  %2 = call double @llvm.exp2.f64(double %1)
+  ret double %2
+}
+
+define double @exp2_log2_exp2_log2(double %a) {
+; CHECK-LABEL: @exp2_log2_exp2_log2(
+; CHECK-NEXT:    [[TMP1:%.*]] = call double @llvm.log2.f64(double [[A:%.*]])
+; CHECK-NEXT:    [[TMP2:%.*]] = call double @llvm.exp2.f64(double [[TMP1]])
+; CHECK-NEXT:    [[TMP3:%.*]] = call double @llvm.log2.f64(double [[TMP2]])
+; CHECK-NEXT:    [[TMP4:%.*]] = call double @llvm.exp2.f64(double [[TMP3]])
+; CHECK-NEXT:    ret double [[TMP4]]
+;
+  %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
+}
+
+define double @exp2_log2_exp2_log2_fast(double %a) {
+; CHECK-LABEL: @exp2_log2_exp2_log2_fast(
+; CHECK-NEXT:    ret 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
+}

Added: llvm/trunk/test/Transforms/InstSimplify/log-intrinsic.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/log-intrinsic.ll?rev=321710&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/log-intrinsic.ll (added)
+++ llvm/trunk/test/Transforms/InstSimplify/log-intrinsic.ll Wed Jan  3 06:37:42 2018
@@ -0,0 +1,71 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -instsimplify -S | FileCheck %s
+
+declare double @llvm.log.f64(double)
+declare double @llvm.exp.f64(double)
+
+define double @log_exp(double %a) {
+; CHECK-LABEL: @log_exp(
+; CHECK-NEXT:    [[TMP1:%.*]] = call double @llvm.exp.f64(double [[A:%.*]])
+; CHECK-NEXT:    [[TMP2:%.*]] = call double @llvm.log.f64(double [[TMP1]])
+; CHECK-NEXT:    ret double [[TMP2]]
+;
+  %1 = call double @llvm.exp.f64(double %a)
+  %2 = call double @llvm.log.f64(double %1)
+  ret double %2
+}
+
+define double @log_exp_fast(double %a) {
+; CHECK-LABEL: @log_exp_fast(
+; CHECK-NEXT:    ret double [[A:%.*]]
+;
+  %1 = call fast double @llvm.exp.f64(double %a)
+  %2 = call fast double @llvm.log.f64(double %1)
+  ret double %2
+}
+
+define double @log_fast_exp_strict(double %a) {
+; CHECK-LABEL: @log_fast_exp_strict(
+; CHECK-NEXT:    ret double [[A:%.*]]
+;
+  %1 = call double @llvm.exp.f64(double %a)
+  %2 = call fast double @llvm.log.f64(double %1)
+  ret double %2
+}
+
+define double @log_strict_exp_fast(double %a) {
+; CHECK-LABEL: @log_strict_exp_fast(
+; CHECK-NEXT:    [[TMP1:%.*]] = call fast double @llvm.exp.f64(double [[A:%.*]])
+; CHECK-NEXT:    [[TMP2:%.*]] = call double @llvm.log.f64(double [[TMP1]])
+; CHECK-NEXT:    ret double [[TMP2]]
+;
+  %1 = call fast double @llvm.exp.f64(double %a)
+  %2 = call double @llvm.log.f64(double %1)
+  ret double %2
+}
+
+define double @log_exp_log_exp(double %a) {
+; CHECK-LABEL: @log_exp_log_exp(
+; CHECK-NEXT:    [[TMP1:%.*]] = call double @llvm.exp.f64(double [[A:%.*]])
+; CHECK-NEXT:    [[TMP2:%.*]] = call double @llvm.log.f64(double [[TMP1]])
+; CHECK-NEXT:    [[TMP3:%.*]] = call double @llvm.exp.f64(double [[TMP2]])
+; CHECK-NEXT:    [[TMP4:%.*]] = call double @llvm.log.f64(double [[TMP3]])
+; CHECK-NEXT:    ret double [[TMP4]]
+;
+  %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
+}
+
+define double @log_exp_log_exp_fast(double %a) {
+; CHECK-LABEL: @log_exp_log_exp_fast(
+; CHECK-NEXT:    ret 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
+}

Added: llvm/trunk/test/Transforms/InstSimplify/log2-intrinsic.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/log2-intrinsic.ll?rev=321710&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/log2-intrinsic.ll (added)
+++ llvm/trunk/test/Transforms/InstSimplify/log2-intrinsic.ll Wed Jan  3 06:37:42 2018
@@ -0,0 +1,71 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -instsimplify -S | FileCheck %s
+
+declare double @llvm.log2.f64(double)
+declare double @llvm.exp2.f64(double)
+
+define double @log2_exp2(double %a) {
+; CHECK-LABEL: @log2_exp2(
+; CHECK-NEXT:    [[TMP1:%.*]] = call double @llvm.exp2.f64(double [[A:%.*]])
+; CHECK-NEXT:    [[TMP2:%.*]] = call double @llvm.log2.f64(double [[TMP1]])
+; CHECK-NEXT:    ret double [[TMP2]]
+;
+  %1 = call double @llvm.exp2.f64(double %a)
+  %2 = call double @llvm.log2.f64(double %1)
+  ret double %2
+}
+
+define double @log2_exp2_fast(double %a) {
+; CHECK-LABEL: @log2_exp2_fast(
+; CHECK-NEXT:    ret double [[A:%.*]]
+;
+  %1 = call fast double @llvm.exp2.f64(double %a)
+  %2 = call fast double @llvm.log2.f64(double %1)
+  ret double %2
+}
+
+define double @log2_fast_exp2_strict(double %a) {
+; CHECK-LABEL: @log2_fast_exp2_strict(
+; CHECK-NEXT:    ret double [[A:%.*]]
+;
+  %1 = call double @llvm.exp2.f64(double %a)
+  %2 = call fast double @llvm.log2.f64(double %1)
+  ret double %2
+}
+
+define double @log2_strict_exp2_fast(double %a) {
+; CHECK-LABEL: @log2_strict_exp2_fast(
+; CHECK-NEXT:    [[TMP1:%.*]] = call fast double @llvm.exp2.f64(double [[A:%.*]])
+; CHECK-NEXT:    [[TMP2:%.*]] = call double @llvm.log2.f64(double [[TMP1]])
+; CHECK-NEXT:    ret double [[TMP2]]
+;
+  %1 = call fast double @llvm.exp2.f64(double %a)
+  %2 = call double @llvm.log2.f64(double %1)
+  ret double %2
+}
+
+define double @log2_exp2_log2_exp2(double %a) {
+; CHECK-LABEL: @log2_exp2_log2_exp2(
+; CHECK-NEXT:    [[TMP1:%.*]] = call double @llvm.exp2.f64(double [[A:%.*]])
+; CHECK-NEXT:    [[TMP2:%.*]] = call double @llvm.log2.f64(double [[TMP1]])
+; CHECK-NEXT:    [[TMP3:%.*]] = call double @llvm.exp2.f64(double [[TMP2]])
+; CHECK-NEXT:    [[TMP4:%.*]] = call double @llvm.log2.f64(double [[TMP3]])
+; CHECK-NEXT:    ret double [[TMP4]]
+;
+  %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
+}
+
+define double @log2_exp2_log2_exp2_fast(double %a) {
+; CHECK-LABEL: @log2_exp2_log2_exp2_fast(
+; CHECK-NEXT:    ret 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
+}




More information about the llvm-commits mailing list