From fa02667075070300c80cef429c46d75b557c6302 Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 6 Nov 2019 22:12:40 +0100 Subject: [PATCH 1/2] Generalize instructions. --- libevmasm/RuleList.h | 332 ++++++++++++------------- libevmasm/SimplificationRule.h | 81 ++++++ libevmasm/SimplificationRules.h | 4 + libyul/optimiser/SimplificationRules.h | 4 + 4 files changed, 255 insertions(+), 166 deletions(-) diff --git a/libevmasm/RuleList.h b/libevmasm/RuleList.h index 58e773f30..c91b140af 100644 --- a/libevmasm/RuleList.h +++ b/libevmasm/RuleList.h @@ -68,40 +68,40 @@ std::vector> simplificationRuleListPart1( { return std::vector> { // arithmetic on constants - {{Instruction::ADD, {A, B}}, [=]{ return A.d() + B.d(); }, false}, - {{Instruction::MUL, {A, B}}, [=]{ return A.d() * B.d(); }, false}, - {{Instruction::SUB, {A, B}}, [=]{ return A.d() - B.d(); }, false}, - {{Instruction::DIV, {A, B}}, [=]{ return B.d() == 0 ? 0 : divWorkaround(A.d(), B.d()); }, false}, - {{Instruction::SDIV, {A, B}}, [=]{ return B.d() == 0 ? 0 : s2u(divWorkaround(u2s(A.d()), u2s(B.d()))); }, false}, - {{Instruction::MOD, {A, B}}, [=]{ return B.d() == 0 ? 0 : modWorkaround(A.d(), B.d()); }, false}, - {{Instruction::SMOD, {A, B}}, [=]{ return B.d() == 0 ? 0 : s2u(modWorkaround(u2s(A.d()), u2s(B.d()))); }, false}, - {{Instruction::EXP, {A, B}}, [=]{ return u256(boost::multiprecision::powm(bigint(A.d()), bigint(B.d()), bigint(1) << 256)); }, false}, - {{Instruction::NOT, {A}}, [=]{ return ~A.d(); }, false}, - {{Instruction::LT, {A, B}}, [=]() -> u256 { return A.d() < B.d() ? 1 : 0; }, false}, - {{Instruction::GT, {A, B}}, [=]() -> u256 { return A.d() > B.d() ? 1 : 0; }, false}, - {{Instruction::SLT, {A, B}}, [=]() -> u256 { return u2s(A.d()) < u2s(B.d()) ? 1 : 0; }, false}, - {{Instruction::SGT, {A, B}}, [=]() -> u256 { return u2s(A.d()) > u2s(B.d()) ? 1 : 0; }, false}, - {{Instruction::EQ, {A, B}}, [=]() -> u256 { return A.d() == B.d() ? 1 : 0; }, false}, - {{Instruction::ISZERO, {A}}, [=]() -> u256 { return A.d() == 0 ? 1 : 0; }, false}, - {{Instruction::AND, {A, B}}, [=]{ return A.d() & B.d(); }, false}, - {{Instruction::OR, {A, B}}, [=]{ return A.d() | B.d(); }, false}, - {{Instruction::XOR, {A, B}}, [=]{ return A.d() ^ B.d(); }, false}, - {{Instruction::BYTE, {A, B}}, [=]{ return A.d() >= 32 ? 0 : (B.d() >> unsigned(8 * (31 - A.d()))) & 0xff; }, false}, - {{Instruction::ADDMOD, {A, B, C}}, [=]{ return C.d() == 0 ? 0 : u256((bigint(A.d()) + bigint(B.d())) % C.d()); }, false}, - {{Instruction::MULMOD, {A, B, C}}, [=]{ return C.d() == 0 ? 0 : u256((bigint(A.d()) * bigint(B.d())) % C.d()); }, false}, - {{Instruction::SIGNEXTEND, {A, B}}, [=]() -> u256 { + {{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 u256(boost::multiprecision::powm(bigint(A.d()), bigint(B.d()), bigint(1) << 256)); }, false}, + {{Pattern::Builtins::NOT, {A}}, [=]{ return ~A.d(); }, false}, + {{Pattern::Builtins::LT, {A, B}}, [=]() -> u256 { return A.d() < B.d() ? 1 : 0; }, false}, + {{Pattern::Builtins::GT, {A, B}}, [=]() -> u256 { return A.d() > B.d() ? 1 : 0; }, false}, + {{Pattern::Builtins::SLT, {A, B}}, [=]() -> u256 { return u2s(A.d()) < u2s(B.d()) ? 1 : 0; }, false}, + {{Pattern::Builtins::SGT, {A, B}}, [=]() -> u256 { return u2s(A.d()) > u2s(B.d()) ? 1 : 0; }, false}, + {{Pattern::Builtins::EQ, {A, B}}, [=]() -> u256 { return A.d() == B.d() ? 1 : 0; }, false}, + {{Pattern::Builtins::ISZERO, {A}}, [=]() -> u256 { 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}}, [=]{ return A.d() >= 32 ? 0 : (B.d() >> unsigned(8 * (31 - A.d()))) & 0xff; }, false}, + {{Pattern::Builtins::ADDMOD, {A, B, C}}, [=]{ return C.d() == 0 ? 0 : u256((bigint(A.d()) + bigint(B.d())) % C.d()); }, false}, + {{Pattern::Builtins::MULMOD, {A, B, C}}, [=]{ return C.d() == 0 ? 0 : u256((bigint(A.d()) * bigint(B.d())) % C.d()); }, false}, + {{Pattern::Builtins::SIGNEXTEND, {A, B}}, [=]() -> u256 { if (A.d() >= 31) return B.d(); unsigned testBit = unsigned(A.d()) * 8 + 7; u256 mask = (u256(1) << testBit) - 1; return boost::multiprecision::bit_test(B.d(), testBit) ? B.d() | ~mask : B.d() & mask; }, false}, - {{Instruction::SHL, {A, B}}, [=]{ + {{Pattern::Builtins::SHL, {A, B}}, [=]{ if (A.d() > 255) return u256(0); return shlWorkaround(B.d(), unsigned(A.d())); }, false}, - {{Instruction::SHR, {A, B}}, [=]{ + {{Pattern::Builtins::SHR, {A, B}}, [=]{ if (A.d() > 255) return u256(0); return B.d() >> unsigned(A.d()); @@ -120,48 +120,48 @@ std::vector> simplificationRuleListPart2( { return std::vector> { // invariants involving known constants - {{Instruction::ADD, {X, 0}}, [=]{ return X; }, false}, - {{Instruction::ADD, {0, X}}, [=]{ return X; }, false}, - {{Instruction::SUB, {X, 0}}, [=]{ return X; }, false}, - {{Instruction::SUB, {~u256(0), X}}, [=]() -> Pattern { return {Instruction::NOT, {X}}; }, false}, - {{Instruction::MUL, {X, 0}}, [=]{ return u256(0); }, true}, - {{Instruction::MUL, {0, X}}, [=]{ return u256(0); }, true}, - {{Instruction::MUL, {X, 1}}, [=]{ return X; }, false}, - {{Instruction::MUL, {1, X}}, [=]{ return X; }, false}, - {{Instruction::MUL, {X, u256(-1)}}, [=]() -> Pattern { return {Instruction::SUB, {0, X}}; }, false}, - {{Instruction::MUL, {u256(-1), X}}, [=]() -> Pattern { return {Instruction::SUB, {0, X}}; }, false}, - {{Instruction::DIV, {X, 0}}, [=]{ return u256(0); }, true}, - {{Instruction::DIV, {0, X}}, [=]{ return u256(0); }, true}, - {{Instruction::DIV, {X, 1}}, [=]{ return X; }, false}, - {{Instruction::SDIV, {X, 0}}, [=]{ return u256(0); }, true}, - {{Instruction::SDIV, {0, X}}, [=]{ return u256(0); }, true}, - {{Instruction::SDIV, {X, 1}}, [=]{ return X; }, false}, - {{Instruction::AND, {X, ~u256(0)}}, [=]{ return X; }, false}, - {{Instruction::AND, {~u256(0), X}}, [=]{ return X; }, false}, - {{Instruction::AND, {X, 0}}, [=]{ return u256(0); }, true}, - {{Instruction::AND, {0, X}}, [=]{ return u256(0); }, true}, - {{Instruction::OR, {X, 0}}, [=]{ return X; }, false}, - {{Instruction::OR, {0, X}}, [=]{ return X; }, false}, - {{Instruction::OR, {X, ~u256(0)}}, [=]{ return ~u256(0); }, true}, - {{Instruction::OR, {~u256(0), X}}, [=]{ return ~u256(0); }, true}, - {{Instruction::XOR, {X, 0}}, [=]{ return X; }, false}, - {{Instruction::XOR, {0, X}}, [=]{ return X; }, false}, - {{Instruction::MOD, {X, 0}}, [=]{ return u256(0); }, true}, - {{Instruction::MOD, {0, X}}, [=]{ return u256(0); }, true}, - {{Instruction::EQ, {X, 0}}, [=]() -> Pattern { return {Instruction::ISZERO, {X}}; }, false }, - {{Instruction::EQ, {0, X}}, [=]() -> Pattern { return {Instruction::ISZERO, {X}}; }, false }, - {{Instruction::SHL, {0, X}}, [=]{ return X; }, false}, - {{Instruction::SHR, {0, X}}, [=]{ return X; }, false}, - {{Instruction::SHL, {X, 0}}, [=]{ return u256(0); }, true}, - {{Instruction::SHR, {X, 0}}, [=]{ return u256(0); }, true}, - {{Instruction::GT, {X, 0}}, [=]() -> Pattern { return {Instruction::ISZERO, {{Instruction::ISZERO, {X}}}}; }, false}, - {{Instruction::LT, {0, X}}, [=]() -> Pattern { return {Instruction::ISZERO, {{Instruction::ISZERO, {X}}}}; }, false}, - {{Instruction::GT, {X, ~u256(0)}}, [=]{ return u256(0); }, true}, - {{Instruction::LT, {~u256(0), X}}, [=]{ return u256(0); }, true}, - {{Instruction::GT, {0, X}}, [=]{ return u256(0); }, true}, - {{Instruction::LT, {X, 0}}, [=]{ return u256(0); }, true}, - {{Instruction::AND, {{Instruction::BYTE, {X, Y}}, {u256(0xff)}}}, [=]() -> Pattern { return {Instruction::BYTE, {X, Y}}; }, false}, - {{Instruction::BYTE, {31, X}}, [=]() -> Pattern { return {Instruction::AND, {X, u256(0xff)}}; }, false} + {{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, {~u256(0), X}}, [=]() -> Pattern { return {Pattern::Builtins::NOT, {X}}; }, false}, + {{Pattern::Builtins::MUL, {X, 0}}, [=]{ return u256(0); }, true}, + {{Pattern::Builtins::MUL, {0, X}}, [=]{ return u256(0); }, true}, + {{Pattern::Builtins::MUL, {X, 1}}, [=]{ return X; }, false}, + {{Pattern::Builtins::MUL, {1, X}}, [=]{ return X; }, false}, + {{Pattern::Builtins::MUL, {X, u256(-1)}}, [=]() -> Pattern { return {Pattern::Builtins::SUB, {0, X}}; }, false}, + {{Pattern::Builtins::MUL, {u256(-1), X}}, [=]() -> Pattern { return {Pattern::Builtins::SUB, {0, X}}; }, false}, + {{Pattern::Builtins::DIV, {X, 0}}, [=]{ return u256(0); }, true}, + {{Pattern::Builtins::DIV, {0, X}}, [=]{ return u256(0); }, true}, + {{Pattern::Builtins::DIV, {X, 1}}, [=]{ return X; }, false}, + {{Pattern::Builtins::SDIV, {X, 0}}, [=]{ return u256(0); }, true}, + {{Pattern::Builtins::SDIV, {0, X}}, [=]{ return u256(0); }, true}, + {{Pattern::Builtins::SDIV, {X, 1}}, [=]{ return X; }, false}, + {{Pattern::Builtins::AND, {X, ~u256(0)}}, [=]{ return X; }, false}, + {{Pattern::Builtins::AND, {~u256(0), X}}, [=]{ return X; }, false}, + {{Pattern::Builtins::AND, {X, 0}}, [=]{ return u256(0); }, true}, + {{Pattern::Builtins::AND, {0, X}}, [=]{ return u256(0); }, true}, + {{Pattern::Builtins::OR, {X, 0}}, [=]{ return X; }, false}, + {{Pattern::Builtins::OR, {0, X}}, [=]{ return X; }, false}, + {{Pattern::Builtins::OR, {X, ~u256(0)}}, [=]{ return ~u256(0); }, true}, + {{Pattern::Builtins::OR, {~u256(0), X}}, [=]{ return ~u256(0); }, true}, + {{Pattern::Builtins::XOR, {X, 0}}, [=]{ return X; }, false}, + {{Pattern::Builtins::XOR, {0, X}}, [=]{ return X; }, false}, + {{Pattern::Builtins::MOD, {X, 0}}, [=]{ return u256(0); }, true}, + {{Pattern::Builtins::MOD, {0, X}}, [=]{ return u256(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 u256(0); }, true}, + {{Pattern::Builtins::SHR, {X, 0}}, [=]{ return u256(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, ~u256(0)}}, [=]{ return u256(0); }, true}, + {{Pattern::Builtins::LT, {~u256(0), X}}, [=]{ return u256(0); }, true}, + {{Pattern::Builtins::GT, {0, X}}, [=]{ return u256(0); }, true}, + {{Pattern::Builtins::LT, {X, 0}}, [=]{ return u256(0); }, true}, + {{Pattern::Builtins::AND, {{Pattern::Builtins::BYTE, {X, Y}}, {u256(0xff)}}}, [=]() -> Pattern { return {Pattern::Builtins::BYTE, {X, Y}}; }, false}, + {{Pattern::Builtins::BYTE, {31, X}}, [=]() -> Pattern { return {Pattern::Builtins::AND, {X, u256(0xff)}}; }, false} }; } @@ -176,16 +176,16 @@ std::vector> simplificationRuleListPart3( { return std::vector> { // operations involving an expression and itself - {{Instruction::AND, {X, X}}, [=]{ return X; }, true}, - {{Instruction::OR, {X, X}}, [=]{ return X; }, true}, - {{Instruction::XOR, {X, X}}, [=]{ return u256(0); }, true}, - {{Instruction::SUB, {X, X}}, [=]{ return u256(0); }, true}, - {{Instruction::EQ, {X, X}}, [=]{ return u256(1); }, true}, - {{Instruction::LT, {X, X}}, [=]{ return u256(0); }, true}, - {{Instruction::SLT, {X, X}}, [=]{ return u256(0); }, true}, - {{Instruction::GT, {X, X}}, [=]{ return u256(0); }, true}, - {{Instruction::SGT, {X, X}}, [=]{ return u256(0); }, true}, - {{Instruction::MOD, {X, X}}, [=]{ return u256(0); }, true} + {{Pattern::Builtins::AND, {X, X}}, [=]{ return X; }, true}, + {{Pattern::Builtins::OR, {X, X}}, [=]{ return X; }, true}, + {{Pattern::Builtins::XOR, {X, X}}, [=]{ return u256(0); }, true}, + {{Pattern::Builtins::SUB, {X, X}}, [=]{ return u256(0); }, true}, + {{Pattern::Builtins::EQ, {X, X}}, [=]{ return u256(1); }, true}, + {{Pattern::Builtins::LT, {X, X}}, [=]{ return u256(0); }, true}, + {{Pattern::Builtins::SLT, {X, X}}, [=]{ return u256(0); }, true}, + {{Pattern::Builtins::GT, {X, X}}, [=]{ return u256(0); }, true}, + {{Pattern::Builtins::SGT, {X, X}}, [=]{ return u256(0); }, true}, + {{Pattern::Builtins::MOD, {X, X}}, [=]{ return u256(0); }, true} }; } @@ -200,23 +200,23 @@ std::vector> simplificationRuleListPart4( { return std::vector> { // logical instruction combinations - {{Instruction::NOT, {{Instruction::NOT, {X}}}}, [=]{ return X; }, false}, - {{Instruction::XOR, {X, {Instruction::XOR, {X, Y}}}}, [=]{ return Y; }, true}, - {{Instruction::XOR, {X, {Instruction::XOR, {Y, X}}}}, [=]{ return Y; }, true}, - {{Instruction::XOR, {{Instruction::XOR, {X, Y}}, X}}, [=]{ return Y; }, true}, - {{Instruction::XOR, {{Instruction::XOR, {Y, X}}, X}}, [=]{ return Y; }, true}, - {{Instruction::OR, {X, {Instruction::AND, {X, Y}}}}, [=]{ return X; }, true}, - {{Instruction::OR, {X, {Instruction::AND, {Y, X}}}}, [=]{ return X; }, true}, - {{Instruction::OR, {{Instruction::AND, {X, Y}}, X}}, [=]{ return X; }, true}, - {{Instruction::OR, {{Instruction::AND, {Y, X}}, X}}, [=]{ return X; }, true}, - {{Instruction::AND, {X, {Instruction::OR, {X, Y}}}}, [=]{ return X; }, true}, - {{Instruction::AND, {X, {Instruction::OR, {Y, X}}}}, [=]{ return X; }, true}, - {{Instruction::AND, {{Instruction::OR, {X, Y}}, X}}, [=]{ return X; }, true}, - {{Instruction::AND, {{Instruction::OR, {Y, X}}, X}}, [=]{ return X; }, true}, - {{Instruction::AND, {X, {Instruction::NOT, {X}}}}, [=]{ return u256(0); }, true}, - {{Instruction::AND, {{Instruction::NOT, {X}}, X}}, [=]{ return u256(0); }, true}, - {{Instruction::OR, {X, {Instruction::NOT, {X}}}}, [=]{ return ~u256(0); }, true}, - {{Instruction::OR, {{Instruction::NOT, {X}}, X}}, [=]{ return ~u256(0); }, true}, + {{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 u256(0); }, true}, + {{Pattern::Builtins::AND, {{Pattern::Builtins::NOT, {X}}, X}}, [=]{ return u256(0); }, true}, + {{Pattern::Builtins::OR, {X, {Pattern::Builtins::NOT, {X}}}}, [=]{ return ~u256(0); }, true}, + {{Pattern::Builtins::OR, {{Pattern::Builtins::NOT, {X}}, X}}, [=]{ return ~u256(0); }, true}, }; } @@ -237,15 +237,15 @@ std::vector> simplificationRuleListPart5( { u256 value = u256(1) << i; rules.push_back({ - {Instruction::MOD, {X, value}}, - [=]() -> Pattern { return {Instruction::AND, {X, value - 1}}; }, + {Pattern::Builtins::MOD, {X, value}}, + [=]() -> Pattern { return {Pattern::Builtins::AND, {X, value - 1}}; }, false }); } // Replace SHL >=256, X with 0 rules.push_back({ - {Instruction::SHL, {A, X}}, + {Pattern::Builtins::SHL, {A, X}}, [=]() -> Pattern { return u256(0); }, true, [=]() { return A.d() >= 256; } @@ -253,7 +253,7 @@ std::vector> simplificationRuleListPart5( // Replace SHR >=256, X with 0 rules.push_back({ - {Instruction::SHR, {A, X}}, + {Pattern::Builtins::SHR, {A, X}}, [=]() -> Pattern { return u256(0); }, true, [=]() { return A.d() >= 256; } @@ -261,27 +261,27 @@ std::vector> simplificationRuleListPart5( // Replace BYTE(A, X), A >= 32 with 0 rules.push_back({ - {Instruction::BYTE, {A, X}}, + {Pattern::Builtins::BYTE, {A, X}}, [=]() -> Pattern { return u256(0); }, true, [=]() { return A.d() >= 32; } }); for (auto const& op: std::vector{ - Instruction::ADDRESS, - Instruction::CALLER, - Instruction::ORIGIN, - Instruction::COINBASE + Pattern::Builtins::ADDRESS, + Pattern::Builtins::CALLER, + Pattern::Builtins::ORIGIN, + Pattern::Builtins::COINBASE }) { u256 const mask = (u256(1) << 160) - 1; rules.push_back({ - {Instruction::AND, {{op, mask}}}, + {Pattern::Builtins::AND, {{op, mask}}}, [=]() -> Pattern { return op; }, false }); rules.push_back({ - {Instruction::AND, {{mask, op}}}, + {Pattern::Builtins::AND, {{mask, op}}}, [=]() -> Pattern { return op; }, false }); @@ -302,27 +302,27 @@ std::vector> simplificationRuleListPart6( std::vector> rules; // Double negation of opcodes with boolean result for (auto const& op: std::vector{ - Instruction::EQ, - Instruction::LT, - Instruction::SLT, - Instruction::GT, - Instruction::SGT + Pattern::Builtins::EQ, + Pattern::Builtins::LT, + Pattern::Builtins::SLT, + Pattern::Builtins::GT, + Pattern::Builtins::SGT }) rules.push_back({ - {Instruction::ISZERO, {{Instruction::ISZERO, {{op, {X, Y}}}}}}, + {Pattern::Builtins::ISZERO, {{Pattern::Builtins::ISZERO, {{op, {X, Y}}}}}}, [=]() -> Pattern { return {op, {X, Y}}; }, false }); rules.push_back({ - {Instruction::ISZERO, {{Instruction::ISZERO, {{Instruction::ISZERO, {X}}}}}}, - [=]() -> Pattern { return {Instruction::ISZERO, {X}}; }, + {Pattern::Builtins::ISZERO, {{Pattern::Builtins::ISZERO, {{Pattern::Builtins::ISZERO, {X}}}}}}, + [=]() -> Pattern { return {Pattern::Builtins::ISZERO, {X}}; }, false }); rules.push_back({ - {Instruction::ISZERO, {{Instruction::XOR, {X, Y}}}}, - [=]() -> Pattern { return { Instruction::EQ, {X, Y} }; }, + {Pattern::Builtins::ISZERO, {{Pattern::Builtins::XOR, {X, Y}}}}, + [=]() -> Pattern { return { Pattern::Builtins::EQ, {X, Y} }; }, false }); @@ -341,11 +341,11 @@ std::vector> simplificationRuleListPart7( std::vector> rules; // Associative operations for (auto const& opFun: std::vector>>{ - {Instruction::ADD, std::plus()}, - {Instruction::MUL, std::multiplies()}, - {Instruction::AND, std::bit_and()}, - {Instruction::OR, std::bit_or()}, - {Instruction::XOR, std::bit_xor()} + {Pattern::Builtins::ADD, std::plus()}, + {Pattern::Builtins::MUL, std::multiplies()}, + {Pattern::Builtins::AND, std::bit_and()}, + {Pattern::Builtins::OR, std::bit_or()}, + {Pattern::Builtins::XOR, std::bit_xor()} }) { auto op = opFun.first; @@ -382,13 +382,13 @@ std::vector> simplificationRuleListPart7( // Combine two SHL by constant rules.push_back({ // SHL(B, SHL(A, X)) -> SHL(min(A+B, 256), X) - {Instruction::SHL, {{B}, {Instruction::SHL, {{A}, {X}}}}}, + {Pattern::Builtins::SHL, {{B}, {Pattern::Builtins::SHL, {{A}, {X}}}}}, [=]() -> Pattern { bigint sum = bigint(A.d()) + B.d(); if (sum >= 256) - return {Instruction::AND, {X, u256(0)}}; + return {Pattern::Builtins::AND, {X, u256(0)}}; else - return {Instruction::SHL, {u256(sum), X}}; + return {Pattern::Builtins::SHL, {u256(sum), X}}; }, false }); @@ -396,13 +396,13 @@ std::vector> simplificationRuleListPart7( // Combine two SHR by constant rules.push_back({ // SHR(B, SHR(A, X)) -> SHR(min(A+B, 256), X) - {Instruction::SHR, {{B}, {Instruction::SHR, {{A}, {X}}}}}, + {Pattern::Builtins::SHR, {{B}, {Pattern::Builtins::SHR, {{A}, {X}}}}}, [=]() -> Pattern { bigint sum = bigint(A.d()) + B.d(); if (sum >= 256) - return {Instruction::AND, {X, u256(0)}}; + return {Pattern::Builtins::AND, {X, u256(0)}}; else - return {Instruction::SHR, {u256(sum), X}}; + return {Pattern::Builtins::SHR, {u256(sum), X}}; }, false }); @@ -410,16 +410,16 @@ std::vector> simplificationRuleListPart7( // Combine SHL-SHR by constant rules.push_back({ // SHR(B, SHL(A, X)) -> AND(SH[L/R]([B - A / A - B], X), Mask) - {Instruction::SHR, {{B}, {Instruction::SHL, {{A}, {X}}}}}, + {Pattern::Builtins::SHR, {{B}, {Pattern::Builtins::SHL, {{A}, {X}}}}}, [=]() -> Pattern { u256 mask = shlWorkaround(u256(-1), unsigned(A.d())) >> unsigned(B.d()); if (A.d() > B.d()) - return {Instruction::AND, {{Instruction::SHL, {A.d() - B.d(), X}}, mask}}; + return {Pattern::Builtins::AND, {{Pattern::Builtins::SHL, {A.d() - B.d(), X}}, mask}}; else if (B.d() > A.d()) - return {Instruction::AND, {{Instruction::SHR, {B.d() - A.d(), X}}, mask}}; + return {Pattern::Builtins::AND, {{Pattern::Builtins::SHR, {B.d() - A.d(), X}}, mask}}; else - return {Instruction::AND, {X, mask}}; + return {Pattern::Builtins::AND, {X, mask}}; }, false, [=] { return A.d() < 256 && B.d() < 256; } @@ -428,41 +428,41 @@ std::vector> simplificationRuleListPart7( // Combine SHR-SHL by constant rules.push_back({ // SHL(B, SHR(A, X)) -> AND(SH[L/R]([B - A / A - B], X), Mask) - {Instruction::SHL, {{B}, {Instruction::SHR, {{A}, {X}}}}}, + {Pattern::Builtins::SHL, {{B}, {Pattern::Builtins::SHR, {{A}, {X}}}}}, [=]() -> Pattern { u256 mask = shlWorkaround(u256(-1) >> unsigned(A.d()), unsigned(B.d())); if (A.d() > B.d()) - return {Instruction::AND, {{Instruction::SHR, {A.d() - B.d(), X}}, mask}}; + return {Pattern::Builtins::AND, {{Pattern::Builtins::SHR, {A.d() - B.d(), X}}, mask}}; else if (B.d() > A.d()) - return {Instruction::AND, {{Instruction::SHL, {B.d() - A.d(), X}}, mask}}; + return {Pattern::Builtins::AND, {{Pattern::Builtins::SHL, {B.d() - A.d(), X}}, mask}}; else - return {Instruction::AND, {X, mask}}; + return {Pattern::Builtins::AND, {X, mask}}; }, false, [=] { return A.d() < 256 && B.d() < 256; } }); // Move AND with constant across SHL and SHR by constant - for (auto shiftOp: {Instruction::SHL, Instruction::SHR}) + for (auto shiftOp: {Pattern::Builtins::SHL, Pattern::Builtins::SHR}) { auto replacement = [=]() -> Pattern { u256 mask = - shiftOp == Instruction::SHL ? + shiftOp == Pattern::Builtins::SHL ? shlWorkaround(A.d(), unsigned(B.d())) : A.d() >> unsigned(B.d()); - return {Instruction::AND, {{shiftOp, {B.d(), X}}, std::move(mask)}}; + return {Pattern::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}, {Instruction::AND, {{X}, {A}}}}}, + {shiftOp, {{B}, {Pattern::Builtins::AND, {{X}, {A}}}}}, replacement, false, [=] { return B.d() < 256; } }); rules.push_back({ // SH[L/R](B, AND(A, X)) -> AND(SH[L/R](B, X), [ A << B / A >> B ]) - {shiftOp, {{B}, {Instruction::AND, {{A}, {X}}}}}, + {shiftOp, {{B}, {Pattern::Builtins::AND, {{A}, {X}}}}}, replacement, false, [=] { return B.d() < 256; } @@ -471,27 +471,27 @@ std::vector> simplificationRuleListPart7( rules.push_back({ // MUL(X, SHL(Y, 1)) -> SHL(Y, X) - {Instruction::MUL, {X, {Instruction::SHL, {Y, u256(1)}}}}, + {Pattern::Builtins::MUL, {X, {Pattern::Builtins::SHL, {Y, u256(1)}}}}, [=]() -> Pattern { - return {Instruction::SHL, {Y, X}}; + return {Pattern::Builtins::SHL, {Y, X}}; }, // Actually only changes the order, does not remove. true }); rules.push_back({ // MUL(SHL(X, 1), Y) -> SHL(X, Y) - {Instruction::MUL, {{Instruction::SHL, {X, u256(1)}}, Y}}, + {Pattern::Builtins::MUL, {{Pattern::Builtins::SHL, {X, u256(1)}}, Y}}, [=]() -> Pattern { - return {Instruction::SHL, {X, Y}}; + return {Pattern::Builtins::SHL, {X, Y}}; }, false }); rules.push_back({ // DIV(X, SHL(Y, 1)) -> SHR(Y, X) - {Instruction::DIV, {X, {Instruction::SHL, {Y, u256(1)}}}}, + {Pattern::Builtins::DIV, {X, {Pattern::Builtins::SHL, {Y, u256(1)}}}}, [=]() -> Pattern { - return {Instruction::SHR, {Y, X}}; + return {Pattern::Builtins::SHR, {Y, X}}; }, // Actually only changes the order, does not remove. true @@ -506,16 +506,16 @@ std::vector> simplificationRuleListPart7( rules.push_back({ // AND(A, SHR(B, X)) -> A & ((2^256-1) >> B) == ((2^256-1) >> B) - {Instruction::AND, {A, {Instruction::SHR, {B, X}}}}, - [=]() -> Pattern { return {Instruction::SHR, {B, X}}; }, + {Pattern::Builtins::AND, {A, {Pattern::Builtins::SHR, {B, X}}}}, + [=]() -> Pattern { return {Pattern::Builtins::SHR, {B, X}}; }, false, feasibilityFunction }); rules.push_back({ // AND(SHR(B, X), A) -> ((2^256-1) >> B) & A == ((2^256-1) >> B) - {Instruction::AND, {{Instruction::SHR, {B, X}}, A}}, - [=]() -> Pattern { return {Instruction::SHR, {B, X}}; }, + {Pattern::Builtins::AND, {{Pattern::Builtins::SHR, {B, X}}, A}}, + [=]() -> Pattern { return {Pattern::Builtins::SHR, {B, X}}; }, false, feasibilityFunction }); @@ -538,28 +538,28 @@ std::vector> simplificationRuleListPart8( rules += std::vector>{ { // X - A -> X + (-A) - {Instruction::SUB, {X, A}}, - [=]() -> Pattern { return {Instruction::ADD, {X, 0 - A.d()}}; }, + {Pattern::Builtins::SUB, {X, A}}, + [=]() -> Pattern { return {Pattern::Builtins::ADD, {X, 0 - A.d()}}; }, false }, { // (X + A) - Y -> (X - Y) + A - {Instruction::SUB, {{Instruction::ADD, {X, A}}, Y}}, - [=]() -> Pattern { return {Instruction::ADD, {{Instruction::SUB, {X, Y}}, A}}; }, + {Pattern::Builtins::SUB, {{Pattern::Builtins::ADD, {X, A}}, Y}}, + [=]() -> Pattern { return {Pattern::Builtins::ADD, {{Pattern::Builtins::SUB, {X, Y}}, A}}; }, false }, { // (A + X) - Y -> (X - Y) + A - {Instruction::SUB, {{Instruction::ADD, {A, X}}, Y}}, - [=]() -> Pattern { return {Instruction::ADD, {{Instruction::SUB, {X, Y}}, A}}; }, + {Pattern::Builtins::SUB, {{Pattern::Builtins::ADD, {A, X}}, Y}}, + [=]() -> Pattern { return {Pattern::Builtins::ADD, {{Pattern::Builtins::SUB, {X, Y}}, A}}; }, false }, { // X - (Y + A) -> (X - Y) + (-A) - {Instruction::SUB, {X, {Instruction::ADD, {Y, A}}}}, - [=]() -> Pattern { return {Instruction::ADD, {{Instruction::SUB, {X, Y}}, 0 - A.d()}}; }, + {Pattern::Builtins::SUB, {X, {Pattern::Builtins::ADD, {Y, A}}}}, + [=]() -> Pattern { return {Pattern::Builtins::ADD, {{Pattern::Builtins::SUB, {X, Y}}, 0 - A.d()}}; }, false }, { // X - (A + Y) -> (X - Y) + (-A) - {Instruction::SUB, {X, {Instruction::ADD, {A, Y}}}}, - [=]() -> Pattern { return {Instruction::ADD, {{Instruction::SUB, {X, Y}}, 0 - A.d()}}; }, + {Pattern::Builtins::SUB, {X, {Pattern::Builtins::ADD, {A, Y}}}}, + [=]() -> Pattern { return {Pattern::Builtins::ADD, {{Pattern::Builtins::SUB, {X, Y}}, 0 - A.d()}}; }, false } }; @@ -582,24 +582,24 @@ std::vector> simplificationRuleListPart9( u256 const mask = (u256(1) << 160) - 1; // CREATE rules.push_back({ - {Instruction::AND, {{Instruction::CREATE, {W, X, Y}}, mask}}, - [=]() -> Pattern { return {Instruction::CREATE, {W, X, Y}}; }, + {Pattern::Builtins::AND, {{Pattern::Builtins::CREATE, {W, X, Y}}, mask}}, + [=]() -> Pattern { return {Pattern::Builtins::CREATE, {W, X, Y}}; }, false }); rules.push_back({ - {Instruction::AND, {{mask, {Instruction::CREATE, {W, X, Y}}}}}, - [=]() -> Pattern { return {Instruction::CREATE, {W, X, Y}}; }, + {Pattern::Builtins::AND, {{mask, {Pattern::Builtins::CREATE, {W, X, Y}}}}}, + [=]() -> Pattern { return {Pattern::Builtins::CREATE, {W, X, Y}}; }, false }); // CREATE2 rules.push_back({ - {Instruction::AND, {{Instruction::CREATE2, {W, X, Y, Z}}, mask}}, - [=]() -> Pattern { return {Instruction::CREATE2, {W, X, Y, Z}}; }, + {Pattern::Builtins::AND, {{Pattern::Builtins::CREATE2, {W, X, Y, Z}}, mask}}, + [=]() -> Pattern { return {Pattern::Builtins::CREATE2, {W, X, Y, Z}}; }, false }); rules.push_back({ - {Instruction::AND, {{mask, {Instruction::CREATE2, {W, X, Y, Z}}}}}, - [=]() -> Pattern { return {Instruction::CREATE2, {W, X, Y, Z}}; }, + {Pattern::Builtins::AND, {{mask, {Pattern::Builtins::CREATE2, {W, X, Y, Z}}}}}, + [=]() -> Pattern { return {Pattern::Builtins::CREATE2, {W, X, Y, Z}}; }, false }); diff --git a/libevmasm/SimplificationRule.h b/libevmasm/SimplificationRule.h index 5c752545f..b35b89551 100644 --- a/libevmasm/SimplificationRule.h +++ b/libevmasm/SimplificationRule.h @@ -20,6 +20,7 @@ #pragma once +#include #include namespace dev @@ -54,5 +55,85 @@ struct SimplificationRule std::function feasible; }; +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; +}; + } } diff --git a/libevmasm/SimplificationRules.h b/libevmasm/SimplificationRules.h index fc45a46c3..a8c782b26 100644 --- a/libevmasm/SimplificationRules.h +++ b/libevmasm/SimplificationRules.h @@ -87,6 +87,10 @@ public: using Expression = ExpressionClasses::Expression; using Id = ExpressionClasses::Id; + using Builtins = dev::eth::EVMBuiltins; + static constexpr size_t WordSize = 256; + using Word = u256; + // Matches a specific constant value. Pattern(unsigned _value): Pattern(u256(_value)) {} // Matches a specific constant value. diff --git a/libyul/optimiser/SimplificationRules.h b/libyul/optimiser/SimplificationRules.h index f9b8c1fe7..1caf323d8 100644 --- a/libyul/optimiser/SimplificationRules.h +++ b/libyul/optimiser/SimplificationRules.h @@ -85,6 +85,10 @@ enum class PatternKind class Pattern { public: + using Builtins = dev::eth::EVMBuiltins; + static constexpr size_t WordSize = 256; + using Word = dev::u256; + /// Matches any expression. Pattern(PatternKind _kind = PatternKind::Any): m_kind(_kind) {} // Matches a specific constant value. From 4b6647f6e1d4938b8f4c77b35e9719d206fb2e3b Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 6 Nov 2019 22:39:13 +0100 Subject: [PATCH 2/2] Generalize word. --- libevmasm/RuleList.h | 201 ++++++++++++++++++++++++------------------- 1 file changed, 112 insertions(+), 89 deletions(-) diff --git a/libevmasm/RuleList.h b/libevmasm/RuleList.h index c91b140af..e67f614ae 100644 --- a/libevmasm/RuleList.h +++ b/libevmasm/RuleList.h @@ -49,7 +49,7 @@ template S modWorkaround(S const& _a, S const& _b) // This works around a bug fixed with Boost 1.64. // https://www.boost.org/doc/libs/1_68_0/libs/multiprecision/doc/html/boost_multiprecision/map/hist.html#boost_multiprecision.map.hist.multiprecision_2_3_1_boost_1_64 -inline u256 shlWorkaround(u256 const& _x, unsigned _amount) +template S shlWorkaround(S const& _x, unsigned _amount) { return u256((bigint(_x) << _amount) & u256(-1)); } @@ -66,6 +66,7 @@ std::vector> simplificationRuleListPart1( Pattern ) { + using Word = typename Pattern::Word; return std::vector> { // arithmetic on constants {{Pattern::Builtins::ADD, {A, B}}, [=]{ return A.d() + B.d(); }, false}, @@ -75,35 +76,40 @@ std::vector> simplificationRuleListPart1( {{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 u256(boost::multiprecision::powm(bigint(A.d()), bigint(B.d()), bigint(1) << 256)); }, 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}}, [=]() -> u256 { return A.d() < B.d() ? 1 : 0; }, false}, - {{Pattern::Builtins::GT, {A, B}}, [=]() -> u256 { return A.d() > B.d() ? 1 : 0; }, false}, - {{Pattern::Builtins::SLT, {A, B}}, [=]() -> u256 { return u2s(A.d()) < u2s(B.d()) ? 1 : 0; }, false}, - {{Pattern::Builtins::SGT, {A, B}}, [=]() -> u256 { return u2s(A.d()) > u2s(B.d()) ? 1 : 0; }, false}, - {{Pattern::Builtins::EQ, {A, B}}, [=]() -> u256 { return A.d() == B.d() ? 1 : 0; }, false}, - {{Pattern::Builtins::ISZERO, {A}}, [=]() -> u256 { return A.d() == 0 ? 1 : 0; }, 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}}, [=]{ return A.d() >= 32 ? 0 : (B.d() >> unsigned(8 * (31 - A.d()))) & 0xff; }, false}, - {{Pattern::Builtins::ADDMOD, {A, B, C}}, [=]{ return C.d() == 0 ? 0 : u256((bigint(A.d()) + bigint(B.d())) % C.d()); }, false}, - {{Pattern::Builtins::MULMOD, {A, B, C}}, [=]{ return C.d() == 0 ? 0 : u256((bigint(A.d()) * bigint(B.d())) % C.d()); }, false}, - {{Pattern::Builtins::SIGNEXTEND, {A, B}}, [=]() -> u256 { - if (A.d() >= 31) + {{Pattern::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 { + if (A.d() >= Pattern::WordSize / 8 - 1) return B.d(); unsigned testBit = unsigned(A.d()) * 8 + 7; - u256 mask = (u256(1) << testBit) - 1; + 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}}, [=]{ - if (A.d() > 255) - return u256(0); + if (A.d() >= Pattern::WordSize) + return Word(0); return shlWorkaround(B.d(), unsigned(A.d())); }, false}, {{Pattern::Builtins::SHR, {A, B}}, [=]{ - if (A.d() > 255) - return u256(0); + if (A.d() >= Pattern::WordSize) + return Word(0); return B.d() >> unsigned(A.d()); }, false} }; @@ -118,50 +124,51 @@ std::vector> simplificationRuleListPart2( Pattern Y ) { + using Word = typename Pattern::Word; return std::vector> { // 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, {~u256(0), X}}, [=]() -> Pattern { return {Pattern::Builtins::NOT, {X}}; }, false}, - {{Pattern::Builtins::MUL, {X, 0}}, [=]{ return u256(0); }, true}, - {{Pattern::Builtins::MUL, {0, X}}, [=]{ return u256(0); }, true}, + {{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, u256(-1)}}, [=]() -> Pattern { return {Pattern::Builtins::SUB, {0, X}}; }, false}, - {{Pattern::Builtins::MUL, {u256(-1), X}}, [=]() -> Pattern { return {Pattern::Builtins::SUB, {0, X}}; }, false}, - {{Pattern::Builtins::DIV, {X, 0}}, [=]{ return u256(0); }, true}, - {{Pattern::Builtins::DIV, {0, X}}, [=]{ return u256(0); }, true}, + {{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 u256(0); }, true}, - {{Pattern::Builtins::SDIV, {0, X}}, [=]{ return u256(0); }, true}, + {{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, ~u256(0)}}, [=]{ return X; }, false}, - {{Pattern::Builtins::AND, {~u256(0), X}}, [=]{ return X; }, false}, - {{Pattern::Builtins::AND, {X, 0}}, [=]{ return u256(0); }, true}, - {{Pattern::Builtins::AND, {0, X}}, [=]{ return u256(0); }, true}, + {{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, ~u256(0)}}, [=]{ return ~u256(0); }, true}, - {{Pattern::Builtins::OR, {~u256(0), X}}, [=]{ return ~u256(0); }, true}, + {{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 u256(0); }, true}, - {{Pattern::Builtins::MOD, {0, X}}, [=]{ return u256(0); }, true}, + {{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 u256(0); }, true}, - {{Pattern::Builtins::SHR, {X, 0}}, [=]{ return u256(0); }, true}, + {{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, ~u256(0)}}, [=]{ return u256(0); }, true}, - {{Pattern::Builtins::LT, {~u256(0), X}}, [=]{ return u256(0); }, true}, - {{Pattern::Builtins::GT, {0, X}}, [=]{ return u256(0); }, true}, - {{Pattern::Builtins::LT, {X, 0}}, [=]{ return u256(0); }, true}, - {{Pattern::Builtins::AND, {{Pattern::Builtins::BYTE, {X, Y}}, {u256(0xff)}}}, [=]() -> Pattern { return {Pattern::Builtins::BYTE, {X, Y}}; }, false}, - {{Pattern::Builtins::BYTE, {31, X}}, [=]() -> Pattern { return {Pattern::Builtins::AND, {X, u256(0xff)}}; }, 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} }; } @@ -174,18 +181,19 @@ std::vector> simplificationRuleListPart3( Pattern ) { + using Word = typename Pattern::Word; return std::vector> { // 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 u256(0); }, true}, - {{Pattern::Builtins::SUB, {X, X}}, [=]{ return u256(0); }, true}, - {{Pattern::Builtins::EQ, {X, X}}, [=]{ return u256(1); }, true}, - {{Pattern::Builtins::LT, {X, X}}, [=]{ return u256(0); }, true}, - {{Pattern::Builtins::SLT, {X, X}}, [=]{ return u256(0); }, true}, - {{Pattern::Builtins::GT, {X, X}}, [=]{ return u256(0); }, true}, - {{Pattern::Builtins::SGT, {X, X}}, [=]{ return u256(0); }, true}, - {{Pattern::Builtins::MOD, {X, X}}, [=]{ return u256(0); }, 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} }; } @@ -198,6 +206,7 @@ std::vector> simplificationRuleListPart4( Pattern Y ) { + using Word = typename Pattern::Word; return std::vector> { // logical instruction combinations {{Pattern::Builtins::NOT, {{Pattern::Builtins::NOT, {X}}}}, [=]{ return X; }, false}, @@ -213,10 +222,10 @@ std::vector> simplificationRuleListPart4( {{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 u256(0); }, true}, - {{Pattern::Builtins::AND, {{Pattern::Builtins::NOT, {X}}, X}}, [=]{ return u256(0); }, true}, - {{Pattern::Builtins::OR, {X, {Pattern::Builtins::NOT, {X}}}}, [=]{ return ~u256(0); }, true}, - {{Pattern::Builtins::OR, {{Pattern::Builtins::NOT, {X}}, X}}, [=]{ return ~u256(0); }, 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}, }; } @@ -230,12 +239,14 @@ std::vector> simplificationRuleListPart5( Pattern ) { + using Word = typename Pattern::Word; + std::vector> rules; // Replace MOD X, with AND X, - 1 - for (size_t i = 0; i < 256; ++i) + for (size_t i = 0; i < Pattern::WordSize; ++i) { - u256 value = u256(1) << i; + Word value = Word(1) << i; rules.push_back({ {Pattern::Builtins::MOD, {X, value}}, [=]() -> Pattern { return {Pattern::Builtins::AND, {X, value - 1}}; }, @@ -246,25 +257,25 @@ std::vector> simplificationRuleListPart5( // Replace SHL >=256, X with 0 rules.push_back({ {Pattern::Builtins::SHL, {A, X}}, - [=]() -> Pattern { return u256(0); }, + [=]() -> Pattern { return Word(0); }, true, - [=]() { return A.d() >= 256; } + [=]() { return A.d() >= Pattern::WordSize; } }); // Replace SHR >=256, X with 0 rules.push_back({ {Pattern::Builtins::SHR, {A, X}}, - [=]() -> Pattern { return u256(0); }, + [=]() -> Pattern { return Word(0); }, true, - [=]() { return A.d() >= 256; } + [=]() { return A.d() >= Pattern::WordSize; } }); // Replace BYTE(A, X), A >= 32 with 0 rules.push_back({ {Pattern::Builtins::BYTE, {A, X}}, - [=]() -> Pattern { return u256(0); }, + [=]() -> Pattern { return Word(0); }, true, - [=]() { return A.d() >= 32; } + [=]() { return A.d() >= Pattern::WordSize / 8; } }); for (auto const& op: std::vector{ @@ -274,7 +285,8 @@ std::vector> simplificationRuleListPart5( Pattern::Builtins::COINBASE }) { - u256 const mask = (u256(1) << 160) - 1; + assertThrow(Pattern::WordSize > 160, OptimizerException, ""); + Word const mask = (Word(1) << 160) - 1; rules.push_back({ {Pattern::Builtins::AND, {{op, mask}}}, [=]() -> Pattern { return op; }, @@ -338,14 +350,15 @@ std::vector> simplificationRuleListPart7( Pattern Y ) { + using Word = typename Pattern::Word; std::vector> rules; // Associative operations - for (auto const& opFun: std::vector>>{ - {Pattern::Builtins::ADD, std::plus()}, - {Pattern::Builtins::MUL, std::multiplies()}, - {Pattern::Builtins::AND, std::bit_and()}, - {Pattern::Builtins::OR, std::bit_or()}, - {Pattern::Builtins::XOR, std::bit_xor()} + for (auto const& opFun: std::vector>>{ + {Pattern::Builtins::ADD, std::plus()}, + {Pattern::Builtins::MUL, std::multiplies()}, + {Pattern::Builtins::AND, std::bit_and()}, + {Pattern::Builtins::OR, std::bit_or()}, + {Pattern::Builtins::XOR, std::bit_xor()} }) { auto op = opFun.first; @@ -385,10 +398,10 @@ std::vector> simplificationRuleListPart7( {Pattern::Builtins::SHL, {{B}, {Pattern::Builtins::SHL, {{A}, {X}}}}}, [=]() -> Pattern { bigint sum = bigint(A.d()) + B.d(); - if (sum >= 256) - return {Pattern::Builtins::AND, {X, u256(0)}}; + if (sum >= Pattern::WordSize) + return {Pattern::Builtins::AND, {X, Word(0)}}; else - return {Pattern::Builtins::SHL, {u256(sum), X}}; + return {Pattern::Builtins::SHL, {Word(sum), X}}; }, false }); @@ -399,10 +412,10 @@ std::vector> simplificationRuleListPart7( {Pattern::Builtins::SHR, {{B}, {Pattern::Builtins::SHR, {{A}, {X}}}}}, [=]() -> Pattern { bigint sum = bigint(A.d()) + B.d(); - if (sum >= 256) - return {Pattern::Builtins::AND, {X, u256(0)}}; + if (sum >= Pattern::WordSize) + return {Pattern::Builtins::AND, {X, Word(0)}}; else - return {Pattern::Builtins::SHR, {u256(sum), X}}; + return {Pattern::Builtins::SHR, {Word(sum), X}}; }, false }); @@ -412,7 +425,7 @@ std::vector> simplificationRuleListPart7( // SHR(B, SHL(A, X)) -> AND(SH[L/R]([B - A / A - B], X), Mask) {Pattern::Builtins::SHR, {{B}, {Pattern::Builtins::SHL, {{A}, {X}}}}}, [=]() -> Pattern { - u256 mask = shlWorkaround(u256(-1), unsigned(A.d())) >> unsigned(B.d()); + 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}}; @@ -422,7 +435,7 @@ std::vector> simplificationRuleListPart7( return {Pattern::Builtins::AND, {X, mask}}; }, false, - [=] { return A.d() < 256 && B.d() < 256; } + [=] { return A.d() < Pattern::WordSize && B.d() < Pattern::WordSize; } }); // Combine SHR-SHL by constant @@ -430,7 +443,7 @@ std::vector> simplificationRuleListPart7( // SHL(B, SHR(A, X)) -> AND(SH[L/R]([B - A / A - B], X), Mask) {Pattern::Builtins::SHL, {{B}, {Pattern::Builtins::SHR, {{A}, {X}}}}}, [=]() -> Pattern { - u256 mask = shlWorkaround(u256(-1) >> unsigned(A.d()), unsigned(B.d())); + 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}}; @@ -440,14 +453,14 @@ std::vector> simplificationRuleListPart7( return {Pattern::Builtins::AND, {X, mask}}; }, false, - [=] { return A.d() < 256 && B.d() < 256; } + [=] { 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}) { auto replacement = [=]() -> Pattern { - u256 mask = + Word mask = shiftOp == Pattern::Builtins::SHL ? shlWorkaround(A.d(), unsigned(B.d())) : A.d() >> unsigned(B.d()); @@ -458,20 +471,20 @@ std::vector> simplificationRuleListPart7( {shiftOp, {{B}, {Pattern::Builtins::AND, {{X}, {A}}}}}, replacement, false, - [=] { return B.d() < 256; } + [=] { 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}}}}}, replacement, false, - [=] { return B.d() < 256; } + [=] { return B.d() < Pattern::WordSize; } }); } rules.push_back({ // MUL(X, SHL(Y, 1)) -> SHL(Y, X) - {Pattern::Builtins::MUL, {X, {Pattern::Builtins::SHL, {Y, u256(1)}}}}, + {Pattern::Builtins::MUL, {X, {Pattern::Builtins::SHL, {Y, Word(1)}}}}, [=]() -> Pattern { return {Pattern::Builtins::SHL, {Y, X}}; }, @@ -480,7 +493,7 @@ std::vector> simplificationRuleListPart7( }); rules.push_back({ // MUL(SHL(X, 1), Y) -> SHL(X, Y) - {Pattern::Builtins::MUL, {{Pattern::Builtins::SHL, {X, u256(1)}}, Y}}, + {Pattern::Builtins::MUL, {{Pattern::Builtins::SHL, {X, Word(1)}}, Y}}, [=]() -> Pattern { return {Pattern::Builtins::SHL, {X, Y}}; }, @@ -489,7 +502,7 @@ std::vector> simplificationRuleListPart7( rules.push_back({ // DIV(X, SHL(Y, 1)) -> SHR(Y, X) - {Pattern::Builtins::DIV, {X, {Pattern::Builtins::SHL, {Y, u256(1)}}}}, + {Pattern::Builtins::DIV, {X, {Pattern::Builtins::SHL, {Y, Word(1)}}}}, [=]() -> Pattern { return {Pattern::Builtins::SHR, {Y, X}}; }, @@ -498,10 +511,10 @@ std::vector> simplificationRuleListPart7( }); std::function feasibilityFunction = [=]() { - if (B.d() > 256) + if (B.d() > Pattern::WordSize) return false; unsigned bAsUint = static_cast(B.d()); - return (A.d() & (u256(-1) >> bAsUint)) == (u256(-1) >> bAsUint); + return (A.d() & ((~Word(0)) >> bAsUint)) == ((~Word(0)) >> bAsUint); }; rules.push_back({ @@ -577,9 +590,11 @@ std::vector> simplificationRuleListPart9( Pattern Z ) { + using Word = typename Pattern::Word; std::vector> rules; - u256 const mask = (u256(1) << 160) - 1; + 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}}, @@ -621,6 +636,14 @@ std::vector> simplificationRuleList( Pattern Z ) { + using Word = typename Pattern::Word; + // Some sanity checks + assertThrow(Pattern::WordSize % 8 == 0, OptimizerException, ""); + assertThrow(Pattern::WordSize >= 8, OptimizerException, ""); + assertThrow(Pattern::WordSize <= 256, OptimizerException, ""); + assertThrow(Word(-1) == ~Word(0), OptimizerException, ""); + assertThrow(Word(-1) + 1 == Word(0), OptimizerException, ""); + std::vector> rules; rules += simplificationRuleListPart1(A, B, C, W, X); rules += simplificationRuleListPart2(A, B, C, W, X);