Skip to content

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

[Cranelift] `a < b && a > b = false` (#11997)

Bongjun Jang6f4d26eaaae5
diff --git a/cranelift/codegen/src/opts/icmp.isle b/cranelift/codegen/src/opts/icmp.isle
index d007831886..a498130fb1 100644
--- a/cranelift/codegen/src/opts/icmp.isle
+++ b/cranelift/codegen/src/opts/icmp.isle
@@ -302,8 +302,45 @@
(rule (simplify (ult cty (bnot ty x) (bnot ty y))) (ugt cty x y))
(rule (simplify (slt cty (bnot ty x) (bnot ty y))) (sgt cty x y))
-
;; a < b ^^ a > b => (a ≠ b)
(rule (simplify (bxor ty (slt cty x y) (sgt cty x y))) (ne cty x y))
(rule (simplify (bxor ty (ult cty x y) (ugt cty x y))) (ne cty x y))
+;; a < b && a > b = false
+(rule (simplify (band ty (sgt ty x y) (slt ty x y))) (iconst_u ty 0))
+(rule (simplify (band ty (slt ty x y) (sgt ty x y))) (iconst_u ty 0))
+(rule (simplify (band ty (ugt ty x y) (ult ty x y))) (iconst_u ty 0))
+(rule (simplify (band ty (ult ty x y) (ugt ty x y))) (iconst_u ty 0))
+(rule
+ (simplify (band ty (sgt ty x (iconst_s _ y)) (ult ty x (iconst_s _ y))))
+ (if-let true (i64_gt_eq y 0))
+ (iconst_u ty 0))
+(rule
+ (simplify (band ty (sgt ty (iconst_s _ x) y) (ult ty (iconst_s _ x) y)))
+ (if-let true (i64_lt x 0))
+ (iconst_u ty 0))
+(rule
+ (simplify (band ty (slt ty x (iconst_s _ y)) (ugt ty x (iconst_s _ y))))
+ (if-let true (i64_lt y 0))
+ (iconst_u ty 0))
+(rule
+ (simplify (band ty (slt ty (iconst_s _ x) y) (ugt ty (iconst_s _ x) y)))
+ (if-let true (i64_gt_eq x 0))
+ (iconst_u ty 0))
+(rule
+ (simplify (band ty (ugt ty x (iconst_s _ y)) (slt ty x (iconst_s _ y))))
+ (if-let true (i64_lt y 0))
+ (iconst_u ty 0))
+(rule
+ (simplify (band ty (ugt ty (iconst_s _ x) y) (slt ty (iconst_s _ x) y)))
+ (if-let true (i64_gt_eq x 0))
+ (iconst_u ty 0))
+(rule
+ (simplify (band ty (ult ty x (iconst_s _ y)) (sgt ty x (iconst_s _ y))))
+ (if-let true (i64_gt_eq y 0))
+ (iconst_u ty 0))
+(rule
+ (simplify (band ty (ult ty (iconst_s _ x) y) (sgt ty (iconst_s _ x) y)))
+ (if-let true (i64_lt x 0))
+ (iconst_u ty 0))
+
diff --git a/cranelift/filetests/filetests/egraph/icmp.clif b/cranelift/filetests/filetests/egraph/icmp.clif
index 10e0caf5c3..31398003b2 100644
--- a/cranelift/filetests/filetests/egraph/icmp.clif
+++ b/cranelift/filetests/filetests/egraph/icmp.clif
@@ -311,3 +311,97 @@ block0(v0: i32, v1: i32):
; return v5
; }
+;; Rule 1: (x <_s y) & (x >_s y) => false
+function %test_slt_sgt(i32, i32) -> i8 fast {
+block0(v0: i32, v1: i32):
+ v2 = icmp slt v0, v1
+ v3 = icmp sgt v0, v1
+ v4 = band v2, v3
+ return v4
+}
+
+; function %test_slt_sgt(i32, i32) -> i8 fast {
+; block0(v0: i32, v1: i32):
+; v5 = iconst.i8 0
+; return v5 ; v5 = 0
+; }
+
+;; Rule 2: (x <_u y) & (x >_u y) => false
+function %test_ult_ugt(i32, i32) -> i8 fast {
+block0(v0: i32, v1: i32):
+ v2 = icmp ult v0, v1
+ v3 = icmp ugt v0, v1
+ v4 = band v2, v3
+ return v4
+}
+
+; function %test_ult_ugt(i32, i32) -> i8 fast {
+; block0(v0: i32, v1: i32):
+; v5 = iconst.i8 0
+; return v5 ; v5 = 0
+; }
+
+;; Rule 3: (x <_s y_const) & (x >_u y_const) => false, if y_const < 0
+function %test_slt_ugt_neg_const_y(i32) -> i8 fast {
+block0(v0: i32):
+ v1 = iconst.i32 -10
+ v2 = icmp slt v0, v1
+ v3 = icmp ugt v0, v1
+ v4 = band v2, v3
+ return v4
+}
+
+; function %test_slt_ugt_neg_const_y(i32) -> i8 fast {
+; block0(v0: i32):
+; v5 = iconst.i8 0
+; return v5 ; v5 = 0
+; }
+
+;; Rule 4: (x_const <_s y) & (x_const >_u y) => false, if x_const >= 0
+function %test_slt_ugt_pos_const_x(i32) -> i8 fast {
+block0(v0: i32):
+ v1 = iconst.i32 10
+ v2 = icmp slt v1, v0
+ v3 = icmp ugt v1, v0
+ v4 = band v2, v3
+ return v4
+}
+
+; function %test_slt_ugt_pos_const_x(i32) -> i8 fast {
+; block0(v0: i32):
+; v9 = iconst.i8 0
+; return v9 ; v9 = 0
+; }
+
+;; Rule 5: (x <_u y_const) & (x >_s y_const) => false, if y_const >= 0
+function %test_ult_sgt_pos_const_y(i32) -> i8 fast {
+block0(v0: i32):
+ v1 = iconst.i32 10
+ v2 = icmp ult v0, v1
+ v3 = icmp sgt v0, v1
+ v4 = band v2, v3
+ return v4
+}
+
+; function %test_ult_sgt_pos_const_y(i32) -> i8 fast {
+; block0(v0: i32):
+; v5 = iconst.i8 0
+; return v5 ; v5 = 0
+; }
+
+;; Rule 6: (x_const <_u y) & (x_const >_s y) => false, if x_const < 0
+function %test_ult_sgt_neg_const_x(i32) -> i8 fast {
+block0(v0: i32):
+ v1 = iconst.i32 -10
+ v2 = icmp ult v1, v0
+ v3 = icmp sgt v1, v0
+ v4 = band v2, v3
+ return v4
+}
+
+; function %test_ult_sgt_neg_const_x(i32) -> i8 fast {
+; block0(v0: i32):
+; v9 = iconst.i8 0
+; return v9 ; v9 = 0
+; }
+
diff --git a/cranelift/filetests/filetests/runtests/icmp.clif b/cranelift/filetests/filetests/runtests/icmp.clif
index 7e69545cca..77f7d66421 100644
--- a/cranelift/filetests/filetests/runtests/icmp.clif
+++ b/cranelift/filetests/filetests/runtests/icmp.clif
@@ -89,3 +89,73 @@ block0(v0: i32, v1: i32):
; run: %test_bxor_ult_ugt(1, 1) == 0
; run: %test_bxor_ult_ugt(1, 3) == 1
+;; Rule 1: (x <_s y) & (x >_s y) => false
+function %test_slt_sgt(i32, i32) -> i8 fast {
+block0(v0: i32, v1: i32):
+ v2 = icmp slt v0, v1 ; (x <_s y)
+ v3 = icmp sgt v0, v1 ; (x >_s y)
+ v4 = band v2, v3 ; (x <_s y) & (x >_s y)
+ return v4
+}
+
+; run: %test_slt_sgt(42, 24) == 0
+
+;; Rule 2: (x <_u y) & (x >_u y) => false
+function %test_ult_ugt(i32, i32) -> i8 fast {
+block0(v0: i32, v1: i32):
+ v2 = icmp ult v0, v1 ; (x <_u y)
+ v3 = icmp ugt v0, v1 ; (x >_u y)
+ v4 = band v2, v3 ; (x <_u y) & (x >_u y)
+ return v4
+}
+
+; run: %test_ult_ugt(42, 24) == 0
+
+;; Rule 3: (x <_s y_const) & (x >_u y_const) => false, if y_const < 0
+function %test_slt_ugt_neg_const_y(i32) -> i8 fast {
+block0(v0: i32):
+ v1 = iconst.i32 -10 ; y_const = -10. Triggers (i64_lt y 0)
+ v2 = icmp slt v0, v1 ; (x <_s -10)
+ v3 = icmp ugt v0, v1 ; (x >_u -10)
+ v4 = band v2, v3
+ return v4
+}
+
+; run: %test_slt_ugt_neg_const_y(42) == 0
+
+;; Rule 4: (x_const <_s y) & (x_const >_u y) => false, if x_const >= 0
+function %test_slt_ugt_pos_const_x(i32) -> i8 fast {
+block0(v0: i32):
+ v1 = iconst.i32 10
+ v2 = icmp slt v1, v0
+ v3 = icmp ugt v1, v0
+ v4 = band v2, v3
+ return v4
+}
+
+; run: %test_slt_ugt_pos_const_x(42) == 0
+
+;; Rule 5: (x <_u y_const) & (x >_s y_const) => false, if y_const >= 0
+function %test_ult_sgt_pos_const_y(i32) -> i8 fast {
+block0(v0: i32):
+ v1 = iconst.i32 10
+ v2 = icmp ult v0, v1
+ v3 = icmp sgt v0, v1
+ v4 = band v2, v3
+ return v4
+}
+
+; run: %test_ult_sgt_pos_const_y(42) == 0
+
+;; Rule 6: (x_const <_u y) & (x_const >_s y) => false, if x_const < 0
+function %test_ult_sgt_neg_const_x(i32) -> i8 fast {
+block0(v0: i32):
+ v1 = iconst.i32 -10
+ v2 = icmp ult v1, v0
+ v3 = icmp sgt v1, v0
+ v4 = band v2, v3
+ return v4
+}
+
+; run: %test_ult_sgt_neg_const_x(42) == 0
+