From 4f02be110c6181839adfc1145bf07a79a095c711 Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 5 May 2021 18:02:35 +0200 Subject: [PATCH] Unused store eliminator. --- Changelog.md | 1 + libevmasm/SemanticInformation.cpp | 4 +- libsolidity/interface/OptimiserSettings.h | 2 +- libyul/CMakeLists.txt | 2 + libyul/optimiser/Suite.cpp | 3 + libyul/optimiser/UnusedStoreEliminator.cpp | 379 ++++++++++++++++++ libyul/optimiser/UnusedStoreEliminator.h | 119 ++++++ .../constant_optimizer_yul/output | 1 - .../output | 12 +- .../output | 12 +- .../output | 12 +- .../debug_info_in_yul_snippet_escaping/output | 2 - .../output | 2 - .../ir_compiler_subobjects/output | 2 - .../output | 16 +- .../output | 3 - .../keccak_optimization_deploy_code/output | 3 - .../keccak_optimization_low_runs/output | 1 - .../output.json | 27 +- .../output.json | 12 +- .../output.json | 12 +- .../output.json | 12 +- .../output.json | 10 +- .../viair_subobject_optimization/output | 11 - test/cmdlineTests/viair_subobjects/output | 2 - test/formal/redundant_store_unrelated.py | 63 +++ .../array_copy_storage_storage_dyn_dyn.sol | 2 +- .../copying/copy_byte_array_to_storage.sol | 2 +- .../delete/delete_storage_array_packed.sol | 2 +- ...ray_pop_long_storage_empty_garbage_ref.sol | 2 +- .../semanticTests/array/reusing_memory.sol | 2 +- .../constructor_arguments_external.sol | 2 +- .../constructor/no_callvalue_check.sol | 2 +- .../semanticTests/constructor_with_params.sol | 2 +- ...ructor_with_params_diamond_inheritance.sol | 2 +- .../constructor_with_params_inheritance.sol | 2 +- .../creation_function_call_with_args.sol | 2 +- .../creation_function_call_with_salt.sol | 2 +- .../functionCall/failed_create.sol | 2 +- .../functionCall/send_zero_ether.sol | 2 +- .../inheritance/value_for_constructor.sol | 2 +- .../using_library_mappings_public.sol | 2 +- .../salted_create/salted_create.sol | 2 +- .../salted_create_with_value.sol | 2 +- .../semanticTests/smoke/constructor.sol | 2 +- .../structs/struct_copy_via_local.sol | 2 +- ...truct_delete_storage_with_arrays_small.sol | 2 +- .../struct_memory_to_storage_function_ptr.sol | 2 +- .../semanticTests/structs/structs.sol | 2 +- .../calldata_to_storage.sol | 2 +- .../memory_to_storage.sol | 2 +- .../various/contract_binary_dependencies.sol | 2 +- test/libyul/YulOptimizerTestCommon.cpp | 10 + .../fullSuite/extcodelength.yul | 40 ++ .../yulOptimizerTests/fullSuite/medium.yul | 8 +- ...remove_redundant_assignments_in_switch.yul | 1 - .../fullSuite/stack_compressor_msize.yul | 2 +- .../static_array_slot_computation.yul | 1 - .../yulOptimizerTests/fullSuite/storage.yul | 1 - .../unusedFunctionParameterPruner.yul | 1 - .../unusedFunctionParameterPruner_loop.yul | 4 +- ...nusedFunctionParameterPruner_recursion.yul | 1 - .../unusedFunctionParameterPruner_return.yul | 2 - .../unusedFunctionParameterPruner_simple.yul | 4 +- .../call_does_not_need_to_write.yul | 24 ++ .../covering_calldatacopy.yul | 59 +++ .../covering_calldatacopy_fixed.yul | 53 +++ .../unusedStoreEliminator/create.yul | 17 + .../create_inside_function.yul | 22 + .../unusedStoreEliminator/function_end.yul | 20 + .../function_side_effects.yul | 55 +++ .../function_side_effects_2.yul | 28 ++ .../if_overwrite_all_branches.yul | 20 + .../unusedStoreEliminator/leave.yul | 23 ++ .../unusedStoreEliminator/memoryguard.yul | 24 ++ .../unusedStoreEliminator/mload.yul | 20 + .../no_storage_inside_function.yul | 27 ++ .../unusedStoreEliminator/overflow.yul | 18 + .../unusedStoreEliminator/overlapping.yul | 21 + .../overlapping_small.yul | 27 ++ .../remove_before_revert.yul | 21 + .../unaligned_access.yul | 17 + .../unusedStoreEliminator/unknown_length2.yul | 33 ++ .../unrelated_relative.yul | 23 ++ .../write_before_recursion.yul | 27 ++ test/yulPhaser/Chromosome.cpp | 2 +- 86 files changed, 1274 insertions(+), 159 deletions(-) create mode 100644 libyul/optimiser/UnusedStoreEliminator.cpp create mode 100644 libyul/optimiser/UnusedStoreEliminator.h create mode 100644 test/formal/redundant_store_unrelated.py create mode 100644 test/libyul/yulOptimizerTests/fullSuite/extcodelength.yul create mode 100644 test/libyul/yulOptimizerTests/unusedStoreEliminator/call_does_not_need_to_write.yul create mode 100644 test/libyul/yulOptimizerTests/unusedStoreEliminator/covering_calldatacopy.yul create mode 100644 test/libyul/yulOptimizerTests/unusedStoreEliminator/covering_calldatacopy_fixed.yul create mode 100644 test/libyul/yulOptimizerTests/unusedStoreEliminator/create.yul create mode 100644 test/libyul/yulOptimizerTests/unusedStoreEliminator/create_inside_function.yul create mode 100644 test/libyul/yulOptimizerTests/unusedStoreEliminator/function_end.yul create mode 100644 test/libyul/yulOptimizerTests/unusedStoreEliminator/function_side_effects.yul create mode 100644 test/libyul/yulOptimizerTests/unusedStoreEliminator/function_side_effects_2.yul create mode 100644 test/libyul/yulOptimizerTests/unusedStoreEliminator/if_overwrite_all_branches.yul create mode 100644 test/libyul/yulOptimizerTests/unusedStoreEliminator/leave.yul create mode 100644 test/libyul/yulOptimizerTests/unusedStoreEliminator/memoryguard.yul create mode 100644 test/libyul/yulOptimizerTests/unusedStoreEliminator/mload.yul create mode 100644 test/libyul/yulOptimizerTests/unusedStoreEliminator/no_storage_inside_function.yul create mode 100644 test/libyul/yulOptimizerTests/unusedStoreEliminator/overflow.yul create mode 100644 test/libyul/yulOptimizerTests/unusedStoreEliminator/overlapping.yul create mode 100644 test/libyul/yulOptimizerTests/unusedStoreEliminator/overlapping_small.yul create mode 100644 test/libyul/yulOptimizerTests/unusedStoreEliminator/remove_before_revert.yul create mode 100644 test/libyul/yulOptimizerTests/unusedStoreEliminator/unaligned_access.yul create mode 100644 test/libyul/yulOptimizerTests/unusedStoreEliminator/unknown_length2.yul create mode 100644 test/libyul/yulOptimizerTests/unusedStoreEliminator/unrelated_relative.yul create mode 100644 test/libyul/yulOptimizerTests/unusedStoreEliminator/write_before_recursion.yul diff --git a/Changelog.md b/Changelog.md index 1b512d3e9..8b9fa530c 100644 --- a/Changelog.md +++ b/Changelog.md @@ -7,6 +7,7 @@ Language Features: Compiler Features: * JSON-AST: Added selector field for errors and events. * Peephole Optimizer: Optimize comparisons in front of conditional jumps and conditional jumps across a single unconditional jump. + * Yul Optimizer: Remove ``sstore`` and ``mstore`` operations that are never read from. Bugfixes: * Yul IR Code Generation: Optimize embedded creation code with correct settings. This fixes potential mismatches between the constructor code of a contract compiled in isolation and the bytecode in ``type(C).creationCode``, resp. the bytecode used for ``new C(...)``. diff --git a/libevmasm/SemanticInformation.cpp b/libevmasm/SemanticInformation.cpp index 8a1266bcf..acbcaa5c0 100644 --- a/libevmasm/SemanticInformation.cpp +++ b/libevmasm/SemanticInformation.cpp @@ -121,7 +121,9 @@ vector SemanticInformation::readWriteOperations( Location::Memory, Effect::Write, paramCount - 2, - paramCount - 1, + // Length is in paramCount - 1, but it is only a max length, + // there is no guarantee that the full area is written to. + {}, {} }); return operations; diff --git a/libsolidity/interface/OptimiserSettings.h b/libsolidity/interface/OptimiserSettings.h index e1c35e9ea..e233048c1 100644 --- a/libsolidity/interface/OptimiserSettings.h +++ b/libsolidity/interface/OptimiserSettings.h @@ -55,7 +55,7 @@ struct OptimiserSettings "xa[rul]" // Prune a bit more in SSA "xa[r]cL" // Turn into SSA again and simplify "gvif" // Run full inliner - "CTUca[r]LsTFOtfDnca[r]Iulc" // SSA plus simplify + "CTUca[r]LSsTFOtfDnca[r]Iulc" // SSA plus simplify "]" "jmul[jul] VcTOcul jmul"; // Make source short and pretty diff --git a/libyul/CMakeLists.txt b/libyul/CMakeLists.txt index fde673e3d..806f094fe 100644 --- a/libyul/CMakeLists.txt +++ b/libyul/CMakeLists.txt @@ -179,6 +179,8 @@ add_library(yul optimiser/UnusedAssignEliminator.h optimiser/UnusedStoreBase.cpp optimiser/UnusedStoreBase.h + optimiser/UnusedStoreEliminator.cpp + optimiser/UnusedStoreEliminator.h optimiser/Rematerialiser.cpp optimiser/Rematerialiser.h optimiser/SMTSolver.cpp diff --git a/libyul/optimiser/Suite.cpp b/libyul/optimiser/Suite.cpp index 0f2194061..181c9bdcb 100644 --- a/libyul/optimiser/Suite.cpp +++ b/libyul/optimiser/Suite.cpp @@ -57,6 +57,7 @@ #include #include #include +#include #include #include #include @@ -222,6 +223,7 @@ map> const& OptimiserSuite::allSteps() LoadResolver, LoopInvariantCodeMotion, UnusedAssignEliminator, + UnusedStoreEliminator, ReasoningBasedSimplifier, Rematerialiser, SSAReverser, @@ -264,6 +266,7 @@ map const& OptimiserSuite::stepNameToAbbreviationMap() {LoopInvariantCodeMotion::name, 'M'}, {ReasoningBasedSimplifier::name, 'R'}, {UnusedAssignEliminator::name, 'r'}, + {UnusedStoreEliminator::name, 'S'}, {Rematerialiser::name, 'm'}, {SSAReverser::name, 'V'}, {SSATransform::name, 'a'}, diff --git a/libyul/optimiser/UnusedStoreEliminator.cpp b/libyul/optimiser/UnusedStoreEliminator.cpp new file mode 100644 index 000000000..e3ff2d77a --- /dev/null +++ b/libyul/optimiser/UnusedStoreEliminator.cpp @@ -0,0 +1,379 @@ +/* + This file is part of solidity. + + solidity is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + solidity is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with solidity. If not, see . +*/ +// SPDX-License-Identifier: GPL-3.0 +/** + * Optimiser component that removes stores to memory and storage slots that are not used + * or overwritten later on. + */ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include + +#include + +using namespace std; +using namespace solidity; +using namespace solidity::yul; + +/// Variable names for special constants that can never appear in actual Yul code. +static string const zero{"@ 0"}; +static string const one{"@ 1"}; +static string const thirtyTwo{"@ 32"}; + + +void UnusedStoreEliminator::run(OptimiserStepContext& _context, Block& _ast) +{ + map functionSideEffects = SideEffectsPropagator::sideEffects( + _context.dialect, + CallGraphGenerator::callGraph(_ast) + ); + + SSAValueTracker ssaValues; + ssaValues(_ast); + map values; + for (auto const& [name, expression]: ssaValues.values()) + values[name] = AssignedValue{expression, {}}; + Expression const zeroLiteral{Literal{{}, LiteralKind::Number, YulString{"0"}, {}}}; + Expression const oneLiteral{Literal{{}, LiteralKind::Number, YulString{"1"}, {}}}; + Expression const thirtyTwoLiteral{Literal{{}, LiteralKind::Number, YulString{"32"}, {}}}; + values[YulString{zero}] = AssignedValue{&zeroLiteral, {}}; + values[YulString{one}] = AssignedValue{&oneLiteral, {}}; + values[YulString{thirtyTwo}] = AssignedValue{&thirtyTwoLiteral, {}}; + + bool const ignoreMemory = MSizeFinder::containsMSize(_context.dialect, _ast); + UnusedStoreEliminator rse{ + _context.dialect, + functionSideEffects, + ControlFlowSideEffectsCollector{_context.dialect, _ast}.functionSideEffectsNamed(), + values, + ignoreMemory + }; + rse(_ast); + rse.changeUndecidedTo(State::Unused, Location::Memory); + rse.changeUndecidedTo(State::Used, Location::Storage); + rse.scheduleUnusedForDeletion(); + + StatementRemover remover(rse.m_pendingRemovals); + remover(_ast); +} + +void UnusedStoreEliminator::operator()(FunctionCall const& _functionCall) +{ + UnusedStoreBase::operator()(_functionCall); + + for (Operation const& op: operationsFromFunctionCall(_functionCall)) + applyOperation(op); + + ControlFlowSideEffects sideEffects; + if (auto builtin = m_dialect.builtin(_functionCall.functionName.name)) + sideEffects = builtin->controlFlowSideEffects; + else + sideEffects = m_controlFlowSideEffects.at(_functionCall.functionName.name); + + if (!sideEffects.canContinue) + { + changeUndecidedTo(State::Unused, Location::Memory); + changeUndecidedTo(sideEffects.canTerminate ? State::Used : State::Unused, Location::Storage); + } +} + +void UnusedStoreEliminator::operator()(FunctionDefinition const& _functionDefinition) +{ + ScopedSaveAndRestore storeOperations(m_storeOperations, {}); + UnusedStoreBase::operator()(_functionDefinition); +} + + +void UnusedStoreEliminator::operator()(Leave const&) +{ + changeUndecidedTo(State::Used); +} + +void UnusedStoreEliminator::visit(Statement const& _statement) +{ + using evmasm::Instruction; + + UnusedStoreBase::visit(_statement); + + auto const* exprStatement = get_if(&_statement); + if (!exprStatement) + return; + + FunctionCall const* funCall = get_if(&exprStatement->expression); + yulAssert(funCall); + optional instruction = toEVMInstruction(m_dialect, funCall->functionName.name); + if (!instruction) + return; + + if (!ranges::all_of(funCall->arguments, [](Expression const& _expr) -> bool { + return get_if(&_expr) || get_if(&_expr); + })) + return; + + // We determine if this is a store instruction without additional side-effects + // both by querying a combination of semantic information and by listing the instructions. + // This way the assert below should be triggered on any change. + using evmasm::SemanticInformation; + bool isStorageWrite = (*instruction == Instruction::SSTORE); + bool isMemoryWrite = + *instruction == Instruction::EXTCODECOPY || + *instruction == Instruction::CODECOPY || + *instruction == Instruction::CALLDATACOPY || + *instruction == Instruction::RETURNDATACOPY || + *instruction == Instruction::MSTORE || + *instruction == Instruction::MSTORE8; + bool isCandidateForRemoval = + SemanticInformation::otherState(*instruction) != SemanticInformation::Write && ( + SemanticInformation::storage(*instruction) == SemanticInformation::Write || + (!m_ignoreMemory && SemanticInformation::memory(*instruction) == SemanticInformation::Write) + ); + yulAssert(isCandidateForRemoval == (isStorageWrite || (!m_ignoreMemory && isMemoryWrite))); + if (isCandidateForRemoval) + { + m_stores[YulString{}].insert({&_statement, State::Undecided}); + vector operations = operationsFromFunctionCall(*funCall); + yulAssert(operations.size() == 1, ""); + m_storeOperations[&_statement] = move(operations.front()); + } +} + +void UnusedStoreEliminator::finalizeFunctionDefinition(FunctionDefinition const&) +{ + changeUndecidedTo(State::Used); + scheduleUnusedForDeletion(); +} + +vector UnusedStoreEliminator::operationsFromFunctionCall( + FunctionCall const& _functionCall +) const +{ + using evmasm::Instruction; + + YulString functionName = _functionCall.functionName.name; + SideEffects sideEffects; + if (BuiltinFunction const* f = m_dialect.builtin(functionName)) + sideEffects = f->sideEffects; + else + sideEffects = m_functionSideEffects.at(functionName); + + optional instruction = toEVMInstruction(m_dialect, functionName); + if (!instruction) + { + vector result; + // Unknown read is worse than unknown write. + if (sideEffects.memory != SideEffects::Effect::None) + result.emplace_back(Operation{Location::Memory, Effect::Read, {}, {}}); + if (sideEffects.storage != SideEffects::Effect::None) + result.emplace_back(Operation{Location::Storage, Effect::Read, {}, {}}); + return result; + } + + using evmasm::SemanticInformation; + + return util::applyMap( + SemanticInformation::readWriteOperations(*instruction), + [&](SemanticInformation::Operation const& _op) -> Operation + { + yulAssert(!(_op.lengthParameter && _op.lengthConstant)); + yulAssert(_op.effect != Effect::None); + Operation ourOp{_op.location, _op.effect, {}, {}}; + if (_op.startParameter) + ourOp.start = identifierNameIfSSA(_functionCall.arguments.at(*_op.startParameter)); + if (_op.lengthParameter) + ourOp.length = identifierNameIfSSA(_functionCall.arguments.at(*_op.lengthParameter)); + if (_op.lengthConstant) + switch (*_op.lengthConstant) + { + case 1: ourOp.length = YulString(one); break; + case 32: ourOp.length = YulString(thirtyTwo); break; + default: yulAssert(false); + } + return ourOp; + } + ); +} + +void UnusedStoreEliminator::applyOperation(UnusedStoreEliminator::Operation const& _operation) +{ + for (auto& [statement, state]: m_stores[YulString{}]) + if (state == State::Undecided) + { + Operation const& storeOperation = m_storeOperations.at(statement); + if (_operation.effect == Effect::Read && !knownUnrelated(storeOperation, _operation)) + state = State::Used; + else if (_operation.effect == Effect::Write && knownCovered(storeOperation, _operation)) + state = State::Unused; + } +} + +bool UnusedStoreEliminator::knownUnrelated( + UnusedStoreEliminator::Operation const& _op1, + UnusedStoreEliminator::Operation const& _op2 +) const +{ + KnowledgeBase knowledge(m_dialect, m_ssaValues); + + if (_op1.location != _op2.location) + return true; + if (_op1.location == Location::Storage) + { + if (_op1.start && _op2.start) + { + yulAssert( + _op1.length && + _op2.length && + knowledge.valueIfKnownConstant(*_op1.length) == 1 && + knowledge.valueIfKnownConstant(*_op2.length) == 1 + ); + return knowledge.knownToBeDifferent(*_op1.start, *_op2.start); + } + } + else + { + yulAssert(_op1.location == Location::Memory, ""); + if ( + (_op1.length && knowledge.knownToBeZero(*_op1.length)) || + (_op2.length && knowledge.knownToBeZero(*_op2.length)) + ) + return true; + + if (_op1.start && _op1.length && _op2.start) + { + optional length1 = knowledge.valueIfKnownConstant(*_op1.length); + optional start1 = knowledge.valueIfKnownConstant(*_op1.start); + optional start2 = knowledge.valueIfKnownConstant(*_op2.start); + if ( + (length1 && start1 && start2) && + *start1 + *length1 >= *start1 && // no overflow + *start1 + *length1 <= *start2 + ) + return true; + } + if (_op2.start && _op2.length && _op1.start) + { + optional length2 = knowledge.valueIfKnownConstant(*_op2.length); + optional start2 = knowledge.valueIfKnownConstant(*_op2.start); + optional start1 = knowledge.valueIfKnownConstant(*_op1.start); + if ( + (length2 && start2 && start1) && + *start2 + *length2 >= *start2 && // no overflow + *start2 + *length2 <= *start1 + ) + return true; + } + + if (_op1.start && _op1.length && _op2.start && _op2.length) + { + optional length1 = knowledge.valueIfKnownConstant(*_op1.length); + optional length2 = knowledge.valueIfKnownConstant(*_op2.length); + if ( + (length1 && *length1 <= 32) && + (length2 && *length2 <= 32) && + knowledge.knownToBeDifferentByAtLeast32(*_op1.start, *_op2.start) + ) + return true; + } + } + + return false; +} + +bool UnusedStoreEliminator::knownCovered( + UnusedStoreEliminator::Operation const& _covered, + UnusedStoreEliminator::Operation const& _covering +) const +{ + if (_covered.location != _covering.location) + return false; + if ( + (_covered.start && _covered.start == _covering.start) && + (_covered.length && _covered.length == _covering.length) + ) + return true; + if (_covered.location == Location::Memory) + { + KnowledgeBase knowledge(m_dialect, m_ssaValues); + + if (_covered.length && knowledge.knownToBeZero(*_covered.length)) + return true; + + // Condition (i = cover_i_ng, e = cover_e_d): + // i.start <= e.start && e.start + e.length <= i.start + i.length + if (!_covered.start || !_covering.start || !_covered.length || !_covering.length) + return false; + optional coveredLength = knowledge.valueIfKnownConstant(*_covered.length); + optional coveringLength = knowledge.valueIfKnownConstant(*_covering.length); + if (knowledge.knownToBeEqual(*_covered.start, *_covering.start)) + if (coveredLength && coveringLength && *coveredLength <= *coveringLength) + return true; + optional coveredStart = knowledge.valueIfKnownConstant(*_covered.start); + optional coveringStart = knowledge.valueIfKnownConstant(*_covering.start); + if (coveredStart && coveringStart && coveredLength && coveringLength) + if ( + *coveringStart <= *coveredStart && + *coveringStart + *coveringLength >= *coveringStart && // no overflow + *coveredStart + *coveredLength >= *coveredStart && // no overflow + *coveredStart + *coveredLength <= *coveringStart + *coveringLength + ) + return true; + + // TODO for this we probably need a non-overflow assumption as above. + // Condition (i = cover_i_ng, e = cover_e_d): + // i.start <= e.start && e.start + e.length <= i.start + i.length + } + return false; +} + +void UnusedStoreEliminator::changeUndecidedTo( + State _newState, + optional _onlyLocation) +{ + for (auto& [statement, state]: m_stores[YulString{}]) + if ( + state == State::Undecided && + (_onlyLocation == nullopt || *_onlyLocation == m_storeOperations.at(statement).location) + ) + state = _newState; +} + +optional UnusedStoreEliminator::identifierNameIfSSA(Expression const& _expression) const +{ + if (Identifier const* identifier = get_if(&_expression)) + if (m_ssaValues.count(identifier->name)) + return {identifier->name}; + return nullopt; +} + +void UnusedStoreEliminator::scheduleUnusedForDeletion() +{ + for (auto const& [statement, state]: m_stores[YulString{}]) + if (state == State::Unused) + m_pendingRemovals.insert(statement); +} diff --git a/libyul/optimiser/UnusedStoreEliminator.h b/libyul/optimiser/UnusedStoreEliminator.h new file mode 100644 index 000000000..dc3065e45 --- /dev/null +++ b/libyul/optimiser/UnusedStoreEliminator.h @@ -0,0 +1,119 @@ +/* + This file is part of solidity. + + solidity is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + solidity is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with solidity. If not, see . +*/ +// SPDX-License-Identifier: GPL-3.0 +/** + * Optimiser component that removes stores to memory and storage slots that are not used + * or overwritten later on. + */ + +#pragma once + +#include +#include +#include +#include +#include + +#include + +#include +#include + +namespace solidity::yul +{ +struct Dialect; +struct AssignedValue; + +/** + * Optimizer component that removes sstore statements if they + * are overwritten in all code paths or never read from. + * + * The m_store member of UnusedStoreBase is only used with the empty yul string + * as key in the first dimension. + * + * Best run in SSA form. + * + * Prerequisite: Disambiguator, ForLoopInitRewriter. + */ +class UnusedStoreEliminator: public UnusedStoreBase +{ +public: + static constexpr char const* name{"UnusedStoreEliminator"}; + static void run(OptimiserStepContext& _context, Block& _ast); + + explicit UnusedStoreEliminator( + Dialect const& _dialect, + std::map const& _functionSideEffects, + std::map _controlFlowSideEffects, + std::map const& _ssaValues, + bool _ignoreMemory + ): + UnusedStoreBase(_dialect), + m_ignoreMemory(_ignoreMemory), + m_functionSideEffects(_functionSideEffects), + m_controlFlowSideEffects(_controlFlowSideEffects), + m_ssaValues(_ssaValues) + {} + + using UnusedStoreBase::operator(); + void operator()(FunctionCall const& _functionCall) override; + void operator()(FunctionDefinition const&) override; + void operator()(Leave const&) override; + + using UnusedStoreBase::visit; + void visit(Statement const& _statement) override; + + using Location = evmasm::SemanticInformation::Location; + using Effect = evmasm::SemanticInformation::Effect; + struct Operation + { + Location location; + Effect effect; + /// Start of affected area. Unknown if not provided. + std::optional start; + /// Length of affected area, unknown if not provided. + /// Unused for storage. + std::optional length; + }; + +private: + void shortcutNestedLoop(TrackedStores const&) override + { + // We might only need to do this for newly introduced stores in the loop. + changeUndecidedTo(State::Used); + } + void finalizeFunctionDefinition(FunctionDefinition const&) override; + + std::vector operationsFromFunctionCall(FunctionCall const& _functionCall) const; + void applyOperation(Operation const& _operation); + bool knownUnrelated(Operation const& _op1, Operation const& _op2) const; + bool knownCovered(Operation const& _covered, Operation const& _covering) const; + + void changeUndecidedTo(State _newState, std::optional _onlyLocation = std::nullopt); + void scheduleUnusedForDeletion(); + + std::optional identifierNameIfSSA(Expression const& _expression) const; + + bool const m_ignoreMemory; + std::map const& m_functionSideEffects; + std::map m_controlFlowSideEffects; + std::map const& m_ssaValues; + + std::map m_storeOperations; +}; + +} diff --git a/test/cmdlineTests/constant_optimizer_yul/output b/test/cmdlineTests/constant_optimizer_yul/output index 2a769811a..ae0b7a0f6 100644 --- a/test/cmdlineTests/constant_optimizer_yul/output +++ b/test/cmdlineTests/constant_optimizer_yul/output @@ -27,7 +27,6 @@ object "C_12" { code { { /// @src 0:61:418 "contract C {..." - mstore(64, memoryguard(0x80)) if callvalue() { revert(0, 0) } /// @src 0:279:410 "assembly {..." sstore(0, 0x1000000000000000000000000000000000000000000000) diff --git a/test/cmdlineTests/debug_info_in_yul_and_evm_asm_print_all/output b/test/cmdlineTests/debug_info_in_yul_and_evm_asm_print_all/output index 7775ff69c..3b0e1d3ba 100644 --- a/test/cmdlineTests/debug_info_in_yul_and_evm_asm_print_all/output +++ b/test/cmdlineTests/debug_info_in_yul_and_evm_asm_print_all/output @@ -201,16 +201,14 @@ object "C_6" { code { { /// @src 0:60:101 "contract C {..." - let _1 := memoryguard(0x80) - mstore(64, _1) if iszero(lt(calldatasize(), 4)) { - let _2 := 0 - if eq(0x26121ff0, shr(224, calldataload(_2))) + let _1 := 0 + if eq(0x26121ff0, shr(224, calldataload(_1))) { - if callvalue() { revert(_2, _2) } - if slt(add(calldatasize(), not(3)), _2) { revert(_2, _2) } - return(_1, _2) + if callvalue() { revert(_1, _1) } + if slt(add(calldatasize(), not(3)), _1) { revert(_1, _1) } + return(memoryguard(0x80), _1) } } revert(0, 0) diff --git a/test/cmdlineTests/debug_info_in_yul_and_evm_asm_print_location_only/output b/test/cmdlineTests/debug_info_in_yul_and_evm_asm_print_location_only/output index 35f680d92..94d748169 100644 --- a/test/cmdlineTests/debug_info_in_yul_and_evm_asm_print_location_only/output +++ b/test/cmdlineTests/debug_info_in_yul_and_evm_asm_print_location_only/output @@ -200,16 +200,14 @@ object "C_6" { code { { /// @src 0:60:101 - let _1 := memoryguard(0x80) - mstore(64, _1) if iszero(lt(calldatasize(), 4)) { - let _2 := 0 - if eq(0x26121ff0, shr(224, calldataload(_2))) + let _1 := 0 + if eq(0x26121ff0, shr(224, calldataload(_1))) { - if callvalue() { revert(_2, _2) } - if slt(add(calldatasize(), not(3)), _2) { revert(_2, _2) } - return(_1, _2) + if callvalue() { revert(_1, _1) } + if slt(add(calldatasize(), not(3)), _1) { revert(_1, _1) } + return(memoryguard(0x80), _1) } } revert(0, 0) diff --git a/test/cmdlineTests/debug_info_in_yul_and_evm_asm_print_none/output b/test/cmdlineTests/debug_info_in_yul_and_evm_asm_print_none/output index 93fad0421..706b8f773 100644 --- a/test/cmdlineTests/debug_info_in_yul_and_evm_asm_print_none/output +++ b/test/cmdlineTests/debug_info_in_yul_and_evm_asm_print_none/output @@ -189,16 +189,14 @@ object "C_6" { object "C_6_deployed" { code { { - let _1 := memoryguard(0x80) - mstore(64, _1) if iszero(lt(calldatasize(), 4)) { - let _2 := 0 - if eq(0x26121ff0, shr(224, calldataload(_2))) + let _1 := 0 + if eq(0x26121ff0, shr(224, calldataload(_1))) { - if callvalue() { revert(_2, _2) } - if slt(add(calldatasize(), not(3)), _2) { revert(_2, _2) } - return(_1, _2) + if callvalue() { revert(_1, _1) } + if slt(add(calldatasize(), not(3)), _1) { revert(_1, _1) } + return(memoryguard(0x80), _1) } } revert(0, 0) diff --git a/test/cmdlineTests/debug_info_in_yul_snippet_escaping/output b/test/cmdlineTests/debug_info_in_yul_snippet_escaping/output index 82a74384e..1408b6021 100644 --- a/test/cmdlineTests/debug_info_in_yul_snippet_escaping/output +++ b/test/cmdlineTests/debug_info_in_yul_snippet_escaping/output @@ -95,7 +95,6 @@ object "C_2" { code { { /// @src 0:265:278 "contract C {}" - mstore(64, memoryguard(0x80)) revert(0, 0) } } @@ -559,7 +558,6 @@ object "D_27" { code { { /// @src 0:265:278 "contract C {}" - mstore(64, memoryguard(0x80)) revert(0, 0) } } diff --git a/test/cmdlineTests/ir_compiler_inheritance_nosubobjects/output b/test/cmdlineTests/ir_compiler_inheritance_nosubobjects/output index 330e208f4..5246a50ee 100644 --- a/test/cmdlineTests/ir_compiler_inheritance_nosubobjects/output +++ b/test/cmdlineTests/ir_compiler_inheritance_nosubobjects/output @@ -24,7 +24,6 @@ object "C_7" { code { { /// @src 0:82:117 "contract C {..." - mstore(64, memoryguard(0x80)) revert(0, 0) } } @@ -58,7 +57,6 @@ object "D_10" { code { { /// @src 0:118:137 "contract D is C {..." - mstore(64, memoryguard(0x80)) revert(0, 0) } } diff --git a/test/cmdlineTests/ir_compiler_subobjects/output b/test/cmdlineTests/ir_compiler_subobjects/output index fbedf38db..80d90aac5 100644 --- a/test/cmdlineTests/ir_compiler_subobjects/output +++ b/test/cmdlineTests/ir_compiler_subobjects/output @@ -24,7 +24,6 @@ object "C_3" { code { { /// @src 0:82:95 "contract C {}" - mstore(64, memoryguard(0x80)) revert(0, 0) } } @@ -110,7 +109,6 @@ object "D_16" { code { { /// @src 0:82:95 "contract C {}" - mstore(64, memoryguard(0x80)) revert(0, 0) } } diff --git a/test/cmdlineTests/ir_with_assembly_no_memoryguard_creation/output b/test/cmdlineTests/ir_with_assembly_no_memoryguard_creation/output index d9b769a12..90c7ddf65 100644 --- a/test/cmdlineTests/ir_with_assembly_no_memoryguard_creation/output +++ b/test/cmdlineTests/ir_with_assembly_no_memoryguard_creation/output @@ -11,11 +11,7 @@ object "D_12" { code { { /// @src 0:82:175 "contract D {..." - mstore(64, 128) if callvalue() { revert(0, 0) } - /// @src 0:115:139 "assembly { mstore(0,0) }" - mstore(0, 0) - /// @src 0:82:175 "contract D {..." let _1 := datasize("D_12_deployed") codecopy(128, dataoffset("D_12_deployed"), _1) return(128, _1) @@ -26,16 +22,14 @@ object "D_12" { code { { /// @src 0:82:175 "contract D {..." - let _1 := memoryguard(0x80) - mstore(64, _1) if iszero(lt(calldatasize(), 4)) { - let _2 := 0 - if eq(0x26121ff0, shr(224, calldataload(_2))) + let _1 := 0 + if eq(0x26121ff0, shr(224, calldataload(_1))) { - if callvalue() { revert(_2, _2) } - if slt(add(calldatasize(), not(3)), _2) { revert(_2, _2) } - return(_1, _2) + if callvalue() { revert(_1, _1) } + if slt(add(calldatasize(), not(3)), _1) { revert(_1, _1) } + return(memoryguard(0x80), _1) } } revert(0, 0) diff --git a/test/cmdlineTests/ir_with_assembly_no_memoryguard_runtime/output b/test/cmdlineTests/ir_with_assembly_no_memoryguard_runtime/output index 9a07c7861..7738519c4 100644 --- a/test/cmdlineTests/ir_with_assembly_no_memoryguard_runtime/output +++ b/test/cmdlineTests/ir_with_assembly_no_memoryguard_runtime/output @@ -24,7 +24,6 @@ object "D_8" { code { { /// @src 0:82:166 "contract D {..." - mstore(64, 128) if iszero(lt(calldatasize(), 4)) { let _1 := 0 @@ -32,8 +31,6 @@ object "D_8" { { if callvalue() { revert(_1, _1) } if slt(add(calldatasize(), not(3)), _1) { revert(_1, _1) } - /// @src 0:134:158 "assembly { mstore(0,0) }" - mstore(/** @src 0:82:166 "contract D {..." */ _1, _1) return(128, _1) } } diff --git a/test/cmdlineTests/keccak_optimization_deploy_code/output b/test/cmdlineTests/keccak_optimization_deploy_code/output index 4abb046f6..b70a029fc 100644 --- a/test/cmdlineTests/keccak_optimization_deploy_code/output +++ b/test/cmdlineTests/keccak_optimization_deploy_code/output @@ -11,7 +11,6 @@ object "C_12" { code { { /// @src 0:62:463 "contract C {..." - mstore(64, 128) if callvalue() { revert(0, 0) } /// @src 0:103:275 "assembly {..." mstore(0, 100) @@ -27,10 +26,8 @@ object "C_12" { code { { /// @src 0:62:463 "contract C {..." - mstore(64, 128) if callvalue() { revert(0, 0) } /// @src 0:317:454 "assembly {..." - mstore(0, 100) sstore(0, 17385872270140913825666367956517731270094621555228275961425792378517567244498) /// @src 0:62:463 "contract C {..." stop() diff --git a/test/cmdlineTests/keccak_optimization_low_runs/output b/test/cmdlineTests/keccak_optimization_low_runs/output index f5df14027..691e37145 100644 --- a/test/cmdlineTests/keccak_optimization_low_runs/output +++ b/test/cmdlineTests/keccak_optimization_low_runs/output @@ -24,7 +24,6 @@ object "C_7" { code { { /// @src 0:62:285 "contract C {..." - mstore(64, 128) if callvalue() { revert(0, 0) } /// @src 0:109:277 "assembly {..." mstore(0, 100) diff --git a/test/cmdlineTests/standard_debug_info_in_evm_asm_via_ir_location/output.json b/test/cmdlineTests/standard_debug_info_in_evm_asm_via_ir_location/output.json index 238934a3f..f7d61a7b1 100644 --- a/test/cmdlineTests/standard_debug_info_in_evm_asm_via_ir_location/output.json +++ b/test/cmdlineTests/standard_debug_info_in_evm_asm_via_ir_location/output.json @@ -1,8 +1,5 @@ {"contracts":{"C":{"C":{"evm":{"assembly":" /* \"C\":79:428 contract C... */ 0xa0 - dup1 - 0x40 - mstore jumpi(tag_6, callvalue) 0x1f bytecodeSize @@ -438,9 +435,6 @@ sub_0: assembly { } "}}},"D":{"D":{"evm":{"assembly":" /* \"D\":91:166 contract D is C(3)... */ 0xa0 - dup1 - 0x40 - mstore jumpi(tag_6, callvalue) 0x1f bytecodeSize @@ -514,13 +508,8 @@ tag_4: tag_1: /* \"C\":147:149 42 */ mstore(0x80, 0x2a) - /* \"D\":107:108 3 */ - 0x03 - /* \"C\":203:219 stateVar = _init */ - 0x00 - /* \"D\":91:166 contract D is C(3)... */ - sstore sub(shl(0xff, 0x01), 0x04) + /* \"D\":91:166 contract D is C(3)... */ dup2 sgt 0x01 @@ -531,9 +520,7 @@ tag_1: 0x03 /* \"D\":91:166 contract D is C(3)... */ add - /* \"C\":203:219 stateVar = _init */ 0x00 - /* \"D\":91:166 contract D is C(3)... */ sstore /* \"D\":113:164 constructor(int _init2)... */ jump\t// out @@ -541,17 +528,9 @@ tag_1: tag_9: pop pop - shl(0xe0, 0x4e487b71) - /* \"C\":203:219 stateVar = _init */ - 0x00 - /* \"D\":91:166 contract D is C(3)... */ - mstore + mstore(0x00, shl(0xe0, 0x4e487b71)) mstore(0x04, 0x11) - 0x24 - /* \"C\":203:219 stateVar = _init */ - 0x00 - /* \"D\":91:166 contract D is C(3)... */ - revert + revert(0x00, 0x24) stop sub_0: assembly { diff --git a/test/cmdlineTests/standard_debug_info_in_yul_and_evm_asm_print_all/output.json b/test/cmdlineTests/standard_debug_info_in_yul_and_evm_asm_print_all/output.json index 16236251d..44c143b71 100644 --- a/test/cmdlineTests/standard_debug_info_in_yul_and_evm_asm_print_all/output.json +++ b/test/cmdlineTests/standard_debug_info_in_yul_and_evm_asm_print_all/output.json @@ -206,16 +206,14 @@ object \"C_6\" { code { { /// @src 0:60:101 \"contract C {...\" - let _1 := memoryguard(0x80) - mstore(64, _1) if iszero(lt(calldatasize(), 4)) { - let _2 := 0 - if eq(0x26121ff0, shr(224, calldataload(_2))) + let _1 := 0 + if eq(0x26121ff0, shr(224, calldataload(_1))) { - if callvalue() { revert(_2, _2) } - if slt(add(calldatasize(), not(3)), _2) { revert(_2, _2) } - return(_1, _2) + if callvalue() { revert(_1, _1) } + if slt(add(calldatasize(), not(3)), _1) { revert(_1, _1) } + return(memoryguard(0x80), _1) } } revert(0, 0) diff --git a/test/cmdlineTests/standard_debug_info_in_yul_and_evm_asm_print_location_only/output.json b/test/cmdlineTests/standard_debug_info_in_yul_and_evm_asm_print_location_only/output.json index d017c8e9c..8dad4c42e 100644 --- a/test/cmdlineTests/standard_debug_info_in_yul_and_evm_asm_print_location_only/output.json +++ b/test/cmdlineTests/standard_debug_info_in_yul_and_evm_asm_print_location_only/output.json @@ -205,16 +205,14 @@ object \"C_6\" { code { { /// @src 0:60:101 - let _1 := memoryguard(0x80) - mstore(64, _1) if iszero(lt(calldatasize(), 4)) { - let _2 := 0 - if eq(0x26121ff0, shr(224, calldataload(_2))) + let _1 := 0 + if eq(0x26121ff0, shr(224, calldataload(_1))) { - if callvalue() { revert(_2, _2) } - if slt(add(calldatasize(), not(3)), _2) { revert(_2, _2) } - return(_1, _2) + if callvalue() { revert(_1, _1) } + if slt(add(calldatasize(), not(3)), _1) { revert(_1, _1) } + return(memoryguard(0x80), _1) } } revert(0, 0) diff --git a/test/cmdlineTests/standard_debug_info_in_yul_and_evm_asm_print_none/output.json b/test/cmdlineTests/standard_debug_info_in_yul_and_evm_asm_print_none/output.json index afe07fad9..1675f7ec5 100644 --- a/test/cmdlineTests/standard_debug_info_in_yul_and_evm_asm_print_none/output.json +++ b/test/cmdlineTests/standard_debug_info_in_yul_and_evm_asm_print_none/output.json @@ -194,16 +194,14 @@ object \"C_6\" { object \"C_6_deployed\" { code { { - let _1 := memoryguard(0x80) - mstore(64, _1) if iszero(lt(calldatasize(), 4)) { - let _2 := 0 - if eq(0x26121ff0, shr(224, calldataload(_2))) + let _1 := 0 + if eq(0x26121ff0, shr(224, calldataload(_1))) { - if callvalue() { revert(_2, _2) } - if slt(add(calldatasize(), not(3)), _2) { revert(_2, _2) } - return(_1, _2) + if callvalue() { revert(_1, _1) } + if slt(add(calldatasize(), not(3)), _1) { revert(_1, _1) } + return(memoryguard(0x80), _1) } } revert(0, 0) diff --git a/test/cmdlineTests/standard_debug_info_in_yul_location/output.json b/test/cmdlineTests/standard_debug_info_in_yul_location/output.json index f593301bc..f21722a63 100644 --- a/test/cmdlineTests/standard_debug_info_in_yul_location/output.json +++ b/test/cmdlineTests/standard_debug_info_in_yul_location/output.json @@ -622,7 +622,6 @@ object \"C_54\" { { /// @src 0:79:435 \"contract C...\" let _1 := memoryguard(0xa0) - mstore(64, _1) if callvalue() { revert(0, 0) } let programSize := datasize(\"C_54\") let argSize := sub(codesize(), programSize) @@ -1469,7 +1468,6 @@ object \"D_72\" { { /// @src 1:91:166 \"contract D is C(3)...\" let _1 := memoryguard(0xa0) - mstore(64, _1) if callvalue() { revert(0, 0) } let programSize := datasize(\"D_72\") let argSize := sub(codesize(), programSize) @@ -1500,15 +1498,13 @@ object \"D_72\" { /// @src 0:154:156 \"42\" mstore(128, 0x2a) /// @src 1:91:166 \"contract D is C(3)...\" - sstore(/** @src 0:210:226 \"stateVar = _init\" */ 0x00, /** @src 1:107:108 \"3\" */ 0x03) - /// @src 1:91:166 \"contract D is C(3)...\" if and(1, sgt(var_init2, sub(shl(255, 1), 4))) { - mstore(/** @src 0:210:226 \"stateVar = _init\" */ 0x00, /** @src 1:91:166 \"contract D is C(3)...\" */ shl(224, 0x4e487b71)) + mstore(0, shl(224, 0x4e487b71)) mstore(4, 0x11) - revert(/** @src 0:210:226 \"stateVar = _init\" */ 0x00, /** @src 1:91:166 \"contract D is C(3)...\" */ 0x24) + revert(0, 0x24) } - sstore(/** @src 0:210:226 \"stateVar = _init\" */ 0x00, /** @src 1:91:166 \"contract D is C(3)...\" */ add(/** @src 1:107:108 \"3\" */ 0x03, /** @src 1:91:166 \"contract D is C(3)...\" */ var_init2)) + sstore(/** @src -1:-1:-1 */ 0, /** @src 1:91:166 \"contract D is C(3)...\" */ add(/** @src 1:107:108 \"3\" */ 0x03, /** @src 1:91:166 \"contract D is C(3)...\" */ var_init2)) } } /// @use-src 0:\"C\", 1:\"D\" diff --git a/test/cmdlineTests/viair_subobject_optimization/output b/test/cmdlineTests/viair_subobject_optimization/output index 8b5666b37..7dec204b1 100644 --- a/test/cmdlineTests/viair_subobject_optimization/output +++ b/test/cmdlineTests/viair_subobject_optimization/output @@ -3,9 +3,6 @@ EVM assembly: /* "viair_subobject_optimization/input.sol":61:668 contract C {... */ 0x80 - dup1 - 0x40 - mstore jumpi(tag_6, callvalue) 0x1f bytecodeSize @@ -86,7 +83,6 @@ stop sub_0: assembly { /* "viair_subobject_optimization/input.sol":61:668 contract C {... */ - mstore(0x40, 0x80) 0x00 dup1 revert @@ -120,9 +116,6 @@ stop sub_0: assembly { /* "viair_subobject_optimization/input.sol":669:772 contract D {... */ 0x80 - dup1 - 0x40 - mstore jumpi(tag_2, iszero(lt(calldatasize, 0x04))) tag_3: pop @@ -283,9 +276,6 @@ sub_0: assembly { sub_0: assembly { /* "viair_subobject_optimization/input.sol":61:668 contract C {... */ 0x80 - dup1 - 0x40 - mstore jumpi(tag_6, callvalue) 0x1f bytecodeSize @@ -366,7 +356,6 @@ sub_0: assembly { sub_0: assembly { /* "viair_subobject_optimization/input.sol":61:668 contract C {... */ - mstore(0x40, 0x80) 0x00 dup1 revert diff --git a/test/cmdlineTests/viair_subobjects/output b/test/cmdlineTests/viair_subobjects/output index 4b388665c..aea2013ec 100644 --- a/test/cmdlineTests/viair_subobjects/output +++ b/test/cmdlineTests/viair_subobjects/output @@ -30,7 +30,6 @@ object "C_3" { code { { /// @src 0:82:95 "contract C {}" - mstore(64, memoryguard(0x80)) revert(0, 0) } } @@ -122,7 +121,6 @@ object "D_16" { code { { /// @src 0:82:95 "contract C {}" - mstore(64, memoryguard(0x80)) revert(0, 0) } } diff --git a/test/formal/redundant_store_unrelated.py b/test/formal/redundant_store_unrelated.py new file mode 100644 index 000000000..2961d0099 --- /dev/null +++ b/test/formal/redundant_store_unrelated.py @@ -0,0 +1,63 @@ +import sys +from z3 import Solver, Int, unsat + +""" +Tests that the conditions inside RedundantStoreEliminator::knownUnrelated +only return "unrelated" incorrectly if one of the operation reverts +due to large memory access. +""" + +n_bits = 256 + +solver = Solver() +solver.set("timeout", 60000) + +def restrict(x): + solver.add(x >= 0) + solver.add(x < 2**n_bits) + +def restrictedInt(x): + var = Int(x) + restrict(var) + return var + +start1 = restrictedInt('start1') +length1 = restrictedInt('length1') +start2 = restrictedInt('start2') +length2 = restrictedInt('length2') + +k = Int('k') +diff = Int('diff') +solver.add(diff == start2 - start1 + k * 2**n_bits) +restrict(diff) +# diff is the result of sub(start2, start1) in EVM + +# These are the conditions in the code. +solver.add(diff >= length1) +solver.add(diff <= 2**(n_bits-1)) + +# We check that the two conditions are conflicting: +# - overlap +# - start1 is small + +# Overlap: +# x is a potential point where the memory operations +# overlap. +# Note that we do not use wrapping arithmetic +# here, because it is not done in the EVM either. +# For example calldatacopy(2**256 - 2, 0, 10) +# (copy 10 bytes from calldata position zero to memory +# position 2**256 - 2) would not write to memory position +# zero either. +x = Int('x') +solver.add(start1 <= x) +solver.add(x < start1 + length1) +solver.add(start2 <= x) +solver.add(x < start2 + length2) + +# start1 is "small": +solver.add(start1 < 2**(n_bits-1)) + +if solver.check() != unsat: + print("Expected unsat but got something else") + sys.exit(1) diff --git a/test/libsolidity/semanticTests/array/copying/array_copy_storage_storage_dyn_dyn.sol b/test/libsolidity/semanticTests/array/copying/array_copy_storage_storage_dyn_dyn.sol index b4b392974..6eb0de421 100644 --- a/test/libsolidity/semanticTests/array/copying/array_copy_storage_storage_dyn_dyn.sol +++ b/test/libsolidity/semanticTests/array/copying/array_copy_storage_storage_dyn_dyn.sol @@ -17,7 +17,7 @@ contract c { // ---- // setData1(uint256,uint256,uint256): 10, 5, 4 -> // copyStorageStorage() -> -// gas irOptimized: 111406 +// gas irOptimized: 111397 // gas legacy: 109278 // gas legacyOptimized: 109268 // getData2(uint256): 5 -> 10, 4 diff --git a/test/libsolidity/semanticTests/array/copying/copy_byte_array_to_storage.sol b/test/libsolidity/semanticTests/array/copying/copy_byte_array_to_storage.sol index 87c238206..b7b8e4be2 100644 --- a/test/libsolidity/semanticTests/array/copying/copy_byte_array_to_storage.sol +++ b/test/libsolidity/semanticTests/array/copying/copy_byte_array_to_storage.sol @@ -48,6 +48,6 @@ contract C { // compileViaYul: also // ---- // f() -> 0xff -// gas irOptimized: 121145 +// gas irOptimized: 121125 // gas legacy: 128005 // gas legacyOptimized: 123446 diff --git a/test/libsolidity/semanticTests/array/delete/delete_storage_array_packed.sol b/test/libsolidity/semanticTests/array/delete/delete_storage_array_packed.sol index 82c8d6b6c..8d0f6358e 100644 --- a/test/libsolidity/semanticTests/array/delete/delete_storage_array_packed.sol +++ b/test/libsolidity/semanticTests/array/delete/delete_storage_array_packed.sol @@ -16,4 +16,4 @@ contract C { // compileViaYul: also // ---- // f() -> 0, 0, 0 -// gas irOptimized: 91098 +// gas irOptimized: 90992 diff --git a/test/libsolidity/semanticTests/array/pop/byte_array_pop_long_storage_empty_garbage_ref.sol b/test/libsolidity/semanticTests/array/pop/byte_array_pop_long_storage_empty_garbage_ref.sol index d2358b1e2..1550df0d4 100644 --- a/test/libsolidity/semanticTests/array/pop/byte_array_pop_long_storage_empty_garbage_ref.sol +++ b/test/libsolidity/semanticTests/array/pop/byte_array_pop_long_storage_empty_garbage_ref.sol @@ -19,5 +19,5 @@ contract c { // test() -> // gas irOptimized: 142640 // gas legacy: 164430 -// gas legacyOptimized: 158513 +// gas legacyOptimized: 157898 // storageEmpty -> 1 diff --git a/test/libsolidity/semanticTests/array/reusing_memory.sol b/test/libsolidity/semanticTests/array/reusing_memory.sol index 7a05b5328..d783be0b2 100644 --- a/test/libsolidity/semanticTests/array/reusing_memory.sol +++ b/test/libsolidity/semanticTests/array/reusing_memory.sol @@ -26,6 +26,6 @@ contract Main { // compileViaYul: also // ---- // f(uint256): 0x34 -> 0x46bddb1178e94d7f2892ff5f366840eb658911794f2c3a44c450aa2c505186c1 -// gas irOptimized: 113613 +// gas irOptimized: 113604 // gas legacy: 126596 // gas legacyOptimized: 113823 diff --git a/test/libsolidity/semanticTests/constructor/constructor_arguments_external.sol b/test/libsolidity/semanticTests/constructor/constructor_arguments_external.sol index bb9567dd0..1fa468869 100644 --- a/test/libsolidity/semanticTests/constructor/constructor_arguments_external.sol +++ b/test/libsolidity/semanticTests/constructor/constructor_arguments_external.sol @@ -19,7 +19,7 @@ contract Main { // compileViaYul: also // ---- // constructor(): "abc", true -// gas irOptimized: 106267 +// gas irOptimized: 106221 // gas legacy: 145838 // gas legacyOptimized: 104017 // getFlag() -> true diff --git a/test/libsolidity/semanticTests/constructor/no_callvalue_check.sol b/test/libsolidity/semanticTests/constructor/no_callvalue_check.sol index 1352c6e1c..e50901945 100644 --- a/test/libsolidity/semanticTests/constructor/no_callvalue_check.sol +++ b/test/libsolidity/semanticTests/constructor/no_callvalue_check.sol @@ -19,6 +19,6 @@ contract C { // compileViaYul: also // ---- // f(), 2000 ether -> true -// gas irOptimized: 123036 +// gas irOptimized: 120036 // gas legacy: 123226 // gas legacyOptimized: 123092 diff --git a/test/libsolidity/semanticTests/constructor_with_params.sol b/test/libsolidity/semanticTests/constructor_with_params.sol index 0b8982d40..21738240a 100644 --- a/test/libsolidity/semanticTests/constructor_with_params.sol +++ b/test/libsolidity/semanticTests/constructor_with_params.sol @@ -11,7 +11,7 @@ contract C { // compileViaYul: also // ---- // constructor(): 2, 0 -> -// gas irOptimized: 103363 +// gas irOptimized: 103290 // gas legacy: 117158 // i() -> 2 // k() -> 0 diff --git a/test/libsolidity/semanticTests/constructor_with_params_diamond_inheritance.sol b/test/libsolidity/semanticTests/constructor_with_params_diamond_inheritance.sol index 8f891116e..635a60c01 100644 --- a/test/libsolidity/semanticTests/constructor_with_params_diamond_inheritance.sol +++ b/test/libsolidity/semanticTests/constructor_with_params_diamond_inheritance.sol @@ -23,7 +23,7 @@ contract D is B, C { // compileViaYul: also // ---- // constructor(): 2, 0 -> -// gas irOptimized: 160166 +// gas irOptimized: 160093 // gas legacy: 170665 // gas legacyOptimized: 145396 // i() -> 2 diff --git a/test/libsolidity/semanticTests/constructor_with_params_inheritance.sol b/test/libsolidity/semanticTests/constructor_with_params_inheritance.sol index 9f33757b1..2d8b667ef 100644 --- a/test/libsolidity/semanticTests/constructor_with_params_inheritance.sol +++ b/test/libsolidity/semanticTests/constructor_with_params_inheritance.sol @@ -14,7 +14,7 @@ contract D is C { // compileViaYul: also // ---- // constructor(): 2, 0 -> -// gas irOptimized: 123980 +// gas irOptimized: 123907 // gas legacy: 139250 // gas legacyOptimized: 119367 // i() -> 2 diff --git a/test/libsolidity/semanticTests/functionCall/creation_function_call_with_args.sol b/test/libsolidity/semanticTests/functionCall/creation_function_call_with_args.sol index 0b9407814..a67b1b5ad 100644 --- a/test/libsolidity/semanticTests/functionCall/creation_function_call_with_args.sol +++ b/test/libsolidity/semanticTests/functionCall/creation_function_call_with_args.sol @@ -17,7 +17,7 @@ contract D { // compileViaYul: also // ---- // constructor(): 2 -> -// gas irOptimized: 203982 +// gas irOptimized: 203909 // gas legacy: 245842 // gas legacyOptimized: 195676 // f() -> 2 diff --git a/test/libsolidity/semanticTests/functionCall/creation_function_call_with_salt.sol b/test/libsolidity/semanticTests/functionCall/creation_function_call_with_salt.sol index a4796ac82..b03734532 100644 --- a/test/libsolidity/semanticTests/functionCall/creation_function_call_with_salt.sol +++ b/test/libsolidity/semanticTests/functionCall/creation_function_call_with_salt.sol @@ -18,7 +18,7 @@ contract D { // compileViaYul: also // ---- // constructor(): 2 -> -// gas irOptimized: 204145 +// gas irOptimized: 204072 // gas legacy: 246202 // gas legacyOptimized: 195914 // f() -> 2 diff --git a/test/libsolidity/semanticTests/functionCall/failed_create.sol b/test/libsolidity/semanticTests/functionCall/failed_create.sol index 639f39945..933bf342d 100644 --- a/test/libsolidity/semanticTests/functionCall/failed_create.sol +++ b/test/libsolidity/semanticTests/functionCall/failed_create.sol @@ -18,7 +18,7 @@ contract C { // compileViaYul: also // ---- // constructor(), 20 wei -// gas irOptimized: 219069 +// gas irOptimized: 216903 // gas legacy: 294335 // gas legacyOptimized: 174279 // f(uint256): 20 -> 1370859564726510389319704988634906228201275401179 diff --git a/test/libsolidity/semanticTests/functionCall/send_zero_ether.sol b/test/libsolidity/semanticTests/functionCall/send_zero_ether.sol index 617a6dec2..1524c46d0 100644 --- a/test/libsolidity/semanticTests/functionCall/send_zero_ether.sol +++ b/test/libsolidity/semanticTests/functionCall/send_zero_ether.sol @@ -19,7 +19,7 @@ contract Main { // compileViaYul: also // ---- // constructor(), 20 wei -> -// gas irOptimized: 102862 +// gas irOptimized: 101782 // gas legacy: 116691 // gas legacyOptimized: 100361 // s() -> true diff --git a/test/libsolidity/semanticTests/inheritance/value_for_constructor.sol b/test/libsolidity/semanticTests/inheritance/value_for_constructor.sol index 52ed2bc76..354e5d82b 100644 --- a/test/libsolidity/semanticTests/inheritance/value_for_constructor.sol +++ b/test/libsolidity/semanticTests/inheritance/value_for_constructor.sol @@ -42,7 +42,7 @@ contract Main { // compileViaYul: also // ---- // constructor(), 22 wei -> -// gas irOptimized: 282981 +// gas irOptimized: 282916 // gas legacy: 402045 // gas legacyOptimized: 266772 // getFlag() -> true diff --git a/test/libsolidity/semanticTests/libraries/using_library_mappings_public.sol b/test/libsolidity/semanticTests/libraries/using_library_mappings_public.sol index e8d22c279..60741d4c4 100644 --- a/test/libsolidity/semanticTests/libraries/using_library_mappings_public.sol +++ b/test/libsolidity/semanticTests/libraries/using_library_mappings_public.sol @@ -22,6 +22,6 @@ contract Test { // ---- // library: Lib // f() -> 1, 0, 0x2a, 0x17, 0, 0x63 -// gas irOptimized: 119561 +// gas irOptimized: 119501 // gas legacy: 124793 // gas legacyOptimized: 119694 diff --git a/test/libsolidity/semanticTests/salted_create/salted_create.sol b/test/libsolidity/semanticTests/salted_create/salted_create.sol index 5374d2352..51b987c2c 100644 --- a/test/libsolidity/semanticTests/salted_create/salted_create.sol +++ b/test/libsolidity/semanticTests/salted_create/salted_create.sol @@ -22,6 +22,6 @@ contract A { // ---- // different_salt() -> true // same_salt() -> true -// gas irOptimized: 98438914 +// gas irOptimized: 98438898 // gas legacy: 98439116 // gas legacyOptimized: 98438970 diff --git a/test/libsolidity/semanticTests/salted_create/salted_create_with_value.sol b/test/libsolidity/semanticTests/salted_create/salted_create_with_value.sol index 780cc32b8..aab46a9a4 100644 --- a/test/libsolidity/semanticTests/salted_create/salted_create_with_value.sol +++ b/test/libsolidity/semanticTests/salted_create/salted_create_with_value.sol @@ -22,6 +22,6 @@ contract A { // compileViaYul: also // ---- // f(), 10 ether -> 3007, 3008, 3009 -// gas irOptimized: 272467 +// gas irOptimized: 272431 // gas legacy: 422501 // gas legacyOptimized: 287472 diff --git a/test/libsolidity/semanticTests/smoke/constructor.sol b/test/libsolidity/semanticTests/smoke/constructor.sol index a6e812e9a..44d7d502f 100644 --- a/test/libsolidity/semanticTests/smoke/constructor.sol +++ b/test/libsolidity/semanticTests/smoke/constructor.sol @@ -14,7 +14,7 @@ contract C { // compileViaYul: also // ---- // constructor(), 2 wei: 3 -> -// gas irOptimized: 111699 +// gas irOptimized: 111607 // gas legacy: 151416 // gas legacyOptimized: 108388 // state() -> 3 diff --git a/test/libsolidity/semanticTests/structs/struct_copy_via_local.sol b/test/libsolidity/semanticTests/structs/struct_copy_via_local.sol index 21757ea40..4305687b7 100644 --- a/test/libsolidity/semanticTests/structs/struct_copy_via_local.sol +++ b/test/libsolidity/semanticTests/structs/struct_copy_via_local.sol @@ -21,6 +21,6 @@ contract c { // compileViaYul: also // ---- // test() -> true -// gas irOptimized: 110186 +// gas irOptimized: 110177 // gas legacy: 110627 // gas legacyOptimized: 109706 diff --git a/test/libsolidity/semanticTests/structs/struct_delete_storage_with_arrays_small.sol b/test/libsolidity/semanticTests/structs/struct_delete_storage_with_arrays_small.sol index 6f88df394..abc10d8aa 100644 --- a/test/libsolidity/semanticTests/structs/struct_delete_storage_with_arrays_small.sol +++ b/test/libsolidity/semanticTests/structs/struct_delete_storage_with_arrays_small.sol @@ -27,4 +27,4 @@ contract C { // compileViaYul: true // ---- // f() -> 0 -// gas irOptimized: 111896 +// gas irOptimized: 112129 diff --git a/test/libsolidity/semanticTests/structs/struct_memory_to_storage_function_ptr.sol b/test/libsolidity/semanticTests/structs/struct_memory_to_storage_function_ptr.sol index fe791a68d..936039f5e 100644 --- a/test/libsolidity/semanticTests/structs/struct_memory_to_storage_function_ptr.sol +++ b/test/libsolidity/semanticTests/structs/struct_memory_to_storage_function_ptr.sol @@ -32,6 +32,6 @@ contract C { // compileViaYul: also // ---- // f() -> 42, 23, 34, 42, 42 -// gas irOptimized: 110966 +// gas irOptimized: 110845 // gas legacy: 112021 // gas legacyOptimized: 110548 diff --git a/test/libsolidity/semanticTests/structs/structs.sol b/test/libsolidity/semanticTests/structs/structs.sol index 3356eac16..412b71d39 100644 --- a/test/libsolidity/semanticTests/structs/structs.sol +++ b/test/libsolidity/semanticTests/structs/structs.sol @@ -32,7 +32,7 @@ contract test { // ---- // check() -> false // set() -> -// gas irOptimized: 134432 +// gas irOptimized: 134411 // gas legacy: 135277 // gas legacyOptimized: 134064 // check() -> true diff --git a/test/libsolidity/semanticTests/userDefinedValueType/calldata_to_storage.sol b/test/libsolidity/semanticTests/userDefinedValueType/calldata_to_storage.sol index 433d1969c..0305fd473 100644 --- a/test/libsolidity/semanticTests/userDefinedValueType/calldata_to_storage.sol +++ b/test/libsolidity/semanticTests/userDefinedValueType/calldata_to_storage.sol @@ -25,7 +25,7 @@ contract C { // ---- // s() -> 0, 0, 0x00, 0 // f((uint8,uint16,bytes2,uint8)): 1, 0xff, "ab", 15 -> -// gas irOptimized: 44786 +// gas irOptimized: 44405 // gas legacy: 47200 // gas legacyOptimized: 44923 // s() -> 1, 0xff, 0x6162000000000000000000000000000000000000000000000000000000000000, 15 diff --git a/test/libsolidity/semanticTests/userDefinedValueType/memory_to_storage.sol b/test/libsolidity/semanticTests/userDefinedValueType/memory_to_storage.sol index 04e993682..b250bea87 100644 --- a/test/libsolidity/semanticTests/userDefinedValueType/memory_to_storage.sol +++ b/test/libsolidity/semanticTests/userDefinedValueType/memory_to_storage.sol @@ -25,7 +25,7 @@ contract C { // ---- // s() -> 0, 0, 0x00, 0 // f((uint8,uint16,bytes2,uint8)): 1, 0xff, "ab", 15 -> -// gas irOptimized: 44536 +// gas irOptimized: 44473 // gas legacy: 46213 // gas legacyOptimized: 44671 // s() -> 1, 0xff, 0x6162000000000000000000000000000000000000000000000000000000000000, 15 diff --git a/test/libsolidity/semanticTests/various/contract_binary_dependencies.sol b/test/libsolidity/semanticTests/various/contract_binary_dependencies.sol index 57942979a..806138562 100644 --- a/test/libsolidity/semanticTests/various/contract_binary_dependencies.sol +++ b/test/libsolidity/semanticTests/various/contract_binary_dependencies.sol @@ -21,4 +21,4 @@ contract C { // compileViaYul: also // ---- // constructor() -> -// gas irOptimized: 104337 +// gas irOptimized: 102813 diff --git a/test/libyul/YulOptimizerTestCommon.cpp b/test/libyul/YulOptimizerTestCommon.cpp index e7711f586..60826665a 100644 --- a/test/libyul/YulOptimizerTestCommon.cpp +++ b/test/libyul/YulOptimizerTestCommon.cpp @@ -54,6 +54,7 @@ #include #include #include +#include #include #include #include @@ -237,6 +238,15 @@ YulOptimizerTestCommon::YulOptimizerTestCommon( ForLoopInitRewriter::run(*m_context, *m_ast); UnusedAssignEliminator::run(*m_context, *m_ast); }}, + {"unusedStoreEliminator", [&]() { + disambiguate(); + ForLoopInitRewriter::run(*m_context, *m_ast); + ExpressionSplitter::run(*m_context, *m_ast); + SSATransform::run(*m_context, *m_ast); + UnusedStoreEliminator::run(*m_context, *m_ast); + SSAReverser::run(*m_context, *m_ast); + ExpressionJoiner::run(*m_context, *m_ast); + }}, {"equalStoreEliminator", [&]() { disambiguate(); FunctionHoister::run(*m_context, *m_ast); diff --git a/test/libyul/yulOptimizerTests/fullSuite/extcodelength.yul b/test/libyul/yulOptimizerTests/fullSuite/extcodelength.yul new file mode 100644 index 000000000..436f5d5fa --- /dev/null +++ b/test/libyul/yulOptimizerTests/fullSuite/extcodelength.yul @@ -0,0 +1,40 @@ +{ + let _1 := 0 + let value := calldataload(4) + if iszero(eq(value, and(value, sub(shl(160, 1), 1)))) { revert(_1, _1) } + let length := extcodesize(value) + let _2 := 0xffffffffffffffff + if gt(length, _2) { revert(0, 0) } + let _3 := not(31) + let memPtr := mload(64) + let newFreePtr := add(memPtr, and(add(and(add(length, 31), _3), 63), _3)) + if or(gt(newFreePtr, _2), lt(newFreePtr, memPtr)) { revert(0, 0) } + mstore(64, newFreePtr) + mstore(memPtr, length) + + // We aim to optimize this out. + extcodecopy(value, add(memPtr, 32), _1, length) + sstore(_1, mload(memPtr)) +} +// ==== +// EVMVersion: >byzantium +// ---- +// step: fullSuite +// +// { +// { +// let value := calldataload(4) +// if iszero(eq(value, and(value, sub(shl(160, 1), 1)))) { revert(0, 0) } +// let length := extcodesize(value) +// let _1 := 0xffffffffffffffff +// if gt(length, _1) { revert(0, 0) } +// let memPtr := mload(64) +// let _2 := not(31) +// let newFreePtr := add(memPtr, and(add(and(add(length, 31), _2), 63), _2)) +// if or(gt(newFreePtr, _1), lt(newFreePtr, memPtr)) { revert(0, 0) } +// mstore(64, newFreePtr) +// mstore(memPtr, length) +// extcodecopy(value, add(memPtr, 32), 0, length) +// sstore(0, mload(memPtr)) +// } +// } diff --git a/test/libyul/yulOptimizerTests/fullSuite/medium.yul b/test/libyul/yulOptimizerTests/fullSuite/medium.yul index 41b77c115..9a6381d79 100644 --- a/test/libyul/yulOptimizerTests/fullSuite/medium.yul +++ b/test/libyul/yulOptimizerTests/fullSuite/medium.yul @@ -23,13 +23,7 @@ // // { // { -// let p := mload(0x40) -// mstore(0x40, add(p, 0x20)) -// mstore(0x40, add(p, 96)) -// let p_1 := add(p, 128) -// mstore(p_1, 2) -// mstore(0x40, 0x20) -// sstore(0, p_1) +// sstore(0, add(mload(0x40), 128)) // sstore(1, 0x20) // } // } diff --git a/test/libyul/yulOptimizerTests/fullSuite/remove_redundant_assignments_in_switch.yul b/test/libyul/yulOptimizerTests/fullSuite/remove_redundant_assignments_in_switch.yul index a0ac5c36d..317661325 100644 --- a/test/libyul/yulOptimizerTests/fullSuite/remove_redundant_assignments_in_switch.yul +++ b/test/libyul/yulOptimizerTests/fullSuite/remove_redundant_assignments_in_switch.yul @@ -15,6 +15,5 @@ // case 0 { } // case 1 { } // default { invalid() } -// mstore(1, 1) // } // } diff --git a/test/libyul/yulOptimizerTests/fullSuite/stack_compressor_msize.yul b/test/libyul/yulOptimizerTests/fullSuite/stack_compressor_msize.yul index 13683e8e6..75e2ff4c2 100644 --- a/test/libyul/yulOptimizerTests/fullSuite/stack_compressor_msize.yul +++ b/test/libyul/yulOptimizerTests/fullSuite/stack_compressor_msize.yul @@ -52,9 +52,9 @@ // pop(keccak256(gcd(_3, _2), or(gt(not(gcd(_3, _2)), _1), _1))) // mstore(lt(or(gt(_1, or(or(gt(or(or(or(gt(or(gt(_6, _9), _1), _8), _7), _5), _1), _1), _4), _1)), _1), _1), _1) // sstore(not(gcd(_3, _2)), _1) -// sstore(0, 0) // sstore(2, _1) // extcodecopy(_1, msize(), _1, _1) +// sstore(0, 0) // sstore(3, _1) // } // function gcd(_a, _b) -> out diff --git a/test/libyul/yulOptimizerTests/fullSuite/static_array_slot_computation.yul b/test/libyul/yulOptimizerTests/fullSuite/static_array_slot_computation.yul index c5f4aa466..72c09d3dc 100644 --- a/test/libyul/yulOptimizerTests/fullSuite/static_array_slot_computation.yul +++ b/test/libyul/yulOptimizerTests/fullSuite/static_array_slot_computation.yul @@ -39,7 +39,6 @@ // mstore(4, 0x32) // revert(_1, 0x24) // } -// mstore(_1, _1) // sstore(0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e56d, 0x05) // } // } diff --git a/test/libyul/yulOptimizerTests/fullSuite/storage.yul b/test/libyul/yulOptimizerTests/fullSuite/storage.yul index 7b3360847..a60b3d348 100644 --- a/test/libyul/yulOptimizerTests/fullSuite/storage.yul +++ b/test/libyul/yulOptimizerTests/fullSuite/storage.yul @@ -8,7 +8,6 @@ // // { // { -// sstore(4, 5) // sstore(4, 3) // sstore(8, 3) // } diff --git a/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner.yul b/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner.yul index b7f0b4afb..6f82ab27a 100644 --- a/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner.yul +++ b/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner.yul @@ -20,7 +20,6 @@ // { // { // let out1, out2 := foo(sload(32)) -// sstore(0, out1) // sstore(0, out2) // let out1_1, out2_1 := foo(sload(8)) // } diff --git a/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_loop.yul b/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_loop.yul index ee648f80f..7955ee12e 100644 --- a/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_loop.yul +++ b/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_loop.yul @@ -18,9 +18,9 @@ // { // { // f() +// f() +// f() // sstore(0, 1) -// f() -// f() // } // function f() // { diff --git a/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_recursion.yul b/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_recursion.yul index c3b75e84e..92d251cab 100644 --- a/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_recursion.yul +++ b/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_recursion.yul @@ -18,7 +18,6 @@ // { // let x, y, z := f() // sstore(0, x) -// sstore(1, y) // sstore(1, z) // } // function f() -> x, y, z diff --git a/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_return.yul b/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_return.yul index 14f0137cb..b066d998b 100644 --- a/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_return.yul +++ b/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_return.yul @@ -22,8 +22,6 @@ // { // { // let out1, out2 := foo(sload(32)) -// sstore(0, out1) -// sstore(0, out2) // sstore(0, 0) // let out1_1, out2_1 := foo(sload(8)) // } diff --git a/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_simple.yul b/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_simple.yul index 68620cc96..785840baa 100644 --- a/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_simple.yul +++ b/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_simple.yul @@ -16,9 +16,9 @@ // { // { // f() +// f() +// f() // sstore(0, 1) -// f() -// f() // } // function f() // { diff --git a/test/libyul/yulOptimizerTests/unusedStoreEliminator/call_does_not_need_to_write.yul b/test/libyul/yulOptimizerTests/unusedStoreEliminator/call_does_not_need_to_write.yul new file mode 100644 index 000000000..eeff779f8 --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedStoreEliminator/call_does_not_need_to_write.yul @@ -0,0 +1,24 @@ +{ + mstore(0, 1) + let x := call( + 0, + 0, + 0, + 0, + 0, + 0, + 0x32 // length is only a max length, so there is no guarantee that the mstore above is overwritten. + ) + sstore(0, mload(0)) +} + +// ---- +// step: unusedStoreEliminator +// +// { +// { +// mstore(0, 1) +// let x := call(0, 0, 0, 0, 0, 0, 0x32) +// sstore(0, mload(0)) +// } +// } diff --git a/test/libyul/yulOptimizerTests/unusedStoreEliminator/covering_calldatacopy.yul b/test/libyul/yulOptimizerTests/unusedStoreEliminator/covering_calldatacopy.yul new file mode 100644 index 000000000..7fd227089 --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedStoreEliminator/covering_calldatacopy.yul @@ -0,0 +1,59 @@ +{ + let start := calldataload(0x10) + if calldataload(0) { + // not covered + mstore(add(start, 2), 7) + calldatacopy(start, 0, 0x20) + } + if calldataload(1) { + // covered + mstore(add(start, 2), 9) + calldatacopy(add(start, 1), 0, 0x21) + } + if calldataload(2) { + // covered + mstore8(add(start, 2), 7) + calldatacopy(start, 0, 3) + } + if calldataload(3) { + // not covered + mstore8(add(start, 3), 7) + calldatacopy(start, 0, 3) + } + sstore(0, keccak256(start, 0x40)) +} +// ---- +// step: unusedStoreEliminator +// +// { +// { +// let start := calldataload(0x10) +// if calldataload(0) +// { +// let _4 := 7 +// mstore(add(start, 2), _4) +// calldatacopy(start, 0, 0x20) +// } +// if calldataload(1) +// { +// let _11 := 9 +// mstore(add(start, 2), _11) +// let _14 := 0x21 +// let _15 := 0 +// calldatacopy(add(start, 1), _15, _14) +// } +// if calldataload(2) +// { +// let _20 := 7 +// mstore8(add(start, 2), _20) +// calldatacopy(start, 0, 3) +// } +// if calldataload(3) +// { +// let _27 := 7 +// mstore8(add(start, 3), _27) +// calldatacopy(start, 0, 3) +// } +// sstore(0, keccak256(start, 0x40)) +// } +// } diff --git a/test/libyul/yulOptimizerTests/unusedStoreEliminator/covering_calldatacopy_fixed.yul b/test/libyul/yulOptimizerTests/unusedStoreEliminator/covering_calldatacopy_fixed.yul new file mode 100644 index 000000000..47e0b95fa --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedStoreEliminator/covering_calldatacopy_fixed.yul @@ -0,0 +1,53 @@ +{ + if calldataload(0) { + // not covered + mstore(2, 7) + calldatacopy(0, 0, 0x20) + } + if calldataload(1) { + // covered + mstore(2, 9) + calldatacopy(1, 0, 0x21) + } + if calldataload(2) { + // covered + mstore8(2, 7) + calldatacopy(0, 0, 3) + } + if calldataload(3) { + // not covered + mstore8(3, 7) + calldatacopy(0, 0, 3) + } + sstore(0, keccak256(0, 0x40)) +} +// ---- +// step: unusedStoreEliminator +// +// { +// { +// if calldataload(0) +// { +// mstore(2, 7) +// calldatacopy(0, 0, 0x20) +// } +// if calldataload(1) +// { +// let _10 := 9 +// let _11 := 2 +// calldatacopy(1, 0, 0x21) +// } +// if calldataload(2) +// { +// let _17 := 7 +// let _18 := 2 +// calldatacopy(0, 0, 3) +// } +// if calldataload(3) +// { +// mstore8(3, 7) +// calldatacopy(0, 0, 3) +// } +// sstore(0, keccak256(0, 0x40)) +// } +// } diff --git a/test/libyul/yulOptimizerTests/unusedStoreEliminator/create.yul b/test/libyul/yulOptimizerTests/unusedStoreEliminator/create.yul new file mode 100644 index 000000000..c3183d9ec --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedStoreEliminator/create.yul @@ -0,0 +1,17 @@ +{ + let x := 5 + sstore(x, 10) + pop(create(0, 0, 0)) + sstore(x, 20) +} +// ---- +// step: unusedStoreEliminator +// +// { +// { +// let x := 5 +// sstore(x, 10) +// pop(create(0, 0, 0)) +// sstore(x, 20) +// } +// } diff --git a/test/libyul/yulOptimizerTests/unusedStoreEliminator/create_inside_function.yul b/test/libyul/yulOptimizerTests/unusedStoreEliminator/create_inside_function.yul new file mode 100644 index 000000000..782c68663 --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedStoreEliminator/create_inside_function.yul @@ -0,0 +1,22 @@ +{ + let x := 5 + function f() { + pop(create(0, 0, 0)) + } + sstore(x, 10) + f() + sstore(x, 20) +} +// ---- +// step: unusedStoreEliminator +// +// { +// { +// let x := 5 +// sstore(x, 10) +// f() +// sstore(x, 20) +// } +// function f() +// { pop(create(0, 0, 0)) } +// } diff --git a/test/libyul/yulOptimizerTests/unusedStoreEliminator/function_end.yul b/test/libyul/yulOptimizerTests/unusedStoreEliminator/function_end.yul new file mode 100644 index 000000000..cb69be363 --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedStoreEliminator/function_end.yul @@ -0,0 +1,20 @@ +{ + function f() { + let x := calldataload(2) + mstore(x, 2) + // This cannot be removed because we do not know what happens after the function. + mstore(x, 3) + } +} +// ---- +// step: unusedStoreEliminator +// +// { +// { } +// function f() +// { +// let x := calldataload(2) +// let _2 := 2 +// mstore(x, 3) +// } +// } diff --git a/test/libyul/yulOptimizerTests/unusedStoreEliminator/function_side_effects.yul b/test/libyul/yulOptimizerTests/unusedStoreEliminator/function_side_effects.yul new file mode 100644 index 000000000..25f9355b3 --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedStoreEliminator/function_side_effects.yul @@ -0,0 +1,55 @@ +{ + function justStop() { return(0, 0) } + function justRevert() { revert(0, 0) } + + let x := 0 + let y := 1 + let a := 0x80 + let b := 7 + let c := 9 + switch calldataload(0) + case 0 + { + sstore(x, y) + mstore(a, b) + justStop() + sstore(x, y) + mstore(a, b) + } + case 1 + { + sstore(x, y) + mstore(a, b) + justRevert() + sstore(x, y) + mstore(a, b) + } +} +// ---- +// step: unusedStoreEliminator +// +// { +// { +// let x := 0 +// let y := 1 +// let a := 0x80 +// let b := 7 +// let c := 9 +// switch calldataload(0) +// case 0 { +// sstore(x, y) +// mstore(a, b) +// justStop() +// sstore(x, y) +// } +// case 1 { +// mstore(a, b) +// justRevert() +// sstore(x, y) +// } +// } +// function justStop() +// { return(0, 0) } +// function justRevert() +// { revert(0, 0) } +// } diff --git a/test/libyul/yulOptimizerTests/unusedStoreEliminator/function_side_effects_2.yul b/test/libyul/yulOptimizerTests/unusedStoreEliminator/function_side_effects_2.yul new file mode 100644 index 000000000..843f7f6e9 --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedStoreEliminator/function_side_effects_2.yul @@ -0,0 +1,28 @@ +{ + let x := 0 + let y := 1 + sstore(x, y) + f() + sstore(x, y) + function f() { + // prevent inlining + f() + return(0, 0) + } + } +// ---- +// step: unusedStoreEliminator +// +// { +// { +// let x := 0 +// let y := 1 +// f() +// sstore(x, y) +// } +// function f() +// { +// f() +// return(0, 0) +// } +// } diff --git a/test/libyul/yulOptimizerTests/unusedStoreEliminator/if_overwrite_all_branches.yul b/test/libyul/yulOptimizerTests/unusedStoreEliminator/if_overwrite_all_branches.yul new file mode 100644 index 000000000..7054dc255 --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedStoreEliminator/if_overwrite_all_branches.yul @@ -0,0 +1,20 @@ +{ + let c := calldataload(0) + // This store will be overwritten in all branches and thus can be removed. + sstore(c, 1) + if c { + sstore(c, 2) + } + sstore(c, 3) +} +// ---- +// step: unusedStoreEliminator +// +// { +// { +// let c := calldataload(0) +// let _2 := 1 +// if c { let _3 := 2 } +// sstore(c, 3) +// } +// } diff --git a/test/libyul/yulOptimizerTests/unusedStoreEliminator/leave.yul b/test/libyul/yulOptimizerTests/unusedStoreEliminator/leave.yul new file mode 100644 index 000000000..92a751554 --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedStoreEliminator/leave.yul @@ -0,0 +1,23 @@ +{ + function f() { + mstore(0, 5) + if calldataload(0) { leave } + mstore(0x20, 5) + revert(0, 0) + } + f() +} +// ---- +// step: unusedStoreEliminator +// +// { +// { f() } +// function f() +// { +// mstore(0, 5) +// if calldataload(0) { leave } +// let _5 := 5 +// let _6 := 0x20 +// revert(0, 0) +// } +// } diff --git a/test/libyul/yulOptimizerTests/unusedStoreEliminator/memoryguard.yul b/test/libyul/yulOptimizerTests/unusedStoreEliminator/memoryguard.yul new file mode 100644 index 000000000..466cd02e5 --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedStoreEliminator/memoryguard.yul @@ -0,0 +1,24 @@ +{ + mstore(0x40, memoryguard(100)) + let free_mem_ptr := mload(0x40) + // redundant + mstore(free_mem_ptr, 100) + // redundant + mstore8(add(free_mem_ptr, 31), 200) + mstore(free_mem_ptr, 300) + return(free_mem_ptr, add(free_mem_ptr, 100)) +} +// ---- +// step: unusedStoreEliminator +// +// { +// { +// mstore(0x40, memoryguard(100)) +// let free_mem_ptr := mload(0x40) +// let _4 := 100 +// let _5 := 200 +// mstore8(add(free_mem_ptr, 31), _5) +// mstore(free_mem_ptr, 300) +// return(free_mem_ptr, add(free_mem_ptr, 100)) +// } +// } diff --git a/test/libyul/yulOptimizerTests/unusedStoreEliminator/mload.yul b/test/libyul/yulOptimizerTests/unusedStoreEliminator/mload.yul new file mode 100644 index 000000000..717332472 --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedStoreEliminator/mload.yul @@ -0,0 +1,20 @@ +{ + let zero := 0 + mstore(zero, 5) + let x := mload(zero) + mstore(zero, 8) + let y := mload(zero) + sstore(zero, y) +} +// ---- +// step: unusedStoreEliminator +// +// { +// { +// let zero := 0 +// mstore(zero, 5) +// let x := mload(zero) +// mstore(zero, 8) +// sstore(zero, mload(zero)) +// } +// } diff --git a/test/libyul/yulOptimizerTests/unusedStoreEliminator/no_storage_inside_function.yul b/test/libyul/yulOptimizerTests/unusedStoreEliminator/no_storage_inside_function.yul new file mode 100644 index 000000000..524b328e9 --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedStoreEliminator/no_storage_inside_function.yul @@ -0,0 +1,27 @@ +{ + function f() -> r { + r := mload(0x20) + } + let x := 5 + sstore(x, 10) // should be removed + mstore(0, 42) // could be removed, but will probably stay? + pop(f()) + sstore(x, 10) +} +// ---- +// step: unusedStoreEliminator +// +// { +// { +// let x := 5 +// let _2 := 10 +// mstore(0, 42) +// pop(f()) +// sstore(x, 10) +// } +// function f() -> r +// { +// r := mload(0x20) +// let r_7 := r +// } +// } diff --git a/test/libyul/yulOptimizerTests/unusedStoreEliminator/overflow.yul b/test/libyul/yulOptimizerTests/unusedStoreEliminator/overflow.yul new file mode 100644 index 000000000..dda59873c --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedStoreEliminator/overflow.yul @@ -0,0 +1,18 @@ +{ + let x := 0 + + calldatacopy(0, 0, 115792089237316195423570985008687907853269984665640564039457584007913129639935) + mstore(x, 20) + return(0, 32) +} +// ---- +// step: unusedStoreEliminator +// +// { +// { +// let x := 0 +// calldatacopy(0, 0, 115792089237316195423570985008687907853269984665640564039457584007913129639935) +// mstore(x, 20) +// return(0, 32) +// } +// } diff --git a/test/libyul/yulOptimizerTests/unusedStoreEliminator/overlapping.yul b/test/libyul/yulOptimizerTests/unusedStoreEliminator/overlapping.yul new file mode 100644 index 000000000..78179dc3e --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedStoreEliminator/overlapping.yul @@ -0,0 +1,21 @@ +{ + let _1 := 0 + if callvalue() { revert(_1, _1) } + mstore(_1, shl(224, 0x4e487b71)) + mstore(4, 0x32) + revert(_1, 0x24) +} +// ==== +// EVMVersion: >=constantinople +// ---- +// step: unusedStoreEliminator +// +// { +// { +// let _1 := 0 +// if callvalue() { revert(_1, _1) } +// mstore(_1, shl(224, 0x4e487b71)) +// mstore(4, 0x32) +// revert(_1, 0x24) +// } +// } diff --git a/test/libyul/yulOptimizerTests/unusedStoreEliminator/overlapping_small.yul b/test/libyul/yulOptimizerTests/unusedStoreEliminator/overlapping_small.yul new file mode 100644 index 000000000..2950a04ce --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedStoreEliminator/overlapping_small.yul @@ -0,0 +1,27 @@ +{ + let _1 := 0 + let _2 := callvalue() + let _3 := 0x4e487b71 + let _4 := 224 + let _5 := 7 + mstore(_1, _5) + let _6 := 0x32 + let _7 := 4 + mstore(_7, _6) + let _8 := 0x24 + revert(_1, _8) +} +// ---- +// step: unusedStoreEliminator +// +// { +// { +// let _1 := 0 +// let _2 := callvalue() +// let _3 := 0x4e487b71 +// let _4 := 224 +// mstore(_1, 7) +// mstore(4, 0x32) +// revert(_1, 0x24) +// } +// } diff --git a/test/libyul/yulOptimizerTests/unusedStoreEliminator/remove_before_revert.yul b/test/libyul/yulOptimizerTests/unusedStoreEliminator/remove_before_revert.yul new file mode 100644 index 000000000..e7777964c --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedStoreEliminator/remove_before_revert.yul @@ -0,0 +1,21 @@ +{ + let c := calldataload(0) + mstore(c, 4) + if c { + sstore(c, 2) + } + let d := 0 + revert(d, d) +} +// ---- +// step: unusedStoreEliminator +// +// { +// { +// let c := calldataload(0) +// let _2 := 4 +// if c { let _3 := 2 } +// let d := 0 +// revert(d, d) +// } +// } diff --git a/test/libyul/yulOptimizerTests/unusedStoreEliminator/unaligned_access.yul b/test/libyul/yulOptimizerTests/unusedStoreEliminator/unaligned_access.yul new file mode 100644 index 000000000..3bfd558fe --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedStoreEliminator/unaligned_access.yul @@ -0,0 +1,17 @@ +{ + let zero := 0 + mstore(zero, 0x1234) + mstore(4, 0x456) + revert(zero, 5) +} +// ---- +// step: unusedStoreEliminator +// +// { +// { +// let zero := 0 +// mstore(zero, 0x1234) +// mstore(4, 0x456) +// revert(zero, 5) +// } +// } diff --git a/test/libyul/yulOptimizerTests/unusedStoreEliminator/unknown_length2.yul b/test/libyul/yulOptimizerTests/unusedStoreEliminator/unknown_length2.yul new file mode 100644 index 000000000..db5f060a6 --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedStoreEliminator/unknown_length2.yul @@ -0,0 +1,33 @@ +{ + let a + switch calldataload(0) + case 0 { a := calldataload(9) } + case 1 { a := calldataload(10) } + + calldatacopy(0x20, 0, a) + let x := mload(0) + sstore(0, x) +} +// ---- +// step: unusedStoreEliminator +// +// { +// { +// let a_9 +// let a := a_9 +// switch calldataload(0) +// case 0 { +// a := calldataload(9) +// let a_10 := a +// } +// case 1 { +// let a_12 := a +// a := calldataload(10) +// let a_11 := a +// } +// let a_13 := a +// let _5 := 0 +// let _6 := 0x20 +// sstore(0, mload(0)) +// } +// } diff --git a/test/libyul/yulOptimizerTests/unusedStoreEliminator/unrelated_relative.yul b/test/libyul/yulOptimizerTests/unusedStoreEliminator/unrelated_relative.yul new file mode 100644 index 000000000..b0b42060f --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedStoreEliminator/unrelated_relative.yul @@ -0,0 +1,23 @@ +{ + let c := calldataload(0) + mstore(c, 4) + mstore(add(c, 0x20), 8) + sstore(0, mload(c)) + mstore(c, 9) + mstore(add(c, 0x20), 20) +} +// ---- +// step: unusedStoreEliminator +// +// { +// { +// let c := calldataload(0) +// mstore(c, 4) +// let _3 := 8 +// let _5 := add(c, 0x20) +// sstore(0, mload(c)) +// let _8 := 9 +// let _9 := 20 +// let _11 := add(c, 0x20) +// } +// } diff --git a/test/libyul/yulOptimizerTests/unusedStoreEliminator/write_before_recursion.yul b/test/libyul/yulOptimizerTests/unusedStoreEliminator/write_before_recursion.yul new file mode 100644 index 000000000..855ae8325 --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedStoreEliminator/write_before_recursion.yul @@ -0,0 +1,27 @@ +{ + sstore(0, 1) + mstore(0, 2) + f() + function f() { + g() + } + function g() { + f() + } +} +// ---- +// step: unusedStoreEliminator +// +// { +// { +// let _1 := 1 +// let _2 := 0 +// let _3 := 2 +// let _4 := 0 +// f() +// } +// function f() +// { g() } +// function g() +// { f() } +// } diff --git a/test/yulPhaser/Chromosome.cpp b/test/yulPhaser/Chromosome.cpp index 1b1e8bed5..90e278f42 100644 --- a/test/yulPhaser/Chromosome.cpp +++ b/test/yulPhaser/Chromosome.cpp @@ -138,7 +138,7 @@ BOOST_AUTO_TEST_CASE(output_operator_should_create_concise_and_unambiguous_strin BOOST_TEST(chromosome.length() == allSteps.size()); BOOST_TEST(chromosome.optimisationSteps() == allSteps); - BOOST_TEST(toString(chromosome) == "flcCUnDEvejsxIOoighFTLMRmVatrpud"); + BOOST_TEST(toString(chromosome) == "flcCUnDEvejsxIOoighFTLMRmVatrpuSd"); } BOOST_AUTO_TEST_CASE(optimisationSteps_should_translate_chromosomes_genes_to_optimisation_step_names)