From: Marc Glisse Date: Tue, 7 Nov 2017 11:04:14 +0000 (+0100) Subject: More fold_negate in match.pd X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=81bd903a6aa90326fb2a3cb451f86423f069793b;p=gcc.git More fold_negate in match.pd gcc/ChangeLog: 2017-11-07 Marc Glisse * fold-const.c (negate_expr_p) [PLUS_EXPR, MINUS_EXPR]: Handle non-scalar integral types. * match.pd (negate_expr_p): Handle MINUS_EXPR. (-(A-B), -(~A)): New transformations. gcc/testsuite/ChangeLog: 2017-11-07 Marc Glisse * gcc.dg/tree-ssa/negminus.c: New test. From-SVN: r254494 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 4b4706b9bab..d06fcee491e 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2017-11-07 Marc Glisse + + * fold-const.c (negate_expr_p) [PLUS_EXPR, MINUS_EXPR]: Handle + non-scalar integral types. + * match.pd (negate_expr_p): Handle MINUS_EXPR. + (-(A-B), -(~A)): New transformations. + 2017-11-07 Tom de Vries * config/powerpcspe/aix43.h (SUBTARGET_OVERRIDE_OPTIONS): Remove diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 1109f5e3bc5..e9cd968887f 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -428,7 +428,7 @@ negate_expr_p (tree t) case PLUS_EXPR: if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)) || HONOR_SIGNED_ZEROS (element_mode (type)) - || (INTEGRAL_TYPE_P (type) + || (ANY_INTEGRAL_TYPE_P (type) && ! TYPE_OVERFLOW_WRAPS (type))) return false; /* -(A + B) -> (-B) - A. */ @@ -441,7 +441,7 @@ negate_expr_p (tree t) /* We can't turn -(A-B) into B-A when we honor signed zeros. */ return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)) && !HONOR_SIGNED_ZEROS (element_mode (type)) - && (! INTEGRAL_TYPE_P (type) + && (! ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type)); case MULT_EXPR: diff --git a/gcc/match.pd b/gcc/match.pd index f22286e0c2e..9113fd183ac 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -958,6 +958,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (match negate_expr_p VECTOR_CST (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type)))) +(match negate_expr_p + (minus @0 @1) + (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)) + || (FLOAT_TYPE_P (type) + && !HONOR_SIGN_DEPENDENT_ROUNDING (type) + && !HONOR_SIGNED_ZEROS (type))))) /* (-A) * (-B) -> A * B */ (simplify @@ -973,6 +979,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && !HONOR_SIGNED_ZEROS (element_mode (type))) (minus (negate @1) @0))) +/* -(A - B) -> B - A. */ +(simplify + (negate (minus @0 @1)) + (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type)) + || (FLOAT_TYPE_P (type) + && !HONOR_SIGN_DEPENDENT_ROUNDING (type) + && !HONOR_SIGNED_ZEROS (type))) + (minus @1 @0))) + /* A - B -> A + (-B) if B is easily negatable. */ (simplify (minus @0 negate_expr_p@1) @@ -1082,6 +1097,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) || !TYPE_UNSIGNED (TREE_TYPE (@0))) (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); })))) +/* Convert - (~A) to A + 1. */ +(simplify + (negate (nop_convert (bit_not @0))) + (plus (view_convert @0) { build_each_one_cst (type); })) + /* Convert ~ (A - 1) or ~ (A + -1) to -A. */ (simplify (bit_not (convert? (minus @0 integer_each_onep))) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 6403283dafa..7f33b7c7337 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2017-11-07 Marc Glisse + + * gcc.dg/tree-ssa/negminus.c: New test. + 2017-11-06 Jeff Law * gcc.target/i386/stack-check-12.c: Revert to initial version. Then.. diff --git a/gcc/testsuite/gcc.dg/tree-ssa/negminus.c b/gcc/testsuite/gcc.dg/tree-ssa/negminus.c new file mode 100644 index 00000000000..f857a007983 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/negminus.c @@ -0,0 +1,21 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fno-rounding-math -fno-signed-zeros -fdump-tree-optimized-raw" } */ + +double f(double a, double b){ + double c = a - b; + return -c; +} + +int g(unsigned x){ + unsigned y = ~x; + int z = (int) y; + return -z; +} + +unsigned h(unsigned a, unsigned b, unsigned c){ + unsigned d = b - c; + unsigned e = a + d; + return -e; +} + +/* { dg-final { scan-tree-dump-not "negate_expr" "optimized"} } */