diff options
author | Alon Zakai <alonzakai@gmail.com> | 2016-10-14 18:18:10 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2016-10-15 10:28:15 -0700 |
commit | 6deb76d51d58ec910855c38eaf4f2cdd5619076d (patch) | |
tree | 3684184200f5f596c0b9b1592fa8a44320f2a15a | |
parent | f38142569b0fc29f3831ada7b23ac928b58c5b42 (diff) | |
download | binaryen-6deb76d51d58ec910855c38eaf4f2cdd5619076d.tar.gz binaryen-6deb76d51d58ec910855c38eaf4f2cdd5619076d.tar.bz2 binaryen-6deb76d51d58ec910855c38eaf4f2cdd5619076d.zip |
optimize out i32.and of -1
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 13 | ||||
-rw-r--r-- | test/emcc_hello_world.fromasm | 110 | ||||
-rw-r--r-- | test/emcc_hello_world.fromasm.imprecise | 110 | ||||
-rw-r--r-- | test/passes/optimize-instructions.txt | 11 | ||||
-rw-r--r-- | test/passes/optimize-instructions.wast | 4 | ||||
-rw-r--r-- | test/unit.fromasm | 7 |
6 files changed, 106 insertions, 149 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index 7547f332a..0b54f9756 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -224,10 +224,15 @@ struct OptimizeInstructions : public WalkerPass<PostWalker<OptimizeInstructions, } } } else if (binary->op == AndInt32) { - if (auto* load = binary->left->dynCast<Load>()) { - if (auto* right = binary->right->dynCast<Const>()) { - if (right->type == i32) { - auto mask = right->value.geti32(); + if (auto* right = binary->right->dynCast<Const>()) { + if (right->type == i32) { + auto mask = right->value.geti32(); + // and with -1 does nothing (common in asm.js output) + if (mask == -1) { + return binary->left; + } + // small loads do not need to be masted, the load itself masks + if (auto* load = binary->left->dynCast<Load>()) { if ((load->bytes == 1 && mask == 0xff) || (load->bytes == 2 && mask == 0xffff)) { load->signed_ = false; diff --git a/test/emcc_hello_world.fromasm b/test/emcc_hello_world.fromasm index e5f68f021..c6815b60d 100644 --- a/test/emcc_hello_world.fromasm +++ b/test/emcc_hello_world.fromasm @@ -4930,15 +4930,12 @@ (block (set_local $12 (i32.add - (i32.and - (call $i32s-div - (i32.add - (get_local $19) - (i32.const 25) - ) - (i32.const 9) + (call $i32s-div + (i32.add + (get_local $19) + (i32.const 25) ) - (i32.const -1) + (i32.const 9) ) (i32.const 1) ) @@ -5246,17 +5243,14 @@ ) (i32.shl (i32.add - (i32.and - (call $i32s-div - (tee_local $11 - (i32.add - (get_local $7) - (i32.const 9216) - ) + (call $i32s-div + (tee_local $11 + (i32.add + (get_local $7) + (i32.const 9216) ) - (i32.const 9) ) - (i32.const -1) + (i32.const 9) ) (i32.const -1024) ) @@ -5268,12 +5262,9 @@ (i32.lt_s (tee_local $11 (i32.add - (i32.and - (call $i32s-rem - (get_local $11) - (i32.const 9) - ) - (i32.const -1) + (call $i32s-rem + (get_local $11) + (i32.const 9) ) (i32.const 1) ) @@ -5323,16 +5314,13 @@ ) (i32.eqz (tee_local $31 - (i32.and - (call $i32u-rem - (tee_local $11 - (i32.load - (get_local $7) - ) + (call $i32u-rem + (tee_local $11 + (i32.load + (get_local $7) ) - (get_local $12) ) - (i32.const -1) + (get_local $12) ) ) ) @@ -5344,12 +5332,9 @@ (f64.const 9007199254740994) (f64.const 9007199254740992) (i32.and - (i32.and - (call $i32u-div - (get_local $11) - (get_local $12) - ) - (i32.const -1) + (call $i32u-div + (get_local $11) + (get_local $12) ) (i32.const 1) ) @@ -5360,12 +5345,9 @@ (i32.lt_u (get_local $31) (tee_local $25 - (i32.and - (call $i32s-div - (get_local $12) - (i32.const 2) - ) - (i32.const -1) + (call $i32s-div + (get_local $12) + (i32.const 2) ) ) ) @@ -5717,12 +5699,9 @@ ) ) (if - (i32.and - (call $i32u-rem - (get_local $19) - (i32.const 10) - ) - (i32.const -1) + (call $i32u-rem + (get_local $19) + (i32.const 10) ) (block (set_local $5 @@ -5748,17 +5727,14 @@ ) (br_if $while-in96 (i32.eqz - (i32.and - (call $i32u-rem - (get_local $19) - (tee_local $7 - (i32.mul - (get_local $7) - (i32.const 10) - ) + (call $i32u-rem + (get_local $19) + (tee_local $7 + (i32.mul + (get_local $7) + (i32.const 10) ) ) - (i32.const -1) ) ) ) @@ -7883,23 +7859,17 @@ ) ) (i32.or - (i32.and - (call $i32u-rem - (get_local $0) - (i32.const 10) - ) - (i32.const -1) + (call $i32u-rem + (get_local $0) + (i32.const 10) ) (i32.const 48) ) ) (set_local $2 - (i32.and - (call $i32u-div - (get_local $0) - (i32.const 10) - ) - (i32.const -1) + (call $i32u-div + (get_local $0) + (i32.const 10) ) ) (if diff --git a/test/emcc_hello_world.fromasm.imprecise b/test/emcc_hello_world.fromasm.imprecise index 589114f82..a16e45353 100644 --- a/test/emcc_hello_world.fromasm.imprecise +++ b/test/emcc_hello_world.fromasm.imprecise @@ -4923,15 +4923,12 @@ (block (set_local $12 (i32.add - (i32.and - (i32.div_s - (i32.add - (get_local $19) - (i32.const 25) - ) - (i32.const 9) + (i32.div_s + (i32.add + (get_local $19) + (i32.const 25) ) - (i32.const -1) + (i32.const 9) ) (i32.const 1) ) @@ -5239,17 +5236,14 @@ ) (i32.shl (i32.add - (i32.and - (i32.div_s - (tee_local $11 - (i32.add - (get_local $7) - (i32.const 9216) - ) + (i32.div_s + (tee_local $11 + (i32.add + (get_local $7) + (i32.const 9216) ) - (i32.const 9) ) - (i32.const -1) + (i32.const 9) ) (i32.const -1024) ) @@ -5261,12 +5255,9 @@ (i32.lt_s (tee_local $11 (i32.add - (i32.and - (i32.rem_s - (get_local $11) - (i32.const 9) - ) - (i32.const -1) + (i32.rem_s + (get_local $11) + (i32.const 9) ) (i32.const 1) ) @@ -5316,16 +5307,13 @@ ) (i32.eqz (tee_local $31 - (i32.and - (i32.rem_u - (tee_local $11 - (i32.load - (get_local $7) - ) + (i32.rem_u + (tee_local $11 + (i32.load + (get_local $7) ) - (get_local $12) ) - (i32.const -1) + (get_local $12) ) ) ) @@ -5337,12 +5325,9 @@ (f64.const 9007199254740994) (f64.const 9007199254740992) (i32.and - (i32.and - (i32.div_u - (get_local $11) - (get_local $12) - ) - (i32.const -1) + (i32.div_u + (get_local $11) + (get_local $12) ) (i32.const 1) ) @@ -5353,12 +5338,9 @@ (i32.lt_u (get_local $31) (tee_local $25 - (i32.and - (i32.div_s - (get_local $12) - (i32.const 2) - ) - (i32.const -1) + (i32.div_s + (get_local $12) + (i32.const 2) ) ) ) @@ -5710,12 +5692,9 @@ ) ) (if - (i32.and - (i32.rem_u - (get_local $19) - (i32.const 10) - ) - (i32.const -1) + (i32.rem_u + (get_local $19) + (i32.const 10) ) (block (set_local $5 @@ -5741,17 +5720,14 @@ ) (br_if $while-in96 (i32.eqz - (i32.and - (i32.rem_u - (get_local $19) - (tee_local $7 - (i32.mul - (get_local $7) - (i32.const 10) - ) + (i32.rem_u + (get_local $19) + (tee_local $7 + (i32.mul + (get_local $7) + (i32.const 10) ) ) - (i32.const -1) ) ) ) @@ -7876,23 +7852,17 @@ ) ) (i32.or - (i32.and - (i32.rem_u - (get_local $0) - (i32.const 10) - ) - (i32.const -1) + (i32.rem_u + (get_local $0) + (i32.const 10) ) (i32.const 48) ) ) (set_local $2 - (i32.and - (i32.div_u - (get_local $0) - (i32.const 10) - ) - (i32.const -1) + (i32.div_u + (get_local $0) + (i32.const 10) ) ) (if diff --git a/test/passes/optimize-instructions.txt b/test/passes/optimize-instructions.txt index 07cc7e895..73076b0c5 100644 --- a/test/passes/optimize-instructions.txt +++ b/test/passes/optimize-instructions.txt @@ -290,4 +290,15 @@ ) ) ) + (func $and-neg1 (type $1) + (drop + (i32.const 100) + ) + (drop + (i32.and + (i32.const 100) + (i32.const 1) + ) + ) + ) ) diff --git a/test/passes/optimize-instructions.wast b/test/passes/optimize-instructions.wast index 939b07ffc..9701941c7 100644 --- a/test/passes/optimize-instructions.wast +++ b/test/passes/optimize-instructions.wast @@ -267,4 +267,8 @@ (i32.store16 (i32.const 10) (i32.and (i32.const -3) (i32.const 65535))) (i32.store16 (i32.const 11) (i32.and (i32.const -4) (i32.const 65534))) ) + (func $and-neg1 + (drop (i32.and (i32.const 100) (i32.const -1))) + (drop (i32.and (i32.const 100) (i32.const 1))) + ) ) diff --git a/test/unit.fromasm b/test/unit.fromasm index 28bd8f223..905d12b86 100644 --- a/test/unit.fromasm +++ b/test/unit.fromasm @@ -254,12 +254,9 @@ ) ) (func $big_uint_div_u (result i32) - (i32.and - (call $i32u-div - (i32.const -1) - (i32.const 2) - ) + (call $i32u-div (i32.const -1) + (i32.const 2) ) ) (func $fr (param $0 f32) |