https://gcc.gnu.org/g:1fcfec23a452427f6fd8596ee6345cf7bb49dd97
commit 1fcfec23a452427f6fd8596ee6345cf7bb49dd97 Author: Kushal Pal <kushalpal...@gmail.com> Date: Mon Aug 19 10:06:49 2024 +0000 Used `IndexVec` for BasicBlocks gcc/rust/ChangeLog: * checks/errors/borrowck/rust-bir-builder-expr-stmt.cc (ExprStmtBuilder::visit): Use strong type as index and remove access to numeric value. * checks/errors/borrowck/rust-bir-builder-internal.h (struct BuilderContext): Likewise. * checks/errors/borrowck/rust-bir-dump.cc (simplify_cfg): Likewise. (Dump::go): Likewise. (Dump::visit): Likewise. * checks/errors/borrowck/rust-bir-fact-collector.h (class FactCollector): Likewise. (points): Likewise. * checks/errors/borrowck/rust-bir.h (struct BasicBlockId): Used IndexVec for BasicBlocks. (struct Function): Likewise. * checks/errors/borrowck/rust-borrow-checker-diagnostics.cc (BorrowCheckerDiagnostics::get_statement): Change the extracted index to strong type. Signed-off-by: Kushal Pal <kushalpal...@gmail.com> Diff: --- .../errors/borrowck/rust-bir-builder-expr-stmt.cc | 6 +++--- .../errors/borrowck/rust-bir-builder-internal.h | 6 +++--- gcc/rust/checks/errors/borrowck/rust-bir-dump.cc | 19 ++++++++----------- .../checks/errors/borrowck/rust-bir-fact-collector.h | 6 +++--- gcc/rust/checks/errors/borrowck/rust-bir.h | 4 +++- .../borrowck/rust-borrow-checker-diagnostics.cc | 4 ++-- 6 files changed, 22 insertions(+), 23 deletions(-) diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc b/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc index 501456633f09..c11cff0ae4df 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc +++ b/gcc/rust/checks/errors/borrowck/rust-bir-builder-expr-stmt.cc @@ -565,7 +565,7 @@ ExprStmtBuilder::visit (HIR::IfExpr &expr) add_jump (if_block, then_start_block); add_jump (if_block, final_block); - auto &then_end_bb = ctx.basic_blocks[then_end_block.value]; + auto &then_end_bb = ctx.basic_blocks[then_end_block]; if (then_end_bb.is_goto_terminated () && then_end_bb.successors.empty ()) add_jump (then_end_block, final_block); } @@ -602,11 +602,11 @@ ExprStmtBuilder::visit (HIR::IfExprConseqElse &expr) add_jump (if_end_bb, then_start_bb); add_jump (if_end_bb, else_start_bb); - auto &then_bb = ctx.basic_blocks[then_end_bb.value]; + auto &then_bb = ctx.basic_blocks[then_end_bb]; if (then_bb.is_goto_terminated () && then_bb.successors.empty ()) add_jump (then_end_bb, final_start_bb); - auto &else_bb = ctx.basic_blocks[else_end_bb.value]; + auto &else_bb = ctx.basic_blocks[else_end_bb]; if (else_bb.is_goto_terminated () && else_bb.successors.empty ()) add_jump (else_end_bb, final_start_bb); } diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-builder-internal.h b/gcc/rust/checks/errors/borrowck/rust-bir-builder-internal.h index 694b2d6fb0cf..8de6b8b00a91 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-builder-internal.h +++ b/gcc/rust/checks/errors/borrowck/rust-bir-builder-internal.h @@ -76,7 +76,7 @@ struct BuilderContext Resolver::Resolver &resolver; // BIR output - std::vector<BasicBlock> basic_blocks; + BasicBlocks basic_blocks; BasicBlockId current_bb = ENTRY_BASIC_BLOCK; /** @@ -107,7 +107,7 @@ public: basic_blocks.emplace_back (); // StartBB } - BasicBlock &get_current_bb () { return basic_blocks[current_bb.value]; } + BasicBlock &get_current_bb () { return basic_blocks[current_bb]; } const LoopAndLabelCtx &lookup_label (NodeId label) { @@ -378,7 +378,7 @@ protected: // CFG helpers void add_jump (BasicBlockId from, BasicBlockId to) { - ctx.basic_blocks[from.value].successors.emplace_back (to); + ctx.basic_blocks[from].successors.emplace_back (to); } void add_jump_to (BasicBlockId bb) { add_jump (ctx.current_bb, bb); } diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc b/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc index 86bc5b1a3d65..5bcf22448c83 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc +++ b/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc @@ -84,7 +84,7 @@ simplify_cfg (Function &func, std::vector<BasicBlockId> &bb_fold_map) // BB0 cannot be folded as it is an entry block. for (BasicBlockId i = {1}; i.value < func.basic_blocks.size (); ++i.value) { - const BasicBlock &bb = func.basic_blocks[bb_fold_map[i.value].value]; + const BasicBlock &bb = func.basic_blocks[bb_fold_map[i.value]]; if (bb.statements.empty () && bb.is_goto_terminated ()) { auto dst = bb.successors.at (0); @@ -149,13 +149,13 @@ Dump::go (bool enable_simplify_cfg) if (bb_fold_map[statement_bb.value] != statement_bb) continue; // This BB was folded. - if (func.basic_blocks[statement_bb.value].statements.empty () - && func.basic_blocks[statement_bb.value].successors.empty ()) + if (func.basic_blocks[statement_bb].statements.empty () + && func.basic_blocks[statement_bb].successors.empty ()) continue; bb_terminated = false; - BasicBlock &bb = func.basic_blocks[statement_bb.value]; + BasicBlock &bb = func.basic_blocks[statement_bb]; stream << "\n"; stream << indentation << "bb" << bb_fold_map[statement_bb.value].value << ": {\n"; @@ -192,8 +192,7 @@ Dump::visit (const Statement &stmt) stream << "switchInt("; visit_move_place (stmt.get_place ()); stream << ") -> ["; - print_comma_separated (stream, - func.basic_blocks[statement_bb.value].successors, + print_comma_separated (stream, func.basic_blocks[statement_bb].successors, [this] (BasicBlockId succ) { stream << "bb" << bb_fold_map[succ.value].value; }); @@ -207,9 +206,8 @@ Dump::visit (const Statement &stmt) case Statement::Kind::GOTO: stream << "goto -> bb" - << bb_fold_map - [func.basic_blocks[statement_bb.value].successors.at (0).value] - .value; + << bb_fold_map[func.basic_blocks[statement_bb].successors.at (0).value] + .value; bb_terminated = true; break; case Statement::Kind::STORAGE_DEAD: @@ -329,8 +327,7 @@ Dump::visit (const CallExpr &expr) visit_move_place (place_id); }); stream << ") -> ["; - print_comma_separated (stream, - func.basic_blocks[statement_bb.value].successors, + print_comma_separated (stream, func.basic_blocks[statement_bb].successors, [this] (BasicBlockId succ) { stream << "bb" << bb_fold_map[succ.value].value; }); diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-fact-collector.h b/gcc/rust/checks/errors/borrowck/rust-bir-fact-collector.h index 0d59f80a3c52..0c3e7330c39e 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-fact-collector.h +++ b/gcc/rust/checks/errors/borrowck/rust-bir-fact-collector.h @@ -40,7 +40,7 @@ class FactCollector : public Visitor // Read-only context. const PlaceDB &place_db; - const std::vector<BasicBlock> &basic_blocks; + const BasicBlocks &basic_blocks; const PlaceId first_local; const location_t location; @@ -212,7 +212,7 @@ protected: // Main collection entry points (for different categories). for (current_bb = ENTRY_BASIC_BLOCK; current_bb.value < basic_blocks.size (); ++current_bb.value) { - auto &bb = basic_blocks[current_bb.value]; + auto &bb = basic_blocks[current_bb]; for (current_stmt = 0; current_stmt < bb.statements.size (); ++current_stmt) { @@ -393,7 +393,7 @@ protected: // Main collection entry points (for different categories). protected: // Statement visitor helpers WARN_UNUSED_RESULT const BasicBlock &get_current_bb () const { - return basic_blocks[current_bb.value]; + return basic_blocks[current_bb]; } WARN_UNUSED_RESULT static Polonius::Point diff --git a/gcc/rust/checks/errors/borrowck/rust-bir.h b/gcc/rust/checks/errors/borrowck/rust-bir.h index 4bfc1477dff0..e303a84715b4 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir.h +++ b/gcc/rust/checks/errors/borrowck/rust-bir.h @@ -30,6 +30,8 @@ namespace Rust { namespace BIR { struct BasicBlock; +struct BasicBlockId; +using BasicBlocks = IndexVec<BasicBlockId, BasicBlock>; class Statement; class AbstractExpr; @@ -42,7 +44,7 @@ struct Function { PlaceDB place_db; std::vector<PlaceId> arguments; - std::vector<BasicBlock> basic_blocks; + BasicBlocks basic_blocks; FreeRegions universal_regions; std::vector<std::pair<FreeRegion, FreeRegion>> universal_region_bounds; std::unordered_map<Polonius::Origin, HIR::LifetimeParam *> region_hir_map; diff --git a/gcc/rust/checks/errors/borrowck/rust-borrow-checker-diagnostics.cc b/gcc/rust/checks/errors/borrowck/rust-borrow-checker-diagnostics.cc index 4002ed4dd345..6c67706780bf 100644 --- a/gcc/rust/checks/errors/borrowck/rust-borrow-checker-diagnostics.cc +++ b/gcc/rust/checks/errors/borrowck/rust-borrow-checker-diagnostics.cc @@ -135,8 +135,8 @@ BorrowCheckerDiagnostics::get_statement (Polonius::Point point) // assert that the extracted indexes are valid rust_assert (bb_index < bir_function.basic_blocks.size ()); rust_assert (statement_index - < bir_function.basic_blocks[bb_index].statements.size ()); - return bir_function.basic_blocks[bb_index].statements[statement_index]; + < bir_function.basic_blocks[{bb_index}].statements.size ()); + return bir_function.basic_blocks[{bb_index}].statements[statement_index]; } const BIR::Loan &