Sethi–Ullman algorithm: Difference between revisions

Content deleted Content added
Adding short description: "Algorithm for minimising register usage"
 
(8 intermediate revisions by 6 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 [[Processor register|registers]] as possible.
 
==Overview==
When [[code generation (compiler)|generating code]] for arithmetic expressions, the [[compiler]] has to decide which is the best way to translate the expression in terms of number of instructions used as well as number of registers needed to evaluate a certain subtree. Especially in the case that free registers are scarce, the [[order of evaluation]] can be important to the length of the generated code, because different orderings may lead to larger or smaller numbers of intermediate values being [[register allocation|spilled]] to memory and then restored. The Sethi–Ullman algorithm (also known as '''Sethi–Ullman numbering''') produces code which needs the fewest instructions possible as well as the fewest storage references (under the assumption that at the most [[commutativity]] and [[associativity]] apply to the operators used, but distributive laws i.e. <math>a * b + a * c = a * (b + c)</math> do not hold). Please note that theThe algorithm succeeds as well if neither [[commutativity]] nor [[associativity]] hold for the expressions used, and therefore arithmetic transformations can not be applied. The algorithm also does not take advantage of common subexpressions or apply directly to expressions represented as general directed acyclic graphs rather than trees.
 
==Simple Sethi–Ullman algorithm==
Line 8 ⟶ 9:
 
# Traverse the [[abstract syntax tree]] in pre- or postorder
## For every leaf node, if it is a non-constant leaf nodeleft-child, assign a 1 (i.e. 1 register is needed to hold the variable/field/etc.), ifotherwise itassign isa the0 left(it child of its parent else assignis a 0.non-constant Forright child everyor constant leaf node (RHS of an operation – literals, values), assign a 0).
## For every non-leaf node ''n'', assignif the numberleft ofand registers needed to evaluate the respectiveright subtrees ofrespectively ''n''.need Ifdifferent the numbernumbers of registers needed in the left subtree (''l'') areand not equal to the number of registers needed in the right subtree (''r''), thethen number of registers needed for the current nodeassign max(''nl'' is max(l,&nbsp;r). If ''l == r''), thenotherwise the number of registers needed for the current node isassign ''r''&nbsp;+&nbsp;1.
# 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.
# Code emission
## If the number of registers needed to compute the left subtree of node ''n'' is bigger than the number of registers for the right subtree, then the left subtree is evaluated first (since it may be possible that the one more register needed by the right subtree to save the result makes the left subtree [[Register spilling|spill]]). If the right subtree needs more registers than the left subtree, the right subtree is evaluated first accordingly. If both subtrees need equal as much registers, then the order of evaluation is irrelevant.
 
===Example===
For an arithmetic expression <math>a = (b + c + f * g) * (d + 3)</math>, the [[abstract syntax tree]] looks like this:
 
=
Line 62:
 
==External links==
*[httphttps://lambda.uta.edu/cse5317/fall02/notes/node40node43.html Code Generation for Trees]
 
{{DEFAULTSORT:Sethi-Ullman algorithm}}