From patchwork Fri Feb 10 00:01:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew MacLeod X-Patchwork-Id: 55144 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp647156wrn; Thu, 9 Feb 2023 16:02:41 -0800 (PST) X-Google-Smtp-Source: AK7set/aeq7FD3uDoYVImmKxf71wCfvzfZE2lGFaz3hRPZ1L10DpclvAy493ktcbHBSSyHhDgXm5 X-Received: by 2002:a50:f691:0:b0:4aa:a255:291e with SMTP id d17-20020a50f691000000b004aaa255291emr13948681edn.32.1675987361027; Thu, 09 Feb 2023 16:02:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675987361; cv=none; d=google.com; s=arc-20160816; b=p3GorL6qnKrV8hPz2eN3YJpImFQ55rqr4+R2yfzOkTyCDTVfFWvN914Y7eEcI6bolr +FCclMgmvReURbMpDIJq3gHqsyRocScPnGUb3Oyb1DBKA8Y1nsQgXbVLV2fkmVqjr6Sh 0hnrQR5nkmpP20fvT9mbkxJlpD2RtXNMuYGAd6Ipz/c6aJ8guXw9Zl4E5Ouqvic/8CNV jnRbsdNOF5+4SQ4gZ7CpX/MmVh7Q/2gybDMs0lNhHXefm2wtrVhJIkDslVLfM6s5gGmK GfpQV0s9gKoGUb62HEpSkzY4Z1aVRU045jfW4KjQkrxkN1YH+NSZLvmbKWZGNMUUtzl8 TOzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:content-language :cc:to:subject:user-agent:mime-version:date:message-id:dmarc-filter :delivered-to:dkim-signature:dkim-filter; bh=9ZJ9PFYbBNf8WceK9QCILllcjwKLowDv7cFXXQyuyac=; b=xsmgNdflrKr0Zsh/fQ0zz+F18r28rwterQyDbu53xS6MfqI5SBtMty/fyS4DWCCAPi 4RiBbF0sxRiGI/w7iqiHp3NSimWFqmkk24yK9fxV7h2+aaVmnMq0QmUIQ+a0BOlwe4Hd C9DI0I8Ffcysi1Xl7t+U2oyPPUoAVJ9mBHuXd++K31ntG+LeX7W5cRuaXuupSrSmbkzT +BmO/i8T79Wnp22oGH+WCO8d3U4vG2BzgeUIxdBdrGYY2IYL/DClgYbWIkiRPAtBcLMy Gc9y+LAGCNBOT1wJAyFIhPl778hW6uw+yGxRe4NO4jowvBowEco8+5hjPpuvj9Ag/Yv5 Go1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=M4xmZKq4; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id q8-20020aa7da88000000b004aaa3346bc5si3761661eds.620.2023.02.09.16.02.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Feb 2023 16:02:41 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=M4xmZKq4; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9EADE3854839 for ; Fri, 10 Feb 2023 00:02:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9EADE3854839 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675987346; bh=9ZJ9PFYbBNf8WceK9QCILllcjwKLowDv7cFXXQyuyac=; h=Date:Subject:To:Cc:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=M4xmZKq4Et6lmaESlaey2+O0z47ntN7xIeMtjtkPQrQtUJVVeinhNYxHKZndAnN/X lfLo1vqBtowd8FPJfJTOwehFgPz//DMUXMHKZMkUGrMZytTOX+wHs2pZeDsS038xIv kQz7RCj+mhbeKiHa5xyRB0urPIV/U8ZUx35q//Lc= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 82C433858C50 for ; Fri, 10 Feb 2023 00:01:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 82C433858C50 Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-576-lDaScEcqOLqZZ7fOSQhtMg-1; Thu, 09 Feb 2023 19:01:32 -0500 X-MC-Unique: lDaScEcqOLqZZ7fOSQhtMg-1 Received: by mail-qv1-f70.google.com with SMTP id e5-20020a056214110500b0053547681552so2200287qvs.8 for ; Thu, 09 Feb 2023 16:01:32 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-language:cc:to:subject:from:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=eT68mvbRfqY6ie2gaDlAMoeHd8XMBlWr4SWbywzxuIE=; b=A2K1Sw5D4NDFRqBc3FiLyLUBt28ASEWOciGSbfiDSZwnh8wQJtI8DBRA9AsneYaerg pA+C+dnKneWl3RNxi2BVeiGYxLc1Ah6cYOekx5loZM7ZhYlxTqb7WpnoGefXloJJi60Z 8B/+ivIgycm7UsabvRmakwqptGvjFSLZwD+Kwwr/PPAQO6juE49xL/t3VlWayPEb4fUL aW7CvAbKvjTCxO78VsEg1q8Emc86M8lHlzF+ZgwiB0Kr9zfeyZAr6eN2baZKOb64GEv9 N5P8q7kY67ktGbx0+cb5f/KKiIER6RVLEMm+hBRe4bbRDFd60EFudZ3TlDGIBVz5EZhw ZTwQ== X-Gm-Message-State: AO0yUKWzR6HM+PtFTu4ev+mQY3gm7DJLXlBTrOqig7XGf6hZPgtWkva/ bcmtH7txFpTA50+v8Oku+16RmipsLPbhjaiD2hNt+FOfYCwl1eIqqmoQM8/fyKao0Uyihs8/BCl JCref1ilIMT+WD+YwtmGsp4Tv1tzJYoUS+l6ci8WNYNvQ5MUCkMTK6wC9sViJuLYcGbnoChNPxn A= X-Received: by 2002:a05:622a:309:b0:3ba:1641:5a49 with SMTP id q9-20020a05622a030900b003ba16415a49mr22713483qtw.8.1675987291083; Thu, 09 Feb 2023 16:01:31 -0800 (PST) X-Received: by 2002:a05:622a:309:b0:3ba:1641:5a49 with SMTP id q9-20020a05622a030900b003ba16415a49mr22713442qtw.8.1675987290758; Thu, 09 Feb 2023 16:01:30 -0800 (PST) Received: from ?IPV6:2607:fea8:a263:f600::de2a? ([2607:fea8:a263:f600::de2a]) by smtp.gmail.com with ESMTPSA id t19-20020ac85313000000b003b9f1b7895asm2302407qtn.10.2023.02.09.16.01.29 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 09 Feb 2023 16:01:30 -0800 (PST) Message-ID: <5f8a85e0-71a3-0394-6f1a-18633cd6c71a@redhat.com> Date: Thu, 9 Feb 2023 19:01:28 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.6.0 Subject: [PATCH] PR tree-optimization/108520 - Add function context for querying global ranges. To: gcc-patches Cc: "hernandez, aldy" , Jakub Jelinek X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: Andrew MacLeod via Gcc-patches From: Andrew MacLeod Reply-To: Andrew MacLeod Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757400123041163642?= X-GMAIL-MSGID: =?utf-8?q?1757400123041163642?= I was about to ping on this, and then found it in my drafts.. Doh! get_range_global() can invoke tree.cc::nonnull_arg_p() if the item being queried is a pointer and a parameter.  This routine assumes the context is CFUN, and this is not always true. This patch simply adds a function context to the get_range_global query, and defaults it to cfun. If the context passed in is anything different than cfun, then it simply chooses not to invoke nonnull_arg_p(). The check_assume function now directly calls gimple_range_global with the correct function context instead of indirectly calling it through the global_range_query->range_of_expr () method. Thats all it should have been doing in the first place really since its always an ssa name. Bootstraps on x86_64-pc-linux-gnu with no regressions.  OK for trunk? Andrew From e64f04f15a47ef91a21668318728a49a90e8dae1 Mon Sep 17 00:00:00 2001 From: Andrew MacLeod Date: Mon, 6 Feb 2023 13:07:01 -0500 Subject: [PATCH] Add function context for querying global ranges. When processing arguments for assume functions, call get_global_range directly and utilize a function context pointer to avoid any assumptions about using cfun. PR tree-optimization/108520 gcc/ * gimple-range-infer.cc (check_assume_func): Invoke gimple_range_global directly instead using global_range_query. * value-query.cc (get_range_global): Add function context and avoid calling nonnull_arg_p if not cfun. (gimple_range_global): Add function context pointer. * value-query.h (imple_range_global): Add function context. gcc/testsuite/ * g++.dg/pr108520.C: New. --- gcc/gimple-range-infer.cc | 2 +- gcc/testsuite/g++.dg/pr108520.C | 17 +++++++++++++++++ gcc/value-query.cc | 10 +++++----- gcc/value-query.h | 4 +++- 4 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/g++.dg/pr108520.C diff --git a/gcc/gimple-range-infer.cc b/gcc/gimple-range-infer.cc index 4677ae21ccc..c765e072731 100644 --- a/gcc/gimple-range-infer.cc +++ b/gcc/gimple-range-infer.cc @@ -84,7 +84,7 @@ gimple_infer_range::check_assume_func (gcall *call) continue; // Query the global range of the default def in the assume function. Value_Range assume_range (type); - global_ranges.range_of_expr (assume_range, default_def); + gimple_range_global (assume_range, default_def, fun); // If there is a non-varying result, add it as an inferred range. if (!assume_range.varying_p ()) { diff --git a/gcc/testsuite/g++.dg/pr108520.C b/gcc/testsuite/g++.dg/pr108520.C new file mode 100644 index 00000000000..6cd677a852d --- /dev/null +++ b/gcc/testsuite/g++.dg/pr108520.C @@ -0,0 +1,17 @@ +// { dg-do compile { target c++23 } } +// { dg-options "-O2" } + +static void foo () {} +struct S { void (*f) (); }; + +[[gnu::nonnull (1)]] +void +bar (void *x) +{ + struct S a[3] = { { foo }, { foo }, { foo } }; + for (struct S *i = a, *e = a + 3; i != e; i++) + { + [[assume (i->f)]]; + i->f (); + } +} diff --git a/gcc/value-query.cc b/gcc/value-query.cc index 5345bb47718..f936e878080 100644 --- a/gcc/value-query.cc +++ b/gcc/value-query.cc @@ -312,7 +312,7 @@ get_ssa_name_ptr_info_nonnull (const_tree name) // return VARYING. static void -get_range_global (vrange &r, tree name) +get_range_global (vrange &r, tree name, struct function *fun = cfun) { tree type = TREE_TYPE (name); @@ -327,7 +327,7 @@ get_range_global (vrange &r, tree name) // anti-ranges for pointers. Note that this is only valid with // default definitions of PARM_DECLs. if (POINTER_TYPE_P (type) - && ((cfun && nonnull_arg_p (sym)) + && ((cfun && fun == cfun && nonnull_arg_p (sym)) || get_ssa_name_ptr_info_nonnull (name))) r.set_nonzero (type); else if (!POINTER_TYPE_P (type)) @@ -378,15 +378,15 @@ get_range_global (vrange &r, tree name) // https://gcc.gnu.org/pipermail/gcc-patches/2021-June/571709.html void -gimple_range_global (vrange &r, tree name) +gimple_range_global (vrange &r, tree name, struct function *fun) { tree type = TREE_TYPE (name); gcc_checking_assert (TREE_CODE (name) == SSA_NAME); - if (SSA_NAME_IS_DEFAULT_DEF (name) || (cfun && cfun->after_inlining) + if (SSA_NAME_IS_DEFAULT_DEF (name) || (fun && fun->after_inlining) || is_a (SSA_NAME_DEF_STMT (name))) { - get_range_global (r, name); + get_range_global (r, name, fun); return; } r.set_varying (type); diff --git a/gcc/value-query.h b/gcc/value-query.h index 03be0be9178..63878968118 100644 --- a/gcc/value-query.h +++ b/gcc/value-query.h @@ -143,6 +143,8 @@ get_range_query (const struct function *fun) return fun->x_range_query ? fun->x_range_query : &global_ranges; } -extern void gimple_range_global (vrange &v, tree name); +// Query the global range of NAME in function F. Default to cfun. +extern void gimple_range_global (vrange &v, tree name, + struct function *f = cfun); #endif // GCC_QUERY_H -- 2.39.0