From patchwork Wed Feb 2 23:35:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 1587818 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=j6K7CEqX; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4JpyrY3dJkz9sFn for ; Thu, 3 Feb 2022 10:37:17 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 47B7E385803B for ; Wed, 2 Feb 2022 23:37:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 47B7E385803B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1643845035; bh=37RJA3oqpRzX35zu5p+2oHiUkJZZTkj8BC9PmUyU3ug=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=j6K7CEqXo7eJfJoi6VPqjTJL9RMAp15GeiSC6s9Psc6yF7uO41wE3QOl/WOlfgpVx QvmK68958W68+xH+s/t+44KWWXrlaKgdk7eJYw2b8bKhfeSSi2UbsOS3DYjCg1AxBE wucu/LrF3oQh8OxU6/sIu3iDWXeCWZBDM7fLzBAM= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-il1-x12c.google.com (mail-il1-x12c.google.com [IPv6:2607:f8b0:4864:20::12c]) by sourceware.org (Postfix) with ESMTPS id 612B73858C2C for ; Wed, 2 Feb 2022 23:35:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 612B73858C2C Received: by mail-il1-x12c.google.com with SMTP id z4so727231ilz.4 for ; Wed, 02 Feb 2022 15:35:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=37RJA3oqpRzX35zu5p+2oHiUkJZZTkj8BC9PmUyU3ug=; b=aN22XJI5+V+ejz8s4bdGxdQRLw3txnTjYBQoqkjEfQYUhU02np5QIw1BFl+CmKldwu PTq0975v74yue1WAQJmMfI9M6kcYpkuKUTCQFdGmPQ/0EfKx7Cxm1xHR7HWn5aEMct61 w9am/8VeWSIHFfRqQnwjD9uVY7xqZzomCPyGuv0cSxNHz/hUu/gHwgAy9QGnLFAyZSNg ZdlWhyldcoEmZDDj9vnpfFwH22G3tRBwv9fC5wCs+ObIVmbUPiX4hO0D+7ZpVowiQyM5 Np4HoKgqmFotqpj8zekkoyVPvHFY2ZBeX5P3+XOSvPS4yRir2HEWL0TkvtNE+J5cwS8V M49Q== X-Gm-Message-State: AOAM533FM+oh6nDPZ/HeTSsGTd00OXFQ7OVgp3ajYv0bCoP8NFQUVCwu fywzb2bPECMlHdz/SjdEsdVjrgzRCuGeUQ== X-Google-Smtp-Source: ABdhPJzTuTC9soejRydHMufysft3zf7m8As6T5SkqRudBpBzdnQWDxW+NPBz3GUDMDY2BDC0nbXWtg== X-Received: by 2002:a05:6e02:1564:: with SMTP id k4mr19424469ilu.63.1643844937179; Wed, 02 Feb 2022 15:35:37 -0800 (PST) Received: from bigbox.redhat.com (97-118-100-142.hlrn.qwest.net. [97.118.100.142]) by smtp.googlemail.com with ESMTPSA id l1sm7481267iln.29.2022.02.02.15.35.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Feb 2022 15:35:36 -0800 (PST) X-Google-Original-From: Martin Sebor To: gcc-patches@gcc.gnu.org Subject: [PATCH 1/3] Make pointer_query cache a private member. Date: Wed, 2 Feb 2022 16:35:28 -0700 Message-Id: <20220202233530.2568647-2-msebor@redhat.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220202233530.2568647-1-msebor@redhat.com> References: <20220202233530.2568647-1-msebor@redhat.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Martin Sebor via Gcc-patches From: Martin Sebor Reply-To: Martin Sebor Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" The first patch in the series make the pointer_query cache a private member of the class and removes the ability to create an object of it without one, or one with an external cache. It also simplifies existing clients of the class that provide an external cache to avoid doing so. 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/gimple-ssa-warn-access.cc | 8 ++-- gcc/pointer-query.cc | 74 +++++++++++++++-------------------- gcc/pointer-query.h | 12 +++--- gcc/tree-ssa-strlen.cc | 8 ++-- 4 files changed, 44 insertions(+), 58 deletions(-) diff --git a/gcc/gimple-ssa-warn-access.cc b/gcc/gimple-ssa-warn-access.cc index ad5e2f4458e..4b3d2c00b03 100644 --- a/gcc/gimple-ssa-warn-access.cc +++ b/gcc/gimple-ssa-warn-access.cc @@ -2137,10 +2137,9 @@ private: /* Return true if use follows an invalidating statement. */ bool use_after_inval_p (gimple *, gimple *, bool = false); - /* A pointer_query object and its cache to store information about - pointers and their targets in. */ + /* A pointer_query object to store information about pointers and + their targets in. */ pointer_query m_ptr_qry; - pointer_query::cache_type m_var_cache; /* Mapping from DECLs and their clobber statements in the function. */ hash_map m_clobbers; /* A bit is set for each basic block whose statements have been assigned @@ -2158,8 +2157,7 @@ private: pass_waccess::pass_waccess (gcc::context *ctxt) : gimple_opt_pass (pass_data_waccess, ctxt), - m_ptr_qry (NULL, &m_var_cache), - m_var_cache (), + m_ptr_qry (NULL), m_clobbers (), m_bb_uids_set (), m_func (), diff --git a/gcc/pointer-query.cc b/gcc/pointer-query.cc index b092ef4fbdc..afbcd0a15ea 100644 --- a/gcc/pointer-query.cc +++ b/gcc/pointer-query.cc @@ -1433,12 +1433,11 @@ ssa_name_limit_t::~ssa_name_limit_t () } /* Default ctor. Initialize object with pointers to the range_query - and cache_type instances to use or null. */ + instance to use or null. */ -pointer_query::pointer_query (range_query *qry /* = NULL */, - cache_type *cache /* = NULL */) -: rvals (qry), var_cache (cache), hits (), misses (), - failures (), depth (), max_depth () +pointer_query::pointer_query (range_query *qry /* = NULL */) + : rvals (qry), hits (), misses (), failures (), depth (), max_depth (), + var_cache () { /* No op. */ } @@ -1449,28 +1448,22 @@ pointer_query::pointer_query (range_query *qry /* = NULL */, const access_ref * pointer_query::get_ref (tree ptr, int ostype /* = 1 */) const { - if (!var_cache) - { - ++misses; - return NULL; - } - unsigned version = SSA_NAME_VERSION (ptr); unsigned idx = version << 1 | (ostype & 1); - if (var_cache->indices.length () <= idx) + if (var_cache.indices.length () <= idx) { ++misses; return NULL; } - unsigned cache_idx = var_cache->indices[idx]; - if (var_cache->access_refs.length () <= cache_idx) + unsigned cache_idx = var_cache.indices[idx]; + if (var_cache.access_refs.length () <= cache_idx) { ++misses; return NULL; } - access_ref &cache_ref = var_cache->access_refs[cache_idx]; + const access_ref &cache_ref = var_cache.access_refs[cache_idx]; if (cache_ref.ref) { ++hits; @@ -1491,17 +1484,17 @@ pointer_query::get_ref (tree ptr, gimple *stmt, access_ref *pref, const unsigned version = TREE_CODE (ptr) == SSA_NAME ? SSA_NAME_VERSION (ptr) : 0; - if (var_cache && version) + if (version) { unsigned idx = version << 1 | (ostype & 1); - if (idx < var_cache->indices.length ()) + if (idx < var_cache.indices.length ()) { - unsigned cache_idx = var_cache->indices[idx] - 1; - if (cache_idx < var_cache->access_refs.length () - && var_cache->access_refs[cache_idx].ref) + unsigned cache_idx = var_cache.indices[idx] - 1; + if (cache_idx < var_cache.access_refs.length () + && var_cache.access_refs[cache_idx].ref) { ++hits; - *pref = var_cache->access_refs[cache_idx]; + *pref = var_cache.access_refs[cache_idx]; return true; } } @@ -1525,7 +1518,7 @@ void pointer_query::put_ref (tree ptr, const access_ref &ref, int ostype /* = 1 */) { /* Only add populated/valid entries. */ - if (!var_cache || !ref.ref || ref.sizrng[0] < 0) + if (!ref.ref || ref.sizrng[0] < 0) return; /* Add REF to the two-level cache. */ @@ -1535,20 +1528,20 @@ pointer_query::put_ref (tree ptr, const access_ref &ref, int ostype /* = 1 */) /* Grow INDICES if necessary. An index is valid if it's nonzero. Its value minus one is the index into ACCESS_REFS. Not all entries are valid. */ - if (var_cache->indices.length () <= idx) - var_cache->indices.safe_grow_cleared (idx + 1); + if (var_cache.indices.length () <= idx) + var_cache.indices.safe_grow_cleared (idx + 1); - if (!var_cache->indices[idx]) - var_cache->indices[idx] = var_cache->access_refs.length () + 1; + if (!var_cache.indices[idx]) + var_cache.indices[idx] = var_cache.access_refs.length () + 1; /* Grow ACCESS_REF cache if necessary. An entry is valid if its REF member is nonnull. All entries except for the last two are valid. Once nonnull, the REF value must stay unchanged. */ - unsigned cache_idx = var_cache->indices[idx]; - if (var_cache->access_refs.length () <= cache_idx) - var_cache->access_refs.safe_grow_cleared (cache_idx + 1); + unsigned cache_idx = var_cache.indices[idx]; + if (var_cache.access_refs.length () <= cache_idx) + var_cache.access_refs.safe_grow_cleared (cache_idx + 1); - access_ref &cache_ref = var_cache->access_refs[cache_idx]; + access_ref &cache_ref = var_cache.access_refs[cache_idx]; if (cache_ref.ref) { gcc_checking_assert (cache_ref.ref == ref.ref); @@ -1563,10 +1556,8 @@ pointer_query::put_ref (tree ptr, const access_ref &ref, int ostype /* = 1 */) void pointer_query::flush_cache () { - if (!var_cache) - return; - var_cache->indices.release (); - var_cache->access_refs.release (); + var_cache.indices.release (); + var_cache.access_refs.release (); } /* Dump statistics and, optionally, cache contents to DUMP_FILE. */ @@ -1574,20 +1565,17 @@ pointer_query::flush_cache () void pointer_query::dump (FILE *dump_file, bool contents /* = false */) { - if (!var_cache) - return; - unsigned nused = 0, nrefs = 0; - unsigned nidxs = var_cache->indices.length (); + unsigned nidxs = var_cache.indices.length (); for (unsigned i = 0; i != nidxs; ++i) { - unsigned ari = var_cache->indices[i]; + unsigned ari = var_cache.indices[i]; if (!ari) continue; ++nused; - const access_ref &aref = var_cache->access_refs[ari]; + const access_ref &aref = var_cache.access_refs[ari]; if (!aref.ref) continue; @@ -1604,7 +1592,7 @@ pointer_query::dump (FILE *dump_file, bool contents /* = false */) " failures: %u\n" " max_depth: %u\n", nidxs, nused, - var_cache->access_refs.length (), nrefs, + var_cache.access_refs.length (), nrefs, hits, misses, failures, max_depth); if (!contents || !nidxs) @@ -1614,11 +1602,11 @@ pointer_query::dump (FILE *dump_file, bool contents /* = false */) for (unsigned i = 0; i != nidxs; ++i) { - unsigned ari = var_cache->indices[i]; + unsigned ari = var_cache.indices[i]; if (!ari) continue; - const access_ref &aref = var_cache->access_refs[ari]; + const access_ref &aref = var_cache.access_refs[ari]; if (!aref.ref) continue; diff --git a/gcc/pointer-query.h b/gcc/pointer-query.h index dbdcd593b79..4c725eeaf34 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 diff --git a/gcc/tree-ssa-strlen.cc b/gcc/tree-ssa-strlen.cc index df97b86d5f8..d5360f0bc28 100644 --- a/gcc/tree-ssa-strlen.cc +++ b/gcc/tree-ssa-strlen.cc @@ -236,8 +236,7 @@ class strlen_pass : public dom_walker public: strlen_pass (cdi_direction direction) : dom_walker (direction), - ptr_qry (&m_ranger, &var_cache), - var_cache (), + ptr_qry (&m_ranger), m_cleanup_cfg (false) { } @@ -301,10 +300,9 @@ public: gimple_ranger m_ranger; - /* A pointer_query object and its cache to store information about - pointers and their targets in. */ + /* A pointer_query object to store information about pointers and + their targets in. */ pointer_query ptr_qry; - pointer_query::cache_type var_cache; gimple_stmt_iterator m_gsi; From patchwork Wed Feb 2 23:35:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 1587819 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=bCikwT8q; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4Jpysf2Hvyz9sFn for ; Thu, 3 Feb 2022 10:38:14 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E1ED8385800D for ; Wed, 2 Feb 2022 23:38:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E1ED8385800D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1643845091; bh=eEz20extFJPp+2tLvLXOrHF8T8wNGARLmgiKiQIt+NI=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=bCikwT8qvtDYmck995xGkjcFurxmTWWyEw4gnXyjyRsazqJlirlj3oM7lnttlbYzn yLVKMKt9QOejfrw/1krG/mlZWL8+4SmYN9qZdNe19ul6nZ/ixrK6SkczoJASYmaqYW mFql3KMfv++joOuyaejEKPgrYGi74Q7OPrO/94DE= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-il1-x130.google.com (mail-il1-x130.google.com [IPv6:2607:f8b0:4864:20::130]) by sourceware.org (Postfix) with ESMTPS id D1BD2385840B for ; Wed, 2 Feb 2022 23:35:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D1BD2385840B Received: by mail-il1-x130.google.com with SMTP id s1so712488ilj.7 for ; Wed, 02 Feb 2022 15:35:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=eEz20extFJPp+2tLvLXOrHF8T8wNGARLmgiKiQIt+NI=; b=J0MWd5Hdjlf148fB0Suah/aSM+hzXPJZ6ENqHuEZt8LRTt24ltEv76QqL71JJ2Dvbr 1X7CAS5Fw+Wu4DDGfJXh0KZaCyHbx/X/HOq8oa0xg08TZFih7Kk1WNRAvmTN2a2speVA PEA5KuTOJMR/TnJ8UFjbtczxOjJNMOyA1yQkwr9aVKGwNvbJREA4Gv6FIohdLcumKM7B 7H4if+3EygZZ6qgzR8s+u64RflXKRJCpVTTz1wC0FQ57vYx8CuCnlGr90PDjX9XjEIql ONLUS++Sdy1RE1e8URvVDnNUOm6An8CIYeH/X1mLObn3o+KZzfFDO+zTfWJzo0bQvwS/ /7ZA== X-Gm-Message-State: AOAM533xXvbexc7hAKbeVAb0Qj2ev/Cgvri1kXmdLSi+oMYN87zfSDRS 3oOvO09JVAJiCCUE7TXPI7iqup0M3H0eMg== X-Google-Smtp-Source: ABdhPJx4laR660fewnZ1YJt6DBwspW1BAGKYQdFJhM5wUmTdi8zbHem3HTiv6ivqGDAt3Z/45v+2IQ== X-Received: by 2002:a05:6e02:15c2:: with SMTP id q2mr12883026ilu.226.1643844938170; Wed, 02 Feb 2022 15:35:38 -0800 (PST) Received: from bigbox.redhat.com (97-118-100-142.hlrn.qwest.net. [97.118.100.142]) by smtp.googlemail.com with ESMTPSA id l1sm7481267iln.29.2022.02.02.15.35.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Feb 2022 15:35:37 -0800 (PST) X-Google-Original-From: Martin Sebor To: gcc-patches@gcc.gnu.org Subject: [PATCH 2/3] Enable pointer_query caching for -Warray-bounds. Date: Wed, 2 Feb 2022 16:35:29 -0700 Message-Id: <20220202233530.2568647-3-msebor@redhat.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220202233530.2568647-1-msebor@redhat.com> References: <20220202233530.2568647-1-msebor@redhat.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Martin Sebor via Gcc-patches From: Martin Sebor Reply-To: Martin Sebor Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" The second patch in the series adds a pointer_query instance to the array bounds checker object and uses it for each invocation to check a function. gcc/ChangeLog: * gimple-array-bounds.cc (array_bounds_checker::array_bounds_checker): Define ctor. (array_bounds_checker::get_value_range): Use new member. (array_bounds_checker::check_mem_ref): Same. * gimple-array-bounds.h (array_bounds_checker::array_bounds_checker): Outline ctor. (array_bounds_checker::m_ptr_query): New member. --- gcc/gimple-array-bounds.cc | 13 ++++++++++--- gcc/gimple-array-bounds.h | 10 ++++++---- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/gcc/gimple-array-bounds.cc b/gcc/gimple-array-bounds.cc index 80c70b49607..7ec8b08c8d2 100644 --- a/gcc/gimple-array-bounds.cc +++ b/gcc/gimple-array-bounds.cc @@ -24,6 +24,7 @@ along with GCC; see the file COPYING3. If not see #include "tree.h" #include "gimple.h" #include "ssa.h" +#include "pointer-query.h" #include "gimple-array-bounds.h" #include "gimple-iterator.h" #include "gimple-walk.h" @@ -37,7 +38,13 @@ along with GCC; see the file COPYING3. If not see #include "domwalk.h" #include "tree-cfg.h" #include "attribs.h" -#include "pointer-query.h" + +array_bounds_checker::array_bounds_checker (struct function *func, + range_query *qry) + : fun (func), m_ptr_qry (qry) +{ + /* No-op. */ +} // This purposely returns a value_range, not a value_range_equiv, to // break the dependency on equivalences for this pass. @@ -45,7 +52,7 @@ along with GCC; see the file COPYING3. If not see const value_range * array_bounds_checker::get_value_range (const_tree op, gimple *stmt) { - return ranges->get_value_range (op, stmt); + return m_ptr_qry.rvals->get_value_range (op, stmt); } /* Try to determine the DECL that REF refers to. Return the DECL or @@ -401,7 +408,7 @@ array_bounds_checker::check_mem_ref (location_t location, tree ref, axssize = wi::to_offset (access_size); access_ref aref; - if (!compute_objsize (ref, m_stmt, 0, &aref, ranges)) + if (!m_ptr_qry.get_ref (ref, m_stmt, &aref, 0)) return false; if (aref.offset_in_range (axssize)) diff --git a/gcc/gimple-array-bounds.h b/gcc/gimple-array-bounds.h index d42146b87c8..eb399271da7 100644 --- a/gcc/gimple-array-bounds.h +++ b/gcc/gimple-array-bounds.h @@ -20,13 +20,14 @@ along with GCC; see the file COPYING3. If not see #ifndef GCC_GIMPLE_ARRAY_BOUNDS_H #define GCC_GIMPLE_ARRAY_BOUNDS_H +#include "pointer-query.h" + class array_bounds_checker { friend class check_array_bounds_dom_walker; public: - array_bounds_checker (struct function *fun, range_query *v) - : fun (fun), ranges (v) { } + array_bounds_checker (struct function *, range_query *); void check (); private: @@ -38,8 +39,9 @@ private: /* Current function. */ struct function *fun; - /* Ranger instance. */ - range_query *ranges; + /* A pointer_query object to store information about pointers and + their targets in. */ + pointer_query m_ptr_qry; /* Current statement. */ gimple *m_stmt; }; From patchwork Wed Feb 2 23:35:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 1587820 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=qV+YsTPI; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4JpytL1pfpz9sFn for ; Thu, 3 Feb 2022 10:38:50 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EA201385AC29 for ; Wed, 2 Feb 2022 23:38:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EA201385AC29 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1643845128; bh=+UincTgxRKKgIAa8xXr2999XonQ32orGMBybaa+lJs0=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=qV+YsTPINNpKHtPYcxYHLFznXlqP6xS0yBcQ2eZ1cm+v+z1rMiAAq9dtNUQX9IMrf WjpOZazoascfRWYXQb8M0oHRV+CPXSj8R3yr93rFvpj0UcKwkypazu18H0cYRmcQwB /nGnehVT1u8s1PGC9C+p8+UblMzxrqOe6Xs9kKe4= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-io1-xd33.google.com (mail-io1-xd33.google.com [IPv6:2607:f8b0:4864:20::d33]) by sourceware.org (Postfix) with ESMTPS id B26413858D37 for ; Wed, 2 Feb 2022 23:35:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B26413858D37 Received: by mail-io1-xd33.google.com with SMTP id c188so1099493iof.6 for ; Wed, 02 Feb 2022 15:35:39 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+UincTgxRKKgIAa8xXr2999XonQ32orGMBybaa+lJs0=; b=pF/jy9EvpmRLYhoCqREhjWK4O60iQMgj4wU+8KFabaa7EkoKn/3z6lMCikqyZhUrso arlm8zud8ll0jNeLs8HP780c1C6OlwWMPp0IFkh2XHT5KH2QA/NLsoQyaMKRBxo2h1i5 dw2bn4XPU38YPq/8DYQFb5cChXMhnnACnI7h+oXC58EzJoyDoPQSXxK9FL+UeeEn4bs2 H4TX7I77tv+PkaijwgxA1+YD66m5I3+JmdEzOD54j8Un4McjduQRLU2ymuAwwYZH1yJB 7F/96TRGiVIxsyk+Oi+FlG8On15HQbqJIShsAc3Uk5xRcOCPKMFXHvhWdZCqV4eHVUQ7 fE5w== X-Gm-Message-State: AOAM532yLGevkKluErc70FspJHwKd8o0lGfr6tAGc4R6gV2Ds2Jvm6ab ma7A3ZzvBSQpytTE0HeOAWiBCueSIBumoQ== X-Google-Smtp-Source: ABdhPJxl2MtobZDz8Wt35Z5Auf9+PLqY1GoNxKLjeDFYYf716TsVTX42dqtgV8DGlv/rFpR7OS/C1g== X-Received: by 2002:a05:6638:25c6:: with SMTP id u6mr9405756jat.187.1643844938838; Wed, 02 Feb 2022 15:35:38 -0800 (PST) Received: from bigbox.redhat.com (97-118-100-142.hlrn.qwest.net. [97.118.100.142]) by smtp.googlemail.com with ESMTPSA id l1sm7481267iln.29.2022.02.02.15.35.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Feb 2022 15:35:38 -0800 (PST) X-Google-Original-From: Martin Sebor To: gcc-patches@gcc.gnu.org Subject: [PATCH 3/3] Enable pointer_query caching for -Wrestrict. Date: Wed, 2 Feb 2022 16:35:30 -0700 Message-Id: <20220202233530.2568647-4-msebor@redhat.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220202233530.2568647-1-msebor@redhat.com> References: <20220202233530.2568647-1-msebor@redhat.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Martin Sebor via Gcc-patches From: Martin Sebor Reply-To: Martin Sebor Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" The third patch in the series adds a pointer_query instance to the wrestrict pass and uses it for each invocation to check a function. gcc/ChangeLog: * gimple-ssa-warn-restrict.cc (class pass_wrestrict): Outline ctor. (pass_wrestrict::m_ptr_qry): New member. (wrestrict_walk): Rename... (pass_wrestrict::check_block): ...to this. (pass_wrestrict::execute): Set up and tear down pointer_query and ranger. (builtin_memref::builtin_memref): Change ctor argument. Simplify. (builtin_access::builtin_access): Same. (builtin_access::m_ptr_qry): New member. (check_call): Rename... (pass_wrestrict::check_call): ...to this. (check_bounds_or_overlap): Change argument. * gimple-ssa-warn-restrict.h (check_bounds_or_overlap): Same. --- gcc/gimple-ssa-warn-restrict.cc | 126 ++++++++++++++++---------------- gcc/gimple-ssa-warn-restrict.h | 2 +- 2 files changed, 62 insertions(+), 66 deletions(-) diff --git a/gcc/gimple-ssa-warn-restrict.cc b/gcc/gimple-ssa-warn-restrict.cc index d1beb01fe89..72418398dad 100644 --- a/gcc/gimple-ssa-warn-restrict.cc +++ b/gcc/gimple-ssa-warn-restrict.cc @@ -62,46 +62,63 @@ const pass_data pass_data_wrestrict = { class pass_wrestrict : public gimple_opt_pass { public: - pass_wrestrict (gcc::context *ctxt) - : gimple_opt_pass (pass_data_wrestrict, ctxt) - { } + pass_wrestrict (gcc::context *); - opt_pass *clone () { return new pass_wrestrict (m_ctxt); } + // opt_pass *clone () { return new pass_wrestrict (m_ctxt); } virtual bool gate (function *); virtual unsigned int execute (function *); + + void check_call (gimple *); + + void check_block (basic_block); + + /* A pointer_query object to store information about pointers and + their targets in. */ + pointer_query m_ptr_qry; }; +pass_wrestrict::pass_wrestrict (gcc::context *ctxt) + : gimple_opt_pass (pass_data_wrestrict, ctxt), + m_ptr_qry () +{ } + bool pass_wrestrict::gate (function *fun ATTRIBUTE_UNUSED) { return warn_array_bounds || warn_restrict || warn_stringop_overflow; } -static void check_call (range_query *, gimple *); - -static void -wrestrict_walk (range_query *query, basic_block bb) +void +pass_wrestrict::check_block (basic_block bb) { /* Iterate over statements, looking for function calls. */ - for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si); - gsi_next (&si)) + for (auto si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si)) { gimple *stmt = gsi_stmt (si); if (!is_gimple_call (stmt)) continue; - check_call (query, stmt); + check_call (stmt); } } unsigned pass_wrestrict::execute (function *fun) { - gimple_ranger ranger; + /* Create a new ranger instance and associate it with FUN. */ + m_ptr_qry.rvals = enable_ranger (fun); + basic_block bb; FOR_EACH_BB_FN (bb, fun) - wrestrict_walk (&ranger, bb); + check_block (bb); + + m_ptr_qry.flush_cache (); + + /* Release the ranger instance and replace it with a global ranger. + Also reset the pointer since calling disable_ranger() deletes it. */ + disable_ranger (fun); + m_ptr_qry.rvals = NULL; return 0; } @@ -145,7 +162,7 @@ public: only the destination reference is. */ bool strbounded_p; - builtin_memref (range_query *, gimple *, tree, tree); + builtin_memref (pointer_query &, gimple *, tree, tree); tree offset_out_of_bounds (int, offset_int[3]) const; @@ -153,7 +170,7 @@ private: /* Call statement to the built-in. */ gimple *stmt; - range_query *query; + pointer_query &m_ptr_qry; /* Ctor helper to set or extend OFFRANGE based on argument. */ void extend_offset_range (tree); @@ -187,7 +204,8 @@ class builtin_access && detect_overlap != &builtin_access::no_overlap); } - builtin_access (range_query *, gimple *, builtin_memref &, builtin_memref &); + builtin_access (pointer_query &, gimple *, + builtin_memref &, builtin_memref &); /* Entry point to determine overlap. */ bool overlap (); @@ -225,7 +243,7 @@ class builtin_access a size SIZE in bytes. If SIZE is NULL_TREE then the size is assumed to be unknown. STMT is the statement in which expr appears in. */ -builtin_memref::builtin_memref (range_query *query, gimple *stmt, tree expr, +builtin_memref::builtin_memref (pointer_query &ptrqry, gimple *stmt, tree expr, tree size) : ptr (expr), ref (), @@ -238,7 +256,7 @@ builtin_memref::builtin_memref (range_query *query, gimple *stmt, tree expr, maxobjsize (tree_to_shwi (max_object_size ())), strbounded_p (), stmt (stmt), - query (query) + m_ptr_qry (ptrqry) { /* Unfortunately, wide_int default ctor is a no-op so array members of the type must be set individually. */ @@ -257,7 +275,7 @@ builtin_memref::builtin_memref (range_query *query, gimple *stmt, tree expr, tree range[2]; /* Determine the size range, allowing for the result to be [0, 0] for SIZE in the anti-range ~[0, N] where N >= PTRDIFF_MAX. */ - get_size_range (query, size, stmt, range, SR_ALLOW_ZERO); + get_size_range (m_ptr_qry.rvals, size, stmt, range, SR_ALLOW_ZERO); sizrange[0] = wi::to_offset (range[0]); sizrange[1] = wi::to_offset (range[1]); /* get_size_range returns SIZE_MAX for the maximum size. @@ -334,23 +352,10 @@ builtin_memref::extend_offset_range (tree offset) /* A pointer offset is represented as sizetype but treated as signed. */ wide_int min, max; - value_range_kind rng; + value_range_kind rng = VR_VARYING; value_range vr; - if (query && query->range_of_expr (vr, offset, stmt)) - { - rng = vr.kind (); - if (!vr.undefined_p ()) - { - min = wi::to_wide (vr.min ()); - max = wi::to_wide (vr.max ()); - } - } - else + if (m_ptr_qry.rvals->range_of_expr (vr, offset, stmt)) { - /* There is a global version here because - check_bounds_or_overlap may be called from gimple - fold during gimple lowering. */ - get_range_query (cfun)->range_of_expr (vr, offset, stmt); rng = vr.kind (); if (!vr.undefined_p ()) { @@ -358,6 +363,7 @@ builtin_memref::extend_offset_range (tree offset) max = wi::to_wide (vr.max ()); } } + if (rng == VR_ANTI_RANGE && wi::lts_p (max, min)) { /* Convert an anti-range whose upper bound is less than @@ -674,7 +680,7 @@ builtin_memref::offset_out_of_bounds (int strict, offset_int ooboff[3]) const /* Create an association between the memory references DST and SRC for access by a call EXPR to a memory or string built-in funtion. */ -builtin_access::builtin_access (range_query *query, gimple *call, +builtin_access::builtin_access (pointer_query &ptrqry, gimple *call, builtin_memref &dst, builtin_memref &src) : dstref (&dst), srcref (&src), sizrange (), ovloff (), ovlsiz (), @@ -766,39 +772,28 @@ builtin_access::builtin_access (range_query *query, gimple *call, return; } - const offset_int maxobjsize = dst.maxobjsize; - /* Try to determine the size of the base object. compute_objsize expects a pointer so create one if BASE is a non-pointer object. */ - tree addr; if (dst.basesize < 0) { - addr = dst.base; - if (!POINTER_TYPE_P (TREE_TYPE (addr))) - addr = build1 (ADDR_EXPR, (TREE_TYPE (addr)), addr); - - if (tree dstsize = compute_objsize (addr, call, ostype)) - dst.basesize = wi::to_offset (dstsize); - else if (POINTER_TYPE_P (TREE_TYPE (addr))) - dst.basesize = HOST_WIDE_INT_MIN; + access_ref aref; + if (ptrqry.get_ref (dst.base, call, &aref, ostype) && aref.base0) + dst.basesize = aref.sizrng[1]; else - dst.basesize = maxobjsize; + dst.basesize = HOST_WIDE_INT_MIN; } if (src.base && src.basesize < 0) { - addr = src.base; - if (!POINTER_TYPE_P (TREE_TYPE (addr))) - addr = build1 (ADDR_EXPR, (TREE_TYPE (addr)), addr); - - if (tree srcsize = compute_objsize (addr, call, ostype)) - src.basesize = wi::to_offset (srcsize); - else if (POINTER_TYPE_P (TREE_TYPE (addr))) - src.basesize = HOST_WIDE_INT_MIN; + access_ref aref; + if (ptrqry.get_ref (src.base, call, &aref, ostype) && aref.base0) + src.basesize = aref.sizrng[1]; else - src.basesize = maxobjsize; + src.basesize = HOST_WIDE_INT_MIN; } + const offset_int maxobjsize = dst.maxobjsize; + /* Make adjustments for references to the same object by string built-in functions to reflect the constraints imposed by the function. */ @@ -814,7 +809,7 @@ builtin_access::builtin_access (range_query *query, gimple *call, tree size = gimple_call_arg (call, sizeargno); tree range[2]; - if (get_size_range (query, size, call, range, true)) + if (get_size_range (ptrqry.rvals, size, call, range, true)) { bounds[0] = wi::to_offset (range[0]); bounds[1] = wi::to_offset (range[1]); @@ -1895,8 +1890,8 @@ maybe_diag_access_bounds (gimple *call, tree func, int strict, /* Check a CALL statement for restrict-violations and issue warnings if/when appropriate. */ -static void -check_call (range_query *query, gimple *call) +void +pass_wrestrict::check_call (gimple *call) { /* Avoid checking the call if it has already been diagnosed for some reason. */ @@ -1986,7 +1981,7 @@ check_call (range_query *query, gimple *call) || (dstwr && !INTEGRAL_TYPE_P (TREE_TYPE (dstwr)))) return; - opt_code opt = check_bounds_or_overlap (query, call, dst, src, dstwr, + opt_code opt = check_bounds_or_overlap (m_ptr_qry, call, dst, src, dstwr, NULL_TREE); /* Avoid diagnosing the call again. */ suppress_warning (call, opt); @@ -2006,25 +2001,26 @@ check_bounds_or_overlap (gimple *call, tree dst, tree src, tree dstsize, tree srcsize, bool bounds_only /* = false */, bool do_warn /* = true */) { - return check_bounds_or_overlap (/*range_query=*/NULL, + pointer_query ptrqry (get_range_query (cfun)); + return check_bounds_or_overlap (ptrqry, call, dst, src, dstsize, srcsize, bounds_only, do_warn); } opt_code -check_bounds_or_overlap (range_query *query, +check_bounds_or_overlap (pointer_query &ptrqry, gimple *call, tree dst, tree src, tree dstsize, tree srcsize, bool bounds_only /* = false */, bool do_warn /* = true */) { tree func = gimple_call_fndecl (call); - builtin_memref dstref (query, call, dst, dstsize); - builtin_memref srcref (query, call, src, srcsize); + builtin_memref dstref (ptrqry, call, dst, dstsize); + builtin_memref srcref (ptrqry, call, src, srcsize); /* Create a descriptor of the access. This may adjust both DSTREF and SRCREF based on one another and the kind of the access. */ - builtin_access acs (query, call, dstref, srcref); + builtin_access acs (ptrqry, call, dstref, srcref); /* Set STRICT to the value of the -Warray-bounds=N argument for string functions or when N > 1. */ diff --git a/gcc/gimple-ssa-warn-restrict.h b/gcc/gimple-ssa-warn-restrict.h index 1c60f51b500..2bdde030023 100644 --- a/gcc/gimple-ssa-warn-restrict.h +++ b/gcc/gimple-ssa-warn-restrict.h @@ -22,7 +22,7 @@ extern opt_code check_bounds_or_overlap (gimple *, tree, tree, tree, tree, bool = false, bool = true); -extern opt_code check_bounds_or_overlap (class range_query *, gimple *, +extern opt_code check_bounds_or_overlap (class pointer_query &, gimple *, tree, tree, tree, tree, bool = false, bool = true);