diff options
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 15 | ||||
-rw-r--r-- | test/emcc_O2_hello_world.fromasm | 60 | ||||
-rw-r--r-- | test/emcc_O2_hello_world.fromasm.imprecise | 60 | ||||
-rw-r--r-- | test/emcc_hello_world.fromasm | 100 | ||||
-rw-r--r-- | test/emcc_hello_world.fromasm.imprecise | 100 | ||||
-rw-r--r-- | test/memorygrowth.fromasm | 60 | ||||
-rw-r--r-- | test/memorygrowth.fromasm.imprecise | 60 | ||||
-rw-r--r-- | test/passes/optimize-instructions.txt | 20 | ||||
-rw-r--r-- | test/passes/optimize-instructions.wast | 22 |
9 files changed, 233 insertions, 264 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index 2315b9e8e..cd0610634 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -173,7 +173,6 @@ struct OptimizeInstructions : public WalkerPass<PostWalker<OptimizeInstructions, if (handOptimized) { curr = handOptimized; replaceCurrent(curr); - continue; } auto iter = database->patternMap.find(curr->_id); if (iter == database->patternMap.end()) return; @@ -214,10 +213,22 @@ struct OptimizeInstructions : public WalkerPass<PostWalker<OptimizeInstructions, auto* get = set->value->dynCast<GetGlobal>(); if (get && get->name == set->name) { ExpressionManipulator::nop(curr); - return curr; + } + } else if (auto* select = curr->dynCast<Select>()) { + // flip select if eqz input and flippable + auto* condition = select->condition->dynCast<Unary>(); + if (condition && condition->op == EqZInt32) { + EffectAnalyzer ifTrue(select->ifTrue); + EffectAnalyzer ifFalse(select->ifFalse); + if (!ifTrue.invalidates(ifFalse)) { + select->condition = condition->value; + std::swap(select->ifTrue, select->ifFalse); + } } } return nullptr; + // TODO: + // * eqz^2 can be removed if flowing into a boolean context (we handle if, but need also br_if and select) } }; diff --git a/test/emcc_O2_hello_world.fromasm b/test/emcc_O2_hello_world.fromasm index 9d8e9644d..f7fa57f61 100644 --- a/test/emcc_O2_hello_world.fromasm +++ b/test/emcc_O2_hello_world.fromasm @@ -3761,7 +3761,6 @@ (get_local $13) (tee_local $12 (select - (i32.const 0) (i32.and (i32.sub (i32.const 0) @@ -3774,11 +3773,10 @@ ) (i32.const 7) ) - (i32.eqz - (i32.and - (get_local $3) - (i32.const 7) - ) + (i32.const 0) + (i32.and + (get_local $3) + (i32.const 7) ) ) ) @@ -3933,7 +3931,6 @@ (i32.add (get_local $20) (select - (i32.const 0) (i32.and (i32.sub (i32.const 0) @@ -3946,11 +3943,10 @@ ) (i32.const 7) ) - (i32.eqz - (i32.and - (get_local $3) - (i32.const 7) - ) + (i32.const 0) + (i32.and + (get_local $3) + (i32.const 7) ) ) ) @@ -3959,7 +3955,6 @@ (i32.add (get_local $17) (select - (i32.const 0) (i32.and (i32.sub (i32.const 0) @@ -3972,11 +3967,10 @@ ) (i32.const 7) ) - (i32.eqz - (i32.and - (get_local $3) - (i32.const 7) - ) + (i32.const 0) + (i32.and + (get_local $3) + (i32.const 7) ) ) ) @@ -5137,7 +5131,6 @@ (i32.add (get_local $12) (select - (i32.const 0) (i32.and (i32.sub (i32.const 0) @@ -5145,11 +5138,10 @@ ) (i32.const 7) ) - (i32.eqz - (i32.and - (get_local $15) - (i32.const 7) - ) + (i32.const 0) + (i32.and + (get_local $15) + (i32.const 7) ) ) ) @@ -5175,7 +5167,6 @@ (get_local $20) (tee_local $17 (select - (i32.const 0) (i32.and (i32.sub (i32.const 0) @@ -5188,11 +5179,10 @@ ) (i32.const 7) ) - (i32.eqz - (i32.and - (get_local $6) - (i32.const 7) - ) + (i32.const 0) + (i32.and + (get_local $6) + (i32.const 7) ) ) ) @@ -5866,7 +5856,6 @@ (get_local $20) (tee_local $1 (select - (i32.const 0) (i32.and (i32.sub (i32.const 0) @@ -5879,11 +5868,10 @@ ) (i32.const 7) ) - (i32.eqz - (i32.and - (get_local $2) - (i32.const 7) - ) + (i32.const 0) + (i32.and + (get_local $2) + (i32.const 7) ) ) ) diff --git a/test/emcc_O2_hello_world.fromasm.imprecise b/test/emcc_O2_hello_world.fromasm.imprecise index be1ef1660..925aa4813 100644 --- a/test/emcc_O2_hello_world.fromasm.imprecise +++ b/test/emcc_O2_hello_world.fromasm.imprecise @@ -3759,7 +3759,6 @@ (get_local $13) (tee_local $12 (select - (i32.const 0) (i32.and (i32.sub (i32.const 0) @@ -3772,11 +3771,10 @@ ) (i32.const 7) ) - (i32.eqz - (i32.and - (get_local $3) - (i32.const 7) - ) + (i32.const 0) + (i32.and + (get_local $3) + (i32.const 7) ) ) ) @@ -3931,7 +3929,6 @@ (i32.add (get_local $20) (select - (i32.const 0) (i32.and (i32.sub (i32.const 0) @@ -3944,11 +3941,10 @@ ) (i32.const 7) ) - (i32.eqz - (i32.and - (get_local $3) - (i32.const 7) - ) + (i32.const 0) + (i32.and + (get_local $3) + (i32.const 7) ) ) ) @@ -3957,7 +3953,6 @@ (i32.add (get_local $17) (select - (i32.const 0) (i32.and (i32.sub (i32.const 0) @@ -3970,11 +3965,10 @@ ) (i32.const 7) ) - (i32.eqz - (i32.and - (get_local $3) - (i32.const 7) - ) + (i32.const 0) + (i32.and + (get_local $3) + (i32.const 7) ) ) ) @@ -5135,7 +5129,6 @@ (i32.add (get_local $12) (select - (i32.const 0) (i32.and (i32.sub (i32.const 0) @@ -5143,11 +5136,10 @@ ) (i32.const 7) ) - (i32.eqz - (i32.and - (get_local $15) - (i32.const 7) - ) + (i32.const 0) + (i32.and + (get_local $15) + (i32.const 7) ) ) ) @@ -5173,7 +5165,6 @@ (get_local $20) (tee_local $17 (select - (i32.const 0) (i32.and (i32.sub (i32.const 0) @@ -5186,11 +5177,10 @@ ) (i32.const 7) ) - (i32.eqz - (i32.and - (get_local $6) - (i32.const 7) - ) + (i32.const 0) + (i32.and + (get_local $6) + (i32.const 7) ) ) ) @@ -5864,7 +5854,6 @@ (get_local $20) (tee_local $1 (select - (i32.const 0) (i32.and (i32.sub (i32.const 0) @@ -5877,11 +5866,10 @@ ) (i32.const 7) ) - (i32.eqz - (i32.and - (get_local $2) - (i32.const 7) - ) + (i32.const 0) + (i32.and + (get_local $2) + (i32.const 7) ) ) ) diff --git a/test/emcc_hello_world.fromasm b/test/emcc_hello_world.fromasm index 94eb84af9..bb4696f9f 100644 --- a/test/emcc_hello_world.fromasm +++ b/test/emcc_hello_world.fromasm @@ -1424,6 +1424,7 @@ ) (set_local $2 (select + (i32.const -1) (if (i32.load (tee_local $10 @@ -1519,12 +1520,10 @@ ) (set_local $1 (select - (i32.const -1) (get_local $1) - (i32.eqz - (i32.load - (get_local $11) - ) + (i32.const -1) + (i32.load + (get_local $11) ) ) ) @@ -1554,16 +1553,13 @@ ) ) ) - (i32.const -1) - (i32.eqz - (i32.and - (tee_local $1 - (i32.load - (get_local $0) - ) + (i32.and + (tee_local $1 + (i32.load + (get_local $0) ) - (i32.const 32) ) + (i32.const 32) ) ) ) @@ -4029,18 +4025,16 @@ ) (set_local $18 (select - (get_local $8) (tee_local $7 (i32.and (get_local $8) (i32.const -65537) ) ) - (i32.eqz - (i32.and - (get_local $8) - (i32.const 8192) - ) + (get_local $8) + (i32.and + (get_local $8) + (i32.const 8192) ) ) ) @@ -4504,14 +4498,12 @@ (block (set_local $1 (select - (i32.const 4091) (i32.const 4093) - (i32.eqz - (tee_local $6 - (i32.and - (get_local $18) - (i32.const 1) - ) + (i32.const 4091) + (tee_local $6 + (i32.and + (get_local $18) + (i32.const 1) ) ) ) @@ -4720,14 +4712,12 @@ (block (set_local $39 (select - (i32.const 4109) (i32.const 4114) - (i32.eqz - (tee_local $1 - (i32.and - (get_local $18) - (i32.const 1) - ) + (i32.const 4109) + (tee_local $1 + (i32.and + (get_local $18) + (i32.const 1) ) ) ) @@ -4806,17 +4796,15 @@ (block (set_local $9 (select - (get_local $39) (i32.add (get_local $39) (i32.const 9) ) - (i32.eqz - (tee_local $6 - (i32.and - (get_local $26) - (i32.const 32) - ) + (get_local $39) + (tee_local $6 + (i32.and + (get_local $26) + (i32.const 32) ) ) ) @@ -5635,15 +5623,13 @@ ) (set_local $5 (select + (get_local $7) (i32.add (get_local $7) (i32.const 4) ) - (get_local $7) - (i32.eqz - (i32.load - (get_local $7) - ) + (i32.load + (get_local $7) ) ) ) @@ -5666,15 +5652,13 @@ (get_local $5) ) (select + (get_local $7) (i32.add (get_local $7) (i32.const 4) ) - (get_local $7) - (i32.eqz - (i32.load - (get_local $7) - ) + (i32.load + (get_local $7) ) ) ) @@ -5990,19 +5974,17 @@ (block (set_local $14 (select - (f64.const 9007199254740992) (f64.const 9007199254740994) - (i32.eqz + (f64.const 9007199254740992) + (i32.and (i32.and - (i32.and - (call_import $i32u-div - (get_local $5) - (get_local $17) - ) - (i32.const -1) + (call_import $i32u-div + (get_local $5) + (get_local $17) ) - (i32.const 1) + (i32.const -1) ) + (i32.const 1) ) ) ) diff --git a/test/emcc_hello_world.fromasm.imprecise b/test/emcc_hello_world.fromasm.imprecise index 79b6acbc8..0e395cf49 100644 --- a/test/emcc_hello_world.fromasm.imprecise +++ b/test/emcc_hello_world.fromasm.imprecise @@ -1417,6 +1417,7 @@ ) (set_local $2 (select + (i32.const -1) (if (i32.load (tee_local $10 @@ -1512,12 +1513,10 @@ ) (set_local $1 (select - (i32.const -1) (get_local $1) - (i32.eqz - (i32.load - (get_local $11) - ) + (i32.const -1) + (i32.load + (get_local $11) ) ) ) @@ -1547,16 +1546,13 @@ ) ) ) - (i32.const -1) - (i32.eqz - (i32.and - (tee_local $1 - (i32.load - (get_local $0) - ) + (i32.and + (tee_local $1 + (i32.load + (get_local $0) ) - (i32.const 32) ) + (i32.const 32) ) ) ) @@ -4022,18 +4018,16 @@ ) (set_local $18 (select - (get_local $8) (tee_local $7 (i32.and (get_local $8) (i32.const -65537) ) ) - (i32.eqz - (i32.and - (get_local $8) - (i32.const 8192) - ) + (get_local $8) + (i32.and + (get_local $8) + (i32.const 8192) ) ) ) @@ -4497,14 +4491,12 @@ (block (set_local $1 (select - (i32.const 4091) (i32.const 4093) - (i32.eqz - (tee_local $6 - (i32.and - (get_local $18) - (i32.const 1) - ) + (i32.const 4091) + (tee_local $6 + (i32.and + (get_local $18) + (i32.const 1) ) ) ) @@ -4713,14 +4705,12 @@ (block (set_local $39 (select - (i32.const 4109) (i32.const 4114) - (i32.eqz - (tee_local $1 - (i32.and - (get_local $18) - (i32.const 1) - ) + (i32.const 4109) + (tee_local $1 + (i32.and + (get_local $18) + (i32.const 1) ) ) ) @@ -4799,17 +4789,15 @@ (block (set_local $9 (select - (get_local $39) (i32.add (get_local $39) (i32.const 9) ) - (i32.eqz - (tee_local $6 - (i32.and - (get_local $26) - (i32.const 32) - ) + (get_local $39) + (tee_local $6 + (i32.and + (get_local $26) + (i32.const 32) ) ) ) @@ -5628,15 +5616,13 @@ ) (set_local $5 (select + (get_local $7) (i32.add (get_local $7) (i32.const 4) ) - (get_local $7) - (i32.eqz - (i32.load - (get_local $7) - ) + (i32.load + (get_local $7) ) ) ) @@ -5659,15 +5645,13 @@ (get_local $5) ) (select + (get_local $7) (i32.add (get_local $7) (i32.const 4) ) - (get_local $7) - (i32.eqz - (i32.load - (get_local $7) - ) + (i32.load + (get_local $7) ) ) ) @@ -5983,19 +5967,17 @@ (block (set_local $14 (select - (f64.const 9007199254740992) (f64.const 9007199254740994) - (i32.eqz + (f64.const 9007199254740992) + (i32.and (i32.and - (i32.and - (i32.div_u - (get_local $5) - (get_local $17) - ) - (i32.const -1) + (i32.div_u + (get_local $5) + (get_local $17) ) - (i32.const 1) + (i32.const -1) ) + (i32.const 1) ) ) ) diff --git a/test/memorygrowth.fromasm b/test/memorygrowth.fromasm index 34a7d1898..d0d1f9214 100644 --- a/test/memorygrowth.fromasm +++ b/test/memorygrowth.fromasm @@ -3810,7 +3810,6 @@ (get_local $10) (tee_local $21 (select - (i32.const 0) (i32.and (i32.sub (i32.const 0) @@ -3823,11 +3822,10 @@ ) (i32.const 7) ) - (i32.eqz - (i32.and - (get_local $1) - (i32.const 7) - ) + (i32.const 0) + (i32.and + (get_local $1) + (i32.const 7) ) ) ) @@ -3984,7 +3982,6 @@ (i32.add (get_local $28) (select - (i32.const 0) (i32.and (i32.sub (i32.const 0) @@ -3997,11 +3994,10 @@ ) (i32.const 7) ) - (i32.eqz - (i32.and - (get_local $1) - (i32.const 7) - ) + (i32.const 0) + (i32.and + (get_local $1) + (i32.const 7) ) ) ) @@ -4010,7 +4006,6 @@ (i32.add (get_local $15) (select - (i32.const 0) (i32.and (i32.sub (i32.const 0) @@ -4023,11 +4018,10 @@ ) (i32.const 7) ) - (i32.eqz - (i32.and - (get_local $1) - (i32.const 7) - ) + (i32.const 0) + (i32.and + (get_local $1) + (i32.const 7) ) ) ) @@ -5201,7 +5195,6 @@ (i32.add (get_local $21) (select - (i32.const 0) (i32.and (i32.sub (i32.const 0) @@ -5209,11 +5202,10 @@ ) (i32.const 7) ) - (i32.eqz - (i32.and - (get_local $24) - (i32.const 7) - ) + (i32.const 0) + (i32.and + (get_local $24) + (i32.const 7) ) ) ) @@ -5239,7 +5231,6 @@ (get_local $28) (tee_local $15 (select - (i32.const 0) (i32.and (i32.sub (i32.const 0) @@ -5252,11 +5243,10 @@ ) (i32.const 7) ) - (i32.eqz - (i32.and - (get_local $4) - (i32.const 7) - ) + (i32.const 0) + (i32.and + (get_local $4) + (i32.const 7) ) ) ) @@ -5930,7 +5920,6 @@ (get_local $28) (tee_local $15 (select - (i32.const 0) (i32.and (i32.sub (i32.const 0) @@ -5943,11 +5932,10 @@ ) (i32.const 7) ) - (i32.eqz - (i32.and - (get_local $4) - (i32.const 7) - ) + (i32.const 0) + (i32.and + (get_local $4) + (i32.const 7) ) ) ) diff --git a/test/memorygrowth.fromasm.imprecise b/test/memorygrowth.fromasm.imprecise index 5e6d64c47..ce121503e 100644 --- a/test/memorygrowth.fromasm.imprecise +++ b/test/memorygrowth.fromasm.imprecise @@ -3808,7 +3808,6 @@ (get_local $10) (tee_local $21 (select - (i32.const 0) (i32.and (i32.sub (i32.const 0) @@ -3821,11 +3820,10 @@ ) (i32.const 7) ) - (i32.eqz - (i32.and - (get_local $1) - (i32.const 7) - ) + (i32.const 0) + (i32.and + (get_local $1) + (i32.const 7) ) ) ) @@ -3982,7 +3980,6 @@ (i32.add (get_local $28) (select - (i32.const 0) (i32.and (i32.sub (i32.const 0) @@ -3995,11 +3992,10 @@ ) (i32.const 7) ) - (i32.eqz - (i32.and - (get_local $1) - (i32.const 7) - ) + (i32.const 0) + (i32.and + (get_local $1) + (i32.const 7) ) ) ) @@ -4008,7 +4004,6 @@ (i32.add (get_local $15) (select - (i32.const 0) (i32.and (i32.sub (i32.const 0) @@ -4021,11 +4016,10 @@ ) (i32.const 7) ) - (i32.eqz - (i32.and - (get_local $1) - (i32.const 7) - ) + (i32.const 0) + (i32.and + (get_local $1) + (i32.const 7) ) ) ) @@ -5199,7 +5193,6 @@ (i32.add (get_local $21) (select - (i32.const 0) (i32.and (i32.sub (i32.const 0) @@ -5207,11 +5200,10 @@ ) (i32.const 7) ) - (i32.eqz - (i32.and - (get_local $24) - (i32.const 7) - ) + (i32.const 0) + (i32.and + (get_local $24) + (i32.const 7) ) ) ) @@ -5237,7 +5229,6 @@ (get_local $28) (tee_local $15 (select - (i32.const 0) (i32.and (i32.sub (i32.const 0) @@ -5250,11 +5241,10 @@ ) (i32.const 7) ) - (i32.eqz - (i32.and - (get_local $4) - (i32.const 7) - ) + (i32.const 0) + (i32.and + (get_local $4) + (i32.const 7) ) ) ) @@ -5928,7 +5918,6 @@ (get_local $28) (tee_local $15 (select - (i32.const 0) (i32.and (i32.sub (i32.const 0) @@ -5941,11 +5930,10 @@ ) (i32.const 7) ) - (i32.eqz - (i32.and - (get_local $4) - (i32.const 7) - ) + (i32.const 0) + (i32.and + (get_local $4) + (i32.const 7) ) ) ) diff --git a/test/passes/optimize-instructions.txt b/test/passes/optimize-instructions.txt index 850c94c28..6d83cbdbc 100644 --- a/test/passes/optimize-instructions.txt +++ b/test/passes/optimize-instructions.txt @@ -185,5 +185,25 @@ (i32.const 123) (nop) ) + (drop + (select + (i32.const 102) + (i32.const 101) + (get_local $i1) + ) + ) + (drop + (select + (tee_local $i1 + (i32.const 103) + ) + (tee_local $i1 + (i32.const 104) + ) + (i32.eqz + (get_local $i1) + ) + ) + ) ) ) diff --git a/test/passes/optimize-instructions.wast b/test/passes/optimize-instructions.wast index 8265a4f70..08868e23a 100644 --- a/test/passes/optimize-instructions.wast +++ b/test/passes/optimize-instructions.wast @@ -218,5 +218,27 @@ ) (nop) ) + (drop + (select + (i32.const 101) + (i32.const 102) + (i32.eqz + (get_local $i1) + ) + ) + ) + (drop + (select + (tee_local $i1 + (i32.const 103) + ) ;; these conflict + (tee_local $i1 + (i32.const 104) + ) + (i32.eqz + (get_local $i1) + ) + ) + ) ) ) |