From 5b7f6ed0b39936a0e29e76a54d94a85bc0787f18 Mon Sep 17 00:00:00 2001 From: Marc Glisse Date: Sun, 20 Nov 2016 14:42:24 +0100 Subject: [PATCH] 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 --- gcc/ChangeLog | 4 ++++ gcc/match.pd | 26 ++++++++++++++++++++++---- gcc/testsuite/ChangeLog | 4 ++++ gcc/testsuite/gcc.dg/tree-ssa/divide-5.c | 10 ++++++++++ 4 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/divide-5.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 23de483c..c64b349 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 79a418f..f97a998 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 e83e9ea..e645366 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 0000000..48cd638 --- /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"} } */ -- 2.7.4