This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix fold-const.c (operand_equal_p)


Hi!

The testcase below is miscompiled on the trunk on x86-64 and ia64 at least
and on 3.4 branch on ia64.
The problem is that fold "optimizes" the ?: into
(unsigned long) d
and d is out of range of the unsigned type (it is negative).
The following patch fixes this for me (in this case it is FIX_TRUNC_EXPR,
but I believe for the other FIX_*_EXPR trees the signedness matters as
well).
Ok to commit?
For 3.4 as well?

2004-08-31  Jakub Jelinek  <jakub@redhat.com>

	* fold-const.c (operand_equal_p): Require equal sign also for
	FIX_{CEIL,TRUNC,FLOOR,ROUND}_EXPR.

	* gcc.c-torture/execute/20040831-1.c: New test.

--- gcc/fold-const.c.jj	2004-08-31 13:55:58.000000000 +0200
+++ gcc/fold-const.c	2004-08-31 23:20:12.007971792 +0200
@@ -2391,10 +2391,21 @@ operand_equal_p (tree arg0, tree arg1, u
     {
     case '1':
       /* Two conversions are equal only if signedness and modes match.  */
-      if ((TREE_CODE (arg0) == NOP_EXPR || TREE_CODE (arg0) == CONVERT_EXPR)
-	  && (TYPE_UNSIGNED (TREE_TYPE (arg0))
-	      != TYPE_UNSIGNED (TREE_TYPE (arg1))))
-	return 0;
+      switch (TREE_CODE (arg0))
+        {
+        case NOP_EXPR:
+        case CONVERT_EXPR:
+        case FIX_CEIL_EXPR:
+        case FIX_TRUNC_EXPR:
+        case FIX_FLOOR_EXPR:
+        case FIX_ROUND_EXPR:
+	  if (TYPE_UNSIGNED (TREE_TYPE (arg0))
+	      != TYPE_UNSIGNED (TREE_TYPE (arg1)))
+	    return 0;
+	  break;
+	default:
+	  break;
+	}
 
       return operand_equal_p (TREE_OPERAND (arg0, 0),
 			      TREE_OPERAND (arg1, 0), flags);
--- gcc/testsuite/gcc.c-torture/execute/20040831-1.c.jj	2004-08-31 23:23:53.043250724 +0200
+++ gcc/testsuite/gcc.c-torture/execute/20040831-1.c	2004-08-31 23:25:42.558057188 +0200
@@ -0,0 +1,14 @@
+/* This testcase was being miscompiled, because operand_equal_p
+   returned that (unsigned long) d and (long) d are equal.  */
+extern void abort (void);
+extern void exit (int);
+
+int
+main (void)
+{
+  double d = -12.0;
+  long l = (d > 10000) ? (unsigned long) d : (long) d;
+  if (l != -12)
+    abort ();
+  exit (0);
+}

	Jakub


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]