Ershov numbers, named after Andrey Petrovich Yershov, are used in code optimization to minimize the amount of register allocations. 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.
The Ershov number n of a node in given expression tree is defined as follows:[1] [2]
n=\begin{cases} max(child1,child2)&child1\nechild2\\ child1+1&child1=child2 \end{cases}
The Ershov number of a node 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.
Suppose we have an expression tree with a '+' operation at the root, and the left and right subtrees 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.
ADD r1, r1, r2
, which adds r1 and r2 and stores the result in r1.The general procedure for generating code using a minimal number of loads and stores from memory is as follows:
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.[3]
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.