2019-10-07 08:06:00 +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
|
2019-10-07 08:06:00 +00:00
|
|
|
#include <libyul/optimiser/ConditionalUnsimplifier.h>
|
|
|
|
#include <libyul/optimiser/Semantics.h>
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/AST.h>
|
2019-10-07 08:06:00 +00:00
|
|
|
#include <libyul/Utilities.h>
|
|
|
|
#include <libyul/optimiser/NameCollector.h>
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
2019-10-07 08:06:00 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::yul;
|
|
|
|
using namespace solidity::util;
|
2019-10-07 08:06:00 +00:00
|
|
|
|
|
|
|
void ConditionalUnsimplifier::operator()(Switch& _switch)
|
|
|
|
{
|
|
|
|
visit(*_switch.expression);
|
2019-11-19 15:42:49 +00:00
|
|
|
if (!holds_alternative<Identifier>(*_switch.expression))
|
2019-10-07 08:06:00 +00:00
|
|
|
{
|
|
|
|
ASTModifier::operator()(_switch);
|
|
|
|
return;
|
|
|
|
}
|
2019-11-19 15:42:49 +00:00
|
|
|
YulString expr = std::get<Identifier>(*_switch.expression).name;
|
2019-10-07 08:06:00 +00:00
|
|
|
for (auto& _case: _switch.cases)
|
|
|
|
{
|
|
|
|
if (_case.value)
|
|
|
|
{
|
|
|
|
(*this)(*_case.value);
|
|
|
|
if (
|
|
|
|
!_case.body.statements.empty() &&
|
2019-11-19 15:42:49 +00:00
|
|
|
holds_alternative<Assignment>(_case.body.statements.front())
|
2019-10-07 08:06:00 +00:00
|
|
|
)
|
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
Assignment const& assignment = std::get<Assignment>(_case.body.statements.front());
|
2019-10-07 08:06:00 +00:00
|
|
|
if (
|
|
|
|
assignment.variableNames.size() == 1 &&
|
|
|
|
assignment.variableNames.front().name == expr &&
|
2019-11-19 15:42:49 +00:00
|
|
|
holds_alternative<Literal>(*assignment.value) &&
|
|
|
|
valueOfLiteral(std::get<Literal>(*assignment.value)) == valueOfLiteral(*_case.value)
|
2019-10-07 08:06:00 +00:00
|
|
|
)
|
|
|
|
_case.body.statements.erase(_case.body.statements.begin());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(*this)(_case.body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConditionalUnsimplifier::operator()(Block& _block)
|
|
|
|
{
|
2019-10-30 16:29:42 +00:00
|
|
|
walkVector(_block.statements);
|
2019-10-07 08:06:00 +00:00
|
|
|
iterateReplacingWindow<2>(
|
|
|
|
_block.statements,
|
|
|
|
[&](Statement& _stmt1, Statement& _stmt2) -> std::optional<vector<Statement>>
|
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
if (holds_alternative<If>(_stmt1))
|
2019-10-07 08:06:00 +00:00
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
If& _if = std::get<If>(_stmt1);
|
2019-10-07 08:06:00 +00:00
|
|
|
if (
|
2019-11-19 15:42:49 +00:00
|
|
|
holds_alternative<Identifier>(*_if.condition) &&
|
2019-10-07 08:06:00 +00:00
|
|
|
!_if.body.statements.empty()
|
|
|
|
)
|
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
YulString condition = std::get<Identifier>(*_if.condition).name;
|
2019-10-07 08:06:00 +00:00
|
|
|
if (
|
2019-11-19 15:42:49 +00:00
|
|
|
holds_alternative<Assignment>(_stmt2) &&
|
2019-10-07 08:06:00 +00:00
|
|
|
TerminationFinder(m_dialect).controlFlowKind(_if.body.statements.back()) !=
|
|
|
|
TerminationFinder::ControlFlow::FlowOut
|
|
|
|
)
|
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
Assignment const& assignment = std::get<Assignment>(_stmt2);
|
2019-10-07 08:06:00 +00:00
|
|
|
if (
|
|
|
|
assignment.variableNames.size() == 1 &&
|
|
|
|
assignment.variableNames.front().name == condition &&
|
2019-11-19 15:42:49 +00:00
|
|
|
holds_alternative<Literal>(*assignment.value) &&
|
|
|
|
valueOfLiteral(std::get<Literal>(*assignment.value)) == 0
|
2019-10-07 08:06:00 +00:00
|
|
|
)
|
|
|
|
return {make_vector<Statement>(std::move(_stmt1))};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|