Feature
Simple optimization which rewrite x < 0 (icmp_imm slt x, 0) to x >>> bit_size(ty) - 1 (ushr_imm x, 31 | 63).
Benefit
x < 0 is pretty common operation. Although LLVM itself applies this rule, but some code generators / optimizers such as Binaryen (wasm-opt / wasm-pack use it) don't such peephole rewrites because replacing the constant 0 to 31 or 63 has a negative effect on entropy when compressing wasm module via gzip or brotli.
Besides, such optimization will improve cranelift IR itself, because it will remove the finalizing bint.i32 / bint.i64 after icmp. For ushr/ushr_imm it is not needed.
Implementation
- Add necessary rewrite for simple_opt before this line
- Add tests for
filetests/simple_preopt/simplily32.clif and filetests/simple_preopt/simplily64.clif
Updated Implementation Plan
Write necessary rules on ISLE instead in simple_opt.rs
Feature
Simple optimization which rewrite
x < 0(icmp_imm slt x, 0) tox >>> bit_size(ty) - 1(ushr_imm x, 31 | 63).Benefit
x < 0is pretty common operation. Although LLVM itself applies this rule, but some code generators / optimizers such as Binaryen (wasm-opt / wasm-pack use it) don't such peephole rewrites because replacing the constant0to31or63has a negative effect on entropy when compressing wasm module via gzip or brotli.Besides, such optimization will improve cranelift IR itself, because it will remove the finalizing
bint.i32/bint.i64aftericmp. Forushr/ushr_immit is not needed.Implementation
filetests/simple_preopt/simplily32.clifandfiletests/simple_preopt/simplily64.clifUpdated Implementation Plan
Write necessary rules on ISLE instead in
simple_opt.rs