On Mon, Mar 31, 2025 at 11:30:20AM -0400, Andrew MacLeod wrote:
> From 7116177599a3bb907d21fe642a4bdcb401e1263b Mon Sep 17 00:00:00 2001
> From: Andrew MacLeod <amacl...@redhat.com>
> Date: Mon, 31 Mar 2025 11:18:22 -0400
> Subject: [PATCH 2/2] Use the current cache when creating inferred ranges.
> 
> 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.
> 
>       * gimple-range-cache.cc (ranger_cache::apply_inferred_ranges): Pass
>       'this' as the range-query to the inferred range constructor.
> ---
>  gcc/gimple-range-cache.cc | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/gcc/gimple-range-cache.cc b/gcc/gimple-range-cache.cc
> index 818b801468a..ecf03319cd4 100644
> --- a/gcc/gimple-range-cache.cc
> +++ b/gcc/gimple-range-cache.cc
> @@ -1861,7 +1861,7 @@ ranger_cache::apply_inferred_ranges (gimple *s)
>    bool update = true;
>  
>    basic_block bb = gimple_bb (s);
> -  gimple_infer_range infer(s);
> +  gimple_infer_range infer(s, this);
>    if (infer.num () == 0)
>      return;
>  
> -- 
> 2.45.0
> 

Now that this patch is in, I've retested my patch and it works fine.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2025-05-02  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

Reply via email to