Content deleted Content added
m →Example |
Adding short description: "Algorithm for minimising register usage" |
||
(26 intermediate revisions by 23 users not shown) | |||
Line 1:
{{Short description|Algorithm for minimising register usage}}
In [[computer science]], the '''Sethi–Ullman algorithm''' is an [[algorithm]] named after [[Ravi Sethi]] and [[Jeffrey D. Ullman]], its inventors, for translating [[abstract syntax tree]]s into [[machine code]] that uses as few
==Overview==
When [[
==Simple Sethi–Ullman algorithm==
The '''simple Sethi–Ullman algorithm''' works as follows (for a [[
# Traverse the [[abstract syntax tree]] in pre- or postorder
## For every leaf node, if it is a non-constant
## For every non-leaf node
# To emit code, if the subtrees need different numbers of registers, evaluate the subtree needing the most registers first (since the register needed to save the result of one subtree may make the other one [[Register spilling|spill]]), otherwise the order is irrelevant.
===Example===
For an arithmetic expression <math>a = (b + c + f * g)
=
Line 49:
/ \ / \
b<sub>'''1'''</sub> c<sub>'''0'''</sub>f<sub>'''1'''</sub> g<sub>'''0'''</sub>
From this tree it can be seen that we need 2 registers to compute the left subtree of the '*', but only 1 register to compute the right subtree. Nodes 'c' and 'g' do not need registers for the following reasons: If T is a tree leaf, then the number of registers to evaluate T is either 1 or 0 depending whether T is a left or a right subtree (since an operation such as add R1, A can handle the right component A directly without storing it into a register). Therefore we shall start to emit code for the left subtree first, because we might run into the situation that we only have 2 registers left to compute the whole expression. If we now computed the right subtree first (which needs only 1 register), we would then need a register to hold the result of the right subtree while computing the left subtree (which would still need 2 registers), therefore needing 3 registers concurrently. Computing the left subtree first needs 2 registers, but the result can be stored in 1, and since the right subtree needs only 1 register to compute, the evaluation of the expression can do with only 2 registers left.
==Advanced Sethi–Ullman algorithm==
Line 56:
==See also==
*[[Strahler number]], the minimum number of registers needed to evaluate an expression without any external storage
*[[Ershov Number]], basically the same concept as Strahler number
==References==
*{{citation|title=The Generation of Optimal Code for Arithmetic Expressions|first1=Ravi|last1=Sethi|author1-link=Ravi Sethi|first2=Jeffrey D.|last2=Ullman|author2-link=Jeffrey D. Ullman|journal=[[Journal of the Association for Computing Machinery]]|volume=17|issue=4|year=1970|pages=715–728|doi=10.1145/321607.321620|hdl=10338.dmlcz/101207|hdl-access=free}}.
==External links==
*[
{{DEFAULTSORT:Sethi-Ullman algorithm}}
[[Category:Compiler construction]]
[[Category:Graph algorithms]]
|