mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[YulOpt] Implement loop-invariant code motion
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
{
|
||||
let b := 1
|
||||
for { let a := 1 } iszero(eq(a, 10)) { a := add(a, 1) } {
|
||||
let c := mload(3) // c cannot be moved because non-movable
|
||||
let not_inv := add(b, c) // no_inv cannot be moved because its value depends on c
|
||||
a := add(a, 1)
|
||||
mstore(a, not_inv)
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// step: loopInvariantCodeMotion
|
||||
// ----
|
||||
// {
|
||||
// let b := 1
|
||||
// let a := 1
|
||||
// for { } iszero(eq(a, 10)) { a := add(a, 1) }
|
||||
// {
|
||||
// let c := mload(3)
|
||||
// let not_inv := add(b, c)
|
||||
// a := add(a, 1)
|
||||
// mstore(a, not_inv)
|
||||
// }
|
||||
// }
|
||||
@@ -0,0 +1,26 @@
|
||||
{
|
||||
let b := 1
|
||||
// tests if c, d, and inv can be moved outside in single pass
|
||||
for { let a := 1 } iszero(eq(a, 10)) { a := add(a, 1) } {
|
||||
let c := b
|
||||
let d := mul(c, 2)
|
||||
let inv := add(c, d)
|
||||
a := add(a, 1)
|
||||
mstore(a, inv)
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// step: loopInvariantCodeMotion
|
||||
// ----
|
||||
// {
|
||||
// let b := 1
|
||||
// let a := 1
|
||||
// let c := b
|
||||
// let d := mul(c, 2)
|
||||
// let inv := add(c, d)
|
||||
// for { } iszero(eq(a, 10)) { a := add(a, 1) }
|
||||
// {
|
||||
// a := add(a, 1)
|
||||
// mstore(a, inv)
|
||||
// }
|
||||
// }
|
||||
@@ -0,0 +1,23 @@
|
||||
{
|
||||
let b := 1
|
||||
for { let a := 1 } iszero(eq(a, 10)) { a := add(a, 1) } {
|
||||
let not_inv := add(b, 42)
|
||||
not_inv := add(not_inv, 1)
|
||||
a := add(a, 1)
|
||||
mstore(a, not_inv)
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// step: loopInvariantCodeMotion
|
||||
// ----
|
||||
// {
|
||||
// let b := 1
|
||||
// let a := 1
|
||||
// for { } iszero(eq(a, 10)) { a := add(a, 1) }
|
||||
// {
|
||||
// let not_inv := add(b, 42)
|
||||
// not_inv := add(not_inv, 1)
|
||||
// a := add(a, 1)
|
||||
// mstore(a, not_inv)
|
||||
// }
|
||||
// }
|
||||
@@ -0,0 +1,21 @@
|
||||
{
|
||||
let b := 0
|
||||
for { let a := 1 } iszero(eq(a, 10)) { a := add(a, 1) } {
|
||||
let inv := mload(b)
|
||||
a := add(a, 1)
|
||||
mstore(a, inv)
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// step: loopInvariantCodeMotion
|
||||
// ----
|
||||
// {
|
||||
// let b := 0
|
||||
// let a := 1
|
||||
// for { } iszero(eq(a, 10)) { a := add(a, 1) }
|
||||
// {
|
||||
// let inv := mload(b)
|
||||
// a := add(a, 1)
|
||||
// mstore(a, inv)
|
||||
// }
|
||||
// }
|
||||
@@ -0,0 +1,25 @@
|
||||
{
|
||||
let b := 1
|
||||
for { let a := 1 } iszero(eq(a, 10)) { a := add(a, 1) } {
|
||||
for { let a2 := 1 } iszero(eq(a2, 10)) { a2 := add(a2, 1) } {
|
||||
let inv := add(b, 42)
|
||||
mstore(a, inv)
|
||||
}
|
||||
a := add(a, 1)
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// step: loopInvariantCodeMotion
|
||||
// ----
|
||||
// {
|
||||
// let b := 1
|
||||
// let a := 1
|
||||
// let inv := add(b, 42)
|
||||
// for { } iszero(eq(a, 10)) { a := add(a, 1) }
|
||||
// {
|
||||
// let a2 := 1
|
||||
// for { } iszero(eq(a2, 10)) { a2 := add(a2, 1) }
|
||||
// { mstore(a, inv) }
|
||||
// a := add(a, 1)
|
||||
// }
|
||||
// }
|
||||
@@ -0,0 +1,21 @@
|
||||
{
|
||||
let b := 1
|
||||
for { let a := 1 } iszero(eq(a, 10)) { a := add(a, 1) } {
|
||||
let inv := add(b, 42)
|
||||
a := add(a, 1)
|
||||
mstore(a, inv)
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// step: loopInvariantCodeMotion
|
||||
// ----
|
||||
// {
|
||||
// let b := 1
|
||||
// let a := 1
|
||||
// let inv := add(b, 42)
|
||||
// for { } iszero(eq(a, 10)) { a := add(a, 1) }
|
||||
// {
|
||||
// a := add(a, 1)
|
||||
// mstore(a, inv)
|
||||
// }
|
||||
// }
|
||||
Reference in New Issue
Block a user