diff options
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 24 | ||||
-rw-r--r-- | test/emcc_O2_hello_world.fromasm | 28 | ||||
-rw-r--r-- | test/emcc_O2_hello_world.fromasm.imprecise | 28 | ||||
-rw-r--r-- | test/emcc_hello_world.fromasm | 10 | ||||
-rw-r--r-- | test/emcc_hello_world.fromasm.imprecise | 10 | ||||
-rw-r--r-- | test/memorygrowth.fromasm | 9 | ||||
-rw-r--r-- | test/memorygrowth.fromasm.imprecise | 9 | ||||
-rw-r--r-- | test/passes/optimize-instructions.txt | 12 | ||||
-rw-r--r-- | test/passes/optimize-instructions.wast | 9 |
9 files changed, 64 insertions, 75 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index 84f126ae9..6454d3510 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -352,14 +352,24 @@ struct OptimizeInstructions : public WalkerPass<PostWalker<OptimizeInstructions, } private: - + // Optimize given that the expression is flowing into a boolean context Expression* optimizeBoolean(Expression* boolean) { - auto* condition = boolean->dynCast<Unary>(); - if (condition && condition->op == EqZInt32) { - auto* condition2 = condition->value->dynCast<Unary>(); - if (condition2 && condition2->op == EqZInt32) { - // double eqz - return condition2->value; + if (auto* unary = boolean->dynCast<Unary>()) { + if (unary && unary->op == EqZInt32) { + auto* unary2 = unary->value->dynCast<Unary>(); + if (unary2 && unary2->op == EqZInt32) { + // double eqz + return unary2->value; + } + } + } else if (auto* binary = boolean->dynCast<Binary>()) { + // x != 0 is just x if it's used as a bool + if (binary->op == NeInt32) { + if (auto* num = binary->right->dynCast<Const>()) { + if (num->value.geti32() == 0) { + return binary->left; + } + } } } return boolean; diff --git a/test/emcc_O2_hello_world.fromasm b/test/emcc_O2_hello_world.fromasm index 8b7d2cffd..78428e1ce 100644 --- a/test/emcc_O2_hello_world.fromasm +++ b/test/emcc_O2_hello_world.fromasm @@ -1973,10 +1973,7 @@ ) ) (i32.const 0) - (i32.ne - (get_local $12) - (i32.const 0) - ) + (get_local $12) ) (block (if @@ -3123,13 +3120,10 @@ ) (if (if i32 - (i32.ne - (tee_local $10 - (i32.load - (i32.const 616) - ) + (tee_local $10 + (i32.load + (i32.const 616) ) - (i32.const 0) ) (i32.or (i32.le_u @@ -3399,10 +3393,7 @@ ) ) (i32.const 0) - (i32.ne - (get_local $5) - (i32.const 0) - ) + (get_local $5) ) ) (if @@ -3695,13 +3686,10 @@ ) ) (br_if $do-in - (i32.ne - (tee_local $3 - (i32.load offset=8 - (get_local $3) - ) + (tee_local $3 + (i32.load offset=8 + (get_local $3) ) - (i32.const 0) ) ) ) diff --git a/test/emcc_O2_hello_world.fromasm.imprecise b/test/emcc_O2_hello_world.fromasm.imprecise index 8fc101afc..0cd471b28 100644 --- a/test/emcc_O2_hello_world.fromasm.imprecise +++ b/test/emcc_O2_hello_world.fromasm.imprecise @@ -1971,10 +1971,7 @@ ) ) (i32.const 0) - (i32.ne - (get_local $12) - (i32.const 0) - ) + (get_local $12) ) (block (if @@ -3121,13 +3118,10 @@ ) (if (if i32 - (i32.ne - (tee_local $10 - (i32.load - (i32.const 616) - ) + (tee_local $10 + (i32.load + (i32.const 616) ) - (i32.const 0) ) (i32.or (i32.le_u @@ -3397,10 +3391,7 @@ ) ) (i32.const 0) - (i32.ne - (get_local $5) - (i32.const 0) - ) + (get_local $5) ) ) (if @@ -3693,13 +3684,10 @@ ) ) (br_if $do-in - (i32.ne - (tee_local $3 - (i32.load offset=8 - (get_local $3) - ) + (tee_local $3 + (i32.load offset=8 + (get_local $3) ) - (i32.const 0) ) ) ) diff --git a/test/emcc_hello_world.fromasm b/test/emcc_hello_world.fromasm index cb6e15b30..7443cdaff 100644 --- a/test/emcc_hello_world.fromasm +++ b/test/emcc_hello_world.fromasm @@ -2146,10 +2146,7 @@ (select (get_local $2) (i32.const 0) - (i32.ne - (get_local $0) - (i32.const 0) - ) + (get_local $0) ) ) (func $___syscall_ret (param $0 i32) (result i32) @@ -4060,10 +4057,7 @@ ) ) (i32.const 4101) - (i32.ne - (get_local $9) - (i32.const 0) - ) + (get_local $9) ) ) (br $jumpthreading$inner$4) diff --git a/test/emcc_hello_world.fromasm.imprecise b/test/emcc_hello_world.fromasm.imprecise index d34035bf2..b3ce6e84e 100644 --- a/test/emcc_hello_world.fromasm.imprecise +++ b/test/emcc_hello_world.fromasm.imprecise @@ -2139,10 +2139,7 @@ (select (get_local $2) (i32.const 0) - (i32.ne - (get_local $0) - (i32.const 0) - ) + (get_local $0) ) ) (func $___syscall_ret (param $0 i32) (result i32) @@ -4052,10 +4049,7 @@ ) ) (i32.const 4101) - (i32.ne - (get_local $9) - (i32.const 0) - ) + (get_local $9) ) ) (br $jumpthreading$inner$4) diff --git a/test/memorygrowth.fromasm b/test/memorygrowth.fromasm index dd14ea0bf..d2c814e8d 100644 --- a/test/memorygrowth.fromasm +++ b/test/memorygrowth.fromasm @@ -3728,13 +3728,10 @@ ) ) (br_if $do-in41 - (i32.ne - (tee_local $2 - (i32.load offset=8 - (get_local $2) - ) + (tee_local $2 + (i32.load offset=8 + (get_local $2) ) - (i32.const 0) ) ) ) diff --git a/test/memorygrowth.fromasm.imprecise b/test/memorygrowth.fromasm.imprecise index a6af9df0e..f9fa51bce 100644 --- a/test/memorygrowth.fromasm.imprecise +++ b/test/memorygrowth.fromasm.imprecise @@ -3726,13 +3726,10 @@ ) ) (br_if $do-in41 - (i32.ne - (tee_local $2 - (i32.load offset=8 - (get_local $2) - ) + (tee_local $2 + (i32.load offset=8 + (get_local $2) ) - (i32.const 0) ) ) ) diff --git a/test/passes/optimize-instructions.txt b/test/passes/optimize-instructions.txt index c93c7d1fa..1bd410822 100644 --- a/test/passes/optimize-instructions.txt +++ b/test/passes/optimize-instructions.txt @@ -1,6 +1,7 @@ (module (type $0 (func (param i32 i64))) (type $1 (func)) + (type $2 (func (result i32))) (memory $0 0) (func $f (type $0) (param $i1 i32) (param $i2 i64) (if @@ -363,4 +364,15 @@ ) ) ) + (func $ne0 (type $2) (result i32) + (if + (call $ne0) + (nop) + ) + (if + (call $ne0) + (nop) + ) + (i32.const 1) + ) ) diff --git a/test/passes/optimize-instructions.wast b/test/passes/optimize-instructions.wast index 99e77ea9e..56e9d08fb 100644 --- a/test/passes/optimize-instructions.wast +++ b/test/passes/optimize-instructions.wast @@ -287,4 +287,13 @@ (drop (i32.div_s (unreachable) (i32.const 1))) ;; not ok (drop (i32.div_s (i32.const 1) (unreachable))) ) + (func $ne0 (result i32) + (if (i32.ne (call $ne0) (i32.const 0)) + (nop) + ) + (if (i32.ne (i32.const 0) (call $ne0)) + (nop) + ) + (i32.const 1) + ) ) |