diff options
author | Jakub Jelinek <jakub@redhat.com> | 2025-05-02 19:40:55 +0200 |
---|---|---|
committer | Jakub Jelinek <jakub@gcc.gnu.org> | 2025-05-02 19:40:55 +0200 |
commit | 1e27e9a3184c948b499a21ff20181611514ed720 (patch) | |
tree | 7ed6e477c0ccdebb28414bdf8cfe796c9afd2a62 | |
parent | c77d04506e6abdc45969d0ff146204be7485244a (diff) | |
download | gcc-1e27e9a3184c948b499a21ff20181611514ed720.zip gcc-1e27e9a3184c948b499a21ff20181611514ed720.tar.gz gcc-1e27e9a3184c948b499a21ff20181611514ed720.tar.bz2 |
ranger: Improve nonnull_if_nonzero attribute [PR117023]
On Mon, Mar 31, 2025 at 11:30:20AM -0400, Andrew MacLeod wrote:
> Infer range processing was adjusted to allow a query to be specified,
> but during VRP folding, ranger w3as not providing a query. This results
> in contextual ranges being missed. Pass the cache in as the query
> which provide a read-only query of the current state.
Now that this patch is in, I've retested my patch and it works fine.
If we can determine a range for the arg2 argument and prove that it
doesn't include zero, we can imply nonzero for the arg1 argument.
2025-05-02 Jakub Jelinek <jakub@redhat.com>
Andrew MacLeod <amacleod@redhat.com>
PR c/117023
* gimple-range-infer.cc (gimple_infer_range::gimple_infer_range):
For nonnull_if_nonzero attribute check also arg2 range if it doesn't
include zero and in that case call add_nonzero too.
* gcc.dg/tree-ssa/pr78154-2.c: New test.
-rw-r--r-- | gcc/gimple-range-infer.cc | 9 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/tree-ssa/pr78154-2.c | 38 |
2 files changed, 45 insertions, 2 deletions
diff --git a/gcc/gimple-range-infer.cc b/gcc/gimple-range-infer.cc index 3b1abbe..72f71b9 100644 --- a/gcc/gimple-range-infer.cc +++ b/gcc/gimple-range-infer.cc @@ -208,8 +208,13 @@ gimple_infer_range::gimple_infer_range (gimple *s, range_query *q, continue; if (integer_nonzerop (arg2)) add_nonzero (arg); - // FIXME: Can one query here whether arg2 has - // nonzero range if it is a SSA_NAME? + else + { + value_range r (TREE_TYPE (arg2)); + if (q->range_of_expr (r, arg2, s) + && !r.contains_p (build_zero_cst (TREE_TYPE (arg2)))) + add_nonzero (arg); + } } } // Fallthru and walk load/store ops now. diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr78154-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr78154-2.c new file mode 100644 index 0000000..3b2cbd8 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr78154-2.c @@ -0,0 +1,38 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-evrp-slim -fdelete-null-pointer-checks" } */ +/* { dg-skip-if "" { keeps_null_pointer_checks } } */ + +void foo (void *, __SIZE_TYPE__) __attribute__((nonnull_if_nonzero (1, 2))); +void baz (void); + +void +bar (void *a, void *b, void *c, void *d, void *e, __SIZE_TYPE__ n) +{ + foo (a, 42); + if (a == 0) + __builtin_abort (); + if (n) + { + foo (b, n); + if (b == 0) + __builtin_abort (); + } + if (n >= 42) + { + foo (c, n - 10); + if (c == 0) + __builtin_abort (); + } + foo (d, 0); + if (d == 0) + baz (); + if (n != 42) + { + foo (e, n); + if (e == 0) + baz (); + } +} + +/* { dg-final { scan-tree-dump-not "__builtin_abort" "evrp" } } */ +/* { dg-final { scan-tree-dump-times "baz \\\(" 2 "evrp" } } */ |