https://gcc.gnu.org/g:97a0d40594a9e5c6c6b18a6548ecbfa22f181e5f

commit 97a0d40594a9e5c6c6b18a6548ecbfa22f181e5f
Author: Jakub Dupak <d...@jakubdupak.com>
Date:   Wed Oct 18 23:27:48 2023 +0200

    borrowck: Dump improve jumps
    
    gcc/rust/ChangeLog:
    
            * checks/errors/borrowck/rust-bir-dump.cc (Dump::go): Improve jumps 
dump.
            (Dump::visit): Improve jumps dump.
            * checks/errors/borrowck/rust-bir-dump.h (class Dump): Improve 
jumps dump.
    
    Signed-off-by: Jakub Dupak <d...@jakubdupak.com>

Diff:
---
 gcc/rust/checks/errors/borrowck/rust-bir-dump.cc | 77 ++++++++++++++++--------
 gcc/rust/checks/errors/borrowck/rust-bir-dump.h  |  4 +-
 2 files changed, 56 insertions(+), 25 deletions(-)

diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc 
b/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc
index 709d36a7cd33..ba530983dade 100644
--- a/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc
+++ b/gcc/rust/checks/errors/borrowck/rust-bir-dump.cc
@@ -6,13 +6,6 @@ namespace BIR {
 
 constexpr auto indentation = "    ";
 
-uint32_t
-get_place_name (PlaceId place_id)
-{
-  rust_assert (place_id >= FIRST_VARIABLE_PLACE);
-  return place_id - FIRST_VARIABLE_PLACE;
-}
-
 uint32_t
 get_lifetime_name (Lifetime lifetime_id)
 {
@@ -44,6 +37,28 @@ print_comma_separated (std::ostream &stream, const 
std::vector<T> &collection,
     }
 }
 
+void
+renumber_places (const Function &func, std::vector<PlaceId> &place_map)
+{
+  // Renumbering places to avoid gaps in the place id space.
+  // This is needed to match MIR shape.
+  size_t next_out_id = 0;
+
+  for (size_t in_id = FIRST_VARIABLE_PLACE; in_id < func.place_db.size ();
+       ++in_id)
+    {
+      const Place &place = func.place_db[in_id];
+      if (place.kind == Place::VARIABLE || place.kind == Place::TEMPORARY)
+       {
+         place_map[in_id] = next_out_id++;
+       }
+      else
+       {
+         place_map[in_id] = INVALID_PLACE;
+       }
+    }
+}
+
 void
 simplify_cfg (Function &func, std::vector<BasicBlockId> &bb_fold_map)
 {
@@ -74,12 +89,16 @@ Dump::go (bool enable_simplify_cfg)
   // To avoid mutation of the BIR, we use indirection through bb_fold_map.
   std::iota (bb_fold_map.begin (), bb_fold_map.end (), 0);
 
+  std::iota (place_map.begin (), place_map.end (), 0);
+
   if (enable_simplify_cfg)
     simplify_cfg (func, bb_fold_map);
 
+  renumber_places (func, place_map);
+
   stream << "fn " << name << "(";
   print_comma_separated (stream, func.arguments, [this] (PlaceId place_id) {
-    stream << "_" << get_place_name (place_id) << ": "
+    stream << "_" << place_map[place_id] << ": "
           << get_tyty_name (place_db[place_id].tyty);
   });
   stream << ") -> " << get_tyty_name (place_db[RETURN_VALUE_PLACE].tyty)
@@ -93,33 +112,30 @@ Dump::go (bool enable_simplify_cfg)
          if (std::find (func.arguments.begin (), func.arguments.end (), id)
              != func.arguments.end ())
            continue;
-         stream << indentation << "let _" << get_place_name (id) << ": "
+         stream << indentation << "let _" << place_map[id] << ": "
                 << get_tyty_name (place_db[id].tyty) << ";\n";
        }
     }
 
-  for (BasicBlockId id = 0; id < func.basic_blocks.size (); ++id)
+  for (node_bb = 0; node_bb < func.basic_blocks.size (); ++node_bb)
     {
-      if (bb_fold_map[id] != id)
+      if (bb_fold_map[node_bb] != node_bb)
        continue; // This BB was folded.
 
-      if (func.basic_blocks[id].statements.empty ()
-         && func.basic_blocks[id].successors.empty ())
+      if (func.basic_blocks[node_bb].statements.empty ()
+         && func.basic_blocks[node_bb].successors.empty ())
        continue;
 
-      BasicBlock &bb = func.basic_blocks[id];
+      BasicBlock &bb = func.basic_blocks[node_bb];
       stream << "\n";
-      stream << indentation << "bb" << bb_fold_map[id] << ": {\n";
+      stream << indentation << "bb" << bb_fold_map[node_bb] << ": {\n";
       for (auto &stmt : bb.statements)
        {
          stream << indentation << indentation;
          visit (stmt);
          stream << ";\n";
        }
-      stream << indentation << "} -> [";
-      for (auto succ : bb.successors)
-       stream << "bb" << bb_fold_map[succ] << ", ";
-      stream << "]\n";
+      stream << indentation << "}\n";
     }
 
   stream << "}\n\n";
@@ -131,19 +147,26 @@ Dump::visit (Node &node)
   switch (node.get_kind ())
     {
       case Node::Kind::ASSIGNMENT: {
-       stream << "_" << get_place_name (node.get_place ()) << " = ";
+       stream << "_" << place_map[node.get_place ()] << " = ";
        node.get_expr ().accept_vis (*this);
        break;
       }
     case Node::Kind::SWITCH:
-      stream << "switch ";
+      stream << "switchInt(";
       visit_move_place (node.get_place ());
+      stream << ") -> [";
+      print_comma_separated (stream, func.basic_blocks[node_bb].successors,
+                            [this] (BasicBlockId succ) {
+                              stream << "bb" << bb_fold_map[succ];
+                            });
+      stream << "]";
       break;
     case Node::Kind::RETURN:
       stream << "return";
       break;
     case Node::Kind::GOTO:
-      stream << "goto";
+      stream << "goto -> bb"
+            << bb_fold_map[func.basic_blocks[node_bb].successors.at (0)];
       break;
     case Node::Kind::STORAGE_DEAD:
       stream << "StorageDead(";
@@ -167,7 +190,7 @@ Dump::visit_place (PlaceId place_id)
     {
     case Place::TEMPORARY:
     case Place::VARIABLE:
-      stream << "_" << get_place_name (place_id);
+      stream << "_" << place_map[place_id];
       break;
     case Place::DEREF:
       stream << "(";
@@ -255,7 +278,13 @@ Dump::visit (CallExpr &expr)
       visit_move_place (place);
       stream << ", ";
     }
-  stream << ")";
+  stream << ") -> [";
+  print_comma_separated (stream,
+                        func.basic_blocks[bb_fold_map[node_bb]].successors,
+                        [this] (const BasicBlockId &dst) {
+                          stream << "bb" << bb_fold_map[dst];
+                        });
+  stream << "]";
 }
 
 void
diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-dump.h 
b/gcc/rust/checks/errors/borrowck/rust-bir-dump.h
index 1f8de045c22c..edf7d1ea1ec3 100644
--- a/gcc/rust/checks/errors/borrowck/rust-bir-dump.h
+++ b/gcc/rust/checks/errors/borrowck/rust-bir-dump.h
@@ -36,13 +36,15 @@ class Dump : public Visitor
   const std::string &name;
 
   std::vector<BasicBlockId> bb_fold_map;
+  std::vector<PlaceId> place_map;
 
   PlaceId node_place = INVALID_PLACE;
+  BasicBlockId node_bb = INVALID_BB;
 
 public:
   Dump (std::ostream &os, Function &func, const std::string &name)
     : stream (os), place_db (func.place_db), func (func), name (name),
-      bb_fold_map (func.basic_blocks.size ())
+      bb_fold_map (func.basic_blocks.size ()), place_map (func.place_db.size 
())
   {}
   void go (bool enable_simplify_cfg = false);

Reply via email to