aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorWilco Dijkstra <wdijkstr@arm.com>2018-11-14 12:45:29 +0000
committerWilco Dijkstra <wilco@gcc.gnu.org>2018-11-14 12:45:29 +0000
commit5e21d7651ef002ba2ca0ad3038b8736c47919471 (patch)
tree5586a58c4cc31595b4cdedf33ea852fd97d90fc8 /gcc
parent8cca0163fb468842f85ac8e8638ea6d1ca2f9181 (diff)
downloadgcc-5e21d7651ef002ba2ca0ad3038b8736c47919471.zip
gcc-5e21d7651ef002ba2ca0ad3038b8736c47919471.tar.gz
gcc-5e21d7651ef002ba2ca0ad3038b8736c47919471.tar.bz2
Simplify floating point comparisons
This patch implements some of the optimizations discussed in PR71026. Simplify (C / x >= 0.0) into x >= 0.0 with -funsafe-math-optimizations (since C / x can underflow to zero if x is huge, it's not safe otherwise). If C is negative the comparison is reversed. Simplify (x * C1) > C2 into x > (C2 / C1) with -funsafe-math-optimizations. If C1 is negative the comparison is reversed. gcc/ PR 71026/tree-optimization * match.pd: Simplify floating point comparisons. gcc/testsuite/ PR 71026/tree-optimization * gcc.dg/div-cmp-1.c: New test. * gcc.dg/div-cmp-2.c: New test. Co-Authored-By: Jackson Woodruff <jackson.woodruff@arm.com> From-SVN: r266142
Diffstat (limited to 'gcc')
-rw-r--r--gcc/ChangeLog6
-rw-r--r--gcc/match.pd31
-rw-r--r--gcc/testsuite/ChangeLog7
-rw-r--r--gcc/testsuite/gcc.dg/div-cmp-1.c29
-rw-r--r--gcc/testsuite/gcc.dg/div-cmp-2.c28
5 files changed, 101 insertions, 0 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 6f9b26e..47d4930 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,9 @@
+2018-11-14 Wilco Dijkstra <wdijkstr@arm.com>
+ Jackson Woodruff <jackson.woodruff@arm.com>
+
+ PR 71026/tree-optimization
+ * match.pd: Simplify floating point comparisons.
+
2018-11-14 Jakub Jelinek <jakub@redhat.com>
PR rtl-optimization/87817
diff --git a/gcc/match.pd b/gcc/match.pd
index d47249a..e9f9e0c 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -405,6 +405,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(rdiv @0 (negate @1))
(rdiv (negate @0) @1))
+(if (flag_unsafe_math_optimizations)
+ /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
+ Since C / x may underflow to zero, do this only for unsafe math. */
+ (for op (lt le gt ge)
+ neg_op (gt ge lt le)
+ (simplify
+ (op (rdiv REAL_CST@0 @1) real_zerop@2)
+ (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
+ (switch
+ (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
+ (op @1 @2))
+ /* For C < 0, use the inverted operator. */
+ (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
+ (neg_op @1 @2)))))))
+
/* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
(for div (trunc_div ceil_div floor_div round_div exact_div)
(simplify
@@ -4098,6 +4113,22 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(rdiv @2 @1))
(rdiv (op @0 @2) @1)))
+ (for cmp (lt le gt ge)
+ neg_cmp (gt ge lt le)
+ /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
+ (simplify
+ (cmp (mult @0 REAL_CST@1) REAL_CST@2)
+ (with
+ { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
+ (if (tem
+ && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
+ || (real_zerop (tem) && !real_zerop (@1))))
+ (switch
+ (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
+ (cmp @0 { tem; }))
+ (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
+ (neg_cmp @0 { tem; })))))))
+
/* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
(for root (SQRT CBRT)
(simplify
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 5f58860..e56706c 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,10 @@
+2018-11-14 Wilco Dijkstra <wdijkstr@arm.com>
+ Jackson Woodruff <jackson.woodruff@arm.com>
+
+ PR 71026/tree-optimization
+ * gcc.dg/div-cmp-1.c: New test.
+ * gcc.dg/div-cmp-2.c: New test.
+
2018-11-14 Jakub Jelinek <jakub@redhat.com>
PR other/88007
diff --git a/gcc/testsuite/gcc.dg/div-cmp-1.c b/gcc/testsuite/gcc.dg/div-cmp-1.c
new file mode 100644
index 0000000..cd1a5cd
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/div-cmp-1.c
@@ -0,0 +1,29 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -funsafe-math-optimizations -fdump-tree-optimized-raw" } */
+
+int
+cmp_mul_1 (float x)
+{
+ return x * 3 <= 100;
+}
+
+int
+cmp_mul_2 (float x)
+{
+ return x * -5 > 100;
+}
+
+int
+div_cmp_1 (float x, float y)
+{
+ return x / 3 <= y;
+}
+
+int
+div_cmp_2 (float x, float y)
+{
+ return x / 3 <= 1;
+}
+
+/* { dg-final { scan-tree-dump-times "mult_expr" 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-not "rdiv_expr" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/div-cmp-2.c b/gcc/testsuite/gcc.dg/div-cmp-2.c
new file mode 100644
index 0000000..95e5400
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/div-cmp-2.c
@@ -0,0 +1,28 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -funsafe-math-optimizations -ffinite-math-only -fdump-tree-optimized-raw" } */
+
+int
+cmp_1 (float x)
+{
+ return 5 / x >= 0;
+}
+
+int
+cmp_2 (float x)
+{
+ return 1 / x <= 0;
+}
+
+int
+cmp_3 (float x)
+{
+ return -2 / x >= 0;
+}
+
+int
+cmp_4 (float x)
+{
+ return -5 / x <= 0;
+}
+
+/* { dg-final { scan-tree-dump-not "rdiv_expr" "optimized" } } */