| 1 |
= LocalRef |
| 2 |
|
| 3 |
<<LocalRef#>> is an optimization pass for the <<SSA#>> |
| 4 |
<<IntermediateLanguage#>>, invoked from <<SSASimplify#>>. |
| 5 |
|
| 6 |
== Description |
| 7 |
|
| 8 |
This pass optimizes `ref` cells local to a <<SSA#>> function: |
| 9 |
|
| 10 |
* global `ref`-s only used in one function are moved to the function |
| 11 |
|
| 12 |
* `ref`-s only created, read from, and written to (i.e., don't escape) |
| 13 |
are converted into function local variables |
| 14 |
|
| 15 |
Uses <<Multi#>> and <<Restore#>>. |
| 16 |
|
| 17 |
== Implementation |
| 18 |
|
| 19 |
* https://github.com/MLton/mlton/blob/master/mlton/ssa/local-ref.fun[`local-ref.fun`] |
| 20 |
|
| 21 |
== Details and Notes |
| 22 |
|
| 23 |
Moving a global `ref` requires the <<Multi#>> analysis, because a |
| 24 |
global `ref` can only be moved into a function that is executed at |
| 25 |
most once. |
| 26 |
|
| 27 |
Conversion of non-escaping `ref`-s is structured in three phases: |
| 28 |
|
| 29 |
* analysis -- a variable `r = Ref_ref x` escapes if |
| 30 |
** `r` is used in any context besides `Ref_assign (r, _)` or `Ref_deref r` |
| 31 |
** all uses `r` reachable from a (direct or indirect) call to `Thread_copyCurrent` are of the same flavor (either `Ref_assign` or `Ref_deref`); this also requires the <<Multi#>> analysis. |
| 32 |
|
| 33 |
* transformation |
| 34 |
+ |
| 35 |
-- |
| 36 |
** rewrites `r = Ref_ref x` to `r = x` |
| 37 |
** rewrites `_ = Ref_assign (r, y)` to `r = y` |
| 38 |
** rewrites `z = Ref_deref r` to `z = r` |
| 39 |
-- |
| 40 |
+ |
| 41 |
Note that the resulting program violates the SSA condition. |
| 42 |
|
| 43 |
* <<Restore#>> -- restore the SSA condition. |