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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2018-12-03 16:19:37 +00:00
|
|
|
#include <libyul/optimiser/StructuralSimplifier.h>
|
|
|
|
#include <libyul/optimiser/Semantics.h>
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/AST.h>
|
2019-01-15 12:40:10 +00:00
|
|
|
#include <libyul/Utilities.h>
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
|
|
|
#include <libsolutil/Visitor.h>
|
2018-12-03 16:19:37 +00:00
|
|
|
|
2019-03-05 18:49:34 +00:00
|
|
|
#include <boost/range/algorithm_ext/erase.hpp>
|
|
|
|
|
2018-12-03 16:19:37 +00:00
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::yul;
|
2018-12-03 16:19:37 +00:00
|
|
|
|
2019-10-28 10:39:30 +00:00
|
|
|
using OptionalStatements = std::optional<vector<Statement>>;
|
2019-03-06 10:33:02 +00:00
|
|
|
|
2018-12-03 16:19:37 +00:00
|
|
|
namespace {
|
|
|
|
|
2019-03-06 10:33:02 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
if (matchingCaseBlock)
|
2019-12-11 16:31:36 +00:00
|
|
|
return util::make_vector<Statement>(std::move(*matchingCaseBlock));
|
2019-05-17 07:41:22 +00:00
|
|
|
else
|
2019-10-28 10:39:30 +00:00
|
|
|
return optional<vector<Statement>>{vector<Statement>{}};
|
2019-03-06 10:33:02 +00:00
|
|
|
}
|
|
|
|
|
2018-12-03 16:19:37 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 14:32:50 +00:00
|
|
|
void StructuralSimplifier::run(OptimiserStepContext&, Block& _ast)
|
|
|
|
{
|
|
|
|
StructuralSimplifier{}(_ast);
|
|
|
|
}
|
|
|
|
|
2018-12-03 16:19:37 +00:00
|
|
|
void StructuralSimplifier::operator()(Block& _block)
|
|
|
|
{
|
|
|
|
simplify(_block.statements);
|
2019-02-27 15:55:42 +00:00
|
|
|
}
|
|
|
|
|
2018-12-03 16:19:37 +00:00
|
|
|
void StructuralSimplifier::simplify(std::vector<yul::Statement>& _statements)
|
|
|
|
{
|
2019-12-11 16:31:36 +00:00
|
|
|
util::GenericVisitor visitor{
|
|
|
|
util::VisitorFallback<OptionalStatements>{},
|
2018-12-03 16:19:37 +00:00
|
|
|
[&](If& _ifStmt) -> OptionalStatements {
|
|
|
|
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-10-28 10:39:30 +00:00
|
|
|
if (std::optional<u256> const constExprVal = hasLiteralValue(*_switchStmt.expression))
|
|
|
|
return replaceConstArgSwitch(_switchStmt, constExprVal.value());
|
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)};
|
2019-05-09 19:56:56 +00:00
|
|
|
return {};
|
2018-12-03 16:19:37 +00:00
|
|
|
}
|
2019-11-24 17:46:43 +00:00
|
|
|
};
|
2018-12-03 16:19:37 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
util::iterateReplacing(
|
2018-12-03 16:19:37 +00:00
|
|
|
_statements,
|
|
|
|
[&](Statement& _stmt) -> OptionalStatements
|
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
OptionalStatements result = std::visit(visitor, _stmt);
|
2018-12-03 16:19:37 +00:00
|
|
|
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)
|
|
|
|
{
|
2019-10-28 10:39:30 +00:00
|
|
|
if (std::optional<u256> value = hasLiteralValue(_expression))
|
2019-09-11 17:16:33 +00:00
|
|
|
return *value != 0;
|
|
|
|
else
|
|
|
|
return false;
|
2018-12-03 16:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StructuralSimplifier::expressionAlwaysFalse(Expression const& _expression)
|
|
|
|
{
|
2019-10-28 10:39:30 +00:00
|
|
|
if (std::optional<u256> value = hasLiteralValue(_expression))
|
2019-09-11 17:16:33 +00:00
|
|
|
return *value == 0;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
std::optional<u256> StructuralSimplifier::hasLiteralValue(Expression const& _expression) const
|
2019-09-11 17:16:33 +00:00
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
if (holds_alternative<Literal>(_expression))
|
|
|
|
return valueOfLiteral(std::get<Literal>(_expression));
|
2019-09-11 17:16:33 +00:00
|
|
|
else
|
2019-10-28 10:39:30 +00:00
|
|
|
return std::optional<u256>();
|
2018-12-03 16:19:37 +00:00
|
|
|
}
|