diff options
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 6 | ||||
-rw-r--r-- | test/passes/optimize-instructions.txt | 31 | ||||
-rw-r--r-- | test/passes/optimize-instructions.wast | 22 |
3 files changed, 44 insertions, 15 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index d1b419a06..c6006d712 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -517,6 +517,12 @@ struct OptimizeInstructions : public WalkerPass<PostWalker<OptimizeInstructions, } } } + // some operations have no effect TODO: many more + if (right->value == Literal(int32_t(0))) { + if (binary->op == ShlInt32 || binary->op == ShrUInt32 || binary->op == ShrSInt32) { + return binary->left; + } + } // the square of some operations can be merged if (auto* left = binary->left->dynCast<Binary>()) { if (left->op == binary->op) { diff --git a/test/passes/optimize-instructions.txt b/test/passes/optimize-instructions.txt index 49c3fca8b..8b42fd2ad 100644 --- a/test/passes/optimize-instructions.txt +++ b/test/passes/optimize-instructions.txt @@ -1965,31 +1965,32 @@ (func $zero-shifts-is-not-sign-ext (type $1) (drop (i32.eq - (i32.shr_s - (i32.shl - (i32.load16_s align=1 - (i32.const 790656516) - ) - (i32.const 0) - ) - (i32.const 0) + (i32.load16_s align=1 + (i32.const 790656516) ) (i32.const -5431187) ) ) (drop (i32.eq - (i32.shr_s - (i32.shl - (i32.load16_s align=1 - (i32.const 790656516) - ) - (i32.const 1) + (i32.shl + (i32.load16_s align=1 + (i32.const 790656516) ) - (i32.const 0) + (i32.const 1) ) (i32.const -5431187) ) ) ) + (func $zero-ops (type $2) (result i32) + (return + (i32.eq + (i32.load16_s align=1 + (i32.const 790656516) + ) + (i32.const -1337) + ) + ) + ) ) diff --git a/test/passes/optimize-instructions.wast b/test/passes/optimize-instructions.wast index 88b2bcdfe..6d1fd60ed 100644 --- a/test/passes/optimize-instructions.wast +++ b/test/passes/optimize-instructions.wast @@ -2395,4 +2395,26 @@ ) ) ) + (func $zero-ops (result i32) + (return + (i32.eq + (i32.const -1337) + (i32.shr_u + (i32.add + (i32.const 0) + (i32.shr_s + (i32.shl + (i32.load16_s align=1 + (i32.const 790656516) + ) + (i32.const 0) + ) + (i32.const 0) + ) + ) + (i32.const 0) + ) + ) + ) + ) ) |