Browse Source

cranelift: add icmp-of-icmp rules for comparisons with 1 (#8510)

* cranelift: add icmp-of-icmp rules for comparisons with 1

* cranelift: add tests for icmp-of-icmp rule
pull/8512/head
Terts Diepraam 6 months ago
committed by GitHub
parent
commit
662c35c079
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 18
      cranelift/codegen/src/opts/icmp.isle
  2. 45
      cranelift/filetests/filetests/egraph/icmp.clif
  3. 56
      cranelift/filetests/filetests/runtests/icmp-of-icmp.clif

18
cranelift/codegen/src/opts/icmp.isle

@ -14,16 +14,34 @@
(rule (simplify (sle (ty_int ty) x x)) (subsume (iconst_u ty 1)))
;; Optimize icmp-of-icmp.
;; ne(icmp(ty, cc, x, y), 0) == icmp(ty, cc, x, y)
;; e.g. neq(ugt(x, y), 0) == ugt(x, y)
(rule (simplify (ne ty
(uextend_maybe _ inner @ (icmp ty _ _ _))
(iconst_u _ 0)))
(subsume inner))
;; eq(icmp(ty, cc, x, y), 0) == icmp(ty, cc_complement, x, y)
;; e.g. eq(ugt(x, y), 0) == ule(x, y)
(rule (simplify (eq ty
(uextend_maybe _ (icmp ty cc x y))
(iconst_u _ 0)))
(subsume (icmp ty (intcc_complement cc) x y)))
;; ne(icmp(ty, cc, x, y), 1) == icmp(ty, cc_complement, x, y)
;; e.g. ne(ugt(x, y), 1) == ule(x, y)
(rule (simplify (ne ty
(uextend_maybe _ (icmp ty cc x y))
(iconst_u _ 1)))
(subsume (icmp ty (intcc_complement cc) x y)))
;; eq(icmp(ty, cc, x, y), 1) == icmp(ty, cc, x, y)
;; e.g. eq(ugt(x, y), 1) == ugt(x, y)
(rule (simplify (eq ty
(uextend_maybe _ inner @ (icmp _ _ _ _))
(iconst_u _ 1)))
(subsume inner))
;; Optimize select-of-uextend-of-icmp to select-of-icmp, because
;; select can take an I8 condition too.
(rule (simplify

45
cranelift/filetests/filetests/egraph/icmp.clif

@ -125,3 +125,48 @@ block0(v0: i8):
; return v4
; }
function %eq_one_of_icmp(i8) -> i8 fast {
block0(v0: i8):
v1 = iconst.i8 5
v2 = icmp eq v0, v1
v3 = iconst.i8 1
v4 = icmp eq v2, v3
return v4
}
; function %eq_one_of_icmp(i8) -> i8 fast {
; block0(v0: i8):
; v1 = iconst.i8 5
; v2 = icmp eq v0, v1 ; v1 = 5
; return v2
; }
function %ne_one_of_icmp(i8) -> i8 fast {
block0(v0: i8):
v1 = iconst.i8 5
v2 = icmp eq v0, v1
v3 = iconst.i8 1
v4 = icmp ne v2, v3
return v4
}
; function %ne_one_of_icmp(i8) -> i8 fast {
; block0(v0: i8):
; v1 = iconst.i8 5
; v5 = icmp ne v0, v1 ; v1 = 5
; return v5
; }
function %negate_lt(i8, i8) -> i8 fast {
block0(v0: i8, v1: i8):
v2 = icmp ult v0, v1
v3 = iconst.i8 1
v4 = icmp ne v2, v3
return v4
}
; function %negate_lt(i8, i8) -> i8 fast {
; block0(v0: i8, v1: i8):
; v5 = icmp uge v0, v1
; return v5
; }

56
cranelift/filetests/filetests/runtests/icmp-of-icmp.clif

@ -0,0 +1,56 @@
test interpret
test run
set opt_level=speed
target aarch64
target x86_64
target s390x
target riscv64
target riscv64 has_c has_zcb
function %eq_eq_zero(i8) -> i8 {
block0(v0: i8):
v1 = iconst.i8 5
v2 = icmp eq v0, v1
v3 = iconst.i8 0
v4 = icmp eq v2, v3
return v4
}
; run: %eq_eq_zero(5) == 0
; run: %eq_eq_zero(3) == 1
function %ne_eq_one(i8) -> i8 {
block0(v0: i8):
v1 = iconst.i8 5
v2 = icmp eq v0, v1
v3 = iconst.i8 1
v4 = icmp ne v2, v3
return v4
}
; run: %ne_eq_one(5) == 0
; run: %ne_eq_one(3) == 1
function %ne_eq_zero(i8) -> i8 {
block0(v0: i8):
v1 = iconst.i8 5
v2 = icmp eq v0, v1
v3 = iconst.i8 0
v4 = icmp ne v2, v3
return v4
}
; run: %ne_eq_zero(5) == 1
; run: %ne_eq_zero(3) == 0
function %eq_eq_one(i8) -> i8 {
block0(v0: i8):
v1 = iconst.i8 5
v2 = icmp eq v0, v1
v3 = iconst.i8 1
v4 = icmp eq v2, v3
return v4
}
; run: %eq_eq_one(5) == 1
; run: %eq_eq_one(3) == 0
Loading…
Cancel
Save