diff options
author | chriseth <chris@ethereum.org> | 2017-06-15 16:50:07 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-15 16:50:07 +0800 |
commit | fb5db94879cdd6358d5b8a8a0097aff4b8dd680b (patch) | |
tree | 4ffd0a808868aa5e743ab1ebf66db77bb0d1da67 /libevmasm | |
parent | 1636602fc434da0b2d1a939870d2886facee209c (diff) | |
parent | 7af9d1000668c9628a2ee6294975cd7558ee1dbf (diff) | |
download | dexon-solidity-fb5db94879cdd6358d5b8a8a0097aff4b8dd680b.tar.gz dexon-solidity-fb5db94879cdd6358d5b8a8a0097aff4b8dd680b.tar.zst dexon-solidity-fb5db94879cdd6358d5b8a8a0097aff4b8dd680b.zip |
Merge pull request #2397 from ethereum/doublepush
Peephole optimizer for double push.
Diffstat (limited to 'libevmasm')
-rw-r--r-- | libevmasm/AssemblyItem.h | 8 | ||||
-rw-r--r-- | libevmasm/ConstantOptimiser.cpp | 5 | ||||
-rw-r--r-- | libevmasm/PeepholeOptimiser.cpp | 23 |
3 files changed, 29 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; - } |