Remove side-effect-free statements.

This commit is contained in:
chriseth 2018-10-28 12:58:21 +01:00
parent 20481055e3
commit fa44d20721
5 changed files with 23 additions and 7 deletions

View File

@ -135,6 +135,8 @@ If there are two assignments to a variable where the first one is a movable expr
and the variable is not used between the two assignments (and the second is not inside
a loop or conditional, the first one is not inside), the first assignment is removed.
This step also removes movable expression statements.
## Function Unifier

View File

@ -85,6 +85,16 @@ void UnusedPruner::operator()(Block& _block)
}};
}
}
else if (statement.type() == typeid(ExpressionStatement))
{
ExpressionStatement& exprStmt = boost::get<ExpressionStatement>(statement);
if (MovableChecker(exprStmt.expression).movable())
{
// pop(x) should be movable!
subtractReferences(ReferencesCounter::countReferences(exprStmt.expression));
statement = Block{std::move(exprStmt.location), {}};
}
}
removeEmptyBlocks(_block);

View File

@ -32,10 +32,8 @@ namespace yul
{
/**
* Optimisation stage that removes unused variables and functions.
*
* TODO: Also remove intermediate variable assignments from movable expressions
* which are not referenced until after the next assignment to the same variable.
* Optimisation stage that removes unused variables and functions and also
* removes movable expression statements.
*
* Note that this does not remove circular references.
*

View File

@ -16,9 +16,7 @@
// {
// let _18 := 0x20
// let allocate__7 := 0x40
// let allocate_p_12 := mload(allocate__7)
// mstore(allocate__7, add(allocate_p_12, _18))
// pop(allocate_p_12)
// mstore(allocate__7, add(mload(allocate__7), _18))
// let allocate_p_12_31 := mload(allocate__7)
// mstore(allocate__7, add(allocate_p_12_31, allocate__7))
// mstore(add(allocate_p_12_31, 96), 2)

View File

@ -0,0 +1,8 @@
{
let a := 1
pop(a)
}
// ----
// unusedPruner
// {
// }