forked from ethereum/solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSSACFGTopologicalSort.h
58 lines (47 loc) · 2.03 KB
/
SSACFGTopologicalSort.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
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/>.
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <libyul/backends/evm/SSAControlFlowGraph.h>
#include <cstddef>
#include <set>
#include <vector>
namespace solidity::yul
{
/// Performs a topological sort on the forward CFG (no back/cross edges)
class ForwardSSACFGTopologicalSort
{
public:
explicit ForwardSSACFGTopologicalSort(SSACFG const& _cfg);
std::vector<size_t> const& preOrder() const { return m_preOrder; }
std::vector<size_t> const& postOrder() const { return m_postOrder; }
std::set<size_t> const& backEdgeTargets() const { return m_backEdgeTargets; }
SSACFG const& cfg() const { return m_cfg; }
bool backEdge(SSACFG::BlockId const& _block1, SSACFG::BlockId const& _block2) const;
size_t preOrderIndexOf(size_t _block) const { return m_blockWisePreOrder[_block]; }
size_t maxSubtreePreOrderIndexOf(size_t _block) const { return m_blockWiseMaxSubtreePreOrder[_block]; }
private:
void dfs(size_t _vertex);
/// Checks if block1 is an ancestor of block2, ie there's a path from block1 to block2 in the dfs tree
bool ancestor(size_t _block1, size_t _block2) const;
SSACFG const& m_cfg;
std::vector<char> m_explored{};
std::vector<size_t> m_postOrder{};
std::vector<size_t> m_preOrder{};
std::vector<size_t> m_blockWisePreOrder{};
std::vector<size_t> m_blockWiseMaxSubtreePreOrder{};
std::vector<std::tuple<size_t, size_t>> m_potentialBackEdges{};
std::set<size_t> m_backEdgeTargets{};
};
}