diff options
-rw-r--r-- | libevmasm/AssemblyItem.h | 8 | ||||
-rw-r--r-- | libevmasm/ConstantOptimiser.cpp | 5 | ||||
-rw-r--r-- | libevmasm/PeepholeOptimiser.cpp | 23 | ||||
-rw-r--r-- | test/libsolidity/SolidityOptimizer.cpp | 26 |
4 files changed, 55 insertions, 7 deletions
diff --git a/libevmasm/AssemblyItem.h b/libevmasm/AssemblyItem.h index 464368fb..d38db927 100644 --- a/libevmasm/AssemblyItem.h +++ b/libevmasm/AssemblyItem.h @@ -148,6 +148,14 @@ private: using AssemblyItems = std::vector<AssemblyItem>; +inline size_t bytesRequired(AssemblyItems const& _items, size_t _addressLength) +{ + size_t size = 0; + for (AssemblyItem const& item: _items) + size += item.bytesRequired(_addressLength); + return size; +} + std::ostream& operator<<(std::ostream& _out, AssemblyItem const& _item); inline std::ostream& operator<<(std::ostream& _out, AssemblyItems const& _items) { diff --git a/libevmasm/ConstantOptimiser.cpp b/libevmasm/ConstantOptimiser.cpp index 0c093ebf..2ecbfa7f 100644 --- a/libevmasm/ConstantOptimiser.cpp +++ b/libevmasm/ConstantOptimiser.cpp @@ -99,10 +99,7 @@ bigint ConstantOptimisationMethod::dataGas(bytes const& _data) const size_t ConstantOptimisationMethod::bytesRequired(AssemblyItems const& _items) { - size_t size = 0; - for (AssemblyItem const& item: _items) - size += item.bytesRequired(3); // assume 3 byte addresses - return size; + return eth::bytesRequired(_items, 3); // assume 3 byte addresses } void ConstantOptimisationMethod::replaceConstants( diff --git a/libevmasm/PeepholeOptimiser.cpp b/libevmasm/PeepholeOptimiser.cpp index 6c92d76b..e94a8ba4 100644 --- a/libevmasm/PeepholeOptimiser.cpp +++ b/libevmasm/PeepholeOptimiser.cpp @@ -136,6 +136,21 @@ struct DoubleSwap: SimplePeepholeOptimizerMethod<DoubleSwap, 2> } }; +struct DoublePush: SimplePeepholeOptimizerMethod<DoublePush, 2> +{ + static bool applySimple(AssemblyItem const& _push1, AssemblyItem const& _push2, std::back_insert_iterator<AssemblyItems> _out) + { + if (_push1.type() == Push && _push2.type() == Push && _push1.data() == _push2.data()) + { + *_out = _push1; + *_out = {Instruction::DUP1, _push2.location()}; + return true; + } + else + return false; + } +}; + struct JumpToNext: SimplePeepholeOptimizerMethod<JumpToNext, 3> { static size_t applySimple( @@ -235,13 +250,15 @@ bool PeepholeOptimiser::optimise() { OptimiserState state {m_items, 0, std::back_inserter(m_optimisedItems)}; while (state.i < m_items.size()) - applyMethods(state, PushPop(), OpPop(), DoubleSwap(), JumpToNext(), UnreachableCode(), TagConjunctions(), Identity()); - if (m_optimisedItems.size() < m_items.size()) + applyMethods(state, PushPop(), OpPop(), DoublePush(), DoubleSwap(), JumpToNext(), UnreachableCode(), TagConjunctions(), Identity()); + if (m_optimisedItems.size() < m_items.size() || ( + m_optimisedItems.size() == m_items.size() && + eth::bytesRequired(m_optimisedItems, 3) < eth::bytesRequired(m_items, 3) + )) { m_items = std::move(m_optimisedItems); return true; } else return false; - } diff --git a/test/libsolidity/SolidityOptimizer.cpp b/test/libsolidity/SolidityOptimizer.cpp index bdcdacff..7afbe04e 100644 --- a/test/libsolidity/SolidityOptimizer.cpp +++ b/test/libsolidity/SolidityOptimizer.cpp @@ -1189,6 +1189,32 @@ BOOST_AUTO_TEST_CASE(clear_unreachable_code) ); } +BOOST_AUTO_TEST_CASE(peephole_double_push) +{ + AssemblyItems items{ + u256(0), + u256(0), + u256(5), + u256(5), + u256(4), + u256(5) + }; + AssemblyItems expectation{ + u256(0), + Instruction::DUP1, + u256(5), + Instruction::DUP1, + u256(4), + u256(5) + }; + PeepholeOptimiser peepOpt(items); + BOOST_REQUIRE(peepOpt.optimise()); + BOOST_CHECK_EQUAL_COLLECTIONS( + items.begin(), items.end(), + expectation.begin(), expectation.end() + ); +} + BOOST_AUTO_TEST_CASE(computing_constants) { char const* sourceCode = R"( |