Hi! On Fri, Nov 22, 2024 at 07:25:23PM -0500, Andrew MacLeod wrote: > I will shortly be submitting , and presumable committing, this patch as > part of a series to improve VRP time for 117467.. > > So it may be in place by the time you need it
> On 11/18/24 09:31, Andrew MacLeod wrote: > > Attached is a pre-approved patch which adds a range_query to the > > inferred range mechanism. > > > > The only change you will need to make is to replace "get_range_query > > (cfun)->" with "q->" which is passed in. > > > > This regstraps on x86 without your patch, and I got as far as a > > bootstrap with your patches.. Sorry for the delay. Only recently the remaining patches have been committed, so I'm getting back to this. The current state of the trunk is that it handles constant arg2 (if it is zero or non-zero) and punts on everything else. I've changed get_range_query (cfun)-> to q-> but unfortunately the test FAILs with it, while it improves the if (n >= 42) case where there is a SSA_NAME for n - 10, it doesn't improve the if (n) case, so it feels like it is querying just the global range rather than the local one from the statement, because on the foo (b, n); statement which is guarded by if (n) n should be provably [1, SIZE_MAX]. The patch still bootstraps/regtests on x86_64-linux and i686-linux and improves at least something, so I guess I could just comment out part of the testcase. Any thoughts why this happens though? And is that something that can be improved for GCC 15 or should wait for GCC 16? 2025-03-04 Jakub Jelinek <ja...@redhat.com> Andrew MacLeod <amacl...@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. --- gcc/gimple-range-infer.cc.jj 2025-01-02 11:23:24.000000000 +0100 +++ gcc/gimple-range-infer.cc 2025-03-03 12:37:52.836895208 +0100 @@ -208,8 +208,13 @@ gimple_infer_range::gimple_infer_range ( 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. --- gcc/testsuite/gcc.dg/tree-ssa/pr78154-2.c.jj 2025-03-03 12:38:37.841273517 +0100 +++ gcc/testsuite/gcc.dg/tree-ssa/pr78154-2.c 2025-03-03 12:38:37.841273517 +0100 @@ -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" } } */ Jakub