Refactor optimizer

Moves more functionality into Optimizer which makes it easier to
update the graph and support constant + dead sweeps in the same pass.