]> git.lizzy.rs Git - rust.git/commitdiff
Avoid memory allocation when removing dead blocks
authorTomasz Miąsko <tomasz.miasko@gmail.com>
Thu, 28 Jan 2021 00:00:00 +0000 (00:00 +0000)
committerTomasz Miąsko <tomasz.miasko@gmail.com>
Thu, 28 Jan 2021 00:00:00 +0000 (00:00 +0000)
Use `reachable_as_bitset` to reuse a bitset from the traversal rather
than allocating it seprately. Additionally check if there are any
unreachable blocks before proceeding.

compiler/rustc_mir/src/transform/simplify.rs

index 289231e52cb41baf65c40823b75eb58f6e2f9d59..11539d3ef30f4a9aa244b71c4df880b90fe22798 100644 (file)
@@ -28,7 +28,6 @@
 //! return.
 
 use crate::transform::MirPass;
-use rustc_index::bit_set::BitSet;
 use rustc_index::vec::{Idx, IndexVec};
 use rustc_middle::mir::visit::{MutVisitor, MutatingUseContext, PlaceContext, Visitor};
 use rustc_middle::mir::*;
@@ -288,17 +287,17 @@ fn strip_nops(&mut self) {
 }
 
 pub fn remove_dead_blocks(body: &mut Body<'_>) {
-    let mut seen = BitSet::new_empty(body.basic_blocks().len());
-    for (bb, _) in traversal::preorder(body) {
-        seen.insert(bb.index());
+    let reachable = traversal::reachable_as_bitset(body);
+    let num_blocks = body.basic_blocks().len();
+    if num_blocks == reachable.count() {
+        return;
     }
 
     let basic_blocks = body.basic_blocks_mut();
-
-    let num_blocks = basic_blocks.len();
     let mut replacements: Vec<_> = (0..num_blocks).map(BasicBlock::new).collect();
     let mut used_blocks = 0;
-    for alive_index in seen.iter() {
+    for alive_index in reachable.iter() {
+        let alive_index = alive_index.index();
         replacements[alive_index] = BasicBlock::new(used_blocks);
         if alive_index != used_blocks {
             // Swap the next alive block data with the current available slot. Since