mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
b7adb2aa42
Fixes: #9220
90 lines
2.5 KiB
C++
90 lines
2.5 KiB
C++
/*
|
|
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
|
|
#include <libyul/optimiser/ConditionalSimplifier.h>
|
|
#include <libyul/optimiser/Semantics.h>
|
|
#include <libyul/AsmData.h>
|
|
#include <libyul/Utilities.h>
|
|
#include <libyul/optimiser/NameCollector.h>
|
|
#include <libsolutil/CommonData.h>
|
|
#include <libsolutil/Visitor.h>
|
|
|
|
using namespace std;
|
|
using namespace solidity;
|
|
using namespace solidity::yul;
|
|
using namespace solidity::util;
|
|
|
|
void ConditionalSimplifier::operator()(Switch& _switch)
|
|
{
|
|
visit(*_switch.expression);
|
|
if (!holds_alternative<Identifier>(*_switch.expression))
|
|
{
|
|
ASTModifier::operator()(_switch);
|
|
return;
|
|
}
|
|
YulString expr = std::get<Identifier>(*_switch.expression).name;
|
|
for (auto& _case: _switch.cases)
|
|
{
|
|
if (_case.value)
|
|
{
|
|
(*this)(*_case.value);
|
|
_case.body.statements.insert(_case.body.statements.begin(),
|
|
Assignment{
|
|
_case.body.location,
|
|
{Identifier{_case.body.location, expr}},
|
|
make_unique<Expression>(*_case.value)
|
|
}
|
|
);
|
|
}
|
|
(*this)(_case.body);
|
|
}
|
|
}
|
|
|
|
void ConditionalSimplifier::operator()(Block& _block)
|
|
{
|
|
iterateReplacing(
|
|
_block.statements,
|
|
[&](Statement& _s) -> std::optional<vector<Statement>>
|
|
{
|
|
visit(_s);
|
|
if (holds_alternative<If>(_s))
|
|
{
|
|
If& _if = std::get<If>(_s);
|
|
if (
|
|
holds_alternative<Identifier>(*_if.condition) &&
|
|
!_if.body.statements.empty() &&
|
|
TerminationFinder(m_dialect).controlFlowKind(_if.body.statements.back()) !=
|
|
TerminationFinder::ControlFlow::FlowOut
|
|
)
|
|
{
|
|
YulString condition = std::get<Identifier>(*_if.condition).name;
|
|
langutil::SourceLocation location = _if.location;
|
|
return make_vector<Statement>(
|
|
std::move(_s),
|
|
Assignment{
|
|
location,
|
|
{Identifier{location, condition}},
|
|
make_unique<Expression>(m_dialect.zeroLiteralForType(m_dialect.boolType))
|
|
}
|
|
);
|
|
}
|
|
}
|
|
return {};
|
|
}
|
|
);
|
|
}
|