| 1 |
\section{The Alpha Back End} |
| 2 |
|
| 3 |
\subsection{Trap Shadows, Floating Exceptions, and Denormalized Numbers on the DEC Alpha} |
| 4 |
|
| 5 |
\emph{By Andrew W. Appel and Lal George, Nov 28, 1995} |
| 6 |
|
| 7 |
See section 4.7.5.1 of the \emph{Alpha Architecture Reference Manual}. |
| 8 |
|
| 9 |
The Alpha has imprecise exceptions, meaning that if a floating |
| 10 |
point instruction raises an IEEE exception, the exception may |
| 11 |
not interrupt the processor until several successive instructions have |
| 12 |
completed. ML, on the other hand, may want a "precise" model |
| 13 |
of floating point exceptions. |
| 14 |
|
| 15 |
Furthermore, the Alpha hardware does not support denormalized numbers |
| 16 |
(for ``gradual underflow''). Instead, underflow always rounds to zero. |
| 17 |
However, each floating operation (add, mult, etc.) has a trapping |
| 18 |
variant that will raise an exception (imprecisely, of course) on |
| 19 |
underflow; in that case, the instruction will produce a zero result |
| 20 |
AND an exception will occur. In fact, there are several variants |
| 21 |
of each instruction; three variants of MULT are: |
| 22 |
\begin{description} |
| 23 |
\item[MULT s1,s2,d] truncate denormalized result to zero; no exception |
| 24 |
\item[MULT/U s1,s2,d] truncate denormalized result to zero; raise UNDERFLOW |
| 25 |
\item[MULT/SU s1,s2,d] software completion, producing denormalized result |
| 26 |
\end{description} |
| 27 |
|
| 28 |
The hardware treats the \verb|MULT/U| and \verb|MULT/SU| |
| 29 |
instructions identically, |
| 30 |
truncating a denormalized result to zero and raising the UNDERFLOW |
| 31 |
exception. But the operating system, on an UNDERFLOW exception, |
| 32 |
examines the faulting instruction to see if it's an \verb|/SU| |
| 33 |
form, and if so, |
| 34 |
recalculates \verb|s1*s2|, puts the right answer in \verb|d|, and continues, |
| 35 |
all without invoking the user's signal handler. |
| 36 |
|
| 37 |
Because most machines compute with denormalized numbers in hardware, |
| 38 |
to maximize portability of SML programs, we use the \verb|MULT/SU| form. |
| 39 |
(and \verb|ADD/SU|, \verb|SUB/SU|, etc.) But to use this form successfully, |
| 40 |
certain rules have to be followed. Basically, d cannot be the same |
| 41 |
register as s1 or s2, because the opsys needs to be able to |
| 42 |
recalculate the operation using the original contents of s1 and s2, |
| 43 |
and the MULT/SU instruction will overwrite d even if it traps. |
| 44 |
|
| 45 |
More generally, we may want to have a sequence of floating-point |
| 46 |
instructions. The rules for such a sequence are: |
| 47 |
|
| 48 |
1. The sequence should end with a \verb|TRAPB| (trap barrier) instruction. |
| 49 |
(This could be relaxed somewhat, but certainly a \verb|TRAPB| would |
| 50 |
be a good idea sometime before the next branch instruction or |
| 51 |
update of an ML reference variable, or any other ML side effect.) |
| 52 |
2. No instruction in the sequence should destroy any operand of itself |
| 53 |
or of any previous instruction in the sequence. |
| 54 |
3. No two instructions in the sequence should write the same destination |
| 55 |
register. |
| 56 |
|
| 57 |
We can achieve these conditions by the following trick in the |
| 58 |
Alpha code generator. Each instruction in the sequence will write |
| 59 |
to a different temporary; this is guaranteed by the translation from |
| 60 |
ML-RISC. At the beginning of the sequence, we will put a special |
| 61 |
pseudo-instruction (we call it \verb|DEFFREG|) that ``defines'' |
| 62 |
the destination |
| 63 |
register of the arithmetic instruction. If there are $K$ arithmetic |
| 64 |
instructions in the sequence, then we'll insert $K$ |
| 65 |
\verb|DEFFREG| instructions |
| 66 |
all at the beginning of the sequence. |
| 67 |
Then, each arithop will not only ``define'' its destination temporary |
| 68 |
but will ``use'' it as well. When all these instructions are fed to |
| 69 |
the liveness analyzer, the resulting interference graph will then |
| 70 |
have inteference edges satisfying conditions 2 and 3 above. |
| 71 |
|
| 72 |
Of course, \verb|DEFFREG| doesn't actually generate any code. In our model |
| 73 |
of the Alpha, every instruction generates exactly 4 bytes of code |
| 74 |
except the ``span-dependent'' ones. Therefore, we'll specify \verb|DEFFREG| |
| 75 |
as a span-dependent instruction whose minimum and maximum sizes are zero. |
| 76 |
|
| 77 |
At the moment, we do not group arithmetic operations into sequences; |
| 78 |
that is, each arithop will be preceded by a single \verb|DEFFREG| and |
| 79 |
followed by a \verb|TRAPB|. To avoid the cost of all those \verb|TRAPB|'s, |
| 80 |
we should improve this when we have time. Warning: Don't put more |
| 81 |
than 31 instructions in the sequence, because they're all required |
| 82 |
to write to different destination registers! |
| 83 |
|
| 84 |
What about multiple traps? For example, suppose a sequence of |
| 85 |
instructions produces an Overflow and a Divide-by-Zero exception? |
| 86 |
ML would like to know only about the earliest trap, but the hardware |
| 87 |
will report \emph{BOTH} traps to the operating system. However, as long |
| 88 |
as the rules above are followed (and the software-completion versions |
| 89 |
of the arithmetic instructions are used), the operating system will |
| 90 |
have enough information to know which instruction produced the |
| 91 |
trap. It is very probable that the operating system will report \emph{ONLY} |
| 92 |
the earlier trap to the user process, but I'm not sure. |
| 93 |
|
| 94 |
For a hint about what the operating system is doing in its own |
| 95 |
trap-handler (with software completion), see section 6.3.2 of |
| 96 |
``\emph{OpenVMS Alpha Software}'' (Part II of the Alpha Architecture |
| 97 |
Manual). This stuff should apply to Unix (OSF1) as well as VMS. |