https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127825
Bug ID: 127825
Summary: (pure|const) attributed functions are not CSE if
inlined
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: marco.rubini08 at gmail dot com
Target Milestone: ---
GCC can simplify common subexpressions involving pure and const attributed
functions, but some optimizations are missed if the function code is inlined in
the caller.
Example code on Godbolt: https://godbolt.org/z/v68scEbKb
Example copied verbatim, add/remove the gnu::noinline attribute and see the
difference in code generation with optimizations enabled.
If the function is inlined, GCC reloads the x variable unnecessarily.
It is an optimization bug because inlining shouldn't reduce optimization
opportunities, at least in simplified cases.
=====
extern int x;
[[gnu::noinline, gnu::const]]
inline int get_x()
{ return x; }
void opaque();
int foo()
{
int p = get_x();
opaque();
int q = get_x();
return (p ^ q);
}
=====