Small refactoring to better separate optimizations from code generation in the API.