4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta name="generator" content="AsciiDoc 8.6.9">
6 <title>LocalRef</title>
7 <link rel="stylesheet" href="./asciidoc.css" type="text/css">
8 <link rel="stylesheet" href="./pygments.css" type="text/css">
11 <script type="text/javascript" src="./asciidoc.js"></script>
12 <script type="text/javascript">
17 <link rel="stylesheet" href="./mlton.css" type="text/css">
19 <body class="article">
21 <div id="banner-home">
22 <a href="./Home">MLton 20180207</a>
30 <div class="sectionbody">
31 <div class="paragraph"><p><a href="LocalRef">LocalRef</a> is an optimization pass for the <a href="SSA">SSA</a>
32 <a href="IntermediateLanguage">IntermediateLanguage</a>, invoked from <a href="SSASimplify">SSASimplify</a>.</p></div>
36 <h2 id="_description">Description</h2>
37 <div class="sectionbody">
38 <div class="paragraph"><p>This pass optimizes <span class="monospaced">ref</span> cells local to a <a href="SSA">SSA</a> function:</p></div>
39 <div class="ulist"><ul>
42 global <span class="monospaced">ref</span>-s only used in one function are moved to the function
47 <span class="monospaced">ref</span>-s only created, read from, and written to (i.e., don’t escape)
48 are converted into function local variables
52 <div class="paragraph"><p>Uses <a href="Multi">Multi</a> and <a href="Restore">Restore</a>.</p></div>
56 <h2 id="_implementation">Implementation</h2>
57 <div class="sectionbody">
58 <div class="ulist"><ul>
61 <a href="https://github.com/MLton/mlton/blob/master/mlton/ssa/local-ref.fun"><span class="monospaced">local-ref.fun</span></a>
68 <h2 id="_details_and_notes">Details and Notes</h2>
69 <div class="sectionbody">
70 <div class="paragraph"><p>Moving a global <span class="monospaced">ref</span> requires the <a href="Multi">Multi</a> analysis, because a
71 global <span class="monospaced">ref</span> can only be moved into a function that is executed at
73 <div class="paragraph"><p>Conversion of non-escaping <span class="monospaced">ref</span>-s is structured in three phases:</p></div>
74 <div class="ulist"><ul>
77 analysis — a variable <span class="monospaced">r = Ref_ref x</span> escapes if
79 <div class="ulist"><ul>
82 <span class="monospaced">r</span> is used in any context besides <span class="monospaced">Ref_assign (r, _)</span> or <span class="monospaced">Ref_deref r</span>
87 all uses <span class="monospaced">r</span> reachable from a (direct or indirect) call to <span class="monospaced">Thread_copyCurrent</span> are of the same flavor (either <span class="monospaced">Ref_assign</span> or <span class="monospaced">Ref_deref</span>); this also requires the <a href="Multi">Multi</a> analysis.
96 <div class="openblock">
98 <div class="ulist"><ul>
101 rewrites <span class="monospaced">r = Ref_ref x</span> to <span class="monospaced">r = x</span>
106 rewrites <span class="monospaced">_ = Ref_assign (r, y)</span> to <span class="monospaced">r = y</span>
111 rewrites <span class="monospaced">z = Ref_deref r</span> to <span class="monospaced">z = r</span>
116 <div class="paragraph"><p>Note that the resulting program violates the SSA condition.</p></div>
120 <a href="Restore">Restore</a> — restore the SSA condition.
127 <div id="footnotes"><hr></div>
129 <div id="footer-text">
131 <div id="footer-badges">