Skip to content

Commit 9d91203

Browse files
WIP v3: got majority test cases working
1 parent fdb906f commit 9d91203

File tree

6 files changed

+326
-46
lines changed

6 files changed

+326
-46
lines changed

include/blocks/basic_blocks.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,13 @@ class basic_block {
1010
public:
1111
typedef std::vector<std::shared_ptr<basic_block>> cfg_block;
1212
basic_block(std::string label): name(label) {};
13-
// only does a shallow copy (leaves out predecessors and successors)
14-
// basic_block(basic_block &bb) {
15-
// bb.branch_expr = branch_expr;
16-
// bb.then_branch = then_branch;
17-
// bb.else_branch = else_branch;
18-
// bb.parent = parent;
19-
// bb.ast_index = ast_index;
20-
// bb.ast_depth = ast_depth;
21-
// bb.id = id;
22-
// bb.name = name;
23-
// }
2413

2514
cfg_block predecessor;
2615
cfg_block successor;
2716
block::expr::Ptr branch_expr;
2817
std::shared_ptr<basic_block> then_branch;
2918
std::shared_ptr<basic_block> else_branch;
19+
bool is_exit_block;
3020
block::stmt::Ptr parent;
3121
unsigned int ast_index;
3222
unsigned int ast_depth;

include/blocks/dominance.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class dominator_analysis {
2222
bool is_postdom_;
2323
std::vector<int> &get_postorder_bb_map();
2424
std::vector<int> &get_postorder();
25+
std::vector<int> &get_preorder_bb_map();
26+
std::vector<int> &get_preorder();
2527
std::vector<int> &get_idom();
2628
std::map<int, std::vector<int>> &get_idom_map();
2729
std::vector<int> &get_postorder_idom_map();
@@ -38,10 +40,14 @@ class dominator_analysis {
3840
std::vector<int> postorder_idom;
3941
std::vector<int> postorder;
4042
std::vector<int> postorder_bb_map;
43+
std::vector<int> preorder;
44+
std::vector<int> preorder_bb_map;
4145
void reverse_cfg();
4246
void postorder_idom_helper(std::vector<bool> &visited, int id);
4347
void postorder_dfs_helper(std::vector<bool> &visited_bbs, int id);
4448
void postorder_dfs();
49+
void preorder_dfs_helper(std::vector<bool> &visited_bbs, int id);
50+
void preorder_dfs();
4551
int intersect(int bb1_id, int bb2_id);
4652
};
4753

src/blocks/basic_blocks.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) {
8181
auto exit_bb = std::make_shared<basic_block>("exit" + std::to_string(basic_block_count));
8282
// assign it a empty stmt_block as parent
8383
exit_bb->parent = std::make_shared<stmt_block>();
84+
// mark the basic block as exit block
85+
exit_bb->is_exit_block = true;
8486
// add mapping in ast to bb map
8587
// exit_bb->ast_to_basic_block_map[exit_bb->parent] = exit_bb;
8688
// set the ast depth of the basic block

src/blocks/dominance.cpp

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ dominator_analysis::dominator_analysis(basic_block::cfg_block cfg, bool is_postd
4040
postorder.reserve(cfg_.size());
4141
postorder_bb_map.reserve(cfg_.size());
4242
postorder_bb_map.assign(cfg_.size(), -1);
43+
preorder.reserve(cfg_.size());
44+
preorder_bb_map.reserve(cfg_.size());
45+
preorder_bb_map.assign(cfg_.size(), -1);
4346

4447
// and call the anaylse function
4548
analyze();
@@ -83,6 +86,34 @@ void dominator_analysis::postorder_dfs() {
8386
}
8487
}
8588

89+
void dominator_analysis::preorder_dfs_helper(std::vector<bool> &visited_bbs, int id) {
90+
for (auto child: cfg_[id]->successor) {
91+
if (!visited_bbs[child->id]) {
92+
visited_bbs[child->id] = true;
93+
preorder.push_back(child->id);
94+
preorder_dfs_helper(visited_bbs, child->id);
95+
}
96+
}
97+
}
98+
99+
void dominator_analysis::preorder_dfs() {
100+
std::vector<bool> visited_bbs(cfg_.size());
101+
visited_bbs.assign(visited_bbs.size(), false);
102+
if (is_postdom_)
103+
visited_bbs[cfg_.size() - 1] = true;
104+
else
105+
visited_bbs[0] = true;
106+
107+
if (is_postdom_) {
108+
preorder.push_back(cfg_.size() - 1);
109+
preorder_dfs_helper(visited_bbs, cfg_.size() - 1);
110+
}
111+
else {
112+
preorder.push_back(0);
113+
preorder_dfs_helper(visited_bbs, 0);
114+
}
115+
}
116+
86117
std::vector<int> &dominator_analysis::get_postorder_bb_map() {
87118
return postorder_bb_map;
88119
}
@@ -91,6 +122,14 @@ std::vector<int> &dominator_analysis::get_postorder() {
91122
return postorder;
92123
}
93124

125+
std::vector<int> &dominator_analysis::get_preorder_bb_map() {
126+
return preorder_bb_map;
127+
}
128+
129+
std::vector<int> &dominator_analysis::get_preorder() {
130+
return preorder;
131+
}
132+
94133
std::vector<int> &dominator_analysis::get_idom() {
95134
return idom;
96135
}
@@ -161,11 +200,14 @@ int dominator_analysis::intersect(int bb1_id, int bb2_id) {
161200
}
162201

163202
void dominator_analysis::analyze() {
164-
postorder_dfs();
203+
preorder_dfs();
204+
postorder_dfs();
205+
for (unsigned int i = 0; i < preorder.size(); i++) {
206+
preorder_bb_map[preorder[i]] = i;
207+
}
165208
for (unsigned int i = 0; i < postorder.size(); i++) {
166209
postorder_bb_map[postorder[i]] = i;
167210
}
168-
169211
if (is_postdom_)
170212
idom[cfg_.size() - 1] = cfg_.size() - 1;
171213
else

0 commit comments

Comments
 (0)