Coverage Report

Created: 2022-08-24 06:55

/src/solidity/libyul/optimiser/ConditionalUnsimplifier.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/ConditionalUnsimplifier.h>
19
#include <libyul/optimiser/Semantics.h>
20
#include <libyul/AST.h>
21
#include <libyul/Utilities.h>
22
#include <libyul/optimiser/NameCollector.h>
23
#include <libyul/ControlFlowSideEffectsCollector.h>
24
#include <libsolutil/CommonData.h>
25
26
using namespace std;
27
using namespace solidity;
28
using namespace solidity::yul;
29
using namespace solidity::util;
30
31
void ConditionalUnsimplifier::run(OptimiserStepContext& _context, Block& _ast)
32
334k
{
33
334k
  ConditionalUnsimplifier{
34
334k
    _context.dialect,
35
334k
    ControlFlowSideEffectsCollector{_context.dialect, _ast}.functionSideEffectsNamed()
36
334k
  }(_ast);
37
334k
}
38
39
void ConditionalUnsimplifier::operator()(Switch& _switch)
40
93.7k
{
41
93.7k
  visit(*_switch.expression);
42
93.7k
  if (!holds_alternative<Identifier>(*_switch.expression))
43
7.96k
  {
44
7.96k
    ASTModifier::operator()(_switch);
45
7.96k
    return;
46
7.96k
  }
47
85.7k
  YulString expr = std::get<Identifier>(*_switch.expression).name;
48
85.7k
  for (auto& _case: _switch.cases)
49
276k
  {
50
276k
    if (_case.value)
51
217k
    {
52
217k
      (*this)(*_case.value);
53
217k
      if (
54
217k
        !_case.body.statements.empty() &&
55
217k
        holds_alternative<Assignment>(_case.body.statements.front())
56
217k
      )
57
213k
      {
58
213k
        Assignment const& assignment = std::get<Assignment>(_case.body.statements.front());
59
213k
        if (
60
213k
          assignment.variableNames.size() == 1 &&
61
213k
          assignment.variableNames.front().name == expr &&
62
213k
          holds_alternative<Literal>(*assignment.value) &&
63
213k
          valueOfLiteral(std::get<Literal>(*assignment.value)) == valueOfLiteral(*_case.value)
64
213k
        )
65
210k
          _case.body.statements.erase(_case.body.statements.begin());
66
213k
      }
67
217k
    }
68
276k
    (*this)(_case.body);
69
276k
  }
70
85.7k
}
71
72
void ConditionalUnsimplifier::operator()(Block& _block)
73
5.30M
{
74
5.30M
  walkVector(_block.statements);
75
5.30M
  iterateReplacingWindow<2>(
76
5.30M
    _block.statements,
77
5.30M
    [&](Statement& _stmt1, Statement& _stmt2) -> std::optional<vector<Statement>>
78
34.9M
    {
79
34.9M
      if (holds_alternative<If>(_stmt1))
80
824k
      {
81
824k
        If& _if = std::get<If>(_stmt1);
82
824k
        if (
83
824k
          holds_alternative<Identifier>(*_if.condition) &&
84
824k
          !_if.body.statements.empty()
85
824k
        )
86
776k
        {
87
776k
          YulString condition = std::get<Identifier>(*_if.condition).name;
88
776k
          if (
89
776k
            holds_alternative<Assignment>(_stmt2) &&
90
776k
            TerminationFinder(m_dialect, &m_functionSideEffects).controlFlowKind(_if.body.statements.back()) !=
91
561k
              TerminationFinder::ControlFlow::FlowOut
92
776k
          )
93
561k
          {
94
561k
            Assignment const& assignment = std::get<Assignment>(_stmt2);
95
561k
            if (
96
561k
              assignment.variableNames.size() == 1 &&
97
561k
              assignment.variableNames.front().name == condition &&
98
561k
              holds_alternative<Literal>(*assignment.value) &&
99
561k
              valueOfLiteral(std::get<Literal>(*assignment.value)) == 0
100
561k
            )
101
560k
              return {make_vector<Statement>(std::move(_stmt1))};
102
561k
          }
103
776k
        }
104
824k
      }
105
34.3M
      return {};
106
34.9M
    }
107
5.30M
  );
108
5.30M
}