aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorchriseth <chris@ethereum.org>2018-02-02 01:02:10 +0800
committerchriseth <chris@ethereum.org>2018-02-07 05:51:30 +0800
commit5523296eaa68a591a331d9b75dc19cf11d1c538e (patch)
tree658443a1c32b42b56c06dca9d491f5c7e980a007
parentf7392cc6983a48ab0d482270912bd5de4042e577 (diff)
downloaddexon-solidity-5523296eaa68a591a331d9b75dc19cf11d1c538e.tar
dexon-solidity-5523296eaa68a591a331d9b75dc19cf11d1c538e.tar.gz
dexon-solidity-5523296eaa68a591a331d9b75dc19cf11d1c538e.tar.bz2
dexon-solidity-5523296eaa68a591a331d9b75dc19cf11d1c538e.tar.lz
dexon-solidity-5523296eaa68a591a331d9b75dc19cf11d1c538e.tar.xz
dexon-solidity-5523296eaa68a591a331d9b75dc19cf11d1c538e.tar.zst
dexon-solidity-5523296eaa68a591a331d9b75dc19cf11d1c538e.zip
Also apply simplification rules that require multiple identical sub-expressions.
-rw-r--r--libjulia/optimiser/ExpressionSimplifier.cpp1
-rw-r--r--libjulia/optimiser/SimplificationRules.cpp16
-rw-r--r--libjulia/optimiser/SyntacticalEquality.cpp75
-rw-r--r--libjulia/optimiser/SyntacticalEquality.h50
-rw-r--r--test/libjulia/Simplifier.cpp24
5 files changed, 163 insertions, 3 deletions
diff --git a/libjulia/optimiser/ExpressionSimplifier.cpp b/libjulia/optimiser/ExpressionSimplifier.cpp
index 1d6d9f8b..bdcf3168 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 5cd13a62..234efea0 100644
--- a/libjulia/optimiser/SimplificationRules.cpp
+++ b/libjulia/optimiser/SimplificationRules.cpp
@@ -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;
}
diff --git a/libjulia/optimiser/SyntacticalEquality.cpp b/libjulia/optimiser/SyntacticalEquality.cpp
new file mode 100644
index 00000000..2b90b091
--- /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 <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);
+
+}
diff --git a/libjulia/optimiser/SyntacticalEquality.h b/libjulia/optimiser/SyntacticalEquality.h
new file mode 100644
index 00000000..b7c09330
--- /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 <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);
+};
+
+}
+}
diff --git a/test/libjulia/Simplifier.cpp b/test/libjulia/Simplifier.cpp
index dc3a0485..b48d45c8 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(