From: Marc Glisse Date: Sun, 20 Nov 2016 13:42:24 +0000 (+0100) Subject: Simplify X / X, 0 / X and X % X X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=5b7f6ed0b39936a0e29e76a54d94a85bc0787f18;p=gcc.git Simplify X / X, 0 / X and X % X 2016-11-20 Marc Glisse gcc/ * match.pd (0 / X, X / X, X % X): New simplifications. gcc/testsuite/ * gcc.dg/tree-ssa/divide-5.c: New file. From-SVN: r242636 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 23de483cc35..c64b3492b75 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,7 @@ +2016-11-20 Marc Glisse + + * match.pd (0 / X, X / X, X % X): New simplifications. + 2016-11-19 Jakub Jelinek * config/i386/i386.c (ix86_can_inline_p): Use || instead of & diff --git a/gcc/match.pd b/gcc/match.pd index 79a418fa31e..f97a99888ca 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -140,19 +140,33 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) || !COMPLEX_FLOAT_TYPE_P (type))) (negate @0))) -/* Make sure to preserve divisions by zero. This is the reason why - we don't simplify x / x to 1 or 0 / x to 0. */ +/* X * 1, X / 1 -> X. */ (for op (mult trunc_div ceil_div floor_div round_div exact_div) (simplify (op @0 integer_onep) (non_lvalue @0))) +/* Preserve explicit divisions by 0: the C++ front-end wants to detect + undefined behavior in constexpr evaluation, and assuming that the division + traps enables better optimizations than these anyway. */ (for div (trunc_div ceil_div floor_div round_div exact_div) + /* 0 / X is always zero. */ + (simplify + (div integer_zerop@0 @1) + /* But not for 0 / 0 so that we can get the proper warnings and errors. */ + (if (!integer_zerop (@1)) + @0)) /* X / -1 is -X. */ (simplify (div @0 integer_minus_onep@1) (if (!TYPE_UNSIGNED (type)) (negate @0))) + /* X / X is one. */ + (simplify + (div @0 @0) + /* But not for 0 / 0 so that we can get the proper warnings and errors. */ + (if (!integer_zerop (@0)) + { build_one_cst (type); })) /* X / abs (X) is X < 0 ? -1 : 1. */ (simplify (div:C @0 (abs @0)) @@ -276,8 +290,6 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (inverse) (mult @0 { inverse; } )))))))) -/* Same applies to modulo operations, but fold is inconsistent here - and simplifies 0 % x to 0, only preserving literal 0 % 0. */ (for mod (ceil_mod floor_mod round_mod trunc_mod) /* 0 % X is always zero. */ (simplify @@ -294,6 +306,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (mod @0 integer_minus_onep@1) (if (!TYPE_UNSIGNED (type)) { build_zero_cst (type); })) + /* X % X is zero. */ + (simplify + (mod @0 @0) + /* But not for 0 % 0 so that we can get the proper warnings and errors. */ + (if (!integer_zerop (@0)) + { build_zero_cst (type); })) /* (X % Y) % Y is just X % Y. */ (simplify (mod (mod@2 @0 @1) @1) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index e83e9ea7349..e64536642dc 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2016-11-20 Marc Glisse + + * gcc.dg/tree-ssa/divide-5.c: New file. + 2016-11-19 Andreas Schwab * gcc.c-torture/execute/comp-goto-1.c (insn_t): Change offset to diff --git a/gcc/testsuite/gcc.dg/tree-ssa/divide-5.c b/gcc/testsuite/gcc.dg/tree-ssa/divide-5.c new file mode 100644 index 00000000000..48cd638e7db --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/divide-5.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-optimized" } */ + +int f(int x){ + int y = x; + int z = 0; + return x / y - x % y + z / y; +} + +/* { dg-final { scan-tree-dump "return 1;" "optimized"} } */