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)
{
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))
--- /dev/null
+/* { 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;
+}