Merge pull request #6410 from ethereum/eliminate-dead-code

Yul Optimizer: Remove dead code
This commit is contained in:
chriseth 2019-04-01 18:12:47 +02:00 committed by GitHub
commit e894e0b967
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 482 additions and 3 deletions

View File

@ -8,6 +8,7 @@ Compiler Features:
* Optimizer: Add rule for shifts by constants larger than 255 for Constantinople.
* Optimizer: Add rule to simplify certain ANDs and SHL combinations
* Yul: Adds break and continue keywords to for-loop syntax.
* Yul Optimizer: Adds steps for detecting and removing of dead code.
Bugfixes:

View File

@ -132,6 +132,22 @@ bool SemanticInformation::altersControlFlow(AssemblyItem const& _item)
}
}
bool SemanticInformation::terminatesControlFlow(AssemblyItem const& _item)
{
if (_item.type() != Operation)
return false;
switch (_item.instruction())
{
case Instruction::RETURN:
case Instruction::SELFDESTRUCT:
case Instruction::STOP:
case Instruction::INVALID:
case Instruction::REVERT:
return true;
default:
return false;
}
}
bool SemanticInformation::isDeterministic(AssemblyItem const& _item)
{

View File

@ -47,6 +47,7 @@ struct SemanticInformation
static bool isSwapInstruction(AssemblyItem const& _item);
static bool isJumpInstruction(AssemblyItem const& _item);
static bool altersControlFlow(AssemblyItem const& _item);
static bool terminatesControlFlow(AssemblyItem const& _item);
/// @returns false if the value put on the stack by _item depends on anything else than
/// the information in the current block header, memory, storage or stack.
static bool isDeterministic(AssemblyItem const& _item);

View File

@ -48,6 +48,8 @@ add_library(yul
optimiser/CommonSubexpressionEliminator.h
optimiser/DataFlowAnalyzer.cpp
optimiser/DataFlowAnalyzer.h
optimiser/DeadCodeEliminator.cpp
optimiser/DeadCodeEliminator.h
optimiser/Disambiguator.cpp
optimiser/Disambiguator.h
optimiser/EquivalentFunctionDetector.cpp

View File

@ -0,0 +1,94 @@
/*
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/>.
*/
/**
* Optimisation stage that removes unreachable code.
*/
#include <libyul/optimiser/DeadCodeEliminator.h>
#include <libyul/AsmData.h>
#include <libevmasm/SemanticInformation.h>
#include <libevmasm/AssemblyItem.h>
#include <algorithm>
using namespace std;
using namespace dev;
using namespace yul;
namespace
{
bool isTerminating(yul::ExpressionStatement const& _exprStmnt)
{
if (_exprStmnt.expression.type() != typeid(FunctionalInstruction))
return false;
auto const& funcInstr = boost::get<FunctionalInstruction>(_exprStmnt.expression);
return eth::SemanticInformation::terminatesControlFlow(funcInstr.instruction);
}
/// Returns an iterator to the first terminating statement or
/// `_block.statements.end()()` when none was found
auto findFirstTerminatingStatement(Block& _block)
{
return find_if(
_block.statements.begin(),
_block.statements.end(),
[](Statement const& _stmnt)
{
if (
_stmnt.type() == typeid(ExpressionStatement) &&
isTerminating(boost::get<ExpressionStatement>(_stmnt))
)
return true;
else if (_stmnt.type() == typeid(Break))
return true;
else if (_stmnt.type() == typeid(Continue))
return true;
return false;
}
);
}
}
void DeadCodeEliminator::operator()(Block& _block)
{
auto& statements = _block.statements;
auto firstTerminatingStatment = findFirstTerminatingStatement(_block);
if (
firstTerminatingStatment != statements.end() &&
firstTerminatingStatment + 1 != statements.end()
)
statements.erase(
std::remove_if(
firstTerminatingStatment + 1,
statements.end(),
[] (Statement const& _s)
{
return _s.type() != typeid(yul::FunctionDefinition);
}
),
statements.end()
);
ASTModifier::operator()(_block);
}

View File

@ -0,0 +1,49 @@
/*
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/>.
*/
/**
* Optimisation stage that removes unused variables and functions.
*/
#pragma once
#include <libyul/optimiser/ASTWalker.h>
#include <libyul/YulString.h>
#include <map>
#include <set>
namespace yul
{
/**
* Optimisation stage that removes unreachable code
*
* Unreachable code is any code within a block which is preceded by a
* return, invalid, break, continue, selfdestruct or revert.
*
* Function definitions are retained as they might be called by earlier
* code and thus are considered reachable.
*
*/
class DeadCodeEliminator: public ASTModifier
{
public:
using ASTModifier::operator();
void operator()(Block& _block) override;
};
}

View File

@ -23,6 +23,7 @@
#include <libyul/optimiser/Disambiguator.h>
#include <libyul/optimiser/VarDeclInitializer.h>
#include <libyul/optimiser/BlockFlattener.h>
#include <libyul/optimiser/DeadCodeEliminator.h>
#include <libyul/optimiser/FunctionGrouper.h>
#include <libyul/optimiser/FunctionHoister.h>
#include <libyul/optimiser/EquivalentFunctionCombiner.h>
@ -70,6 +71,7 @@ void OptimiserSuite::run(
VarDeclInitializer{}(ast);
FunctionHoister{}(ast);
BlockFlattener{}(ast);
DeadCodeEliminator{}(ast);
FunctionGrouper{}(ast);
EquivalentFunctionCombiner::run(ast);
UnusedPruner::runUntilStabilised(*_dialect, ast, reservedIdentifiers);
@ -107,6 +109,7 @@ void OptimiserSuite::run(
// still in SSA, perform structural simplification
StructuralSimplifier{*_dialect}(ast);
BlockFlattener{}(ast);
DeadCodeEliminator{}(ast);
UnusedPruner::runUntilStabilised(*_dialect, ast, reservedIdentifiers);
}
{
@ -158,6 +161,7 @@ void OptimiserSuite::run(
ExpressionSimplifier::run(*_dialect, ast);
StructuralSimplifier{*_dialect}(ast);
BlockFlattener{}(ast);
DeadCodeEliminator{}(ast);
CommonSubexpressionEliminator{*_dialect}(ast);
SSATransform::run(ast, dispenser);
RedundantAssignEliminator::run(*_dialect, ast);
@ -192,6 +196,7 @@ void OptimiserSuite::run(
// message once we perform code generation.
StackCompressor::run(_dialect, ast, _optimizeStackAllocation, stackCompressorMaxIterations);
BlockFlattener{}(ast);
DeadCodeEliminator{}(ast);
VarNameCleaner{ast, *_dialect, reservedIdentifiers}(ast);
yul::AsmAnalyzer::analyzeStrictAssertCorrect(_dialect, ast);

View File

@ -22,6 +22,7 @@
#include <libyul/optimiser/BlockFlattener.h>
#include <libyul/optimiser/VarDeclInitializer.h>
#include <libyul/optimiser/VarNameCleaner.h>
#include <libyul/optimiser/DeadCodeEliminator.h>
#include <libyul/optimiser/Disambiguator.h>
#include <libyul/optimiser/CommonSubexpressionEliminator.h>
#include <libyul/optimiser/NameCollector.h>
@ -190,6 +191,7 @@ bool YulOptimizerTest::run(ostream& _stream, string const& _linePrefix, bool con
CommonSubexpressionEliminator{*m_dialect}(*m_ast);
ExpressionSimplifier::run(*m_dialect, *m_ast);
UnusedPruner::runUntilStabilised(*m_dialect, *m_ast);
DeadCodeEliminator{}(*m_ast);
ExpressionJoiner::run(*m_ast);
ExpressionJoiner::run(*m_ast);
}
@ -198,6 +200,11 @@ bool YulOptimizerTest::run(ostream& _stream, string const& _linePrefix, bool con
disambiguate();
UnusedPruner::runUntilStabilised(*m_dialect, *m_ast);
}
else if (m_optimizerStep == "deadCodeEliminator")
{
disambiguate();
DeadCodeEliminator{}(*m_ast);
}
else if (m_optimizerStep == "ssaTransform")
{
disambiguate();

View File

@ -0,0 +1,33 @@
{
for {
let a := 20
}
lt(a, 40)
{
a := add(a, 2)
}
{
a := a
if lt(a, 0)
{ break }
}
}
// ----
// deadCodeEliminator
// {
// for {
// let a := 20
// }
// lt(a, 40)
// {
// a := add(a, 2)
// }
// {
// a := a
// if lt(a, 0)
// {
// break
// }
// }
// }

View File

@ -0,0 +1,31 @@
{
for {
let a := 20
}
lt(a, 40)
{
a := add(a, 2)
}
{
a := a
break
mstore(0, a)
a := add(a, 10)
}
}
// ----
// deadCodeEliminator
// {
// for {
// let a := 20
// }
// lt(a, 40)
// {
// a := add(a, 2)
// }
// {
// a := a
// break
// }
// }

View File

@ -0,0 +1,31 @@
{
for {
let a := 20
}
lt(a, 40)
{
a := add(a, 2)
}
{
a := a
continue
mstore(0, a)
a := add(a, 10)
}
}
// ----
// deadCodeEliminator
// {
// for {
// let a := 20
// }
// lt(a, 40)
// {
// a := add(a, 2)
// }
// {
// a := a
// continue
// }
// }

View File

@ -0,0 +1,23 @@
{
let b := 20
revert(0, 0)
for {
let a := 20
}
lt(a, 40)
{
a := add(a, 2)
}
{
a := a
mstore(0, a)
a := add(a, 10)
}
}
// ----
// deadCodeEliminator
// {
// let b := 20
// revert(0, 0)
// }

View File

@ -0,0 +1,23 @@
{
let b := 20
stop()
for {
let a := 20
}
lt(a, 40)
{
a := add(a, 2)
}
{
a := a
mstore(0, a)
a := add(a, 10)
}
}
// ----
// deadCodeEliminator
// {
// let b := 20
// stop()
// }

View File

@ -0,0 +1,24 @@
{
fun()
revert(0, 0)
function fun()
{
return(1, 1)
pop(sub(10, 5))
}
pop(add(1, 1))
}
// ----
// deadCodeEliminator
// {
// fun()
// revert(0, 0)
// function fun()
// {
// return(1, 1)
// }
// }

View File

@ -0,0 +1,30 @@
{
let y := mload(0)
switch y
case 0 {
y := 8 }
case 1 {
y := 9
revert(0, 0)
y := 10
}
default {
y := 10 }
}
// ----
// deadCodeEliminator
// {
// let y := mload(0)
// switch y
// case 0 {
// y := 8
// }
// case 1 {
// y := 9
// revert(0, 0)
// }
// default {
// y := 10
// }
// }

View File

@ -0,0 +1,14 @@
{
{
revert(0, 0)
}
mstore(0, 0)
}
// ----
// deadCodeEliminator
// {
// {
// revert(0, 0)
// }
// mstore(0, 0)
// }

View File

@ -0,0 +1,29 @@
{
for {
let a := 20
}
lt(a, 40)
{
a := add(a, 2)
}
{
a := a
break
}
}
// ----
// deadCodeEliminator
// {
// for {
// let a := 20
// }
// lt(a, 40)
// {
// a := add(a, 2)
// }
// {
// a := a
// break
// }
// }

View File

@ -0,0 +1,29 @@
{
for {
let a := 20
}
lt(a, 40)
{
a := add(a, 2)
}
{
a := a
continue
}
}
// ----
// deadCodeEliminator
// {
// for {
// let a := 20
// }
// lt(a, 40)
// {
// a := add(a, 2)
// }
// {
// a := a
// continue
// }
// }

View File

@ -0,0 +1,35 @@
{
let b := 20
for {
let a := 20
}
lt(a, 40)
{
a := add(a, 2)
}
{
a := a
mstore(0, a)
a := add(a, 10)
}
stop()
}
// ----
// deadCodeEliminator
// {
// let b := 20
// for {
// let a := 20
// }
// lt(a, 40)
// {
// a := add(a, 2)
// }
// {
// a := a
// mstore(0, a)
// a := add(a, 10)
// }
// stop()
// }

View File

@ -332,8 +332,6 @@
// }
// mstore(i_1, 0x01)
// return(i_1, 0x20)
// mstore(i_1, 404)
// revert(i_1, 0x20)
// function validatePairing(t2)
// {
// let t2_x := calldataload(t2)

View File

@ -44,6 +44,7 @@
#include <libyul/optimiser/Rematerialiser.h>
#include <libyul/optimiser/ExpressionSimplifier.h>
#include <libyul/optimiser/UnusedPruner.h>
#include <libyul/optimiser/DeadCodeEliminator.h>
#include <libyul/optimiser/ExpressionJoiner.h>
#include <libyul/optimiser/RedundantAssignEliminator.h>
#include <libyul/optimiser/SSAReverser.h>
@ -132,7 +133,7 @@ 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-pre-rewriter/" << endl;
cout << " s(t)ructural simplifier/equi(v)alent function combiner/ssa re(V)erser/? " << endl;
cout << " stack com(p)ressor? " << endl;
cout << " stack com(p)ressor/(D)ead code eliminator/? " << endl;
cout.flush();
int option = readStandardInputChar();
cout << ' ' << char(option) << endl;
@ -182,6 +183,9 @@ public:
case 'u':
UnusedPruner::runUntilStabilised(*m_dialect, *m_ast);
break;
case 'D':
DeadCodeEliminator{}(*m_ast);
break;
case 'a':
SSATransform::run(*m_ast, *m_nameDispenser);
break;