From 564e405c138dc1891ca92462a585bf89444d7dd1 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Wed, 19 Jul 2017 15:10:05 +0200 Subject: [PATCH] re PR tree-optimization/81346 (Missed constant propagation into comparison) PR tree-optimization/81346 * match.pd: Optimize (X - 1U) <= INT_MAX-1U into (int) X > 0. * gcc.dg/tree-ssa/pr81346-5.c: New test. From-SVN: r250342 --- gcc/ChangeLog | 5 +++++ gcc/match.pd | 13 +++++++++++++ gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gcc.dg/tree-ssa/pr81346-5.c | 17 +++++++++++++++++ 4 files changed, 40 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr81346-5.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index bdd1234ac81..c87edc1fcf8 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2017-07-19 Jakub Jelinek + + PR tree-optimization/81346 + * match.pd: Optimize (X - 1U) <= INT_MAX-1U into (int) X > 0. + 2017-07-19 Tom de Vries * config/nvptx/nvptx.md (VECIM): Add V2DI. diff --git a/gcc/match.pd b/gcc/match.pd index fe270934c03..56d13b3c175 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1125,6 +1125,19 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && wi::neg_p (@1, TYPE_SIGN (TREE_TYPE (@1)))) (cmp @2 @0)))))) +/* (X - 1U) <= INT_MAX-1U into (int) X > 0. */ +(for cmp (le gt) + icmp (gt le) + (simplify + (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2) + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) + && TYPE_UNSIGNED (TREE_TYPE (@0)) + && TYPE_PRECISION (TREE_TYPE (@0)) > 1 + && wi::eq_p (@2, wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), + SIGNED) - 1)) + (with { tree stype = signed_type_for (TREE_TYPE (@0)); } + (icmp (convert:stype @0) { build_int_cst (stype, 0); }))))) + /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */ (for cmp (simple_comparison) (simplify diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index e0526a5c49c..a193548b004 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2017-07-19 Jakub Jelinek + + PR tree-optimization/81346 + * gcc.dg/tree-ssa/pr81346-5.c: New test. + 2017-07-19 Tom de Vries * gcc.target/nvptx/decl-init.c: Update alignment. diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr81346-5.c b/gcc/testsuite/gcc.dg/tree-ssa/pr81346-5.c new file mode 100644 index 00000000000..14562a11152 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr81346-5.c @@ -0,0 +1,17 @@ +/* PR tree-optimization/81346 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* { dg-final { scan-tree-dump-times "\\(signed int\\) x" 10 "optimized" } } */ +/* { dg-final { scan-tree-dump-times " <= 0;" 5 "optimized" } } */ +/* { dg-final { scan-tree-dump-times " > 0;" 5 "optimized" } } */ + +int f1 (unsigned x) { return x - 1 <= __INT_MAX__ - 1; } +int f2 (unsigned x) { unsigned a = 1, b = __INT_MAX__ - 1; return x - a <= b; } +int f3 (unsigned x) { return x - 1 < __INT_MAX__; } +int f4 (unsigned x) { unsigned a = 1, b = __INT_MAX__; return x - a < b; } +int f5 (unsigned x) { return x >= 1 && x <= __INT_MAX__; } +int f6 (unsigned x) { return x - 1 > __INT_MAX__ - 1; } +int f7 (unsigned x) { unsigned a = 1, b = __INT_MAX__ - 1; return x - a > b; } +int f8 (unsigned x) { return x - 1 >= __INT_MAX__; } +int f9 (unsigned x) { unsigned a = 1, b = __INT_MAX__; return x - a >= b; } +int f10 (unsigned x) { return x < 1 || x > __INT_MAX__; } -- 2.30.2