diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index cbd8c932359..0616194f124 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -1363,9 +1363,8 @@ struct OptimizeInstructions auto type = binary->right->type; auto* right = binary->right->cast(); if (type.isInteger()) { - auto constRight = right->value.getInteger(); // operations on zero - if (constRight == 0LL) { + if (right->value == Literal::makeFromInt32(0, type)) { if (binary->op == Abstract::getBinary(type, Abstract::Shl) || binary->op == Abstract::getBinary(type, Abstract::ShrU) || binary->op == Abstract::getBinary(type, Abstract::ShrS) || @@ -1382,7 +1381,7 @@ struct OptimizeInstructions } } // operations on one - if (constRight == 1LL) { + if (right->value == Literal::makeFromInt32(1, type)) { // (signed)x % 1 ==> 0 if (binary->op == Abstract::getBinary(type, Abstract::RemS) && !EffectAnalyzer(getPassOptions(), features, binary->left) @@ -1392,7 +1391,8 @@ struct OptimizeInstructions } } // operations on all 1s - if (constRight == -1LL) { + if (right->value == Literal(int32_t(-1)) || + right->value == Literal(int64_t(-1))) { if (binary->op == Abstract::getBinary(type, Abstract::And)) { // x & -1 ==> x return binary->left; @@ -1447,7 +1447,7 @@ struct OptimizeInstructions // subtractions than the more common additions). if (binary->op == Abstract::getBinary(type, Abstract::Add) || binary->op == Abstract::getBinary(type, Abstract::Sub)) { - auto value = constRight; + auto value = right->value.getInteger(); if (value == 0x40 || value == 0x2000 || value == 0x100000 || value == 0x8000000 || value == 0x400000000LL || value == 0x20000000000LL || value == 0x1000000000000LL || diff --git a/test/passes/optimize-instructions_all-features.txt b/test/passes/optimize-instructions_all-features.txt index 8da31f1077a..120c9ac1991 100644 --- a/test/passes/optimize-instructions_all-features.txt +++ b/test/passes/optimize-instructions_all-features.txt @@ -3381,6 +3381,18 @@ ) ) (func $rhs-is-neg-one (param $x i32) (param $y i64) (param $fx f32) (param $fy f64) + (drop + (local.get $x) + ) + (drop + (local.get $y) + ) + (drop + (i64.and + (local.get $y) + (i64.const 4294967295) + ) + ) (drop (i32.sub (local.get $x) @@ -3422,6 +3434,12 @@ (drop (i32.const 1) ) + (drop + (i64.le_u + (local.get $y) + (i64.const 4294967295) + ) + ) (drop (i32.le_s (local.get $x) diff --git a/test/passes/optimize-instructions_all-features.wast b/test/passes/optimize-instructions_all-features.wast index a0fff2936f0..ff73828c58d 100644 --- a/test/passes/optimize-instructions_all-features.wast +++ b/test/passes/optimize-instructions_all-features.wast @@ -3842,6 +3842,18 @@ ) ) (func $rhs-is-neg-one (param $x i32) (param $y i64) (param $fx f32) (param $fy f64) + (drop (i32.and + (local.get $x) + (i32.const -1) + )) + (drop (i64.and + (local.get $y) + (i64.const -1) + )) + (drop (i64.and ;; skip + (local.get $y) + (i64.const 4294967295) + )) (drop (i32.sub (local.get $x) (i32.const -1) @@ -3881,6 +3893,10 @@ (local.get $y) (i64.const -1) )) + (drop (i64.le_u ;; skip + (local.get $y) + (i64.const 4294967295) + )) (drop (i32.le_s (local.get $x) (i32.const -1)