* [PATCH] Move A - (A & B) -> ~B & A
@ 2015-07-17 11:05 Marek Polacek
2015-07-17 12:57 ` Richard Biener
0 siblings, 1 reply; 5+ messages in thread
From: Marek Polacek @ 2015-07-17 11:05 UTC (permalink / raw)
To: GCC Patches, Richard Biener
This moves one pattern from fold-const.c into match.pd.
Since no test was testing that pattern, I added a new test.
As a follow up I'll move the "(A & ~B) - (A & B) into (A ^ B) - B"
pattern.
Bootstrapped/regtested on x86_64-linux, ok for trunk?
2015-07-17 Marek Polacek <polacek@redhat.com>
* fold-const.c (fold_binary_loc): Move A - (A & B) into ~B & A ...
* match.pd: ... here.
* gcc.dg/fold-minus-7.c: New test.
diff --git gcc/fold-const.c gcc/fold-const.c
index 93dd29d..fa321f4 100644
--- gcc/fold-const.c
+++ gcc/fold-const.c
@@ -9777,30 +9777,6 @@ fold_binary_loc (location_t loc,
if (! FLOAT_TYPE_P (type))
{
- /* Fold A - (A & B) into ~B & A. */
- if (!TREE_SIDE_EFFECTS (arg0)
- && TREE_CODE (arg1) == BIT_AND_EXPR)
- {
- if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
- {
- tree arg10 = fold_convert_loc (loc, type,
- TREE_OPERAND (arg1, 0));
- return fold_build2_loc (loc, BIT_AND_EXPR, type,
- fold_build1_loc (loc, BIT_NOT_EXPR,
- type, arg10),
- fold_convert_loc (loc, type, arg0));
- }
- if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
- {
- tree arg11 = fold_convert_loc (loc,
- type, TREE_OPERAND (arg1, 1));
- return fold_build2_loc (loc, BIT_AND_EXPR, type,
- fold_build1_loc (loc, BIT_NOT_EXPR,
- type, arg11),
- fold_convert_loc (loc, type, arg0));
- }
- }
-
/* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
any power of 2 minus 1. */
if (TREE_CODE (arg0) == BIT_AND_EXPR
diff --git gcc/match.pd gcc/match.pd
index c335ada..066d5de 100644
--- gcc/match.pd
+++ gcc/match.pd
@@ -662,6 +662,10 @@ along with GCC; see the file COPYING3. If not see
(bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
(bit_xor (bit_and (bit_xor @0 @1) @2) @0))
+/* Fold A - (A & B) into ~B & A. */
+(simplify
+ (minus (convert? @0) (convert? (bit_and:c @0 @1)))
+ (convert (bit_and (bit_not @1) @0)))
/* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
(simplify
diff --git gcc/testsuite/gcc.dg/fold-minus-7.c gcc/testsuite/gcc.dg/fold-minus-7.c
index e69de29..7a49faa 100644
--- gcc/testsuite/gcc.dg/fold-minus-7.c
+++ gcc/testsuite/gcc.dg/fold-minus-7.c
@@ -0,0 +1,36 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-cddce1" } */
+
+int
+f1 (int a, int b)
+{
+ int tem = a & b;
+ return a - tem;
+}
+
+int
+f2 (int a, int b)
+{
+ int tem = b & a;
+ return a - tem;
+}
+
+int
+f3 (unsigned int a, int b)
+{
+ return a - (a & b);
+}
+
+int
+f4 (int a, unsigned int b)
+{
+ return a - (a & b);
+}
+
+int
+f5 (int a, int b)
+{
+ return a - (unsigned) (b & a);
+}
+
+/* { dg-final { scan-tree-dump-not " - " "cddce1" } } */
Marek
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] Move A - (A & B) -> ~B & A
2015-07-17 11:05 [PATCH] Move A - (A & B) -> ~B & A Marek Polacek
@ 2015-07-17 12:57 ` Richard Biener
2015-07-17 15:43 ` Marek Polacek
0 siblings, 1 reply; 5+ messages in thread
From: Richard Biener @ 2015-07-17 12:57 UTC (permalink / raw)
To: Marek Polacek, GCC Patches
On July 17, 2015 12:28:46 PM GMT+02:00, Marek Polacek <polacek@redhat.com> wrote:
>This moves one pattern from fold-const.c into match.pd.
>Since no test was testing that pattern, I added a new test.
>As a follow up I'll move the "(A & ~B) - (A & B) into (A ^ B) - B"
>pattern.
>
>Bootstrapped/regtested on x86_64-linux, ok for trunk?
>
>2015-07-17 Marek Polacek <polacek@redhat.com>
>
> * fold-const.c (fold_binary_loc): Move A - (A & B) into ~B & A ...
> * match.pd: ... here.
>
> * gcc.dg/fold-minus-7.c: New test.
>
>diff --git gcc/fold-const.c gcc/fold-const.c
>index 93dd29d..fa321f4 100644
>--- gcc/fold-const.c
>+++ gcc/fold-const.c
>@@ -9777,30 +9777,6 @@ fold_binary_loc (location_t loc,
>
> if (! FLOAT_TYPE_P (type))
> {
>- /* Fold A - (A & B) into ~B & A. */
>- if (!TREE_SIDE_EFFECTS (arg0)
>- && TREE_CODE (arg1) == BIT_AND_EXPR)
>- {
>- if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
>- {
>- tree arg10 = fold_convert_loc (loc, type,
>- TREE_OPERAND (arg1, 0));
>- return fold_build2_loc (loc, BIT_AND_EXPR, type,
>- fold_build1_loc (loc, BIT_NOT_EXPR,
>- type, arg10),
>- fold_convert_loc (loc, type, arg0));
>- }
>- if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
>- {
>- tree arg11 = fold_convert_loc (loc,
>- type, TREE_OPERAND (arg1, 1));
>- return fold_build2_loc (loc, BIT_AND_EXPR, type,
>- fold_build1_loc (loc, BIT_NOT_EXPR,
>- type, arg11),
>- fold_convert_loc (loc, type, arg0));
>- }
>- }
>-
> /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
> any power of 2 minus 1. */
> if (TREE_CODE (arg0) == BIT_AND_EXPR
>diff --git gcc/match.pd gcc/match.pd
>index c335ada..066d5de 100644
>--- gcc/match.pd
>+++ gcc/match.pd
>@@ -662,6 +662,10 @@ along with GCC; see the file COPYING3. If not see
> (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
> (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
>
>+/* Fold A - (A & B) into ~B & A. */
>+(simplify
>+ (minus (convert? @0) (convert? (bit_and:c @0 @1)))
I think you want :s on the bit_and and its convert?
Are you sure you don't need to check for a nop conversion here?
Richard.
>+ (convert (bit_and (bit_not @1) @0)))
>
> /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
> (simplify
>diff --git gcc/testsuite/gcc.dg/fold-minus-7.c
>gcc/testsuite/gcc.dg/fold-minus-7.c
>index e69de29..7a49faa 100644
>--- gcc/testsuite/gcc.dg/fold-minus-7.c
>+++ gcc/testsuite/gcc.dg/fold-minus-7.c
>@@ -0,0 +1,36 @@
>+/* { dg-do compile } */
>+/* { dg-options "-O -fdump-tree-cddce1" } */
>+
>+int
>+f1 (int a, int b)
>+{
>+ int tem = a & b;
>+ return a - tem;
>+}
>+
>+int
>+f2 (int a, int b)
>+{
>+ int tem = b & a;
>+ return a - tem;
>+}
>+
>+int
>+f3 (unsigned int a, int b)
>+{
>+ return a - (a & b);
>+}
>+
>+int
>+f4 (int a, unsigned int b)
>+{
>+ return a - (a & b);
>+}
>+
>+int
>+f5 (int a, int b)
>+{
>+ return a - (unsigned) (b & a);
>+}
>+
>+/* { dg-final { scan-tree-dump-not " - " "cddce1" } } */
>
> Marek
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] Move A - (A & B) -> ~B & A
2015-07-17 12:57 ` Richard Biener
@ 2015-07-17 15:43 ` Marek Polacek
2015-07-17 16:01 ` Richard Biener
2015-08-13 21:26 ` Marc Glisse
0 siblings, 2 replies; 5+ messages in thread
From: Marek Polacek @ 2015-07-17 15:43 UTC (permalink / raw)
To: Richard Biener; +Cc: GCC Patches
On Fri, Jul 17, 2015 at 02:23:50PM +0200, Richard Biener wrote:
> >+/* Fold A - (A & B) into ~B & A. */
> >+(simplify
> >+ (minus (convert? @0) (convert? (bit_and:c @0 @1)))
>
> I think you want :s on the bit_and and its convert?
Ok, I keep forgetting about this :(.
> Are you sure you don't need to check for a nop conversion here?
I thought I didn't need it here, and couldn't find a testcase which
would break without tree_nop_conversion_p checks, but that doesn't mean
much. So for extra safety I put the checks there.
Bootstrapped/regtested on x86_64-linux, ok for trunk?
2015-07-17 Marek Polacek <polacek@redhat.com>
* fold-const.c (fold_binary_loc): Move A - (A & B) into ~B & A ...
* match.pd: ... here.
* gcc.dg/fold-minus-7.c: New test.
diff --git gcc/fold-const.c gcc/fold-const.c
index 93dd29d..fa321f4 100644
--- gcc/fold-const.c
+++ gcc/fold-const.c
@@ -9777,30 +9777,6 @@ fold_binary_loc (location_t loc,
if (! FLOAT_TYPE_P (type))
{
- /* Fold A - (A & B) into ~B & A. */
- if (!TREE_SIDE_EFFECTS (arg0)
- && TREE_CODE (arg1) == BIT_AND_EXPR)
- {
- if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
- {
- tree arg10 = fold_convert_loc (loc, type,
- TREE_OPERAND (arg1, 0));
- return fold_build2_loc (loc, BIT_AND_EXPR, type,
- fold_build1_loc (loc, BIT_NOT_EXPR,
- type, arg10),
- fold_convert_loc (loc, type, arg0));
- }
- if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
- {
- tree arg11 = fold_convert_loc (loc,
- type, TREE_OPERAND (arg1, 1));
- return fold_build2_loc (loc, BIT_AND_EXPR, type,
- fold_build1_loc (loc, BIT_NOT_EXPR,
- type, arg11),
- fold_convert_loc (loc, type, arg0));
- }
- }
-
/* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
any power of 2 minus 1. */
if (TREE_CODE (arg0) == BIT_AND_EXPR
diff --git gcc/match.pd gcc/match.pd
index c335ada..700a692 100644
--- gcc/match.pd
+++ gcc/match.pd
@@ -662,6 +662,12 @@ along with GCC; see the file COPYING3. If not see
(bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
(bit_xor (bit_and (bit_xor @0 @1) @2) @0))
+/* Fold A - (A & B) into ~B & A. */
+(simplify
+ (minus (convert? @0) (convert?:s (bit_and:cs @0 @1)))
+ (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
+ && tree_nop_conversion_p (type, TREE_TYPE (@1)))
+ (convert (bit_and (bit_not @1) @0))))
/* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
(simplify
diff --git gcc/testsuite/gcc.dg/fold-minus-7.c gcc/testsuite/gcc.dg/fold-minus-7.c
index e69de29..7a49faa 100644
--- gcc/testsuite/gcc.dg/fold-minus-7.c
+++ gcc/testsuite/gcc.dg/fold-minus-7.c
@@ -0,0 +1,36 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-cddce1" } */
+
+int
+f1 (int a, int b)
+{
+ int tem = a & b;
+ return a - tem;
+}
+
+int
+f2 (int a, int b)
+{
+ int tem = b & a;
+ return a - tem;
+}
+
+int
+f3 (unsigned int a, int b)
+{
+ return a - (a & b);
+}
+
+int
+f4 (int a, unsigned int b)
+{
+ return a - (a & b);
+}
+
+int
+f5 (int a, int b)
+{
+ return a - (unsigned) (b & a);
+}
+
+/* { dg-final { scan-tree-dump-not " - " "cddce1" } } */
Marek
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] Move A - (A & B) -> ~B & A
2015-07-17 15:43 ` Marek Polacek
@ 2015-07-17 16:01 ` Richard Biener
2015-08-13 21:26 ` Marc Glisse
1 sibling, 0 replies; 5+ messages in thread
From: Richard Biener @ 2015-07-17 16:01 UTC (permalink / raw)
To: Marek Polacek; +Cc: GCC Patches
On July 17, 2015 5:23:21 PM GMT+02:00, Marek Polacek <polacek@redhat.com> wrote:
>On Fri, Jul 17, 2015 at 02:23:50PM +0200, Richard Biener wrote:
>> >+/* Fold A - (A & B) into ~B & A. */
>> >+(simplify
>> >+ (minus (convert? @0) (convert? (bit_and:c @0 @1)))
>>
>> I think you want :s on the bit_and and its convert?
>
>Ok, I keep forgetting about this :(.
>
>> Are you sure you don't need to check for a nop conversion here?
>
>I thought I didn't need it here, and couldn't find a testcase which
>would break without tree_nop_conversion_p checks, but that doesn't mean
>much. So for extra safety I put the checks there.
>
>Bootstrapped/regtested on x86_64-linux, ok for trunk?
OK
Thanks
Richard
>2015-07-17 Marek Polacek <polacek@redhat.com>
>
> * fold-const.c (fold_binary_loc): Move A - (A & B) into ~B & A ...
> * match.pd: ... here.
>
> * gcc.dg/fold-minus-7.c: New test.
>
>diff --git gcc/fold-const.c gcc/fold-const.c
>index 93dd29d..fa321f4 100644
>--- gcc/fold-const.c
>+++ gcc/fold-const.c
>@@ -9777,30 +9777,6 @@ fold_binary_loc (location_t loc,
>
> if (! FLOAT_TYPE_P (type))
> {
>- /* Fold A - (A & B) into ~B & A. */
>- if (!TREE_SIDE_EFFECTS (arg0)
>- && TREE_CODE (arg1) == BIT_AND_EXPR)
>- {
>- if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
>- {
>- tree arg10 = fold_convert_loc (loc, type,
>- TREE_OPERAND (arg1, 0));
>- return fold_build2_loc (loc, BIT_AND_EXPR, type,
>- fold_build1_loc (loc, BIT_NOT_EXPR,
>- type, arg10),
>- fold_convert_loc (loc, type, arg0));
>- }
>- if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
>- {
>- tree arg11 = fold_convert_loc (loc,
>- type, TREE_OPERAND (arg1, 1));
>- return fold_build2_loc (loc, BIT_AND_EXPR, type,
>- fold_build1_loc (loc, BIT_NOT_EXPR,
>- type, arg11),
>- fold_convert_loc (loc, type, arg0));
>- }
>- }
>-
> /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
> any power of 2 minus 1. */
> if (TREE_CODE (arg0) == BIT_AND_EXPR
>diff --git gcc/match.pd gcc/match.pd
>index c335ada..700a692 100644
>--- gcc/match.pd
>+++ gcc/match.pd
>@@ -662,6 +662,12 @@ along with GCC; see the file COPYING3. If not see
> (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
> (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
>
>+/* Fold A - (A & B) into ~B & A. */
>+(simplify
>+ (minus (convert? @0) (convert?:s (bit_and:cs @0 @1)))
>+ (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
>+ && tree_nop_conversion_p (type, TREE_TYPE (@1)))
>+ (convert (bit_and (bit_not @1) @0))))
>
> /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
> (simplify
>diff --git gcc/testsuite/gcc.dg/fold-minus-7.c
>gcc/testsuite/gcc.dg/fold-minus-7.c
>index e69de29..7a49faa 100644
>--- gcc/testsuite/gcc.dg/fold-minus-7.c
>+++ gcc/testsuite/gcc.dg/fold-minus-7.c
>@@ -0,0 +1,36 @@
>+/* { dg-do compile } */
>+/* { dg-options "-O -fdump-tree-cddce1" } */
>+
>+int
>+f1 (int a, int b)
>+{
>+ int tem = a & b;
>+ return a - tem;
>+}
>+
>+int
>+f2 (int a, int b)
>+{
>+ int tem = b & a;
>+ return a - tem;
>+}
>+
>+int
>+f3 (unsigned int a, int b)
>+{
>+ return a - (a & b);
>+}
>+
>+int
>+f4 (int a, unsigned int b)
>+{
>+ return a - (a & b);
>+}
>+
>+int
>+f5 (int a, int b)
>+{
>+ return a - (unsigned) (b & a);
>+}
>+
>+/* { dg-final { scan-tree-dump-not " - " "cddce1" } } */
>
> Marek
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] Move A - (A & B) -> ~B & A
2015-07-17 15:43 ` Marek Polacek
2015-07-17 16:01 ` Richard Biener
@ 2015-08-13 21:26 ` Marc Glisse
1 sibling, 0 replies; 5+ messages in thread
From: Marc Glisse @ 2015-08-13 21:26 UTC (permalink / raw)
To: Marek Polacek; +Cc: Richard Biener, GCC Patches
On Fri, 17 Jul 2015, Marek Polacek wrote:
> On Fri, Jul 17, 2015 at 02:23:50PM +0200, Richard Biener wrote:
>>> +/* Fold A - (A & B) into ~B & A. */
>>> +(simplify
>>> + (minus (convert? @0) (convert? (bit_and:c @0 @1)))
>>
>> I think you want :s on the bit_and and its convert?
>
> Ok, I keep forgetting about this :(.
>
>> Are you sure you don't need to check for a nop conversion here?
>
> I thought I didn't need it here, and couldn't find a testcase which
> would break without tree_nop_conversion_p checks, but that doesn't mean
> much. So for extra safety I put the checks there.
I believe you were right that we don't need the checks, it should also be
valid for narrowing / extending conversions.
Note that the equivalent "(X & Y) ^ Y as ~X & Y" does not have the
optional converts.
> Bootstrapped/regtested on x86_64-linux, ok for trunk?
>
> 2015-07-17 Marek Polacek <polacek@redhat.com>
>
> * fold-const.c (fold_binary_loc): Move A - (A & B) into ~B & A ...
> * match.pd: ... here.
>
> * gcc.dg/fold-minus-7.c: New test.
>
> diff --git gcc/fold-const.c gcc/fold-const.c
> index 93dd29d..fa321f4 100644
> --- gcc/fold-const.c
> +++ gcc/fold-const.c
> @@ -9777,30 +9777,6 @@ fold_binary_loc (location_t loc,
>
> if (! FLOAT_TYPE_P (type))
> {
> - /* Fold A - (A & B) into ~B & A. */
> - if (!TREE_SIDE_EFFECTS (arg0)
> - && TREE_CODE (arg1) == BIT_AND_EXPR)
> - {
> - if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
> - {
> - tree arg10 = fold_convert_loc (loc, type,
> - TREE_OPERAND (arg1, 0));
> - return fold_build2_loc (loc, BIT_AND_EXPR, type,
> - fold_build1_loc (loc, BIT_NOT_EXPR,
> - type, arg10),
> - fold_convert_loc (loc, type, arg0));
> - }
> - if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
> - {
> - tree arg11 = fold_convert_loc (loc,
> - type, TREE_OPERAND (arg1, 1));
> - return fold_build2_loc (loc, BIT_AND_EXPR, type,
> - fold_build1_loc (loc, BIT_NOT_EXPR,
> - type, arg11),
> - fold_convert_loc (loc, type, arg0));
> - }
> - }
> -
> /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
> any power of 2 minus 1. */
> if (TREE_CODE (arg0) == BIT_AND_EXPR
> diff --git gcc/match.pd gcc/match.pd
> index c335ada..700a692 100644
> --- gcc/match.pd
> +++ gcc/match.pd
> @@ -662,6 +662,12 @@ along with GCC; see the file COPYING3. If not see
> (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
> (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
>
> +/* Fold A - (A & B) into ~B & A. */
> +(simplify
> + (minus (convert? @0) (convert?:s (bit_and:cs @0 @1)))
> + (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
> + && tree_nop_conversion_p (type, TREE_TYPE (@1)))
> + (convert (bit_and (bit_not @1) @0))))
>
> /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
> (simplify
> diff --git gcc/testsuite/gcc.dg/fold-minus-7.c gcc/testsuite/gcc.dg/fold-minus-7.c
> index e69de29..7a49faa 100644
> --- gcc/testsuite/gcc.dg/fold-minus-7.c
> +++ gcc/testsuite/gcc.dg/fold-minus-7.c
> @@ -0,0 +1,36 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O -fdump-tree-cddce1" } */
> +
> +int
> +f1 (int a, int b)
> +{
> + int tem = a & b;
> + return a - tem;
> +}
> +
> +int
> +f2 (int a, int b)
> +{
> + int tem = b & a;
> + return a - tem;
> +}
> +
> +int
> +f3 (unsigned int a, int b)
> +{
> + return a - (a & b);
> +}
> +
> +int
> +f4 (int a, unsigned int b)
> +{
> + return a - (a & b);
> +}
> +
> +int
> +f5 (int a, int b)
> +{
> + return a - (unsigned) (b & a);
> +}
> +
> +/* { dg-final { scan-tree-dump-not " - " "cddce1" } } */
>
> Marek
>
--
Marc Glisse
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2015-08-13 21:08 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-17 11:05 [PATCH] Move A - (A & B) -> ~B & A Marek Polacek
2015-07-17 12:57 ` Richard Biener
2015-07-17 15:43 ` Marek Polacek
2015-07-17 16:01 ` Richard Biener
2015-08-13 21:26 ` Marc Glisse
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).