Currently a load from a constant table gets the range of the whole
table, no matter what is known of its index. For example, with
static const int tbl[4] = { 0, 5, 6, 7 };
and i in [0, 1], tbl[i] is currently [0, 0][5, 7]. With this patch we
get [0, 0][5, 5].
This work comes from my DOM parity obsession, but goes beyond what DOM
gets here. DOM folds a load from a constant table only when the index
can be determined to be a constant, as on the path through i = 1 in:
if (c)
i = 1;
if (tbl[i] == 5)
With this patch we also narrow a load whose index is in a range:
if (i < 2 && tbl[i] > 5)
tbl[i] is [0, 0][5, 5], and we can fold the comparison away.
This implements the following TODO in the code:
/* TODO: If the array index in the expr is an SSA_NAME with a known
range, we could use just values loaded from the corresponding array
elements. */
Oh, and we no longer have to special case INTEGER_CSTs, as ranger
happily handles those.
I've also added a testcase for r17-4029-g7dab38c9d7104e, a phiopt
tweak which couldn't be properly tested until now.
Tested on ppc64le Linux.
gcc/ChangeLog:
* gimple-range-fold.cc (intersects_p): New.
(class ctor_ref_range): Add SRC.
(ctor_ref_range::accumulate_range_from_array_ref): Load only from
the elements the range of the index reaches, a constant index
included.
(fold_using_range::fold_stmt): Pass the source to
range_from_readonly_var.
(fold_using_range::range_from_readonly_var): Add SRC.
* gimple-range-fold.h (fold_using_range::range_from_readonly_var):
Adjust.
gcc/testsuite/ChangeLog:
* gcc.dg/tree-ssa/phi-opt-52.c: New test.
* gcc.dg/tree-ssa/vrp-from-cst-agg-10.c: New test.
* gcc.dg/tree-ssa/vrp-from-cst-agg-11.c: New test.
* gcc.dg/tree-ssa/vrp-from-cst-agg-13.c: New test.
This directory contains the GNU Compiler Collection (GCC).
The GNU Compiler Collection is free software. See the files whose
names start with COPYING for copying permission. The manuals, and
some of the runtime libraries, are under different terms; see the
individual source files for details.
The directory INSTALL contains copies of the installation information
as HTML and plain text. The source of this information is
gcc/doc/install.texi. The installation information includes details
of what is included in the GCC sources and what files GCC installs.
See the file gcc/doc/gcc.texi (together with other files that it
includes) for usage and porting information. An online readable
version of the manual is in the files gcc/doc/gcc.info*.
See http://gcc.gnu.org/bugs/ for how to report bugs usefully.
Copyright years on GCC source files may be listed using range
notation, e.g., 1987-2012, indicating that every year in the range,
inclusive, is a copyrightable year that could otherwise be listed
individually.