mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Unused store eliminator.
This commit is contained in:
parent
81666f8232
commit
45ebe08dea
@ -4,6 +4,7 @@ Language Features:
|
||||
|
||||
|
||||
Compiler Features:
|
||||
* Yul Optimizer: Remove redundant calls to ``mstore`` and ``sstore``.
|
||||
|
||||
|
||||
Bugfixes:
|
||||
|
@ -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
|
||||
|
||||
|
@ -177,6 +177,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
|
||||
|
@ -56,6 +56,7 @@
|
||||
#include <libyul/optimiser/StructuralSimplifier.h>
|
||||
#include <libyul/optimiser/SyntacticalEquality.h>
|
||||
#include <libyul/optimiser/UnusedAssignEliminator.h>
|
||||
#include <libyul/optimiser/UnusedStoreEliminator.h>
|
||||
#include <libyul/optimiser/VarNameCleaner.h>
|
||||
#include <libyul/optimiser/LoadResolver.h>
|
||||
#include <libyul/optimiser/LoopInvariantCodeMotion.h>
|
||||
@ -220,6 +221,7 @@ map<string, unique_ptr<OptimiserStep>> const& OptimiserSuite::allSteps()
|
||||
LoadResolver,
|
||||
LoopInvariantCodeMotion,
|
||||
UnusedAssignEliminator,
|
||||
UnusedStoreEliminator,
|
||||
ReasoningBasedSimplifier,
|
||||
Rematerialiser,
|
||||
SSAReverser,
|
||||
@ -261,6 +263,7 @@ map<string, char> const& OptimiserSuite::stepNameToAbbreviationMap()
|
||||
{LoopInvariantCodeMotion::name, 'M'},
|
||||
{ReasoningBasedSimplifier::name, 'R'},
|
||||
{UnusedAssignEliminator::name, 'r'},
|
||||
{UnusedStoreEliminator::name, 'S'},
|
||||
{Rematerialiser::name, 'm'},
|
||||
{SSAReverser::name, 'V'},
|
||||
{SSATransform::name, 'a'},
|
||||
|
452
libyul/optimiser/UnusedStoreEliminator.cpp
Normal file
452
libyul/optimiser/UnusedStoreEliminator.cpp
Normal file
@ -0,0 +1,452 @@
|
||||
/*
|
||||
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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
// 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 <libyul/optimiser/UnusedStoreEliminator.h>
|
||||
|
||||
#include <libyul/optimiser/Semantics.h>
|
||||
#include <libyul/optimiser/OptimizerUtilities.h>
|
||||
#include <libyul/optimiser/Semantics.h>
|
||||
#include <libyul/optimiser/SSAValueTracker.h>
|
||||
#include <libyul/optimiser/DataFlowAnalyzer.h>
|
||||
#include <libyul/optimiser/KnowledgeBase.h>
|
||||
#include <libyul/ControlFlowSideEffectsCollector.h>
|
||||
#include <libyul/AST.h>
|
||||
|
||||
#include <libsolutil/CommonData.h>
|
||||
|
||||
#include <libevmasm/Instruction.h>
|
||||
#include <libevmasm/SemanticInformation.h>
|
||||
|
||||
#include <range/v3/algorithm/all_of.hpp>
|
||||
|
||||
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<YulString, SideEffects> functionSideEffects = SideEffectsPropagator::sideEffects(
|
||||
_context.dialect,
|
||||
CallGraphGenerator::callGraph(_ast)
|
||||
);
|
||||
|
||||
SSAValueTracker ssaValues;
|
||||
ssaValues(_ast);
|
||||
map<YulString, AssignedValue> 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<ExpressionStatement>(&_statement);
|
||||
if (!exprStatement)
|
||||
return;
|
||||
|
||||
FunctionCall const* funCall = get_if<FunctionCall>(&exprStatement->expression);
|
||||
if (!funCall)
|
||||
return;
|
||||
optional<Instruction> instruction = toEVMInstruction(m_dialect, funCall->functionName.name);
|
||||
if (!instruction)
|
||||
return;
|
||||
|
||||
if (!ranges::all_of(funCall->arguments, [](Expression const& _expr) -> bool {
|
||||
return get_if<Identifier>(&_expr) || get_if<Literal>(&_expr);
|
||||
}))
|
||||
return;
|
||||
|
||||
if (
|
||||
*instruction == Instruction::SSTORE ||
|
||||
(!m_ignoreMemory && (
|
||||
*instruction == Instruction::EXTCODECOPY ||
|
||||
*instruction == Instruction::CODECOPY ||
|
||||
*instruction == Instruction::CALLDATACOPY ||
|
||||
*instruction == Instruction::RETURNDATACOPY ||
|
||||
*instruction == Instruction::MSTORE ||
|
||||
*instruction == Instruction::MSTORE8
|
||||
))
|
||||
)
|
||||
{
|
||||
m_stores[YulString{}].insert({&_statement, State::Undecided});
|
||||
vector<Operation> operations = operationsFromFunctionCall(*funCall);
|
||||
yulAssert(operations.size() == 1, "");
|
||||
m_storeOperations[&_statement] = move(operations.front());
|
||||
}
|
||||
}
|
||||
|
||||
void UnusedStoreEliminator::finalizeFunctionDefinition(FunctionDefinition const&)
|
||||
{
|
||||
changeUndecidedTo(State::Used);
|
||||
scheduleUnusedForDeletion();
|
||||
}
|
||||
|
||||
vector<UnusedStoreEliminator::Operation> 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> instruction = toEVMInstruction(m_dialect, functionName);
|
||||
if (!instruction)
|
||||
{
|
||||
vector<Operation> 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;
|
||||
}
|
||||
|
||||
switch (*instruction)
|
||||
{
|
||||
case Instruction::SSTORE:
|
||||
case Instruction::SLOAD:
|
||||
case Instruction::MSTORE:
|
||||
case Instruction::MSTORE8:
|
||||
case Instruction::MLOAD:
|
||||
case Instruction::REVERT:
|
||||
case Instruction::RETURN:
|
||||
case Instruction::EXTCODECOPY:
|
||||
case Instruction::CODECOPY:
|
||||
case Instruction::CALLDATACOPY:
|
||||
case Instruction::RETURNDATACOPY:
|
||||
case Instruction::KECCAK256:
|
||||
case Instruction::LOG0:
|
||||
case Instruction::LOG1:
|
||||
case Instruction::LOG2:
|
||||
case Instruction::LOG3:
|
||||
case Instruction::LOG4:
|
||||
{
|
||||
Operation op;
|
||||
if (sideEffects.memory == SideEffects::Write || sideEffects.storage == SideEffects::Write)
|
||||
op.effect = Effect::Write;
|
||||
else
|
||||
op.effect = Effect::Read;
|
||||
|
||||
op.location =
|
||||
(instruction == Instruction::SSTORE || instruction == Instruction::SLOAD) ?
|
||||
Location::Storage :
|
||||
Location::Memory;
|
||||
|
||||
if (*instruction == Instruction::EXTCODECOPY)
|
||||
op.start = identifierNameIfSSA(_functionCall.arguments.at(1));
|
||||
else
|
||||
op.start = identifierNameIfSSA(_functionCall.arguments.at(0));
|
||||
|
||||
if (instruction == Instruction::MSTORE || instruction == Instruction::MLOAD)
|
||||
op.length = YulString(thirtyTwo);
|
||||
else if (instruction == Instruction::MSTORE8)
|
||||
op.length = YulString(one);
|
||||
else if (
|
||||
instruction == Instruction::REVERT ||
|
||||
instruction == Instruction::RETURN ||
|
||||
instruction == Instruction::KECCAK256 ||
|
||||
instruction == Instruction::LOG0 ||
|
||||
instruction == Instruction::LOG1 ||
|
||||
instruction == Instruction::LOG2 ||
|
||||
instruction == Instruction::LOG3 ||
|
||||
instruction == Instruction::LOG4
|
||||
)
|
||||
op.length = identifierNameIfSSA(_functionCall.arguments.at(1));
|
||||
else if (*instruction == Instruction::EXTCODECOPY)
|
||||
op.length = identifierNameIfSSA(_functionCall.arguments.at(3));
|
||||
else if (
|
||||
instruction == Instruction::CALLDATACOPY ||
|
||||
instruction == Instruction::CODECOPY ||
|
||||
instruction == Instruction::RETURNDATACOPY
|
||||
)
|
||||
op.length = identifierNameIfSSA(_functionCall.arguments.at(2));
|
||||
else if (instruction == Instruction::SSTORE || instruction == Instruction::SLOAD)
|
||||
// Storage operations, length is unused / non-sensical
|
||||
op.length = {};
|
||||
else
|
||||
yulAssert(false);
|
||||
|
||||
return {op};
|
||||
}
|
||||
case Instruction::STATICCALL:
|
||||
case Instruction::CALL:
|
||||
case Instruction::CALLCODE:
|
||||
case Instruction::DELEGATECALL:
|
||||
{
|
||||
size_t arguments = _functionCall.arguments.size();
|
||||
return vector<Operation>{
|
||||
Operation{
|
||||
Location::Memory,
|
||||
Effect::Read,
|
||||
identifierNameIfSSA(_functionCall.arguments.at(arguments - 4)),
|
||||
identifierNameIfSSA(_functionCall.arguments.at(arguments - 3))
|
||||
},
|
||||
// Unknown read includes unknown write.
|
||||
Operation{Location::Storage, Effect::Read, {}, {}},
|
||||
Operation{
|
||||
Location::Memory,
|
||||
Effect::Write,
|
||||
identifierNameIfSSA(_functionCall.arguments.at(arguments - 2)),
|
||||
identifierNameIfSSA(_functionCall.arguments.at(arguments - 1))
|
||||
}
|
||||
};
|
||||
}
|
||||
case Instruction::CREATE:
|
||||
case Instruction::CREATE2:
|
||||
return vector<Operation>{
|
||||
Operation{
|
||||
Location::Memory,
|
||||
Effect::Read,
|
||||
identifierNameIfSSA(_functionCall.arguments.at(1)),
|
||||
identifierNameIfSSA(_functionCall.arguments.at(2))
|
||||
},
|
||||
// Unknown read includes unknown write.
|
||||
Operation{Location::Storage, Effect::Read, {}, {}},
|
||||
};
|
||||
case Instruction::MSIZE:
|
||||
// This is just to satisfy the assert below.
|
||||
return vector<Operation>{};
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
yulAssert(
|
||||
evmasm::SemanticInformation::storage(*instruction) ==
|
||||
evmasm::SemanticInformation::None,
|
||||
""
|
||||
);
|
||||
yulAssert(
|
||||
evmasm::SemanticInformation::memory(*instruction) ==
|
||||
evmasm::SemanticInformation::None,
|
||||
""
|
||||
);
|
||||
yulAssert(
|
||||
sideEffects.memory == SideEffects::Effect::None &&
|
||||
sideEffects.storage == SideEffects::Effect::None,
|
||||
""
|
||||
);
|
||||
return {};
|
||||
}
|
||||
|
||||
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)
|
||||
return knowledge.knownToBeDifferent(*_op1.start, *_op2.start);
|
||||
}
|
||||
else
|
||||
{
|
||||
u256 largestPositive = (u256(1) << 128) - 1;
|
||||
|
||||
yulAssert(_op1.location == Location::Memory, "");
|
||||
if (_op1.length && knowledge.knownToBeZero(*_op1.length))
|
||||
return true;
|
||||
if (_op2.length && knowledge.knownToBeZero(*_op2.length))
|
||||
return true;
|
||||
|
||||
// 1.start + 1.length <= 2.start
|
||||
if (_op1.length && _op2.start && _op1.start)
|
||||
{
|
||||
optional<u256> length1 = knowledge.valueIfKnownConstant(*_op1.length);
|
||||
optional<u256> diff = knowledge.differenceIfKnownConstant(*_op2.start, *_op1.start);
|
||||
// diff = 2.start - 1.start
|
||||
if (length1 && diff)
|
||||
if (
|
||||
*length1 <= *diff &&
|
||||
*length1 <= largestPositive &&
|
||||
*diff <= largestPositive
|
||||
)
|
||||
return true;
|
||||
}
|
||||
// 2.start + 2.length <= 1.start
|
||||
if (_op2.length && _op1.start && _op2.start)
|
||||
{
|
||||
optional<u256> length2 = knowledge.valueIfKnownConstant(*_op2.length);
|
||||
optional<u256> diff = knowledge.differenceIfKnownConstant(*_op1.start, *_op2.start);
|
||||
// diff = 1.start - 2.start
|
||||
if (length2 && diff)
|
||||
if (*length2 <= *diff && *length2 <= largestPositive && *diff <= largestPositive)
|
||||
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) &&
|
||||
// length is unused for storage
|
||||
(_covered.location == Location::Storage || (_covered.length && _covered.length == _covering.length))
|
||||
)
|
||||
return true;
|
||||
if (_covered.location == Location::Memory)
|
||||
{
|
||||
KnowledgeBase knowledge(m_dialect, m_ssaValues);
|
||||
u256 largestPositive = (u256(1) << 128) - 1;
|
||||
|
||||
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<u256> startDiff = knowledge.differenceIfKnownConstant(*_covered.start, *_covering.start);
|
||||
optional<u256> coveredLength = knowledge.valueIfKnownConstant(*_covered.length);
|
||||
optional<u256> lengthDiff = knowledge.differenceIfKnownConstant(*_covering.length, *_covered.length);
|
||||
if (
|
||||
(startDiff && coveredLength && lengthDiff) &&
|
||||
// i.start <= e.start
|
||||
*startDiff <= largestPositive &&
|
||||
// e.length <= i.length
|
||||
*lengthDiff <= largestPositive &&
|
||||
// e.start - i.start <= i.length - e.length <=> e.start + e.length <= i.start + i.length
|
||||
*startDiff <= *lengthDiff &&
|
||||
// Just a safety measure against overflow.
|
||||
*coveredLength <= largestPositive
|
||||
)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void UnusedStoreEliminator::changeUndecidedTo(
|
||||
State _newState,
|
||||
optional<UnusedStoreEliminator::Location> _onlyLocation)
|
||||
{
|
||||
for (auto& [statement, state]: m_stores[YulString{}])
|
||||
if (
|
||||
state == State::Undecided &&
|
||||
(_onlyLocation == nullopt || *_onlyLocation == m_storeOperations.at(statement).location)
|
||||
)
|
||||
state = _newState;
|
||||
}
|
||||
|
||||
optional<YulString> UnusedStoreEliminator::identifierNameIfSSA(Expression const& _expression) const
|
||||
{
|
||||
if (Identifier const* identifier = get_if<Identifier>(&_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);
|
||||
}
|
117
libyul/optimiser/UnusedStoreEliminator.h
Normal file
117
libyul/optimiser/UnusedStoreEliminator.h
Normal file
@ -0,0 +1,117 @@
|
||||
/*
|
||||
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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
// 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 <libyul/ASTForward.h>
|
||||
#include <libyul/optimiser/ASTWalker.h>
|
||||
#include <libyul/optimiser/OptimiserStep.h>
|
||||
#include <libyul/optimiser/Semantics.h>
|
||||
#include <libyul/optimiser/UnusedStoreBase.h>
|
||||
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
namespace solidity::yul
|
||||
{
|
||||
struct Dialect;
|
||||
struct AssignedValue;
|
||||
|
||||
/**
|
||||
* Optimizer component that removes mstore, sstore and similar 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<YulString, SideEffects> const& _functionSideEffects,
|
||||
std::map<YulString, ControlFlowSideEffects> _controlFlowSideEffects,
|
||||
std::map<YulString, AssignedValue> 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;
|
||||
|
||||
enum class Location { Storage, Memory };
|
||||
enum class Effect { Read, Write };
|
||||
struct Operation
|
||||
{
|
||||
Location location;
|
||||
Effect effect;
|
||||
/// Start of affected area. Unknown if not provided.
|
||||
std::optional<YulString> start;
|
||||
/// Length of affected area, unknown if not provided.
|
||||
/// Unused for storage.
|
||||
std::optional<YulString> 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 { scheduleUnusedForDeletion(); }
|
||||
|
||||
std::vector<Operation> 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<Location> _onlyLocation = std::nullopt);
|
||||
void scheduleUnusedForDeletion();
|
||||
|
||||
std::optional<YulString> identifierNameIfSSA(Expression const& _expression) const;
|
||||
|
||||
bool const m_ignoreMemory;
|
||||
std::map<YulString, SideEffects> const& m_functionSideEffects;
|
||||
std::map<YulString, ControlFlowSideEffects> m_controlFlowSideEffects;
|
||||
std::map<YulString, AssignedValue> const& m_ssaValues;
|
||||
|
||||
std::map<Statement const*, Operation> m_storeOperations;
|
||||
};
|
||||
|
||||
}
|
@ -11,7 +11,6 @@ object "C_12" {
|
||||
code {
|
||||
{
|
||||
/// @src 0:61:418 "contract C {..."
|
||||
mstore(64, 128)
|
||||
if callvalue() { revert(0, 0) }
|
||||
/// @src 0:103:238 "assembly {..."
|
||||
sstore(0, shl(180, 1))
|
||||
@ -26,7 +25,6 @@ object "C_12" {
|
||||
code {
|
||||
{
|
||||
/// @src 0:61:418 "contract C {..."
|
||||
mstore(64, 128)
|
||||
if callvalue() { revert(0, 0) }
|
||||
/// @src 0:279:410 "assembly {..."
|
||||
sstore(0, 0x1000000000000000000000000000000000000000000000)
|
||||
|
@ -195,16 +195,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)
|
||||
|
@ -194,16 +194,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)
|
||||
|
@ -183,16 +183,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)
|
||||
|
@ -95,7 +95,6 @@ object "C_2" {
|
||||
code {
|
||||
{
|
||||
/// @src 0:265:278 "contract C {}"
|
||||
mstore(64, memoryguard(0x80))
|
||||
revert(0, 0)
|
||||
}
|
||||
}
|
||||
@ -553,7 +552,6 @@ object "D_27" {
|
||||
code {
|
||||
{
|
||||
/// @src 0:265:278 "contract C {}"
|
||||
mstore(64, memoryguard(0x80))
|
||||
revert(0, 0)
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,6 @@ object "D_12" {
|
||||
code {
|
||||
{
|
||||
/// @src 0:82:161 "contract D {..."
|
||||
mstore(64, 128)
|
||||
if callvalue() { revert(0, 0) }
|
||||
let _1 := datasize("D_12_deployed")
|
||||
codecopy(128, dataoffset("D_12_deployed"), _1)
|
||||
@ -23,16 +22,14 @@ object "D_12" {
|
||||
code {
|
||||
{
|
||||
/// @src 0:82:161 "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)
|
||||
|
@ -24,7 +24,6 @@ object "D_8" {
|
||||
code {
|
||||
{
|
||||
/// @src 0:82:153 "contract D {..."
|
||||
mstore(64, 128)
|
||||
if iszero(lt(calldatasize(), 4))
|
||||
{
|
||||
let _1 := 0
|
||||
|
@ -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()
|
||||
|
@ -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)
|
||||
|
@ -1,8 +1,5 @@
|
||||
{"contracts":{"C":{"C":{"evm":{"assembly":" /* \"C\":79:428 contract C... */
|
||||
0xa0
|
||||
dup1
|
||||
0x40
|
||||
mstore
|
||||
jumpi(tag_6, callvalue)
|
||||
0x1f
|
||||
bytecodeSize
|
||||
@ -452,9 +449,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
|
||||
@ -528,13 +522,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
|
||||
@ -545,9 +534,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
|
||||
@ -555,17 +542,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 {
|
||||
|
@ -200,16 +200,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)
|
||||
|
@ -199,16 +199,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)
|
||||
|
@ -188,16 +188,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)
|
||||
|
@ -592,7 +592,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)
|
||||
@ -1417,7 +1416,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)
|
||||
@ -1448,15 +1446,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\"
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -48,6 +48,6 @@ contract C {
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// f() -> 0xff
|
||||
// gas irOptimized: 121145
|
||||
// gas irOptimized: 121125
|
||||
// gas legacy: 128035
|
||||
// gas legacyOptimized: 123476
|
||||
|
@ -16,4 +16,4 @@ contract C {
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// f() -> 0, 0, 0
|
||||
// gas irOptimized: 91098
|
||||
// gas irOptimized: 90992
|
||||
|
@ -19,5 +19,5 @@ contract c {
|
||||
// test() ->
|
||||
// gas irOptimized: 142640
|
||||
// gas legacy: 165363
|
||||
// gas legacyOptimized: 159446
|
||||
// gas legacyOptimized: 158831
|
||||
// storageEmpty -> 1
|
||||
|
@ -26,6 +26,6 @@ contract Main {
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// f(uint256): 0x34 -> 0x46bddb1178e94d7f2892ff5f366840eb658911794f2c3a44c450aa2c505186c1
|
||||
// gas irOptimized: 113581
|
||||
// gas irOptimized: 113572
|
||||
// gas legacy: 126596
|
||||
// gas legacyOptimized: 113823
|
||||
|
@ -19,6 +19,6 @@ contract C {
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// f(), 2000 ether -> true
|
||||
// gas irOptimized: 123037
|
||||
// gas irOptimized: 120037
|
||||
// gas legacy: 123226
|
||||
// gas legacyOptimized: 123092
|
||||
|
@ -22,6 +22,6 @@ contract A {
|
||||
// ----
|
||||
// different_salt() -> true
|
||||
// same_salt() -> true
|
||||
// gas irOptimized: 98438914
|
||||
// gas irOptimized: 98438898
|
||||
// gas legacy: 98439116
|
||||
// gas legacyOptimized: 98438970
|
||||
|
@ -22,6 +22,6 @@ contract A {
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// f(), 10 ether -> 3007, 3008, 3009
|
||||
// gas irOptimized: 272947
|
||||
// gas irOptimized: 272920
|
||||
// gas legacy: 422501
|
||||
// gas legacyOptimized: 287472
|
||||
|
@ -21,6 +21,6 @@ contract c {
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// test() -> true
|
||||
// gas irOptimized: 110186
|
||||
// gas irOptimized: 110177
|
||||
// gas legacy: 110627
|
||||
// gas legacyOptimized: 109706
|
||||
|
@ -33,4 +33,4 @@ contract C {
|
||||
// compileViaYul: true
|
||||
// ----
|
||||
// f() -> 0, 0, 0
|
||||
// gas irOptimized: 117289
|
||||
// gas irOptimized: 117277
|
||||
|
@ -27,4 +27,4 @@ contract C {
|
||||
// compileViaYul: true
|
||||
// ----
|
||||
// f() -> 0
|
||||
// gas irOptimized: 111896
|
||||
// gas irOptimized: 112129
|
||||
|
@ -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
|
||||
|
@ -32,7 +32,7 @@ contract test {
|
||||
// ----
|
||||
// check() -> false
|
||||
// set() ->
|
||||
// gas irOptimized: 134335
|
||||
// gas irOptimized: 134314
|
||||
// gas legacy: 135277
|
||||
// gas legacyOptimized: 134064
|
||||
// check() -> true
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -53,6 +53,7 @@
|
||||
#include <libyul/optimiser/SSATransform.h>
|
||||
#include <libyul/optimiser/Semantics.h>
|
||||
#include <libyul/optimiser/UnusedAssignEliminator.h>
|
||||
#include <libyul/optimiser/UnusedStoreEliminator.h>
|
||||
#include <libyul/optimiser/StructuralSimplifier.h>
|
||||
#include <libyul/optimiser/StackCompressor.h>
|
||||
#include <libyul/optimiser/Suite.h>
|
||||
@ -236,6 +237,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);
|
||||
}},
|
||||
{"ssaPlusCleanup", [&]() {
|
||||
disambiguate();
|
||||
ForLoopInitRewriter::run(*m_context, *m_ast);
|
||||
|
37
test/libyul/yulOptimizerTests/fullSuite/extcodelength.yul
Normal file
37
test/libyul/yulOptimizerTests/fullSuite/extcodelength.yul
Normal file
@ -0,0 +1,37 @@
|
||||
{
|
||||
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) }
|
||||
// sstore(0, length)
|
||||
// }
|
||||
// }
|
@ -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)
|
||||
// }
|
||||
// }
|
||||
|
@ -15,6 +15,5 @@
|
||||
// case 0 { }
|
||||
// case 1 { }
|
||||
// default { invalid() }
|
||||
// mstore(1, 1)
|
||||
// }
|
||||
// }
|
||||
|
@ -52,7 +52,6 @@
|
||||
// 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)
|
||||
|
@ -39,7 +39,6 @@
|
||||
// mstore(4, 0x32)
|
||||
// revert(_1, 0x24)
|
||||
// }
|
||||
// mstore(_1, _1)
|
||||
// sstore(0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e56d, 0x05)
|
||||
// }
|
||||
// }
|
||||
|
@ -8,7 +8,6 @@
|
||||
//
|
||||
// {
|
||||
// {
|
||||
// sstore(4, 5)
|
||||
// sstore(4, 3)
|
||||
// sstore(8, 3)
|
||||
// }
|
||||
|
@ -20,7 +20,6 @@
|
||||
// {
|
||||
// {
|
||||
// let out1, out2 := foo(sload(32))
|
||||
// sstore(0, out1)
|
||||
// sstore(0, out2)
|
||||
// let out1_1, out2_1 := foo(sload(8))
|
||||
// }
|
||||
|
@ -18,9 +18,7 @@
|
||||
// {
|
||||
// {
|
||||
// f()
|
||||
// sstore(0, 1)
|
||||
// f()
|
||||
// sstore(0, 1)
|
||||
// f()
|
||||
// sstore(0, 1)
|
||||
// }
|
||||
|
@ -18,7 +18,6 @@
|
||||
// {
|
||||
// let x, y, z := f()
|
||||
// sstore(0, x)
|
||||
// sstore(1, y)
|
||||
// sstore(1, z)
|
||||
// }
|
||||
// function f() -> x, y, z
|
||||
|
@ -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))
|
||||
// }
|
||||
|
@ -16,9 +16,7 @@
|
||||
// {
|
||||
// {
|
||||
// f()
|
||||
// sstore(0, 1)
|
||||
// f()
|
||||
// sstore(0, 1)
|
||||
// f()
|
||||
// sstore(0, 1)
|
||||
// }
|
||||
|
@ -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
|
||||
// let _13 := add(start, 2)
|
||||
// let _14 := 0x21
|
||||
// let _15 := 0
|
||||
// calldatacopy(add(start, 1), _15, _14)
|
||||
// }
|
||||
// if calldataload(2)
|
||||
// {
|
||||
// let _20 := 7
|
||||
// let _22 := add(start, 2)
|
||||
// calldatacopy(start, 0, 3)
|
||||
// }
|
||||
// if calldataload(3)
|
||||
// {
|
||||
// let _27 := 7
|
||||
// mstore8(add(start, 3), _27)
|
||||
// calldatacopy(start, 0, 3)
|
||||
// }
|
||||
// sstore(0, keccak256(start, 0x40))
|
||||
// }
|
||||
// }
|
@ -0,0 +1,15 @@
|
||||
{
|
||||
sstore(5, 10)
|
||||
pop(create(0, 0, 0))
|
||||
sstore(5, 20)
|
||||
}
|
||||
// ----
|
||||
// step: unusedStoreEliminator
|
||||
//
|
||||
// {
|
||||
// {
|
||||
// sstore(5, 10)
|
||||
// pop(create(0, 0, 0))
|
||||
// sstore(5, 20)
|
||||
// }
|
||||
// }
|
@ -0,0 +1,20 @@
|
||||
{
|
||||
function f() {
|
||||
let x := calldataload(2)
|
||||
sstore(x, 2)
|
||||
// This cannot be removed because we do not know what happens after the function.
|
||||
sstore(x, 3)
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// step: unusedStoreEliminator
|
||||
//
|
||||
// {
|
||||
// { }
|
||||
// function f()
|
||||
// {
|
||||
// let x := calldataload(2)
|
||||
// let _2 := 2
|
||||
// sstore(x, 3)
|
||||
// }
|
||||
// }
|
@ -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) }
|
||||
// }
|
@ -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)
|
||||
// }
|
||||
// }
|
@ -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)
|
||||
// }
|
||||
// }
|
@ -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)
|
||||
// }
|
||||
// }
|
@ -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
|
||||
// let _7 := add(free_mem_ptr, 31)
|
||||
// mstore(free_mem_ptr, 300)
|
||||
// return(free_mem_ptr, add(free_mem_ptr, 100))
|
||||
// }
|
||||
// }
|
@ -0,0 +1,18 @@
|
||||
{
|
||||
mstore(0, 5)
|
||||
let x := mload(0)
|
||||
mstore(0, 8)
|
||||
let y := mload(0)
|
||||
sstore(0, y)
|
||||
}
|
||||
// ----
|
||||
// step: unusedStoreEliminator
|
||||
//
|
||||
// {
|
||||
// {
|
||||
// mstore(0, 5)
|
||||
// let x := mload(0)
|
||||
// mstore(0, 8)
|
||||
// sstore(0, mload(0))
|
||||
// }
|
||||
// }
|
@ -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)
|
||||
// }
|
||||
// }
|
@ -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)
|
||||
// }
|
||||
// }
|
@ -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)
|
||||
// }
|
||||
// }
|
@ -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)
|
||||
// }
|
||||
// }
|
@ -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))
|
||||
// }
|
||||
// }
|
@ -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)
|
||||
// }
|
||||
// }
|
@ -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() }
|
||||
// }
|
@ -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) == "flcCUnDvejsxIOoighFTLMRrmVatpud");
|
||||
BOOST_TEST(toString(chromosome) == "flcCUnDvejsxIOoighFTLMRrSmVatpud");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(optimisationSteps_should_translate_chromosomes_genes_to_optimisation_step_names)
|
||||
|
Loading…
Reference in New Issue
Block a user