Yul Optimizer: Simplify start offset of zero-length operations.

This commit is contained in:
Daniel Kirchner 2022-04-06 18:33:42 +02:00 committed by Marenz
parent b6f11b3392
commit 6ce4ff262b
7 changed files with 57 additions and 4 deletions

View File

@ -8,9 +8,11 @@ Compiler Features:
* Yul IR Code Generation: Improved copy routines for arrays with packed storage layout.
* Yul Optimizer: Add rule to convert `mod(mul(X, Y), A)` into `mulmod(X, Y, A)`, if `A` is a power of two.
* Yul Optimizer: Add rule to convert `mod(add(X, Y), A)` into `addmod(X, Y, A)`, if `A` is a power of two.
* Yul Optimizer: Simplify the starting offset of zero-length operations to zero.
* Code Generator: More efficient code for checked addition and subtraction.
* Error Reporter: More readable and informative error/warning messages.
Bugfixes:
* Commandline Interface: Disallow the following options outside of the compiler mode: ``--via-ir``,``--metadata-literal``, ``--metadata-hash``, ``--model-checker-show-unproved``, ``--model-checker-div-mod-no-slacks``, ``--model-checker-engine``, ``--model-checker-invariants``, ``--model-checker-solvers``, ``--model-checker-timeout``, ``--model-checker-contracts``, ``--model-checker-targets``.

View File

@ -398,7 +398,7 @@ bool DataFlowAnalyzer::inScope(YulString _variableName) const
return false;
}
optional<u256> DataFlowAnalyzer::valueOfIdentifier(YulString const& _name)
optional<u256> DataFlowAnalyzer::valueOfIdentifier(YulString const& _name) const
{
if (AssignedValue const* value = variableValue(_name))
if (Literal const* literal = get_if<Literal>(value->value))

View File

@ -146,7 +146,7 @@ protected:
bool inScope(YulString _variableName) const;
/// Returns the literal value of the identifier, if it exists.
std::optional<u256> valueOfIdentifier(YulString const& _name);
std::optional<u256> valueOfIdentifier(YulString const& _name) const;
enum class StoreLoadLocation {
Memory = 0,

View File

@ -23,7 +23,11 @@
#include <libyul/optimiser/SimplificationRules.h>
#include <libyul/optimiser/OptimiserStep.h>
#include <libyul/optimiser/OptimizerUtilities.h>
#include <libyul/AST.h>
#include <libyul/Utilities.h>
#include <libevmasm/SemanticInformation.h>
using namespace std;
using namespace solidity;
@ -44,4 +48,29 @@ void ExpressionSimplifier::visit(Expression& _expression)
[this](YulString _var) { return variableValue(_var); }
))
_expression = match->action().toExpression(debugDataOf(_expression));
if (auto* functionCall = get_if<FunctionCall>(&_expression))
if (optional<evmasm::Instruction> instruction = toEVMInstruction(m_dialect, functionCall->functionName.name))
for (auto op: evmasm::SemanticInformation::readWriteOperations(*instruction))
if (op.startParameter && op.lengthParameter)
{
Expression& startArgument = functionCall->arguments.at(*op.startParameter);
Expression const& lengthArgument = functionCall->arguments.at(*op.lengthParameter);
if (
knownToBeZero(lengthArgument) &&
!knownToBeZero(startArgument) &&
!holds_alternative<FunctionCall>(startArgument)
)
startArgument = Literal{debugDataOf(startArgument), LiteralKind::Number, "0"_yulstring, {}};
}
}
bool ExpressionSimplifier::knownToBeZero(Expression const& _expression) const
{
if (auto const* literal = get_if<Literal>(&_expression))
return valueOfLiteral(*literal) == 0;
else if (auto const* identifier = get_if<Identifier>(&_expression))
return valueOfIdentifier(identifier->name) == 0;
else
return false;
}

View File

@ -52,6 +52,7 @@ public:
private:
explicit ExpressionSimplifier(Dialect const& _dialect): DataFlowAnalyzer(_dialect) {}
bool knownToBeZero(Expression const& _expression) const;
};
}

View File

@ -1,5 +1,5 @@
{
for {} div(create(0, 1, 0), shl(msize(), 1)) {}
for {} div(create(0, 1, 1), shl(msize(), 1)) {}
{
}
}
@ -10,7 +10,7 @@
//
// {
// {
// for { } div(create(0, 1, 0), shl(msize(), 1)) { }
// for { } div(create(0, 1, 1), shl(msize(), 1)) { }
// { }
// }
// }

View File

@ -0,0 +1,21 @@
{
revert(calldataload(0), 0)
revert(call(0,0,0,0,0,0,0), 0)
calldatacopy(calldataload(1), calldataload(2), 0)
return(calldataload(3), 0)
codecopy(calldataload(4), calldataload(5), sub(42,42))
}
// ----
// step: expressionSimplifier
//
// {
// {
// let _1 := 0
// revert(0, _1)
// pop(call(_1, _1, _1, _1, _1, _1, _1))
// revert(0, _1)
// calldatacopy(0, calldataload(2), _1)
// return(0, _1)
// codecopy(0, calldataload(5), 0)
// }
// }