solidity/libyul/optimiser/ControlFlowSimplifier.h

83 lines
2.8 KiB
C
Raw Permalink 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/>.
*/
// SPDX-License-Identifier: GPL-3.0
2019-05-09 19:56:56 +00:00
#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
2019-12-11 16:31:36 +00:00
namespace solidity::yul
2019-05-09 19:56:56 +00:00
{
2019-05-20 12:30:32 +00:00
struct Dialect;
2019-09-23 14:32:50 +00:00
struct OptimiserStepContext;
class TypeInfo;
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``
2019-10-24 16:52:54 +00:00
* - remove ``leave`` at the end of a function.
2019-05-09 19:56:56 +00:00
*
* 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;
2019-10-24 16:52:54 +00:00
void operator()(FunctionDefinition& _funDef) override;
void visit(Statement& _st) override;
2019-05-09 19:56:56 +00:00
private:
ControlFlowSimplifier(Dialect const& _dialect, TypeInfo const& _typeInfo):
m_dialect(_dialect),
m_typeInfo(_typeInfo)
{}
2019-09-23 14:32:50 +00:00
2019-05-09 19:56:56 +00:00
void simplify(std::vector<Statement>& _statements);
std::optional<std::vector<Statement>> reduceNoCaseSwitch(Switch& _switchStmt) const;
std::optional<std::vector<Statement>> reduceSingleCaseSwitch(Switch& _switchStmt) const;
2019-05-20 12:30:32 +00:00
Dialect const& m_dialect;
TypeInfo const& m_typeInfo;
size_t m_numBreakStatements = 0;
size_t m_numContinueStatements = 0;
2019-05-09 19:56:56 +00:00
};
}