From 423d3001044e606d9f0c01c0cec407a050a5df49 Mon Sep 17 00:00:00 2001 From: Vedant Paranjape <22630228+VedantParanjape@users.noreply.github.com> Date: Wed, 1 Nov 2023 19:56:03 +0530 Subject: [PATCH] Fix bugs and emit guards for latches that cut through and go to loop headers that are not immediate parent --- include/blocks/loops.h | 4 +- src/blocks/basic_blocks.cpp | 4 +- src/blocks/loops.cpp | 743 ++++++-------------------------- src/builder/builder_context.cpp | 9 +- 4 files changed, 145 insertions(+), 615 deletions(-) diff --git a/include/blocks/loops.h b/include/blocks/loops.h index 0751167..3981881 100644 --- a/include/blocks/loops.h +++ b/include/blocks/loops.h @@ -7,10 +7,11 @@ #include using namespace block; +class loop_info; class loop { public: loop(std::shared_ptr header): header_block(header) {} - stmt::Ptr convert_to_ast_impl(dominator_analysis &dta_, std::vector, stmt_block::Ptr>> &return_blocks); + stmt::Ptr convert_to_ast_impl(loop_info &li, dominator_analysis &dta_, std::vector, stmt_block::Ptr>> &return_blocks, stmt::Ptr &jump_condition_def, stmt::Ptr &jump_condition_block); struct loop_bounds_ { stmt::Ptr ind_var; @@ -33,6 +34,7 @@ class loop { basic_block::cfg_block loop_latch_blocks; basic_block::cfg_block loop_exit_blocks; std::vector> subloops; + while_stmt::Ptr structured_ast_loop; }; class loop_info { diff --git a/src/blocks/basic_blocks.cpp b/src/blocks/basic_blocks.cpp index 66f863f..139e720 100644 --- a/src/blocks/basic_blocks.cpp +++ b/src/blocks/basic_blocks.cpp @@ -96,7 +96,7 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) { work_list.pop_front(); // push the exit block to the work_list work_list.push_front(exit_bb); - + std::cerr << "inside if handler: " << bb->name << "\n"; // if there is a then_stmt, create a basic block for it if (to(if_stmt_->then_stmt)->stmts.size() != 0) { auto then_bb = std::make_shared(std::to_string(++basic_block_count)); @@ -112,6 +112,7 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) { bb->then_branch = then_bb; // push the block to the work_list, to expand it further work_list.push_front(then_bb); + std::cerr << "inside then" << "\n"; } // if there is a else_stmt, create a basic block for it if (to(if_stmt_->else_stmt)->stmts.size() != 0) { @@ -128,6 +129,7 @@ basic_block::cfg_block generate_basic_blocks(block::stmt_block::Ptr ast) { bb->else_branch = else_bb; // push the block to the work_list, to expand it further work_list.insert(work_list.begin() + 1, else_bb); + std::cerr << "inside else" << "\n"; } // if there is no then/else block, then have the exit block as successor as well. diff --git a/src/blocks/loops.cpp b/src/blocks/loops.cpp index 736ced0..a38589f 100644 --- a/src/blocks/loops.cpp +++ b/src/blocks/loops.cpp @@ -235,9 +235,12 @@ void loop_info::analyze() { std::set visited_blocks; std::map ast_parent_map_loop; -stmt::Ptr loop::convert_to_ast_impl(dominator_analysis &dta_, std::vector, stmt_block::Ptr>> &return_blocks) { +// std::map, while_stmt::Ptr> return_blocks_parent_loop; +int jump_condition_counter = 0; +stmt::Ptr loop::convert_to_ast_impl(loop_info &li, dominator_analysis &dta_, std::vector, stmt_block::Ptr>> &return_blocks, stmt::Ptr &jump_condition_def, stmt::Ptr &jump_condition_block) { while_stmt::Ptr while_block = std::make_shared(); while_block->body = std::make_shared(); + structured_ast_loop = while_block; if (!condition_block) { while_block->cond = std::make_shared(); @@ -260,9 +263,11 @@ stmt::Ptr loop::convert_to_ast_impl(dominator_analysis &dta_, std::vectorheader_block->parent == bb->parent) { std::cerr << "found subloop\n"; std::vector, stmt_block::Ptr>> loop_out_blocks; - ast->stmts.push_back(subloop->convert_to_ast_impl(dta_, loop_out_blocks)); + stmt::Ptr jump_def, jump_block; + ast->stmts.push_back(subloop->convert_to_ast_impl(li, dta_, loop_out_blocks, jump_def, jump_block)); for (auto block: loop_out_blocks) { + // return_blocks_parent_loop.insert({block.first, to(ast->stmts.back())}); worklist.push_back({block.first, block.second ? block.second : ast}); } std::cerr << "finish subloop\n"; @@ -355,18 +360,30 @@ stmt::Ptr loop::convert_to_ast_impl(dominator_analysis &dta_, std::vectorcond = to(bb->parent)->cond; + bb->parent->dump(std::cerr, 0); + std::cerr << bb->successor[0]->id << " " << bb->successor[1]->id << "\n"; if (bb->then_branch) { - std::cerr << "non-cond if then\n"; + std::cerr << "non-cond if then: " << bb->id << "\n"; ast_parent_map_loop[to(if_stmt_copy->then_stmt)] = ast; - worklist.push_back({bb->then_branch, to(if_stmt_copy->then_stmt)}); + if (!blocks_id_map.count(bb->then_branch->id) && !bb->then_branch->is_exit_block) { + return_blocks.push_back({bb->then_branch, to(if_stmt_copy->then_stmt)}); + } + else { + worklist.push_back({bb->then_branch, to(if_stmt_copy->then_stmt)}); + } visited.insert(bb->then_branch); } if (bb->else_branch) { - std::cerr << "non-cond if else\n"; + std::cerr << "non-cond if else: " << bb->id <<"\n"; ast_parent_map_loop[to(if_stmt_copy->else_stmt)] = ast; - worklist.push_back({bb->else_branch, to(if_stmt_copy->else_stmt)}); + if (!blocks_id_map.count(bb->else_branch->id) && !bb->else_branch->is_exit_block) { + return_blocks.push_back({bb->else_branch, to(if_stmt_copy->else_stmt)}); + } + else { + worklist.push_back({bb->else_branch, to(if_stmt_copy->else_stmt)}); + } visited.insert(bb->else_branch); } @@ -382,7 +399,8 @@ stmt::Ptr loop::convert_to_ast_impl(dominator_analysis &dta_, std::vector(bb->parent)) { if (to(bb->parent)->label1 == to(header_block->parent)->label1) { bool is_last_block = false; - + bool is_goto_to_outerloop = false; + bb->parent->dump(std::cerr, 0); if (dta_.get_preorder_bb_map()[bb->id] == (int)dta_.get_preorder().size() - 1) { is_last_block = true; } @@ -411,17 +429,118 @@ stmt::Ptr loop::convert_to_ast_impl(dominator_analysis &dta_, std::vectorast_depth - 2 > (int)header_block->ast_depth) { + // figure out the parent while loop for this basic block + auto target_bb = bb->successor[0]; + auto pointer_bb = bb; + std::vector> loop_parent_tree; + do { + std::cerr << "iter: " << pointer_bb->id << "\n"; + + if (pointer_bb->predecessor.size() > 1) { + auto min_preorder_pred = std::min_element(pointer_bb->predecessor.begin(), pointer_bb->predecessor.end(), + [&dta_](std::shared_ptr bb1, std::shared_ptr bb2) { + return dta_.get_preorder_bb_map()[bb1->id] < dta_.get_preorder_bb_map()[bb2->id]; + }); + if (min_preorder_pred != pointer_bb->predecessor.end()) { + pointer_bb = *min_preorder_pred; + } + } + else { + pointer_bb = pointer_bb->predecessor[0]; + } + + for (auto loop: li.loops) { + if (loop->header_block == pointer_bb) { + loop_parent_tree.push_back(loop); + break; + } + } + } while (target_bb != pointer_bb); + + for (auto loops : loop_parent_tree) { + std::cerr << loops->header_block->id << "\n"; + } + + while_stmt::Ptr jump_target_loop = loop_parent_tree.back()->structured_ast_loop; + while_stmt::Ptr guard_target_loop = (*(loop_parent_tree.rbegin() + 1))->structured_ast_loop; + + std::cerr << "handingling break cond\n"; + is_goto_to_outerloop = true; + + auto jump_cond_def = std::make_shared(); + jump_cond_def->var_name = "control_guard" + std::to_string(jump_condition_counter++); + + auto scalar_type1 = std::make_shared(); + jump_cond_def->var_type = scalar_type1; + scalar_type1->scalar_type_id = scalar_type::INT_TYPE; + + auto var_expr1 = std::make_shared(); + var_expr1->var1 = jump_cond_def; + + auto const_expr1 = std::make_shared(); + const_expr1->value = 1; + const_expr1->is_64bit = false; + + auto assign_expr1 = std::make_shared(); + assign_expr1->var1 = var_expr1; + assign_expr1->expr1 = const_expr1; + + auto jump_expr = std::make_shared(); + jump_expr->expr1 = assign_expr1; + + ast->stmts.push_back(jump_expr); + + auto var_expr2 = std::make_shared(); + var_expr2->var1 = jump_cond_def; + + auto const_expr2 = std::make_shared(); + const_expr2->value = 0; + const_expr2->is_64bit = false; + + auto assign_expr2 = std::make_shared(); + assign_expr2->var1 = var_expr2; + assign_expr2->expr1 = const_expr2; + + auto expr_stmt2 = std::make_shared(); + expr_stmt2->expr1 = assign_expr2; + + // guard decl stmt + auto var_decl1 = std::make_shared(); + var_decl1->decl_var = jump_cond_def; + var_decl1->init_expr = const_expr2; + + auto while_body = to(while_block->body); + while_body->stmts.insert(while_body->stmts.begin(), var_decl1); + + // guard if stmt + auto if_stmt1 = std::make_shared(); + if_stmt1->else_stmt = std::make_shared(); + auto stmt_block1 = std::make_shared(); + if_stmt1->then_stmt = stmt_block1; + stmt_block1->stmts.push_back(std::make_shared()); + + auto var_expr3 = std::make_shared(); + var_expr3->var1 = jump_cond_def; + if_stmt1->cond = var_expr3; + + auto guard_while_body = to(guard_target_loop->body); + auto jump_while_body = to(jump_target_loop->body); + + guard_while_body->stmts.insert(guard_while_body->stmts.begin(), to(expr_stmt2)); + + auto guard_decl_insertion_point = std::find(jump_while_body->stmts.begin(), jump_while_body->stmts.end(), to(guard_target_loop)); + if (guard_decl_insertion_point != jump_while_body->stmts.end()) { + jump_while_body->stmts.insert(guard_decl_insertion_point + 1, to(if_stmt1)); + } + } if (!is_last_block) { std::cerr << "inserted continue: " << bb->id << loop_id << "\n"; - ast->stmts.push_back(to(std::make_shared())); + ast->stmts.push_back(is_goto_to_outerloop ? to(std::make_shared()) : to(std::make_shared())); while_block->continue_blocks.push_back(ast); } visited.insert(bb); } - else { - ast->stmts.push_back(to(std::make_shared())); - visited.insert(bb); - } } else { assert(bb->successor.size() <= 1); @@ -483,111 +602,6 @@ stmt::Ptr loop::convert_to_ast_impl(dominator_analysis &dta_, std::vector(exit_bb->parent)) - // assert(0 && "loop exit block must be an if_stmt"); - - // if (exit_bb->then_branch && !blocks_id_map.count(exit_bb->then_branch->id)) - // target_branch = 0; - // else if (exit_bb->else_branch && !blocks_id_map.count(exit_bb->else_branch->id)) - // target_branch = 1; - - // if (target_branch == -1) - // assert(0 && "one of the branches of if should have the exiting block"); - - // if (ast_parent_map.count(exit_bb->parent)) { - // std::cerr << "inside parent map\n"; - // auto temp = ast_parent_map[exit_bb->parent]; - // std::cerr << temp.second << "\n"; - // std::cerr << temp.first->stmts.size() << "\n"; - // std::vector &temp_ast = to(temp.second == -1 ? temp.first : to(temp.first->stmts[temp.second])->then_stmt)->stmts; - // std::cerr << "ast size: " << temp_ast.size() << "\n"; - - // std::shared_ptr exiting_bb = target_branch ? exit_bb->else_branch : exit_bb->then_branch; - // std::cerr << exiting_bb->id << "\n"; - // unsigned int preorder_index = dta_.get_preorder_bb_map()[exiting_bb->id] + 1; - // unsigned int unique_exit_index = dta_.get_preorder_bb_map()[unique_exit_block->id]; - // std::cerr << preorder_index << " " << unique_exit_index << "\n"; - - // for (;preorder_index < unique_exit_index; preorder_index++) { - // std::shared_ptr exiting_bb = dta_.cfg_[dta_.get_preorder()[preorder_index]]; - // if (!exiting_bb->is_exit_block) { - // visited_blocks.insert(exiting_bb->parent); - // temp_ast.push_back(exiting_bb->parent); - // } - // } - // std::cerr << preorder_index << " " << unique_exit_index << "\n"; - // if (preorder_index == unique_exit_index) - // temp_ast.push_back(std::make_shared()); - - // // if (preorder_index == unique_exit_index + 1) - // // temp_ast.push_back(std::make_shared()); - // } - // else { - // std::cerr << "not inside parent map\n"; - // } - // } - - // manage loop latches - // for (auto latch: loop_latch_blocks) { - // if (ast_parent_map.count(latch->parent)) { - // visited_blocks.insert(latch->parent); - // std::cerr << "inside latch map\n"; - // auto temp = ast_parent_map[latch->parent]; - // std::cerr << temp.second << "\n"; - // std::cerr << temp.first->stmts.size() << "\n"; - // std::vector &temp_ast = temp.first->stmts; - // std::cerr << "ast size: " << temp_ast.size() << "\n"; - - // bool is_last_block = false; - // // bool is_latch_in_other_loop = false; - - // if (dta_.get_preorder_bb_map()[latch->id] == (int)dta_.get_preorder().size() - 1) { - // is_last_block = true; - // } - // else { - // int next_preorder = dta_.get_preorder_bb_map()[latch->id] + 1 < (int)dta_.get_preorder().size() ? dta_.get_preorder()[dta_.get_preorder_bb_map()[latch->id] + 1] : -1; - // int next_next_preorder = dta_.get_preorder_bb_map()[next_preorder] + 1 < (int)dta_.get_preorder().size() ? dta_.get_preorder()[dta_.get_preorder_bb_map()[next_preorder] + 1] : -1; - - // if (blocks_id_map.count(next_preorder)) - // is_last_block = false; - // else { - // if (unique_exit_block && (next_preorder == (int)unique_exit_block->id)) - // is_last_block = true; - // else if (unique_exit_block && (next_next_preorder == (int)unique_exit_block->id)) - // is_last_block = true; - // else if (next_preorder != -1 && next_next_preorder == -1 && dta_.cfg_[next_preorder]->is_exit_block) - // is_last_block = true; - // else if (next_preorder != -1 && next_next_preorder != -1 && dta_.cfg_[next_preorder]->is_exit_block && isa(dta_.cfg_[next_next_preorder]->parent)) - // is_last_block = true; - // else - // is_last_block = false; - // } - // } - - // // if (latch->ast_to_basic_block_map.count(loop_latch_ast)) { - // // std::cerr << "break in latch: " << latch->ast_to_basic_block_map.at(loop_latch_ast)->id << "\n"; - // // auto bb = latch->ast_to_basic_block_map.at(loop_latch_ast); - // // for (auto subloop: loop->subloops) { - // // if (subloop->blocks_id_map.count(bb->id)) - // // is_latch_in_other_loop = true; - // // } - // // } - - // temp_ast.pop_back(); - // if (!is_last_block) { - // while_block->continue_blocks.push_back(temp.first); - // temp_ast.push_back(std::make_shared()); - // } - // } - // } - return to(while_block); } @@ -610,9 +624,10 @@ block::stmt_block::Ptr loop_info::convert_to_ast(block::stmt_block::Ptr ast) { for (auto loop : top_level_loops) { if (loop->header_block->parent == bb->parent) { std::cerr << "found outerloop\n"; - + stmt::Ptr jump_def, jump_block; std::vector, stmt_block::Ptr>> loop_out_blocks; - ast->stmts.push_back(loop->convert_to_ast_impl(dta, loop_out_blocks)); + ast->stmts.push_back(loop->convert_to_ast_impl(*this, dta, loop_out_blocks, jump_def, jump_block)); + loop->structured_ast_loop = to(ast->stmts.back()); for (auto block: loop_out_blocks) { worklist.push_back({block.first, block.second ? block.second : ast}); @@ -684,496 +699,6 @@ block::stmt_block::Ptr loop_info::convert_to_ast(block::stmt_block::Ptr ast) { } } } - // // iterate using preorder bb - // // use stack for current parent block - // std::stack parent_stack; - // parent_stack.push(to(return_ast)); - - // for (unsigned int i = 0; i < dta.get_preorder().size(); i++) { - // auto bb = dta.cfg_[dta.get_preorder()[i]]; - - // std::cerr << "bb: " << bb->id << "\n"; - // if (isa(bb->parent)) { - // std::cerr << "inside label block\n"; - - // for (auto loop: top_level_loops) { - // if (loop->header_block->parent == bb->parent) { - // to(parent_stack.top())->stmts.push_back(loop->convert_to_ast_impl(dta)); - // break; - // } - // } - // } - // else if (isa(bb->parent)) { - // std::cerr << "inside if block\n"; - - // if (bb_loop_map.count(bb->id)) - // continue; - // std::cerr << "inside if block (exit 1)\n"; - - // if (visited_blocks.count(bb->parent)) - // continue; - // std::cerr << "inside if block (exit 2)\n"; - - // if (dta.get_preorder().size() <= i + 1) - // continue; - // std::cerr << "inside if block (exit 3)\n"; - - // if_stmt::Ptr if_stmt_copy = std::make_shared(); - // if_stmt_copy->then_stmt = to(std::make_shared()); - // if_stmt_copy->else_stmt = to(std::make_shared()); - // if_stmt_copy->cond = to(bb->parent)->cond; - - // int next_block = -1; - // if (dta.get_preorder()[i + 1] == (int)bb->then_branch->id) - // next_block = 0; - // else if (dta.get_preorder()[i + 1] == (int)bb->else_branch->id) - // next_block = 1; - - // assert(next_block != -1); - - // to(parent_stack.top())->stmts.push_back(if_stmt_copy); - // if (next_block == 0) { - // parent_stack.push(to(if_stmt_copy->then_stmt)); - // } - // else if (next_block == 1) { - // parent_stack.push(to(if_stmt_copy->else_stmt)); - // } - - // } - // else if (bb->is_exit_block) { - // std::cerr << "inside exit block\n"; - - // if (bb_loop_map.count(bb->id)) - // continue; - // std::cerr << "inside exit block (exit 1)\n"; - - // if (visited_blocks.count(bb->parent)) - // continue; - - // parent_stack.pop(); - // } - // else { - // std::cerr << "inside default block\n"; - - // if (bb_loop_map.count(bb->id)) - // continue; - // std::cerr << "inside default block (exit 1)\n"; - - // if (visited_blocks.count(bb->parent)) - // continue; - // std::cerr << "inside default block (exit 2)\n"; - - // to(parent_stack.top())->stmts.push_back(bb->parent); - // } - // } - - // for (auto bb: dta.cfg_) { - // std::cerr << bb->id << " " << bb_loop_map.count(bb->id) << " " << visited_blocks.count(bb->parent) << " " << bb->is_exit_block << "\n"; - // if (isa(bb->parent)) { - // std::cerr << "inside label block\n"; - // for (auto loop: top_level_loops) { - // if (loop->header_block->parent == bb->parent) { - // if (ast_parent_map_global.count(bb->parent)) { - // ast_parent_map_global[bb->parent]->stmts.push_back(loop->convert_to_ast_impl(dta)); - // } - // else { - // return_ast->stmts.push_back(loop->convert_to_ast_impl(dta)); - // } - // } - // } - // } - // else if (!bb_loop_map.count(bb->id) && !visited_blocks.count(bb->parent) && !bb->is_exit_block) { - // std::cerr << "inside if block\n"; - // stmt::Ptr push_block = bb->parent; - - // if (isa(bb->parent)) { - // if_stmt::Ptr if_stmt_copy = std::make_shared(); - // if_stmt_copy->then_stmt = to(std::make_shared()); - // if_stmt_copy->else_stmt = to(std::make_shared()); - // if_stmt_copy->cond = to(bb->parent)->cond; - // push_block = to(if_stmt_copy); - - // for (auto stmt: to(to(bb->parent)->then_stmt)->stmts) { - // if (!isa(stmt)) { - // std::cerr << "ifstmt\n"; - // visited_blocks.insert(stmt); - // to(if_stmt_copy->then_stmt)->stmts.push_back(stmt); - // } - // ast_parent_map_global.insert({stmt, to(if_stmt_copy->then_stmt)}); - // } - // for (auto stmt: to(to(bb->parent)->else_stmt)->stmts) { - // if (!isa(stmt)) { - // std::cerr << "elsestmt\n"; - // visited_blocks.insert(stmt); - // to(if_stmt_copy->else_stmt)->stmts.push_back(stmt); - // } - // ast_parent_map_global.insert({stmt, to(if_stmt_copy->else_stmt)}); - // } - // } - // else if (isa(bb->parent)) { - // continue; - // } - - // visited_blocks.insert(bb->parent); - // return_ast->stmts.push_back(push_block); - // } - // } return return_ast; - -// std::cerr << "before ast\n"; - // ast->dump(std::cerr, 0); - // std::cerr << "before ast\n"; - - // for (auto loop_map: preorder_loops_map) { - // for (auto preorder: loop_map.second) { - // replace_loop_exits(loops[preorder], ast, dta); - // replace_loop_latches(loops[preorder], ast, dta); - // } - // } - - // std::cerr << "after ast\n"; - // ast->dump(std::cerr, 0); - // std::cerr << "after ast\n"; - - // return ast; - - - // std::cerr << "before ast\n"; - // ast->dump(std::cerr, 0); - // std::cerr << "before ast\n"; - // block::stmt::Ptr loop_header_ast = get_loop_block(loops[postorder]->header_block, ast); - // std::cerr << loops[postorder]->header_block->ast_index << "\n"; - // loop_header_ast->dump(std::cerr, 0); - // while_stmt::Ptr while_block = std::make_shared(); - // while_block->body = std::make_shared(); - // while_block->cond = std::make_shared(); - // to(while_block->cond)->value = 1; - // if (isa(loop_header_ast)) { - // loop_header_ast = to(loop_header_ast)->body; - // } - - // if (isa(loop_header_ast)) { - // unsigned int ast_index = loops[postorder]->header_block->ast_index; - // // handle unconditional loops - // if (to(loop_header_ast)->stmts[ast_index] == loops[postorder]->header_block->parent && !isa(to(loop_header_ast)->stmts[ast_index + 1])) { - // for (auto bb: loops[postorder]->blocks) { - // to(while_block->body)->stmts.push_back(bb->parent); - // } - // // pop loop backedge - // to(while_block->body)->stmts.pop_back(); - - // // set always true condition - // while_block->cond = std::make_shared(); - // to(while_block->cond)->value = 1; - - // // unconditional loops can have only one backedge !? - // assert(loops[postorder]->loop_latch_blocks.size() == 1); - // for (unsigned int i = ast_index + 2; i < loops[postorder]->loop_latch_blocks[0]->ast_index; i++) { - // std::cerr << i << "\n"; - // worklist.push_back(std::make_tuple(i, std::ref(to(loop_header_ast)->stmts), nullptr)); - // } - - // worklist.push_back(std::make_tuple(ast_index, std::ref(to(loop_header_ast)->stmts), to(while_block))); - // } - // else if (to(loop_header_ast)->stmts[ast_index] == loops[postorder]->header_block->parent) { - // stmt_block::Ptr then_block = to(to(to(loop_header_ast)->stmts[ast_index + 1])->then_stmt); - // stmt_block::Ptr else_block = to(to(to(loop_header_ast)->stmts[ast_index + 1])->else_stmt); - // std::cerr << "stmt block\n"; - - // // while_block->cond = to(to(loop_header_ast)->stmts[ast_index + 1])->cond; - // if (then_block->stmts.size() == 0 && else_block->stmts.size() != 0) { - // while_block->cond = to(to(loop_header_ast)->stmts[ast_index + 1])->cond; - // not_expr::Ptr new_cond = std::make_shared(); - // new_cond->static_offset = while_block->cond->static_offset; - // new_cond->expr1 = while_block->cond; - // while_block->cond = new_cond; - - // for (auto body_stmt: else_block->stmts) { - // to(while_block->body)->stmts.push_back(body_stmt); - // } - - // auto backedge_iter = std::find(backedge_blocks.begin(), backedge_blocks.end(), else_block); - // if (backedge_iter != backedge_blocks.end()) { - // std::cerr << "replaced BE\n"; - // std::replace(backedge_blocks.begin(), backedge_blocks.end(), *backedge_iter, to(while_block->body)); - // } - // while_block->continue_blocks.insert(while_block->continue_blocks.begin(), backedge_blocks.begin(), backedge_blocks.end()); - // } - // // else if (then_block->stmts.size() == 1 && isa(then_block->stmts.back())) { - // // not_expr::Ptr new_cond = std::make_shared(); - // // new_cond->static_offset = while_block->cond->static_offset; - // // new_cond->expr1 = while_block->cond; - // // while_block->cond = new_cond; - - // // // if (else_block->stmts.size() != 0) - // // for (auto body_stmt: else_block->stmts) { - // // to(while_block->body)->stmts.push_back(body_stmt); - // // } - // // // else { - // // then_block->stmts.pop_back(); - // // for (auto stmt: then_block->stmts) - // // to(loop_header_ast)->stmts.push_back(stmt); - // // // } - - // // auto backedge_iter = std::find(backedge_blocks.begin(), backedge_blocks.end(), else_block); - // // if (backedge_iter != backedge_blocks.end()) { - // // std::cerr << "replaced BE\n"; - // // std::replace(backedge_blocks.begin(), backedge_blocks.end(), *backedge_iter, to(while_block->body)); - // // } - // // while_block->continue_blocks.insert(while_block->continue_blocks.begin(), backedge_blocks.begin(), backedge_blocks.end()); - // // } - // // else if (then_block->stmts.size() == 1 && isa(then_block->stmts.back())) { - // // not_expr::Ptr new_cond = std::make_shared(); - // // new_cond->static_offset = while_block->cond->static_offset; - // // new_cond->expr1 = while_block->cond; - // // while_block->cond = new_cond; - - // // then_block->stmts.pop_back(); - // // for (auto stmt: then_block->stmts) - // // to(loop_header_ast)->stmts.push_back(stmt); - // // } - // // else { - // // for (auto body_stmt: then_block->stmts) { - // // to(while_block->body)->stmts.push_back(body_stmt); - // // } - - // // auto backedge_iter = std::find(backedge_blocks.begin(), backedge_blocks.end(), then_block); - // // if (backedge_iter != backedge_blocks.end()) { - // // std::cerr << "replaced BE\n"; - // // std::replace(backedge_blocks.begin(), backedge_blocks.end(), *backedge_iter, to(while_block->body)); - // // } - // // while_block->continue_blocks.insert(while_block->continue_blocks.begin(), backedge_blocks.begin(), backedge_blocks.end()); - // // } - - // // for (auto body_stmt: else_block->stmts) { - // // to(while_block->body)->stmts.push_back(body_stmt); - // // } - // // if block to be replaced with while block - // worklist.push_back(std::make_tuple(ast_index, std::ref(to(loop_header_ast)->stmts), to(while_block))); - // } - // } - // else if (isa(loop_header_ast)) { - // unsigned int ast_index = loops[postorder]->header_block->ast_index; - // stmt_block::Ptr if_then_block = to(to(loop_header_ast)->then_stmt); - // stmt_block::Ptr if_else_block = to(to(loop_header_ast)->else_stmt); - - // if (if_then_block->stmts.size() != 0) { - // std::cerr << "if then block\n"; - // // handle unconditional loops - // if (if_then_block->stmts[ast_index] == loops[postorder]->header_block->parent && !isa(if_then_block->stmts[ast_index + 1])) { - // for (auto bb: loops[postorder]->blocks) { - // to(while_block->body)->stmts.push_back(bb->parent); - // } - // // pop loop backedge - // to(while_block->body)->stmts.pop_back(); - - // // set always true condition - // while_block->cond = std::make_shared(); - // to(while_block->cond)->value = 1; - - // // unconditional loops can have only one backedge !? - // assert(loops[postorder]->loop_latch_blocks.size() == 1); - // for (unsigned int i = ast_index + 2; i < loops[postorder]->loop_latch_blocks[0]->ast_index; i++) { - // worklist.push_back(std::make_tuple(i, std::ref(if_then_block->stmts), nullptr)); - // } - - // worklist.push_back(std::make_tuple(ast_index, std::ref(if_then_block->stmts), to(while_block))); - // } - // else if (if_then_block->stmts[ast_index] == loops[postorder]->header_block->parent) { - // stmt_block::Ptr then_block = to(to(if_then_block->stmts[ast_index + 1])->then_stmt); - // stmt_block::Ptr else_block = to(to(if_then_block->stmts[ast_index + 1])->else_stmt); - - // // while_block->cond = to(if_then_block->stmts[ast_index + 1])->cond; - // if (then_block->stmts.size() == 0 && else_block->stmts.size() != 0) { - // while_block->cond = to(if_then_block->stmts[ast_index + 1])->cond; - // not_expr::Ptr new_cond = std::make_shared(); - // new_cond->static_offset = while_block->cond->static_offset; - // new_cond->expr1 = while_block->cond; - // while_block->cond = new_cond; - - // for (auto body_stmt: else_block->stmts) { - // to(while_block->body)->stmts.push_back(body_stmt); - // } - - // auto backedge_iter = std::find(backedge_blocks.begin(), backedge_blocks.end(), else_block); - // if (backedge_iter != backedge_blocks.end()) { - // std::cerr << "replaced BE\n"; - // std::replace(backedge_blocks.begin(), backedge_blocks.end(), *backedge_iter, to(while_block->body)); - // } - // while_block->continue_blocks.insert(while_block->continue_blocks.begin(), backedge_blocks.begin(), backedge_blocks.end()); - // } - // // else if (then_block->stmts.size() == 1 && isa(then_block->stmts.back())) { - // // not_expr::Ptr new_cond = std::make_shared(); - // // new_cond->static_offset = while_block->cond->static_offset; - // // new_cond->expr1 = while_block->cond; - // // while_block->cond = new_cond; - - // // for (auto body_stmt: else_block->stmts) { - // // to(while_block->body)->stmts.push_back(body_stmt); - // // } - - // // then_block->stmts.pop_back(); - // // for (auto stmt: then_block->stmts) - // // if_then_block->stmts.push_back(stmt); - - // // auto backedge_iter = std::find(backedge_blocks.begin(), backedge_blocks.end(), else_block); - // // if (backedge_iter != backedge_blocks.end()) { - // // std::cerr << "replaced BE\n"; - // // std::replace(backedge_blocks.begin(), backedge_blocks.end(), *backedge_iter, to(while_block->body)); - // // } - // // while_block->continue_blocks.insert(while_block->continue_blocks.begin(), backedge_blocks.begin(), backedge_blocks.end()); - // // } - // // else if (then_block->stmts.size() == 1 && isa(then_block->stmts.back())) { - // // not_expr::Ptr new_cond = std::make_shared(); - // // new_cond->static_offset = while_block->cond->static_offset; - // // new_cond->expr1 = while_block->cond; - // // while_block->cond = new_cond; - - // // then_block->stmts.pop_back(); - // // for (auto stmt: then_block->stmts) - // // if_then_block->stmts.push_back(stmt); - // // } - // // else { - // // for (auto body_stmt: then_block->stmts) { - // // to(while_block->body)->stmts.push_back(body_stmt); - // // } - - // // auto backedge_iter = std::find(backedge_blocks.begin(), backedge_blocks.end(), then_block); - // // if (backedge_iter != backedge_blocks.end()) { - // // std::cerr << "replaced BE\n"; - // // std::replace(backedge_blocks.begin(), backedge_blocks.end(), *backedge_iter, to(while_block->body)); - // // } - // // while_block->continue_blocks.insert(while_block->continue_blocks.begin(), backedge_blocks.begin(), backedge_blocks.end()); - // // } - - // // for (auto body_stmt: else_block->stmts) { - // // to(while_block->body)->stmts.push_back(body_stmt); - // // } - // // if block to be replaced with while block - // worklist.push_back(std::make_tuple(ast_index, std::ref(if_then_block->stmts), to(while_block))); - // } - // } - - // if (if_else_block->stmts.size() != 0) { - // std::cerr << "if else block\n"; - // // handle unconditional loops - // if (if_else_block->stmts[ast_index] == loops[postorder]->header_block->parent && !isa(if_else_block->stmts[ast_index + 1])) { - // for (auto bb: loops[postorder]->blocks) { - // to(while_block->body)->stmts.push_back(bb->parent); - // } - // // pop loop backedge - // to(while_block->body)->stmts.pop_back(); - - // // set always true condition - // while_block->cond = std::make_shared(); - // to(while_block->cond)->value = 1; - - // // unconditional loops can have only one backedge !? - // assert(loops[postorder]->loop_latch_blocks.size() == 1); - // for (unsigned int i = ast_index + 2; i < loops[postorder]->loop_latch_blocks[0]->ast_index; i++) { - // worklist.push_back(std::make_tuple(i, std::ref(if_else_block->stmts), nullptr)); - // } - - // worklist.push_back(std::make_tuple(ast_index, std::ref(if_else_block->stmts), to(while_block))); - // } - // else if (if_else_block->stmts[ast_index] == loops[postorder]->header_block->parent) { - // stmt_block::Ptr then_block = to(to(if_else_block->stmts[ast_index + 1])->then_stmt); - // stmt_block::Ptr else_block = to(to(if_else_block->stmts[ast_index + 1])->else_stmt); - - // // while_block->cond = to(if_else_block->stmts[ast_index + 1])->cond; - // if (then_block->stmts.size() == 0 && else_block->stmts.size() != 0) { - // while_block->cond = to(if_else_block->stmts[ast_index + 1])->cond; - // not_expr::Ptr new_cond = std::make_shared(); - // new_cond->static_offset = while_block->cond->static_offset; - // new_cond->expr1 = while_block->cond; - // while_block->cond = new_cond; - - // for (auto body_stmt: else_block->stmts) { - // to(while_block->body)->stmts.push_back(body_stmt); - // } - - // auto backedge_iter = std::find(backedge_blocks.begin(), backedge_blocks.end(), else_block); - // if (backedge_iter != backedge_blocks.end()) { - // std::cerr << "replaced BE\n"; - // std::replace(backedge_blocks.begin(), backedge_blocks.end(), *backedge_iter, to(while_block->body)); - // } - // while_block->continue_blocks.insert(while_block->continue_blocks.begin(), backedge_blocks.begin(), backedge_blocks.end()); - // } - // // else if (then_block->stmts.size() == 1 && isa(then_block->stmts.back())) { - // // not_expr::Ptr new_cond = std::make_shared(); - // // new_cond->static_offset = while_block->cond->static_offset; - // // new_cond->expr1 = while_block->cond; - // // while_block->cond = new_cond; - - // // for (auto body_stmt: else_block->stmts) { - // // to(while_block->body)->stmts.push_back(body_stmt); - // // } - - // // then_block->stmts.pop_back(); - // // for (auto stmt: then_block->stmts) - // // if_else_block->stmts.push_back(stmt); - - // // auto backedge_iter = std::find(backedge_blocks.begin(), backedge_blocks.end(), else_block); - // // if (backedge_iter != backedge_blocks.end()) { - // // std::cerr << "replaced BE\n"; - // // std::replace(backedge_blocks.begin(), backedge_blocks.end(), *backedge_iter, to(while_block->body)); - // // } - // // while_block->continue_blocks.insert(while_block->continue_blocks.begin(), backedge_blocks.begin(), backedge_blocks.end()); - // // } - // // else if (then_block->stmts.size() == 1 && isa(then_block->stmts.back())) { - // // not_expr::Ptr new_cond = std::make_shared(); - // // new_cond->static_offset = while_block->cond->static_offset; - // // new_cond->expr1 = while_block->cond; - // // while_block->cond = new_cond; - - // // then_block->stmts.pop_back(); - // // for (auto stmt: then_block->stmts) - // // if_else_block->stmts.push_back(stmt); - // // } - // // else { - // // for (auto body_stmt: then_block->stmts) { - // // to(while_block->body)->stmts.push_back(body_stmt); - // // } - - // // auto backedge_iter = std::find(backedge_blocks.begin(), backedge_blocks.end(), then_block); - // // if (backedge_iter != backedge_blocks.end()) { - // // std::cerr << "replaced BE\n"; - // // std::replace(backedge_blocks.begin(), backedge_blocks.end(), *backedge_iter, to(while_block->body)); - // // } - // // while_block->continue_blocks.insert(while_block->continue_blocks.begin(), backedge_blocks.begin(), backedge_blocks.end()); - // // } - - // // for (auto body_stmt: else_block->stmts) { - // // to(while_block->body)->stmts.push_back(body_stmt); - // // } - // // if block to be replaced with while block - // worklist.push_back(std::make_tuple(ast_index, std::ref(if_else_block->stmts), to(while_block))); - // } - // } - // } - - // // process worklist - // std::sort(worklist.begin(), worklist.end(), [](std::tuple>, stmt::Ptr> a, std::tuple>, stmt::Ptr> b) { - // return std::get<0>(a) > std::get<0>(b); - // }); - // for (auto item : worklist) { - // std::vector &temp_ast = std::get<1>(item); - // if (std::get<2>(item)) { - // std::replace(temp_ast.begin(), temp_ast.end(), temp_ast[std::get<0>(item) + 1], std::get<2>(item)); - // temp_ast.erase(temp_ast.begin() + std::get<0>(item)); - // } - // } - - // for (auto item : worklist) { - // std::vector &temp_ast = std::get<1>(item); - // if (!std::get<2>(item)) { - // temp_ast.erase(temp_ast.begin() + std::get<0>(item)); - // } - // } - // worklist.clear(); - - // std::cerr << "after ast\n"; - // ast->dump(std::cerr, 0); - // std::cerr << "after ast\n"; } diff --git a/src/builder/builder_context.cpp b/src/builder/builder_context.cpp index 31c5859..674570a 100644 --- a/src/builder/builder_context.cpp +++ b/src/builder/builder_context.cpp @@ -359,8 +359,8 @@ block::stmt::Ptr builder_context::extract_ast_from_function_impl(void) { std::cerr << "get_idom(int) test: get_idom(0): " << dom.get_idom(0) << "\n"; std::cerr << "get_idom(int) test: get_idom(-1): " << dom.get_idom(-1) << "\n"; - for (int i: dom.get_idom()) { - std::cerr << i << "\n"; + for (unsigned int i = 0; i < dom.get_idom().size(); i++) { + std::cerr << i << " : " << dom.get_idom()[i] << "\n"; } std::cerr << "== idom ==\n"; @@ -418,8 +418,8 @@ block::stmt::Ptr builder_context::extract_ast_from_function_impl(void) { std::cerr << "get_idom(int) test: get_idom(0): " << post_dom.get_idom(0) << "\n"; std::cerr << "get_idom(int) test: get_idom(-1): " << post_dom.get_idom(-1) << "\n"; - for (int i: post_dom.get_idom()) { - std::cerr << i << "\n"; + for (unsigned int i = 0; i < post_dom.get_idom().size(); i++) { + std::cerr << i << " : " << post_dom.get_idom()[i] << "\n"; } std::cerr << "== (postdom) idom ==\n"; @@ -498,6 +498,7 @@ block::stmt::Ptr builder_context::extract_ast_from_function_impl(void) { std::cerr << "++++++ loop info ++++++ \n"; + // return ast; std::cerr << "++++++ convert to ast ++++++ \n"; ast = LI.convert_to_ast(block::to(ast)); std::cerr << "++++++ convert to ast ++++++ \n";