Ershov Number
   HOME

TheInfoList



OR:

Ershov numbers, named after Andrey Petrovich Yershov, are used in
code optimization In computer science, program optimization, code optimization, or software optimization, is the process of modifying a software system to make some aspect of it work more efficiently or use fewer resources. In general, a computer program may be o ...
to minimize the amount of
register allocation In compiler optimization, register allocation is the process of assigning local automatic variables and expression results to a limited number of processor registers. Register allocation can happen over a basic block (''local register allocation' ...
s. Ershov numbers can be used in methods to optimally select registers when there is only one expression in a code block. Given an expression E = E1 ''op'' E2 the goal is to generate code so as to either minimize the number of registers used, or, if an insufficient number of registers is available, to minimize the number of nonregister temporaries required.


Definition

The Ershov number ''n'' of a node in given
expression tree A binary expression tree is a specific kind of a binary tree used to represent Expression (mathematics), expressions. Two common types of expressions that a binary expression tree can represent are algebraic and boolean algebra, boolean. These tre ...
is defined as follows: # Every leaf has ''n'' = 1. # For a node with one child, ''n'' is the same as child's. # For a node with two children, ''n'' is defined as: : n = \begin max(child_1, child_2) & child_1 \ne child_2 \\ child_1 + 1 & child_1 = child_2 \end The Ershov number of a
node In general, a node is a localized swelling (a "knot") or a point of intersection (a vertex). Node may refer to: In mathematics *Vertex (graph theory), a vertex in a mathematical graph *Vertex (geometry), a point where two or more curves, lines, ...
represents the minimum number of registers required to evaluate the subexpression whose root is that node. The idea is that we evaluate the child with the larger Ershov number first, then the other child, then perform the operation at the root.


Example

Suppose we have an expression tree with a '+' operation at the root, and the left and right
subtree In computer science, a tree is a widely used abstract data type that represents a hierarchical tree structure with a set of connected nodes. Each node in the tree can be connected to many children (depending on the type of tree), but must be conn ...
s have Ershov numbers of 3 and 4, respectively. The Ershov number of this node is 4, so we should be able to generate code for the expression using 4 registers. # Generate code to evaluate the right child using registers r1, r2, r3, and r4. Place the result in r1. # Generate code to evaluate the left child using registers r2, r3, and r4. Place the result in r2. # Issue the instruction ADD r1, r1, r2, which adds r1 and r2 and stores the result in r1.


Code generation

The general procedure for generating code using a minimal number of loads and stores from memory is as follows: # Generate code for the child with the largest Ershov number first # Issue an instruction to store the result in a temporary register, or, if none is available, a temporary location in memory # Generate code for the child with the smaller Ershov number # Issue an instruction to load the temporary variable back into a register # Issue an instruction to perform the operation at the root In the ideal case, if there are ''n'' registers and the first subexpression requires ''n'' registers and the next subexpression requires ''n - 1'' registers, a single register can be used to store the result of the first expression, and there will still be ''n - 1'' registers available to compute the next subexpression, therefore requiring no loads or stores from memory at all. If the Ershov number of the root of the expression tree is greater than the number of registers available, the Ershov number can also be used to determine the amount of additional temporary memory space required, for example on the
stack Stack may refer to: Places * Stack Island, an island game reserve in Bass Strait, south-eastern Australia, in Tasmania’s Hunter Island Group * Blue Stack Mountains, in Co. Donegal, Ireland People * Stack (surname) (including a list of people ...
.


See also

*
Strahler number In mathematics, the Strahler number or Horton–Strahler number of a mathematical tree is a numerical measure of its branching complexity. These numbers were first developed in hydrology by and ; in this application, they are referred to as the ...
, the minimum number of registers needed to evaluate an expression without any external storage *
Sethi–Ullman algorithm In computer science, the Sethi–Ullman algorithm is an algorithm named after Ravi Sethi and Jeffrey D. Ullman, its inventors, for translating abstract syntax trees into machine code that uses as few registers as possible. Overview When generatin ...
, basically the same concept


References

{{reflist Syntax Software optimization