diff options
-rw-r--r-- | libevmasm/ExpressionClasses.cpp | 2 | ||||
-rw-r--r-- | libevmasm/RuleList.h | 15 | ||||
-rw-r--r-- | libevmasm/SimplificationRule.h | 45 | ||||
-rw-r--r-- | libevmasm/SimplificationRules.cpp | 11 | ||||
-rw-r--r-- | libevmasm/SimplificationRules.h | 9 | ||||
-rw-r--r-- | libjulia/optimiser/ExpressionSimplifier.cpp | 4 | ||||
-rw-r--r-- | libjulia/optimiser/SimplificationRules.cpp | 10 | ||||
-rw-r--r-- | libjulia/optimiser/SimplificationRules.h | 9 |
8 files changed, 75 insertions, 30 deletions
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 <functional> #include <libevmasm/Instruction.h> +#include <libevmasm/SimplificationRule.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. /// 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 <class Pattern> -std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>> simplificationRuleList( +std::vector<SimplificationRule<Pattern>> simplificationRuleList( Pattern A, Pattern B, Pattern C, @@ -58,8 +57,8 @@ std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>> simplificationR Pattern Y ) { - std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>> rules; - rules += std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>>{ + std::vector<SimplificationRule<Pattern>> rules; + rules += std::vector<SimplificationRule<Pattern>>{ // 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<std::tuple<Pattern, std::function<Pattern()>, bool>> simplificationR // xa can be (X, A) or (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) {op, {{op, xa}, B}}, [=]() -> 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 - rules += std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>>{ + rules += std::vector<SimplificationRule<Pattern>>{ { // 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 <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; +}; + +} +} 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 <libevmasm/ExpressionClasses.h> #include <utility> -#include <tuple> #include <functional> #include <boost/range/adaptor/reversed.hpp> #include <boost/noncopyable.hpp> @@ -38,7 +37,7 @@ using namespace dev; using namespace dev::eth; -tuple<Pattern, function<Pattern()>, bool> const* Rules::findFirstMatch( +SimplificationRule<Pattern> const* Rules::findFirstMatch( Expression const& _expr, ExpressionClasses const& _classes ) @@ -48,22 +47,22 @@ tuple<Pattern, function<Pattern()>, 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<std::tuple<Pattern, std::function<Pattern ()>, bool>> const& _rules) +void Rules::addRules(std::vector<SimplificationRule<Pattern>> const& _rules) { for (auto const& r: _rules) 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() 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 <libevmasm/ExpressionClasses.h> +#include <libevmasm/SimplificationRule.h> #include <functional> #include <vector> @@ -47,21 +48,21 @@ public: /// @returns a pointer to the first matching pattern and sets the match /// groups accordingly. - std::tuple<Pattern, std::function<Pattern()>, bool> const* findFirstMatch( + SimplificationRule<Pattern> const* findFirstMatch( Expression const& _expr, ExpressionClasses const& _classes ); private: - void addRules(std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>> const& _rules); - void addRule(std::tuple<Pattern, std::function<Pattern()>, bool> const& _rule); + void addRules(std::vector<SimplificationRule<Pattern>> const& _rules); + void addRule(SimplificationRule<Pattern> const& _rule); void resetMatchGroups() { m_matchGroups.clear(); } std::map<unsigned, Expression const*> m_matchGroups; /// Pattern to match, replacement to be applied and flag indicating whether /// 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]; }; /** 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<Pattern, function<Pattern()>, bool> const* SimplificationRules::findFirstMatch(Expression const& _expr) +SimplificationRule<Pattern> const* SimplificationRules::findFirstMatch(Expression const& _expr) { if (_expr.type() != typeid(FunctionalInstruction)) return nullptr; @@ -45,21 +45,21 @@ tuple<Pattern, function<Pattern()>, 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<tuple<Pattern, function<Pattern()>, bool>> const& _rules) +void SimplificationRules::addRules(vector<SimplificationRule<Pattern>> const& _rules) { for (auto const& r: _rules) 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() 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 <libevmasm/ExpressionClasses.h> +#include <libevmasm/SimplificationRule.h> #include <libjulia/ASTDataForward.h> @@ -48,16 +49,16 @@ public: /// @returns a pointer to the first matching pattern and sets the match /// groups accordingly. - static std::tuple<Pattern, std::function<Pattern()>, bool> const* findFirstMatch(Expression const& _expr); + static SimplificationRule<Pattern> const* findFirstMatch(Expression const& _expr); private: - void addRules(std::vector<std::tuple<Pattern, std::function<Pattern()>, bool>> const& _rules); - void addRule(std::tuple<Pattern, std::function<Pattern()>, bool> const& _rule); + void addRules(std::vector<SimplificationRule<Pattern>> const& _rules); + void addRule(SimplificationRule<Pattern> const& _rule); void resetMatchGroups() { m_matchGroups.clear(); } 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 |