2015-06-09 Richard Biener <rguenther@suse.de>
authorrguenth <rguenth@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 9 Jun 2015 12:31:43 +0000 (12:31 +0000)
committerrguenth <rguenth@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 9 Jun 2015 12:31:43 +0000 (12:31 +0000)
PR middle-end/66423
* match.pd: Handle A % (unsigned)(1 << B).

* gcc.dg/fold-modpow2.c: New testcase.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@224279 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/ChangeLog
gcc/match.pd
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/fold-modpow2.c [new file with mode: 0644]

index 7adf74a..4ad4d66 100644 (file)
@@ -1,3 +1,8 @@
+2015-06-09  Richard Biener  <rguenther@suse.de>
+
+       PR middle-end/66423
+       * match.pd: Handle A % (unsigned)(1 << B).
+
 2015-06-09  Aldy Hernandez  <aldyh@redhat.com>
 
        * varasm.c (output_object_block_htab): Remove.
index abd7851..48a3047 100644 (file)
@@ -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)
index 80fba49..5b5c718 100644 (file)
@@ -1,5 +1,10 @@
 2015-06-09  Richard Biener  <rguenther@suse.de>
 
+       PR middle-end/66423
+       * gcc.dg/fold-modpow2.c: New testcase.
+
+2015-06-09  Richard Biener  <rguenther@suse.de>
+
        PR tree-optimization/66419
        * gcc.dg/vect/bb-slp-37.c: New testcase.
 
diff --git a/gcc/testsuite/gcc.dg/fold-modpow2.c b/gcc/testsuite/gcc.dg/fold-modpow2.c
new file mode 100644 (file)
index 0000000..4541b1c
--- /dev/null
@@ -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" } } */