Browse Source

Optimizer interface.

cl-refactor
chriseth 10 years ago
parent
commit
2f935d722c
  1. 18
      libevmcore/Assembly.cpp
  2. 43
      libevmcore/CommonSubexpressionEliminator.cpp
  3. 70
      libevmcore/CommonSubexpressionEliminator.h

18
libevmcore/Assembly.cpp

@ -20,10 +20,9 @@
*/
#include "Assembly.h"
#include <fstream>
#include <libdevcore/Log.h>
#include <libevmcore/CommonSubexpressionEliminator.h>
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)
{

43
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 <http://www.gnu.org/licenses/>.
*/
/**
* @file CommonSubexpressionEliminator.cpp
* @author Christian <c@ethdev.com>
* @date 2015
* Optimizer step for common subexpression elimination and stack reorganisation.
*/
#include <libevmcore/CommonSubexpressionEliminator.h>
#include <libevmcore/Assembly.h>
using namespace std;
using namespace dev;
using namespace dev::eth;
vector<AssemblyItem> CommonSubexpressionEliminator::getOptimizedItems() const
{
return vector<AssemblyItem>();
}
bool CommonSubexpressionEliminator::breaksBasicBlock(AssemblyItem const&)
{
return true;
}
void CommonSubexpressionEliminator::feedItem(AssemblyItem const&)
{
}

70
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 <http://www.gnu.org/licenses/>.
*/
/**
* @file CommonSubexpressionEliminator.h
* @author Christian <c@ethdev.com>
* @date 2015
* Optimizer step for common subexpression elimination and stack reorganisation.
*/
#pragma once
#include <vector>
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 <class _AssemblyItemIterator>
_AssemblyItemIterator feedItems(_AssemblyItemIterator _iterator, _AssemblyItemIterator _end);
/// @returns the resulting items after optimization.
std::vector<AssemblyItem> 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 <class _AssemblyItemIterator>
_AssemblyItemIterator CommonSubexpressionEliminator::feedItems(
_AssemblyItemIterator _iterator,
_AssemblyItemIterator _end
)
{
while (_iterator != _end && !breaksBasicBlock(*_iterator))
feedItem(*_iterator);
return _iterator;
}
}
}
Loading…
Cancel
Save