X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Flibrustc_mir%2Futil%2Fliveness.rs;h=b12ad1e4c15cca55f8bee1d39e1690a8de49ebc9;hb=5d1433b1f401d6b2a43a5728924c3725014b8cdc;hp=01eebeb8c55a55a68ed4045b34d1c057743cbcdb;hpb=86282d0b0f7a4a7083579ade68a9cd69410531c5;p=rust.git diff --git a/src/librustc_mir/util/liveness.rs b/src/librustc_mir/util/liveness.rs index 01eebeb8c55..b12ad1e4c15 100644 --- a/src/librustc_mir/util/liveness.rs +++ b/src/librustc_mir/util/liveness.rs @@ -24,20 +24,20 @@ //! generator yield points, all pre-existing references are invalidated, so this //! doesn't matter). +use crate::transform::MirSource; +use crate::util::pretty::{dump_enabled, write_basic_block, write_mir_intro}; use rustc::mir::visit::{ - PlaceContext, Visitor, MutatingUseContext, NonMutatingUseContext, NonUseContext, + MutatingUseContext, NonMutatingUseContext, NonUseContext, PlaceContext, Visitor, }; use rustc::mir::Local; use rustc::mir::*; use rustc::ty::{self, TyCtxt}; +use rustc_data_structures::work_queue::WorkQueue; use rustc_index::bit_set::BitSet; use rustc_index::vec::{Idx, IndexVec}; -use rustc_data_structures::work_queue::WorkQueue; use std::fs; -use std::io::{self, Write}; +use std::io::{self, BufWriter, Write}; use std::path::{Path, PathBuf}; -use crate::transform::MirSource; -use crate::util::pretty::{dump_enabled, write_basic_block, write_mir_intro}; pub type LiveVarSet = BitSet; @@ -56,22 +56,14 @@ pub struct LivenessResult { /// Computes which local variables are live within the given function /// `mir`, including drops. -pub fn liveness_of_locals( - body: ReadOnlyBodyAndCache<'_, '_>, -) -> LivenessResult { +pub fn liveness_of_locals(body: ReadOnlyBodyAndCache<'_, '_>) -> LivenessResult { let num_live_vars = body.local_decls.len(); - let def_use: IndexVec<_, DefsUses> = body - .basic_blocks() - .iter() - .map(|b| block(b, num_live_vars)) - .collect(); + let def_use: IndexVec<_, DefsUses> = + body.basic_blocks().iter().map(|b| block(b, num_live_vars)).collect(); - let mut outs: IndexVec<_, LiveVarSet> = body - .basic_blocks() - .indices() - .map(|_| LiveVarSet::new_empty(num_live_vars)) - .collect(); + let mut outs: IndexVec<_, LiveVarSet> = + body.basic_blocks().indices().map(|_| LiveVarSet::new_empty(num_live_vars)).collect(); let mut bits = LiveVarSet::new_empty(num_live_vars); @@ -83,8 +75,7 @@ pub fn liveness_of_locals( // FIXME(ecstaticmorse): Reverse post-order on the reverse CFG may generate a better iteration // order when cycles are present, but the overhead of computing the reverse CFG may outweigh // any benefits. Benchmark this and find out. - let mut dirty_queue: WorkQueue - = WorkQueue::with_none(body.basic_blocks().len()); + let mut dirty_queue: WorkQueue = WorkQueue::with_none(body.basic_blocks().len()); for (bb, _) in traversal::postorder(&body) { dirty_queue.insert(bb); } @@ -192,8 +183,7 @@ pub fn categorize(context: PlaceContext) -> Option { } } -struct DefsUsesVisitor -{ +struct DefsUsesVisitor { defs_uses: DefsUses, } @@ -238,8 +228,7 @@ fn add_use(&mut self, index: Local) { } } -impl<'tcx> Visitor<'tcx> for DefsUsesVisitor -{ +impl<'tcx> Visitor<'tcx> for DefsUsesVisitor { fn visit_local(&mut self, &local: &Local, context: PlaceContext, _: Location) { match categorize(context) { Some(DefUse::Def) => self.defs_uses.add_def(local), @@ -249,10 +238,7 @@ fn visit_local(&mut self, &local: &Local, context: PlaceContext, _: Location) { } } -fn block( - b: &BasicBlockData<'_>, - locals: usize, -) -> DefsUses { +fn block(b: &BasicBlockData<'_>, locals: usize) -> DefsUses { let mut visitor = DefsUsesVisitor { defs_uses: DefsUses { defs: LiveVarSet::new_empty(locals), @@ -260,10 +246,7 @@ fn block( }, }; - let dummy_location = Location { - block: BasicBlock::new(0), - statement_index: 0, - }; + let dummy_location = Location { block: BasicBlock::new(0), statement_index: 0 }; // Visit the various parts of the basic block in reverse. If we go // forward, the logic in `add_def` and `add_use` would be wrong. @@ -305,7 +288,8 @@ fn dump_matched_mir_node<'tcx>( let item_id = tcx.hir().as_local_hir_id(source.def_id()).unwrap(); let file_name = format!("rustc.node{}{}-liveness.mir", item_id, pass_name); file_path.push(&file_name); - let _ = fs::File::create(&file_path).and_then(|mut file| { + let _ = fs::File::create(&file_path).and_then(|file| { + let mut file = BufWriter::new(file); writeln!(file, "// MIR local liveness analysis for `{}`", node_path)?; writeln!(file, "// source = {:?}", source)?; writeln!(file, "// pass_name = {}", pass_name)?; @@ -325,10 +309,8 @@ pub fn write_mir_fn<'tcx>( write_mir_intro(tcx, src, body, w)?; for block in body.basic_blocks().indices() { let print = |w: &mut dyn Write, prefix, result: &IndexVec| { - let live: Vec = result[block] - .iter() - .map(|local| format!("{:?}", local)) - .collect(); + let live: Vec = + result[block].iter().map(|local| format!("{:?}", local)).collect(); writeln!(w, "{} {{{}}}", prefix, live.join(", ")) }; write_basic_block(tcx, block, body, &mut |_, _| Ok(()), w)?;