public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] PR tree-optimization/108520 - Add function context for querying global ranges.
@ 2023-02-10  0:01 Andrew MacLeod
  2023-02-10  7:45 ` Richard Biener
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew MacLeod @ 2023-02-10  0:01 UTC (permalink / raw)
  To: gcc-patches; +Cc: hernandez, aldy, Jakub Jelinek

[-- Attachment #1: Type: text/plain, Size: 835 bytes --]

I was about to ping on this, and then found it in my drafts.. Doh!


get_range_global() can invoke tree.cc::nonnull_arg_p() if the item being 
queried is a pointer and a parameter.  This routine assumes the context 
is CFUN, and this is not always true.

This patch simply adds a function context to the get_range_global query, 
and defaults it to cfun. If the context passed in is anything different 
than cfun, then it simply chooses not to invoke nonnull_arg_p().

The check_assume function now directly calls gimple_range_global with 
the correct function context instead of indirectly calling it through 
the global_range_query->range_of_expr () method. Thats all it should 
have been doing in the first place really since its always an ssa name.

Bootstraps on x86_64-pc-linux-gnu with no regressions.  OK for trunk?

Andrew



[-- Attachment #2: 0001-Add-function-context-for-querying-global-ranges.patch --]
[-- Type: text/x-patch, Size: 4210 bytes --]

From e64f04f15a47ef91a21668318728a49a90e8dae1 Mon Sep 17 00:00:00 2001
From: Andrew MacLeod <amacleod@redhat.com>
Date: Mon, 6 Feb 2023 13:07:01 -0500
Subject: [PATCH] Add function context for querying global ranges.

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       |  2 +-
 gcc/testsuite/g++.dg/pr108520.C | 17 +++++++++++++++++
 gcc/value-query.cc              | 10 +++++-----
 gcc/value-query.h               |  4 +++-
 4 files changed, 26 insertions(+), 7 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/pr108520.C

diff --git a/gcc/gimple-range-infer.cc b/gcc/gimple-range-infer.cc
index 4677ae21ccc..c765e072731 100644
--- a/gcc/gimple-range-infer.cc
+++ b/gcc/gimple-range-infer.cc
@@ -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
index 00000000000..6cd677a852d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/pr108520.C
@@ -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 ();
+    }
+}
diff --git a/gcc/value-query.cc b/gcc/value-query.cc
index 5345bb47718..f936e878080 100644
--- a/gcc/value-query.cc
+++ b/gcc/value-query.cc
@@ -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);
diff --git a/gcc/value-query.h b/gcc/value-query.h
index 03be0be9178..63878968118 100644
--- a/gcc/value-query.h
+++ b/gcc/value-query.h
@@ -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
-- 
2.39.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] PR tree-optimization/108520 - Add function context for querying global ranges.
  2023-02-10  0:01 [PATCH] PR tree-optimization/108520 - Add function context for querying global ranges Andrew MacLeod
@ 2023-02-10  7:45 ` Richard Biener
  2023-02-10 14:43   ` Andrew MacLeod
  0 siblings, 1 reply; 3+ messages in thread
From: Richard Biener @ 2023-02-10  7:45 UTC (permalink / raw)
  To: Andrew MacLeod; +Cc: gcc-patches, hernandez, aldy, Jakub Jelinek

On Fri, Feb 10, 2023 at 1:02 AM Andrew MacLeod via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> I was about to ping on this, and then found it in my drafts.. Doh!
>
>
> get_range_global() can invoke tree.cc::nonnull_arg_p() if the item being
> queried is a pointer and a parameter.  This routine assumes the context
> is CFUN, and this is not always true.

Can you share the backtrace where the context is different and cfun not NULL?
I'm curious ..

> This patch simply adds a function context to the get_range_global query,
> and defaults it to cfun. If the context passed in is anything different
> than cfun, then it simply chooses not to invoke nonnull_arg_p().
>
> The check_assume function now directly calls gimple_range_global with
> the correct function context instead of indirectly calling it through
> the global_range_query->range_of_expr () method. Thats all it should
> have been doing in the first place really since its always an ssa name.
>
> Bootstraps on x86_64-pc-linux-gnu with no regressions.  OK for trunk?

OK.

Note that in the end nonnull_arg_p should get a struct function argument
as well, and its flag_delete_null_pointer_checks then need to become
opt_for_fn (fn->decl, flag_delete_null_pointer_checks).

I'll also note that for functions with many arguments nonnull_arg_p is
quite expensive and nonnull_arg_p on a PARM_DECL default-def
should be reflected on its range by a pass and we shouldn't re-query
this in any on-demand called function.

Richard.

> Andrew
>
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] PR tree-optimization/108520 - Add function context for querying global ranges.
  2023-02-10  7:45 ` Richard Biener
@ 2023-02-10 14:43   ` Andrew MacLeod
  0 siblings, 0 replies; 3+ messages in thread
From: Andrew MacLeod @ 2023-02-10 14:43 UTC (permalink / raw)
  To: Richard Biener; +Cc: gcc-patches, hernandez, aldy, Jakub Jelinek


On 2/10/23 02:45, Richard Biener wrote:
> On Fri, Feb 10, 2023 at 1:02 AM Andrew MacLeod via Gcc-patches
> <gcc-patches@gcc.gnu.org> wrote:
>> I was about to ping on this, and then found it in my drafts.. Doh!
>>
>>
>> get_range_global() can invoke tree.cc::nonnull_arg_p() if the item being
>> queried is a pointer and a parameter.  This routine assumes the context
>> is CFUN, and this is not always true.
> Can you share the backtrace where the context is different and cfun not NULL?
> I'm curious ..


Its from the assume pass when we are trying to map the actual p[aramater 
passed to a function with the global value we assigned to the parameter 
in that function.ie

    // determine x has a global range of [10, 200] in this function.
assume_func (int x) { if (x>=10 || x<=200) return 1; else return 0; }

myfunc() {
   int y_3
   assume_func (y_3)   //  Here, we lookup assume_fund parameter list, 
and assign [10,200]
                      //  to the range of y_3 via the inferred range 
mechanism

}

The traceback is:

0x1e77e95 nonnull_arg_p(tree_node const*)
     /gcc/master/gcc/gcc/tree.cc:14438
0x1e96241 get_range_global
     /gcc/master/gcc/gcc/value-query.cc:330
0x1e96668 global_range_query::range_of_expr(vrange&, tree_node*, gimple*)
     /gcc/master/gcc/gcc/value-query.cc:406
0x32483c4 gimple_infer_range::check_assume_func(gcall*)
     /gcc/master/gcc/gcc/gimple-range-infer.cc:87
0x32488e8 gimple_infer_range::gimple_infer_range(gimple*)
     /gcc/master/gcc/gcc/gimple-range-infer.cc:166
0x3249635 infer_range_manager::register_all_uses(tree_node*)
     /gcc/master/gcc/gcc/gimple-range-infer.cc:374
0x3248e94 infer_range_manager::has_range_p(tree_node*, basic_block_def*)
     /gcc/master/gcc/gcc/gimple-range-infer.cc:273
0x3248f7e infer_range_manager::maybe_adjust_range(vrange&, tree_node*, 
basic_block_def*)
     /gcc/master/gcc/gcc/gimple-range-infer.cc:290
0x1c8b6e1 path_range_query::adjust_for_non_null_uses(basic_block_def*)
     /gcc/master/gcc/gcc/gimple-range-path.cc:501
0x1c8bf23 path_range_query::compute_ranges(bitmap_head const*)
     /gcc/master/gcc/gcc/gimple-range-path.cc:622
0x1c8a40a path_range_query::reset_path(vec<basic_block_def*, va_heap, 
vl_ptr> const&, bitmap_head const*)
     /gcc/master/gcc/gcc/gimple-range-path.cc:229
0x1c89af0 path_range_query::path_range_query(gimple_ranger&, 
vec<basic_block_def*, va_heap, vl_ptr> const&, bitmap_head const*, bool)
     /gcc/master/gcc/gcc/gimple-range-path.cc:50
0x1d52a96 back_threader::find_taken_edge_cond(vec<basic_block_def*, 
va_heap, vl_ptr> const&, gcond*)
     /gcc/master/gcc/gcc/tree-ssa-threadbackward.cc:324

>> This patch simply adds a function context to the get_range_global query,
>> and defaults it to cfun. If the context passed in is anything different
>> than cfun, then it simply chooses not to invoke nonnull_arg_p().
>>
>> The check_assume function now directly calls gimple_range_global with
>> the correct function context instead of indirectly calling it through
>> the global_range_query->range_of_expr () method. Thats all it should
>> have been doing in the first place really since its always an ssa name.
>>
>> Bootstraps on x86_64-pc-linux-gnu with no regressions.  OK for trunk?
> OK.
>
> Note that in the end nonnull_arg_p should get a struct function argument
> as well, and its flag_delete_null_pointer_checks then need to become
> opt_for_fn (fn->decl, flag_delete_null_pointer_checks).
>
> I'll also note that for functions with many arguments nonnull_arg_p is
> quite expensive and nonnull_arg_p on a PARM_DECL default-def
> should be reflected on its range by a pass and we shouldn't re-query
> this in any on-demand called function.

At the moment, Its only being queried thjis way as we try to resolve 
these assume_functions.  IF we enhance this to utilize IPA information 
next release in a similar way, it'll become more prevalent



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-02-10 14:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-10  0:01 [PATCH] PR tree-optimization/108520 - Add function context for querying global ranges Andrew MacLeod
2023-02-10  7:45 ` Richard Biener
2023-02-10 14:43   ` Andrew MacLeod

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).