http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42970
--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> --- Consider LTO. Note that the issue is that while we remove assignments to unused variables from calls at the caller side we never remove a never used return value from a return statement. This keeps the computation of 'counter' and 'counter' itself life and thus weakens IPA reference analysis. For the following testcase we should be able to remove the call to quantum_gate_counter _without using the trick of inlining it_. Basically some IPA optimization should figure out that 'return counter' is dead and it and all producing stmts can be elimiated. You cannot elimiate the return stmt if you don't change the functions signature, but if you don't want to do that you can for example just return 0. It get's tricky with the requirements in the description but the idea was that the early IPA-SRA pass would clone the function for "unused return value", local passes then would eliminate all stmts in the clone making it const and thus we win. The tricky part here is that early opts don't see the whole program so that quantum_gate_counter is exported (and we share the 'counter' variable with the original function which cannot be eliminated). static int __attribute__((noinline,noclone)) quantum_gate_counter(int inc) { static int counter = 0; if(inc > 0) counter += inc; else if(inc < 0) counter = 0; return counter; } int main() { quantum_gate_counter (1); return 0; }