fold-const.c (associate_trees): Only optimize NEGATE_EXPR in one of the operands...
authorJakub Jelinek <jakub@redhat.com>
Mon, 5 Aug 2002 19:26:27 +0000 (21:26 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Mon, 5 Aug 2002 19:26:27 +0000 (21:26 +0200)
* 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.

From-SVN: r56058

gcc/ChangeLog
gcc/fold-const.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.c-torture/execute/20020805-1.c [new file with mode: 0644]

index c59b32008b9eda867ab0d8373b430b3557c2e833..baa6118452abc81de70056f219cc5f69dcbda0d5 100644 (file)
@@ -1,3 +1,8 @@
+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.
+
 2002-08-05  Douglas B Rupp  <rupp@gnat.com>
 
        * config.gcc (i[34567]86-*-interix*): Replace interix.o with winnt.o
index 7751e6587018ff0fb25c54bcad17a41418942d2e..033dbfcfc62da4dedbab772bfd895878c9db949b 100644 (file)
@@ -1001,14 +1001,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)));
index ea7dbc63a4a0397a55c2afc414d840445b572dae..6f0baea2a7f6d4ae949101df626c0adf80b6ec8a 100644 (file)
@@ -2,6 +2,8 @@
 
        * consistency.vlad/layout/endian.c: Include string.h.
 
+       * gcc.c-torture/execute/20020805-1.c: New test.
+
 2002-08-04  Gabriel Dos Reis  <gdr@nerim.net>
 
        * g++.dg/other/conversion1.C: New test.
diff --git a/gcc/testsuite/gcc.c-torture/execute/20020805-1.c b/gcc/testsuite/gcc.c-torture/execute/20020805-1.c
new file mode 100644 (file)
index 0000000..e7d5179
--- /dev/null
@@ -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);
+}