<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="AsciiDoc 8.6.8">
<title>KnownCase</title>
<link rel="stylesheet" href="./asciidoc.css" type="text/css">
<link rel="stylesheet" href="./pygments.css" type="text/css">


<script type="text/javascript" src="./asciidoc.js"></script>
<script type="text/javascript">
/*<![CDATA[*/
asciidoc.install();
/*]]>*/
</script>
<link rel="stylesheet" href="./mlton.css" type="text/css"/>
</head>
<body class="article">
<div id="banner">
<div id="banner-home">
<a href="./Home">MLton 20130715</a>
</div>
</div>
<div id="header">
<h1>KnownCase</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p><a href="KnownCase">KnownCase</a> is an optimization pass for the <a href="SSA">SSA</a>
<a href="IntermediateLanguage">IntermediateLanguage</a>, invoked from <a href="SSASimplify">SSASimplify</a>.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_description">Description</h2>
<div class="sectionbody">
<div class="paragraph"><p>This pass duplicates and simplifies <span class="monospaced">Case</span> transfers when the
constructor of the scrutinee is known.</p></div>
<div class="paragraph"><p>Uses <a href="Restore">Restore</a>.</p></div>
<div class="paragraph"><p>For example, the program</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">val</span><span class="w"> </span><span class="k">rec</span><span class="w"> </span><span class="n">last</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">  </span><span class="k">fn</span><span class="w"> </span><span class="p">[]</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="mi">0</span><span class="w"></span>
<span class="w">   </span><span class="p">|</span><span class="w"> </span><span class="p">[</span><span class="n">x</span><span class="p">]</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">x</span><span class="w"></span>
<span class="w">   </span><span class="p">|</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="n">::</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">last</span><span class="w"> </span><span class="n">l</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="n">+</span><span class="w"> </span><span class="n">last</span><span class="w"> </span><span class="p">[</span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="mi">7</span><span class="p">]</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>gives rise to the <a href="SSA">SSA</a> function</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>fun last_0 (x_142) = loopS_1 ()
  loopS_1 ()
    loop_11 (x_142)
  loop_11 (x_143)
    case x_143 of
      nil_1 =&gt; L_73 | ::_0 =&gt; L_74
  L_73 ()
    return global_5
  L_74 (x_145, x_144)
    case x_145 of
      nil_1 =&gt; L_75 | _ =&gt; L_76
  L_75 ()
    return x_144
  L_76 ()
    loop_11 (x_145)</pre>
</div></div>
<div class="paragraph"><p>which is simplified to</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>fun last_0 (x_142) = loopS_1 ()
  loopS_1 ()
    case x_142 of
      nil_1 =&gt; L_73 | ::_0 =&gt; L_118
  L_73 ()
    return global_5
  L_118 (x_230, x_229)
    L_74 (x_230, x_229, x_142)
  L_74 (x_145, x_144, x_232)
    case x_145 of
      nil_1 =&gt; L_75 | ::_0 =&gt; L_114
  L_75 ()
    return x_144
  L_114 (x_227, x_226)
    L_74 (x_227, x_226, x_145)</pre>
</div></div>
</div>
</div>
<div class="sect1">
<h2 id="_implementation">Implementation</h2>
<div class="sectionbody">
<div class="ulist"><ul>
<li>
<p>
<a href="https://github.com/MLton/mlton/blob/master/mlton/ssa/known-case.fun"><span class="monospaced">known-case.fun</span></a>
</p>
</li>
</ul></div>
</div>
</div>
<div class="sect1">
<h2 id="_details_and_notes">Details and Notes</h2>
<div class="sectionbody">
<div class="paragraph"><p>One interesting aspect of <a href="KnownCase">KnownCase</a>, is that it often has the
effect of unrolling list traversals by one iteration, moving the
<span class="monospaced">nil</span>/<span class="monospaced">::</span> check to the end of the loop, rather than the beginning.</p></div>
</div>
</div>
</div>
<div id="footnotes"><hr></div>
<div id="footer">
<div id="footer-text">
</div>
<div id="footer-badges">
</div>
</div>
</body>
</html>
