On Fri, 12 Jan 2018, Wilco Dijkstra wrote:
Hi,
Here is the updated version:
This patch implements some of the optimizations discussed in
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71026.
Simplify (C / x >= 0.0) into x >= 0.0 with -fno-signed-zeros
and -ffinite-math-only. If C is negative the comparison is reversed.
Only handle >= and <= for now since C / x can underflow if C is small.
Simplify (x * C1) > C2 into x > (C2 / C1) with -funsafe-math-optimizations.
If C1 is negative the comparison is reversed.
OK for commit?
ChangeLog
2018-01-10 Wilco Dijkstra <wdijk...@arm.com>
Jackson Woodruff <jackson.woodr...@arm.com>
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.
--
diff --git a/gcc/match.pd b/gcc/match.pd
index
435125a317275527661fba011a9d26e507d293a6..8a6fee906de6a750201362119862f8326868f26b
100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -376,6 +376,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(rdiv @0 (negate @1))
(rdiv (negate @0) @1))
+/* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
+ Only handle >= and <= since C / x may underflow to zero. */
+(for op (le ge)
+ res_op (lt ge)
+ neg_op (ge lt)
+ (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)))
+ (res_op @1 @2))
+ /* For C < 0, use the inverted operator. */
+ (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
+ (neg_op @1 @2))))))
Let's try with C = DBL_MIN and x = ±DBL_MAX. I don't believe it involves
signed zeros or infinities, just an underflow. First, the result depends
on the rounding mode. And in the default round-to-nearest, both divisions
give 0, and thus compare the same with 0, but we replace that with a sign
test on x, where they clearly give opposite answers.
What would be the proper flag to test to check if we care about underflow?
--
Marc Glisse