2018-01-17 16:56:33 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
solidity is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2018-01-17 16:56:33 +00:00
|
|
|
/**
|
|
|
|
* @date 2018
|
|
|
|
* Templatized list of simplification rules.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-02-19 16:12:28 +00:00
|
|
|
|
2018-01-17 16:56:33 +00:00
|
|
|
#include <libevmasm/Instruction.h>
|
2018-02-06 11:20:00 +00:00
|
|
|
#include <libevmasm/SimplificationRule.h>
|
2018-01-17 16:56:33 +00:00
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
2018-01-17 18:18:42 +00:00
|
|
|
|
2019-03-28 11:47:21 +00:00
|
|
|
#include <boost/multiprecision/detail/min_max.hpp>
|
|
|
|
|
2020-05-15 23:59:57 +00:00
|
|
|
#include <libyul/Dialect.h>
|
|
|
|
#include <libyul/backends/evm/EVMDialect.h>
|
|
|
|
#include <liblangutil/EVMVersion.h>
|
|
|
|
|
2019-03-28 11:47:21 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <functional>
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
namespace solidity::evmasm
|
2018-01-17 16:56:33 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
template <class S> S divWorkaround(S const& _a, S const& _b)
|
|
|
|
{
|
|
|
|
return (S)(bigint(_a) / bigint(_b));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class S> S modWorkaround(S const& _a, S const& _b)
|
|
|
|
{
|
|
|
|
return (S)(bigint(_a) % bigint(_b));
|
|
|
|
}
|
|
|
|
|
2019-04-16 11:14:13 +00:00
|
|
|
// This works around a bug fixed with Boost 1.64.
|
2021-09-24 12:17:48 +00:00
|
|
|
// https://www.boost.org/doc/libs/release/libs/multiprecision/doc/html/boost_multiprecision/map/hist.html#boost_multiprecision.map.hist.multiprecision_2_3_1_boost_1_64
|
2019-11-06 21:39:13 +00:00
|
|
|
template <class S> S shlWorkaround(S const& _x, unsigned _amount)
|
2019-04-16 11:14:13 +00:00
|
|
|
{
|
|
|
|
return u256((bigint(_x) << _amount) & u256(-1));
|
|
|
|
}
|
|
|
|
|
2021-03-11 10:30:22 +00:00
|
|
|
/// @returns k if _x == 2**k, nullopt otherwise
|
|
|
|
inline std::optional<size_t> binaryLogarithm(u256 const& _x)
|
|
|
|
{
|
2021-03-11 13:51:35 +00:00
|
|
|
if (_x == 0)
|
2021-03-11 10:30:22 +00:00
|
|
|
return std::nullopt;
|
|
|
|
size_t msb = boost::multiprecision::msb(_x);
|
|
|
|
return (u256(1) << msb) == _x ? std::make_optional(msb) : std::nullopt;
|
|
|
|
}
|
|
|
|
|
2019-01-18 11:34:36 +00:00
|
|
|
// simplificationRuleList below was split up into parts to prevent
|
2018-09-14 13:49:55 +00:00
|
|
|
// stack overflows in the JavaScript optimizer for emscripten builds
|
|
|
|
// that affected certain browser versions.
|
2018-01-17 16:56:33 +00:00
|
|
|
template <class Pattern>
|
2018-09-14 13:49:55 +00:00
|
|
|
std::vector<SimplificationRule<Pattern>> simplificationRuleListPart1(
|
2018-01-17 16:56:33 +00:00
|
|
|
Pattern A,
|
|
|
|
Pattern B,
|
|
|
|
Pattern C,
|
2019-01-18 11:34:36 +00:00
|
|
|
Pattern,
|
|
|
|
Pattern
|
2018-01-17 16:56:33 +00:00
|
|
|
)
|
|
|
|
{
|
2019-11-06 21:39:13 +00:00
|
|
|
using Word = typename Pattern::Word;
|
2019-11-07 20:29:56 +00:00
|
|
|
using Builtins = typename Pattern::Builtins;
|
2020-08-03 20:42:39 +00:00
|
|
|
return std::vector<SimplificationRule<Pattern>>{
|
2018-07-10 07:18:59 +00:00
|
|
|
// arithmetic on constants
|
2020-08-03 20:42:39 +00:00
|
|
|
{Builtins::ADD(A, B), [=]{ return A.d() + B.d(); }},
|
|
|
|
{Builtins::MUL(A, B), [=]{ return A.d() * B.d(); }},
|
|
|
|
{Builtins::SUB(A, B), [=]{ return A.d() - B.d(); }},
|
|
|
|
{Builtins::DIV(A, B), [=]{ return B.d() == 0 ? 0 : divWorkaround(A.d(), B.d()); }},
|
|
|
|
{Builtins::SDIV(A, B), [=]{ return B.d() == 0 ? 0 : s2u(divWorkaround(u2s(A.d()), u2s(B.d()))); }},
|
|
|
|
{Builtins::MOD(A, B), [=]{ return B.d() == 0 ? 0 : modWorkaround(A.d(), B.d()); }},
|
|
|
|
{Builtins::SMOD(A, B), [=]{ return B.d() == 0 ? 0 : s2u(modWorkaround(u2s(A.d()), u2s(B.d()))); }},
|
|
|
|
{Builtins::EXP(A, B), [=]{ return Word(boost::multiprecision::powm(bigint(A.d()), bigint(B.d()), bigint(1) << Pattern::WordSize)); }},
|
|
|
|
{Builtins::NOT(A), [=]{ return ~A.d(); }},
|
|
|
|
{Builtins::LT(A, B), [=]() -> Word { return A.d() < B.d() ? 1 : 0; }},
|
|
|
|
{Builtins::GT(A, B), [=]() -> Word { return A.d() > B.d() ? 1 : 0; }},
|
|
|
|
{Builtins::SLT(A, B), [=]() -> Word { return u2s(A.d()) < u2s(B.d()) ? 1 : 0; }},
|
|
|
|
{Builtins::SGT(A, B), [=]() -> Word { return u2s(A.d()) > u2s(B.d()) ? 1 : 0; }},
|
|
|
|
{Builtins::EQ(A, B), [=]() -> Word { return A.d() == B.d() ? 1 : 0; }},
|
|
|
|
{Builtins::ISZERO(A), [=]() -> Word { return A.d() == 0 ? 1 : 0; }},
|
|
|
|
{Builtins::AND(A, B), [=]{ return A.d() & B.d(); }},
|
|
|
|
{Builtins::OR(A, B), [=]{ return A.d() | B.d(); }},
|
|
|
|
{Builtins::XOR(A, B), [=]{ return A.d() ^ B.d(); }},
|
2019-11-07 20:29:56 +00:00
|
|
|
{Builtins::BYTE(A, B), [=]{
|
2019-11-06 21:39:13 +00:00
|
|
|
return
|
|
|
|
A.d() >= Pattern::WordSize / 8 ?
|
|
|
|
0 :
|
|
|
|
(B.d() >> unsigned(8 * (Pattern::WordSize / 8 - 1 - A.d()))) & 0xff;
|
2020-08-03 20:42:39 +00:00
|
|
|
}},
|
|
|
|
{Builtins::ADDMOD(A, B, C), [=]{ return C.d() == 0 ? 0 : Word((bigint(A.d()) + bigint(B.d())) % C.d()); }},
|
|
|
|
{Builtins::MULMOD(A, B, C), [=]{ return C.d() == 0 ? 0 : Word((bigint(A.d()) * bigint(B.d())) % C.d()); }},
|
2019-11-07 20:29:56 +00:00
|
|
|
{Builtins::SIGNEXTEND(A, B), [=]() -> Word {
|
2019-11-06 21:39:13 +00:00
|
|
|
if (A.d() >= Pattern::WordSize / 8 - 1)
|
2018-01-17 16:56:33 +00:00
|
|
|
return B.d();
|
|
|
|
unsigned testBit = unsigned(A.d()) * 8 + 7;
|
2019-11-06 21:39:13 +00:00
|
|
|
Word mask = (Word(1) << testBit) - 1;
|
2019-02-22 11:53:05 +00:00
|
|
|
return boost::multiprecision::bit_test(B.d(), testBit) ? B.d() | ~mask : B.d() & mask;
|
2020-08-03 20:42:39 +00:00
|
|
|
}},
|
2019-11-07 20:29:56 +00:00
|
|
|
{Builtins::SHL(A, B), [=]{
|
2019-11-06 21:39:13 +00:00
|
|
|
if (A.d() >= Pattern::WordSize)
|
|
|
|
return Word(0);
|
2019-04-16 11:14:13 +00:00
|
|
|
return shlWorkaround(B.d(), unsigned(A.d()));
|
2020-08-03 20:42:39 +00:00
|
|
|
}},
|
2019-11-07 20:29:56 +00:00
|
|
|
{Builtins::SHR(A, B), [=]{
|
2019-11-06 21:39:13 +00:00
|
|
|
if (A.d() >= Pattern::WordSize)
|
|
|
|
return Word(0);
|
2017-05-10 09:46:44 +00:00
|
|
|
return B.d() >> unsigned(A.d());
|
2020-08-03 20:42:39 +00:00
|
|
|
}}
|
2019-01-18 11:34:36 +00:00
|
|
|
};
|
|
|
|
}
|
2018-01-17 16:56:33 +00:00
|
|
|
|
2019-11-07 20:29:56 +00:00
|
|
|
|
2019-01-18 11:34:36 +00:00
|
|
|
template <class Pattern>
|
|
|
|
std::vector<SimplificationRule<Pattern>> simplificationRuleListPart2(
|
|
|
|
Pattern,
|
|
|
|
Pattern,
|
|
|
|
Pattern,
|
|
|
|
Pattern X,
|
2019-02-26 12:12:46 +00:00
|
|
|
Pattern Y
|
2019-01-18 11:34:36 +00:00
|
|
|
)
|
|
|
|
{
|
2019-11-06 21:39:13 +00:00
|
|
|
using Word = typename Pattern::Word;
|
2019-11-07 20:29:56 +00:00
|
|
|
using Builtins = typename Pattern::Builtins;
|
2019-01-18 11:34:36 +00:00
|
|
|
return std::vector<SimplificationRule<Pattern>> {
|
2018-01-18 12:56:42 +00:00
|
|
|
// invariants involving known constants
|
2020-08-03 20:42:39 +00:00
|
|
|
{Builtins::ADD(X, 0), [=]{ return X; }},
|
|
|
|
{Builtins::ADD(0, X), [=]{ return X; }},
|
|
|
|
{Builtins::SUB(X, 0), [=]{ return X; }},
|
|
|
|
{Builtins::SUB(~Word(0), X), [=]() -> Pattern { return Builtins::NOT(X); }},
|
|
|
|
{Builtins::MUL(X, 0), [=]{ return Word(0); }},
|
|
|
|
{Builtins::MUL(0, X), [=]{ return Word(0); }},
|
|
|
|
{Builtins::MUL(X, 1), [=]{ return X; }},
|
|
|
|
{Builtins::MUL(1, X), [=]{ return X; }},
|
|
|
|
{Builtins::MUL(X, Word(-1)), [=]() -> Pattern { return Builtins::SUB(0, X); }},
|
|
|
|
{Builtins::MUL(Word(-1), X), [=]() -> Pattern { return Builtins::SUB(0, X); }},
|
|
|
|
{Builtins::DIV(X, 0), [=]{ return Word(0); }},
|
|
|
|
{Builtins::DIV(0, X), [=]{ return Word(0); }},
|
|
|
|
{Builtins::DIV(X, 1), [=]{ return X; }},
|
|
|
|
{Builtins::SDIV(X, 0), [=]{ return Word(0); }},
|
|
|
|
{Builtins::SDIV(0, X), [=]{ return Word(0); }},
|
|
|
|
{Builtins::SDIV(X, 1), [=]{ return X; }},
|
|
|
|
{Builtins::AND(X, ~Word(0)), [=]{ return X; }},
|
|
|
|
{Builtins::AND(~Word(0), X), [=]{ return X; }},
|
|
|
|
{Builtins::AND(X, 0), [=]{ return Word(0); }},
|
|
|
|
{Builtins::AND(0, X), [=]{ return Word(0); }},
|
|
|
|
{Builtins::OR(X, 0), [=]{ return X; }},
|
|
|
|
{Builtins::OR(0, X), [=]{ return X; }},
|
|
|
|
{Builtins::OR(X, ~Word(0)), [=]{ return ~Word(0); }},
|
|
|
|
{Builtins::OR(~Word(0), X), [=]{ return ~Word(0); }},
|
|
|
|
{Builtins::XOR(X, 0), [=]{ return X; }},
|
|
|
|
{Builtins::XOR(0, X), [=]{ return X; }},
|
|
|
|
{Builtins::MOD(X, 0), [=]{ return Word(0); }},
|
|
|
|
{Builtins::MOD(0, X), [=]{ return Word(0); }},
|
|
|
|
{Builtins::EQ(X, 0), [=]() -> Pattern { return Builtins::ISZERO(X); },},
|
|
|
|
{Builtins::EQ(0, X), [=]() -> Pattern { return Builtins::ISZERO(X); },},
|
|
|
|
{Builtins::SHL(0, X), [=]{ return X; }},
|
|
|
|
{Builtins::SHR(0, X), [=]{ return X; }},
|
|
|
|
{Builtins::SHL(X, 0), [=]{ return Word(0); }},
|
|
|
|
{Builtins::SHR(X, 0), [=]{ return Word(0); }},
|
|
|
|
{Builtins::GT(X, 0), [=]() -> Pattern { return Builtins::ISZERO(Builtins::ISZERO(X)); }},
|
|
|
|
{Builtins::LT(0, X), [=]() -> Pattern { return Builtins::ISZERO(Builtins::ISZERO(X)); }},
|
|
|
|
{Builtins::GT(X, ~Word(0)), [=]{ return Word(0); }},
|
|
|
|
{Builtins::LT(~Word(0), X), [=]{ return Word(0); }},
|
|
|
|
{Builtins::GT(0, X), [=]{ return Word(0); }},
|
|
|
|
{Builtins::LT(X, 0), [=]{ return Word(0); }},
|
|
|
|
{Builtins::AND(Builtins::BYTE(X, Y), Word(0xff)), [=]() -> Pattern { return Builtins::BYTE(X, Y); }},
|
|
|
|
{Builtins::BYTE(Word(Pattern::WordSize / 8 - 1), X), [=]() -> Pattern { return Builtins::AND(X, Word(0xff)); }},
|
2019-01-18 11:34:36 +00:00
|
|
|
};
|
|
|
|
}
|
2018-01-17 16:56:33 +00:00
|
|
|
|
2019-01-18 11:34:36 +00:00
|
|
|
template <class Pattern>
|
|
|
|
std::vector<SimplificationRule<Pattern>> simplificationRuleListPart3(
|
|
|
|
Pattern,
|
|
|
|
Pattern,
|
|
|
|
Pattern,
|
|
|
|
Pattern X,
|
|
|
|
Pattern
|
|
|
|
)
|
|
|
|
{
|
2019-11-06 21:39:13 +00:00
|
|
|
using Word = typename Pattern::Word;
|
2019-11-07 20:29:56 +00:00
|
|
|
using Builtins = typename Pattern::Builtins;
|
2019-01-18 11:34:36 +00:00
|
|
|
return std::vector<SimplificationRule<Pattern>> {
|
2018-01-17 16:56:33 +00:00
|
|
|
// operations involving an expression and itself
|
2020-08-03 20:42:39 +00:00
|
|
|
{Builtins::AND(X, X), [=]{ return X; }},
|
|
|
|
{Builtins::OR(X, X), [=]{ return X; }},
|
|
|
|
{Builtins::XOR(X, X), [=]{ return Word(0); }},
|
|
|
|
{Builtins::SUB(X, X), [=]{ return Word(0); }},
|
|
|
|
{Builtins::EQ(X, X), [=]{ return Word(1); }},
|
|
|
|
{Builtins::LT(X, X), [=]{ return Word(0); }},
|
|
|
|
{Builtins::SLT(X, X), [=]{ return Word(0); }},
|
|
|
|
{Builtins::GT(X, X), [=]{ return Word(0); }},
|
|
|
|
{Builtins::SGT(X, X), [=]{ return Word(0); }},
|
|
|
|
{Builtins::MOD(X, X), [=]{ return Word(0); }}
|
2019-01-18 11:34:36 +00:00
|
|
|
};
|
|
|
|
}
|
2018-01-17 16:56:33 +00:00
|
|
|
|
2019-01-18 11:34:36 +00:00
|
|
|
template <class Pattern>
|
|
|
|
std::vector<SimplificationRule<Pattern>> simplificationRuleListPart4(
|
|
|
|
Pattern,
|
|
|
|
Pattern,
|
|
|
|
Pattern,
|
|
|
|
Pattern X,
|
|
|
|
Pattern Y
|
|
|
|
)
|
|
|
|
{
|
2019-11-06 21:39:13 +00:00
|
|
|
using Word = typename Pattern::Word;
|
2019-11-07 20:29:56 +00:00
|
|
|
using Builtins = typename Pattern::Builtins;
|
2019-01-18 11:34:36 +00:00
|
|
|
return std::vector<SimplificationRule<Pattern>> {
|
2018-01-17 16:56:33 +00:00
|
|
|
// logical instruction combinations
|
2020-08-03 20:42:39 +00:00
|
|
|
{Builtins::NOT(Builtins::NOT(X)), [=]{ return X; }},
|
|
|
|
{Builtins::XOR(X, Builtins::XOR(X, Y)), [=]{ return Y; }},
|
|
|
|
{Builtins::XOR(X, Builtins::XOR(Y, X)), [=]{ return Y; }},
|
|
|
|
{Builtins::XOR(Builtins::XOR(X, Y), X), [=]{ return Y; }},
|
|
|
|
{Builtins::XOR(Builtins::XOR(Y, X), X), [=]{ return Y; }},
|
|
|
|
{Builtins::OR(X, Builtins::AND(X, Y)), [=]{ return X; }},
|
|
|
|
{Builtins::OR(X, Builtins::AND(Y, X)), [=]{ return X; }},
|
|
|
|
{Builtins::OR(Builtins::AND(X, Y), X), [=]{ return X; }},
|
|
|
|
{Builtins::OR(Builtins::AND(Y, X), X), [=]{ return X; }},
|
|
|
|
{Builtins::AND(X, Builtins::OR(X, Y)), [=]{ return X; }},
|
|
|
|
{Builtins::AND(X, Builtins::OR(Y, X)), [=]{ return X; }},
|
|
|
|
{Builtins::AND(Builtins::OR(X, Y), X), [=]{ return X; }},
|
|
|
|
{Builtins::AND(Builtins::OR(Y, X), X), [=]{ return X; }},
|
|
|
|
{Builtins::AND(X, Builtins::NOT(X)), [=]{ return Word(0); }},
|
|
|
|
{Builtins::AND(Builtins::NOT(X), X), [=]{ return Word(0); }},
|
|
|
|
{Builtins::OR(X, Builtins::NOT(X)), [=]{ return ~Word(0); }},
|
|
|
|
{Builtins::OR(Builtins::NOT(X), X), [=]{ return ~Word(0); }},
|
2018-01-17 16:56:33 +00:00
|
|
|
};
|
2018-09-14 13:49:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 13:47:58 +00:00
|
|
|
template <class Pattern>
|
|
|
|
std::vector<SimplificationRule<Pattern>> simplificationRuleListPart4_5(
|
2021-08-05 09:32:28 +00:00
|
|
|
Pattern A,
|
|
|
|
Pattern B,
|
2020-04-20 13:47:58 +00:00
|
|
|
Pattern,
|
|
|
|
Pattern X,
|
|
|
|
Pattern Y
|
|
|
|
)
|
|
|
|
{
|
|
|
|
using Builtins = typename Pattern::Builtins;
|
|
|
|
return std::vector<SimplificationRule<Pattern>>{
|
|
|
|
// idempotent operations
|
2020-08-03 20:42:39 +00:00
|
|
|
{Builtins::AND(Builtins::AND(X, Y), Y), [=]{ return Builtins::AND(X, Y); }},
|
|
|
|
{Builtins::AND(Y, Builtins::AND(X, Y)), [=]{ return Builtins::AND(X, Y); }},
|
|
|
|
{Builtins::AND(Builtins::AND(Y, X), Y), [=]{ return Builtins::AND(Y, X); }},
|
|
|
|
{Builtins::AND(Y, Builtins::AND(Y, X)), [=]{ return Builtins::AND(Y, X); }},
|
|
|
|
{Builtins::OR(Builtins::OR(X, Y), Y), [=]{ return Builtins::OR(X, Y); }},
|
|
|
|
{Builtins::OR(Y, Builtins::OR(X, Y)), [=]{ return Builtins::OR(X, Y); }},
|
|
|
|
{Builtins::OR(Builtins::OR(Y, X), Y), [=]{ return Builtins::OR(Y, X); }},
|
|
|
|
{Builtins::OR(Y, Builtins::OR(Y, X)), [=]{ return Builtins::OR(Y, X); }},
|
2021-08-05 09:32:28 +00:00
|
|
|
{Builtins::SIGNEXTEND(X, Builtins::SIGNEXTEND(X, Y)), [=]() { return Builtins::SIGNEXTEND(X, Y); }},
|
|
|
|
{Builtins::SIGNEXTEND(A, Builtins::SIGNEXTEND(B, X)), [=]() {
|
|
|
|
return Builtins::SIGNEXTEND(A.d() < B.d() ? A.d() : B.d(), X);
|
|
|
|
}},
|
2020-04-20 13:47:58 +00:00
|
|
|
};
|
|
|
|
}
|
2018-09-14 13:49:55 +00:00
|
|
|
|
|
|
|
template <class Pattern>
|
2019-01-18 11:34:36 +00:00
|
|
|
std::vector<SimplificationRule<Pattern>> simplificationRuleListPart5(
|
2022-06-11 10:43:55 +00:00
|
|
|
bool _forYulOptimizer,
|
2019-02-21 11:34:42 +00:00
|
|
|
Pattern A,
|
2021-08-05 09:32:28 +00:00
|
|
|
Pattern B,
|
2018-09-14 13:49:55 +00:00
|
|
|
Pattern,
|
|
|
|
Pattern X,
|
2022-05-01 21:10:37 +00:00
|
|
|
Pattern Y
|
2018-09-14 13:49:55 +00:00
|
|
|
)
|
|
|
|
{
|
2019-11-06 21:39:13 +00:00
|
|
|
using Word = typename Pattern::Word;
|
2019-11-07 20:29:56 +00:00
|
|
|
using Builtins = typename Pattern::Builtins;
|
2019-11-06 21:39:13 +00:00
|
|
|
|
2018-09-14 13:49:55 +00:00
|
|
|
std::vector<SimplificationRule<Pattern>> rules;
|
2018-01-17 16:56:33 +00:00
|
|
|
|
2022-06-11 10:43:55 +00:00
|
|
|
// The libevmasm optimizer does not support rules resulting in opcodes with more than two arguments.
|
|
|
|
if (_forYulOptimizer)
|
|
|
|
{
|
|
|
|
// Replace MOD(MUL(X, Y), A) with MULMOD(X, Y, A) iff A=2**N
|
|
|
|
rules.push_back({
|
|
|
|
Builtins::MOD(Builtins::MUL(X, Y), A),
|
|
|
|
[=]() -> Pattern { return Builtins::MULMOD(X, Y, A); },
|
|
|
|
[=] {
|
|
|
|
return A.d() > 0 && ((A.d() & (A.d() - 1)) == 0);
|
|
|
|
}
|
|
|
|
});
|
2022-05-01 21:10:37 +00:00
|
|
|
|
2022-06-11 10:43:55 +00:00
|
|
|
// Replace MOD(ADD(X, Y), A) with ADDMOD(X, Y, A) iff A=2**N
|
|
|
|
rules.push_back({
|
|
|
|
Builtins::MOD(Builtins::ADD(X, Y), A),
|
|
|
|
[=]() -> Pattern { return Builtins::ADDMOD(X, Y, A); },
|
|
|
|
[=] {
|
|
|
|
return A.d() > 0 && ((A.d() & (A.d() - 1)) == 0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-05-01 21:10:37 +00:00
|
|
|
|
2018-01-04 12:05:01 +00:00
|
|
|
// Replace MOD X, <power-of-two> with AND X, <power-of-two> - 1
|
2019-11-06 21:39:13 +00:00
|
|
|
for (size_t i = 0; i < Pattern::WordSize; ++i)
|
2018-01-04 12:05:01 +00:00
|
|
|
{
|
2019-11-06 21:39:13 +00:00
|
|
|
Word value = Word(1) << i;
|
2018-01-04 12:05:01 +00:00
|
|
|
rules.push_back({
|
2019-11-07 20:29:56 +00:00
|
|
|
Builtins::MOD(X, value),
|
2020-08-03 20:42:39 +00:00
|
|
|
[=]() -> Pattern { return Builtins::AND(X, value - 1); }
|
2018-01-04 12:05:01 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-21 11:34:42 +00:00
|
|
|
// Replace SHL >=256, X with 0
|
|
|
|
rules.push_back({
|
2019-11-07 20:29:56 +00:00
|
|
|
Builtins::SHL(A, X),
|
2019-11-06 21:39:13 +00:00
|
|
|
[=]() -> Pattern { return Word(0); },
|
|
|
|
[=]() { return A.d() >= Pattern::WordSize; }
|
2019-02-21 11:34:42 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Replace SHR >=256, X with 0
|
|
|
|
rules.push_back({
|
2019-11-07 20:29:56 +00:00
|
|
|
Builtins::SHR(A, X),
|
2019-11-06 21:39:13 +00:00
|
|
|
[=]() -> Pattern { return Word(0); },
|
|
|
|
[=]() { return A.d() >= Pattern::WordSize; }
|
2019-02-21 11:34:42 +00:00
|
|
|
});
|
|
|
|
|
2019-08-28 15:34:31 +00:00
|
|
|
// Replace BYTE(A, X), A >= 32 with 0
|
|
|
|
rules.push_back({
|
2019-11-07 20:29:56 +00:00
|
|
|
Builtins::BYTE(A, X),
|
2019-11-06 21:39:13 +00:00
|
|
|
[=]() -> Pattern { return Word(0); },
|
|
|
|
[=]() { return A.d() >= Pattern::WordSize / 8; }
|
2019-08-28 15:34:31 +00:00
|
|
|
});
|
|
|
|
|
2021-08-05 09:32:28 +00:00
|
|
|
// Replace SIGNEXTEND(A, X), A >= 31 with ID
|
|
|
|
rules.push_back({
|
|
|
|
Builtins::SIGNEXTEND(A, X),
|
|
|
|
[=]() -> Pattern { return X; },
|
|
|
|
[=]() { return A.d() >= Pattern::WordSize / 8 - 1; }
|
|
|
|
});
|
|
|
|
rules.push_back({
|
|
|
|
Builtins::AND(A, Builtins::SIGNEXTEND(B, X)),
|
|
|
|
[=]() -> Pattern { return Builtins::AND(A, X); },
|
|
|
|
[=]() {
|
|
|
|
return
|
|
|
|
B.d() < Pattern::WordSize / 8 - 1 &&
|
|
|
|
(A.d() & ((u256(1) << static_cast<size_t>((B.d() + 1) * 8)) - 1)) == A.d();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
rules.push_back({
|
|
|
|
Builtins::AND(Builtins::SIGNEXTEND(B, X), A),
|
|
|
|
[=]() -> Pattern { return Builtins::AND(A, X); },
|
|
|
|
[=]() {
|
|
|
|
return
|
|
|
|
B.d() < Pattern::WordSize / 8 - 1 &&
|
|
|
|
(A.d() & ((u256(1) << static_cast<size_t>((B.d() + 1) * 8)) - 1)) == A.d();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-11-07 20:29:56 +00:00
|
|
|
for (auto instr: {
|
|
|
|
Instruction::ADDRESS,
|
|
|
|
Instruction::CALLER,
|
|
|
|
Instruction::ORIGIN,
|
|
|
|
Instruction::COINBASE
|
2018-04-18 12:36:20 +00:00
|
|
|
})
|
|
|
|
{
|
2019-11-06 21:39:13 +00:00
|
|
|
assertThrow(Pattern::WordSize > 160, OptimizerException, "");
|
|
|
|
Word const mask = (Word(1) << 160) - 1;
|
2018-04-18 12:36:20 +00:00
|
|
|
rules.push_back({
|
2019-11-07 20:29:56 +00:00
|
|
|
Builtins::AND(Pattern{instr}, mask),
|
2020-08-03 20:42:39 +00:00
|
|
|
[=]() -> Pattern { return {instr}; }
|
2018-04-18 12:36:20 +00:00
|
|
|
});
|
|
|
|
rules.push_back({
|
2019-11-07 20:29:56 +00:00
|
|
|
Builtins::AND(mask, Pattern{instr}),
|
2020-08-03 20:42:39 +00:00
|
|
|
[=]() -> Pattern { return {instr}; }
|
2018-04-18 12:36:20 +00:00
|
|
|
});
|
|
|
|
}
|
2019-09-09 11:37:25 +00:00
|
|
|
|
2019-01-18 11:34:36 +00:00
|
|
|
return rules;
|
|
|
|
}
|
2018-04-18 12:36:20 +00:00
|
|
|
|
2019-01-18 11:34:36 +00:00
|
|
|
template <class Pattern>
|
|
|
|
std::vector<SimplificationRule<Pattern>> simplificationRuleListPart6(
|
|
|
|
Pattern,
|
|
|
|
Pattern,
|
|
|
|
Pattern,
|
|
|
|
Pattern X,
|
|
|
|
Pattern Y
|
|
|
|
)
|
|
|
|
{
|
2019-11-07 20:29:56 +00:00
|
|
|
using Builtins = typename Pattern::Builtins;
|
|
|
|
|
2019-01-18 11:34:36 +00:00
|
|
|
std::vector<SimplificationRule<Pattern>> rules;
|
2018-01-18 12:56:42 +00:00
|
|
|
// Double negation of opcodes with boolean result
|
2019-11-07 20:29:56 +00:00
|
|
|
for (auto instr: {
|
|
|
|
Instruction::EQ,
|
|
|
|
Instruction::LT,
|
|
|
|
Instruction::SLT,
|
|
|
|
Instruction::GT,
|
|
|
|
Instruction::SGT
|
2018-01-17 16:56:33 +00:00
|
|
|
})
|
2019-11-07 20:29:56 +00:00
|
|
|
{
|
|
|
|
typename Builtins::PatternGeneratorInstance op{instr};
|
2018-01-17 16:56:33 +00:00
|
|
|
rules.push_back({
|
2019-11-07 20:29:56 +00:00
|
|
|
Builtins::ISZERO(Builtins::ISZERO(op(X, Y))),
|
2020-08-03 20:42:39 +00:00
|
|
|
[=]() -> Pattern { return op(X, Y); }
|
2018-01-17 16:56:33 +00:00
|
|
|
});
|
2019-11-07 20:29:56 +00:00
|
|
|
}
|
2018-01-17 16:56:33 +00:00
|
|
|
|
|
|
|
rules.push_back({
|
2019-11-07 20:29:56 +00:00
|
|
|
Builtins::ISZERO(Builtins::ISZERO(Builtins::ISZERO(X))),
|
2020-08-03 20:42:39 +00:00
|
|
|
[=]() -> Pattern { return Builtins::ISZERO(X); }
|
2018-01-17 16:56:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
rules.push_back({
|
2019-11-07 20:29:56 +00:00
|
|
|
Builtins::ISZERO(Builtins::XOR(X, Y)),
|
2020-08-03 20:42:39 +00:00
|
|
|
[=]() -> Pattern { return Builtins::EQ(X, Y); }
|
2018-01-17 16:56:33 +00:00
|
|
|
});
|
|
|
|
|
2020-12-22 14:08:37 +00:00
|
|
|
rules.push_back({
|
|
|
|
Builtins::ISZERO(Builtins::SUB(X, Y)),
|
|
|
|
[=]() -> Pattern { return Builtins::EQ(X, Y); }
|
|
|
|
});
|
|
|
|
|
2019-01-18 11:34:36 +00:00
|
|
|
return rules;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Pattern>
|
|
|
|
std::vector<SimplificationRule<Pattern>> simplificationRuleListPart7(
|
|
|
|
Pattern A,
|
|
|
|
Pattern B,
|
|
|
|
Pattern,
|
|
|
|
Pattern X,
|
2022-09-15 10:19:18 +00:00
|
|
|
Pattern Y,
|
|
|
|
Pattern Z
|
2019-01-18 11:34:36 +00:00
|
|
|
)
|
|
|
|
{
|
2019-11-06 21:39:13 +00:00
|
|
|
using Word = typename Pattern::Word;
|
2019-11-07 20:29:56 +00:00
|
|
|
using Builtins = typename Pattern::Builtins;
|
|
|
|
|
2019-01-18 11:34:36 +00:00
|
|
|
std::vector<SimplificationRule<Pattern>> rules;
|
2018-01-17 16:56:33 +00:00
|
|
|
// Associative operations
|
2019-11-07 20:29:56 +00:00
|
|
|
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>()}
|
2018-01-17 16:56:33 +00:00
|
|
|
})
|
|
|
|
{
|
2019-11-07 20:29:56 +00:00
|
|
|
typename Builtins::PatternGeneratorInstance op{instrAndFunc.first};
|
|
|
|
std::function<Word(Word, Word)> fun = instrAndFunc.second;
|
2018-01-18 12:56:42 +00:00
|
|
|
// 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)
|
2019-11-07 20:29:56 +00:00
|
|
|
for (auto const& opXA: {op(X, A), op(A, X)})
|
2018-01-18 12:56:42 +00:00
|
|
|
{
|
2018-02-06 11:20:00 +00:00
|
|
|
rules += std::vector<SimplificationRule<Pattern>>{{
|
2018-01-18 12:56:42 +00:00
|
|
|
// (X+A)+B -> X+(A+B)
|
2019-11-07 20:29:56 +00:00
|
|
|
op(opXA, B),
|
2020-08-03 20:42:39 +00:00
|
|
|
[=]() -> Pattern { return op(X, fun(A.d(), B.d())); }
|
2018-01-18 12:56:42 +00:00
|
|
|
}, {
|
|
|
|
// (X+A)+Y -> (X+Y)+A
|
2019-11-07 20:29:56 +00:00
|
|
|
op(opXA, Y),
|
2020-08-03 20:42:39 +00:00
|
|
|
[=]() -> Pattern { return op(op(X, Y), A); }
|
2018-01-18 12:56:42 +00:00
|
|
|
}, {
|
|
|
|
// B+(X+A) -> X+(A+B)
|
2019-11-07 20:29:56 +00:00
|
|
|
op(B, opXA),
|
2020-08-03 20:42:39 +00:00
|
|
|
[=]() -> Pattern { return op(X, fun(A.d(), B.d())); }
|
2018-01-18 12:56:42 +00:00
|
|
|
}, {
|
|
|
|
// Y+(X+A) -> (Y+X)+A
|
2019-11-07 20:29:56 +00:00
|
|
|
op(Y, opXA),
|
2020-08-03 20:42:39 +00:00
|
|
|
[=]() -> Pattern { return op(op(Y, X), A); }
|
2018-01-18 12:56:42 +00:00
|
|
|
}};
|
|
|
|
}
|
2018-01-17 16:56:33 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 11:14:13 +00:00
|
|
|
// Combine two SHL by constant
|
2019-02-19 16:12:28 +00:00
|
|
|
rules.push_back({
|
|
|
|
// SHL(B, SHL(A, X)) -> SHL(min(A+B, 256), X)
|
2019-11-07 20:29:56 +00:00
|
|
|
Builtins::SHL(B, Builtins::SHL(A, X)),
|
2019-03-12 10:41:47 +00:00
|
|
|
[=]() -> Pattern {
|
|
|
|
bigint sum = bigint(A.d()) + B.d();
|
2019-11-06 21:39:13 +00:00
|
|
|
if (sum >= Pattern::WordSize)
|
2019-11-07 20:29:56 +00:00
|
|
|
return Builtins::AND(X, Word(0));
|
2019-03-12 10:41:47 +00:00
|
|
|
else
|
2019-11-07 20:29:56 +00:00
|
|
|
return Builtins::SHL(Word(sum), X);
|
2020-08-03 20:42:39 +00:00
|
|
|
}
|
2019-02-19 16:12:28 +00:00
|
|
|
});
|
|
|
|
|
2019-04-16 11:14:13 +00:00
|
|
|
// Combine two SHR by constant
|
2019-02-19 16:12:28 +00:00
|
|
|
rules.push_back({
|
|
|
|
// SHR(B, SHR(A, X)) -> SHR(min(A+B, 256), X)
|
2019-11-07 20:29:56 +00:00
|
|
|
Builtins::SHR(B, Builtins::SHR(A, X)),
|
2019-03-12 10:41:47 +00:00
|
|
|
[=]() -> Pattern {
|
|
|
|
bigint sum = bigint(A.d()) + B.d();
|
2019-11-06 21:39:13 +00:00
|
|
|
if (sum >= Pattern::WordSize)
|
2019-11-07 20:29:56 +00:00
|
|
|
return Builtins::AND(X, Word(0));
|
2019-03-12 10:41:47 +00:00
|
|
|
else
|
2019-11-07 20:29:56 +00:00
|
|
|
return Builtins::SHR(Word(sum), X);
|
2020-08-03 20:42:39 +00:00
|
|
|
}
|
2019-02-19 16:12:28 +00:00
|
|
|
});
|
|
|
|
|
2019-04-16 11:14:13 +00:00
|
|
|
// Combine SHL-SHR by constant
|
|
|
|
rules.push_back({
|
|
|
|
// SHR(B, SHL(A, X)) -> AND(SH[L/R]([B - A / A - B], X), Mask)
|
2019-11-07 20:29:56 +00:00
|
|
|
Builtins::SHR(B, Builtins::SHL(A, X)),
|
2019-04-16 11:14:13 +00:00
|
|
|
[=]() -> Pattern {
|
2019-11-06 21:39:13 +00:00
|
|
|
Word mask = shlWorkaround(~Word(0), unsigned(A.d())) >> unsigned(B.d());
|
2019-04-16 11:14:13 +00:00
|
|
|
|
|
|
|
if (A.d() > B.d())
|
2019-11-07 20:29:56 +00:00
|
|
|
return Builtins::AND(Builtins::SHL(A.d() - B.d(), X), mask);
|
2019-04-16 11:14:13 +00:00
|
|
|
else if (B.d() > A.d())
|
2019-11-07 20:29:56 +00:00
|
|
|
return Builtins::AND(Builtins::SHR(B.d() - A.d(), X), mask);
|
2019-04-16 11:14:13 +00:00
|
|
|
else
|
2019-11-07 20:29:56 +00:00
|
|
|
return Builtins::AND(X, mask);
|
2019-04-16 11:14:13 +00:00
|
|
|
},
|
2019-11-06 21:39:13 +00:00
|
|
|
[=] { return A.d() < Pattern::WordSize && B.d() < Pattern::WordSize; }
|
2019-04-16 11:14:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Combine SHR-SHL by constant
|
|
|
|
rules.push_back({
|
|
|
|
// SHL(B, SHR(A, X)) -> AND(SH[L/R]([B - A / A - B], X), Mask)
|
2019-11-07 20:29:56 +00:00
|
|
|
Builtins::SHL(B, Builtins::SHR(A, X)),
|
2019-04-16 11:14:13 +00:00
|
|
|
[=]() -> Pattern {
|
2019-11-06 21:39:13 +00:00
|
|
|
Word mask = shlWorkaround((~Word(0)) >> unsigned(A.d()), unsigned(B.d()));
|
2019-04-16 11:14:13 +00:00
|
|
|
|
|
|
|
if (A.d() > B.d())
|
2019-11-07 20:29:56 +00:00
|
|
|
return Builtins::AND(Builtins::SHR(A.d() - B.d(), X), mask);
|
2019-04-16 11:14:13 +00:00
|
|
|
else if (B.d() > A.d())
|
2019-11-07 20:29:56 +00:00
|
|
|
return Builtins::AND(Builtins::SHL(B.d() - A.d(), X), mask);
|
2019-04-16 11:14:13 +00:00
|
|
|
else
|
2019-11-07 20:29:56 +00:00
|
|
|
return Builtins::AND(X, mask);
|
2019-04-16 11:14:13 +00:00
|
|
|
},
|
2019-11-06 21:39:13 +00:00
|
|
|
[=] { return A.d() < Pattern::WordSize && B.d() < Pattern::WordSize; }
|
2019-04-16 11:14:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Move AND with constant across SHL and SHR by constant
|
2019-11-07 20:29:56 +00:00
|
|
|
for (auto instr: {Instruction::SHL, Instruction::SHR})
|
2019-04-16 11:14:13 +00:00
|
|
|
{
|
2019-11-07 20:29:56 +00:00
|
|
|
typename Builtins::PatternGeneratorInstance shiftOp{instr};
|
2019-04-16 11:14:13 +00:00
|
|
|
auto replacement = [=]() -> Pattern {
|
2019-11-06 21:39:13 +00:00
|
|
|
Word mask =
|
2019-11-07 20:29:56 +00:00
|
|
|
instr == Instruction::SHL ?
|
2019-04-16 11:14:13 +00:00
|
|
|
shlWorkaround(A.d(), unsigned(B.d())) :
|
|
|
|
A.d() >> unsigned(B.d());
|
2019-11-07 20:29:56 +00:00
|
|
|
return Builtins::AND(shiftOp(B.d(), X), std::move(mask));
|
2019-04-16 11:14:13 +00:00
|
|
|
};
|
|
|
|
rules.push_back({
|
|
|
|
// SH[L/R](B, AND(X, A)) -> AND(SH[L/R](B, X), [ A << B / A >> B ])
|
2019-11-07 20:29:56 +00:00
|
|
|
shiftOp(B, Builtins::AND(X, A)),
|
2019-04-16 11:14:13 +00:00
|
|
|
replacement,
|
2019-11-06 21:39:13 +00:00
|
|
|
[=] { return B.d() < Pattern::WordSize; }
|
2019-04-16 11:14:13 +00:00
|
|
|
});
|
|
|
|
rules.push_back({
|
|
|
|
// SH[L/R](B, AND(A, X)) -> AND(SH[L/R](B, X), [ A << B / A >> B ])
|
2019-11-07 20:29:56 +00:00
|
|
|
shiftOp(B, Builtins::AND(A, X)),
|
2019-04-16 11:14:13 +00:00
|
|
|
replacement,
|
2019-11-06 21:39:13 +00:00
|
|
|
[=] { return B.d() < Pattern::WordSize; }
|
2019-04-16 11:14:13 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-04 09:23:59 +00:00
|
|
|
// Combine alternating AND/OR/AND with constant,
|
|
|
|
// AND(OR(AND(X, A), Y), B) -> OR(AND(X, A & B), AND(Y, B))
|
|
|
|
// Many versions due to commutativity.
|
|
|
|
for (auto const& inner: {Builtins::AND(X, A), Builtins::AND(A, X)})
|
|
|
|
for (auto const& second: {Builtins::OR(inner, Y), Builtins::OR(Y, inner)})
|
|
|
|
{
|
|
|
|
// We might swap X and Y but this is not an issue anymore.
|
|
|
|
rules.push_back({
|
|
|
|
Builtins::AND(second, B),
|
|
|
|
[=]() -> Pattern { return Builtins::OR(Builtins::AND(X, A.d() & B.d()), Builtins::AND(Y, B)); }
|
|
|
|
});
|
|
|
|
rules.push_back({
|
|
|
|
Builtins::AND(B, second),
|
|
|
|
[=]() -> Pattern { return Builtins::OR(Builtins::AND(X, A.d() & B.d()), Builtins::AND(Y, B)); }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-14 14:01:50 +00:00
|
|
|
rules.push_back({
|
|
|
|
// MUL(X, SHL(Y, 1)) -> SHL(Y, X)
|
2019-11-07 20:29:56 +00:00
|
|
|
Builtins::MUL(X, Builtins::SHL(Y, Word(1))),
|
2019-05-14 14:01:50 +00:00
|
|
|
[=]() -> Pattern {
|
2019-11-07 20:29:56 +00:00
|
|
|
return Builtins::SHL(Y, X);
|
2020-08-03 20:42:39 +00:00
|
|
|
}
|
2019-05-14 14:01:50 +00:00
|
|
|
});
|
|
|
|
rules.push_back({
|
|
|
|
// MUL(SHL(X, 1), Y) -> SHL(X, Y)
|
2019-11-07 20:29:56 +00:00
|
|
|
Builtins::MUL(Builtins::SHL(X, Word(1)), Y),
|
2019-05-14 14:01:50 +00:00
|
|
|
[=]() -> Pattern {
|
2019-11-07 20:29:56 +00:00
|
|
|
return Builtins::SHL(X, Y);
|
2020-08-03 20:42:39 +00:00
|
|
|
}
|
2019-05-14 14:01:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
rules.push_back({
|
|
|
|
// DIV(X, SHL(Y, 1)) -> SHR(Y, X)
|
2019-11-07 20:29:56 +00:00
|
|
|
Builtins::DIV(X, Builtins::SHL(Y, Word(1))),
|
2019-05-14 14:01:50 +00:00
|
|
|
[=]() -> Pattern {
|
2019-11-07 20:29:56 +00:00
|
|
|
return Builtins::SHR(Y, X);
|
2020-08-03 20:42:39 +00:00
|
|
|
}
|
2019-05-14 14:01:50 +00:00
|
|
|
});
|
2019-03-13 11:23:01 +00:00
|
|
|
|
|
|
|
std::function<bool()> feasibilityFunction = [=]() {
|
2019-11-06 21:39:13 +00:00
|
|
|
if (B.d() > Pattern::WordSize)
|
2019-03-13 11:23:01 +00:00
|
|
|
return false;
|
|
|
|
unsigned bAsUint = static_cast<unsigned>(B.d());
|
2019-11-06 21:39:13 +00:00
|
|
|
return (A.d() & ((~Word(0)) >> bAsUint)) == ((~Word(0)) >> bAsUint);
|
2019-03-13 11:23:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
rules.push_back({
|
|
|
|
// AND(A, SHR(B, X)) -> A & ((2^256-1) >> B) == ((2^256-1) >> B)
|
2019-11-07 20:29:56 +00:00
|
|
|
Builtins::AND(A, Builtins::SHR(B, X)),
|
|
|
|
[=]() -> Pattern { return Builtins::SHR(B, X); },
|
2019-03-13 11:23:01 +00:00
|
|
|
feasibilityFunction
|
|
|
|
});
|
|
|
|
|
|
|
|
rules.push_back({
|
|
|
|
// AND(SHR(B, X), A) -> ((2^256-1) >> B) & A == ((2^256-1) >> B)
|
2019-11-07 20:29:56 +00:00
|
|
|
Builtins::AND(Builtins::SHR(B, X), A),
|
|
|
|
[=]() -> Pattern { return Builtins::SHR(B, X); },
|
2019-03-13 11:23:01 +00:00
|
|
|
feasibilityFunction
|
|
|
|
});
|
|
|
|
|
2022-09-15 10:19:18 +00:00
|
|
|
rules.push_back({
|
|
|
|
// AND(SHL(Z, X), SHL(Z, Y)) -> SHL(Z, AND(X, Y))
|
|
|
|
Builtins::AND(Builtins::SHL(Z, X), Builtins::SHL(Z, Y)),
|
|
|
|
[=]() -> Pattern { return Builtins::SHL(Z, Builtins::AND(X, Y)); }
|
|
|
|
});
|
|
|
|
|
2020-06-04 09:11:53 +00:00
|
|
|
rules.push_back({
|
|
|
|
Builtins::BYTE(A, Builtins::SHL(B, X)),
|
|
|
|
[=]() -> Pattern { return Builtins::BYTE(A.d() + B.d() / 8, X); },
|
|
|
|
[=] { return B.d() % 8 == 0 && A.d() <= 32 && B.d() <= 256; }
|
|
|
|
});
|
|
|
|
|
|
|
|
rules.push_back({
|
|
|
|
Builtins::BYTE(A, Builtins::SHR(B, X)),
|
2020-08-02 11:29:19 +00:00
|
|
|
[=]() -> Pattern { return Word(0); },
|
2020-08-04 09:25:09 +00:00
|
|
|
[=] { return A.d() < B.d() / 8; }
|
2020-08-02 11:29:19 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
rules.push_back({
|
|
|
|
Builtins::BYTE(A, Builtins::SHR(B, X)),
|
|
|
|
[=]() -> Pattern { return Builtins::BYTE(A.d() - B.d() / 8, X); },
|
|
|
|
[=] {
|
|
|
|
return B.d() % 8 == 0 && A.d() < Pattern::WordSize / 8 && B.d() <= Pattern::WordSize && A.d() >= B.d() / 8;
|
|
|
|
}
|
2020-06-04 09:11:53 +00:00
|
|
|
});
|
|
|
|
|
2021-08-05 09:32:28 +00:00
|
|
|
rules.push_back({
|
|
|
|
Builtins::SHL(A, Builtins::SIGNEXTEND(B, X)),
|
|
|
|
[=]() -> Pattern { return Builtins::SIGNEXTEND((A.d() >> 3) + B.d(), Builtins::SHL(A, X)); },
|
|
|
|
[=] { return (A.d() & 7) == 0 && A.d() <= Pattern::WordSize && B.d() <= Pattern::WordSize / 8; }
|
|
|
|
});
|
|
|
|
|
|
|
|
rules.push_back({
|
|
|
|
Builtins::SIGNEXTEND(A, Builtins::SHR(B, X)),
|
|
|
|
[=]() -> Pattern { return Builtins::SAR(B, X); },
|
|
|
|
[=] {
|
|
|
|
return
|
|
|
|
B.d() % 8 == 0 &&
|
|
|
|
B.d() <= Pattern::WordSize &&
|
|
|
|
A.d() <= Pattern::WordSize &&
|
|
|
|
(Pattern::WordSize - B.d()) / 8 == A.d() + 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-01-18 11:34:36 +00:00
|
|
|
return rules;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Pattern>
|
|
|
|
std::vector<SimplificationRule<Pattern>> simplificationRuleListPart8(
|
|
|
|
Pattern A,
|
|
|
|
Pattern,
|
|
|
|
Pattern,
|
|
|
|
Pattern X,
|
|
|
|
Pattern Y
|
|
|
|
)
|
|
|
|
{
|
2019-11-07 20:29:56 +00:00
|
|
|
using Builtins = typename Pattern::Builtins;
|
2019-01-18 11:34:36 +00:00
|
|
|
std::vector<SimplificationRule<Pattern>> rules;
|
|
|
|
|
2018-01-17 16:56:33 +00:00
|
|
|
// move constants across subtractions
|
2018-02-06 11:20:00 +00:00
|
|
|
rules += std::vector<SimplificationRule<Pattern>>{
|
2018-01-17 16:56:33 +00:00
|
|
|
{
|
|
|
|
// X - A -> X + (-A)
|
2019-11-07 20:29:56 +00:00
|
|
|
Builtins::SUB(X, A),
|
2020-08-03 20:42:39 +00:00
|
|
|
[=]() -> Pattern { return Builtins::ADD(X, 0 - A.d()); }
|
2018-01-17 16:56:33 +00:00
|
|
|
}, {
|
|
|
|
// (X + A) - Y -> (X - Y) + A
|
2019-11-07 20:29:56 +00:00
|
|
|
Builtins::SUB(Builtins::ADD(X, A), Y),
|
2020-08-03 20:42:39 +00:00
|
|
|
[=]() -> Pattern { return Builtins::ADD(Builtins::SUB(X, Y), A); }
|
2018-01-17 16:56:33 +00:00
|
|
|
}, {
|
|
|
|
// (A + X) - Y -> (X - Y) + A
|
2019-11-07 20:29:56 +00:00
|
|
|
Builtins::SUB(Builtins::ADD(A, X), Y),
|
2020-08-03 20:42:39 +00:00
|
|
|
[=]() -> Pattern { return Builtins::ADD(Builtins::SUB(X, Y), A); }
|
2018-01-17 16:56:33 +00:00
|
|
|
}, {
|
|
|
|
// X - (Y + A) -> (X - Y) + (-A)
|
2019-11-07 20:29:56 +00:00
|
|
|
Builtins::SUB(X, Builtins::ADD(Y, A)),
|
2020-08-03 20:42:39 +00:00
|
|
|
[=]() -> Pattern { return Builtins::ADD(Builtins::SUB(X, Y), 0 - A.d()); }
|
2018-01-17 16:56:33 +00:00
|
|
|
}, {
|
|
|
|
// X - (A + Y) -> (X - Y) + (-A)
|
2019-11-07 20:29:56 +00:00
|
|
|
Builtins::SUB(X, Builtins::ADD(A, Y)),
|
2020-08-03 20:42:39 +00:00
|
|
|
[=]() -> Pattern { return Builtins::ADD(Builtins::SUB(X, Y), 0 - A.d()); }
|
2021-08-18 16:27:57 +00:00
|
|
|
}, {
|
|
|
|
// (X - A) - Y -> (X - Y) - A
|
|
|
|
Builtins::SUB(Builtins::SUB(X, A), Y),
|
|
|
|
[=]() -> Pattern { return Builtins::SUB(Builtins::SUB(X, Y), A); }
|
|
|
|
}, {
|
|
|
|
// (A - X) - Y -> A - (X + Y)
|
|
|
|
Builtins::SUB(Builtins::SUB(A, X), Y),
|
|
|
|
[=]() -> Pattern { return Builtins::SUB(A, Builtins::ADD(X, Y)); }
|
|
|
|
}, {
|
|
|
|
// X - (Y - A) -> (X - Y) + A
|
|
|
|
Builtins::SUB(X, Builtins::SUB(Y, A)),
|
|
|
|
[=]() -> Pattern { return Builtins::ADD(Builtins::SUB(X, Y), A.d()); }
|
|
|
|
}, {
|
|
|
|
// X - (A - Y) -> (X + Y) + (-A)
|
|
|
|
Builtins::SUB(X, Builtins::SUB(A, Y)),
|
|
|
|
[=]() -> Pattern { return Builtins::ADD(Builtins::ADD(X, Y), 0 - A.d()); }
|
2018-01-17 16:56:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
return rules;
|
|
|
|
}
|
|
|
|
|
2020-05-15 23:59:57 +00:00
|
|
|
template<class Pattern>
|
|
|
|
std::vector<SimplificationRule<Pattern>> evmRuleList(
|
|
|
|
langutil::EVMVersion _evmVersion,
|
2021-03-11 10:30:22 +00:00
|
|
|
Pattern A,
|
2020-05-15 23:59:57 +00:00
|
|
|
Pattern,
|
|
|
|
Pattern,
|
|
|
|
Pattern,
|
2020-07-28 18:26:22 +00:00
|
|
|
Pattern X,
|
2020-05-15 23:59:57 +00:00
|
|
|
Pattern,
|
|
|
|
Pattern
|
|
|
|
)
|
|
|
|
{
|
|
|
|
using Builtins = typename Pattern::Builtins;
|
2020-07-28 18:26:22 +00:00
|
|
|
using Word = typename Pattern::Word;
|
2020-05-15 23:59:57 +00:00
|
|
|
std::vector<SimplificationRule<Pattern>> rules;
|
|
|
|
|
|
|
|
if (_evmVersion.hasSelfBalance())
|
|
|
|
rules.push_back({
|
|
|
|
Builtins::BALANCE(Instruction::ADDRESS),
|
2020-08-03 20:42:39 +00:00
|
|
|
[]() -> Pattern { return Instruction::SELFBALANCE; }
|
2020-05-15 23:59:57 +00:00
|
|
|
});
|
|
|
|
|
2020-07-28 18:26:22 +00:00
|
|
|
rules.emplace_back(
|
|
|
|
Builtins::EXP(0, X),
|
|
|
|
[=]() -> Pattern { return Builtins::ISZERO(X); }
|
|
|
|
);
|
|
|
|
rules.emplace_back(
|
|
|
|
Builtins::EXP(1, X),
|
|
|
|
[=]() -> Pattern { return Word(1); }
|
|
|
|
);
|
|
|
|
if (_evmVersion.hasBitwiseShifting())
|
2021-03-11 10:30:22 +00:00
|
|
|
{
|
2020-07-28 18:26:22 +00:00
|
|
|
rules.emplace_back(
|
|
|
|
Builtins::EXP(2, X),
|
|
|
|
[=]() -> Pattern { return Builtins::SHL(X, 1); }
|
|
|
|
);
|
2021-03-11 10:30:22 +00:00
|
|
|
rules.emplace_back(
|
|
|
|
Builtins::MUL(A, X),
|
|
|
|
[=]() -> Pattern { return Builtins::SHL(u256(*binaryLogarithm(A.d())), X); },
|
|
|
|
[=] { return binaryLogarithm(A.d()).has_value(); }
|
|
|
|
);
|
|
|
|
rules.emplace_back(
|
|
|
|
Builtins::MUL(X, A),
|
|
|
|
[=]() -> Pattern { return Builtins::SHL(u256(*binaryLogarithm(A.d())), X); },
|
|
|
|
[=] { return binaryLogarithm(A.d()).has_value(); }
|
|
|
|
);
|
|
|
|
rules.emplace_back(
|
|
|
|
Builtins::DIV(X, A),
|
|
|
|
[=]() -> Pattern { return Builtins::SHR(u256(*binaryLogarithm(A.d())), X); },
|
|
|
|
[=] { return binaryLogarithm(A.d()).has_value(); }
|
|
|
|
);
|
|
|
|
}
|
2020-09-25 15:05:15 +00:00
|
|
|
rules.emplace_back(
|
|
|
|
Builtins::EXP(Word(-1), X),
|
|
|
|
[=]() -> Pattern
|
|
|
|
{
|
|
|
|
return Builtins::SUB(
|
|
|
|
Builtins::ISZERO(Builtins::AND(X, Word(1))),
|
|
|
|
Builtins::AND(X, Word(1))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2020-07-28 18:26:22 +00:00
|
|
|
|
2020-05-15 23:59:57 +00:00
|
|
|
return rules;
|
|
|
|
}
|
|
|
|
|
2018-09-14 13:49:55 +00:00
|
|
|
/// @returns a list of simplification rules given certain match placeholders.
|
2019-09-09 11:37:25 +00:00
|
|
|
/// A, B and C should represent constants, W, X, Y, and Z arbitrary expressions.
|
2018-09-14 13:49:55 +00:00
|
|
|
/// The simplifications should never change the order of evaluation of
|
|
|
|
/// arbitrary operations.
|
|
|
|
template <class Pattern>
|
|
|
|
std::vector<SimplificationRule<Pattern>> simplificationRuleList(
|
2020-05-15 23:59:57 +00:00
|
|
|
std::optional<langutil::EVMVersion> _evmVersion,
|
2018-09-14 13:49:55 +00:00
|
|
|
Pattern A,
|
|
|
|
Pattern B,
|
|
|
|
Pattern C,
|
2019-09-09 11:37:25 +00:00
|
|
|
Pattern W,
|
2018-09-14 13:49:55 +00:00
|
|
|
Pattern X,
|
2019-09-09 11:37:25 +00:00
|
|
|
Pattern Y,
|
|
|
|
Pattern Z
|
2018-09-14 13:49:55 +00:00
|
|
|
)
|
|
|
|
{
|
2019-11-06 21:39:13 +00:00
|
|
|
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, "");
|
|
|
|
|
2018-09-14 13:49:55 +00:00
|
|
|
std::vector<SimplificationRule<Pattern>> rules;
|
2019-09-09 11:37:25 +00:00
|
|
|
rules += simplificationRuleListPart1(A, B, C, W, X);
|
|
|
|
rules += simplificationRuleListPart2(A, B, C, W, X);
|
|
|
|
rules += simplificationRuleListPart3(A, B, C, W, X);
|
|
|
|
rules += simplificationRuleListPart4(A, B, C, W, X);
|
2020-04-20 13:47:58 +00:00
|
|
|
rules += simplificationRuleListPart4_5(A, B, C, W, X);
|
2022-06-11 10:43:55 +00:00
|
|
|
rules += simplificationRuleListPart5(_evmVersion.has_value(), A, B, C, W, X);
|
2019-09-09 11:37:25 +00:00
|
|
|
rules += simplificationRuleListPart6(A, B, C, W, X);
|
2022-09-15 10:19:18 +00:00
|
|
|
rules += simplificationRuleListPart7(A, B, C, W, X, Y);
|
2019-09-09 11:37:25 +00:00
|
|
|
rules += simplificationRuleListPart8(A, B, C, W, X);
|
2020-05-15 23:59:57 +00:00
|
|
|
|
|
|
|
if (_evmVersion.has_value())
|
|
|
|
rules += evmRuleList(*_evmVersion, A, B, C, W, X, Y, Z);
|
|
|
|
|
2018-09-14 13:49:55 +00:00
|
|
|
return rules;
|
|
|
|
}
|
|
|
|
|
2018-01-17 16:56:33 +00:00
|
|
|
}
|