Coverage Report

Created: 2025-09-08 08:10

/src/solidity/libyul/optimiser/FunctionGrouper.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
 * Optimiser component that changes the code of a block so that all non-function definition
20
 * statements are moved to a block of their own followed by all function definitions.
21
 */
22
23
#include <libyul/optimiser/FunctionGrouper.h>
24
25
#include <libyul/AST.h>
26
27
using namespace solidity;
28
using namespace solidity::yul;
29
30
31
void FunctionGrouper::operator()(Block& _block)
32
2.00M
{
33
2.00M
  if (alreadyGrouped(_block))
34
1.59M
    return;
35
36
408k
  std::vector<Statement> reordered;
37
408k
  reordered.emplace_back(Block{_block.debugData, {}});
38
39
408k
  for (auto&& statement: _block.statements)
40
759k
  {
41
759k
    if (std::holds_alternative<FunctionDefinition>(statement))
42
579k
      reordered.emplace_back(std::move(statement));
43
180k
    else
44
180k
      std::get<Block>(reordered.front()).statements.emplace_back(std::move(statement));
45
759k
  }
46
408k
  _block.statements = std::move(reordered);
47
408k
}
48
49
bool FunctionGrouper::alreadyGrouped(Block const& _block)
50
2.00M
{
51
2.00M
  if (_block.statements.empty())
52
303k
    return false;
53
1.69M
  if (!std::holds_alternative<Block>(_block.statements.front()))
54
104k
    return false;
55
5.25M
  for (size_t i = 1; i < _block.statements.size(); ++i)
56
3.66M
    if (!std::holds_alternative<FunctionDefinition>(_block.statements.at(i)))
57
661
      return false;
58
1.59M
  return true;
59
1.59M
}