mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Refactor SideEffects struct
This commit is contained in:
parent
58bfe0b0d2
commit
3cbe65e4f3
@ -155,8 +155,10 @@ KnownState::StoreOperation KnownState::feedItem(AssemblyItem const& _item, bool
|
|||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
bool invMem = SemanticInformation::invalidatesMemory(_item.instruction());
|
bool invMem =
|
||||||
bool invStor = SemanticInformation::invalidatesStorage(_item.instruction());
|
SemanticInformation::memory(_item.instruction()) == SemanticInformation::Write;
|
||||||
|
bool invStor =
|
||||||
|
SemanticInformation::storage(_item.instruction()) == SemanticInformation::Write;
|
||||||
// We could be a bit more fine-grained here (CALL only invalidates part of
|
// We could be a bit more fine-grained here (CALL only invalidates part of
|
||||||
// memory, etc), but we do not for now.
|
// memory, etc), but we do not for now.
|
||||||
if (invMem)
|
if (invMem)
|
||||||
@ -420,4 +422,3 @@ KnownState::Id KnownState::tagUnion(set<u256> _tags)
|
|||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -233,7 +233,7 @@ bool SemanticInformation::movable(Instruction _instruction)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SemanticInformation::sideEffectFree(Instruction _instruction)
|
bool SemanticInformation::canBeRemoved(Instruction _instruction)
|
||||||
{
|
{
|
||||||
// These are not really functional.
|
// These are not really functional.
|
||||||
assertThrow(!isDupInstruction(_instruction) && !isSwapInstruction(_instruction), AssemblyException, "");
|
assertThrow(!isDupInstruction(_instruction) && !isSwapInstruction(_instruction), AssemblyException, "");
|
||||||
@ -241,15 +241,15 @@ bool SemanticInformation::sideEffectFree(Instruction _instruction)
|
|||||||
return !instructionInfo(_instruction).sideEffects;
|
return !instructionInfo(_instruction).sideEffects;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SemanticInformation::sideEffectFreeIfNoMSize(Instruction _instruction)
|
bool SemanticInformation::canBeRemovedIfNoMSize(Instruction _instruction)
|
||||||
{
|
{
|
||||||
if (_instruction == Instruction::KECCAK256 || _instruction == Instruction::MLOAD)
|
if (_instruction == Instruction::KECCAK256 || _instruction == Instruction::MLOAD)
|
||||||
return true;
|
return true;
|
||||||
else
|
else
|
||||||
return sideEffectFree(_instruction);
|
return canBeRemoved(_instruction);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SemanticInformation::invalidatesMemory(Instruction _instruction)
|
SemanticInformation::Effect SemanticInformation::memory(Instruction _instruction)
|
||||||
{
|
{
|
||||||
switch (_instruction)
|
switch (_instruction)
|
||||||
{
|
{
|
||||||
@ -263,13 +263,47 @@ bool SemanticInformation::invalidatesMemory(Instruction _instruction)
|
|||||||
case Instruction::CALLCODE:
|
case Instruction::CALLCODE:
|
||||||
case Instruction::DELEGATECALL:
|
case Instruction::DELEGATECALL:
|
||||||
case Instruction::STATICCALL:
|
case Instruction::STATICCALL:
|
||||||
return true;
|
return SemanticInformation::Write;
|
||||||
|
|
||||||
|
case Instruction::CREATE:
|
||||||
|
case Instruction::CREATE2:
|
||||||
|
case Instruction::KECCAK256:
|
||||||
|
case Instruction::MLOAD:
|
||||||
|
case Instruction::MSIZE:
|
||||||
|
case Instruction::RETURN:
|
||||||
|
case Instruction::REVERT:
|
||||||
|
case Instruction::LOG0:
|
||||||
|
case Instruction::LOG1:
|
||||||
|
case Instruction::LOG2:
|
||||||
|
case Instruction::LOG3:
|
||||||
|
case Instruction::LOG4:
|
||||||
|
return SemanticInformation::Read;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return false;
|
return SemanticInformation::None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SemanticInformation::invalidatesStorage(Instruction _instruction)
|
bool SemanticInformation::movableApartFromEffects(Instruction _instruction)
|
||||||
|
{
|
||||||
|
switch (_instruction)
|
||||||
|
{
|
||||||
|
case Instruction::EXTCODEHASH:
|
||||||
|
case Instruction::EXTCODESIZE:
|
||||||
|
case Instruction::RETURNDATASIZE:
|
||||||
|
case Instruction::BALANCE:
|
||||||
|
case Instruction::SELFBALANCE:
|
||||||
|
case Instruction::SLOAD:
|
||||||
|
case Instruction::KECCAK256:
|
||||||
|
case Instruction::MLOAD:
|
||||||
|
return true;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return movable(_instruction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SemanticInformation::Effect SemanticInformation::storage(Instruction _instruction)
|
||||||
{
|
{
|
||||||
switch (_instruction)
|
switch (_instruction)
|
||||||
{
|
{
|
||||||
@ -279,9 +313,45 @@ bool SemanticInformation::invalidatesStorage(Instruction _instruction)
|
|||||||
case Instruction::CREATE:
|
case Instruction::CREATE:
|
||||||
case Instruction::CREATE2:
|
case Instruction::CREATE2:
|
||||||
case Instruction::SSTORE:
|
case Instruction::SSTORE:
|
||||||
return true;
|
return SemanticInformation::Write;
|
||||||
|
|
||||||
|
case Instruction::SLOAD:
|
||||||
|
case Instruction::STATICCALL:
|
||||||
|
return SemanticInformation::Read;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return false;
|
return SemanticInformation::None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SemanticInformation::Effect SemanticInformation::otherState(Instruction _instruction)
|
||||||
|
{
|
||||||
|
switch (_instruction)
|
||||||
|
{
|
||||||
|
case Instruction::CALL:
|
||||||
|
case Instruction::CALLCODE:
|
||||||
|
case Instruction::DELEGATECALL:
|
||||||
|
case Instruction::CREATE:
|
||||||
|
case Instruction::CREATE2:
|
||||||
|
case Instruction::SELFDESTRUCT:
|
||||||
|
case Instruction::STATICCALL: // because it can affect returndatasize
|
||||||
|
// Strictly speaking, log0, .., log4 writes to the state, but the EVM cannot read it, so they
|
||||||
|
// are just marked as having 'other side effects.'
|
||||||
|
return SemanticInformation::Write;
|
||||||
|
|
||||||
|
case Instruction::EXTCODESIZE:
|
||||||
|
case Instruction::EXTCODEHASH:
|
||||||
|
case Instruction::RETURNDATASIZE:
|
||||||
|
case Instruction::BALANCE:
|
||||||
|
case Instruction::SELFBALANCE:
|
||||||
|
case Instruction::RETURNDATACOPY:
|
||||||
|
case Instruction::EXTCODECOPY:
|
||||||
|
// PC and GAS are specifically excluded here. Instructions such as CALLER, CALLVALUE,
|
||||||
|
// ADDRESS are excluded because they cannot change during execution.
|
||||||
|
return SemanticInformation::Read;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return SemanticInformation::None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,6 +36,15 @@ class AssemblyItem;
|
|||||||
*/
|
*/
|
||||||
struct SemanticInformation
|
struct SemanticInformation
|
||||||
{
|
{
|
||||||
|
/// Corresponds to the effect that a YUL-builtin has on a generic data location (storage, memory
|
||||||
|
/// and other blockchain state).
|
||||||
|
enum Effect
|
||||||
|
{
|
||||||
|
None,
|
||||||
|
Read,
|
||||||
|
Write
|
||||||
|
};
|
||||||
|
|
||||||
/// @returns true if the given items starts a new block for common subexpression analysis.
|
/// @returns true if the given items starts a new block for common subexpression analysis.
|
||||||
/// @param _msizeImportant if false, consider an operation non-breaking if its only side-effect is that it modifies msize.
|
/// @param _msizeImportant if false, consider an operation non-breaking if its only side-effect is that it modifies msize.
|
||||||
static bool breaksCSEAnalysisBlock(AssemblyItem const& _item, bool _msizeImportant);
|
static bool breaksCSEAnalysisBlock(AssemblyItem const& _item, bool _msizeImportant);
|
||||||
@ -57,20 +66,23 @@ struct SemanticInformation
|
|||||||
/// without altering the semantics. This means it cannot depend on storage or memory,
|
/// without altering the semantics. This means it cannot depend on storage or memory,
|
||||||
/// cannot have any side-effects, but it can depend on a call-constant state of the blockchain.
|
/// cannot have any side-effects, but it can depend on a call-constant state of the blockchain.
|
||||||
static bool movable(Instruction _instruction);
|
static bool movable(Instruction _instruction);
|
||||||
|
/// If true, the expressions in this code can be moved or copied (together with their arguments)
|
||||||
|
/// across control flow branches and instructions as long as these instructions' 'effects' do
|
||||||
|
/// not influence the 'effects' of the aforementioned expressions.
|
||||||
|
static bool movableApartFromEffects(Instruction _instruction);
|
||||||
/// @returns true if the instruction can be removed without changing the semantics.
|
/// @returns true if the instruction can be removed without changing the semantics.
|
||||||
/// This does not mean that it has to be deterministic or retrieve information from
|
/// This does not mean that it has to be deterministic or retrieve information from
|
||||||
/// somewhere else than purely the values of its arguments.
|
/// somewhere else than purely the values of its arguments.
|
||||||
static bool sideEffectFree(Instruction _instruction);
|
static bool canBeRemoved(Instruction _instruction);
|
||||||
/// @returns true if the instruction can be removed without changing the semantics.
|
/// @returns true if the instruction can be removed without changing the semantics.
|
||||||
/// This does not mean that it has to be deterministic or retrieve information from
|
/// This does not mean that it has to be deterministic or retrieve information from
|
||||||
/// somewhere else than purely the values of its arguments.
|
/// somewhere else than purely the values of its arguments.
|
||||||
/// If true, the instruction is still allowed to influence the value returned by the
|
/// If true, the instruction is still allowed to influence the value returned by the
|
||||||
/// msize instruction.
|
/// msize instruction.
|
||||||
static bool sideEffectFreeIfNoMSize(Instruction _instruction);
|
static bool canBeRemovedIfNoMSize(Instruction _instruction);
|
||||||
/// @returns true if the given instruction modifies memory.
|
static Effect memory(Instruction _instruction);
|
||||||
static bool invalidatesMemory(Instruction _instruction);
|
static Effect storage(Instruction _instruction);
|
||||||
/// @returns true if the given instruction modifies storage (even indirectly).
|
static Effect otherState(Instruction _instruction);
|
||||||
static bool invalidatesStorage(Instruction _instruction);
|
|
||||||
static bool invalidInPureFunctions(Instruction _instruction);
|
static bool invalidInPureFunctions(Instruction _instruction);
|
||||||
static bool invalidInViewFunctions(Instruction _instruction);
|
static bool invalidInViewFunctions(Instruction _instruction);
|
||||||
};
|
};
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <set>
|
#include <set>
|
||||||
|
|
||||||
namespace solidity::yul
|
namespace solidity::yul
|
||||||
@ -30,6 +31,20 @@ namespace solidity::yul
|
|||||||
*/
|
*/
|
||||||
struct SideEffects
|
struct SideEffects
|
||||||
{
|
{
|
||||||
|
/// Corresponds to the effect that a Yul-builtin has on a generic data location (storage, memory
|
||||||
|
/// and other blockchain state).
|
||||||
|
enum Effect
|
||||||
|
{
|
||||||
|
None,
|
||||||
|
Read,
|
||||||
|
Write
|
||||||
|
};
|
||||||
|
|
||||||
|
friend Effect operator+(Effect const& _a, Effect const& _b)
|
||||||
|
{
|
||||||
|
return static_cast<Effect>(std::max(static_cast<int>(_a), static_cast<int>(_b)));
|
||||||
|
}
|
||||||
|
|
||||||
/// If true, expressions in this code can be freely moved and copied without altering the
|
/// If true, expressions in this code can be freely moved and copied without altering the
|
||||||
/// semantics.
|
/// semantics.
|
||||||
/// At statement level, it means that functions containing this code can be
|
/// At statement level, it means that functions containing this code can be
|
||||||
@ -38,22 +53,34 @@ struct SideEffects
|
|||||||
/// This means it cannot depend on storage or memory, cannot have any side-effects,
|
/// This means it cannot depend on storage or memory, cannot have any side-effects,
|
||||||
/// but it can depend on state that is constant across an EVM-call.
|
/// but it can depend on state that is constant across an EVM-call.
|
||||||
bool movable = true;
|
bool movable = true;
|
||||||
|
/// If true, the expressions in this code can be moved or copied (together with their arguments)
|
||||||
|
/// across control flow branches and instructions as long as these instructions' 'effects' do
|
||||||
|
/// not influence the 'effects' of the aforementioned expressions.
|
||||||
|
bool movableApartFromEffects = true;
|
||||||
/// If true, the code can be removed without changing the semantics.
|
/// If true, the code can be removed without changing the semantics.
|
||||||
bool sideEffectFree = true;
|
bool canBeRemoved = true;
|
||||||
/// If true, the code can be removed without changing the semantics as long as
|
/// If true, the code can be removed without changing the semantics as long as
|
||||||
/// the whole program does not contain the msize instruction.
|
/// the whole program does not contain the msize instruction.
|
||||||
bool sideEffectFreeIfNoMSize = true;
|
bool canBeRemovedIfNoMSize = true;
|
||||||
/// If false, storage is guaranteed to be unchanged by the code under all
|
/// If false, the code calls a for-loop or a recursive function, and therefore potentially loops
|
||||||
/// circumstances.
|
/// infinitely. All builtins are set to true by default, even `invalid()`.
|
||||||
bool invalidatesStorage = false;
|
bool cannotLoop = true;
|
||||||
/// If false, memory is guaranteed to be unchanged by the code under all
|
/// Can write, read or have no effect on the blockchain state, when the value of `otherState` is
|
||||||
/// circumstances.
|
/// `Write`, `Read` or `None` respectively.
|
||||||
bool invalidatesMemory = false;
|
Effect otherState = None;
|
||||||
|
/// Can write, read or have no effect on storage, when the value of `storage` is `Write`, `Read`
|
||||||
|
/// or `None` respectively. When the value is `Write`, the expression can invalidate storage,
|
||||||
|
/// potentially indirectly through external calls.
|
||||||
|
Effect storage = None;
|
||||||
|
/// Can write, read or have no effect on memory, when the value of `memory` is `Write`, `Read`
|
||||||
|
/// or `None` respectively. Note that, when the value is `Read`, the expression can have an
|
||||||
|
/// effect on `msize()`.
|
||||||
|
Effect memory = None;
|
||||||
|
|
||||||
/// @returns the worst-case side effects.
|
/// @returns the worst-case side effects.
|
||||||
static SideEffects worst()
|
static SideEffects worst()
|
||||||
{
|
{
|
||||||
return SideEffects{false, false, false, true, true};
|
return SideEffects{false, false, false, false, false, Write, Write, Write};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @returns the combined side effects of two pieces of code.
|
/// @returns the combined side effects of two pieces of code.
|
||||||
@ -61,10 +88,13 @@ struct SideEffects
|
|||||||
{
|
{
|
||||||
return SideEffects{
|
return SideEffects{
|
||||||
movable && _other.movable,
|
movable && _other.movable,
|
||||||
sideEffectFree && _other.sideEffectFree,
|
movableApartFromEffects && _other.movableApartFromEffects,
|
||||||
sideEffectFreeIfNoMSize && _other.sideEffectFreeIfNoMSize,
|
canBeRemoved && _other.canBeRemoved,
|
||||||
invalidatesStorage || _other.invalidatesStorage,
|
canBeRemovedIfNoMSize && _other.canBeRemovedIfNoMSize,
|
||||||
invalidatesMemory || _other.invalidatesMemory
|
cannotLoop && _other.cannotLoop,
|
||||||
|
otherState + _other.otherState,
|
||||||
|
storage + _other.storage,
|
||||||
|
memory + _other.memory
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,10 +109,13 @@ struct SideEffects
|
|||||||
{
|
{
|
||||||
return
|
return
|
||||||
movable == _other.movable &&
|
movable == _other.movable &&
|
||||||
sideEffectFree == _other.sideEffectFree &&
|
movableApartFromEffects == _other.movableApartFromEffects &&
|
||||||
sideEffectFreeIfNoMSize == _other.sideEffectFreeIfNoMSize &&
|
canBeRemoved == _other.canBeRemoved &&
|
||||||
invalidatesStorage == _other.invalidatesStorage &&
|
canBeRemovedIfNoMSize == _other.canBeRemovedIfNoMSize &&
|
||||||
invalidatesMemory == _other.invalidatesMemory;
|
cannotLoop == _other.cannotLoop &&
|
||||||
|
otherState == _other.otherState &&
|
||||||
|
storage == _other.storage &&
|
||||||
|
memory == _other.memory;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -190,7 +190,7 @@ map<YulString, BuiltinFunctionForEVM> createBuiltins(langutil::EVMVersion _evmVe
|
|||||||
"datacopy",
|
"datacopy",
|
||||||
3,
|
3,
|
||||||
0,
|
0,
|
||||||
SideEffects{false, false, false, false, true},
|
SideEffects{false, true, false, false, true, SideEffects::None, SideEffects::None, SideEffects::Write},
|
||||||
{},
|
{},
|
||||||
[](
|
[](
|
||||||
FunctionCall const& _call,
|
FunctionCall const& _call,
|
||||||
@ -206,7 +206,7 @@ map<YulString, BuiltinFunctionForEVM> createBuiltins(langutil::EVMVersion _evmVe
|
|||||||
"setimmutable",
|
"setimmutable",
|
||||||
2,
|
2,
|
||||||
0,
|
0,
|
||||||
SideEffects{false, false, false, false, true},
|
SideEffects{false, false, false, false, true, SideEffects::None, SideEffects::None, SideEffects::Write},
|
||||||
{LiteralKind::String, std::nullopt},
|
{LiteralKind::String, std::nullopt},
|
||||||
[](
|
[](
|
||||||
FunctionCall const& _call,
|
FunctionCall const& _call,
|
||||||
@ -281,12 +281,20 @@ EVMDialect const& EVMDialect::strictAssemblyForEVMObjects(langutil::EVMVersion _
|
|||||||
|
|
||||||
SideEffects EVMDialect::sideEffectsOfInstruction(evmasm::Instruction _instruction)
|
SideEffects EVMDialect::sideEffectsOfInstruction(evmasm::Instruction _instruction)
|
||||||
{
|
{
|
||||||
|
auto translate = [](evmasm::SemanticInformation::Effect _e) -> SideEffects::Effect
|
||||||
|
{
|
||||||
|
return static_cast<SideEffects::Effect>(_e);
|
||||||
|
};
|
||||||
|
|
||||||
return SideEffects{
|
return SideEffects{
|
||||||
evmasm::SemanticInformation::movable(_instruction),
|
evmasm::SemanticInformation::movable(_instruction),
|
||||||
evmasm::SemanticInformation::sideEffectFree(_instruction),
|
evmasm::SemanticInformation::movableApartFromEffects(_instruction),
|
||||||
evmasm::SemanticInformation::sideEffectFreeIfNoMSize(_instruction),
|
evmasm::SemanticInformation::canBeRemoved(_instruction),
|
||||||
evmasm::SemanticInformation::invalidatesStorage(_instruction),
|
evmasm::SemanticInformation::canBeRemovedIfNoMSize(_instruction),
|
||||||
evmasm::SemanticInformation::invalidatesMemory(_instruction)
|
true, // cannotLoop
|
||||||
|
translate(evmasm::SemanticInformation::otherState(_instruction)),
|
||||||
|
translate(evmasm::SemanticInformation::storage(_instruction)),
|
||||||
|
translate(evmasm::SemanticInformation::memory(_instruction)),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,27 +86,34 @@ WasmDialect::WasmDialect()
|
|||||||
addFunction("i64.extend_i32_u", {i32}, {i64});
|
addFunction("i64.extend_i32_u", {i32}, {i64});
|
||||||
|
|
||||||
addFunction("i32.store", {i32, i32}, {}, false);
|
addFunction("i32.store", {i32, i32}, {}, false);
|
||||||
m_functions["i32.store"_yulstring].sideEffects.invalidatesStorage = false;
|
m_functions["i32.store"_yulstring].sideEffects.storage = SideEffects::None;
|
||||||
|
m_functions["i32.store"_yulstring].sideEffects.otherState = SideEffects::None;
|
||||||
addFunction("i64.store", {i32, i64}, {}, false);
|
addFunction("i64.store", {i32, i64}, {}, false);
|
||||||
m_functions["i64.store"_yulstring].sideEffects.invalidatesStorage = false;
|
|
||||||
// TODO: add i32.store16, i64.store8, i64.store16, i64.store32
|
// TODO: add i32.store16, i64.store8, i64.store16, i64.store32
|
||||||
|
m_functions["i64.store"_yulstring].sideEffects.storage = SideEffects::None;
|
||||||
|
m_functions["i64.store"_yulstring].sideEffects.otherState = SideEffects::None;
|
||||||
|
|
||||||
addFunction("i32.store8", {i32, i32}, {}, false);
|
addFunction("i32.store8", {i32, i32}, {}, false);
|
||||||
m_functions["i32.store8"_yulstring].sideEffects.invalidatesStorage = false;
|
m_functions["i32.store8"_yulstring].sideEffects.storage = SideEffects::None;
|
||||||
|
m_functions["i32.store8"_yulstring].sideEffects.otherState = SideEffects::None;
|
||||||
|
|
||||||
addFunction("i64.store8", {i32, i64}, {}, false);
|
addFunction("i64.store8", {i32, i64}, {}, false);
|
||||||
m_functions["i64.store8"_yulstring].sideEffects.invalidatesStorage = false;
|
m_functions["i64.store8"_yulstring].sideEffects.storage = SideEffects::None;
|
||||||
|
m_functions["i64.store8"_yulstring].sideEffects.otherState = SideEffects::None;
|
||||||
|
|
||||||
addFunction("i32.load", {i32}, {i32}, false);
|
addFunction("i32.load", {i32}, {i32}, false);
|
||||||
m_functions["i32.load"_yulstring].sideEffects.invalidatesStorage = false;
|
m_functions["i32.load"_yulstring].sideEffects.canBeRemoved = true;
|
||||||
m_functions["i32.load"_yulstring].sideEffects.invalidatesMemory = false;
|
m_functions["i32.load"_yulstring].sideEffects.canBeRemovedIfNoMSize = true;
|
||||||
m_functions["i32.load"_yulstring].sideEffects.sideEffectFree = true;
|
m_functions["i32.load"_yulstring].sideEffects.storage = SideEffects::None;
|
||||||
m_functions["i32.load"_yulstring].sideEffects.sideEffectFreeIfNoMSize = true;
|
m_functions["i32.load"_yulstring].sideEffects.memory = SideEffects::Read;
|
||||||
|
m_functions["i32.load"_yulstring].sideEffects.otherState = SideEffects::None;
|
||||||
addFunction("i64.load", {i32}, {i64}, false);
|
addFunction("i64.load", {i32}, {i64}, false);
|
||||||
m_functions["i64.load"_yulstring].sideEffects.invalidatesStorage = false;
|
|
||||||
m_functions["i64.load"_yulstring].sideEffects.invalidatesMemory = false;
|
|
||||||
m_functions["i64.load"_yulstring].sideEffects.sideEffectFree = true;
|
|
||||||
m_functions["i64.load"_yulstring].sideEffects.sideEffectFreeIfNoMSize = true;
|
|
||||||
// TODO: add i32.load8, i32.load16, i64.load8, i64.load16, i64.load32
|
// TODO: add i32.load8, i32.load16, i64.load8, i64.load16, i64.load32
|
||||||
|
m_functions["i64.load"_yulstring].sideEffects.canBeRemoved = true;
|
||||||
|
m_functions["i64.load"_yulstring].sideEffects.canBeRemovedIfNoMSize = true;
|
||||||
|
m_functions["i64.load"_yulstring].sideEffects.storage = SideEffects::None;
|
||||||
|
m_functions["i64.load"_yulstring].sideEffects.memory = SideEffects::Read;
|
||||||
|
m_functions["i64.load"_yulstring].sideEffects.otherState = SideEffects::None;
|
||||||
|
|
||||||
// Drop is actually overloaded for all types, but Yul does not support that.
|
// Drop is actually overloaded for all types, but Yul does not support that.
|
||||||
// Because of that, we introduce "i32.drop" and "i64.drop".
|
// Because of that, we introduce "i32.drop" and "i64.drop".
|
||||||
@ -115,8 +122,9 @@ WasmDialect::WasmDialect()
|
|||||||
|
|
||||||
addFunction("nop", {}, {});
|
addFunction("nop", {}, {});
|
||||||
addFunction("unreachable", {}, {}, false);
|
addFunction("unreachable", {}, {}, false);
|
||||||
m_functions["unreachable"_yulstring].sideEffects.invalidatesStorage = false;
|
m_functions["unreachable"_yulstring].sideEffects.storage = SideEffects::None;
|
||||||
m_functions["unreachable"_yulstring].sideEffects.invalidatesMemory = false;
|
m_functions["unreachable"_yulstring].sideEffects.memory = SideEffects::None;
|
||||||
|
m_functions["unreachable"_yulstring].sideEffects.otherState = SideEffects::None;
|
||||||
m_functions["unreachable"_yulstring].controlFlowSideEffects.terminates = true;
|
m_functions["unreachable"_yulstring].controlFlowSideEffects.terminates = true;
|
||||||
m_functions["unreachable"_yulstring].controlFlowSideEffects.reverts = true;
|
m_functions["unreachable"_yulstring].controlFlowSideEffects.reverts = true;
|
||||||
|
|
||||||
@ -219,10 +227,24 @@ void WasmDialect::addEthereumExternals()
|
|||||||
f.returns.emplace_back(YulString(p));
|
f.returns.emplace_back(YulString(p));
|
||||||
// TODO some of them are side effect free.
|
// TODO some of them are side effect free.
|
||||||
f.sideEffects = SideEffects::worst();
|
f.sideEffects = SideEffects::worst();
|
||||||
|
f.sideEffects.cannotLoop = true;
|
||||||
|
f.sideEffects.movableApartFromEffects = !ext.controlFlowSideEffects.terminates;
|
||||||
f.controlFlowSideEffects = ext.controlFlowSideEffects;
|
f.controlFlowSideEffects = ext.controlFlowSideEffects;
|
||||||
f.isMSize = false;
|
f.isMSize = false;
|
||||||
f.sideEffects.invalidatesStorage = (ext.name == "storageStore");
|
|
||||||
f.literalArguments.clear();
|
f.literalArguments.clear();
|
||||||
|
|
||||||
|
static set<string> const writesToStorage{
|
||||||
|
"storageStore",
|
||||||
|
"call",
|
||||||
|
"callcode",
|
||||||
|
"callDelegate",
|
||||||
|
"create"
|
||||||
|
};
|
||||||
|
static set<string> const readsStorage{"storageLoad", "callStatic"};
|
||||||
|
if (readsStorage.count(ext.name))
|
||||||
|
f.sideEffects.storage = SideEffects::Read;
|
||||||
|
else if (!writesToStorage.count(ext.name))
|
||||||
|
f.sideEffects.storage = SideEffects::None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -241,6 +263,9 @@ void WasmDialect::addFunction(
|
|||||||
yulAssert(_returns.size() <= 1, "The Wasm 1.0 specification only allows up to 1 return value.");
|
yulAssert(_returns.size() <= 1, "The Wasm 1.0 specification only allows up to 1 return value.");
|
||||||
f.returns = std::move(_returns);
|
f.returns = std::move(_returns);
|
||||||
f.sideEffects = _movable ? SideEffects{} : SideEffects::worst();
|
f.sideEffects = _movable ? SideEffects{} : SideEffects::worst();
|
||||||
|
f.sideEffects.cannotLoop = true;
|
||||||
|
// TODO This should be improved when LoopInvariantCodeMotion gets specialized for WASM
|
||||||
|
f.sideEffects.movableApartFromEffects = _movable;
|
||||||
f.isMSize = false;
|
f.isMSize = false;
|
||||||
f.literalArguments = std::move(_literalArguments);
|
f.literalArguments = std::move(_literalArguments);
|
||||||
}
|
}
|
||||||
|
@ -106,8 +106,9 @@ map<YulString, SideEffects> SideEffectsPropagator::sideEffects(
|
|||||||
for (auto const& function: _directCallGraph.functionsWithLoops + _directCallGraph.recursiveFunctions())
|
for (auto const& function: _directCallGraph.functionsWithLoops + _directCallGraph.recursiveFunctions())
|
||||||
{
|
{
|
||||||
ret[function].movable = false;
|
ret[function].movable = false;
|
||||||
ret[function].sideEffectFree = false;
|
ret[function].canBeRemoved = false;
|
||||||
ret[function].sideEffectFreeIfNoMSize = false;
|
ret[function].canBeRemovedIfNoMSize = false;
|
||||||
|
ret[function].cannotLoop = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto const& call: _directCallGraph.functionCalls)
|
for (auto const& call: _directCallGraph.functionCalls)
|
||||||
|
@ -59,16 +59,17 @@ public:
|
|||||||
void operator()(FunctionCall const& _functionCall) override;
|
void operator()(FunctionCall const& _functionCall) override;
|
||||||
|
|
||||||
bool movable() const { return m_sideEffects.movable; }
|
bool movable() const { return m_sideEffects.movable; }
|
||||||
bool sideEffectFree(bool _allowMSizeModification = false) const
|
bool canBeRemoved(bool _allowMSizeModification = false) const
|
||||||
{
|
{
|
||||||
if (_allowMSizeModification)
|
if (_allowMSizeModification)
|
||||||
return sideEffectFreeIfNoMSize();
|
return m_sideEffects.canBeRemovedIfNoMSize;
|
||||||
else
|
else
|
||||||
return m_sideEffects.sideEffectFree;
|
return m_sideEffects.canBeRemoved;
|
||||||
}
|
}
|
||||||
bool sideEffectFreeIfNoMSize() const { return m_sideEffects.sideEffectFreeIfNoMSize; }
|
bool cannotLoop() const { return m_sideEffects.cannotLoop; }
|
||||||
bool invalidatesStorage() const { return m_sideEffects.invalidatesStorage; }
|
bool invalidatesStorage() const { return m_sideEffects.storage == SideEffects::Write; }
|
||||||
bool invalidatesMemory() const { return m_sideEffects.invalidatesMemory; }
|
bool invalidatesMemory() const { return m_sideEffects.memory == SideEffects::Write; }
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Dialect const& m_dialect;
|
Dialect const& m_dialect;
|
||||||
|
@ -93,7 +93,7 @@ void UnusedPruner::operator()(Block& _block)
|
|||||||
statement = Block{std::move(varDecl.location), {}};
|
statement = Block{std::move(varDecl.location), {}};
|
||||||
else if (
|
else if (
|
||||||
SideEffectsCollector(m_dialect, *varDecl.value, m_functionSideEffects).
|
SideEffectsCollector(m_dialect, *varDecl.value, m_functionSideEffects).
|
||||||
sideEffectFree(m_allowMSizeOptimization)
|
canBeRemoved(m_allowMSizeOptimization)
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
subtractReferences(ReferencesCounter::countReferences(*varDecl.value));
|
subtractReferences(ReferencesCounter::countReferences(*varDecl.value));
|
||||||
@ -112,7 +112,7 @@ void UnusedPruner::operator()(Block& _block)
|
|||||||
ExpressionStatement& exprStmt = std::get<ExpressionStatement>(statement);
|
ExpressionStatement& exprStmt = std::get<ExpressionStatement>(statement);
|
||||||
if (
|
if (
|
||||||
SideEffectsCollector(m_dialect, exprStmt.expression, m_functionSideEffects).
|
SideEffectsCollector(m_dialect, exprStmt.expression, m_functionSideEffects).
|
||||||
sideEffectFree(m_allowMSizeOptimization)
|
canBeRemoved(m_allowMSizeOptimization)
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
subtractReferences(ReferencesCounter::countReferences(exprStmt.expression));
|
subtractReferences(ReferencesCounter::countReferences(exprStmt.expression));
|
||||||
|
@ -49,14 +49,27 @@ string toString(SideEffects const& _sideEffects)
|
|||||||
vector<string> ret;
|
vector<string> ret;
|
||||||
if (_sideEffects.movable)
|
if (_sideEffects.movable)
|
||||||
ret.emplace_back("movable");
|
ret.emplace_back("movable");
|
||||||
if (_sideEffects.sideEffectFree)
|
if (_sideEffects.movableApartFromEffects)
|
||||||
ret.emplace_back("sideEffectFree");
|
ret.emplace_back("movable apart from effects");
|
||||||
if (_sideEffects.sideEffectFreeIfNoMSize)
|
if (_sideEffects.canBeRemoved)
|
||||||
ret.emplace_back("sideEffectFreeIfNoMSize");
|
ret.emplace_back("can be removed");
|
||||||
if (_sideEffects.invalidatesStorage)
|
if (_sideEffects.canBeRemovedIfNoMSize)
|
||||||
ret.emplace_back("invalidatesStorage");
|
ret.emplace_back("can be removed if no msize");
|
||||||
if (_sideEffects.invalidatesMemory)
|
if (!_sideEffects.cannotLoop)
|
||||||
ret.emplace_back("invalidatesMemory");
|
ret.emplace_back("can loop");
|
||||||
|
if (_sideEffects.otherState == SideEffects::Write)
|
||||||
|
ret.emplace_back("writes other state");
|
||||||
|
else if (_sideEffects.otherState == SideEffects::Read)
|
||||||
|
ret.emplace_back("reads other state");
|
||||||
|
if (_sideEffects.storage == SideEffects::Write)
|
||||||
|
ret.emplace_back("writes storage");
|
||||||
|
else if (_sideEffects.storage == SideEffects::Read)
|
||||||
|
ret.emplace_back("reads storage");
|
||||||
|
if (_sideEffects.memory == SideEffects::Write)
|
||||||
|
ret.emplace_back("writes memory");
|
||||||
|
else if (_sideEffects.memory == SideEffects::Read)
|
||||||
|
ret.emplace_back("reads memory");
|
||||||
|
|
||||||
return joinHumanReadable(ret);
|
return joinHumanReadable(ret);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
function c() { b() }
|
function c() { b() }
|
||||||
}
|
}
|
||||||
// ----
|
// ----
|
||||||
// : movable, sideEffectFree, sideEffectFreeIfNoMSize
|
// : movable, movable apart from effects, can be removed, can be removed if no msize
|
||||||
// a:
|
// a: movable apart from effects, can loop
|
||||||
// b:
|
// b: movable apart from effects, can loop
|
||||||
// c:
|
// c: movable apart from effects, can loop
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
function b() { a() }
|
function b() { a() }
|
||||||
}
|
}
|
||||||
// ----
|
// ----
|
||||||
// : movable, sideEffectFree, sideEffectFreeIfNoMSize
|
// : movable, movable apart from effects, can be removed, can be removed if no msize
|
||||||
// a:
|
// a: movable apart from effects, can loop
|
||||||
// b:
|
// b: movable apart from effects, can loop
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
// ----
|
// ----
|
||||||
// : movable, sideEffectFree, sideEffectFreeIfNoMSize
|
// : movable, movable apart from effects, can be removed, can be removed if no msize
|
||||||
|
@ -2,4 +2,4 @@
|
|||||||
sstore(0, 1)
|
sstore(0, 1)
|
||||||
}
|
}
|
||||||
// ----
|
// ----
|
||||||
// : invalidatesStorage
|
// : writes storage
|
||||||
|
@ -6,9 +6,9 @@
|
|||||||
function i() { pop(msize()) }
|
function i() { pop(msize()) }
|
||||||
}
|
}
|
||||||
// ----
|
// ----
|
||||||
// : movable, sideEffectFree, sideEffectFreeIfNoMSize
|
// : movable, movable apart from effects, can be removed, can be removed if no msize
|
||||||
// a: invalidatesMemory
|
// a: writes memory
|
||||||
// f: invalidatesMemory
|
// f: writes memory
|
||||||
// g: invalidatesStorage
|
// g: writes storage
|
||||||
// h: sideEffectFreeIfNoMSize
|
// h: movable apart from effects, can be removed if no msize, reads memory
|
||||||
// i: sideEffectFree, sideEffectFreeIfNoMSize
|
// i: can be removed, can be removed if no msize, reads memory
|
||||||
|
@ -7,5 +7,5 @@
|
|||||||
sstore(0, mload(0))
|
sstore(0, mload(0))
|
||||||
}
|
}
|
||||||
// ----
|
// ----
|
||||||
// : invalidatesStorage, invalidatesMemory
|
// : can loop, writes storage, writes memory
|
||||||
// foo: invalidatesMemory
|
// foo: can loop, writes memory
|
||||||
|
@ -15,8 +15,8 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// ----
|
// ----
|
||||||
// : movable, sideEffectFree, sideEffectFreeIfNoMSize
|
// : movable, movable apart from effects, can be removed, can be removed if no msize
|
||||||
// a: invalidatesStorage
|
// a: can loop, writes storage
|
||||||
// b: invalidatesStorage
|
// b: can loop, writes storage
|
||||||
// c: invalidatesStorage, invalidatesMemory
|
// c: can loop, writes storage, writes memory
|
||||||
// d: movable, sideEffectFree, sideEffectFreeIfNoMSize
|
// d: movable, movable apart from effects, can be removed, can be removed if no msize
|
||||||
|
@ -5,8 +5,8 @@
|
|||||||
function h() { invalid() }
|
function h() { invalid() }
|
||||||
}
|
}
|
||||||
// ----
|
// ----
|
||||||
// : movable, sideEffectFree, sideEffectFreeIfNoMSize
|
// : movable, movable apart from effects, can be removed, can be removed if no msize
|
||||||
// a: sideEffectFree, sideEffectFreeIfNoMSize
|
// a: can be removed, can be removed if no msize
|
||||||
// f: sideEffectFree, sideEffectFreeIfNoMSize
|
// f: can be removed, can be removed if no msize
|
||||||
// g:
|
// g:
|
||||||
// h:
|
// h:
|
||||||
|
@ -2,5 +2,5 @@
|
|||||||
function a() { a() }
|
function a() { a() }
|
||||||
}
|
}
|
||||||
// ----
|
// ----
|
||||||
// : movable, sideEffectFree, sideEffectFreeIfNoMSize
|
// : movable, movable apart from effects, can be removed, can be removed if no msize
|
||||||
// a:
|
// a: movable apart from effects, can loop
|
||||||
|
@ -6,9 +6,9 @@
|
|||||||
function i() { let z := mload(0) }
|
function i() { let z := mload(0) }
|
||||||
}
|
}
|
||||||
// ----
|
// ----
|
||||||
// : movable, sideEffectFree, sideEffectFreeIfNoMSize
|
// : movable, movable apart from effects, can be removed, can be removed if no msize
|
||||||
// a: movable, sideEffectFree, sideEffectFreeIfNoMSize
|
// a: movable, movable apart from effects, can be removed, can be removed if no msize
|
||||||
// f: invalidatesMemory
|
// f: writes memory
|
||||||
// g: invalidatesStorage
|
// g: writes storage
|
||||||
// h: sideEffectFree, sideEffectFreeIfNoMSize
|
// h: can be removed, can be removed if no msize, reads memory
|
||||||
// i: sideEffectFreeIfNoMSize
|
// i: movable apart from effects, can be removed if no msize, reads memory
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
function g() { sstore(0, 1) }
|
function g() { sstore(0, 1) }
|
||||||
}
|
}
|
||||||
// ----
|
// ----
|
||||||
// : movable, sideEffectFree, sideEffectFreeIfNoMSize
|
// : movable, movable apart from effects, can be removed, can be removed if no msize
|
||||||
// a: invalidatesStorage, invalidatesMemory
|
// a: writes other state, writes storage, writes memory
|
||||||
// f: invalidatesStorage, invalidatesMemory
|
// f: writes other state, writes storage, writes memory
|
||||||
// g: invalidatesStorage
|
// g: writes storage
|
||||||
|
@ -5,8 +5,8 @@
|
|||||||
function h() { pop(sload(0))}
|
function h() { pop(sload(0))}
|
||||||
}
|
}
|
||||||
// ----
|
// ----
|
||||||
// : movable, sideEffectFree, sideEffectFreeIfNoMSize
|
// : movable, movable apart from effects, can be removed, can be removed if no msize
|
||||||
// a: invalidatesStorage
|
// a: writes storage
|
||||||
// f: invalidatesStorage
|
// f: writes storage
|
||||||
// g: invalidatesStorage, invalidatesMemory
|
// g: writes other state, writes storage, writes memory
|
||||||
// h: sideEffectFree, sideEffectFreeIfNoMSize
|
// h: movable apart from effects, can be removed, can be removed if no msize, reads storage
|
||||||
|
@ -31,10 +31,10 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// ----
|
// ----
|
||||||
// : invalidatesStorage, invalidatesMemory
|
// : writes storage, writes memory
|
||||||
// f: sideEffectFreeIfNoMSize
|
// f: movable apart from effects, can be removed if no msize, reads memory
|
||||||
// g: invalidatesStorage, invalidatesMemory
|
// g: writes storage, writes memory
|
||||||
// h: invalidatesStorage, invalidatesMemory
|
// h: writes storage, writes memory
|
||||||
// i: invalidatesStorage
|
// i: writes storage
|
||||||
// r: movable, sideEffectFree, sideEffectFreeIfNoMSize
|
// r: movable, movable apart from effects, can be removed, can be removed if no msize
|
||||||
// t: invalidatesMemory
|
// t: writes memory
|
||||||
|
@ -4,6 +4,6 @@
|
|||||||
pop(f())
|
pop(f())
|
||||||
}
|
}
|
||||||
// ----
|
// ----
|
||||||
// :
|
// : movable apart from effects, can loop
|
||||||
// f:
|
// f: movable apart from effects, can loop
|
||||||
// g:
|
// g: movable apart from effects, can loop
|
||||||
|
Loading…
Reference in New Issue
Block a user