Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kind of dead code can GCC eliminate from the final output?

I have always been told that compiler is sufficient smart to eliminate dead code. Much of the code that I am writing has a lot of information known at compile time but the code has to be written in most generic form. I don't know any assembly so I cannot examine the generated assembly. What kind of code that can be effectively eliminated in the final executable?

Few examples but not limited to

f(bool b){
 if(b){
  //some code
 }else{
  //some code
 }
}
f(true);
//////////////////////////
template<bool b>
f(){
 if(b){
  //some code
 }else{
  //some code
 }
}
f<true>();
///////////////////////////

What if definition of f is in other objective code and the the called f(true) is in main. Will link time optimisation effectively eliminate the dead code? What is the coding style/compiler option/trick to facilitate dead code elimination?

like image 778
leon Avatar asked May 30 '12 02:05

leon


People also ask

What is logically dead code?

Logically dead code are branches of software that cannot be reached given the logical conditions. Finding logically dead code is important because it can indicate the software was not written as originally intended. So simply, a code change made the branch no longer necessary.

Does Java do dead code elimination?

Eclipse compiler for Java (ECJ) is open source incremented compiler. We reform features of ECJ, related to optimization technique called as dead code detection and elimination. ECJ identifies the dead code. We are extending this compiler to eliminate the dead code.

What is the necessity of dead code elimination?

Dead code elimination removes unneeded instructions from the program. Dead code is a section in the source code of a program, which is executed but whose result is never used in any other computation. Dead code execution prevents from wastes of computation time and memory.


2 Answers

Typically, if you're compiling with the -O flag on the following flags are turned on:

      -fauto-inc-dec 
      -fcompare-elim 
      -fcprop-registers 
      -fdce  
      [...]

-fdce stands for Dead Code Elimination. I'd suggest you compile your binaries with and without (i.e. by turning off explicitly) this option to make sure if your binaries are as optimized as you'd like them to be.

Read about the different passes of the compiler:

  • SSA Aggressive Dead Code Elimination. Turned on by the `-fssa-dce' option. This pass performs elimination of code considered unnecessary because it has no externally visible effects on the program. It operates in linear time.

As for helping the linker with dead code elimination go through this presentation. Two major takeaways being:

Compile your modules with -ffunction-sections -fdata-sections – there are no downsides to it!

  • This includes static libraries, not just binaries – make it possible for users of your library to benefit from more efficient dead code removal.
  • Link your binaries with --gc-sections, unless you have to link against nasty third-party static library which uses magic sections.

You may also want to take a look at this GCC bug (to see what chances of optimization may be missed and why).

like image 130
dirkgently Avatar answered Nov 08 '22 13:11

dirkgently


Your example focuses on dead code elimination inside functions.

Another type of dead code elimination, is removal of entire unused symbols (functions or variables) which can be achieved with:

-fdata-sections -ffunction-sections -Wl,--gc-sections

as mentioned at: How to remove unused C/C++ symbols with GCC and ld?

These flags are not enabled in the various GCC -O levels (-O1, -O2, etc) by default.