mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Out of body again.
This commit is contained in:
@@ -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());
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user