From ea9e0d6c27405d256b4888e9e860e469037c911d Mon Sep 17 00:00:00 2001 From: Martin Sebor Date: Wed, 17 Nov 2021 15:09:23 -0700 Subject: Avoid pathological function redeclarations when checking access sizes [PR102759]. Resolves: PR tree-optimization/102759 - ICE: Segmentation fault in maybe_check_access_sizes since r12-2976-gb48d4e6818674898 gcc/ChangeLog: PR tree-optimization/102759 * gimple-array-bounds.cc (build_printable_array_type): Move... * gimple-ssa-warn-access.cc (build_printable_array_type): Avoid pathological function redeclarations that remove a previously declared prototype. Improve formatting of function arguments in informational notes. * pointer-query.cc (build_printable_array_type): ...to here. * pointer-query.h (build_printable_array_type): Declared. gcc/testsuite/ChangeLog: PR tree-optimization/102759 * gcc.dg/Warray-parameter-10.c: New test. * gcc.dg/Wstringop-overflow-82.c: New test. --- gcc/pointer-query.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'gcc/pointer-query.h') diff --git a/gcc/pointer-query.h b/gcc/pointer-query.h index c8215b6..fbea331 100644 --- a/gcc/pointer-query.h +++ b/gcc/pointer-query.h @@ -275,4 +275,7 @@ extern tree array_elt_at_offset (tree, HOST_WIDE_INT, HOST_WIDE_INT * = nullptr, HOST_WIDE_INT * = nullptr); +/* Helper to build an array type that can be printed. */ +extern tree build_printable_array_type (tree, unsigned HOST_WIDE_INT); + #endif // GCC_POINTER_QUERY_H -- cgit v1.1 From f9379fcb0d482cf4e2efd93b313523ab21fdc7da Mon Sep 17 00:00:00 2001 From: Martin Sebor Date: Sat, 4 Dec 2021 16:46:17 -0700 Subject: Move bndrng from access_ref to access_data. gcc/ChangeLog: * gimple-ssa-warn-access.cc (check_access): Adjust to member name change. (pass_waccess::check_strncmp): Same. * pointer-query.cc (access_ref::access_ref): Remove arguments. Simpilfy. (access_data::access_data): Define new ctors. (access_data::set_bound): Define new member function. (compute_objsize_r): Remove unnecessary code. * pointer-query.h (struct access_ref): Remove ctor arguments. (struct access_data): Declare ctor overloads. (access_data::dst_bndrng): New member. (access_data::src_bndrng): New member. --- gcc/pointer-query.h | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) (limited to 'gcc/pointer-query.h') diff --git a/gcc/pointer-query.h b/gcc/pointer-query.h index fbea331..82cb81b 100644 --- a/gcc/pointer-query.h +++ b/gcc/pointer-query.h @@ -61,8 +61,7 @@ class pointer_query; struct access_ref { /* Set the bounds of the reference. */ - access_ref (range_query *query = nullptr, tree = NULL_TREE, - gimple * = nullptr, bool = false); + access_ref (); /* Return the PHI node REF refers to or null if it doesn't. */ gphi *phi () const; @@ -129,11 +128,6 @@ struct access_ref offset_int sizrng[2]; /* The minimum and maximum offset computed. */ offset_int offmax[2]; - /* Range of the bound of the access: denotes that the access - is at least BNDRNG[0] bytes but no more than BNDRNG[1]. - For string functions the size of the actual access is - further constrained by the length of the string. */ - offset_int bndrng[2]; /* Used to fold integer expressions when called from front ends. */ tree (*eval)(tree); @@ -206,23 +200,18 @@ struct access_data { /* Set the access to at most MAXWRITE and MAXREAD bytes, and at least 1 when MINWRITE or MINREAD, respectively, is set. */ - access_data (range_query *query, gimple *stmt, access_mode mode, - tree maxwrite = NULL_TREE, bool minwrite = false, - tree maxread = NULL_TREE, bool minread = false) - : stmt (stmt), call (), - dst (query, maxwrite, stmt, minwrite), - src (query, maxread, stmt, minread), - mode (mode) { } + access_data (range_query *, gimple *, access_mode, + tree = NULL_TREE, bool = false, + tree = NULL_TREE, bool = false); /* Set the access to at most MAXWRITE and MAXREAD bytes, and at least 1 when MINWRITE or MINREAD, respectively, is set. */ - access_data (range_query *query, tree expr, access_mode mode, - tree maxwrite = NULL_TREE, bool minwrite = false, - tree maxread = NULL_TREE, bool minread = false) - : stmt (), call (expr), - dst (query, maxwrite, nullptr, minwrite), - src (query, maxread, nullptr, minread), - mode (mode) { } + access_data (range_query *, tree, access_mode, + tree = NULL_TREE, bool = false, + tree = NULL_TREE, bool = false); + + /* Constructor helper. */ + static void set_bound (offset_int[2], tree, bool, range_query *, gimple *); /* Access statement. */ gimple *stmt; @@ -230,6 +219,14 @@ struct access_data tree call; /* Destination and source of the access. */ access_ref dst, src; + + /* Range of the bound of the access: denotes that the access is at + least XXX_BNDRNG[0] bytes but no more than XXX_BNDRNG[1]. For + string functions the size of the actual access is further + constrained by the length of the string. */ + offset_int dst_bndrng[2]; + offset_int src_bndrng[2]; + /* Read-only for functions like memcmp or strlen, write-only for memset, read-write for memcpy or strcat. */ access_mode mode; -- cgit v1.1 From 9354a7d70caef1c94b8d0a66bb2fd4d86c037e7d Mon Sep 17 00:00:00 2001 From: Martin Sebor Date: Sat, 4 Dec 2021 16:57:48 -0700 Subject: Pass GIMPLE statement to compute_objsize. gcc/ChangeLog: * gimple-ssa-warn-restrict.c (builtin_access::builtin_access): Pass GIMPLE statement to compute_objsize. * pointer-query.cc (compute_objsize): Add a statement argument. * pointer-query.h (compute_objsize): Define a new overload. --- gcc/pointer-query.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'gcc/pointer-query.h') diff --git a/gcc/pointer-query.h b/gcc/pointer-query.h index 82cb81b..cbc87c8 100644 --- a/gcc/pointer-query.h +++ b/gcc/pointer-query.h @@ -260,8 +260,13 @@ inline tree compute_objsize (tree ptr, int ostype, access_ref *pref) } /* Legacy/transitional API. Should not be used in new code. */ -extern tree compute_objsize (tree, int, tree * = nullptr, tree * = nullptr, - range_query * = nullptr); +extern tree compute_objsize (tree, gimple *, int, tree * = nullptr, + tree * = nullptr, range_query * = nullptr); +inline tree compute_objsize (tree ptr, int ostype, tree *pdecl = nullptr, + tree *poff = nullptr, range_query *rvals = nullptr) +{ + return compute_objsize (ptr, nullptr, ostype, pdecl, poff, rvals); +} /* Return the field at the constant offset. */ extern tree field_at_offset (tree, tree, HOST_WIDE_INT, -- cgit v1.1 From 10d185b9845ad96edf45d55ca066a0258d3c3f9a Mon Sep 17 00:00:00 2001 From: Martin Sebor Date: Mon, 6 Dec 2021 09:23:22 -0700 Subject: Introduce access_ref::merge_ref. gcc/ChangeLog: * pointer-query.cc (access_ref::merge_ref): Define new function. (access_ref::get_ref): Move code into merge_ref and call it. * pointer-query.h (access_ref::merge_ref): Declare new function. --- gcc/pointer-query.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'gcc/pointer-query.h') diff --git a/gcc/pointer-query.h b/gcc/pointer-query.h index cbc87c8..fe46f71 100644 --- a/gcc/pointer-query.h +++ b/gcc/pointer-query.h @@ -66,6 +66,10 @@ struct access_ref /* Return the PHI node REF refers to or null if it doesn't. */ gphi *phi () const; + /* Merge the result for a pointer with *THIS. */ + bool merge_ref (vec *all_refs, tree, gimple *, int, bool, + ssa_name_limit_t &, pointer_query &); + /* Return the object to which REF refers. */ tree get_ref (vec *, access_ref * = nullptr, int = 1, ssa_name_limit_t * = nullptr, pointer_query * = nullptr) const; -- cgit v1.1 From 1334d889d4adbff999fd33d5e1672ecd6bb66dd7 Mon Sep 17 00:00:00 2001 From: Martin Sebor Date: Mon, 6 Dec 2021 09:33:32 -0700 Subject: Refactor compute_objsize_r into helpers. gcc/ChangeLog: * pointer-query.cc (compute_objsize_r): Add an argument. (gimple_call_return_array): Pass a new argument to compute_objsize_r. (access_ref::merge_ref): Same. (access_ref::inform_access): Add an argument and use it. (access_data::access_data): Initialize new member. (handle_min_max_size): Pass a new argument to compute_objsize_r. (handle_decl): New function. (handle_array_ref): Pass a new argument to compute_objsize_r. Avoid incrementing deref. (set_component_ref_size): New function. (handle_component_ref): New function. (handle_mem_ref): Pass a new argument to compute_objsize_r. Only increment deref after successfully computing object size. (handle_ssa_name): New function. (compute_objsize_r): Move code into helpers and call them. (compute_objsize): Pass a new argument to compute_objsize_r. * pointer-query.h (access_ref::inform_access): Add an argument. (access_data::ostype): New member. --- gcc/pointer-query.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'gcc/pointer-query.h') diff --git a/gcc/pointer-query.h b/gcc/pointer-query.h index fe46f71..a7ac7d3 100644 --- a/gcc/pointer-query.h +++ b/gcc/pointer-query.h @@ -122,7 +122,7 @@ struct access_ref /* Issue an informational message describing the target of an access with the given mode. */ - void inform_access (access_mode) const; + void inform_access (access_mode, int = 1) const; /* Reference to the accessed object(s). */ tree ref; @@ -234,6 +234,8 @@ struct access_data /* Read-only for functions like memcmp or strlen, write-only for memset, read-write for memcpy or strcat. */ access_mode mode; + /* The object size type. */ + int ostype; }; enum size_range_flags -- cgit v1.1 From 6dfb1059b31576930427648e03d9d06e6d68bbf0 Mon Sep 17 00:00:00 2001 From: Martin Sebor Date: Mon, 6 Dec 2021 09:52:32 -0700 Subject: Add a new dump function. gcc/ChangeLog: * pointer-query.cc (access_ref::dump): Define new function (pointer_query::dump): Call it. * pointer-query.h (access_ref::dump): Declare new function. --- gcc/pointer-query.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'gcc/pointer-query.h') diff --git a/gcc/pointer-query.h b/gcc/pointer-query.h index a7ac7d3..25101b7 100644 --- a/gcc/pointer-query.h +++ b/gcc/pointer-query.h @@ -124,6 +124,9 @@ struct access_ref with the given mode. */ void inform_access (access_mode, int = 1) const; + /* Dump *THIS to a file. */ + void dump (FILE *) const; + /* Reference to the accessed object(s). */ tree ref; -- cgit v1.1 From 7adcbafe45f8001b698967defe682687b52c0007 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Mon, 3 Jan 2022 10:42:10 +0100 Subject: Update copyright years. --- gcc/pointer-query.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gcc/pointer-query.h') diff --git a/gcc/pointer-query.h b/gcc/pointer-query.h index 25101b7..7dc965b 100644 --- a/gcc/pointer-query.h +++ b/gcc/pointer-query.h @@ -1,6 +1,6 @@ /* Definitions of the pointer_query and related classes. - Copyright (C) 2020-2021 Free Software Foundation, Inc. + Copyright (C) 2020-2022 Free Software Foundation, Inc. This file is part of GCC. -- cgit v1.1 From 58ec0964b1d2f2ab197916cd661728f6a7a1736b Mon Sep 17 00:00:00 2001 From: Martin Sebor Date: Tue, 25 Jan 2022 14:20:51 -0700 Subject: Avoid recomputing PHI results after failure (PR104203). Resolves: PR tree-optimization/104203 - huge compile-time regression in pointer_query gcc/ChangeLog: PR tree-optimization/104203 * gimple-ssa-warn-access.cc (pass_data pass_data_waccess): Use TV_WARN_ACCESS. * pointer-query.cc (access_ref::merge_ref): Change return type. Convert failure to a conservative success. (access_ref::get_ref): Adjust to the change above. Short-circuit PHI evaluation after first failure turned into conservative success. * pointer-query.h (access_ref::merge_ref): Change return type. * timevar.def (TV_WARN_ACCESS): New timer variable. --- gcc/pointer-query.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gcc/pointer-query.h') diff --git a/gcc/pointer-query.h b/gcc/pointer-query.h index 7dc965b..dbdcd59 100644 --- a/gcc/pointer-query.h +++ b/gcc/pointer-query.h @@ -67,7 +67,7 @@ struct access_ref gphi *phi () const; /* Merge the result for a pointer with *THIS. */ - bool merge_ref (vec *all_refs, tree, gimple *, int, bool, + void merge_ref (vec *all_refs, tree, gimple *, int, bool, ssa_name_limit_t &, pointer_query &); /* Return the object to which REF refers. */ -- cgit v1.1 From 68e9b7b69a1d36ff86c54d52fba034737d9433c3 Mon Sep 17 00:00:00 2001 From: Martin Sebor Date: Thu, 3 Feb 2022 13:56:50 -0700 Subject: Make pointer_query cache a private member. gcc/ChangeLog: * gimple-ssa-warn-access.cc (pass_waccess::pass_waccess): Remove pointer_query cache. * pointer-query.cc (pointer_query::pointer_query): Remove cache argument. Zero-initialize new cache member. (pointer_query::get_ref): Replace cache pointer with direct access. (pointer_query::put_ref): Same. (pointer_query::flush_cache): Same. (pointer_query::dump): Same. * pointer-query.h (class pointer_query): Remove cache argument from ctor. Change cache pointer to cache subobject member. * tree-ssa-strlen.cc: Remove pointer_query cache. --- gcc/pointer-query.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'gcc/pointer-query.h') diff --git a/gcc/pointer-query.h b/gcc/pointer-query.h index dbdcd59..4c725ee 100644 --- a/gcc/pointer-query.h +++ b/gcc/pointer-query.h @@ -159,7 +159,6 @@ class pointer_query { DISABLE_COPY_AND_ASSIGN (pointer_query); -public: /* Type of the two-level cache object defined by clients of the class to have pointer SSA_NAMEs cached for speedy access. */ struct cache_type @@ -170,8 +169,9 @@ public: vec access_refs; }; - /* Construct an object with the given Ranger instance and cache. */ - explicit pointer_query (range_query * = nullptr, cache_type * = nullptr); +public: + /* Construct an object with the given Ranger instance. */ + explicit pointer_query (range_query * = nullptr); /* Retrieve the access_ref for a variable from cache if it's there. */ const access_ref* get_ref (tree, int = 1) const; @@ -190,8 +190,6 @@ public: /* A Ranger instance. May be null to use global ranges. */ range_query *rvals; - /* Cache of SSA_NAMEs. May be null to disable caching. */ - cache_type *var_cache; /* Cache performance counters. */ mutable unsigned hits; @@ -199,6 +197,10 @@ public: mutable unsigned failures; mutable unsigned depth; mutable unsigned max_depth; + +private: + /* Cache of SSA_NAMEs. May be null to disable caching. */ + cache_type var_cache; }; /* Describes a pair of references used in an access by built-in -- cgit v1.1 From e78d98f76f8d6ee4d06fc98f7f7c3ea4b86c828c Mon Sep 17 00:00:00 2001 From: Martin Sebor Date: Thu, 3 Feb 2022 19:44:44 -0700 Subject: Use auto_vec for pointer_query cache for auto cleanup. gcc/Changelog: * pointer-query.h (pointer_query::cache_type): Use auto_vec for auto cleanup. --- gcc/pointer-query.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'gcc/pointer-query.h') diff --git a/gcc/pointer-query.h b/gcc/pointer-query.h index 4c725ee..801a240 100644 --- a/gcc/pointer-query.h +++ b/gcc/pointer-query.h @@ -164,9 +164,9 @@ class pointer_query struct cache_type { /* 1-based indices into cache. */ - vec indices; + auto_vec indices; /* The cache itself. */ - vec access_refs; + auto_vec access_refs; }; public: -- cgit v1.1