https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83504
Bug ID: 83504 Summary: incorrect attribute const interpretation on function overloads Product: gcc Version: 8.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- When another overload of a function is subsequently declared const, all versions of GCC treat calls to a prior overload of the same function as const and eliminate multiple calls to it with the same argument, even if reads global memory. As a result, GCC emits the wrong code for uses of such a function as in the test case below. $ cat u.C && gcc -O2 -S -Wall -Wextra -fdump-tree-optimized=/dev/stdout u.C int i; int f (int) { return i; } int __attribute__ ((const)) f (); void g (void) { i = 0; int j = f (0); i = 1; if (j == f (0)) __builtin_abort (); } ;; Function f (_Z1fi, funcdef_no=0, decl_uid=2326, cgraph_uid=0, symbol_order=1) f (int D.2325) { int _2; <bb 2> [local count: 1073741825]: _2 = i; return _2; } ;; Function g (_Z1gv, funcdef_no=1, decl_uid=2329, cgraph_uid=1, symbol_order=2) g () { <bb 2> [local count: 1073741825]: i = 1; return; }