> > I have in the meantime briefly tested following. > > But if you want to the above way, then at least the testcase could be > useful. Though, not sure if the above is all that is needed. Shouldn't > set_const_flag_1 upon TREE_READONLY (node->decl) = 0; also adjust > TREE_TYPE on the function to > tree fntype = TREE_TYPE (node->decl); > TREE_TYPE (node->decl) > = build_qualified_type (fntype, > TYPE_QUALS (fntype) & ~TYPE_QUAL_CONST); > too (perhaps guarded on TREE_READONLY (fntype))? > > 2023-03-16 Jakub Jelinek <ja...@redhat.com> > > PR tree-optimization/106912 > gcc/ > * calls.h (ignore_const_fntype): Declare. > * calls.cc (flags_from_decl_or_type): Ignore TYPE_READONLY > on types if ignore_const_fntype. > * tree-profile.cc: Include calls.h. > (tree_profiling): Set ignore_const_fntype if profile_arc_flag > or flag_test_coverage. > gcc/lto/ > * lto-lang.cc (lto_post_options): Set ignore_const_fntype if > profile_arc_flag or flag_test_coverage and not flag_auto_profile. > gcc/testsuite/ > * gcc.dg/pr106912.c: New test. > > --- gcc/calls.h.jj 2023-01-02 09:32:51.252868185 +0100 > +++ gcc/calls.h 2023-03-16 12:23:51.632460586 +0100 > @@ -134,5 +134,6 @@ extern void maybe_complain_about_tail_ca > > extern rtx rtx_for_static_chain (const_tree, bool); > extern bool cxx17_empty_base_field_p (const_tree); > +extern bool ignore_const_fntype; > > #endif // GCC_CALLS_H > --- gcc/calls.cc.jj 2023-02-21 11:44:48.460464845 +0100 > +++ gcc/calls.cc 2023-03-16 12:27:45.427032110 +0100 > @@ -800,6 +800,13 @@ is_tm_builtin (const_tree fndecl) > return false; > } > > +/* Set if flags_from_decl_or_type should ignore TYPE_READONLY of function > + types. This is used when tree-profile.cc instruments const calls, > + clears TREE_READONLY on FUNCTION_DECLs which have been instrumented, but > + for function types or indirect calls we don't know if the callees have > been > + instrumented or not. */ > +bool ignore_const_fntype; > + > /* Detect flags (function attributes) from the function decl or type node. > */ > > int > @@ -849,7 +856,7 @@ flags_from_decl_or_type (const_tree exp) > } > else if (TYPE_P (exp)) > { > - if (TYPE_READONLY (exp)) > + if (TYPE_READONLY (exp) && !ignore_const_fntype)
I think we want to ignore PURE flag too: if callee modifies counters seen by caller, we need to take that into account when optimizing it. It is not very pretty to have global flag for prifled state, but I can't think of scenario where this would break, so perhaps it is good way to go? Honza