Optimisation: What is Dead Code Elimination

This is one of the simpler optimisations. It simply removes code that is not tied to any side effect or return value.

Dead code is any code which is written to but never read or exists in an unreachable position.

Example:

if(false)
{
int never = 20;
never += 10;
}

int x = 10;
int y = 30
int z = y + 10;
return z;

In this example the code within the ‘if’ statement will never be reachable due to the condition being false, so that code will be removed.

Following on, the variable ‘x’ is never used. It only has a value written to it and is never read. So it is safe to remove this variable from the code.

This results in:

int y= 30;
int z = y + 10;
return z;

Much less work for the computer!