solidity/libyul/optimiser/ControlFlowSimplifier.h

72 lines
2.4 KiB
C
Raw Normal View History

2019-05-09 19:56:56 +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/>.
*/
#pragma once
#include <libyul/optimiser/ASTWalker.h>
2019-09-23 14:32:50 +00:00
#include <libyul/optimiser/OptimiserStep.h>
2019-05-09 19:56:56 +00:00
namespace yul
{
2019-05-20 12:30:32 +00:00
struct Dialect;
2019-09-23 14:32:50 +00:00
struct OptimiserStepContext;
2019-05-09 19:56:56 +00:00
/**
* Simplifies several control-flow structures:
* - replace if with empty body with pop(condition)
* - remove empty default switch case
* - remove empty switch case if no default case exists
* - replace switch with no cases with pop(expression)
* - turn switch with single case into if
* - replace switch with only default case with pop(expression) and body
* - replace switch with const expr with matching case body
* - replace ``for`` with terminating control flow and without other break/continue by ``if``
*
* None of these operations depend on the data flow. The StructuralSimplifier
* performs similar tasks that do depend on data flow.
*
* The ControlFlowSimplifier does record the presence or absence of ``break``
* and ``continue`` statements during its traversal.
*
* Prerequisite: Disambiguator, FunctionHoister, ForLoopInitRewriter.
2019-05-09 19:56:56 +00:00
*
* Important: Introduces EVM opcodes and thus can only be used on EVM code for now.
*/
class ControlFlowSimplifier: public ASTModifier
{
public:
2019-09-23 14:32:50 +00:00
static constexpr char const* name{"ControlFlowSimplifier"};
static void run(OptimiserStepContext&, Block& _ast);
2019-05-20 12:30:32 +00:00
2019-05-09 19:56:56 +00:00
using ASTModifier::operator();
void operator()(Break&) override { ++m_numBreakStatements; }
void operator()(Continue&) override { ++m_numContinueStatements; }
2019-05-09 19:56:56 +00:00
void operator()(Block& _block) override;
void visit(Statement& _st) override;
2019-05-09 19:56:56 +00:00
private:
2019-09-23 14:32:50 +00:00
ControlFlowSimplifier(Dialect const& _dialect): m_dialect(_dialect) {}
2019-05-09 19:56:56 +00:00
void simplify(std::vector<Statement>& _statements);
2019-05-20 12:30:32 +00:00
Dialect const& m_dialect;
size_t m_numBreakStatements = 0;
size_t m_numContinueStatements = 0;
2019-05-09 19:56:56 +00:00
};
}