Skip to content

Commit 67ed12f

Browse files
WIP v2: Add infra to construct a while loop using the loop_info analysis (34/48 tests pass)
1 parent 94be2dd commit 67ed12f

File tree

5 files changed

+270
-69
lines changed

5 files changed

+270
-69
lines changed

include/blocks/basic_blocks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <vector>
55
#include <deque>
66
#include <string>
7+
#include <map>
78

89
class basic_block {
910
public:
@@ -18,6 +19,7 @@ class basic_block {
1819
unsigned int ast_depth;
1920
unsigned int id;
2021
std::string name;
22+
// static std::map<block::stmt::Ptr, std::shared_ptr<basic_block>> ast_to_basic_block_map;
2123
};
2224

2325
basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast);

include/blocks/loops.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class loop_info {
4040
std::shared_ptr<loop> allocate_loop(std::shared_ptr<basic_block> header);
4141
block::stmt_block::Ptr convert_to_ast(block::stmt_block::Ptr ast);
4242
std::map<unsigned int, std::vector<int>> postorder_loops_map;
43+
std::map<unsigned int, std::vector<int>> preorder_loops_map;
4344
std::vector<std::shared_ptr<loop>> loops;
4445
std::vector<std::shared_ptr<loop>> top_level_loops;
4546

@@ -48,6 +49,7 @@ class loop_info {
4849
dominator_analysis dta;
4950
std::map<int, std::shared_ptr<loop>> bb_loop_map;
5051
void postorder_dfs_helper(std::vector<int> &postorder_loops_map, std::vector<bool> &visited_loops, int id);
52+
void preorder_dfs_helper(std::vector<int> &preorder_loops_map, std::vector<bool> &visited_loops, int id);
5153
// discover loops during traversal of the abstract syntax tree
5254
void analyze();
5355
};

src/blocks/basic_blocks.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) {
1313
for (auto st: ast->stmts) {
1414
auto bb = std::make_shared<basic_block>(std::to_string(basic_block_count));
1515
bb->parent = st;
16+
// bb->ast_to_basic_block_map[bb->parent] = bb;
1617
bb->ast_index = ast_index_counter++;
1718
bb->ast_depth = 0;
1819
work_list.push_back(bb);
@@ -40,6 +41,7 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) {
4041
for (auto st: stmt_block_->stmts) {
4142
stmt_block_list.push_back(std::make_shared<basic_block>(std::to_string(basic_block_count++)));
4243
stmt_block_list.back()->parent = st;
44+
// stmt_block_list.back()->ast_to_basic_block_map[bb->parent] = stmt_block_list.back();
4345
stmt_block_list.back()->ast_index = ast_index_counter++;
4446
stmt_block_list.back()->ast_depth = bb->ast_depth + 1;
4547
}
@@ -79,6 +81,8 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) {
7981
auto exit_bb = std::make_shared<basic_block>("exit" + std::to_string(basic_block_count));
8082
// assign it a empty stmt_block as parent
8183
exit_bb->parent = std::make_shared<stmt_block>();
84+
// add mapping in ast to bb map
85+
// exit_bb->ast_to_basic_block_map[exit_bb->parent] = exit_bb;
8286
// set the ast depth of the basic block
8387
exit_bb->ast_depth = bb->ast_depth;
8488
// check if this is the last block, if yes the successor will be empty
@@ -98,6 +102,8 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) {
98102
auto then_bb = std::make_shared<basic_block>(std::to_string(++basic_block_count));
99103
// set the parent of this block as the then stmts
100104
then_bb->parent = if_stmt_->then_stmt;
105+
// add mapping in ast to bb map
106+
// then_bb->ast_to_basic_block_map[then_bb->parent] = then_bb;
101107
// set the ast depth of the basic block
102108
then_bb->ast_depth = bb->ast_depth;
103109
// set the successor of this block to be the exit block
@@ -112,6 +118,8 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) {
112118
auto else_bb = std::make_shared<basic_block>(std::to_string(++basic_block_count));
113119
// set the parent of this block as the else stmts
114120
else_bb->parent = if_stmt_->else_stmt;
121+
// add mapping in ast to bb map
122+
// else_bb->ast_to_basic_block_map[else_bb->parent] = else_bb;
115123
// set the ast depth of the basic block
116124
else_bb->ast_depth = bb->ast_depth;
117125
// set the successor of this block to be the exit block

0 commit comments

Comments
 (0)