Import Upstream version 20180207
[hcoop/debian/mlton.git] / doc / guide / src / CommonSubexp.adoc
CommitLineData
7f918cf1
CE
1CommonSubexp
2============
3
4<:CommonSubexp:> is an optimization pass for the <:SSA:>
5<:IntermediateLanguage:>, invoked from <:SSASimplify:>.
6
7== Description ==
8
9It eliminates instances of common subexpressions.
10
11== Implementation ==
12
13* <!ViewGitFile(mlton,master,mlton/ssa/common-subexp.fun)>
14
15== Details and Notes ==
16
17In addition to getting the usual sorts of things like
18
19* {empty}
20+
21----
22(w + 0wx1) + (w + 0wx1)
23----
24+
25rewritten to
26+
27----
28let val w' = w + 0wx1 in w' + w' end
29----
30
31it also gets things like
32
33* {empty}
34+
35----
36val a = Array_uninit n
37val b = Array_length a
38----
39+
40rewritten to
41+
42----
43val a = Array_uninit n
44val b = n
45----
46
47`Arith` transfers are handled specially. The _result_ of an `Arith`
48transfer can be used in _common_ `Arith` transfers that it dominates:
49
50* {empty}
51+
52----
53val l = (n + m) + (n + m)
54
55val k = (l + n) + ((l + m) handle Overflow => ((l + m)
56 handle Overflow => l + n))
57----
58+
59is rewritten so that `(n + m)` is computed exactly once, as are
60`(l + n)` and `(l + m)`.