From c961a3079dda8735363872cdb84c489d61846003 Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 6 Feb 2018 12:20:00 +0100 Subject: Turn simplification rule tuple into struct. --- libevmasm/ExpressionClasses.cpp | 2 +- libevmasm/RuleList.h | 15 +++++----- libevmasm/SimplificationRule.h | 45 +++++++++++++++++++++++++++++ libevmasm/SimplificationRules.cpp | 11 ++++--- libevmasm/SimplificationRules.h | 9 +++--- libjulia/optimiser/ExpressionSimplifier.cpp | 4 +-- libjulia/optimiser/SimplificationRules.cpp | 10 +++---- libjulia/optimiser/SimplificationRules.h | 9 +++--- 8 files changed, 75 insertions(+), 30 deletions(-) create mode 100644 libevmasm/SimplificationRule.h diff --git a/libevmasm/ExpressionClasses.cpp b/libevmasm/ExpressionClasses.cpp index 3825f5ed..69b33ec5 100644 --- a/libevmasm/ExpressionClasses.cpp +++ b/libevmasm/ExpressionClasses.cpp @@ -202,7 +202,7 @@ ExpressionClasses::Id ExpressionClasses::tryToSimplify(Expression const& _expr) //cout << "with rule " << match->first.toString() << endl; //ExpressionTemplate t(match->second()); //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; diff --git a/libevmasm/RuleList.h b/libevmasm/RuleList.h index f8a6ce73..2312d673 100644 --- a/libevmasm/RuleList.h +++ b/libevmasm/RuleList.h @@ -25,6 +25,7 @@ #include #include +#include #include @@ -45,12 +46,10 @@ template S modWorkaround(S const& _a, S const& _b) /// @returns a list of simplification rules given certain match placeholders. /// 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 -/// arbitrary operations, though. +/// arbitrary operations. template -std::vector, bool>> simplificationRuleList( +std::vector> simplificationRuleList( Pattern A, Pattern B, Pattern C, @@ -58,8 +57,8 @@ std::vector, bool>> simplificationR Pattern Y ) { - std::vector, bool>> rules; - rules += std::vector, bool>>{ + std::vector> rules; + rules += std::vector>{ // arithmetics on constants {{Instruction::ADD, {A, B}}, [=]{ return A.d() + B.d(); }, false}, {{Instruction::MUL, {A, B}}, [=]{ return A.d() * B.d(); }, false}, @@ -196,7 +195,7 @@ std::vector, bool>> simplificationR // xa can be (X, A) or (A, X) for (auto xa: {std::vector{X, A}, std::vector{A, X}}) { - rules += std::vector, bool>>{{ + rules += std::vector>{{ // (X+A)+B -> X+(A+B) {op, {{op, xa}, B}}, [=]() -> Pattern { return {op, {X, fun(A.d(), B.d())}}; }, @@ -221,7 +220,7 @@ std::vector, bool>> simplificationR } // move constants across subtractions - rules += std::vector, bool>>{ + rules += std::vector>{ { // X - A -> X + (-A) {Instruction::SUB, {X, A}}, diff --git a/libevmasm/SimplificationRule.h b/libevmasm/SimplificationRule.h new file mode 100644 index 00000000..7b4dea68 --- /dev/null +++ b/libevmasm/SimplificationRule.h @@ -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 . +*/ +/** + * Expression simplification pattern. + */ + +#pragma once + +#include + +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 +struct SimplificationRule +{ + Pattern pattern; + std::function action; + bool removesNonConstants; +}; + +} +} diff --git a/libevmasm/SimplificationRules.cpp b/libevmasm/SimplificationRules.cpp index d81a993f..53a5f9fc 100644 --- a/libevmasm/SimplificationRules.cpp +++ b/libevmasm/SimplificationRules.cpp @@ -23,7 +23,6 @@ #include #include -#include #include #include #include @@ -38,7 +37,7 @@ using namespace dev; using namespace dev::eth; -tuple, bool> const* Rules::findFirstMatch( +SimplificationRule const* Rules::findFirstMatch( Expression const& _expr, ExpressionClasses const& _classes ) @@ -48,22 +47,22 @@ tuple, bool> const* Rules::findFirstMatch( assertThrow(_expr.item, OptimizerException, ""); 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; resetMatchGroups(); } return nullptr; } -void Rules::addRules(std::vector, bool>> const& _rules) +void Rules::addRules(std::vector> const& _rules) { for (auto const& r: _rules) addRule(r); } -void Rules::addRule(std::tuple, bool> const& _rule) +void Rules::addRule(SimplificationRule const& _rule) { - m_rules[byte(std::get<0>(_rule).instruction())].push_back(_rule); + m_rules[byte(_rule.pattern.instruction())].push_back(_rule); } Rules::Rules() diff --git a/libevmasm/SimplificationRules.h b/libevmasm/SimplificationRules.h index 6040fd76..53f7e595 100644 --- a/libevmasm/SimplificationRules.h +++ b/libevmasm/SimplificationRules.h @@ -24,6 +24,7 @@ #pragma once #include +#include #include #include @@ -47,21 +48,21 @@ public: /// @returns a pointer to the first matching pattern and sets the match /// groups accordingly. - std::tuple, bool> const* findFirstMatch( + SimplificationRule const* findFirstMatch( Expression const& _expr, ExpressionClasses const& _classes ); private: - void addRules(std::vector, bool>> const& _rules); - void addRule(std::tuple, bool> const& _rule); + void addRules(std::vector> const& _rules); + void addRule(SimplificationRule const& _rule); void resetMatchGroups() { m_matchGroups.clear(); } std::map m_matchGroups; /// Pattern to match, replacement to be applied and flag indicating whether /// the replacement might remove some elements (except constants). - std::vector, bool>> m_rules[256]; + std::vector> m_rules[256]; }; /** diff --git a/libjulia/optimiser/ExpressionSimplifier.cpp b/libjulia/optimiser/ExpressionSimplifier.cpp index bdcf3168..3d471cb3 100644 --- a/libjulia/optimiser/ExpressionSimplifier.cpp +++ b/libjulia/optimiser/ExpressionSimplifier.cpp @@ -43,8 +43,8 @@ void ExpressionSimplifier::visit(Expression& _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". // We only require "Does not cause side-effects". - if (std::get<2>(*match) && !MovableChecker(_expression).movable()) + if (match->removesNonConstants && !MovableChecker(_expression).movable()) return; - _expression = std::get<1>(*match)().toExpression(locationOf(_expression)); + _expression = match->action().toExpression(locationOf(_expression)); } } diff --git a/libjulia/optimiser/SimplificationRules.cpp b/libjulia/optimiser/SimplificationRules.cpp index 234efea0..a439caef 100644 --- a/libjulia/optimiser/SimplificationRules.cpp +++ b/libjulia/optimiser/SimplificationRules.cpp @@ -34,7 +34,7 @@ using namespace dev; using namespace dev::julia; -tuple, bool> const* SimplificationRules::findFirstMatch(Expression const& _expr) +SimplificationRule const* SimplificationRules::findFirstMatch(Expression const& _expr) { if (_expr.type() != typeid(FunctionalInstruction)) return nullptr; @@ -45,21 +45,21 @@ tuple, bool> const* SimplificationRules::findFirstM for (auto const& rule: rules.m_rules[byte(instruction.instruction)]) { rules.resetMatchGroups(); - if (std::get<0>(rule).matches(_expr)) + if (rule.pattern.matches(_expr)) return &rule; } return nullptr; } -void SimplificationRules::addRules(vector, bool>> const& _rules) +void SimplificationRules::addRules(vector> const& _rules) { for (auto const& r: _rules) addRule(r); } -void SimplificationRules::addRule(tuple, bool> const& _rule) +void SimplificationRules::addRule(SimplificationRule const& _rule) { - m_rules[byte(std::get<0>(_rule).instruction())].push_back(_rule); + m_rules[byte(_rule.pattern.instruction())].push_back(_rule); } SimplificationRules::SimplificationRules() diff --git a/libjulia/optimiser/SimplificationRules.h b/libjulia/optimiser/SimplificationRules.h index 96f3de21..68b640b1 100644 --- a/libjulia/optimiser/SimplificationRules.h +++ b/libjulia/optimiser/SimplificationRules.h @@ -21,6 +21,7 @@ #pragma once #include +#include #include @@ -48,16 +49,16 @@ public: /// @returns a pointer to the first matching pattern and sets the match /// groups accordingly. - static std::tuple, bool> const* findFirstMatch(Expression const& _expr); + static SimplificationRule const* findFirstMatch(Expression const& _expr); private: - void addRules(std::vector, bool>> const& _rules); - void addRule(std::tuple, bool> const& _rule); + void addRules(std::vector> const& _rules); + void addRule(SimplificationRule const& _rule); void resetMatchGroups() { m_matchGroups.clear(); } std::map m_matchGroups; - std::vector, bool>> m_rules[256]; + std::vector> m_rules[256]; }; enum class PatternKind -- cgit