Skip to content

cranelift: Add x < 0 -> x >>> bit_size(ty) - 1 lowering rule #4607

@MaxGraey

Description

@MaxGraey

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions