Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Dead-code elimination

From Wikipedia, the free encyclopedia
(Redirected fromDead code elimination)
Compiler optimization to remove code which does not affect the program results

Incompiler theory,dead-code elimination (DCE,dead-code removal,dead-code stripping, ordead-code strip) is acompiler optimization to removedead code (code that does not affect the program results). Removing such code has several benefits: it shrinksprogram size (an important consideration in some contexts); reduces resource usage, such as the number of bytes to be transferred[1]; and allows the running program to avoid executing irrelevantoperations, which reduces itsrunning 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 affectsdead variables (written to, but never read again), that is, irrelevant to the program.

Examples

[edit]

Consider the following example written inC.

intfoo(void){inta=24;intb=25;// Assignment to dead variableintc;c=a*4;returnc;b=24;// Unreachable codereturn0;}

Simple analysis of the uses of values would show that the value ofb after the first assignment is not used insidefoo. Furthermore,b is declared as a local variable insidefoo, so its value cannot be used outsidefoo. Thus, the variableb isdead 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 tob. Thus, the assignment isunreachable and can be removed.If the procedure had a more complexcontrol flow, such as a label after the return statement and agoto elsewhere in the procedure, then a feasible execution path might exist to the assignment tob.

Also, even though some calculations are performed in the function, their values are not stored in locations accessible outside thescope of this function. Furthermore, given the function returns a static value (96), it may be simplified to the value it returns (this simplification is calledconstant 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 apreprocessor. Consider the following code.

// set DEBUG_MODE to falseconstexprboolDEBUG_MODE=false;intmain(void){inta=5;intb=6;intc;c=a*(b/2);if(DEBUG_MODE){printf("%d\n",c);}returnc;}

Because the constantDEBUG_MODE will always evaluate tofalse (due to being defined as so), 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 indebugging to optionally activate blocks of code; using an optimizer with dead-code elimination eliminates the need for using apreprocessor 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 operatorstrength reduction insert new computations into the code and render the older, more expensive computations dead.[2] 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 fromdata-flow analysis.[3] An algorithm based onstatic single-assignment form (SSA) appears in the original journal article onSSA form by Ron Cytron et al.[4] Robert Shillingsburg (aka Shillner) improved on the algorithm and developed a companion algorithm for removing useless control-flow operations.[5]

Dynamic dead-code elimination

[edit]

Dead code is normally considered deadunconditionally. Therefore, it is reasonable attempting to remove dead code through dead-code elimination atcompile time.

However, in practice it is also common for code sections to represent dead or unreachable code onlyunder certain conditions, which may not be known at the time of compilation or assembly. Such conditions may be imposed by differentruntime environments (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.[6][7] 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.[6][7] 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 atload orruntime are calleddynamic dead-code elimination[8][9][10] ordynamic dead-instruction elimination.[11]

Most programming languages, compilers and operating systems offer no or little more support thandynamic loading of libraries andlate linking, therefore software utilizing dynamic dead-code elimination is very rare in conjunction with languagescompiled ahead-of-time or written inassembly language.[12][13][14] However, language implementations doingjust-in-time compilation may dynamically optimize for dead-code elimination.[10][15][16]

Although with a rather different focus, similar approaches are sometimes also utilized fordynamic software updating andhot patching.

See also

[edit]

References

[edit]
  1. ^Malavolta, Ivano et al. “JavaScript Dead Code Identification, Elimination, and Empirical Assessment.” IEEE transactions on software engineering 49.7 (2023): 3692–3714. Web.
  2. ^Allen, Frances;Cocke, John;Kennedy, Ken (June 1981). "Reduction of Operator Strength". InJones, Neil D.;Muchnick, Steven Stanley (eds.).Program Flow Analysis: Theory & Application.Prentice-Hall.ISBN 0-13729681-9.
  3. ^Kennedy, Ken (June 1981). "A Survey of Data-flow Analysis Techniques". InJones, Neil D.;Muchnick, Steven Stanley (eds.).Program Flow Analysis: Theory & Application.Prentice-Hall.ISBN 0-13729681-9.
  4. ^Cytron, Ron K.;Ferrante, Jeanne; Rosen, Barry K.; Zadeck, F. Kenneth (1991).Efficiently Computing Static Single Assignment Form and the Program Dependence Graph.ACM TOPLAS 13(4).
  5. ^Cooper, Keith D.; Torczon, Linda (2003) [2002-01-01].Engineering a Compiler.Morgan Kaufmann. pp. 498ff.ISBN 978-1-55860698-2.
  6. ^abPaul, Matthias R. (2002-04-03) [2001-06-18]."[fd-dev] Ctrl+Alt+Del".freedos-dev.Archived from the original on 2017-09-09. Retrieved2017-09-09.[…] any of the […] options can be "permanently" excluded at installation time (will also save the memory for the corresponding code excerpts due to ourDynamic 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.
  7. ^abPaul, Matthias R. (2002-04-06)."[fd-dev] Ctrl+Alt+Del".freedos-dev.Archived from the original on 2019-04-27. Retrieved2019-04-27.[…] 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 […]
  8. ^Thammanur, Sathyanarayan (2001-01-31).A Just in Time Register Allocation and Code Optimization Framework for Embedded Systems (MS thesis).University of Cincinnati, Engineering: Computer Engineering. ucin982089462.[1]Archived 2019-07-28 at theWayback Machine[2]Archived 2019-07-28 at theWayback Machine
  9. ^Kubice, Jan (2024-10-17)."Dynamic Dead Code Elimination: Optimizing for Flexibility".
  10. ^abConway, Andrew (1995-12-04)."Cyclic data structures".Newsgroupcomp.lang.functional.Archived from the original on 2017-09-09. Retrieved2017-07-03.[…]Lazy evaluation is basicallydynamic dead code elimination. […] (NB. Possibly the first public use of the termdynamic dead-code elimination, though only conceptually and with a focus on lazy evaluation infunctional languages.)
  11. ^Butts, J. Adam; Sohi, Guri (October 2002)."Dynamic Dead-Instruction Detection and Elimination"(PDF). San Jose, CA, USA: Computer Science Department,University of Wisconsin-Madison.ASPLOS XACM 1-58113-574-2/02/0010.Archived(PDF) from the original on 2019-04-20. Retrieved2017-06-23.
  12. ^Paul, Matthias R.; Frinke, Axel C. (1997-10-13) [first published 1991],FreeKEYB - Enhanced DOS keyboard and console driver (User Manual) (v6.5 ed.)[3] (NB. FreeKEYB is aUnicode-based dynamically configurable successor of K3PLUS supporting mostkeyboard layouts,code pages, andcountry codes. Utilizing an off-the-shelfmacro assembler as well as a framework of automatic pre- and post-processing analysis tools to generate dependency andcode morphingmeta data to be embedded into theexecutable file alongside thebinary code and a self-discarding,relaxing andrelocating loader, the driver implements byte-level granulardynamic dead-code elimination andrelocation techniques atload-time as well asself-modifying code and reconfigurability atrun-time to minimize its memory footprint downto close thecanonical 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 forDOS 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.)
  13. ^Paul, Matthias R.; Frinke, Axel C. (2006-01-16),FreeKEYB - Advanced international DOS keyboard and console driver (User Manual) (v7 preliminary ed.)
  14. ^Paul, Matthias R. (2001-04-10)."[ANN] FreeDOS beta 6 released" (in German).Newsgroupde.comp.os.msdos.Archived from the original on 2017-09-09. Retrieved2017-07-02.[…] brandneue[s] Feature, derdynamischen 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 granulardynamic dead-code elimination for softwareassembled orcompiled ahead-of-time.)
  15. ^Johng, Yessong; Danielsson, Per; Ehnsiö, Per; Hermansson, Mats; Jolanki, Mika; Moore, Scott; Strander, Lars; Wettergren, Lars (2002-11-08). "Chapter 5. Java overview and iSeries implementation - 5.1.1. Miscellaneous components".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(PDF). Red Books.IBM Corp. p. 41.ISBN 0-73842461-7. SG24-6545-00.Archived(PDF) from the original on 2013-10-08. Retrieved2019-04-20.[4]
  16. ^Polito, Guillermo (2015)."Virtualization Support for Application Runtime Specialization and Extension - Programming Languages"(PDF).Universite des Sciences et Technologies de Lille. pp. 111–124. HAL Id: tel-01251173.Archived(PDF) from the original on 2017-06-23. Retrieved2017-06-23.

Further reading

[edit]

External links

[edit]
Basic block
Loop
Data-flow
analysis
SSA-based
Code generation
Functional
Global
Other
Static analysis
Retrieved from "https://en.wikipedia.org/w/index.php?title=Dead-code_elimination&oldid=1328172281"
Category:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp