diff mbox

[PR25530] Convert (unsigned t / 2) * 2 into (unsigned t & ~1)

Message ID DM2PR0701MB1018B6682277C9F319FC55208E840@DM2PR0701MB1018.namprd07.prod.outlook.com
State New
Headers show

Commit Message

Hurugalawadi, Naveen July 21, 2015, 9:16 a.m. UTC
Hi,

>> handle exact_div differently, like fold-const.c does.
>> Then expressing ~1 with the result expression is really excessive - you
>> should simply build this with @1 - 1 if @1 is a power of two.

Thanks for the review and comments.

Please find attached the modified patch as per your comments.

Please review the same and let me know if any further modifications are required.

Regression Tested on X86_64.

Thanks,
Naveen

gcc/testsuite/ChangeLog:

2015-07-21  Naveen H.S  <Naveen.Hurugalawadi@caviumnetworks.com>

	PR middle-end/25530
	* gcc.dg/pr25530.c: New test.

gcc/ChangeLog:

2015-07-21  Naveen H.S  <Naveen.Hurugalawadi@caviumnetworks.com>

	PR middle-end/25530
	* match.pd (mult (exact_div @0 INTEGET_CST@1) @1) : 	New simplifier.
	(mult (trunc_div @0 integer_pow2p@1) @1) : New simplifier.

Comments

Richard Biener July 22, 2015, 12:07 p.m. UTC | #1
On Tue, Jul 21, 2015 at 11:16 AM, Hurugalawadi, Naveen
<Naveen.Hurugalawadi@caviumnetworks.com> wrote:
> Hi,
>
>>> handle exact_div differently, like fold-const.c does.
>>> Then expressing ~1 with the result expression is really excessive - you
>>> should simply build this with @1 - 1 if @1 is a power of two.

(*)

> Thanks for the review and comments.
>
> Please find attached the modified patch as per your comments.
>
> Please review the same and let me know if any further modifications are required.
>
> Regression Tested on X86_64.

We already have

+(simplify
+ (mult (exact_div @0 INTEGET_CST@1) @1)
+  @0)

as

/* (X /[ex] A) * A -> X.  */
(simplify
  (mult (convert? (exact_div @0 @1)) @1)
  /* Look through a sign-changing conversion.  */
  (convert @0))

as before the comment applies to your second pattern.

+(simplify
+ (mult (trunc_div @0 integer_pow2p@1) @1)
+  (bit_and @0 (negate @1)))

This doesn't work for signed types at least.
-1 / 2 * 2 == 0, not -2.  Your previous patch correctly
restricted this to unsigned types.

Thanks,
Richard.

> Thanks,
> Naveen
>
> gcc/testsuite/ChangeLog:
>
> 2015-07-21  Naveen H.S  <Naveen.Hurugalawadi@caviumnetworks.com>
>
>         PR middle-end/25530
>         * gcc.dg/pr25530.c: New test.
>
> gcc/ChangeLog:
>
> 2015-07-21  Naveen H.S  <Naveen.Hurugalawadi@caviumnetworks.com>
>
>         PR middle-end/25530
>         * match.pd (mult (exact_div @0 INTEGET_CST@1) @1) :     New simplifier.
>         (mult (trunc_div @0 integer_pow2p@1) @1) : New simplifier.
diff mbox

Patch

--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -280,6 +280,15 @@  along with GCC; see the file COPYING3.  If not see
 	&& integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
    (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
 
+/* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1.  */
+(simplify
+ (mult (exact_div @0 INTEGET_CST@1) @1)
+  @0)
+
+(simplify
+ (mult (trunc_div @0 integer_pow2p@1) @1)
+  (bit_and @0 (negate @1)))
+
 /* X % Y is smaller than Y.  */
 (for cmp (lt ge)
  (simplify
new file mode 100644
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr25530.c
@@ -0,0 +1,14 @@ 
+new file mode 100644
+--- /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" } } */