Sethi-Ullman algorithm

Sethi-Ullman algorithm

When 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 if free registers are scarce). The Sethi-Ullman algorithm (also known as Sethi-Ullman numbering) fulfills the property of producing code which needs the least number of instructions possible as well as the least number of storage references (under the assumption that at the most commutativity and associativity apply to the operators used, but distributive laws i.e. a * b + a * c = a * (b + c) do not hold). Please note that the algorithm succeeds as well if neither commutativity nor associativity hold for the expressions used, and therefore arithmetic transformations can not be applied.

imple 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 non-constant leaf node, assign a 1 (i.e. 1 register is needed to hold the variable/field/etc.). For every constant leaf node (RHS of an operation - literals, values), assign a 0.
## For every non-leaf node "n", assign the number of registers needed to evaluate the respective subtrees of "n". If the number of registers needed in the left subtree ("l") are not equal to the number of registers needed in the right subtree ("r"), the number of registers needed for the current node "n" is max(l, r). If "l = r", then the number of registers needed for the current node is l + 1.
# 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 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 a = (b + c) * (d + 3), the abstract syntax tree looks like this:

= / a * / / + + / / b c d 3

To continue with the algorithm, we need only to examine the arithmetic expression (b + c) * (d + 3), i.e. we only have to look at the right subtree of the assignment '=':

* / / + + / / b c d 3

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 (b + c) * (d + 3) is a constant):

*2 / / +2 +1 / / b1 c1d1 30

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. 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

In an advanced version of the Sethi-Ullman algorithm, the arithmetic expressions are first transformed, exploiting the algebraic properties of the operators used.

External links

* [http://portal.acm.org/citation.cfm?doid=321607.321620 The Generation of Optimal Code for Arithmetic Expressions] Ravi Sethi, J. D. Ullman, Journal of the ACM, Vol. 17, No. 4, October 1970, pp. 715-728
* [http://lambda.uta.edu/cse5317/fall02/notes/node40.html Code Generation for Trees]


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать реферат

Look at other dictionaries:

  • Ullmann — Ulmann (variations: Ullmann, Ullman) is a surname originating from Ulmer Mann (person from Ulm, Germany). Another explanation is that the name goes back to the Old High German word Uodalrich ( uodal = heritage; rich = powerful). People* Al Ullman …   Wikipedia

  • List of mathematics articles (S) — NOTOC S S duality S matrix S plane S transform S unit S.O.S. Mathematics SA subgroup Saccheri quadrilateral Sacks spiral Sacred geometry Saddle node bifurcation Saddle point Saddle surface Sadleirian Professor of Pure Mathematics Safe prime Safe… …   Wikipedia

  • Compiler — This article is about the computing term. For the anime, see Compiler (anime). A diagram of the operation of a typical multi language, multi target compiler A compiler is a computer program (or set of programs) that transforms source code written …   Wikipedia

  • LR parser — In computer science, an LR parser is a parser for context free grammars that reads input from Left to right and produces a Rightmost derivation. The term LR( k ) parser is also used; here the k refers to the number of unconsumed look ahead input… …   Wikipedia

  • Available expression — In the field of compiler optimizations, available expressions is an analysis algorithm that determines for each point in the program the set of expressions that need not be recomputed. Those expressions are said to be available at such a point.… …   Wikipedia

  • Alfred Aho — Infobox Scientist name = Alfred Vaino Aho caption = birth date = birth place = death date = death place = residence = nationality = field = Computer Science work institution = Columbia University alma mater = doctoral advisor =Alfred Vaino Aho… …   Wikipedia

  • Top-down parsing — is a strategy of analyzing unknown data relationships by hypothesizing general parse tree structures and then considering whether the known fundamental structures are compatible with the hypothesis. It occurs in the analysis of both natural… …   Wikipedia

  • List of important publications in computer science — This is a list of important publications in computer science, organized by field. Some reasons why a particular publication might be regarded as important: Topic creator – A publication that created a new topic Breakthrough – A publication that… …   Wikipedia

  • Autómata finito — Un autómata finito (AF) o máquina de estado finito es un modelo matemático que realiza cómputos en forma automática sobre una entrada para producir una salida. Este modelo está conformado por un alfabeto, un conjunto de estados y un conjunto de… …   Wikipedia Español

  • Parsing — In computer science and linguistics, parsing, or, more formally, syntactic analysis, is the process of analyzing a sequence of tokens to determine their grammatical structure with respect to a given (more or less) formal grammar.Parsing is also… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”