Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a C++ compiler/linker allowed to remove unused methods?

Is a C++ compiler or linker (by any C++ standard) allowed to remove an unused method? Compilers seem to be allowed to remove unused static functions, linkers are allowed to remove unused functions. But i have found no information what it looks like for methods of classes. When the method is virtual this gets really interesting.

like image 453
versat Avatar asked Feb 26 '16 09:02

versat


3 Answers

Yes.

If the method is unused, then there is no way to tell it has been removed - so a linker can do so. Note that taking the address of a method may well count as "using" the method - not just actually calling it.

Linkers are quite likely to remove non-virtual member functions (it's easy and saves space).

They could remove unused virtual functions, but the compiler would have to add a lot of information about which virtual functions it was calling so that the linker could remove unused ones (and possibly compact the vtable). In practise, I don't think linkers do this because the gain is probably small and the amount of development effort required rather large.

like image 194
Martin Bonner supports Monica Avatar answered Nov 20 '22 15:11

Martin Bonner supports Monica


The C++ standard works on a more abstract level. It does not require a C++ implementation to actually be composed of individual tools like a compiler and a linker.

Incidentally, I just searched the draft PDF on my machine, and there is only one instance of the word "linker" in the entire 1368-pages document, and even that one is just in a footnote on page 22 about character sets.

What the standard actually does talk about is the so-called "as-if" rule.

Citing §1.9:

(...) conforming implementations are required to emulate (only) the observable behavior of the abstract machine as explained below.

In a footnote for that sentence, it further says:

This provision is sometimes called the “as-if” rule, because an implementation is free to disregard any requirement of this International Standard as long as the result is as if the requirement had been obeyed, as far as can be determined from the observable behavior of the program.

If a function is not used anywhere in your program in any way, then it cannot have any impact on observable behaviour. The "as-if" rule alone thus gives a compiler or linker full liberty to remove it from the executable result.

like image 36
Christian Hackl Avatar answered Nov 20 '22 15:11

Christian Hackl


Since the C++ standard says nothing about this, it is probably more accurate to say it does not prevent removal of unused member functions, than to say it permits their removal. But it is also equally valid to say it does not require their removal. That, incidentally, is true for all functions.

If programs in the build chain (compiler, linker, etc) can detect that any symbol (e.g. static or non-static member function) is not being used, it can safely remove them.

The harder practical problem is probably detecting that a non-static member function is not being called. Even more so, if it is virtual, since the static type of an object is what determines which override of a virtual function is called. Detecting such cases is not impossible in principle, but would require a fair amount of analysis.

In the end it will come down to quality of implementation of the build chain, since the standard requires nothing in particular. That comes down to whether the vendor chooses to implement such optimisations. And, since this is not a behaviour that many developers actually seek (the most likely people to seek it will have a premature optimisation fetish) not many would.

like image 2
Peter Avatar answered Nov 20 '22 14:11

Peter