Skip to content

Commit c38ad8d

Browse files
Improved handling of unconditional loops
1 parent 9f9971c commit c38ad8d

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/blocks/loops.cpp

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,6 @@ void loop_info::analyze() {
152152
}
153153
}
154154

155-
// Populate loop condition block
156-
for(auto loop: loops) {
157-
if (!loop->header_block)
158-
continue;
159-
160-
std::shared_ptr<basic_block> header = loop->header_block;
161-
assert(header->successor.size() == 1 && "loop header cannot have more than one successor");
162-
if (isa<if_stmt>(header->successor[0]->parent))
163-
loop->condition_block = header->successor[0];
164-
}
165-
166155
// Populate the loop exits
167156
for (auto loop: loops) {
168157
if (!loop->header_block)
@@ -198,6 +187,22 @@ void loop_info::analyze() {
198187
loop->unique_exit_block = dta.cfg_[unique_postdom];
199188
}
200189

190+
// Populate loop condition block
191+
for(auto loop: loops) {
192+
if (!loop->header_block)
193+
continue;
194+
195+
// this might be an unconditional loop or
196+
// infinite loop.
197+
if (loop->loop_exit_blocks.empty())
198+
continue;
199+
200+
std::shared_ptr<basic_block> header = loop->header_block;
201+
assert(header->successor.size() == 1 && "loop header cannot have more than one successor");
202+
if (isa<if_stmt>(header->successor[0]->parent))
203+
loop->condition_block = header->successor[0];
204+
}
205+
201206
// Assign id to the loops
202207
for (unsigned int i = 0; i < loops.size(); i++) {
203208
loops[i]->loop_id = i;
@@ -297,7 +302,7 @@ stmt::Ptr loop::convert_to_ast_impl(dominator_analysis &dta_, std::vector<std::p
297302
worklist.push_back({bb->successor[1], nullptr});
298303
visited.insert(bb->successor[1]);
299304
}
300-
else {
305+
else if (blocks_id_map.count(bb->successor[1]->id) && !blocks_id_map.count(bb->successor[0]->id)){
301306
std::cerr << "inserting out of loop block (0): " << bb->successor[0]->id << bb->successor[0]->is_exit_block << "\n";
302307
worklist.push_back({bb->successor[0], nullptr});
303308
visited.insert(bb->successor[0]);
@@ -316,7 +321,8 @@ stmt::Ptr loop::convert_to_ast_impl(dominator_analysis &dta_, std::vector<std::p
316321
// visited.insert(bb->successor[1]);
317322
// }
318323
}
319-
else if (bb->else_branch && blocks_id_map.count(bb->else_branch->id)) {
324+
325+
if (bb->else_branch && blocks_id_map.count(bb->else_branch->id)) {
320326
not_expr::Ptr negated_cond = std::make_shared<not_expr>();
321327
negated_cond->static_offset = while_block->cond->static_offset;
322328
negated_cond->expr1 = while_block->cond;
@@ -340,7 +346,7 @@ stmt::Ptr loop::convert_to_ast_impl(dominator_analysis &dta_, std::vector<std::p
340346
worklist.push_back({bb->successor[0], ast_parent_map_loop[to<stmt_block>(while_block->body)]});
341347
visited.insert(bb->successor[0]);
342348
}
343-
else {
349+
else if (!blocks_id_map.count(bb->successor[1]->id) && blocks_id_map.count(bb->successor[0]->id)) {
344350
std::cerr << "inserting out of loop block (1): " << bb->successor[1]->id << bb->successor[1]->is_exit_block << "\n";
345351
worklist.push_back({bb->successor[1], nullptr});
346352
visited.insert(bb->successor[1]);

0 commit comments

Comments
 (0)