22
33using namespace block ;
44
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) {
66 // TODO: Add a check for size, it should be greater than 2.
77 idom.reserve (cfg_.size ());
88 idom.assign (cfg_.size (), -1 );
@@ -14,7 +14,7 @@ dominator_tree::dominator_tree(std::vector<std::shared_ptr<basic_block>> &cfg) :
1414 analyze ();
1515}
1616
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) {
1818 for (auto child: cfg_[id]->successor ) {
1919 if (!visited_bbs[child->id ]) {
2020 visited_bbs[child->id ] = true ;
@@ -23,7 +23,7 @@ void dominator_tree::postorder_dfs_helper(std::vector<bool> &visited_bbs, int id
2323 }
2424 }
2525}
26- void dominator_tree ::postorder_dfs () {
26+ void dominator_analysis ::postorder_dfs () {
2727 std::vector<bool > visited_bbs (cfg_.size ());
2828 visited_bbs.assign (visited_bbs.size (), false );
2929 visited_bbs[0 ] = true ;
@@ -32,26 +32,39 @@ void dominator_tree::postorder_dfs() {
3232 postorder.push_back (0 );
3333}
3434
35- std::vector<int > &dominator_tree ::get_postorder_bb_map () {
35+ std::vector<int > &dominator_analysis ::get_postorder_bb_map () {
3636 return postorder_bb_map;
3737}
3838
39- std::vector<int > &dominator_tree ::get_postorder () {
39+ std::vector<int > &dominator_analysis ::get_postorder () {
4040 return postorder;
4141}
4242
43- std::vector<int > &dominator_tree ::get_idom () {
43+ std::vector<int > &dominator_analysis ::get_idom () {
4444 return idom;
4545}
4646
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 ()) {
4953 return -1 ;
5054 }
5155
5256 return idom[bb_id];
5357}
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) {
5568 if (bb1_id == 0 ) {
5669 return true ;
5770 }
@@ -67,11 +80,11 @@ bool dominator_tree::dominates(int bb1_id, int bb2_id) {
6780 return false ;
6881}
6982
70- bool dominator_tree ::is_reachable_from_entry (int bb_id) {
83+ bool dominator_analysis ::is_reachable_from_entry (int bb_id) {
7184 return dominates (0 , bb_id);
7285}
7386
74- int dominator_tree ::intersect (int bb1_id, int bb2_id) {
87+ int dominator_analysis ::intersect (int bb1_id, int bb2_id) {
7588 while (bb1_id != bb2_id) {
7689 if (postorder_bb_map[bb1_id] < postorder_bb_map[bb2_id]) {
7790 bb1_id = idom[bb1_id];
@@ -84,7 +97,7 @@ int dominator_tree::intersect(int bb1_id, int bb2_id) {
8497 return bb1_id;
8598}
8699
87- void dominator_tree ::analyze () {
100+ void dominator_analysis ::analyze () {
88101 postorder_dfs ();
89102 for (unsigned int i = 0 ; i < postorder.size (); i++) {
90103 postorder_bb_map[postorder[i]] = i;
@@ -113,4 +126,23 @@ void dominator_tree::analyze() {
113126 }
114127 }
115128 } 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+ // }
116148}
0 commit comments