Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/passes/OptimizeInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2157,6 +2157,67 @@ struct OptimizeInstructions
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.
Expand Down
130 changes: 130 additions & 0 deletions test/lit/passes/optimize-instructions.wast
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The text here is wrong, == should be != (but the optimizer output is right).

Same in the comment below, != should be ==.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Good catch. Do you want a separate PR which fix this comment or just append this minor fix with some other existing PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A separate PR might be simplest. I am not up to date on the other PRs and am not sure when they will land. A separate PR can be landed immediately.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(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
Expand Down
2 changes: 1 addition & 1 deletion test/lit/wat-kitchen-sink.wast
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down