Skip to content

Commit a3de6cd

Browse files
authored
Merge pull request #12762 from ethereum/emptyReturnRevert
Optimize ``return(x,0) -> pop(x) return(0,0)`` (and also for ``revert``).
2 parents f01a09f + a9c2186 commit a3de6cd

File tree

47 files changed

+115
-59
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+115
-59
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Language Features:
88

99
Compiler Features:
1010
* Code Generator: More efficient overflow checks for multiplication.
11+
* Yul Optimizer: Simplify the starting offset of zero-length operations to zero.
1112

1213

1314
Bugfixes:

libyul/optimiser/DataFlowAnalyzer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ bool DataFlowAnalyzer::inScope(YulString _variableName) const
411411
return false;
412412
}
413413

414-
optional<u256> DataFlowAnalyzer::valueOfIdentifier(YulString const& _name)
414+
optional<u256> DataFlowAnalyzer::valueOfIdentifier(YulString const& _name) const
415415
{
416416
if (AssignedValue const* value = variableValue(_name))
417417
if (Literal const* literal = get_if<Literal>(value->value))

libyul/optimiser/DataFlowAnalyzer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class DataFlowAnalyzer: public ASTModifier
148148
bool inScope(YulString _variableName) const;
149149

150150
/// Returns the literal value of the identifier, if it exists.
151-
std::optional<u256> valueOfIdentifier(YulString const& _name);
151+
std::optional<u256> valueOfIdentifier(YulString const& _name) const;
152152

153153
enum class StoreLoadLocation {
154154
Memory = 0,

libyul/optimiser/ExpressionSimplifier.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323

2424
#include <libyul/optimiser/SimplificationRules.h>
2525
#include <libyul/optimiser/OptimiserStep.h>
26+
#include <libyul/optimiser/OptimizerUtilities.h>
2627
#include <libyul/AST.h>
28+
#include <libyul/Utilities.h>
29+
30+
#include <libevmasm/SemanticInformation.h>
2731

2832
using namespace std;
2933
using namespace solidity;
@@ -44,4 +48,29 @@ void ExpressionSimplifier::visit(Expression& _expression)
4448
[this](YulString _var) { return variableValue(_var); }
4549
))
4650
_expression = match->action().toExpression(debugDataOf(_expression));
51+
52+
if (auto* functionCall = get_if<FunctionCall>(&_expression))
53+
if (optional<evmasm::Instruction> instruction = toEVMInstruction(m_dialect, functionCall->functionName.name))
54+
for (auto op: evmasm::SemanticInformation::readWriteOperations(*instruction))
55+
if (op.startParameter && op.lengthParameter)
56+
{
57+
Expression& startArgument = functionCall->arguments.at(*op.startParameter);
58+
Expression const& lengthArgument = functionCall->arguments.at(*op.lengthParameter);
59+
if (
60+
knownToBeZero(lengthArgument) &&
61+
!knownToBeZero(startArgument) &&
62+
!holds_alternative<FunctionCall>(startArgument)
63+
)
64+
startArgument = Literal{debugDataOf(startArgument), LiteralKind::Number, "0"_yulstring, {}};
65+
}
66+
}
67+
68+
bool ExpressionSimplifier::knownToBeZero(Expression const& _expression) const
69+
{
70+
if (auto const* literal = get_if<Literal>(&_expression))
71+
return valueOfLiteral(*literal) == 0;
72+
else if (auto const* identifier = get_if<Identifier>(&_expression))
73+
return valueOfIdentifier(identifier->name) == 0;
74+
else
75+
return false;
4776
}

libyul/optimiser/ExpressionSimplifier.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class ExpressionSimplifier: public DataFlowAnalyzer
5454
explicit ExpressionSimplifier(Dialect const& _dialect):
5555
DataFlowAnalyzer(_dialect, MemoryAndStorage::Ignore)
5656
{}
57+
bool knownToBeZero(Expression const& _expression) const;
5758
};
5859

5960
}

test/cmdlineTests/debug_info_in_yul_and_evm_asm_print_all/output

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ object "C_6" {
194194
{
195195
if callvalue() { revert(_1, _1) }
196196
if slt(add(calldatasize(), not(3)), _1) { revert(_1, _1) }
197-
return(memoryguard(0x80), _1)
197+
return(_1, _1)
198198
}
199199
}
200200
revert(0, 0)

test/cmdlineTests/debug_info_in_yul_and_evm_asm_print_location_only/output

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ object "C_6" {
193193
{
194194
if callvalue() { revert(_1, _1) }
195195
if slt(add(calldatasize(), not(3)), _1) { revert(_1, _1) }
196-
return(memoryguard(0x80), _1)
196+
return(_1, _1)
197197
}
198198
}
199199
revert(0, 0)

test/cmdlineTests/debug_info_in_yul_and_evm_asm_print_none/output

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ object "C_6" {
182182
{
183183
if callvalue() { revert(_1, _1) }
184184
if slt(add(calldatasize(), not(3)), _1) { revert(_1, _1) }
185-
return(memoryguard(0x80), _1)
185+
return(_1, _1)
186186
}
187187
}
188188
revert(0, 0)

test/cmdlineTests/ir_compiler_subobjects/output

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ object "D_16" {
7171
returndatacopy(pos, _2, returndatasize())
7272
revert(pos, returndatasize())
7373
}
74-
return(mload(64), _2)
74+
return(_2, _2)
7575
}
7676
}
7777
revert(0, 0)

test/cmdlineTests/ir_with_assembly_no_memoryguard_creation/output

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ object "D_12" {
2222
{
2323
if callvalue() { revert(_1, _1) }
2424
if slt(add(calldatasize(), not(3)), _1) { revert(_1, _1) }
25-
return(memoryguard(0x80), _1)
25+
return(_1, _1)
2626
}
2727
}
2828
revert(0, 0)

0 commit comments

Comments
 (0)