mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Also optimize memory.
This commit is contained in:
parent
8572600401
commit
2b979cba38
@ -96,6 +96,8 @@ add_library(yul
|
||||
optimiser/InlinableExpressionFunctionFinder.h
|
||||
optimiser/KnowledgeBase.cpp
|
||||
optimiser/KnowledgeBase.h
|
||||
optimiser/LoadResolver.cpp
|
||||
optimiser/LoadResolver.h
|
||||
optimiser/MainFunction.cpp
|
||||
optimiser/MainFunction.h
|
||||
optimiser/Metrics.cpp
|
||||
@ -111,8 +113,6 @@ 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
|
||||
|
@ -60,6 +60,9 @@ struct BuiltinFunction
|
||||
/// 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 false, memory before and after the function is the same under every circumstance.
|
||||
/// If the function does not return, this can be false.
|
||||
bool invalidatesMemory = true;
|
||||
/// If true, can only accept literals as arguments and they cannot be moved to variables.
|
||||
bool literalArguments = false;
|
||||
};
|
||||
|
@ -55,6 +55,7 @@ pair<YulString, BuiltinFunctionForEVM> createEVMFunction(
|
||||
f.sideEffectFreeIfNoMSize = eth::SemanticInformation::sideEffectFreeIfNoMSize(_instruction);
|
||||
f.isMSize = _instruction == dev::eth::Instruction::MSIZE;
|
||||
f.invalidatesStorage = eth::SemanticInformation::invalidatesStorage(_instruction);
|
||||
f.invalidatesMemory = eth::SemanticInformation::invalidatesMemory(_instruction);
|
||||
f.literalArguments = false;
|
||||
f.instruction = _instruction;
|
||||
f.generateCode = [_instruction](
|
||||
@ -78,6 +79,7 @@ pair<YulString, BuiltinFunctionForEVM> createFunction(
|
||||
bool _sideEffectFree,
|
||||
bool _sideEffectFreeIfNoMSize,
|
||||
bool _invalidatesStorage,
|
||||
bool _invalidatesMemory,
|
||||
bool _literalArguments,
|
||||
std::function<void(FunctionCall const&, AbstractAssembly&, BuiltinContext&, std::function<void()>)> _generateCode
|
||||
)
|
||||
@ -93,6 +95,7 @@ pair<YulString, BuiltinFunctionForEVM> createFunction(
|
||||
f.sideEffectFreeIfNoMSize = _sideEffectFreeIfNoMSize;
|
||||
f.isMSize = false;
|
||||
f.invalidatesStorage = _invalidatesStorage;
|
||||
f.invalidatesMemory = _invalidatesMemory;
|
||||
f.instruction = {};
|
||||
f.generateCode = std::move(_generateCode);
|
||||
return {name, f};
|
||||
@ -113,7 +116,7 @@ map<YulString, BuiltinFunctionForEVM> createBuiltins(langutil::EVMVersion _evmVe
|
||||
|
||||
if (_objectAccess)
|
||||
{
|
||||
builtins.emplace(createFunction("datasize", 1, 1, true, true, true, false, true, [](
|
||||
builtins.emplace(createFunction("datasize", 1, 1, true, true, true, false, false, true, [](
|
||||
FunctionCall const& _call,
|
||||
AbstractAssembly& _assembly,
|
||||
BuiltinContext& _context,
|
||||
@ -134,7 +137,7 @@ map<YulString, BuiltinFunctionForEVM> createBuiltins(langutil::EVMVersion _evmVe
|
||||
_assembly.appendDataSize(_context.subIDs.at(dataName));
|
||||
}
|
||||
}));
|
||||
builtins.emplace(createFunction("dataoffset", 1, 1, true, true, true, false, true, [](
|
||||
builtins.emplace(createFunction("dataoffset", 1, 1, true, true, true, false, false, true, [](
|
||||
FunctionCall const& _call,
|
||||
AbstractAssembly& _assembly,
|
||||
BuiltinContext& _context,
|
||||
@ -155,7 +158,7 @@ map<YulString, BuiltinFunctionForEVM> createBuiltins(langutil::EVMVersion _evmVe
|
||||
_assembly.appendDataOffset(_context.subIDs.at(dataName));
|
||||
}
|
||||
}));
|
||||
builtins.emplace(createFunction("datacopy", 3, 0, false, false, false, false, false, [](
|
||||
builtins.emplace(createFunction("datacopy", 3, 0, false, false, false, false, true, false, [](
|
||||
FunctionCall const&,
|
||||
AbstractAssembly& _assembly,
|
||||
BuiltinContext&,
|
||||
|
@ -84,5 +84,6 @@ void WasmDialect::addFunction(string _name, size_t _params, size_t _returns)
|
||||
f.sideEffectFreeIfNoMSize = false;
|
||||
f.isMSize = false;
|
||||
f.invalidatesStorage = true;
|
||||
f.invalidatesMemory = true;
|
||||
f.literalArguments = false;
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ using namespace yul;
|
||||
|
||||
void DataFlowAnalyzer::operator()(ExpressionStatement& _statement)
|
||||
{
|
||||
if (boost::optional<pair<YulString, YulString>> vars = isSimpleSStore(_statement))
|
||||
if (auto vars = isSimpleStore(dev::eth::Instruction::SSTORE, _statement))
|
||||
{
|
||||
ASTModifier::operator()(_statement);
|
||||
m_storage.set(vars->first, vars->second);
|
||||
@ -54,9 +54,22 @@ void DataFlowAnalyzer::operator()(ExpressionStatement& _statement)
|
||||
for (YulString const& key: keysToErase)
|
||||
m_storage.eraseKey(key);
|
||||
}
|
||||
else if (auto vars = isSimpleStore(dev::eth::Instruction::MSTORE, _statement))
|
||||
{
|
||||
ASTModifier::operator()(_statement);
|
||||
set<YulString> keysToErase;
|
||||
for (auto const& item: m_memory.values)
|
||||
if (!m_knowledgeBase.knownToBeDifferentByAtLeast32(vars->first, item.first))
|
||||
keysToErase.insert(item.first);
|
||||
// TODO is it fine to do that here?
|
||||
// can we also move the storage above?
|
||||
m_memory.set(vars->first, vars->second);
|
||||
for (YulString const& key: keysToErase)
|
||||
m_memory.eraseKey(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
clearStorageKnowledgeIfInvalidated(_statement.expression);
|
||||
clearKnowledgeIfInvalidated(_statement.expression);
|
||||
ASTModifier::operator()(_statement);
|
||||
}
|
||||
}
|
||||
@ -67,7 +80,7 @@ void DataFlowAnalyzer::operator()(Assignment& _assignment)
|
||||
for (auto const& var: _assignment.variableNames)
|
||||
names.emplace(var.name);
|
||||
assertThrow(_assignment.value, OptimizerException, "");
|
||||
clearStorageKnowledgeIfInvalidated(*_assignment.value);
|
||||
clearKnowledgeIfInvalidated(*_assignment.value);
|
||||
visit(*_assignment.value);
|
||||
handleAssignment(names, _assignment.value.get());
|
||||
}
|
||||
@ -81,7 +94,7 @@ void DataFlowAnalyzer::operator()(VariableDeclaration& _varDecl)
|
||||
|
||||
if (_varDecl.value)
|
||||
{
|
||||
clearStorageKnowledgeIfInvalidated(*_varDecl.value);
|
||||
clearKnowledgeIfInvalidated(*_varDecl.value);
|
||||
visit(*_varDecl.value);
|
||||
}
|
||||
|
||||
@ -90,12 +103,13 @@ void DataFlowAnalyzer::operator()(VariableDeclaration& _varDecl)
|
||||
|
||||
void DataFlowAnalyzer::operator()(If& _if)
|
||||
{
|
||||
clearStorageKnowledgeIfInvalidated(*_if.condition);
|
||||
clearKnowledgeIfInvalidated(*_if.condition);
|
||||
InvertibleMap<YulString, YulString> storage = m_storage;
|
||||
InvertibleMap<YulString, YulString> memory = m_memory;
|
||||
|
||||
ASTModifier::operator()(_if);
|
||||
|
||||
joinStorageKnowledge(storage);
|
||||
joinKnowledge(storage, memory);
|
||||
|
||||
Assignments assignments;
|
||||
assignments(_if.body);
|
||||
@ -104,24 +118,25 @@ void DataFlowAnalyzer::operator()(If& _if)
|
||||
|
||||
void DataFlowAnalyzer::operator()(Switch& _switch)
|
||||
{
|
||||
clearStorageKnowledgeIfInvalidated(*_switch.expression);
|
||||
clearKnowledgeIfInvalidated(*_switch.expression);
|
||||
visit(*_switch.expression);
|
||||
set<YulString> assignedVariables;
|
||||
for (auto& _case: _switch.cases)
|
||||
{
|
||||
InvertibleMap<YulString, YulString> storage = m_storage;
|
||||
InvertibleMap<YulString, YulString> memory = m_memory;
|
||||
(*this)(_case.body);
|
||||
joinStorageKnowledge(storage);
|
||||
joinKnowledge(storage, memory);
|
||||
|
||||
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);
|
||||
clearKnowledgeIfInvalidated(_case.body);
|
||||
}
|
||||
for (auto& _case: _switch.cases)
|
||||
clearStorageKnowledgeIfInvalidated(_case.body);
|
||||
clearKnowledgeIfInvalidated(_case.body);
|
||||
clearValues(assignedVariables);
|
||||
}
|
||||
|
||||
@ -132,9 +147,11 @@ void DataFlowAnalyzer::operator()(FunctionDefinition& _fun)
|
||||
map<YulString, Expression const*> value;
|
||||
InvertibleRelation<YulString> references;
|
||||
InvertibleMap<YulString, YulString> storage;
|
||||
InvertibleMap<YulString, YulString> memory;
|
||||
m_value.swap(value);
|
||||
swap(m_references, references);
|
||||
swap(m_storage, storage);
|
||||
swap(m_memory, memory);
|
||||
pushScope(true);
|
||||
|
||||
for (auto const& parameter: _fun.parameters)
|
||||
@ -150,6 +167,7 @@ void DataFlowAnalyzer::operator()(FunctionDefinition& _fun)
|
||||
m_value.swap(value);
|
||||
swap(m_references, references);
|
||||
swap(m_storage, storage);
|
||||
swap(m_memory, memory);
|
||||
}
|
||||
|
||||
void DataFlowAnalyzer::operator()(ForLoop& _for)
|
||||
@ -167,19 +185,19 @@ void DataFlowAnalyzer::operator()(ForLoop& _for)
|
||||
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);
|
||||
clearKnowledgeIfInvalidated(*_for.condition);
|
||||
clearKnowledgeIfInvalidated(_for.post);
|
||||
clearKnowledgeIfInvalidated(_for.body);
|
||||
|
||||
visit(*_for.condition);
|
||||
(*this)(_for.body);
|
||||
clearValues(assignmentsSinceCont.names());
|
||||
clearStorageKnowledgeIfInvalidated(_for.body);
|
||||
clearKnowledgeIfInvalidated(_for.body);
|
||||
(*this)(_for.post);
|
||||
clearValues(assignments.names());
|
||||
clearStorageKnowledgeIfInvalidated(*_for.condition);
|
||||
clearStorageKnowledgeIfInvalidated(_for.post);
|
||||
clearStorageKnowledgeIfInvalidated(_for.body);
|
||||
clearKnowledgeIfInvalidated(*_for.condition);
|
||||
clearKnowledgeIfInvalidated(_for.post);
|
||||
clearKnowledgeIfInvalidated(_for.body);
|
||||
}
|
||||
|
||||
void DataFlowAnalyzer::operator()(Block& _block)
|
||||
@ -219,6 +237,10 @@ void DataFlowAnalyzer::handleAssignment(set<YulString> const& _variables, Expres
|
||||
m_storage.eraseKey(name);
|
||||
// assignment to slot contents denoted by "name"
|
||||
m_storage.eraseValue(name);
|
||||
// assignment to slot denoted by "name"
|
||||
m_memory.eraseKey(name);
|
||||
// assignment to slot contents denoted by "name"
|
||||
m_memory.eraseValue(name);
|
||||
}
|
||||
}
|
||||
|
||||
@ -257,6 +279,10 @@ void DataFlowAnalyzer::clearValues(set<YulString> _variables)
|
||||
m_storage.eraseKey(name);
|
||||
// clear slot contents denoted by "name"
|
||||
m_storage.eraseValue(name);
|
||||
// assignment to slot denoted by "name"
|
||||
m_memory.eraseKey(name);
|
||||
// assignment to slot contents denoted by "name"
|
||||
m_memory.eraseValue(name);
|
||||
}
|
||||
|
||||
// Also clear variables that reference variables to be cleared.
|
||||
@ -271,29 +297,51 @@ void DataFlowAnalyzer::clearValues(set<YulString> _variables)
|
||||
m_references.eraseKey(name);
|
||||
}
|
||||
|
||||
void DataFlowAnalyzer::clearStorageKnowledgeIfInvalidated(Block const& _block)
|
||||
void DataFlowAnalyzer::clearKnowledgeIfInvalidated(Block const& _block)
|
||||
{
|
||||
if (SideEffectsCollector(m_dialect, _block).invalidatesStorage())
|
||||
SideEffectsCollector sideEffects(m_dialect, _block);
|
||||
if (sideEffects.invalidatesStorage())
|
||||
m_storage.clear();
|
||||
if (sideEffects.invalidatesMemory())
|
||||
m_memory.clear();
|
||||
}
|
||||
|
||||
void DataFlowAnalyzer::clearStorageKnowledgeIfInvalidated(Expression const& _expr)
|
||||
void DataFlowAnalyzer::clearKnowledgeIfInvalidated(Expression const& _expr)
|
||||
{
|
||||
if (SideEffectsCollector(m_dialect, _expr).invalidatesStorage())
|
||||
SideEffectsCollector sideEffects(m_dialect, _expr);
|
||||
if (sideEffects.invalidatesStorage())
|
||||
m_storage.clear();
|
||||
if (sideEffects.invalidatesMemory())
|
||||
m_memory.clear();
|
||||
}
|
||||
|
||||
void DataFlowAnalyzer::joinStorageKnowledge(InvertibleMap<YulString, YulString> const& _other)
|
||||
void DataFlowAnalyzer::joinKnowledge(
|
||||
InvertibleMap<YulString, YulString> const& _olderStorage,
|
||||
InvertibleMap<YulString, YulString> const& _olderMemory
|
||||
)
|
||||
{
|
||||
joinKnowledgeHelper(m_storage, _olderStorage);
|
||||
joinKnowledgeHelper(m_memory, _olderMemory);
|
||||
}
|
||||
|
||||
void DataFlowAnalyzer::joinKnowledgeHelper(
|
||||
InvertibleMap<YulString, YulString>& _this,
|
||||
InvertibleMap<YulString, YulString> const& _older
|
||||
)
|
||||
{
|
||||
// We clear if the key does not exist in the older map or if the value is different.
|
||||
// This also works for memory because _older is an "older version"
|
||||
// of m_memory and thus any overlapping write would have cleared the keys
|
||||
// that are not known to be different inside m_memory already.
|
||||
set<YulString> keysToErase;
|
||||
for (auto const& item: m_storage.values)
|
||||
for (auto const& item: _this.values)
|
||||
{
|
||||
auto it = _other.values.find(item.first);
|
||||
if (it == _other.values.end() || it->second != item.second)
|
||||
auto it = _older.values.find(item.first);
|
||||
if (it == _older.values.end() || it->second != item.second)
|
||||
keysToErase.insert(item.first);
|
||||
}
|
||||
for (auto const& key: keysToErase)
|
||||
m_storage.eraseKey(key);
|
||||
_this.eraseKey(key);
|
||||
}
|
||||
|
||||
bool DataFlowAnalyzer::inScope(YulString _variableName) const
|
||||
@ -308,16 +356,22 @@ bool DataFlowAnalyzer::inScope(YulString _variableName) const
|
||||
return false;
|
||||
}
|
||||
|
||||
boost::optional<pair<YulString, YulString>> DataFlowAnalyzer::isSimpleSStore(
|
||||
boost::optional<pair<YulString, YulString>> DataFlowAnalyzer::isSimpleStore(
|
||||
dev::eth::Instruction _store,
|
||||
ExpressionStatement const& _statement
|
||||
) const
|
||||
{
|
||||
yulAssert(
|
||||
_store == dev::eth::Instruction::MSTORE ||
|
||||
_store == dev::eth::Instruction::SSTORE,
|
||||
""
|
||||
);
|
||||
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 (builtin->instruction == _store)
|
||||
if (
|
||||
funCall.arguments.at(0).type() == typeid(Identifier) &&
|
||||
funCall.arguments.at(1).type() == typeid(Identifier)
|
||||
|
@ -27,6 +27,9 @@
|
||||
#include <libyul/YulString.h>
|
||||
#include <libyul/AsmData.h>
|
||||
|
||||
// TODO avoid
|
||||
#include <libevmasm/Instruction.h>
|
||||
|
||||
#include <libdevcore/InvertibleMap.h>
|
||||
|
||||
#include <map>
|
||||
@ -93,18 +96,32 @@ 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 or memory if they may be modified inside the block.
|
||||
void clearKnowledgeIfInvalidated(Block const& _block);
|
||||
|
||||
/// Clears knowledge about storage if storage may be modified inside the expression.
|
||||
void clearStorageKnowledgeIfInvalidated(Expression const& _expression);
|
||||
/// Clears knowledge about storage or memory if they may be modified inside the expression.
|
||||
void clearKnowledgeIfInvalidated(Expression const& _expression);
|
||||
|
||||
void joinStorageKnowledge(InvertibleMap<YulString, YulString> const& _other);
|
||||
/// Joins knowledge about storage and memory with an older point in the control-flow.
|
||||
/// This only works if the current state is a direct successor of the older point,
|
||||
/// i.e. `_otherStorage` and `_otherMemory` cannot have additional changes.
|
||||
void joinKnowledge(
|
||||
InvertibleMap<YulString, YulString> const& _olderStorage,
|
||||
InvertibleMap<YulString, YulString> const& _olderMemory
|
||||
);
|
||||
|
||||
static void joinKnowledgeHelper(
|
||||
InvertibleMap<YulString, YulString>& _thisData,
|
||||
InvertibleMap<YulString, YulString> const& _olderData
|
||||
);
|
||||
|
||||
/// Returns true iff the variable is in scope.
|
||||
bool inScope(YulString _variableName) const;
|
||||
|
||||
boost::optional<std::pair<YulString, YulString>> isSimpleSStore(ExpressionStatement const& _statement) const;
|
||||
boost::optional<std::pair<YulString, YulString>> isSimpleStore(
|
||||
dev::eth::Instruction _store,
|
||||
ExpressionStatement const& _statement
|
||||
) const;
|
||||
|
||||
Dialect const& m_dialect;
|
||||
|
||||
@ -115,6 +132,7 @@ protected:
|
||||
InvertibleRelation<YulString> m_references;
|
||||
|
||||
InvertibleMap<YulString, YulString> m_storage;
|
||||
InvertibleMap<YulString, YulString> m_memory;
|
||||
|
||||
KnowledgeBase m_knowledgeBase;
|
||||
|
||||
|
@ -19,30 +19,48 @@
|
||||
* currently stored in storage, if known.
|
||||
*/
|
||||
|
||||
#include <libyul/optimiser/SLoadResolver.h>
|
||||
#include <libyul/optimiser/LoadResolver.h>
|
||||
|
||||
#include <libyul/backends/evm/EVMDialect.h>
|
||||
#include <libyul/optimiser/Semantics.h>
|
||||
#include <libyul/AsmData.h>
|
||||
|
||||
using namespace std;
|
||||
using namespace dev;
|
||||
using namespace yul;
|
||||
|
||||
void SLoadResolver::visit(Expression& _e)
|
||||
void LoadResolver::run(Dialect const& _dialect, Block& _ast)
|
||||
{
|
||||
bool containsMSize = SideEffectsCollector(_dialect, _ast).containsMSize();
|
||||
LoadResolver{_dialect, !containsMSize}(_ast);
|
||||
}
|
||||
|
||||
void LoadResolver::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))
|
||||
if (!builtin->parameters.empty() && funCall.arguments.at(0).type() == typeid(Identifier))
|
||||
{
|
||||
YulString key = boost::get<Identifier>(funCall.arguments.at(0)).name;
|
||||
if (
|
||||
builtin->instruction == dev::eth::Instruction::SLOAD &&
|
||||
m_storage.values.count(key)
|
||||
)
|
||||
{
|
||||
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;
|
||||
}
|
||||
_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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -29,21 +29,29 @@ namespace yul
|
||||
struct EVMDialect;
|
||||
|
||||
/**
|
||||
* Optimisation stage that replaces expressions of type ``sload(x)`` by the value
|
||||
* currently stored in storage, if known.
|
||||
* Optimisation stage that replaces expressions of type ``sload(x)`` and ``mload(x)`` by the value
|
||||
* currently stored in storage resp. memory, if known.
|
||||
*
|
||||
* Works best if the code is in SSA form.
|
||||
*
|
||||
* Prerequisite: Disambiguator, ForLoopInitRewriter.
|
||||
*/
|
||||
class SLoadResolver: public DataFlowAnalyzer
|
||||
class LoadResolver: public DataFlowAnalyzer
|
||||
{
|
||||
public:
|
||||
SLoadResolver(Dialect const& _dialect): DataFlowAnalyzer(_dialect) {}
|
||||
static void run(Dialect const& _dialect, Block& _ast);
|
||||
|
||||
private:
|
||||
LoadResolver(Dialect const& _dialect, bool _optimizeMLoad):
|
||||
DataFlowAnalyzer(_dialect),
|
||||
m_optimizeMLoad(_optimizeMLoad)
|
||||
{}
|
||||
|
||||
protected:
|
||||
using ASTModifier::visit;
|
||||
void visit(Expression& _e) override;
|
||||
|
||||
bool m_optimizeMLoad = false;
|
||||
};
|
||||
|
||||
}
|
@ -65,6 +65,8 @@ void SideEffectsCollector::operator()(FunctionalInstruction const& _instr)
|
||||
m_containsMSize = true;
|
||||
if (eth::SemanticInformation::invalidatesStorage(_instr.instruction))
|
||||
m_invalidatesStorage = true;
|
||||
if (eth::SemanticInformation::invalidatesMemory(_instr.instruction))
|
||||
m_invalidatesMemory = true;
|
||||
}
|
||||
|
||||
void SideEffectsCollector::operator()(FunctionCall const& _functionCall)
|
||||
@ -83,6 +85,8 @@ void SideEffectsCollector::operator()(FunctionCall const& _functionCall)
|
||||
m_containsMSize = true;
|
||||
if (f->invalidatesStorage)
|
||||
m_invalidatesStorage = true;
|
||||
if (f->invalidatesMemory)
|
||||
m_invalidatesMemory = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -90,6 +94,7 @@ void SideEffectsCollector::operator()(FunctionCall const& _functionCall)
|
||||
m_sideEffectFree = false;
|
||||
m_sideEffectFreeIfNoMSize = false;
|
||||
m_invalidatesStorage = true;
|
||||
m_invalidatesMemory = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -55,6 +55,7 @@ public:
|
||||
bool sideEffectFreeIfNoMSize() const { return m_sideEffectFreeIfNoMSize; }
|
||||
bool containsMSize() const { return m_containsMSize; }
|
||||
bool invalidatesStorage() const { return m_invalidatesStorage; }
|
||||
bool invalidatesMemory() const { return m_invalidatesMemory; }
|
||||
|
||||
private:
|
||||
Dialect const& m_dialect;
|
||||
@ -73,6 +74,7 @@ private:
|
||||
/// If false, storage is guaranteed to be unchanged by the coded under all
|
||||
/// circumstances.
|
||||
bool m_invalidatesStorage = false;
|
||||
bool m_invalidatesMemory = false;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -92,7 +94,6 @@ public:
|
||||
void visit(Statement const&) override;
|
||||
using ASTWalker::visit;
|
||||
|
||||
|
||||
std::set<YulString> const& referencedVariables() const { return m_variableReferences; }
|
||||
|
||||
private:
|
||||
|
@ -35,6 +35,7 @@
|
||||
#include <libyul/optimiser/FullInliner.h>
|
||||
#include <libyul/optimiser/ForLoopConditionIntoBody.h>
|
||||
#include <libyul/optimiser/ForLoopInitRewriter.h>
|
||||
#include <libyul/optimiser/LoadResolver.h>
|
||||
#include <libyul/optimiser/MainFunction.h>
|
||||
#include <libyul/optimiser/NameDisplacer.h>
|
||||
#include <libyul/optimiser/Rematerialiser.h>
|
||||
@ -43,7 +44,6 @@
|
||||
#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>
|
||||
@ -253,7 +253,7 @@ 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")
|
||||
else if (m_optimizerStep == "loadResolver")
|
||||
{
|
||||
disambiguate();
|
||||
ForLoopInitRewriter{}(*m_ast);
|
||||
@ -262,7 +262,7 @@ TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _line
|
||||
CommonSubexpressionEliminator{*m_dialect}(*m_ast);
|
||||
ExpressionSimplifier::run(*m_dialect, *m_ast);
|
||||
|
||||
SLoadResolver{*m_dialect}(*m_ast);
|
||||
LoadResolver::run(*m_dialect, *m_ast);
|
||||
|
||||
UnusedPruner::runUntilStabilised(*m_dialect, *m_ast);
|
||||
ExpressionJoiner::run(*m_ast);
|
||||
|
@ -0,0 +1,38 @@
|
||||
{
|
||||
mstore(2, 9)
|
||||
sstore(0, mload(2))
|
||||
pop(call(0, 0, 0, 0, 0, 0, 0))
|
||||
sstore(0, mload(2))
|
||||
|
||||
mstore(2, 10)
|
||||
mstore8(calldataload(0), 4)
|
||||
sstore(0, mload(2))
|
||||
|
||||
mstore(2, 10)
|
||||
g()
|
||||
sstore(0, mload(2))
|
||||
|
||||
function g() {}
|
||||
}
|
||||
// ====
|
||||
// step: loadResolver
|
||||
// ----
|
||||
// {
|
||||
// let _1 := 9
|
||||
// let _2 := 2
|
||||
// mstore(_2, _1)
|
||||
// let _4 := _1
|
||||
// let _5 := 0
|
||||
// sstore(_5, _4)
|
||||
// pop(call(_5, _5, _5, _5, _5, _5, _5))
|
||||
// sstore(_5, mload(_2))
|
||||
// let _17 := 10
|
||||
// mstore(_2, _17)
|
||||
// mstore8(calldataload(_5), 4)
|
||||
// sstore(_5, mload(_2))
|
||||
// mstore(_2, _17)
|
||||
// g()
|
||||
// sstore(_5, mload(_2))
|
||||
// function g()
|
||||
// { }
|
||||
// }
|
@ -0,0 +1,17 @@
|
||||
{
|
||||
// No mload removal because of msize
|
||||
mstore(calldataload(0), msize())
|
||||
let t := mload(calldataload(10))
|
||||
let q := mload(calldataload(0))
|
||||
sstore(t, q)
|
||||
}
|
||||
// ====
|
||||
// step: loadResolver
|
||||
// ----
|
||||
// {
|
||||
// let _1 := msize()
|
||||
// let _3 := calldataload(0)
|
||||
// mstore(_3, _1)
|
||||
// let t := mload(calldataload(10))
|
||||
// sstore(t, mload(_3))
|
||||
// }
|
@ -0,0 +1,22 @@
|
||||
{
|
||||
mstore(calldataload(0), calldataload(10))
|
||||
if calldataload(1) {
|
||||
mstore(calldataload(0), 1)
|
||||
}
|
||||
let t := mload(0)
|
||||
let q := mload(calldataload(0))
|
||||
sstore(t, q)
|
||||
}
|
||||
// ====
|
||||
// step: loadResolver
|
||||
// ----
|
||||
// {
|
||||
// let _2 := calldataload(10)
|
||||
// let _3 := 0
|
||||
// let _4 := calldataload(_3)
|
||||
// mstore(_4, _2)
|
||||
// let _5 := 1
|
||||
// if calldataload(_5) { mstore(_4, _5) }
|
||||
// let t := mload(_3)
|
||||
// sstore(t, mload(_4))
|
||||
// }
|
@ -0,0 +1,20 @@
|
||||
{
|
||||
mstore(calldataload(0), calldataload(10))
|
||||
if calldataload(1) {
|
||||
mstore(add(calldataload(0), 0x20), 1)
|
||||
}
|
||||
let t := mload(add(calldataload(0), 0x20))
|
||||
let q := mload(calldataload(0))
|
||||
sstore(t, q)
|
||||
}
|
||||
// ====
|
||||
// step: loadResolver
|
||||
// ----
|
||||
// {
|
||||
// let _2 := calldataload(10)
|
||||
// let _4 := calldataload(0)
|
||||
// mstore(_4, _2)
|
||||
// let _5 := 1
|
||||
// if calldataload(_5) { mstore(add(_4, 0x20), _5) }
|
||||
// sstore(mload(add(_4, 0x20)), _2)
|
||||
// }
|
@ -0,0 +1,22 @@
|
||||
{
|
||||
mstore(calldataload(0), calldataload(10))
|
||||
if calldataload(1) {
|
||||
mstore(0, 1)
|
||||
}
|
||||
let t := mload(0)
|
||||
let q := mload(calldataload(0))
|
||||
sstore(t, q)
|
||||
}
|
||||
// ====
|
||||
// step: loadResolver
|
||||
// ----
|
||||
// {
|
||||
// let _2 := calldataload(10)
|
||||
// let _3 := 0
|
||||
// let _4 := calldataload(_3)
|
||||
// mstore(_4, _2)
|
||||
// let _5 := 1
|
||||
// if calldataload(_5) { mstore(_3, _5) }
|
||||
// let t := mload(_3)
|
||||
// sstore(t, mload(_4))
|
||||
// }
|
@ -5,7 +5,7 @@
|
||||
mstore(0, sload(a))
|
||||
}
|
||||
// ====
|
||||
// step: sloadResolver
|
||||
// step: loadResolver
|
||||
// ----
|
||||
// {
|
||||
// let _1 := 0
|
@ -15,7 +15,7 @@
|
||||
mstore(sload(a), 11)
|
||||
}
|
||||
// ====
|
||||
// step: sloadResolver
|
||||
// step: loadResolver
|
||||
// ----
|
||||
// {
|
||||
// let x := calldataload(1)
|
@ -0,0 +1,24 @@
|
||||
{
|
||||
let x := calldataload(1)
|
||||
let a := add(x, 10)
|
||||
let b := add(x, 42)
|
||||
mstore(a, 7)
|
||||
// does not invalidate the first store, because the
|
||||
// difference is larger than 32, even if the absolute
|
||||
// values are unknown
|
||||
mstore(b, 8)
|
||||
sstore(mload(a), mload(b))
|
||||
}
|
||||
// ====
|
||||
// step: loadResolver
|
||||
// ----
|
||||
// {
|
||||
// let x := calldataload(1)
|
||||
// let a := add(x, 10)
|
||||
// let b := add(x, 42)
|
||||
// let _4 := 7
|
||||
// mstore(a, _4)
|
||||
// let _5 := 8
|
||||
// mstore(b, _5)
|
||||
// sstore(_4, _5)
|
||||
// }
|
@ -7,7 +7,7 @@
|
||||
mstore(0, sload(x))
|
||||
}
|
||||
// ====
|
||||
// step: sloadResolver
|
||||
// step: loadResolver
|
||||
// ----
|
||||
// {
|
||||
// let x := calldataload(1)
|
@ -7,7 +7,7 @@
|
||||
mstore(0, sload(x))
|
||||
}
|
||||
// ====
|
||||
// step: sloadResolver
|
||||
// step: loadResolver
|
||||
// ----
|
||||
// {
|
||||
// let x := calldataload(1)
|
@ -10,7 +10,7 @@
|
||||
mstore(sload(a), sload(b))
|
||||
}
|
||||
// ====
|
||||
// step: sloadResolver
|
||||
// step: loadResolver
|
||||
// ----
|
||||
// {
|
||||
// let x := calldataload(1)
|
@ -5,7 +5,7 @@
|
||||
mstore(t, q)
|
||||
}
|
||||
// ====
|
||||
// step: sloadResolver
|
||||
// step: loadResolver
|
||||
// ----
|
||||
// {
|
||||
// let _2 := calldataload(10)
|
14
test/libyul/yulOptimizerTests/loadResolver/simple_memory.yul
Normal file
14
test/libyul/yulOptimizerTests/loadResolver/simple_memory.yul
Normal file
@ -0,0 +1,14 @@
|
||||
{
|
||||
mstore(calldataload(0), calldataload(10))
|
||||
let t := mload(calldataload(10))
|
||||
let q := mload(calldataload(0))
|
||||
sstore(t, q)
|
||||
}
|
||||
// ====
|
||||
// step: loadResolver
|
||||
// ----
|
||||
// {
|
||||
// let _2 := calldataload(10)
|
||||
// mstore(calldataload(0), _2)
|
||||
// sstore(mload(_2), _2)
|
||||
// }
|
Loading…
Reference in New Issue
Block a user