Extract side effects into their own struct.

This commit is contained in:
chriseth
2019-08-14 15:06:10 +02:00
parent d5744b3c1c
commit 7d30fbdef0
8 changed files with 136 additions and 107 deletions
+4 -29
View File
@@ -33,6 +33,7 @@ using namespace std;
using namespace dev;
using namespace yul;
SideEffectsCollector::SideEffectsCollector(Dialect const& _dialect, Expression const& _expression):
SideEffectsCollector(_dialect)
{
@@ -55,16 +56,7 @@ void SideEffectsCollector::operator()(FunctionalInstruction const& _instr)
{
ASTWalker::operator()(_instr);
if (!eth::SemanticInformation::movable(_instr.instruction))
m_movable = false;
if (!eth::SemanticInformation::sideEffectFree(_instr.instruction))
m_sideEffectFree = false;
if (!eth::SemanticInformation::sideEffectFreeIfNoMSize(_instr.instruction))
m_sideEffectFreeIfNoMSize = false;
if (eth::SemanticInformation::invalidatesStorage(_instr.instruction))
m_invalidatesStorage = true;
if (eth::SemanticInformation::invalidatesMemory(_instr.instruction))
m_invalidatesMemory = true;
m_sideEffects += EVMDialect::sideEffectsOfInstruction(_instr.instruction);
}
void SideEffectsCollector::operator()(FunctionCall const& _functionCall)
@@ -72,26 +64,9 @@ void SideEffectsCollector::operator()(FunctionCall const& _functionCall)
ASTWalker::operator()(_functionCall);
if (BuiltinFunction const* f = m_dialect.builtin(_functionCall.functionName.name))
{
if (!f->movable)
m_movable = false;
if (!f->sideEffectFree)
m_sideEffectFree = false;
if (!f->sideEffectFreeIfNoMSize)
m_sideEffectFreeIfNoMSize = false;
if (f->invalidatesStorage)
m_invalidatesStorage = true;
if (f->invalidatesMemory)
m_invalidatesMemory = true;
}
m_sideEffects += f->sideEffects;
else
{
m_movable = false;
m_sideEffectFree = false;
m_sideEffectFreeIfNoMSize = false;
m_invalidatesStorage = true;
m_invalidatesMemory = true;
}
m_sideEffects += SideEffects::worst();
}
bool MSizeFinder::containsMSize(Dialect const& _dialect, Block const& _ast)
+7 -17
View File
@@ -21,6 +21,7 @@
#pragma once
#include <libyul/optimiser/ASTWalker.h>
#include <libyul/SideEffects.h>
#include <set>
@@ -44,32 +45,21 @@ public:
void operator()(FunctionalInstruction const& _functionalInstruction) override;
void operator()(FunctionCall const& _functionCall) override;
bool movable() const { return m_movable; }
bool movable() const { return m_sideEffects.movable; }
bool sideEffectFree(bool _allowMSizeModification = false) const
{
if (_allowMSizeModification)
return sideEffectFreeIfNoMSize();
else
return m_sideEffectFree;
return m_sideEffects.sideEffectFree;
}
bool sideEffectFreeIfNoMSize() const { return m_sideEffectFreeIfNoMSize; }
bool invalidatesStorage() const { return m_invalidatesStorage; }
bool invalidatesMemory() const { return m_invalidatesMemory; }
bool sideEffectFreeIfNoMSize() const { return m_sideEffects.sideEffectFreeIfNoMSize; }
bool invalidatesStorage() const { return m_sideEffects.invalidatesStorage; }
bool invalidatesMemory() const { return m_sideEffects.invalidatesMemory; }
private:
Dialect const& m_dialect;
/// Is the current expression movable or not.
bool m_movable = true;
/// Is the current expression side-effect free, i.e. can be removed
/// without changing the semantics.
bool m_sideEffectFree = true;
/// Is the current expression side-effect free up to msize, i.e. can be removed
/// without changing the semantics except for the value returned by the msize instruction.
bool m_sideEffectFreeIfNoMSize = true;
/// If false, storage is guaranteed to be unchanged by the code under all
/// circumstances.
bool m_invalidatesStorage = false;
bool m_invalidatesMemory = false;
SideEffects m_sideEffects;
};
/**