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

This commit is contained in:
Daniel Kirchner
2022-07-08 12:29:58 +02:00
committed by Marenz
parent b6f11b3392
commit 6ce4ff262b
7 changed files with 57 additions and 4 deletions
+1 -1
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))
+1 -1
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,
+29
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;
}
+1
View File
@@ -52,6 +52,7 @@ public:
private:
explicit ExpressionSimplifier(Dialect const& _dialect): DataFlowAnalyzer(_dialect) {}
bool knownToBeZero(Expression const& _expression) const;
};
}