2019-07-22 07:16:56 +00:00
|
|
|
/*
|
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
|
|
|
/**
|
|
|
|
* Specific AST walkers that collect semantical facts.
|
|
|
|
*/
|
|
|
|
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/optimiser/Semantics.h>
|
2017-11-22 10:11:48 +00:00
|
|
|
|
2021-04-22 10:46:25 +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-12-20 17:55:32 +00:00
|
|
|
#include <libyul/Dialect.h>
|
2017-11-22 10:11:48 +00:00
|
|
|
|
|
|
|
#include <libevmasm/SemanticInformation.h>
|
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
|
|
|
#include <libsolutil/Algorithms.h>
|
2017-11-22 10:11:48 +00:00
|
|
|
|
2021-09-16 14:33:28 +00:00
|
|
|
#include <limits>
|
|
|
|
|
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-08-13 12:40:26 +00:00
|
|
|
|
2019-08-29 13:26:36 +00:00
|
|
|
SideEffectsCollector::SideEffectsCollector(
|
|
|
|
Dialect const& _dialect,
|
|
|
|
Expression const& _expression,
|
|
|
|
map<YulString, SideEffects> const* _functionSideEffects
|
|
|
|
):
|
|
|
|
SideEffectsCollector(_dialect, _functionSideEffects)
|
2018-12-20 17:55:32 +00:00
|
|
|
{
|
2019-05-27 11:11:00 +00:00
|
|
|
visit(_expression);
|
2018-12-20 17:55:32 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:11:00 +00:00
|
|
|
SideEffectsCollector::SideEffectsCollector(Dialect const& _dialect, Statement const& _statement):
|
|
|
|
SideEffectsCollector(_dialect)
|
2017-11-22 10:11:48 +00:00
|
|
|
{
|
2019-05-27 11:11:00 +00:00
|
|
|
visit(_statement);
|
2017-11-22 10:11:48 +00:00
|
|
|
}
|
|
|
|
|
2019-09-02 12:22:53 +00:00
|
|
|
SideEffectsCollector::SideEffectsCollector(
|
|
|
|
Dialect const& _dialect,
|
|
|
|
Block const& _ast,
|
|
|
|
map<YulString, SideEffects> const* _functionSideEffects
|
|
|
|
):
|
|
|
|
SideEffectsCollector(_dialect, _functionSideEffects)
|
2020-07-03 13:10:29 +00:00
|
|
|
{
|
|
|
|
operator()(_ast);
|
|
|
|
}
|
|
|
|
|
|
|
|
SideEffectsCollector::SideEffectsCollector(
|
|
|
|
Dialect const& _dialect,
|
|
|
|
ForLoop const& _ast,
|
|
|
|
map<YulString, SideEffects> const* _functionSideEffects
|
|
|
|
):
|
|
|
|
SideEffectsCollector(_dialect, _functionSideEffects)
|
2017-11-22 10:11:48 +00:00
|
|
|
{
|
2019-05-27 11:11:00 +00:00
|
|
|
operator()(_ast);
|
2017-11-22 10:11:48 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:11:00 +00:00
|
|
|
void SideEffectsCollector::operator()(FunctionCall const& _functionCall)
|
2017-11-22 10:11:48 +00:00
|
|
|
{
|
2019-05-13 16:36:25 +00:00
|
|
|
ASTWalker::operator()(_functionCall);
|
|
|
|
|
2019-08-13 16:38:11 +00:00
|
|
|
YulString functionName = _functionCall.functionName.name;
|
|
|
|
if (BuiltinFunction const* f = m_dialect.builtin(functionName))
|
2019-08-13 12:40:26 +00:00
|
|
|
m_sideEffects += f->sideEffects;
|
2019-08-13 16:38:11 +00:00
|
|
|
else if (m_functionSideEffects && m_functionSideEffects->count(functionName))
|
|
|
|
m_sideEffects += m_functionSideEffects->at(functionName);
|
2019-05-13 16:36:25 +00:00
|
|
|
else
|
2019-08-13 12:40:26 +00:00
|
|
|
m_sideEffects += SideEffects::worst();
|
2017-11-22 10:11:48 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 11:34:33 +00:00
|
|
|
bool MSizeFinder::containsMSize(Dialect const& _dialect, Block const& _ast)
|
|
|
|
{
|
|
|
|
MSizeFinder finder(_dialect);
|
|
|
|
finder(_ast);
|
|
|
|
return finder.m_msizeFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MSizeFinder::operator()(FunctionCall const& _functionCall)
|
|
|
|
{
|
|
|
|
ASTWalker::operator()(_functionCall);
|
|
|
|
|
|
|
|
if (BuiltinFunction const* f = m_dialect.builtin(_functionCall.functionName.name))
|
|
|
|
if (f->isMSize)
|
|
|
|
m_msizeFound = true;
|
|
|
|
}
|
|
|
|
|
2019-08-13 14:25:12 +00:00
|
|
|
map<YulString, SideEffects> SideEffectsPropagator::sideEffects(
|
|
|
|
Dialect const& _dialect,
|
2019-12-02 11:33:24 +00:00
|
|
|
CallGraph const& _directCallGraph
|
2019-08-13 14:25:12 +00:00
|
|
|
)
|
|
|
|
{
|
2019-12-02 11:33:24 +00:00
|
|
|
// Any loop currently makes a function non-movable, because
|
|
|
|
// it could be a non-terminating loop.
|
|
|
|
// The same is true for any function part of a call cycle.
|
|
|
|
// In the future, we should refine that, because the property
|
|
|
|
// is actually a bit different from "not movable".
|
|
|
|
|
2019-08-13 14:25:12 +00:00
|
|
|
map<YulString, SideEffects> ret;
|
2020-07-15 17:50:19 +00:00
|
|
|
for (auto const& function: _directCallGraph.functionsWithLoops + _directCallGraph.recursiveFunctions())
|
2019-12-02 11:33:24 +00:00
|
|
|
{
|
|
|
|
ret[function].movable = false;
|
2020-07-01 10:07:00 +00:00
|
|
|
ret[function].canBeRemoved = false;
|
|
|
|
ret[function].canBeRemovedIfNoMSize = false;
|
|
|
|
ret[function].cannotLoop = false;
|
2019-12-02 11:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto const& call: _directCallGraph.functionCalls)
|
2019-08-13 14:25:12 +00:00
|
|
|
{
|
|
|
|
YulString funName = call.first;
|
|
|
|
SideEffects sideEffects;
|
2021-01-05 13:05:30 +00:00
|
|
|
auto _visit = [&, visited = std::set<YulString>{}](YulString _function, auto&& _recurse) mutable {
|
|
|
|
if (!visited.insert(_function).second)
|
|
|
|
return;
|
|
|
|
if (sideEffects == SideEffects::worst())
|
|
|
|
return;
|
|
|
|
if (BuiltinFunction const* f = _dialect.builtin(_function))
|
|
|
|
sideEffects += f->sideEffects;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ret.count(_function))
|
|
|
|
sideEffects += ret[_function];
|
|
|
|
for (YulString callee: _directCallGraph.functionCalls.at(_function))
|
|
|
|
_recurse(callee, _recurse);
|
2019-08-13 14:25:12 +00:00
|
|
|
}
|
2021-01-05 13:05:30 +00:00
|
|
|
};
|
|
|
|
for (auto const& _v: call.second)
|
|
|
|
_visit(_v, _visit);
|
2019-12-02 11:33:24 +00:00
|
|
|
ret[funName] += sideEffects;
|
2019-08-13 14:25:12 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:11:00 +00:00
|
|
|
MovableChecker::MovableChecker(Dialect const& _dialect, Expression const& _expression):
|
|
|
|
MovableChecker(_dialect)
|
|
|
|
{
|
|
|
|
visit(_expression);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MovableChecker::operator()(Identifier const& _identifier)
|
|
|
|
{
|
|
|
|
SideEffectsCollector::operator()(_identifier);
|
|
|
|
m_variableReferences.emplace(_identifier.name);
|
|
|
|
}
|
|
|
|
|
2017-11-22 10:11:48 +00:00
|
|
|
void MovableChecker::visit(Statement const&)
|
|
|
|
{
|
2018-05-09 09:43:14 +00:00
|
|
|
assertThrow(false, OptimizerException, "Movability for statement requested.");
|
2017-11-22 10:11:48 +00:00
|
|
|
}
|
2019-05-13 09:00:45 +00:00
|
|
|
|
|
|
|
pair<TerminationFinder::ControlFlow, size_t> TerminationFinder::firstUnconditionalControlFlowChange(
|
|
|
|
vector<Statement> const& _statements
|
|
|
|
)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < _statements.size(); ++i)
|
|
|
|
{
|
|
|
|
ControlFlow controlFlow = controlFlowKind(_statements[i]);
|
|
|
|
if (controlFlow != ControlFlow::FlowOut)
|
|
|
|
return {controlFlow, i};
|
|
|
|
}
|
2020-06-02 13:34:28 +00:00
|
|
|
return {ControlFlow::FlowOut, numeric_limits<size_t>::max()};
|
2019-05-13 09:00:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TerminationFinder::ControlFlow TerminationFinder::controlFlowKind(Statement const& _statement)
|
|
|
|
{
|
|
|
|
if (
|
2021-10-05 16:46:49 +00:00
|
|
|
holds_alternative<VariableDeclaration>(_statement) &&
|
|
|
|
std::get<VariableDeclaration>(_statement).value &&
|
|
|
|
containsNonContinuingFunctionCall(*std::get<VariableDeclaration>(_statement).value)
|
|
|
|
)
|
|
|
|
return ControlFlow::Terminate;
|
|
|
|
else if (
|
|
|
|
holds_alternative<Assignment>(_statement) &&
|
|
|
|
containsNonContinuingFunctionCall(*std::get<Assignment>(_statement).value)
|
|
|
|
)
|
|
|
|
return ControlFlow::Terminate;
|
|
|
|
else if (
|
2019-11-19 15:42:49 +00:00
|
|
|
holds_alternative<ExpressionStatement>(_statement) &&
|
2021-10-05 16:46:49 +00:00
|
|
|
containsNonContinuingFunctionCall(std::get<ExpressionStatement>(_statement).expression)
|
2019-05-13 09:00:45 +00:00
|
|
|
)
|
|
|
|
return ControlFlow::Terminate;
|
2019-11-19 15:42:49 +00:00
|
|
|
else if (holds_alternative<Break>(_statement))
|
2019-05-13 09:00:45 +00:00
|
|
|
return ControlFlow::Break;
|
2019-11-19 15:42:49 +00:00
|
|
|
else if (holds_alternative<Continue>(_statement))
|
2019-05-13 09:00:45 +00:00
|
|
|
return ControlFlow::Continue;
|
2019-11-20 12:09:29 +00:00
|
|
|
else if (holds_alternative<Leave>(_statement))
|
2019-10-28 14:25:02 +00:00
|
|
|
return ControlFlow::Leave;
|
2019-05-13 09:00:45 +00:00
|
|
|
else
|
|
|
|
return ControlFlow::FlowOut;
|
|
|
|
}
|
|
|
|
|
2021-10-05 16:46:49 +00:00
|
|
|
bool TerminationFinder::containsNonContinuingFunctionCall(Expression const& _expr)
|
2019-05-13 09:00:45 +00:00
|
|
|
{
|
2021-10-05 16:46:49 +00:00
|
|
|
if (auto functionCall = std::get_if<FunctionCall>(&_expr))
|
|
|
|
{
|
|
|
|
for (auto const& arg: functionCall->arguments)
|
|
|
|
if (containsNonContinuingFunctionCall(arg))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (auto builtin = m_dialect.builtin(functionCall->functionName.name))
|
|
|
|
return !builtin->controlFlowSideEffects.canContinue;
|
|
|
|
else if (m_functionSideEffects && m_functionSideEffects->count(functionCall->functionName.name))
|
|
|
|
return !m_functionSideEffects->at(functionCall->functionName.name).canContinue;
|
|
|
|
}
|
2019-05-20 12:30:32 +00:00
|
|
|
return false;
|
2019-05-13 09:00:45 +00:00
|
|
|
}
|