Coverage Report

Created: 2022-08-24 06:32

/src/solidity/libyul/optimiser/ConditionalUnsimplifier.cpp
Line
Count
Source (jump to first uncovered line)
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
0
{
33
0
  ConditionalUnsimplifier{
34
0
    _context.dialect,
35
0
    ControlFlowSideEffectsCollector{_context.dialect, _ast}.functionSideEffectsNamed()
36
0
  }(_ast);
37
0
}
38
39
void ConditionalUnsimplifier::operator()(Switch& _switch)
40
0
{
41
0
  visit(*_switch.expression);
42
0
  if (!holds_alternative<Identifier>(*_switch.expression))
43
0
  {
44
0
    ASTModifier::operator()(_switch);
45
0
    return;
46
0
  }
47
0
  YulString expr = std::get<Identifier>(*_switch.expression).name;
48
0
  for (auto& _case: _switch.cases)
49
0
  {
50
0
    if (_case.value)
51
0
    {
52
0
      (*this)(*_case.value);
53
0
      if (
54
0
        !_case.body.statements.empty() &&
55
0
        holds_alternative<Assignment>(_case.body.statements.front())
56
0
      )
57
0
      {
58
0
        Assignment const& assignment = std::get<Assignment>(_case.body.statements.front());
59
0
        if (
60
0
          assignment.variableNames.size() == 1 &&
61
0
          assignment.variableNames.front().name == expr &&
62
0
          holds_alternative<Literal>(*assignment.value) &&
63
0
          valueOfLiteral(std::get<Literal>(*assignment.value)) == valueOfLiteral(*_case.value)
64
0
        )
65
0
          _case.body.statements.erase(_case.body.statements.begin());
66
0
      }
67
0
    }
68
0
    (*this)(_case.body);
69
0
  }
70
0
}
71
72
void ConditionalUnsimplifier::operator()(Block& _block)
73
0
{
74
0
  walkVector(_block.statements);
75
0
  iterateReplacingWindow<2>(
76
0
    _block.statements,
77
0
    [&](Statement& _stmt1, Statement& _stmt2) -> std::optional<vector<Statement>>
78
0
    {
79
0
      if (holds_alternative<If>(_stmt1))
80
0
      {
81
0
        If& _if = std::get<If>(_stmt1);
82
0
        if (
83
0
          holds_alternative<Identifier>(*_if.condition) &&
84
0
          !_if.body.statements.empty()
85
0
        )
86
0
        {
87
0
          YulString condition = std::get<Identifier>(*_if.condition).name;
88
0
          if (
89
0
            holds_alternative<Assignment>(_stmt2) &&
90
0
            TerminationFinder(m_dialect, &m_functionSideEffects).controlFlowKind(_if.body.statements.back()) !=
91
0
              TerminationFinder::ControlFlow::FlowOut
92
0
          )
93
0
          {
94
0
            Assignment const& assignment = std::get<Assignment>(_stmt2);
95
0
            if (
96
0
              assignment.variableNames.size() == 1 &&
97
0
              assignment.variableNames.front().name == condition &&
98
0
              holds_alternative<Literal>(*assignment.value) &&
99
0
              valueOfLiteral(std::get<Literal>(*assignment.value)) == 0
100
0
            )
101
0
              return {make_vector<Statement>(std::move(_stmt1))};
102
0
          }
103
0
        }
104
0
      }
105
0
      return {};
106
0
    }
107
0
  );
108
0
}