From e86b5cf14ec1fafc8d1004d0bcf0dcef52f6cc7d Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 28 Aug 2019 17:34:31 +0200 Subject: [PATCH] Replace byte(A, X) by zero if A >= 32 --- Changelog.md | 1 + libevmasm/RuleList.h | 8 ++++++++ .../expressionSimplifier/large_byte_access.yul | 16 ++++++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 test/libyul/yulOptimizerTests/expressionSimplifier/large_byte_access.yul diff --git a/Changelog.md b/Changelog.md index f4ee60640..bc165d4f6 100644 --- a/Changelog.md +++ b/Changelog.md @@ -5,6 +5,7 @@ Language Features: Compiler Features: + * Optimizer: Add rule that replaces the BYTE opcode by 0 if the first argument is larger than 31. Bugfixes: diff --git a/libevmasm/RuleList.h b/libevmasm/RuleList.h index 70f136d9d..ce00e95df 100644 --- a/libevmasm/RuleList.h +++ b/libevmasm/RuleList.h @@ -259,6 +259,14 @@ std::vector> simplificationRuleListPart5( [=]() { return A.d() >= 256; } }); + // Replace BYTE(A, X), A >= 32 with 0 + rules.push_back({ + {Instruction::BYTE, {A, X}}, + [=]() -> Pattern { return u256(0); }, + true, + [=]() { return A.d() >= 32; } + }); + for (auto const& op: std::vector{ Instruction::ADDRESS, Instruction::CALLER, diff --git a/test/libyul/yulOptimizerTests/expressionSimplifier/large_byte_access.yul b/test/libyul/yulOptimizerTests/expressionSimplifier/large_byte_access.yul new file mode 100644 index 000000000..d5e7c0ad4 --- /dev/null +++ b/test/libyul/yulOptimizerTests/expressionSimplifier/large_byte_access.yul @@ -0,0 +1,16 @@ +{ + let a := calldataload(0) + let b := byte(33, a) + let c := byte(20, a) + // create cannot be removed. + let d := byte(33, create(0, 0, 0x20)) +} +// ==== +// step: expressionSimplifier +// ---- +// { +// let a := calldataload(0) +// let b := 0 +// let c := byte(20, a) +// let d := byte(33, create(0, 0, 0x20)) +// }