From da186c1f433028066a476955a36ce48647b13c6b Mon Sep 17 00:00:00 2001 From: Richard Biener Date: Thu, 29 Sep 2016 12:28:19 +0000 Subject: [PATCH] re PR middle-end/77407 (Optimize integer i / abs (i) into the sign of i) 2016-09-29 Richard Biener PR middle-end/77407 * match.pd: Add X / abs (X) -> X < 0 ? -1 : 1 and X / -X -> -1 simplifications. * gcc.dg/pr77407.c: New testcase. From-SVN: r240616 --- gcc/ChangeLog | 6 ++++++ gcc/match.pd | 17 +++++++++++++++-- gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gcc.dg/pr77407.c | 18 ++++++++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr77407.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 5ebd880f444..6b832ed26d0 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2016-09-29 Richard Biener + + PR middle-end/77407 + * match.pd: Add X / abs (X) -> X < 0 ? -1 : 1 and + X / -X -> -1 simplifications. + 2016-09-29 Richard Biener PR middle-end/55152 diff --git a/gcc/match.pd b/gcc/match.pd index 553e50d2d03..ba7e0132e31 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -147,12 +147,25 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (op @0 integer_onep) (non_lvalue @0))) -/* X / -1 is -X. */ (for div (trunc_div ceil_div floor_div round_div exact_div) + /* X / -1 is -X. */ (simplify (div @0 integer_minus_onep@1) (if (!TYPE_UNSIGNED (type)) - (negate @0)))) + (negate @0))) + /* X / abs (X) is X < 0 ? -1 : 1. */ + (simplify + (div @0 (abs @0)) + (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) + && TYPE_OVERFLOW_UNDEFINED (type)) + (cond (lt @0 { build_zero_cst (type); }) + { build_minus_one_cst (type); } { build_one_cst (type); }))) + /* X / -X is -1. */ + (simplify + (div @0 (negate @0)) + (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) + && TYPE_OVERFLOW_UNDEFINED (type)) + { build_minus_one_cst (type); }))) /* For unsigned integral types, FLOOR_DIV_EXPR is the same as TRUNC_DIV_EXPR. Rewrite into the latter in this case. */ diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index a88a975d6a0..9269d58bb18 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2016-09-28 Richard Biener + + PR middle-end/77407 + * gcc.dg/pr77407.c: New testcase. + 2016-09-29 Richard Biener PR middle-end/55152 diff --git a/gcc/testsuite/gcc.dg/pr77407.c b/gcc/testsuite/gcc.dg/pr77407.c new file mode 100644 index 00000000000..8cad857154a --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr77407.c @@ -0,0 +1,18 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fstrict-overflow -fdump-tree-gimple" } */ + +int foo (int c) +{ + if (c != 0) + c /= __builtin_abs (c); + return c; +} + +int bar (int c) +{ + if (c != 0) + c /= -c; + return c; +} + +/* { dg-final { scan-tree-dump-times "/" 0 "gimple" } } */ -- 2.30.2