From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id 93F353853D46 for ; Wed, 7 Dec 2022 10:27:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 93F353853D46 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 87F9421BFC for ; Wed, 7 Dec 2022 10:27:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1670408859; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=tet+9B+QQBmKEzf0OuYuOmq1uJkHcIc5Hkl3gWgkqMA=; b=IPqFqG1Ioou6rKTsOzV3qim/wUa/npg8VqSmOxdh5Wuh16XateOUBbQc2cyh7pFiEWAmDo /xKCzkUXfM5qNQjHvrj33pSXjeMoLZkFCuTpVQa7AYbvX/O35zYTVjJnC8rNcd98jMvmVF J2eZxgRAYHAtuvm3p1SCVMEip2iRQEM= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1670408859; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=tet+9B+QQBmKEzf0OuYuOmq1uJkHcIc5Hkl3gWgkqMA=; b=rr9rBeVYGPbL/n3+5c/8Ns53StlhDX34MaixTWkIJR3iSk7NWKEmiI3nLzVNpeHPN0aEKo yfUKSHGaYuCeDOAA== Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap1.suse-dmz.suse.de (Postfix) with ESMTPS id 73934134CD for ; Wed, 7 Dec 2022 10:27:39 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap1.suse-dmz.suse.de with ESMTPSA id RfkcG5tqkGOADAAAGKfGzw (envelope-from ) for ; Wed, 07 Dec 2022 10:27:39 +0000 Date: Wed, 7 Dec 2022 11:27:39 +0100 (CET) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] ipa/105676 - pure attribute suggestion for const function MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Message-Id: <20221207102739.73934134CD@imap1.suse-dmz.suse.de> X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,SPF_HELO_NONE,SPF_PASS,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 List-Id: When a function is declared const (even though it technically accesses memory), ipa-modref discovering pureness shouldn't end up suggesting that attribute. The following thus exempts 'const' functions from ipa_make_function_pure handling. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR ipa/105676 * ipa-pure-const.cc (ipa_make_function_pure): Skip also for functions already being const. * gcc.dg/pr105676.c: New testcase. --- gcc/ipa-pure-const.cc | 5 +++-- gcc/testsuite/gcc.dg/pr105676.c | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr105676.c diff --git a/gcc/ipa-pure-const.cc b/gcc/ipa-pure-const.cc index 572a6da274f..0b748eee0ee 100644 --- a/gcc/ipa-pure-const.cc +++ b/gcc/ipa-pure-const.cc @@ -1526,8 +1526,9 @@ ipa_make_function_pure (struct cgraph_node *node, bool looping, bool local) { bool cdtor = false; - if (DECL_PURE_P (node->decl) - && (looping || !DECL_LOOPING_CONST_OR_PURE_P (node->decl))) + if (TREE_READONLY (node->decl) + || (DECL_PURE_P (node->decl) + && (looping || !DECL_LOOPING_CONST_OR_PURE_P (node->decl)))) return false; warn_function_pure (node->decl, !looping); if (local && skip_function_for_local_pure_const (node)) diff --git a/gcc/testsuite/gcc.dg/pr105676.c b/gcc/testsuite/gcc.dg/pr105676.c new file mode 100644 index 00000000000..077fc18a17f --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr105676.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -Wsuggest-attribute=pure" } */ + +__attribute__((const)) +extern int do_expensive_calculation(void); + +__attribute__((const)) +int getval(void) /* { dg-bogus "candidate for attribute" } */ +{ + static int cache = -1; + if (cache == -1) + cache = do_expensive_calculation(); + return cache; +} -- 2.35.3