2017-12-07 14:43:23 +00:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Optimiser component that performs function inlining for arbitrary functions.
|
|
|
|
*/
|
|
|
|
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/optimiser/FullInliner.h>
|
2017-12-07 14:43:23 +00:00
|
|
|
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/optimiser/ASTCopier.h>
|
|
|
|
#include <libyul/optimiser/ASTWalker.h>
|
|
|
|
#include <libyul/optimiser/NameCollector.h>
|
2019-01-15 12:40:10 +00:00
|
|
|
#include <libyul/optimiser/OptimizerUtilities.h>
|
2018-01-22 23:06:55 +00:00
|
|
|
#include <libyul/optimiser/Metrics.h>
|
|
|
|
#include <libyul/optimiser/SSAValueTracker.h>
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/Exceptions.h>
|
2018-11-23 10:18:57 +00:00
|
|
|
#include <libyul/AsmData.h>
|
2017-12-07 14:43:23 +00:00
|
|
|
|
|
|
|
#include <libdevcore/CommonData.h>
|
2018-10-02 08:46:59 +00:00
|
|
|
#include <libdevcore/Visitor.h>
|
2017-12-07 14:43:23 +00:00
|
|
|
|
|
|
|
#include <boost/range/adaptor/reversed.hpp>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dev;
|
2018-11-21 11:42:34 +00:00
|
|
|
using namespace yul;
|
2017-12-07 14:43:23 +00:00
|
|
|
using namespace dev::solidity;
|
|
|
|
|
2018-10-16 19:38:47 +00:00
|
|
|
FullInliner::FullInliner(Block& _ast, NameDispenser& _dispenser):
|
|
|
|
m_ast(_ast), m_nameDispenser(_dispenser)
|
2017-12-07 14:43:23 +00:00
|
|
|
{
|
2018-01-22 23:06:55 +00:00
|
|
|
// Determine constants
|
|
|
|
SSAValueTracker tracker;
|
|
|
|
tracker(m_ast);
|
|
|
|
for (auto const& ssaValue: tracker.values())
|
|
|
|
if (ssaValue.second && ssaValue.second->type() == typeid(Literal))
|
2018-10-29 14:12:02 +00:00
|
|
|
m_constants.emplace(ssaValue.first);
|
2018-01-22 23:06:55 +00:00
|
|
|
|
2018-12-12 17:56:02 +00:00
|
|
|
// Store size of global statements.
|
|
|
|
m_functionSizes[YulString{}] = CodeSize::codeSize(_ast);
|
2018-10-29 14:12:02 +00:00
|
|
|
map<YulString, size_t> references = ReferencesCounter::countReferences(m_ast);
|
2018-10-16 19:38:47 +00:00
|
|
|
for (auto& statement: m_ast.statements)
|
2017-12-07 14:43:23 +00:00
|
|
|
{
|
2018-10-16 19:38:47 +00:00
|
|
|
if (statement.type() != typeid(FunctionDefinition))
|
|
|
|
continue;
|
|
|
|
FunctionDefinition& fun = boost::get<FunctionDefinition>(statement);
|
2017-12-07 14:43:23 +00:00
|
|
|
m_functions[fun.name] = &fun;
|
2018-01-22 23:06:55 +00:00
|
|
|
// Always inline functions that are only called once.
|
|
|
|
if (references[fun.name] == 1)
|
2018-12-12 17:56:02 +00:00
|
|
|
m_singleUse.emplace(fun.name);
|
2018-01-22 23:06:55 +00:00
|
|
|
updateCodeSize(fun);
|
2017-12-07 14:43:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FullInliner::run()
|
|
|
|
{
|
2018-10-16 19:38:47 +00:00
|
|
|
for (auto& statement: m_ast.statements)
|
|
|
|
if (statement.type() == typeid(Block))
|
2018-10-29 14:12:02 +00:00
|
|
|
handleBlock({}, boost::get<Block>(statement));
|
2018-10-02 08:46:59 +00:00
|
|
|
|
2018-01-22 23:06:55 +00:00
|
|
|
// TODO it might be good to determine a visiting order:
|
|
|
|
// first handle functions that are called from many places.
|
2018-10-16 15:21:14 +00:00
|
|
|
for (auto const& fun: m_functions)
|
2018-01-22 23:06:55 +00:00
|
|
|
{
|
2018-10-16 15:21:14 +00:00
|
|
|
handleBlock(fun.second->name, fun.second->body);
|
2018-01-22 23:06:55 +00:00
|
|
|
updateCodeSize(*fun.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-09 15:51:09 +00:00
|
|
|
void FullInliner::updateCodeSize(FunctionDefinition const& _fun)
|
2018-01-22 23:06:55 +00:00
|
|
|
{
|
2019-01-09 15:51:09 +00:00
|
|
|
m_functionSizes[_fun.name] = CodeSize::codeSize(_fun.body);
|
2017-12-07 14:43:23 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 14:12:02 +00:00
|
|
|
void FullInliner::handleBlock(YulString _currentFunctionName, Block& _block)
|
2017-12-07 14:43:23 +00:00
|
|
|
{
|
2018-10-02 08:46:59 +00:00
|
|
|
InlineModifier{*this, m_nameDispenser, _currentFunctionName}(_block);
|
2017-12-07 14:43:23 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 14:12:02 +00:00
|
|
|
bool FullInliner::shallInline(FunctionCall const& _funCall, YulString _callSite)
|
2018-01-22 23:06:55 +00:00
|
|
|
{
|
|
|
|
// No recursive inlining
|
|
|
|
if (_funCall.functionName.name == _callSite)
|
|
|
|
return false;
|
|
|
|
|
2018-12-05 10:53:31 +00:00
|
|
|
FunctionDefinition* calledFunction = function(_funCall.functionName.name);
|
|
|
|
if (!calledFunction)
|
|
|
|
return false;
|
|
|
|
|
2019-01-09 15:51:09 +00:00
|
|
|
// Inline really, really tiny functions
|
|
|
|
size_t size = m_functionSizes.at(calledFunction->name);
|
|
|
|
if (size <= 1)
|
|
|
|
return true;
|
|
|
|
|
2018-12-12 17:56:02 +00:00
|
|
|
// Do not inline into already big functions.
|
2019-01-09 15:51:09 +00:00
|
|
|
if (m_functionSizes.at(_callSite) > 45)
|
2018-12-12 17:56:02 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (m_singleUse.count(calledFunction->name))
|
2018-01-22 23:06:55 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Constant arguments might provide a means for further optimization, so they cause a bonus.
|
|
|
|
bool constantArg = false;
|
|
|
|
for (auto const& argument: _funCall.arguments)
|
|
|
|
if (argument.type() == typeid(Literal) || (
|
|
|
|
argument.type() == typeid(Identifier) &&
|
|
|
|
m_constants.count(boost::get<Identifier>(argument).name)
|
|
|
|
))
|
|
|
|
{
|
|
|
|
constantArg = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-01-09 15:51:09 +00:00
|
|
|
return (size < 6 || (constantArg && size < 12));
|
2018-12-12 17:56:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FullInliner::tentativelyUpdateCodeSize(YulString _function, YulString _callSite)
|
|
|
|
{
|
|
|
|
m_functionSizes.at(_callSite) += m_functionSizes.at(_function);
|
2018-01-22 23:06:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-07 14:43:23 +00:00
|
|
|
void InlineModifier::operator()(Block& _block)
|
|
|
|
{
|
2018-10-02 08:46:59 +00:00
|
|
|
function<boost::optional<vector<Statement>>(Statement&)> f = [&](Statement& _statement) -> boost::optional<vector<Statement>> {
|
|
|
|
visit(_statement);
|
|
|
|
return tryInlineStatement(_statement);
|
|
|
|
};
|
|
|
|
iterateReplacing(_block.statements, f);
|
2017-12-07 14:43:23 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 08:46:59 +00:00
|
|
|
boost::optional<vector<Statement>> InlineModifier::tryInlineStatement(Statement& _statement)
|
2017-12-07 14:43:23 +00:00
|
|
|
{
|
2018-10-02 08:46:59 +00:00
|
|
|
// Only inline for expression statements, assignments and variable declarations.
|
|
|
|
Expression* e = boost::apply_visitor(GenericFallbackReturnsVisitor<Expression*, ExpressionStatement, Assignment, VariableDeclaration>(
|
|
|
|
[](ExpressionStatement& _s) { return &_s.expression; },
|
|
|
|
[](Assignment& _s) { return _s.value.get(); },
|
|
|
|
[](VariableDeclaration& _s) { return _s.value.get(); }
|
|
|
|
), _statement);
|
|
|
|
if (e)
|
2017-12-07 14:43:23 +00:00
|
|
|
{
|
2018-10-02 08:46:59 +00:00
|
|
|
// Only inline direct function calls.
|
|
|
|
FunctionCall* funCall = boost::apply_visitor(GenericFallbackReturnsVisitor<FunctionCall*, FunctionCall&>(
|
|
|
|
[](FunctionCall& _e) { return &_e; }
|
|
|
|
), *e);
|
2018-01-22 23:06:55 +00:00
|
|
|
if (funCall && m_driver.shallInline(*funCall, m_currentFunction))
|
|
|
|
return performInline(_statement, *funCall);
|
2018-01-18 13:40:11 +00:00
|
|
|
}
|
2018-10-02 08:46:59 +00:00
|
|
|
return {};
|
2018-01-18 13:40:11 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:21:14 +00:00
|
|
|
vector<Statement> InlineModifier::performInline(Statement& _statement, FunctionCall& _funCall)
|
2017-12-07 14:43:23 +00:00
|
|
|
{
|
2018-10-02 08:46:59 +00:00
|
|
|
vector<Statement> newStatements;
|
2018-10-29 14:12:02 +00:00
|
|
|
map<YulString, YulString> variableReplacements;
|
2018-10-02 08:46:59 +00:00
|
|
|
|
2018-12-05 10:53:31 +00:00
|
|
|
FunctionDefinition* function = m_driver.function(_funCall.functionName.name);
|
|
|
|
assertThrow(!!function, OptimizerException, "Attempt to inline invalid function.");
|
2018-10-16 15:21:14 +00:00
|
|
|
|
2018-12-12 17:56:02 +00:00
|
|
|
m_driver.tentativelyUpdateCodeSize(function->name, m_currentFunction);
|
|
|
|
|
2018-12-13 14:38:14 +00:00
|
|
|
static Expression const zero{Literal{{}, LiteralKind::Number, YulString{"0"}, {}}};
|
|
|
|
|
2018-10-02 08:46:59 +00:00
|
|
|
// helper function to create a new variable that is supposed to model
|
|
|
|
// an existing variable.
|
|
|
|
auto newVariable = [&](TypedName const& _existingVariable, Expression* _value) {
|
2019-03-04 17:03:30 +00:00
|
|
|
YulString newName = m_nameDispenser.newName(_existingVariable.name);
|
2018-10-02 08:46:59 +00:00
|
|
|
variableReplacements[_existingVariable.name] = newName;
|
|
|
|
VariableDeclaration varDecl{_funCall.location, {{_funCall.location, newName, _existingVariable.type}}, {}};
|
|
|
|
if (_value)
|
2019-01-09 13:05:03 +00:00
|
|
|
varDecl.value = make_unique<Expression>(std::move(*_value));
|
2018-12-13 14:38:14 +00:00
|
|
|
else
|
2019-01-09 13:05:03 +00:00
|
|
|
varDecl.value = make_unique<Expression>(zero);
|
2018-10-02 08:46:59 +00:00
|
|
|
newStatements.emplace_back(std::move(varDecl));
|
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < _funCall.arguments.size(); ++i)
|
2018-12-05 10:53:31 +00:00
|
|
|
newVariable(function->parameters[i], &_funCall.arguments[i]);
|
|
|
|
for (auto const& var: function->returnVariables)
|
2018-10-02 08:46:59 +00:00
|
|
|
newVariable(var, nullptr);
|
|
|
|
|
2019-03-04 17:03:30 +00:00
|
|
|
Statement newBody = BodyCopier(m_nameDispenser, variableReplacements)(function->body);
|
2018-10-02 08:46:59 +00:00
|
|
|
newStatements += std::move(boost::get<Block>(newBody).statements);
|
|
|
|
|
|
|
|
boost::apply_visitor(GenericFallbackVisitor<Assignment, VariableDeclaration>{
|
|
|
|
[&](Assignment& _assignment)
|
2017-12-07 14:43:23 +00:00
|
|
|
{
|
2018-10-02 08:46:59 +00:00
|
|
|
for (size_t i = 0; i < _assignment.variableNames.size(); ++i)
|
|
|
|
newStatements.emplace_back(Assignment{
|
|
|
|
_assignment.location,
|
|
|
|
{_assignment.variableNames[i]},
|
2019-01-09 13:05:03 +00:00
|
|
|
make_unique<Expression>(Identifier{
|
2018-10-02 08:46:59 +00:00
|
|
|
_assignment.location,
|
2018-12-05 10:53:31 +00:00
|
|
|
variableReplacements.at(function->returnVariables[i].name)
|
2018-10-02 08:46:59 +00:00
|
|
|
})
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[&](VariableDeclaration& _varDecl)
|
2017-12-07 14:43:23 +00:00
|
|
|
{
|
2018-10-02 08:46:59 +00:00
|
|
|
for (size_t i = 0; i < _varDecl.variables.size(); ++i)
|
|
|
|
newStatements.emplace_back(VariableDeclaration{
|
|
|
|
_varDecl.location,
|
|
|
|
{std::move(_varDecl.variables[i])},
|
2019-01-09 13:05:03 +00:00
|
|
|
make_unique<Expression>(Identifier{
|
2018-10-02 08:46:59 +00:00
|
|
|
_varDecl.location,
|
2018-12-05 10:53:31 +00:00
|
|
|
variableReplacements.at(function->returnVariables[i].name)
|
2018-10-02 08:46:59 +00:00
|
|
|
})
|
|
|
|
});
|
2017-12-07 14:43:23 +00:00
|
|
|
}
|
2018-10-02 08:46:59 +00:00
|
|
|
// nothing to be done for expression statement
|
|
|
|
}, _statement);
|
|
|
|
return newStatements;
|
2017-12-07 14:43:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Statement BodyCopier::operator()(VariableDeclaration const& _varDecl)
|
|
|
|
{
|
|
|
|
for (auto const& var: _varDecl.variables)
|
2019-03-04 17:03:30 +00:00
|
|
|
m_variableReplacements[var.name] = m_nameDispenser.newName(var.name);
|
2017-12-07 14:43:23 +00:00
|
|
|
return ASTCopier::operator()(_varDecl);
|
|
|
|
}
|
|
|
|
|
2019-01-09 13:05:03 +00:00
|
|
|
Statement BodyCopier::operator()(FunctionDefinition const&)
|
2017-12-07 14:43:23 +00:00
|
|
|
{
|
2018-05-09 09:43:14 +00:00
|
|
|
assertThrow(false, OptimizerException, "Function hoisting has to be done before function inlining.");
|
2019-01-09 13:05:03 +00:00
|
|
|
return {};
|
2017-12-07 14:43:23 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 14:12:02 +00:00
|
|
|
YulString BodyCopier::translateIdentifier(YulString _name)
|
2017-12-07 14:43:23 +00:00
|
|
|
{
|
|
|
|
if (m_variableReplacements.count(_name))
|
|
|
|
return m_variableReplacements.at(_name);
|
|
|
|
else
|
|
|
|
return _name;
|
|
|
|
}
|