diff --git a/libjulia/optimiser/ExpressionSimplifier.cpp b/libjulia/optimiser/ExpressionSimplifier.cpp index 1d6d9f8bf..bdcf31687 100644 --- a/libjulia/optimiser/ExpressionSimplifier.cpp +++ b/libjulia/optimiser/ExpressionSimplifier.cpp @@ -40,6 +40,7 @@ void ExpressionSimplifier::visit(Expression& _expression) ASTModifier::visit(_expression); while (auto match = SimplificationRules::findFirstMatch(_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()) diff --git a/libjulia/optimiser/SimplificationRules.cpp b/libjulia/optimiser/SimplificationRules.cpp index 5cd13a62a..234efea08 100644 --- a/libjulia/optimiser/SimplificationRules.cpp +++ b/libjulia/optimiser/SimplificationRules.cpp @@ -22,6 +22,8 @@ #include #include +#include +#include #include @@ -121,12 +123,20 @@ bool Pattern::matches(Expression const& _expr) const { assertThrow(m_arguments.empty(), OptimizerException, ""); } - // We do not support matching multiple expressions that require the same value. + // We support matching multiple expressions that require the same value + // based on identical ASTs, which have to be movable. if (m_matchGroup) { if (m_matchGroups->count(m_matchGroup)) - return false; - (*m_matchGroups)[m_matchGroup] = &_expr; + { + Expression const* firstMatch = (*m_matchGroups)[m_matchGroup]; + assertThrow(firstMatch, OptimizerException, "Match set but to null."); + return + SyntacticalEqualityChecker::equal(*firstMatch, _expr) && + MovableChecker(_expr).movable(); + } + else + (*m_matchGroups)[m_matchGroup] = &_expr; } return true; } diff --git a/libjulia/optimiser/SyntacticalEquality.cpp b/libjulia/optimiser/SyntacticalEquality.cpp new file mode 100644 index 000000000..2b90b0911 --- /dev/null +++ b/libjulia/optimiser/SyntacticalEquality.cpp @@ -0,0 +1,75 @@ +/*( + 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 . +*/ +/** + * Component that can compare ASTs for equality on a syntactic basis. + */ + +#include + +#include +#include + +#include + +using namespace std; +using namespace dev; +using namespace dev::julia; + +bool SyntacticalEqualityChecker::equal(Expression const& _e1, Expression const& _e2) +{ + if (_e1.type() != _e2.type()) + return false; + + // TODO This should be replaced by some kind of AST walker as soon as it gets + // more complex. + if (_e1.type() == typeid(FunctionalInstruction)) + { + auto const& e1 = boost::get(_e1); + auto const& e2 = boost::get(_e2); + return + e1.instruction == e2.instruction && + equalVector(e1.arguments, e2.arguments); + } + else if (_e1.type() == typeid(FunctionCall)) + { + auto const& e1 = boost::get(_e1); + auto const& e2 = boost::get(_e2); + return + equal(e1.functionName, e2.functionName) && + equalVector(e1.arguments, e2.arguments); + } + else if (_e1.type() == typeid(Identifier)) + return boost::get(_e1).name == boost::get(_e2).name; + else if (_e1.type() == typeid(Literal)) + { + auto const& e1 = boost::get(_e1); + auto const& e2 = boost::get(_e2); + return e1.kind == e2.kind && e1.value == e2.value && e1.type == e2.type; + } + else + { + solAssert(false, "Invlid expression"); + } + return false; +} + +bool SyntacticalEqualityChecker::equalVector(vector const& _e1, vector const& _e2) +{ + return _e1.size() == _e2.size() && + std::equal(begin(_e1), end(_e1), begin(_e2), SyntacticalEqualityChecker::equal); + +} diff --git a/libjulia/optimiser/SyntacticalEquality.h b/libjulia/optimiser/SyntacticalEquality.h new file mode 100644 index 000000000..b7c09330f --- /dev/null +++ b/libjulia/optimiser/SyntacticalEquality.h @@ -0,0 +1,50 @@ +/* + 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 . +*/ +/** + * Component that can compare ASTs for equality on a syntactic basis. + */ + +#pragma once + +#include + +#include + +namespace dev +{ +namespace julia +{ + +/** + * Component that can compare ASTs for equality on a syntactic basis. + * Ignores source locations but requires exact matches otherwise. + * + * TODO: Only implemented for Expressions for now. + * A future version might also recognize renamed variables and thus could be used to + * remove duplicate functions. + */ +class SyntacticalEqualityChecker +{ +public: + static bool equal(Expression const& _e1, Expression const& _e2); + +protected: + static bool equalVector(std::vector const& _e1, std::vector const& _e2); +}; + +} +} diff --git a/test/libjulia/Simplifier.cpp b/test/libjulia/Simplifier.cpp index dc3a0485e..b48d45c84 100644 --- a/test/libjulia/Simplifier.cpp +++ b/test/libjulia/Simplifier.cpp @@ -87,6 +87,30 @@ BOOST_AUTO_TEST_CASE(constant_propagation) ); } +BOOST_AUTO_TEST_CASE(identity_rules_simple) +{ + CHECK( + "{ let a := mload(0) let b := sub(a, a) }", + "{ let a := mload(0) let b := 0 }" + ); +} + +BOOST_AUTO_TEST_CASE(identity_rules_complex) +{ + CHECK( + "{ let a := sub(calldataload(0), calldataload(0)) }", + "{ let a := 0 }" + ); +} + +BOOST_AUTO_TEST_CASE(identity_rules_negative) +{ + CHECK( + "{ let a := sub(calldataload(1), calldataload(0)) }", + "{ let a := sub(calldataload(1), calldataload(0)) }" + ); +} + BOOST_AUTO_TEST_CASE(including_function_calls) { CHECK(