From 4864297f7858617a5fe406d3651a46446b41db7a Mon Sep 17 00:00:00 2001 From: Richard Biener Date: Mon, 20 Aug 2018 14:01:05 +0000 Subject: [PATCH] re PR c++/78655 (gcc doesn't exploit the fact that the result of pointer addition can not be nullptr) 2018-08-20 Richard Biener PR tree-optimization/78655 * tree-vrp.c (extract_range_from_binary_expr_1): Make pointer + offset nonnull if either operand is nonnull work. * gcc.dg/tree-ssa/evrp11.c: New testcase. From-SVN: r263662 --- gcc/ChangeLog | 6 ++++++ gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gcc.dg/tree-ssa/evrp11.c | 23 +++++++++++++++++++++++ gcc/tree-vrp.c | 7 ++++++- 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/evrp11.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 3dbcd1c82db..6cf673c7382 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2018-08-20 Richard Biener + + PR tree-optimization/78655 + * tree-vrp.c (extract_range_from_binary_expr_1): Make + pointer + offset nonnull if either operand is nonnull work. + 2018-08-20 Tom de Vries * dwarf2out.c (add_scalar_info): Don't add reference to existing die diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index e89f4a4c093..1eaec9008da 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2018-08-20 Richard Biener + + PR tree-optimization/78655 + * gcc.dg/tree-ssa/evrp11.c: New testcase. + 2018-08-18 Iain Sandoe * gcc.dg/debug/dwarf2/pr80263.c: Suppress pubtypes output diff --git a/gcc/testsuite/gcc.dg/tree-ssa/evrp11.c b/gcc/testsuite/gcc.dg/tree-ssa/evrp11.c new file mode 100644 index 00000000000..f1373bd8683 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/evrp11.c @@ -0,0 +1,23 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-evrp" } */ + +extern void link_error (); + +void foo (int *x) +{ + int *p = x + 1; + if (p == 0) + link_error (); +} + +void bar (char *x, int a) +{ + if (a != 0) + { + char *p = x + a; + if (p == 0) + link_error (); + } +} + +/* { dg-final { scan-tree-dump-not "link_error" "evrp" } } */ diff --git a/gcc/tree-vrp.c b/gcc/tree-vrp.c index d553a254878..2ddb0c2c197 100644 --- a/gcc/tree-vrp.c +++ b/gcc/tree-vrp.c @@ -1437,6 +1437,7 @@ extract_range_from_binary_expr_1 (value_range *vr, && code != PLUS_EXPR && code != MINUS_EXPR && code != RSHIFT_EXPR + && code != POINTER_PLUS_EXPR && (vr0.type == VR_VARYING || vr1.type == VR_VARYING || vr0.type != vr1.type @@ -1467,7 +1468,11 @@ extract_range_from_binary_expr_1 (value_range *vr, { /* For pointer types, we are really only interested in asserting whether the expression evaluates to non-NULL. */ - if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1)) + if (range_is_nonnull (&vr0) + || range_is_nonnull (&vr1) + || (vr1.type == VR_RANGE + && !symbolic_range_p (&vr1) + && !range_includes_zero_p (vr1.min, vr1.max))) set_value_range_to_nonnull (vr, expr_type); else if (range_is_null (&vr0) && range_is_null (&vr1)) set_value_range_to_null (vr, expr_type); -- 2.30.2