Review suggestions.

This commit is contained in:
Daniel Kirchner 2021-04-06 14:46:47 +02:00
parent 8916e64a1b
commit 46fa86fed3
8 changed files with 99 additions and 98 deletions

View File

@ -68,15 +68,16 @@ namespace
template<typename Container>
auto eraseFirst(Container& _container) { return _container.erase(begin(_container)); }
optional<Statement> tryExtractFirstStatement(Identifier const* _identifier, vector<Case>& _cases)
optional<Statement> tryExtractFirstStatement(Identifier const* _switchExpressionAsIdentifier, vector<Case>& _cases)
{
yulAssert(!_cases.empty() && !_cases.front().body.statements.empty(), "");
Statement& referenceStatement = _cases.front().body.statements.front();
if (_identifier)
if (_switchExpressionAsIdentifier)
{
Assignments assignments;
visit(assignments, referenceStatement);
if (assignments.names().count(_identifier->name))
if (assignments.names().count(_switchExpressionAsIdentifier->name))
return nullopt;
}
@ -136,11 +137,11 @@ void CommonSwitchCasePrefixMover::operator()(Block& _block)
Identifier const* identifier = std::get_if<Identifier>(switchStatement->expression.get());
if (!identifier && !holds_alternative<Literal>(*switchStatement->expression.get()))
return {};
return nullopt;
// We need to be able to tell how the default case behaves.
if (switchStatement->cases.back().value)
return {};
if (switchStatement->cases.back().value != nullptr)
return nullopt;
vector<Statement> result;
while (!switchStatement->cases.front().body.statements.empty())
@ -155,7 +156,7 @@ void CommonSwitchCasePrefixMover::operator()(Block& _block)
return result;
}
}
return {};
return nullopt;
}
);
}

View File

@ -1,12 +1,12 @@
{
let x := calldataload(0)
switch x
case 0 {
x := calldataload(1)
}
default {
x := calldataload(1)
}
let x := calldataload(0)
switch x
case 0 {
x := calldataload(1)
}
default {
x := calldataload(1)
}
}
// ----
// step: commonSwitchCasePrefixMover

View File

@ -1,25 +1,25 @@
{
switch mload(0)
case 0 {
sstore(1, 0)
}
default {
sstore(1, 0)
}
switch msize()
case 0 {
sstore(1, 0)
}
default {
sstore(1, 0)
}
switch sload(0)
case 0 {
sstore(1, 0)
}
default {
sstore(1, 0)
}
switch mload(0)
case 0 {
sstore(1, 0)
}
default {
sstore(1, 0)
}
switch msize()
case 0 {
sstore(1, 0)
}
default {
sstore(1, 0)
}
switch sload(0)
case 0 {
sstore(1, 0)
}
default {
sstore(1, 0)
}
}
// ----
// step: commonSwitchCasePrefixMover

View File

@ -1,20 +1,20 @@
{
let x := calldataload(0)
switch x
case 0 {
for { let a := 0 } lt(a, 42) { a := add(a, 1) } {
let b := 23
sstore(add(b, a), a)
let x := calldataload(0)
switch x
case 0 {
for { let a := 0 } lt(a, 42) { a := add(a, 1) } {
let b := 23
sstore(add(b, a), a)
}
sstore(0, 23)
}
sstore(0, 23)
}
default {
for { let b := 0 } lt(b, 42) { b := add(b, 1) } {
let c := 23
sstore(add(c, b), b)
default {
for { let b := 0 } lt(b, 42) { b := add(b, 1) } {
let c := 23
sstore(add(c, b), b)
}
sstore(0, 32)
}
sstore(0, 32)
}
}
// ----
// step: commonSwitchCasePrefixMover

View File

@ -1,13 +1,13 @@
{
let a := calldataload(42)
switch a
case 0 {
let b := calldataload(23)
switch b
case 0 { sstore(0, 1) }
default { sstore(0, 1) }
}
default { sstore(1, 2) }
let a := calldataload(42)
switch a
case 0 {
let b := calldataload(23)
switch b
case 0 { sstore(0, 1) }
default { sstore(0, 1) }
}
default { sstore(1, 2) }
}
// ----
// step: commonSwitchCasePrefixMover

View File

@ -1,17 +1,17 @@
{
function f() -> x {
x := calldataload(0)
sstore(0, 42)
}
switch f()
case 0 {
let x := sload(0)
sstore(1, x)
}
default {
let x := sload(0)
sstore(1, x)
}
function f() -> x {
x := calldataload(0)
sstore(0, 42)
}
switch f()
case 0 {
let x := sload(0)
sstore(1, x)
}
default {
let x := sload(0)
sstore(1, x)
}
}
// ----
// step: commonSwitchCasePrefixMover

View File

@ -1,16 +1,16 @@
{
let x := calldataload(0)
switch x
case 0 {
sstore(0, 1)
sstore(1, 2)
sstore(2, 3)
}
default {
sstore(0, 1)
sstore(1, 2)
sstore(2, 4)
}
let x := calldataload(0)
switch x
case 0 {
sstore(0, 1)
sstore(1, 2)
sstore(2, 3)
}
default {
sstore(0, 1)
sstore(1, 2)
sstore(2, 4)
}
}
// ----
// step: commonSwitchCasePrefixMover

View File

@ -1,18 +1,18 @@
{
let x := calldataload(0)
switch x
case 0 {
sstore(0, 23)
let a := calldataload(42)
sstore(1, a)
sstore(2, sub(a, 2))
}
default {
sstore(0, 23)
let b := calldataload(42)
sstore(1, b)
sstore(2, sub(b, 1))
}
let x := calldataload(0)
switch x
case 0 {
sstore(0, 23)
let a := calldataload(42)
sstore(1, a)
sstore(2, sub(a, 2))
}
default {
sstore(0, 23)
let b := calldataload(42)
sstore(1, b)
sstore(2, sub(b, 1))
}
}
// ----
// step: commonSwitchCasePrefixMover