Skip to content

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

[Cranelift] `min x y < y => false` (#11998)

Bongjun Jang7d413555c075
diff --git a/cranelift/codegen/src/opts/selects.isle b/cranelift/codegen/src/opts/selects.isle
index 4ec353c3fb..6ba40558e7 100644
--- a/cranelift/codegen/src/opts/selects.isle
+++ b/cranelift/codegen/src/opts/selects.isle
@@ -102,3 +102,15 @@
(rule (simplify (select ty d a (select ty d _ y))) (select ty d a y))
(rule (simplify (select ty d (select ty d x _) a)) (select ty d x a))
+;; min x y < x => false and its commutative versions
+(rule (simplify (sgt ty (smin _ x y) x)) (iconst_u ty 0))
+(rule (simplify (sgt ty (smin _ x y) y)) (iconst_u ty 0))
+(rule (simplify (slt ty x (smin _ x z))) (iconst_u ty 0))
+(rule (simplify (slt ty x (smin _ y x))) (iconst_u ty 0))
+(rule (simplify (ugt ty (umin _ x y) x)) (iconst_u ty 0))
+(rule (simplify (ugt ty (umin _ x y) y)) (iconst_u ty 0))
+(rule (simplify (ult ty x (umin _ x z))) (iconst_u ty 0))
+(rule (simplify (ult ty x (umin _ y x))) (iconst_u ty 0))
+
+
+
diff --git a/cranelift/filetests/filetests/egraph/selects.clif b/cranelift/filetests/filetests/egraph/selects.clif
index 08b3ab2ed2..81a1b00a3b 100644
--- a/cranelift/filetests/filetests/egraph/selects.clif
+++ b/cranelift/filetests/filetests/egraph/selects.clif
@@ -61,3 +61,31 @@ block0(v0: i8):
; return v8
; }
+function %test_sgt_select_slt_min(i32, i32) -> i8 fast {
+block0(v0: i32, v1: i32):
+ v2 = icmp slt v1, v0
+ v3 = select v2, v1, v0
+ v4 = icmp sgt v3, v0
+ return v4
+}
+
+; function %test_sgt_select_slt_min(i32, i32) -> i8 fast {
+; block0(v0: i32, v1: i32):
+; v7 = iconst.i8 0
+; return v7 ; v7 = 0
+; }
+
+function %test_ugt_select_ult_min(i32, i32) -> i8 fast {
+block0(v0: i32, v1: i32):
+ v2 = icmp ult v1, v0
+ v3 = select v2, v1, v0
+ v4 = icmp ugt v3, v0
+ return v4
+}
+
+; function %test_ugt_select_ult_min(i32, i32) -> i8 fast {
+; block0(v0: i32, v1: i32):
+; v7 = iconst.i8 0
+; return v7 ; v7 = 0
+; }
+
diff --git a/cranelift/filetests/filetests/runtests/select.clif b/cranelift/filetests/filetests/runtests/select.clif
index d05a215479..4b6c121c6e 100644
--- a/cranelift/filetests/filetests/runtests/select.clif
+++ b/cranelift/filetests/filetests/runtests/select.clif
@@ -441,3 +441,24 @@ block0(v0: i64, v1: i64):
; run: %simplify_select_ult_ne_i64(7, 9) == 0
; run: %simplify_select_ult_ne_i64(3, 1) == 1
; run: %simplify_select_ult_ne_i64(-1, 42) == 1
+
+function %test_sgt_select_slt_min(i32, i32) -> i8 fast {
+block0(v0: i32, v1: i32):
+ v2 = icmp slt v1, v0 ; (slt y, x)
+ v3 = select v2, v1, v0 ; select( (y <_s x), y, x) -> smin(y, x)
+ v4 = icmp sgt v3, v0 ; sgt( min(y, x), x )
+ return v4
+}
+
+; run: %test_sgt_select_slt_min(42, 444) == 0
+
+function %test_ugt_select_ult_min(i32, i32) -> i8 fast {
+block0(v0: i32, v1: i32):
+ v2 = icmp ult v1, v0 ; (slt y, x)
+ v3 = select v2, v1, v0 ; select( (y <_s x), y, x) -> umin(y, x)
+ v4 = icmp ugt v3, v0 ; sgt( min(y, x), x )
+ return v4
+}
+
+; run: %test_ugt_select_ult_min(234234, 5544) == 0
+