<!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>CommonSubexp</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>CommonSubexp</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p><a href="CommonSubexp">CommonSubexp</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>It eliminates instances of common subexpressions.</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/common-subexp.fun"><span class="monospaced">common-subexp.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>In addition to getting the usual sorts of things like</p></div>
<div class="ulist"><ul>
<li>
<p>
</p>
<div class="listingblock">
<div class="content monospaced">
<pre>(w + 0wx1) + (w + 0wx1)</pre>
</div></div>
<div class="paragraph"><p>rewritten to</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>let val w' = w + 0wx1 in w' + w' end</pre>
</div></div>
</li>
</ul></div>
<div class="paragraph"><p>it also gets things like</p></div>
<div class="ulist"><ul>
<li>
<p>
</p>
<div class="listingblock">
<div class="content monospaced">
<pre>val a = Array_array n
val b = Array_length a</pre>
</div></div>
<div class="paragraph"><p>rewritten to</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>val a = Array_array n
val b = n</pre>
</div></div>
</li>
</ul></div>
<div class="paragraph"><p><span class="monospaced">Arith</span> transfers are handled specially.  The <em>result</em> of an <span class="monospaced">Arith</span>
transfer can be used in <em>common</em> <span class="monospaced">Arith</span> transfers that it dominates:</p></div>
<div class="ulist"><ul>
<li>
<p>
</p>
<div class="listingblock">
<div class="content monospaced">
<pre>val l = (n + m) + (n + m)

val k = (l + n) + ((l + m) handle Overflow =&gt; ((l + m)
                                               handle Overflow =&gt; l + n))</pre>
</div></div>
<div class="paragraph"><p>is rewritten so that <span class="monospaced">(n + m)</span> is computed exactly once, as are
<span class="monospaced">(l + n)</span> and <span class="monospaced">(l + m)</span>.</p></div>
</li>
</ul></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>
