aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
Diffstat (limited to 'gcc')
-rw-r--r--gcc/ChangeLog4
-rw-r--r--gcc/match.pd26
-rw-r--r--gcc/testsuite/ChangeLog4
-rw-r--r--gcc/testsuite/gcc.dg/tree-ssa/divide-5.c10
4 files changed, 40 insertions, 4 deletions
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 <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 &
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 <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
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"} } */