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

@@ -0,0 +1,43 @@
test optimize
set opt_level=none
set use_egraphs=true
target x86_64
function %f0() -> i8 {
block0:
v1 = iconst.i8 51
v2 = imul.i8 v1, v1
return v2
}
; check: v9 = iconst.i8 41
; nextln: return v9
function %f1() -> i16 {
block0:
v1 = iconst.i16 1
v2 = bnot.i16 v1
return v2
}
; check: v3 = iconst.i16 0xfffe
; nextln: return v3
function %f2(i8) -> i8 {
block0(v1: i8):
v2 = icmp eq v1, v1
return v2
}
; check: v3 = iconst.i8 1
; check: return v3
function %f3(i8) -> i8 {
block0(v1: i8):
v2 = icmp ne v1, v1
return v2
}
; check: v3 = iconst.i8 0
; check: return v3

View File

@@ -0,0 +1,16 @@
test interpret
test run
set opt_level=speed_and_size
set use_egraphs=true
target aarch64
function %a(i64) -> i8 system_v {
block0(v0: i64):
v6 = iconst.i8 51
v17 = imul v6, v6 ; v6 = 51, v6 = 51
v18 = icmp eq v17, v17
v52 = imul v18, v18
return v52
}
; run: %a(129) == 1