]> gcc.gnu.org Git - gcc.git/commitdiff
ipa/105676 - pure attribute suggestion for const function
authorRichard Biener <rguenther@suse.de>
Wed, 7 Dec 2022 09:26:01 +0000 (10:26 +0100)
committerRichard Biener <rguenther@suse.de>
Mon, 17 Apr 2023 09:13:16 +0000 (11:13 +0200)
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)

gcc/ipa-pure-const.cc
gcc/testsuite/gcc.dg/pr105676.c [new file with mode: 0644]

index 2b4950a5969115f1d05dc8b618c0d64c9aa2f68f..2642df91e638909c282a25d48b4291a9deba855b 100644 (file)
@@ -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 (file)
index 0000000..077fc18
--- /dev/null
@@ -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;
+}
This page took 0.082104 seconds and 5 git commands to generate.