mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #3351 from ethereum/remove_unused
Remove unused variables and functions
This commit is contained in:
commit
6b917eb528
@ -71,8 +71,8 @@ protected:
|
|||||||
template <class T>
|
template <class T>
|
||||||
void walkVector(T const& _statements)
|
void walkVector(T const& _statements)
|
||||||
{
|
{
|
||||||
for (auto const& st: _statements)
|
for (auto const& statement: _statements)
|
||||||
visit(st);
|
visit(statement);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -42,3 +42,28 @@ void NameCollector::operator ()(FunctionDefinition const& _funDef)
|
|||||||
m_names.insert(ret.name);
|
m_names.insert(ret.name);
|
||||||
ASTWalker::operator ()(_funDef);
|
ASTWalker::operator ()(_funDef);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ReferencesCounter::operator()(Identifier const& _identifier)
|
||||||
|
{
|
||||||
|
++m_references[_identifier.name];
|
||||||
|
}
|
||||||
|
|
||||||
|
void ReferencesCounter::operator()(FunctionCall const& _funCall)
|
||||||
|
{
|
||||||
|
++m_references[_funCall.functionName.name];
|
||||||
|
ASTWalker::operator()(_funCall);
|
||||||
|
}
|
||||||
|
|
||||||
|
map<string, size_t> ReferencesCounter::countReferences(Block const& _block)
|
||||||
|
{
|
||||||
|
ReferencesCounter counter;
|
||||||
|
counter(_block);
|
||||||
|
return counter.references();
|
||||||
|
}
|
||||||
|
|
||||||
|
map<string, size_t> ReferencesCounter::countReferences(Expression const& _expression)
|
||||||
|
{
|
||||||
|
ReferencesCounter counter;
|
||||||
|
counter.visit(_expression);
|
||||||
|
return counter.references();
|
||||||
|
}
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
* Specific AST walker that collects all defined names.
|
* Specific AST walkers that collect facts about identifiers and definitions.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
@ -48,5 +48,23 @@ private:
|
|||||||
std::map<std::string, FunctionDefinition const*> m_functions;
|
std::map<std::string, FunctionDefinition const*> m_functions;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specific AST walker that counts all references to all declarations.
|
||||||
|
*/
|
||||||
|
class ReferencesCounter: public ASTWalker
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using ASTWalker::operator ();
|
||||||
|
virtual void operator()(Identifier const& _identifier);
|
||||||
|
virtual void operator()(FunctionCall const& _funCall);
|
||||||
|
|
||||||
|
static std::map<std::string, size_t> countReferences(Block const& _block);
|
||||||
|
static std::map<std::string, size_t> countReferences(Expression const& _expression);
|
||||||
|
|
||||||
|
std::map<std::string, size_t> const& references() const { return m_references; }
|
||||||
|
private:
|
||||||
|
std::map<std::string, size_t> m_references;
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
116
libjulia/optimiser/UnusedPruner.cpp
Normal file
116
libjulia/optimiser/UnusedPruner.cpp
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
/*(
|
||||||
|
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);
|
||||||
|
// Multi-variable declarations are special. We can only remove it
|
||||||
|
// if all vairables are unused and the right-hand-side is either
|
||||||
|
// movable or it return a single value. In the latter case, we
|
||||||
|
// replace `let a := f()` by `pop(f())` (in pure IULIA, this will be
|
||||||
|
// `drop(f())`).
|
||||||
|
if (boost::algorithm::none_of(
|
||||||
|
varDecl.variables,
|
||||||
|
[=](TypedName const& _typedName) { return used(_typedName.name); }
|
||||||
|
))
|
||||||
|
{
|
||||||
|
if (!varDecl.value)
|
||||||
|
statement = Block{std::move(varDecl.location), {}};
|
||||||
|
else if (MovableChecker(*varDecl.value).movable())
|
||||||
|
{
|
||||||
|
subtractReferences(ReferencesCounter::countReferences(*varDecl.value));
|
||||||
|
statement = Block{std::move(varDecl.location), {}};
|
||||||
|
}
|
||||||
|
else if (varDecl.variables.size() == 1)
|
||||||
|
// In pure IULIA, this should be replaced by a function call to `drop`
|
||||||
|
// instead of `pop`.
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
67
libjulia/optimiser/UnusedPruner.h
Normal file
67
libjulia/optimiser/UnusedPruner.h
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
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;
|
||||||
|
|
||||||
|
// @returns true iff the code changed in the previous run.
|
||||||
|
bool shouldRunAgain() const { return m_shouldRunAgain; }
|
||||||
|
|
||||||
|
// Run the pruner until the code does not change anymore.
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
39
libjulia/optimiser/Utilities.cpp
Normal file
39
libjulia/optimiser/Utilities.cpp
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
/*(
|
||||||
|
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/>.
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* Some useful snippets for the optimiser.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <libjulia/optimiser/Utilities.h>
|
||||||
|
|
||||||
|
#include <libsolidity/inlineasm/AsmData.h>
|
||||||
|
|
||||||
|
#include <libdevcore/CommonData.h>
|
||||||
|
|
||||||
|
#include <boost/range/algorithm_ext/erase.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace dev;
|
||||||
|
using namespace dev::julia;
|
||||||
|
|
||||||
|
void dev::julia::removeEmptyBlocks(Block& _block)
|
||||||
|
{
|
||||||
|
auto isEmptyBlock = [](Statement const& _st) -> bool {
|
||||||
|
return _st.type() == typeid(Block) && boost::get<Block>(_st).statements.empty();
|
||||||
|
};
|
||||||
|
boost::range::remove_erase_if(_block.statements, isEmptyBlock);
|
||||||
|
}
|
34
libjulia/optimiser/Utilities.h
Normal file
34
libjulia/optimiser/Utilities.h
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
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/>.
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* Small useful snippets for the optimiser.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <libjulia/ASTDataForward.h>
|
||||||
|
|
||||||
|
namespace dev
|
||||||
|
{
|
||||||
|
namespace julia
|
||||||
|
{
|
||||||
|
|
||||||
|
/// Removes statements that are just empty blocks (non-recursive).
|
||||||
|
void removeEmptyBlocks(Block& _block);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
129
test/libjulia/UnusedPruner.cpp
Normal file
129
test/libjulia/UnusedPruner.cpp
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
/*
|
||||||
|
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(multi_declarations)
|
||||||
|
{
|
||||||
|
CHECK(
|
||||||
|
"{ let x, y }",
|
||||||
|
"{ }"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(multi_assignments)
|
||||||
|
{
|
||||||
|
CHECK(
|
||||||
|
"{ let x, y x := 1 y := 2 }",
|
||||||
|
"{ let x, y x := 1 y := 2 }"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(multi_partial_assignments)
|
||||||
|
{
|
||||||
|
CHECK(
|
||||||
|
"{ let x, y x := 1 }",
|
||||||
|
"{ let x, y x := 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(intermediate_multi_assignment){
|
||||||
|
CHECK(
|
||||||
|
"{ let a, b function f() -> x { } a := f() b := 1 }",
|
||||||
|
"{ let a, b function f() -> x { } a := f() b := 1 }"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
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()
|
Loading…
Reference in New Issue
Block a user