Coverage Report

Created: 2022-08-24 06:55

/src/solidity/libyul/optimiser/ExpressionSimplifier.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
/**
19
 * Optimiser component that uses the simplification rules to simplify expressions.
20
 */
21
22
#include <libyul/optimiser/ExpressionSimplifier.h>
23
24
#include <libyul/optimiser/SimplificationRules.h>
25
#include <libyul/optimiser/OptimiserStep.h>
26
#include <libyul/optimiser/OptimizerUtilities.h>
27
#include <libyul/AST.h>
28
#include <libyul/Utilities.h>
29
30
#include <libevmasm/SemanticInformation.h>
31
32
using namespace std;
33
using namespace solidity;
34
using namespace solidity::yul;
35
36
void ExpressionSimplifier::run(OptimiserStepContext& _context, Block& _ast)
37
277k
{
38
277k
  ExpressionSimplifier{_context.dialect}(_ast);
39
277k
}
40
41
void ExpressionSimplifier::visit(Expression& _expression)
42
58.6M
{
43
58.6M
  ASTModifier::visit(_expression);
44
45
59.2M
  while (auto const* match = SimplificationRules::findFirstMatch(
46
58.6M
    _expression,
47
58.6M
    m_dialect,
48
58.6M
    [this](YulString _var) { return variableValue(_var); }
49
58.6M
  ))
50
692k
    _expression = match->action().toExpression(debugDataOf(_expression));
51
52
58.6M
  if (auto* functionCall = get_if<FunctionCall>(&_expression))
53
10.6M
    if (optional<evmasm::Instruction> instruction = toEVMInstruction(m_dialect, functionCall->functionName.name))
54
8.78M
      for (auto op: evmasm::SemanticInformation::readWriteOperations(*instruction))
55
5.79M
        if (op.startParameter && op.lengthParameter)
56
1.07M
        {
57
1.07M
          Expression& startArgument = functionCall->arguments.at(*op.startParameter);
58
1.07M
          Expression const& lengthArgument = functionCall->arguments.at(*op.lengthParameter);
59
1.07M
          if (
60
1.07M
            knownToBeZero(lengthArgument) &&
61
1.07M
            !knownToBeZero(startArgument) &&
62
1.07M
            !holds_alternative<FunctionCall>(startArgument)
63
1.07M
          )
64
6.57k
            startArgument = Literal{debugDataOf(startArgument), LiteralKind::Number, "0"_yulstring, {}};
65
1.07M
        }
66
58.6M
}
67
68
bool ExpressionSimplifier::knownToBeZero(Expression const& _expression) const
69
1.25M
{
70
1.25M
  if (auto const* literal = get_if<Literal>(&_expression))
71
97
    return valueOfLiteral(*literal) == 0;
72
1.25M
  else if (auto const* identifier = get_if<Identifier>(&_expression))
73
1.25M
    return valueOfIdentifier(identifier->name) == 0;
74
0
  else
75
0
    return false;
76
1.25M
}