]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Add function context for querying global ranges.
authorAndrew MacLeod <amacleod@redhat.com>
Mon, 6 Feb 2023 18:07:01 +0000 (13:07 -0500)
committerAndrew MacLeod <amacleod@redhat.com>
Fri, 10 Feb 2023 14:46:33 +0000 (09:46 -0500)
When processing arguments for assume functions, call get_global_range
directly and utilize a function context pointer to avoid any assumptions
about using cfun.

PR tree-optimization/108520
gcc/
* gimple-range-infer.cc (check_assume_func): Invoke
gimple_range_global directly instead using global_range_query.
* value-query.cc (get_range_global): Add function context and
avoid calling nonnull_arg_p if not cfun.
(gimple_range_global): Add function context pointer.
* value-query.h (imple_range_global): Add function context.

gcc/testsuite/
* g++.dg/pr108520.C: New.

gcc/gimple-range-infer.cc
gcc/testsuite/g++.dg/pr108520.C [new file with mode: 0644]
gcc/value-query.cc
gcc/value-query.h

index 4677ae21ccced21c05b4ffde82dd9bc81e25ad45..c765e07273187c5240c41508752d320c0877cc62 100644 (file)
@@ -84,7 +84,7 @@ gimple_infer_range::check_assume_func (gcall *call)
            continue;
          // Query the global range of the default def in the assume function.
          Value_Range assume_range (type);
-         global_ranges.range_of_expr (assume_range, default_def);
+         gimple_range_global (assume_range, default_def, fun);
          // If there is a non-varying result, add it as an inferred range.
          if (!assume_range.varying_p ())
            {
diff --git a/gcc/testsuite/g++.dg/pr108520.C b/gcc/testsuite/g++.dg/pr108520.C
new file mode 100644 (file)
index 0000000..6cd677a
--- /dev/null
@@ -0,0 +1,17 @@
+// { dg-do compile { target c++23 } }
+// { dg-options "-O2" }
+
+static void foo () {}
+struct S { void (*f) (); };
+
+[[gnu::nonnull (1)]]
+void
+bar (void *x)
+{
+  struct S a[3] = { { foo }, { foo }, { foo } };
+  for (struct S *i = a, *e = a + 3; i != e; i++)
+    {
+      [[assume (i->f)]];
+      i->f ();
+    }
+}
index 5345bb4771857ab2ba637e2b1dee68220a493fbf..f936e878080c38c6b4e124dc085a3d1db29a7545 100644 (file)
@@ -312,7 +312,7 @@ get_ssa_name_ptr_info_nonnull (const_tree name)
 // return VARYING.
 
 static void
-get_range_global (vrange &r, tree name)
+get_range_global (vrange &r, tree name, struct function *fun = cfun)
 {
   tree type = TREE_TYPE (name);
 
@@ -327,7 +327,7 @@ get_range_global (vrange &r, tree name)
          // anti-ranges for pointers.  Note that this is only valid with
          // default definitions of PARM_DECLs.
          if (POINTER_TYPE_P (type)
-             && ((cfun && nonnull_arg_p (sym))
+             && ((cfun && fun == cfun && nonnull_arg_p (sym))
                  || get_ssa_name_ptr_info_nonnull (name)))
            r.set_nonzero (type);
          else if (!POINTER_TYPE_P (type))
@@ -378,15 +378,15 @@ get_range_global (vrange &r, tree name)
 // https://gcc.gnu.org/pipermail/gcc-patches/2021-June/571709.html
 
 void
-gimple_range_global (vrange &r, tree name)
+gimple_range_global (vrange &r, tree name, struct function *fun)
 {
   tree type = TREE_TYPE (name);
   gcc_checking_assert (TREE_CODE (name) == SSA_NAME);
 
-  if (SSA_NAME_IS_DEFAULT_DEF (name) || (cfun && cfun->after_inlining)
+  if (SSA_NAME_IS_DEFAULT_DEF (name) || (fun && fun->after_inlining)
       || is_a<gphi *> (SSA_NAME_DEF_STMT (name)))
     {
-      get_range_global (r, name);
+      get_range_global (r, name, fun);
       return;
     }
   r.set_varying (type);
index 03be0be9178e1386f5d54b0b7ba9490aae369ea3..63878968118cc17e89dd349b24381e52b67801fc 100644 (file)
@@ -143,6 +143,8 @@ get_range_query (const struct function *fun)
   return fun->x_range_query ? fun->x_range_query : &global_ranges;
 }
 
-extern void gimple_range_global (vrange &v, tree name);
+// Query the global range of NAME in function F.  Default to cfun.
+extern void gimple_range_global (vrange &v, tree name,
+                                struct function *f = cfun);
 
 #endif // GCC_QUERY_H