This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] Fix fold_binary with MIN_EXPR/MAX_EXPR (PR middle-end/36137) (take 2)
On Tue, May 06, 2008 at 03:27:05PM +0200, Richard Guenther wrote:
> > ? Sure, that works too, but IMHO MIN_EXPR/MAX_EXPR are so rare that
> > slowing down every fold_binary because of that is not desirable.
>
> I think we don't care and this solution is cleaner.
>
> Ok, I still like the simple generic fix more.
Ok, here is it. Works in quick testing, ok for trunk/4.3 if full testing
succeeds?
2008-05-06 Jakub Jelinek <jakub@redhat.com>
PR middle-end/36137
* fold-const.c (fold_binary): Use STRIP_SIGN_NOPS instead of
STRIP_NOPS on arguments even for MIN_EXPR and MAX_EXPR.
* gcc.c-torture/execute/20080506-1.c: New test.
--- gcc/fold-const.c.jj 2008-04-30 16:14:55.000000000 +0200
+++ gcc/fold-const.c 2008-05-06 15:37:24.000000000 +0200
@@ -1,6 +1,6 @@
/* Fold a constant sub-tree into a single node for C-compiler
Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
- 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
+ 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
Free Software Foundation, Inc.
This file is part of GCC.
@@ -9261,7 +9261,8 @@ fold_binary (enum tree_code code, tree t
safe for every expression, except for a comparison expression
because its signedness is derived from its operands. So, in
the latter case, only strip conversions that don't change the
- signedness.
+ signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
+ preserved.
Note that this is done as an internal manipulation within the
constant folder, in order to find the simplest representation
@@ -9269,7 +9270,7 @@ fold_binary (enum tree_code code, tree t
cases, the appropriate type conversions should be put back in
the tree that will get out of the constant folder. */
- if (kind == tcc_comparison)
+ if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
{
STRIP_SIGN_NOPS (arg0);
STRIP_SIGN_NOPS (arg1);
--- gcc/testsuite/gcc.c-torture/execute/20080506-1.c.jj 2008-05-06 13:35:12.000000000 +0200
+++ gcc/testsuite/gcc.c-torture/execute/20080506-1.c 2008-05-06 13:24:47.000000000 +0200
@@ -0,0 +1,21 @@
+/* PR middle-end/36137 */
+extern void abort (void);
+
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
+
+int
+main ()
+{
+ unsigned int u;
+ int i = -1;
+
+ u = MAX ((unsigned int) MAX (i, 0), 1);
+ if (u != 1)
+ abort ();
+
+ u = MIN ((unsigned int) MAX (i, 0), (unsigned int) i);
+ if (u != 0)
+ abort ();
+ return 0;
+}
Jakub