Coverage Report

Created: 2022-08-24 06:40

/src/solidity/libyul/optimiser/OptimizerUtilities.cpp
Line
Count
Source
1
/*
2
  This file is part of solidity.
3
4
  solidity is free software: you can redistribute it and/or modify
5
  it under the terms of the GNU General Public License as published by
6
  the Free Software Foundation, either version 3 of the License, or
7
  (at your option) any later version.
8
9
  solidity is distributed in the hope that it will be useful,
10
  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
  GNU General Public License for more details.
13
14
  You should have received a copy of the GNU General Public License
15
  along with solidity.  If not, see <http://www.gnu.org/licenses/>.
16
*/
17
// SPDX-License-Identifier: GPL-3.0
18
/**
19
 * Some useful snippets for the optimiser.
20
 */
21
22
#include <libyul/optimiser/OptimizerUtilities.h>
23
24
#include <libyul/backends/evm/EVMDialect.h>
25
26
#include <libyul/Dialect.h>
27
#include <libyul/AST.h>
28
29
#include <liblangutil/Token.h>
30
#include <libsolutil/CommonData.h>
31
32
#include <range/v3/action/remove_if.hpp>
33
34
using namespace std;
35
using namespace solidity;
36
using namespace solidity::langutil;
37
using namespace solidity::util;
38
using namespace solidity::yul;
39
40
void yul::removeEmptyBlocks(Block& _block)
41
11.2M
{
42
29.8M
  auto isEmptyBlock = [](Statement const& _st) -> bool {
43
29.8M
    return holds_alternative<Block>(_st) && std::get<Block>(_st).statements.empty();
44
29.8M
  };
45
11.2M
  ranges::actions::remove_if(_block.statements, isEmptyBlock);
46
11.2M
}
47
48
bool yul::isRestrictedIdentifier(Dialect const& _dialect, YulString const& _identifier)
49
13.2M
{
50
13.2M
  return _identifier.empty() || TokenTraits::isYulKeyword(_identifier.str()) || _dialect.reservedIdentifier(_identifier);
51
13.2M
}
52
53
optional<evmasm::Instruction> yul::toEVMInstruction(Dialect const& _dialect, YulString const& _name)
54
4.84M
{
55
4.84M
  if (auto const* dialect = dynamic_cast<EVMDialect const*>(&_dialect))
56
4.84M
    if (BuiltinFunctionForEVM const* builtin = dialect->builtin(_name))
57
4.26M
      return builtin->instruction;
58
583k
  return nullopt;
59
4.84M
}
60
61
void StatementRemover::operator()(Block& _block)
62
7.68M
{
63
7.68M
  util::iterateReplacing(
64
7.68M
    _block.statements,
65
7.68M
    [&](Statement& _statement) -> std::optional<vector<Statement>>
66
38.2M
    {
67
38.2M
      if (m_toRemove.count(&_statement))
68
23.3k
        return {vector<Statement>{}};
69
38.2M
      else
70
38.2M
        return nullopt;
71
38.2M
    }
72
7.68M
  );
73
7.68M
  ASTModifier::operator()(_block);
74
7.68M
}