| 1 |
\section{MLRISC Intermediate Representation} |
| 2 |
The MLRISC intermediate language is called |
| 3 |
\newdef{MLTREE} At the lowest level, the core of MLTREE is a |
| 4 |
\italics{Register Transfer Language (RTL)} |
| 5 |
but represented in tree form. The tree |
| 6 |
form makes it convenient to use tree pattern matching tools like |
| 7 |
BURG (where appropriate) to do target instruction selection. Thus a |
| 8 |
tree such as: |
| 9 |
|
| 10 |
\begin{SML} |
| 11 |
MV(32, t, |
| 12 |
ADDT(32, MULT(32, REG(32, b), REG(32, b)), |
| 13 |
MULT(32, MULT(REG(32, a), LI(4)), REG(32, c)))) |
| 14 |
\end{SML} |
| 15 |
|
| 16 |
computes \sml{t := b*b + 4*a*c} to 32-bit precision. |
| 17 |
The nodes \sml{ADDT} and |
| 18 |
\sml{MULT} are the trapping form of addition and multiplication, |
| 19 |
and \sml{LI} is used for integer constants. An infinite number |
| 20 |
of registers are assumed by the model, however depending on the |
| 21 |
target machine the first \sml{0..K} registers map onto the first |
| 22 |
\sml{K} registers on the target machine. Everything else is |
| 23 |
assumed to be a pseudo-register. The \sml{REG} node is used to |
| 24 |
indicate a general purpose register. |
| 25 |
|
| 26 |
|
| 27 |
The core MLTREE language makes no assumptions about instructions or |
| 28 |
calling convections of the target architecture. Trees can be |
| 29 |
created and combined in almost any form, with certain meaningless |
| 30 |
trees such as \sml{LOAD(32, FLOAD(64, LI 0))} being forbidden by the |
| 31 |
MLTREE type structure. |
| 32 |
|
| 33 |
Such pure trees are nice but inadequate in real compilers. One |
| 34 |
needs to be able to propagate front end specific information, such |
| 35 |
as frame sizes and frame offsets where the actual values are only |
| 36 |
available after register allocation and spilling. One could add |
| 37 |
support for frames in MLRISC, however this becomes a slippery slope |
| 38 |
because some compilers (e.g. SML/NJ) do not have a conventional |
| 39 |
notion of frames --- indeed there is no runtime stack in the |
| 40 |
execution of SML/NJ. A frame organization for one person may not |
| 41 |
meet the needs for another, and so on. In MLRISC, the special |
| 42 |
requirements of different compilers is communicated into the MLTREE |
| 43 |
language, and subsequently into the optimizations phases, by |
| 44 |
specializing the MLTREE data structure with client specific |
| 45 |
information. There are currently \emph{five} dimensions over |
| 46 |
which one could specialize the MLTREE language. |
| 47 |
|
| 48 |
\begin{description} |
| 49 |
\item[Constants] Constants are an |
| 50 |
abstraction for integer literals whose value is known after |
| 51 |
certain phases of code generation. Frame sizes and offsets are an |
| 52 |
example. |
| 53 |
\image{MLRISC intermediate representation}{pictures/png/mlrisc-ir.png}{align=right} |
| 54 |
\item[Regions] While the data |
| 55 |
dependencies between arithmetic operations is implicit in the |
| 56 |
instruction, the data dependencies between memory operations is |
| 57 |
not. Regions are an abstract view of memory that make this |
| 58 |
dependence explicit and is specially useful for instruction |
| 59 |
reordering. |
| 60 |
|
| 61 |
\item[Pseudo-ops] Pseudo-ops are |
| 62 |
intended to correspond to pseudo-op directives provided by native |
| 63 |
assemblers to lay out data, jump tables, and perform alignment. |
| 64 |
|
| 65 |
\item[Annotations] |
| 66 |
\href{annotations.html}{Annotations} are used |
| 67 |
for injecting semantics and other program information from the front-end |
| 68 |
into the backend. For example, a probability annotation can be |
| 69 |
attached to a branch instruction. Similarly, line number annotations |
| 70 |
can be attached to basic blocks to aid debugging. |
| 71 |
In many language implementations function local variables are |
| 72 |
spilled to activation frames on the stack. Spill slots contribute |
| 73 |
to the size of a function's frame. When an instruction produces a |
| 74 |
spill, we may need to update the frame associated to that |
| 75 |
instruction (increase the size of its spilling area). The frame |
| 76 |
for the current function can be injected in an annotation, which |
| 77 |
can be later examined by the spill callback during register allocation. |
| 78 |
|
| 79 |
Annotations are |
| 80 |
implemented as an universal type and can be arbitrarily extended. |
| 81 |
Individual annotations can be associated |
| 82 |
with compiler objects of varying granularity, |
| 83 |
from compilation units, to regions, basic blocks, flow edges, |
| 84 |
and down to the instructions. |
| 85 |
|
| 86 |
|
| 87 |
\item[User Defined Extensions] |
| 88 |
In the most extreme case, the basic constructors defined in the MLTREE |
| 89 |
language may be inadequate for the task at hand. |
| 90 |
MLTREE allows the client to arbitrarily extend |
| 91 |
the set of statements and expressions to more closely match the |
| 92 |
source language and the target architecture(s). |
| 93 |
|
| 94 |
For example, when using MLRISC for the backend of a DSP compiler |
| 95 |
it may be useful to extend the set of MLRISC operators to include |
| 96 |
fix point and saturated arithmetic. |
| 97 |
Similarly, when developing a language for loop parallelization, it may |
| 98 |
be useful to extend the MLTREE language with higher-level loop |
| 99 |
constructs. |
| 100 |
\end{description} |
| 101 |
|
| 102 |
\subsection{Examples} |
| 103 |
|
| 104 |
In the SML/NJ compiler, an encoding of a list of registers |
| 105 |
is passed to the garbage collector as the roots of live |
| 106 |
variables. This encoding cannot be computed until register |
| 107 |
allocation has been performed, therefore the integer literal |
| 108 |
encoding is represented as an abstract |
| 109 |
\href{constants.html}{constant}. |
| 110 |
|
| 111 |
Again, in the SML/NJ compiler, most stores are for initializing |
| 112 |
records in the allocation space, therefore representing every slot in |
| 113 |
the allocation space as a unique region allows one to commute |
| 114 |
most store instructions. Similarly, most loads are from |
| 115 |
\emph{immutable} records, and a simple analysis marks these are |
| 116 |
being accesses to \emph{read-only} memory. Read-only memory is |
| 117 |
characterized as having multiple \emph{uses} but no |
| 118 |
\emph{definitions}. |
| 119 |
|
| 120 |
In the TIL compiler, a \emph{trace table} is generated for |
| 121 |
every call site that records the set of live variables, their |
| 122 |
location (register or stack offset), and the type associated with |
| 123 |
the variable. This table is integrated into the program using the |
| 124 |
abstract pseudo-op mechanism. An interesting aspect of these tables |
| 125 |
is that they may need adjustment based on the results of register |
| 126 |
spilling. |
| 127 |
|
| 128 |
The more convention use of the psuedo-op abstraction is to |
| 129 |
propagate function prologue and epilogue information. |
| 130 |
|
| 131 |
The constants abstraction are created by a tree node called |
| 132 |
\sml{CONST}. In the SML/NJ compiler, the tree that communicates |
| 133 |
garbage collection information looks like: |
| 134 |
|
| 135 |
\begin{verbatim} |
| 136 |
MV(32, maskReg, CONST{r110,r200,r300,r400 ...}) |
| 137 |
\end{verbatim} |
| 138 |
|
| 139 |
where \sml{maskReg} is a dedicated register. On the DEC Alpha, |
| 140 |
this would get translated to: |
| 141 |
|
| 142 |
\begin{verbatim} |
| 143 |
LDA maskReg, {encode(r110,r200,r300,r400, ...)} |
| 144 |
\end{verbatim} |
| 145 |
|
| 146 |
which indicates that the alpha instruction set (and optimization |
| 147 |
suite) know about these types of values. Further, after |
| 148 |
register allocation, the \sml{LDA} instruction may not be |
| 149 |
sufficient as the encoding may result in a value that is too large |
| 150 |
as an operand to \sml{LDA}. Two instructions may ultimately be |
| 151 |
required to load the encoding into the \sml{maskReg} |
| 152 |
register. This expansion is done during |
| 153 |
\href{span-dep.html}{span-dependency resolution}. |
| 154 |
|
| 155 |
All these examples are intended to indicate that one |
| 156 |
intermediate representation and optimization suite does not fit |
| 157 |
all, but that the intermediate representation and optimization |
| 158 |
suite needs to be specialized to the needs of the client. |