This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: c/7479: wrong arithmetic output
- From: Jakub Jelinek <jakub at redhat dot com>
- To: rth at redhat dot com, Christian Ehrhardt <ehrhardt at mathematik dot uni-ulm dot de>
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Wed, 4 Dec 2002 08:59:39 -0500
- Subject: Re: c/7479: wrong arithmetic output
- References: <20021204134442.13013.qmail@theseus.mathematik.uni-ulm.de>
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
On Wed, Dec 04, 2002 at 02:44:42PM +0100, Christian Ehrhardt wrote:
>
> http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=7479
>
> Hi,
>
> confirmed on 3.2.1. I tracked this down to a bug in associate_trees that
> is fixed in 3.3. I didn't check the acutal patch but this change log
> entry is quite obviously the fix I'm looking for.
>
> | 2002-08-05 Jakub Jelinek <jakub@redhat.com>
> |
> | * fold-const.c (associate_trees): Only optimize NEGATE_EXPR in one
> | of the operands into MINUS_EXPR if code is PLUS_EXPR.
>
> Could this go into 3.2.2 and the PR closed afterwards?
Richard, ok to commit to 3.2 branch? It is on gcc-3_2-rhl8-branch for months,
so should be tested enough.
2002-08-05 Jakub Jelinek <jakub@redhat.com>
* fold-const.c (associate_trees): Only optimize NEGATE_EXPR in one
of the operands into MINUS_EXPR if code is PLUS_EXPR.
* gcc.c-torture/execute/20020805-1.c: New test.
--- gcc/testsuite/gcc.c-torture/execute/20020805-1.c.jj 2002-08-05 18:27:42.000000000 +0200
+++ gcc/testsuite/gcc.c-torture/execute/20020805-1.c 2002-08-05 18:26:42.000000000 +0200
@@ -0,0 +1,21 @@
+/* This testcase was miscompiled on IA-32, because fold-const
+ assumed associate_trees is always done on PLUS_EXPR. */
+
+extern void abort (void);
+extern void exit (int);
+
+void check (unsigned int m)
+{
+ if (m != (unsigned int) -1)
+ abort ();
+}
+
+unsigned int n = 1;
+
+int main (void)
+{
+ unsigned int m;
+ m = (1 | (2 - n)) | (-n);
+ check (m);
+ exit (0);
+}
--- gcc/fold-const.c.jj 2002-08-05 18:16:25.000000000 +0200
+++ gcc/fold-const.c 2002-08-05 18:16:25.000000000 +0200
@@ -1500,14 +1500,16 @@ associate_trees (t1, t2, code, type)
if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
|| TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
{
- if (TREE_CODE (t1) == NEGATE_EXPR)
- return build (MINUS_EXPR, type, convert (type, t2),
- convert (type, TREE_OPERAND (t1, 0)));
- else if (TREE_CODE (t2) == NEGATE_EXPR)
- return build (MINUS_EXPR, type, convert (type, t1),
- convert (type, TREE_OPERAND (t2, 0)));
- else
- return build (code, type, convert (type, t1), convert (type, t2));
+ if (code == PLUS_EXPR)
+ {
+ if (TREE_CODE (t1) == NEGATE_EXPR)
+ return build (MINUS_EXPR, type, convert (type, t2),
+ convert (type, TREE_OPERAND (t1, 0)));
+ else if (TREE_CODE (t2) == NEGATE_EXPR)
+ return build (MINUS_EXPR, type, convert (type, t1),
+ convert (type, TREE_OPERAND (t2, 0)));
+ }
+ return build (code, type, convert (type, t1), convert (type, t2));
}
return fold (build (code, type, convert (type, t1), convert (type, t2)));
Jakub