mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Simplify patterns.
This commit is contained in:
parent
3e04fd6efa
commit
e70ef0b820
@ -67,47 +67,48 @@ std::vector<SimplificationRule<Pattern>> simplificationRuleListPart1(
|
||||
)
|
||||
{
|
||||
using Word = typename Pattern::Word;
|
||||
using Builtins = typename Pattern::Builtins;
|
||||
return std::vector<SimplificationRule<Pattern>> {
|
||||
// arithmetic on constants
|
||||
{{Pattern::Builtins::ADD, {A, B}}, [=]{ return A.d() + B.d(); }, false},
|
||||
{{Pattern::Builtins::MUL, {A, B}}, [=]{ return A.d() * B.d(); }, false},
|
||||
{{Pattern::Builtins::SUB, {A, B}}, [=]{ return A.d() - B.d(); }, false},
|
||||
{{Pattern::Builtins::DIV, {A, B}}, [=]{ return B.d() == 0 ? 0 : divWorkaround(A.d(), B.d()); }, false},
|
||||
{{Pattern::Builtins::SDIV, {A, B}}, [=]{ return B.d() == 0 ? 0 : s2u(divWorkaround(u2s(A.d()), u2s(B.d()))); }, false},
|
||||
{{Pattern::Builtins::MOD, {A, B}}, [=]{ return B.d() == 0 ? 0 : modWorkaround(A.d(), B.d()); }, false},
|
||||
{{Pattern::Builtins::SMOD, {A, B}}, [=]{ return B.d() == 0 ? 0 : s2u(modWorkaround(u2s(A.d()), u2s(B.d()))); }, false},
|
||||
{{Pattern::Builtins::EXP, {A, B}}, [=]{ return Word(boost::multiprecision::powm(bigint(A.d()), bigint(B.d()), bigint(1) << Pattern::WordSize)); }, false},
|
||||
{{Pattern::Builtins::NOT, {A}}, [=]{ return ~A.d(); }, false},
|
||||
{{Pattern::Builtins::LT, {A, B}}, [=]() -> Word { return A.d() < B.d() ? 1 : 0; }, false},
|
||||
{{Pattern::Builtins::GT, {A, B}}, [=]() -> Word { return A.d() > B.d() ? 1 : 0; }, false},
|
||||
{{Pattern::Builtins::SLT, {A, B}}, [=]() -> Word { return u2s(A.d()) < u2s(B.d()) ? 1 : 0; }, false},
|
||||
{{Pattern::Builtins::SGT, {A, B}}, [=]() -> Word { return u2s(A.d()) > u2s(B.d()) ? 1 : 0; }, false},
|
||||
{{Pattern::Builtins::EQ, {A, B}}, [=]() -> Word { return A.d() == B.d() ? 1 : 0; }, false},
|
||||
{{Pattern::Builtins::ISZERO, {A}}, [=]() -> Word { return A.d() == 0 ? 1 : 0; }, false},
|
||||
{{Pattern::Builtins::AND, {A, B}}, [=]{ return A.d() & B.d(); }, false},
|
||||
{{Pattern::Builtins::OR, {A, B}}, [=]{ return A.d() | B.d(); }, false},
|
||||
{{Pattern::Builtins::XOR, {A, B}}, [=]{ return A.d() ^ B.d(); }, false},
|
||||
{{Pattern::Builtins::BYTE, {A, B}}, [=]{
|
||||
{Builtins::ADD(A, B), [=]{ return A.d() + B.d(); }, false},
|
||||
{Builtins::MUL(A, B), [=]{ return A.d() * B.d(); }, false},
|
||||
{Builtins::SUB(A, B), [=]{ return A.d() - B.d(); }, false},
|
||||
{Builtins::DIV(A, B), [=]{ return B.d() == 0 ? 0 : divWorkaround(A.d(), B.d()); }, false},
|
||||
{Builtins::SDIV(A, B), [=]{ return B.d() == 0 ? 0 : s2u(divWorkaround(u2s(A.d()), u2s(B.d()))); }, false},
|
||||
{Builtins::MOD(A, B), [=]{ return B.d() == 0 ? 0 : modWorkaround(A.d(), B.d()); }, false},
|
||||
{Builtins::SMOD(A, B), [=]{ return B.d() == 0 ? 0 : s2u(modWorkaround(u2s(A.d()), u2s(B.d()))); }, false},
|
||||
{Builtins::EXP(A, B), [=]{ return Word(boost::multiprecision::powm(bigint(A.d()), bigint(B.d()), bigint(1) << Pattern::WordSize)); }, false},
|
||||
{Builtins::NOT(A), [=]{ return ~A.d(); }, false},
|
||||
{Builtins::LT(A, B), [=]() -> Word { return A.d() < B.d() ? 1 : 0; }, false},
|
||||
{Builtins::GT(A, B), [=]() -> Word { return A.d() > B.d() ? 1 : 0; }, false},
|
||||
{Builtins::SLT(A, B), [=]() -> Word { return u2s(A.d()) < u2s(B.d()) ? 1 : 0; }, false},
|
||||
{Builtins::SGT(A, B), [=]() -> Word { return u2s(A.d()) > u2s(B.d()) ? 1 : 0; }, false},
|
||||
{Builtins::EQ(A, B), [=]() -> Word { return A.d() == B.d() ? 1 : 0; }, false},
|
||||
{Builtins::ISZERO(A), [=]() -> Word { return A.d() == 0 ? 1 : 0; }, false},
|
||||
{Builtins::AND(A, B), [=]{ return A.d() & B.d(); }, false},
|
||||
{Builtins::OR(A, B), [=]{ return A.d() | B.d(); }, false},
|
||||
{Builtins::XOR(A, B), [=]{ return A.d() ^ B.d(); }, false},
|
||||
{Builtins::BYTE(A, B), [=]{
|
||||
return
|
||||
A.d() >= Pattern::WordSize / 8 ?
|
||||
0 :
|
||||
(B.d() >> unsigned(8 * (Pattern::WordSize / 8 - 1 - A.d()))) & 0xff;
|
||||
}, false},
|
||||
{{Pattern::Builtins::ADDMOD, {A, B, C}}, [=]{ return C.d() == 0 ? 0 : Word((bigint(A.d()) + bigint(B.d())) % C.d()); }, false},
|
||||
{{Pattern::Builtins::MULMOD, {A, B, C}}, [=]{ return C.d() == 0 ? 0 : Word((bigint(A.d()) * bigint(B.d())) % C.d()); }, false},
|
||||
{{Pattern::Builtins::SIGNEXTEND, {A, B}}, [=]() -> Word {
|
||||
{Builtins::ADDMOD(A, B, C), [=]{ return C.d() == 0 ? 0 : Word((bigint(A.d()) + bigint(B.d())) % C.d()); }, false},
|
||||
{Builtins::MULMOD(A, B, C), [=]{ return C.d() == 0 ? 0 : Word((bigint(A.d()) * bigint(B.d())) % C.d()); }, false},
|
||||
{Builtins::SIGNEXTEND(A, B), [=]() -> Word {
|
||||
if (A.d() >= Pattern::WordSize / 8 - 1)
|
||||
return B.d();
|
||||
unsigned testBit = unsigned(A.d()) * 8 + 7;
|
||||
Word mask = (Word(1) << testBit) - 1;
|
||||
return boost::multiprecision::bit_test(B.d(), testBit) ? B.d() | ~mask : B.d() & mask;
|
||||
}, false},
|
||||
{{Pattern::Builtins::SHL, {A, B}}, [=]{
|
||||
{Builtins::SHL(A, B), [=]{
|
||||
if (A.d() >= Pattern::WordSize)
|
||||
return Word(0);
|
||||
return shlWorkaround(B.d(), unsigned(A.d()));
|
||||
}, false},
|
||||
{{Pattern::Builtins::SHR, {A, B}}, [=]{
|
||||
{Builtins::SHR(A, B), [=]{
|
||||
if (A.d() >= Pattern::WordSize)
|
||||
return Word(0);
|
||||
return B.d() >> unsigned(A.d());
|
||||
@ -115,6 +116,7 @@ std::vector<SimplificationRule<Pattern>> simplificationRuleListPart1(
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
template <class Pattern>
|
||||
std::vector<SimplificationRule<Pattern>> simplificationRuleListPart2(
|
||||
Pattern,
|
||||
@ -125,50 +127,51 @@ std::vector<SimplificationRule<Pattern>> simplificationRuleListPart2(
|
||||
)
|
||||
{
|
||||
using Word = typename Pattern::Word;
|
||||
using Builtins = typename Pattern::Builtins;
|
||||
return std::vector<SimplificationRule<Pattern>> {
|
||||
// invariants involving known constants
|
||||
{{Pattern::Builtins::ADD, {X, 0}}, [=]{ return X; }, false},
|
||||
{{Pattern::Builtins::ADD, {0, X}}, [=]{ return X; }, false},
|
||||
{{Pattern::Builtins::SUB, {X, 0}}, [=]{ return X; }, false},
|
||||
{{Pattern::Builtins::SUB, {~Word(0), X}}, [=]() -> Pattern { return {Pattern::Builtins::NOT, {X}}; }, false},
|
||||
{{Pattern::Builtins::MUL, {X, 0}}, [=]{ return Word(0); }, true},
|
||||
{{Pattern::Builtins::MUL, {0, X}}, [=]{ return Word(0); }, true},
|
||||
{{Pattern::Builtins::MUL, {X, 1}}, [=]{ return X; }, false},
|
||||
{{Pattern::Builtins::MUL, {1, X}}, [=]{ return X; }, false},
|
||||
{{Pattern::Builtins::MUL, {X, Word(-1)}}, [=]() -> Pattern { return {Pattern::Builtins::SUB, {0, X}}; }, false},
|
||||
{{Pattern::Builtins::MUL, {Word(-1), X}}, [=]() -> Pattern { return {Pattern::Builtins::SUB, {0, X}}; }, false},
|
||||
{{Pattern::Builtins::DIV, {X, 0}}, [=]{ return Word(0); }, true},
|
||||
{{Pattern::Builtins::DIV, {0, X}}, [=]{ return Word(0); }, true},
|
||||
{{Pattern::Builtins::DIV, {X, 1}}, [=]{ return X; }, false},
|
||||
{{Pattern::Builtins::SDIV, {X, 0}}, [=]{ return Word(0); }, true},
|
||||
{{Pattern::Builtins::SDIV, {0, X}}, [=]{ return Word(0); }, true},
|
||||
{{Pattern::Builtins::SDIV, {X, 1}}, [=]{ return X; }, false},
|
||||
{{Pattern::Builtins::AND, {X, ~Word(0)}}, [=]{ return X; }, false},
|
||||
{{Pattern::Builtins::AND, {~Word(0), X}}, [=]{ return X; }, false},
|
||||
{{Pattern::Builtins::AND, {X, 0}}, [=]{ return Word(0); }, true},
|
||||
{{Pattern::Builtins::AND, {0, X}}, [=]{ return Word(0); }, true},
|
||||
{{Pattern::Builtins::OR, {X, 0}}, [=]{ return X; }, false},
|
||||
{{Pattern::Builtins::OR, {0, X}}, [=]{ return X; }, false},
|
||||
{{Pattern::Builtins::OR, {X, ~Word(0)}}, [=]{ return ~Word(0); }, true},
|
||||
{{Pattern::Builtins::OR, {~Word(0), X}}, [=]{ return ~Word(0); }, true},
|
||||
{{Pattern::Builtins::XOR, {X, 0}}, [=]{ return X; }, false},
|
||||
{{Pattern::Builtins::XOR, {0, X}}, [=]{ return X; }, false},
|
||||
{{Pattern::Builtins::MOD, {X, 0}}, [=]{ return Word(0); }, true},
|
||||
{{Pattern::Builtins::MOD, {0, X}}, [=]{ return Word(0); }, true},
|
||||
{{Pattern::Builtins::EQ, {X, 0}}, [=]() -> Pattern { return {Pattern::Builtins::ISZERO, {X}}; }, false },
|
||||
{{Pattern::Builtins::EQ, {0, X}}, [=]() -> Pattern { return {Pattern::Builtins::ISZERO, {X}}; }, false },
|
||||
{{Pattern::Builtins::SHL, {0, X}}, [=]{ return X; }, false},
|
||||
{{Pattern::Builtins::SHR, {0, X}}, [=]{ return X; }, false},
|
||||
{{Pattern::Builtins::SHL, {X, 0}}, [=]{ return Word(0); }, true},
|
||||
{{Pattern::Builtins::SHR, {X, 0}}, [=]{ return Word(0); }, true},
|
||||
{{Pattern::Builtins::GT, {X, 0}}, [=]() -> Pattern { return {Pattern::Builtins::ISZERO, {{Pattern::Builtins::ISZERO, {X}}}}; }, false},
|
||||
{{Pattern::Builtins::LT, {0, X}}, [=]() -> Pattern { return {Pattern::Builtins::ISZERO, {{Pattern::Builtins::ISZERO, {X}}}}; }, false},
|
||||
{{Pattern::Builtins::GT, {X, ~Word(0)}}, [=]{ return Word(0); }, true},
|
||||
{{Pattern::Builtins::LT, {~Word(0), X}}, [=]{ return Word(0); }, true},
|
||||
{{Pattern::Builtins::GT, {0, X}}, [=]{ return Word(0); }, true},
|
||||
{{Pattern::Builtins::LT, {X, 0}}, [=]{ return Word(0); }, true},
|
||||
{{Pattern::Builtins::AND, {{Pattern::Builtins::BYTE, {X, Y}}, {Word(0xff)}}}, [=]() -> Pattern { return {Pattern::Builtins::BYTE, {X, Y}}; }, false},
|
||||
{{Pattern::Builtins::BYTE, {Pattern::WordSize / 8 - 1, X}}, [=]() -> Pattern { return {Pattern::Builtins::AND, {X, Word(0xff)}}; }, false}
|
||||
{Builtins::ADD(X, 0), [=]{ return X; }, false},
|
||||
{Builtins::ADD(0, X), [=]{ return X; }, false},
|
||||
{Builtins::SUB(X, 0), [=]{ return X; }, false},
|
||||
{Builtins::SUB(~Word(0), X), [=]() -> Pattern { return Builtins::NOT(X); }, false},
|
||||
{Builtins::MUL(X, 0), [=]{ return Word(0); }, true},
|
||||
{Builtins::MUL(0, X), [=]{ return Word(0); }, true},
|
||||
{Builtins::MUL(X, 1), [=]{ return X; }, false},
|
||||
{Builtins::MUL(1, X), [=]{ return X; }, false},
|
||||
{Builtins::MUL(X, Word(-1)), [=]() -> Pattern { return Builtins::SUB(0, X); }, false},
|
||||
{Builtins::MUL(Word(-1), X), [=]() -> Pattern { return Builtins::SUB(0, X); }, false},
|
||||
{Builtins::DIV(X, 0), [=]{ return Word(0); }, true},
|
||||
{Builtins::DIV(0, X), [=]{ return Word(0); }, true},
|
||||
{Builtins::DIV(X, 1), [=]{ return X; }, false},
|
||||
{Builtins::SDIV(X, 0), [=]{ return Word(0); }, true},
|
||||
{Builtins::SDIV(0, X), [=]{ return Word(0); }, true},
|
||||
{Builtins::SDIV(X, 1), [=]{ return X; }, false},
|
||||
{Builtins::AND(X, ~Word(0)), [=]{ return X; }, false},
|
||||
{Builtins::AND(~Word(0), X), [=]{ return X; }, false},
|
||||
{Builtins::AND(X, 0), [=]{ return Word(0); }, true},
|
||||
{Builtins::AND(0, X), [=]{ return Word(0); }, true},
|
||||
{Builtins::OR(X, 0), [=]{ return X; }, false},
|
||||
{Builtins::OR(0, X), [=]{ return X; }, false},
|
||||
{Builtins::OR(X, ~Word(0)), [=]{ return ~Word(0); }, true},
|
||||
{Builtins::OR(~Word(0), X), [=]{ return ~Word(0); }, true},
|
||||
{Builtins::XOR(X, 0), [=]{ return X; }, false},
|
||||
{Builtins::XOR(0, X), [=]{ return X; }, false},
|
||||
{Builtins::MOD(X, 0), [=]{ return Word(0); }, true},
|
||||
{Builtins::MOD(0, X), [=]{ return Word(0); }, true},
|
||||
{Builtins::EQ(X, 0), [=]() -> Pattern { return Builtins::ISZERO(X); }, false },
|
||||
{Builtins::EQ(0, X), [=]() -> Pattern { return Builtins::ISZERO(X); }, false },
|
||||
{Builtins::SHL(0, X), [=]{ return X; }, false},
|
||||
{Builtins::SHR(0, X), [=]{ return X; }, false},
|
||||
{Builtins::SHL(X, 0), [=]{ return Word(0); }, true},
|
||||
{Builtins::SHR(X, 0), [=]{ return Word(0); }, true},
|
||||
{Builtins::GT(X, 0), [=]() -> Pattern { return Builtins::ISZERO(Builtins::ISZERO(X)); }, false},
|
||||
{Builtins::LT(0, X), [=]() -> Pattern { return Builtins::ISZERO(Builtins::ISZERO(X)); }, false},
|
||||
{Builtins::GT(X, ~Word(0)), [=]{ return Word(0); }, true},
|
||||
{Builtins::LT(~Word(0), X), [=]{ return Word(0); }, true},
|
||||
{Builtins::GT(0, X), [=]{ return Word(0); }, true},
|
||||
{Builtins::LT(X, 0), [=]{ return Word(0); }, true},
|
||||
{Builtins::AND(Builtins::BYTE(X, Y), Word(0xff)), [=]() -> Pattern { return Builtins::BYTE(X, Y); }, false},
|
||||
{Builtins::BYTE(Word(Pattern::WordSize / 8 - 1), X), [=]() -> Pattern { return Builtins::AND(X, Word(0xff)); }, false}
|
||||
};
|
||||
}
|
||||
|
||||
@ -182,18 +185,19 @@ std::vector<SimplificationRule<Pattern>> simplificationRuleListPart3(
|
||||
)
|
||||
{
|
||||
using Word = typename Pattern::Word;
|
||||
using Builtins = typename Pattern::Builtins;
|
||||
return std::vector<SimplificationRule<Pattern>> {
|
||||
// operations involving an expression and itself
|
||||
{{Pattern::Builtins::AND, {X, X}}, [=]{ return X; }, true},
|
||||
{{Pattern::Builtins::OR, {X, X}}, [=]{ return X; }, true},
|
||||
{{Pattern::Builtins::XOR, {X, X}}, [=]{ return Word(0); }, true},
|
||||
{{Pattern::Builtins::SUB, {X, X}}, [=]{ return Word(0); }, true},
|
||||
{{Pattern::Builtins::EQ, {X, X}}, [=]{ return Word(1); }, true},
|
||||
{{Pattern::Builtins::LT, {X, X}}, [=]{ return Word(0); }, true},
|
||||
{{Pattern::Builtins::SLT, {X, X}}, [=]{ return Word(0); }, true},
|
||||
{{Pattern::Builtins::GT, {X, X}}, [=]{ return Word(0); }, true},
|
||||
{{Pattern::Builtins::SGT, {X, X}}, [=]{ return Word(0); }, true},
|
||||
{{Pattern::Builtins::MOD, {X, X}}, [=]{ return Word(0); }, true}
|
||||
{Builtins::AND(X, X), [=]{ return X; }, true},
|
||||
{Builtins::OR(X, X), [=]{ return X; }, true},
|
||||
{Builtins::XOR(X, X), [=]{ return Word(0); }, true},
|
||||
{Builtins::SUB(X, X), [=]{ return Word(0); }, true},
|
||||
{Builtins::EQ(X, X), [=]{ return Word(1); }, true},
|
||||
{Builtins::LT(X, X), [=]{ return Word(0); }, true},
|
||||
{Builtins::SLT(X, X), [=]{ return Word(0); }, true},
|
||||
{Builtins::GT(X, X), [=]{ return Word(0); }, true},
|
||||
{Builtins::SGT(X, X), [=]{ return Word(0); }, true},
|
||||
{Builtins::MOD(X, X), [=]{ return Word(0); }, true}
|
||||
};
|
||||
}
|
||||
|
||||
@ -207,25 +211,26 @@ std::vector<SimplificationRule<Pattern>> simplificationRuleListPart4(
|
||||
)
|
||||
{
|
||||
using Word = typename Pattern::Word;
|
||||
using Builtins = typename Pattern::Builtins;
|
||||
return std::vector<SimplificationRule<Pattern>> {
|
||||
// logical instruction combinations
|
||||
{{Pattern::Builtins::NOT, {{Pattern::Builtins::NOT, {X}}}}, [=]{ return X; }, false},
|
||||
{{Pattern::Builtins::XOR, {X, {Pattern::Builtins::XOR, {X, Y}}}}, [=]{ return Y; }, true},
|
||||
{{Pattern::Builtins::XOR, {X, {Pattern::Builtins::XOR, {Y, X}}}}, [=]{ return Y; }, true},
|
||||
{{Pattern::Builtins::XOR, {{Pattern::Builtins::XOR, {X, Y}}, X}}, [=]{ return Y; }, true},
|
||||
{{Pattern::Builtins::XOR, {{Pattern::Builtins::XOR, {Y, X}}, X}}, [=]{ return Y; }, true},
|
||||
{{Pattern::Builtins::OR, {X, {Pattern::Builtins::AND, {X, Y}}}}, [=]{ return X; }, true},
|
||||
{{Pattern::Builtins::OR, {X, {Pattern::Builtins::AND, {Y, X}}}}, [=]{ return X; }, true},
|
||||
{{Pattern::Builtins::OR, {{Pattern::Builtins::AND, {X, Y}}, X}}, [=]{ return X; }, true},
|
||||
{{Pattern::Builtins::OR, {{Pattern::Builtins::AND, {Y, X}}, X}}, [=]{ return X; }, true},
|
||||
{{Pattern::Builtins::AND, {X, {Pattern::Builtins::OR, {X, Y}}}}, [=]{ return X; }, true},
|
||||
{{Pattern::Builtins::AND, {X, {Pattern::Builtins::OR, {Y, X}}}}, [=]{ return X; }, true},
|
||||
{{Pattern::Builtins::AND, {{Pattern::Builtins::OR, {X, Y}}, X}}, [=]{ return X; }, true},
|
||||
{{Pattern::Builtins::AND, {{Pattern::Builtins::OR, {Y, X}}, X}}, [=]{ return X; }, true},
|
||||
{{Pattern::Builtins::AND, {X, {Pattern::Builtins::NOT, {X}}}}, [=]{ return Word(0); }, true},
|
||||
{{Pattern::Builtins::AND, {{Pattern::Builtins::NOT, {X}}, X}}, [=]{ return Word(0); }, true},
|
||||
{{Pattern::Builtins::OR, {X, {Pattern::Builtins::NOT, {X}}}}, [=]{ return ~Word(0); }, true},
|
||||
{{Pattern::Builtins::OR, {{Pattern::Builtins::NOT, {X}}, X}}, [=]{ return ~Word(0); }, true},
|
||||
{Builtins::NOT(Builtins::NOT(X)), [=]{ return X; }, false},
|
||||
{Builtins::XOR(X, Builtins::XOR(X, Y)), [=]{ return Y; }, true},
|
||||
{Builtins::XOR(X, Builtins::XOR(Y, X)), [=]{ return Y; }, true},
|
||||
{Builtins::XOR(Builtins::XOR(X, Y), X), [=]{ return Y; }, true},
|
||||
{Builtins::XOR(Builtins::XOR(Y, X), X), [=]{ return Y; }, true},
|
||||
{Builtins::OR(X, Builtins::AND(X, Y)), [=]{ return X; }, true},
|
||||
{Builtins::OR(X, Builtins::AND(Y, X)), [=]{ return X; }, true},
|
||||
{Builtins::OR(Builtins::AND(X, Y), X), [=]{ return X; }, true},
|
||||
{Builtins::OR(Builtins::AND(Y, X), X), [=]{ return X; }, true},
|
||||
{Builtins::AND(X, Builtins::OR(X, Y)), [=]{ return X; }, true},
|
||||
{Builtins::AND(X, Builtins::OR(Y, X)), [=]{ return X; }, true},
|
||||
{Builtins::AND(Builtins::OR(X, Y), X), [=]{ return X; }, true},
|
||||
{Builtins::AND(Builtins::OR(Y, X), X), [=]{ return X; }, true},
|
||||
{Builtins::AND(X, Builtins::NOT(X)), [=]{ return Word(0); }, true},
|
||||
{Builtins::AND(Builtins::NOT(X), X), [=]{ return Word(0); }, true},
|
||||
{Builtins::OR(X, Builtins::NOT(X)), [=]{ return ~Word(0); }, true},
|
||||
{Builtins::OR(Builtins::NOT(X), X), [=]{ return ~Word(0); }, true},
|
||||
};
|
||||
}
|
||||
|
||||
@ -240,6 +245,7 @@ std::vector<SimplificationRule<Pattern>> simplificationRuleListPart5(
|
||||
)
|
||||
{
|
||||
using Word = typename Pattern::Word;
|
||||
using Builtins = typename Pattern::Builtins;
|
||||
|
||||
std::vector<SimplificationRule<Pattern>> rules;
|
||||
|
||||
@ -248,15 +254,15 @@ std::vector<SimplificationRule<Pattern>> simplificationRuleListPart5(
|
||||
{
|
||||
Word value = Word(1) << i;
|
||||
rules.push_back({
|
||||
{Pattern::Builtins::MOD, {X, value}},
|
||||
[=]() -> Pattern { return {Pattern::Builtins::AND, {X, value - 1}}; },
|
||||
Builtins::MOD(X, value),
|
||||
[=]() -> Pattern { return Builtins::AND(X, value - 1); },
|
||||
false
|
||||
});
|
||||
}
|
||||
|
||||
// Replace SHL >=256, X with 0
|
||||
rules.push_back({
|
||||
{Pattern::Builtins::SHL, {A, X}},
|
||||
Builtins::SHL(A, X),
|
||||
[=]() -> Pattern { return Word(0); },
|
||||
true,
|
||||
[=]() { return A.d() >= Pattern::WordSize; }
|
||||
@ -264,7 +270,7 @@ std::vector<SimplificationRule<Pattern>> simplificationRuleListPart5(
|
||||
|
||||
// Replace SHR >=256, X with 0
|
||||
rules.push_back({
|
||||
{Pattern::Builtins::SHR, {A, X}},
|
||||
Builtins::SHR(A, X),
|
||||
[=]() -> Pattern { return Word(0); },
|
||||
true,
|
||||
[=]() { return A.d() >= Pattern::WordSize; }
|
||||
@ -272,29 +278,29 @@ std::vector<SimplificationRule<Pattern>> simplificationRuleListPart5(
|
||||
|
||||
// Replace BYTE(A, X), A >= 32 with 0
|
||||
rules.push_back({
|
||||
{Pattern::Builtins::BYTE, {A, X}},
|
||||
Builtins::BYTE(A, X),
|
||||
[=]() -> Pattern { return Word(0); },
|
||||
true,
|
||||
[=]() { return A.d() >= Pattern::WordSize / 8; }
|
||||
});
|
||||
|
||||
for (auto const& op: std::vector<Instruction>{
|
||||
Pattern::Builtins::ADDRESS,
|
||||
Pattern::Builtins::CALLER,
|
||||
Pattern::Builtins::ORIGIN,
|
||||
Pattern::Builtins::COINBASE
|
||||
for (auto instr: {
|
||||
Instruction::ADDRESS,
|
||||
Instruction::CALLER,
|
||||
Instruction::ORIGIN,
|
||||
Instruction::COINBASE
|
||||
})
|
||||
{
|
||||
assertThrow(Pattern::WordSize > 160, OptimizerException, "");
|
||||
Word const mask = (Word(1) << 160) - 1;
|
||||
rules.push_back({
|
||||
{Pattern::Builtins::AND, {{op, mask}}},
|
||||
[=]() -> Pattern { return op; },
|
||||
Builtins::AND(Pattern{instr}, mask),
|
||||
[=]() -> Pattern { return {instr}; },
|
||||
false
|
||||
});
|
||||
rules.push_back({
|
||||
{Pattern::Builtins::AND, {{mask, op}}},
|
||||
[=]() -> Pattern { return op; },
|
||||
Builtins::AND(mask, Pattern{instr}),
|
||||
[=]() -> Pattern { return {instr}; },
|
||||
false
|
||||
});
|
||||
}
|
||||
@ -311,30 +317,35 @@ std::vector<SimplificationRule<Pattern>> simplificationRuleListPart6(
|
||||
Pattern Y
|
||||
)
|
||||
{
|
||||
using Builtins = typename Pattern::Builtins;
|
||||
|
||||
std::vector<SimplificationRule<Pattern>> rules;
|
||||
// Double negation of opcodes with boolean result
|
||||
for (auto const& op: std::vector<Instruction>{
|
||||
Pattern::Builtins::EQ,
|
||||
Pattern::Builtins::LT,
|
||||
Pattern::Builtins::SLT,
|
||||
Pattern::Builtins::GT,
|
||||
Pattern::Builtins::SGT
|
||||
for (auto instr: {
|
||||
Instruction::EQ,
|
||||
Instruction::LT,
|
||||
Instruction::SLT,
|
||||
Instruction::GT,
|
||||
Instruction::SGT
|
||||
})
|
||||
{
|
||||
typename Builtins::PatternGeneratorInstance op{instr};
|
||||
rules.push_back({
|
||||
{Pattern::Builtins::ISZERO, {{Pattern::Builtins::ISZERO, {{op, {X, Y}}}}}},
|
||||
[=]() -> Pattern { return {op, {X, Y}}; },
|
||||
Builtins::ISZERO(Builtins::ISZERO(op(X, Y))),
|
||||
[=]() -> Pattern { return op(X, Y); },
|
||||
false
|
||||
});
|
||||
}
|
||||
|
||||
rules.push_back({
|
||||
{Pattern::Builtins::ISZERO, {{Pattern::Builtins::ISZERO, {{Pattern::Builtins::ISZERO, {X}}}}}},
|
||||
[=]() -> Pattern { return {Pattern::Builtins::ISZERO, {X}}; },
|
||||
Builtins::ISZERO(Builtins::ISZERO(Builtins::ISZERO(X))),
|
||||
[=]() -> Pattern { return Builtins::ISZERO(X); },
|
||||
false
|
||||
});
|
||||
|
||||
rules.push_back({
|
||||
{Pattern::Builtins::ISZERO, {{Pattern::Builtins::XOR, {X, Y}}}},
|
||||
[=]() -> Pattern { return { Pattern::Builtins::EQ, {X, Y} }; },
|
||||
Builtins::ISZERO(Builtins::XOR(X, Y)),
|
||||
[=]() -> Pattern { return Builtins::EQ(X, Y); },
|
||||
false
|
||||
});
|
||||
|
||||
@ -351,42 +362,44 @@ std::vector<SimplificationRule<Pattern>> simplificationRuleListPart7(
|
||||
)
|
||||
{
|
||||
using Word = typename Pattern::Word;
|
||||
using Builtins = typename Pattern::Builtins;
|
||||
|
||||
std::vector<SimplificationRule<Pattern>> rules;
|
||||
// Associative operations
|
||||
for (auto const& opFun: std::vector<std::pair<Instruction,std::function<Word(Word const&,Word const&)>>>{
|
||||
{Pattern::Builtins::ADD, std::plus<Word>()},
|
||||
{Pattern::Builtins::MUL, std::multiplies<Word>()},
|
||||
{Pattern::Builtins::AND, std::bit_and<Word>()},
|
||||
{Pattern::Builtins::OR, std::bit_or<Word>()},
|
||||
{Pattern::Builtins::XOR, std::bit_xor<Word>()}
|
||||
for (auto&& instrAndFunc: std::vector<std::pair<Instruction, std::function<Word(Word, Word)>>>{
|
||||
{Instruction::ADD, std::plus<Word>()},
|
||||
{Instruction::MUL, std::multiplies<Word>()},
|
||||
{Instruction::AND, std::bit_and<Word>()},
|
||||
{Instruction::OR, std::bit_or<Word>()},
|
||||
{Instruction::XOR, std::bit_xor<Word>()}
|
||||
})
|
||||
{
|
||||
auto op = opFun.first;
|
||||
auto fun = opFun.second;
|
||||
typename Builtins::PatternGeneratorInstance op{instrAndFunc.first};
|
||||
std::function<Word(Word, Word)> fun = instrAndFunc.second;
|
||||
// Moving constants to the outside, order matters here - we first add rules
|
||||
// for constants and then for non-constants.
|
||||
// xa can be (X, A) or (A, X)
|
||||
for (auto xa: {std::vector<Pattern>{X, A}, std::vector<Pattern>{A, X}})
|
||||
for (auto const& opXA: {op(X, A), op(A, X)})
|
||||
{
|
||||
rules += std::vector<SimplificationRule<Pattern>>{{
|
||||
// (X+A)+B -> X+(A+B)
|
||||
{op, {{op, xa}, B}},
|
||||
[=]() -> Pattern { return {op, {X, fun(A.d(), B.d())}}; },
|
||||
op(opXA, B),
|
||||
[=]() -> Pattern { return op(X, fun(A.d(), B.d())); },
|
||||
false
|
||||
}, {
|
||||
// (X+A)+Y -> (X+Y)+A
|
||||
{op, {{op, xa}, Y}},
|
||||
[=]() -> Pattern { return {op, {{op, {X, Y}}, A}}; },
|
||||
op(opXA, Y),
|
||||
[=]() -> Pattern { return op(op(X, Y), A); },
|
||||
false
|
||||
}, {
|
||||
// B+(X+A) -> X+(A+B)
|
||||
{op, {B, {op, xa}}},
|
||||
[=]() -> Pattern { return {op, {X, fun(A.d(), B.d())}}; },
|
||||
op(B, opXA),
|
||||
[=]() -> Pattern { return op(X, fun(A.d(), B.d())); },
|
||||
false
|
||||
}, {
|
||||
// Y+(X+A) -> (Y+X)+A
|
||||
{op, {Y, {op, xa}}},
|
||||
[=]() -> Pattern { return {op, {{op, {Y, X}}, A}}; },
|
||||
op(Y, opXA),
|
||||
[=]() -> Pattern { return op(op(Y, X), A); },
|
||||
false
|
||||
}};
|
||||
}
|
||||
@ -395,13 +408,13 @@ std::vector<SimplificationRule<Pattern>> simplificationRuleListPart7(
|
||||
// Combine two SHL by constant
|
||||
rules.push_back({
|
||||
// SHL(B, SHL(A, X)) -> SHL(min(A+B, 256), X)
|
||||
{Pattern::Builtins::SHL, {{B}, {Pattern::Builtins::SHL, {{A}, {X}}}}},
|
||||
Builtins::SHL(B, Builtins::SHL(A, X)),
|
||||
[=]() -> Pattern {
|
||||
bigint sum = bigint(A.d()) + B.d();
|
||||
if (sum >= Pattern::WordSize)
|
||||
return {Pattern::Builtins::AND, {X, Word(0)}};
|
||||
return Builtins::AND(X, Word(0));
|
||||
else
|
||||
return {Pattern::Builtins::SHL, {Word(sum), X}};
|
||||
return Builtins::SHL(Word(sum), X);
|
||||
},
|
||||
false
|
||||
});
|
||||
@ -409,13 +422,13 @@ std::vector<SimplificationRule<Pattern>> simplificationRuleListPart7(
|
||||
// Combine two SHR by constant
|
||||
rules.push_back({
|
||||
// SHR(B, SHR(A, X)) -> SHR(min(A+B, 256), X)
|
||||
{Pattern::Builtins::SHR, {{B}, {Pattern::Builtins::SHR, {{A}, {X}}}}},
|
||||
Builtins::SHR(B, Builtins::SHR(A, X)),
|
||||
[=]() -> Pattern {
|
||||
bigint sum = bigint(A.d()) + B.d();
|
||||
if (sum >= Pattern::WordSize)
|
||||
return {Pattern::Builtins::AND, {X, Word(0)}};
|
||||
return Builtins::AND(X, Word(0));
|
||||
else
|
||||
return {Pattern::Builtins::SHR, {Word(sum), X}};
|
||||
return Builtins::SHR(Word(sum), X);
|
||||
},
|
||||
false
|
||||
});
|
||||
@ -423,16 +436,16 @@ std::vector<SimplificationRule<Pattern>> simplificationRuleListPart7(
|
||||
// Combine SHL-SHR by constant
|
||||
rules.push_back({
|
||||
// SHR(B, SHL(A, X)) -> AND(SH[L/R]([B - A / A - B], X), Mask)
|
||||
{Pattern::Builtins::SHR, {{B}, {Pattern::Builtins::SHL, {{A}, {X}}}}},
|
||||
Builtins::SHR(B, Builtins::SHL(A, X)),
|
||||
[=]() -> Pattern {
|
||||
Word mask = shlWorkaround(~Word(0), unsigned(A.d())) >> unsigned(B.d());
|
||||
|
||||
if (A.d() > B.d())
|
||||
return {Pattern::Builtins::AND, {{Pattern::Builtins::SHL, {A.d() - B.d(), X}}, mask}};
|
||||
return Builtins::AND(Builtins::SHL(A.d() - B.d(), X), mask);
|
||||
else if (B.d() > A.d())
|
||||
return {Pattern::Builtins::AND, {{Pattern::Builtins::SHR, {B.d() - A.d(), X}}, mask}};
|
||||
return Builtins::AND(Builtins::SHR(B.d() - A.d(), X), mask);
|
||||
else
|
||||
return {Pattern::Builtins::AND, {X, mask}};
|
||||
return Builtins::AND(X, mask);
|
||||
},
|
||||
false,
|
||||
[=] { return A.d() < Pattern::WordSize && B.d() < Pattern::WordSize; }
|
||||
@ -441,41 +454,42 @@ std::vector<SimplificationRule<Pattern>> simplificationRuleListPart7(
|
||||
// Combine SHR-SHL by constant
|
||||
rules.push_back({
|
||||
// SHL(B, SHR(A, X)) -> AND(SH[L/R]([B - A / A - B], X), Mask)
|
||||
{Pattern::Builtins::SHL, {{B}, {Pattern::Builtins::SHR, {{A}, {X}}}}},
|
||||
Builtins::SHL(B, Builtins::SHR(A, X)),
|
||||
[=]() -> Pattern {
|
||||
Word mask = shlWorkaround((~Word(0)) >> unsigned(A.d()), unsigned(B.d()));
|
||||
|
||||
if (A.d() > B.d())
|
||||
return {Pattern::Builtins::AND, {{Pattern::Builtins::SHR, {A.d() - B.d(), X}}, mask}};
|
||||
return Builtins::AND(Builtins::SHR(A.d() - B.d(), X), mask);
|
||||
else if (B.d() > A.d())
|
||||
return {Pattern::Builtins::AND, {{Pattern::Builtins::SHL, {B.d() - A.d(), X}}, mask}};
|
||||
return Builtins::AND(Builtins::SHL(B.d() - A.d(), X), mask);
|
||||
else
|
||||
return {Pattern::Builtins::AND, {X, mask}};
|
||||
return Builtins::AND(X, mask);
|
||||
},
|
||||
false,
|
||||
[=] { return A.d() < Pattern::WordSize && B.d() < Pattern::WordSize; }
|
||||
});
|
||||
|
||||
// Move AND with constant across SHL and SHR by constant
|
||||
for (auto shiftOp: {Pattern::Builtins::SHL, Pattern::Builtins::SHR})
|
||||
for (auto instr: {Instruction::SHL, Instruction::SHR})
|
||||
{
|
||||
typename Builtins::PatternGeneratorInstance shiftOp{instr};
|
||||
auto replacement = [=]() -> Pattern {
|
||||
Word mask =
|
||||
shiftOp == Pattern::Builtins::SHL ?
|
||||
instr == Instruction::SHL ?
|
||||
shlWorkaround(A.d(), unsigned(B.d())) :
|
||||
A.d() >> unsigned(B.d());
|
||||
return {Pattern::Builtins::AND, {{shiftOp, {B.d(), X}}, std::move(mask)}};
|
||||
return Builtins::AND(shiftOp(B.d(), X), std::move(mask));
|
||||
};
|
||||
rules.push_back({
|
||||
// SH[L/R](B, AND(X, A)) -> AND(SH[L/R](B, X), [ A << B / A >> B ])
|
||||
{shiftOp, {{B}, {Pattern::Builtins::AND, {{X}, {A}}}}},
|
||||
shiftOp(B, Builtins::AND(X, A)),
|
||||
replacement,
|
||||
false,
|
||||
[=] { return B.d() < Pattern::WordSize; }
|
||||
});
|
||||
rules.push_back({
|
||||
// SH[L/R](B, AND(A, X)) -> AND(SH[L/R](B, X), [ A << B / A >> B ])
|
||||
{shiftOp, {{B}, {Pattern::Builtins::AND, {{A}, {X}}}}},
|
||||
shiftOp(B, Builtins::AND(A, X)),
|
||||
replacement,
|
||||
false,
|
||||
[=] { return B.d() < Pattern::WordSize; }
|
||||
@ -484,27 +498,27 @@ std::vector<SimplificationRule<Pattern>> simplificationRuleListPart7(
|
||||
|
||||
rules.push_back({
|
||||
// MUL(X, SHL(Y, 1)) -> SHL(Y, X)
|
||||
{Pattern::Builtins::MUL, {X, {Pattern::Builtins::SHL, {Y, Word(1)}}}},
|
||||
Builtins::MUL(X, Builtins::SHL(Y, Word(1))),
|
||||
[=]() -> Pattern {
|
||||
return {Pattern::Builtins::SHL, {Y, X}};
|
||||
return Builtins::SHL(Y, X);
|
||||
},
|
||||
// Actually only changes the order, does not remove.
|
||||
true
|
||||
});
|
||||
rules.push_back({
|
||||
// MUL(SHL(X, 1), Y) -> SHL(X, Y)
|
||||
{Pattern::Builtins::MUL, {{Pattern::Builtins::SHL, {X, Word(1)}}, Y}},
|
||||
Builtins::MUL(Builtins::SHL(X, Word(1)), Y),
|
||||
[=]() -> Pattern {
|
||||
return {Pattern::Builtins::SHL, {X, Y}};
|
||||
return Builtins::SHL(X, Y);
|
||||
},
|
||||
false
|
||||
});
|
||||
|
||||
rules.push_back({
|
||||
// DIV(X, SHL(Y, 1)) -> SHR(Y, X)
|
||||
{Pattern::Builtins::DIV, {X, {Pattern::Builtins::SHL, {Y, Word(1)}}}},
|
||||
Builtins::DIV(X, Builtins::SHL(Y, Word(1))),
|
||||
[=]() -> Pattern {
|
||||
return {Pattern::Builtins::SHR, {Y, X}};
|
||||
return Builtins::SHR(Y, X);
|
||||
},
|
||||
// Actually only changes the order, does not remove.
|
||||
true
|
||||
@ -519,16 +533,16 @@ std::vector<SimplificationRule<Pattern>> simplificationRuleListPart7(
|
||||
|
||||
rules.push_back({
|
||||
// AND(A, SHR(B, X)) -> A & ((2^256-1) >> B) == ((2^256-1) >> B)
|
||||
{Pattern::Builtins::AND, {A, {Pattern::Builtins::SHR, {B, X}}}},
|
||||
[=]() -> Pattern { return {Pattern::Builtins::SHR, {B, X}}; },
|
||||
Builtins::AND(A, Builtins::SHR(B, X)),
|
||||
[=]() -> Pattern { return Builtins::SHR(B, X); },
|
||||
false,
|
||||
feasibilityFunction
|
||||
});
|
||||
|
||||
rules.push_back({
|
||||
// AND(SHR(B, X), A) -> ((2^256-1) >> B) & A == ((2^256-1) >> B)
|
||||
{Pattern::Builtins::AND, {{Pattern::Builtins::SHR, {B, X}}, A}},
|
||||
[=]() -> Pattern { return {Pattern::Builtins::SHR, {B, X}}; },
|
||||
Builtins::AND(Builtins::SHR(B, X), A),
|
||||
[=]() -> Pattern { return Builtins::SHR(B, X); },
|
||||
false,
|
||||
feasibilityFunction
|
||||
});
|
||||
@ -545,34 +559,35 @@ std::vector<SimplificationRule<Pattern>> simplificationRuleListPart8(
|
||||
Pattern Y
|
||||
)
|
||||
{
|
||||
using Builtins = typename Pattern::Builtins;
|
||||
std::vector<SimplificationRule<Pattern>> rules;
|
||||
|
||||
// move constants across subtractions
|
||||
rules += std::vector<SimplificationRule<Pattern>>{
|
||||
{
|
||||
// X - A -> X + (-A)
|
||||
{Pattern::Builtins::SUB, {X, A}},
|
||||
[=]() -> Pattern { return {Pattern::Builtins::ADD, {X, 0 - A.d()}}; },
|
||||
Builtins::SUB(X, A),
|
||||
[=]() -> Pattern { return Builtins::ADD(X, 0 - A.d()); },
|
||||
false
|
||||
}, {
|
||||
// (X + A) - Y -> (X - Y) + A
|
||||
{Pattern::Builtins::SUB, {{Pattern::Builtins::ADD, {X, A}}, Y}},
|
||||
[=]() -> Pattern { return {Pattern::Builtins::ADD, {{Pattern::Builtins::SUB, {X, Y}}, A}}; },
|
||||
Builtins::SUB(Builtins::ADD(X, A), Y),
|
||||
[=]() -> Pattern { return Builtins::ADD(Builtins::SUB(X, Y), A); },
|
||||
false
|
||||
}, {
|
||||
// (A + X) - Y -> (X - Y) + A
|
||||
{Pattern::Builtins::SUB, {{Pattern::Builtins::ADD, {A, X}}, Y}},
|
||||
[=]() -> Pattern { return {Pattern::Builtins::ADD, {{Pattern::Builtins::SUB, {X, Y}}, A}}; },
|
||||
Builtins::SUB(Builtins::ADD(A, X), Y),
|
||||
[=]() -> Pattern { return Builtins::ADD(Builtins::SUB(X, Y), A); },
|
||||
false
|
||||
}, {
|
||||
// X - (Y + A) -> (X - Y) + (-A)
|
||||
{Pattern::Builtins::SUB, {X, {Pattern::Builtins::ADD, {Y, A}}}},
|
||||
[=]() -> Pattern { return {Pattern::Builtins::ADD, {{Pattern::Builtins::SUB, {X, Y}}, 0 - A.d()}}; },
|
||||
Builtins::SUB(X, Builtins::ADD(Y, A)),
|
||||
[=]() -> Pattern { return Builtins::ADD(Builtins::SUB(X, Y), 0 - A.d()); },
|
||||
false
|
||||
}, {
|
||||
// X - (A + Y) -> (X - Y) + (-A)
|
||||
{Pattern::Builtins::SUB, {X, {Pattern::Builtins::ADD, {A, Y}}}},
|
||||
[=]() -> Pattern { return {Pattern::Builtins::ADD, {{Pattern::Builtins::SUB, {X, Y}}, 0 - A.d()}}; },
|
||||
Builtins::SUB(X, Builtins::ADD(A, Y)),
|
||||
[=]() -> Pattern { return Builtins::ADD(Builtins::SUB(X, Y), 0 - A.d()); },
|
||||
false
|
||||
}
|
||||
};
|
||||
@ -591,30 +606,31 @@ std::vector<SimplificationRule<Pattern>> simplificationRuleListPart9(
|
||||
)
|
||||
{
|
||||
using Word = typename Pattern::Word;
|
||||
using Builtins = typename Pattern::Builtins;
|
||||
std::vector<SimplificationRule<Pattern>> rules;
|
||||
|
||||
assertThrow(Pattern::WordSize > 160, OptimizerException, "");
|
||||
Word const mask = (Word(1) << 160) - 1;
|
||||
// CREATE
|
||||
rules.push_back({
|
||||
{Pattern::Builtins::AND, {{Pattern::Builtins::CREATE, {W, X, Y}}, mask}},
|
||||
[=]() -> Pattern { return {Pattern::Builtins::CREATE, {W, X, Y}}; },
|
||||
Builtins::AND(Builtins::CREATE(W, X, Y), mask),
|
||||
[=]() -> Pattern { return Builtins::CREATE(W, X, Y); },
|
||||
false
|
||||
});
|
||||
rules.push_back({
|
||||
{Pattern::Builtins::AND, {{mask, {Pattern::Builtins::CREATE, {W, X, Y}}}}},
|
||||
[=]() -> Pattern { return {Pattern::Builtins::CREATE, {W, X, Y}}; },
|
||||
Builtins::AND(mask, Builtins::CREATE(W, X, Y)),
|
||||
[=]() -> Pattern { return Builtins::CREATE(W, X, Y); },
|
||||
false
|
||||
});
|
||||
// CREATE2
|
||||
rules.push_back({
|
||||
{Pattern::Builtins::AND, {{Pattern::Builtins::CREATE2, {W, X, Y, Z}}, mask}},
|
||||
[=]() -> Pattern { return {Pattern::Builtins::CREATE2, {W, X, Y, Z}}; },
|
||||
Builtins::AND(Builtins::CREATE2(W, X, Y, Z), mask),
|
||||
[=]() -> Pattern { return Builtins::CREATE2(W, X, Y, Z); },
|
||||
false
|
||||
});
|
||||
rules.push_back({
|
||||
{Pattern::Builtins::AND, {{mask, {Pattern::Builtins::CREATE2, {W, X, Y, Z}}}}},
|
||||
[=]() -> Pattern { return {Pattern::Builtins::CREATE2, {W, X, Y, Z}}; },
|
||||
Builtins::AND(mask, Builtins::CREATE2(W, X, Y, Z)),
|
||||
[=]() -> Pattern { return Builtins::CREATE2(W, X, Y, Z); },
|
||||
false
|
||||
});
|
||||
|
||||
|
@ -21,6 +21,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <libevmasm/Instruction.h>
|
||||
#include <libdevcore/CommonData.h>
|
||||
#include <functional>
|
||||
|
||||
namespace dev
|
||||
@ -55,84 +56,105 @@ struct SimplificationRule
|
||||
std::function<bool()> feasible;
|
||||
};
|
||||
|
||||
template <typename Pattern>
|
||||
struct EVMBuiltins
|
||||
{
|
||||
using InstrType = Instruction;
|
||||
static auto constexpr STOP = Instruction::STOP;
|
||||
static auto constexpr ADD = Instruction::ADD;
|
||||
static auto constexpr SUB = Instruction::SUB;
|
||||
static auto constexpr MUL = Instruction::MUL;
|
||||
static auto constexpr DIV = Instruction::DIV;
|
||||
static auto constexpr SDIV = Instruction::SDIV;
|
||||
static auto constexpr MOD = Instruction::MOD;
|
||||
static auto constexpr SMOD = Instruction::SMOD;
|
||||
static auto constexpr EXP = Instruction::EXP;
|
||||
static auto constexpr NOT = Instruction::NOT;
|
||||
static auto constexpr LT = Instruction::LT;
|
||||
static auto constexpr GT = Instruction::GT;
|
||||
static auto constexpr SLT = Instruction::SLT;
|
||||
static auto constexpr SGT = Instruction::SGT;
|
||||
static auto constexpr EQ = Instruction::EQ;
|
||||
static auto constexpr ISZERO = Instruction::ISZERO;
|
||||
static auto constexpr AND = Instruction::AND;
|
||||
static auto constexpr OR = Instruction::OR;
|
||||
static auto constexpr XOR = Instruction::XOR;
|
||||
static auto constexpr BYTE = Instruction::BYTE;
|
||||
static auto constexpr SHL = Instruction::SHL;
|
||||
static auto constexpr SHR = Instruction::SHR;
|
||||
static auto constexpr SAR = Instruction::SAR;
|
||||
static auto constexpr ADDMOD = Instruction::ADDMOD;
|
||||
static auto constexpr MULMOD = Instruction::MULMOD;
|
||||
static auto constexpr SIGNEXTEND = Instruction::SIGNEXTEND;
|
||||
static auto constexpr KECCAK256 = Instruction::KECCAK256;
|
||||
static auto constexpr ADDRESS = Instruction::ADDRESS;
|
||||
static auto constexpr BALANCE = Instruction::BALANCE;
|
||||
static auto constexpr ORIGIN = Instruction::ORIGIN;
|
||||
static auto constexpr CALLER = Instruction::CALLER;
|
||||
static auto constexpr CALLVALUE = Instruction::CALLVALUE;
|
||||
static auto constexpr CALLDATALOAD = Instruction::CALLDATALOAD;
|
||||
static auto constexpr CALLDATASIZE = Instruction::CALLDATASIZE;
|
||||
static auto constexpr CALLDATACOPY = Instruction::CALLDATACOPY;
|
||||
static auto constexpr CODESIZE = Instruction::CODESIZE;
|
||||
static auto constexpr CODECOPY = Instruction::CODECOPY;
|
||||
static auto constexpr GASPRICE = Instruction::GASPRICE;
|
||||
static auto constexpr EXTCODESIZE = Instruction::EXTCODESIZE;
|
||||
static auto constexpr EXTCODECOPY = Instruction::EXTCODECOPY;
|
||||
static auto constexpr RETURNDATASIZE = Instruction::RETURNDATASIZE;
|
||||
static auto constexpr RETURNDATACOPY = Instruction::RETURNDATACOPY;
|
||||
static auto constexpr EXTCODEHASH = Instruction::EXTCODEHASH;
|
||||
static auto constexpr BLOCKHASH = Instruction::BLOCKHASH;
|
||||
static auto constexpr COINBASE = Instruction::COINBASE;
|
||||
static auto constexpr TIMESTAMP = Instruction::TIMESTAMP;
|
||||
static auto constexpr NUMBER = Instruction::NUMBER;
|
||||
static auto constexpr DIFFICULTY = Instruction::DIFFICULTY;
|
||||
static auto constexpr GASLIMIT = Instruction::GASLIMIT;
|
||||
static auto constexpr CHAINID = Instruction::CHAINID;
|
||||
static auto constexpr SELFBALANCE = Instruction::SELFBALANCE;
|
||||
static auto constexpr POP = Instruction::POP;
|
||||
static auto constexpr MLOAD = Instruction::MLOAD;
|
||||
static auto constexpr MSTORE = Instruction::MSTORE;
|
||||
static auto constexpr MSTORE8 = Instruction::MSTORE8;
|
||||
static auto constexpr SLOAD = Instruction::SLOAD;
|
||||
static auto constexpr SSTORE = Instruction::SSTORE;
|
||||
static auto constexpr PC = Instruction::PC;
|
||||
static auto constexpr MSIZE = Instruction::MSIZE;
|
||||
static auto constexpr GAS = Instruction::GAS;
|
||||
static auto constexpr LOG0 = Instruction::LOG0;
|
||||
static auto constexpr LOG1 = Instruction::LOG1;
|
||||
static auto constexpr LOG2 = Instruction::LOG2;
|
||||
static auto constexpr LOG3 = Instruction::LOG3;
|
||||
static auto constexpr LOG4 = Instruction::LOG4;
|
||||
static auto constexpr CREATE = Instruction::CREATE;
|
||||
static auto constexpr CALL = Instruction::CALL;
|
||||
static auto constexpr CALLCODE = Instruction::CALLCODE;
|
||||
static auto constexpr STATICCALL = Instruction::STATICCALL;
|
||||
static auto constexpr RETURN = Instruction::RETURN;
|
||||
static auto constexpr DELEGATECALL = Instruction::DELEGATECALL;
|
||||
static auto constexpr CREATE2 = Instruction::CREATE2;
|
||||
static auto constexpr REVERT = Instruction::REVERT;
|
||||
static auto constexpr INVALID = Instruction::INVALID;
|
||||
static auto constexpr SELFDESTRUCT = Instruction::SELFDESTRUCT;
|
||||
|
||||
template<Instruction inst>
|
||||
struct PatternGenerator
|
||||
{
|
||||
template<typename... Args> constexpr Pattern operator()(Args&&... _args) const
|
||||
{
|
||||
return {inst, {std::forward<Args>(_args)...}};
|
||||
};
|
||||
};
|
||||
|
||||
struct PatternGeneratorInstance
|
||||
{
|
||||
Instruction instruction;
|
||||
template<typename... Args> constexpr Pattern operator()(Args&&... _args) const
|
||||
{
|
||||
return {instruction, {std::forward<Args>(_args)...}};
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
static auto constexpr STOP = PatternGenerator<Instruction::STOP>{};
|
||||
static auto constexpr ADD = PatternGenerator<Instruction::ADD>{};
|
||||
static auto constexpr SUB = PatternGenerator<Instruction::SUB>{};
|
||||
static auto constexpr MUL = PatternGenerator<Instruction::MUL>{};
|
||||
static auto constexpr DIV = PatternGenerator<Instruction::DIV>{};
|
||||
static auto constexpr SDIV = PatternGenerator<Instruction::SDIV>{};
|
||||
static auto constexpr MOD = PatternGenerator<Instruction::MOD>{};
|
||||
static auto constexpr SMOD = PatternGenerator<Instruction::SMOD>{};
|
||||
static auto constexpr EXP = PatternGenerator<Instruction::EXP>{};
|
||||
static auto constexpr NOT = PatternGenerator<Instruction::NOT>{};
|
||||
static auto constexpr LT = PatternGenerator<Instruction::LT>{};
|
||||
static auto constexpr GT = PatternGenerator<Instruction::GT>{};
|
||||
static auto constexpr SLT = PatternGenerator<Instruction::SLT>{};
|
||||
static auto constexpr SGT = PatternGenerator<Instruction::SGT>{};
|
||||
static auto constexpr EQ = PatternGenerator<Instruction::EQ>{};
|
||||
static auto constexpr ISZERO = PatternGenerator<Instruction::ISZERO>{};
|
||||
static auto constexpr AND = PatternGenerator<Instruction::AND>{};
|
||||
static auto constexpr OR = PatternGenerator<Instruction::OR>{};
|
||||
static auto constexpr XOR = PatternGenerator<Instruction::XOR>{};
|
||||
static auto constexpr BYTE = PatternGenerator<Instruction::BYTE>{};
|
||||
static auto constexpr SHL = PatternGenerator<Instruction::SHL>{};
|
||||
static auto constexpr SHR = PatternGenerator<Instruction::SHR>{};
|
||||
static auto constexpr SAR = PatternGenerator<Instruction::SAR>{};
|
||||
static auto constexpr ADDMOD = PatternGenerator<Instruction::ADDMOD>{};
|
||||
static auto constexpr MULMOD = PatternGenerator<Instruction::MULMOD>{};
|
||||
static auto constexpr SIGNEXTEND = PatternGenerator<Instruction::SIGNEXTEND>{};
|
||||
static auto constexpr KECCAK256 = PatternGenerator<Instruction::KECCAK256>{};
|
||||
static auto constexpr ADDRESS = PatternGenerator<Instruction::ADDRESS>{};
|
||||
static auto constexpr BALANCE = PatternGenerator<Instruction::BALANCE>{};
|
||||
static auto constexpr ORIGIN = PatternGenerator<Instruction::ORIGIN>{};
|
||||
static auto constexpr CALLER = PatternGenerator<Instruction::CALLER>{};
|
||||
static auto constexpr CALLVALUE = PatternGenerator<Instruction::CALLVALUE>{};
|
||||
static auto constexpr CALLDATALOAD = PatternGenerator<Instruction::CALLDATALOAD>{};
|
||||
static auto constexpr CALLDATASIZE = PatternGenerator<Instruction::CALLDATASIZE>{};
|
||||
static auto constexpr CALLDATACOPY = PatternGenerator<Instruction::CALLDATACOPY>{};
|
||||
static auto constexpr CODESIZE = PatternGenerator<Instruction::CODESIZE>{};
|
||||
static auto constexpr CODECOPY = PatternGenerator<Instruction::CODECOPY>{};
|
||||
static auto constexpr GASPRICE = PatternGenerator<Instruction::GASPRICE>{};
|
||||
static auto constexpr EXTCODESIZE = PatternGenerator<Instruction::EXTCODESIZE>{};
|
||||
static auto constexpr EXTCODECOPY = PatternGenerator<Instruction::EXTCODECOPY>{};
|
||||
static auto constexpr RETURNDATASIZE = PatternGenerator<Instruction::RETURNDATASIZE>{};
|
||||
static auto constexpr RETURNDATACOPY = PatternGenerator<Instruction::RETURNDATACOPY>{};
|
||||
static auto constexpr EXTCODEHASH = PatternGenerator<Instruction::EXTCODEHASH>{};
|
||||
static auto constexpr BLOCKHASH = PatternGenerator<Instruction::BLOCKHASH>{};
|
||||
static auto constexpr COINBASE = PatternGenerator<Instruction::COINBASE>{};
|
||||
static auto constexpr TIMESTAMP = PatternGenerator<Instruction::TIMESTAMP>{};
|
||||
static auto constexpr NUMBER = PatternGenerator<Instruction::NUMBER>{};
|
||||
static auto constexpr DIFFICULTY = PatternGenerator<Instruction::DIFFICULTY>{};
|
||||
static auto constexpr GASLIMIT = PatternGenerator<Instruction::GASLIMIT>{};
|
||||
static auto constexpr CHAINID = PatternGenerator<Instruction::CHAINID>{};
|
||||
static auto constexpr SELFBALANCE = PatternGenerator<Instruction::SELFBALANCE>{};
|
||||
static auto constexpr POP = PatternGenerator<Instruction::POP>{};
|
||||
static auto constexpr MLOAD = PatternGenerator<Instruction::MLOAD>{};
|
||||
static auto constexpr MSTORE = PatternGenerator<Instruction::MSTORE>{};
|
||||
static auto constexpr MSTORE8 = PatternGenerator<Instruction::MSTORE8>{};
|
||||
static auto constexpr SLOAD = PatternGenerator<Instruction::SLOAD>{};
|
||||
static auto constexpr SSTORE = PatternGenerator<Instruction::SSTORE>{};
|
||||
static auto constexpr PC = PatternGenerator<Instruction::PC>{};
|
||||
static auto constexpr MSIZE = PatternGenerator<Instruction::MSIZE>{};
|
||||
static auto constexpr GAS = PatternGenerator<Instruction::GAS>{};
|
||||
static auto constexpr LOG0 = PatternGenerator<Instruction::LOG0>{};
|
||||
static auto constexpr LOG1 = PatternGenerator<Instruction::LOG1>{};
|
||||
static auto constexpr LOG2 = PatternGenerator<Instruction::LOG2>{};
|
||||
static auto constexpr LOG3 = PatternGenerator<Instruction::LOG3>{};
|
||||
static auto constexpr LOG4 = PatternGenerator<Instruction::LOG4>{};
|
||||
static auto constexpr CREATE = PatternGenerator<Instruction::CREATE>{};
|
||||
static auto constexpr CALL = PatternGenerator<Instruction::CALL>{};
|
||||
static auto constexpr CALLCODE = PatternGenerator<Instruction::CALLCODE>{};
|
||||
static auto constexpr STATICCALL = PatternGenerator<Instruction::STATICCALL>{};
|
||||
static auto constexpr RETURN = PatternGenerator<Instruction::RETURN>{};
|
||||
static auto constexpr DELEGATECALL = PatternGenerator<Instruction::DELEGATECALL>{};
|
||||
static auto constexpr CREATE2 = PatternGenerator<Instruction::CREATE2>{};
|
||||
static auto constexpr REVERT = PatternGenerator<Instruction::REVERT>{};
|
||||
static auto constexpr INVALID = PatternGenerator<Instruction::INVALID>{};
|
||||
static auto constexpr SELFDESTRUCT = PatternGenerator<Instruction::SELFDESTRUCT>{};
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -99,7 +99,7 @@ Rules::Rules()
|
||||
assertThrow(isInitialized(), OptimizerException, "Rule list not properly initialized.");
|
||||
}
|
||||
|
||||
Pattern::Pattern(Instruction _instruction, std::vector<Pattern> const& _arguments):
|
||||
Pattern::Pattern(Instruction _instruction, std::initializer_list<Pattern> _arguments):
|
||||
m_type(Operation),
|
||||
m_instruction(_instruction),
|
||||
m_arguments(_arguments)
|
||||
|
@ -26,6 +26,8 @@
|
||||
#include <libevmasm/ExpressionClasses.h>
|
||||
#include <libevmasm/SimplificationRule.h>
|
||||
|
||||
#include <libdevcore/CommonData.h>
|
||||
|
||||
#include <boost/noncopyable.hpp>
|
||||
|
||||
#include <functional>
|
||||
@ -87,18 +89,20 @@ public:
|
||||
using Expression = ExpressionClasses::Expression;
|
||||
using Id = ExpressionClasses::Id;
|
||||
|
||||
using Builtins = dev::eth::EVMBuiltins;
|
||||
using Builtins = dev::eth::EVMBuiltins<Pattern>;
|
||||
static constexpr size_t WordSize = 256;
|
||||
using Word = u256;
|
||||
|
||||
// Matches a specific constant value.
|
||||
Pattern(unsigned _value): Pattern(u256(_value)) {}
|
||||
Pattern(int _value): Pattern(u256(_value)) {}
|
||||
Pattern(long unsigned _value): Pattern(u256(_value)) {}
|
||||
// Matches a specific constant value.
|
||||
Pattern(u256 const& _value): m_type(Push), m_requireDataMatch(true), m_data(std::make_shared<u256>(_value)) {}
|
||||
// Matches a specific assembly item type or anything if not given.
|
||||
Pattern(AssemblyItemType _type = UndefinedItem): m_type(_type) {}
|
||||
// Matches a given instruction with given arguments
|
||||
Pattern(Instruction _instruction, std::vector<Pattern> const& _arguments = {});
|
||||
Pattern(Instruction _instruction, std::initializer_list<Pattern> _arguments = {});
|
||||
/// Sets this pattern to be part of the match group with the identifier @a _group.
|
||||
/// Inside one rule, all patterns in the same match group have to match expressions from the
|
||||
/// same expression equivalence class.
|
||||
|
@ -113,7 +113,7 @@ SimplificationRules::SimplificationRules()
|
||||
assertThrow(isInitialized(), OptimizerException, "Rule list not properly initialized.");
|
||||
}
|
||||
|
||||
yul::Pattern::Pattern(dev::eth::Instruction _instruction, vector<Pattern> const& _arguments):
|
||||
yul::Pattern::Pattern(dev::eth::Instruction _instruction, initializer_list<Pattern> _arguments):
|
||||
m_kind(PatternKind::Operation),
|
||||
m_instruction(_instruction),
|
||||
m_arguments(_arguments)
|
||||
|
@ -25,6 +25,8 @@
|
||||
#include <libyul/AsmDataForward.h>
|
||||
#include <libyul/AsmData.h>
|
||||
|
||||
#include <libdevcore/CommonData.h>
|
||||
|
||||
#include <boost/noncopyable.hpp>
|
||||
|
||||
#include <functional>
|
||||
@ -85,7 +87,7 @@ enum class PatternKind
|
||||
class Pattern
|
||||
{
|
||||
public:
|
||||
using Builtins = dev::eth::EVMBuiltins;
|
||||
using Builtins = dev::eth::EVMBuiltins<Pattern>;
|
||||
static constexpr size_t WordSize = 256;
|
||||
using Word = dev::u256;
|
||||
|
||||
@ -93,10 +95,12 @@ public:
|
||||
Pattern(PatternKind _kind = PatternKind::Any): m_kind(_kind) {}
|
||||
// Matches a specific constant value.
|
||||
Pattern(unsigned _value): Pattern(dev::u256(_value)) {}
|
||||
Pattern(int _value): Pattern(dev::u256(_value)) {}
|
||||
Pattern(long unsigned _value): Pattern(dev::u256(_value)) {}
|
||||
// Matches a specific constant value.
|
||||
Pattern(dev::u256 const& _value): m_kind(PatternKind::Constant), m_data(std::make_shared<dev::u256>(_value)) {}
|
||||
// Matches a given instruction with given arguments
|
||||
Pattern(dev::eth::Instruction _instruction, std::vector<Pattern> const& _arguments = {});
|
||||
Pattern(dev::eth::Instruction _instruction, std::initializer_list<Pattern> _arguments = {});
|
||||
/// Sets this pattern to be part of the match group with the identifier @a _group.
|
||||
/// Inside one rule, all patterns in the same match group have to match expressions from the
|
||||
/// same expression equivalence class.
|
||||
|
Loading…
Reference in New Issue
Block a user