From 6a435314929c9dc355c0f8d1c3299e1214ce580f Mon Sep 17 00:00:00 2001 From: Wilco Dijkstra Date: Tue, 7 Nov 2017 12:38:55 +0000 Subject: [PATCH] PR71026: Canonicalize negates in division Canonicalize x / (- y) into (-x) / y. This moves negates out of the RHS of a division in order to allow further simplifications and potentially more reciprocal CSEs. 2017-11-07 Wilco Dijkstra Jackson Woodruff gcc/ PR tree-optimization/71026 * match.pd: Canonicalize negate in division. testsuite/ PR 71026/tree-optimization/71026 * gcc.dg/div_neg: New test. From-SVN: r254497 --- gcc/ChangeLog | 6 ++++++ gcc/match.pd | 5 +++++ gcc/testsuite/ChangeLog | 8 +++++++- gcc/testsuite/gcc.dg/div_neg.c | 10 ++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/div_neg.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index ad93a20244a..2fb808b5dd0 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2017-11-07 Wilco Dijkstra + Jackson Woodruff + + PR tree-optimization/71026 + * match.pd: Canonicalize negate in division. + 2017-11-07 Sudakshina Das PR middle-end/80131 diff --git a/gcc/match.pd b/gcc/match.pd index 49134bcb04f..903c06530be 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -354,6 +354,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (rdiv @0 (rdiv:s @1 @2)) (mult (rdiv @0 @1) @2))) +/* Simplify x / (- y) to -x / y. */ +(simplify + (rdiv @0 (negate @1)) + (rdiv (negate @0) @1)) + /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */ (for div (trunc_div ceil_div floor_div round_div exact_div) (simplify diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 2032379bcd7..8d41c8425c9 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,7 +1,13 @@ +2017-10-17 Wilco Dijkstra + Jackson Woodruff + + PR tree-optimization/71026 + * gcc.dg/div_neg: New test. + 2017-11-07 Sudakshina Das PR middle-end/80131 - * testsuite/gcc.dg/pr80131-1.c: New Test. + * gcc.dg/pr80131-1.c: New Test. 2017-11-07 Marc Glisse diff --git a/gcc/testsuite/gcc.dg/div_neg.c b/gcc/testsuite/gcc.dg/div_neg.c new file mode 100644 index 00000000000..da499cda2fb --- /dev/null +++ b/gcc/testsuite/gcc.dg/div_neg.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +float +div_neg (float x, float y) +{ + return (-x / y) * (x / -y); +} + +/* { dg-final { scan-tree-dump-times " / " 1 "optimized" } } */ -- 2.30.2