/src/solidity/libyul/optimiser/KnowledgeBase.h
Line | Count | Source |
1 | | /* |
2 | | This file is part of solidity. |
3 | | |
4 | | solidity is free software: you can redistribute it and/or modify |
5 | | it under the terms of the GNU General Public License as published by |
6 | | the Free Software Foundation, either version 3 of the License, or |
7 | | (at your option) any later version. |
8 | | |
9 | | solidity is distributed in the hope that it will be useful, |
10 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | GNU General Public License for more details. |
13 | | |
14 | | You should have received a copy of the GNU General Public License |
15 | | along with solidity. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | // SPDX-License-Identifier: GPL-3.0 |
18 | | /** |
19 | | * Class that can answer questions about values of variables and their relations. |
20 | | */ |
21 | | |
22 | | #pragma once |
23 | | |
24 | | #include <libyul/ASTForward.h> |
25 | | #include <libyul/YulString.h> |
26 | | |
27 | | #include <libsolutil/Common.h> |
28 | | #include <libsolutil/Numeric.h> |
29 | | |
30 | | #include <map> |
31 | | #include <functional> |
32 | | |
33 | | namespace solidity::yul |
34 | | { |
35 | | |
36 | | struct Dialect; |
37 | | struct AssignedValue; |
38 | | |
39 | | /** |
40 | | * Class that can answer questions about values of variables and their relations. |
41 | | */ |
42 | | class KnowledgeBase |
43 | | { |
44 | | public: |
45 | | KnowledgeBase( |
46 | | Dialect const& _dialect, |
47 | | std::function<AssignedValue const*(YulString)> _variableValues |
48 | | ): |
49 | | m_dialect(_dialect), |
50 | | m_variableValues(std::move(_variableValues)) |
51 | 653k | {} |
52 | | |
53 | | bool knownToBeDifferent(YulString _a, YulString _b); |
54 | | std::optional<u256> differenceIfKnownConstant(YulString _a, YulString _b); |
55 | | bool knownToBeDifferentByAtLeast32(YulString _a, YulString _b); |
56 | 117k | bool knownToBeEqual(YulString _a, YulString _b) const { return _a == _b; } |
57 | | bool knownToBeZero(YulString _a); |
58 | | std::optional<u256> valueIfKnownConstant(YulString _a); |
59 | | |
60 | | private: |
61 | | Expression simplify(Expression _expression); |
62 | | Expression simplifyRecursively(Expression _expression); |
63 | | |
64 | | Dialect const& m_dialect; |
65 | | std::function<AssignedValue const*(YulString)> m_variableValues; |
66 | | size_t m_counter = 0; |
67 | | }; |
68 | | |
69 | | } |