Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

[Cranelift] add select simplification rules (#12742)

Hyunbin Kimbc7272a3c8dd
diff --git a/cranelift/codegen/src/opts/selects.isle b/cranelift/codegen/src/opts/selects.isle
index 1772adb4f9..77b2b8b943 100644
--- a/cranelift/codegen/src/opts/selects.isle
+++ b/cranelift/codegen/src/opts/selects.isle
@@ -112,5 +112,84 @@
(rule (simplify (ult (fits_in_64 ty) x (umin _ x z))) (iconst_u ty 0))
(rule (simplify (ult (fits_in_64 ty) x (umin _ y x))) (iconst_u ty 0))
-
-
+;; (x == y) ? x : y => y
+;; select(eq/ne) patterns that always collapse to y.
+(rule (simplify (select ty (eq cty x y) x y)) (subsume y))
+(rule (simplify (select ty (ne cty x y) y x)) (subsume y))
+(rule (simplify (select ty (eq cty y x) x y)) (subsume y))
+(rule (simplify (select ty (ne cty y x) y x)) (subsume y))
+
+;; (x | (y != z)) ? y : z ==> y
+(rule (simplify (select ty (bor cty x (ne cty y z)) y z)) (subsume y))
+(rule (simplify (select ty (bor cty (ne cty y z) x) y z)) (subsume y))
+(rule (simplify (select ty (bor cty x (ne cty z y)) y z)) (subsume y))
+(rule (simplify (select ty (bor cty (ne cty z y) x) y z)) (subsume y))
+
+;; (x == y) ? x : (x | y) ==> (x | y)
+;; and equivalent operand-order variants.
+(rule (simplify (select ty (eq cty x y) x (bor ty x y))) (bor ty x y))
+(rule (simplify (select ty (ne cty x y) (bor ty x y) x)) (bor ty x y))
+(rule (simplify (select ty (eq cty x y) x (bor ty y x))) (bor ty x y))
+(rule (simplify (select ty (ne cty x y) (bor ty y x) x)) (bor ty x y))
+(rule (simplify (select ty (eq cty y x) x (bor ty x y))) (bor ty x y))
+(rule (simplify (select ty (ne cty y x) (bor ty x y) x)) (bor ty x y))
+(rule (simplify (select ty (eq cty y x) x (bor ty y x))) (bor ty x y))
+(rule (simplify (select ty (ne cty y x) (bor ty y x) x)) (bor ty x y))
+
+;; (x != y) ? (x & y) : y ==> (x & y)
+;; and equivalent operand-order variants.
+(rule (simplify (select ty (ne cty x y) (band ty x y) y)) (band ty x y))
+(rule (simplify (select ty (eq cty x y) y (band ty x y))) (band ty x y))
+(rule (simplify (select ty (ne cty x y) (band ty y x) y)) (band ty x y))
+(rule (simplify (select ty (eq cty x y) y (band ty y x))) (band ty x y))
+(rule (simplify (select ty (ne cty y x) (band ty x y) y)) (band ty x y))
+(rule (simplify (select ty (eq cty y x) y (band ty x y))) (band ty x y))
+(rule (simplify (select ty (ne cty y x) (band ty y x) y)) (band ty x y))
+(rule (simplify (select ty (eq cty y x) y (band ty y x))) (band ty x y))
+
+;; (x <_u y) ? 1 : (x == y) ==> (x <=_u y)
+;; and equivalent operand-order variants.
+(rule (simplify (select ty (ult cty x y) (iconst_u ty 1) (eq ty x y))) (ule ty x y))
+(rule (simplify (select ty (uge cty x y) (eq ty x y) (iconst_u ty 1))) (ule ty x y))
+(rule (simplify (select ty (ugt cty y x) (iconst_u ty 1) (eq ty x y))) (ule ty x y))
+(rule (simplify (select ty (ule cty y x) (eq ty x y) (iconst_u ty 1))) (ule ty x y))
+(rule (simplify (select ty (ult cty x y) (iconst_u ty 1) (eq ty y x))) (ule ty x y))
+(rule (simplify (select ty (uge cty x y) (eq ty y x) (iconst_u ty 1))) (ule ty x y))
+(rule (simplify (select ty (ugt cty y x) (iconst_u ty 1) (eq ty y x))) (ule ty x y))
+(rule (simplify (select ty (ule cty y x) (eq ty y x) (iconst_u ty 1))) (ule ty x y))
+
+;; (x < y) ? 1 : (x > y) ==> (x != y)
+;; and equivalent operand-order variants.
+(rule (simplify (select ty (slt cty x y) (iconst_u ty 1) (sgt ty x y))) (ne ty x y))
+(rule (simplify (select ty (sge cty x y) (sgt ty x y) (iconst_u ty 1))) (ne ty x y))
+(rule (simplify (select ty (sgt cty y x) (iconst_u ty 1) (sgt ty x y))) (ne ty x y))
+(rule (simplify (select ty (sle cty y x) (sgt ty x y) (iconst_u ty 1))) (ne ty x y))
+(rule (simplify (select ty (slt cty x y) (iconst_u ty 1) (slt ty y x))) (ne ty x y))
+(rule (simplify (select ty (sge cty x y) (slt ty y x) (iconst_u ty 1))) (ne ty x y))
+(rule (simplify (select ty (sgt cty y x) (iconst_u ty 1) (slt ty y x))) (ne ty x y))
+(rule (simplify (select ty (sle cty y x) (slt ty y x) (iconst_u ty 1))) (ne ty x y))
+(rule (simplify (select ty (ult cty x y) (iconst_u ty 1) (ugt ty x y))) (ne ty x y))
+(rule (simplify (select ty (uge cty x y) (ugt ty x y) (iconst_u ty 1))) (ne ty x y))
+(rule (simplify (select ty (ugt cty y x) (iconst_u ty 1) (ugt ty x y))) (ne ty x y))
+(rule (simplify (select ty (ule cty y x) (ugt ty x y) (iconst_u ty 1))) (ne ty x y))
+(rule (simplify (select ty (ult cty x y) (iconst_u ty 1) (ult ty y x))) (ne ty x y))
+(rule (simplify (select ty (uge cty x y) (ult ty y x) (iconst_u ty 1))) (ne ty x y))
+(rule (simplify (select ty (ugt cty y x) (iconst_u ty 1) (ult ty y x))) (ne ty x y))
+(rule (simplify (select ty (ule cty y x) (ult ty y x) (iconst_u ty 1))) (ne ty x y))
+
+;; (x ? 1 : (x | y)) ==> (x ? 1 : y)
+(rule (simplify (select ty x (iconst_u ty 1) (bor ty x y))) (select ty x (iconst_u ty 1) y))
+(rule (simplify (select ty x (iconst_u ty 1) (bor ty y x))) (select ty x (iconst_u ty 1) y))
+
+;; if x == n then (y - n) else (y - x) ==> (y - x)
+;; and equivalent predicate/operand-order variants.
+(rule (simplify (select ty (eq cty x (iconst_u ty n)) (isub ty y (iconst_u ty n)) (isub ty y x))) (isub ty y x))
+(rule (simplify (select ty (ne cty x (iconst_u ty n)) (isub ty y x) (isub ty y (iconst_u ty n)))) (isub ty y x))
+(rule (simplify (select ty (eq cty (iconst_u ty n) x) (isub ty y (iconst_u ty n)) (isub ty y x))) (isub ty y x))
+(rule (simplify (select ty (ne cty (iconst_u ty n) x) (isub ty y x) (isub ty y (iconst_u ty n)))) (isub ty y x))
+
+;; select(cond, eq(z, y), eq(p, y)) ==> eq(y, select(cond, z, p))
+(rule (simplify (select ty x (eq ty z y) (eq ty (iconst_u cty p) y))) (eq ty y (select cty x z (iconst_u cty p))))
+(rule (simplify (select ty x (eq ty z y) (eq ty y (iconst_u cty p)))) (eq ty y (select cty x z (iconst_u cty p))))
+(rule (simplify (select ty x (eq ty y z) (eq ty (iconst_u cty p) y))) (eq ty y (select cty x z (iconst_u cty p))))
+(rule (simplify (select ty x (eq ty y z) (eq ty y (iconst_u cty p)))) (eq ty y (select cty x z (iconst_u cty p))))
diff --git a/cranelift/filetests/filetests/egraph/selects.clif b/cranelift/filetests/filetests/egraph/selects.clif
index 81a1b00a3b..216dd07398 100644
--- a/cranelift/filetests/filetests/egraph/selects.clif
+++ b/cranelift/filetests/filetests/egraph/selects.clif
@@ -89,3 +89,142 @@ block0(v0: i32, v1: i32):
; return v7 ; v7 = 0
; }
+function %select_eq_xy_xy_to_y(i32, i32) -> i32 fast {
+block0(v0: i32, v1: i32):
+ v2 = icmp eq v0, v1
+ v3 = select v2, v0, v1
+ return v3
+}
+
+; function %select_eq_xy_xy_to_y(i32, i32) -> i32 fast {
+; block0(v0: i32, v1: i32):
+; return v1
+; }
+
+;; (x | (y != z)) ? y : z ==> y
+function %select_bor_ne_to_y(i8, i8, i8) -> i8 fast {
+block0(v0: i8, v1: i8, v2: i8):
+ v3 = icmp ne v1, v2
+ v4 = bor v0, v3
+ v5 = select v4, v1, v2
+ return v5
+}
+
+; function %select_bor_ne_to_y(i8, i8, i8) -> i8 fast {
+; block0(v0: i8, v1: i8, v2: i8):
+; return v1
+; }
+
+;; (x == y) ? x : (x | y) ==> (x | y)
+function %select_eq_xy_x_borxy_to_borxy(i32, i32) -> i32 fast {
+block0(v0: i32, v1: i32):
+ v2 = icmp eq v0, v1
+ v3 = bor v0, v1
+ v4 = select v2, v0, v3
+ return v4
+}
+
+; function %select_eq_xy_x_borxy_to_borxy(i32, i32) -> i32 fast {
+; block0(v0: i32, v1: i32):
+; v3 = bor v0, v1
+; return v3
+; }
+
+;; (x != y) ? (x & y) : y ==> (x & y)
+function %select_ne_xy_bandxy_y_to_bandxy(i32, i32) -> i32 fast {
+block0(v0: i32, v1: i32):
+ v2 = icmp ne v0, v1
+ v3 = band v0, v1
+ v4 = select v2, v3, v1
+ return v4
+}
+
+; function %select_ne_xy_bandxy_y_to_bandxy(i32, i32) -> i32 fast {
+; block0(v0: i32, v1: i32):
+; v3 = band v0, v1
+; return v3
+; }
+
+;; (x <_u y) ? 1 : (x == y) ==> (x <=_u y)
+function %select_ult_one_eq_to_ule(i8, i8) -> i8 fast {
+block0(v0: i8, v1: i8):
+ v2 = icmp ult v0, v1
+ v3 = iconst.i8 1
+ v4 = icmp eq v0, v1
+ v5 = select v2, v3, v4
+ return v5
+}
+
+; function %select_ult_one_eq_to_ule(i8, i8) -> i8 fast {
+; block0(v0: i8, v1: i8):
+; v6 = icmp ule v0, v1
+; return v6
+; }
+
+;; (x < y) ? 1 : (x > y) ==> (x != y)
+function %select_slt_one_sgt_to_ne(i8, i8) -> i8 fast {
+block0(v0: i8, v1: i8):
+ v2 = icmp slt v0, v1
+ v3 = iconst.i8 1
+ v4 = icmp sgt v0, v1
+ v5 = select v2, v3, v4
+ return v5
+}
+
+; function %select_slt_one_sgt_to_ne(i8, i8) -> i8 fast {
+; block0(v0: i8, v1: i8):
+; v6 = icmp ne v0, v1
+; return v6
+; }
+
+;; (x ? 1 : (x | y)) ==> (x ? 1 : y)
+function %select_x_one_borxy_to_select_x_one_y(i8, i8) -> i8 fast {
+block0(v0: i8, v1: i8):
+ v2 = iconst.i8 1
+ v3 = bor v0, v1
+ v4 = select v0, v2, v3
+ return v4
+}
+
+; function %select_x_one_borxy_to_select_x_one_y(i8, i8) -> i8 fast {
+; block0(v0: i8, v1: i8):
+; v2 = iconst.i8 1
+; v5 = select v0, v2, v1 ; v2 = 1
+; return v5
+; }
+
+;; if x == n then (y - n) else (y - x) ==> (y - x)
+function %select_eq_xn_yminusn_yminusx_to_yminusx(i32, i32) -> i32 fast {
+block0(v0: i32, v1: i32):
+ v2 = iconst.i32 7
+ v3 = icmp eq v0, v2
+ v4 = isub v1, v2
+ v5 = isub v1, v0
+ v6 = select v3, v4, v5
+ return v6
+}
+
+; function %select_eq_xn_yminusn_yminusx_to_yminusx(i32, i32) -> i32 fast {
+; block0(v0: i32, v1: i32):
+; v5 = isub v1, v0
+; return v5
+; }
+
+;; select(cond, eq(z, y), eq(p, y)) ==> eq(y, select(cond, z, p))
+function %select_eq_eq_to_eq_select(i8, i32, i32) -> i8 fast {
+block0(v0: i8, v1: i32, v2: i32):
+ v3 = icmp eq v2, v1
+ v4 = iconst.i32 7
+ v5 = icmp eq v4, v1
+ v6 = select v0, v3, v5
+ return v6
+}
+
+; function %select_eq_eq_to_eq_select(i8, i32, i32) -> i8 fast {
+; block0(v0: i8, v1: i32, v2: i32):
+; v4 = iconst.i32 7
+; v9 = select v0, v2, v4 ; v4 = 7
+; v10 = icmp eq v1, v9
+; return v10
+; }
+
diff --git a/cranelift/filetests/filetests/runtests/select.clif b/cranelift/filetests/filetests/runtests/select.clif
index 4b6c121c6e..d15c3b2d81 100644
--- a/cranelift/filetests/filetests/runtests/select.clif
+++ b/cranelift/filetests/filetests/runtests/select.clif
@@ -462,3 +462,106 @@ block0(v0: i32, v1: i32):
; run: %test_ugt_select_ult_min(234234, 5544) == 0
+function %select_eq_xy_xy_to_y_rt(i32, i32) -> i32 fast {
+block0(v0: i32, v1: i32):
+ v2 = icmp eq v0, v1
+ v3 = select v2, v0, v1
+ return v3
+}
+; run: %select_eq_xy_xy_to_y_rt(42, 35) == 35
+; run: %select_eq_xy_xy_to_y_rt(42, 42) == 42
+
+function %select_bor_ne_to_y_rt(i8, i8, i8) -> i8 fast {
+block0(v0: i8, v1: i8, v2: i8):
+ v3 = icmp ne v1, v2
+ v4 = bor v0, v3
+ v5 = select v4, v1, v2
+ return v5
+}
+; run: %select_bor_ne_to_y_rt(0, 7, 7) == 7
+; run: %select_bor_ne_to_y_rt(0, 7, 9) == 7
+; run: %select_bor_ne_to_y_rt(1, 7, 7) == 7
+
+function %select_eq_xy_x_borxy_to_borxy_rt(i32, i32) -> i32 fast {
+block0(v0: i32, v1: i32):
+ v2 = icmp eq v0, v1
+ v3 = bor v0, v1
+ v4 = select v2, v0, v3
+ return v4
+}
+; run: %select_eq_xy_x_borxy_to_borxy_rt(7, 7) == 7
+; run: %select_eq_xy_x_borxy_to_borxy_rt(6, 10) == 14
+; run: %select_eq_xy_x_borxy_to_borxy_rt(0, 5) == 5
+
+function %select_ne_xy_bandxy_y_to_bandxy_rt(i32, i32) -> i32 fast {
+block0(v0: i32, v1: i32):
+ v2 = icmp ne v0, v1
+ v3 = band v0, v1
+ v4 = select v2, v3, v1
+ return v4
+}
+; run: %select_ne_xy_bandxy_y_to_bandxy_rt(6, 10) == 2
+; run: %select_ne_xy_bandxy_y_to_bandxy_rt(7, 7) == 7
+; run: %select_ne_xy_bandxy_y_to_bandxy_rt(0, 5) == 0
+
+function %select_ult_one_eq_to_ule_rt(i8, i8) -> i8 fast {
+block0(v0: i8, v1: i8):
+ v2 = icmp ult v0, v1
+ v3 = iconst.i8 1
+ v4 = icmp eq v0, v1
+ v5 = select v2, v3, v4
+ return v5
+}
+; run: %select_ult_one_eq_to_ule_rt(1, 2) == 1
+; run: %select_ult_one_eq_to_ule_rt(2, 2) == 1
+; run: %select_ult_one_eq_to_ule_rt(3, 2) == 0
+
+function %select_slt_one_sgt_to_ne_rt(i8, i8) -> i8 fast {
+block0(v0: i8, v1: i8):
+ v2 = icmp slt v0, v1
+ v3 = iconst.i8 1
+ v4 = icmp sgt v0, v1
+ v5 = select v2, v3, v4
+ return v5
+}
+; run: %select_slt_one_sgt_to_ne_rt(1, 2) == 1
+; run: %select_slt_one_sgt_to_ne_rt(2, 1) == 1
+; run: %select_slt_one_sgt_to_ne_rt(2, 2) == 0
+
+function %select_x_one_borxy_to_select_x_one_y_rt(i8, i8) -> i8 fast {
+block0(v0: i8, v1: i8):
+ v2 = iconst.i8 1
+ v3 = bor v0, v1
+ v4 = select v0, v2, v3
+ return v4
+}
+; run: %select_x_one_borxy_to_select_x_one_y_rt(0, 5) == 5
+; run: %select_x_one_borxy_to_select_x_one_y_rt(1, 5) == 1
+; run: %select_x_one_borxy_to_select_x_one_y_rt(2, 5) == 1
+
+function %select_eq_xn_yminusn_yminusx_to_yminusx_rt(i32, i32) -> i32 fast {
+block0(v0: i32, v1: i32):
+ v2 = iconst.i32 7
+ v3 = icmp eq v0, v2
+ v4 = isub v1, v2
+ v5 = isub v1, v0
+ v6 = select v3, v4, v5
+ return v6
+}
+; run: %select_eq_xn_yminusn_yminusx_to_yminusx_rt(7, 10) == 3
+; run: %select_eq_xn_yminusn_yminusx_to_yminusx_rt(3, 10) == 7
+; run: %select_eq_xn_yminusn_yminusx_to_yminusx_rt(0, 10) == 10
+
+function %select_eq_eq_to_eq_select_rt(i8, i32, i32) -> i8 fast {
+block0(v0: i8, v1: i32, v2: i32):
+ v3 = icmp eq v2, v1
+ v4 = iconst.i32 7
+ v5 = icmp eq v4, v1
+ v6 = select v0, v3, v5
+ return v6
+}
+; run: %select_eq_eq_to_eq_select_rt(0, 5, 5) == 0
+; run: %select_eq_eq_to_eq_select_rt(0, 7, 5) == 1
+; run: %select_eq_eq_to_eq_select_rt(1, 5, 5) == 1
+; run: %select_eq_eq_to_eq_select_rt(1, 5, 7) == 0
+