Skip to content

Commit 2716dcb

Browse files
WIP v5: generates clean and correct code for all testcases
1 parent 2bc015a commit 2716dcb

File tree

1 file changed

+68
-10
lines changed

1 file changed

+68
-10
lines changed

src/blocks/loops.cpp

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ stmt::Ptr loop::convert_to_ast_impl(dominator_analysis &dta_, std::vector<std::p
257257
for (auto block: loop_out_blocks) {
258258
worklist.push_back({block.first, block.second ? block.second : ast});
259259
}
260+
std::cerr << "finish subloop\n";
261+
260262
break;
261263
}
262264
}
@@ -272,26 +274,72 @@ stmt::Ptr loop::convert_to_ast_impl(dominator_analysis &dta_, std::vector<std::p
272274
while_block->cond = to<if_stmt>(bb->parent)->cond;
273275

274276
if (to<stmt_block>(to<if_stmt>(bb->parent)->then_stmt)->stmts.size() == 0) {
277+
std::cerr << "negated if cond\n";
275278
not_expr::Ptr negated_cond = std::make_shared<not_expr>();
276279
negated_cond->static_offset = while_block->cond->static_offset;
277280
negated_cond->expr1 = while_block->cond;
281+
while_block->cond = negated_cond;
278282

279283
if (bb->else_branch) {
280284
ast_parent_map_loop[to<stmt_block>(while_block->body)] = ast;
281285
worklist.push_back({bb->else_branch, to<stmt_block>(while_block->body)});
282286
visited.insert(bb->else_branch);
283287
}
288+
289+
if (!blocks_id_map.count(bb->successor[1]->id) && blocks_id_map.count(bb->successor[0]->id)) {
290+
std::cerr << "inserting out of loop block (1): " << bb->successor[0]->id << bb->successor[1]->is_exit_block << "\n";
291+
ast_parent_map_loop[to<stmt_block>(while_block->body)] = ast;
292+
worklist.push_back({bb->successor[1], nullptr});
293+
visited.insert(bb->successor[1]);
294+
}
295+
else {
296+
std::cerr << "inserting out of loop block (0): " << bb->successor[0]->id << bb->successor[0]->is_exit_block << "\n";
297+
worklist.push_back({bb->successor[0], nullptr});
298+
visited.insert(bb->successor[0]);
299+
}
284300
}
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-
}
301+
else {
302+
if (bb->then_branch && blocks_id_map.count(bb->then_branch->id)) {
303+
std::cerr << "loop cond then branch: " << bb->then_branch->id << "\n";
304+
ast_parent_map_loop[to<stmt_block>(while_block->body)] = ast;
305+
worklist.push_back({bb->then_branch, to<stmt_block>(while_block->body)});
306+
visited.insert(bb->then_branch);
307+
308+
// if (!blocks_id_map.count(bb->successor[1]->id)) {
309+
// std::cerr << "pushing out of loop branch (1) out: " << bb->successor[1]->id << "\n";
310+
// worklist.push_back({bb->successor[1], ast_parent_map_loop[to<stmt_block>(while_block->body)]});
311+
// visited.insert(bb->successor[1]);
312+
// }
313+
}
314+
else if (bb->else_branch && blocks_id_map.count(bb->else_branch->id)) {
315+
not_expr::Ptr negated_cond = std::make_shared<not_expr>();
316+
negated_cond->static_offset = while_block->cond->static_offset;
317+
negated_cond->expr1 = while_block->cond;
318+
while_block->cond = negated_cond;
290319

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]);
320+
std::cerr << "loop cond else branch: " << bb->else_branch->id << "\n";
321+
ast_parent_map_loop[to<stmt_block>(while_block->body)] = ast;
322+
worklist.push_back({bb->else_branch, to<stmt_block>(while_block->body)});
323+
visited.insert(bb->else_branch);
324+
325+
// if (!blocks_id_map.count(bb->successor[0]->id)) {
326+
// std::cerr << "pushing out of loop branch (0) out: " << bb->successor[0]->id << "\n";
327+
// worklist.push_back({bb->successor[0], ast_parent_map_loop[to<stmt_block>(while_block->body)]});
328+
// visited.insert(bb->successor[0]);
329+
// }
330+
}
331+
332+
if (blocks_id_map.count(bb->successor[1]->id) && !blocks_id_map.count(bb->successor[0]->id)) {
333+
std::cerr << "inserting out of loop block (0): " << bb->successor[0]->id << bb->successor[0]->is_exit_block << "\n";
334+
ast_parent_map_loop[to<stmt_block>(while_block->body)] = ast;
335+
worklist.push_back({bb->successor[0], ast_parent_map_loop[to<stmt_block>(while_block->body)]});
336+
visited.insert(bb->successor[0]);
337+
}
338+
else {
339+
std::cerr << "inserting out of loop block (1): " << bb->successor[1]->id << bb->successor[1]->is_exit_block << "\n";
340+
worklist.push_back({bb->successor[1], nullptr});
341+
visited.insert(bb->successor[1]);
342+
}
295343
}
296344
}
297345
else {
@@ -362,7 +410,8 @@ stmt::Ptr loop::convert_to_ast_impl(dominator_analysis &dta_, std::vector<std::p
362410
else {
363411
assert(bb->successor.size() <= 1);
364412
bool exit_bb_succ = false;
365-
if (bb->is_exit_block) {
413+
414+
if (bb->is_exit_block && !blocks_id_map.count(bb->id)) {
366415
for (auto subloop: subloops) {
367416
if (bb == subloop->unique_exit_block) {
368417
ast->stmts.push_back(to<stmt>(std::make_shared<break_stmt>()));
@@ -390,6 +439,12 @@ stmt::Ptr loop::convert_to_ast_impl(dominator_analysis &dta_, std::vector<std::p
390439
}
391440
}
392441

442+
if (!blocks_id_map.count(bb->id) && !bb->is_exit_block) {
443+
std::cerr << "case for 26: " << bb->id << worklist.size() << loop_id << "\n";
444+
return_blocks.push_back({bb, nullptr});
445+
continue;
446+
}
447+
393448
if (!bb->is_exit_block && !isa<stmt_block>(bb->parent)) {
394449
std::cerr << "bb: " << bb->id << "\n";
395450
ast->dump(std::cerr, 0);
@@ -546,6 +601,8 @@ block::stmt_block::Ptr loop_info::convert_to_ast(block::stmt_block::Ptr ast) {
546601
for (auto block: loop_out_blocks) {
547602
worklist.push_back({block.first, block.second ? block.second : ast});
548603
}
604+
std::cerr << "finish outerloop\n";
605+
std::cerr << worklist.size() << "\n";
549606
break;
550607
}
551608
}
@@ -581,6 +638,7 @@ block::stmt_block::Ptr loop_info::convert_to_ast(block::stmt_block::Ptr ast) {
581638
if (bb->is_exit_block) {
582639
for (auto loop: loops) {
583640
if (bb == loop->unique_exit_block) {
641+
std::cerr << "inserted break: " << bb->id << "\n";
584642
ast->stmts.push_back(to<stmt>(std::make_shared<break_stmt>()));
585643
}
586644
}

0 commit comments

Comments
 (0)