From 75909726d04384b0dd9c240b93af9180c4083931 Mon Sep 17 00:00:00 2001 From: Vedant Paranjape <22630228+VedantParanjape@users.noreply.github.com> Date: Tue, 20 Jun 2023 19:33:04 +0530 Subject: [PATCH] Move the CFG generator driver to builder_context.cpp --- include/blocks/basic_blocks.h | 1 + include/blocks/loop_finder.h | 1 - include/builder/builder_context.h | 1 + src/blocks/basic_blocks.cpp | 17 ++++++++--- src/blocks/loop_finder.cpp | 48 +++++++++---------------------- src/builder/builder_context.cpp | 22 ++++++++++++++ 6 files changed, 50 insertions(+), 40 deletions(-) diff --git a/include/blocks/basic_blocks.h b/include/blocks/basic_blocks.h index c9e92de..7239298 100644 --- a/include/blocks/basic_blocks.h +++ b/include/blocks/basic_blocks.h @@ -14,6 +14,7 @@ class basic_block { std::vector> successor; block::expr::Ptr branch_expr; block::stmt::Ptr parent; + unsigned int index; std::string name; }; diff --git a/include/blocks/loop_finder.h b/include/blocks/loop_finder.h index 841cd41..317cca8 100644 --- a/include/blocks/loop_finder.h +++ b/include/blocks/loop_finder.h @@ -2,7 +2,6 @@ #define LOOP_FINDER_H #include "blocks/block_visitor.h" #include "blocks/stmt.h" -#include "blocks/basic_blocks.h" namespace block { class loop_finder : public block_visitor { diff --git a/include/builder/builder_context.h b/include/builder/builder_context.h index 113bc88..9714dc4 100644 --- a/include/builder/builder_context.h +++ b/include/builder/builder_context.h @@ -1,5 +1,6 @@ #ifndef BUILDER_CONTEXT #define BUILDER_CONTEXT +#include "blocks/basic_blocks.h" #include "blocks/expr.h" #include "blocks/stmt.h" #include "builder/forward_declarations.h" diff --git a/src/blocks/basic_blocks.cpp b/src/blocks/basic_blocks.cpp index 9458dc9..4957fe7 100644 --- a/src/blocks/basic_blocks.cpp +++ b/src/blocks/basic_blocks.cpp @@ -12,6 +12,7 @@ std::vector> generate_basic_blocks(block::stmt_bloc for (auto st: ast->stmts) { auto bb = std::make_shared(std::to_string(basic_block_count)); bb->parent = st; + // bb->index = ; work_list.push_back(bb); basic_block_count++; } @@ -21,7 +22,7 @@ std::vector> generate_basic_blocks(block::stmt_bloc work_list[i]->successor.push_back(work_list[i+1]); } - // step 3: process blocks + // step 3: process blocks: every xx_stmt type statement is made out into a basic block while (work_list.size()) { auto bb = work_list.front(); @@ -31,22 +32,30 @@ std::vector> generate_basic_blocks(block::stmt_bloc if (stmt_block_->stmts.size() > 0) { std::vector> stmt_block_list; - + + // convert all statements of this stmt_block into a basic block for (auto st: stmt_block_->stmts) { stmt_block_list.push_back(std::make_shared(std::to_string(basic_block_count++))); stmt_block_list.back()->parent = st; } - + + // set the basic block successors for (unsigned i = 0; stmt_block_list.size() != 0 && i < stmt_block_list.size() - 1; i++) { stmt_block_list[i]->successor.push_back(stmt_block_list[i+1]); } + // since we insert these stmts between bb1 ---> bb2 ==> bb1 ---> (bb-a1...bb-an) ---> bb2 + // point the successor of the stmt_block_list to the basic block that bb1's successor + // pointed to. After this, clear the bb1's successor and push the front of stmt_block_list + // to bb1's successor list. stmt_block_list.back()->successor.push_back(bb->successor.front()); bb->successor.clear(); bb->successor.push_back(stmt_block_list.front()); + // push a rather empty-ish basic block, which will branch to the next basic block, or the next statement. return_list.push_back(bb); work_list.pop_front(); + // now insert the pending blocks to be processed at the front of the work_list work_list.insert(work_list.begin(), stmt_block_list.begin(), stmt_block_list.end()); } else { @@ -67,7 +76,7 @@ std::vector> generate_basic_blocks(block::stmt_bloc exit_bb->parent = std::make_shared(); // check if this is the last block, if yes the successor will be empty if (bb->successor.size()) { - // set the successor to the block that if_stmt pointer to earlier + // set the successor to the block that if_stmt successor pointer to earlier exit_bb->successor.push_back(bb->successor.front()); // clear the successor block from the if_stmt bb->successor.clear(); diff --git a/src/blocks/loop_finder.cpp b/src/blocks/loop_finder.cpp index 92d39f4..e4eac34 100644 --- a/src/blocks/loop_finder.cpp +++ b/src/blocks/loop_finder.cpp @@ -129,43 +129,21 @@ static void trim_from_parents(std::vector &parents, std::vector } void loop_finder::visit(stmt_block::Ptr a) { - std::vector> BBs = generate_basic_blocks(a); - - std::cout << "++++++ basic blocks ++++++ \n"; - for (auto bb: BBs) { - std::cout << bb->name << ":" << " ; "; - for (auto pred: bb->predecessor) { - std::cout << pred->name << ", "; - } - std::cout << "\n"; - if (bb->branch_expr) { - std::cout << " "; - bb->branch_expr->dump(std::cout, 0); - } - std::cout << " "; - std::cout << "br "; - for (auto branches: bb->successor) { - std::cout << branches->name << ", "; + while (1) { + label_stmt::Ptr found_label = nullptr; + for (auto stmt : a->stmts) { + if (isa(stmt)) { + found_label = to(stmt); + } } - std::cout << "\n"; + if (found_label == nullptr) + break; + visit_label(found_label, a); + } + // Once all labels are done, visit the instructions normally + for (auto stmt : a->stmts) { + stmt->accept(this); } - std::cout << "++++++ basic blocks ++++++ \n"; - - // while (1) { - // label_stmt::Ptr found_label = nullptr; - // for (auto stmt : a->stmts) { - // if (isa(stmt)) { - // found_label = to(stmt); - // } - // } - // if (found_label == nullptr) - // break; - // visit_label(found_label, a); - // } - // // Once all labels are done, visit the instructions normally - // for (auto stmt : a->stmts) { - // stmt->accept(this); - // } } void loop_finder::visit_label(label_stmt::Ptr a, stmt_block::Ptr parent) { diff --git a/src/builder/builder_context.cpp b/src/builder/builder_context.cpp index 0b7aefc..327ebff 100644 --- a/src/builder/builder_context.cpp +++ b/src/builder/builder_context.cpp @@ -292,6 +292,28 @@ block::stmt::Ptr builder_context::extract_ast_from_function_impl(void) { block::eliminate_redundant_vars(ast); } + std::vector> BBs = generate_basic_blocks(block::to(ast)); + + std::cerr << "++++++ basic blocks ++++++ \n"; + for (auto bb: BBs) { + std::cerr << bb->name << ":" << " ; "; + for (auto pred: bb->predecessor) { + std::cerr << pred->name << ", "; + } + std::cerr << "\n"; + if (bb->branch_expr) { + std::cerr << " "; + bb->branch_expr->dump(std::cerr, 0); + } + std::cerr << " "; + std::cerr << "br "; + for (auto branches: bb->successor) { + std::cerr << branches->name << ", "; + } + std::cerr << "\n"; + } + std::cerr << "++++++ basic blocks ++++++ \n"; + if (feature_unstructured) return ast;