public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-9407] ipa/105676 - pure attribute suggestion for const function
@ 2023-04-17  9:14 Richard Biener
  0 siblings, 0 replies; only message in thread
From: Richard Biener @ 2023-04-17  9:14 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:96785885ceed34638d4b58e88cba6e6e8368c0e3

commit r12-9407-g96785885ceed34638d4b58e88cba6e6e8368c0e3
Author: Richard Biener <rguenther@suse.de>
Date:   Wed Dec 7 10:26:01 2022 +0100

    ipa/105676 - pure attribute suggestion for const function
    
    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.
    
            PR ipa/105676
            * ipa-pure-const.cc (ipa_make_function_pure): Skip also
            for functions already being const.
    
            * gcc.dg/pr105676.c: New testcase.
    
    (cherry picked from commit 45e09c2eb9c2bdd34ef777e06ddc9908dd0664f9)

Diff:
---
 gcc/ipa-pure-const.cc           |  5 +++--
 gcc/testsuite/gcc.dg/pr105676.c | 14 ++++++++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/gcc/ipa-pure-const.cc b/gcc/ipa-pure-const.cc
index 2b4950a5969..2642df91e63 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;
+}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-04-17  9:14 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-17  9:14 [gcc r12-9407] ipa/105676 - pure attribute suggestion for const function Richard Biener

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).