user649558
user649558

Reputation: 57

compiler optimization for non-called function

I have a function in legacy code, that is no longer being called.

My question is: would the compiler optimize for a function which is not being called, or would the executable file include the code of that function?

Upvotes: 2

Views: 188

Answers (4)

Mike Rossi
Mike Rossi

Reputation: 291

Dead code removal is usually done by the linker (as the compiler does not have a clue as to which functions are used or not). However, sometimes the compiler itself can remove functions that have static linkage.

This is because by default all functions have external linkage. The reserved term "extern" which is used while declaring external linkage variables could (and in fact is) omitting while declaring functions. So if those are not declared static those could be used elsewhere and compiler don't know nothing about it.

Also, GCC (if that's what you're using) has SSA Aggressive Dead Code Elimination (the -fssa-dce flag) which can help with removal of unneeded code.

If you're looking for something to remove dead functions or sections then you can use gcov http://gcc.gnu.org/onlinedocs/gcc/Gcov-Intro.html#Gcov-Intro

Upvotes: 0

Joseph Lormand
Joseph Lormand

Reputation: 694

There's a good chance it's in the file because of the possibility of run-time access through dynamic means. Such as a concatenated string yielding a plethora of different function names and being used to access them.

Although this type of implementation is rare, it is still a possibility and thus the code must remain available.

Upvotes: 0

justin
justin

Reputation: 104708

possibly. it's implementation, toolset, and build parameter-defined.

altering your optimizations settings, linker flags, and the visibility (static/private/extern/internal/anonymous namespace) can increase the probability that it will be omitted from the final executable.

Upvotes: 3

Leonid Volnitsky
Leonid Volnitsky

Reputation: 9144

If it is compiled to object file, than compiler does not know if your function will be used or not. Unless you are using link time optimization (lto) or whole program optimization options. If function is in header - you can make it static, so that compiler can optimize it away.

Upvotes: 0

Related Questions