| 1 |
\section{Register Allocation} |
| 2 |
All the optimization modules are written in a generic fashion but |
| 3 |
parameterized over architecture and client information. The Standard |
| 4 |
ML module system is a central mechanism to the design and |
| 5 |
organization of MLRISC. Parameterized modules in Standard ML are |
| 6 |
provided by \newdef{functors}, that takes the |
| 7 |
specification of input modules and produces a module that matches some |
| 8 |
output specification. In particular, SML/NJ modules are |
| 9 |
\emph{higher order}, which means that a functor can yield functors as a |
| 10 |
result. I will use register allocation as an example. |
| 11 |
|
| 12 |
\image{Back end optimizations}{pictures/png/hof-1.png}{align=left} |
| 13 |
|
| 14 |
The register allocator is written has a higher order functor which |
| 15 |
when applied to suitable arguments produces an integer or floating |
| 16 |
point register allocator. The figure is simplifed because the output |
| 17 |
functor is not restricted to integer and floating point allocators |
| 18 |
but could also be other types of allocators, for example, condition |
| 19 |
code. The integer and floating point register allocators are |
| 20 |
functors that only take \emph{client specific} parameters as |
| 21 |
input, whereas the higher-order takes architectural parameters as |
| 22 |
input. The client specific parameters include: |
| 23 |
\begin{SML} |
| 24 |
nFreeRegs : int |
| 25 |
dedicated : int list |
| 26 |
spill : .. |
| 27 |
reload : .. |
| 28 |
\end{SML} |
| 29 |
|
| 30 |
where: |
| 31 |
\begin{description} |
| 32 |
\item[\sml{nFreeRegs}] is the number of free registers or |
| 33 |
essentially the number of colors available for coloring the |
| 34 |
interference graph. |
| 35 |
|
| 36 |
\item[\sml{dedicated}] is the list of dedicated registers. It |
| 37 |
is useful to exclude these from the graph-color process to reduce |
| 38 |
the size of the data structures created. |
| 39 |
|
| 40 |
\item[\sml{spill/reload}] are functions that describe how to |
| 41 |
spill and reload registers that need to be spilled or reloaded in |
| 42 |
an instruction. These two functions are perhaps the most |
| 43 |
complicated pieces of information that need to be supplied by a |
| 44 |
client of MLRISC. |
| 45 |
\end{description} |
| 46 |
|
| 47 |
The architecture specific parameters supplied to the higher-order |
| 48 |
functor include: |
| 49 |
\begin{SML} |
| 50 |
firstPseudoReg : int |
| 51 |
maxPseudoR : unit -> int |
| 52 |
defUse : instruction -> (int list * int list) |
| 53 |
\end{SML} |
| 54 |
|
| 55 |
where: |
| 56 |
\begin{description} |
| 57 |
\item[\sml{firstPseudoR}] is an integer representing the first |
| 58 |
pseudo register. Any register below this value is a physical |
| 59 |
register. |
| 60 |
|
| 61 |
\item[\sml{maxPseudoR}] is a function that returns an |
| 62 |
integer indicating the number of the highest pseudo-register that |
| 63 |
has been used in the program. This number is useful in estimating |
| 64 |
the intial size of various tables. |
| 65 |
|
| 66 |
\item[\sml{defUse}] is a function that returns the |
| 67 |
registers defined and used by an instruction. |
| 68 |
\end{description} |
| 69 |
|
| 70 |
These parameters are largely self explanatory, however, there are |
| 71 |
addition architectural parameters that relate to the internal |
| 72 |
representation of instructions that would be ugly to explain. For |
| 73 |
example there is the need for a module that does liveness analysis |
| 74 |
over the register class that is being allocated. This type of |
| 75 |
complexity can be shielded from a user. For the DEC Alpha the |
| 76 |
situation is as shown in the figure: |
| 77 |
|
| 78 |
\image{Back end optimizations}{pictures/png/hof-2.png}{align=center} |
| 79 |
|
| 80 |
The client only sees the functors on the right, to which only client |
| 81 |
specific information need be provided. There is the illusion of a |
| 82 |
dedicated DEC Alpha integer and floating point register |
| 83 |
allocator. There are several advantages to this: |
| 84 |
\begin{itemize} |
| 85 |
\item The architectural parameters that are implementation specific |
| 86 |
do not need to be explained to a user, and are supplied by someone |
| 87 |
that intimately understands the port to the target architecture. |
| 88 |
|
| 89 |
\item The number of parameters that a client supplies is |
| 90 |
reduced. |
| 91 |
|
| 92 |
\item The parameters that the client supplies is restricted to |
| 93 |
things that concern the front end. |
| 94 |
\end{itemize} |