Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
MaxGraey opened this issue Aug 4, 2022 · 4 comments · Fixed by #4625
Closed

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

MaxGraey opened this issue Aug 4, 2022 · 4 comments · Fixed by #4625

Comments

@MaxGraey
Copy link
Contributor

MaxGraey commented Aug 4, 2022

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

@MaxGraey MaxGraey changed the title [Feature] Add x < 0 -> x >>> it_size(ty) - 1 peephole rule [Feature] Add x < 0 -> x >>> bit_size(ty) - 1 peephole rule Aug 4, 2022
@MaxGraey MaxGraey changed the title [Feature] Add x < 0 -> x >>> bit_size(ty) - 1 peephole rule cranelift: Add x < 0 -> x >>> bit_size(ty) - 1 peephole rule Aug 4, 2022
@bjorn3
Copy link
Contributor

bjorn3 commented Aug 4, 2022

This wouldn't help on platforms without flag registers, right? Those have icmp and bint merged together already. If anything it would prevent icmp+brz/brnz fusion I think, even on platforms with flag registers.

@MaxGraey
Copy link
Contributor Author

MaxGraey commented Aug 4, 2022

Hmm, so perhaps it's better to do on lower level (may be ISLE) for only specific platforms after icmp+brz/brnz fusion?

@bjorn3
Copy link
Contributor

bjorn3 commented Aug 4, 2022

I think so.

@cfallin
Copy link
Member

cfallin commented Aug 4, 2022

We can definitely incorporate this as a lowering rule case in the backends. The ISLE and lowering machinery is smart enough to do the right thing for the (brnz (icmp ...)) case and allow for a rule for (icmp (IntCC.NP) ...) when used directly to compute that value. cc @elliottt as he is in the middle of br+compare stuff in x86-64 right now...

@MaxGraey MaxGraey changed the title cranelift: Add x < 0 -> x >>> bit_size(ty) - 1 peephole rule cranelift: Add x < 0 -> x >>> bit_size(ty) - 1 lowering rule Aug 4, 2022
elliottt added a commit that referenced this issue Aug 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants