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:
@@ -41,6 +41,7 @@
|
||||
#include <libyul/optimiser/ForLoopConditionOutOfBody.h>
|
||||
#include <libyul/optimiser/ForLoopInitRewriter.h>
|
||||
#include <libyul/optimiser/LoadResolver.h>
|
||||
#include <libyul/optimiser/LoopInvariantCodeMotion.h>
|
||||
#include <libyul/optimiser/MainFunction.h>
|
||||
#include <libyul/optimiser/NameDisplacer.h>
|
||||
#include <libyul/optimiser/Rematerialiser.h>
|
||||
@@ -279,6 +280,12 @@ TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _line
|
||||
ExpressionJoiner::run(*m_context, *m_ast);
|
||||
ExpressionJoiner::run(*m_context, *m_ast);
|
||||
}
|
||||
else if (m_optimizerStep == "loopInvariantCodeMotion")
|
||||
{
|
||||
disambiguate();
|
||||
ForLoopInitRewriter::run(*m_context, *m_ast);
|
||||
LoopInvariantCodeMotion::run(*m_context, *m_ast);
|
||||
}
|
||||
else if (m_optimizerStep == "controlFlowSimplifier")
|
||||
{
|
||||
disambiguate();
|
||||
|
||||
@@ -483,7 +483,7 @@
|
||||
// let _5 := 0xffffffffffffffff
|
||||
// if gt(offset, _5) { revert(_1, _1) }
|
||||
// let value2 := abi_decode_t_array$_t_uint256_$dyn_memory_ptr(add(_4, offset), _3)
|
||||
// let offset_1 := calldataload(add(_4, 96))
|
||||
// let offset_1 := calldataload(add(_4, 0x60))
|
||||
// if gt(offset_1, _5) { revert(_1, _1) }
|
||||
// let value3 := abi_decode_t_array$_t_array$_t_uint256_$2_memory_$dyn_memory_ptr(add(_4, offset_1), _3)
|
||||
// sstore(calldataload(_4), calldataload(add(_4, 0x20)))
|
||||
|
||||
@@ -311,7 +311,7 @@
|
||||
// }
|
||||
// b := add(b, _5)
|
||||
// }
|
||||
// if lt(m, n) { validatePairing(0x64) }
|
||||
// if lt(m, n) { validatePairing(100) }
|
||||
// if iszero(eq(mod(keccak256(0x2a0, add(b, not(671))), _2), challenge))
|
||||
// {
|
||||
// mstore(0, 404)
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
// {
|
||||
// {
|
||||
// let y := mload(0x20)
|
||||
// for { } and(y, 8) { if y { revert(0, 0) } }
|
||||
// for { } iszero(iszero(and(y, 8))) { if y { revert(0, 0) } }
|
||||
// {
|
||||
// if y { continue }
|
||||
// sstore(1, 0)
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
{
|
||||
sstore(0, array_sum(calldataload(0)))
|
||||
|
||||
function array_sum(x) -> sum {
|
||||
let length := calldataload(x)
|
||||
for { let i := 0 } lt(i, length) { i := add(i, 1) } {
|
||||
sum := add(sum, array_load(x, i))
|
||||
}
|
||||
}
|
||||
function array_load(x, i) -> v {
|
||||
let len := calldataload(x)
|
||||
if iszero(lt(i, len)) { revert(0, 0) }
|
||||
let data := add(x, 0x20)
|
||||
v := calldataload(add(data, mul(i, 0x20)))
|
||||
// this is just to have some additional code that
|
||||
// can be moved out of the loop.
|
||||
v := add(v, calldataload(7))
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// step: fullSuite
|
||||
// ----
|
||||
// {
|
||||
// {
|
||||
// let _1 := calldataload(0)
|
||||
// let sum := 0
|
||||
// let i := sum
|
||||
// for { } lt(i, calldataload(_1)) { i := add(i, 1) }
|
||||
// {
|
||||
// sum := add(sum, add(calldataload(add(add(_1, mul(i, 0x20)), 0x20)), calldataload(7)))
|
||||
// }
|
||||
// sstore(0, sum)
|
||||
// }
|
||||
// }
|
||||
@@ -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)
|
||||
// }
|
||||
// }
|
||||
@@ -62,6 +62,7 @@
|
||||
#include <libyul/optimiser/VarDeclInitializer.h>
|
||||
#include <libyul/optimiser/VarNameCleaner.h>
|
||||
#include <libyul/optimiser/LoadResolver.h>
|
||||
#include <libyul/optimiser/LoopInvariantCodeMotion.h>
|
||||
|
||||
#include <libyul/backends/evm/EVMDialect.h>
|
||||
|
||||
@@ -142,7 +143,7 @@ public:
|
||||
cout << " (r)edundant assign elim./re(m)aterializer/f(o)r-loop-init-rewriter/for-loop-condition-(I)nto-body/" << endl;
|
||||
cout << " for-loop-condition-(O)ut-of-body/s(t)ructural simplifier/equi(v)alent function combiner/ssa re(V)erser/" << endl;
|
||||
cout << " co(n)trol flow simplifier/stack com(p)ressor/(D)ead code eliminator/(L)oad resolver/" << endl;
|
||||
cout << " (C)onditional simplifier?" << endl;
|
||||
cout << " (C)onditional simplifier/loop-invariant code (M)otion?" << endl;
|
||||
cout.flush();
|
||||
int option = readStandardInputChar();
|
||||
cout << ' ' << char(option) << endl;
|
||||
@@ -237,6 +238,9 @@ public:
|
||||
case 'L':
|
||||
LoadResolver::run(context, *m_ast);
|
||||
break;
|
||||
case 'M':
|
||||
LoopInvariantCodeMotion::run(context, *m_ast);
|
||||
break;
|
||||
default:
|
||||
cout << "Unknown option." << endl;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user