Re: [PATCH 2/2] VR-VALUES: Rewrite test_for_singularity using range_op_handler
On 9/5/23 01:12, Andrew Pinski wrote: On Mon, Sep 4, 2023 at 11:06 PM Jeff Law via Gcc-patches wrote: On 9/1/23 11:30, Andrew Pinski via Gcc-patches wrote: So it turns out there was a simplier way of starting to improve VRP to start to fix PR 110131, PR 108360, and PR 108397. That was rewrite test_for_singularity to use range_op_handler and Value_Range. This patch implements that and OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: * vr-values.cc (test_for_singularity): Add edge argument and rewrite using range_op_handler. (simplify_compare_using_range_pairs): Use Value_Range instead of value_range and update test_for_singularity call. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/vrp124.c: New test. * gcc.dg/tree-ssa/vrp125.c: New test. --- diff --git a/gcc/vr-values.cc b/gcc/vr-values.cc index 52ab4fe6109..2474e57ee90 100644 --- a/gcc/vr-values.cc +++ b/gcc/vr-values.cc @@ -904,69 +904,33 @@ simplify_using_ranges::simplify_bit_ops_using_ranges } /* We are comparing trees OP1 and OP2 using COND_CODE. OP1 has - a known value range VR. + a known value range OP1_RANGE. If there is one and only one value which will satisfy the - conditional, then return that value. Else return NULL. - - If signed overflow must be undefined for the value to satisfy - the conditional, then set *STRICT_OVERFLOW_P to true. */ + conditional on the EDGE, then return that value. + Else return NULL. */ static tree test_for_singularity (enum tree_code cond_code, tree op1, - tree op2, const value_range *vr) + tree op2, const int_range_max _range, bool edge) { - tree min = NULL; - tree max = NULL; - - /* Extract minimum/maximum values which satisfy the conditional as it was - written. */ - if (cond_code == LE_EXPR || cond_code == LT_EXPR) + /* This is already a singularity. */ + if (cond_code == NE_EXPR || cond_code == EQ_EXPR) +return NULL; I don't think this is necessarily the right thing to do for NE. Consider if op1 has the range [0,1] and op2 has the value 1. If the code is NE, then we should be able to return a singularity of 0 since that's the only value for x where x ne 1 is true given the range for x. The "false" edge singularity is already known when NE is supplied. I don't think changing it to the "true" edge singularity will be helpful all of the time; preferring the value of 0 is a different story. But that is a different patch and for a different location rather than inside VRP; it should be in either isel or expand (more likely isel). I forgot something critically important here. Specifically, this code is supposed to be subsumed by Ranger. The whole point of this routine is to rewrite to EQ/NE comparisons so that we can expose equivalences on the true/false arm of the conditional (and NE is just as important as EQ). It's not really about preferring any particular value like 0. The problem with this routine is it loses information after the code has been transformed. Let's say we had a test x < 4. If we assume that VRP is able to prove X doesn't have any of the values [MIN..3], then we can change the test to x == 4 and propagate 4 for any uses of X in the true arm. But on the false ARM we end up with x != 4 which is a wider range than [5..MAX]. So if we were to instantiate a new Ranger after the transformation we'd lose information on the false arm. More importantly, I think the transformation was bad for either SCEV or loop iteration analysis. When Andrew, Aldy and I kicked this problem around the consensus was that Ranger should find and propagate the equivalence, including making it visible to jump threading. That should make the rewriting totally unnecessary. So the net is we really ought to be doing here is looking for cases where this code actually helps code generation and if it does we need to understand how/why as this code is supposed to go away. Given you're already poking around in here, you might have such cases handy :-) If you do, I'm sure Andrew, Aldy and I would love to see them. jeff
Re: [PATCH 2/2] VR-VALUES: Rewrite test_for_singularity using range_op_handler
On 9/1/23 02:40, Andrew Pinski wrote: On Fri, Aug 11, 2023 at 8:08 AM Andrew MacLeod via Gcc-patches wrote: If this is only going to work with integers, you might want to check that somewhere or switch to irange and int_range_max.. You can make it work with any kind (if you know op1 is a constant) by simply doing Value_Range op1_range (TREE_TYPE (op1)) get_global_range_query->range_of_expr (op1_range, op1) That will convert trees to a the appropriate range... THis is also true for integer constants... but you can also just do the WI conversion like you do. The routine also get confusing to read because it passes in op0 and op1, but of course ranger uses op1 and op2 nomenclature, and it looks a bit confusing :-P I'd change the operands passed in to op1 and op2 if we are rewriting the routine. Ranger using the nomenclature of op1/op2 and gimple is inconsistent with trees and other parts of GCC. It seems like we have to live with this inconsistency now too. Renaming things in this one function to op1/op2 might be ok but the rest of the file uses op0/op1 too; most likely because it was originally written before gimple. I think it would be good to have this written in the coding style, which way should we have it for new code; if we start at 0 or 1 for operands. It might reduce differences based on who wrote which part (and even to some extent when). I don't really care which one is picked as long as we pick one. Thanks, Andrew Pinski I certainly wont argue it would be good to be consistent, but of course its quite prevalent. Perhaps we should rewrite vr-values.cc to change the terminology in one patch? long term some of it is likely to get absorbed into rangeops, and what isn't could/should be made vrange/irange aware... no one has gotten to it yet. we could change the terminology as the routines are reworked too... Andrew
Re: [PATCH 2/2] VR-VALUES: Rewrite test_for_singularity using range_op_handler
On Mon, Sep 4, 2023 at 11:06 PM Jeff Law via Gcc-patches wrote: > > > > On 9/1/23 11:30, Andrew Pinski via Gcc-patches wrote: > > So it turns out there was a simplier way of starting to > > improve VRP to start to fix PR 110131, PR 108360, and PR 108397. > > That was rewrite test_for_singularity to use range_op_handler > > and Value_Range. > > > > This patch implements that and > > > > OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. > > > > gcc/ChangeLog: > > > > * vr-values.cc (test_for_singularity): Add edge argument > > and rewrite using range_op_handler. > > (simplify_compare_using_range_pairs): Use Value_Range > > instead of value_range and update test_for_singularity call. > > > > gcc/testsuite/ChangeLog: > > > > * gcc.dg/tree-ssa/vrp124.c: New test. > > * gcc.dg/tree-ssa/vrp125.c: New test. > > --- > > > diff --git a/gcc/vr-values.cc b/gcc/vr-values.cc > > index 52ab4fe6109..2474e57ee90 100644 > > --- a/gcc/vr-values.cc > > +++ b/gcc/vr-values.cc > > @@ -904,69 +904,33 @@ simplify_using_ranges::simplify_bit_ops_using_ranges > > } > > > > /* We are comparing trees OP1 and OP2 using COND_CODE. OP1 has > > - a known value range VR. > > + a known value range OP1_RANGE. > > > > If there is one and only one value which will satisfy the > > - conditional, then return that value. Else return NULL. > > - > > - If signed overflow must be undefined for the value to satisfy > > - the conditional, then set *STRICT_OVERFLOW_P to true. */ > > + conditional on the EDGE, then return that value. > > + Else return NULL. */ > > > > static tree > > test_for_singularity (enum tree_code cond_code, tree op1, > > - tree op2, const value_range *vr) > > + tree op2, const int_range_max _range, bool edge) > > { > > - tree min = NULL; > > - tree max = NULL; > > - > > - /* Extract minimum/maximum values which satisfy the conditional as it was > > - written. */ > > - if (cond_code == LE_EXPR || cond_code == LT_EXPR) > > + /* This is already a singularity. */ > > + if (cond_code == NE_EXPR || cond_code == EQ_EXPR) > > +return NULL; > I don't think this is necessarily the right thing to do for NE. > > Consider if op1 has the range [0,1] and op2 has the value 1. If the > code is NE, then we should be able to return a singularity of 0 since > that's the only value for x where x ne 1 is true given the range for x. The "false" edge singularity is already known when NE is supplied. I don't think changing it to the "true" edge singularity will be helpful all of the time; preferring the value of 0 is a different story. But that is a different patch and for a different location rather than inside VRP; it should be in either isel or expand (more likely isel). Thanks, Andrew > > > > I like what you're trying to do, it just needs a bit of refinement I think. > > jeff
Re: [PATCH 2/2] VR-VALUES: Rewrite test_for_singularity using range_op_handler
On 9/1/23 11:30, Andrew Pinski via Gcc-patches wrote: So it turns out there was a simplier way of starting to improve VRP to start to fix PR 110131, PR 108360, and PR 108397. That was rewrite test_for_singularity to use range_op_handler and Value_Range. This patch implements that and OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: * vr-values.cc (test_for_singularity): Add edge argument and rewrite using range_op_handler. (simplify_compare_using_range_pairs): Use Value_Range instead of value_range and update test_for_singularity call. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/vrp124.c: New test. * gcc.dg/tree-ssa/vrp125.c: New test. --- diff --git a/gcc/vr-values.cc b/gcc/vr-values.cc index 52ab4fe6109..2474e57ee90 100644 --- a/gcc/vr-values.cc +++ b/gcc/vr-values.cc @@ -904,69 +904,33 @@ simplify_using_ranges::simplify_bit_ops_using_ranges } /* We are comparing trees OP1 and OP2 using COND_CODE. OP1 has - a known value range VR. + a known value range OP1_RANGE. If there is one and only one value which will satisfy the - conditional, then return that value. Else return NULL. - - If signed overflow must be undefined for the value to satisfy - the conditional, then set *STRICT_OVERFLOW_P to true. */ + conditional on the EDGE, then return that value. + Else return NULL. */ static tree test_for_singularity (enum tree_code cond_code, tree op1, - tree op2, const value_range *vr) + tree op2, const int_range_max _range, bool edge) { - tree min = NULL; - tree max = NULL; - - /* Extract minimum/maximum values which satisfy the conditional as it was - written. */ - if (cond_code == LE_EXPR || cond_code == LT_EXPR) + /* This is already a singularity. */ + if (cond_code == NE_EXPR || cond_code == EQ_EXPR) +return NULL; I don't think this is necessarily the right thing to do for NE. Consider if op1 has the range [0,1] and op2 has the value 1. If the code is NE, then we should be able to return a singularity of 0 since that's the only value for x where x ne 1 is true given the range for x. I like what you're trying to do, it just needs a bit of refinement I think. jeff
[PATCH 2/2] VR-VALUES: Rewrite test_for_singularity using range_op_handler
So it turns out there was a simplier way of starting to improve VRP to start to fix PR 110131, PR 108360, and PR 108397. That was rewrite test_for_singularity to use range_op_handler and Value_Range. This patch implements that and OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: * vr-values.cc (test_for_singularity): Add edge argument and rewrite using range_op_handler. (simplify_compare_using_range_pairs): Use Value_Range instead of value_range and update test_for_singularity call. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/vrp124.c: New test. * gcc.dg/tree-ssa/vrp125.c: New test. --- gcc/testsuite/gcc.dg/tree-ssa/vrp124.c | 44 gcc/testsuite/gcc.dg/tree-ssa/vrp125.c | 44 gcc/vr-values.cc | 99 -- 3 files changed, 117 insertions(+), 70 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/vrp124.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/vrp125.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp124.c b/gcc/testsuite/gcc.dg/tree-ssa/vrp124.c new file mode 100644 index 000..6ccbda35d1b --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp124.c @@ -0,0 +1,44 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +/* Should be optimized to a == -100 */ +int g(int a) +{ + if (a == -100 || a >= 0) +; + else +return 0; + return a < 0; +} + +/* Should optimize to a == 0 */ +int f(int a) +{ + if (a == 0 || a > 100) +; + else +return 0; + return a < 50; +} + +/* Should be optimized to a == 0. */ +int f2(int a) +{ + if (a == 0 || a > 100) +; + else +return 0; + return a < 100; +} + +/* Should optimize to a == 100 */ +int f1(int a) +{ + if (a < 0 || a == 100) +; + else +return 0; + return a > 50; +} + +/* { dg-final { scan-tree-dump-not "goto " "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp125.c b/gcc/testsuite/gcc.dg/tree-ssa/vrp125.c new file mode 100644 index 000..f6c2f8e35f1 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp125.c @@ -0,0 +1,44 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +/* Should be optimized to a == -100 */ +int g(int a) +{ + if (a == -100 || a == -50 || a >= 0) +; + else +return 0; + return a < -50; +} + +/* Should optimize to a == 0 */ +int f(int a) +{ + if (a == 0 || a == 50 || a > 100) +; + else +return 0; + return a < 50; +} + +/* Should be optimized to a == 0. */ +int f2(int a) +{ + if (a == 0 || a == 50 || a > 100) +; + else +return 0; + return a < 25; +} + +/* Should optimize to a == 100 */ +int f1(int a) +{ + if (a < 0 || a == 50 || a == 100) +; + else +return 0; + return a > 50; +} + +/* { dg-final { scan-tree-dump-not "goto " "optimized" } } */ diff --git a/gcc/vr-values.cc b/gcc/vr-values.cc index 52ab4fe6109..2474e57ee90 100644 --- a/gcc/vr-values.cc +++ b/gcc/vr-values.cc @@ -904,69 +904,33 @@ simplify_using_ranges::simplify_bit_ops_using_ranges } /* We are comparing trees OP1 and OP2 using COND_CODE. OP1 has - a known value range VR. + a known value range OP1_RANGE. If there is one and only one value which will satisfy the - conditional, then return that value. Else return NULL. - - If signed overflow must be undefined for the value to satisfy - the conditional, then set *STRICT_OVERFLOW_P to true. */ + conditional on the EDGE, then return that value. + Else return NULL. */ static tree test_for_singularity (enum tree_code cond_code, tree op1, - tree op2, const value_range *vr) + tree op2, const int_range_max _range, bool edge) { - tree min = NULL; - tree max = NULL; - - /* Extract minimum/maximum values which satisfy the conditional as it was - written. */ - if (cond_code == LE_EXPR || cond_code == LT_EXPR) + /* This is already a singularity. */ + if (cond_code == NE_EXPR || cond_code == EQ_EXPR) +return NULL; + auto range_op = range_op_handler (cond_code); + wide_int w = wi::to_wide (op2); + int_range<1> op2_range (TREE_TYPE (op2), w, w); + int_range_max vr; + if (range_op.op1_range (vr, TREE_TYPE (op1), + edge ? range_true () : range_false (), + op2_range)) { - min = TYPE_MIN_VALUE (TREE_TYPE (op1)); - - max = op2; - if (cond_code == LT_EXPR) - { - tree one = build_int_cst (TREE_TYPE (op1), 1); - max = fold_build2 (MINUS_EXPR, TREE_TYPE (op1), max, one); - /* Signal to compare_values_warnv this expr doesn't overflow. */ - if (EXPR_P (max)) - suppress_warning (max, OPT_Woverflow); - } -} - else if (cond_code == GE_EXPR || cond_code == GT_EXPR) -{ - max = TYPE_MAX_VALUE (TREE_TYPE (op1)); - - min = op2; - if (cond_code == GT_EXPR) - { - tree one = build_int_cst
Re: [PATCH 2/2] VR-VALUES: Rewrite test_for_singularity using range_op_handler
On Fri, Aug 11, 2023 at 8:08 AM Andrew MacLeod via Gcc-patches wrote: > > > On 8/11/23 05:51, Richard Biener wrote: > > On Fri, Aug 11, 2023 at 11:17 AM Andrew Pinski via Gcc-patches > > wrote: > >> So it turns out there was a simplier way of starting to > >> improve VRP to start to fix PR 110131, PR 108360, and PR 108397. > >> That was rewrite test_for_singularity to use range_op_handler > >> and Value_Range. > >> > >> This patch implements that and > >> > >> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. > > I'm hoping Andrew/Aldy can have a look here. > > > > Richard. > > > >> gcc/ChangeLog: > >> > >> * vr-values.cc (test_for_singularity): Add edge argument > >> and rewrite using range_op_handler. > >> (simplify_compare_using_range_pairs): Use Value_Range > >> instead of value_range and update test_for_singularity call. > >> > >> gcc/testsuite/ChangeLog: > >> > >> * gcc.dg/tree-ssa/vrp124.c: New test. > >> * gcc.dg/tree-ssa/vrp125.c: New test. > >> --- > >> gcc/testsuite/gcc.dg/tree-ssa/vrp124.c | 44 + > >> gcc/testsuite/gcc.dg/tree-ssa/vrp125.c | 44 + > >> gcc/vr-values.cc | 91 -- > >> 3 files changed, 114 insertions(+), 65 deletions(-) > >> create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/vrp124.c > >> create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/vrp125.c > >> > >> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp124.c > >> b/gcc/testsuite/gcc.dg/tree-ssa/vrp124.c > >> new file mode 100644 > >> index 000..6ccbda35d1b > >> --- /dev/null > >> +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp124.c > >> @@ -0,0 +1,44 @@ > >> +/* { dg-do compile } */ > >> +/* { dg-options "-O2 -fdump-tree-optimized" } */ > >> + > >> +/* Should be optimized to a == -100 */ > >> +int g(int a) > >> +{ > >> + if (a == -100 || a >= 0) > >> +; > >> + else > >> +return 0; > >> + return a < 0; > >> +} > >> + > >> +/* Should optimize to a == 0 */ > >> +int f(int a) > >> +{ > >> + if (a == 0 || a > 100) > >> +; > >> + else > >> +return 0; > >> + return a < 50; > >> +} > >> + > >> +/* Should be optimized to a == 0. */ > >> +int f2(int a) > >> +{ > >> + if (a == 0 || a > 100) > >> +; > >> + else > >> +return 0; > >> + return a < 100; > >> +} > >> + > >> +/* Should optimize to a == 100 */ > >> +int f1(int a) > >> +{ > >> + if (a < 0 || a == 100) > >> +; > >> + else > >> +return 0; > >> + return a > 50; > >> +} > >> + > >> +/* { dg-final { scan-tree-dump-not "goto " "optimized" } } */ > >> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp125.c > >> b/gcc/testsuite/gcc.dg/tree-ssa/vrp125.c > >> new file mode 100644 > >> index 000..f6c2f8e35f1 > >> --- /dev/null > >> +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp125.c > >> @@ -0,0 +1,44 @@ > >> +/* { dg-do compile } */ > >> +/* { dg-options "-O2 -fdump-tree-optimized" } */ > >> + > >> +/* Should be optimized to a == -100 */ > >> +int g(int a) > >> +{ > >> + if (a == -100 || a == -50 || a >= 0) > >> +; > >> + else > >> +return 0; > >> + return a < -50; > >> +} > >> + > >> +/* Should optimize to a == 0 */ > >> +int f(int a) > >> +{ > >> + if (a == 0 || a == 50 || a > 100) > >> +; > >> + else > >> +return 0; > >> + return a < 50; > >> +} > >> + > >> +/* Should be optimized to a == 0. */ > >> +int f2(int a) > >> +{ > >> + if (a == 0 || a == 50 || a > 100) > >> +; > >> + else > >> +return 0; > >> + return a < 25; > >> +} > >> + > >> +/* Should optimize to a == 100 */ > >> +int f1(int a) > >> +{ > >> + if (a < 0 || a == 50 || a == 100) > >> +; > >> + else > >> +return 0; > >> + return a > 50; > >> +} > >> + > >> +/* { dg-final { scan-tree-dump-not "goto " "optimized" } } */ > >> diff --git a/gcc/vr-values.cc b/gcc/vr-values.cc > >> index a4fddd62841..7004b0224bd 100644 > >> --- a/gcc/vr-values.cc > >> +++ b/gcc/vr-values.cc > >> @@ -907,66 +907,30 @@ simplify_using_ranges::simplify_bit_ops_using_ranges > >> a known value range VR. > >> > >> If there is one and only one value which will satisfy the > >> - conditional, then return that value. Else return NULL. > >> - > >> - If signed overflow must be undefined for the value to satisfy > >> - the conditional, then set *STRICT_OVERFLOW_P to true. */ > >> + conditional on the EDGE, then return that value. > >> + Else return NULL. */ > >> > >> static tree > >> test_for_singularity (enum tree_code cond_code, tree op0, > >> - tree op1, const value_range *vr) > >> + tree op1, Value_Range vr, bool edge) > > VR should be a "vrange &". THis is the top level base class for all > ranges of all types/kinds, and what we usually pass values around as if > we want tohem to be any kind. If this is inetger only, we'd pass a an > 'irange &' > > Value_Range is the opposite. Its the sink that contains one of each kind > of range and can switch around
Re: [PATCH 2/2] VR-VALUES: Rewrite test_for_singularity using range_op_handler
On Fri, Aug 11, 2023 at 8:08 AM Andrew MacLeod via Gcc-patches wrote: > > > On 8/11/23 05:51, Richard Biener wrote: > > On Fri, Aug 11, 2023 at 11:17 AM Andrew Pinski via Gcc-patches > > wrote: > >> So it turns out there was a simplier way of starting to > >> improve VRP to start to fix PR 110131, PR 108360, and PR 108397. > >> That was rewrite test_for_singularity to use range_op_handler > >> and Value_Range. > >> > >> This patch implements that and > >> > >> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. > > I'm hoping Andrew/Aldy can have a look here. > > > > Richard. > > > >> gcc/ChangeLog: > >> > >> * vr-values.cc (test_for_singularity): Add edge argument > >> and rewrite using range_op_handler. > >> (simplify_compare_using_range_pairs): Use Value_Range > >> instead of value_range and update test_for_singularity call. > >> > >> gcc/testsuite/ChangeLog: > >> > >> * gcc.dg/tree-ssa/vrp124.c: New test. > >> * gcc.dg/tree-ssa/vrp125.c: New test. > >> --- > >> gcc/testsuite/gcc.dg/tree-ssa/vrp124.c | 44 + > >> gcc/testsuite/gcc.dg/tree-ssa/vrp125.c | 44 + > >> gcc/vr-values.cc | 91 -- > >> 3 files changed, 114 insertions(+), 65 deletions(-) > >> create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/vrp124.c > >> create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/vrp125.c > >> > >> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp124.c > >> b/gcc/testsuite/gcc.dg/tree-ssa/vrp124.c > >> new file mode 100644 > >> index 000..6ccbda35d1b > >> --- /dev/null > >> +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp124.c > >> @@ -0,0 +1,44 @@ > >> +/* { dg-do compile } */ > >> +/* { dg-options "-O2 -fdump-tree-optimized" } */ > >> + > >> +/* Should be optimized to a == -100 */ > >> +int g(int a) > >> +{ > >> + if (a == -100 || a >= 0) > >> +; > >> + else > >> +return 0; > >> + return a < 0; > >> +} > >> + > >> +/* Should optimize to a == 0 */ > >> +int f(int a) > >> +{ > >> + if (a == 0 || a > 100) > >> +; > >> + else > >> +return 0; > >> + return a < 50; > >> +} > >> + > >> +/* Should be optimized to a == 0. */ > >> +int f2(int a) > >> +{ > >> + if (a == 0 || a > 100) > >> +; > >> + else > >> +return 0; > >> + return a < 100; > >> +} > >> + > >> +/* Should optimize to a == 100 */ > >> +int f1(int a) > >> +{ > >> + if (a < 0 || a == 100) > >> +; > >> + else > >> +return 0; > >> + return a > 50; > >> +} > >> + > >> +/* { dg-final { scan-tree-dump-not "goto " "optimized" } } */ > >> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp125.c > >> b/gcc/testsuite/gcc.dg/tree-ssa/vrp125.c > >> new file mode 100644 > >> index 000..f6c2f8e35f1 > >> --- /dev/null > >> +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp125.c > >> @@ -0,0 +1,44 @@ > >> +/* { dg-do compile } */ > >> +/* { dg-options "-O2 -fdump-tree-optimized" } */ > >> + > >> +/* Should be optimized to a == -100 */ > >> +int g(int a) > >> +{ > >> + if (a == -100 || a == -50 || a >= 0) > >> +; > >> + else > >> +return 0; > >> + return a < -50; > >> +} > >> + > >> +/* Should optimize to a == 0 */ > >> +int f(int a) > >> +{ > >> + if (a == 0 || a == 50 || a > 100) > >> +; > >> + else > >> +return 0; > >> + return a < 50; > >> +} > >> + > >> +/* Should be optimized to a == 0. */ > >> +int f2(int a) > >> +{ > >> + if (a == 0 || a == 50 || a > 100) > >> +; > >> + else > >> +return 0; > >> + return a < 25; > >> +} > >> + > >> +/* Should optimize to a == 100 */ > >> +int f1(int a) > >> +{ > >> + if (a < 0 || a == 50 || a == 100) > >> +; > >> + else > >> +return 0; > >> + return a > 50; > >> +} > >> + > >> +/* { dg-final { scan-tree-dump-not "goto " "optimized" } } */ > >> diff --git a/gcc/vr-values.cc b/gcc/vr-values.cc > >> index a4fddd62841..7004b0224bd 100644 > >> --- a/gcc/vr-values.cc > >> +++ b/gcc/vr-values.cc > >> @@ -907,66 +907,30 @@ simplify_using_ranges::simplify_bit_ops_using_ranges > >> a known value range VR. > >> > >> If there is one and only one value which will satisfy the > >> - conditional, then return that value. Else return NULL. > >> - > >> - If signed overflow must be undefined for the value to satisfy > >> - the conditional, then set *STRICT_OVERFLOW_P to true. */ > >> + conditional on the EDGE, then return that value. > >> + Else return NULL. */ > >> > >> static tree > >> test_for_singularity (enum tree_code cond_code, tree op0, > >> - tree op1, const value_range *vr) > >> + tree op1, Value_Range vr, bool edge) > > VR should be a "vrange &". THis is the top level base class for all > ranges of all types/kinds, and what we usually pass values around as if > we want tohem to be any kind. If this is inetger only, we'd pass a an > 'irange &' > > Value_Range is the opposite. Its the sink that contains one of each kind > of range and can switch around
Re: [PATCH 2/2] VR-VALUES: Rewrite test_for_singularity using range_op_handler
On 8/11/23 05:51, Richard Biener wrote: On Fri, Aug 11, 2023 at 11:17 AM Andrew Pinski via Gcc-patches wrote: So it turns out there was a simplier way of starting to improve VRP to start to fix PR 110131, PR 108360, and PR 108397. That was rewrite test_for_singularity to use range_op_handler and Value_Range. This patch implements that and OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. I'm hoping Andrew/Aldy can have a look here. Richard. gcc/ChangeLog: * vr-values.cc (test_for_singularity): Add edge argument and rewrite using range_op_handler. (simplify_compare_using_range_pairs): Use Value_Range instead of value_range and update test_for_singularity call. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/vrp124.c: New test. * gcc.dg/tree-ssa/vrp125.c: New test. --- gcc/testsuite/gcc.dg/tree-ssa/vrp124.c | 44 + gcc/testsuite/gcc.dg/tree-ssa/vrp125.c | 44 + gcc/vr-values.cc | 91 -- 3 files changed, 114 insertions(+), 65 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/vrp124.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/vrp125.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp124.c b/gcc/testsuite/gcc.dg/tree-ssa/vrp124.c new file mode 100644 index 000..6ccbda35d1b --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp124.c @@ -0,0 +1,44 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +/* Should be optimized to a == -100 */ +int g(int a) +{ + if (a == -100 || a >= 0) +; + else +return 0; + return a < 0; +} + +/* Should optimize to a == 0 */ +int f(int a) +{ + if (a == 0 || a > 100) +; + else +return 0; + return a < 50; +} + +/* Should be optimized to a == 0. */ +int f2(int a) +{ + if (a == 0 || a > 100) +; + else +return 0; + return a < 100; +} + +/* Should optimize to a == 100 */ +int f1(int a) +{ + if (a < 0 || a == 100) +; + else +return 0; + return a > 50; +} + +/* { dg-final { scan-tree-dump-not "goto " "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp125.c b/gcc/testsuite/gcc.dg/tree-ssa/vrp125.c new file mode 100644 index 000..f6c2f8e35f1 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp125.c @@ -0,0 +1,44 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +/* Should be optimized to a == -100 */ +int g(int a) +{ + if (a == -100 || a == -50 || a >= 0) +; + else +return 0; + return a < -50; +} + +/* Should optimize to a == 0 */ +int f(int a) +{ + if (a == 0 || a == 50 || a > 100) +; + else +return 0; + return a < 50; +} + +/* Should be optimized to a == 0. */ +int f2(int a) +{ + if (a == 0 || a == 50 || a > 100) +; + else +return 0; + return a < 25; +} + +/* Should optimize to a == 100 */ +int f1(int a) +{ + if (a < 0 || a == 50 || a == 100) +; + else +return 0; + return a > 50; +} + +/* { dg-final { scan-tree-dump-not "goto " "optimized" } } */ diff --git a/gcc/vr-values.cc b/gcc/vr-values.cc index a4fddd62841..7004b0224bd 100644 --- a/gcc/vr-values.cc +++ b/gcc/vr-values.cc @@ -907,66 +907,30 @@ simplify_using_ranges::simplify_bit_ops_using_ranges a known value range VR. If there is one and only one value which will satisfy the - conditional, then return that value. Else return NULL. - - If signed overflow must be undefined for the value to satisfy - the conditional, then set *STRICT_OVERFLOW_P to true. */ + conditional on the EDGE, then return that value. + Else return NULL. */ static tree test_for_singularity (enum tree_code cond_code, tree op0, - tree op1, const value_range *vr) + tree op1, Value_Range vr, bool edge) VR should be a "vrange &". THis is the top level base class for all ranges of all types/kinds, and what we usually pass values around as if we want tohem to be any kind. If this is inetger only, we'd pass a an 'irange &' Value_Range is the opposite. Its the sink that contains one of each kind of range and can switch around between them as needed. You do not want to pass that by value! The generic engine uses these so it can suppose floats. int, pointers, whatever... { - tree min = NULL; - tree max = NULL; - - /* Extract minimum/maximum values which satisfy the conditional as it was - written. */ - if (cond_code == LE_EXPR || cond_code == LT_EXPR) + /* This is already a singularity. */ + if (cond_code == NE_EXPR || cond_code == EQ_EXPR) +return NULL; + auto range_op = range_op_handler (cond_code); + int_range<2> op1_range (TREE_TYPE (op0)); + wide_int w = wi::to_wide (op1); + op1_range.set (TREE_TYPE (op1), w, w); If this is only going to work with integers, you might want to check that somewhere or switch to irange and int_range_max.. You can make it work with any kind (if you know op1 is a constant) by
Re: [PATCH 2/2] VR-VALUES: Rewrite test_for_singularity using range_op_handler
On 8/11/23 03:51, Richard Biener via Gcc-patches wrote: On Fri, Aug 11, 2023 at 11:17 AM Andrew Pinski via Gcc-patches wrote: So it turns out there was a simplier way of starting to improve VRP to start to fix PR 110131, PR 108360, and PR 108397. That was rewrite test_for_singularity to use range_op_handler and Value_Range. This patch implements that and OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. I'm hoping Andrew/Aldy can have a look here. It's actually pretty simple stuff. Instead of open-coding range identification for op1 and simplification of that range using op0's known range (VR), instead we generate a real range for op1 and intersect that result with the known range for op0. If the result is a singleton, return it. Simpler and more effective in the end. I guess the interactions with the warning subsystem are a non-issue in the updated code since it doesn't return an expression, but the singleton value (when in the hell did it start returning an expression, that just seems wrong given the result is supposed to be a singleton!) LGTM. Jeff
Re: [PATCH 2/2] VR-VALUES: Rewrite test_for_singularity using range_op_handler
On Fri, Aug 11, 2023 at 11:17 AM Andrew Pinski via Gcc-patches wrote: > > So it turns out there was a simplier way of starting to > improve VRP to start to fix PR 110131, PR 108360, and PR 108397. > That was rewrite test_for_singularity to use range_op_handler > and Value_Range. > > This patch implements that and > > OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. I'm hoping Andrew/Aldy can have a look here. Richard. > gcc/ChangeLog: > > * vr-values.cc (test_for_singularity): Add edge argument > and rewrite using range_op_handler. > (simplify_compare_using_range_pairs): Use Value_Range > instead of value_range and update test_for_singularity call. > > gcc/testsuite/ChangeLog: > > * gcc.dg/tree-ssa/vrp124.c: New test. > * gcc.dg/tree-ssa/vrp125.c: New test. > --- > gcc/testsuite/gcc.dg/tree-ssa/vrp124.c | 44 + > gcc/testsuite/gcc.dg/tree-ssa/vrp125.c | 44 + > gcc/vr-values.cc | 91 -- > 3 files changed, 114 insertions(+), 65 deletions(-) > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/vrp124.c > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/vrp125.c > > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp124.c > b/gcc/testsuite/gcc.dg/tree-ssa/vrp124.c > new file mode 100644 > index 000..6ccbda35d1b > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp124.c > @@ -0,0 +1,44 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-optimized" } */ > + > +/* Should be optimized to a == -100 */ > +int g(int a) > +{ > + if (a == -100 || a >= 0) > +; > + else > +return 0; > + return a < 0; > +} > + > +/* Should optimize to a == 0 */ > +int f(int a) > +{ > + if (a == 0 || a > 100) > +; > + else > +return 0; > + return a < 50; > +} > + > +/* Should be optimized to a == 0. */ > +int f2(int a) > +{ > + if (a == 0 || a > 100) > +; > + else > +return 0; > + return a < 100; > +} > + > +/* Should optimize to a == 100 */ > +int f1(int a) > +{ > + if (a < 0 || a == 100) > +; > + else > +return 0; > + return a > 50; > +} > + > +/* { dg-final { scan-tree-dump-not "goto " "optimized" } } */ > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp125.c > b/gcc/testsuite/gcc.dg/tree-ssa/vrp125.c > new file mode 100644 > index 000..f6c2f8e35f1 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp125.c > @@ -0,0 +1,44 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-optimized" } */ > + > +/* Should be optimized to a == -100 */ > +int g(int a) > +{ > + if (a == -100 || a == -50 || a >= 0) > +; > + else > +return 0; > + return a < -50; > +} > + > +/* Should optimize to a == 0 */ > +int f(int a) > +{ > + if (a == 0 || a == 50 || a > 100) > +; > + else > +return 0; > + return a < 50; > +} > + > +/* Should be optimized to a == 0. */ > +int f2(int a) > +{ > + if (a == 0 || a == 50 || a > 100) > +; > + else > +return 0; > + return a < 25; > +} > + > +/* Should optimize to a == 100 */ > +int f1(int a) > +{ > + if (a < 0 || a == 50 || a == 100) > +; > + else > +return 0; > + return a > 50; > +} > + > +/* { dg-final { scan-tree-dump-not "goto " "optimized" } } */ > diff --git a/gcc/vr-values.cc b/gcc/vr-values.cc > index a4fddd62841..7004b0224bd 100644 > --- a/gcc/vr-values.cc > +++ b/gcc/vr-values.cc > @@ -907,66 +907,30 @@ simplify_using_ranges::simplify_bit_ops_using_ranges > a known value range VR. > > If there is one and only one value which will satisfy the > - conditional, then return that value. Else return NULL. > - > - If signed overflow must be undefined for the value to satisfy > - the conditional, then set *STRICT_OVERFLOW_P to true. */ > + conditional on the EDGE, then return that value. > + Else return NULL. */ > > static tree > test_for_singularity (enum tree_code cond_code, tree op0, > - tree op1, const value_range *vr) > + tree op1, Value_Range vr, bool edge) > { > - tree min = NULL; > - tree max = NULL; > - > - /* Extract minimum/maximum values which satisfy the conditional as it was > - written. */ > - if (cond_code == LE_EXPR || cond_code == LT_EXPR) > + /* This is already a singularity. */ > + if (cond_code == NE_EXPR || cond_code == EQ_EXPR) > +return NULL; > + auto range_op = range_op_handler (cond_code); > + int_range<2> op1_range (TREE_TYPE (op0)); > + wide_int w = wi::to_wide (op1); > + op1_range.set (TREE_TYPE (op1), w, w); > + Value_Range vr1(TREE_TYPE (op0)); > + if (range_op.op1_range (vr1, TREE_TYPE (op0), > + edge ? range_true () : range_false (), > + op1_range)) > { > - min = TYPE_MIN_VALUE (TREE_TYPE (op0)); > - > - max = op1; > - if (cond_code == LT_EXPR) > - { > - tree one = build_int_cst (TREE_TYPE (op0), 1); > - max = fold_build2 (MINUS_EXPR, TREE_TYPE
[PATCH 2/2] VR-VALUES: Rewrite test_for_singularity using range_op_handler
So it turns out there was a simplier way of starting to improve VRP to start to fix PR 110131, PR 108360, and PR 108397. That was rewrite test_for_singularity to use range_op_handler and Value_Range. This patch implements that and OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: * vr-values.cc (test_for_singularity): Add edge argument and rewrite using range_op_handler. (simplify_compare_using_range_pairs): Use Value_Range instead of value_range and update test_for_singularity call. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/vrp124.c: New test. * gcc.dg/tree-ssa/vrp125.c: New test. --- gcc/testsuite/gcc.dg/tree-ssa/vrp124.c | 44 + gcc/testsuite/gcc.dg/tree-ssa/vrp125.c | 44 + gcc/vr-values.cc | 91 -- 3 files changed, 114 insertions(+), 65 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/vrp124.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/vrp125.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp124.c b/gcc/testsuite/gcc.dg/tree-ssa/vrp124.c new file mode 100644 index 000..6ccbda35d1b --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp124.c @@ -0,0 +1,44 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +/* Should be optimized to a == -100 */ +int g(int a) +{ + if (a == -100 || a >= 0) +; + else +return 0; + return a < 0; +} + +/* Should optimize to a == 0 */ +int f(int a) +{ + if (a == 0 || a > 100) +; + else +return 0; + return a < 50; +} + +/* Should be optimized to a == 0. */ +int f2(int a) +{ + if (a == 0 || a > 100) +; + else +return 0; + return a < 100; +} + +/* Should optimize to a == 100 */ +int f1(int a) +{ + if (a < 0 || a == 100) +; + else +return 0; + return a > 50; +} + +/* { dg-final { scan-tree-dump-not "goto " "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp125.c b/gcc/testsuite/gcc.dg/tree-ssa/vrp125.c new file mode 100644 index 000..f6c2f8e35f1 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp125.c @@ -0,0 +1,44 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +/* Should be optimized to a == -100 */ +int g(int a) +{ + if (a == -100 || a == -50 || a >= 0) +; + else +return 0; + return a < -50; +} + +/* Should optimize to a == 0 */ +int f(int a) +{ + if (a == 0 || a == 50 || a > 100) +; + else +return 0; + return a < 50; +} + +/* Should be optimized to a == 0. */ +int f2(int a) +{ + if (a == 0 || a == 50 || a > 100) +; + else +return 0; + return a < 25; +} + +/* Should optimize to a == 100 */ +int f1(int a) +{ + if (a < 0 || a == 50 || a == 100) +; + else +return 0; + return a > 50; +} + +/* { dg-final { scan-tree-dump-not "goto " "optimized" } } */ diff --git a/gcc/vr-values.cc b/gcc/vr-values.cc index a4fddd62841..7004b0224bd 100644 --- a/gcc/vr-values.cc +++ b/gcc/vr-values.cc @@ -907,66 +907,30 @@ simplify_using_ranges::simplify_bit_ops_using_ranges a known value range VR. If there is one and only one value which will satisfy the - conditional, then return that value. Else return NULL. - - If signed overflow must be undefined for the value to satisfy - the conditional, then set *STRICT_OVERFLOW_P to true. */ + conditional on the EDGE, then return that value. + Else return NULL. */ static tree test_for_singularity (enum tree_code cond_code, tree op0, - tree op1, const value_range *vr) + tree op1, Value_Range vr, bool edge) { - tree min = NULL; - tree max = NULL; - - /* Extract minimum/maximum values which satisfy the conditional as it was - written. */ - if (cond_code == LE_EXPR || cond_code == LT_EXPR) + /* This is already a singularity. */ + if (cond_code == NE_EXPR || cond_code == EQ_EXPR) +return NULL; + auto range_op = range_op_handler (cond_code); + int_range<2> op1_range (TREE_TYPE (op0)); + wide_int w = wi::to_wide (op1); + op1_range.set (TREE_TYPE (op1), w, w); + Value_Range vr1(TREE_TYPE (op0)); + if (range_op.op1_range (vr1, TREE_TYPE (op0), + edge ? range_true () : range_false (), + op1_range)) { - min = TYPE_MIN_VALUE (TREE_TYPE (op0)); - - max = op1; - if (cond_code == LT_EXPR) - { - tree one = build_int_cst (TREE_TYPE (op0), 1); - max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one); - /* Signal to compare_values_warnv this expr doesn't overflow. */ - if (EXPR_P (max)) - suppress_warning (max, OPT_Woverflow); - } -} - else if (cond_code == GE_EXPR || cond_code == GT_EXPR) -{ - max = TYPE_MAX_VALUE (TREE_TYPE (op0)); - - min = op1; - if (cond_code == GT_EXPR) - { - tree one = build_int_cst (TREE_TYPE (op0), 1); - min = fold_build2 (PLUS_EXPR,