Skip to content

Commit 2005723

Browse files
Now postdom can also handle infinite loops
1 parent 4bfdc9d commit 2005723

File tree

4 files changed

+68
-28
lines changed

4 files changed

+68
-28
lines changed

include/blocks/dominance.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class dominator_analysis {
2020
dominator_analysis(basic_block::cfg_block cfg, bool is_postdom = false);
2121
basic_block::cfg_block cfg_;
2222
bool is_postdom_;
23+
int max_depth;
24+
unsigned int max_depth_bb_id;
2325
std::vector<int> &get_postorder_bb_map();
2426
std::vector<int> &get_postorder();
2527
std::vector<int> &get_preorder_bb_map();
@@ -44,10 +46,10 @@ class dominator_analysis {
4446
std::vector<int> preorder_bb_map;
4547
void reverse_cfg();
4648
void postorder_idom_helper(std::vector<bool> &visited, int id);
47-
void postorder_dfs_helper(std::vector<bool> &visited_bbs, int id);
48-
void postorder_dfs();
49+
void postorder_dfs_helper(std::vector<bool> &visited_bbs, int id, int depth);
50+
void postorder_dfs(bool reverse_cfg);
4951
void preorder_dfs_helper(std::vector<bool> &visited_bbs, int id);
50-
void preorder_dfs();
52+
void preorder_dfs(bool reverse_cfg);
5153
int intersect(int bb1_id, int bb2_id);
5254
};
5355

src/blocks/dominance.cpp

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

202229
void 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
}

src/blocks/loops.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ void loop_info::analyze() {
184184
continue;
185185

186186
int unique_postdom = post_dta.get_idom(loop->loop_exit_blocks[0]->id);
187+
if (unique_postdom == -1)
188+
continue;
189+
187190
bool unique_postdom_flag = true;
188191
for (auto exit_bb: loop->loop_exit_blocks) {
189192
if (post_dta.get_idom(exit_bb->id) != unique_postdom) {
@@ -270,6 +273,8 @@ stmt::Ptr loop::convert_to_ast_impl(dominator_analysis &dta_, std::vector<std::p
270273
if_stmt::Ptr if_stmt_copy = std::make_shared<if_stmt>();
271274
if_stmt_copy->then_stmt = to<stmt>(std::make_shared<stmt_block>());
272275
if_stmt_copy->else_stmt = to<stmt>(std::make_shared<stmt_block>());
276+
if_stmt_copy->annotation = to<if_stmt>(bb->parent)->annotation;
277+
273278
if (condition_block == bb) {
274279
while_block->cond = to<if_stmt>(bb->parent)->cond;
275280

@@ -621,6 +626,7 @@ block::stmt_block::Ptr loop_info::convert_to_ast(block::stmt_block::Ptr ast) {
621626
if_stmt_copy->then_stmt = to<stmt>(std::make_shared<stmt_block>());
622627
if_stmt_copy->else_stmt = to<stmt>(std::make_shared<stmt_block>());
623628
if_stmt_copy->cond = to<if_stmt>(bb->parent)->cond;
629+
if_stmt_copy->annotation = to<if_stmt>(bb->parent)->annotation;
624630

625631
// push the then branch onto worklist. (worklist should be a pair <processed, destination>) ?
626632
if (bb->then_branch) {

src/builder/builder_context.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ block::stmt::Ptr builder_context::extract_ast_from_function_impl(void) {
294294
block::eliminate_redundant_vars(ast);
295295
}
296296

297+
// return ast;
297298
if (feature_unstructured)
298299
return ast;
299300

@@ -325,6 +326,8 @@ block::stmt::Ptr builder_context::extract_ast_from_function_impl(void) {
325326
dominator_analysis dom(BBs);
326327
dominator_analysis post_dom(post_BBs, true);
327328

329+
std::cerr << "max depth: " << dom.max_depth << "\n";
330+
std::cerr << "max depth bb id: " << dom.max_depth_bb_id << "\n";
328331
std::cerr << "== postorder map ==\n";
329332
for (int i: dom.get_postorder_bb_map()) {
330333
std::cerr << i << "\n";
@@ -382,6 +385,8 @@ block::stmt::Ptr builder_context::extract_ast_from_function_impl(void) {
382385
}
383386
std::cerr << "== postorder idom ==\n";
384387

388+
std::cerr << "(postdom) max depth: " << post_dom.max_depth << "\n";
389+
std::cerr << "(postdom) max depth bb id: " << post_dom.max_depth_bb_id << "\n";
385390
std::cerr << "== (postdom) postorder map ==\n";
386391
for (int i: post_dom.get_postorder_bb_map()) {
387392
std::cerr << i << "\n";
@@ -491,13 +496,13 @@ block::stmt::Ptr builder_context::extract_ast_from_function_impl(void) {
491496
std::cerr << "++++++ loop info ++++++ \n";
492497

493498
std::cerr << "++++++ convert to ast ++++++ \n";
494-
// ast = LI.convert_to_ast(block::to<block::stmt_block>(ast));
499+
ast = LI.convert_to_ast(block::to<block::stmt_block>(ast));
495500
std::cerr << "++++++ convert to ast ++++++ \n";
496501

497-
block::loop_finder finder;
498-
finder.ast = ast;
499-
ast->accept(&finder);
500-
return ast;
502+
// block::loop_finder finder;
503+
// finder.ast = ast;
504+
// ast->accept(&finder);
505+
// return ast;
501506

502507
block::for_loop_finder for_finder;
503508
for_finder.ast = ast;

0 commit comments

Comments
 (0)