Out of body again.

This commit is contained in:
chriseth 2019-09-11 18:55:31 +02:00
parent 58f0f9dbea
commit 7a79742f3d
11 changed files with 223 additions and 75 deletions

View File

@ -88,6 +88,8 @@ add_library(yul
optimiser/ExpressionSplitter.h
optimiser/ForLoopConditionIntoBody.cpp
optimiser/ForLoopConditionIntoBody.h
optimiser/ForLoopConditionOutOfBody.cpp
optimiser/ForLoopConditionOutOfBody.h
optimiser/ForLoopInitRewriter.cpp
optimiser/ForLoopInitRewriter.h
optimiser/FullInliner.cpp

View File

@ -0,0 +1,69 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#include <libyul/optimiser/ForLoopConditionOutOfBody.h>
#include <libyul/optimiser/Semantics.h>
#include <libyul/AsmData.h>
#include <libyul/Utilities.h>
#include <libdevcore/CommonData.h>
using namespace std;
using namespace dev;
using namespace yul;
void ForLoopConditionOutOfBody::operator()(ForLoop& _forLoop)
{
ASTModifier::operator()(_forLoop);
if (
!m_dialect.booleanNegationFunction() ||
_forLoop.condition->type() != typeid(Literal) ||
valueOfLiteral(boost::get<Literal>(*_forLoop.condition)) == u256(0) ||
_forLoop.body.statements.empty() ||
_forLoop.body.statements.front().type() != typeid(If)
)
return;
If& firstStatement = boost::get<If>(_forLoop.body.statements.front());
if (
firstStatement.body.statements.empty() ||
firstStatement.body.statements.front().type() != typeid(Break)
)
return;
if (!SideEffectsCollector(m_dialect, *firstStatement.condition).movable())
return;
YulString iszero = m_dialect.booleanNegationFunction()->name;
langutil::SourceLocation location = locationOf(*firstStatement.condition);
if (
firstStatement.condition->type() == typeid(FunctionCall) &&
boost::get<FunctionCall>(*firstStatement.condition).functionName.name == iszero
)
_forLoop.condition = make_unique<Expression>(std::move(boost::get<FunctionCall>(*firstStatement.condition).arguments.front()));
else
_forLoop.condition = make_unique<Expression>(FunctionCall{
location,
Identifier{location, iszero},
make_vector<Expression>(
std::move(*firstStatement.condition)
)
});
_forLoop.body.statements.erase(_forLoop.body.statements.begin());
}

View File

@ -0,0 +1,69 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <libyul/optimiser/ASTWalker.h>
#include <libyul/Dialect.h>
namespace yul
{
/**
* Reverses the transformation of ForLoopConditionIntoBody.
*
* For any movable ``c``, it turns
*
* for { ... } 1 { ... } {
* if iszero(c) { break }
* ...
* }
*
* into
*
* for { ... } c { ... } {
* ...
* }
*
* and it turns
*
* for { ... } 1 { ... } {
* if c { break }
* ...
* }
*
* into
*
* for { ... } iszero(c) { ... } {
* ...
* }
*
* The LiteralRematerialiser should be run before this step.
*/
class ForLoopConditionOutOfBody: public ASTModifier
{
public:
ForLoopConditionOutOfBody(Dialect const& _dialect):
m_dialect(_dialect)
{}
using ASTModifier::operator();
void operator()(ForLoop& _forLoop) override;
private:
Dialect const& m_dialect;
};
}

View File

@ -34,6 +34,7 @@
#include <libyul/optimiser/ExpressionInliner.h>
#include <libyul/optimiser/FullInliner.h>
#include <libyul/optimiser/ForLoopConditionIntoBody.h>
#include <libyul/optimiser/ForLoopConditionOutOfBody.h>
#include <libyul/optimiser/ForLoopInitRewriter.h>
#include <libyul/optimiser/Rematerialiser.h>
#include <libyul/optimiser/UnusedPruner.h>
@ -127,12 +128,14 @@ void OptimiserSuite::run(
{
// still in SSA, perform structural simplification
ControlFlowSimplifier{_dialect}(ast);
LiteralRematerialiser{_dialect}(ast);
ForLoopConditionOutOfBody{_dialect}(ast);
ControlFlowSimplifier{_dialect}(ast);
StructuralSimplifier{}(ast);
ControlFlowSimplifier{_dialect}(ast);
BlockFlattener{}(ast);
DeadCodeEliminator{_dialect}(ast);
ForLoopConditionIntoBody{_dialect}(ast);
UnusedPruner::runUntilStabilisedOnFullAST(_dialect, ast, reservedIdentifiers);
}
@ -187,6 +190,7 @@ void OptimiserSuite::run(
LoadResolver::run(_dialect, ast);
ExpressionSimplifier::run(_dialect, ast);
LiteralRematerialiser{_dialect}(ast);
ForLoopConditionOutOfBody{_dialect}(ast);
StructuralSimplifier{}(ast);
BlockFlattener{}(ast);
DeadCodeEliminator{_dialect}(ast);
@ -195,6 +199,7 @@ void OptimiserSuite::run(
SSATransform::run(ast, dispenser);
RedundantAssignEliminator::run(_dialect, ast);
RedundantAssignEliminator::run(_dialect, ast);
ForLoopConditionIntoBody{_dialect}(ast);
UnusedPruner::runUntilStabilisedOnFullAST(_dialect, ast, reservedIdentifiers);
CommonSubexpressionEliminator::run(_dialect, ast);
}
@ -212,6 +217,9 @@ void OptimiserSuite::run(
SSAReverser::run(ast);
CommonSubexpressionEliminator::run(_dialect, ast);
LiteralRematerialiser{_dialect}(ast);
ForLoopConditionOutOfBody{_dialect}(ast);
CommonSubexpressionEliminator::run(_dialect, ast);
UnusedPruner::runUntilStabilisedOnFullAST(_dialect, ast, reservedIdentifiers);
ExpressionJoiner::run(ast);
@ -232,6 +240,9 @@ void OptimiserSuite::run(
BlockFlattener{}(ast);
DeadCodeEliminator{_dialect}(ast);
ControlFlowSimplifier{_dialect}(ast);
LiteralRematerialiser{_dialect}(ast);
ForLoopConditionOutOfBody{_dialect}(ast);
CommonSubexpressionEliminator::run(_dialect, ast);
FunctionGrouper{}(ast);

View File

@ -26,9 +26,9 @@
(param $x4 i64)
(result i64)
(local $v i64)
(if (i64.ne (i64.const 0) (i64.or (i64.or (get_local $x1) (get_local $x2)) (get_local $x3))) (then
(if (i64.ne (get_local $v) (i64.or (i64.or (get_local $x1) (get_local $x2)) (get_local $x3))) (then
(unreachable)))
(if (i64.ne (i64.const 0) (i64.shr_u (get_local $x4) (i64.const 32))) (then
(if (i64.ne (get_local $v) (i64.shr_u (get_local $x4) (i64.const 32))) (then
(unreachable)))
(set_local $v (get_local $x4))
(get_local $v)
@ -107,9 +107,9 @@
(param $x4 i64)
(result i64)
(local $v i64)
(if (i64.ne (i64.const 0) (i64.or (i64.or (get_local $x1) (get_local $x2)) (get_local $x3))) (then
(if (i64.ne (get_local $v) (i64.or (i64.or (get_local $x1) (get_local $x2)) (get_local $x3))) (then
(unreachable)))
(if (i64.ne (i64.const 0) (i64.shr_u (get_local $x4) (i64.const 32))) (then
(if (i64.ne (get_local $v) (i64.shr_u (get_local $x4) (i64.const 32))) (then
(unreachable)))
(set_local $v (get_local $x4))
(get_local $v)

View File

@ -17,9 +17,9 @@ contract C {
// optimize-yul: true
// ----
// creation:
// codeDepositCost: 624200
// executionCost: 657
// totalCost: 624857
// codeDepositCost: 608400
// executionCost: 645
// totalCost: 609045
// external:
// a(): 429
// b(uint256): 884

View File

@ -36,6 +36,7 @@
#include <libyul/optimiser/ExpressionInliner.h>
#include <libyul/optimiser/FullInliner.h>
#include <libyul/optimiser/ForLoopConditionIntoBody.h>
#include <libyul/optimiser/ForLoopConditionOutOfBody.h>
#include <libyul/optimiser/ForLoopInitRewriter.h>
#include <libyul/optimiser/LoadResolver.h>
#include <libyul/optimiser/MainFunction.h>

View File

@ -465,21 +465,19 @@
// let _1 := 0
// let _2 := mload(_1)
// let pos := 0x20
// let pos_1 := pos
// let length := mload(_2)
// mstore(pos, length)
// pos := 64
// let srcPtr := add(_2, pos_1)
// let srcPtr := add(_2, 0x20)
// let i := _1
// for { } 1 { i := add(i, 1) }
// for { } lt(i, length) { i := add(i, 1) }
// {
// if iszero(lt(i, length)) { break }
// abi_encode_t_array$_t_contract$_C_$55_$3_memory_to_t_array$_t_address_$3_memory_ptr(mload(srcPtr), pos)
// srcPtr := add(srcPtr, pos_1)
// srcPtr := add(srcPtr, 0x20)
// pos := add(pos, 0x60)
// }
// let _3 := mload(64)
// let _4 := mload(pos_1)
// let _4 := mload(0x20)
// if slt(sub(_3, _4), 128) { revert(_1, _1) }
// let offset := calldataload(add(_4, 64))
// let _5 := 0xffffffffffffffff
@ -488,7 +486,7 @@
// let offset_1 := calldataload(add(_4, 96))
// 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, pos_1)))
// sstore(calldataload(_4), calldataload(add(_4, 0x20)))
// sstore(value2, value3)
// sstore(_1, pos)
// }
@ -504,9 +502,8 @@
// let src := add(offset, _1)
// if gt(add(add(offset, mul(length, 0x40)), _1), end) { revert(0, 0) }
// let i := 0
// for { } 1 { i := add(i, 1) }
// for { } lt(i, length) { i := add(i, 1) }
// {
// if iszero(lt(i, length)) { break }
// if iszero(slt(add(src, 0x1f), end)) { revert(0, 0) }
// let dst_1 := allocateMemory(array_allocation_size_t_array$_t_uint256_$2_memory(0x2))
// let dst_2 := dst_1
@ -514,9 +511,8 @@
// let _2 := add(src, 0x40)
// if gt(_2, end) { revert(0, 0) }
// let i_1 := 0
// for { } 1 { i_1 := add(i_1, 1) }
// for { } lt(i_1, 0x2) { i_1 := add(i_1, 1) }
// {
// if iszero(lt(i_1, 0x2)) { break }
// mstore(dst_1, calldataload(src_1))
// dst_1 := add(dst_1, _1)
// src_1 := add(src_1, _1)
@ -538,9 +534,8 @@
// let src := add(offset, _1)
// if gt(add(add(offset, mul(length, _1)), _1), end) { revert(0, 0) }
// let i := 0
// for { } 1 { i := add(i, 1) }
// for { } lt(i, length) { i := add(i, 1) }
// {
// if iszero(lt(i, length)) { break }
// mstore(dst, calldataload(src))
// dst := add(dst, _1)
// src := add(src, _1)
@ -550,9 +545,8 @@
// {
// let srcPtr := value
// let i := 0
// for { } 1 { i := add(i, 1) }
// for { } lt(i, 0x3) { i := add(i, 1) }
// {
// if iszero(lt(i, 0x3)) { break }
// mstore(pos, and(mload(srcPtr), sub(shl(160, 1), 1)))
// srcPtr := add(srcPtr, 0x20)
// pos := add(pos, 0x20)
@ -567,12 +561,12 @@
// }
// function array_allocation_size_t_array$_t_address_$dyn_memory(length) -> size
// {
// if gt(length, 0xffffffffffffffff) { revert(0, 0) }
// if gt(length, 0xffffffffffffffff) { revert(size, size) }
// size := add(mul(length, 0x20), 0x20)
// }
// function array_allocation_size_t_array$_t_uint256_$2_memory(length) -> size
// {
// if gt(length, 0xffffffffffffffff) { revert(0, 0) }
// if gt(length, 0xffffffffffffffff) { revert(size, size) }
// size := mul(length, 0x20)
// }
// }

View File

@ -233,13 +233,14 @@
// ----
// {
// {
// mstore(0x80, 7673901602397024137095011250362199966051872585513276903826533215767972925880)
// let _1 := 0x80
// mstore(_1, 7673901602397024137095011250362199966051872585513276903826533215767972925880)
// mstore(0xa0, 8489654445897228341090914135473290831551238522473825886865492707826370766375)
// let notes := add(0x04, calldataload(0x04))
// let m := calldataload(0x24)
// let n := calldataload(notes)
// let _1 := 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001
// let challenge := mod(calldataload(0x44), _1)
// let _2 := 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001
// let challenge := mod(calldataload(0x44), _2)
// if gt(m, n)
// {
// mstore(0x00, 404)
@ -249,77 +250,75 @@
// mstore(0x2a0, caller())
// mstore(0x2c0, kn)
// mstore(0x2e0, m)
// kn := mulmod(sub(_1, kn), challenge, _1)
// kn := mulmod(sub(_2, kn), challenge, _2)
// hashCommitments(notes, n)
// let b := add(0x300, mul(n, 0x80))
// let b := add(0x300, mul(n, _1))
// let i := 0
// let i_1 := i
// for { } 1 { i := add(i, 0x01) }
// for { } lt(i, n) { i := add(i, 0x01) }
// {
// if iszero(lt(i, n)) { break }
// let _2 := add(calldataload(0x04), mul(i, 0xc0))
// let noteIndex := add(_2, 0x24)
// let k := i_1
// let a := calldataload(add(_2, 0x44))
// let _3 := add(calldataload(0x04), mul(i, 0xc0))
// let noteIndex := add(_3, 0x24)
// let k := 0
// let a := calldataload(add(_3, 0x44))
// let c := challenge
// let _3 := add(i, 0x01)
// switch eq(_3, n)
// let _4 := add(i, 0x01)
// switch eq(_4, n)
// case 1 {
// k := kn
// if eq(m, n) { k := sub(_1, kn) }
// if eq(m, n) { k := sub(_2, kn) }
// }
// case 0 { k := calldataload(noteIndex) }
// validateCommitment(noteIndex, k, a)
// switch gt(_3, m)
// switch gt(_4, m)
// case 1 {
// kn := addmod(kn, sub(_1, k), _1)
// let x := mod(mload(i_1), _1)
// k := mulmod(k, x, _1)
// a := mulmod(a, x, _1)
// c := mulmod(challenge, x, _1)
// mstore(i_1, keccak256(i_1, 0x20))
// kn := addmod(kn, sub(_2, k), _2)
// let x := mod(mload(0), _2)
// k := mulmod(k, x, _2)
// a := mulmod(a, x, _2)
// c := mulmod(challenge, x, _2)
// mstore(0, keccak256(0, 0x20))
// }
// case 0 { kn := addmod(kn, k, _1) }
// let _4 := 0x40
// calldatacopy(0xe0, add(_2, 164), _4)
// calldatacopy(0x20, add(_2, 100), _4)
// mstore(0x120, sub(_1, c))
// case 0 { kn := addmod(kn, k, _2) }
// let _5 := 0x40
// calldatacopy(0xe0, add(_3, 164), _5)
// calldatacopy(0x20, add(_3, 100), _5)
// mstore(0x120, sub(_2, c))
// mstore(0x60, k)
// mstore(0xc0, a)
// let result := call(gas(), 7, i_1, 0xe0, 0x60, 0x1a0, _4)
// let result_1 := and(result, call(gas(), 7, i_1, 0x20, 0x60, 0x120, _4))
// let result_2 := and(result_1, call(gas(), 7, i_1, 0x80, 0x60, 0x160, _4))
// let result_3 := and(result_2, call(gas(), 6, i_1, 0x120, 0x80, 0x160, _4))
// result := and(result_3, call(gas(), 6, i_1, 0x160, 0x80, b, _4))
// let result := call(gas(), 7, 0, 0xe0, 0x60, 0x1a0, _5)
// let result_1 := and(result, call(gas(), 7, 0, 0x20, 0x60, 0x120, _5))
// let result_2 := and(result_1, call(gas(), 7, 0, _1, 0x60, 0x160, _5))
// let result_3 := and(result_2, call(gas(), 6, 0, 0x120, _1, 0x160, _5))
// result := and(result_3, call(gas(), 6, 0, 0x160, _1, b, _5))
// if eq(i, m)
// {
// mstore(0x260, mload(0x20))
// mstore(0x280, mload(_4))
// mstore(0x280, mload(_5))
// mstore(0x1e0, mload(0xe0))
// mstore(0x200, sub(0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47, mload(0x100)))
// }
// if gt(i, m)
// {
// mstore(0x60, c)
// let result_4 := and(result, call(gas(), 7, i_1, 0x20, 0x60, 0x220, _4))
// let result_5 := and(result_4, call(gas(), 6, i_1, 0x220, 0x80, 0x260, _4))
// result := and(result_5, call(gas(), 6, i_1, 0x1a0, 0x80, 0x1e0, _4))
// let result_4 := and(result, call(gas(), 7, 0, 0x20, 0x60, 0x220, _5))
// let result_5 := and(result_4, call(gas(), 6, 0, 0x220, _1, 0x260, _5))
// result := and(result_5, call(gas(), 6, 0, 0x1a0, _1, 0x1e0, _5))
// }
// if iszero(result)
// {
// mstore(i_1, 400)
// revert(i_1, 0x20)
// mstore(0, 400)
// revert(0, 0x20)
// }
// b := add(b, _4)
// b := add(b, _5)
// }
// if lt(m, n) { validatePairing(0x64) }
// if iszero(eq(mod(keccak256(0x2a0, add(b, not(671))), _1), challenge))
// if iszero(eq(mod(keccak256(0x2a0, add(b, not(671))), _2), challenge))
// {
// mstore(i_1, 404)
// revert(i_1, 0x20)
// mstore(0, 404)
// revert(0, 0x20)
// }
// mstore(i_1, 0x01)
// return(i_1, 0x20)
// mstore(0, 0x01)
// return(0, 0x20)
// }
// function validatePairing(t2)
// {
@ -374,9 +373,8 @@
// function hashCommitments(notes, n)
// {
// let i := 0
// for { } 1 { i := add(i, 0x01) }
// for { } lt(i, n) { i := add(i, 0x01) }
// {
// if iszero(lt(i, n)) { break }
// calldatacopy(add(0x300, mul(i, 0x80)), add(add(notes, mul(i, 0xc0)), 0x60), 0x80)
// }
// mstore(0, keccak256(0x300, mul(n, 0x80)))

View File

@ -47,9 +47,9 @@
// }
// function abi_decode_t_bytes_calldata_ptr(offset, end) -> arrayPos, length
// {
// if iszero(slt(add(offset, 0x1f), end)) { revert(0, 0) }
// if iszero(slt(add(offset, 0x1f), end)) { revert(arrayPos, arrayPos) }
// length := calldataload(offset)
// if gt(length, 0xffffffffffffffff) { revert(0, 0) }
// if gt(length, 0xffffffffffffffff) { revert(arrayPos, arrayPos) }
// arrayPos := add(offset, 0x20)
// if gt(add(add(offset, length), 0x20), end) { revert(0, 0) }
// }

View File

@ -43,6 +43,7 @@
#include <libyul/optimiser/ExpressionInliner.h>
#include <libyul/optimiser/FullInliner.h>
#include <libyul/optimiser/ForLoopConditionIntoBody.h>
#include <libyul/optimiser/ForLoopConditionOutOfBody.h>
#include <libyul/optimiser/ForLoopInitRewriter.h>
#include <libyul/optimiser/MainFunction.h>
#include <libyul/optimiser/Rematerialiser.h>
@ -134,8 +135,8 @@ public:
}
cout << "(q)quit/(f)flatten/(c)se/initialize var(d)ecls/(x)plit/(j)oin/(g)rouper/(h)oister/" << endl;
cout << " (e)xpr inline/(i)nline/(s)implify/varname c(l)eaner/(u)nusedprune/ss(a) transform/" << endl;
cout << " (r)edundant assign elim./re(m)aterializer/f(o)r-loop-init-rewriter/f(O)r-loop-condition-into-body/" << endl;
cout << " s(t)ructural simplifier/equi(v)alent function combiner/ssa re(V)erser/? " << endl;
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.flush();
int option = readStandardInputChar();
@ -151,6 +152,9 @@ public:
ForLoopInitRewriter{}(*m_ast);
break;
case 'O':
ForLoopConditionOutOfBody{m_dialect}(*m_ast);
break;
case 'I':
ForLoopConditionIntoBody{m_dialect}(*m_ast);
break;
case 'c':