Turn simplification rule tuple into struct.

This commit is contained in:
chriseth 2018-02-06 12:20:00 +01:00
parent 5523296eaa
commit c961a3079d
8 changed files with 75 additions and 30 deletions

View File

@ -202,7 +202,7 @@ ExpressionClasses::Id ExpressionClasses::tryToSimplify(Expression const& _expr)
//cout << "with rule " << match->first.toString() << endl; //cout << "with rule " << match->first.toString() << endl;
//ExpressionTemplate t(match->second()); //ExpressionTemplate t(match->second());
//cout << "to " << match->second().toString() << endl; //cout << "to " << match->second().toString() << endl;
return rebuildExpression(ExpressionTemplate(std::get<1>(*match)(), _expr.item->location())); return rebuildExpression(ExpressionTemplate(match->action(), _expr.item->location()));
} }
return -1; return -1;

View File

@ -25,6 +25,7 @@
#include <functional> #include <functional>
#include <libevmasm/Instruction.h> #include <libevmasm/Instruction.h>
#include <libevmasm/SimplificationRule.h>
#include <libdevcore/CommonData.h> #include <libdevcore/CommonData.h>
@ -45,12 +46,10 @@ template <class S> S modWorkaround(S const& _a, S const& _b)
/// @returns a list of simplification rules given certain match placeholders. /// @returns a list of simplification rules given certain match placeholders.
/// A, B and C should represent constants, X and Y arbitrary expressions. /// A, B and C should represent constants, X and Y arbitrary expressions.
/// The third element in the tuple is a boolean flag that indicates whether
/// any non-constant elements in the pattern are removed by applying it.
/// The simplifications should neven change the order of evaluation of /// The simplifications should neven change the order of evaluation of
/// arbitrary operations, though. /// arbitrary operations.
template <class Pattern> template <class Pattern>
std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>> simplificationRuleList( std::vector<SimplificationRule<Pattern>> simplificationRuleList(
Pattern A, Pattern A,
Pattern B, Pattern B,
Pattern C, Pattern C,
@ -58,8 +57,8 @@ std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>> simplificationR
Pattern Y Pattern Y
) )
{ {
std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>> rules; std::vector<SimplificationRule<Pattern>> rules;
rules += std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>>{ rules += std::vector<SimplificationRule<Pattern>>{
// arithmetics on constants // arithmetics on constants
{{Instruction::ADD, {A, B}}, [=]{ return A.d() + B.d(); }, false}, {{Instruction::ADD, {A, B}}, [=]{ return A.d() + B.d(); }, false},
{{Instruction::MUL, {A, B}}, [=]{ return A.d() * B.d(); }, false}, {{Instruction::MUL, {A, B}}, [=]{ return A.d() * B.d(); }, false},
@ -196,7 +195,7 @@ std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>> simplificationR
// xa can be (X, A) or (A, X) // xa can be (X, A) or (A, X)
for (auto xa: {std::vector<Pattern>{X, A}, std::vector<Pattern>{A, X}}) for (auto xa: {std::vector<Pattern>{X, A}, std::vector<Pattern>{A, X}})
{ {
rules += std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>>{{ rules += std::vector<SimplificationRule<Pattern>>{{
// (X+A)+B -> X+(A+B) // (X+A)+B -> X+(A+B)
{op, {{op, xa}, B}}, {op, {{op, xa}, B}},
[=]() -> Pattern { return {op, {X, fun(A.d(), B.d())}}; }, [=]() -> Pattern { return {op, {X, fun(A.d(), B.d())}}; },
@ -221,7 +220,7 @@ std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>> simplificationR
} }
// move constants across subtractions // move constants across subtractions
rules += std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>>{ rules += std::vector<SimplificationRule<Pattern>>{
{ {
// X - A -> X + (-A) // X - A -> X + (-A)
{Instruction::SUB, {X, A}}, {Instruction::SUB, {X, A}},

View File

@ -0,0 +1,45 @@
/*
This file is part of solidity.
solidity 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.
solidity 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 solidity. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Expression simplification pattern.
*/
#pragma once
#include <functional>
namespace dev
{
namespace solidity
{
/**
* Rule that contains a pattern, an action that can be applied
* after the pattern has matched and a bool that indicates
* whether the action would remove something from the expression
* than is not a constant literal.
*/
template <class Pattern>
struct SimplificationRule
{
Pattern pattern;
std::function<Pattern()> action;
bool removesNonConstants;
};
}
}

View File

@ -23,7 +23,6 @@
#include <libevmasm/ExpressionClasses.h> #include <libevmasm/ExpressionClasses.h>
#include <utility> #include <utility>
#include <tuple>
#include <functional> #include <functional>
#include <boost/range/adaptor/reversed.hpp> #include <boost/range/adaptor/reversed.hpp>
#include <boost/noncopyable.hpp> #include <boost/noncopyable.hpp>
@ -38,7 +37,7 @@ using namespace dev;
using namespace dev::eth; using namespace dev::eth;
tuple<Pattern, function<Pattern()>, bool> const* Rules::findFirstMatch( SimplificationRule<Pattern> const* Rules::findFirstMatch(
Expression const& _expr, Expression const& _expr,
ExpressionClasses const& _classes ExpressionClasses const& _classes
) )
@ -48,22 +47,22 @@ tuple<Pattern, function<Pattern()>, bool> const* Rules::findFirstMatch(
assertThrow(_expr.item, OptimizerException, ""); assertThrow(_expr.item, OptimizerException, "");
for (auto const& rule: m_rules[byte(_expr.item->instruction())]) for (auto const& rule: m_rules[byte(_expr.item->instruction())])
{ {
if (std::get<0>(rule).matches(_expr, _classes)) if (rule.pattern.matches(_expr, _classes))
return &rule; return &rule;
resetMatchGroups(); resetMatchGroups();
} }
return nullptr; return nullptr;
} }
void Rules::addRules(std::vector<std::tuple<Pattern, std::function<Pattern ()>, bool>> const& _rules) void Rules::addRules(std::vector<SimplificationRule<Pattern>> const& _rules)
{ {
for (auto const& r: _rules) for (auto const& r: _rules)
addRule(r); addRule(r);
} }
void Rules::addRule(std::tuple<Pattern, std::function<Pattern()>, bool> const& _rule) void Rules::addRule(SimplificationRule<Pattern> const& _rule)
{ {
m_rules[byte(std::get<0>(_rule).instruction())].push_back(_rule); m_rules[byte(_rule.pattern.instruction())].push_back(_rule);
} }
Rules::Rules() Rules::Rules()

View File

@ -24,6 +24,7 @@
#pragma once #pragma once
#include <libevmasm/ExpressionClasses.h> #include <libevmasm/ExpressionClasses.h>
#include <libevmasm/SimplificationRule.h>
#include <functional> #include <functional>
#include <vector> #include <vector>
@ -47,21 +48,21 @@ public:
/// @returns a pointer to the first matching pattern and sets the match /// @returns a pointer to the first matching pattern and sets the match
/// groups accordingly. /// groups accordingly.
std::tuple<Pattern, std::function<Pattern()>, bool> const* findFirstMatch( SimplificationRule<Pattern> const* findFirstMatch(
Expression const& _expr, Expression const& _expr,
ExpressionClasses const& _classes ExpressionClasses const& _classes
); );
private: private:
void addRules(std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>> const& _rules); void addRules(std::vector<SimplificationRule<Pattern>> const& _rules);
void addRule(std::tuple<Pattern, std::function<Pattern()>, bool> const& _rule); void addRule(SimplificationRule<Pattern> const& _rule);
void resetMatchGroups() { m_matchGroups.clear(); } void resetMatchGroups() { m_matchGroups.clear(); }
std::map<unsigned, Expression const*> m_matchGroups; std::map<unsigned, Expression const*> m_matchGroups;
/// Pattern to match, replacement to be applied and flag indicating whether /// Pattern to match, replacement to be applied and flag indicating whether
/// the replacement might remove some elements (except constants). /// the replacement might remove some elements (except constants).
std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>> m_rules[256]; std::vector<SimplificationRule<Pattern>> m_rules[256];
}; };
/** /**

View File

@ -43,8 +43,8 @@ void ExpressionSimplifier::visit(Expression& _expression)
// Do not apply the rule if it removes non-constant parts of the expression. // Do not apply the rule if it removes non-constant parts of the expression.
// TODO: The check could actually be less strict than "movable". // TODO: The check could actually be less strict than "movable".
// We only require "Does not cause side-effects". // We only require "Does not cause side-effects".
if (std::get<2>(*match) && !MovableChecker(_expression).movable()) if (match->removesNonConstants && !MovableChecker(_expression).movable())
return; return;
_expression = std::get<1>(*match)().toExpression(locationOf(_expression)); _expression = match->action().toExpression(locationOf(_expression));
} }
} }

View File

@ -34,7 +34,7 @@ using namespace dev;
using namespace dev::julia; using namespace dev::julia;
tuple<Pattern, function<Pattern()>, bool> const* SimplificationRules::findFirstMatch(Expression const& _expr) SimplificationRule<Pattern> const* SimplificationRules::findFirstMatch(Expression const& _expr)
{ {
if (_expr.type() != typeid(FunctionalInstruction)) if (_expr.type() != typeid(FunctionalInstruction))
return nullptr; return nullptr;
@ -45,21 +45,21 @@ tuple<Pattern, function<Pattern()>, bool> const* SimplificationRules::findFirstM
for (auto const& rule: rules.m_rules[byte(instruction.instruction)]) for (auto const& rule: rules.m_rules[byte(instruction.instruction)])
{ {
rules.resetMatchGroups(); rules.resetMatchGroups();
if (std::get<0>(rule).matches(_expr)) if (rule.pattern.matches(_expr))
return &rule; return &rule;
} }
return nullptr; return nullptr;
} }
void SimplificationRules::addRules(vector<tuple<Pattern, function<Pattern()>, bool>> const& _rules) void SimplificationRules::addRules(vector<SimplificationRule<Pattern>> const& _rules)
{ {
for (auto const& r: _rules) for (auto const& r: _rules)
addRule(r); addRule(r);
} }
void SimplificationRules::addRule(tuple<Pattern, function<Pattern()>, bool> const& _rule) void SimplificationRules::addRule(SimplificationRule<Pattern> const& _rule)
{ {
m_rules[byte(std::get<0>(_rule).instruction())].push_back(_rule); m_rules[byte(_rule.pattern.instruction())].push_back(_rule);
} }
SimplificationRules::SimplificationRules() SimplificationRules::SimplificationRules()

View File

@ -21,6 +21,7 @@
#pragma once #pragma once
#include <libevmasm/ExpressionClasses.h> #include <libevmasm/ExpressionClasses.h>
#include <libevmasm/SimplificationRule.h>
#include <libjulia/ASTDataForward.h> #include <libjulia/ASTDataForward.h>
@ -48,16 +49,16 @@ public:
/// @returns a pointer to the first matching pattern and sets the match /// @returns a pointer to the first matching pattern and sets the match
/// groups accordingly. /// groups accordingly.
static std::tuple<Pattern, std::function<Pattern()>, bool> const* findFirstMatch(Expression const& _expr); static SimplificationRule<Pattern> const* findFirstMatch(Expression const& _expr);
private: private:
void addRules(std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>> const& _rules); void addRules(std::vector<SimplificationRule<Pattern>> const& _rules);
void addRule(std::tuple<Pattern, std::function<Pattern()>, bool> const& _rule); void addRule(SimplificationRule<Pattern> const& _rule);
void resetMatchGroups() { m_matchGroups.clear(); } void resetMatchGroups() { m_matchGroups.clear(); }
std::map<unsigned, Expression const*> m_matchGroups; std::map<unsigned, Expression const*> m_matchGroups;
std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>> m_rules[256]; std::vector<SimplificationRule<Pattern>> m_rules[256];
}; };
enum class PatternKind enum class PatternKind