This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH, Fix PR 31098] a*C == b*C is not foldded to a == b with --fast-math/floating point
Hi,
The attached patch addresses the points raised by Paolo, Kaveh and Tom.
Hit "send" to early. Missed the ChangeLog. Here it is.
2007-10-13 Pranav Bhandarkar <pranav.bhandarkar@celunite.com>
* fold-const.c (fold_comparison):fold (a OP c) CMP (b OP c) -> a
CMP b, iff c is not zero. Check the appropriate flags before doing
this.
* testsuite/gcc.dg/pr31098.c: New
Thanks,
Pranav
Pranav Bhandarkar
GNU Tools
Celunite, Inc.
(www.celunite.com)
+91-98220 82881
Pranav Bhandarkar wrote:
Hi,
The attached patch addresses the points raised by Paolo, Kaveh and Tom.
Bootstrapped and regtested on i686-pc-linux-gnu.
Is this ok ?
cheers!
Pranav
Pranav Bhandarkar
GNU Tools
Celunite, Inc.
(www.celunite.com)
+91-98220 82881
------------------------------------------------------------------------
Index: fold-const.c
===================================================================
--- fold-const.c (revision 128907)
+++ fold-const.c (working copy)
@@ -9048,6 +9048,31 @@
return fold_build2 (code, type, TREE_OPERAND (arg1, 0),
TREE_OPERAND (arg0, 0));
+ /* (a OP c) CMP (b OP c) -> a CMP b, iff c is not zero
+ if c is negative then convert into (a swap(CMP) b) */
+ if (flag_unsafe_math_optimizations
+ && !HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
+ && !flag_trapping_math && (TREE_CODE (arg0) == TREE_CODE (arg1))
+ && BINARY_CLASS_P (arg0) && !real_zerop (TREE_OPERAND (arg0,1)))
+ {
+ if (TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
+ && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST )
+ {
+ REAL_VALUE_TYPE *cst0, *cst1;
+ cst0 = &TREE_REAL_CST (TREE_OPERAND (arg0, 1));
+ cst1 = &TREE_REAL_CST (TREE_OPERAND (arg1, 1));
+
+ if (real_compare (EQ_EXPR, cst0, cst1))
+ {
+ if (real_isneg (cst0))
+ return fold_build2 (swap_tree_comparison (code), type, TREE_OPERAND (arg0,0), TREE_OPERAND (arg1,0));
+ else
+ return fold_build2 (code, type, TREE_OPERAND (arg0,0), TREE_OPERAND (arg1,0));
+
+ }
+ }
+ }
+
if (TREE_CODE (arg1) == REAL_CST)
{
REAL_VALUE_TYPE cst;
------------------------------------------------------------------------
--- /dev/null 2006-05-22 19:55:23.000000000 +0530
+++ pr31098.c 2007-10-11 02:02:14.000000000 +0530
@@ -0,0 +1,23 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-original -funsafe-math-optimizations -ffinite-math-only -fno-trapping-math" } */
+
+int f1(float a, float b)
+{
+ return a*10 == b*10;
+}
+
+int f2(float a, float b)
+{
+ return a*10 == 10*b;
+}
+int f3(float a, float b)
+{
+ return a*-10 >= b*-10;
+}
+int f4(float a, float b)
+{
+ return a*-10 >= -10*b;
+}
+/* { dg-final { scan-tree-dump-times "return a == b" 2 "original" } } */
+/* { dg-final { scan-tree-dump-times "return a <= b" 2 "original" } } */
+/* { dg-final { cleanup-tree-dump "original" } } */