solidity/libyul/optimiser/FullInliner.cpp

197 lines
6.4 KiB
C++
Raw Normal View History

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>
2018-10-02 08:46:59 +00:00
#include <libyul/optimiser/Utilities.h>
2018-10-15 09:52:35 +00:00
#include <libyul/Exceptions.h>
2017-12-07 14:43:23 +00:00
#include <libsolidity/inlineasm/AsmData.h>
#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;
using namespace dev::yul;
2017-12-07 14:43:23 +00:00
using namespace dev::solidity;
FullInliner::FullInliner(Block& _ast):
m_ast(_ast)
{
2018-05-09 09:43:14 +00:00
assertThrow(m_ast.statements.size() >= 1, OptimizerException, "");
assertThrow(m_ast.statements.front().type() == typeid(Block), OptimizerException, "");
2017-12-07 14:43:23 +00:00
m_nameDispenser.m_usedNames = NameCollector(m_ast).names();
for (size_t i = 1; i < m_ast.statements.size(); ++i)
{
2018-05-09 09:43:14 +00:00
assertThrow(m_ast.statements.at(i).type() == typeid(FunctionDefinition), OptimizerException, "");
2017-12-07 14:43:23 +00:00
FunctionDefinition& fun = boost::get<FunctionDefinition>(m_ast.statements.at(i));
m_functions[fun.name] = &fun;
m_functionsToVisit.insert(&fun);
}
}
void FullInliner::run()
{
2018-05-09 09:43:14 +00:00
assertThrow(m_ast.statements[0].type() == typeid(Block), OptimizerException, "");
2018-10-02 08:46:59 +00:00
handleBlock("", boost::get<Block>(m_ast.statements[0]));
2017-12-07 14:43:23 +00:00
while (!m_functionsToVisit.empty())
handleFunction(**m_functionsToVisit.begin());
}
void FullInliner::handleFunction(FunctionDefinition& _fun)
{
if (!m_functionsToVisit.count(&_fun))
return;
m_functionsToVisit.erase(&_fun);
2018-10-02 08:46:59 +00:00
handleBlock(_fun.name, _fun.body);
2017-12-07 14:43:23 +00:00
}
2018-10-02 08:46:59 +00:00
void FullInliner::handleBlock(string const& _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
}
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);
if (funCall)
2017-12-07 14:43:23 +00:00
{
2018-10-02 08:46:59 +00:00
FunctionDefinition& fun = m_driver.function(funCall->functionName.name);
m_driver.handleFunction(fun);
2017-12-07 14:43:23 +00:00
2018-10-02 08:46:59 +00:00
// TODO: Insert good heuristic here. Perhaps implement that inside the driver.
bool doInline = funCall->functionName.name != m_currentFunction;
2017-12-07 14:43:23 +00:00
2018-10-02 08:46:59 +00:00
if (doInline)
return performInline(_statement, *funCall, fun);
}
}
2018-10-02 08:46:59 +00:00
return {};
}
2018-10-02 08:46:59 +00:00
vector<Statement> InlineModifier::performInline(Statement& _statement, FunctionCall& _funCall, FunctionDefinition& _function)
2017-12-07 14:43:23 +00:00
{
2018-10-02 08:46:59 +00:00
vector<Statement> newStatements;
map<string, string> variableReplacements;
// helper function to create a new variable that is supposed to model
// an existing variable.
auto newVariable = [&](TypedName const& _existingVariable, Expression* _value) {
string newName = m_nameDispenser.newName(_function.name + "_" + _existingVariable.name);
variableReplacements[_existingVariable.name] = newName;
VariableDeclaration varDecl{_funCall.location, {{_funCall.location, newName, _existingVariable.type}}, {}};
if (_value)
varDecl.value = make_shared<Expression>(std::move(*_value));
newStatements.emplace_back(std::move(varDecl));
};
for (size_t i = 0; i < _funCall.arguments.size(); ++i)
newVariable(_function.parameters[i], &_funCall.arguments[i]);
for (auto const& var: _function.returnVariables)
newVariable(var, nullptr);
Statement newBody = BodyCopier(m_nameDispenser, _function.name + "_", variableReplacements)(_function.body);
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]},
make_shared<Expression>(Identifier{
_assignment.location,
variableReplacements.at(_function.returnVariables[i].name)
})
});
},
[&](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])},
make_shared<Expression>(Identifier{
_varDecl.location,
variableReplacements.at(_function.returnVariables[i].name)
})
});
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
}
string InlineModifier::newName(string const& _prefix)
{
return m_nameDispenser.newName(_prefix);
}
Statement BodyCopier::operator()(VariableDeclaration const& _varDecl)
{
for (auto const& var: _varDecl.variables)
m_variableReplacements[var.name] = m_nameDispenser.newName(m_varNamePrefix + var.name);
return ASTCopier::operator()(_varDecl);
}
Statement BodyCopier::operator()(FunctionDefinition const& _funDef)
{
2018-05-09 09:43:14 +00:00
assertThrow(false, OptimizerException, "Function hoisting has to be done before function inlining.");
2017-12-07 14:43:23 +00:00
return _funDef;
}
string BodyCopier::translateIdentifier(string const& _name)
{
if (m_variableReplacements.count(_name))
return m_variableReplacements.at(_name);
else
return _name;
}