Fix optimization rules for narrow types: wrap i8 results to 8 bits. (#5409)

* Fix optimization rules for narrow types: wrap i8 results to 8 bits.

This fixes #5405.

In the egraph mid-end's optimization rules, we were rewriting e.g. imuls
of two iconsts to an iconst of the result, but without masking off the
high bits (beyond the result type's width). This was producing iconsts
with set high bits beyond their types' width, which is not legal.

In addition, this PR adds some optimizations to the algebraic rules to
recognize e.g. `x == x` (and all other integer comparison operators) and
resolve to 1 or 0 as appropriate.

* Review feedback.

* Review feedback, again.
This commit is contained in:
Chris Fallin
2022-12-09 14:29:25 -08:00
committed by GitHub
parent e913cf3647
commit 244dce93f6
6 changed files with 120 additions and 15 deletions

View File

@@ -509,6 +509,16 @@ macro_rules! isle_common_prelude_methods {
Imm64::new(x as i64)
}
#[inline]
fn imm64_masked(&mut self, ty: Type, x: u64) -> Imm64 {
debug_assert!(ty.bits() <= 64);
// Careful: we can't do `(1 << bits) - 1` because that
// would overflow for `bits == 64`. Instead,
// right-shift an all-ones mask.
let mask = u64::MAX >> (64 - ty.bits());
Imm64::new((x & mask) as i64)
}
#[inline]
fn simm32(&mut self, x: Imm64) -> Option<u32> {
let x64: i64 = x.into();