MikeInnes / WebAssembly.jl

Other
83 stars 11 forks source link

Optimisation: Useless sets #15

Closed sjorn3 closed 6 years ago

sjorn3 commented 6 years ago

Implement drop removal. Currently only used for removing the useless sets generated by the liveness check, but could be quite easily generalised if need be. If removing a useless set creates more of them, removes them too without needing to recompute liveness.

Here's the same example from before:

function addTwo(x, y)
  res = 10+x
  t = 10+x
  r = 431*y
  p = t*r
  q = p+2
  return res
end
f = (func $#addTwo_Int64_Int64 (param i64) (param i64) (result i64)
  (local i64) (local i64) (local i64) (local i64) (local i64)
  (i64.const 10)
  (get_local 0)
  (i64.add)
  (set_local 2)
  (i64.const 10)
  (get_local 0)
  (i64.add)
  (set_local 3)
  (i64.const 431)
  (get_local 1)
  (i64.mul)
  (set_local 4)
  (get_local 3)
  (get_local 4)
  (i64.mul)
  (set_local 5)
  (get_local 2)
  (return))

The unnecessary calculation of q is completely removed:

(func $#addTwo_Int64_Int64 (param i64) (param i64) (result i64)
  (local i64) (local i64) (local i64) (local i64) (local i64)
  (i64.const 10)
  (get_local 0)
  (i64.add)
  (set_local 2)
  (get_local 2)
  (return))

Putting it all together then, the function is reduced to the following:

(func $#addTwo_Int64_Int64 (param i64) (param i64) (result i64)
  (i64.const 10)
  (get_local 0)
  (i64.add)
  (return))
sjorn3 commented 6 years ago

Contained in #19