mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Knowledge about storage.
This commit is contained in:
parent
9bb7160c4c
commit
1f9d11c644
@ -20,6 +20,48 @@
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
/**
|
||||
* Data structure that keeps track of values and keys of a mapping.
|
||||
*/
|
||||
template <class K, class V>
|
||||
struct InvertibleMap
|
||||
{
|
||||
std::map<K, V> values;
|
||||
// references[x] == {y | values[y] == x}
|
||||
std::map<V, std::set<K>> references;
|
||||
|
||||
void set(K _key, V _value)
|
||||
{
|
||||
if (values.count(_key))
|
||||
references[values[_key]].erase(_key);
|
||||
values[_key] = _value;
|
||||
references[_value].insert(_key);
|
||||
}
|
||||
|
||||
void eraseKey(K _key)
|
||||
{
|
||||
if (values.count(_key))
|
||||
references[values[_key]].erase(_key);
|
||||
values.erase(_key);
|
||||
}
|
||||
|
||||
void eraseValue(V _value)
|
||||
{
|
||||
if (references.count(_value))
|
||||
{
|
||||
for (V v: references[_value])
|
||||
values.erase(v);
|
||||
references.erase(_value);
|
||||
}
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
values.clear();
|
||||
references.clear();
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct InvertibleRelation
|
||||
{
|
||||
|
@ -94,6 +94,8 @@ add_library(yul
|
||||
optimiser/FunctionHoister.h
|
||||
optimiser/InlinableExpressionFunctionFinder.cpp
|
||||
optimiser/InlinableExpressionFunctionFinder.h
|
||||
optimiser/KnowledgeBase.cpp
|
||||
optimiser/KnowledgeBase.h
|
||||
optimiser/MainFunction.cpp
|
||||
optimiser/MainFunction.h
|
||||
optimiser/Metrics.cpp
|
||||
@ -109,6 +111,8 @@ add_library(yul
|
||||
optimiser/RedundantAssignEliminator.cpp
|
||||
optimiser/RedundantAssignEliminator.h
|
||||
optimiser/Rematerialiser.cpp
|
||||
optimiser/SLoadResolver.cpp
|
||||
optimiser/SLoadResolver.h
|
||||
optimiser/Rematerialiser.h
|
||||
optimiser/SSAReverser.cpp
|
||||
optimiser/SSAReverser.h
|
||||
|
@ -57,6 +57,9 @@ struct BuiltinFunction
|
||||
bool sideEffectFreeIfNoMSize = false;
|
||||
/// If true, this is the msize instruction.
|
||||
bool isMSize = false;
|
||||
/// If false, storage of the current contract before and after the function is the same
|
||||
/// under every circumstance. If the function does not return, this can be false.
|
||||
bool invalidatesStorage = true;
|
||||
/// If true, can only accept literals as arguments and they cannot be moved to variables.
|
||||
bool literalArguments = false;
|
||||
};
|
||||
|
@ -54,6 +54,7 @@ pair<YulString, BuiltinFunctionForEVM> createEVMFunction(
|
||||
f.sideEffectFree = eth::SemanticInformation::sideEffectFree(_instruction);
|
||||
f.sideEffectFreeIfNoMSize = eth::SemanticInformation::sideEffectFreeIfNoMSize(_instruction);
|
||||
f.isMSize = _instruction == dev::eth::Instruction::MSIZE;
|
||||
f.invalidatesStorage = eth::SemanticInformation::invalidatesStorage(_instruction);
|
||||
f.literalArguments = false;
|
||||
f.instruction = _instruction;
|
||||
f.generateCode = [_instruction](
|
||||
@ -76,6 +77,7 @@ pair<YulString, BuiltinFunctionForEVM> createFunction(
|
||||
bool _movable,
|
||||
bool _sideEffectFree,
|
||||
bool _sideEffectFreeIfNoMSize,
|
||||
bool _invalidatesStorage,
|
||||
bool _literalArguments,
|
||||
std::function<void(FunctionCall const&, AbstractAssembly&, BuiltinContext&, std::function<void()>)> _generateCode
|
||||
)
|
||||
@ -90,6 +92,7 @@ pair<YulString, BuiltinFunctionForEVM> createFunction(
|
||||
f.sideEffectFree = _sideEffectFree;
|
||||
f.sideEffectFreeIfNoMSize = _sideEffectFreeIfNoMSize;
|
||||
f.isMSize = false;
|
||||
f.invalidatesStorage = _invalidatesStorage;
|
||||
f.instruction = {};
|
||||
f.generateCode = std::move(_generateCode);
|
||||
return {name, f};
|
||||
@ -110,7 +113,7 @@ map<YulString, BuiltinFunctionForEVM> createBuiltins(langutil::EVMVersion _evmVe
|
||||
|
||||
if (_objectAccess)
|
||||
{
|
||||
builtins.emplace(createFunction("datasize", 1, 1, true, true, true, true, [](
|
||||
builtins.emplace(createFunction("datasize", 1, 1, true, true, true, false, true, [](
|
||||
FunctionCall const& _call,
|
||||
AbstractAssembly& _assembly,
|
||||
BuiltinContext& _context,
|
||||
@ -131,7 +134,7 @@ map<YulString, BuiltinFunctionForEVM> createBuiltins(langutil::EVMVersion _evmVe
|
||||
_assembly.appendDataSize(_context.subIDs.at(dataName));
|
||||
}
|
||||
}));
|
||||
builtins.emplace(createFunction("dataoffset", 1, 1, true, true, true, true, [](
|
||||
builtins.emplace(createFunction("dataoffset", 1, 1, true, true, true, false, true, [](
|
||||
FunctionCall const& _call,
|
||||
AbstractAssembly& _assembly,
|
||||
BuiltinContext& _context,
|
||||
@ -152,7 +155,7 @@ map<YulString, BuiltinFunctionForEVM> createBuiltins(langutil::EVMVersion _evmVe
|
||||
_assembly.appendDataOffset(_context.subIDs.at(dataName));
|
||||
}
|
||||
}));
|
||||
builtins.emplace(createFunction("datacopy", 3, 0, false, false, false, false, [](
|
||||
builtins.emplace(createFunction("datacopy", 3, 0, false, false, false, false, false, [](
|
||||
FunctionCall const&,
|
||||
AbstractAssembly& _assembly,
|
||||
BuiltinContext&,
|
||||
|
@ -83,5 +83,6 @@ void WasmDialect::addFunction(string _name, size_t _params, size_t _returns)
|
||||
f.sideEffectFree = false;
|
||||
f.sideEffectFreeIfNoMSize = false;
|
||||
f.isMSize = false;
|
||||
f.invalidatesStorage = true;
|
||||
f.literalArguments = false;
|
||||
}
|
||||
|
@ -26,21 +26,48 @@
|
||||
#include <libyul/optimiser/Semantics.h>
|
||||
#include <libyul/Exceptions.h>
|
||||
#include <libyul/AsmData.h>
|
||||
#include <libyul/backends/evm/EVMDialect.h>
|
||||
|
||||
#include <libdevcore/CommonData.h>
|
||||
|
||||
#include <boost/range/adaptor/reversed.hpp>
|
||||
#include <boost/range/algorithm_ext/erase.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace dev;
|
||||
using namespace yul;
|
||||
|
||||
|
||||
void DataFlowAnalyzer::operator()(ExpressionStatement& _statement)
|
||||
{
|
||||
if (boost::optional<pair<YulString, YulString>> vars = isSimpleSStore(_statement))
|
||||
{
|
||||
ASTModifier::operator()(_statement);
|
||||
m_storage.set(vars->first, vars->second);
|
||||
set<YulString> keysToErase;
|
||||
for (auto const& item: m_storage.values)
|
||||
if (!(
|
||||
m_knowledgeBase.knownToBeDifferent(vars->first, item.first) ||
|
||||
m_knowledgeBase.knownToBeEqual(vars->second, item.second)
|
||||
))
|
||||
keysToErase.insert(item.first);
|
||||
for (YulString const& key: keysToErase)
|
||||
m_storage.eraseKey(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
clearStorageKnowledgeIfInvalidated(_statement.expression);
|
||||
ASTModifier::operator()(_statement);
|
||||
}
|
||||
}
|
||||
|
||||
void DataFlowAnalyzer::operator()(Assignment& _assignment)
|
||||
{
|
||||
set<YulString> names;
|
||||
for (auto const& var: _assignment.variableNames)
|
||||
names.emplace(var.name);
|
||||
assertThrow(_assignment.value, OptimizerException, "");
|
||||
clearStorageKnowledgeIfInvalidated(*_assignment.value);
|
||||
visit(*_assignment.value);
|
||||
handleAssignment(names, _assignment.value.get());
|
||||
}
|
||||
@ -53,15 +80,23 @@ void DataFlowAnalyzer::operator()(VariableDeclaration& _varDecl)
|
||||
m_variableScopes.back().variables += names;
|
||||
|
||||
if (_varDecl.value)
|
||||
{
|
||||
clearStorageKnowledgeIfInvalidated(*_varDecl.value);
|
||||
visit(*_varDecl.value);
|
||||
}
|
||||
|
||||
handleAssignment(names, _varDecl.value.get());
|
||||
}
|
||||
|
||||
void DataFlowAnalyzer::operator()(If& _if)
|
||||
{
|
||||
clearStorageKnowledgeIfInvalidated(*_if.condition);
|
||||
InvertibleMap<YulString, YulString> storage = m_storage;
|
||||
|
||||
ASTModifier::operator()(_if);
|
||||
|
||||
joinStorageKnowledge(storage);
|
||||
|
||||
Assignments assignments;
|
||||
assignments(_if.body);
|
||||
clearValues(assignments.names());
|
||||
@ -69,17 +104,24 @@ void DataFlowAnalyzer::operator()(If& _if)
|
||||
|
||||
void DataFlowAnalyzer::operator()(Switch& _switch)
|
||||
{
|
||||
clearStorageKnowledgeIfInvalidated(*_switch.expression);
|
||||
visit(*_switch.expression);
|
||||
set<YulString> assignedVariables;
|
||||
for (auto& _case: _switch.cases)
|
||||
{
|
||||
InvertibleMap<YulString, YulString> storage = m_storage;
|
||||
(*this)(_case.body);
|
||||
joinStorageKnowledge(storage);
|
||||
|
||||
Assignments assignments;
|
||||
assignments(_case.body);
|
||||
assignedVariables += assignments.names();
|
||||
// This is a little too destructive, we could retain the old values.
|
||||
clearValues(assignments.names());
|
||||
clearStorageKnowledgeIfInvalidated(_case.body);
|
||||
}
|
||||
for (auto& _case: _switch.cases)
|
||||
clearStorageKnowledgeIfInvalidated(_case.body);
|
||||
clearValues(assignedVariables);
|
||||
}
|
||||
|
||||
@ -89,8 +131,10 @@ void DataFlowAnalyzer::operator()(FunctionDefinition& _fun)
|
||||
// but this could be difficult if it is subclassed.
|
||||
map<YulString, Expression const*> value;
|
||||
InvertibleRelation<YulString> references;
|
||||
InvertibleMap<YulString, YulString> storage;
|
||||
m_value.swap(value);
|
||||
swap(m_references, references);
|
||||
swap(m_storage, storage);
|
||||
pushScope(true);
|
||||
|
||||
for (auto const& parameter: _fun.parameters)
|
||||
@ -105,6 +149,7 @@ void DataFlowAnalyzer::operator()(FunctionDefinition& _fun)
|
||||
popScope();
|
||||
m_value.swap(value);
|
||||
swap(m_references, references);
|
||||
swap(m_storage, storage);
|
||||
}
|
||||
|
||||
void DataFlowAnalyzer::operator()(ForLoop& _for)
|
||||
@ -121,11 +166,20 @@ void DataFlowAnalyzer::operator()(ForLoop& _for)
|
||||
assignments(_for.post);
|
||||
clearValues(assignments.names());
|
||||
|
||||
// break/continue are tricky for storage and thus we almost always clear here.
|
||||
clearStorageKnowledgeIfInvalidated(*_for.condition);
|
||||
clearStorageKnowledgeIfInvalidated(_for.post);
|
||||
clearStorageKnowledgeIfInvalidated(_for.body);
|
||||
|
||||
visit(*_for.condition);
|
||||
(*this)(_for.body);
|
||||
clearValues(assignmentsSinceCont.names());
|
||||
clearStorageKnowledgeIfInvalidated(_for.body);
|
||||
(*this)(_for.post);
|
||||
clearValues(assignments.names());
|
||||
clearStorageKnowledgeIfInvalidated(*_for.condition);
|
||||
clearStorageKnowledgeIfInvalidated(_for.post);
|
||||
clearStorageKnowledgeIfInvalidated(_for.body);
|
||||
}
|
||||
|
||||
void DataFlowAnalyzer::operator()(Block& _block)
|
||||
@ -159,7 +213,13 @@ void DataFlowAnalyzer::handleAssignment(set<YulString> const& _variables, Expres
|
||||
|
||||
auto const& referencedVariables = movableChecker.referencedVariables();
|
||||
for (auto const& name: _variables)
|
||||
{
|
||||
m_references.set(name, referencedVariables);
|
||||
// assignment to slot denoted by "name"
|
||||
m_storage.eraseKey(name);
|
||||
// assignment to slot contents denoted by "name"
|
||||
m_storage.eraseValue(name);
|
||||
}
|
||||
}
|
||||
|
||||
void DataFlowAnalyzer::pushScope(bool _functionScope)
|
||||
@ -188,7 +248,18 @@ void DataFlowAnalyzer::clearValues(set<YulString> _variables)
|
||||
// This cannot be easily tested since the substitutions will be done
|
||||
// one by one on the fly, and the last line will just be add(1, 1)
|
||||
|
||||
// Clear variables that reference variables to be cleared.
|
||||
// First clear storage knowledge, because we do not have to clear
|
||||
// storage knowledge of variables whose expression has changed,
|
||||
// since the value is still unchanged.
|
||||
for (auto const& name: _variables)
|
||||
{
|
||||
// clear slot denoted by "name"
|
||||
m_storage.eraseKey(name);
|
||||
// clear slot contents denoted by "name"
|
||||
m_storage.eraseValue(name);
|
||||
}
|
||||
|
||||
// Also clear variables that reference variables to be cleared.
|
||||
for (auto const& name: _variables)
|
||||
for (auto const& ref: m_references.backward[name])
|
||||
_variables.emplace(ref);
|
||||
@ -200,6 +271,31 @@ void DataFlowAnalyzer::clearValues(set<YulString> _variables)
|
||||
m_references.eraseKey(name);
|
||||
}
|
||||
|
||||
void DataFlowAnalyzer::clearStorageKnowledgeIfInvalidated(Block const& _block)
|
||||
{
|
||||
if (SideEffectsCollector(m_dialect, _block).invalidatesStorage())
|
||||
m_storage.clear();
|
||||
}
|
||||
|
||||
void DataFlowAnalyzer::clearStorageKnowledgeIfInvalidated(Expression const& _expr)
|
||||
{
|
||||
if (SideEffectsCollector(m_dialect, _expr).invalidatesStorage())
|
||||
m_storage.clear();
|
||||
}
|
||||
|
||||
void DataFlowAnalyzer::joinStorageKnowledge(InvertibleMap<YulString, YulString> const& _other)
|
||||
{
|
||||
set<YulString> keysToErase;
|
||||
for (auto const& item: m_storage.values)
|
||||
{
|
||||
auto it = _other.values.find(item.first);
|
||||
if (it == _other.values.end() || it->second != item.second)
|
||||
keysToErase.insert(item.first);
|
||||
}
|
||||
for (auto const& key: keysToErase)
|
||||
m_storage.eraseKey(key);
|
||||
}
|
||||
|
||||
bool DataFlowAnalyzer::inScope(YulString _variableName) const
|
||||
{
|
||||
for (auto const& scope: m_variableScopes | boost::adaptors::reversed)
|
||||
@ -211,3 +307,27 @@ bool DataFlowAnalyzer::inScope(YulString _variableName) const
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
boost::optional<pair<YulString, YulString>> DataFlowAnalyzer::isSimpleSStore(
|
||||
ExpressionStatement const& _statement
|
||||
) const
|
||||
{
|
||||
if (_statement.expression.type() == typeid(FunctionCall))
|
||||
{
|
||||
FunctionCall const& funCall = boost::get<FunctionCall>(_statement.expression);
|
||||
if (EVMDialect const* dialect = dynamic_cast<EVMDialect const*>(&m_dialect))
|
||||
if (auto const* builtin = dialect->builtin(funCall.functionName.name))
|
||||
if (builtin->instruction == dev::eth::Instruction::SSTORE)
|
||||
if (
|
||||
funCall.arguments.at(0).type() == typeid(Identifier) &&
|
||||
funCall.arguments.at(1).type() == typeid(Identifier)
|
||||
)
|
||||
{
|
||||
YulString key = boost::get<Identifier>(funCall.arguments.at(0)).name;
|
||||
YulString value = boost::get<Identifier>(funCall.arguments.at(1)).name;
|
||||
return make_pair(key, value);
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
|
@ -23,6 +23,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <libyul/optimiser/ASTWalker.h>
|
||||
#include <libyul/optimiser/KnowledgeBase.h>
|
||||
#include <libyul/YulString.h>
|
||||
#include <libyul/AsmData.h>
|
||||
|
||||
@ -42,14 +43,34 @@ struct Dialect;
|
||||
*
|
||||
* A special zero constant expression is used for the default value of variables.
|
||||
*
|
||||
* The class also tracks contents in storage and memory. Both keys and values
|
||||
* are names of variables. Whenever such a variable is re-assigned, the knowledge
|
||||
* is cleared.
|
||||
*
|
||||
* For elementary statements, we check if it is an SSTORE(x, y) / MSTORE(x, y)
|
||||
* If yes, visit the statement. Then record that fact and clear all storage slots t
|
||||
* where we cannot prove x != t or y == m_storage[t] using the current values of the variables x and t.
|
||||
* Otherwise, determine if the statement invalidates storage/memory. If yes, clear all knowledge
|
||||
* about storage/memory before visiting the statement. Then visit the statement.
|
||||
*
|
||||
* For forward-joining control flow, storage/memory information from the branches is combined.
|
||||
* If the keys or values are different or non-existent in one branch, the key is deleted.
|
||||
* This works also for memory (where addresses overlap) because one branch is always an
|
||||
* older version of the other and thus overlapping contents would have been deleted already
|
||||
* at the point of assignment.
|
||||
*
|
||||
* Prerequisite: Disambiguator, ForLoopInitRewriter.
|
||||
*/
|
||||
class DataFlowAnalyzer: public ASTModifier
|
||||
{
|
||||
public:
|
||||
explicit DataFlowAnalyzer(Dialect const& _dialect): m_dialect(_dialect) {}
|
||||
explicit DataFlowAnalyzer(Dialect const& _dialect):
|
||||
m_dialect(_dialect),
|
||||
m_knowledgeBase(_dialect, m_value)
|
||||
{}
|
||||
|
||||
using ASTModifier::operator();
|
||||
void operator()(ExpressionStatement& _statement) override;
|
||||
void operator()(Assignment& _assignment) override;
|
||||
void operator()(VariableDeclaration& _varDecl) override;
|
||||
void operator()(If& _if) override;
|
||||
@ -72,15 +93,31 @@ protected:
|
||||
/// for example at points where control flow is merged.
|
||||
void clearValues(std::set<YulString> _names);
|
||||
|
||||
/// Clears knowledge about storage if storage may be modified inside the block.
|
||||
void clearStorageKnowledgeIfInvalidated(Block const& _block);
|
||||
|
||||
/// Clears knowledge about storage if storage may be modified inside the expression.
|
||||
void clearStorageKnowledgeIfInvalidated(Expression const& _expression);
|
||||
|
||||
void joinStorageKnowledge(InvertibleMap<YulString, YulString> const& _other);
|
||||
|
||||
/// Returns true iff the variable is in scope.
|
||||
bool inScope(YulString _variableName) const;
|
||||
|
||||
boost::optional<std::pair<YulString, YulString>> isSimpleSStore(ExpressionStatement const& _statement) const;
|
||||
|
||||
Dialect const& m_dialect;
|
||||
|
||||
/// Current values of variables, always movable.
|
||||
std::map<YulString, Expression const*> m_value;
|
||||
/// m_references.forward[a].contains(b) <=> the current expression assigned to a references b
|
||||
/// m_references.backward[b].contains(a) <=> the current expression assigned to a references b
|
||||
InvertibleRelation<YulString> m_references;
|
||||
|
||||
InvertibleMap<YulString, YulString> m_storage;
|
||||
|
||||
KnowledgeBase m_knowledgeBase;
|
||||
|
||||
struct Scope
|
||||
{
|
||||
explicit Scope(bool _isFunction): isFunction(_isFunction) {}
|
||||
@ -92,7 +129,6 @@ protected:
|
||||
Expression const m_zero{Literal{{}, LiteralKind::Number, YulString{"0"}, {}}};
|
||||
/// List of scopes.
|
||||
std::vector<Scope> m_variableScopes;
|
||||
Dialect const& m_dialect;
|
||||
};
|
||||
|
||||
}
|
||||
|
31
libyul/optimiser/KnowledgeBase.cpp
Normal file
31
libyul/optimiser/KnowledgeBase.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
/*
|
||||
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/>.
|
||||
*/
|
||||
/**
|
||||
* Class that can answer questions about values of variables and their relations.
|
||||
*/
|
||||
|
||||
#include <libyul/optimiser/KnowledgeBase.h>
|
||||
|
||||
using namespace yul;
|
||||
|
||||
bool KnowledgeBase::knownToBeDifferent(YulString, YulString) const
|
||||
{
|
||||
// TODO try to use the simplification rules together with the
|
||||
// current values to turn `sub(_a, _b)` into a nonzero constant.
|
||||
// If that fails, try `eq(_a, _b)`.
|
||||
return false;
|
||||
}
|
53
libyul/optimiser/KnowledgeBase.h
Normal file
53
libyul/optimiser/KnowledgeBase.h
Normal file
@ -0,0 +1,53 @@
|
||||
/*
|
||||
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/>.
|
||||
*/
|
||||
/**
|
||||
* Class that can answer questions about values of variables and their relations.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <libyul/AsmDataForward.h>
|
||||
#include <libyul/YulString.h>
|
||||
#include <map>
|
||||
|
||||
namespace yul
|
||||
{
|
||||
|
||||
class Dialect;
|
||||
|
||||
/**
|
||||
* Class that can answer questions about values of variables and their relations.
|
||||
*
|
||||
* The reference to the map of values provided at construction is assumed to be updating.
|
||||
*/
|
||||
class KnowledgeBase
|
||||
{
|
||||
public:
|
||||
KnowledgeBase(Dialect const& _dialect, std::map<YulString, Expression const*> const& _variableValues):
|
||||
m_dialect(_dialect),
|
||||
m_variableValues(_variableValues)
|
||||
{}
|
||||
|
||||
bool knownToBeDifferent(YulString _a, YulString _b) const;
|
||||
bool knownToBeEqual(YulString _a, YulString _b) const { return _a == _b; }
|
||||
|
||||
private:
|
||||
Dialect const& m_dialect;
|
||||
std::map<YulString, Expression const*> const& m_variableValues;
|
||||
};
|
||||
|
||||
}
|
48
libyul/optimiser/SLoadResolver.cpp
Normal file
48
libyul/optimiser/SLoadResolver.cpp
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
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/>.
|
||||
*/
|
||||
/**
|
||||
* Optimisation stage that replaces expressions of type ``sload(x)`` by the value
|
||||
* currently stored in storage, if known.
|
||||
*/
|
||||
|
||||
#include <libyul/optimiser/SLoadResolver.h>
|
||||
|
||||
#include <libyul/backends/evm/EVMDialect.h>
|
||||
#include <libyul/AsmData.h>
|
||||
|
||||
using namespace std;
|
||||
using namespace dev;
|
||||
using namespace yul;
|
||||
|
||||
void SLoadResolver::visit(Expression& _e)
|
||||
{
|
||||
if (_e.type() == typeid(FunctionCall))
|
||||
{
|
||||
FunctionCall const& funCall = boost::get<FunctionCall>(_e);
|
||||
if (auto const* builtin = dynamic_cast<EVMDialect const&>(m_dialect).builtin(funCall.functionName.name))
|
||||
if (builtin->instruction == dev::eth::Instruction::SLOAD)
|
||||
if (funCall.arguments.at(0).type() == typeid(Identifier))
|
||||
{
|
||||
YulString key = boost::get<Identifier>(funCall.arguments.at(0)).name;
|
||||
if (m_storage.values.count(key))
|
||||
{
|
||||
_e = Identifier{locationOf(_e), m_storage.values[key]};
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
49
libyul/optimiser/SLoadResolver.h
Normal file
49
libyul/optimiser/SLoadResolver.h
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
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/>.
|
||||
*/
|
||||
/**
|
||||
* Optimisation stage that replaces expressions of type ``sload(x)`` by the value
|
||||
* currently stored in storage, if known.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <libyul/optimiser/DataFlowAnalyzer.h>
|
||||
|
||||
namespace yul
|
||||
{
|
||||
|
||||
struct EVMDialect;
|
||||
|
||||
/**
|
||||
* Optimisation stage that replaces expressions of type ``sload(x)`` by the value
|
||||
* currently stored in storage, if known.
|
||||
*
|
||||
* Works best if the code is in SSA form.
|
||||
*
|
||||
* Prerequisite: Disambiguator, ForLoopInitRewriter.
|
||||
*/
|
||||
class SLoadResolver: public DataFlowAnalyzer
|
||||
{
|
||||
public:
|
||||
SLoadResolver(Dialect const& _dialect): DataFlowAnalyzer(_dialect) {}
|
||||
|
||||
protected:
|
||||
using ASTModifier::visit;
|
||||
void visit(Expression& _e) override;
|
||||
};
|
||||
|
||||
}
|
@ -63,6 +63,8 @@ void SideEffectsCollector::operator()(FunctionalInstruction const& _instr)
|
||||
m_sideEffectFreeIfNoMSize = false;
|
||||
if (_instr.instruction == eth::Instruction::MSIZE)
|
||||
m_containsMSize = true;
|
||||
if (eth::SemanticInformation::invalidatesStorage(_instr.instruction))
|
||||
m_invalidatesStorage = true;
|
||||
}
|
||||
|
||||
void SideEffectsCollector::operator()(FunctionCall const& _functionCall)
|
||||
@ -79,12 +81,15 @@ void SideEffectsCollector::operator()(FunctionCall const& _functionCall)
|
||||
m_sideEffectFreeIfNoMSize = false;
|
||||
if (f->isMSize)
|
||||
m_containsMSize = true;
|
||||
if (f->invalidatesStorage)
|
||||
m_invalidatesStorage = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_movable = false;
|
||||
m_sideEffectFree = false;
|
||||
m_sideEffectFreeIfNoMSize = false;
|
||||
m_invalidatesStorage = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -54,6 +54,7 @@ public:
|
||||
}
|
||||
bool sideEffectFreeIfNoMSize() const { return m_sideEffectFreeIfNoMSize; }
|
||||
bool containsMSize() const { return m_containsMSize; }
|
||||
bool invalidatesStorage() const { return m_invalidatesStorage; }
|
||||
|
||||
private:
|
||||
Dialect const& m_dialect;
|
||||
@ -69,6 +70,9 @@ private:
|
||||
/// Note that this is a purely syntactic property meaning that even if this is false,
|
||||
/// the code can still contain calls to functions that contain the msize instruction.
|
||||
bool m_containsMSize = false;
|
||||
/// If false, storage is guaranteed to be unchanged by the coded under all
|
||||
/// circumstances.
|
||||
bool m_invalidatesStorage = false;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -88,6 +92,7 @@ public:
|
||||
void visit(Statement const&) override;
|
||||
using ASTWalker::visit;
|
||||
|
||||
|
||||
std::set<YulString> const& referencedVariables() const { return m_variableReferences; }
|
||||
|
||||
private:
|
||||
|
@ -43,6 +43,7 @@
|
||||
#include <libyul/optimiser/ExpressionJoiner.h>
|
||||
#include <libyul/optimiser/SSAReverser.h>
|
||||
#include <libyul/optimiser/SSATransform.h>
|
||||
#include <libyul/optimiser/SLoadResolver.h>
|
||||
#include <libyul/optimiser/RedundantAssignEliminator.h>
|
||||
#include <libyul/optimiser/StructuralSimplifier.h>
|
||||
#include <libyul/optimiser/StackCompressor.h>
|
||||
@ -252,6 +253,21 @@ TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _line
|
||||
SSATransform::run(*m_ast, nameDispenser);
|
||||
RedundantAssignEliminator::run(*m_dialect, *m_ast);
|
||||
}
|
||||
else if (m_optimizerStep == "sloadResolver")
|
||||
{
|
||||
disambiguate();
|
||||
ForLoopInitRewriter{}(*m_ast);
|
||||
NameDispenser nameDispenser{*m_dialect, *m_ast};
|
||||
ExpressionSplitter{*m_dialect, nameDispenser}(*m_ast);
|
||||
CommonSubexpressionEliminator{*m_dialect}(*m_ast);
|
||||
ExpressionSimplifier::run(*m_dialect, *m_ast);
|
||||
|
||||
SLoadResolver{*m_dialect}(*m_ast);
|
||||
|
||||
UnusedPruner::runUntilStabilised(*m_dialect, *m_ast);
|
||||
ExpressionJoiner::run(*m_ast);
|
||||
ExpressionJoiner::run(*m_ast);
|
||||
}
|
||||
else if (m_optimizerStep == "controlFlowSimplifier")
|
||||
{
|
||||
disambiguate();
|
||||
|
15
test/libyul/yulOptimizerTests/fullSuite/storage.yul
Normal file
15
test/libyul/yulOptimizerTests/fullSuite/storage.yul
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
sstore(4, 5)
|
||||
sstore(4, 3)
|
||||
sstore(8, sload(4))
|
||||
}
|
||||
// ====
|
||||
// step: fullSuite
|
||||
// ----
|
||||
// {
|
||||
// {
|
||||
// sstore(4, 5)
|
||||
// sstore(4, 3)
|
||||
// sstore(8, sload(4))
|
||||
// }
|
||||
// }
|
16
test/libyul/yulOptimizerTests/sloadResolver/reassign.yul
Normal file
16
test/libyul/yulOptimizerTests/sloadResolver/reassign.yul
Normal file
@ -0,0 +1,16 @@
|
||||
{
|
||||
let a := calldataload(0)
|
||||
sstore(a, 6)
|
||||
a := calldataload(2)
|
||||
mstore(0, sload(a))
|
||||
}
|
||||
// ====
|
||||
// step: sloadResolver
|
||||
// ----
|
||||
// {
|
||||
// let _1 := 0
|
||||
// let a := calldataload(_1)
|
||||
// sstore(a, 6)
|
||||
// a := calldataload(2)
|
||||
// mstore(_1, sload(a))
|
||||
// }
|
@ -0,0 +1,32 @@
|
||||
{
|
||||
let x := calldataload(1)
|
||||
let a := add(x, 10)
|
||||
sstore(a, 7)
|
||||
// This clears the expression assigned to ``a`` but
|
||||
// should not clear storage knowledge
|
||||
x := 9
|
||||
mstore(sload(a), 11)
|
||||
// This, on the other hand, actually clears knowledge
|
||||
a := 33
|
||||
mstore(sload(a), 11)
|
||||
// Try again with different expression to avoid
|
||||
// clearing because we cannot know if it is different
|
||||
a := 39
|
||||
mstore(sload(a), 11)
|
||||
}
|
||||
// ====
|
||||
// step: sloadResolver
|
||||
// ----
|
||||
// {
|
||||
// let x := calldataload(1)
|
||||
// let a := add(x, 10)
|
||||
// let _3 := 7
|
||||
// sstore(a, _3)
|
||||
// x := 9
|
||||
// let _4 := 11
|
||||
// mstore(_3, _4)
|
||||
// a := 33
|
||||
// mstore(sload(a), _4)
|
||||
// a := 39
|
||||
// mstore(sload(a), _4)
|
||||
// }
|
19
test/libyul/yulOptimizerTests/sloadResolver/second_store.yul
Normal file
19
test/libyul/yulOptimizerTests/sloadResolver/second_store.yul
Normal file
@ -0,0 +1,19 @@
|
||||
{
|
||||
let x := calldataload(1)
|
||||
sstore(x, 7)
|
||||
sstore(calldataload(0), 6)
|
||||
// We cannot replace this because we do not know
|
||||
// if the two slots are different.
|
||||
mstore(0, sload(x))
|
||||
}
|
||||
// ====
|
||||
// step: sloadResolver
|
||||
// ----
|
||||
// {
|
||||
// let x := calldataload(1)
|
||||
// sstore(x, 7)
|
||||
// let _3 := 6
|
||||
// let _4 := 0
|
||||
// sstore(calldataload(_4), _3)
|
||||
// mstore(_4, sload(x))
|
||||
// }
|
@ -0,0 +1,19 @@
|
||||
{
|
||||
let x := calldataload(1)
|
||||
sstore(x, 7)
|
||||
sstore(calldataload(0), 7)
|
||||
// We can replace this because both values that were
|
||||
// written are 7.
|
||||
mstore(0, sload(x))
|
||||
}
|
||||
// ====
|
||||
// step: sloadResolver
|
||||
// ----
|
||||
// {
|
||||
// let x := calldataload(1)
|
||||
// let _2 := 7
|
||||
// sstore(x, _2)
|
||||
// let _4 := 0
|
||||
// sstore(calldataload(_4), _2)
|
||||
// mstore(_4, _2)
|
||||
// }
|
14
test/libyul/yulOptimizerTests/sloadResolver/simple.yul
Normal file
14
test/libyul/yulOptimizerTests/sloadResolver/simple.yul
Normal file
@ -0,0 +1,14 @@
|
||||
{
|
||||
sstore(calldataload(0), calldataload(10))
|
||||
let t := sload(calldataload(10))
|
||||
let q := sload(calldataload(0))
|
||||
mstore(t, q)
|
||||
}
|
||||
// ====
|
||||
// step: sloadResolver
|
||||
// ----
|
||||
// {
|
||||
// let _2 := calldataload(10)
|
||||
// sstore(calldataload(0), _2)
|
||||
// mstore(sload(_2), _2)
|
||||
// }
|
Loading…
Reference in New Issue
Block a user