Skip to content

Commit

Permalink
Merge pull request ethereum#9546 from ethereum/expToShift
Browse files Browse the repository at this point in the history
Optimization rule: Replace exp by shl.
  • Loading branch information
chriseth authored Sep 21, 2020
2 parents b571fd0 + 684fff3 commit 306fef3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Compiler Features:
* Yul Optimizer: Inline into functions further down in the call graph first.
* Yul Optimizer: Try to simplify function names.
* Yul IR Generator: Report source locations related to unimplemented features.

* Optimizer: Optimize ``exp`` when base is 0, 1 or 2.

Bugfixes:
* Code generator: Fix internal error on stripping dynamic types from return parameters on EVM versions without ``RETURNDATACOPY``.
Expand Down
17 changes: 16 additions & 1 deletion libevmasm/RuleList.h
Original file line number Diff line number Diff line change
Expand Up @@ -615,12 +615,13 @@ std::vector<SimplificationRule<Pattern>> evmRuleList(
Pattern,
Pattern,
Pattern,
Pattern,
Pattern X,
Pattern,
Pattern
)
{
using Builtins = typename Pattern::Builtins;
using Word = typename Pattern::Word;
std::vector<SimplificationRule<Pattern>> rules;

if (_evmVersion.hasSelfBalance())
Expand All @@ -629,6 +630,20 @@ std::vector<SimplificationRule<Pattern>> evmRuleList(
[]() -> Pattern { return Instruction::SELFBALANCE; }
});

rules.emplace_back(
Builtins::EXP(0, X),
[=]() -> Pattern { return Builtins::ISZERO(X); }
);
rules.emplace_back(
Builtins::EXP(1, X),
[=]() -> Pattern { return Word(1); }
);
if (_evmVersion.hasBitwiseShifting())
rules.emplace_back(
Builtins::EXP(2, X),
[=]() -> Pattern { return Builtins::SHL(X, 1); }
);

return rules;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
let t := calldataload(0)
sstore(0, exp(0, t))
sstore(1, exp(1, t))
sstore(2, exp(2, t))
// The following should not be simplified
sstore(3, exp(8, t))
}
// ====
// EVMVersion: >=constantinople
// ----
// step: expressionSimplifier
//
// {
// let _1 := 0
// let t := calldataload(_1)
// sstore(_1, iszero(t))
// sstore(1, 1)
// let _8 := 2
// sstore(_8, shl(t, 1))
// sstore(3, exp(8, t))
// }

0 comments on commit 306fef3

Please sign in to comment.