22
33using namespace block ;
44
5- dominator_analysis::dominator_analysis (basic_block::cfg_block &cfg) : cfg_(cfg) {
5+ void dominator_analysis::reverse_cfg () {
6+ // TODO: Add a check for size, it should be greater than 2.
7+ if (cfg_.size () == 0 )
8+ assert (0 );
9+
10+ std::shared_ptr<basic_block> virtual_exit_bb = std::make_shared<basic_block>(" virtualexit0" );
11+ virtual_exit_bb->id = cfg_.size ();
12+ cfg_.push_back (virtual_exit_bb);
13+
14+ for (auto bb: cfg_) {
15+ if (bb->successor .size () == 0 ) {
16+ bb->successor .push_back (virtual_exit_bb);
17+ virtual_exit_bb->predecessor .push_back (bb);
18+ }
19+ }
20+
21+ for (auto bb: cfg_) {
22+ basic_block::cfg_block temp_pred = bb->predecessor ;
23+ bb->predecessor .clear ();
24+ bb->predecessor .insert (bb->predecessor .begin (), bb->successor .begin (), bb->successor .end ());
25+ std::reverse (bb->predecessor .begin (), bb->predecessor .end ());
26+ bb->successor .clear ();
27+ bb->successor .insert (bb->successor .begin (), temp_pred.begin (), temp_pred.end ());
28+ std::reverse (bb->successor .begin (), bb->successor .end ());
29+ }
30+ }
31+
32+ dominator_analysis::dominator_analysis (basic_block::cfg_block cfg, bool is_postdom) : cfg_(cfg), is_postdom_(is_postdom) {
33+ if (is_postdom) {
34+ reverse_cfg ();
35+ }
36+
637 // TODO: Add a check for size, it should be greater than 2.
738 idom.reserve (cfg_.size ());
839 idom.assign (cfg_.size (), -1 );
@@ -16,7 +47,6 @@ dominator_analysis::dominator_analysis(basic_block::cfg_block &cfg) : cfg_(cfg)
1647
1748void dominator_analysis::postorder_idom_helper (std::vector<bool > &visited, int id) {
1849 for (int idom_id: idom_map[id]) {
19- // std::cerr << idom_id << "\n";
2050 if (idom_id != -1 && !visited[idom_id]) {
2151 visited[idom_id] = true ;
2252 postorder_idom_helper (visited, idom_id);
@@ -26,21 +56,31 @@ void dominator_analysis::postorder_idom_helper(std::vector<bool> &visited, int i
2656}
2757
2858void dominator_analysis::postorder_dfs_helper (std::vector<bool > &visited_bbs, int id) {
29- for (auto child: cfg_[id]->successor ) {
30- if (!visited_bbs[child->id ]) {
31- visited_bbs[child->id ] = true ;
32- postorder_dfs_helper (visited_bbs, child->id );
33- postorder.push_back (child->id );
59+ for (auto child: cfg_[id]->successor ) {
60+ if (!visited_bbs[child->id ]) {
61+ visited_bbs[child->id ] = true ;
62+ postorder_dfs_helper (visited_bbs, child->id );
63+ postorder.push_back (child->id );
64+ }
3465 }
35- }
3666}
67+
3768void dominator_analysis::postorder_dfs () {
3869 std::vector<bool > visited_bbs (cfg_.size ());
3970 visited_bbs.assign (visited_bbs.size (), false );
40- visited_bbs[0 ] = true ;
41-
42- postorder_dfs_helper (visited_bbs, 0 );
43- postorder.push_back (0 );
71+ if (is_postdom_)
72+ visited_bbs[cfg_.size () - 1 ] = true ;
73+ else
74+ visited_bbs[0 ] = true ;
75+
76+ if (is_postdom_) {
77+ postorder_dfs_helper (visited_bbs, cfg_.size () - 1 );
78+ postorder.push_back (cfg_.size () - 1 );
79+ }
80+ else {
81+ postorder_dfs_helper (visited_bbs, 0 );
82+ postorder.push_back (0 );
83+ }
4484}
4585
4686std::vector<int > &dominator_analysis::get_postorder_bb_map () {
@@ -121,12 +161,16 @@ int dominator_analysis::intersect(int bb1_id, int bb2_id) {
121161}
122162
123163void dominator_analysis::analyze () {
124- postorder_dfs ();
164+ postorder_dfs ();
125165 for (unsigned int i = 0 ; i < postorder.size (); i++) {
126166 postorder_bb_map[postorder[i]] = i;
127167 }
128168
129- idom[0 ] = 0 ;
169+ if (is_postdom_)
170+ idom[cfg_.size () - 1 ] = cfg_.size () - 1 ;
171+ else
172+ idom[0 ] = 0 ;
173+
130174 bool change = false ;
131175
132176 do {
@@ -135,7 +179,7 @@ void dominator_analysis::analyze() {
135179 int postorder_bb_num = postorder[i];
136180 std::shared_ptr<basic_block> bb = cfg_[postorder_bb_num];
137181 int bb_id_idom = bb->predecessor [0 ]->id ;
138-
182+
139183 for (unsigned int j = 1 ; j < bb->predecessor .size (); j++) {
140184 int bb_id_idom_next = bb->predecessor [j]->id ;
141185 if (idom[bb_id_idom_next] != -1 ) {
@@ -150,7 +194,6 @@ void dominator_analysis::analyze() {
150194 }
151195 } while (change);
152196
153-
154197 // build a map of dominators for easy traversal.
155198 for (unsigned int i = 0 ; i < idom.size (); i++) {
156199 idom_map[idom[i]].push_back (i);
0 commit comments