Dead Code Elimination

Dead Code Elimination is a software optimization that removes code that has no effect on the program's outcome. You might ask why someone would write this type of source code, yet even at the source code level, it can readily seep into huge, long-lived projects. Removing such code has multiple advantages: it decreases programme size and allows the current programme to avoid doing unnecessary operations, resulting in faster execution. It can also help with future improvements by making the programme structure simpler.

In the example below, the value assigned to i is never used, and the dead store can be eliminated. The first assignment to global is dead, and the third assignment to global is unreachable; both can be eliminated.

 

int global;

void f ()

{

  int i;

  i = 1;          /* dead store */

  global = 1;     /* dead store */

  global = 2;

  return;

  global = 3;     /* unreachable */

}

 

Below is the code fragment after dead code elimination.

int global;

void f ()

{

  global = 2;

  return;

}

 


 

Example 2:

int foo(void)

 {

   int a = 24;

   int b = 25; /* Assignment to dead variable */

   int c;

   c = a * 4;

   return c;

   b = 24; /* Unreachable code */

   return 0;

 }