Skip to content

Commit 2bc015a

Browse files
WIP v4: able to generate loop exit breaks as well
1 parent cea27bc commit 2bc015a

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

src/blocks/loops.cpp

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -281,25 +281,17 @@ stmt::Ptr loop::convert_to_ast_impl(dominator_analysis &dta_, std::vector<std::p
281281
worklist.push_back({bb->else_branch, to<stmt_block>(while_block->body)});
282282
visited.insert(bb->else_branch);
283283
}
284-
285-
if (bb->successor.size() == 2 && bb->successor[1]->is_exit_block) {
286-
std::cerr << "inserting out of loop block" << bb->successor[1]->id << bb->successor[1]->is_exit_block << "\n";
287-
worklist.push_back({bb->successor[1], nullptr});
288-
visited.insert(bb->successor[1]);
289-
}
290284
}
291-
else {
292-
if (bb->then_branch) {
293-
ast_parent_map_loop[to<stmt_block>(while_block->body)] = ast;
294-
worklist.push_back({bb->then_branch, to<stmt_block>(while_block->body)});
295-
visited.insert(bb->then_branch);
296-
}
285+
else if (bb->then_branch) {
286+
ast_parent_map_loop[to<stmt_block>(while_block->body)] = ast;
287+
worklist.push_back({bb->then_branch, to<stmt_block>(while_block->body)});
288+
visited.insert(bb->then_branch);
289+
}
297290

298-
if (bb->successor.size() == 2 && bb->successor[1]->is_exit_block) {
299-
std::cerr << "inserting out of loop block" << bb->successor[1]->id << bb->successor[1]->is_exit_block << "\n";
300-
worklist.push_back({bb->successor[1], nullptr});
301-
visited.insert(bb->successor[1]);
302-
}
291+
if (bb->successor.size() == 2 && bb->successor[1]->is_exit_block) {
292+
std::cerr << "inserting out of loop block" << bb->successor[1]->id << bb->successor[1]->is_exit_block << "\n";
293+
worklist.push_back({bb->successor[1], nullptr});
294+
visited.insert(bb->successor[1]);
303295
}
304296
}
305297
else {
@@ -318,6 +310,13 @@ stmt::Ptr loop::convert_to_ast_impl(dominator_analysis &dta_, std::vector<std::p
318310
worklist.push_back({bb->else_branch, to<stmt_block>(if_stmt_copy->else_stmt)});
319311
visited.insert(bb->else_branch);
320312
}
313+
314+
if ((!bb->then_branch || !bb->else_branch) && bb->successor.size() == 2 && bb->successor[1]->is_exit_block) {
315+
std::cerr << "inserting out of loop block" << bb->successor[1]->id << bb->successor[1]->is_exit_block << "\n";
316+
worklist.push_back({bb->successor[1], nullptr});
317+
visited.insert(bb->successor[1]);
318+
}
319+
321320
ast->stmts.push_back(to<stmt>(if_stmt_copy));
322321
}
323322
}
@@ -351,7 +350,7 @@ stmt::Ptr loop::convert_to_ast_impl(dominator_analysis &dta_, std::vector<std::p
351350

352351
if (!is_last_block) {
353352
ast->stmts.push_back(to<stmt>(std::make_shared<continue_stmt>()));
354-
// TODO: also insert in continue stmt vector in while_stmt
353+
while_block->continue_blocks.push_back(ast);
355354
}
356355
visited.insert(bb);
357356
}
@@ -364,6 +363,11 @@ stmt::Ptr loop::convert_to_ast_impl(dominator_analysis &dta_, std::vector<std::p
364363
assert(bb->successor.size() <= 1);
365364
bool exit_bb_succ = false;
366365
if (bb->is_exit_block) {
366+
for (auto subloop: subloops) {
367+
if (bb == subloop->unique_exit_block) {
368+
ast->stmts.push_back(to<stmt>(std::make_shared<break_stmt>()));
369+
}
370+
}
367371
for (auto exit: loop_exit_blocks) {
368372
for (auto pred: bb->predecessor) {
369373
if (pred->id == exit->id && bb->successor.size()) {
@@ -380,6 +384,10 @@ stmt::Ptr loop::convert_to_ast_impl(dominator_analysis &dta_, std::vector<std::p
380384

381385
if (exit_bb_succ)
382386
continue;
387+
388+
if (bb == unique_exit_block && dta_.get_preorder_bb_map()[bb->id] != (int)dta_.get_preorder().size() - 1) {
389+
ast->stmts.push_back(to<stmt>(std::make_shared<break_stmt>()));
390+
}
383391
}
384392

385393
if (!bb->is_exit_block && !isa<stmt_block>(bb->parent)) {
@@ -570,6 +578,14 @@ block::stmt_block::Ptr loop_info::convert_to_ast(block::stmt_block::Ptr ast) {
570578
else {
571579
assert(bb->successor.size() <= 1);
572580

581+
if (bb->is_exit_block) {
582+
for (auto loop: loops) {
583+
if (bb == loop->unique_exit_block) {
584+
ast->stmts.push_back(to<stmt>(std::make_shared<break_stmt>()));
585+
}
586+
}
587+
}
588+
573589
if (bb_loop_map.count(bb->id))
574590
continue;
575591

0 commit comments

Comments
 (0)