Skip to content

Commit e996fe6

Browse files
committed
Yul Optimizer: Simplify start offset of zero-length operations.
1 parent fbef272 commit e996fe6

File tree

7 files changed

+56
-4
lines changed

7 files changed

+56
-4
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/libyul/yulOptimizerTests/expressionSimplifier/side_effects_in_for_condition.yul

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
for {} div(create(0, 1, 0), shl(msize(), 1)) {}
2+
for {} div(create(0, 1, 1), shl(msize(), 1)) {}
33
{
44
}
55
}
@@ -10,7 +10,7 @@
1010
//
1111
// {
1212
// {
13-
// for { } div(create(0, 1, 0), shl(msize(), 1)) { }
13+
// for { } div(create(0, 1, 1), shl(msize(), 1)) { }
1414
// { }
1515
// }
1616
// }
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
revert(calldataload(0), 0)
3+
revert(call(0,0,0,0,0,0,0), 0)
4+
calldatacopy(calldataload(1), calldataload(2), 0)
5+
return(calldataload(3), 0)
6+
codecopy(calldataload(4), calldataload(5), sub(42,42))
7+
}
8+
// ----
9+
// step: expressionSimplifier
10+
//
11+
// {
12+
// {
13+
// let _1 := 0
14+
// revert(0, _1)
15+
// pop(call(_1, _1, _1, _1, _1, _1, _1))
16+
// revert(0, _1)
17+
// calldatacopy(0, calldataload(2), _1)
18+
// return(0, _1)
19+
// codecopy(0, calldataload(5), 0)
20+
// }
21+
// }

0 commit comments

Comments
 (0)