solidity/libyul/optimiser/Semantics.cpp

204 lines
5.9 KiB
C++
Raw Normal View History

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/>.
*/
// 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
#include <libyul/optimiser/OptimizerUtilities.h>
2018-10-15 09:52:35 +00:00
#include <libyul/Exceptions.h>
#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>
#include <libsolutil/CommonData.h>
#include <libsolutil/Algorithms.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
SideEffectsCollector::SideEffectsCollector(
Dialect const& _dialect,
Expression const& _expression,
map<YulString, SideEffects> const* _functionSideEffects
):
SideEffectsCollector(_dialect, _functionSideEffects)
2018-12-20 17:55:32 +00:00
{
visit(_expression);
2018-12-20 17:55:32 +00:00
}
SideEffectsCollector::SideEffectsCollector(Dialect const& _dialect, Statement const& _statement):
SideEffectsCollector(_dialect)
2017-11-22 10:11:48 +00:00
{
visit(_statement);
2017-11-22 10:11:48 +00:00
}
SideEffectsCollector::SideEffectsCollector(
Dialect const& _dialect,
Block const& _ast,
map<YulString, SideEffects> const* _functionSideEffects
):
SideEffectsCollector(_dialect, _functionSideEffects)
{
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
{
operator()(_ast);
2017-11-22 10:11:48 +00:00
}
void SideEffectsCollector::operator()(FunctionCall const& _functionCall)
2017-11-22 10:11:48 +00:00
{
ASTWalker::operator()(_functionCall);
YulString functionName = _functionCall.functionName.name;
if (BuiltinFunction const* f = m_dialect.builtin(functionName))
m_sideEffects += f->sideEffects;
else if (m_functionSideEffects && m_functionSideEffects->count(functionName))
m_sideEffects += m_functionSideEffects->at(functionName);
else
m_sideEffects += SideEffects::worst();
2017-11-22 10:11:48 +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;
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;
}
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};
}
return {ControlFlow::FlowOut, numeric_limits<size_t>::max()};
2019-05-13 09:00:45 +00:00
}
TerminationFinder::ControlFlow TerminationFinder::controlFlowKind(Statement const& _statement)
{
if (
holds_alternative<ExpressionStatement>(_statement) &&
isTerminatingBuiltin(std::get<ExpressionStatement>(_statement))
2019-05-13 09:00:45 +00:00
)
return ControlFlow::Terminate;
else if (holds_alternative<Break>(_statement))
2019-05-13 09:00:45 +00:00
return ControlFlow::Break;
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;
}
bool TerminationFinder::isTerminatingBuiltin(ExpressionStatement const& _exprStmnt)
{
if (holds_alternative<FunctionCall>(_exprStmnt.expression))
if (auto instruction = toEVMInstruction(m_dialect, std::get<FunctionCall>(_exprStmnt.expression).functionName.name))
return evmasm::SemanticInformation::terminatesControlFlow(*instruction);
2019-05-20 12:30:32 +00:00
return false;
2019-05-13 09:00:45 +00:00
}