Convert strlen pass from evrp to ranger.
authorAldy Hernandez <aldyh@redhat.com>
Fri, 8 Oct 2021 13:54:23 +0000 (15:54 +0200)
committerAldy Hernandez <aldyh@redhat.com>
Tue, 9 Nov 2021 08:55:58 +0000 (09:55 +0100)
commit6b8b959675a3e14cfdd2145bd62e4260eb193765
treeaaeb4a8542b28b6f9362a21c8127e1ecda9498a3
parentcc6b8cd9a21b363815998b11e5cc7529557a9ce5
Convert strlen pass from evrp to ranger.

The following patch converts the strlen pass from evrp to ranger,
leaving DOM as the last remaining user.

No additional cleanups have been done.  For example, the strlen pass
still has uses of VR_ANTI_RANGE, and the sprintf still passes around
pairs of integers instead of using a proper range.  Fixing this
could further improve these passes.

Basically the entire patch is just adjusting the calls to range_of_expr
to include context.  The previous context of si->stmt was mostly
empty, so not really useful ;-).

With ranger we are now able to remove the range calculation from
before_dom_children entirely.  Just working with the ranger on-demand
catches all the strlen and sprintf testcases with the exception of
builtin-sprintf-warn-22.c which is due to a limitation of the sprintf
code.  I have XFAILed the test and documented what the problem is.

On a positive note, these changes found two possible sprintf overflow
bugs in the C++ and Fortran front-ends which I have fixed below.

Tested on x86-64 Linux.

gcc/ChangeLog:

* tree-ssa-strlen.c (compare_nonzero_chars): Pass statement
context to ranger.
(get_addr_stridx): Same.
(get_stridx): Same.
(get_range_strlen_dynamic): Same.
(handle_builtin_strlen): Same.
(handle_builtin_strchr): Same.
(handle_builtin_strcpy): Same.
(maybe_diag_stxncpy_trunc): Same.
(handle_builtin_stxncpy_strncat): Same.
(handle_builtin_memcpy): Same.
(handle_builtin_strcat): Same.
(handle_alloc_call): Same.
(handle_builtin_memset): Same.
(handle_builtin_string_cmp): Same.
(handle_pointer_plus): Same.
(count_nonzero_bytes_addr): Same.
(count_nonzero_bytes): Same.
(handle_store): Same.
(fold_strstr_to_strncmp): Same.
(handle_integral_assign): Same.
(check_and_optimize_stmt): Same.
(class strlen_dom_walker): Replace evrp with ranger.
(strlen_dom_walker::before_dom_children): Remove evrp.
(strlen_dom_walker::after_dom_children): Remove evrp.
* gimple-ssa-warn-access.cc (maybe_check_access_sizes):
Restrict sprintf output.

gcc/cp/ChangeLog:

* ptree.c (cxx_print_xnode): Add more space to pfx array.

gcc/fortran/ChangeLog:

* misc.c (gfc_dummy_typename): Make sure ts->kind is
non-negative.

gcc/testsuite/ChangeLog:

* gcc.dg/tree-ssa/builtin-sprintf-warn-22.c: XFAIL.
gcc/cp/ptree.c
gcc/fortran/misc.c
gcc/gimple-ssa-warn-access.cc
gcc/testsuite/gcc.dg/tree-ssa/builtin-sprintf-warn-22.c
gcc/tree-ssa-strlen.c