Coverage Report

Created: 2026-06-30 07:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/solidity/libyul/optimiser/ExpressionSimplifier.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
/**
19
 * Optimiser component that uses the simplification rules to simplify expressions.
20
 */
21
22
#include <libyul/optimiser/ExpressionSimplifier.h>
23
24
#include <libyul/backends/evm/EVMDialect.h>
25
#include <libyul/optimiser/SimplificationRules.h>
26
#include <libyul/optimiser/OptimiserStep.h>
27
#include <libyul/optimiser/OptimizerUtilities.h>
28
#include <libyul/optimiser/Semantics.h>
29
#include <libyul/AST.h>
30
#include <libyul/Utilities.h>
31
32
#include <libevmasm/SemanticInformation.h>
33
34
using namespace solidity;
35
using namespace solidity::yul;
36
37
void ExpressionSimplifier::run(OptimiserStepContext& _context, Block& _ast)
38
442k
{
39
442k
  ExpressionSimplifier{_context.dialect}(_ast);
40
442k
}
41
42
void ExpressionSimplifier::visit(Expression& _expression)
43
82.2M
{
44
82.2M
  ASTModifier::visit(_expression);
45
46
83.6M
  while (auto const* match = SimplificationRules::findFirstMatch(
47
82.2M
    _expression,
48
82.2M
    m_dialect,
49
92.3M
    [this](YulName const& _var) -> AssignedValue const* {
50
92.3M
      AssignedValue const* value = variableValue(_var);
51
92.3M
      if (!value || !value->value)
52
21.2M
        return nullptr;
53
54
      // check that all variables in the value expression are in current scope
55
71.1M
      MovableChecker const checker(m_dialect, *value->value);
56
71.1M
      for (YulName const& referencedVar: checker.referencedVariables())
57
39.0M
        if (!inScope(referencedVar))
58
3.13k
          return nullptr;  // don't substitute if any referenced var is out of scope
59
60
71.1M
      return value;
61
71.1M
    }
62
82.2M
  ))
63
1.34M
  {
64
1.34M
    auto const* evmDialect = dynamic_cast<EVMDialect const*>(&m_dialect);
65
1.34M
    yulAssert(evmDialect);
66
1.34M
    _expression = match->action().toExpression(debugDataOf(_expression), *evmDialect);
67
1.34M
  }
68
69
82.2M
  if (auto* functionCall = std::get_if<FunctionCall>(&_expression))
70
14.8M
    if (std::optional<evmasm::Instruction> instruction = toEVMInstruction(m_dialect, functionCall->functionName))
71
12.6M
      for (auto op: evmasm::SemanticInformation::readWriteOperations(*instruction))
72
9.14M
        if (op.startParameter && op.lengthParameter)
73
1.47M
        {
74
1.47M
          Expression& startArgument = functionCall->arguments.at(*op.startParameter);
75
1.47M
          Expression const& lengthArgument = functionCall->arguments.at(*op.lengthParameter);
76
1.47M
          if (
77
1.47M
            knownToBeZero(lengthArgument) &&
78
394k
            !knownToBeZero(startArgument) &&
79
16.9k
            !std::holds_alternative<FunctionCall>(startArgument)
80
1.47M
          )
81
16.7k
            startArgument = Literal{debugDataOf(startArgument), LiteralKind::Number, LiteralValue{0, std::nullopt}};
82
1.47M
        }
83
82.2M
}
84
85
bool ExpressionSimplifier::knownToBeZero(Expression const& _expression) const
86
1.87M
{
87
1.87M
  if (auto const* literal = std::get_if<Literal>(&_expression))
88
483k
    return literal->value.value() == 0;
89
1.38M
  else if (auto const* identifier = std::get_if<Identifier>(&_expression))
90
1.38M
    return valueOfIdentifier(identifier->name) == 0;
91
347
  else
92
347
    return false;
93
1.87M
}