This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PR25530] Convert (unsigned t / 2) * 2 into (unsigned t & ~1)
- From: "Hurugalawadi, Naveen" <Naveen dot Hurugalawadi at caviumnetworks dot com>
- To: "gcc-patches at gcc dot gnu dot org" <gcc-patches at gcc dot gnu dot org>
- Date: Tue, 7 Jul 2015 04:52:59 +0000
- Subject: [PR25530] Convert (unsigned t / 2) * 2 into (unsigned t & ~1)
- Authentication-results: sourceware.org; auth=none
- Authentication-results: gcc.gnu.org; dkim=none (message not signed) header.d=none;
Hi,
Please find attached the patch PR25530.patch that converts the pattern:-
(unsigned / 2) * 2 is into (unsigned & ~1).
Please review and let me know if its okay.
Regression tested on AARH64 and x86_64.
Thanks,
Naveen
gcc/testsuite/ChangeLog:
2015-07-07 Naveen H.S <Naveen.Hurugalawadi@caviumnetworks.com>
PR middle-end/25530
* gcc.dg/pr25530.c: New test.
gcc/ChangeLog:
2015-07-07 Naveen H.S <Naveen.Hurugalawadi@caviumnetworks.com>
PR middle-end/25530
* match.pd (mult (div @0 INTEGER_CST@1) INTEGER_CST@1) :
New simplifier.
diff --git a/gcc/match.pd b/gcc/match.pd
index 53e911a..2c9e408 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -529,6 +529,17 @@ along with GCC; see the file COPYING3. If not see
(bitop (bit_and:c @0 @1) (bit_and @2 @1))
(bit_and (bitop @0 @2) @1)))
+/* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
+/* PR25530. */
+(for div (trunc_div ceil_div floor_div round_div exact_div)
+ (simplify
+ (mult (div @0 INTEGER_CST@1) INTEGER_CST@1)
+ (with { tree n2 = build_int_cst (TREE_TYPE (@0),
+ wi::exact_log2 (@1)); }
+ (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
+ (bit_and @0 (lshift (rshift { build_minus_one_cst (TREE_TYPE (@0)); }
+ { n2; }) { n2; }))))))
+
/* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
(simplify
(bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
diff --git a/gcc/testsuite/gcc.dg/pr25530.c b/gcc/testsuite/gcc.dg/pr25530.c
new file mode 100644
index 0000000..61e19cc
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr25530.c
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int
+f (unsigned t)
+{
+ return (t / 2) * 2;
+}
+
+/* { dg-final { scan-tree-dump "\& -2" "optimized" } } */