aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Biener <rguenther@suse.de>2022-12-07 10:26:01 +0100
committerRichard Biener <rguenther@suse.de>2022-12-07 11:28:00 +0100
commit45e09c2eb9c2bdd34ef777e06ddc9908dd0664f9 (patch)
treea6b559ea9c8b4decd2d5c1efbac5b7be49a3ac01
parent3a1a141f79c83ad38f7db3a21d8a4dcfe625c176 (diff)
downloadgcc-45e09c2eb9c2bdd34ef777e06ddc9908dd0664f9.zip
gcc-45e09c2eb9c2bdd34ef777e06ddc9908dd0664f9.tar.gz
gcc-45e09c2eb9c2bdd34ef777e06ddc9908dd0664f9.tar.bz2
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.
-rw-r--r--gcc/ipa-pure-const.cc5
-rw-r--r--gcc/testsuite/gcc.dg/pr105676.c14
2 files changed, 17 insertions, 2 deletions
diff --git a/gcc/ipa-pure-const.cc b/gcc/ipa-pure-const.cc
index 572a6da..0b748ee 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 0000000..077fc18
--- /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;
+}