From 4ab1e111ef0669bbcbd87a5396d2d209c7e55948 Mon Sep 17 00:00:00 2001 From: Richard Biener Date: Tue, 9 Jun 2015 12:31:43 +0000 Subject: [PATCH] re PR tree-optimization/66423 (a % (1 << b) no longer gets folded to a & (1 << b) for unsigned a) 2015-06-09 Richard Biener PR middle-end/66423 * match.pd: Handle A % (unsigned)(1 << B). * gcc.dg/fold-modpow2.c: New testcase. From-SVN: r224279 --- gcc/ChangeLog | 5 +++++ gcc/match.pd | 5 +++-- gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gcc.dg/fold-modpow2.c | 11 +++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/fold-modpow2.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 7adf74a3a1d..4ad4d66bb43 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2015-06-09 Richard Biener + + PR middle-end/66423 + * match.pd: Handle A % (unsigned)(1 << B). + 2015-06-09 Aldy Hernandez * varasm.c (output_object_block_htab): Remove. diff --git a/gcc/match.pd b/gcc/match.pd index abd785178fc..48a304703a0 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -248,11 +248,12 @@ along with GCC; see the file COPYING3. If not see (lshift INTEGER_CST@1 @2)) (for mod (trunc_mod floor_mod) (simplify - (mod @0 (power_of_two_cand@1 @2)) + (mod @0 (convert?@3 (power_of_two_cand@1 @2))) (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0)) + && tree_nop_conversion_p (type, TREE_TYPE (@3)) && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0) - (bit_and @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))) + (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); })))))) /* X % Y is smaller than Y. */ (for cmp (lt ge) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 80fba49c3de..5b5c718cd40 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2015-06-09 Richard Biener + + PR middle-end/66423 + * gcc.dg/fold-modpow2.c: New testcase. + 2015-06-09 Richard Biener PR tree-optimization/66419 diff --git a/gcc/testsuite/gcc.dg/fold-modpow2.c b/gcc/testsuite/gcc.dg/fold-modpow2.c new file mode 100644 index 00000000000..4541b1ccf02 --- /dev/null +++ b/gcc/testsuite/gcc.dg/fold-modpow2.c @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-fdump-tree-original" } */ + +unsigned int +my_mod (unsigned int a, unsigned int b) +{ + return a % (1 << b); +} + +/* The above should be simplified to (unsigned int) ((1 << b) + -1) & a */ +/* { dg-final { scan-tree-dump "& a;" "original" } } */ -- 2.30.2