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
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!
{{Short description|Compiler optimization to remove code which does not affect the program results}} {{Use dmy dates|date=April 2019|cs1-dates=y}} {{Use list-defined references|date=December 2021}} In [[compiler theory]], '''dead-code elimination''' ('''DCE''', '''dead-code removal''', '''dead-code stripping''', or '''dead-code strip''') is a [[compiler optimization]] to remove [[dead code]] (code that does not affect the program results). Removing such code has several benefits: it shrinks [[executable|program]] size, an important consideration in some contexts, it reduces resource usage such as the number of bytes to be transferred<ref>Malavolta, Ivano et al. “JavaScript Dead Code Identification, Elimination, and Empirical Assessment.” IEEE transactions on software engineering 49.7 (2023): 3692–3714. Web.</ref> and it allows the running program to avoid executing irrelevant [[CPU instruction|operations]], which reduces its [[execution (computing)|running time]]. It can also enable further optimizations by simplifying program structure. Dead code includes code that can never be executed ([[unreachable code]]), and code that only affects [[dead variable]]s (written to, but never read again), that is, irrelevant to the program. ==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" /> ==Dynamic dead-code elimination== Dead code is normally considered dead ''unconditionally''. Therefore, it is reasonable attempting to remove dead code through dead-code elimination at [[compile time]]. However, in practice it is also common for code sections to represent dead or unreachable code only ''under certain conditions'', which may not be known at the time of compilation or assembly. Such conditions may be imposed by different [[runtime environment]]s (for example different versions of an operating system, or different sets and combinations of drivers or services loaded in a particular target environment), which may require different sets of special cases in the code, but at the same time become conditionally dead code for the other cases.<ref name="Paul_2002_DDCE" /><ref name="Paul_2002_DDCE2" /> Also, the software (for example, a driver or resident service) may be configurable to include or exclude certain features depending on user preferences, rendering unused code portions useless in a particular scenario.<ref name="Paul_2002_DDCE" /><ref name="Paul_2002_DDCE2" /> While modular software may be developed to dynamically load libraries on demand only, in most cases, it is not possible to load only the relevant routines from a particular library, and even if this would be supported, a routine may still include code sections which can be considered dead code in a given scenario, but could not be ruled out at compile time, already. The techniques used to dynamically detect demand, identify and resolve dependencies, remove such conditionally dead code, and to recombine the remaining code at [[load time|load]] or [[runtime (computing)|runtime]] are called '''dynamic dead-code elimination'''<ref name="Thammanur_2001_JIT" /><ref name="Kubice_2024" /><ref name="Conway_1995_DDCE" /> or '''dynamic dead-instruction elimination'''.<ref name="Butts_2002" /> Most programming languages, compilers and operating systems offer no or little more support than [[dynamic loading]] of libraries and [[late linking]], therefore software utilizing dynamic dead-code elimination is very rare in conjunction with languages [[ahead-of-time compilation|compiled ahead-of-time]] or written in [[assembly language]].<ref name="Paul_1997_FreeKEYB" /><ref name="Paul_2006_FreeKEYB" /><ref name="Paul_2001_DDCE" /> However, language implementations doing [[just-in-time compilation]] may dynamically optimize for dead-code elimination.<ref name="Conway_1995_DDCE" /><ref name="IBM_2002_Java" /><ref name="Polito_2015" /> Although with a rather different focus, similar approaches are sometimes also utilized for [[dynamic software updating]] and [[hot patching]]. ==See also== * [[Redundant code]] * [[Simplification (symbolic computation)]] * [[Partial-redundancy elimination]] * [[Conjunction elimination]] * [[Dynamic software updating]] * [[Dynamic coupling (computing)]] * [[Self-relocation]] * [[Software cruft]] * [[Tree shaking]] * [[Post-pass optimization]] * [[Profile-guided optimization]] * [[Superoptimizer]] * [[Function multi-versioning]] ==References== {{reflist|refs= <ref name="Allen_Cocke_Kennedy_1981">{{cite book |author-first1=Frances |author-last1=Allen |author-first2=John |author-last2=Cocke |author-link2=John Cocke (computer scientist) |author-first3=Ken |author-last3=Kennedy |author-link3=Ken Kennedy (computer scientist) |chapter=Reduction of Operator Strength |title=Program Flow Analysis: Theory & Application |editor-first1=Neil D. |editor-last1=Jones |editor-link1=Neil D. Jones |editor-first2=Steven Stanley |editor-last2=Muchnick |editor-link2=Steven Stanley Muchnick |publisher=[[Prentice-Hall]] |date=June 1981 |isbn=0-13729681-9}}</ref> <ref name="Kennedy_1981">{{cite book |author-first=Ken |author-last=Kennedy |author-link=Ken Kennedy (computer scientist) |chapter=A Survey of Data-flow Analysis Techniques |title=Program Flow Analysis: Theory & Application |editor-first1=Neil D. |editor-last1=Jones |editor-link1=Neil D. Jones |editor-first2=Steven Stanley |editor-last2=Muchnick |editor-link2=Steven Stanley Muchnick |publisher=[[Prentice-Hall]] |date=June 1981 |isbn=0-13729681-9}}</ref> <ref name="Cytron_Ferrante_Rosen_Zadeck_1991">{{cite book |author-first1=Ron K. |author-last1=Cytron |author-first2=Jeanne |author-last2=Ferrante |author-link2=Jeanne Ferrante |author-first3=Barry K. |author-last3=Rosen |author-first4=F. Kenneth |author-last4=Zadeck |title=Efficiently Computing Static Single Assignment Form and the Program Dependence Graph |id=[[ACM TOPLAS]] 13(4) |date=1991}}</ref> <ref name="Cooper_Torczon_2003">{{cite book |author-first1=Keith D. |author-last1=Cooper |author-link1=Keith D. Cooper |author-first2=Linda |author-last2=Torczon |title=Engineering a Compiler |publisher=[[Morgan Kaufmann]] |date=2003 |orig-year=2002-01-01 |pages=498ff |isbn=978-1-55860698-2}}</ref> <ref name="Conway_1995_DDCE">{{cite newsgroup |title=Cyclic data structures |author-first=Andrew |author-last=Conway |date=1995-12-04 |newsgroup=comp.lang.functional |url=https://groups.google.com/d/msg/comp.lang.functional/zFsRAGroeVM/2L5TIgcG-1IJ |access-date=2017-07-03 |url-status=live |archive-url=https://archive.today/20170909085605/https://groups.google.com/forum/%23!msg/comp.lang.functional/zFsRAGroeVM/2L5TIgcG-1IJ |archive-date=2017-09-09 |quote=[…] [[Lazy evaluation]] is basically [[dynamic dead-code elimination|dynamic dead code elimination]]. […]}} (NB. Possibly the first public use of the term ''dynamic dead-code elimination'', though only conceptually and with a focus on lazy evaluation in [[functional language]]s.)</ref> <ref name="Paul_2001_DDCE">{{cite newsgroup |title=[ANN] FreeDOS beta 6 released |author-first=Matthias R. |author-last=Paul |date=2001-04-10 |language=German |newsgroup=de.comp.os.msdos |url=https://groups.google.com/d/msg/de.comp.os.msdos/qCZs8p6MyPQ/Pksl0Pv6qM8J |access-date=2017-07-02 |url-status=live |archive-url=https://archive.today/20170909084250/https://groups.google.com/forum/%23!msg/de.comp.os.msdos/qCZs8p6MyPQ/Pksl0Pv6qM8J |archive-date=2017-09-09 |quote=[…] brandneue[s] Feature, der [[dynamic dead-code elimination|dynamischen Dead-Code-Elimination]], die die jeweils notwendigen Bestandteile des Treibers erst zum Installationszeitpunkt zusammenbastelt und reloziert, so daß keine ungenutzten Code- oder Datenbereiche mehr resident bleiben (z.B. wenn jemand ein bestimmtes FreeKEYB-Feature nicht benötigt). […]}} (NB. This represents the first known implementation of byte-level granular ''dynamic dead-code elimination'' for software [[assembly language|assembled]] or [[ahead-of-time compilation|compiled ahead-of-time]].)</ref> <ref name="Paul_2002_DDCE">{{cite web |title=[fd-dev] Ctrl+Alt+Del |author-first=Matthias R. |author-last=Paul |date=2002-04-03 |orig-year=2001-06-18 |work=freedos-dev |url=https://marc.info/?l=freedos-dev&m=101783474625117 |access-date=2017-09-09 |url-status=live |archive-url=https://archive.today/20170909084942/https://marc.info/?l=freedos-dev&m=101783474625117 |archive-date=2017-09-09 |quote=[…] any of the […] options can be "permanently" excluded at installation time (will also save the memory for the corresponding code excerpts due to our [[Dynamic Dead Code Elimination]]), or it can be disabled or enabled at any later time via API functions in case someone wants to keep a user from being able to reboot the machine. […] we are considering to add more synchronous cache flush calls […] Due to our Dynamic Dead Code Elimination method this would not cause any kind of bloating when not needed in a particular target configuration as a particular cache flush call would be included in FreeKEYB's runtime image only if the corresponding disk cache is loaded as well or FreeKEYB was told by command line switches to load the corresponding support.}}</ref> <ref name="Paul_2002_DDCE2">{{cite web |title=[fd-dev] Ctrl+Alt+Del |author-first=Matthias R. |author-last=Paul |date=2002-04-06 |work=freedos-dev |url=https://marc.info/?l=freedos-dev&m=101807225917568&w=2 |access-date=2019-04-27 |url-status=live |archive-url=https://archive.today/20190427131940/https://marc.info/?l=freedos-dev&m=101807225917568&w=2 |archive-date=2019-04-27 |quote=[…] FreeKEYB builds the driver's runtime image at initialization time depending on the type of machine it is being loaded on, the type of keyboard, layout, country and code page used, the type of mouse and video adapter(s) installed, the other drivers loaded on that system, the operating system and the load and relocation method(s) used, the individual features included, and the configuration options specified in the command line. Due to the large number of command line switches and options supported […] (around fifty switches […] with multiple possible settings) there is a high number of feature combinations with uncountable dependencies […] resulting in […] endless number of […] different target images. FreeKEYB's Dynamic Dead Code Elimination technique manages to resolve […] these […] dependencies and […] remove dead code and data […] is not restricted to […] include or exclude a somewhat limited number of modules or whole sub-routines and fix up some dispatch tables as in classical TSR programming, but […] works […] at […] byte level […] able to remove […] individual instructions in the middle of larger routines […] distributed all over the code to handle a particular case or support a specific feature […] special tools are used to analyze the code […] and create […] fixup tables […] automated […] using conditional defines […] to declare the various cases […] not only optional at assembly time but at initialization time […] without the […] overhead of having at least some amount of dead code left in the runtime image […] to keep track of all the dependencies between […] these conditionals, dynamically build and relocate the runtime image, fix up all the references between these small, changing, and moving binary parts […] still allowing to use the tiny .COM/.SYS style […] model […] is done at initialization time […] API to import and export object structures between FreeKEYB and the calling application […] to transparently resize and move them internally […] at runtime […]}}</ref> <ref name="Paul_1997_FreeKEYB">{{citation |title=FreeKEYB - Enhanced DOS keyboard and console driver |edition=v6.5 |author-first1=Matthias R. |author-last1=Paul |author-first2=Axel C. |author-last2=Frinke |type=User Manual |date=1997-10-13 |orig-year=first published 1991}} [https://web.archive.org/web/20190309194320/http://sta.c64.org/dosprg/fk657p1.zip<!-- FreeKEYB 6.57p1 Beta as of 2004-08-17 with outdated and incomplete documentation -->] (NB. FreeKEYB is a [[Unicode]]-based dynamically configurable successor of K3PLUS supporting most [[keyboard layout]]s, [[code page]]s, and [[List of country calling codes|country code]]s. Utilizing an off-the-shelf [[macro assembler]] as well as a framework of automatic pre- and post-processing analysis tools to generate dependency and [[code morphing]] [[meta data]] to be embedded into the [[executable file]] alongside the [[binary code]] and a self-discarding, [[instruction relaxation|relaxing]] and [[relocating loader]], the driver implements byte-level granular [[dynamic dead-code elimination]] and [[relocation (computing)|relocation]] techniques at [[load-time]] as well as [[self-modifying code]] and reconfigurability at [[runtime (computing)|run-time]] to minimize its memory footprint downto close the [[canonical form]] depending on the underlying hardware, operating system, and driver configuration as well as the selected feature set and locale (about sixty configuration switches with hundreds of options for an almost unlimited number of possible combinations). This complexity and the dynamics are hidden from users, who deal with a single executable file just like they would do with a conventional driver. K3PLUS was an extended keyboard driver for [[DOS]] widely distributed in Germany at its time, with adaptations to a handful of other European languages available. It supported a sub-set of features already, but did not implement dynamic dead-code elimination.)</ref> <ref name="Paul_2006_FreeKEYB">{{citation |title=FreeKEYB - Advanced international DOS keyboard and console driver |edition=v7 preliminary |author-first1=Matthias R. |author-last1=Paul |author-first2=Axel C. |author-last2=Frinke |type=User Manual |date=2006-01-16}}</ref> <ref name="Butts_2002">{{cite web |title=Dynamic Dead-Instruction Detection and Elimination |author-first1=J. Adam |author-last1=Butts |author-first2=Guri |author-last2=Sohi |publisher=Computer Science Department, [[University of Wisconsin-Madison]] |date=October 2002 |id=[[ACM ASPLOS|ASPLOS]] X [[Association for Computing Machinery|ACM]] 1-58113-574-2/02/0010 |location=San Jose, CA, USA |url=https://ftp.cs.wisc.edu/sohi/papers/2002/deadinstructions.asplos.pdf |access-date=2017-06-23 |url-status=live |archive-url=https://archive.today/20190420144100/http://ftp.cs.wisc.edu/sohi/papers/2002/deadinstructions.asplos.pdf |archive-date=2019-04-20}}</ref> <ref name="Kubice_2024">{{cite web |title=Dynamic Dead Code Elimination: Optimizing for Flexibility |author-first=Jan |author-last=Kubice |date=2024-10-17 |url=https://www.teachit.cz/tutorial.php?id=50&title=Dynamic+Dead+Code+Elimination%3A+Optimizing+for+Flexibility+%5BEN%5D}}</ref> <ref name="IBM_2002_Java">{{cite book |title=Intentia Movex Java on the IBM iSeries Server - An Implementation Guide - Overview of Movex Java on the iSeries server - Movex Java on iSeries installation and configuration - Operational tips and techniques |chapter=Chapter 5. Java overview and iSeries implementation - 5.1.1. Miscellaneous components |author-first1=Yessong |author-last1=Johng |author-first2=Per |author-last2=Danielsson |author-first3=Per |author-last3=Ehnsiö |author-first4=Mats |author-last4=Hermansson |author-first5=Mika |author-last5=Jolanki |author-first6=Scott |author-last6=Moore |author-first7=Lars |author-last7=Strander |author-first8=Lars |author-last8=Wettergren |page=41 |series=Red Books |publisher=[[IBM Corp.]] |date=2002-11-08 |id=SG24-6545-00 |isbn=0-73842461-7 |url=https://www.redbooks.ibm.com/redbooks/pdfs/sg246545.pdf |access-date=2019-04-20 |url-status=live |archive-url=https://web.archive.org/web/20131008194558/http://www.redbooks.ibm.com/redbooks/pdfs/sg246545.pdf |archive-date=2013-10-08}} [https://www.redbooks.ibm.com/abstracts/sg246545.html<!-- https://web.archive.org/web/20190420150738/https://www.redbooks.ibm.com/abstracts/sg246545.html -->]</ref> <ref name="Polito_2015">{{cite web |title=Virtualization Support for Application Runtime Specialization and Extension - Programming Languages |author-first=Guillermo |author-last=Polito |publisher=[[Universite des Sciences et Technologies de Lille]] |date=2015 |id=HAL Id: tel-01251173 |pages=111–124 |url=https://hal.inria.fr/tel-01251173/file/Poli15Thesis.pdf |access-date=2017-06-23 |url-status=live |archive-url=https://web.archive.org/web/20170623021500/https://hal.inria.fr/tel-01251173/file/Poli15Thesis.pdf |archive-date=2017-06-23}}</ref> <ref name="Thammanur_2001_JIT">{{cite thesis |title=A Just in Time Register Allocation and Code Optimization Framework for Embedded Systems |author-first=Sathyanarayan |author-last=Thammanur |date=2001-01-31 |type=MS thesis |id=ucin982089462 |publisher=[[University of Cincinnati]], Engineering: Computer Engineering |url=http://rave.ohiolink.edu/etdc/view?acc_num=ucin982089462}} [https://etd.ohiolink.edu/!etd.send_file?accession=ucin982089462&disposition=inline] {{Webarchive|url=https://web.archive.org/web/20190728221938/https://etd.ohiolink.edu/!etd.send_file?accession=ucin982089462&disposition=inline |date=2019-07-28}}[https://etd.ohiolink.edu/!etd.send_file?accession=ucin982089462&disposition=attachment] {{Webarchive|url=https://web.archive.org/web/20190728221727/https://etd.ohiolink.edu/!etd.send_file?accession=ucin982089462&disposition=attachment |date=2019-07-28}}</ref> }} ==Further reading== * {{cite journal |title=Partial dead code elimination using slicing transformations |journal=Proceedings of the ACM SIGPLAN 1997 Conference on Programming Language Design and Implementation (PLDI '97) |author-first1=Rastislav |author-last1=Bodík |author-first2=Rajiv |author-last2=Gupta |author-link2=Rajiv Gupta (technocrat) |date=June 1997 |pages=682–694}} * {{cite book |author-first1=Alfred Vaino |author-last1=Aho |author-link1=Alfred Vaino Aho |author-first2=Ravi |author-last2=Sethi |author-link2=Ravi Sethi |author-first3=Jeffrey David |author-last3=Ullman |author-link3=Jeffrey David Ullman |title=Compilers - Principles, Techniques and Tools |url=https://archive.org/details/compilersprincip00ahoa |url-access=registration |publisher=[[Addison Wesley Publishing Company]] |date=1986 |isbn=0-201-10194-7}} * {{cite book |author-first=Steven Stanley |author-last=Muchnick |author-link=Steven Stanley Muchnick |title=Advanced Compiler Design and Implementation |publisher=[[Morgan Kaufmann Publishers]] |date=1997 |isbn=1-55860-320-4 |url-access=registration |url=https://archive.org/details/advancedcompiler00much }} * {{cite book |author-first1=Dick |author-last1=Grune |author-link1=Dick Grune |author-first2=Henri Elle |author-last2=Bal |author-link2=Henri Elle Bal |author-first3=Ceriel J. H. |author-last3=Jacobs |author-first4=Koen G. |author-last4=Langendoen |title=Modern Compiler Design |publisher=[[John Wiley & Sons, Inc.]] |date=2000 |isbn=0-471-97697-0}} * {{cite book |author-last1=Kennedy |author-first1=Ken |author-link1=Ken Kennedy (computer scientist) |author-last2=Allen |author-first2=Randy |title=Optimizing Compilers for Modern Architectures: A Dependence-Based Approach |url=https://archive.org/details/optimizingcompil00alle_837 |url-access=limited |chapter=Chapter 4.4. Data Flow Analysis - Chapter 4.4.2. Dead Code Elimination |publisher=[[Academic Press]] / [[Morgan Kaufmann Publishers]] / [[Elsevier]] |date=2002 |edition=2011 digital print of 1st |isbn=978-1-55860-286-1 |lccn=2001092381 |pages=[https://archive.org/details/optimizingcompil00alle_837/page/n134 137], 145–147, 167}} * {{cite journal |date=January 2001 |orig-date=1999-11-02 |title=alto: a link-time optimizer for the Compaq Alpha. |author-first1=Robert |author-last1=Muth |author-first2=Saumya K. |author-last2=Debray |author-first3=Scott |author-last3=Watterson |author-first4=Koen |author-last4=De Bosschere |journal=Software: Practice and Experience |volume=31 |issue=1 |doi=10.1002/1097-024X(200101)31:1<67::AID-SPE357>3.0.CO;2-A |s2cid=442062 |citeseerx=10.1.1.33.4933 |pages=67–101}} [https://web.archive.org/web/20100721082900/http://www.cs.arizona.edu/~debray/Publications/alto.pdf] ==External links== * [https://archive.today/20130112201318/http://www.futurechips.org/tips-for-power-coders/how-to-trick-cc-compilers-into-generating-terrible-code.html How to trick C/C++ compilers into generating terrible code?] {{Compiler optimizations}} [[Category:Compiler optimizations]]
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)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Cite book
(
edit
)
Template:Cite journal
(
edit
)
Template:Compiler optimizations
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)
Template:Use dmy dates
(
edit
)
Template:Use list-defined references
(
edit
)