From 58f1e185ff6d108171b9c8ea043796c85a65fcec Mon Sep 17 00:00:00 2001 From: Andrew Pinski Date: Sat, 5 Aug 2023 09:23:26 -0700 Subject: MATCH: Extend min_value/max_value to pointer types 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. 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 2278029..de54b17 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 0000000..d4287ab --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr96695-1.c @@ -0,0 +1,18 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-ifcombine" } */ + +#include + +_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 0000000..dfe7525 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr96695-10.c @@ -0,0 +1,20 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +#include + +_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 0000000..d3c3616 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr96695-11.c @@ -0,0 +1,18 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-ifcombine" } */ + +#include + +_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 0000000..9ce2ddf --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr96695-12.c @@ -0,0 +1,18 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-dce3" } */ + +#include + +_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 0000000..6d347c3 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr96695-2.c @@ -0,0 +1,18 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +#include + +_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 0000000..b205b27 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr96695-3.c @@ -0,0 +1,20 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-ifcombine" } */ + +#include + +_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 0000000..a5aa8de --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr96695-4.c @@ -0,0 +1,21 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +#include + +_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 0000000..54d8f1a --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr96695-5.c @@ -0,0 +1,19 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-ifcombine" } */ + +#include + +_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 0000000..1574524 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr96695-6.c @@ -0,0 +1,20 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +#include + +_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 0000000..7977ae1 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr96695-7.c @@ -0,0 +1,19 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-ifcombine" } */ + +#include + +_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 0000000..ecda865 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr96695-8.c @@ -0,0 +1,19 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +#include + +_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 0000000..b87522c --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr96695-9.c @@ -0,0 +1,20 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-ifcombine" } */ + +#include + +_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" } } */ -- cgit v1.1