diff options
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 18 | ||||
-rw-r--r-- | src/support/bits.cpp | 2 | ||||
-rw-r--r-- | test/passes/optimize-instructions_all-features.txt | 11 | ||||
-rw-r--r-- | test/passes/optimize-instructions_all-features.wast | 15 |
4 files changed, 36 insertions, 10 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index c7219dd3c..d019a983f 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -1113,8 +1113,8 @@ private: struct SeekState { Expression* curr; - int64_t mul; - SeekState(Expression* curr, int64_t mul) : curr(curr), mul(mul) {} + uint64_t mul; + SeekState(Expression* curr, uint64_t mul) : curr(curr), mul(mul) {} }; std::vector<SeekState> seekStack; seekStack.emplace_back(binary, 1); @@ -1124,8 +1124,8 @@ private: auto curr = state.curr; auto mul = state.mul; if (auto* c = curr->dynCast<Const>()) { - int64_t value = c->value.getInteger(); - if (value != 0LL) { + uint64_t value = c->value.getInteger(); + if (value != 0ULL) { constant += value * mul; constants.push_back(c); } @@ -1147,17 +1147,19 @@ private: } else if (binary->op == Abstract::getBinary(binary->type, Abstract::Shl)) { if (auto* c = binary->right->dynCast<Const>()) { - seekStack.emplace_back( - binary->left, mul * Bits::pow2(Bits::getEffectiveShifts(c))); + seekStack.emplace_back(binary->left, + mul << Bits::getEffectiveShifts(c)); continue; } } else if (binary->op == Abstract::getBinary(binary->type, Abstract::Mul)) { if (auto* c = binary->left->dynCast<Const>()) { - seekStack.emplace_back(binary->right, mul * c->value.getInteger()); + seekStack.emplace_back(binary->right, + mul * (uint64_t)c->value.getInteger()); continue; } else if (auto* c = binary->right->dynCast<Const>()) { - seekStack.emplace_back(binary->left, mul * c->value.getInteger()); + seekStack.emplace_back(binary->left, + mul * (uint64_t)c->value.getInteger()); continue; } } diff --git a/src/support/bits.cpp b/src/support/bits.cpp index 46deddc97..749e6fb54 100644 --- a/src/support/bits.cpp +++ b/src/support/bits.cpp @@ -196,7 +196,7 @@ uint32_t log2(uint32_t v) { return 31 - countLeadingZeroes(v); } -uint32_t pow2(uint32_t v) { return 1 << (v & 31); } +uint32_t pow2(uint32_t v) { return v < 32 ? 1 << v : 0; } } // namespace Bits diff --git a/test/passes/optimize-instructions_all-features.txt b/test/passes/optimize-instructions_all-features.txt index 3e792cd9a..15f0eea86 100644 --- a/test/passes/optimize-instructions_all-features.txt +++ b/test/passes/optimize-instructions_all-features.txt @@ -1,7 +1,7 @@ (module (type $i32_i32_=>_none (func (param i32 i32))) - (type $i32_=>_i32 (func (param i32) (result i32))) (type $none_=>_i32 (func (result i32))) + (type $i32_=>_i32 (func (param i32) (result i32))) (type $i32_i64_=>_none (func (param i32 i64))) (type $none_=>_none (func)) (type $i32_i32_=>_i32 (func (param i32 i32) (result i32))) @@ -2362,6 +2362,15 @@ ) ) ) + (func $zero-ops-64-special (result i32) + (return + (i32.wrap_i64 + (i64.popcnt + (i64.const 7377) + ) + ) + ) + ) (func $sign-ext-1-and-ne (result i32) (i32.ne (i32.and diff --git a/test/passes/optimize-instructions_all-features.wast b/test/passes/optimize-instructions_all-features.wast index cdd203fa4..02f55b20c 100644 --- a/test/passes/optimize-instructions_all-features.wast +++ b/test/passes/optimize-instructions_all-features.wast @@ -2683,6 +2683,21 @@ ) ) ) + (func $zero-ops-64-special (result i32) + (return + (i32.wrap_i64 + (i64.popcnt + (i64.sub + (i64.shl + (i64.const 4294783828) + (i64.const 17179869183) + ) + (i64.const -7377) + ) + ) + ) + ) + ) (func $sign-ext-1-and-ne (result i32) (select (i32.ne |