A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
fix(cranelift): fix sdiv by most negative number (#11931)
Emma Turnerf95d2dab6538
diff --git a/cranelift/codegen/src/opts/arithmetic.isle b/cranelift/codegen/src/opts/arithmetic.isleindex aba392058d..b6c3e00aa8 100644--- a/cranelift/codegen/src/opts/arithmetic.isle+++ b/cranelift/codegen/src/opts/arithmetic.isle@@ -86,6 +86,10 @@;; Signed `x / d` when d is a power of two is a bit more involved...(rule (simplify_skeleton (sdiv x (iconst_u ty (u64_extract_power_of_two d))))(if-let true (u64_gt d 1))+ ;; This rule musn't fire for the most negative number - which looks like+ ;; a power of two (sign bit set and otherwise all zeros)+ (if-let true (u32_lt (u64_trailing_zeros d)+ (u32_sub (ty_bits ty) 1)))(let ((k u32 (u64_trailing_zeros d))(t1 Value (sshr ty x (iconst_u ty (u32_sub k 1))))(t2 Value (ushr ty t1 (iconst_u ty (u32_sub (ty_bits ty) k))))diff --git a/cranelift/filetests/filetests/runtests/sdiv.clif b/cranelift/filetests/filetests/runtests/sdiv.clifindex 0efd348515..33b68718ec 100644--- a/cranelift/filetests/filetests/runtests/sdiv.clif+++ b/cranelift/filetests/filetests/runtests/sdiv.clif@@ -1,5 +1,7 @@test interprettest run+;; if opt_level=none, none of the constant denominator optimisations fire+set opt_level=speedtarget aarch64target s390xtarget riscv64@@ -190,3 +192,23 @@ block0(v0: i64):; run: %sdiv_by_const_neg_1337_i64(-56155) == 42; run: %sdiv_by_const_neg_1337_i64(-57490) == 42; run: %sdiv_by_const_neg_1337_i64(-57491) == 43++function %sdiv_by_const_neg_i64min_i64(i64) -> i64 {+block0(v0: i64):+ v1 = iconst.i64 -9223372036854775808+ v2 = sdiv v0, v1+ return v2+}++; run: %sdiv_by_const_neg_i64min_i64(-9223372036854775808) == 1+++function %sdiv_by_const_neg_i32min_i32(i32) -> i32 {+block0(v0: i32):+ v1 = iconst.i32 -2147483648+ v2 = sdiv v0, v1+ return v2+}++; run: %sdiv_by_const_neg_i32min_i32(-2147483648) == 1+