-
Notifications
You must be signed in to change notification settings - Fork 6.1k
[Yul] Add structural simplifier. #5584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| /* | ||
| 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/StructuralSimplifier.h> | ||
| #include <libyul/optimiser/Semantics.h> | ||
| #include <libyul/optimiser/Utilities.h> | ||
| #include <libyul/AsmData.h> | ||
| #include <libdevcore/CommonData.h> | ||
| #include <libdevcore/Visitor.h> | ||
|
|
||
| using namespace std; | ||
| using namespace dev; | ||
| using namespace yul; | ||
|
|
||
| namespace { | ||
|
|
||
| ExpressionStatement makePopExpressionStatement(langutil::SourceLocation const& _location, Expression&& _expression) | ||
| { | ||
| return {_location, FunctionalInstruction{ | ||
| _location, | ||
| solidity::Instruction::POP, | ||
| {std::move(_expression)} | ||
| }}; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| void StructuralSimplifier::operator()(Block& _block) | ||
| { | ||
| pushScope(false); | ||
| simplify(_block.statements); | ||
| popScope(); | ||
| } | ||
|
|
||
| void StructuralSimplifier::simplify(std::vector<yul::Statement>& _statements) | ||
| { | ||
| using OptionalStatements = boost::optional<vector<Statement>>; | ||
| GenericFallbackReturnsVisitor<OptionalStatements, If, Switch, ForLoop> const visitor( | ||
| [&](If& _ifStmt) -> OptionalStatements { | ||
| if (_ifStmt.body.statements.empty()) | ||
| return {{makePopExpressionStatement(_ifStmt.location, std::move(*_ifStmt.condition))}}; | ||
| if (expressionAlwaysTrue(*_ifStmt.condition)) | ||
| return {std::move(_ifStmt.body.statements)}; | ||
| else if (expressionAlwaysFalse(*_ifStmt.condition)) | ||
| return {{}}; | ||
| return {}; | ||
| }, | ||
| [](Switch& _switchStmt) -> OptionalStatements { | ||
| if (_switchStmt.cases.size() == 1) | ||
| { | ||
| auto& switchCase = _switchStmt.cases.front(); | ||
| auto loc = locationOf(*_switchStmt.expression); | ||
| if (switchCase.value) | ||
| return {{If{ | ||
| std::move(_switchStmt.location), | ||
| make_shared<Expression>(FunctionalInstruction{ | ||
| std::move(loc), | ||
| solidity::Instruction::EQ, | ||
| {std::move(*switchCase.value), std::move(*_switchStmt.expression)} | ||
| }), std::move(switchCase.body)}}}; | ||
| else | ||
| return {{ | ||
| makePopExpressionStatement(loc, std::move(*_switchStmt.expression)), | ||
| std::move(switchCase.body) | ||
| }}; | ||
| } | ||
| else | ||
| return {}; | ||
| }, | ||
| [&](ForLoop& _forLoop) -> OptionalStatements { | ||
| if (expressionAlwaysFalse(*_forLoop.condition)) | ||
| return {std::move(_forLoop.pre.statements)}; | ||
| else | ||
| return {}; | ||
| } | ||
| ); | ||
|
|
||
| iterateReplacing( | ||
| _statements, | ||
| [&](Statement& _stmt) -> OptionalStatements | ||
| { | ||
| visit(_stmt); | ||
| OptionalStatements result = boost::apply_visitor(visitor, _stmt); | ||
| if (result) | ||
| simplify(*result); | ||
| return result; | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| bool StructuralSimplifier::expressionAlwaysTrue(Expression const& _expression) | ||
| { | ||
| return boost::apply_visitor(GenericFallbackReturnsVisitor<bool, Identifier const, Literal const>( | ||
| [&](Identifier const& _identifier) -> bool { | ||
| if (auto expr = m_value[_identifier.name]) | ||
| return expressionAlwaysTrue(*expr); | ||
| return false; | ||
| }, | ||
| [](Literal const& _literal) -> bool { | ||
| static YulString const trueString("true"); | ||
| return | ||
| (_literal.kind == LiteralKind::Boolean && _literal.value == trueString) || | ||
| (_literal.kind == LiteralKind::Number && valueOfNumberLiteral(_literal) != u256(0)) | ||
| ; | ||
| } | ||
| ), _expression); | ||
| } | ||
|
|
||
| bool StructuralSimplifier::expressionAlwaysFalse(Expression const& _expression) | ||
| { | ||
| return boost::apply_visitor(GenericFallbackReturnsVisitor<bool, Identifier const, Literal const>( | ||
| [&](Identifier const& _identifier) -> bool { | ||
| if (auto expr = m_value[_identifier.name]) | ||
| return expressionAlwaysFalse(*expr); | ||
| return false; | ||
| }, | ||
| [](Literal const& _literal) -> bool { | ||
| static YulString const falseString("false"); | ||
| return | ||
| (_literal.kind == LiteralKind::Boolean && _literal.value == falseString) || | ||
| (_literal.kind == LiteralKind::Number && valueOfNumberLiteral(_literal) == u256(0)) | ||
| ; | ||
| } | ||
| ), _expression); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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/>. | ||
| */ | ||
| #pragma once | ||
|
|
||
| #include <libyul/optimiser/ASTWalker.h> | ||
| #include <libyul/optimiser/DataFlowAnalyzer.h> | ||
|
|
||
| namespace yul | ||
| { | ||
|
|
||
| /** | ||
| * Structural simplifier. Performs the following simplification steps: | ||
| * - replace if with empty body with pop(condition) | ||
| * - replace if with true condition with its body | ||
| * - remove if with false condition | ||
| * - turn switch with single case into if | ||
| * - replace switch with only default case with pop(expression) and body | ||
| * - remove for with false condition | ||
| * | ||
| * Prerequisites: Disambiguator | ||
| * | ||
| * Important: Can only be used on EVM code. | ||
| */ | ||
| class StructuralSimplifier: public DataFlowAnalyzer | ||
| { | ||
| public: | ||
| using DataFlowAnalyzer::operator(); | ||
| void operator()(Block& _block) override; | ||
| private: | ||
| void simplify(std::vector<Statement>& _statements); | ||
| bool expressionAlwaysTrue(Expression const &_expression); | ||
| bool expressionAlwaysFalse(Expression const &_expression); | ||
| }; | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -599,10 +599,6 @@ | |
| // revert(_2, _2) | ||
| // } | ||
| // let abi_decode_abi_decode_length_14_1069 := 0x2 | ||
| // if _2 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
| // { | ||
| // revert(_2, _2) | ||
| // } | ||
| // let allocateMe_memPtr_315 := mload(abi_encode_pos_590) | ||
| // let allocateMe_newFreePtr := add(allocateMe_memPtr_315, abi_encode_pos_590) | ||
| // if or(gt(allocateMe_newFreePtr, _945), lt(allocateMe_newFreePtr, allocateMe_memPtr_315)) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
test/libyul/yulOptimizerTests/structuralSimplifier/empty_if_movable_condition.yul
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { let a := mload(0) if a {} } | ||
| // ---- | ||
| // structuralSimplifier | ||
| // { | ||
| // let a := mload(0) | ||
| // pop(a) | ||
| // } |
6 changes: 6 additions & 0 deletions
6
test/libyul/yulOptimizerTests/structuralSimplifier/empty_if_non_movable_condition.yul
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { if mload(0) {} } | ||
| // ---- | ||
| // structuralSimplifier | ||
| // { | ||
| // pop(mload(0)) | ||
| // } |
10 changes: 10 additions & 0 deletions
10
test/libyul/yulOptimizerTests/structuralSimplifier/for_false_condition.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| for { let a := 42 } 0 { a := a } { | ||
| let b := a | ||
| } | ||
| } | ||
| // ---- | ||
| // structuralSimplifier | ||
| // { | ||
| // let a := 42 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check |
||
| // } | ||
5 changes: 5 additions & 0 deletions
5
test/libyul/yulOptimizerTests/structuralSimplifier/if_false_condition.yul
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { if 0 { mstore(0, 0) } } | ||
| // ---- | ||
| // structuralSimplifier | ||
| // { | ||
| // } |
6 changes: 6 additions & 0 deletions
6
test/libyul/yulOptimizerTests/structuralSimplifier/if_true_condition.yul
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { if 1 { mstore(0, 0) } } | ||
| // ---- | ||
| // structuralSimplifier | ||
| // { | ||
| // mstore(0, 0) | ||
| // } |
6 changes: 6 additions & 0 deletions
6
test/libyul/yulOptimizerTests/structuralSimplifier/nested.yul
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { if 1 { if 1 { for { mstore(0, 0) } 0 {} { mstore(2, 3) } if 0 { mstore(1, 2) } } } } | ||
| // ---- | ||
| // structuralSimplifier | ||
| // { | ||
| // mstore(0, 0) | ||
| // } |
11 changes: 11 additions & 0 deletions
11
test/libyul/yulOptimizerTests/structuralSimplifier/switch_only_default.yul
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| switch mload(0) default { mstore(1, 2) } | ||
| } | ||
| // ---- | ||
| // structuralSimplifier | ||
| // { | ||
| // pop(mload(0)) | ||
| // { | ||
| // mstore(1, 2) | ||
| // } | ||
| // } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.