user1958850
user1958850

Reputation: 495

If a function is never called does the compiler omit it when creating the program?

When the compiler runs its optimizations, does it omit functions that are never called? I remember reading something that says not to include unnecessary files because it will create needless bloat in a program so I can only assume from that statement that when it's compiled it does include said functions that are never used, but I don't see any logical reason for a compiler not to remove them and I would assume that most modern compilers would probably remove extra unused functions.

Upvotes: 5

Views: 3659

Answers (3)

Omnifarious
Omnifarious

Reputation: 56088

Generally speaking, the answer is 'no'. But some compilers will eliminate some functions in some specific cases. And the linker will also not include functions from statically linked libraries that are in source files in which no function in that file is needed.

One common case that's usually eliminated is a static function that is never called and who's address is never taken. Another is a static function that's inlined everywhere it's called and who's address is never taken.

It's completely up to the compiler.

But, in general, it's very hard to do because the compiler doesn't usually have a view of the whole program to build a comprehensive call graph. And the linker isn't smart enough.

Additionally, some files may end up being parts of libraries. And the whole purpose of libraries is to have a bunch of code that's not called from the library itself. The compiler cannot determine at compile time whether a given piece of code is going into a library or not. So again, that means for functions with 'external linkage', it's up to the linker to elide them if they aren't used.

Upvotes: 7

Joseph Mansfield
Joseph Mansfield

Reputation: 110758

It can do. C++ compilers merely operate under the as-if rule. As long as the resulting program behaves as the standard dictates, the compiler can do whatever it likes to get there.

conforming implementations are required to emulate (only) the observable behavior of the abstract machine

If the function is never called, then of course the compiler could get rid of it, but it's entirely up to the compiler whether it does or not. However, the compilation model of C++ requires that translation units are compiled separately. It is often impossible or very difficult for the compiler to know that the function is never called from any other translation unit.

Upvotes: 3

sharptooth
sharptooth

Reputation: 170549

It will largely depend on the compiler, but yes, in some setups the compiler can completely eliminate the functions that are not called. The compiler must ensure that the function is indeed never called.

Specifically functions that are marked static can be eliminated quite easily - just look in the same translation unit. It gets harder if a function can potentially be called from another translation unit. Visual C++ has a special setting for this, called "function-level linking" and a linker settings for removing unreferenced functions. When these two are used together the unreferenced functions will be eliminated completely.

Upvotes: 4

Related Questions