Compiling by Proving: Language-Agnostic Automatic Optimization from Formal Semantics