Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Sethi–Ullman algorithm
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Simple Sethi–Ullman algorithm== The '''simple Sethi–Ullman algorithm''' works as follows (for a [[load/store architecture]]): # Traverse the [[abstract syntax tree]] in pre- or postorder ## For every leaf node, if it is a non-constant left-child, assign a 1 (i.e. 1 register is needed to hold the variable/field/etc.), otherwise assign a 0 (it is a non-constant right child or constant leaf node (RHS of an operation – literals, values)). ## For every non-leaf node, if the left and right subtrees respectively need different numbers of registers ''l'' and ''r'', then assign max(''l'', ''r''), otherwise assign ''r'' + 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. ===Example=== For an arithmetic expression <math>a = (b + c + f * g)*(d+3)</math>, the [[abstract syntax tree]] looks like this: = / \ a * / \ / \ + + / \ / \ / \ d 3 + * / \ / \ b c f g To continue with the algorithm, we need only to examine the arithmetic expression <math>(b + c + f * g) * (d + 3)</math>, i.e. we only have to look at the right subtree of the assignment '=': * / \ / \ + + / \ / \ / \ d 3 + * / \ / \ b c f g Now we start traversing the tree (in preorder for now), assigning the number of registers needed to evaluate each subtree (note that the last summand in the expression <math>(b + c + f * g) * (d + 3)</math> is a constant): *<sub>'''2'''</sub> / \ / \ +<sub>'''2'''</sub> +<sub>'''1'''</sub> / \ / \ / \ d<sub>'''1'''</sub> 3<sub>'''0'''</sub> +<sub>'''1'''</sub> *<sub>'''1'''</sub> / \ / \ 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.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)