https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68219
Bug ID: 68219 Summary: ICF could fold functions called via a table of function pointers Product: gcc Version: unknown Status: UNCONFIRMED Severity: enhancement Priority: P3 Component: ipa Assignee: unassigned at gcc dot gnu.org Reporter: jay.foad at gmail dot com Target Milestone: --- Consider this simplified example: $ cat x.cpp namespace { template<int N> int f() { return 0; } } int g(int n) { static int (*const t[4])() = { f<0>, f<1>, f<2>, f<3> }; return t[n](); } I can see from the output of "gcc -S -o - -O3 x.cpp" that GCC generates four separate but identical function bodies for f<0>, f<1>, f<2> and f<3>. It would be nice if it could generate just one copy. (I realise that there might be concerns about the standard specifying that different functions should have different addresses; but in a case like this the compiler should be able to prove that the addresses of the functions don't escape.)