From a5427bc63ab91fe9b4cc1553141e7d77be28f047 Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Tue, 14 May 2019 16:01:50 +0200 Subject: [PATCH] Add optimizer rules for multiplication and division by left-shifted one. --- Changelog.md | 1 + libevmasm/RuleList.h | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/Changelog.md b/Changelog.md index a10da4252..a94e83ebe 100644 --- a/Changelog.md +++ b/Changelog.md @@ -11,6 +11,7 @@ Compiler Features: * SMTChecker: Inline external function calls to ``this``. * Assembler: Encode the compiler version in the deployed bytecode. * Yul Optimizer: Simplify single-run ``for`` loops to ``if`` statements. + * Optimizer: Add rules for multiplication and division by left-shifted one. Bugfixes: diff --git a/libevmasm/RuleList.h b/libevmasm/RuleList.h index eecef224e..2f3a8b63c 100644 --- a/libevmasm/RuleList.h +++ b/libevmasm/RuleList.h @@ -391,6 +391,31 @@ std::vector> simplificationRuleListPart7( false }); + rules.push_back({ + // MUL(X, SHL(Y, 1)) -> SHL(Y, X) + {Instruction::MUL, {X, {Instruction::SHL, {Y, u256(1)}}}}, + [=]() -> Pattern { + return {Instruction::SHL, {Y, X}}; + }, + false + }); + rules.push_back({ + // MUL(SHL(X, 1), Y) -> SHL(X, Y) + {Instruction::MUL, {{Instruction::SHL, {X, u256(1)}}, Y}}, + [=]() -> Pattern { + return {Instruction::SHL, {X, Y}}; + }, + false + }); + + rules.push_back({ + // DIV(X, SHL(Y, 1)) -> SHR(Y, X) + {Instruction::DIV, {X, {Instruction::SHL, {Y, u256(1)}}}}, + [=]() -> Pattern { + return {Instruction::SHR, {Y, X}}; + }, + false + }); std::function feasibilityFunction = [=]() { if (B.d() > 256)