summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Graey <maxgraey@gmail.com>2020-11-11 01:56:14 +0200
committerGitHub <noreply@github.com>2020-11-10 15:56:14 -0800
commitbb124f995527f650d98bdac30b00b125b76fab9c (patch)
tree05c63599a575cf40b1aff5ed5c4da7211324bb28
parent236296d8563e0f90c3b57f32f1f8f63bac414b89 (diff)
downloadbinaryen-bb124f995527f650d98bdac30b00b125b76fab9c.tar.gz
binaryen-bb124f995527f650d98bdac30b00b125b76fab9c.tar.bz2
binaryen-bb124f995527f650d98bdac30b00b125b76fab9c.zip
Optimize i32(x) % C_pot in boolean context (#3307)
bool(i32(x) % C_pot) -> bool(i32(x) & (C_pot - 1)) bool(i32(x) % min_s) -> bool(i32(x) & max_s) For all other situations we already do this for (i32|i64).rem_s
-rw-r--r--src/passes/OptimizeInstructions.cpp19
-rw-r--r--test/passes/optimize-instructions_all-features.txt20
-rw-r--r--test/passes/optimize-instructions_all-features.wast18
3 files changed, 55 insertions, 2 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp
index 3952c765f..51a188c15 100644
--- a/src/passes/OptimizeInstructions.cpp
+++ b/src/passes/OptimizeInstructions.cpp
@@ -431,8 +431,8 @@ struct OptimizeInstructions
curr->cast<Binary>()->right = y;
return curr;
}
- // i32(x) <<>> (y & 32) ==> x
- // i64(x) <<>> (y & 64) ==> x
+ // i32(x) <<>> (y & C) ==> x, where (C & 31) == 0
+ // i64(x) <<>> (y & C) ==> x, where (C & 63) == 0
if (((c->type == Type::i32 && (c->value.geti32() & 31) == 0) ||
(c->type == Type::i64 && (c->value.geti64() & 63LL) == 0LL)) &&
!effects(y).hasSideEffects()) {
@@ -1052,6 +1052,21 @@ private:
// return binary;
// }
}
+ } else if (binary->op == RemSInt32) {
+ // bool(i32(x) % C_pot) ==> bool(x & (C_pot - 1))
+ // bool(i32(x) % min_s) ==> bool(x & max_s)
+ if (auto* c = binary->right->dynCast<Const>()) {
+ if (c->value.isSignedMin() ||
+ Bits::isPowerOf2(c->value.abs().geti32())) {
+ binary->op = AndInt32;
+ if (c->value.isSignedMin()) {
+ c->value = Literal::makeSignedMax(Type::i32);
+ } else {
+ c->value = c->value.abs().sub(Literal::makeOne(Type::i32));
+ }
+ return binary;
+ }
+ }
}
if (auto* ext = Properties::getSignExtValue(binary)) {
// use a cheaper zero-extent, we just care about the boolean value
diff --git a/test/passes/optimize-instructions_all-features.txt b/test/passes/optimize-instructions_all-features.txt
index a5cc470fb..88b053835 100644
--- a/test/passes/optimize-instructions_all-features.txt
+++ b/test/passes/optimize-instructions_all-features.txt
@@ -4005,6 +4005,26 @@
(i32.const 0)
)
)
+ (drop
+ (if (result i32)
+ (i32.and
+ (local.get $x)
+ (i32.const 3)
+ )
+ (i32.const 1)
+ (i32.const 0)
+ )
+ )
+ (drop
+ (if (result i32)
+ (i32.and
+ (local.get $x)
+ (i32.const 2147483647)
+ )
+ (i32.const 1)
+ (i32.const 0)
+ )
+ )
)
(func $optimize-bitwise-oprations (param $x i32) (param $y i32) (param $z i64) (param $w i64)
(drop
diff --git a/test/passes/optimize-instructions_all-features.wast b/test/passes/optimize-instructions_all-features.wast
index b28536519..5804c1b2a 100644
--- a/test/passes/optimize-instructions_all-features.wast
+++ b/test/passes/optimize-instructions_all-features.wast
@@ -4517,6 +4517,24 @@
)
(i32.const 0)
))
+ ;; (signed)x % 4 ? 1 : 0
+ (drop (if (result i32)
+ (i32.rem_s
+ (local.get $x)
+ (i32.const 4)
+ )
+ (i32.const 1)
+ (i32.const 0)
+ ))
+ ;; (signed)x % min_s ? 1 : 0
+ (drop (if (result i32)
+ (i32.rem_s
+ (local.get $x)
+ (i32.const 0x80000000)
+ )
+ (i32.const 1)
+ (i32.const 0)
+ ))
)
(func $optimize-bitwise-oprations (param $x i32) (param $y i32) (param $z i64) (param $w i64)
;; ~(1 << x) -> rotl(-2, x)