2018-12-03 16:19:37 +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/>.
|
|
|
|
*/
|
|
|
|
#include <libyul/optimiser/StructuralSimplifier.h>
|
|
|
|
#include <libyul/optimiser/Semantics.h>
|
|
|
|
#include <libyul/AsmData.h>
|
2019-01-15 12:40:10 +00:00
|
|
|
#include <libyul/Utilities.h>
|
2018-12-03 16:19:37 +00:00
|
|
|
#include <libdevcore/CommonData.h>
|
|
|
|
#include <libdevcore/Visitor.h>
|
|
|
|
|
2019-03-05 18:49:34 +00:00
|
|
|
#include <boost/range/algorithm_ext/erase.hpp>
|
2019-03-06 10:33:02 +00:00
|
|
|
#include <boost/algorithm/cxx11/any_of.hpp>
|
2019-03-05 18:49:34 +00:00
|
|
|
|
2018-12-03 16:19:37 +00:00
|
|
|
using namespace std;
|
|
|
|
using namespace dev;
|
|
|
|
using namespace yul;
|
|
|
|
|
2019-03-06 10:33:02 +00:00
|
|
|
using OptionalStatements = boost::optional<vector<Statement>>;
|
|
|
|
|
2018-12-03 16:19:37 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
ExpressionStatement makePopExpressionStatement(langutil::SourceLocation const& _location, Expression&& _expression)
|
|
|
|
{
|
|
|
|
return {_location, FunctionalInstruction{
|
|
|
|
_location,
|
|
|
|
solidity::Instruction::POP,
|
|
|
|
{std::move(_expression)}
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2019-03-06 10:33:02 +00:00
|
|
|
void removeEmptyDefaultFromSwitch(Switch& _switchStmt)
|
|
|
|
{
|
|
|
|
boost::remove_erase_if(
|
|
|
|
_switchStmt.cases,
|
|
|
|
[](Case const& _case) { return !_case.value && _case.body.statements.empty(); }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeEmptyCasesFromSwitch(Switch& _switchStmt)
|
|
|
|
{
|
|
|
|
bool hasDefault = boost::algorithm::any_of(
|
|
|
|
_switchStmt.cases,
|
|
|
|
[](Case const& _case) { return !_case.value; }
|
|
|
|
);
|
|
|
|
|
|
|
|
if (hasDefault)
|
|
|
|
return;
|
|
|
|
|
|
|
|
boost::remove_erase_if(
|
|
|
|
_switchStmt.cases,
|
|
|
|
[](Case const& _case) { return _case.body.statements.empty(); }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
OptionalStatements replaceConstArgSwitch(Switch& _switchStmt, u256 const& _constExprVal)
|
|
|
|
{
|
|
|
|
Block* matchingCaseBlock = nullptr;
|
|
|
|
Case* defaultCase = nullptr;
|
|
|
|
|
|
|
|
for (auto& _case: _switchStmt.cases)
|
|
|
|
{
|
|
|
|
if (_case.value && valueOfLiteral(*_case.value) == _constExprVal)
|
|
|
|
{
|
|
|
|
matchingCaseBlock = &_case.body;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (!_case.value)
|
|
|
|
defaultCase = &_case;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!matchingCaseBlock && defaultCase)
|
|
|
|
matchingCaseBlock = &defaultCase->body;
|
|
|
|
|
|
|
|
OptionalStatements s = vector<Statement>{};
|
|
|
|
|
|
|
|
if (matchingCaseBlock)
|
|
|
|
s->emplace_back(std::move(*matchingCaseBlock));
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
OptionalStatements reduceSingleCaseSwitch(Switch& _switchStmt)
|
|
|
|
{
|
|
|
|
yulAssert(_switchStmt.cases.size() == 1, "Expected only one case!");
|
|
|
|
|
|
|
|
auto& switchCase = _switchStmt.cases.front();
|
|
|
|
auto loc = locationOf(*_switchStmt.expression);
|
|
|
|
if (switchCase.value)
|
|
|
|
{
|
|
|
|
OptionalStatements s = vector<Statement>{};
|
|
|
|
s->emplace_back(If{
|
|
|
|
std::move(_switchStmt.location),
|
|
|
|
make_unique<Expression>(FunctionalInstruction{
|
2019-03-06 18:26:26 +00:00
|
|
|
std::move(loc),
|
|
|
|
solidity::Instruction::EQ,
|
|
|
|
{std::move(*switchCase.value), std::move(*_switchStmt.expression)}
|
|
|
|
}),
|
|
|
|
std::move(switchCase.body)
|
|
|
|
});
|
2019-03-06 10:33:02 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
OptionalStatements s = vector<Statement>{};
|
|
|
|
s->emplace_back(makePopExpressionStatement(loc, std::move(*_switchStmt.expression)));
|
|
|
|
s->emplace_back(std::move(switchCase.body));
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-03 16:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StructuralSimplifier::operator()(Block& _block)
|
|
|
|
{
|
|
|
|
pushScope(false);
|
|
|
|
simplify(_block.statements);
|
|
|
|
popScope();
|
|
|
|
}
|
|
|
|
|
2019-03-06 10:33:02 +00:00
|
|
|
OptionalStatements StructuralSimplifier::reduceNoCaseSwitch(Switch& _switchStmt) const
|
|
|
|
{
|
|
|
|
yulAssert(_switchStmt.cases.empty(), "Expected no case!");
|
|
|
|
|
|
|
|
auto loc = locationOf(*_switchStmt.expression);
|
|
|
|
|
|
|
|
OptionalStatements s = vector<Statement>{};
|
|
|
|
|
|
|
|
s->emplace_back(makePopExpressionStatement(loc, std::move(*_switchStmt.expression)));
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-02-27 15:55:42 +00:00
|
|
|
boost::optional<dev::u256> StructuralSimplifier::hasLiteralValue(Expression const& _expression) const
|
|
|
|
{
|
|
|
|
Expression const* expr = &_expression;
|
|
|
|
|
|
|
|
if (expr->type() == typeid(Identifier))
|
|
|
|
{
|
|
|
|
Identifier const& ident = boost::get<Identifier>(*expr);
|
|
|
|
if (m_value.count(ident.name))
|
|
|
|
expr = m_value.at(ident.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expr && expr->type() == typeid(Literal))
|
|
|
|
{
|
|
|
|
Literal const& literal = boost::get<Literal>(*expr);
|
2019-03-05 16:32:13 +00:00
|
|
|
return valueOfLiteral(literal);
|
2019-02-27 15:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return boost::optional<u256>();
|
|
|
|
}
|
|
|
|
|
2018-12-03 16:19:37 +00:00
|
|
|
void StructuralSimplifier::simplify(std::vector<yul::Statement>& _statements)
|
|
|
|
{
|
|
|
|
GenericFallbackReturnsVisitor<OptionalStatements, If, Switch, ForLoop> const visitor(
|
|
|
|
[&](If& _ifStmt) -> OptionalStatements {
|
|
|
|
if (_ifStmt.body.statements.empty())
|
2019-01-09 13:05:03 +00:00
|
|
|
{
|
|
|
|
OptionalStatements s = vector<Statement>{};
|
|
|
|
s->emplace_back(makePopExpressionStatement(_ifStmt.location, std::move(*_ifStmt.condition)));
|
|
|
|
return s;
|
|
|
|
}
|
2018-12-03 16:19:37 +00:00
|
|
|
if (expressionAlwaysTrue(*_ifStmt.condition))
|
|
|
|
return {std::move(_ifStmt.body.statements)};
|
|
|
|
else if (expressionAlwaysFalse(*_ifStmt.condition))
|
2018-12-12 06:51:23 +00:00
|
|
|
return {vector<Statement>{}};
|
2018-12-03 16:19:37 +00:00
|
|
|
return {};
|
|
|
|
},
|
2019-02-27 15:55:42 +00:00
|
|
|
[&](Switch& _switchStmt) -> OptionalStatements {
|
2019-03-06 10:33:02 +00:00
|
|
|
removeEmptyDefaultFromSwitch(_switchStmt);
|
|
|
|
removeEmptyCasesFromSwitch(_switchStmt);
|
2019-03-05 18:41:29 +00:00
|
|
|
|
2019-03-06 10:33:02 +00:00
|
|
|
if (_switchStmt.cases.empty())
|
|
|
|
return reduceNoCaseSwitch(_switchStmt);
|
|
|
|
else if (_switchStmt.cases.size() == 1)
|
|
|
|
return reduceSingleCaseSwitch(_switchStmt);
|
2019-02-27 15:55:42 +00:00
|
|
|
else if (boost::optional<u256> const constExprVal = hasLiteralValue(*_switchStmt.expression))
|
2019-03-06 10:33:02 +00:00
|
|
|
return replaceConstArgSwitch(_switchStmt, constExprVal.get());
|
2019-03-05 18:49:34 +00:00
|
|
|
|
|
|
|
return {};
|
2018-12-03 16:19:37 +00:00
|
|
|
},
|
|
|
|
[&](ForLoop& _forLoop) -> OptionalStatements {
|
|
|
|
if (expressionAlwaysFalse(*_forLoop.condition))
|
|
|
|
return {std::move(_forLoop.pre.statements)};
|
|
|
|
else
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
iterateReplacing(
|
|
|
|
_statements,
|
|
|
|
[&](Statement& _stmt) -> OptionalStatements
|
|
|
|
{
|
|
|
|
OptionalStatements result = boost::apply_visitor(visitor, _stmt);
|
|
|
|
if (result)
|
|
|
|
simplify(*result);
|
2019-03-06 18:26:26 +00:00
|
|
|
else
|
|
|
|
visit(_stmt);
|
2018-12-03 16:19:37 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StructuralSimplifier::expressionAlwaysTrue(Expression const& _expression)
|
|
|
|
{
|
|
|
|
return boost::apply_visitor(GenericFallbackReturnsVisitor<bool, Identifier const, Literal const>(
|
|
|
|
[&](Identifier const& _identifier) -> bool {
|
|
|
|
if (auto expr = m_value[_identifier.name])
|
|
|
|
return expressionAlwaysTrue(*expr);
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
[](Literal const& _literal) -> bool {
|
|
|
|
static YulString const trueString("true");
|
|
|
|
return
|
|
|
|
(_literal.kind == LiteralKind::Boolean && _literal.value == trueString) ||
|
|
|
|
(_literal.kind == LiteralKind::Number && valueOfNumberLiteral(_literal) != u256(0))
|
|
|
|
;
|
|
|
|
}
|
|
|
|
), _expression);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StructuralSimplifier::expressionAlwaysFalse(Expression const& _expression)
|
|
|
|
{
|
|
|
|
return boost::apply_visitor(GenericFallbackReturnsVisitor<bool, Identifier const, Literal const>(
|
|
|
|
[&](Identifier const& _identifier) -> bool {
|
|
|
|
if (auto expr = m_value[_identifier.name])
|
|
|
|
return expressionAlwaysFalse(*expr);
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
[](Literal const& _literal) -> bool {
|
|
|
|
static YulString const falseString("false");
|
|
|
|
return
|
|
|
|
(_literal.kind == LiteralKind::Boolean && _literal.value == falseString) ||
|
|
|
|
(_literal.kind == LiteralKind::Number && valueOfNumberLiteral(_literal) == u256(0))
|
|
|
|
;
|
|
|
|
}
|
|
|
|
), _expression);
|
|
|
|
}
|