Unreachable code

From Wikipedia, the free encyclopedia
Jump to: navigation, search

Contents

Unreachable code is a computer programming term for code in the source code of a program which can never be executed because there exists no control flow path to the code from the rest of the program.[1]

Unreachable code is sometimes also called dead code[citation needed], although dead code may also refer to code that is executed but has no effect on the output of a program.

Unreachable code is generally considered undesirable for a number of reasons, including:

  • Occupies unnecessary memory
  • Causes unnecessary caching of instructions into the CPU instruction cache - which also decreases data locality.
  • From the perspective of program maintenance; time and effort may be spent maintaining and documenting a piece of code which is in fact unreachable, hence never executed.

[edit] Causes

The existence of unreachable code can be due to various factors, such as:

  • programming errors in complex conditional branches;
  • a consequence of the internal transformations performed by an optimizing compiler;
  • incomplete testing of a new or modified program that failed to test the unreachable code;
  • while fixing one bug, a programmer created a second bug that bypassed the unreachable code and was not discovered during testing;
  • obsolete code that a programmer decided not to delete because it was intermingled with functional code;
  • obsolete code that a programmer forgot to delete;
  • previously useful code that will never be reached, because future input data will never cause that code to be executed;
  • complex obsolete code that was intentionally retained but made unreachable so that it could be revived later if needed;
  • debugging constructs and vestigial development code which have yet to be removed from a program.

In the latter five cases, code which is currently unreachable is there as part of a legacy, i.e. code that was once useful but is no longer used or required.

[edit] Examples

Consider the following fragment of C code:

 int foo (int iX, int iY)
 {
  return iX + iY;
  int iZ = iX*iY;
 }

The definition int iZ = iX*iY; is never reached as the function returns before the definition is reached. Therefore the definition of iZ can be discarded.

[edit] Analysis

Detecting unreachable code is a form of static analysis and involves performing control flow analysis to find any code that will never be executed regardless of the values of variables and other conditions at run time. In some languages (e.g. Java [2] ) some forms of unreachable code are explicitly disallowed. The optimization that removes unreachable code is known as dead code elimination.

Code may become unreachable as a consequence of the internal transformations performed by an optimizing compiler (e.g., common subexpression elimination).

In practice the sophistication of the analysis performed has a significant impact on the amount of unreachable code that is detected. For example, constant folding and simple flow analysis shows that the statement xyz in the following code is unreachable:

 int iN = 2 + 1;
 
 if (iN == 4)
 {
   xyz
 }

However, a great deal more sophistication is needed to work out that the statement xyz is unreachable in the following code:

 double dX = sqrt(2);
 
 if (dX > 5)
 {
   xyz
 }

The unreachable code elimination technique is in the same class of optimizations as dead code elimination and redundant code elimination.

[edit] Unreachability vs. profiling

In some cases, a practical approach may be a combination of simple unreachability criteria and use of a profiler to handle the more complex cases. Profiling in general can not prove anything about the unreachability of a piece of code, but may be a good heuristic for finding potentially unreachable code. Once a suspect piece of code is found, other methods, such as a more powerful code analysis tool, or even analysis by hand, could be used to decide whether the code is truly unreachable.

[edit] References

  1. ^ Debray, S. K.; Evans, W., Muth, R., and De Sutter, B. (2000-03). "Compiler techniques for code compaction." (in English) (PDF). Volume 22, issue 2. New York, USA: ACM Transactions on Programming Languages & Systems (TOPLAS). Bibcode 378-415. http://doi.acm.org/10.1145/349214.349233. 
  2. ^ "Java Language Specification". http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.21. 
  • Appel, A. W. 1998 Modern Compiler Implementation in Java. Cambridge University Press.
  • Muchnick S. S. 1997 Advanced Compiler Design and Implementation. Morgan Kaufmann.

[edit] See also

Personal tools
Namespaces
Variants
Actions
Navigation
Interaction
Toolbox
Print/export
Languages