diff --git a/gcc/ChangeLog b/gcc/ChangeLog index f9b467d70fab64876013419a3895ab9508bc172d..cc7b153f1de95bb20aa33ed543a1e527fc83d4e1 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2005-11-26 Andrew Pinski <pinskia@physics.uc.edu> + + PR middle-end/23669 + * fold-const.c (fold_binary): Convert -A/-B to A/B for signed types + when overflow is undefined. + 2005-11-26 Hans-Peter Nilsson <hp@axis.com> * doc/md.texi (Insn Canonicalizations): Refer to the diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 59037d5975801660eb3d8a03a8b225a2f7d68043..63f12949ab71e3ed269c9a22a4a386d759724384 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -8440,6 +8440,19 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1) && TREE_INT_CST_HIGH (arg1) == -1) return fold_convert (type, negate_expr (arg0)); + /* Convert -A / -B to A / B when the type is signed and overflow is + undefined. */ + if (!TYPE_UNSIGNED (type) && !flag_wrapv + && TREE_CODE (arg0) == NEGATE_EXPR + && negate_expr_p (arg1)) + return fold_build2 (code, type, TREE_OPERAND (arg0, 0), + negate_expr (arg1)); + if (!TYPE_UNSIGNED (type) && !flag_wrapv + && TREE_CODE (arg1) == NEGATE_EXPR + && negate_expr_p (arg0)) + return fold_build2 (code, type, negate_expr (arg0), + TREE_OPERAND (arg1, 0)); + /* If arg0 is a multiple of arg1, then rewrite to the fastest div operation, EXACT_DIV_EXPR. diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 126a70d1b07e94ab048e52aeef96e5804105763f..2cbcd6deef5bfc409ab952f45074aa4673e163b3 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2005-11-26 Andrew Pinski <pinskia@physics.uc.edu> + + PR middle-end/23669 + * gcc.dg/tree-ssa/divide-1.c: New test. + * gcc.dg/tree-ssa/divide-2.c: New test. + 2005-11-26 Janne Blomqvist <jb@gcc.gnu.org> PR libgfortran/24945 diff --git a/gcc/testsuite/gcc.dg/tree-ssa/divide-1.c b/gcc/testsuite/gcc.dg/tree-ssa/divide-1.c new file mode 100644 index 0000000000000000000000000000000000000000..90acd8b7abc37d1e975cd8318d1c6bc44cea4bde --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/divide-1.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-optimized" } */ + +int f(int a) +{ + return (-a)/10; +} + + +/* { dg-final { scan-tree-dump-times "-a / 10" 0 "optimized"} } */ +/* { dg-final { scan-tree-dump-times "a / -10" 1 "optimized"} } */ + +/* { dg-final { cleanup-tree-dump "optimized" } } */ + diff --git a/gcc/testsuite/gcc.dg/tree-ssa/divide-2.c b/gcc/testsuite/gcc.dg/tree-ssa/divide-2.c new file mode 100644 index 0000000000000000000000000000000000000000..5d719c025017bdedeb6dc8176ec4acaa0d892367 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/divide-2.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-optimized" } */ + +int f(int a) +{ + return 10/-a; +} + + +/* { dg-final { scan-tree-dump-times "10 / -a" 0 "optimized"} } */ +/* { dg-final { scan-tree-dump-times "-10 / a" 1 "optimized"} } */ + +/* { dg-final { cleanup-tree-dump "optimized" } } */ +