[MLton] A couple of minor optimizations

Vesa Karvonen vesa.a.j.k at gmail.com
Thu Dec 13 03:26:27 PST 2007


I was thinking about implementing rational numbers.  For an efficient
library implementation it would seem important to me that the compiler can
do constant folding on gcd (otherwise no constant folding can be performed
on rational arithmetic at all).  So, I took a look at how MLton implements
gcd and found out, pleasantly, that MLton's gcd is subject to constant
folding.  I also noticed a couple of other things.

IntInf_gcd does not seem to be recognized as a commutative operation by
MLton, which seems to have an effect on common subexpression elimination.
I assume this is merely an oversight.

Similarly, I noticed that Real_qequal is considered commutative,
but Real_equal isn't.  As far as I can see, Real_equal is also
commutative.

I also noticed that Real_abs and Real_neg have been marked as
DependsOnState.  This seems unnecessarily pessimistic, because both of
those operations only manipulate the sign bit (clear and invert it,
respectively) and don't depend on the rounding mode.

So, below is small patch that (re)enables the corresponding optimizations.

-Vesa Karvonen

Index: atoms/prim.fun
===================================================================
--- atoms/prim.fun	(revision 6257)
+++ atoms/prim.fun	(working copy)
@@ -741,6 +741,7 @@
     | MLton_equal => true
     | Real_add _ => true
     | Real_mul _ => true
+    | Real_equal _ => true
     | Real_qequal _ => true
     | Word_add _ => true
     | Word_addCheck _ => true
@@ -834,7 +835,7 @@
        | Real_Math_sin _ => DependsOnState (* depends on rounding mode *)
        | Real_Math_sqrt _ => DependsOnState (* depends on rounding mode *)
        | Real_Math_tan _ => DependsOnState (* depends on rounding mode *)
-       | Real_abs _ => DependsOnState (* depends on rounding mode *)
+       | Real_abs _ => Functional
        | Real_add _ => DependsOnState (* depends on rounding mode *)
        | Real_castToWord _ => Functional
        | Real_div _ => DependsOnState (* depends on rounding mode *)
@@ -845,7 +846,7 @@
        | Real_mul _ => DependsOnState (* depends on rounding mode *)
        | Real_muladd _ => DependsOnState (* depends on rounding mode *)
        | Real_mulsub _ => DependsOnState (* depends on rounding mode *)
-       | Real_neg _ => DependsOnState (* depends on rounding mode *)
+       | Real_neg _ => Functional
        | Real_qequal _ => Functional
        | Real_rndToReal _ => DependsOnState (* depends on rounding mode *)
        | Real_rndToWord _ => Functional
Index: ssa/common-subexp.fun
===================================================================
--- ssa/common-subexp.fun	(revision 6257)
+++ ssa/common-subexp.fun	(working copy)
@@ -82,6 +82,7 @@
                      if (case Prim.name prim of
                             IntInf_add => true
                           | IntInf_andb => true
+                          | IntInf_gcd => true
                           | IntInf_mul => true
                           | IntInf_orb => true
                           | IntInf_xorb => true



More information about the MLton mailing list