On 5/25/2021 10:16 AM, Aldy Hernandez via Gcc-patches wrote:
The interface is now an inline function for get_range_query() and an
external function for get_global_range_query), as discussed.
There are no magic cfun uses behind the scenes.
The annoying downcast is gone.
Passes can now decide if they want to export global ranges after they
use a ranger.
I've adjusted the ChangeLog entries, as well as the commit text.
I've addressed everything discussed.
OK pending tests?
Aldy
0001-Common-API-for-accessing-global-and-on-demand-ranges.patch
From eeb7627ddf686d5affb08dcad3674b560ef3ce6d Mon Sep 17 00:00:00 2001
From: Aldy Hernandez <al...@redhat.com>
Date: Wed, 19 May 2021 18:27:05 +0200
Subject: [PATCH 1/5] Common API for accessing global and on-demand ranges.
This patch provides a generic API for accessing global ranges. It is
meant to replace get_range_info() and get_ptr_nonnull() with one
common interface. It uses the same API as the ranger (class
range_query), so there will now be one API for accessing local and
global ranges alike.
Follow-up patches will convert all users of get_range_info and
get_ptr_nonnull to this API.
For get_range_info, instead of:
if (!POINTER_TYPE_P (TREE_TYPE (name)) && SSA_NAME_RANGE_INFO (name))
get_range_info (name, vr);
You can now do:
get_range_query (cfun)->range_of_expr (vr, name, [stmt]);
...as well as any other of the range_query methods (range_on_edge,
range_of_stmt, value_of_expr, value_on_edge, value_on_stmt, etc).
As per the API, range_of_expr will work on constants, SSA names, and
anything we support in irange::supports_type_p().
For pointers, the interface is the same, so instead of:
else if (POINTER_TYPE_P (TREE_TYPE (name)) && SSA_NAME_PTR_INFO (name))
{
if (get_ptr_nonnull (name))
stuff();
}
One can do:
get_range_query (cfun)->range_of_expr (vr, name, [stmt]);
if (vr.nonzero_p ())
stuff ();
Along with this interface, we are providing a mechanism by which a
pass can use an on-demand ranger transparently, without having to
change its code. Of course, this assumes all get_range_info() and
get_ptr_nonnull() users have been converted to the new API, which
follow-up patches will do.
If a pass would prefer to use an on-demand ranger with finer grained
and context aware ranges, all it would have to do is call
enable_ranger() at the beginning of the pass, and disable_ranger() at
the end of the pass.
Note, that to use context aware ranges, any user of range_of_expr()
would need to pass additional context. For example, the optional
gimple statement (or perhaps use range_on_edge or range_of_stmt).
The observant reader will note that get_range_query is tied to a
struct function, which may not be available in certain contexts, such
as at RTL time, gimple-fold, or some other places where we may or may
not have cfun set.
For cases where we are sure there is no function, you can use
get_global_range_query() instead of get_range_query(fun). The API is
the same.
For cases where a function may be called with or without a function,
you could use the following idiom:
range_query *query = cfun ? get_range_query (cfun)
: get_global_range_query ();
query->range_of_expr (range, expr, [stmt]);
The default range query obtained by get_range_query() is the global
range query, unless the user has enabled an on-demand ranger with
enable_ranger(), in which case it will use the currently active ranger.
That is, until disable_ranger() is called, at which point, we revert
back to global ranges.
We think this provides a generic way of accessing ranges, both
globally and locally, without having to keep track of types,
SSA_NAME_RANGE_INFO, and SSA_NAME_PTR_INFO. We also hope this can be
used to transition passes from global to on-demand ranges when
appropriate.
gcc/ChangeLog:
* function.c (allocate_struct_function): Set cfun->x_range_query.
* function.h (struct function): Declare x_range_query.
(get_range_query): New.
(get_global_range_query): New.
* gimple-range-cache.cc (ssa_global_cache::ssa_global_cache):
Remove call to safe_grow_cleared.
* gimple-range.cc (get_range_global): New.
(gimple_range_global): Move from gimple-range.h.
(get_global_range_query): New.
(global_range_query::range_of_expr): New.
(enable_ranger): New.
(disable_ranger): New.
* gimple-range.h (gimple_range_global): Move to gimple-range.cc.
(class global_range_query): New.
(enable_ranger): New.
(disable_ranger): New.
* gimple-ssa-evrp.c (evrp_folder::~evrp_folder): Rename
dump_all_value_ranges to dump.
* tree-vrp.c (vrp_prop::finalize): Same.
* value-query.cc (range_query::dump): New.
* value-query.h (range_query::dump): New.
* vr-values.c (vr_values::dump_all_value_ranges): Rename to...
(vr_values::dump): ...this.
* vr-values.h (class vr_values): Rename dump_all_value_ranges to
dump and make virtual.
OK
jeff