* [PATCH 0/3] Fix PR 111972 @ 2023-12-02 6:37 Andrew Pinski 2023-12-02 6:37 ` [PATCH 1/3] MATCH: Fix zero_one_valued_p's convert pattern Andrew Pinski ` (2 more replies) 0 siblings, 3 replies; 7+ messages in thread From: Andrew Pinski @ 2023-12-02 6:37 UTC (permalink / raw) To: gcc-patches This patch set fixes PR 111972 and the fallout from it. The first patch is a fix to zero_one_valued_p's convert pattern which I hit while running the testsuite with the last patch. The second patch is a fix for -fanalyzer which I had implemented with a different version of the 3rd patch while I was working at Marvell. And the 3rd patch fixes the issue by having the following as canonical forms: * `a ^ 1` is the canonical form of `(convert_back)(zero_one == 0)` (and `(convert_back)(zero_one != 1)`). * `(convert)a` is the canonical form of `(convert)(zero_one != 0)` (and `(convert)(zero_one == 1)`). Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com> Andrew Pinski (3): MATCH: Fix zero_one_valued_p's convert pattern Remove check of unsigned_char in maybe_undo_optimize_bit_field_compare. MATCH: (convert)(zero_one !=/== 0/1) for outer type and zero_one type are the same gcc/analyzer/region-model-manager.cc | 3 -- gcc/match.pd | 24 +++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr110637-1.c | 10 +++++++ gcc/testsuite/gcc.dg/tree-ssa/pr110637-2.c | 13 +++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr110637-3.c | 14 +++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr111972-1.c | 34 ++++++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr69270.c | 4 +-- gcc/testsuite/gcc.target/i386/pr110790-2.c | 16 ++++++++-- 8 files changed, 111 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110637-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110637-2.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110637-3.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr111972-1.c -- 2.34.1 ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 1/3] MATCH: Fix zero_one_valued_p's convert pattern 2023-12-02 6:37 [PATCH 0/3] Fix PR 111972 Andrew Pinski @ 2023-12-02 6:37 ` Andrew Pinski 2023-12-04 14:21 ` Richard Biener 2023-12-02 6:37 ` [PATCH 2/3] Remove check of unsigned_char in maybe_undo_optimize_bit_field_compare Andrew Pinski 2023-12-02 6:37 ` [PATCH 3/3] MATCH: (convert)(zero_one !=/== 0/1) for outer type and zero_one type are the same Andrew Pinski 2 siblings, 1 reply; 7+ messages in thread From: Andrew Pinski @ 2023-12-02 6:37 UTC (permalink / raw) To: gcc-patches While working on PR 111972, I was getting a regression due to zero_one_valued_p matching a signed 1 bit integer when it came to convert. This patch fixes that by checking the outer type too. Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: * match.pd (zero_one_valued_p): For convert make sure type is not a signed 1-bit integer. Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com> --- gcc/match.pd | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gcc/match.pd b/gcc/match.pd index 26383e55767..4d554ba4721 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -2247,6 +2247,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)) && (TYPE_UNSIGNED (TREE_TYPE (@1)) || TYPE_PRECISION (TREE_TYPE (@1)) > 1) + && INTEGRAL_TYPE_P (type) + && (TYPE_UNSIGNED (type) + || TYPE_PRECISION (type) > 1) && wi::leu_p (tree_nonzero_bits (@1), 1)))) /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }. */ -- 2.39.3 ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 1/3] MATCH: Fix zero_one_valued_p's convert pattern 2023-12-02 6:37 ` [PATCH 1/3] MATCH: Fix zero_one_valued_p's convert pattern Andrew Pinski @ 2023-12-04 14:21 ` Richard Biener 0 siblings, 0 replies; 7+ messages in thread From: Richard Biener @ 2023-12-04 14:21 UTC (permalink / raw) To: Andrew Pinski; +Cc: gcc-patches On Sat, Dec 2, 2023 at 7:38 AM Andrew Pinski <quic_apinski@quicinc.com> wrote: > > While working on PR 111972, I was getting a regression > due to zero_one_valued_p matching a signed 1 bit integer > when it came to convert. This patch fixes that by checking > the outer type too. > > Bootstrapped and tested on x86_64-linux-gnu with no regressions. OK > gcc/ChangeLog: > > * match.pd (zero_one_valued_p): For convert > make sure type is not a signed 1-bit integer. > > Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com> > --- > gcc/match.pd | 3 +++ > 1 file changed, 3 insertions(+) > > diff --git a/gcc/match.pd b/gcc/match.pd > index 26383e55767..4d554ba4721 100644 > --- a/gcc/match.pd > +++ b/gcc/match.pd > @@ -2247,6 +2247,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)) > && (TYPE_UNSIGNED (TREE_TYPE (@1)) > || TYPE_PRECISION (TREE_TYPE (@1)) > 1) > + && INTEGRAL_TYPE_P (type) > + && (TYPE_UNSIGNED (type) > + || TYPE_PRECISION (type) > 1) > && wi::leu_p (tree_nonzero_bits (@1), 1)))) > > /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }. */ > -- > 2.39.3 > ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 2/3] Remove check of unsigned_char in maybe_undo_optimize_bit_field_compare. 2023-12-02 6:37 [PATCH 0/3] Fix PR 111972 Andrew Pinski 2023-12-02 6:37 ` [PATCH 1/3] MATCH: Fix zero_one_valued_p's convert pattern Andrew Pinski @ 2023-12-02 6:37 ` Andrew Pinski 2023-12-02 6:37 ` [PATCH 3/3] MATCH: (convert)(zero_one !=/== 0/1) for outer type and zero_one type are the same Andrew Pinski 2 siblings, 0 replies; 7+ messages in thread From: Andrew Pinski @ 2023-12-02 6:37 UTC (permalink / raw) To: gcc-patches From: Andrew Pinski <apinski@marvell.com> The check for the type seems unnecessary and gets in the way sometimes. Also with a patch I am working on for match.pd, it causes a failure to happen. Before my patch the IR was: _1 = BIT_FIELD_REF <s, 8, 16>; _2 = _1 & 1; _3 = _2 != 0; _4 = (int) _3; __analyzer_eval (_4); Where _2 was an unsigned char type. And After my patch we have: _1 = BIT_FIELD_REF <s, 8, 16>; _2 = (int) _1; _3 = _2 & 1; __analyzer_eval (_3); But in this case, the BIT_AND_EXPR is in an int type. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/analyzer/ChangeLog: * region-model-manager.cc (maybe_undo_optimize_bit_field_compare): Remove the check for type being unsigned_char_type_node. --- gcc/analyzer/region-model-manager.cc | 3 --- 1 file changed, 3 deletions(-) diff --git a/gcc/analyzer/region-model-manager.cc b/gcc/analyzer/region-model-manager.cc index 921edc55868..9a17b9d2878 100644 --- a/gcc/analyzer/region-model-manager.cc +++ b/gcc/analyzer/region-model-manager.cc @@ -586,9 +586,6 @@ maybe_undo_optimize_bit_field_compare (tree type, tree cst, const svalue *arg1) { - if (type != unsigned_char_type_node) - return NULL; - const binding_map &map = compound_sval->get_map (); unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (cst); /* If "mask" is a contiguous range of set bits, see if the -- 2.39.3 ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 3/3] MATCH: (convert)(zero_one !=/== 0/1) for outer type and zero_one type are the same 2023-12-02 6:37 [PATCH 0/3] Fix PR 111972 Andrew Pinski 2023-12-02 6:37 ` [PATCH 1/3] MATCH: Fix zero_one_valued_p's convert pattern Andrew Pinski 2023-12-02 6:37 ` [PATCH 2/3] Remove check of unsigned_char in maybe_undo_optimize_bit_field_compare Andrew Pinski @ 2023-12-02 6:37 ` Andrew Pinski 2023-12-04 14:21 ` Richard Biener 2 siblings, 1 reply; 7+ messages in thread From: Andrew Pinski @ 2023-12-02 6:37 UTC (permalink / raw) To: gcc-patches When I moved two_value to match.pd, I removed the check for the {0,+-1} as I had placed it after the {0,+-1} case for cond in match.pd. In the case of {0,+-1} and non boolean, before we would optmize those case to just `(convert)a` but after we would get `(convert)(a != 0)` which was not handled anyways to just `(convert)a`. So this adds a pattern to match `(convert)(zeroone != 0)` and simplify to `(convert)zeroone`. Also this optimizes (convert)(zeroone == 0) into (zeroone^1) if the type match. This can only be done on the gimple level as if zeroone was defined by (a&1), fold will convert (a&1)^1 back into `(convert)(zeroone == 0)` and an infinite loop will happen. Note the testcase pr69270.c needed a slight update due to not matching exactly a scan pattern, this update makes it more robust and will match before and afterwards and if there are other changes in this area too. Note the testcase gcc.target/i386/pr110790-2.c needs a slight update for better code generation in LP64 bit mode. Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: PR tree-optimization/111972 PR tree-optimization/110637 * match.pd (`(convert)(zeroone !=/== CST)`): Match and simplify to ((convert)zeroone){,^1}. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/pr110637-1.c: New test. * gcc.dg/tree-ssa/pr110637-2.c: New test. * gcc.dg/tree-ssa/pr110637-3.c: New test. * gcc.dg/tree-ssa/pr111972-1.c: New test. * gcc.dg/tree-ssa/pr69270.c: Update testcase. * gcc.target/i386/pr110790-2.c: Update testcase. Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com> --- gcc/match.pd | 21 +++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr110637-1.c | 10 +++++++ gcc/testsuite/gcc.dg/tree-ssa/pr110637-2.c | 13 +++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr110637-3.c | 14 +++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr111972-1.c | 34 ++++++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr69270.c | 4 +-- gcc/testsuite/gcc.target/i386/pr110790-2.c | 16 ++++++++-- 7 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110637-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110637-2.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110637-3.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr111972-1.c diff --git a/gcc/match.pd b/gcc/match.pd index 4d554ba4721..656b2c9edda 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3332,6 +3332,27 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0))) (rcmp @0 @1)))) +/* (type)([0,1]@a != 0) -> (type)a + (type)([0,1]@a == 1) -> (type)a + (type)([0,1]@a == 0) -> a ^ 1 + (type)([0,1]@a != 1) -> a ^ 1. */ +(for eqne (eq ne) + (simplify + (convert (eqne zero_one_valued_p@0 INTEGER_CST@1)) + (if ((integer_zerop (@1) || integer_onep (@1))) + (if ((eqne == EQ_EXPR) ^ integer_zerop (@1)) + (convert @0) + /* a^1 can only be produced for gimple as + fold has the exact opposite transformation + for `(X & 1) ^ 1`. + See `Fold ~X & 1 as (X & 1) == 0.` + and `Fold (X ^ 1) & 1 as (X & 1) == 0.` in fold-const.cc. + Only do this if the types match as (type)(a == 0) is + canonical form normally, while `a ^ 1` is canonical when + there is no type change. */ + (if (GIMPLE && types_match (type, TREE_TYPE (@0))) + (bit_xor @0 { build_one_cst (type); } )))))) + /* We can't reassociate at all for saturating types. */ (if (!TYPE_SATURATING (type)) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110637-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-1.c new file mode 100644 index 00000000000..3d03b0992a4 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-1.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-optimized" } */ +int f(int a) +{ + int b = (a & 1)!=0; + return b; +} + +/* This should be optimized to just return (a & 1); */ +/* { dg-final { scan-tree-dump-not " == " "optimized"} } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110637-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-2.c new file mode 100644 index 00000000000..f1c5b90353a --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-2.c @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-optimized" } */ +int f(int a) +{ + int b = a & 1; + int c = b == 0; + return c; +} + +/* This should be optimized to just return `(a&1) ^ 1` or `(~a) & 1`. */ +/* { dg-final { scan-tree-dump-not " == " "optimized"} } */ +/* { dg-final { scan-tree-dump-times "~a" 1 "optimized"} } */ +/* { dg-final { scan-tree-dump-times " & 1" 1 "optimized"} } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110637-3.c b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-3.c new file mode 100644 index 00000000000..ce80146d9df --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-3.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-optimized" } */ +int f(int a) +{ + int b = a & 1; + int c = b == 0; + int d = ~a; + int e = d & 1; + return c == e; +} + +/* This should be optimized to just `return 1` */ +/* { dg-final { scan-tree-dump-not " == " "optimized"} } */ +/* { dg-final { scan-tree-dump-times "return 1" 1 "optimized"} } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr111972-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr111972-1.c new file mode 100644 index 00000000000..0611808ed50 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr111972-1.c @@ -0,0 +1,34 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-phiopt" } */ +double +foo() { + long n3 = 3450000, xtra = 7270; + long i,ix; + long j; + double Check; + + /* Section 3, Conditional jumps */ + j = 0; + { + for (ix=0; ix<xtra; ix++) + { + for(i=0; i<n3; i++) + { + if(j==1) j = 2; + else j = 3; + if(j>2) j = 0; + else j = 1; + if(j<1) j = 1; + else j = 0; + } + } + } + Check = Check + (double)j; + return Check; +} + +/* the above if statements in loop should be optimized to just `j ^ 1` + and should not be (type)(j != 1). */ +/* { dg-final { scan-tree-dump-not " != 1" "phiopt2"} } */ +/* { dg-final { scan-tree-dump-times " \\^ 1" 1 "phiopt2"} } */ + diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr69270.c b/gcc/testsuite/gcc.dg/tree-ssa/pr69270.c index 0d66cc4383f..b08ec9d6ddb 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/pr69270.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr69270.c @@ -7,8 +7,8 @@ /* { dg-final { scan-tree-dump-times "Replaced .bufferstep_\[0-9\]+. with constant .1." 1 "dom3"} } */ /* And some assignments ought to fold down to constants. */ -/* { dg-final { scan-tree-dump-times "Folded to: _\[0-9\]+ = 1;" 1 "dom3"} } */ -/* { dg-final { scan-tree-dump-times "Folded to: _\[0-9\]+ = 0;" 1 "dom3"} } */ +/* { dg-final { scan-tree-dump-times "Folded to: (?:bufferstep)?_\[0-9\]+ = 1;" 1 "dom3"} } */ +/* { dg-final { scan-tree-dump-times "Folded to: (?:bufferstep)?_\[0-9\]+ = 0;" 1 "dom3"} } */ /* The XOR operations should have been optimized to constants. */ /* { dg-final { scan-tree-dump-not "bit_xor" "dom3"} } */ diff --git a/gcc/testsuite/gcc.target/i386/pr110790-2.c b/gcc/testsuite/gcc.target/i386/pr110790-2.c index 8b9d650c6e9..16c73cb7465 100644 --- a/gcc/testsuite/gcc.target/i386/pr110790-2.c +++ b/gcc/testsuite/gcc.target/i386/pr110790-2.c @@ -9,5 +9,17 @@ refmpn_tstbit_bad (mp_srcptr ptr, unsigned long bit) return (((ptr)[(bit)/(32 - 0)] & (((mp_limb_t) 1L) << ((bit)%(32 - 0)))) != 0); } -/* { dg-final { scan-assembler "bt\[ql\]" } } */ -/* { dg-final { scan-assembler "setc" } } */ +/* 32bit produces: + btl %eax, %edx + setc %al + movzbl %al, %eax + */ +/* { dg-final { scan-assembler "bt\[ql\]" { target { ! lp64 } } } } */ +/* { dg-final { scan-assembler "setc" { target { ! lp64 } } } } */ + +/* 64bit produces: + shrq %cl, %rax + andl $1, %eax + */ +/* { dg-final { scan-assembler-times "shrq" 2 { target { lp64 } } } } */ +/* { dg-final { scan-assembler-times "andl" 2 { target { lp64 } } } } */ -- 2.39.3 ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 3/3] MATCH: (convert)(zero_one !=/== 0/1) for outer type and zero_one type are the same 2023-12-02 6:37 ` [PATCH 3/3] MATCH: (convert)(zero_one !=/== 0/1) for outer type and zero_one type are the same Andrew Pinski @ 2023-12-04 14:21 ` Richard Biener 2023-12-04 21:14 ` Andrew Pinski (QUIC) 0 siblings, 1 reply; 7+ messages in thread From: Richard Biener @ 2023-12-04 14:21 UTC (permalink / raw) To: Andrew Pinski; +Cc: gcc-patches On Sat, Dec 2, 2023 at 7:38 AM Andrew Pinski <quic_apinski@quicinc.com> wrote: > > When I moved two_value to match.pd, I removed the check for the {0,+-1} > as I had placed it after the {0,+-1} case for cond in match.pd. > In the case of {0,+-1} and non boolean, before we would optmize those > case to just `(convert)a` but after we would get `(convert)(a != 0)` > which was not handled anyways to just `(convert)a`. > So this adds a pattern to match `(convert)(zeroone != 0)` and simplify > to `(convert)zeroone`. > > Also this optimizes (convert)(zeroone == 0) into (zeroone^1) if the > type match. This can only be done on the gimple level as if zeroone > was defined by (a&1), fold will convert (a&1)^1 back into > `(convert)(zeroone == 0)` and an infinite loop will happen. So fold converts (a&1)^1 to (convert)(a&1 == 0)? Can we fix (remove) this instead or do we rely on that? > Note the testcase pr69270.c needed a slight update due to not matching > exactly a scan pattern, this update makes it more robust and will match > before and afterwards and if there are other changes in this area too. > > Note the testcase gcc.target/i386/pr110790-2.c needs a slight update > for better code generation in LP64 bit mode. > > Bootstrapped and tested on x86_64-linux-gnu with no regressions. Otherwise OK. Thanks, Richard. > gcc/ChangeLog: > > PR tree-optimization/111972 > PR tree-optimization/110637 > * match.pd (`(convert)(zeroone !=/== CST)`): Match > and simplify to ((convert)zeroone){,^1}. > > gcc/testsuite/ChangeLog: > > * gcc.dg/tree-ssa/pr110637-1.c: New test. > * gcc.dg/tree-ssa/pr110637-2.c: New test. > * gcc.dg/tree-ssa/pr110637-3.c: New test. > * gcc.dg/tree-ssa/pr111972-1.c: New test. > * gcc.dg/tree-ssa/pr69270.c: Update testcase. > * gcc.target/i386/pr110790-2.c: Update testcase. > > Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com> > --- > gcc/match.pd | 21 +++++++++++++ > gcc/testsuite/gcc.dg/tree-ssa/pr110637-1.c | 10 +++++++ > gcc/testsuite/gcc.dg/tree-ssa/pr110637-2.c | 13 +++++++++ > gcc/testsuite/gcc.dg/tree-ssa/pr110637-3.c | 14 +++++++++ > gcc/testsuite/gcc.dg/tree-ssa/pr111972-1.c | 34 ++++++++++++++++++++++ > gcc/testsuite/gcc.dg/tree-ssa/pr69270.c | 4 +-- > gcc/testsuite/gcc.target/i386/pr110790-2.c | 16 ++++++++-- > 7 files changed, 108 insertions(+), 4 deletions(-) > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110637-1.c > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110637-2.c > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110637-3.c > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr111972-1.c > > diff --git a/gcc/match.pd b/gcc/match.pd > index 4d554ba4721..656b2c9edda 100644 > --- a/gcc/match.pd > +++ b/gcc/match.pd > @@ -3332,6 +3332,27 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0))) > (rcmp @0 @1)))) > > +/* (type)([0,1]@a != 0) -> (type)a > + (type)([0,1]@a == 1) -> (type)a > + (type)([0,1]@a == 0) -> a ^ 1 > + (type)([0,1]@a != 1) -> a ^ 1. */ > +(for eqne (eq ne) > + (simplify > + (convert (eqne zero_one_valued_p@0 INTEGER_CST@1)) > + (if ((integer_zerop (@1) || integer_onep (@1))) > + (if ((eqne == EQ_EXPR) ^ integer_zerop (@1)) > + (convert @0) > + /* a^1 can only be produced for gimple as > + fold has the exact opposite transformation > + for `(X & 1) ^ 1`. > + See `Fold ~X & 1 as (X & 1) == 0.` > + and `Fold (X ^ 1) & 1 as (X & 1) == 0.` in fold-const.cc. > + Only do this if the types match as (type)(a == 0) is > + canonical form normally, while `a ^ 1` is canonical when > + there is no type change. */ > + (if (GIMPLE && types_match (type, TREE_TYPE (@0))) > + (bit_xor @0 { build_one_cst (type); } )))))) > + > /* We can't reassociate at all for saturating types. */ > (if (!TYPE_SATURATING (type)) > > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110637-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-1.c > new file mode 100644 > index 00000000000..3d03b0992a4 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-1.c > @@ -0,0 +1,10 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O1 -fdump-tree-optimized" } */ > +int f(int a) > +{ > + int b = (a & 1)!=0; > + return b; > +} > + > +/* This should be optimized to just return (a & 1); */ > +/* { dg-final { scan-tree-dump-not " == " "optimized"} } */ > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110637-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-2.c > new file mode 100644 > index 00000000000..f1c5b90353a > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-2.c > @@ -0,0 +1,13 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O1 -fdump-tree-optimized" } */ > +int f(int a) > +{ > + int b = a & 1; > + int c = b == 0; > + return c; > +} > + > +/* This should be optimized to just return `(a&1) ^ 1` or `(~a) & 1`. */ > +/* { dg-final { scan-tree-dump-not " == " "optimized"} } */ > +/* { dg-final { scan-tree-dump-times "~a" 1 "optimized"} } */ > +/* { dg-final { scan-tree-dump-times " & 1" 1 "optimized"} } */ > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110637-3.c b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-3.c > new file mode 100644 > index 00000000000..ce80146d9df > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-3.c > @@ -0,0 +1,14 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O1 -fdump-tree-optimized" } */ > +int f(int a) > +{ > + int b = a & 1; > + int c = b == 0; > + int d = ~a; > + int e = d & 1; > + return c == e; > +} > + > +/* This should be optimized to just `return 1` */ > +/* { dg-final { scan-tree-dump-not " == " "optimized"} } */ > +/* { dg-final { scan-tree-dump-times "return 1" 1 "optimized"} } */ > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr111972-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr111972-1.c > new file mode 100644 > index 00000000000..0611808ed50 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr111972-1.c > @@ -0,0 +1,34 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O1 -fdump-tree-phiopt" } */ > +double > +foo() { > + long n3 = 3450000, xtra = 7270; > + long i,ix; > + long j; > + double Check; > + > + /* Section 3, Conditional jumps */ > + j = 0; > + { > + for (ix=0; ix<xtra; ix++) > + { > + for(i=0; i<n3; i++) > + { > + if(j==1) j = 2; > + else j = 3; > + if(j>2) j = 0; > + else j = 1; > + if(j<1) j = 1; > + else j = 0; > + } > + } > + } > + Check = Check + (double)j; > + return Check; > +} > + > +/* the above if statements in loop should be optimized to just `j ^ 1` > + and should not be (type)(j != 1). */ > +/* { dg-final { scan-tree-dump-not " != 1" "phiopt2"} } */ > +/* { dg-final { scan-tree-dump-times " \\^ 1" 1 "phiopt2"} } */ > + > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr69270.c b/gcc/testsuite/gcc.dg/tree-ssa/pr69270.c > index 0d66cc4383f..b08ec9d6ddb 100644 > --- a/gcc/testsuite/gcc.dg/tree-ssa/pr69270.c > +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr69270.c > @@ -7,8 +7,8 @@ > /* { dg-final { scan-tree-dump-times "Replaced .bufferstep_\[0-9\]+. with constant .1." 1 "dom3"} } */ > > /* And some assignments ought to fold down to constants. */ > -/* { dg-final { scan-tree-dump-times "Folded to: _\[0-9\]+ = 1;" 1 "dom3"} } */ > -/* { dg-final { scan-tree-dump-times "Folded to: _\[0-9\]+ = 0;" 1 "dom3"} } */ > +/* { dg-final { scan-tree-dump-times "Folded to: (?:bufferstep)?_\[0-9\]+ = 1;" 1 "dom3"} } */ > +/* { dg-final { scan-tree-dump-times "Folded to: (?:bufferstep)?_\[0-9\]+ = 0;" 1 "dom3"} } */ > > /* The XOR operations should have been optimized to constants. */ > /* { dg-final { scan-tree-dump-not "bit_xor" "dom3"} } */ > diff --git a/gcc/testsuite/gcc.target/i386/pr110790-2.c b/gcc/testsuite/gcc.target/i386/pr110790-2.c > index 8b9d650c6e9..16c73cb7465 100644 > --- a/gcc/testsuite/gcc.target/i386/pr110790-2.c > +++ b/gcc/testsuite/gcc.target/i386/pr110790-2.c > @@ -9,5 +9,17 @@ refmpn_tstbit_bad (mp_srcptr ptr, unsigned long bit) > return (((ptr)[(bit)/(32 - 0)] & (((mp_limb_t) 1L) << ((bit)%(32 - 0)))) != 0); > } > > -/* { dg-final { scan-assembler "bt\[ql\]" } } */ > -/* { dg-final { scan-assembler "setc" } } */ > +/* 32bit produces: > + btl %eax, %edx > + setc %al > + movzbl %al, %eax > + */ > +/* { dg-final { scan-assembler "bt\[ql\]" { target { ! lp64 } } } } */ > +/* { dg-final { scan-assembler "setc" { target { ! lp64 } } } } */ > + > +/* 64bit produces: > + shrq %cl, %rax > + andl $1, %eax > + */ > +/* { dg-final { scan-assembler-times "shrq" 2 { target { lp64 } } } } */ > +/* { dg-final { scan-assembler-times "andl" 2 { target { lp64 } } } } */ > -- > 2.39.3 > ^ permalink raw reply [flat|nested] 7+ messages in thread
* RE: [PATCH 3/3] MATCH: (convert)(zero_one !=/== 0/1) for outer type and zero_one type are the same 2023-12-04 14:21 ` Richard Biener @ 2023-12-04 21:14 ` Andrew Pinski (QUIC) 0 siblings, 0 replies; 7+ messages in thread From: Andrew Pinski (QUIC) @ 2023-12-04 21:14 UTC (permalink / raw) To: Richard Biener, Andrew Pinski (QUIC); +Cc: gcc-patches > -----Original Message----- > From: Richard Biener <richard.guenther@gmail.com> > Sent: Monday, December 4, 2023 6:22 AM > To: Andrew Pinski (QUIC) <quic_apinski@quicinc.com> > Cc: gcc-patches@gcc.gnu.org > Subject: Re: [PATCH 3/3] MATCH: (convert)(zero_one !=/== 0/1) for outer > type and zero_one type are the same > > On Sat, Dec 2, 2023 at 7:38 AM Andrew Pinski <quic_apinski@quicinc.com> > wrote: > > > > When I moved two_value to match.pd, I removed the check for the > > {0,+-1} as I had placed it after the {0,+-1} case for cond in match.pd. > > In the case of {0,+-1} and non boolean, before we would optmize those > > case to just `(convert)a` but after we would get `(convert)(a != 0)` > > which was not handled anyways to just `(convert)a`. > > So this adds a pattern to match `(convert)(zeroone != 0)` and simplify > > to `(convert)zeroone`. > > > > Also this optimizes (convert)(zeroone == 0) into (zeroone^1) if the > > type match. This can only be done on the gimple level as if zeroone > > was defined by (a&1), fold will convert (a&1)^1 back into > > `(convert)(zeroone == 0)` and an infinite loop will happen. > > So fold converts (a&1)^1 to (convert)(a&1 == 0)? Can we fix (remove) this > instead or do we rely on that? I have not tried to remove it but I will try to see if we depend on this. Thanks, Andrew > > > Note the testcase pr69270.c needed a slight update due to not matching > > exactly a scan pattern, this update makes it more robust and will > > match before and afterwards and if there are other changes in this area too. > > > > Note the testcase gcc.target/i386/pr110790-2.c needs a slight update > > for better code generation in LP64 bit mode. > > > > Bootstrapped and tested on x86_64-linux-gnu with no regressions. > > Otherwise OK. > > Thanks, > Richard. > > > gcc/ChangeLog: > > > > PR tree-optimization/111972 > > PR tree-optimization/110637 > > * match.pd (`(convert)(zeroone !=/== CST)`): Match > > and simplify to ((convert)zeroone){,^1}. > > > > gcc/testsuite/ChangeLog: > > > > * gcc.dg/tree-ssa/pr110637-1.c: New test. > > * gcc.dg/tree-ssa/pr110637-2.c: New test. > > * gcc.dg/tree-ssa/pr110637-3.c: New test. > > * gcc.dg/tree-ssa/pr111972-1.c: New test. > > * gcc.dg/tree-ssa/pr69270.c: Update testcase. > > * gcc.target/i386/pr110790-2.c: Update testcase. > > > > Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com> > > --- > > gcc/match.pd | 21 +++++++++++++ > > gcc/testsuite/gcc.dg/tree-ssa/pr110637-1.c | 10 +++++++ > > gcc/testsuite/gcc.dg/tree-ssa/pr110637-2.c | 13 +++++++++ > > gcc/testsuite/gcc.dg/tree-ssa/pr110637-3.c | 14 +++++++++ > > gcc/testsuite/gcc.dg/tree-ssa/pr111972-1.c | 34 > ++++++++++++++++++++++ > > gcc/testsuite/gcc.dg/tree-ssa/pr69270.c | 4 +-- > > gcc/testsuite/gcc.target/i386/pr110790-2.c | 16 ++++++++-- > > 7 files changed, 108 insertions(+), 4 deletions(-) create mode > > 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110637-1.c > > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110637-2.c > > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110637-3.c > > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr111972-1.c > > > > diff --git a/gcc/match.pd b/gcc/match.pd index > > 4d554ba4721..656b2c9edda 100644 > > --- a/gcc/match.pd > > +++ b/gcc/match.pd > > @@ -3332,6 +3332,27 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > > (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE > (@0))) > > (rcmp @0 @1)))) > > > > +/* (type)([0,1]@a != 0) -> (type)a > > + (type)([0,1]@a == 1) -> (type)a > > + (type)([0,1]@a == 0) -> a ^ 1 > > + (type)([0,1]@a != 1) -> a ^ 1. */ (for eqne (eq ne) (simplify > > + (convert (eqne zero_one_valued_p@0 INTEGER_CST@1)) > > + (if ((integer_zerop (@1) || integer_onep (@1))) > > + (if ((eqne == EQ_EXPR) ^ integer_zerop (@1)) > > + (convert @0) > > + /* a^1 can only be produced for gimple as > > + fold has the exact opposite transformation > > + for `(X & 1) ^ 1`. > > + See `Fold ~X & 1 as (X & 1) == 0.` > > + and `Fold (X ^ 1) & 1 as (X & 1) == 0.` in fold-const.cc. > > + Only do this if the types match as (type)(a == 0) is > > + canonical form normally, while `a ^ 1` is canonical when > > + there is no type change. */ > > + (if (GIMPLE && types_match (type, TREE_TYPE (@0))) > > + (bit_xor @0 { build_one_cst (type); } )))))) > > + > > /* We can't reassociate at all for saturating types. */ (if > > (!TYPE_SATURATING (type)) > > > > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110637-1.c > > b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-1.c > > new file mode 100644 > > index 00000000000..3d03b0992a4 > > --- /dev/null > > +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-1.c > > @@ -0,0 +1,10 @@ > > +/* { dg-do compile } */ > > +/* { dg-options "-O1 -fdump-tree-optimized" } */ int f(int a) { > > + int b = (a & 1)!=0; > > + return b; > > +} > > + > > +/* This should be optimized to just return (a & 1); */ > > +/* { dg-final { scan-tree-dump-not " == " "optimized"} } */ > > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110637-2.c > > b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-2.c > > new file mode 100644 > > index 00000000000..f1c5b90353a > > --- /dev/null > > +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-2.c > > @@ -0,0 +1,13 @@ > > +/* { dg-do compile } */ > > +/* { dg-options "-O1 -fdump-tree-optimized" } */ int f(int a) { > > + int b = a & 1; > > + int c = b == 0; > > + return c; > > +} > > + > > +/* This should be optimized to just return `(a&1) ^ 1` or `(~a) & 1`. > > +*/ > > +/* { dg-final { scan-tree-dump-not " == " "optimized"} } */ > > +/* { dg-final { scan-tree-dump-times "~a" 1 "optimized"} } */ > > +/* { dg-final { scan-tree-dump-times " & 1" 1 "optimized"} } */ > > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110637-3.c > > b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-3.c > > new file mode 100644 > > index 00000000000..ce80146d9df > > --- /dev/null > > +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-3.c > > @@ -0,0 +1,14 @@ > > +/* { dg-do compile } */ > > +/* { dg-options "-O1 -fdump-tree-optimized" } */ int f(int a) { > > + int b = a & 1; > > + int c = b == 0; > > + int d = ~a; > > + int e = d & 1; > > + return c == e; > > +} > > + > > +/* This should be optimized to just `return 1` */ > > +/* { dg-final { scan-tree-dump-not " == " "optimized"} } */ > > +/* { dg-final { scan-tree-dump-times "return 1" 1 "optimized"} } */ > > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr111972-1.c > > b/gcc/testsuite/gcc.dg/tree-ssa/pr111972-1.c > > new file mode 100644 > > index 00000000000..0611808ed50 > > --- /dev/null > > +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr111972-1.c > > @@ -0,0 +1,34 @@ > > +/* { dg-do compile } */ > > +/* { dg-options "-O1 -fdump-tree-phiopt" } */ double > > +foo() { > > + long n3 = 3450000, xtra = 7270; > > + long i,ix; > > + long j; > > + double Check; > > + > > + /* Section 3, Conditional jumps */ > > + j = 0; > > + { > > + for (ix=0; ix<xtra; ix++) > > + { > > + for(i=0; i<n3; i++) > > + { > > + if(j==1) j = 2; > > + else j = 3; > > + if(j>2) j = 0; > > + else j = 1; > > + if(j<1) j = 1; > > + else j = 0; > > + } > > + } > > + } > > + Check = Check + (double)j; > > + return Check; > > +} > > + > > +/* the above if statements in loop should be optimized to just `j ^ 1` > > + and should not be (type)(j != 1). */ > > +/* { dg-final { scan-tree-dump-not " != 1" "phiopt2"} } */ > > +/* { dg-final { scan-tree-dump-times " \\^ 1" 1 "phiopt2"} } */ > > + > > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr69270.c > > b/gcc/testsuite/gcc.dg/tree-ssa/pr69270.c > > index 0d66cc4383f..b08ec9d6ddb 100644 > > --- a/gcc/testsuite/gcc.dg/tree-ssa/pr69270.c > > +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr69270.c > > @@ -7,8 +7,8 @@ > > /* { dg-final { scan-tree-dump-times "Replaced .bufferstep_\[0-9\]+. > > with constant .1." 1 "dom3"} } */ > > > > /* And some assignments ought to fold down to constants. */ > > -/* { dg-final { scan-tree-dump-times "Folded to: _\[0-9\]+ = 1;" 1 > > "dom3"} } */ > > -/* { dg-final { scan-tree-dump-times "Folded to: _\[0-9\]+ = 0;" 1 > > "dom3"} } */ > > +/* { dg-final { scan-tree-dump-times "Folded to: > > +(?:bufferstep)?_\[0-9\]+ = 1;" 1 "dom3"} } */ > > +/* { dg-final { scan-tree-dump-times "Folded to: > > +(?:bufferstep)?_\[0-9\]+ = 0;" 1 "dom3"} } */ > > > > /* The XOR operations should have been optimized to constants. */ > > /* { dg-final { scan-tree-dump-not "bit_xor" "dom3"} } */ diff --git > > a/gcc/testsuite/gcc.target/i386/pr110790-2.c > > b/gcc/testsuite/gcc.target/i386/pr110790-2.c > > index 8b9d650c6e9..16c73cb7465 100644 > > --- a/gcc/testsuite/gcc.target/i386/pr110790-2.c > > +++ b/gcc/testsuite/gcc.target/i386/pr110790-2.c > > @@ -9,5 +9,17 @@ refmpn_tstbit_bad (mp_srcptr ptr, unsigned long bit) > > return (((ptr)[(bit)/(32 - 0)] & (((mp_limb_t) 1L) << ((bit)%(32 - > > 0)))) != 0); } > > > > -/* { dg-final { scan-assembler "bt\[ql\]" } } */ > > -/* { dg-final { scan-assembler "setc" } } */ > > +/* 32bit produces: > > + btl %eax, %edx > > + setc %al > > + movzbl %al, %eax > > + */ > > +/* { dg-final { scan-assembler "bt\[ql\]" { target { ! lp64 } } } } > > +*/ > > +/* { dg-final { scan-assembler "setc" { target { ! lp64 } } } } */ > > + > > +/* 64bit produces: > > + shrq %cl, %rax > > + andl $1, %eax > > + */ > > +/* { dg-final { scan-assembler-times "shrq" 2 { target { lp64 } } } } > > +*/ > > +/* { dg-final { scan-assembler-times "andl" 2 { target { lp64 } } } } > > +*/ > > -- > > 2.39.3 > > ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2023-12-04 21:14 UTC | newest] Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2023-12-02 6:37 [PATCH 0/3] Fix PR 111972 Andrew Pinski 2023-12-02 6:37 ` [PATCH 1/3] MATCH: Fix zero_one_valued_p's convert pattern Andrew Pinski 2023-12-04 14:21 ` Richard Biener 2023-12-02 6:37 ` [PATCH 2/3] Remove check of unsigned_char in maybe_undo_optimize_bit_field_compare Andrew Pinski 2023-12-02 6:37 ` [PATCH 3/3] MATCH: (convert)(zero_one !=/== 0/1) for outer type and zero_one type are the same Andrew Pinski 2023-12-04 14:21 ` Richard Biener 2023-12-04 21:14 ` Andrew Pinski (QUIC)
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).