From 2542ab4a0ceb642b1138b7710f933556498a118d Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Tue, 23 Jun 2020 14:00:39 +0300 Subject: [PATCH 1/2] add edge case tests --- .../optimize-instructions_all-features.txt | 18 ++++++++++++++++++ .../optimize-instructions_all-features.wast | 16 ++++++++++++++++ test/wasm2js/start_func.2asm.js | 1 + test/wasm2js/start_func.2asm.js.opt | 1 + 4 files changed, 36 insertions(+) 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) diff --git a/test/wasm2js/start_func.2asm.js b/test/wasm2js/start_func.2asm.js index 8d3e6f4ae8f..8e9654c86d2 100644 --- a/test/wasm2js/start_func.2asm.js +++ b/test/wasm2js/start_func.2asm.js @@ -58,4 +58,5 @@ function asmFunc(global, env, buffer) { } var memasmFunc = new ArrayBuffer(65536); +var bufferView = new Uint8Array(memasmFunc); var retasmFunc = asmFunc({Math,Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,NaN,Infinity}, {abort:function() { throw new Error('abort'); }},memasmFunc); diff --git a/test/wasm2js/start_func.2asm.js.opt b/test/wasm2js/start_func.2asm.js.opt index d2976352d44..97d94a03ffa 100644 --- a/test/wasm2js/start_func.2asm.js.opt +++ b/test/wasm2js/start_func.2asm.js.opt @@ -58,4 +58,5 @@ function asmFunc(global, env, buffer) { } var memasmFunc = new ArrayBuffer(65536); +var bufferView = new Uint8Array(memasmFunc); var retasmFunc = asmFunc({Math,Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,NaN,Infinity}, {abort:function() { throw new Error('abort'); }},memasmFunc); From e0b975c657378ca84aefe0e027261518ac9ed7c1 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Tue, 23 Jun 2020 17:06:43 +0300 Subject: [PATCH 2/2] use first approach for avoiding problems in binaryen.js --- src/passes/OptimizeInstructions.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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 ||