Skip to content

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

Use a wrapping add in egraph optimizations (#11588)

Alex Crichton60a0443d7a7d
diff --git a/cranelift/codegen/src/opts/selects.isle b/cranelift/codegen/src/opts/selects.isle
index 8d24e54243..a80896b615 100644
--- a/cranelift/codegen/src/opts/selects.isle
+++ b/cranelift/codegen/src/opts/selects.isle
@@ -96,5 +96,5 @@
(rule (simplify
(iadd ty (select ty c (iconst_u ty x) (iconst_u ty y)) (iconst_u ty z)))
(select ty c
- (iconst ty (imm64_masked ty (u64_add x z)))
- (iconst ty (imm64_masked ty (u64_add y z)))))
+ (iconst ty (imm64_masked ty (u64_wrapping_add x z)))
+ (iconst ty (imm64_masked ty (u64_wrapping_add y z)))))
diff --git a/cranelift/filetests/filetests/egraph/selects.clif b/cranelift/filetests/filetests/egraph/selects.clif
index 519e227115..08b3ab2ed2 100644
--- a/cranelift/filetests/filetests/egraph/selects.clif
+++ b/cranelift/filetests/filetests/egraph/selects.clif
@@ -41,3 +41,23 @@ block0(v0: i8):
; return v8
; }
+
+;; (iadd ty (select ty c x y) z) -> (select ty c (x + z) (y + z))
+function %simplify_iadd_select_const_i64_big_constants(i8) -> i64 fast {
+block0(v0: i8):
+ v1 = iconst.i64 0xa000_0000_0000_0000
+ v2 = iconst.i64 0xb000_0000_0000_0000
+ v3 = select v0, v1, v2
+ v4 = iconst.i64 0xc000_0000_0000_0000
+ v5 = iadd v3, v4
+ return v5
+}
+
+; function %simplify_iadd_select_const_i64_big_constants(i8) -> i64 fast {
+; block0(v0: i8):
+; v6 = iconst.i64 0x6000_0000_0000_0000
+; v7 = iconst.i64 0x7000_0000_0000_0000
+; v8 = select v0, v6, v7 ; v6 = 0x6000_0000_0000_0000, v7 = 0x7000_0000_0000_0000
+; return v8
+; }
+