Merge pull request #3374 from ethereum/optim-mod-pow2

Replace MOD with AND if constant is power of 2
This commit is contained in:
Alex Beregszaszi 2018-02-13 00:39:49 +00:00 committed by GitHub
commit 27ef979458
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 0 deletions

View File

@ -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,

View File

@ -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()