From 2716dcb40d19aee641f456e99ab11e0341d84d04 Mon Sep 17 00:00:00 2001 From: Vedant Paranjape <22630228+VedantParanjape@users.noreply.github.com> Date: Fri, 29 Sep 2023 01:47:00 +0530 Subject: [PATCH] WIP v5: generates clean and correct code for all testcases --- src/blocks/loops.cpp | 78 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 68 insertions(+), 10 deletions(-) diff --git a/src/blocks/loops.cpp b/src/blocks/loops.cpp index 283bde2..d2a35eb 100644 --- a/src/blocks/loops.cpp +++ b/src/blocks/loops.cpp @@ -257,6 +257,8 @@ stmt::Ptr loop::convert_to_ast_impl(dominator_analysis &dta_, std::vectorcond = to(bb->parent)->cond; if (to(to(bb->parent)->then_stmt)->stmts.size() == 0) { + std::cerr << "negated if cond\n"; not_expr::Ptr negated_cond = std::make_shared(); negated_cond->static_offset = while_block->cond->static_offset; negated_cond->expr1 = while_block->cond; + while_block->cond = negated_cond; if (bb->else_branch) { ast_parent_map_loop[to(while_block->body)] = ast; worklist.push_back({bb->else_branch, to(while_block->body)}); visited.insert(bb->else_branch); } + + if (!blocks_id_map.count(bb->successor[1]->id) && blocks_id_map.count(bb->successor[0]->id)) { + std::cerr << "inserting out of loop block (1): " << bb->successor[0]->id << bb->successor[1]->is_exit_block << "\n"; + ast_parent_map_loop[to(while_block->body)] = ast; + worklist.push_back({bb->successor[1], nullptr}); + visited.insert(bb->successor[1]); + } + else { + std::cerr << "inserting out of loop block (0): " << bb->successor[0]->id << bb->successor[0]->is_exit_block << "\n"; + worklist.push_back({bb->successor[0], nullptr}); + visited.insert(bb->successor[0]); + } } - else if (bb->then_branch) { - ast_parent_map_loop[to(while_block->body)] = ast; - worklist.push_back({bb->then_branch, to(while_block->body)}); - visited.insert(bb->then_branch); - } + else { + if (bb->then_branch && blocks_id_map.count(bb->then_branch->id)) { + std::cerr << "loop cond then branch: " << bb->then_branch->id << "\n"; + ast_parent_map_loop[to(while_block->body)] = ast; + worklist.push_back({bb->then_branch, to(while_block->body)}); + visited.insert(bb->then_branch); + + // if (!blocks_id_map.count(bb->successor[1]->id)) { + // std::cerr << "pushing out of loop branch (1) out: " << bb->successor[1]->id << "\n"; + // worklist.push_back({bb->successor[1], ast_parent_map_loop[to(while_block->body)]}); + // visited.insert(bb->successor[1]); + // } + } + else if (bb->else_branch && blocks_id_map.count(bb->else_branch->id)) { + not_expr::Ptr negated_cond = std::make_shared(); + negated_cond->static_offset = while_block->cond->static_offset; + negated_cond->expr1 = while_block->cond; + while_block->cond = negated_cond; - if (bb->successor.size() == 2 && bb->successor[1]->is_exit_block) { - std::cerr << "inserting out of loop block" << bb->successor[1]->id << bb->successor[1]->is_exit_block << "\n"; - worklist.push_back({bb->successor[1], nullptr}); - visited.insert(bb->successor[1]); + std::cerr << "loop cond else branch: " << bb->else_branch->id << "\n"; + ast_parent_map_loop[to(while_block->body)] = ast; + worklist.push_back({bb->else_branch, to(while_block->body)}); + visited.insert(bb->else_branch); + + // if (!blocks_id_map.count(bb->successor[0]->id)) { + // std::cerr << "pushing out of loop branch (0) out: " << bb->successor[0]->id << "\n"; + // worklist.push_back({bb->successor[0], ast_parent_map_loop[to(while_block->body)]}); + // visited.insert(bb->successor[0]); + // } + } + + if (blocks_id_map.count(bb->successor[1]->id) && !blocks_id_map.count(bb->successor[0]->id)) { + std::cerr << "inserting out of loop block (0): " << bb->successor[0]->id << bb->successor[0]->is_exit_block << "\n"; + ast_parent_map_loop[to(while_block->body)] = ast; + worklist.push_back({bb->successor[0], ast_parent_map_loop[to(while_block->body)]}); + visited.insert(bb->successor[0]); + } + else { + std::cerr << "inserting out of loop block (1): " << bb->successor[1]->id << bb->successor[1]->is_exit_block << "\n"; + worklist.push_back({bb->successor[1], nullptr}); + visited.insert(bb->successor[1]); + } } } else { @@ -362,7 +410,8 @@ stmt::Ptr loop::convert_to_ast_impl(dominator_analysis &dta_, std::vectorsuccessor.size() <= 1); bool exit_bb_succ = false; - if (bb->is_exit_block) { + + if (bb->is_exit_block && !blocks_id_map.count(bb->id)) { for (auto subloop: subloops) { if (bb == subloop->unique_exit_block) { ast->stmts.push_back(to(std::make_shared())); @@ -390,6 +439,12 @@ stmt::Ptr loop::convert_to_ast_impl(dominator_analysis &dta_, std::vectorid) && !bb->is_exit_block) { + std::cerr << "case for 26: " << bb->id << worklist.size() << loop_id << "\n"; + return_blocks.push_back({bb, nullptr}); + continue; + } + if (!bb->is_exit_block && !isa(bb->parent)) { std::cerr << "bb: " << bb->id << "\n"; ast->dump(std::cerr, 0); @@ -546,6 +601,8 @@ block::stmt_block::Ptr loop_info::convert_to_ast(block::stmt_block::Ptr ast) { for (auto block: loop_out_blocks) { worklist.push_back({block.first, block.second ? block.second : ast}); } + std::cerr << "finish outerloop\n"; + std::cerr << worklist.size() << "\n"; break; } } @@ -581,6 +638,7 @@ block::stmt_block::Ptr loop_info::convert_to_ast(block::stmt_block::Ptr ast) { if (bb->is_exit_block) { for (auto loop: loops) { if (bb == loop->unique_exit_block) { + std::cerr << "inserted break: " << bb->id << "\n"; ast->stmts.push_back(to(std::make_shared())); } }