@@ -8,16 +8,28 @@ void dominator_analysis::reverse_cfg() {
88 assert (0 );
99
1010 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-
1411 for (auto bb: cfg_) {
1512 if (bb->successor .size () == 0 ) {
1613 bb->successor .push_back (virtual_exit_bb);
1714 virtual_exit_bb->predecessor .push_back (bb);
1815 }
1916 }
2017
18+ // if CFG is an inifite loop, we don't have a exit block
19+ // so we need to find the farthest block from the entry
20+ // of the loop and consider that as one of the exit blocks
21+ if (!virtual_exit_bb->predecessor .size ()) {
22+ std::cerr << " infinite loop\n " ;
23+ postorder_dfs (false );
24+
25+ auto bb_virtual_backedge = cfg_[max_depth_bb_id];
26+ bb_virtual_backedge->successor .push_back (virtual_exit_bb);
27+ virtual_exit_bb->predecessor .push_back (bb_virtual_backedge);
28+ }
29+
30+ virtual_exit_bb->id = cfg_.size ();
31+ cfg_.push_back (virtual_exit_bb);
32+
2133 for (auto bb: cfg_) {
2234 basic_block::cfg_block temp_pred = bb->predecessor ;
2335 bb->predecessor .clear ();
@@ -35,12 +47,17 @@ dominator_analysis::dominator_analysis(basic_block::cfg_block cfg, bool is_postd
3547 }
3648
3749 // TODO: Add a check for size, it should be greater than 2.
50+ idom.clear ();
3851 idom.reserve (cfg_.size ());
3952 idom.assign (cfg_.size (), -1 );
53+ postorder.clear ();
4054 postorder.reserve (cfg_.size ());
55+ postorder_bb_map.clear ();
4156 postorder_bb_map.reserve (cfg_.size ());
4257 postorder_bb_map.assign (cfg_.size (), -1 );
58+ preorder.clear ();
4359 preorder.reserve (cfg_.size ());
60+ preorder_bb_map.clear ();
4461 preorder_bb_map.reserve (cfg_.size ());
4562 preorder_bb_map.assign (cfg_.size (), -1 );
4663
@@ -58,30 +75,40 @@ void dominator_analysis::postorder_idom_helper(std::vector<bool> &visited, int i
5875 }
5976}
6077
61- void dominator_analysis::postorder_dfs_helper (std::vector<bool > &visited_bbs, int id) {
62- for (auto child: cfg_[id]->successor ) {
63- if (!visited_bbs[child->id ]) {
64- visited_bbs[child->id ] = true ;
65- postorder_dfs_helper (visited_bbs, child->id );
66- postorder.push_back (child->id );
67- }
78+ void dominator_analysis::postorder_dfs_helper (std::vector<bool > &visited_bbs, int id, int depth) {
79+ if (depth > max_depth) {
80+ max_depth = depth;
81+ max_depth_bb_id = id;
82+ }
83+
84+ for (auto child: cfg_[id]->successor ) {
85+ if (!visited_bbs[child->id ]) {
86+ visited_bbs[child->id ] = true ;
87+ postorder_dfs_helper (visited_bbs, child->id , depth + 1 );
88+ postorder.push_back (child->id );
6889 }
90+ }
6991}
7092
71- void dominator_analysis::postorder_dfs () {
93+ void dominator_analysis::postorder_dfs (bool reverse_cfg) {
94+ int current_depth = 0 ;
95+ max_depth = current_depth;
96+
7297 std::vector<bool > visited_bbs (cfg_.size ());
7398 visited_bbs.assign (visited_bbs.size (), false );
74- if (is_postdom_ )
99+ if (reverse_cfg )
75100 visited_bbs[cfg_.size () - 1 ] = true ;
76101 else
77102 visited_bbs[0 ] = true ;
78103
79- if (is_postdom_) {
80- postorder_dfs_helper (visited_bbs, cfg_.size () - 1 );
104+ if (reverse_cfg) {
105+ max_depth_bb_id = cfg_.size () - 1 ;
106+ postorder_dfs_helper (visited_bbs, cfg_.size () - 1 , current_depth + 1 );
81107 postorder.push_back (cfg_.size () - 1 );
82108 }
83109 else {
84- postorder_dfs_helper (visited_bbs, 0 );
110+ max_depth_bb_id = 0 ;
111+ postorder_dfs_helper (visited_bbs, 0 , current_depth + 1 );
85112 postorder.push_back (0 );
86113 }
87114}
@@ -96,15 +123,15 @@ void dominator_analysis::preorder_dfs_helper(std::vector<bool> &visited_bbs, int
96123 }
97124}
98125
99- void dominator_analysis::preorder_dfs () {
126+ void dominator_analysis::preorder_dfs (bool reverse_cfg ) {
100127 std::vector<bool > visited_bbs (cfg_.size ());
101128 visited_bbs.assign (visited_bbs.size (), false );
102- if (is_postdom_ )
129+ if (reverse_cfg )
103130 visited_bbs[cfg_.size () - 1 ] = true ;
104131 else
105132 visited_bbs[0 ] = true ;
106133
107- if (is_postdom_ ) {
134+ if (reverse_cfg ) {
108135 preorder.push_back (cfg_.size () - 1 );
109136 preorder_dfs_helper (visited_bbs, cfg_.size () - 1 );
110137 }
@@ -200,8 +227,8 @@ int dominator_analysis::intersect(int bb1_id, int bb2_id) {
200227}
201228
202229void dominator_analysis::analyze () {
203- preorder_dfs ();
204- postorder_dfs ();
230+ preorder_dfs (is_postdom_ );
231+ postorder_dfs (is_postdom_ );
205232 for (unsigned int i = 0 ; i < preorder.size (); i++) {
206233 preorder_bb_map[preorder[i]] = i;
207234 }
0 commit comments