2018-10-01 16:27:07 +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
|
2018-10-01 16:27:07 +00:00
|
|
|
/**
|
|
|
|
* Optimiser component that undoes what the ExpressionSplitter did, i.e.
|
|
|
|
* it more or less inlines variable declarations.
|
|
|
|
*/
|
|
|
|
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/optimiser/ExpressionJoiner.h>
|
2018-10-01 16:27:07 +00:00
|
|
|
|
2021-11-09 12:25:00 +00:00
|
|
|
#include <libyul/optimiser/FunctionGrouper.h>
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/optimiser/NameCollector.h>
|
2019-01-15 12:40:10 +00:00
|
|
|
#include <libyul/optimiser/OptimizerUtilities.h>
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/Exceptions.h>
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/AST.h>
|
2018-10-01 16:27:07 +00:00
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
2018-10-01 16:27:07 +00:00
|
|
|
|
2021-03-31 18:03:04 +00:00
|
|
|
#include <range/v3/view/reverse.hpp>
|
2018-10-01 16:27:07 +00:00
|
|
|
|
2021-09-16 14:33:28 +00:00
|
|
|
#include <limits>
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::yul;
|
2018-10-01 16:27:07 +00:00
|
|
|
|
2021-11-09 12:25:00 +00:00
|
|
|
void ExpressionJoiner::run(OptimiserStepContext& _context, Block& _ast)
|
2019-09-23 14:32:50 +00:00
|
|
|
{
|
|
|
|
ExpressionJoiner{_ast}(_ast);
|
2021-11-09 12:25:00 +00:00
|
|
|
FunctionGrouper::run(_context, _ast);
|
2019-09-23 14:32:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-01 16:27:07 +00:00
|
|
|
void ExpressionJoiner::operator()(FunctionCall& _funCall)
|
|
|
|
{
|
|
|
|
handleArguments(_funCall.arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExpressionJoiner::operator()(Block& _block)
|
|
|
|
{
|
|
|
|
resetLatestStatementPointer();
|
|
|
|
for (size_t i = 0; i < _block.statements.size(); ++i)
|
|
|
|
{
|
|
|
|
visit(_block.statements[i]);
|
|
|
|
m_currentBlock = &_block;
|
|
|
|
m_latestStatementInBlock = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
removeEmptyBlocks(_block);
|
|
|
|
resetLatestStatementPointer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExpressionJoiner::visit(Expression& _e)
|
|
|
|
{
|
2023-08-23 02:03:44 +00:00
|
|
|
if (std::holds_alternative<Identifier>(_e))
|
2018-10-01 16:27:07 +00:00
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
Identifier const& identifier = std::get<Identifier>(_e);
|
2018-10-26 11:38:53 +00:00
|
|
|
if (isLatestStatementVarDeclJoinable(identifier))
|
2018-10-01 16:27:07 +00:00
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
VariableDeclaration& varDecl = std::get<VariableDeclaration>(*latestStatement());
|
2018-10-01 16:27:07 +00:00
|
|
|
_e = std::move(*varDecl.value);
|
2018-10-26 11:38:53 +00:00
|
|
|
|
2018-10-01 16:27:07 +00:00
|
|
|
// Delete the variable declaration (also get the moved-from structure back into a sane state)
|
|
|
|
*latestStatement() = Block();
|
|
|
|
|
|
|
|
decrementLatestStatementPointer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ASTModifier::visit(_e);
|
|
|
|
}
|
|
|
|
|
|
|
|
ExpressionJoiner::ExpressionJoiner(Block& _ast)
|
|
|
|
{
|
2022-11-10 21:02:36 +00:00
|
|
|
m_references = VariableReferencesCounter::countReferences(_ast);
|
2018-10-01 16:27:07 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 02:03:44 +00:00
|
|
|
void ExpressionJoiner::handleArguments(std::vector<Expression>& _arguments)
|
2018-10-01 16:27:07 +00:00
|
|
|
{
|
|
|
|
// We have to fill from left to right, but we can only
|
|
|
|
// fill if everything to the right is just an identifier
|
|
|
|
// or a literal.
|
|
|
|
// Also we only descend into function calls if everything
|
|
|
|
// on the right is an identifier or literal.
|
|
|
|
|
|
|
|
size_t i = _arguments.size();
|
2021-03-31 18:03:04 +00:00
|
|
|
for (Expression const& arg: _arguments | ranges::views::reverse)
|
2018-10-01 16:27:07 +00:00
|
|
|
{
|
|
|
|
--i;
|
2023-08-23 02:03:44 +00:00
|
|
|
if (!std::holds_alternative<Identifier>(arg) && !std::holds_alternative<Literal>(arg))
|
2018-10-01 16:27:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// i points to the last element that is neither an identifier nor a literal,
|
|
|
|
// or to the first element if all of them are identifiers or literals.
|
|
|
|
|
|
|
|
for (; i < _arguments.size(); ++i)
|
|
|
|
visit(_arguments.at(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExpressionJoiner::decrementLatestStatementPointer()
|
|
|
|
{
|
|
|
|
if (!m_currentBlock)
|
|
|
|
return;
|
|
|
|
if (m_latestStatementInBlock > 0)
|
|
|
|
--m_latestStatementInBlock;
|
|
|
|
else
|
|
|
|
resetLatestStatementPointer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExpressionJoiner::resetLatestStatementPointer()
|
|
|
|
{
|
|
|
|
m_currentBlock = nullptr;
|
2023-08-23 02:03:44 +00:00
|
|
|
m_latestStatementInBlock = std::numeric_limits<size_t>::max();
|
2018-10-01 16:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Statement* ExpressionJoiner::latestStatement()
|
|
|
|
{
|
|
|
|
if (!m_currentBlock)
|
|
|
|
return nullptr;
|
|
|
|
else
|
|
|
|
return &m_currentBlock->statements.at(m_latestStatementInBlock);
|
|
|
|
}
|
|
|
|
|
2018-10-26 11:38:53 +00:00
|
|
|
bool ExpressionJoiner::isLatestStatementVarDeclJoinable(Identifier const& _identifier)
|
2018-10-01 16:27:07 +00:00
|
|
|
{
|
|
|
|
Statement const* statement = latestStatement();
|
2023-08-23 02:03:44 +00:00
|
|
|
if (!statement || !std::holds_alternative<VariableDeclaration>(*statement))
|
2018-10-01 16:27:07 +00:00
|
|
|
return false;
|
2019-11-19 15:42:49 +00:00
|
|
|
VariableDeclaration const& varDecl = std::get<VariableDeclaration>(*statement);
|
2018-10-01 16:27:07 +00:00
|
|
|
if (varDecl.variables.size() != 1 || !varDecl.value)
|
|
|
|
return false;
|
2018-10-26 11:38:53 +00:00
|
|
|
assertThrow(varDecl.variables.size() == 1, OptimizerException, "");
|
|
|
|
assertThrow(varDecl.value, OptimizerException, "");
|
|
|
|
return varDecl.variables.at(0).name == _identifier.name && m_references[_identifier.name] == 1;
|
2018-10-01 16:27:07 +00:00
|
|
|
}
|