Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do compilers remove unused functions, definitions, variables, macros, includes etc

Sometimes when programming, we define or declare functions, definitions, variables, macros includes and data structures. but never use those after that.

  1. Do those unused resources automatically get remove by the compiler (modern compilers capable of optimizing)?
  2. Is there a way to recognize those?
like image 832
Nayana Adassuriya Avatar asked Apr 03 '13 08:04

Nayana Adassuriya


2 Answers

It depends:

Macros are converted to program text by the compiler. They don't represent anything other than the text that replaces them, and don't live beyond compile time (unless... see below).

Local variables and such are likely removed, if they don't have a non-trivial constructor or destructor. (You don't want something like scoped_lock removed just because you don't reference it later in the code.) The same holds for variables and functions with internal linkage (e.g. defined at namespace scope with the keyword static). In most cases, however, the resources needed for such objects is minimal, and the savings negligible.

Functions are a bit different, and depend. A virtual function will generally be considered "used" if there is ever an instance of its type, and it will almost certainly be present, even if it is never called.

Beyond that (and this applies to global variables as well): it's up to the linker. The granularity of most linkers is the object file which results from compiling a "translation unit": that object file either is or is not part of your program. If you tell the linker to incorporate the object file, then you should get everything that is in it. If you put the object file in a (static) library, and tell the linker to use this, then the linker will incorporate the object file into your program if and only if it resolves an otherwise unresolved external. But if it incorporates the object file, it generally will incorporate all of it. (Any good library will put each non-virtual function in a separate object file, so you don't get more than you need.)

In this regard, DLL's behave like object files (despite their name). If you link your object files into a DLL, the program which uses it will get all of the DLL, or none.

Finally: although not part of your program, the object files and the final executable will often contain symbolic information; the best systems will even maintain information concerning macros, so that the debugger can display things the way you wrote them. (How far it can do this with macros is debatable.)

like image 185
James Kanze Avatar answered Oct 26 '22 07:10

James Kanze


If either the compiler or the linker can see that there are no references to C functions or C variables, they can (and usually do) remove those unused things.

Unused macro definitions don't make it into the compiled code at all. And the same holds for typedefs and the like.

It is, however, harder to remove unused code and data implemented in the assembly parts of programs.

And it's not always obvious to the compiler if some referenced variable or some code is ever going to be used or executed.

So, yeah, these days, most of clearly unused stuff is removed.

like image 24
Alexey Frunze Avatar answered Oct 26 '22 06:10

Alexey Frunze