Compiler Design
Dead Code Elimination
Dead Code Elimination
› Also known as DCE, dead code removal, dead code
stripping, or dead code strip.
› Compiler optimization process to remove code.
What Is Dead Code?!!!
› Dead code is one or more than one code statements,
which are:
• Code that can never be executed (unreachable code).
• Or if executed, their output is never used.
• Code that only affects dead variables (written to, but never read again).
Benefits of Dead Code Elimination
› Two benefits :
1. It shrinks program size.
2. Reduces its running time.
Example of Dead Code
int global;
Void function()
{
int i;
i=1; //dead store//
global=1; //dead store//
global=2;
return;
global=3; //unreachable//
}
Dead Code Elimination
› The code fragment after dead code elimination.
int global;
void function()
{
global=2;
return;
}
Dead Code Elimination via Preprocessor
int main(void)
{
int a = 5;
int b = 6;
int c;
c = a * (b >> 1);
if (0) { /* DEBUG */
printf("%dn", c);
}
return c;
}
Dynamic Dead Code Elimination
› The techniques used to dynamically detect demand,
identify and resolve dependencies, remove conditionally
dead code, and recombine the remaining code at load
or runtime.
› Remove dead code through dead code elimination
at compile time.
› Very rare with languages compiled ahead-of-time.
› language implementations doing just-in-time compilation.
Partially Dead Code
› Value computed by partially dead statement is
sometimes used and sometimes not used.
Another Example of Partial Dead Code
REFERENCE
› http://www.tutorialspoint.com/compiler_design/compile
r_design_code_optimization.htm
› https://en.wikipedia.org/wiki/Dead_code_elimination
› http://www.compileroptimizations.com/category/dead_c
ode_elimination.htm
Dead Code Elimination

Dead Code Elimination

  • 1.
  • 2.
    Dead Code Elimination ›Also known as DCE, dead code removal, dead code stripping, or dead code strip. › Compiler optimization process to remove code.
  • 3.
    What Is DeadCode?!!! › Dead code is one or more than one code statements, which are: • Code that can never be executed (unreachable code). • Or if executed, their output is never used. • Code that only affects dead variables (written to, but never read again).
  • 4.
    Benefits of DeadCode Elimination › Two benefits : 1. It shrinks program size. 2. Reduces its running time.
  • 5.
    Example of DeadCode int global; Void function() { int i; i=1; //dead store// global=1; //dead store// global=2; return; global=3; //unreachable// }
  • 6.
    Dead Code Elimination ›The code fragment after dead code elimination. int global; void function() { global=2; return; }
  • 7.
    Dead Code Eliminationvia Preprocessor int main(void) { int a = 5; int b = 6; int c; c = a * (b >> 1); if (0) { /* DEBUG */ printf("%dn", c); } return c; }
  • 8.
    Dynamic Dead CodeElimination › The techniques used to dynamically detect demand, identify and resolve dependencies, remove conditionally dead code, and recombine the remaining code at load or runtime. › Remove dead code through dead code elimination at compile time. › Very rare with languages compiled ahead-of-time. › language implementations doing just-in-time compilation.
  • 9.
    Partially Dead Code ›Value computed by partially dead statement is sometimes used and sometimes not used.
  • 10.
    Another Example ofPartial Dead Code
  • 11.