Instruction selection
title: "Instruction selection" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["compiler-optimizations"] topic_path: "general/compiler-optimizations" source: "https://en.wikipedia.org/wiki/Instruction_selection" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0
NOTOC In computer science, instruction selection is the stage of a compiler backend that transforms its middle-level intermediate representation (IR) into a low-level IR. In a typical compiler, instruction selection precedes both instruction scheduling and register allocation; hence its output IR has an infinite set of pseudo-registers (often known as temporaries) and may still be – and typically is – subject to peephole optimization. Otherwise, it closely resembles the target machine code, bytecode, or assembly language.
For example, for the following sequence of middle-level IR code t1 = a t2 = b t3 = t1 + t2 a = t3 b = t1
a good instruction sequence for the x86 architecture is
::code[lang=asm] MOV EAX, a XCHG EAX, b ADD a, EAX ::
For a comprehensive survey on instruction selection, see. | last = Blindell | first = Gabriel S. Hjort | title = Survey on Instruction Selection: An Extensive and Modern Literature Review | year = 2013 | arxiv = 1306.4898 | isbn = 978-91-7501-898-0 | last = Blindell | first = Gabriel S. Hjort | title = Instruction Selection: Principles, Methods, & Applications | url = https://www.springer.com/us/book/9783319340173 | publisher = Springer | doi = 10.1007/978-3-319-34019-7 | year = 2016 | isbn = 978-3-319-34017-3 | s2cid = 13390131
Macro expansion
The simplest approach to instruction selection is known as macro expansion or interpretative code generation. A macro-expanding instruction selector operates by matching templates over the middle-level IR. Upon a match the corresponding macro is executed, using the matched portion of the IR as input, which emits the appropriate target instructions. Macro expansion can be done either directly on the textual representation of the middle-level IR, or the IR can first be transformed into a graphical representation which is then traversed depth-first. In the latter, a template matches one or more adjacent nodes in the graph.
Unless the target machine is very simple, macro expansion in isolation typically generates inefficient code. To mitigate this limitation, compilers that apply this approach typically combine it with peephole optimization to replace combinations of simple instructions with more complex equivalents that increase performance and reduce code size. This is known as the Davidson-Fraser approach and is currently applied in GCC.
Graph covering
Another approach is to first transform the middle-level IR into a graph and then cover the graph using patterns. A pattern is a template that matches a portion of the graph and can be implemented with a single instruction provided by the target machine. The goal is to cover the graph such that the total cost of the selected patterns is minimized, where the cost typically represents the number of cycles it takes to execute the instruction. For tree-shaped graphs, the least-cost cover can be found in linear time using dynamic programming, but for DAGs and full-fledged graphs the problem becomes NP-complete and thus is most often solved using either greedy algorithms or methods from combinatorial optimization.
References
References
- Brown, P.. (1969). "A Survey of Macro Processors". Annual Review in Automatic Programming.
- Cattell, R. G. G.. (1979). "A Survey and Critique of Some Models of Code Generation". School of Computer Science, Carnegie Mellon University.
- (1982). "Retargetable Compiler Code Generation". Computing Surveys.
- Lunell, H.. (1983). "Code Generator Writing Systems". Linköping University.
- (1974). "The PASCAL (P) Compiler Implementation Notes". Instituts für Informatik.
- (1969). "A Base for a Mobile Programming System". Communications of the ACM.
- Wilcox, T. R.. (1971). "Generating Machine Code for High-Level Programming Languages". Cornell University.
- (1984). "Code Selection Through Object Code Optimization". ACM Transactions on Programming Languages and Systems.
- (1989). "Code Generation Using Tree Matching and Dynamic Programming". ACM Transactions on Programming Languages and Systems.
- (1994). "Proceedings of 7th International Symposium on High-Level Synthesis".
- (1999). "Proceedings of the 36th ACM/IEEE conference on Design automation conference - DAC '99".
- (2010). "Combined Scheduling and Instruction Selection for Processors with Reconfigurable Cell Fabric". Proceedings of the 21st International Conference on Application-Specific Architectures and Processors (ASAP'10).
::callout[type=info title="Wikipedia Source"] This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page. ::