2017-11-22 10:11:48 +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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2017-11-22 10:11:48 +00:00
|
|
|
/**
|
2018-02-06 14:30:36 +00:00
|
|
|
* Optimiser component that performs function inlining inside expressions.
|
2017-11-22 10:11:48 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/optimiser/ExpressionInliner.h>
|
2017-11-22 10:11:48 +00:00
|
|
|
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/optimiser/InlinableExpressionFunctionFinder.h>
|
2019-07-17 16:59:27 +00:00
|
|
|
#include <libyul/optimiser/Metrics.h>
|
|
|
|
#include <libyul/optimiser/NameCollector.h>
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/optimiser/Substitution.h>
|
|
|
|
#include <libyul/optimiser/Semantics.h>
|
2019-09-23 14:32:50 +00:00
|
|
|
#include <libyul/optimiser/OptimiserStep.h>
|
2017-11-22 10:11:48 +00:00
|
|
|
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/AST.h>
|
2017-11-22 10:11:48 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::yul;
|
2017-11-22 10:11:48 +00:00
|
|
|
|
2019-09-23 14:32:50 +00:00
|
|
|
void ExpressionInliner::run(OptimiserStepContext& _context, Block& _ast)
|
2017-11-22 10:11:48 +00:00
|
|
|
{
|
2018-02-06 14:30:36 +00:00
|
|
|
InlinableExpressionFunctionFinder funFinder;
|
2019-09-23 14:32:50 +00:00
|
|
|
funFinder(_ast);
|
|
|
|
ExpressionInliner inliner{_context.dialect, funFinder.inlinableFunctions()};
|
|
|
|
inliner(_ast);
|
2017-11-22 10:11:48 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 14:30:36 +00:00
|
|
|
void ExpressionInliner::operator()(FunctionDefinition& _fun)
|
|
|
|
{
|
|
|
|
ASTModifier::operator()(_fun);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExpressionInliner::visit(Expression& _expression)
|
2017-11-22 10:11:48 +00:00
|
|
|
{
|
|
|
|
ASTModifier::visit(_expression);
|
2019-11-19 15:42:49 +00:00
|
|
|
if (holds_alternative<FunctionCall>(_expression))
|
2017-11-22 10:11:48 +00:00
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
FunctionCall& funCall = std::get<FunctionCall>(_expression);
|
2019-07-17 16:59:27 +00:00
|
|
|
if (!m_inlinableFunctions.count(funCall.functionName.name))
|
|
|
|
return;
|
|
|
|
FunctionDefinition const& fun = *m_inlinableFunctions.at(funCall.functionName.name);
|
2017-11-22 10:11:48 +00:00
|
|
|
|
2019-07-17 16:59:27 +00:00
|
|
|
map<YulString, Expression const*> substitutions;
|
|
|
|
for (size_t i = 0; i < funCall.arguments.size(); i++)
|
2017-11-22 10:11:48 +00:00
|
|
|
{
|
2019-07-17 16:59:27 +00:00
|
|
|
Expression const& arg = funCall.arguments[i];
|
|
|
|
YulString paraName = fun.parameters[i].name;
|
|
|
|
|
|
|
|
if (!SideEffectsCollector(m_dialect, arg).movable())
|
|
|
|
return;
|
|
|
|
|
|
|
|
size_t refs = ReferencesCounter::countReferences(fun.body)[paraName];
|
|
|
|
size_t cost = CodeCost::codeCost(m_dialect, arg);
|
|
|
|
|
|
|
|
if (refs > 1 && cost > 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
substitutions[paraName] = &arg;
|
2017-11-22 10:11:48 +00:00
|
|
|
}
|
2019-07-17 16:59:27 +00:00
|
|
|
|
2019-11-19 15:42:49 +00:00
|
|
|
_expression = Substitution(substitutions).translate(*std::get<Assignment>(fun.body.statements.front()).value);
|
2017-11-22 10:11:48 +00:00
|
|
|
}
|
|
|
|
}
|