My thinking is to provide hand-crafted primitives to transform byte code, and maybe use an existing optimizer to do the search.
https://github.com/google/souper
https://github.com/google/souper