From 037b97ef4a53c30f2800777a88d754a898bccc2b Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Thu, 4 Jan 2018 12:05:01 +0000 Subject: [PATCH 1/2] Replace MOD with AND if constant is power of 2 --- libevmasm/RuleList.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/libevmasm/RuleList.h b/libevmasm/RuleList.h index 2312d673c..da522cec8 100644 --- a/libevmasm/RuleList.h +++ b/libevmasm/RuleList.h @@ -153,6 +153,17 @@ std::vector> simplificationRuleList( {{Instruction::OR, {{Instruction::NOT, {X}}, X}}, [=]{ return ~u256(0); }, true}, }; + // Replace MOD X, with AND X, - 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::EQ, From 1e09d6ba7b7117d5139472cc2c4af6d7f3ae91e9 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 9 Feb 2018 19:00:16 +0100 Subject: [PATCH 2/2] Test for Mod-And rule. --- test/libjulia/Simplifier.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/libjulia/Simplifier.cpp b/test/libjulia/Simplifier.cpp index b48d45c84..4d4e8d53a 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()