diff options
author | chriseth <chris@ethereum.org> | 2017-12-20 20:48:23 +0800 |
---|---|---|
committer | Alex Beregszaszi <alex@rtfs.hu> | 2018-02-06 00:43:39 +0800 |
commit | b0407cfac02bf8158385871e651591f9bee44ce0 (patch) | |
tree | 484071cb30a4a5351757235ac384e01e41c2d40d | |
parent | 9c85589122473d623da7e3de0e301662f5d835c8 (diff) | |
download | dexon-solidity-b0407cfac02bf8158385871e651591f9bee44ce0.tar dexon-solidity-b0407cfac02bf8158385871e651591f9bee44ce0.tar.gz dexon-solidity-b0407cfac02bf8158385871e651591f9bee44ce0.tar.bz2 dexon-solidity-b0407cfac02bf8158385871e651591f9bee44ce0.tar.lz dexon-solidity-b0407cfac02bf8158385871e651591f9bee44ce0.tar.xz dexon-solidity-b0407cfac02bf8158385871e651591f9bee44ce0.tar.zst dexon-solidity-b0407cfac02bf8158385871e651591f9bee44ce0.zip |
Unused variable and function remover.
-rw-r--r-- | libjulia/optimiser/UnusedPruner.cpp | 107 | ||||
-rw-r--r-- | libjulia/optimiser/UnusedPruner.h | 65 | ||||
-rw-r--r-- | test/libjulia/UnusedPruner.cpp | 98 |
3 files changed, 270 insertions, 0 deletions
diff --git a/libjulia/optimiser/UnusedPruner.cpp b/libjulia/optimiser/UnusedPruner.cpp new file mode 100644 index 00000000..e33bef78 --- /dev/null +++ b/libjulia/optimiser/UnusedPruner.cpp @@ -0,0 +1,107 @@ +/*( + 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/>. +*/ +/** + * Optimisation stage that removes unused variables and functions. + */ + +#include <libjulia/optimiser/UnusedPruner.h> + +#include <libjulia/optimiser/NameCollector.h> +#include <libjulia/optimiser/Semantics.h> +#include <libjulia/optimiser/Utilities.h> + +#include <libsolidity/inlineasm/AsmData.h> + +#include <boost/algorithm/cxx11/none_of.hpp> + +using namespace std; +using namespace dev; +using namespace dev::julia; + +UnusedPruner::UnusedPruner(Block& _ast) +{ + ReferencesCounter counter; + counter(_ast); + + m_references = counter.references(); +} + +void UnusedPruner::operator()(Block& _block) +{ + for (auto&& statement: _block.statements) + if (statement.type() == typeid(FunctionDefinition)) + { + FunctionDefinition& funDef = boost::get<FunctionDefinition>(statement); + if (!used(funDef.name)) + { + subtractReferences(ReferencesCounter::countReferences(funDef.body)); + statement = Block{std::move(funDef.location), {}}; + } + } + else if (statement.type() == typeid(VariableDeclaration)) + { + VariableDeclaration& varDecl = boost::get<VariableDeclaration>(statement); + if (boost::algorithm::none_of( + varDecl.variables, + [=](TypedName const& _typedName) { return used(_typedName.name); } + )) + { + if (!varDecl.value || MovableChecker(*varDecl.value).movable()) + { + subtractReferences(ReferencesCounter::countReferences(*varDecl.value)); + statement = Block{std::move(varDecl.location), {}}; + } + else if (varDecl.value && varDecl.variables.size() == 1) + statement = ExpressionStatement{varDecl.location, FunctionalInstruction{ + varDecl.location, + solidity::Instruction::POP, + {*std::move(varDecl.value)} + }}; + } + } + + removeEmptyBlocks(_block); + + ASTModifier::operator()(_block); +} + +void UnusedPruner::runUntilStabilised(Block& _ast) +{ + while (true) + { + UnusedPruner pruner(_ast); + pruner(_ast); + if (!pruner.shouldRunAgain()) + return; + } +} + +bool UnusedPruner::used(string const& _name) const +{ + return m_references.count(_name) && m_references.at(_name) > 0; +} + +void UnusedPruner::subtractReferences(map<string, size_t> const& _subtrahend) +{ + for (auto const& ref: _subtrahend) + { + solAssert(m_references.count(ref.first), ""); + solAssert(m_references.at(ref.first) >= ref.second, ""); + m_references[ref.first] -= ref.second; + m_shouldRunAgain = true; + } +} diff --git a/libjulia/optimiser/UnusedPruner.h b/libjulia/optimiser/UnusedPruner.h new file mode 100644 index 00000000..8b521232 --- /dev/null +++ b/libjulia/optimiser/UnusedPruner.h @@ -0,0 +1,65 @@ +/* + 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/>. +*/ +/** + * Optimisation stage that removes unused variables and functions. + */ + +#pragma once + +#include <libjulia/optimiser/ASTWalker.h> + +#include <string> +#include <map> +#include <set> + +namespace dev +{ +namespace julia +{ + +/** + * Optimisation stage that removes unused variables and functions. + * + * TODO: Also remove intermediate variable assignments from movable expressions + * which are not referenced until after the next assignment to the same variable. + * + * Note that this does not remove circular references. + * + * Prerequisite: Disambiguator + */ +class UnusedPruner: public ASTModifier +{ +public: + explicit UnusedPruner(Block& _ast); + + using ASTModifier::operator(); + virtual void operator()(Block& _block) override; + + bool shouldRunAgain() const { return m_shouldRunAgain; } + + static void runUntilStabilised(Block& _ast); + +private: + bool used(std::string const& _name) const; + void subtractReferences(std::map<std::string, size_t> const& _subtrahend); + + bool m_shouldRunAgain = false; + std::map<std::string, size_t> m_references; +}; + +} +} diff --git a/test/libjulia/UnusedPruner.cpp b/test/libjulia/UnusedPruner.cpp new file mode 100644 index 00000000..e646cee6 --- /dev/null +++ b/test/libjulia/UnusedPruner.cpp @@ -0,0 +1,98 @@ +/* + 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/>. +*/ +/** + * @date 2017 + * Unit tests for the pruning of unused variables and functions. + */ + +#include <test/libjulia/Common.h> + +#include <libjulia/optimiser/UnusedPruner.h> + +#include <libsolidity/inlineasm/AsmPrinter.h> + +#include <boost/test/unit_test.hpp> + +#include <boost/range/adaptors.hpp> +#include <boost/algorithm/string/join.hpp> + +using namespace std; +using namespace dev; +using namespace dev::julia; +using namespace dev::julia::test; +using namespace dev::solidity; + + +#define CHECK(_original, _expectation)\ +do\ +{\ + assembly::AsmPrinter p;\ + Block b = disambiguate(_original, false);\ + UnusedPruner::runUntilStabilised(b);\ + string result = p(b);\ + BOOST_CHECK_EQUAL(result, format(_expectation, false));\ +}\ +while(false) + +BOOST_AUTO_TEST_SUITE(IuliaUnusedPruner) + +BOOST_AUTO_TEST_CASE(smoke_test) +{ + CHECK("{ }", "{ }"); +} + +BOOST_AUTO_TEST_CASE(trivial) +{ + CHECK( + "{ let a := 1 let b := 1 mstore(0, 1) }", + "{ mstore(0, 1) }" + ); +} + +BOOST_AUTO_TEST_CASE(functions) +{ + CHECK( + "{ function f() { let a := 1 } function g() { f() } }", + "{ }" + ); +} + +BOOST_AUTO_TEST_CASE(intermediate_assignment) +{ + CHECK( + "{ let a := 1 a := 4 let b := 1 }", + "{ let a := 1 a := 4 }" + ); +} + +BOOST_AUTO_TEST_CASE(multi_declare) +{ + CHECK( + "{ function f() -> x, y { } let a, b := f() }", + "{ function f() -> x, y { } let a, b := f() }" + ); +} + +BOOST_AUTO_TEST_CASE(multi_assign) +{ + CHECK( + "{ let a let b function f() -> x, y { } a, b := f() }", + "{ let a let b function f() -> x, y { } a, b := f() }" + ); +} + +BOOST_AUTO_TEST_SUITE_END() |