* [PATCH 1/2] MATCH: PR 106164 : Optimize `(X CMP1 Y) AND/IOR (X CMP2 Y)`
@ 2023-07-31 5:34 Andrew Pinski
2023-07-31 5:34 ` [PATCH 2/2] MATCH: Add `a == b | a cmp b` and `a != b & a cmp b` simplifications Andrew Pinski
2023-07-31 10:50 ` [PATCH 1/2] MATCH: PR 106164 : Optimize `(X CMP1 Y) AND/IOR (X CMP2 Y)` Richard Biener
0 siblings, 2 replies; 5+ messages in thread
From: Andrew Pinski @ 2023-07-31 5:34 UTC (permalink / raw)
To: gcc-patches; +Cc: Andrew Pinski
I noticed that there are patterns that optimize
`(X CMP1 CST1) AND/IOR (X CMP2 CST2)` and we can easily extend
them to support the `(X CMP1 Y) AND/IOR (X CMP2 Y)` by saying they
compare equal. This allows for this kind of optimization for integral
and pointer types (which have the same semantics).
OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
gcc/ChangeLog:
PR tree-optimization/106164
* match.pd: Extend the `(X CMP1 CST1) AND/IOR (X CMP2 CST2)`
patterns to support `(X CMP1 Y) AND/IOR (X CMP2 Y)`.
gcc/testsuite/ChangeLog:
PR tree-optimization/106164
* gcc.dg/tree-ssa/cmpbit-1.c: New test.
---
gcc/match.pd | 66 +++++++++++++++++++-----
gcc/testsuite/gcc.dg/tree-ssa/cmpbit-1.c | 38 ++++++++++++++
2 files changed, 90 insertions(+), 14 deletions(-)
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cmpbit-1.c
diff --git a/gcc/match.pd b/gcc/match.pd
index 73eb249f704..00af5d99119 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -2799,14 +2799,24 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
/* Convert (X == CST1) && (X OP2 CST2) to a known value
based on CST1 OP2 CST2. Similarly for (X != CST1). */
+/* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
+ Similarly for (X != Y). */
(for code1 (eq ne)
(for code2 (eq ne lt gt le ge)
(simplify
- (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
+ (bit_and:c (code1@3 @0 @1) (code2@4 @0 @2))
+ (if ((TREE_CODE (@1) == INTEGER_CST
+ && TREE_CODE (@2) == INTEGER_CST)
+ || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
+ || POINTER_TYPE_P (TREE_TYPE (@1)))
+ && operand_equal_p (@1, @2)))
(with
{
- int cmp = tree_int_cst_compare (@1, @2);
+ int cmp = 0;
+ if (TREE_CODE (@1) == INTEGER_CST
+ && TREE_CODE (@2) == INTEGER_CST)
+ cmp = tree_int_cst_compare (@1, @2);
bool val;
switch (code2)
{
@@ -2822,17 +2832,26 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(switch
(if (code1 == EQ_EXPR && val) @3)
(if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
- (if (code1 == NE_EXPR && !val) @4))))))
+ (if (code1 == NE_EXPR && !val) @4)))))))
-/* Convert (X OP1 CST1) && (X OP2 CST2). */
+/* Convert (X OP1 CST1) && (X OP2 CST2).
+ Convert (X OP1 Y) && (X OP2 Y). */
(for code1 (lt le gt ge)
(for code2 (lt le gt ge)
(simplify
- (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2))
+ (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2))
+ (if ((TREE_CODE (@1) == INTEGER_CST
+ && TREE_CODE (@2) == INTEGER_CST)
+ || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
+ || POINTER_TYPE_P (TREE_TYPE (@1)))
+ && operand_equal_p (@1, @2)))
(with
{
- int cmp = tree_int_cst_compare (@1, @2);
+ int cmp = 0;
+ if (TREE_CODE (@1) == INTEGER_CST
+ && TREE_CODE (@2) == INTEGER_CST)
+ cmp = tree_int_cst_compare (@1, @2);
}
(switch
/* Choose the more restrictive of two < or <= comparisons. */
@@ -2861,18 +2880,28 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
&& (code1 == GT_EXPR || code1 == GE_EXPR)
&& (code2 == LT_EXPR || code2 == LE_EXPR))
{ constant_boolean_node (false, type); })
- )))))
+ ))))))
/* Convert (X == CST1) || (X OP2 CST2) to a known value
based on CST1 OP2 CST2. Similarly for (X != CST1). */
+/* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type.
+ Similarly for (X != Y). */
(for code1 (eq ne)
(for code2 (eq ne lt gt le ge)
(simplify
- (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
+ (bit_ior:c (code1@3 @0 @1) (code2@4 @0 @2))
+ (if ((TREE_CODE (@1) == INTEGER_CST
+ && TREE_CODE (@2) == INTEGER_CST)
+ || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
+ || POINTER_TYPE_P (TREE_TYPE (@1)))
+ && operand_equal_p (@1, @2)))
(with
{
- int cmp = tree_int_cst_compare (@1, @2);
+ int cmp = 0;
+ if (TREE_CODE (@1) == INTEGER_CST
+ && TREE_CODE (@2) == INTEGER_CST)
+ cmp = tree_int_cst_compare (@1, @2);
bool val;
switch (code2)
{
@@ -2888,17 +2917,26 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(switch
(if (code1 == EQ_EXPR && val) @4)
(if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
- (if (code1 == NE_EXPR && !val) @3))))))
+ (if (code1 == NE_EXPR && !val) @3)))))))
-/* Convert (X OP1 CST1) || (X OP2 CST2). */
+/* Convert (X OP1 CST1) || (X OP2 CST2).
+ Convert (X OP1 Y) || (X OP2 Y). */
(for code1 (lt le gt ge)
(for code2 (lt le gt ge)
(simplify
- (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
+ (bit_ior (code1@3 @0 @1) (code2@4 @0 @2))
+ (if ((TREE_CODE (@1) == INTEGER_CST
+ && TREE_CODE (@2) == INTEGER_CST)
+ || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
+ || POINTER_TYPE_P (TREE_TYPE (@1)))
+ && operand_equal_p (@1, @2)))
(with
{
- int cmp = tree_int_cst_compare (@1, @2);
+ int cmp = 0;
+ if (TREE_CODE (@1) == INTEGER_CST
+ && TREE_CODE (@2) == INTEGER_CST)
+ cmp = tree_int_cst_compare (@1, @2);
}
(switch
/* Choose the more restrictive of two < or <= comparisons. */
@@ -2927,7 +2965,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
&& (code1 == GT_EXPR || code1 == GE_EXPR)
&& (code2 == LT_EXPR || code2 == LE_EXPR))
{ constant_boolean_node (true, type); })
- )))))
+ ))))))
/* We can't reassociate at all for saturating types. */
(if (!TYPE_SATURATING (type))
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cmpbit-1.c b/gcc/testsuite/gcc.dg/tree-ssa/cmpbit-1.c
new file mode 100644
index 00000000000..c219e047ba8
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/cmpbit-1.c
@@ -0,0 +1,38 @@
+/* { dg-do compile } */
+/* { dg-options "-O1 -fno-tree-reassoc -fdump-tree-optimized-raw" } */
+_Bool f(int a, int b)
+{
+ _Bool c = a > b;
+ _Bool d = a >= b;
+ return c & d;
+}
+
+_Bool f1(int a, int b)
+{
+ _Bool c = a > b;
+ _Bool d = a >= b;
+ return c | d;
+}
+
+_Bool g(int a, int b)
+{
+ _Bool c = a < b;
+ _Bool d = a <= b;
+ return c & d;
+}
+
+_Bool g1(int a, int b)
+{
+ _Bool c = a < b;
+ _Bool d = a <= b;
+ return c | d;
+}
+
+
+/* We should be able to optimize these without reassociation too. */
+/* { dg-final { scan-tree-dump-not "bit_and_expr," "optimized" } } */
+/* { dg-final { scan-tree-dump-not "bit_ior_expr," "optimized" } } */
+/* { dg-final { scan-tree-dump-times "gt_expr," 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "ge_expr," 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "lt_expr," 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "le_expr," 1 "optimized" } } */
--
2.31.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 2/2] MATCH: Add `a == b | a cmp b` and `a != b & a cmp b` simplifications
2023-07-31 5:34 [PATCH 1/2] MATCH: PR 106164 : Optimize `(X CMP1 Y) AND/IOR (X CMP2 Y)` Andrew Pinski
@ 2023-07-31 5:34 ` Andrew Pinski
2023-07-31 10:52 ` Richard Biener
2023-07-31 10:50 ` [PATCH 1/2] MATCH: PR 106164 : Optimize `(X CMP1 Y) AND/IOR (X CMP2 Y)` Richard Biener
1 sibling, 1 reply; 5+ messages in thread
From: Andrew Pinski @ 2023-07-31 5:34 UTC (permalink / raw)
To: gcc-patches; +Cc: Andrew Pinski
Even though these are done by combine_comparisons, we can add them to match
to allow simplifcations during match rather than just during reassoc/ifcombine.
OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
gcc/ChangeLog:
PR tree-optimization/106164
* match.pd (`a != b & a <= b`, `a != b & a >= b`,
`a == b | a < b`, `a == b | a > b`): Handle these cases
too.
gcc/testsuite/ChangeLog:
PR tree-optimization/106164
* gcc.dg/tree-ssa/cmpbit-2.c: New test.
---
gcc/match.pd | 32 +++++++++++++++++--
gcc/testsuite/gcc.dg/tree-ssa/cmpbit-2.c | 39 ++++++++++++++++++++++++
2 files changed, 69 insertions(+), 2 deletions(-)
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cmpbit-2.c
diff --git a/gcc/match.pd b/gcc/match.pd
index 00af5d99119..cf8057701ea 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -2832,7 +2832,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(switch
(if (code1 == EQ_EXPR && val) @3)
(if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
- (if (code1 == NE_EXPR && !val) @4)))))))
+ (if (code1 == NE_EXPR && !val) @4)
+ (if (code1 == NE_EXPR
+ && code2 == GE_EXPR
+ && cmp == 0)
+ (gt @0 @1))
+ (if (code1 == NE_EXPR
+ && code2 == LE_EXPR
+ && cmp == 0)
+ (lt @0 @1))
+ )
+ )
+ )
+ )
+ )
+)
/* Convert (X OP1 CST1) && (X OP2 CST2).
Convert (X OP1 Y) && (X OP2 Y). */
@@ -2917,7 +2931,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(switch
(if (code1 == EQ_EXPR && val) @4)
(if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
- (if (code1 == NE_EXPR && !val) @3)))))))
+ (if (code1 == NE_EXPR && !val) @3)
+ (if (code1 == EQ_EXPR
+ && code2 == GT_EXPR
+ && cmp == 0)
+ (ge @0 @1))
+ (if (code1 == EQ_EXPR
+ && code2 == LT_EXPR
+ && cmp == 0)
+ (le @0 @1))
+ )
+ )
+ )
+ )
+ )
+)
/* Convert (X OP1 CST1) || (X OP2 CST2).
Convert (X OP1 Y) || (X OP2 Y). */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cmpbit-2.c b/gcc/testsuite/gcc.dg/tree-ssa/cmpbit-2.c
new file mode 100644
index 00000000000..c4226ef01af
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/cmpbit-2.c
@@ -0,0 +1,39 @@
+/* { dg-do compile } */
+/* { dg-options "-O1 -fno-tree-reassoc -fdump-tree-optimized-raw" } */
+
+_Bool f(int a, int b)
+{
+ _Bool c = a == b;
+ _Bool d = a > b;
+ return c | d;
+}
+
+_Bool f1(int a, int b)
+{
+ _Bool c = a != b;
+ _Bool d = a >= b;
+ return c & d;
+}
+
+_Bool g(int a, int b)
+{
+ _Bool c = a == b;
+ _Bool d = a < b;
+ return c | d;
+}
+
+_Bool g1(int a, int b)
+{
+ _Bool c = a != b;
+ _Bool d = a <= b;
+ return c & d;
+}
+
+
+/* We should be able to optimize these without reassociation too. */
+/* { dg-final { scan-tree-dump-not "bit_and_expr," "optimized" } } */
+/* { dg-final { scan-tree-dump-not "bit_ior_expr," "optimized" } } */
+/* { dg-final { scan-tree-dump-times "gt_expr," 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "ge_expr," 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "lt_expr," 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "le_expr," 1 "optimized" } } */
--
2.31.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/2] MATCH: PR 106164 : Optimize `(X CMP1 Y) AND/IOR (X CMP2 Y)`
2023-07-31 5:34 [PATCH 1/2] MATCH: PR 106164 : Optimize `(X CMP1 Y) AND/IOR (X CMP2 Y)` Andrew Pinski
2023-07-31 5:34 ` [PATCH 2/2] MATCH: Add `a == b | a cmp b` and `a != b & a cmp b` simplifications Andrew Pinski
@ 2023-07-31 10:50 ` Richard Biener
1 sibling, 0 replies; 5+ messages in thread
From: Richard Biener @ 2023-07-31 10:50 UTC (permalink / raw)
To: Andrew Pinski; +Cc: gcc-patches
On Mon, Jul 31, 2023 at 7:35 AM Andrew Pinski via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> I noticed that there are patterns that optimize
> `(X CMP1 CST1) AND/IOR (X CMP2 CST2)` and we can easily extend
> them to support the `(X CMP1 Y) AND/IOR (X CMP2 Y)` by saying they
> compare equal. This allows for this kind of optimization for integral
> and pointer types (which have the same semantics).
>
> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
OK.
Thanks,
Richard.
> gcc/ChangeLog:
>
> PR tree-optimization/106164
> * match.pd: Extend the `(X CMP1 CST1) AND/IOR (X CMP2 CST2)`
> patterns to support `(X CMP1 Y) AND/IOR (X CMP2 Y)`.
>
> gcc/testsuite/ChangeLog:
>
> PR tree-optimization/106164
> * gcc.dg/tree-ssa/cmpbit-1.c: New test.
> ---
> gcc/match.pd | 66 +++++++++++++++++++-----
> gcc/testsuite/gcc.dg/tree-ssa/cmpbit-1.c | 38 ++++++++++++++
> 2 files changed, 90 insertions(+), 14 deletions(-)
> create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cmpbit-1.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 73eb249f704..00af5d99119 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -2799,14 +2799,24 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>
> /* Convert (X == CST1) && (X OP2 CST2) to a known value
> based on CST1 OP2 CST2. Similarly for (X != CST1). */
> +/* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
> + Similarly for (X != Y). */
>
> (for code1 (eq ne)
> (for code2 (eq ne lt gt le ge)
> (simplify
> - (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
> + (bit_and:c (code1@3 @0 @1) (code2@4 @0 @2))
> + (if ((TREE_CODE (@1) == INTEGER_CST
> + && TREE_CODE (@2) == INTEGER_CST)
> + || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
> + || POINTER_TYPE_P (TREE_TYPE (@1)))
> + && operand_equal_p (@1, @2)))
> (with
> {
> - int cmp = tree_int_cst_compare (@1, @2);
> + int cmp = 0;
> + if (TREE_CODE (@1) == INTEGER_CST
> + && TREE_CODE (@2) == INTEGER_CST)
> + cmp = tree_int_cst_compare (@1, @2);
> bool val;
> switch (code2)
> {
> @@ -2822,17 +2832,26 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> (switch
> (if (code1 == EQ_EXPR && val) @3)
> (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
> - (if (code1 == NE_EXPR && !val) @4))))))
> + (if (code1 == NE_EXPR && !val) @4)))))))
>
> -/* Convert (X OP1 CST1) && (X OP2 CST2). */
> +/* Convert (X OP1 CST1) && (X OP2 CST2).
> + Convert (X OP1 Y) && (X OP2 Y). */
>
> (for code1 (lt le gt ge)
> (for code2 (lt le gt ge)
> (simplify
> - (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2))
> + (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2))
> + (if ((TREE_CODE (@1) == INTEGER_CST
> + && TREE_CODE (@2) == INTEGER_CST)
> + || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
> + || POINTER_TYPE_P (TREE_TYPE (@1)))
> + && operand_equal_p (@1, @2)))
> (with
> {
> - int cmp = tree_int_cst_compare (@1, @2);
> + int cmp = 0;
> + if (TREE_CODE (@1) == INTEGER_CST
> + && TREE_CODE (@2) == INTEGER_CST)
> + cmp = tree_int_cst_compare (@1, @2);
> }
> (switch
> /* Choose the more restrictive of two < or <= comparisons. */
> @@ -2861,18 +2880,28 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> && (code1 == GT_EXPR || code1 == GE_EXPR)
> && (code2 == LT_EXPR || code2 == LE_EXPR))
> { constant_boolean_node (false, type); })
> - )))))
> + ))))))
>
> /* Convert (X == CST1) || (X OP2 CST2) to a known value
> based on CST1 OP2 CST2. Similarly for (X != CST1). */
> +/* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type.
> + Similarly for (X != Y). */
>
> (for code1 (eq ne)
> (for code2 (eq ne lt gt le ge)
> (simplify
> - (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
> + (bit_ior:c (code1@3 @0 @1) (code2@4 @0 @2))
> + (if ((TREE_CODE (@1) == INTEGER_CST
> + && TREE_CODE (@2) == INTEGER_CST)
> + || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
> + || POINTER_TYPE_P (TREE_TYPE (@1)))
> + && operand_equal_p (@1, @2)))
> (with
> {
> - int cmp = tree_int_cst_compare (@1, @2);
> + int cmp = 0;
> + if (TREE_CODE (@1) == INTEGER_CST
> + && TREE_CODE (@2) == INTEGER_CST)
> + cmp = tree_int_cst_compare (@1, @2);
> bool val;
> switch (code2)
> {
> @@ -2888,17 +2917,26 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> (switch
> (if (code1 == EQ_EXPR && val) @4)
> (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
> - (if (code1 == NE_EXPR && !val) @3))))))
> + (if (code1 == NE_EXPR && !val) @3)))))))
>
> -/* Convert (X OP1 CST1) || (X OP2 CST2). */
> +/* Convert (X OP1 CST1) || (X OP2 CST2).
> + Convert (X OP1 Y) || (X OP2 Y). */
>
> (for code1 (lt le gt ge)
> (for code2 (lt le gt ge)
> (simplify
> - (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
> + (bit_ior (code1@3 @0 @1) (code2@4 @0 @2))
> + (if ((TREE_CODE (@1) == INTEGER_CST
> + && TREE_CODE (@2) == INTEGER_CST)
> + || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
> + || POINTER_TYPE_P (TREE_TYPE (@1)))
> + && operand_equal_p (@1, @2)))
> (with
> {
> - int cmp = tree_int_cst_compare (@1, @2);
> + int cmp = 0;
> + if (TREE_CODE (@1) == INTEGER_CST
> + && TREE_CODE (@2) == INTEGER_CST)
> + cmp = tree_int_cst_compare (@1, @2);
> }
> (switch
> /* Choose the more restrictive of two < or <= comparisons. */
> @@ -2927,7 +2965,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> && (code1 == GT_EXPR || code1 == GE_EXPR)
> && (code2 == LT_EXPR || code2 == LE_EXPR))
> { constant_boolean_node (true, type); })
> - )))))
> + ))))))
>
> /* We can't reassociate at all for saturating types. */
> (if (!TYPE_SATURATING (type))
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cmpbit-1.c b/gcc/testsuite/gcc.dg/tree-ssa/cmpbit-1.c
> new file mode 100644
> index 00000000000..c219e047ba8
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/cmpbit-1.c
> @@ -0,0 +1,38 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O1 -fno-tree-reassoc -fdump-tree-optimized-raw" } */
> +_Bool f(int a, int b)
> +{
> + _Bool c = a > b;
> + _Bool d = a >= b;
> + return c & d;
> +}
> +
> +_Bool f1(int a, int b)
> +{
> + _Bool c = a > b;
> + _Bool d = a >= b;
> + return c | d;
> +}
> +
> +_Bool g(int a, int b)
> +{
> + _Bool c = a < b;
> + _Bool d = a <= b;
> + return c & d;
> +}
> +
> +_Bool g1(int a, int b)
> +{
> + _Bool c = a < b;
> + _Bool d = a <= b;
> + return c | d;
> +}
> +
> +
> +/* We should be able to optimize these without reassociation too. */
> +/* { dg-final { scan-tree-dump-not "bit_and_expr," "optimized" } } */
> +/* { dg-final { scan-tree-dump-not "bit_ior_expr," "optimized" } } */
> +/* { dg-final { scan-tree-dump-times "gt_expr," 1 "optimized" } } */
> +/* { dg-final { scan-tree-dump-times "ge_expr," 1 "optimized" } } */
> +/* { dg-final { scan-tree-dump-times "lt_expr," 1 "optimized" } } */
> +/* { dg-final { scan-tree-dump-times "le_expr," 1 "optimized" } } */
> --
> 2.31.1
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 2/2] MATCH: Add `a == b | a cmp b` and `a != b & a cmp b` simplifications
2023-07-31 5:34 ` [PATCH 2/2] MATCH: Add `a == b | a cmp b` and `a != b & a cmp b` simplifications Andrew Pinski
@ 2023-07-31 10:52 ` Richard Biener
2023-07-31 17:11 ` Andrew Pinski
0 siblings, 1 reply; 5+ messages in thread
From: Richard Biener @ 2023-07-31 10:52 UTC (permalink / raw)
To: Andrew Pinski; +Cc: gcc-patches
On Mon, Jul 31, 2023 at 7:35 AM Andrew Pinski via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> Even though these are done by combine_comparisons, we can add them to match
> to allow simplifcations during match rather than just during reassoc/ifcombine.
>
> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
OK. Note we want to get rid of the GENERIC folding parts of
maybe_fold_{and,or}_comparisons which is what those passes rely on.
Richard.
> gcc/ChangeLog:
>
> PR tree-optimization/106164
> * match.pd (`a != b & a <= b`, `a != b & a >= b`,
> `a == b | a < b`, `a == b | a > b`): Handle these cases
> too.
>
> gcc/testsuite/ChangeLog:
>
> PR tree-optimization/106164
> * gcc.dg/tree-ssa/cmpbit-2.c: New test.
> ---
> gcc/match.pd | 32 +++++++++++++++++--
> gcc/testsuite/gcc.dg/tree-ssa/cmpbit-2.c | 39 ++++++++++++++++++++++++
> 2 files changed, 69 insertions(+), 2 deletions(-)
> create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cmpbit-2.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 00af5d99119..cf8057701ea 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -2832,7 +2832,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> (switch
> (if (code1 == EQ_EXPR && val) @3)
> (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
> - (if (code1 == NE_EXPR && !val) @4)))))))
> + (if (code1 == NE_EXPR && !val) @4)
> + (if (code1 == NE_EXPR
> + && code2 == GE_EXPR
> + && cmp == 0)
> + (gt @0 @1))
> + (if (code1 == NE_EXPR
> + && code2 == LE_EXPR
> + && cmp == 0)
> + (lt @0 @1))
> + )
> + )
> + )
> + )
> + )
> +)
>
> /* Convert (X OP1 CST1) && (X OP2 CST2).
> Convert (X OP1 Y) && (X OP2 Y). */
> @@ -2917,7 +2931,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> (switch
> (if (code1 == EQ_EXPR && val) @4)
> (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
> - (if (code1 == NE_EXPR && !val) @3)))))))
> + (if (code1 == NE_EXPR && !val) @3)
> + (if (code1 == EQ_EXPR
> + && code2 == GT_EXPR
> + && cmp == 0)
> + (ge @0 @1))
> + (if (code1 == EQ_EXPR
> + && code2 == LT_EXPR
> + && cmp == 0)
> + (le @0 @1))
> + )
> + )
> + )
> + )
> + )
> +)
>
> /* Convert (X OP1 CST1) || (X OP2 CST2).
> Convert (X OP1 Y) || (X OP2 Y). */
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cmpbit-2.c b/gcc/testsuite/gcc.dg/tree-ssa/cmpbit-2.c
> new file mode 100644
> index 00000000000..c4226ef01af
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/cmpbit-2.c
> @@ -0,0 +1,39 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O1 -fno-tree-reassoc -fdump-tree-optimized-raw" } */
> +
> +_Bool f(int a, int b)
> +{
> + _Bool c = a == b;
> + _Bool d = a > b;
> + return c | d;
> +}
> +
> +_Bool f1(int a, int b)
> +{
> + _Bool c = a != b;
> + _Bool d = a >= b;
> + return c & d;
> +}
> +
> +_Bool g(int a, int b)
> +{
> + _Bool c = a == b;
> + _Bool d = a < b;
> + return c | d;
> +}
> +
> +_Bool g1(int a, int b)
> +{
> + _Bool c = a != b;
> + _Bool d = a <= b;
> + return c & d;
> +}
> +
> +
> +/* We should be able to optimize these without reassociation too. */
> +/* { dg-final { scan-tree-dump-not "bit_and_expr," "optimized" } } */
> +/* { dg-final { scan-tree-dump-not "bit_ior_expr," "optimized" } } */
> +/* { dg-final { scan-tree-dump-times "gt_expr," 1 "optimized" } } */
> +/* { dg-final { scan-tree-dump-times "ge_expr," 1 "optimized" } } */
> +/* { dg-final { scan-tree-dump-times "lt_expr," 1 "optimized" } } */
> +/* { dg-final { scan-tree-dump-times "le_expr," 1 "optimized" } } */
> --
> 2.31.1
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 2/2] MATCH: Add `a == b | a cmp b` and `a != b & a cmp b` simplifications
2023-07-31 10:52 ` Richard Biener
@ 2023-07-31 17:11 ` Andrew Pinski
0 siblings, 0 replies; 5+ messages in thread
From: Andrew Pinski @ 2023-07-31 17:11 UTC (permalink / raw)
To: Richard Biener; +Cc: Andrew Pinski, gcc-patches
On Mon, Jul 31, 2023 at 3:53 AM Richard Biener via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> On Mon, Jul 31, 2023 at 7:35 AM Andrew Pinski via Gcc-patches
> <gcc-patches@gcc.gnu.org> wrote:
> >
> > Even though these are done by combine_comparisons, we can add them to match
> > to allow simplifcations during match rather than just during reassoc/ifcombine.
> >
> > OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
>
> OK. Note we want to get rid of the GENERIC folding parts of
> maybe_fold_{and,or}_comparisons which is what those passes rely on.
Yes that was the idea here but there are still a few more cases that
need to be handled: Floating point, and some integer constants due to
`a <= 1` changing to `a < 0`.
Thanks,
Andrew
>
> Richard.
>
> > gcc/ChangeLog:
> >
> > PR tree-optimization/106164
> > * match.pd (`a != b & a <= b`, `a != b & a >= b`,
> > `a == b | a < b`, `a == b | a > b`): Handle these cases
> > too.
> >
> > gcc/testsuite/ChangeLog:
> >
> > PR tree-optimization/106164
> > * gcc.dg/tree-ssa/cmpbit-2.c: New test.
> > ---
> > gcc/match.pd | 32 +++++++++++++++++--
> > gcc/testsuite/gcc.dg/tree-ssa/cmpbit-2.c | 39 ++++++++++++++++++++++++
> > 2 files changed, 69 insertions(+), 2 deletions(-)
> > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cmpbit-2.c
> >
> > diff --git a/gcc/match.pd b/gcc/match.pd
> > index 00af5d99119..cf8057701ea 100644
> > --- a/gcc/match.pd
> > +++ b/gcc/match.pd
> > @@ -2832,7 +2832,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> > (switch
> > (if (code1 == EQ_EXPR && val) @3)
> > (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
> > - (if (code1 == NE_EXPR && !val) @4)))))))
> > + (if (code1 == NE_EXPR && !val) @4)
> > + (if (code1 == NE_EXPR
> > + && code2 == GE_EXPR
> > + && cmp == 0)
> > + (gt @0 @1))
> > + (if (code1 == NE_EXPR
> > + && code2 == LE_EXPR
> > + && cmp == 0)
> > + (lt @0 @1))
> > + )
> > + )
> > + )
> > + )
> > + )
> > +)
> >
> > /* Convert (X OP1 CST1) && (X OP2 CST2).
> > Convert (X OP1 Y) && (X OP2 Y). */
> > @@ -2917,7 +2931,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> > (switch
> > (if (code1 == EQ_EXPR && val) @4)
> > (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
> > - (if (code1 == NE_EXPR && !val) @3)))))))
> > + (if (code1 == NE_EXPR && !val) @3)
> > + (if (code1 == EQ_EXPR
> > + && code2 == GT_EXPR
> > + && cmp == 0)
> > + (ge @0 @1))
> > + (if (code1 == EQ_EXPR
> > + && code2 == LT_EXPR
> > + && cmp == 0)
> > + (le @0 @1))
> > + )
> > + )
> > + )
> > + )
> > + )
> > +)
> >
> > /* Convert (X OP1 CST1) || (X OP2 CST2).
> > Convert (X OP1 Y) || (X OP2 Y). */
> > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cmpbit-2.c b/gcc/testsuite/gcc.dg/tree-ssa/cmpbit-2.c
> > new file mode 100644
> > index 00000000000..c4226ef01af
> > --- /dev/null
> > +++ b/gcc/testsuite/gcc.dg/tree-ssa/cmpbit-2.c
> > @@ -0,0 +1,39 @@
> > +/* { dg-do compile } */
> > +/* { dg-options "-O1 -fno-tree-reassoc -fdump-tree-optimized-raw" } */
> > +
> > +_Bool f(int a, int b)
> > +{
> > + _Bool c = a == b;
> > + _Bool d = a > b;
> > + return c | d;
> > +}
> > +
> > +_Bool f1(int a, int b)
> > +{
> > + _Bool c = a != b;
> > + _Bool d = a >= b;
> > + return c & d;
> > +}
> > +
> > +_Bool g(int a, int b)
> > +{
> > + _Bool c = a == b;
> > + _Bool d = a < b;
> > + return c | d;
> > +}
> > +
> > +_Bool g1(int a, int b)
> > +{
> > + _Bool c = a != b;
> > + _Bool d = a <= b;
> > + return c & d;
> > +}
> > +
> > +
> > +/* We should be able to optimize these without reassociation too. */
> > +/* { dg-final { scan-tree-dump-not "bit_and_expr," "optimized" } } */
> > +/* { dg-final { scan-tree-dump-not "bit_ior_expr," "optimized" } } */
> > +/* { dg-final { scan-tree-dump-times "gt_expr," 1 "optimized" } } */
> > +/* { dg-final { scan-tree-dump-times "ge_expr," 1 "optimized" } } */
> > +/* { dg-final { scan-tree-dump-times "lt_expr," 1 "optimized" } } */
> > +/* { dg-final { scan-tree-dump-times "le_expr," 1 "optimized" } } */
> > --
> > 2.31.1
> >
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2023-07-31 17:11 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-31 5:34 [PATCH 1/2] MATCH: PR 106164 : Optimize `(X CMP1 Y) AND/IOR (X CMP2 Y)` Andrew Pinski
2023-07-31 5:34 ` [PATCH 2/2] MATCH: Add `a == b | a cmp b` and `a != b & a cmp b` simplifications Andrew Pinski
2023-07-31 10:52 ` Richard Biener
2023-07-31 17:11 ` Andrew Pinski
2023-07-31 10:50 ` [PATCH 1/2] MATCH: PR 106164 : Optimize `(X CMP1 Y) AND/IOR (X CMP2 Y)` Richard Biener
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).