Replace shifts by larger than 255 with 0

This commit is contained in:
Alex Beregszaszi 2019-02-21 11:34:42 +00:00 committed by Mathias Baumann
parent 65026a0a25
commit 84fbf605aa
3 changed files with 60 additions and 1 deletions

View File

@ -5,6 +5,7 @@ Language Features:
Compiler Features:
* SMTChecker: Support arithmetic compound assignment operators.
* Optimizer: Add rule for shifts by constants larger than 255 for Constantinople.
* Yul: Adds break and continue keywords to for-loop syntax.

View File

@ -216,7 +216,7 @@ std::vector<SimplificationRule<Pattern>> simplificationRuleListPart4(
template <class Pattern>
std::vector<SimplificationRule<Pattern>> simplificationRuleListPart5(
Pattern,
Pattern A,
Pattern,
Pattern,
Pattern X,
@ -236,6 +236,22 @@ std::vector<SimplificationRule<Pattern>> simplificationRuleListPart5(
});
}
// Replace SHL >=256, X with 0
rules.push_back({
{Instruction::SHL, {A, X}},
[=]() -> Pattern { return u256(0); },
true,
[=]() { return A.d() >= 256; }
});
// Replace SHR >=256, X with 0
rules.push_back({
{Instruction::SHR, {A, X}},
[=]() -> Pattern { return u256(0); },
true,
[=]() { return A.d() >= 256; }
});
for (auto const& op: std::vector<Instruction>{
Instruction::ADDRESS,
Instruction::CALLER,

View File

@ -1250,6 +1250,48 @@ BOOST_AUTO_TEST_CASE(cse_remove_unwanted_masking_of_address)
});
}
BOOST_AUTO_TEST_CASE(cse_replace_too_large_shift)
{
if (!dev::test::Options::get().evmVersion().hasBitwiseShifting())
return;
checkCSE({
Instruction::CALLVALUE,
u256(299),
Instruction::SHL
}, {
u256(0)
});
checkCSE({
Instruction::CALLVALUE,
u256(299),
Instruction::SHR
}, {
u256(0)
});
checkCSE({
Instruction::CALLVALUE,
u256(255),
Instruction::SHL
}, {
Instruction::CALLVALUE,
u256(255),
Instruction::SHL
});
checkCSE({
Instruction::CALLVALUE,
u256(255),
Instruction::SHR
}, {
Instruction::CALLVALUE,
u256(255),
Instruction::SHR
});
}
BOOST_AUTO_TEST_SUITE_END()
}