Merge pull request #7519 from ethereum/conditionalUnsimplifier

Conditional unsimplifier
This commit is contained in:
chriseth 2019-11-01 14:44:24 +01:00 committed by GitHub
commit 73954f16f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
32 changed files with 749 additions and 9 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,10 @@ add_library(yul
optimiser/CallGraphGenerator.h
optimiser/CommonSubexpressionEliminator.cpp
optimiser/CommonSubexpressionEliminator.h
optimiser/ConditionalSimplifier.cpp
optimiser/ConditionalSimplifier.h
optimiser/ConditionalUnsimplifier.cpp
optimiser/ConditionalUnsimplifier.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

@ -0,0 +1,98 @@
/*
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/ConditionalUnsimplifier.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 ConditionalUnsimplifier::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);
if (
!_case.body.statements.empty() &&
_case.body.statements.front().type() == typeid(Assignment)
)
{
Assignment const& assignment = boost::get<Assignment>(_case.body.statements.front());
if (
assignment.variableNames.size() == 1 &&
assignment.variableNames.front().name == expr &&
assignment.value->type() == typeid(Literal) &&
valueOfLiteral(boost::get<Literal>(*assignment.value)) == valueOfLiteral(*_case.value)
)
_case.body.statements.erase(_case.body.statements.begin());
}
}
(*this)(_case.body);
}
}
void ConditionalUnsimplifier::operator()(Block& _block)
{
walkVector(_block.statements);
iterateReplacingWindow<2>(
_block.statements,
[&](Statement& _stmt1, Statement& _stmt2) -> std::optional<vector<Statement>>
{
if (_stmt1.type() == typeid(If))
{
If& _if = boost::get<If>(_stmt1);
if (
_if.condition->type() == typeid(Identifier) &&
!_if.body.statements.empty()
)
{
YulString condition = boost::get<Identifier>(*_if.condition).name;
if (
_stmt2.type() == typeid(Assignment) &&
TerminationFinder(m_dialect).controlFlowKind(_if.body.statements.back()) !=
TerminationFinder::ControlFlow::FlowOut
)
{
Assignment const& assignment = boost::get<Assignment>(_stmt2);
if (
assignment.variableNames.size() == 1 &&
assignment.variableNames.front().name == condition &&
assignment.value->type() == typeid(Literal) &&
valueOfLiteral(boost::get<Literal>(*assignment.value)) == 0
)
return {make_vector<Statement>(std::move(_stmt1))};
}
}
}
return {};
}
);
}

View File

@ -0,0 +1,51 @@
/*
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
{
/**
* Reverse of conditional simplifier.
*
*/
class ConditionalUnsimplifier: public ASTModifier
{
public:
static constexpr char const* name{"ConditionalUnsimplifier"};
static void run(OptimiserStepContext& _context, Block& _ast)
{
ConditionalUnsimplifier{_context.dialect}(_ast);
}
using ASTModifier::operator();
void operator()(Switch& _switch) override;
void operator()(Block& _block) override;
private:
explicit ConditionalUnsimplifier(Dialect const& _dialect):
m_dialect(_dialect)
{}
Dialect const& m_dialect;
};
}

View File

@ -31,7 +31,11 @@ void ForLoopConditionIntoBody::run(OptimiserStepContext& _context, Block& _ast)
void ForLoopConditionIntoBody::operator()(ForLoop& _forLoop)
{
if (m_dialect.booleanNegationFunction() && _forLoop.condition->type() != typeid(Literal))
if (
m_dialect.booleanNegationFunction() &&
_forLoop.condition->type() != typeid(Literal) &&
_forLoop.condition->type() != typeid(Identifier)
)
{
langutil::SourceLocation loc = locationOf(*_forLoop.condition);
_forLoop.body.statements.insert(

View File

@ -25,6 +25,8 @@
#include <libyul/optimiser/BlockFlattener.h>
#include <libyul/optimiser/CallGraphGenerator.h>
#include <libyul/optimiser/ControlFlowSimplifier.h>
#include <libyul/optimiser/ConditionalSimplifier.h>
#include <libyul/optimiser/ConditionalUnsimplifier.h>
#include <libyul/optimiser/DeadCodeEliminator.h>
#include <libyul/optimiser/FunctionGrouper.h>
#include <libyul/optimiser/FunctionHoister.h>
@ -36,6 +38,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>
@ -98,6 +101,7 @@ void OptimiserSuite::run(
BlockFlattener::name,
ControlFlowSimplifier::name,
LiteralRematerialiser::name,
ConditionalUnsimplifier::name,
StructuralSimplifier::name,
ControlFlowSimplifier::name,
ForLoopConditionIntoBody::name,
@ -130,8 +134,13 @@ void OptimiserSuite::run(
}
{
// still in SSA, perform structural simplification
// perform structural simplification
suite.runSequence({
CommonSubexpressionEliminator::name,
ConditionalSimplifier::name,
LiteralRematerialiser::name,
ConditionalUnsimplifier::name,
StructuralSimplifier::name,
LiteralRematerialiser::name,
ForLoopConditionOutOfBody::name,
ControlFlowSimplifier::name,
@ -200,6 +209,10 @@ void OptimiserSuite::run(
{
// SSA plus simplify
suite.runSequence({
ConditionalSimplifier::name,
LiteralRematerialiser::name,
ConditionalUnsimplifier::name,
CommonSubexpressionEliminator::name,
SSATransform::name,
RedundantAssignEliminator::name,
RedundantAssignEliminator::name,
@ -315,6 +328,8 @@ map<string, unique_ptr<OptimiserStep>> const& OptimiserSuite::allSteps()
instance = optimiserStepCollection<
BlockFlattener,
CommonSubexpressionEliminator,
ConditionalSimplifier,
ConditionalUnsimplifier,
ControlFlowSimplifier,
DeadCodeEliminator,
EquivalentFunctionCombiner,

View File

@ -27,6 +27,8 @@
#include <libyul/optimiser/DeadCodeEliminator.h>
#include <libyul/optimiser/Disambiguator.h>
#include <libyul/optimiser/CallGraphGenerator.h>
#include <libyul/optimiser/ConditionalUnsimplifier.h>
#include <libyul/optimiser/ConditionalSimplifier.h>
#include <libyul/optimiser/CommonSubexpressionEliminator.h>
#include <libyul/optimiser/NameCollector.h>
#include <libyul/optimiser/EquivalentFunctionCombiner.h>
@ -157,6 +159,16 @@ TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _line
disambiguate();
CommonSubexpressionEliminator::run(*m_context, *m_ast);
}
else if (m_optimizerStep == "conditionalUnsimplifier")
{
disambiguate();
ConditionalUnsimplifier::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

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

View File

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

View File

@ -0,0 +1,22 @@
{
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
}
}
// ====
// step: conditionalUnsimplifier
// ----
// {
// let x := mload(0)
// let y := mload(0)
// if x { revert(0, 0) }
// if y { revert(0, 0) }
// for { } and(x, y) { }
// { x := 2 }
// }

View File

@ -0,0 +1,21 @@
{
let x
for {} x { sstore(1, x) } {
if x { continue }
x := 0
sstore(0, x)
}
sstore(0, x)
}
// ====
// step: conditionalUnsimplifier
// ----
// {
// let x
// for { } x { sstore(1, x) }
// {
// if x { continue }
// sstore(0, x)
// }
// sstore(0, x)
// }

View File

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

View File

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

View File

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

View File

@ -0,0 +1,22 @@
{
let x := calldataload(0)
switch x
case 0 { x := 0 }
case 1 { x := 1 }
case 2 { x := 8 /* wrong literal */ }
default { }
pop(x)
}
// ====
// step: conditionalUnsimplifier
// ----
// {
// let x := calldataload(0)
// switch x
// case 0 { }
// case 1 { }
// case 2 { x := 8 }
// default { }
// pop(x)
// }

View File

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

View File

@ -17,8 +17,8 @@
// { }
// for { } 0 { }
// { }
// for { } 1 { }
// { if iszero(a) { break } }
// for { } a { }
// { }
// for { } 1 { }
// {
// if iszero(add(a, a)) { break }

View File

@ -0,0 +1,22 @@
{
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) } }
// {
// if y { continue }
// sstore(1, 0)
// }
// 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,10 @@
// ----
// {
// {
// let _1 := mload(0x40)
// mstore(0x40, add(_1, 0x20))
// mstore(0x40, add(_1, 96))
// mstore(add(_1, 128), 2)
// let p := mload(0x40)
// mstore(0x40, add(p, 0x20))
// mstore(0x40, add(p, 96))
// mstore(add(p, 128), 2)
// mstore(0x40, 0x20)
// }
// }

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;