[PATCH] Fix fold_cond_expr_with_comparison (PR middle-end/40747)
Jakub Jelinek
jakub@redhat.com
Wed Jul 15 09:55:00 GMT 2009
Hi!
This is something Paolo Bonzini fixed already for > and >= to MAX
optimization, but is needed for MIN as well. Since Richard's
http://gcc.gnu.org/ml/gcc-patches/2008-04/msg01303.html
change operand_equal_p ignores signedness and precision for INTEGER_CSTs,
so we need to make sure MIN_EXPR is done in the right type.
For MAX_EXPR the patch just cleans up formatting.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk/4.4?
2009-07-14 Jakub Jelinek <jakub@redhat.com>
PR middle-end/40747
* fold-const.c (fold_cond_expr_with_comparison): When folding
< and <= to MIN, make sure the MIN uses the same type as the
comparison's operands.
* gcc.c-torture/execute/pr40747.c: New test.
--- gcc/fold-const.c.jj 2009-07-09 08:10:38.000000000 +0200
+++ gcc/fold-const.c 2009-07-14 20:39:46.000000000 +0200
@@ -5310,31 +5310,35 @@ fold_cond_expr_with_comparison (tree typ
return fold_build3 (COND_EXPR, type, arg0, arg1, arg2);
case LT_EXPR:
- /* If C1 is C2 + 1, this is min(A, C2). */
+ /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
+ MIN_EXPR, to preserve the signedness of the comparison. */
if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
OEP_ONLY_CONST)
&& operand_equal_p (arg01,
const_binop (PLUS_EXPR, arg2,
build_int_cst (type, 1), 0),
OEP_ONLY_CONST))
- return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
- type,
- fold_convert (type, arg1),
- arg2));
+ {
+ tem = fold_build2 (MIN_EXPR, TREE_TYPE (arg00), arg00,
+ fold_convert (TREE_TYPE (arg00), arg2));
+ return pedantic_non_lvalue (fold_convert (type, tem));
+ }
break;
case LE_EXPR:
- /* If C1 is C2 - 1, this is min(A, C2). */
+ /* If C1 is C2 - 1, this is min(A, C2), with the same care
+ as above. */
if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
OEP_ONLY_CONST)
&& operand_equal_p (arg01,
const_binop (MINUS_EXPR, arg2,
build_int_cst (type, 1), 0),
OEP_ONLY_CONST))
- return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
- type,
- fold_convert (type, arg1),
- arg2));
+ {
+ tem = fold_build2 (MIN_EXPR, TREE_TYPE (arg00), arg00,
+ fold_convert (TREE_TYPE (arg00), arg2));
+ return pedantic_non_lvalue (fold_convert (type, tem));
+ }
break;
case GT_EXPR:
@@ -5346,11 +5350,11 @@ fold_cond_expr_with_comparison (tree typ
const_binop (MINUS_EXPR, arg2,
build_int_cst (type, 1), 0),
OEP_ONLY_CONST))
- return pedantic_non_lvalue (fold_convert (type,
- fold_build2 (MAX_EXPR, TREE_TYPE (arg00),
- arg00,
- fold_convert (TREE_TYPE (arg00),
- arg2))));
+ {
+ tem = fold_build2 (MAX_EXPR, TREE_TYPE (arg00), arg00,
+ fold_convert (TREE_TYPE (arg00), arg2));
+ return pedantic_non_lvalue (fold_convert (type, tem));
+ }
break;
case GE_EXPR:
@@ -5361,11 +5365,11 @@ fold_cond_expr_with_comparison (tree typ
const_binop (PLUS_EXPR, arg2,
build_int_cst (type, 1), 0),
OEP_ONLY_CONST))
- return pedantic_non_lvalue (fold_convert (type,
- fold_build2 (MAX_EXPR, TREE_TYPE (arg00),
- arg00,
- fold_convert (TREE_TYPE (arg00),
- arg2))));
+ {
+ tem = fold_build2 (MAX_EXPR, TREE_TYPE (arg00), arg00,
+ fold_convert (TREE_TYPE (arg00), arg2));
+ return pedantic_non_lvalue (fold_convert (type, tem));
+ }
break;
case NE_EXPR:
break;
--- gcc/testsuite/gcc.c-torture/execute/pr40747.c.jj 2009-07-14 20:49:14.000000000 +0200
+++ gcc/testsuite/gcc.c-torture/execute/pr40747.c 2009-07-14 20:48:55.000000000 +0200
@@ -0,0 +1,22 @@
+/* PR middle-end/40747 */
+
+extern void abort (void);
+
+int
+foo (int i)
+{
+ return (i < 4 && i >= 0) ? i : 4;
+}
+
+int
+main ()
+{
+ if (foo (-1) != 4) abort ();
+ if (foo (0) != 0) abort ();
+ if (foo (1) != 1) abort ();
+ if (foo (2) != 2) abort ();
+ if (foo (3) != 3) abort ();
+ if (foo (4) != 4) abort ();
+ if (foo (5) != 4) abort ();
+ return 0;
+}
Jakub
More information about the Gcc-patches
mailing list