summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Graey <maxgraey@gmail.com>2022-08-27 04:58:35 +0300
committerGitHub <noreply@github.com>2022-08-26 18:58:35 -0700
commitd5b6972c9963be063d9f8db4ed8c99166c1c7ac5 (patch)
treef9c34a2730439312d163a4828e85919388779956
parentc929cf6b5ce444e1fadd52f6d289142ad5b3df5a (diff)
downloadbinaryen-d5b6972c9963be063d9f8db4ed8c99166c1c7ac5.tar.gz
binaryen-d5b6972c9963be063d9f8db4ed8c99166c1c7ac5.tar.bz2
binaryen-d5b6972c9963be063d9f8db4ed8c99166c1c7ac5.zip
[OptimizeInstructions] Canonicalize relational ops near min/max values (#4282)
A continuation of #4272. ``` (signed)x < s_min + 1 ==> x == s_min (signed)x >= s_min + 1 ==> x != s_min (signed)x > s_max - 1 ==> x == s_max (signed)x <= s_max - 1 ==> x != s_max (unsigned)x <= u_max - 1 ==> x != u_max (unsigned)x > u_max - 1 ==> x == u_max ```
-rw-r--r--src/passes/OptimizeInstructions.cpp61
-rw-r--r--test/lit/passes/optimize-instructions.wast130
-rw-r--r--test/lit/wat-kitchen-sink.wast2
3 files changed, 192 insertions, 1 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp
index 8b71da824..b50f99fbc 100644
--- a/src/passes/OptimizeInstructions.cpp
+++ b/src/passes/OptimizeInstructions.cpp
@@ -2157,6 +2157,67 @@ private:
c->value = Literal::makeZero(c->type);
return;
}
+ // Prefer compare to signed min (s_min) instead of s_min + 1.
+ // (signed)x < s_min + 1 ==> x == s_min
+ if (binary->op == LtSInt32 && c->value.geti32() == INT32_MIN + 1) {
+ binary->op = EqInt32;
+ c->value = Literal::makeSignedMin(Type::i32);
+ return;
+ }
+ if (binary->op == LtSInt64 && c->value.geti64() == INT64_MIN + 1) {
+ binary->op = EqInt64;
+ c->value = Literal::makeSignedMin(Type::i64);
+ return;
+ }
+ // (signed)x >= s_min + 1 ==> x != s_min
+ if (binary->op == GeSInt32 && c->value.geti32() == INT32_MIN + 1) {
+ binary->op = NeInt32;
+ c->value = Literal::makeSignedMin(Type::i32);
+ return;
+ }
+ if (binary->op == GeSInt64 && c->value.geti64() == INT64_MIN + 1) {
+ binary->op = NeInt64;
+ c->value = Literal::makeSignedMin(Type::i64);
+ return;
+ }
+ // Prefer compare to signed max (s_max) instead of s_max - 1.
+ // (signed)x > s_max - 1 ==> x == s_max
+ if (binary->op == GtSInt32 && c->value.geti32() == INT32_MAX - 1) {
+ binary->op = EqInt32;
+ c->value = Literal::makeSignedMax(Type::i32);
+ return;
+ }
+ if (binary->op == GtSInt64 && c->value.geti64() == INT64_MAX - 1) {
+ binary->op = EqInt64;
+ c->value = Literal::makeSignedMax(Type::i64);
+ return;
+ }
+ // (signed)x <= s_max - 1 ==> x != s_max
+ if (binary->op == LeSInt32 && c->value.geti32() == INT32_MAX - 1) {
+ binary->op = NeInt32;
+ c->value = Literal::makeSignedMax(Type::i32);
+ return;
+ }
+ if (binary->op == LeSInt64 && c->value.geti64() == INT64_MAX - 1) {
+ binary->op = NeInt64;
+ c->value = Literal::makeSignedMax(Type::i64);
+ return;
+ }
+ // Prefer compare to unsigned max (u_max) instead of u_max - 1.
+ // (unsigned)x <= u_max - 1 ==> x != u_max
+ if (binary->op == Abstract::getBinary(c->type, Abstract::LeU) &&
+ c->value.getInteger() == (int64_t)(UINT64_MAX - 1)) {
+ binary->op = Abstract::getBinary(c->type, Abstract::Ne);
+ c->value = Literal::makeUnsignedMax(c->type);
+ return;
+ }
+ // (unsigned)x > u_max - 1 ==> x == u_max
+ if (binary->op == Abstract::getBinary(c->type, Abstract::GtU) &&
+ c->value.getInteger() == (int64_t)(UINT64_MAX - 1)) {
+ binary->op = Abstract::getBinary(c->type, Abstract::Eq);
+ c->value = Literal::makeUnsignedMax(c->type);
+ return;
+ }
return;
}
// Prefer a get on the right.
diff --git a/test/lit/passes/optimize-instructions.wast b/test/lit/passes/optimize-instructions.wast
index 73e7fd286..91e08e328 100644
--- a/test/lit/passes/optimize-instructions.wast
+++ b/test/lit/passes/optimize-instructions.wast
@@ -1644,6 +1644,136 @@
(i32.const 1)
))
)
+ ;; CHECK: (func $canonicalize-cmp-near-min-max (param $x i32) (param $y i64)
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (i32.eq
+ ;; CHECK-NEXT: (local.get $x)
+ ;; CHECK-NEXT: (i32.const -2147483648)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (i64.eq
+ ;; CHECK-NEXT: (local.get $y)
+ ;; CHECK-NEXT: (i64.const -9223372036854775808)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (i32.ne
+ ;; CHECK-NEXT: (local.get $x)
+ ;; CHECK-NEXT: (i32.const -2147483648)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (i64.ne
+ ;; CHECK-NEXT: (local.get $y)
+ ;; CHECK-NEXT: (i64.const -9223372036854775808)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (i32.eq
+ ;; CHECK-NEXT: (local.get $x)
+ ;; CHECK-NEXT: (i32.const 2147483647)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (i64.eq
+ ;; CHECK-NEXT: (local.get $y)
+ ;; CHECK-NEXT: (i64.const 9223372036854775807)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (i32.ne
+ ;; CHECK-NEXT: (local.get $x)
+ ;; CHECK-NEXT: (i32.const 2147483647)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (i64.ne
+ ;; CHECK-NEXT: (local.get $y)
+ ;; CHECK-NEXT: (i64.const 9223372036854775807)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (i32.ne
+ ;; CHECK-NEXT: (local.get $x)
+ ;; CHECK-NEXT: (i32.const -1)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (i64.ne
+ ;; CHECK-NEXT: (local.get $y)
+ ;; CHECK-NEXT: (i64.const -1)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (i32.eq
+ ;; CHECK-NEXT: (local.get $x)
+ ;; CHECK-NEXT: (i32.const -1)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (i64.eq
+ ;; CHECK-NEXT: (local.get $y)
+ ;; CHECK-NEXT: (i64.const -1)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ (func $canonicalize-cmp-near-min-max (param $x i32) (param $y i64)
+ ;; (signed)x < s_min + 1 ==> x == s_min
+ (drop (i32.lt_s
+ (local.get $x)
+ (i32.const -2147483647)
+ ))
+ (drop (i64.lt_s
+ (local.get $y)
+ (i64.const -9223372036854775807)
+ ))
+ ;; (signed)x >= s_min + 1 ==> x != s_min
+ (drop (i32.ge_s
+ (local.get $x)
+ (i32.const -2147483647)
+ ))
+ (drop (i64.ge_s
+ (local.get $y)
+ (i64.const -9223372036854775807)
+ ))
+ ;; (signed)x > s_max - 1 ==> x == s_max
+ (drop (i32.gt_s
+ (local.get $x)
+ (i32.const 2147483646)
+ ))
+ (drop (i64.gt_s
+ (local.get $y)
+ (i64.const 9223372036854775806)
+ ))
+ ;; (signed)x <= s_max - 1 ==> x != s_max
+ (drop (i32.le_s
+ (local.get $x)
+ (i32.const 2147483646)
+ ))
+ (drop (i64.le_s
+ (local.get $y)
+ (i64.const 9223372036854775806)
+ ))
+ ;; (unsigned)x <= u_max - 1 ==> x == u_max
+ (drop (i32.le_u
+ (local.get $x)
+ (i32.const -2)
+ ))
+ (drop (i64.le_u
+ (local.get $y)
+ (i64.const -2)
+ ))
+ ;; (unsigned)x > u_max - 1 ==> x != u_max
+ (drop (i32.gt_u
+ (local.get $x)
+ (i32.const -2)
+ ))
+ (drop (i64.gt_u
+ (local.get $y)
+ (i64.const -2)
+ ))
+ )
;; CHECK: (func $canonicalize-cmp-const (param $x i32) (param $fx f64)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.le_s
diff --git a/test/lit/wat-kitchen-sink.wast b/test/lit/wat-kitchen-sink.wast
index 09c983c5d..a8a9ea42d 100644
--- a/test/lit/wat-kitchen-sink.wast
+++ b/test/lit/wat-kitchen-sink.wast
@@ -9,7 +9,7 @@
;; CHECK: (type $i32_=>_none (func_subtype (param i32) func))
- ;; CHECK: (rec
+ ;; CHECK: (rec
;; CHECK-NEXT: (type $s0 (struct_subtype data))
(type $s0 (sub (struct)))
;; CHECK: (type $s1 (struct_subtype data))