[MLton] Optimization pass: eliminate-replace

Wesley W. Terpstra wesley at terpstra.ca
Fri Sep 25 13:26:54 PDT 2009


I am considering an optimization pass to run after refFlatten that turns:
  val y = !x
  val () = x := y
into a noop.

I think the best way to do this is run a DFS and tag refs with a serial
number. A deref (!) records the serial number on the assigned variable and
any reassignments of that variable. Finally, if a reference is written to
(:=), check the variable to see if it is a load of this variable with
identical serial number, if so, remove the assignment. If not, increase the
serial number.

That way in
  val y = !x
  val () = x := !x + 1
  val z = !x
  val a = !x
  val () = x := z
  val () = x := a
  val () = x := y
the 2nd and 3rd assignments get eliminated, but not 1st and 4th.

This optimization is specifically a follow-up optimization to DeepFlatten.
If DeepFlatten turned a ref tuple into a record, chances are that a lot of
the fields are unmodified when it gets rewritten. In my particular
application only 1-3 fields out of 21 or more get changed, so this would be
a good speed-up for me at least.

Any comments?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mlton.org/pipermail/mlton/attachments/20090925/45f9a36b/attachment.html


More information about the MLton mailing list