Cranelift: Generalize (x << k) >> k optimization (#5746)

* Generalize unsigned `(x << k) >> k` optimization

Split the existing rule into three parts:
- A dual of the rule for `(x >> k) << k` that is only valid for unsigned
  shifts.
- Known-bits analysis for `(band (uextend x) k)`.
- A new rule for converting `sextend` to `uextend` if the sign-extended
  bits are masked out anyway.

The first two together cover the existing rule.

* Generalize signed `(x << k) >> k` optimization

* Review comments

* Generalize sign-extending shifts further

The shifts can be eliminated even if the shift amount isn't exactly
equal to the difference in bit-widths between the narrow and wide types.

* Add filetests
This commit is contained in:
Jamey Sharp
2023-02-27 09:34:46 -08:00
committed by GitHub
parent 6f64e39dda
commit 6cf7155052
4 changed files with 209 additions and 13 deletions

View File

@@ -144,6 +144,12 @@
(decl pure u64_eq (u64 u64) bool)
(extern constructor u64_eq u64_eq)
(decl pure u64_le (u64 u64) bool)
(extern constructor u64_le u64_le)
(decl pure u64_lt (u64 u64) bool)
(extern constructor u64_lt u64_lt)
(decl pure i64_sextend_imm64 (Type Imm64) i64)
(extern constructor i64_sextend_imm64 i64_sextend_imm64)