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/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/Exceptions.h>
|
2018-11-23 10:18:57 +00:00
|
|
|
#include <libyul/AsmData.h>
|
2018-12-20 17:55:32 +00:00
|
|
|
#include <libyul/Dialect.h>
|
2019-05-20 12:30:32 +00:00
|
|
|
#include <libyul/backends/evm/EVMDialect.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
|
|
|
|
|
|
|
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)
|
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;
|
2019-12-02 11:33:24 +00:00
|
|
|
for (auto const& function: _directCallGraph.functionsWithLoops)
|
|
|
|
{
|
|
|
|
ret[function].movable = false;
|
|
|
|
ret[function].sideEffectFree = false;
|
|
|
|
ret[function].sideEffectFreeIfNoMSize = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect recursive functions.
|
|
|
|
for (auto const& call: _directCallGraph.functionCalls)
|
|
|
|
{
|
|
|
|
// TODO we could shortcut the search as soon as we find a
|
|
|
|
// function that has as bad side-effects as we can
|
|
|
|
// ever achieve via recursion.
|
2019-12-11 16:31:36 +00:00
|
|
|
auto search = [&](YulString const& _functionName, util::CycleDetector<YulString>& _cycleDetector, size_t) {
|
2019-12-02 11:33:24 +00:00
|
|
|
for (auto const& callee: _directCallGraph.functionCalls.at(_functionName))
|
|
|
|
if (!_dialect.builtin(callee))
|
|
|
|
if (_cycleDetector.run(callee))
|
|
|
|
return;
|
|
|
|
};
|
2019-12-11 16:31:36 +00:00
|
|
|
if (util::CycleDetector<YulString>(search).run(call.first))
|
2019-12-02 11:33:24 +00:00
|
|
|
{
|
|
|
|
ret[call.first].movable = false;
|
|
|
|
ret[call.first].sideEffectFree = false;
|
|
|
|
ret[call.first].sideEffectFreeIfNoMSize = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto const& call: _directCallGraph.functionCalls)
|
2019-08-13 14:25:12 +00:00
|
|
|
{
|
|
|
|
YulString funName = call.first;
|
|
|
|
SideEffects sideEffects;
|
2019-12-11 16:31:36 +00:00
|
|
|
util::BreadthFirstSearch<YulString>{call.second, {funName}}.run(
|
2019-08-13 14:25:12 +00:00
|
|
|
[&](YulString _function, auto&& _addChild) {
|
|
|
|
if (sideEffects == SideEffects::worst())
|
|
|
|
return;
|
|
|
|
if (BuiltinFunction const* f = _dialect.builtin(_function))
|
|
|
|
sideEffects += f->sideEffects;
|
|
|
|
else
|
2019-12-02 11:33:24 +00:00
|
|
|
{
|
|
|
|
if (ret.count(_function))
|
|
|
|
sideEffects += ret[_function];
|
|
|
|
for (YulString callee: _directCallGraph.functionCalls.at(_function))
|
2019-08-13 14:25:12 +00:00
|
|
|
_addChild(callee);
|
2019-12-02 11:33:24 +00:00
|
|
|
}
|
2019-08-13 14:25:12 +00:00
|
|
|
}
|
|
|
|
);
|
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 (
|
2019-11-19 15:42:49 +00:00
|
|
|
holds_alternative<ExpressionStatement>(_statement) &&
|
|
|
|
isTerminatingBuiltin(std::get<ExpressionStatement>(_statement))
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TerminationFinder::isTerminatingBuiltin(ExpressionStatement const& _exprStmnt)
|
|
|
|
{
|
2019-11-20 11:27:40 +00:00
|
|
|
if (holds_alternative<FunctionCall>(_exprStmnt.expression))
|
2019-05-20 12:30:32 +00:00
|
|
|
if (auto const* dialect = dynamic_cast<EVMDialect const*>(&m_dialect))
|
2019-11-19 15:42:49 +00:00
|
|
|
if (auto const* builtin = dialect->builtin(std::get<FunctionCall>(_exprStmnt.expression).functionName.name))
|
2019-05-20 12:30:32 +00:00
|
|
|
if (builtin->instruction)
|
2019-12-11 16:31:36 +00:00
|
|
|
return evmasm::SemanticInformation::terminatesControlFlow(*builtin->instruction);
|
2019-05-20 12:30:32 +00:00
|
|
|
return false;
|
2019-05-13 09:00:45 +00:00
|
|
|
}
|