summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/passes/OptimizeInstructions.cpp8
-rw-r--r--test/passes/optimize-instructions_all-features.txt16
-rw-r--r--test/passes/optimize-instructions_all-features.wast22
3 files changed, 38 insertions, 8 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp
index 0ce1f5f95..b11df3276 100644
--- a/src/passes/OptimizeInstructions.cpp
+++ b/src/passes/OptimizeInstructions.cpp
@@ -346,6 +346,7 @@ struct OptimizeInstructions
{
// x <<>> (C & (31 | 63)) ==> x <<>> C'
// x <<>> (y & (31 | 63)) ==> x <<>> y
+ // x <<>> (y & (32 | 64)) ==> x
// where '<<>>':
// '<<', '>>', '>>>'. 'rotl' or 'rotr'
BinaryOp op;
@@ -375,6 +376,13 @@ struct OptimizeInstructions
curr->cast<Binary>()->right = y;
return curr;
}
+ // i32(x) <<>> (y & 32) ==> x
+ // i64(x) <<>> (y & 64) ==> x
+ if (((c->type == Type::i32 && (c->value.geti32() & 31) == 0) ||
+ (c->type == Type::i64 && (c->value.geti64() & 63LL) == 0LL)) &&
+ !effects(y).hasSideEffects()) {
+ return x;
+ }
}
}
{
diff --git a/test/passes/optimize-instructions_all-features.txt b/test/passes/optimize-instructions_all-features.txt
index 27daa5ce1..80bd7a89d 100644
--- a/test/passes/optimize-instructions_all-features.txt
+++ b/test/passes/optimize-instructions_all-features.txt
@@ -4968,11 +4968,17 @@
)
)
(drop
- (i32.shl
- (local.get $x)
- (i32.and
- (local.get $y)
- (i32.const 32)
+ (local.get $x)
+ )
+ (drop
+ (local.get $z)
+ )
+ (drop
+ (i64.shl
+ (local.get $z)
+ (i64.and
+ (local.get $w)
+ (i64.const 32)
)
)
)
diff --git a/test/passes/optimize-instructions_all-features.wast b/test/passes/optimize-instructions_all-features.wast
index ad422bd98..c3a6aaf59 100644
--- a/test/passes/optimize-instructions_all-features.wast
+++ b/test/passes/optimize-instructions_all-features.wast
@@ -5458,15 +5458,31 @@
(i64.const 63)
)
))
-
- ;; skip
- (drop (i32.shl
+ ;; i32(x) >> (y & 32) -> x
+ (drop (i32.shr_u
(local.get $x)
(i32.and
(local.get $y)
(i32.const 32)
)
))
+ ;; i64(x) >> (y & 64) -> x
+ (drop (i64.shr_u
+ (local.get $z)
+ (i64.and
+ (local.get $w)
+ (i64.const 128)
+ )
+ ))
+
+ ;; skip
+ (drop (i64.shl
+ (local.get $z)
+ (i64.and
+ (local.get $w)
+ (i64.const 32)
+ )
+ ))
;; skip
(drop (i64.shr_u
(local.get $z)