{{Short description|Compiler optimization technique}} {{Use dmy dates|date=August 2022|cs1-dates=y}} {{Use list-defined references|date=August 2022}} '''Superoptimization''' is the process where a compiler automatically finds the optimal sequence for a loop-free sequence of instructions. Real-world compilers generally cannot produce genuinely ''optimal'' code, and while most standard compiler optimizations only improve code partly, a superoptimizer's goal is to find the optimal sequence, the canonical form. Superoptimizers can be used to improve conventional optimizers by highlighting missed opportunities so a human can write additional rules.

==History== The term superoptimization was coined by Alexia Massalin in the 1987 paper ''Superoptimizer: A Look at the Smallest Program''.<ref name="Massalin_1987_Superoptimizer"/> The label "program optimization" has been given to a field that does not aspire to optimize but only to improve. This misnomer forced Massalin to call her system a superoptimizer, which is actually an optimizer to find an optimal program.<ref name="Denali"/>

In 1992, the GNU Superoptimizer (GSO) was developed to integrate into the GNU Compiler Collection (GCC).<ref name="GCCsuperACM"/><ref name="GCCsuperopt"/> Later work further developed and extended these ideas.

==Techniques== Traditionally, superoptimizing is performed via exhaustive brute-force search in the space of valid instruction sequences. This is a costly method, and largely impractical for general-purpose compilers. Yet, it has been shown to be useful in optimizing performance-critical inner loops. It is also possible to use a SMT solver to approach the problem, vastly improving the search efficiency (although inputs more complex than a basic block remain out of reach).<ref name="jangda">{{cite conference |last1=Jangda |first1=Abhinav |last2=Yorsh |first2=Greta |title=Unbounded superoptimization |conference=Onward!’17, October 25–27, 2017, Vancouver, Canada |date=25 October 2017 |pages=78–88 |doi=10.1145/3133850.3133856}}</ref>

In 2001, goal-directed superoptimizing was demonstrated in the Denali project by Compaq research.<ref name="Joshi_2001"/> In 2006, answer set declarative programming was applied to superoptimization in the ''Total Optimisation using Answer Set Technology'' (TOAST) project<ref name="TOAST-KRR"/> at the University of Bath.<ref name="TOAST-book"/><ref name="crick-phd"/>

Superoptimization can be used to automatically generate general-purpose peephole optimizers.<ref name="Bansal_2006"/>

==Publicly available superoptimizers== Several superoptimizers are available for free download.

* For the x86 family of instruction sets: ** GNU superoptimizer (superopt)<ref name="GSO_1992"/> (GSO) (1992)<ref name="GCCsuperACM"/><ref name="GCCsuperopt"/> &ndash; also supports many other ISAs ** STOKE,<ref name="STOKE"/> a stochastic optimizer<ref name="Bansal_2008"/> for x86-64 x86 assembly language. * For ARM: ** Unbounded Superoptimizer,<ref name=jangda/> transforming LLVM IR into ARMv7-A assembly * For embedded systems: ** PIC microcontroller SuperOptimizer (2003)<ref name="Serpell_2003_1"/><ref name="Serpell_2003_2"/> ** A feasibility study by Embecosm (2014) for AVR, based on GSO<ref name="Embecosm_2014"/><ref>[https://www.embecosm.com/research/superoptimization/ Superoptimization – Embecosm] [https://github.com/embecosm/gnu-superopt Source Code]</ref> * For the JVM: ** Clojure superoptimizer for the Java virtual machine (2012)<ref name="Hume_2012"/> * For LLVM IR: ** souper<ref name="souper"/> superoptimizer for programs in the LLVM intermediate language. * For WebAssembly ** slumps<ref name="slumps"/> provides superoptimization for WASM programs based on souper.

==See also== * Peephole optimization * Dead code elimination * Metacompilation

==References== {{Reflist|refs= <ref name="Massalin_1987_Superoptimizer">{{cite journal |doi=10.1145/36177.36194 |title=Superoptimizer: A look at the smallest program |date=1987 |author-last=Massalin |author-first=Henry |journal=ACM SIGARCH Computer Architecture News |volume=15 |issue=5 |pages=122–126 |url=https://web.stanford.edu/class/cs343/resources/superoptimizer.pdf |access-date=2023-05-01 |quote=Given an instruction set, the superoptimizer finds the shortest program to compute a function. Startling programs have been generated, many of them engaging in convoluted bit-fiddling bearing little resemblance to the source programs which defined the functions. The key idea in the superoptimizer is a probabilistic test that makes exhaustive searches practical for programs of useful size.}}</ref> <ref name="Denali">{{cite journal |last1=Joshi |first1=Rajeev |last2=Nelson |first2=Greg |last3=Randall |first3=Keith |title=Denali: A Goal-directed Superoptimizer |journal=ACM SIGPLAN Notices |date=2002 |volume=37 |issue=5 |pages=304–314 |doi=10.1145/543552.512566 |url=https://dl.acm.org/doi/10.1145/543552.512566|url-access=subscription }}</ref> <ref name="GCCsuperACM">{{cite book |author-last1=Granlund |author-first1=Torbjörn |author-last2=Kenner |author-first2=Richard |chapter=Eliminating branches using a superoptimizer and the GNU C compiler |title=Proceedings of the ACM SIGPLAN 1992 conference on Programming language design and implementation - PLDI '92 |date=1992 |isbn=978-0-89791475-8 |doi=10.1145/143095.14314 |doi-broken-date=7 September 2025 |citeseerx=10.1.1.58.3509 |s2cid=8825539}}</ref> <ref name="GCCsuperopt">{{cite web |url=https://ftp.gnu.org/gnu/superopt/ |title=Index of /gnu/superopt |date=1995-06-14 |website=GNU Operating System |publisher=Free Software Foundation, Inc. |access-date=2023-05-01}}</ref> <ref name="Joshi_2001">{{cite web |url=https://hpl.hp.com/techreports/Compaq-DEC/SRC-RR-171.html |title=Denali: a goal-directed superoptimizer |author-last1=Joshi |author-first1=Rajeev |author-last2=Nelson |author-first2=Greg |author-last3=Randall |author-first3=Keith |date=2001-07-30 |department=Compaq Systems Research Center |website=HP Labs |publisher=Hewlett-Packard Co. |access-date=2023-05-01}}</ref> <ref name="TOAST-book">{{cite book |author-last1=Brain |author-first1=Martin |author-last2=Crick |author-first2=Tom |author-last3=De Vos |author-first3=Marina |author-last4=Fitch |author-first4=John |editor-last1=Etalle |editor-first1=Sandro |editor-last2=Truszczyński |editor-first2=Mirosław |title=Logic Programming |publisher=Springer-Verlag |date=2006-08-17 |pages=270–284 |chapter=TOAST: Applying Answer Set Programming to Superoptimisation |series=Lecture Notes in Computer Science |volume=4079 |doi=10.1007/11799573_21|isbn=978-3-540-36636-2}}</ref> <ref name="crick-phd">{{cite thesis |last=Crick |first=Tom |date=2009 |title=Superoptimisation: Provably Optimal Code Generation using Answer Set Programming |url=https://researchportal.bath.ac.uk/en/studentTheses/superoptimisation-provably-optimal-code-generation-using-answer-s |degree=PhD |publisher=University of Bath | access-date=2024-11-15}}</ref> <ref name="TOAST-KRR">{{cite web |title=TOAST – KRRwiki |author=<!--Wiki writer(s); no by-lines.--> |date=2007-08-07 |department=Department of Computer Science, Mathematical Foundations Group |website=Knowledge Representation and Reasoning (KRR) group |publisher=University of Bath |url=http://krr.cs.bath.ac.uk/index.php/TOAST |access-date=2016-09-03 |url-status=dead|archive-url=https://web.archive.org/web/20121128143632/http://krr.cs.bath.ac.uk/index.php/TOAST |archive-date=2012-11-28}}</ref> <ref name="Bansal_2006">{{cite web |url=https://sorav.compiler.ai/pubs/asplos06.pdf |title=Automatic Generation of Peephole Superoptimizers |author-last1=Bansal |author-first1=Sorav |author-last2=Aiken |author-first2=Alex |date=2006 |access-date=2023-05-01}}</ref> <ref name="Bansal_2008">{{cite web |url=https://sorav.compiler.ai/pubs/osdi08.pdf |title=Binary Translation Using Peephole Superoptimizers |author-last1=Bansal |author-first1=Sorav |author-last2=Aiken |author-first2=Alex |date=2008 |access-date=2023-05-01}}</ref> <ref name="Serpell_2003_1">{{cite web |url=https://sites.google.com/site/danielserpell/picmicrocontrollersuperoptimizer |title=SuperOptimizer for Microchip's PIC microcontrollers |author-last=Serpell |author-first=Daniel |date=2003 |website=Google Sites |access-date=2016-09-06 |archive-url=https://web.archive.org/web/20161011090623/https://sites.google.com/site/danielserpell/picmicrocontrollersuperoptimizer |archive-date=2016-10-11 |url-status=live}}</ref> <ref name="Serpell_2003_2">{{cite web |url=http://freecode.com/projects/picsuperoprimizer/ |title=PIC Microcontroller SuperOptimizer |author-last=Serpell |author-first=Daniel |date=2003-06-21 |website=Freecode |publisher=Slashdot Media |access-date=2016-09-06 |archive-url=https://web.archive.org/web/20160917164828/http://freecode.com/projects/picsuperoprimizer/ |archive-date=2016-09-17 |url-status=live}}</ref> <ref name="Hume_2012">{{cite web |url=https://github.com/twhume/superoptimiser |title=Clojure program to exhaustively search for optimal Java programs |author-last=Hume |author-first=Tom |date=2012-08-21 |website=GitHub |access-date=2016-09-06 |archive-url=https://web.archive.org/web/20180610233155/https://github.com/twhume/superoptimiser |archive-date=2018-06-10 |url-status=live}}</ref> <ref name="GSO_1992">{{cite web |url=https://www.gnu.org/software/superopt/ |title=GNU Superoptimizer}}</ref> <ref name="STOKE">{{cite web |author=StanfordPL |url=https://github.com/StanfordPL/stoke |title=STOKE|website=GitHub }}</ref> <ref name="Embecosm_2014">{{cite web |url=http://superoptimization.org |title=A feasibility study by Embecosm}}</ref> <ref name="souper">{{cite arXiv |last1=Sasnauskas |first1=Raimondas |last2=Chen |first2=Yang |last3=Collingbourne |first3=Peter |last4=Ketema |first4=Jeroen |last5=Lup |first5=Gratian |last6=Taneja |first6=Jubi |last7=Regehr |first7=John |title=Souper: A Synthesizing Superoptimizer |date=2017 |class=cs.PL |eprint=1711.04422}} [https://github.com/google/souper GitHub source code]</ref> <ref name="slumps">{{cite conference |last1=Cabrera Arteaga |first1=Javier |last2=Donde |first2=Shrinish |last3=Gu |first3=Jian |last4=Floros |first4=Orestis |last5=Satabin |first5=Lucas |last6=Baudry |first6=Benoit |last7=Monperrus |first7=Martin |title=Superoptimization of WebAssembly bytecode |date=23 March 2020 |pages=36–40 |doi=10.1145/3397537.3397567 |arxiv=2002.10213 |conference=MoreVMs: Workshop on Modern Language Runtimes, Ecosystems, and VMs}} [https://github.com/KTH/slumps/tree/master/superoptimizer GitHub source code]</ref> }}

{{Compiler optimizations}}

Category:Compiler optimizations