pan/bi: Fix bunches of issues around peephole optimizations
This adds a bunch of unit tests to our peephole optimizer, fixes a bunch of bugs the tests uncovered, reworks the way we do peephole optimization to make these bugs less likely, and builds on top of the new (better) foundation for more optimization.