Conditional simplifier.

This commit is contained in:
chriseth 2019-09-11 11:42:59 +02:00
parent 1bd2b202e9
commit 5d6e983be3
20 changed files with 525 additions and 52 deletions

View File

@ -26,6 +26,7 @@ Compiler Features:
* SMTChecker: Add loop support to the CHC engine.
* Yul Optimizer: Take side-effect-freeness of user-defined functions into account.
* Yul Optimizer: Remove redundant mload/sload operations.
* Yul Optimizer: Use the fact that branch conditions have certain value inside the branch.
Bugfixes:

View File

@ -66,6 +66,8 @@ add_library(yul
optimiser/CallGraphGenerator.h
optimiser/CommonSubexpressionEliminator.cpp
optimiser/CommonSubexpressionEliminator.h
optimiser/ConditionalSimplifier.cpp
optimiser/ConditionalSimplifier.h
optimiser/ControlFlowSimplifier.cpp
optimiser/ControlFlowSimplifier.h
optimiser/DataFlowAnalyzer.cpp

View File

@ -0,0 +1,92 @@
/*
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/ConditionalSimplifier.h>
#include <libyul/optimiser/Semantics.h>
#include <libyul/AsmData.h>
#include <libyul/Utilities.h>
#include <libyul/optimiser/NameCollector.h>
#include <libdevcore/CommonData.h>
#include <libdevcore/Visitor.h>
using namespace std;
using namespace dev;
using namespace yul;
void ConditionalSimplifier::operator()(Switch& _switch)
{
visit(*_switch.expression);
if (_switch.expression->type() != typeid(Identifier))
{
ASTModifier::operator()(_switch);
return;
}
YulString expr = boost::get<Identifier>(*_switch.expression).name;
for (auto& _case: _switch.cases)
{
if (_case.value)
{
(*this)(*_case.value);
_case.body.statements.insert(_case.body.statements.begin(),
Assignment{
_case.body.location,
{Identifier{_case.body.location, expr}},
make_unique<Expression>(*_case.value)
}
);
}
(*this)(_case.body);
}
}
void ConditionalSimplifier::operator()(Block& _block)
{
iterateReplacing(
_block.statements,
[&](Statement& _s) -> std::optional<vector<Statement>>
{
visit(_s);
if (_s.type() == typeid(If))
{
If& _if = boost::get<If>(_s);
if (
_if.condition->type() == typeid(Identifier) &&
!_if.body.statements.empty() &&
TerminationFinder(m_dialect).controlFlowKind(_if.body.statements.back()) !=
TerminationFinder::ControlFlow::FlowOut
)
{
YulString condition = boost::get<Identifier>(*_if.condition).name;
langutil::SourceLocation location = _if.location;
return make_vector<Statement>(
std::move(_s),
Assignment{
location,
{Identifier{location, condition}},
make_unique<Expression>(Literal{
location,
LiteralKind::Number,
"0"_yulstring,
{}
})
}
);
}
}
return {};
}
);
}

View File

@ -0,0 +1,71 @@
/*
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/optimiser/OptimiserStep.h>
#include <libyul/Dialect.h>
#include <libdevcore/Common.h>
namespace yul
{
/**
* Conditional simplifier.
*
* Inserts assignments to condition variables if the value can be determined
* from the control-flow.
*
* Destroys SSA form.
*
* Currently, this tool is very limited, mostly because we do not yet have support
* for boolean types. Since conditions only check for expressions being nonzero,
* we cannot assign a specific value.
*
* Current features:
* - switch cases: insert "<condition> := <caseLabel>"
* - after if statement with terminating control-flow, insert "<condition> := 0"
*
* Future features:
* - allow replacements by "1"
* - take termination of user-defined functions into account
*
* Works best with SSA form and if dead code removal has run before.
*
* Prerequisite: Disambiguator.
*/
class ConditionalSimplifier: public ASTModifier
{
public:
static constexpr char const* name{"ConditionalSimplifier"};
static void run(OptimiserStepContext& _context, Block& _ast)
{
ConditionalSimplifier{_context.dialect}(_ast);
}
using ASTModifier::operator();
void operator()(Switch& _switch) override;
void operator()(Block& _block) override;
private:
explicit ConditionalSimplifier(Dialect const& _dialect):
m_dialect(_dialect)
{}
Dialect const& m_dialect;
};
}

View File

@ -25,6 +25,7 @@
#include <libyul/optimiser/BlockFlattener.h>
#include <libyul/optimiser/CallGraphGenerator.h>
#include <libyul/optimiser/ControlFlowSimplifier.h>
#include <libyul/optimiser/ConditionalSimplifier.h>
#include <libyul/optimiser/DeadCodeEliminator.h>
#include <libyul/optimiser/FunctionGrouper.h>
#include <libyul/optimiser/FunctionHoister.h>
@ -36,6 +37,7 @@
#include <libyul/optimiser/ForLoopConditionIntoBody.h>
#include <libyul/optimiser/ForLoopConditionOutOfBody.h>
#include <libyul/optimiser/ForLoopInitRewriter.h>
#include <libyul/optimiser/ForLoopConditionIntoBody.h>
#include <libyul/optimiser/Rematerialiser.h>
#include <libyul/optimiser/UnusedPruner.h>
#include <libyul/optimiser/ExpressionSimplifier.h>
@ -96,6 +98,7 @@ void OptimiserSuite::run(
EquivalentFunctionCombiner::name,
UnusedPruner::name,
BlockFlattener::name,
ConditionalSimplifier::name,
ControlFlowSimplifier::name,
LiteralRematerialiser::name,
StructuralSimplifier::name,
@ -130,9 +133,12 @@ void OptimiserSuite::run(
}
{
// still in SSA, perform structural simplification
// perform structural simplification
suite.runSequence({
CommonSubexpressionEliminator::name,
ConditionalSimplifier::name,
LiteralRematerialiser::name,
StructuralSimplifier::name,
ForLoopConditionOutOfBody::name,
ControlFlowSimplifier::name,
StructuralSimplifier::name,
@ -200,6 +206,8 @@ void OptimiserSuite::run(
{
// SSA plus simplify
suite.runSequence({
ConditionalSimplifier::name,
CommonSubexpressionEliminator::name,
SSATransform::name,
RedundantAssignEliminator::name,
RedundantAssignEliminator::name,
@ -315,6 +323,7 @@ map<string, unique_ptr<OptimiserStep>> const& OptimiserSuite::allSteps()
instance = optimiserStepCollection<
BlockFlattener,
CommonSubexpressionEliminator,
ConditionalSimplifier,
ControlFlowSimplifier,
DeadCodeEliminator,
EquivalentFunctionCombiner,

View File

@ -27,6 +27,7 @@
#include <libyul/optimiser/DeadCodeEliminator.h>
#include <libyul/optimiser/Disambiguator.h>
#include <libyul/optimiser/CallGraphGenerator.h>
#include <libyul/optimiser/ConditionalSimplifier.h>
#include <libyul/optimiser/CommonSubexpressionEliminator.h>
#include <libyul/optimiser/NameCollector.h>
#include <libyul/optimiser/EquivalentFunctionCombiner.h>
@ -157,6 +158,11 @@ TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _line
disambiguate();
CommonSubexpressionEliminator::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "conditionalSimplifier")
{
disambiguate();
ConditionalSimplifier::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "expressionSplitter")
ExpressionSplitter::run(*m_context, *m_ast);
else if (m_optimizerStep == "expressionJoiner")

View File

@ -0,0 +1,17 @@
{
let y := mload(0x20)
for {} and(y, 8) { pop(y) } {
if y { break }
}
}
// ====
// step: conditionalSimplifier
// ----
// {
// let y := mload(0x20)
// for { } and(y, 8) { pop(y) }
// {
// if y { break }
// y := 0
// }
// }

View File

@ -0,0 +1,17 @@
{
let y := mload(0x20)
for {} and(y, 8) { pop(y) } {
if y { continue }
}
}
// ====
// step: conditionalSimplifier
// ----
// {
// let y := mload(0x20)
// for { } and(y, 8) { pop(y) }
// {
// if y { continue }
// y := 0
// }
// }

View File

@ -0,0 +1,22 @@
{
let x := mload(0)
let y := mload(0)
if x { revert(0, 0) }
if y { revert(0, 0) }
for {} and(x, y) {} {
x := 2
}
}
// ====
// step: conditionalSimplifier
// ----
// {
// let x := mload(0)
// let y := mload(0)
// if x { revert(0, 0) }
// x := 0
// if y { revert(0, 0) }
// y := 0
// for { } and(x, y) { }
// { x := 2 }
// }

View File

@ -0,0 +1,23 @@
{
let x
for {} x { sstore(1, x) } {
if x { continue }
// x is 0 here, but should not be 0
// anymore in the for loop post block
sstore(0, x)
}
sstore(0, x)
}
// ====
// step: conditionalSimplifier
// ----
// {
// let x
// for { } x { sstore(1, x) }
// {
// if x { continue }
// x := 0
// sstore(0, x)
// }
// sstore(0, x)
// }

View File

@ -0,0 +1,32 @@
{
let x := mload(0)
for {} 1 {} {
if x { sstore(7, 8) break sstore(8, 9) }
sstore(1, x)
if x { sstore(7, 8) break }
sstore(10, x)
}
}
// ====
// step: conditionalSimplifier
// ----
// {
// let x := mload(0)
// for { } 1 { }
// {
// if x
// {
// sstore(7, 8)
// break
// sstore(8, 9)
// }
// sstore(1, x)
// if x
// {
// sstore(7, 8)
// break
// }
// x := 0
// sstore(10, x)
// }
// }

View File

@ -0,0 +1,13 @@
{
let x := mload(0)
if x { sstore(0, x) }
sstore(1, x)
}
// ====
// step: conditionalSimplifier
// ----
// {
// let x := mload(0)
// if x { sstore(0, x) }
// sstore(1, x)
// }

View File

@ -0,0 +1,18 @@
{
let x := mload(0)
if x { sstore(0, x) revert(0, 0) }
sstore(1, x)
}
// ====
// step: conditionalSimplifier
// ----
// {
// let x := mload(0)
// if x
// {
// sstore(0, x)
// revert(0, 0)
// }
// x := 0
// sstore(1, x)
// }

View File

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

View File

@ -0,0 +1,5 @@
{ }
// ====
// step: conditionalSimplifier
// ----
// { }

View File

@ -233,15 +233,11 @@
// ----
// {
// {
// let _1 := 0x80
// mstore(_1, 7673901602397024137095011250362199966051872585513276903826533215767972925880)
// mstore(0x80, 7673901602397024137095011250362199966051872585513276903826533215767972925880)
// mstore(0xa0, 8489654445897228341090914135473290831551238522473825886865492707826370766375)
// let notes := add(0x04, calldataload(0x04))
// let m := calldataload(0x24)
// let n := calldataload(notes)
// let _2 := 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001
// let challenge := mod(calldataload(0x44), _2)
// if gt(m, n)
// let n := calldataload(add(0x04, calldataload(0x04)))
// let _1 := 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001
// if gt(calldataload(0x24), n)
// {
// mstore(0x00, 404)
// revert(0x00, 0x20)
@ -249,70 +245,95 @@
// let kn := calldataload(add(calldatasize(), not(191)))
// mstore(0x2a0, caller())
// mstore(0x2c0, kn)
// mstore(0x2e0, m)
// kn := mulmod(sub(_2, kn), challenge, _2)
// hashCommitments(notes, n)
// let b := add(0x300, mul(n, _1))
// mstore(0x2e0, calldataload(0x24))
// kn := mulmod(sub(_1, kn), mod(calldataload(0x44), _1), _1)
// hashCommitments(add(0x04, calldataload(0x04)), n)
// let b := add(0x300, mul(n, 0x80))
// let i := 0
// for { } lt(i, n) { i := add(i, 0x01) }
// {
// let _3 := add(calldataload(0x04), mul(i, 0xc0))
// let noteIndex := add(_3, 0x24)
// let _2 := add(calldataload(0x04), mul(i, 0xc0))
// let k := 0
// let a := calldataload(add(_3, 0x44))
// let c := challenge
// let _4 := add(i, 0x01)
// switch eq(_4, n)
// let a := calldataload(add(_2, 0x44))
// let c := mod(calldataload(0x44), _1)
// let _3 := eq(add(i, 0x01), n)
// let _4 := _3
// let _5 := _3
// let _6 := _3
// switch _3
// case 1 {
// _3 := 0x01
// _4 := _3
// _5 := _3
// _6 := _3
// k := kn
// if eq(m, n) { k := sub(_2, kn) }
// if eq(calldataload(0x24), n) { k := sub(_1, kn) }
// }
// case 0 { k := calldataload(noteIndex) }
// validateCommitment(noteIndex, k, a)
// switch gt(_4, m)
// case 0 {
// _3 := 0
// _4 := _3
// _5 := _3
// _6 := _3
// k := calldataload(add(_2, 0x24))
// }
// validateCommitment(add(_2, 0x24), k, a)
// let _7 := gt(add(i, 0x01), calldataload(0x24))
// let _8 := _7
// let _9 := _7
// let _10 := _7
// switch _7
// case 1 {
// 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)
// _7 := 0x01
// _8 := _7
// _9 := _7
// _10 := _7
// kn := addmod(kn, sub(_1, k), _1)
// let x := mod(mload(0), _1)
// k := mulmod(k, x, _1)
// a := mulmod(a, x, _1)
// c := mulmod(c, x, _1)
// mstore(0, keccak256(0, 0x20))
// }
// 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))
// case 0 {
// _7 := 0
// _8 := _7
// _9 := _7
// _10 := _7
// kn := addmod(kn, k, _1)
// }
// calldatacopy(0xe0, add(_2, 164), 0x40)
// calldatacopy(0x20, add(_2, 100), 0x40)
// mstore(0x120, sub(_1, c))
// mstore(0x60, k)
// mstore(0xc0, a)
// 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)
// let result := call(gas(), 7, 0, 0xe0, 0x60, 0x1a0, 0x40)
// let result_1 := and(result, call(gas(), 7, 0, 0x20, 0x60, 0x120, 0x40))
// let result_2 := and(result_1, call(gas(), 7, 0, 0x80, 0x60, 0x160, 0x40))
// let result_3 := and(result_2, call(gas(), 6, 0, 0x120, 0x80, 0x160, 0x40))
// result := and(result_3, call(gas(), 6, 0, 0x160, 0x80, b, 0x40))
// if eq(i, calldataload(0x24))
// {
// mstore(0x260, mload(0x20))
// mstore(0x280, mload(_5))
// mstore(0x280, mload(0x40))
// mstore(0x1e0, mload(0xe0))
// mstore(0x200, sub(0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47, mload(0x100)))
// }
// if gt(i, m)
// if gt(i, calldataload(0x24))
// {
// mstore(0x60, c)
// 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))
// let result_4 := and(result, call(gas(), 7, 0, 0x20, 0x60, 0x220, 0x40))
// let result_5 := and(result_4, call(gas(), 6, 0, 0x220, 0x80, 0x260, 0x40))
// result := and(result_5, call(gas(), 6, 0, 0x1a0, 0x80, 0x1e0, 0x40))
// }
// if iszero(result)
// {
// mstore(0, 400)
// revert(0, 0x20)
// }
// b := add(b, _5)
// b := add(b, 0x40)
// }
// if lt(m, n) { validatePairing(0x64) }
// if iszero(eq(mod(keccak256(0x2a0, add(b, not(671))), _2), challenge))
// if lt(calldataload(0x24), n) { validatePairing(0x64) }
// if iszero(eq(mod(keccak256(0x2a0, add(b, not(671))), _1), mod(calldataload(0x44), _1)))
// {
// mstore(0, 404)
// revert(0, 0x20)

View File

@ -0,0 +1,28 @@
{
let y := mload(0x20)
for {} and(y, 8) { if y { revert(0, 0) } } {
if y { continue }
sstore(1, y)
}
if y { revert(0, 0) }
}
// ====
// step: fullSuite
// ----
// {
// {
// let y := mload(0x20)
// for { }
// and(y, 8)
// {
// if y { revert(0, 0) }
// y := 0
// }
// {
// if y { continue }
// y := 0
// sstore(1, y)
// }
// if y { revert(0, 0) }
// }
// }

View File

@ -0,0 +1,31 @@
{
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)))
}
}
// ====
// step: fullSuite
// ----
// {
// {
// let _1 := calldataload(0)
// let sum := 0
// let i := sum
// for { } lt(i, calldataload(_1)) { i := add(i, 1) }
// {
// sum := add(sum, calldataload(add(add(_1, mul(i, 0x20)), 0x20)))
// }
// sstore(0, sum)
// }
// }

View File

@ -21,10 +21,50 @@
// ----
// {
// {
// let _1 := mload(0x40)
// mstore(0x40, add(_1, 0x20))
// mstore(0x40, add(_1, 96))
// mstore(add(_1, 128), 2)
// let p := 0
// let p_1 := p
// let p_2 := p
// let p_3 := p
// let p_4 := p
// let p_5 := p
// let p_6 := p
// let p_7 := p
// let p_8 := p
// let p_9 := p
// let p_10 := p
// let p_11 := mload(0x40)
// mstore(0x40, add(p_11, 0x20))
// mstore(0x40, add(p_11, 96))
// mstore(add(p_11, 128), 2)
// let _1 := 1
// mstore(0x40, 0x20)
// for { } iszero(p) { }
// {
// p := 0
// if p_1 { break }
// p_1 := p
// if p_2 { break }
// p_2 := p
// if p_3 { break }
// p_3 := p
// if p_4 { break }
// p_4 := p
// if p_5 { break }
// p_5 := p
// if p_6 { break }
// p_6 := p
// if p_7 { break }
// p_7 := p
// if p_8 { break }
// p_8 := p
// if p_9 { break }
// p_9 := p
// if p_10 { break }
// p_10 := p
// if iszero(_1) { break }
// if _1 { break }
// _1 := p
// mstore(128, 0x40)
// }
// }
// }

View File

@ -34,6 +34,7 @@
#include <libyul/optimiser/Disambiguator.h>
#include <libyul/optimiser/CallGraphGenerator.h>
#include <libyul/optimiser/CommonSubexpressionEliminator.h>
#include <libyul/optimiser/ConditionalSimplifier.h>
#include <libyul/optimiser/ControlFlowSimplifier.h>
#include <libyul/optimiser/NameCollector.h>
#include <libyul/optimiser/EquivalentFunctionCombiner.h>
@ -139,7 +140,8 @@ public:
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/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 << " co(n)trol flow simplifier/stack com(p)ressor/(D)ead code eliminator/(L)oad resolver/ " << endl;
cout << " (C)onditional simplifier?" << endl;
cout.flush();
int option = readStandardInputChar();
cout << ' ' << char(option) << endl;
@ -164,6 +166,9 @@ public:
case 'c':
CommonSubexpressionEliminator::run(context, *m_ast);
break;
case 'C':
ConditionalSimplifier::run(context, *m_ast);
break;
case 'd':
VarDeclInitializer::run(context, *m_ast);
break;