https://gcc.gnu.org/g:152ecf846f65c760c73ea77e8612eeeb4531fde4

commit r15-8371-g152ecf846f65c760c73ea77e8612eeeb4531fde4
Author: Kushal Pal <kushalpal...@gmail.com>
Date:   Wed Aug 28 06:16:11 2024 +0000

    gccrs: Use `IndexVec` for bb_fold_map
    
    gcc/rust/ChangeLog:
    
            * checks/errors/borrowck/rust-bir-dump.cc (simplify_cfg):
            Used `IndexVec` for bb_fold_map.
            (Dump::go): Use strong type as index instead of value as now we
            are using `IndexVec`.
            (Dump::visit): Likewise.
            * checks/errors/borrowck/rust-bir-dump.h (class Dump): Use
            `IndexVec` for bb_fold_map.
            * checks/errors/borrowck/rust-bir-place.h: Add constructor for
            `IndexVec` that can reserve size.
    
    Signed-off-by: Kushal Pal <kushalpal...@gmail.com>

Diff:
---
 gcc/rust/checks/errors/borrowck/rust-bir-dump.cc | 27 ++++++++++++------------
 gcc/rust/checks/errors/borrowck/rust-bir-dump.h  |  2 +-
 gcc/rust/checks/errors/borrowck/rust-bir-place.h |  3 +++
 3 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc 
b/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc
index b01d08a0456c..c9e01545c6aa 100644
--- a/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc
+++ b/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc
@@ -71,7 +71,7 @@ renumber_places (const Function &func, std::vector<PlaceId> 
&place_map)
 }
 
 void
-simplify_cfg (Function &func, std::vector<BasicBlockId> &bb_fold_map)
+simplify_cfg (Function &func, IndexVec<BasicBlockId, BasicBlockId> 
&bb_fold_map)
 {
   // The BIR builder can generate many useless basic blocks, which contain only
   // a goto.
@@ -84,11 +84,11 @@ 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]];
+         const BasicBlock &bb = func.basic_blocks[bb_fold_map[i]];
          if (bb.statements.empty () && bb.is_goto_terminated ())
            {
              auto dst = bb.successors.at (0);
-             if (bb_fold_map[dst.value] != dst)
+             if (bb_fold_map[dst] != dst)
                {
                  rust_error_at (
                    UNKNOWN_LOCATION,
@@ -100,12 +100,12 @@ simplify_cfg (Function &func, std::vector<BasicBlockId> 
&bb_fold_map)
                  for (BasicBlockId i = ENTRY_BASIC_BLOCK;
                       i.value < bb_fold_map.size (); ++i.value)
                    {
-                     bb_fold_map[i.value] = i;
+                     bb_fold_map[i] = i;
                    }
                  stabilized = true;
                  break;
                }
-             bb_fold_map[i.value] = dst;
+             bb_fold_map[i] = dst;
              stabilized = false;
            }
        }
@@ -119,7 +119,7 @@ Dump::go (bool enable_simplify_cfg)
   for (BasicBlockId i = ENTRY_BASIC_BLOCK; i.value < bb_fold_map.size ();
        ++i.value)
     {
-      bb_fold_map[i.value] = i;
+      bb_fold_map[i] = i;
     }
   for (size_t i = 0; i < place_map.size (); ++i)
     {
@@ -146,7 +146,7 @@ Dump::go (bool enable_simplify_cfg)
   for (statement_bb = ENTRY_BASIC_BLOCK;
        statement_bb.value < func.basic_blocks.size (); ++statement_bb.value)
     {
-      if (bb_fold_map[statement_bb.value] != statement_bb)
+      if (bb_fold_map[statement_bb] != statement_bb)
        continue; // This BB was folded.
 
       if (func.basic_blocks[statement_bb].statements.empty ()
@@ -157,7 +157,7 @@ Dump::go (bool enable_simplify_cfg)
 
       BasicBlock &bb = func.basic_blocks[statement_bb];
       stream << "\n";
-      stream << indentation << "bb" << bb_fold_map[statement_bb.value].value
+      stream << indentation << "bb" << bb_fold_map[statement_bb].value
             << ": {\n";
       size_t i = 0;
       for (auto &stmt : bb.statements)
@@ -168,8 +168,8 @@ Dump::go (bool enable_simplify_cfg)
        }
       if (!bb_terminated)
        stream << indentation << indentation << "goto -> bb"
-              << bb_fold_map[bb.successors.at (0).value].value << ";\t\t"
-              << i++ << "\n";
+              << bb_fold_map[bb.successors.at (0)].value << ";\t\t" << i++
+              << "\n";
 
       stream << indentation << "}\n";
     }
@@ -194,7 +194,7 @@ Dump::visit (const Statement &stmt)
       stream << ") -> [";
       print_comma_separated (stream, 
func.basic_blocks[statement_bb].successors,
                             [this] (BasicBlockId succ) {
-                              stream << "bb" << bb_fold_map[succ.value].value;
+                              stream << "bb" << bb_fold_map[succ].value;
                             });
       stream << "]";
       bb_terminated = true;
@@ -206,8 +206,7 @@ Dump::visit (const Statement &stmt)
     case Statement::Kind::GOTO:
       stream
        << "goto -> bb"
-       << bb_fold_map[func.basic_blocks[statement_bb].successors.at (0).value]
-            .value;
+       << bb_fold_map[func.basic_blocks[statement_bb].successors.at (0)].value;
       bb_terminated = true;
       break;
     case Statement::Kind::STORAGE_DEAD:
@@ -329,7 +328,7 @@ Dump::visit (const CallExpr &expr)
   stream << ") -> [";
   print_comma_separated (stream, func.basic_blocks[statement_bb].successors,
                         [this] (BasicBlockId succ) {
-                          stream << "bb" << bb_fold_map[succ.value].value;
+                          stream << "bb" << bb_fold_map[succ].value;
                         });
   stream << "]";
   bb_terminated = true;
diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-dump.h 
b/gcc/rust/checks/errors/borrowck/rust-bir-dump.h
index e88681f7d4af..bd6cbb1ee50a 100644
--- a/gcc/rust/checks/errors/borrowck/rust-bir-dump.h
+++ b/gcc/rust/checks/errors/borrowck/rust-bir-dump.h
@@ -34,7 +34,7 @@ class Dump : public Visitor
   Function &func;
   const std::string &name;
 
-  std::vector<BasicBlockId> bb_fold_map;
+  IndexVec<BasicBlockId, BasicBlockId> bb_fold_map;
   std::vector<PlaceId> place_map;
 
   PlaceId statement_place = INVALID_PLACE;
diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-place.h 
b/gcc/rust/checks/errors/borrowck/rust-bir-place.h
index 148bd2a7f495..a1621b7455d8 100644
--- a/gcc/rust/checks/errors/borrowck/rust-bir-place.h
+++ b/gcc/rust/checks/errors/borrowck/rust-bir-place.h
@@ -205,6 +205,9 @@ template <typename I, typename T> class IndexVec
   std::vector<T> internal_vector;
 
 public:
+  IndexVec () = default;
+  IndexVec (size_t size) { internal_vector.reserve (size); }
+
   T &at (I pid) { return internal_vector[pid.value]; }
   const T &at (I pid) const { return internal_vector[pid.value]; }
   T &operator[] (I pid) { return internal_vector[pid.value]; }

Reply via email to