On Sat, Aug 5, 2023 at 10:44 PM Andrew Pinski via Gcc-patches <gcc-patches@gcc.gnu.org> wrote: > > Since we already had the infrastructure to optimize > `(x == 0) && (x > y)` to false for integer types, > this extends the same to pointer types as indirectly > requested by PR 96695. > > OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
OK. Thanks, Richard. > gcc/ChangeLog: > > PR tree-optimization/96695 > * match.pd (min_value, max_value): Extend to > pointer types too. > > gcc/testsuite/ChangeLog: > > PR tree-optimization/96695 > * gcc.dg/pr96695-1.c: New test. > * gcc.dg/pr96695-10.c: New test. > * gcc.dg/pr96695-11.c: New test. > * gcc.dg/pr96695-12.c: New test. > * gcc.dg/pr96695-2.c: New test. > * gcc.dg/pr96695-3.c: New test. > * gcc.dg/pr96695-4.c: New test. > * gcc.dg/pr96695-5.c: New test. > * gcc.dg/pr96695-6.c: New test. > * gcc.dg/pr96695-7.c: New test. > * gcc.dg/pr96695-8.c: New test. > * gcc.dg/pr96695-9.c: New test. > --- > gcc/match.pd | 6 ++++-- > gcc/testsuite/gcc.dg/pr96695-1.c | 18 ++++++++++++++++++ > gcc/testsuite/gcc.dg/pr96695-10.c | 20 ++++++++++++++++++++ > gcc/testsuite/gcc.dg/pr96695-11.c | 18 ++++++++++++++++++ > gcc/testsuite/gcc.dg/pr96695-12.c | 18 ++++++++++++++++++ > gcc/testsuite/gcc.dg/pr96695-2.c | 18 ++++++++++++++++++ > gcc/testsuite/gcc.dg/pr96695-3.c | 20 ++++++++++++++++++++ > gcc/testsuite/gcc.dg/pr96695-4.c | 21 +++++++++++++++++++++ > gcc/testsuite/gcc.dg/pr96695-5.c | 19 +++++++++++++++++++ > gcc/testsuite/gcc.dg/pr96695-6.c | 20 ++++++++++++++++++++ > gcc/testsuite/gcc.dg/pr96695-7.c | 19 +++++++++++++++++++ > gcc/testsuite/gcc.dg/pr96695-8.c | 19 +++++++++++++++++++ > gcc/testsuite/gcc.dg/pr96695-9.c | 20 ++++++++++++++++++++ > 13 files changed, 234 insertions(+), 2 deletions(-) > create mode 100644 gcc/testsuite/gcc.dg/pr96695-1.c > create mode 100644 gcc/testsuite/gcc.dg/pr96695-10.c > create mode 100644 gcc/testsuite/gcc.dg/pr96695-11.c > create mode 100644 gcc/testsuite/gcc.dg/pr96695-12.c > create mode 100644 gcc/testsuite/gcc.dg/pr96695-2.c > create mode 100644 gcc/testsuite/gcc.dg/pr96695-3.c > create mode 100644 gcc/testsuite/gcc.dg/pr96695-4.c > create mode 100644 gcc/testsuite/gcc.dg/pr96695-5.c > create mode 100644 gcc/testsuite/gcc.dg/pr96695-6.c > create mode 100644 gcc/testsuite/gcc.dg/pr96695-7.c > create mode 100644 gcc/testsuite/gcc.dg/pr96695-8.c > create mode 100644 gcc/testsuite/gcc.dg/pr96695-9.c > > diff --git a/gcc/match.pd b/gcc/match.pd > index 2278029d608..de54b17abba 100644 > --- a/gcc/match.pd > +++ b/gcc/match.pd > @@ -2733,12 +2733,14 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > > (match min_value > INTEGER_CST > - (if (INTEGRAL_TYPE_P (type) > + (if ((INTEGRAL_TYPE_P (type) > + || POINTER_TYPE_P(type)) > && wi::eq_p (wi::to_wide (t), wi::min_value (type))))) > > (match max_value > INTEGER_CST > - (if (INTEGRAL_TYPE_P (type) > + (if ((INTEGRAL_TYPE_P (type) > + || POINTER_TYPE_P(type)) > && wi::eq_p (wi::to_wide (t), wi::max_value (type))))) > > /* x > y && x != XXX_MIN --> x > y > diff --git a/gcc/testsuite/gcc.dg/pr96695-1.c > b/gcc/testsuite/gcc.dg/pr96695-1.c > new file mode 100644 > index 00000000000..d4287ab4c8c > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/pr96695-1.c > @@ -0,0 +1,18 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-ifcombine" } */ > + > +#include <limits.h> > + > +_Bool and1(unsigned *x, unsigned *y) > +{ > + /* x > y && x != 0 --> x > y */ > + return x > y && x != 0; > +} > + > +_Bool and2(unsigned *x, unsigned *y) > +{ > + /* x < y && x != -1 --> x < y */ > + return x < y && x != (unsigned*)-1; > +} > + > +/* { dg-final { scan-tree-dump-not " != " "ifcombine" } } */ > diff --git a/gcc/testsuite/gcc.dg/pr96695-10.c > b/gcc/testsuite/gcc.dg/pr96695-10.c > new file mode 100644 > index 00000000000..dfe752526f0 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/pr96695-10.c > @@ -0,0 +1,20 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-optimized" } */ > + > +#include <limits.h> > + > +_Bool or1(unsigned *x, unsigned *y) > +{ > + /* x <= y || x != 0 --> true */ > + return x <= y || x != 0; > +} > + > +_Bool or2(unsigned *x, unsigned *y) > +{ > + /* x >= y || x != -1 --> true */ > + return x >= y || x != (unsigned*)-1; > +} > + > +/* { dg-final { scan-tree-dump-not " != " "optimized" } } */ > +/* { dg-final { scan-tree-dump-not " <= " "optimized" } } */ > +/* { dg-final { scan-tree-dump-not " >= " "optimized" } } */ > diff --git a/gcc/testsuite/gcc.dg/pr96695-11.c > b/gcc/testsuite/gcc.dg/pr96695-11.c > new file mode 100644 > index 00000000000..d3c36168b98 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/pr96695-11.c > @@ -0,0 +1,18 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-ifcombine" } */ > + > +#include <limits.h> > + > +_Bool or1(unsigned *x, unsigned *y) > +{ > + /* x <= y || x == 0 --> x <= y */ > + return x <= y || x == 0; > +} > + > +_Bool or2(unsigned *x, unsigned *y) > +{ > + /* x >= y || x == -1 --> x >= y */ > + return x >= y || x == (unsigned*)-1; > +} > + > +/* { dg-final { scan-tree-dump-not " == " "ifcombine" } } */ > diff --git a/gcc/testsuite/gcc.dg/pr96695-12.c > b/gcc/testsuite/gcc.dg/pr96695-12.c > new file mode 100644 > index 00000000000..9ce2ddf8a44 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/pr96695-12.c > @@ -0,0 +1,18 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-dce3" } */ > + > +#include <limits.h> > + > +_Bool or1(unsigned *x, unsigned *y) > +{ > + /* x <= y || x == 0 --> x <= y */ > + return x <= y || x == 0; > +} > + > +_Bool or2(unsigned *x, unsigned *y) > +{ > + /* x >= y || x == -1 --> x >= y */ > + return x >= y || x == (unsigned*)-1; > +} > + > +/* { dg-final { scan-tree-dump-not " == " "dce3" } } */ > diff --git a/gcc/testsuite/gcc.dg/pr96695-2.c > b/gcc/testsuite/gcc.dg/pr96695-2.c > new file mode 100644 > index 00000000000..6d347c3b264 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/pr96695-2.c > @@ -0,0 +1,18 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-optimized" } */ > + > +#include <limits.h> > + > +_Bool and1(unsigned *x, unsigned *y) > +{ > + /* x > y && x != 0 --> x > y */ > + return x > y && x != 0; > +} > + > +_Bool and2(unsigned *x, unsigned *y) > +{ > + /* x < y && x != -1 --> x < y */ > + return x < y && x != (unsigned*)-1; > +} > + > +/* { dg-final { scan-tree-dump-not " != " "optimized" } } */ > diff --git a/gcc/testsuite/gcc.dg/pr96695-3.c > b/gcc/testsuite/gcc.dg/pr96695-3.c > new file mode 100644 > index 00000000000..b205b27dc8a > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/pr96695-3.c > @@ -0,0 +1,20 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-ifcombine" } */ > + > +#include <limits.h> > + > +_Bool and1(unsigned *x, unsigned *y) > +{ > + /* x > y && x == 0 --> false */ > + return x > y && x == 0; > +} > + > +_Bool and2(unsigned *x, unsigned *y) > +{ > + /* x < y && x == -1 --> false */ > + return x < y && x == (unsigned*)-1; > +} > + > +/* { dg-final { scan-tree-dump-not " == " "ifcombine" } } */ > +/* { dg-final { scan-tree-dump-not " > " "ifcombine" } } */ > +/* { dg-final { scan-tree-dump-not " < " "ifcombine" } } */ > diff --git a/gcc/testsuite/gcc.dg/pr96695-4.c > b/gcc/testsuite/gcc.dg/pr96695-4.c > new file mode 100644 > index 00000000000..a5aa8de8dd0 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/pr96695-4.c > @@ -0,0 +1,21 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-optimized" } */ > + > +#include <limits.h> > + > +_Bool and1(unsigned *x, unsigned *y) > +{ > + /* x > y && x == 0 --> false */ > + return x > y && x == 0; > +} > + > +_Bool and2(unsigned *x, unsigned *y) > +{ > + /* x < y && x == -1 --> false */ > + return x < y && x == (unsigned*)-1; > +} > + > + > +/* { dg-final { scan-tree-dump-not " == " "optimized" } } */ > +/* { dg-final { scan-tree-dump-not " > " "optimized" } } */ > +/* { dg-final { scan-tree-dump-not " < " "optimized" } } */ > diff --git a/gcc/testsuite/gcc.dg/pr96695-5.c > b/gcc/testsuite/gcc.dg/pr96695-5.c > new file mode 100644 > index 00000000000..54d8f1a3a7e > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/pr96695-5.c > @@ -0,0 +1,19 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-ifcombine" } */ > + > +#include <limits.h> > + > +_Bool and1(unsigned *x, unsigned *y) > +{ > + /* x <= y && x == 0 --> x == 0 */ > + return x <= y && x == 0; > +} > + > +_Bool and2(unsigned *x, unsigned *y) > +{ > + /* x >= y && x == -1 --> x == -1 */ > + return x >= y && x == (unsigned*)-1; > +} > + > +/* { dg-final { scan-tree-dump-not " <= " "ifcombine" } } */ > +/* { dg-final { scan-tree-dump-not " >= " "ifcombine" } } */ > diff --git a/gcc/testsuite/gcc.dg/pr96695-6.c > b/gcc/testsuite/gcc.dg/pr96695-6.c > new file mode 100644 > index 00000000000..15745246a89 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/pr96695-6.c > @@ -0,0 +1,20 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-optimized" } */ > + > +#include <limits.h> > + > +_Bool and1(unsigned *x, unsigned *y) > +{ > + /* x <= y && x == 0 --> x == 0 */ > + return x <= y && x == 0; > +} > + > +_Bool and2(unsigned *x, unsigned *y) > +{ > + /* x >= y && x == -1 --> x == -1 */ > + return x >= y && x == (unsigned*)-1; > +} > + > + > +/* { dg-final { scan-tree-dump-not " <= " "optimized" } } */ > +/* { dg-final { scan-tree-dump-not " >= " "optimized" } } */ > diff --git a/gcc/testsuite/gcc.dg/pr96695-7.c > b/gcc/testsuite/gcc.dg/pr96695-7.c > new file mode 100644 > index 00000000000..7977ae12f81 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/pr96695-7.c > @@ -0,0 +1,19 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-ifcombine" } */ > + > +#include <limits.h> > + > +_Bool or1(unsigned *x, unsigned *y) > +{ > + /* x > y || x != 0 --> x != 0 */ > + return x > y || x != 0; > +} > + > +_Bool or2(unsigned *x, unsigned *y) > +{ > + /* x < y || x != -1 --> x != -1 */ > + return x < y || x != (unsigned*)-1; > +} > + > +/* { dg-final { scan-tree-dump-not " > " "ifcombine" } } */ > +/* { dg-final { scan-tree-dump-not " < " "ifcombine" } } */ > diff --git a/gcc/testsuite/gcc.dg/pr96695-8.c > b/gcc/testsuite/gcc.dg/pr96695-8.c > new file mode 100644 > index 00000000000..ecda865f79f > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/pr96695-8.c > @@ -0,0 +1,19 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-optimized" } */ > + > +#include <limits.h> > + > +_Bool or1(unsigned *x, unsigned *y) > +{ > + /* x > y || x != 0 --> x != 0 */ > + return x > y || x != 0; > +} > + > +_Bool or2(unsigned *x, unsigned *y) > +{ > + /* x < y || x != -1 --> x != -1 */ > + return x < y || x != (unsigned*)-1; > +} > + > +/* { dg-final { scan-tree-dump-not " > " "optimized" } } */ > +/* { dg-final { scan-tree-dump-not " < " "optimized" } } */ > diff --git a/gcc/testsuite/gcc.dg/pr96695-9.c > b/gcc/testsuite/gcc.dg/pr96695-9.c > new file mode 100644 > index 00000000000..b87522c4635 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/pr96695-9.c > @@ -0,0 +1,20 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-ifcombine" } */ > + > +#include <limits.h> > + > +_Bool or1(unsigned *x, unsigned *y) > +{ > + /* x <= y || x != 0 --> true */ > + return x <= y || x != 0; > +} > + > +_Bool or2(unsigned *x, unsigned *y) > +{ > + /* x >= y || x != -1 --> true */ > + return x >= y || x != (unsigned*)-1; > +} > + > +/* { dg-final { scan-tree-dump-not " != " "ifcombine" } } */ > +/* { dg-final { scan-tree-dump-not " <= " "ifcombine" } } */ > +/* { dg-final { scan-tree-dump-not " >= " "ifcombine" } } */ > -- > 2.31.1 >