<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="AsciiDoc 8.6.10">
<title>SimplifyTypes</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 20201002</a>
</div>
</div>
<div id="header">
<h1>SimplifyTypes</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p><a href="SimplifyTypes">SimplifyTypes</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 computes a "cardinality" of each datatype, which is an
abstraction of the number of values of the datatype.</p></div>
<div class="ulist"><ul>
<li>
<p>
<span class="monospaced">Zero</span> means the datatype has no values (except for bottom).
</p>
</li>
<li>
<p>
<span class="monospaced">One</span> means the datatype has one value (except for bottom).
</p>
</li>
<li>
<p>
<span class="monospaced">Many</span> means the datatype has many values.
</p>
</li>
</ul></div>
<div class="paragraph"><p>This pass removes all datatypes whose cardinality is <span class="monospaced">Zero</span> or <span class="monospaced">One</span>
and removes:</p></div>
<div class="ulist"><ul>
<li>
<p>
components of tuples
</p>
</li>
<li>
<p>
function args
</p>
</li>
<li>
<p>
constructor args
</p>
</li>
</ul></div>
<div class="paragraph"><p>which are such datatypes.</p></div>
<div class="paragraph"><p>This pass marks constructors as one of:</p></div>
<div class="ulist"><ul>
<li>
<p>
<span class="monospaced">Useless</span>: it never appears in a <span class="monospaced">ConApp</span>.
</p>
</li>
<li>
<p>
<span class="monospaced">Transparent</span>: it is the only variant in its datatype and its argument type does not contain any uses of <span class="monospaced">array</span> or <span class="monospaced">vector</span>.
</p>
</li>
<li>
<p>
<span class="monospaced">Useful</span>: otherwise
</p>
</li>
</ul></div>
<div class="paragraph"><p>This pass also removes <span class="monospaced">Useless</span> and <span class="monospaced">Transparent</span> constructors.</p></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/simplify-types.fun"><span class="monospaced">simplify-types.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>This pass must happen before polymorphic equality is implemented because</p></div>
<div class="ulist"><ul>
<li>
<p>
it will make polymorphic equality faster because some types are simpler
</p>
</li>
<li>
<p>
it removes uses of polymorphic equality that must return true
</p>
</li>
</ul></div>
<div class="paragraph"><p>We must keep track of <span class="monospaced">Transparent</span> constructors whose argument type
uses <span class="monospaced">array</span> because of datatypes like the following:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">datatype</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="n">array</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Such a datatype has <span class="monospaced">Cardinality.Many</span>, but we cannot eliminate the
datatype and replace the lhs by the rhs, i.e. we must keep the
circularity around.</p></div>
<div class="paragraph"><p>Must do similar things for <span class="monospaced">vectors</span>.</p></div>
<div class="paragraph"><p>Also, to eliminate as many <span class="monospaced">Transparent</span> constructors as possible, for
something like the following,</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">datatype</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">u</span><span class="w"> </span><span class="n">array</span><span class="w"></span>
<span class="w">     </span><span class="k">and</span><span class="w"> </span><span class="n">u</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">U</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="n">vector</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>we (arbitrarily) expand one of the datatypes first.  The result will
be something like</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">datatype</span><span class="w"> </span><span class="n">u</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">U</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">u</span><span class="w"> </span><span class="n">array</span><span class="w"> </span><span class="n">array</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>where all uses of <span class="monospaced">t</span> are replaced by <span class="monospaced">u array</span>.</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>
