FractalFir / rustc_codegen_clr

This rust compiler backend(module) emmits valid CIL (.NET IR), enabling you to use Rust in .NET projects.
MIT License
1.51k stars 35 forks source link

Enhancement - implement the `blt` and `blt.un` instructions, and use them for optimizations. #43

Closed FractalFir closed 4 months ago

FractalFir commented 5 months ago

Currently, the project uses long CIL sequences to compare values and branch.

    ldarg.0
    ldarg.1
   clt // 2 bytes long, compare less than
brtrue // 1 byte + offset, branch if true

Those sequences can be shortened, using the blt instructions:

   ldarg.0
   ldarg.1
blt // 1 byte + offset, branch if less than

This more optimal sequence is not only more space efficient, it is also better for the JIT compiler.

Steps needed:

[ ] Add this instruction, by adding a new variant of the CILRoot enum [ ] Add support for exporting this instruction. [ ] Add optimizations, changing the sequence clt brtrue into just blt [ ] Add some basic unit tests

This only requires changes to the cilly crate, so no Rust-specific experience is needed.

Helpful sources: List of CIL instructions CIL Root optimization function