mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Also apply simplification rules that require multiple identical sub-expressions.
This commit is contained in:
parent
f7392cc698
commit
5523296eaa
@ -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())
|
||||
|
@ -22,6 +22,8 @@
|
||||
|
||||
#include <libjulia/optimiser/Utilities.h>
|
||||
#include <libjulia/optimiser/ASTCopier.h>
|
||||
#include <libjulia/optimiser/Semantics.h>
|
||||
#include <libjulia/optimiser/SyntacticalEquality.h>
|
||||
|
||||
#include <libsolidity/inlineasm/AsmData.h>
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
75
libjulia/optimiser/SyntacticalEquality.cpp
Normal file
75
libjulia/optimiser/SyntacticalEquality.cpp
Normal file
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/**
|
||||
* Component that can compare ASTs for equality on a syntactic basis.
|
||||
*/
|
||||
|
||||
#include <libjulia/optimiser/SyntacticalEquality.h>
|
||||
|
||||
#include <libsolidity/inlineasm/AsmData.h>
|
||||
#include <libsolidity/interface/Exceptions.h>
|
||||
|
||||
#include <libdevcore/CommonData.h>
|
||||
|
||||
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<FunctionalInstruction>(_e1);
|
||||
auto const& e2 = boost::get<FunctionalInstruction>(_e2);
|
||||
return
|
||||
e1.instruction == e2.instruction &&
|
||||
equalVector(e1.arguments, e2.arguments);
|
||||
}
|
||||
else if (_e1.type() == typeid(FunctionCall))
|
||||
{
|
||||
auto const& e1 = boost::get<FunctionCall>(_e1);
|
||||
auto const& e2 = boost::get<FunctionCall>(_e2);
|
||||
return
|
||||
equal(e1.functionName, e2.functionName) &&
|
||||
equalVector(e1.arguments, e2.arguments);
|
||||
}
|
||||
else if (_e1.type() == typeid(Identifier))
|
||||
return boost::get<Identifier>(_e1).name == boost::get<Identifier>(_e2).name;
|
||||
else if (_e1.type() == typeid(Literal))
|
||||
{
|
||||
auto const& e1 = boost::get<Literal>(_e1);
|
||||
auto const& e2 = boost::get<Literal>(_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<Expression> const& _e1, vector<Expression> const& _e2)
|
||||
{
|
||||
return _e1.size() == _e2.size() &&
|
||||
std::equal(begin(_e1), end(_e1), begin(_e2), SyntacticalEqualityChecker::equal);
|
||||
|
||||
}
|
50
libjulia/optimiser/SyntacticalEquality.h
Normal file
50
libjulia/optimiser/SyntacticalEquality.h
Normal file
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/**
|
||||
* Component that can compare ASTs for equality on a syntactic basis.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <libjulia/ASTDataForward.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
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<Expression> const& _e1, std::vector<Expression> const& _e2);
|
||||
};
|
||||
|
||||
}
|
||||
}
|
@ -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(
|
||||
|
Loading…
Reference in New Issue
Block a user