<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 2.0.26">
<title>KnownCase</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700">
<link rel="stylesheet" href="./asciidoctor.css">
<link rel="stylesheet" href="./mlton.css">

</head>
<body class="article">
<div id="mlton-header">
<div id="mlton-header-text">
<h2>
<a href="./Home">
MLton
20241230+git20251029+dfsg-5
</a>
</h2>
</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</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 <code>Case</code> 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">
<pre class="rouge highlight"><code data-lang="sml">val rec last =
  fn [] =&gt; 0
   | [x] =&gt; x
   | _ :: l =&gt; last l

val _ = 1 + last [2, 3, 4, 5, 6, 7]</code></pre>
</div>
</div>
<div class="paragraph">
<p>gives rise to the <a href="SSA">SSA</a> function</p>
</div>
<div class="listingblock">
<div class="content">
<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">
<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"><code>known-case.fun</code></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</a>, is that it often has the
effect of unrolling list traversals by one iteration, moving the
<code>nil</code>/<code>::</code> check to the end of the loop, rather than the beginning.</p>
</div>
</div>
</div>
</div>
</body>
</html>