Coverage Report

Created: 2022-08-24 06:40

/src/solidity/libyul/optimiser/CircularReferencesPruner.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
#include <libyul/optimiser/CircularReferencesPruner.h>
19
20
#include <libyul/optimiser/CallGraphGenerator.h>
21
#include <libyul/optimiser/FunctionGrouper.h>
22
#include <libyul/optimiser/OptimizerUtilities.h>
23
#include <libyul/AST.h>
24
25
#include <libsolutil/Algorithms.h>
26
27
using namespace std;
28
using namespace solidity::yul;
29
30
void CircularReferencesPruner::run(OptimiserStepContext& _context, Block& _ast)
31
236k
{
32
236k
  CircularReferencesPruner{_context.reservedIdentifiers}(_ast);
33
236k
  FunctionGrouper::run(_context, _ast);
34
236k
}
35
36
void CircularReferencesPruner::operator()(Block& _block)
37
236k
{
38
236k
  set<YulString> functionsToKeep =
39
236k
    functionsCalledFromOutermostContext(CallGraphGenerator::callGraph(_block));
40
41
236k
  for (auto&& statement: _block.statements)
42
661k
    if (holds_alternative<FunctionDefinition>(statement))
43
424k
    {
44
424k
      FunctionDefinition const& funDef = std::get<FunctionDefinition>(statement);
45
424k
      if (!functionsToKeep.count(funDef.name))
46
310
        statement = Block{};
47
424k
    }
48
49
236k
  removeEmptyBlocks(_block);
50
236k
}
51
52
set<YulString> CircularReferencesPruner::functionsCalledFromOutermostContext(CallGraph const& _callGraph)
53
236k
{
54
236k
  set<YulString> verticesToTraverse = m_reservedIdentifiers;
55
236k
  verticesToTraverse.insert(YulString(""));
56
57
236k
  return util::BreadthFirstSearch<YulString>{{verticesToTraverse.begin(), verticesToTraverse.end()}}.run(
58
660k
    [&_callGraph](YulString _function, auto&& _addChild) {
59
660k
      if (_callGraph.functionCalls.count(_function))
60
660k
        for (auto const& callee: _callGraph.functionCalls.at(_function))
61
1.90M
          if (_callGraph.functionCalls.count(callee))
62
528k
            _addChild(callee);
63
660k
    }).visited;
64
236k
}