| 1 |
\section{The MLTREE Language} |
| 2 |
|
| 3 |
\newdef{MLTree} is the |
| 4 |
register transfer language used in the MLRISC system. |
| 5 |
It serves two important purposes: |
| 6 |
\image{MLTree}{pictures/png/mlrisc-ir.png}{align=right} |
| 7 |
\begin{enumerate} |
| 8 |
\item As an intermediate representation for a compiler front-end |
| 9 |
to talk to the MLRISC system, |
| 10 |
\item As specifications for instruction semantics |
| 11 |
\end{enumerate} |
| 12 |
The latter is needed for optimizations which require precise knowledge of such; |
| 13 |
for example, algebraic simplification and constant folding. |
| 14 |
|
| 15 |
MLTree is a low-level \newdef{typed} language: |
| 16 |
all operations are typed by its width or precision. |
| 17 |
Operations on floating point, integer, and condition code |
| 18 |
are also segregated, to prevent accidental misuse. |
| 19 |
MLTree is also \emph{tree-oriented} so that it is possible to write efficient |
| 20 |
MLTree transformation routines that uses SML pattern matching. |
| 21 |
|
| 22 |
Here are a few examples of MLTree statements. |
| 23 |
\begin{SML} |
| 24 |
MV(32,t, |
| 25 |
ADDT(32, |
| 26 |
MULT(32,REG(32,b),REG(32,b)), |
| 27 |
MULT(32, |
| 28 |
MULT(32,LI(4),REG(32,a)),REG(32,c)))) |
| 29 |
\end{SML} |
| 30 |
computes \sml{t := b*b + 4*a*c}, all in 32-bit precision and overflow |
| 31 |
trap enabled; while |
| 32 |
\begin{SML} |
| 33 |
MV(32,t, |
| 34 |
ADD(32, |
| 35 |
CVTI2I(32,SIGN_EXTEND,8, |
| 36 |
LOAD(8, |
| 37 |
ADD(32,REG(32,a),REG(32,i)))))) |
| 38 |
\end{SML} |
| 39 |
loads the byte in address \sml{a+i} and sign extend it to a 32-bit |
| 40 |
value. |
| 41 |
|
| 42 |
The statement |
| 43 |
\begin{SML} |
| 44 |
IF([],CMP(64,GE,REG(64,a),LI 0), |
| 45 |
MV(64, t, REG(64, a)), |
| 46 |
MV(64, t, NEG(64, REG(64, a))) |
| 47 |
) |
| 48 |
\end{SML} |
| 49 |
in more traditional form means: |
| 50 |
\begin{verbatim} |
| 51 |
if a >= 0 then |
| 52 |
t := a |
| 53 |
else |
| 54 |
t := -a |
| 55 |
\end{verbatim} |
| 56 |
This example can be also expressed in a few different ways: |
| 57 |
\begin{enumerate} |
| 58 |
\item With the conditional move construct described in |
| 59 |
Section~\ref{sec:cond-move}: |
| 60 |
\begin{SML} |
| 61 |
MV(64, t, |
| 62 |
COND(CMP(64, GE, REG(64, a)), |
| 63 |
REG(64, a), |
| 64 |
NEG(64, REG(64, a)))) |
| 65 |
\end{SML} |
| 66 |
\item With explicit branching using the conditional branch |
| 67 |
construct \verb|BCC|: |
| 68 |
\begin{SML} |
| 69 |
MV(64, t, REG(64, a)); |
| 70 |
BCC([], CMP(64, GE, REG(64, a)), L1); |
| 71 |
MV(64, t, NEG(64, REG(64, a))); |
| 72 |
DEFINE L1; |
| 73 |
\end{SML} |
| 74 |
\end{enumerate} |
| 75 |
\subsection{The Definitions} |
| 76 |
|
| 77 |
MLTree is defined in the signature \mlrischref{mltree/mltree.sig}{\sml{MLTREE}} |
| 78 |
and the functor \mlrischref{mltree/mltree.sml}{\sml{MLTreeF}} |
| 79 |
|
| 80 |
The functor \sml{MLTreeF} is parameterized in terms of |
| 81 |
the label expression type, the client supplied region datatype, |
| 82 |
the instruction stream type, and the client defined MLTree extensions. |
| 83 |
\begin{SML} |
| 84 |
functor MLTreeF |
| 85 |
(structure LabelExp : \href{labelexp.html}{LABELEXP} |
| 86 |
structure Region : \href{regions.html}{REGION} |
| 87 |
structure Stream : \href{streams.html}{INSTRUCTION_STREAM} |
| 88 |
structure Extension : \mlrischref{mltree/mltree-extension.sig}{MLTREE_EXTENSION} |
| 89 |
) : MLTREE |
| 90 |
\end{SML} |
| 91 |
|
| 92 |
\subsubsection{Basic Types} |
| 93 |
|
| 94 |
The basic types in MLTree are statements (\newtype{stm}) |
| 95 |
integer expressions (\newtype{rexp}), |
| 96 |
floating point expression (\newtype{fexp}), |
| 97 |
and conditional expressions (\newtype{ccexp}). |
| 98 |
Statements are evaluated for their effects, |
| 99 |
while expressions are evaluated for their value. (Some expressions |
| 100 |
could also have trapping effects. The semantics of traps are unspecified.) |
| 101 |
These types are parameterized by an extension |
| 102 |
type, which we can use to extend the set of MLTree |
| 103 |
operators. How this is used is described in Section~\ref{sec:mltree-extension}. |
| 104 |
|
| 105 |
References to registers are represented internally as integers, and are denoted |
| 106 |
as the type \sml{reg}. In addition, we use the types \sml{src} and \sml{dst} |
| 107 |
as abbreviations for source and destination registers. |
| 108 |
\begin{SML} |
| 109 |
type reg = int |
| 110 |
type src = reg |
| 111 |
type dst = reg |
| 112 |
\end{SML} |
| 113 |
|
| 114 |
All operators on MLTree are \emph{typed} |
| 115 |
by the number of bits that |
| 116 |
they work on. For example, 32-bit addition between \sml{a} and \sml{b} |
| 117 |
is written as \sml{ADD(32,a,b)}, while 64-bit addition between the same |
| 118 |
is written as \sml{ADD(64,a,b)}. Floating point operations are |
| 119 |
denoted in the same manner. For example, IEEE single-precision floating |
| 120 |
point add is written as \sml{FADD(32,a,b)}, while the same in |
| 121 |
double-precision is written as \sml{FADD(64,a,b)} |
| 122 |
|
| 123 |
Note that these types are low level. Higher level distinctions such |
| 124 |
as signed and unsigned integer value, are not distinguished by the type. |
| 125 |
Instead, operators are usually partitioned into signed and unsigned versions, |
| 126 |
and it is legal (and often useful!) to mix signed and unsigned operators in |
| 127 |
an expression. |
| 128 |
|
| 129 |
Currently, we don't provide a direct way to specify non-IEEE floating point |
| 130 |
together with |
| 131 |
IEEE floating point arithmetic. If this distinction is needed then |
| 132 |
it can be encoded using the extension mechanism described |
| 133 |
in Section~\ref{sec:mltree-extension}. |
| 134 |
|
| 135 |
We use the types \sml{ty} and \sml{fty} to stand for the number of |
| 136 |
bits in integer and floating point operations. |
| 137 |
\begin{SML} |
| 138 |
type ty = int |
| 139 |
type fty = int |
| 140 |
\end{SML} |
| 141 |
|
| 142 |
\subsubsection{The Basis} |
| 143 |
The signature \mlrischref{mltree/mltree-basis.sig}{MLTREE\_BASIS} |
| 144 |
defines the basic helper types used in the MLTREE signature. |
| 145 |
\begin{SML} |
| 146 |
signature MLTREE_BASIS = |
| 147 |
sig |
| 148 |
|
| 149 |
datatype cond = LT | LTU | LE | LEU | EQ | NE | GE | GEU | GT | GTU |
| 150 |
|
| 151 |
datatype fcond = |
| 152 |
? | !<=> | == | ?= | !<> | !?>= | < | ?< | !>= | !?> | |
| 153 |
<= | ?<= | !> | !?<= | > | ?> | !<= | !?< | >= | ?>= | |
| 154 |
!< | !?= | <> | != | !? | <=> | ?<> |
| 155 |
|
| 156 |
datatype ext = SIGN_EXTEND | ZERO_EXTEND |
| 157 |
|
| 158 |
datatype rounding_mode = TO_NEAREST | TO_NEGINF | TO_POSINF | TO_ZERO |
| 159 |
|
| 160 |
type ty = int |
| 161 |
type fty = int |
| 162 |
|
| 163 |
end |
| 164 |
\end{SML} |
| 165 |
|
| 166 |
The most important of these are the |
| 167 |
types \newtype{cond} and \newtype{fcond}, which represent the set of integer |
| 168 |
and floating point comparisions. These types can be combined with |
| 169 |
the comparison constructors \verb|CMP| and \verb|FCMP| to form |
| 170 |
integer and floating point comparisions. |
| 171 |
\begin{Table}{|c|c|}{align=left} \hline |
| 172 |
Operator & Comparison \\ \hline |
| 173 |
\sml{LT} & Signed less than \\ |
| 174 |
\sml{LTU} & Unsigned less than \\ |
| 175 |
\sml{LE} & Signed less than or equal \\ |
| 176 |
\sml{LEU} & Unsigned less than or equal \\ |
| 177 |
\sml{EQ} & Equal \\ |
| 178 |
\sml{NE} & Not equal \\ |
| 179 |
\sml{GE} & Signed greater than or equal \\ |
| 180 |
\sml{GEU} & Unsigned greater than or equal \\ |
| 181 |
\sml{GT} & Signed greater than \\ |
| 182 |
\sml{GTU} & Unsigned greater than \\ |
| 183 |
\hline |
| 184 |
\end{Table} |
| 185 |
|
| 186 |
Floating point comparisons can be ``decoded'' as follows. |
| 187 |
In IEEE floating point, there are four different basic comparisons |
| 188 |
tests that we can performed given two numbers $a$ and $y$: |
| 189 |
\begin{description} |
| 190 |
\item[$a < b$] Is $a$ less than $b$? |
| 191 |
\item[$a = b$] Is $a$ equal to $b$? |
| 192 |
\item[$a > b$] Is $a$ greater than to $b$? |
| 193 |
\item[$a ? b$] Are $a$ and $b$ unordered (incomparable)? |
| 194 |
\end{description} |
| 195 |
Comparisons can be joined together. For example, |
| 196 |
given two double-precision floating point expressions $a$ and $b$, |
| 197 |
the expression \verb|FCMP(64,<=>,a,b)| |
| 198 |
asks whether $a$ is less than, equal to or greater than $b$, i.e.~whether |
| 199 |
$a$ and $b$ are comparable. |
| 200 |
The special symbol \verb|!| negates |
| 201 |
the meaning the of comparison. For example, \verb|FCMP(64,!>=,a,b)| |
| 202 |
means testing whether $a$ is less than or incomparable with $b$. |
| 203 |
|
| 204 |
\subsection{Integer Expressions} |
| 205 |
|
| 206 |
A reference to the $i$th |
| 207 |
integer register with an $n$-bit value is written |
| 208 |
as \sml{REG(}$n$,$i$\sml{)}. The operators \sml{LI}, \sml{LI32}, |
| 209 |
and \sml{LABEL}, \sml{CONST} are used to represent constant expressions |
| 210 |
of various forms. The sizes of these constants are inferred from context. |
| 211 |
\begin{SML} |
| 212 |
REG : ty * reg -> rexp |
| 213 |
LI : int -> rexp |
| 214 |
LI32 : Word32.word -> rexp |
| 215 |
LABEL : LabelExp.labexp -> rexp |
| 216 |
CONST : Constant.const -> rexp |
| 217 |
\end{SML} |
| 218 |
|
| 219 |
The following figure lists all the basic integer operators and their |
| 220 |
intuitive meanings. All operators except \sml{NOTB, NEG, NEGT} are binary |
| 221 |
and have the type |
| 222 |
\begin{SML} |
| 223 |
ty * rexp * rexp -> rexp |
| 224 |
\end{SML} |
| 225 |
The operators \sml{NOTB, NEG, NEGT} have the type |
| 226 |
\begin{SML} |
| 227 |
ty * rexp -> rexp |
| 228 |
\end{SML} |
| 229 |
|
| 230 |
\begin{tabular}{|l|l|} \hline |
| 231 |
\sml{ADD} & Twos complement addition \\ |
| 232 |
\sml{NEG} & negation \\ |
| 233 |
\sml{SUB} & Twos complement subtraction \\ |
| 234 |
\sml{MULS} & Signed multiplication \\ |
| 235 |
\sml{DIVS} & Signed division, round to zero (nontrapping) \\ |
| 236 |
\sml{QUOTS} & Signed division, round to negative infinity (nontrapping) \\ |
| 237 |
\sml{REMS} & Signed remainder (???) \\ |
| 238 |
\sml{MULU} & Unsigned multiplication \\ |
| 239 |
\sml{DIVU} & Unsigned division \\ |
| 240 |
\sml{REMU} & Unsigned remainder \\ |
| 241 |
\sml{NEGT} & signed negation, trap on overflow \\ |
| 242 |
\sml{ADDT} & Signed addition, trap on overflow \\ |
| 243 |
\sml{SUBT} & Signed subtraction, trap on overflow \\ |
| 244 |
\sml{MULT} & Signed multiplication, trap on overflow \\ |
| 245 |
\sml{DIVT} & Signed division, round to zero, |
| 246 |
trap on overflow or division by zero \\ |
| 247 |
\sml{QUOTT} & Signed division, round to negative infinity, trap on overflow or division by zero \\ |
| 248 |
\sml{REMT} & Signed remainder, trap on division by zero \\ |
| 249 |
\sml{ANDB} & bitwise and \\ |
| 250 |
\sml{ORB} & bitwise or \\ |
| 251 |
\sml{XORB} & bitwise exclusive or \\ |
| 252 |
\sml{NOTB} & ones complement \\ |
| 253 |
\sml{SRA} & arithmetic right shift \\ |
| 254 |
\sml{SRL} & logical right shift \\ |
| 255 |
\sml{SLL} & logical left shift \\ |
| 256 |
\hline\end{tabular} |
| 257 |
|
| 258 |
\subsubsection{Sign and Zero Extension} |
| 259 |
Sign extension and zero extension are written using the operator |
| 260 |
\sml{CVTI2I}. \sml{CVTI2I(}$m$,\sml{SIGN_EXTEND},$n$,$e$\sml{)} |
| 261 |
sign extends the $n$-bit value $e$ to an $m$-bit value, i.e. the |
| 262 |
$n-1$th bit is of $e$ is treated as the sign bit. Similarly, |
| 263 |
\sml{CVTI2I(}$m$,\sml{ZERO_EXTEND},$n$,$e$\sml{)} |
| 264 |
zero extends an $n$-bit value to an $m$-bit |
| 265 |
value. If $m \le n$, then |
| 266 |
\sml{CVTI2I(}$m$,\sml{SIGN_EXTEND},$n$,$e$\sml{)} = |
| 267 |
\sml{CVTI2I}($m$,\sml{ZERO_EXTEND},$n$,$e$\sml{)}. |
| 268 |
|
| 269 |
\begin{SML} |
| 270 |
datatype ext = SIGN_EXTEND | ZERO_EXTEND |
| 271 |
CVTI2I : ty * ext * ty * rexp -> rexp |
| 272 |
\end{SML} |
| 273 |
|
| 274 |
\subsubsection{Conditional Move} \label{sec:cond-move} |
| 275 |
Most new superscalar architectures incorporate conditional move |
| 276 |
instructions in their ISAs. |
| 277 |
Modern VLIW architectures also directly support full predication. |
| 278 |
Since branching (especially with data dependent branches) can |
| 279 |
introduce extra latencies in highly pipelined architectures, |
| 280 |
condtional moves should be used in place of short branch sequences. |
| 281 |
MLTree provide a conditional move instruction \sml{COND}, |
| 282 |
to make it possible to directly express conditional moves without using |
| 283 |
branches. |
| 284 |
\begin{SML} |
| 285 |
COND : ty * ccexp * rexp * rexp -> rexp |
| 286 |
\end{SML} |
| 287 |
|
| 288 |
Semantically, \sml{COND(}\emph{ty},\emph{cc},$a$,$b$\sml{)} means to evaluate |
| 289 |
\emph{cc}, and if \emph{cc} evaluates to true then the value of the entire expression is |
| 290 |
$a$; otherwise the value is $b$. Note that $a$ and $b$ are allowed to be |
| 291 |
\emph{eagerly} |
| 292 |
evaluated. In fact, we are allowed to evaluate to \emph{both} |
| 293 |
branches, one branch, or neither~\footnote{When possible.}. |
| 294 |
|
| 295 |
Various idioms of the \sml{COND} form are useful for expressing common |
| 296 |
constructs in many programming languages. For example, MLTree does not |
| 297 |
provide a primitive construct for converting an integer value \sml{x} to a |
| 298 |
boolean value (0 or 1). But using \sml{COND}, this is expressible as |
| 299 |
\sml{COND(32,CMP(32,NE,x,LI 0),LI 1,LI 0)}. SML/NJ represents |
| 300 |
the boolean values true and false as machine integers 3 and 1 respectively. |
| 301 |
To convert a boolean condition $e$ into an ML boolean value, we can use |
| 302 |
\begin{SML} |
| 303 |
COND(32,e,LI 3,LI 1) |
| 304 |
\end{SML} |
| 305 |
|
| 306 |
Common C idioms can be easily mapped into the \sml{COND} form. For example, |
| 307 |
\begin{itemize} |
| 308 |
\item \verb|if (e1) x = y| translates into |
| 309 |
\sml{MV(32,x,COND(32,e1,REG(32,y),REG(32,x)))} |
| 310 |
\item |
| 311 |
\begin{verbatim} |
| 312 |
x = e1; |
| 313 |
if (e2) x = y |
| 314 |
\end{verbatim} |
| 315 |
translates into |
| 316 |
\sml{MV(32,x,COND(32,e2,REG(32,y),e1))} |
| 317 |
\item \verb|x = e1 == e2| translates into |
| 318 |
\sml{MV(32,x,COND(32,CMP(32,EQ,e1,e2),LI 1,LI 0)} |
| 319 |
\item \verb|x = ! e| translates into |
| 320 |
\sml{MV(32,x,COND(32,CMP(32,NE,e,LI 0),LI 1,LI 0)} |
| 321 |
\item \verb|x = e ? y : z| translates into |
| 322 |
\sml{MV(32,x,COND(32,e,REG(32,y),REG(32,z)))}, and |
| 323 |
\item \verb|x = y < z ? y : z| translates into |
| 324 |
\begin{alltt} |
| 325 |
MV(32,x, |
| 326 |
COND(32, |
| 327 |
CMP(32,LT,REG(32,y),REG(32,z)), |
| 328 |
REG(32,y),REG(32,z))) |
| 329 |
\end{alltt} |
| 330 |
\end{itemize} |
| 331 |
|
| 332 |
In general, the \sml{COND} form should be used in place of MLTree's branching |
| 333 |
constructs whenever possible, since the former is usually highly |
| 334 |
optimized in various MLRISC backends. |
| 335 |
|
| 336 |
\subsubsection{Integer Loads} |
| 337 |
|
| 338 |
Integer loads are written using the constructor \verb|LOAD|. |
| 339 |
\begin{SML} |
| 340 |
LOAD : ty * rexp * Region.region -> rexp |
| 341 |
\end{SML} |
| 342 |
The client is required to specify a \href{regions.html}{region} that |
| 343 |
serves as aliasing information for the load. |
| 344 |
|
| 345 |
\subsubsection{Miscellaneous Integer Operators} |
| 346 |
|
| 347 |
An expression of the \sml{LET}($s$,$e$) evaluates the statement $s$ for |
| 348 |
its effect, and then return the value of expression $e$. |
| 349 |
\begin{SML} |
| 350 |
LET : stm * rexp -> rexp |
| 351 |
\end{SML} |
| 352 |
Since the order of evaluation is MLTree operators are |
| 353 |
\emph{unspecified} |
| 354 |
the use of this operator should be severely restricted to only |
| 355 |
\emph{side-effect}-free forms. |
| 356 |
|
| 357 |
\subsection{Floating Point Expressions} |
| 358 |
|
| 359 |
Floating registers are referenced using the term \sml{FREG}. The |
| 360 |
$i$th floating point register with type $n$ is written |
| 361 |
as \sml{FREG(}$n$,$i$\sml{)}. |
| 362 |
\begin{SML} |
| 363 |
FREG : fty * src -> fexp |
| 364 |
\end{SML} |
| 365 |
|
| 366 |
Built-in floating point operations include addition (\sml{FADD}), |
| 367 |
subtraction (\sml{FSUB}), multiplication (\sml{FMUL}), division |
| 368 |
(\sml{FDIV}), absolute value (\sml{FABS}), negation (\sml{FNEG}) |
| 369 |
and square root (\sml{FSQRT}). |
| 370 |
\begin{SML} |
| 371 |
FADD : fty * fexp * fexp -> fexp |
| 372 |
FSUB : fty * fexp * fexp -> fexp |
| 373 |
FMUL : fty * fexp * fexp -> fexp |
| 374 |
FDIV : fty * fexp * fexp -> fexp |
| 375 |
FABS : fty * fexp -> fexp |
| 376 |
FNEG : fty * fexp -> fexp |
| 377 |
FSQRT : fty * fexp -> fexp |
| 378 |
\end{SML} |
| 379 |
|
| 380 |
A special operator is provided for manipulating signs. |
| 381 |
To combine the sign of $a$ with the magnitude of $b$, we can |
| 382 |
write \sml{FCOPYSIGN(}$a$,$b$\sml{)}\footnote{What should |
| 383 |
happen if $a$ or $b$ is nan?}. |
| 384 |
\begin{SML} |
| 385 |
FCOPYSIGN : fty * fexp * fexp -> fexp |
| 386 |
\end{SML} |
| 387 |
|
| 388 |
To convert an $n$-bit signed integer $e$ into an $m$-bit floating point value, |
| 389 |
we can write \sml{CVTI2F(}$m$,$n$,$e$\sml{)}\footnote{What happen to unsigned integers?}. |
| 390 |
\begin{SML} |
| 391 |
CVTI2F : fty * ty * rexp -> fexp |
| 392 |
\end{SML} |
| 393 |
|
| 394 |
Similarly, to convert an $n$-bit floating point value $e$ to an $m$-bit |
| 395 |
floating point value, we can write \sml{CVTF2F(}$m$,$n$,$e$\sml{)}\footnote{ |
| 396 |
What is the rounding semantics?}. |
| 397 |
\begin{SML} |
| 398 |
CVTF2F : fty * fty * -> fexp |
| 399 |
\end{SML} |
| 400 |
|
| 401 |
\begin{SML} |
| 402 |
datatype rounding_mode = TO_NEAREST | TO_NEGINF | TO_POSINF | TO_ZERO |
| 403 |
CVTF2I : ty * rounding_mode * fty * fexp -> rexp |
| 404 |
\end{SML} |
| 405 |
|
| 406 |
\begin{SML} |
| 407 |
FLOAD : fty * rexp * Region.region -> fexp |
| 408 |
\end{SML} |
| 409 |
|
| 410 |
\subsection{Condition Expressions} |
| 411 |
Unlike languages like C, MLTree makes the distinction between condition |
| 412 |
expressions and integer expressions. This distinction is necessary for |
| 413 |
two purposes: |
| 414 |
\begin{itemize} |
| 415 |
\item It clarifies the proper meaning intended in a program, and |
| 416 |
\item It makes to possible for a MLRISC backend to map condition |
| 417 |
expressions efficiently onto various machine architectures with different |
| 418 |
condition code models. For example, architectures like the Intel x86, |
| 419 |
Sparc V8, and PowerPC contains dedicated condition code registers, which |
| 420 |
are read from and written to by branching and comparison instructions. |
| 421 |
On the other hand, architectures such as the Texas Instrument C6, PA RISC, |
| 422 |
Sparc V9, and Alpha does not include dedicated condition code registers. |
| 423 |
Conditional code registers in these architectures |
| 424 |
can be simulated by integer registers. |
| 425 |
\end{itemize} |
| 426 |
|
| 427 |
|
| 428 |
A conditional code register bit can be referenced using the constructors |
| 429 |
\sml{CC} and \sml{FCC}. Note that the \emph{condition} must be specified |
| 430 |
together with the condition code register. |
| 431 |
\begin{SML} |
| 432 |
CC : Basis.cond * src -> ccexp |
| 433 |
FCC : Basis.fcond * src -> ccexp |
| 434 |
\end{SML} |
| 435 |
For example, to test the \verb|Z| bit of the \verb|%psr| register on the |
| 436 |
Sparc architecture, we can used \sml{CC(EQ,SparcCells.psr)}. |
| 437 |
|
| 438 |
The comparison operators \sml{CMP} and \sml{FCMP} performs integer and |
| 439 |
floating point tests. Both of these are \emph{typed} by the precision |
| 440 |
in which the test must be performed under. |
| 441 |
\begin{SML} |
| 442 |
CMP : ty * Basis.cond * rexp * rexp -> ccexp |
| 443 |
FCMP : fty * Basis.fcond * fexp * fexp -> ccexp |
| 444 |
\end{SML} |
| 445 |
|
| 446 |
Condition code expressions may be combined with the following |
| 447 |
logical connectives, which have the obvious meanings. |
| 448 |
\begin{SML} |
| 449 |
TRUE : ccexp |
| 450 |
FALSE : ccexp |
| 451 |
NOT : ccexp -> ccexp |
| 452 |
AND : ccexp * ccexp -> ccexp |
| 453 |
OR : ccexp * ccexp -> ccexp |
| 454 |
XOR : ccexp * ccexp -> ccexp |
| 455 |
\end{SML} |
| 456 |
|
| 457 |
\subsection{Statements} |
| 458 |
|
| 459 |
Statement forms in MLTree includes assignments, parallel copies, |
| 460 |
jumps and condition branches, calls and returns, stores, sequencing, |
| 461 |
and annotation. |
| 462 |
|
| 463 |
\subsubsection{Assignments} |
| 464 |
|
| 465 |
Assignments are segregated among the integer, floating point and |
| 466 |
conditional code types. In addition, all assignments are \emph{typed} |
| 467 |
by the precision of destination register. |
| 468 |
|
| 469 |
\begin{SML} |
| 470 |
MV : ty * dst * rexp -> stm |
| 471 |
FMV : fty * dst * fexp -> stm |
| 472 |
CCMV : dst * ccexp -> stm |
| 473 |
\end{SML} |
| 474 |
|
| 475 |
\subsubsection{Parallel Copies} |
| 476 |
|
| 477 |
Special forms are provided for parallel copies for integer and |
| 478 |
floating point registers. It is important to emphasize that |
| 479 |
the semantics is that all assignments are performed in parallel. |
| 480 |
|
| 481 |
\begin{SML} |
| 482 |
COPY : ty * dst list * src list -> stm |
| 483 |
FCOPY : fty * dst list * src list -> stm |
| 484 |
\end{SML} |
| 485 |
|
| 486 |
\subsubsection{Jumps and Conditional Branches} |
| 487 |
|
| 488 |
Jumps and conditional branches in MLTree take two additional set of |
| 489 |
annotations. The first represents the \newdef{control flow} and is denoted |
| 490 |
by the type \sml{controlflow}. The second represent |
| 491 |
\newdef{control-dependence} and \newdef{anti-control-dependence} |
| 492 |
and is denoted by the type \sml{ctrl}. |
| 493 |
|
| 494 |
\begin{SML} |
| 495 |
type controlflow = Label.label list |
| 496 |
type ctrl = reg list |
| 497 |
\end{SML} |
| 498 |
Control flow annotation is simply a list of labels, which represents |
| 499 |
the set of possible targets of the associated jump. Control dependence |
| 500 |
annotations attached to a branch or jump instruction represents the |
| 501 |
new definition of \newdef{pseudo control dependence predicates}. These |
| 502 |
predicates have no associated dynamic semantics; rather they are used |
| 503 |
to constraint the set of potential code motion in an optimizer |
| 504 |
(more on this later). |
| 505 |
|
| 506 |
The primitive jumps and conditional branch forms are represented |
| 507 |
by the constructors \sml{JMP}, \sml{BCC}. |
| 508 |
\begin{SML} |
| 509 |
JMP : ctrl * rexp * controlflow -> stm |
| 510 |
BCC : ctrl * ccexp * Label.label -> stm |
| 511 |
\end{SML} |
| 512 |
|
| 513 |
In addition to \sml{JMP} and \sml{BCC}, |
| 514 |
there is a \emph{structured} if/then/else statement. |
| 515 |
\begin{SML} |
| 516 |
IF : ctrl * ccexp * stm * stm -> stm |
| 517 |
\end{SML} |
| 518 |
|
| 519 |
Semantically, \sml{IF}($c,x,y,z$) is identical to |
| 520 |
\begin{SML} |
| 521 |
BCC(\(c\), \(x\), L1) |
| 522 |
\(z\) |
| 523 |
JMP([], L2) |
| 524 |
DEFINE L1 |
| 525 |
\(y\) |
| 526 |
DEFINE L2 |
| 527 |
\end{SML} |
| 528 |
where \verb|L1| and \verb|L2| are new labels, as expected. |
| 529 |
|
| 530 |
Here's an example of how control dependence predicates are used. |
| 531 |
Consider the following MLTree statement: |
| 532 |
\begin{SML} |
| 533 |
IF([p], CMP(32, NE, REG(32, a), LI 0), |
| 534 |
MV(32, b, PRED(LOAD(32, m, ...)), p), |
| 535 |
MV(32, b, LOAD(32, n, ...))) |
| 536 |
\end{SML} |
| 537 |
In the first alternative of the \verb|IF|, the \verb|LOAD| |
| 538 |
expression is constrainted by the control dependence |
| 539 |
predicate \verb|p| defined in the \verb|IF|, |
| 540 |
using the predicate constructor \verb|PRED|. These states that |
| 541 |
the load is \emph{control dependent} on the test of the branch, |
| 542 |
and thus it may not be legally hoisted above the branch without |
| 543 |
potentially violating the semantics of the program. |
| 544 |
For example, |
| 545 |
semantics violation may happen if the value of \verb|m| and \verb|a| |
| 546 |
is corrolated, and whenever \verb|a| = 0, the address in \verb|m| is |
| 547 |
not a legal address. |
| 548 |
|
| 549 |
Note that on architectures with speculative loads, |
| 550 |
the control dependence information can be used to |
| 551 |
guide the transformation of control dependent loads into speculative loads. |
| 552 |
|
| 553 |
Now in constrast, the \verb|LOAD| in the second alternative is not |
| 554 |
control dependent on the control dependent predicate \verb|p|, and |
| 555 |
thus it is safe and legal to hoist the load above the test, as in |
| 556 |
\begin{SML} |
| 557 |
MV(32, b, LOAD(32, n, ...)); |
| 558 |
IF([p], CMP(32, NE, REG(32, a), LI 0), |
| 559 |
MV(32, b, PRED(LOAD(32, m, ...)), p), |
| 560 |
SEQ [] |
| 561 |
) |
| 562 |
\end{SML} |
| 563 |
Of course, such transformation is only performed if the optimizer |
| 564 |
phases think that it can benefit performance. Thus the control dependence |
| 565 |
information does \emph{not} directly specify any transformations, but it |
| 566 |
is rather used to indicate when aggressive code motions are legal and safe. |
| 567 |
|
| 568 |
\subsubsection{Calls and Returns} |
| 569 |
|
| 570 |
Calls and returns in MLTree are specified using the constructors |
| 571 |
\verb|CALL| and \verb|RET|, which have the following types. |
| 572 |
\begin{SML} |
| 573 |
CALL : rexp * controlflow * mlrisc * mlrisc * |
| 574 |
ctrl * ctrl * Region.region -> stm |
| 575 |
RET : ctrl * controlflow -> stm |
| 576 |
\end{SML} |
| 577 |
|
| 578 |
The \verb|CALL| form is particularly complex, and require some explanation. |
| 579 |
Basically the seven parameters are, in order: |
| 580 |
\begin{description} |
| 581 |
\item[address] of the called routine. |
| 582 |
\item[control flow] annotation for this call. This information |
| 583 |
specifies the potential targets of this call instruction. Currently |
| 584 |
this information is ignored but will be useful for interprocedural |
| 585 |
optimizations in the future. |
| 586 |
\item[definition and use] These lists specify the list of |
| 587 |
potential definition and uses during the execution of the call. |
| 588 |
Definitions and uses are represented as the type \newtype{mlrisc} list. |
| 589 |
The contructors for this type is: |
| 590 |
\begin{SML} |
| 591 |
CCR : ccexp -> mlrisc |
| 592 |
GPR : rexp -> mlrisc |
| 593 |
FPR : fexp -> mlrisc |
| 594 |
\end{SML} |
| 595 |
\item[definition of control and anti-control dependence] |
| 596 |
These two lists specifies definitions of control and anti-control dependence. |
| 597 |
\item[region] annotation for the call, which summarizes |
| 598 |
the set of potential memory references during execution of the call. |
| 599 |
\end{description} |
| 600 |
|
| 601 |
The matching return statement constructor \verb|RET| has two |
| 602 |
arguments. These are: |
| 603 |
\begin{description} |
| 604 |
\item[anti-control dependence] This parameter represents |
| 605 |
the set of anti-control dependence predicates defined by the return |
| 606 |
statement. |
| 607 |
\item[control flow] This parameter specifies the set of matching |
| 608 |
procedure entry points of this return. For example, suppose we have |
| 609 |
a procedure with entry points \verb|f| and \verb|f'|. |
| 610 |
Then the MLTree statements |
| 611 |
\begin{verbatim} |
| 612 |
f: ... |
| 613 |
JMP L1 |
| 614 |
f': ... |
| 615 |
L1: ... |
| 616 |
RET ([], [f, f']) |
| 617 |
\end{verbatim} |
| 618 |
\noindent can be used to specify that the return is either from |
| 619 |
the entries \verb|f| or \verb|f'|. |
| 620 |
\end{description} |
| 621 |
|
| 622 |
\subsubsection{Stores} |
| 623 |
Stores to integer and floating points are specified using the |
| 624 |
constructors \verb|STORE| and \verb|FSTORE|. |
| 625 |
\begin{SML} |
| 626 |
STORE : ty * rexp * rexp * Region.region -> stm |
| 627 |
FSTORE : fty * rexp * fexp * Region.region -> stm |
| 628 |
\end{SML} |
| 629 |
|
| 630 |
The general form is |
| 631 |
\begin{SML} |
| 632 |
STORE(\(width\), \(address\), \(data\), \(region\)) |
| 633 |
\end{SML} |
| 634 |
|
| 635 |
Stores for condition codes are not provided. |
| 636 |
\subsubsection{Miscelleneous Statements} |
| 637 |
|
| 638 |
Other useful statement forms of MLTree are for sequencing (\verb|SEQ|), |
| 639 |
defining a local label (\verb|DEFINE|). |
| 640 |
\begin{SML} |
| 641 |
SEQ : stm list -> stm |
| 642 |
DEFINE : Label.label -> stm |
| 643 |
\end{SML} |
| 644 |
The constructor \sml{DEFINE L} has the same meaning as |
| 645 |
executing the method \sml{defineLabel L} in the |
| 646 |
\href{stream.html}{stream interface}. |
| 647 |
|
| 648 |
\subsection{Annotations} |
| 649 |
\href{annotations.html}{Annotations} are used as the generic mechanism for |
| 650 |
exchanging information between different phases of the MLRISC system, and |
| 651 |
between a compiler front end and the MLRISC back end. |
| 652 |
The following constructors can be used to annotate a MLTree term with |
| 653 |
an annotation: |
| 654 |
\begin{SML} |
| 655 |
MARK : rexp * Annotations.annotation -> rexp |
| 656 |
FMARK : fexp * Annotations.annotation -> fexp |
| 657 |
CCMARK : ccexp * Annotations.annotation -> ccexp |
| 658 |
ANNOTATION : stm * Annotations.annotation -> stm |
| 659 |
\end{SML} |