Merge pull request #9546 from ethereum/expToShift

Optimization rule: Replace exp by shl.
This commit is contained in:
chriseth 2020-09-21 12:21:47 +02:00 committed by GitHub
commit 306fef334b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 2 deletions

View File

@ -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``.

View File

@ -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())
@ -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;
}

View File

@ -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))
// }