diff options
author | Alex Beregszaszi <alex@rtfs.hu> | 2018-02-13 08:39:49 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-13 08:39:49 +0800 |
commit | 27ef979458226a7e5a89588c7c3c57b516bad304 (patch) | |
tree | 79c7cdc041ce9cac218bf096b4dd523055a557f3 | |
parent | c7ce6db32a3a5ea8d74c6cc1f866a214a6393b41 (diff) | |
parent | 1e09d6ba7b7117d5139472cc2c4af6d7f3ae91e9 (diff) | |
download | dexon-solidity-27ef979458226a7e5a89588c7c3c57b516bad304.tar.gz dexon-solidity-27ef979458226a7e5a89588c7c3c57b516bad304.tar.zst dexon-solidity-27ef979458226a7e5a89588c7c3c57b516bad304.zip |
Merge pull request #3374 from ethereum/optim-mod-pow2
Replace MOD with AND if constant is power of 2
-rw-r--r-- | libevmasm/RuleList.h | 11 | ||||
-rw-r--r-- | test/libjulia/Simplifier.cpp | 12 |
2 files changed, 23 insertions, 0 deletions
diff --git a/libevmasm/RuleList.h b/libevmasm/RuleList.h index 2312d673..da522cec 100644 --- a/libevmasm/RuleList.h +++ b/libevmasm/RuleList.h @@ -153,6 +153,17 @@ std::vector<SimplificationRule<Pattern>> simplificationRuleList( {{Instruction::OR, {{Instruction::NOT, {X}}, X}}, [=]{ return ~u256(0); }, true}, }; + // Replace MOD X, <power-of-two> with AND X, <power-of-two> - 1 + for (size_t i = 0; i < 256; ++i) + { + u256 value = u256(1) << i; + rules.push_back({ + {Instruction::MOD, {X, value}}, + [=]() -> Pattern { return {Instruction::AND, {X, value - 1}}; }, + false + }); + } + // Double negation of opcodes with boolean result for (auto const& op: std::vector<Instruction>{ Instruction::EQ, diff --git a/test/libjulia/Simplifier.cpp b/test/libjulia/Simplifier.cpp index b48d45c8..4d4e8d53 100644 --- a/test/libjulia/Simplifier.cpp +++ b/test/libjulia/Simplifier.cpp @@ -127,4 +127,16 @@ BOOST_AUTO_TEST_CASE(inside_for) ); } +BOOST_AUTO_TEST_CASE(mod_and) +{ + CHECK( + "{ mstore(0, mod(calldataload(0), exp(2, 8))) }", + "{ mstore(0, and(calldataload(0), 255)) }" + ); + CHECK( + "{ mstore(0, mod(calldataload(0), exp(2, 255))) }", + "{ mstore(0, and(calldataload(0), 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff)) }" + ); +} + BOOST_AUTO_TEST_SUITE_END() |