mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #7324 from ethereum/activateLoadResolver
[Yul] Activate load resolver.
This commit is contained in:
commit
96b6b45658
@ -8,6 +8,7 @@ Compiler Features:
|
|||||||
* ABI Output: Change sorting order of functions from selector to kind, name.
|
* ABI Output: Change sorting order of functions from selector to kind, name.
|
||||||
* Optimizer: Add rule that replaces the BYTE opcode by 0 if the first argument is larger than 31.
|
* Optimizer: Add rule that replaces the BYTE opcode by 0 if the first argument is larger than 31.
|
||||||
* Yul Optimizer: Take side-effect-freeness of user-defined functions into account.
|
* Yul Optimizer: Take side-effect-freeness of user-defined functions into account.
|
||||||
|
* Yul Optimizer: Remove redundant mload/sload operations.
|
||||||
|
|
||||||
|
|
||||||
Bugfixes:
|
Bugfixes:
|
||||||
|
@ -297,7 +297,7 @@ void DataFlowAnalyzer::clearValues(set<YulString> _variables)
|
|||||||
|
|
||||||
void DataFlowAnalyzer::clearKnowledgeIfInvalidated(Block const& _block)
|
void DataFlowAnalyzer::clearKnowledgeIfInvalidated(Block const& _block)
|
||||||
{
|
{
|
||||||
SideEffectsCollector sideEffects(m_dialect, _block);
|
SideEffectsCollector sideEffects(m_dialect, _block, &m_functionSideEffects);
|
||||||
if (sideEffects.invalidatesStorage())
|
if (sideEffects.invalidatesStorage())
|
||||||
m_storage.clear();
|
m_storage.clear();
|
||||||
if (sideEffects.invalidatesMemory())
|
if (sideEffects.invalidatesMemory())
|
||||||
@ -306,7 +306,7 @@ void DataFlowAnalyzer::clearKnowledgeIfInvalidated(Block const& _block)
|
|||||||
|
|
||||||
void DataFlowAnalyzer::clearKnowledgeIfInvalidated(Expression const& _expr)
|
void DataFlowAnalyzer::clearKnowledgeIfInvalidated(Expression const& _expr)
|
||||||
{
|
{
|
||||||
SideEffectsCollector sideEffects(m_dialect, _expr);
|
SideEffectsCollector sideEffects(m_dialect, _expr, &m_functionSideEffects);
|
||||||
if (sideEffects.invalidatesStorage())
|
if (sideEffects.invalidatesStorage())
|
||||||
m_storage.clear();
|
m_storage.clear();
|
||||||
if (sideEffects.invalidatesMemory())
|
if (sideEffects.invalidatesMemory())
|
||||||
|
@ -23,6 +23,8 @@
|
|||||||
|
|
||||||
#include <libyul/backends/evm/EVMDialect.h>
|
#include <libyul/backends/evm/EVMDialect.h>
|
||||||
#include <libyul/optimiser/Semantics.h>
|
#include <libyul/optimiser/Semantics.h>
|
||||||
|
#include <libyul/optimiser/CallGraphGenerator.h>
|
||||||
|
#include <libyul/SideEffects.h>
|
||||||
#include <libyul/AsmData.h>
|
#include <libyul/AsmData.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@ -32,35 +34,53 @@ using namespace yul;
|
|||||||
void LoadResolver::run(Dialect const& _dialect, Block& _ast)
|
void LoadResolver::run(Dialect const& _dialect, Block& _ast)
|
||||||
{
|
{
|
||||||
bool containsMSize = MSizeFinder::containsMSize(_dialect, _ast);
|
bool containsMSize = MSizeFinder::containsMSize(_dialect, _ast);
|
||||||
LoadResolver{_dialect, !containsMSize}(_ast);
|
LoadResolver{
|
||||||
|
_dialect,
|
||||||
|
SideEffectsPropagator::sideEffects(_dialect, CallGraphGenerator::callGraph(_ast)),
|
||||||
|
!containsMSize
|
||||||
|
}(_ast);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LoadResolver::visit(Expression& _e)
|
void LoadResolver::visit(Expression& _e)
|
||||||
{
|
{
|
||||||
|
DataFlowAnalyzer::visit(_e);
|
||||||
|
|
||||||
|
if (!dynamic_cast<EVMDialect const*>(&m_dialect))
|
||||||
|
return;
|
||||||
|
|
||||||
if (_e.type() == typeid(FunctionCall))
|
if (_e.type() == typeid(FunctionCall))
|
||||||
{
|
{
|
||||||
FunctionCall const& funCall = boost::get<FunctionCall>(_e);
|
FunctionCall const& funCall = boost::get<FunctionCall>(_e);
|
||||||
if (auto const* builtin = dynamic_cast<EVMDialect const&>(m_dialect).builtin(funCall.functionName.name))
|
if (auto const* builtin = dynamic_cast<EVMDialect const&>(m_dialect).builtin(funCall.functionName.name))
|
||||||
if (!builtin->parameters.empty() && funCall.arguments.at(0).type() == typeid(Identifier))
|
if (builtin->instruction)
|
||||||
{
|
tryResolve(_e, *builtin->instruction, funCall.arguments);
|
||||||
YulString key = boost::get<Identifier>(funCall.arguments.at(0)).name;
|
}
|
||||||
if (
|
else if (_e.type() == typeid(FunctionalInstruction))
|
||||||
builtin->instruction == dev::eth::Instruction::SLOAD &&
|
{
|
||||||
m_storage.values.count(key)
|
FunctionalInstruction const& instruction = boost::get<FunctionalInstruction>(_e);
|
||||||
)
|
tryResolve(_e, instruction.instruction, instruction.arguments);
|
||||||
{
|
|
||||||
_e = Identifier{locationOf(_e), m_storage.values[key]};
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else if (
|
|
||||||
m_optimizeMLoad &&
|
|
||||||
builtin->instruction == dev::eth::Instruction::MLOAD &&
|
|
||||||
m_memory.values.count(key)
|
|
||||||
)
|
|
||||||
{
|
|
||||||
_e = Identifier{locationOf(_e), m_memory.values[key]};
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LoadResolver::tryResolve(
|
||||||
|
Expression& _e,
|
||||||
|
dev::eth::Instruction _instruction,
|
||||||
|
vector<Expression> const& _arguments
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (_arguments.empty() || _arguments.at(0).type() != typeid(Identifier))
|
||||||
|
return;
|
||||||
|
|
||||||
|
YulString key = boost::get<Identifier>(_arguments.at(0)).name;
|
||||||
|
if (
|
||||||
|
_instruction == dev::eth::Instruction::SLOAD &&
|
||||||
|
m_storage.values.count(key)
|
||||||
|
)
|
||||||
|
_e = Identifier{locationOf(_e), m_storage.values[key]};
|
||||||
|
else if (
|
||||||
|
m_optimizeMLoad &&
|
||||||
|
_instruction == dev::eth::Instruction::MLOAD &&
|
||||||
|
m_memory.values.count(key)
|
||||||
|
)
|
||||||
|
_e = Identifier{locationOf(_e), m_memory.values[key]};
|
||||||
|
}
|
||||||
|
@ -22,11 +22,13 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <libyul/optimiser/DataFlowAnalyzer.h>
|
#include <libyul/optimiser/DataFlowAnalyzer.h>
|
||||||
|
#include <libevmasm/Instruction.h>
|
||||||
|
|
||||||
namespace yul
|
namespace yul
|
||||||
{
|
{
|
||||||
|
|
||||||
struct EVMDialect;
|
struct EVMDialect;
|
||||||
|
struct BuiltinFunctionForEVM;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Optimisation stage that replaces expressions of type ``sload(x)`` and ``mload(x)`` by the value
|
* Optimisation stage that replaces expressions of type ``sload(x)`` and ``mload(x)`` by the value
|
||||||
@ -39,11 +41,16 @@ struct EVMDialect;
|
|||||||
class LoadResolver: public DataFlowAnalyzer
|
class LoadResolver: public DataFlowAnalyzer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
/// Run the load resolver on the given complete AST.
|
||||||
static void run(Dialect const& _dialect, Block& _ast);
|
static void run(Dialect const& _dialect, Block& _ast);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
LoadResolver(Dialect const& _dialect, bool _optimizeMLoad):
|
LoadResolver(
|
||||||
DataFlowAnalyzer(_dialect),
|
Dialect const& _dialect,
|
||||||
|
std::map<YulString, SideEffects> _functionSideEffects,
|
||||||
|
bool _optimizeMLoad
|
||||||
|
):
|
||||||
|
DataFlowAnalyzer(_dialect, std::move(_functionSideEffects)),
|
||||||
m_optimizeMLoad(_optimizeMLoad)
|
m_optimizeMLoad(_optimizeMLoad)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
@ -51,6 +58,12 @@ protected:
|
|||||||
using ASTModifier::visit;
|
using ASTModifier::visit;
|
||||||
void visit(Expression& _e) override;
|
void visit(Expression& _e) override;
|
||||||
|
|
||||||
|
void tryResolve(
|
||||||
|
Expression& _e,
|
||||||
|
dev::eth::Instruction _instruction,
|
||||||
|
std::vector<Expression> const& _arguments
|
||||||
|
);
|
||||||
|
|
||||||
bool m_optimizeMLoad = false;
|
bool m_optimizeMLoad = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -51,8 +51,12 @@ SideEffectsCollector::SideEffectsCollector(Dialect const& _dialect, Statement co
|
|||||||
visit(_statement);
|
visit(_statement);
|
||||||
}
|
}
|
||||||
|
|
||||||
SideEffectsCollector::SideEffectsCollector(Dialect const& _dialect, Block const& _ast):
|
SideEffectsCollector::SideEffectsCollector(
|
||||||
SideEffectsCollector(_dialect)
|
Dialect const& _dialect,
|
||||||
|
Block const& _ast,
|
||||||
|
map<YulString, SideEffects> const* _functionSideEffects
|
||||||
|
):
|
||||||
|
SideEffectsCollector(_dialect, _functionSideEffects)
|
||||||
{
|
{
|
||||||
operator()(_ast);
|
operator()(_ast);
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,11 @@ public:
|
|||||||
std::map<YulString, SideEffects> const* _functionSideEffects = nullptr
|
std::map<YulString, SideEffects> const* _functionSideEffects = nullptr
|
||||||
);
|
);
|
||||||
SideEffectsCollector(Dialect const& _dialect, Statement const& _statement);
|
SideEffectsCollector(Dialect const& _dialect, Statement const& _statement);
|
||||||
SideEffectsCollector(Dialect const& _dialect, Block const& _ast);
|
SideEffectsCollector(
|
||||||
|
Dialect const& _dialect,
|
||||||
|
Block const& _ast,
|
||||||
|
std::map<YulString, SideEffects> const* _functionSideEffects = nullptr
|
||||||
|
);
|
||||||
|
|
||||||
using ASTWalker::operator();
|
using ASTWalker::operator();
|
||||||
void operator()(FunctionalInstruction const& _functionalInstruction) override;
|
void operator()(FunctionalInstruction const& _functionalInstruction) override;
|
||||||
|
@ -45,6 +45,7 @@
|
|||||||
#include <libyul/optimiser/StructuralSimplifier.h>
|
#include <libyul/optimiser/StructuralSimplifier.h>
|
||||||
#include <libyul/optimiser/RedundantAssignEliminator.h>
|
#include <libyul/optimiser/RedundantAssignEliminator.h>
|
||||||
#include <libyul/optimiser/VarNameCleaner.h>
|
#include <libyul/optimiser/VarNameCleaner.h>
|
||||||
|
#include <libyul/optimiser/LoadResolver.h>
|
||||||
#include <libyul/optimiser/Metrics.h>
|
#include <libyul/optimiser/Metrics.h>
|
||||||
#include <libyul/backends/evm/ConstantOptimiser.h>
|
#include <libyul/backends/evm/ConstantOptimiser.h>
|
||||||
#include <libyul/AsmAnalysis.h>
|
#include <libyul/AsmAnalysis.h>
|
||||||
@ -118,6 +119,7 @@ void OptimiserSuite::run(
|
|||||||
ExpressionSimplifier::run(_dialect, ast);
|
ExpressionSimplifier::run(_dialect, ast);
|
||||||
|
|
||||||
CommonSubexpressionEliminator::run(_dialect, ast);
|
CommonSubexpressionEliminator::run(_dialect, ast);
|
||||||
|
LoadResolver::run(_dialect, ast);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -132,6 +134,7 @@ void OptimiserSuite::run(
|
|||||||
|
|
||||||
{
|
{
|
||||||
// simplify again
|
// simplify again
|
||||||
|
LoadResolver::run(_dialect, ast);
|
||||||
CommonSubexpressionEliminator::run(_dialect, ast);
|
CommonSubexpressionEliminator::run(_dialect, ast);
|
||||||
UnusedPruner::runUntilStabilisedOnFullAST(_dialect, ast, reservedIdentifiers);
|
UnusedPruner::runUntilStabilisedOnFullAST(_dialect, ast, reservedIdentifiers);
|
||||||
}
|
}
|
||||||
@ -161,6 +164,7 @@ void OptimiserSuite::run(
|
|||||||
RedundantAssignEliminator::run(_dialect, ast);
|
RedundantAssignEliminator::run(_dialect, ast);
|
||||||
RedundantAssignEliminator::run(_dialect, ast);
|
RedundantAssignEliminator::run(_dialect, ast);
|
||||||
CommonSubexpressionEliminator::run(_dialect, ast);
|
CommonSubexpressionEliminator::run(_dialect, ast);
|
||||||
|
LoadResolver::run(_dialect, ast);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -176,6 +180,7 @@ void OptimiserSuite::run(
|
|||||||
SSATransform::run(ast, dispenser);
|
SSATransform::run(ast, dispenser);
|
||||||
RedundantAssignEliminator::run(_dialect, ast);
|
RedundantAssignEliminator::run(_dialect, ast);
|
||||||
RedundantAssignEliminator::run(_dialect, ast);
|
RedundantAssignEliminator::run(_dialect, ast);
|
||||||
|
LoadResolver::run(_dialect, ast);
|
||||||
ExpressionSimplifier::run(_dialect, ast);
|
ExpressionSimplifier::run(_dialect, ast);
|
||||||
StructuralSimplifier{_dialect}(ast);
|
StructuralSimplifier{_dialect}(ast);
|
||||||
BlockFlattener{}(ast);
|
BlockFlattener{}(ast);
|
||||||
|
@ -21,11 +21,10 @@
|
|||||||
// ----
|
// ----
|
||||||
// {
|
// {
|
||||||
// {
|
// {
|
||||||
// let _1 := 0x40
|
// let _1 := mload(0x40)
|
||||||
// mstore(_1, add(mload(_1), 0x20))
|
// mstore(0x40, add(_1, 0x20))
|
||||||
// let p := mload(_1)
|
// mstore(0x40, add(_1, 96))
|
||||||
// mstore(_1, add(p, _1))
|
// mstore(add(_1, 128), 2)
|
||||||
// mstore(add(p, 96), 2)
|
// mstore(0x40, 0x20)
|
||||||
// mstore(_1, 0x20)
|
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
@ -10,6 +10,6 @@
|
|||||||
// {
|
// {
|
||||||
// sstore(4, 5)
|
// sstore(4, 5)
|
||||||
// sstore(4, 3)
|
// sstore(4, 3)
|
||||||
// sstore(8, sload(4))
|
// sstore(8, 3)
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
18
test/libyul/yulOptimizerTests/loadResolver/loop.yul
Normal file
18
test/libyul/yulOptimizerTests/loadResolver/loop.yul
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
sstore(0, 123213)
|
||||||
|
for {let x := 0 let y} lt(x, sload(0)) {
|
||||||
|
x := add(x, 1)} {y := add(x, y)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ====
|
||||||
|
// step: loadResolver
|
||||||
|
// ----
|
||||||
|
// {
|
||||||
|
// let _1 := 123213
|
||||||
|
// let _2 := 0
|
||||||
|
// sstore(_2, _1)
|
||||||
|
// let x := _2
|
||||||
|
// let y
|
||||||
|
// for { } lt(x, _1) { x := add(x, 1) }
|
||||||
|
// { y := add(x, y) }
|
||||||
|
// }
|
@ -31,5 +31,5 @@
|
|||||||
// mstore8(calldataload(_5), 4)
|
// mstore8(calldataload(_5), 4)
|
||||||
// sstore(_5, mload(_2))
|
// sstore(_5, mload(_2))
|
||||||
// mstore(_2, _17)
|
// mstore(_2, _17)
|
||||||
// sstore(_5, mload(_2))
|
// sstore(_5, _17)
|
||||||
// }
|
// }
|
||||||
|
@ -0,0 +1,28 @@
|
|||||||
|
{
|
||||||
|
function stores() { mstore(0, 1) }
|
||||||
|
function reads() { sstore(9, mload(7)) }
|
||||||
|
|
||||||
|
mstore(2, 9)
|
||||||
|
reads()
|
||||||
|
sstore(0, mload(2))
|
||||||
|
stores()
|
||||||
|
sstore(0, mload(2))
|
||||||
|
}
|
||||||
|
// ====
|
||||||
|
// step: loadResolver
|
||||||
|
// ----
|
||||||
|
// {
|
||||||
|
// function stores()
|
||||||
|
// { mstore(0, 1) }
|
||||||
|
// function reads()
|
||||||
|
// { sstore(9, mload(7)) }
|
||||||
|
// let _6 := 9
|
||||||
|
// let _7 := 2
|
||||||
|
// mstore(_7, _6)
|
||||||
|
// reads()
|
||||||
|
// let _9 := _6
|
||||||
|
// let _10 := 0
|
||||||
|
// sstore(_10, _9)
|
||||||
|
// stores()
|
||||||
|
// sstore(_10, mload(_7))
|
||||||
|
// }
|
Loading…
Reference in New Issue
Block a user