mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Make DataFlowAnalyzer aware of storage / memory slot after sload / mload.
This commit is contained in:
parent
a1c33249f1
commit
e0b1d8b9bd
@ -9,6 +9,7 @@ Compiler Features:
|
|||||||
* NatSpec: Inherit tags from unique base if derived function does not provide any.
|
* NatSpec: Inherit tags from unique base if derived function does not provide any.
|
||||||
* Commandline Interface: Prevent some incompatible commandline options from being used together.
|
* Commandline Interface: Prevent some incompatible commandline options from being used together.
|
||||||
* NatSpec: Support NatSpec comments on events.
|
* NatSpec: Support NatSpec comments on events.
|
||||||
|
* Yul Optimizer: Store knowledge about storage / memory after ``a := sload(x)`` / ``a := mload(x)``.
|
||||||
|
|
||||||
Bugfixes:
|
Bugfixes:
|
||||||
* NatSpec: Do not consider ``////`` and ``/***`` as NatSpec comments.
|
* NatSpec: Do not consider ``////`` and ``/***`` as NatSpec comments.
|
||||||
|
@ -253,6 +253,21 @@ void DataFlowAnalyzer::handleAssignment(set<YulString> const& _variables, Expres
|
|||||||
// assignment to slot contents denoted by "name"
|
// assignment to slot contents denoted by "name"
|
||||||
m_memory.eraseValue(name);
|
m_memory.eraseValue(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (_value && _variables.size() == 1)
|
||||||
|
{
|
||||||
|
YulString variable = *_variables.begin();
|
||||||
|
if (!movableChecker.referencedVariables().count(variable))
|
||||||
|
{
|
||||||
|
// This might erase additional knowledge about the slot.
|
||||||
|
// On the other hand, if we knew the value in the slot
|
||||||
|
// already, then the sload() / mload() would have been replaced by a variable anyway.
|
||||||
|
if (auto key = isSimpleLoad(evmasm::Instruction::MLOAD, *_value))
|
||||||
|
m_memory.set(*key, variable);
|
||||||
|
else if (auto key = isSimpleLoad(evmasm::Instruction::SLOAD, *_value))
|
||||||
|
m_storage.set(*key, variable);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DataFlowAnalyzer::pushScope(bool _functionScope)
|
void DataFlowAnalyzer::pushScope(bool _functionScope)
|
||||||
@ -401,3 +416,25 @@ std::optional<pair<YulString, YulString>> DataFlowAnalyzer::isSimpleStore(
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::optional<YulString> DataFlowAnalyzer::isSimpleLoad(
|
||||||
|
evmasm::Instruction _load,
|
||||||
|
Expression const& _expression
|
||||||
|
) const
|
||||||
|
{
|
||||||
|
yulAssert(
|
||||||
|
_load == evmasm::Instruction::MLOAD ||
|
||||||
|
_load == evmasm::Instruction::SLOAD,
|
||||||
|
""
|
||||||
|
);
|
||||||
|
if (holds_alternative<FunctionCall>(_expression))
|
||||||
|
{
|
||||||
|
FunctionCall const& funCall = std::get<FunctionCall>(_expression);
|
||||||
|
if (EVMDialect const* dialect = dynamic_cast<EVMDialect const*>(&m_dialect))
|
||||||
|
if (auto const* builtin = dialect->builtin(funCall.functionName.name))
|
||||||
|
if (builtin->instruction == _load)
|
||||||
|
if (holds_alternative<Identifier>(funCall.arguments.at(0)))
|
||||||
|
return std::get<Identifier>(funCall.arguments.at(0)).name;
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -142,11 +142,20 @@ protected:
|
|||||||
/// Returns true iff the variable is in scope.
|
/// Returns true iff the variable is in scope.
|
||||||
bool inScope(YulString _variableName) const;
|
bool inScope(YulString _variableName) const;
|
||||||
|
|
||||||
|
/// Checks if the statement is sstore(a, b) / mstore(a, b)
|
||||||
|
/// where a and b are variables and returns these variables in that case.
|
||||||
std::optional<std::pair<YulString, YulString>> isSimpleStore(
|
std::optional<std::pair<YulString, YulString>> isSimpleStore(
|
||||||
evmasm::Instruction _store,
|
evmasm::Instruction _store,
|
||||||
ExpressionStatement const& _statement
|
ExpressionStatement const& _statement
|
||||||
) const;
|
) const;
|
||||||
|
|
||||||
|
/// Checks if the expression is sload(a) / mload(a)
|
||||||
|
/// where a is a variable and returns the variable in that case.
|
||||||
|
std::optional<YulString> isSimpleLoad(
|
||||||
|
evmasm::Instruction _load,
|
||||||
|
Expression const& _expression
|
||||||
|
) const;
|
||||||
|
|
||||||
Dialect const& m_dialect;
|
Dialect const& m_dialect;
|
||||||
/// Side-effects of user-defined functions. Worst-case side-effects are assumed
|
/// Side-effects of user-defined functions. Worst-case side-effects are assumed
|
||||||
/// if this is not provided or the function is not found.
|
/// if this is not provided or the function is not found.
|
||||||
|
13
test/libyul/yulOptimizerTests/loadResolver/double_mload.yul
Normal file
13
test/libyul/yulOptimizerTests/loadResolver/double_mload.yul
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
let x := calldataload(0)
|
||||||
|
let a := mload(x)
|
||||||
|
let b := mload(x)
|
||||||
|
sstore(a, b)
|
||||||
|
}
|
||||||
|
// ----
|
||||||
|
// step: loadResolver
|
||||||
|
//
|
||||||
|
// {
|
||||||
|
// let a := mload(calldataload(0))
|
||||||
|
// sstore(a, a)
|
||||||
|
// }
|
@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
let x := calldataload(0)
|
||||||
|
let a := mload(x)
|
||||||
|
x := 7
|
||||||
|
let b := mload(x)
|
||||||
|
sstore(a, b)
|
||||||
|
}
|
||||||
|
// ----
|
||||||
|
// step: loadResolver
|
||||||
|
//
|
||||||
|
// {
|
||||||
|
// let x := calldataload(0)
|
||||||
|
// let a := mload(x)
|
||||||
|
// x := 7
|
||||||
|
// sstore(a, mload(x))
|
||||||
|
// }
|
@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
let x := calldataload(0)
|
||||||
|
let a := mload(x)
|
||||||
|
a := 7
|
||||||
|
let b := mload(x)
|
||||||
|
sstore(a, b)
|
||||||
|
}
|
||||||
|
// ----
|
||||||
|
// step: loadResolver
|
||||||
|
//
|
||||||
|
// {
|
||||||
|
// let x := calldataload(0)
|
||||||
|
// let a := mload(x)
|
||||||
|
// a := 7
|
||||||
|
// sstore(a, mload(x))
|
||||||
|
// }
|
@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
let x := mload(calldataload(0))
|
||||||
|
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(0)
|
||||||
|
// let x := mload(_2)
|
||||||
|
// let _3 := 1
|
||||||
|
// if calldataload(_3) { mstore(add(_2, 0x20), _3) }
|
||||||
|
// sstore(mload(add(_2, 0x20)), x)
|
||||||
|
// }
|
@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
let b := mload(2)
|
||||||
|
if calldataload(1) {
|
||||||
|
mstore(2, 7)
|
||||||
|
// Re-writing the old value, should allow to eliminate the load below.
|
||||||
|
mstore(2, b)
|
||||||
|
}
|
||||||
|
sstore(0, mload(2))
|
||||||
|
}
|
||||||
|
// ----
|
||||||
|
// step: loadResolver
|
||||||
|
//
|
||||||
|
// {
|
||||||
|
// let _1 := 2
|
||||||
|
// let b := mload(_1)
|
||||||
|
// if calldataload(1)
|
||||||
|
// {
|
||||||
|
// mstore(_1, 7)
|
||||||
|
// mstore(_1, b)
|
||||||
|
// }
|
||||||
|
// sstore(0, b)
|
||||||
|
// }
|
@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
let b := mload(2)
|
||||||
|
sstore(0, b)
|
||||||
|
if calldataload(1) {
|
||||||
|
mstore(2, 7)
|
||||||
|
}
|
||||||
|
sstore(0, mload(2))
|
||||||
|
}
|
||||||
|
// ----
|
||||||
|
// step: loadResolver
|
||||||
|
//
|
||||||
|
// {
|
||||||
|
// let _1 := 2
|
||||||
|
// let b := mload(_1)
|
||||||
|
// let _2 := 0
|
||||||
|
// sstore(_2, b)
|
||||||
|
// if calldataload(1) { mstore(_1, 7) }
|
||||||
|
// sstore(_2, mload(_1))
|
||||||
|
// }
|
15
test/libyul/yulOptimizerTests/loadResolver/mload_self.yul
Normal file
15
test/libyul/yulOptimizerTests/loadResolver/mload_self.yul
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
let x := calldataload(0)
|
||||||
|
x := mload(x)
|
||||||
|
let y := mload(x)
|
||||||
|
sstore(0, y)
|
||||||
|
}
|
||||||
|
// ----
|
||||||
|
// step: loadResolver
|
||||||
|
//
|
||||||
|
// {
|
||||||
|
// let _1 := 0
|
||||||
|
// let x := calldataload(_1)
|
||||||
|
// x := mload(x)
|
||||||
|
// sstore(_1, mload(x))
|
||||||
|
// }
|
@ -0,0 +1,30 @@
|
|||||||
|
{
|
||||||
|
let x := calldataload(0)
|
||||||
|
let len := sload(x)
|
||||||
|
let sum
|
||||||
|
for { let i := 0} lt(i, sload(x)) { i := add(i, 1) } {
|
||||||
|
let p := add(x, add(i, 1))
|
||||||
|
if gt(p, sload(x)) { revert(0, 0) }
|
||||||
|
sum := add(sum, sload(p))
|
||||||
|
}
|
||||||
|
mstore(0, sum)
|
||||||
|
return(0, 0x20)
|
||||||
|
}
|
||||||
|
// ----
|
||||||
|
// step: loadResolver
|
||||||
|
//
|
||||||
|
// {
|
||||||
|
// let _1 := 0
|
||||||
|
// let x := calldataload(_1)
|
||||||
|
// let len := sload(x)
|
||||||
|
// let sum
|
||||||
|
// let i := _1
|
||||||
|
// for { } lt(i, len) { i := add(i, 1) }
|
||||||
|
// {
|
||||||
|
// let p := add(add(x, i), 1)
|
||||||
|
// if gt(p, len) { revert(_1, _1) }
|
||||||
|
// sum := add(sum, sload(p))
|
||||||
|
// }
|
||||||
|
// mstore(_1, sum)
|
||||||
|
// return(_1, 0x20)
|
||||||
|
// }
|
Loading…
Reference in New Issue
Block a user