summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/passes/OptimizeInstructions.cpp4
-rw-r--r--test/passes/optimize-instructions.txt19
-rw-r--r--test/passes/optimize-instructions.wast23
3 files changed, 38 insertions, 8 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp
index c81119f75..efd9e0037 100644
--- a/src/passes/OptimizeInstructions.cpp
+++ b/src/passes/OptimizeInstructions.cpp
@@ -179,8 +179,8 @@ static Index getMaxBits(Expression* curr) {
case AddInt32: case SubInt32: case MulInt32:
case DivSInt32: case DivUInt32: case RemSInt32:
case RemUInt32: case RotLInt32: case RotRInt32: return 32;
- case AndInt32: case XorInt32: return std::min(getMaxBits(binary->left), getMaxBits(binary->right));
- case OrInt32: return std::max(getMaxBits(binary->left), getMaxBits(binary->right));
+ case AndInt32: return std::min(getMaxBits(binary->left), getMaxBits(binary->right));
+ case OrInt32: case XorInt32: return std::max(getMaxBits(binary->left), getMaxBits(binary->right));
case ShlInt32: {
if (auto* shifts = binary->right->dynCast<Const>()) {
return std::min(Index(32), getMaxBits(binary->left) + shifts->value.geti32());
diff --git a/test/passes/optimize-instructions.txt b/test/passes/optimize-instructions.txt
index e4ab82365..3b4718f6a 100644
--- a/test/passes/optimize-instructions.txt
+++ b/test/passes/optimize-instructions.txt
@@ -669,15 +669,15 @@
(drop
(i32.xor
(i32.const 127)
- (i32.const 128)
+ (i32.const 126)
)
)
(drop
(i32.shr_s
(i32.shl
(i32.xor
+ (i32.const 127)
(i32.const 128)
- (i32.const 129)
)
(i32.const 24)
)
@@ -914,6 +914,21 @@
(i32.const 25)
)
)
+ (drop
+ (i32.shr_s
+ (i32.shl
+ (i32.xor
+ (i32.le_u
+ (get_local $0)
+ (i32.const 2)
+ )
+ (get_local $0)
+ )
+ (i32.const 24)
+ )
+ (i32.const 24)
+ )
+ )
)
(func $linear-sums (type $4) (param $0 i32) (param $1 i32)
(drop
diff --git a/test/passes/optimize-instructions.wast b/test/passes/optimize-instructions.wast
index 7cfcd7a0d..f8c93d03c 100644
--- a/test/passes/optimize-instructions.wast
+++ b/test/passes/optimize-instructions.wast
@@ -654,9 +654,9 @@
(drop
(i32.shr_s
(i32.shl
- (i32.xor ;; takes the min, here it is ok
+ (i32.xor ;; takes the max, here it is ok
(i32.const 127)
- (i32.const 128)
+ (i32.const 126)
)
(i32.const 24)
)
@@ -666,9 +666,9 @@
(drop
(i32.shr_s
(i32.shl
- (i32.xor ;; takes the min, here it is not
+ (i32.xor ;; takes the max, here it is not
+ (i32.const 127)
(i32.const 128)
- (i32.const 129)
)
(i32.const 24)
)
@@ -1013,6 +1013,21 @@
(i32.const 24)
)
)
+ (drop ;; fuzz testcase
+ (i32.shr_s
+ (i32.shl
+ (i32.xor ;; should be 32 bits
+ (i32.le_u ;; 1 bit
+ (get_local $0)
+ (i32.const 2)
+ )
+ (get_local $0) ;; unknown, so 32 bits
+ )
+ (i32.const 24)
+ )
+ (i32.const 24)
+ )
+ )
)
(func $linear-sums (param $0 i32) (param $1 i32)
(drop