Coverage Report

Created: 2022-08-24 06:28

/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
0
{
38
0
  ExpressionSimplifier{_context.dialect}(_ast);
39
0
}
40
41
void ExpressionSimplifier::visit(Expression& _expression)
42
0
{
43
0
  ASTModifier::visit(_expression);
44
45
0
  while (auto const* match = SimplificationRules::findFirstMatch(
46
0
    _expression,
47
0
    m_dialect,
48
0
    [this](YulString _var) { return variableValue(_var); }
49
0
  ))
50
0
    _expression = match->action().toExpression(debugDataOf(_expression));
51
52
0
  if (auto* functionCall = get_if<FunctionCall>(&_expression))
53
0
    if (optional<evmasm::Instruction> instruction = toEVMInstruction(m_dialect, functionCall->functionName.name))
54
0
      for (auto op: evmasm::SemanticInformation::readWriteOperations(*instruction))
55
0
        if (op.startParameter && op.lengthParameter)
56
0
        {
57
0
          Expression& startArgument = functionCall->arguments.at(*op.startParameter);
58
0
          Expression const& lengthArgument = functionCall->arguments.at(*op.lengthParameter);
59
0
          if (
60
0
            knownToBeZero(lengthArgument) &&
61
0
            !knownToBeZero(startArgument) &&
62
0
            !holds_alternative<FunctionCall>(startArgument)
63
0
          )
64
0
            startArgument = Literal{debugDataOf(startArgument), LiteralKind::Number, "0"_yulstring, {}};
65
0
        }
66
0
}
67
68
bool ExpressionSimplifier::knownToBeZero(Expression const& _expression) const
69
0
{
70
0
  if (auto const* literal = get_if<Literal>(&_expression))
71
0
    return valueOfLiteral(*literal) == 0;
72
0
  else if (auto const* identifier = get_if<Identifier>(&_expression))
73
0
    return valueOfIdentifier(identifier->name) == 0;
74
0
  else
75
0
    return false;
76
0
}