Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Dead-code elimination
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Examples== Consider the following example written in [[C (programming language)|C]]. <syntaxhighlight lang="c"> 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; } </syntaxhighlight> Simple analysis of the uses of values would show that the value of <code>b</code> after the first assignment is not used inside <code>foo</code>. Furthermore, <code>b</code> is declared as a local variable inside <code>foo</code>, so its value cannot be used outside <code>foo</code>. Thus, the variable <code>b</code> is ''dead'' and an optimizer can reclaim its storage space and eliminate its initialization. Furthermore, because the first return statement is executed unconditionally and there is no label after it which a "goto" could reach, no feasible execution path reaches the second assignment to <code>b</code>. Thus, the assignment is ''unreachable'' and can be removed. If the procedure had a more complex [[control flow]], such as a label after the return statement and a <code>goto</code> elsewhere in the procedure, then a feasible execution path might exist to the assignment to <code>b</code>. Also, even though some calculations are performed in the function, their values are not stored in locations accessible outside the [[scope (programming)|scope]] of this function. Furthermore, given the function returns a static value (96), it may be simplified to the value it returns (this simplification is called [[constant folding]]). Most advanced compilers have options to activate dead-code elimination, sometimes at varying levels. A lower level might only remove instructions that cannot be executed. A higher level might also not reserve space for unused variables. A yet higher level might determine instructions or functions that serve no purpose and eliminate them. A common use of dead-code elimination is as an alternative to optional code inclusion via a [[preprocessor]]. Consider the following code. <syntaxhighlight lang="c"> int main(void) { int a = 5; int b = 6; int c; c = a * (b / 2); if (0) { /* DEBUG */ printf("%d\n", c); } return c; } </syntaxhighlight> Because the expression 0 will always evaluate to [[False (logic)|false]], the code inside the if statement can never be executed, and dead-code elimination would remove it entirely from the optimized program. This technique is common in [[debugging]] to optionally activate blocks of code; using an optimizer with dead-code elimination eliminates the need for using a [[preprocessor]] to perform the same task. In practice, much of the dead code that an optimizer finds is created by other transformations in the optimizer. For example, the classic techniques for operator [[strength reduction]] insert new computations into the code and render the older, more expensive computations dead.<ref name="Allen_Cocke_Kennedy_1981" /> Subsequent dead-code elimination removes those calculations and completes the effect (without complicating the strength-reduction algorithm). Historically, dead-code elimination was performed using information derived from [[data-flow analysis]].<ref name="Kennedy_1981" /> An algorithm based on [[static single-assignment form]] (SSA) appears in the original journal article on ''SSA'' form by Ron<!-- K.--> Cytron et al.<ref name="Cytron_Ferrante_Rosen_Zadeck_1991" /> Robert<!-- Allen --> Shillingsburg (aka Shillner) improved on the algorithm and developed a companion algorithm for removing useless control-flow operations.<ref name="Cooper_Torczon_2003" />
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)