Coverage Report

Created: 2022-08-24 06:38

/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
284k
{
32
284k
  CircularReferencesPruner{_context.reservedIdentifiers}(_ast);
33
284k
  FunctionGrouper::run(_context, _ast);
34
284k
}
35
36
void CircularReferencesPruner::operator()(Block& _block)
37
284k
{
38
284k
  set<YulString> functionsToKeep =
39
284k
    functionsCalledFromOutermostContext(CallGraphGenerator::callGraph(_block));
40
41
284k
  for (auto&& statement: _block.statements)
42
1.14M
    if (holds_alternative<FunctionDefinition>(statement))
43
860k
    {
44
860k
      FunctionDefinition const& funDef = std::get<FunctionDefinition>(statement);
45
860k
      if (!functionsToKeep.count(funDef.name))
46
385
        statement = Block{};
47
860k
    }
48
49
284k
  removeEmptyBlocks(_block);
50
284k
}
51
52
set<YulString> CircularReferencesPruner::functionsCalledFromOutermostContext(CallGraph const& _callGraph)
53
284k
{
54
284k
  set<YulString> verticesToTraverse = m_reservedIdentifiers;
55
284k
  verticesToTraverse.insert(YulString(""));
56
57
284k
  return util::BreadthFirstSearch<YulString>{{verticesToTraverse.begin(), verticesToTraverse.end()}}.run(
58
1.14M
    [&_callGraph](YulString _function, auto&& _addChild) {
59
1.14M
      if (_callGraph.functionCalls.count(_function))
60
1.14M
        for (auto const& callee: _callGraph.functionCalls.at(_function))
61
3.96M
          if (_callGraph.functionCalls.count(callee))
62
1.53M
            _addChild(callee);
63
1.14M
    }).visited;
64
284k
}