From 2f935d722c5c7847d55bd8bb63c7ec5d440d456c Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 17 Mar 2015 22:50:01 +0100 Subject: [PATCH] Optimizer interface. --- libevmcore/Assembly.cpp | 18 ++++- libevmcore/CommonSubexpressionEliminator.cpp | 43 ++++++++++++ libevmcore/CommonSubexpressionEliminator.h | 70 ++++++++++++++++++++ 3 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 libevmcore/CommonSubexpressionEliminator.cpp create mode 100644 libevmcore/CommonSubexpressionEliminator.h diff --git a/libevmcore/Assembly.cpp b/libevmcore/Assembly.cpp index 68f326bfd..6ff6768d3 100644 --- a/libevmcore/Assembly.cpp +++ b/libevmcore/Assembly.cpp @@ -20,10 +20,9 @@ */ #include "Assembly.h" - #include - #include +#include using namespace std; using namespace dev; @@ -411,6 +410,21 @@ Assembly& Assembly::optimise(bool _enable) copt << *this; + copt << "Performing common subexpression elimination..."; + AssemblyItems optimizedItems; + for (auto iter = m_items.begin(); iter != m_items.end(); ++iter) + { + CommonSubexpressionEliminator eliminator; + iter = eliminator.feedItems(iter, m_items.end()); + optimizedItems += eliminator.getOptimizedItems(); + if (iter != m_items.end()) + optimizedItems.push_back(*iter); + } + copt << "Old size: " << m_items.size() << ", new size: " << optimizedItems.size(); + swap(m_items, optimizedItems); + + copt << *this; + unsigned total = 0; for (unsigned count = 1; count > 0; total += count) { diff --git a/libevmcore/CommonSubexpressionEliminator.cpp b/libevmcore/CommonSubexpressionEliminator.cpp new file mode 100644 index 000000000..e3215e28d --- /dev/null +++ b/libevmcore/CommonSubexpressionEliminator.cpp @@ -0,0 +1,43 @@ +/* + This file is part of cpp-ethereum. + + cpp-ethereum is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + cpp-ethereum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cpp-ethereum. If not, see . +*/ +/** + * @file CommonSubexpressionEliminator.cpp + * @author Christian + * @date 2015 + * Optimizer step for common subexpression elimination and stack reorganisation. + */ + +#include +#include + +using namespace std; +using namespace dev; +using namespace dev::eth; + +vector CommonSubexpressionEliminator::getOptimizedItems() const +{ + return vector(); +} + +bool CommonSubexpressionEliminator::breaksBasicBlock(AssemblyItem const&) +{ + return true; +} + +void CommonSubexpressionEliminator::feedItem(AssemblyItem const&) +{ +} diff --git a/libevmcore/CommonSubexpressionEliminator.h b/libevmcore/CommonSubexpressionEliminator.h new file mode 100644 index 000000000..ab04d2a8e --- /dev/null +++ b/libevmcore/CommonSubexpressionEliminator.h @@ -0,0 +1,70 @@ +/* + This file is part of cpp-ethereum. + + cpp-ethereum is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + cpp-ethereum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cpp-ethereum. If not, see . +*/ +/** + * @file CommonSubexpressionEliminator.h + * @author Christian + * @date 2015 + * Optimizer step for common subexpression elimination and stack reorganisation. + */ + +#pragma once + +#include + +namespace dev +{ +namespace eth +{ + +class AssemblyItem; + +/** + * Optimizer step that performs common subexpression elimination and stack reorginasation, + * i.e. it tries to infer equality among expressions and compute the values of two expressions + * known to be equal only once. + */ +class CommonSubexpressionEliminator +{ +public: + /// Feeds AssemblyItems into the eliminator and @returns the iterator pointing at the first + /// item that must be fed into a new instance of the eliminator. + template + _AssemblyItemIterator feedItems(_AssemblyItemIterator _iterator, _AssemblyItemIterator _end); + + /// @returns the resulting items after optimization. + std::vector getOptimizedItems() const; + +private: + /// @returns true if the given items starts a new basic block + bool breaksBasicBlock(AssemblyItem const& _item); + /// Feeds the item into the system for analysis. + void feedItem(AssemblyItem const& _item); +}; + +template +_AssemblyItemIterator CommonSubexpressionEliminator::feedItems( + _AssemblyItemIterator _iterator, + _AssemblyItemIterator _end +) +{ + while (_iterator != _end && !breaksBasicBlock(*_iterator)) + feedItem(*_iterator); + return _iterator; +} + +} +}