2
2
3
3
using namespace block ;
4
4
5
- dominator_tree::dominator_tree (std::vector<std::shared_ptr<basic_block>> &cfg) : cfg_(cfg) {
5
+ dominator_analysis::dominator_analysis (basic_block::cfg_block &cfg) : cfg_(cfg) {
6
6
// TODO: Add a check for size, it should be greater than 2.
7
7
idom.reserve (cfg_.size ());
8
8
idom.assign (cfg_.size (), -1 );
@@ -14,7 +14,7 @@ dominator_tree::dominator_tree(std::vector<std::shared_ptr<basic_block>> &cfg) :
14
14
analyze ();
15
15
}
16
16
17
- void dominator_tree ::postorder_dfs_helper (std::vector<bool > &visited_bbs, int id) {
17
+ void dominator_analysis ::postorder_dfs_helper (std::vector<bool > &visited_bbs, int id) {
18
18
for (auto child: cfg_[id]->successor ) {
19
19
if (!visited_bbs[child->id ]) {
20
20
visited_bbs[child->id ] = true ;
@@ -23,7 +23,7 @@ void dominator_tree::postorder_dfs_helper(std::vector<bool> &visited_bbs, int id
23
23
}
24
24
}
25
25
}
26
- void dominator_tree ::postorder_dfs () {
26
+ void dominator_analysis ::postorder_dfs () {
27
27
std::vector<bool > visited_bbs (cfg_.size ());
28
28
visited_bbs.assign (visited_bbs.size (), false );
29
29
visited_bbs[0 ] = true ;
@@ -32,26 +32,39 @@ void dominator_tree::postorder_dfs() {
32
32
postorder.push_back (0 );
33
33
}
34
34
35
- std::vector<int > &dominator_tree ::get_postorder_bb_map () {
35
+ std::vector<int > &dominator_analysis ::get_postorder_bb_map () {
36
36
return postorder_bb_map;
37
37
}
38
38
39
- std::vector<int > &dominator_tree ::get_postorder () {
39
+ std::vector<int > &dominator_analysis ::get_postorder () {
40
40
return postorder;
41
41
}
42
42
43
- std::vector<int > &dominator_tree ::get_idom () {
43
+ std::vector<int > &dominator_analysis ::get_idom () {
44
44
return idom;
45
45
}
46
46
47
- int dominator_tree::get_idom (int bb_id) {
48
- if (bb_id >= 0 && bb_id < idom.size ()) {
47
+ std::map<int , std::vector<int >> &dominator_analysis::get_idom_map () {
48
+ return idom_map;
49
+ }
50
+
51
+ int dominator_analysis::get_idom (int bb_id) {
52
+ if (bb_id < 0 || bb_id >= (int )idom.size ()) {
49
53
return -1 ;
50
54
}
51
55
52
56
return idom[bb_id];
53
57
}
54
- bool dominator_tree::dominates (int bb1_id, int bb2_id) {
58
+
59
+ std::vector<int > dominator_analysis::get_idom_map (int bb_id) {
60
+ if (bb_id < 0 || bb_id >= (int )idom_map.size ()) {
61
+ return {};
62
+ }
63
+
64
+ return idom_map[bb_id];
65
+ }
66
+
67
+ bool dominator_analysis::dominates (int bb1_id, int bb2_id) {
55
68
if (bb1_id == 0 ) {
56
69
return true ;
57
70
}
@@ -67,11 +80,11 @@ bool dominator_tree::dominates(int bb1_id, int bb2_id) {
67
80
return false ;
68
81
}
69
82
70
- bool dominator_tree ::is_reachable_from_entry (int bb_id) {
83
+ bool dominator_analysis ::is_reachable_from_entry (int bb_id) {
71
84
return dominates (0 , bb_id);
72
85
}
73
86
74
- int dominator_tree ::intersect (int bb1_id, int bb2_id) {
87
+ int dominator_analysis ::intersect (int bb1_id, int bb2_id) {
75
88
while (bb1_id != bb2_id) {
76
89
if (postorder_bb_map[bb1_id] < postorder_bb_map[bb2_id]) {
77
90
bb1_id = idom[bb1_id];
@@ -84,7 +97,7 @@ int dominator_tree::intersect(int bb1_id, int bb2_id) {
84
97
return bb1_id;
85
98
}
86
99
87
- void dominator_tree ::analyze () {
100
+ void dominator_analysis ::analyze () {
88
101
postorder_dfs ();
89
102
for (unsigned int i = 0 ; i < postorder.size (); i++) {
90
103
postorder_bb_map[postorder[i]] = i;
@@ -113,4 +126,23 @@ void dominator_tree::analyze() {
113
126
}
114
127
}
115
128
} while (change);
129
+
130
+
131
+ // build a map of dominators for easy traversal.
132
+ for (unsigned int i = 0 ; i < idom.size (); i++) {
133
+ idom_map[idom[i]].push_back (i);
134
+ }
135
+
136
+ for (unsigned int i = 0 ; i < idom.size (); i++) {
137
+ if (idom_map[i].empty ())
138
+ idom_map[i].push_back (-1 );
139
+ }
140
+
141
+ // for (auto key: idom_map) {
142
+ // std::cout << key.first << ": ";
143
+ // for (int id: key.second) {
144
+ // std::cout << id << " ";
145
+ // }
146
+ // std::cout << "\n";
147
+ // }
116
148
}
0 commit comments