Commit 5b7f6ed0 by Marc Glisse Committed by Marc Glisse

Simplify X / X, 0 / X and X % X

2016-11-20  Marc Glisse  <marc.glisse@inria.fr>

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
parent 62a7df9f
2016-11-20 Marc Glisse <marc.glisse@inria.fr>
* match.pd (0 / X, X / X, X % X): New simplifications.
2016-11-19 Jakub Jelinek <jakub@redhat.com>
* config/i386/i386.c (ix86_can_inline_p): Use || instead of &
......@@ -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)
......
2016-11-20 Marc Glisse <marc.glisse@inria.fr>
* gcc.dg/tree-ssa/divide-5.c: New file.
2016-11-19 Andreas Schwab <schwab@linux-m68k.org>
* gcc.c-torture/execute/comp-goto-1.c (insn_t): Change offset to
......
/* { 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"} } */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment