summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/passes/OptimizeInstructions.cpp117
-rw-r--r--test/passes/optimize-instructions_all-features.txt106
-rw-r--r--test/passes/optimize-instructions_all-features.wast146
-rw-r--r--test/wasm2js/br_table_temp.2asm.js.opt6
4 files changed, 356 insertions, 19 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp
index 66c5269ff..cc8048511 100644
--- a/src/passes/OptimizeInstructions.cpp
+++ b/src/passes/OptimizeInstructions.cpp
@@ -275,6 +275,30 @@ struct OptimizeInstructions
}
}
{
+ // eqz(x - y) => x == y
+ Binary* inner;
+ if (matches(curr,
+ unary(Abstract::EqZ,
+ binary(&inner, Abstract::Sub, any(), any())))) {
+ inner->op = Abstract::getBinary(inner->left->type, Abstract::Eq);
+ inner->type = Type::i32;
+ return inner;
+ }
+ }
+ {
+ // eqz(x + C) => x == -C
+ Const* c;
+ Binary* inner;
+ if (matches(curr,
+ unary(Abstract::EqZ,
+ binary(&inner, Abstract::Add, any(), ival(&c))))) {
+ c->value = c->value.neg();
+ inner->op = Abstract::getBinary(c->type, Abstract::Eq);
+ inner->type = Type::i32;
+ return inner;
+ }
+ }
+ {
// eqz((signed)x % C_pot) => eqz(x & (abs(C_pot) - 1))
Const* c;
Binary* inner;
@@ -362,6 +386,24 @@ struct OptimizeInstructions
}
}
}
+ {
+ // unsigned(x) >= 0 => i32(1)
+ Const* c;
+ Expression* x;
+ if (matches(curr, binary(Abstract::GeU, pure(&x), ival(&c))) &&
+ c->value.isZero()) {
+ c->value = Literal::makeOne(Type::i32);
+ c->type = Type::i32;
+ return c;
+ }
+ // unsigned(x) < 0 => i32(0)
+ if (matches(curr, binary(Abstract::LtU, pure(&x), ival(&c))) &&
+ c->value.isZero()) {
+ c->value = Literal::makeZero(Type::i32);
+ c->type = Type::i32;
+ return c;
+ }
+ }
}
if (auto* select = curr->dynCast<Select>()) {
@@ -1659,31 +1701,31 @@ private:
}
// TODO: templatize on type?
- Expression* optimizeRelational(Binary* binary) {
- // TODO: inequalities can also work, if the constants do not overflow
- auto type = binary->right->type;
- // integer math, even on 2s complement, allows stuff like
- // x + 5 == 7
- // =>
- // x == 2
- if (binary->left->type.isInteger()) {
- if (binary->op == Abstract::getBinary(type, Abstract::Eq) ||
- binary->op == Abstract::getBinary(type, Abstract::Ne)) {
- if (auto* left = binary->left->dynCast<Binary>()) {
+ Expression* optimizeRelational(Binary* curr) {
+ auto type = curr->right->type;
+ if (curr->left->type.isInteger()) {
+ if (curr->op == Abstract::getBinary(type, Abstract::Eq) ||
+ curr->op == Abstract::getBinary(type, Abstract::Ne)) {
+ if (auto* left = curr->left->dynCast<Binary>()) {
+ // TODO: inequalities can also work, if the constants do not overflow
+ // integer math, even on 2s complement, allows stuff like
+ // x + 5 == 7
+ // =>
+ // x == 2
if (left->op == Abstract::getBinary(type, Abstract::Add) ||
left->op == Abstract::getBinary(type, Abstract::Sub)) {
if (auto* leftConst = left->right->dynCast<Const>()) {
- if (auto* rightConst = binary->right->dynCast<Const>()) {
+ if (auto* rightConst = curr->right->dynCast<Const>()) {
return combineRelationalConstants(
- binary, left, leftConst, nullptr, rightConst);
- } else if (auto* rightBinary = binary->right->dynCast<Binary>()) {
+ curr, left, leftConst, nullptr, rightConst);
+ } else if (auto* rightBinary = curr->right->dynCast<Binary>()) {
if (rightBinary->op ==
Abstract::getBinary(type, Abstract::Add) ||
rightBinary->op ==
Abstract::getBinary(type, Abstract::Sub)) {
if (auto* rightConst = rightBinary->right->dynCast<Const>()) {
return combineRelationalConstants(
- binary, left, leftConst, rightBinary, rightConst);
+ curr, left, leftConst, rightBinary, rightConst);
}
}
}
@@ -1691,6 +1733,51 @@ private:
}
}
}
+ // signed(x - y) <=> 0 => x <=> y
+ //
+ // unsigned(x - y) > 0 => x != y
+ // unsigned(x - y) <= 0 => x == y
+ {
+ using namespace Match;
+
+ BinaryOp op;
+ Binary* inner;
+ // unsigned(x - y) > 0 => x != y
+ if (matches(curr,
+ binary(Abstract::GtU,
+ binary(&inner, Abstract::Sub, any(), any()),
+ ival(0)))) {
+ curr->op = Abstract::getBinary(type, Abstract::Ne);
+ curr->right = inner->right;
+ curr->left = inner->left;
+ return curr;
+ }
+ // unsigned(x - y) <= 0 => x == y
+ if (matches(curr,
+ binary(Abstract::LeU,
+ binary(&inner, Abstract::Sub, any(), any()),
+ ival(0)))) {
+ curr->op = Abstract::getBinary(type, Abstract::Eq);
+ curr->right = inner->right;
+ curr->left = inner->left;
+ return curr;
+ }
+ // signed(x - y) <=> 0 => x <=> y
+ if (matches(curr,
+ binary(&op,
+ binary(&inner, Abstract::Sub, any(), any()),
+ ival(0))) &&
+ (op == Abstract::getBinary(type, Abstract::Eq) ||
+ op == Abstract::getBinary(type, Abstract::Ne) ||
+ op == Abstract::getBinary(type, Abstract::LeS) ||
+ op == Abstract::getBinary(type, Abstract::LtS) ||
+ op == Abstract::getBinary(type, Abstract::GeS) ||
+ op == Abstract::getBinary(type, Abstract::GtS))) {
+ curr->right = inner->right;
+ curr->left = inner->left;
+ return curr;
+ }
+ }
}
return nullptr;
}
diff --git a/test/passes/optimize-instructions_all-features.txt b/test/passes/optimize-instructions_all-features.txt
index b5e720353..7eebb3867 100644
--- a/test/passes/optimize-instructions_all-features.txt
+++ b/test/passes/optimize-instructions_all-features.txt
@@ -6,9 +6,9 @@
(type $none_=>_none (func))
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(type $i32_=>_none (func (param i32)))
+ (type $i32_i32_i64_i64_=>_none (func (param i32 i32 i64 i64)))
(type $none_=>_i64 (func (result i64)))
(type $i64_=>_i64 (func (param i64) (result i64)))
- (type $i32_i32_i64_i64_=>_none (func (param i32 i32 i64 i64)))
(type $i32_i64_f32_=>_none (func (param i32 i64 f32)))
(type $i32_i64_f32_f64_=>_none (func (param i32 i64 f32 f64)))
(type $none_=>_anyref (func (result anyref)))
@@ -4354,6 +4354,110 @@
)
)
)
+ (func $optimize-relationals (param $x i32) (param $y i32) (param $X i64) (param $Y i64)
+ (drop
+ (i32.eq
+ (local.get $x)
+ (i32.const -2147483647)
+ )
+ )
+ (drop
+ (i32.eq
+ (local.get $x)
+ (i32.const -2147483648)
+ )
+ )
+ (drop
+ (i32.eq
+ (local.get $x)
+ (i32.const 2147483647)
+ )
+ )
+ (drop
+ (i32.eq
+ (local.get $x)
+ (local.get $y)
+ )
+ )
+ (drop
+ (i64.eq
+ (local.get $X)
+ (local.get $Y)
+ )
+ )
+ (drop
+ (i32.eq
+ (local.get $x)
+ (local.get $y)
+ )
+ )
+ (drop
+ (i64.eq
+ (local.get $X)
+ (local.get $Y)
+ )
+ )
+ (drop
+ (i32.ne
+ (local.get $x)
+ (local.get $y)
+ )
+ )
+ (drop
+ (i64.ne
+ (local.get $X)
+ (local.get $Y)
+ )
+ )
+ (drop
+ (i32.gt_s
+ (local.get $x)
+ (local.get $y)
+ )
+ )
+ (drop
+ (i32.ge_s
+ (local.get $x)
+ (local.get $y)
+ )
+ )
+ (drop
+ (i32.ne
+ (local.get $x)
+ (local.get $y)
+ )
+ )
+ (drop
+ (i32.const 1)
+ )
+ (drop
+ (i32.const 1)
+ )
+ (drop
+ (i32.lt_s
+ (local.get $x)
+ (local.get $y)
+ )
+ )
+ (drop
+ (i32.le_s
+ (local.get $x)
+ (local.get $y)
+ )
+ )
+ (drop
+ (i32.const 0)
+ )
+ (drop
+ (i32.const 0)
+ )
+ (drop
+ (i32.eq
+ (local.get $x)
+ (local.get $y)
+ )
+ )
+ )
(func $unsigned-context (param $x i32) (param $y i64)
(drop
(i32.div_u
diff --git a/test/passes/optimize-instructions_all-features.wast b/test/passes/optimize-instructions_all-features.wast
index f857c1f5f..c2c1609ba 100644
--- a/test/passes/optimize-instructions_all-features.wast
+++ b/test/passes/optimize-instructions_all-features.wast
@@ -4842,6 +4842,152 @@
)
))
)
+ (func $optimize-relationals (param $x i32) (param $y i32) (param $X i64) (param $Y i64)
+ ;; eqz(x + 0x7FFFFFFF) -> x == -2147483647
+ (drop (i32.eqz
+ (i32.add
+ (local.get $x)
+ (i32.const 0x7FFFFFFF)
+ )
+ ))
+ ;; eqz(x + 0x80000000) -> x == -2147483648
+ (drop (i32.eqz
+ (i32.add
+ (local.get $x)
+ (i32.const 0x80000000)
+ )
+ ))
+ ;; eqz(x + 0x80000001) -> x == 2147483647
+ (drop (i32.eqz
+ (i32.add
+ (local.get $x)
+ (i32.const 0x80000001)
+ )
+ ))
+ ;; eqz(x - y)
+ (drop (i32.eqz
+ (i32.sub
+ (local.get $x)
+ (local.get $y)
+ )
+ ))
+ (drop (i64.eqz
+ (i64.sub
+ (local.get $X)
+ (local.get $Y)
+ )
+ ))
+ ;; x - y == 0
+ (drop (i32.eq
+ (i32.sub
+ (local.get $x)
+ (local.get $y)
+ )
+ (i32.const 0)
+ ))
+ (drop (i64.eq
+ (i64.sub
+ (local.get $X)
+ (local.get $Y)
+ )
+ (i64.const 0)
+ ))
+ ;; x - y != 0
+ (drop (i32.ne
+ (i32.sub
+ (local.get $x)
+ (local.get $y)
+ )
+ (i32.const 0)
+ ))
+ (drop (i64.ne
+ (i64.sub
+ (local.get $X)
+ (local.get $Y)
+ )
+ (i64.const 0)
+ ))
+ ;; i32(x - y) > 0 -> x > y
+ (drop (i32.gt_s
+ (i32.sub
+ (local.get $x)
+ (local.get $y)
+ )
+ (i32.const 0)
+ ))
+ ;; i32(x - y) >= 0 -> x >= y
+ (drop (i32.ge_s
+ (i32.sub
+ (local.get $x)
+ (local.get $y)
+ )
+ (i32.const 0)
+ ))
+ ;; u32(x - y) > 0 -> x != y
+ (drop (i32.gt_u
+ (i32.sub
+ (local.get $x)
+ (local.get $y)
+ )
+ (i32.const 0)
+ ))
+ ;; u32(x - y) >= 0 -> 1
+ (drop (i32.ge_u
+ (i32.sub
+ (local.get $x)
+ (local.get $y)
+ )
+ (i32.const 0)
+ ))
+ ;; u64(x - y) >= 0 -> i32(1)
+ (drop (i64.ge_u
+ (i64.sub
+ (local.get $X)
+ (local.get $Y)
+ )
+ (i64.const 0)
+ ))
+ ;; i32(x - y) < 0 -> x < y
+ (drop (i32.lt_s
+ (i32.sub
+ (local.get $x)
+ (local.get $y)
+ )
+ (i32.const 0)
+ ))
+ ;; i32(x - y) <= 0 -> x <= y
+ (drop (i32.le_s
+ (i32.sub
+ (local.get $x)
+ (local.get $y)
+ )
+ (i32.const 0)
+ ))
+ ;; u32(x - y) < 0 -> 0
+ (drop (i32.lt_u
+ (i32.sub
+ (local.get $x)
+ (local.get $y)
+ )
+ (i32.const 0)
+ ))
+ ;; u64(x - y) < 0 -> i32(0)
+ (drop (i64.lt_u
+ (i64.sub
+ (local.get $X)
+ (local.get $Y)
+ )
+ (i64.const 0)
+ ))
+ ;; u32(x - y) <= 0 -> x == y
+ (drop (i32.le_u
+ (i32.sub
+ (local.get $x)
+ (local.get $y)
+ )
+ (i32.const 0)
+ ))
+ )
(func $unsigned-context (param $x i32) (param $y i64)
(drop (i32.div_s
(i32.and
diff --git a/test/wasm2js/br_table_temp.2asm.js.opt b/test/wasm2js/br_table_temp.2asm.js.opt
index 4d73ee3b6..c96c0f18e 100644
--- a/test/wasm2js/br_table_temp.2asm.js.opt
+++ b/test/wasm2js/br_table_temp.2asm.js.opt
@@ -12611,10 +12611,10 @@ function asmFunc(global, env) {
function $63($0) {
$0 = $0 | 0;
- if ($0 - 1 | 0) {
- $0 = 9
- } else {
+ if (($0 | 0) == 1) {
$0 = 8
+ } else {
+ $0 = 9
}
return $0 | 0;
}