On 11/9/21 9:58 AM, Jakub Jelinek wrote:
On Tue, Nov 09, 2021 at 09:41:08AM -0500, Andrew MacLeod wrote:
Yeah, Im not particular about how we do this...  I think thats perfectly
reasonable.   Would something like the following solve this issue?
Yes, but see below.

commit 17a5b03c95549b5488bc8dd2af4f6e2cc9ddf098
Author: Andrew MacLeod <amacl...@redhat.com>
Date:   Tue Nov 9 09:29:23 2021 -0500

     Keep x_range_query NULL for global ranges.
Instead of x_range_query alwasy pointing to an object, have it default to
     NULL and return a pointer to the global query in that case.
* function.c (allocate_struct_function): Set x_range_query to NULL.
             * function.h (get_range_query): Return context query or global.
             * gimple-range.cc (enable_ranger): Check current query is NULL.
             (disable_ranger): Clear function current query field.
             * value_query.cc (global_range_query_ptr): New.
             * value-query.h (global_ranges): Remove.

diff --git a/gcc/function.c b/gcc/function.c
index af3d57b32a3..8768c5fcf22 100644
--- a/gcc/function.c
+++ b/gcc/function.c
@@ -4874,7 +4874,7 @@ allocate_struct_function (tree fndecl, bool abstract_p)
    cfun->debug_nonbind_markers = lang_hooks.emits_begin_stmt
      && MAY_HAVE_DEBUG_MARKER_STMTS;
- cfun->x_range_query = &global_ranges;
+  cfun->x_range_query = NULL;
This isn't needed, at the start of function we do
   cfun = ggc_cleared_alloc<function> ();
which already zero initializes the whole structure, including x_range_query.
So instead this can be removed.

--- a/gcc/function.h
+++ b/gcc/function.h
@@ -725,7 +725,9 @@ extern void used_types_insert (tree);
  ATTRIBUTE_RETURNS_NONNULL inline range_query *
  get_range_query (const struct function *fun)
  {
-  return fun->x_range_query;
+  // From value-query.h
+  extern range_query *global_range_query_ptr;
+  return fun->x_range_query ? fun->x_range_query : global_range_query_ptr;
Wouldn't it be better to do:
   extern range_query global_ranges;
   return fun->x_range_query ? fun->x_range_query : &global_ranges;
I think declaring a variable extern can be done with incomplete type
and &var is cheaper than ptr, because for the latter you need to
read the pointer value from memory, while for &var you can just
compute the address of the var which you need to compute for reading
ptr from memory too.

        

yeah, that doesnt work because range_query is a pure virtual. However, there also does not seem to be any reason why we need to jump thru hoops since get_range_query() doesn't need to be in function.h..   If I relocate it to value-query.h like so it seems to work quite well...   How about this?

Andrew


commit 0d5b27e95b7aef4415163e4277de06b48437d6f8
Author: Andrew MacLeod <amacl...@redhat.com>
Date:   Tue Nov 9 09:29:23 2021 -0500

    Keep x_range_query NULL for global ranges.
    
    Instead of x_range_query always pointing to an object, have it default to
    NULL and return a pointer to the global query in that case.
    
            * function.c (allocate_struct_function): Don't set x_range_query.
            * function.h (get_range_query): Move to value-query.h.
            * gimple-range.cc (enable_ranger): Check that query is currently NULL.
            (disable_ranger): Clear function current query field.
            * value_query.cc (get_global_range_query): Relocate to:
            * value-query.h (get_global_range_query): Here and inline.
            (get_range_query): Relocate here from function.h.

diff --git a/gcc/function.c b/gcc/function.c
index af3d57b32a3..61b3bd036b8 100644
--- a/gcc/function.c
+++ b/gcc/function.c
@@ -4873,8 +4873,6 @@ allocate_struct_function (tree fndecl, bool abstract_p)
      binding annotations among them.  */
   cfun->debug_nonbind_markers = lang_hooks.emits_begin_stmt
     && MAY_HAVE_DEBUG_MARKER_STMTS;
-
-  cfun->x_range_query = &global_ranges;
 }
 
 /* This is like allocate_struct_function, but pushes a new cfun for FNDECL
diff --git a/gcc/function.h b/gcc/function.h
index 36003e7576a..899430833ce 100644
--- a/gcc/function.h
+++ b/gcc/function.h
@@ -719,15 +719,4 @@ extern const char *current_function_name (void);
 
 extern void used_types_insert (tree);
 
-/* Returns the currently active range access class.  When there is no active
-   range class, global ranges are used.  Never returns null.  */
-
-ATTRIBUTE_RETURNS_NONNULL inline range_query *
-get_range_query (const struct function *fun)
-{
-  return fun->x_range_query;
-}
-
-extern range_query *get_global_range_query ();
-
 #endif  /* GCC_FUNCTION_H */
diff --git a/gcc/gimple-range.cc b/gcc/gimple-range.cc
index 87dba6e81d8..a2b68b2bc80 100644
--- a/gcc/gimple-range.cc
+++ b/gcc/gimple-range.cc
@@ -467,6 +467,7 @@ enable_ranger (struct function *fun)
 {
   gimple_ranger *r;
 
+  gcc_checking_assert (!fun->x_range_query);
   r = new gimple_ranger;
   fun->x_range_query = r;
 
@@ -479,7 +480,7 @@ enable_ranger (struct function *fun)
 void
 disable_ranger (struct function *fun)
 {
+  gcc_checking_assert (fun->x_range_query);
   delete fun->x_range_query;
-
-  fun->x_range_query = &global_ranges;
+  fun->x_range_query = NULL;
 }
diff --git a/gcc/value-query.cc b/gcc/value-query.cc
index 17ebd86ce5f..b7d9e6653b1 100644
--- a/gcc/value-query.cc
+++ b/gcc/value-query.cc
@@ -435,14 +435,6 @@ gimple_range_global (tree name)
 
 global_range_query global_ranges;
 
-// Like get_range_query, but for accessing global ranges.
-
-range_query *
-get_global_range_query ()
-{
-  return &global_ranges;
-}
-
 bool
 global_range_query::range_of_expr (irange &r, tree expr, gimple *stmt)
 {
diff --git a/gcc/value-query.h b/gcc/value-query.h
index 5161d23714b..323772c8deb 100644
--- a/gcc/value-query.h
+++ b/gcc/value-query.h
@@ -127,6 +127,17 @@ public:
 };
 
 extern global_range_query global_ranges;
+inline range_query *get_global_range_query () { return &global_ranges; }
+
+/* Returns the currently active range access class.  When there is no active
+   range class, global ranges are used.  Never returns null.  */
+
+ATTRIBUTE_RETURNS_NONNULL inline range_query *
+get_range_query (const struct function *fun)
+{
+  return fun->x_range_query ? fun->x_range_query : &global_ranges;
+}
+
 extern value_range gimple_range_global (tree name);
 extern bool update_global_range (irange &r, tree name);
 

Reply via email to