Skip to content

Commit 4e3339e

Browse files
committed
move NllLivenessMap and LocalWithRegion to liveness_map
1 parent 67685de commit 4e3339e

File tree

7 files changed

+93
-71
lines changed

7 files changed

+93
-71
lines changed

src/librustc/mir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2933,4 +2933,4 @@ impl<'tcx> TypeFoldable<'tcx> for Literal<'tcx> {
29332933
}
29342934
}
29352935

2936-
newtype_index!(LocalWithRegion);
2936+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use rustc_data_structures::indexed_vec::IndexVec;
12+
use rustc::mir::{Mir, Local};
13+
use util::liveness::LiveVariableMap;
14+
use rustc_data_structures::indexed_vec::Idx;
15+
use rustc::ty::TypeFoldable;
16+
17+
crate struct NllLivenessMap {
18+
pub from_local: IndexVec<Local, Option<LocalWithRegion>>,
19+
pub to_local: IndexVec<LocalWithRegion, Local>,
20+
21+
}
22+
23+
impl LiveVariableMap for NllLivenessMap {
24+
type LiveVar = LocalWithRegion;
25+
26+
fn from_local(&self, local: Local) -> Option<Self::LiveVar> {
27+
self.from_local[local]
28+
}
29+
30+
fn from_live_var(&self, local: Self::LiveVar) -> Local {
31+
self.to_local[local]
32+
}
33+
34+
fn num_variables(&self) -> usize {
35+
self.to_local.len()
36+
}
37+
}
38+
39+
impl NllLivenessMap {
40+
pub fn compute(mir: &Mir) -> Self {
41+
let mut to_local = IndexVec::default();
42+
let from_local: IndexVec<Local,Option<_>> = mir
43+
.local_decls
44+
.iter_enumerated()
45+
.map(|(local, local_decl)| {
46+
if local_decl.ty.has_free_regions() {
47+
Some(to_local.push(local))
48+
}
49+
else {
50+
None
51+
}
52+
}).collect();
53+
54+
Self { from_local, to_local }
55+
}
56+
}
57+
58+
newtype_index!(LocalWithRegion);

src/librustc_mir/borrow_check/nll/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ use borrow_check::location::{LocationIndex, LocationTable};
1313
use borrow_check::nll::facts::AllFactsExt;
1414
use borrow_check::nll::type_check::MirTypeckRegionConstraints;
1515
use borrow_check::nll::region_infer::values::RegionValueElements;
16+
use borrow_check::nll::liveness_map::{NllLivenessMap, LocalWithRegion};
1617
use dataflow::indexes::BorrowIndex;
1718
use dataflow::move_paths::MoveData;
1819
use dataflow::FlowAtLocation;
1920
use dataflow::MaybeInitializedPlaces;
2021
use rustc::hir::def_id::DefId;
2122
use rustc::infer::InferCtxt;
22-
use rustc::mir::{ClosureOutlivesSubject, ClosureRegionRequirements, Mir, LocalWithRegion};
23+
use rustc::mir::{ClosureOutlivesSubject, ClosureRegionRequirements, Mir};
2324
use rustc::ty::{self, RegionKind, RegionVid};
2425
use rustc::util::nodemap::FxHashMap;
2526
use std::collections::BTreeSet;
@@ -30,7 +31,7 @@ use std::path::PathBuf;
3031
use std::rc::Rc;
3132
use std::str::FromStr;
3233
use transform::MirSource;
33-
use util::liveness::{LivenessResults, LocalSet, NllLivenessMap};
34+
use util::liveness::{LivenessResults, LiveVarSet};
3435

3536
use self::mir_util::PassWhere;
3637
use polonius_engine::{Algorithm, Output};
@@ -45,6 +46,7 @@ crate mod region_infer;
4546
mod renumber;
4647
crate mod type_check;
4748
mod universal_regions;
49+
crate mod liveness_map;
4850

4951
mod constraints;
5052

@@ -409,8 +411,8 @@ impl ToRegionVid for RegionVid {
409411
}
410412

411413
fn live_variable_set(
412-
regular: &LocalSet<LocalWithRegion>,
413-
drops: &LocalSet<LocalWithRegion>
414+
regular: &LiveVarSet<LocalWithRegion>,
415+
drops: &LiveVarSet<LocalWithRegion>
414416
) -> String {
415417
// sort and deduplicate:
416418
let all_locals: BTreeSet<_> = regular.iter().chain(drops.iter()).collect();

src/librustc_mir/borrow_check/nll/type_check/liveness.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use borrow_check::nll::{NllLivenessMap, LocalWithRegion};
1112
use borrow_check::nll::type_check::AtLocation;
1213
use dataflow::move_paths::{HasMoveData, MoveData};
1314
use dataflow::MaybeInitializedPlaces;
1415
use dataflow::{FlowAtLocation, FlowsAtLocation};
1516
use rustc::infer::canonical::QueryRegionConstraint;
16-
use rustc::mir::LocalWithRegion;
1717
use rustc::mir::{BasicBlock, Location, Mir};
1818
use rustc::traits::query::dropck_outlives::DropckOutlivesResult;
1919
use rustc::traits::query::type_op::outlives::DropckOutlives;
2020
use rustc::traits::query::type_op::TypeOp;
2121
use rustc::ty::{Ty, TypeFoldable};
2222
use rustc_data_structures::fx::FxHashMap;
2323
use std::rc::Rc;
24-
use util::liveness::{NllLivenessMap, LivenessResults, LiveVariableMap };
24+
use util::liveness::{LivenessResults, LiveVariableMap };
2525

2626
use super::TypeChecker;
2727

src/librustc_mir/borrow_check/nll/type_check/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use borrow_check::nll::region_infer::{ClosureRegionRequirementsExt, TypeTest};
1919
use borrow_check::nll::region_infer::values::{RegionValues, RegionValueElements};
2020
use borrow_check::nll::universal_regions::UniversalRegions;
2121
use borrow_check::nll::ToRegionVid;
22+
use borrow_check::nll::LocalWithRegion;
2223
use dataflow::move_paths::MoveData;
2324
use dataflow::FlowAtLocation;
2425
use dataflow::MaybeInitializedPlaces;

src/librustc_mir/transform/generator.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ struct SuspensionPoint {
130130
state: u32,
131131
resume: BasicBlock,
132132
drop: Option<BasicBlock>,
133-
storage_liveness: liveness::LocalSet<Local>,
133+
storage_liveness: liveness::LiveVarSet<Local>,
134134
}
135135

136136
struct TransformVisitor<'a, 'tcx: 'a> {
@@ -145,7 +145,7 @@ struct TransformVisitor<'a, 'tcx: 'a> {
145145
remap: HashMap<Local, (Ty<'tcx>, usize)>,
146146

147147
// A map from a suspension point in a block to the locals which have live storage at that point
148-
storage_liveness: HashMap<BasicBlock, liveness::LocalSet<Local>>,
148+
storage_liveness: HashMap<BasicBlock, liveness::LiveVarSet<Local>>,
149149

150150
// A list of suspension points, generated during the transform
151151
suspension_points: Vec<SuspensionPoint>,
@@ -317,7 +317,7 @@ fn replace_result_variable<'tcx>(ret_ty: Ty<'tcx>,
317317
new_ret_local
318318
}
319319

320-
struct StorageIgnored(liveness::LocalSet<Local>);
320+
struct StorageIgnored(liveness::LiveVarSet<Local>);
321321

322322
impl<'tcx> Visitor<'tcx> for StorageIgnored {
323323
fn visit_statement(&mut self,
@@ -332,7 +332,7 @@ impl<'tcx> Visitor<'tcx> for StorageIgnored {
332332
}
333333
}
334334

335-
struct BorrowedLocals(liveness::LocalSet<Local>);
335+
struct BorrowedLocals(liveness::LiveVarSet<Local>);
336336

337337
fn mark_as_borrowed<'tcx>(place: &Place<'tcx>, locals: &mut BorrowedLocals) {
338338
match *place {
@@ -365,8 +365,8 @@ fn locals_live_across_suspend_points<'a, 'tcx,>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
365365
mir: &Mir<'tcx>,
366366
source: MirSource,
367367
movable: bool) ->
368-
(liveness::LocalSet<Local>,
369-
HashMap<BasicBlock, liveness::LocalSet<Local>>) {
368+
(liveness::LiveVarSet<Local>,
369+
HashMap<BasicBlock, liveness::LiveVarSet<Local>>) {
370370
let dead_unwinds = IdxSetBuf::new_empty(mir.basic_blocks().len());
371371
let node_id = tcx.hir.as_local_node_id(source.def_id).unwrap();
372372

@@ -396,7 +396,7 @@ fn locals_live_across_suspend_points<'a, 'tcx,>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
396396
};
397397

398398
// Calculate the liveness of MIR locals ignoring borrows.
399-
let mut set = liveness::LocalSet::new_empty(mir.local_decls.len());
399+
let mut set = liveness::LiveVarSet::new_empty(mir.local_decls.len());
400400
let mut liveness = liveness::liveness_of_locals(
401401
mir,
402402
LivenessMode {
@@ -479,7 +479,7 @@ fn compute_layout<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
479479
mir: &mut Mir<'tcx>)
480480
-> (HashMap<Local, (Ty<'tcx>, usize)>,
481481
GeneratorLayout<'tcx>,
482-
HashMap<BasicBlock, liveness::LocalSet<Local>>)
482+
HashMap<BasicBlock, liveness::LiveVarSet<Local>>)
483483
{
484484
// Use a liveness analysis to compute locals which are live across a suspension point
485485
let (live_locals, storage_liveness) = locals_live_across_suspend_points(tcx,

src/librustc_mir/util/liveness.rs

Lines changed: 17 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use rustc::mir::visit::MirVisitable;
3737
use rustc::mir::visit::{PlaceContext, Visitor};
3838
use rustc::mir::Local;
3939
use rustc::mir::*;
40-
use rustc::ty::{item_path, TyCtxt, TypeFoldable};
40+
use rustc::ty::{item_path, TyCtxt};
4141
use rustc_data_structures::indexed_set::IdxSetBuf;
4242
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
4343
use rustc_data_structures::work_queue::WorkQueue;
@@ -47,7 +47,7 @@ use std::path::{Path, PathBuf};
4747
use transform::MirSource;
4848
use util::pretty::{dump_enabled, write_basic_block, write_mir_intro};
4949

50-
pub type LocalSet<V> = IdxSetBuf<V>;
50+
pub type LiveVarSet<V> = IdxSetBuf<V>;
5151

5252
/// This gives the result of the liveness analysis at the boundary of
5353
/// basic blocks. You can use `simulate_block` to obtain the
@@ -63,7 +63,7 @@ pub struct LivenessResult<V: Idx> {
6363

6464
/// Live variables on exit to each basic block. This is equal to
6565
/// the union of the `ins` for each successor.
66-
pub outs: IndexVec<BasicBlock, LocalSet<V>>,
66+
pub outs: IndexVec<BasicBlock, LiveVarSet<V>>,
6767
}
6868

6969
/// Defines the mapping to/from the MIR local variables (`Local`) to
@@ -176,13 +176,13 @@ pub fn liveness_of_locals<'tcx, V: Idx>(
176176
.map(|b| block(mode, map, b, num_live_vars))
177177
.collect();
178178

179-
let mut outs: IndexVec<_, LocalSet<V>> = mir
179+
let mut outs: IndexVec<_, LiveVarSet<V>> = mir
180180
.basic_blocks()
181181
.indices()
182-
.map(|_| LocalSet::new_empty(num_live_vars))
182+
.map(|_| LiveVarSet::new_empty(num_live_vars))
183183
.collect();
184184

185-
let mut bits = LocalSet::new_empty(num_live_vars);
185+
let mut bits = LiveVarSet::new_empty(num_live_vars);
186186

187187
// queue of things that need to be re-processed, and a set containing
188188
// the things currently in the queue
@@ -223,7 +223,7 @@ impl<V: Idx> LivenessResult<V> {
223223
map: &impl LiveVariableMap<LiveVar = V>,
224224
mut callback: OP,
225225
) where
226-
OP: FnMut(Location, &LocalSet<V>),
226+
OP: FnMut(Location, &LiveVarSet<V>),
227227
{
228228
let data = &mir[block];
229229

@@ -244,8 +244,8 @@ impl<V: Idx> LivenessResult<V> {
244244
mode: self.mode,
245245
map,
246246
defs_uses: DefsUses {
247-
defs: LocalSet::new_empty(num_live_vars),
248-
uses: LocalSet::new_empty(num_live_vars),
247+
defs: LiveVarSet::new_empty(num_live_vars),
248+
uses: LiveVarSet::new_empty(num_live_vars),
249249
},
250250
};
251251
// Visit the various parts of the basic block in reverse. If we go
@@ -362,8 +362,8 @@ where
362362

363363
#[derive(Eq, PartialEq, Clone)]
364364
struct DefsUses<V: Idx> {
365-
defs: LocalSet<V>,
366-
uses: LocalSet<V>,
365+
defs: LiveVarSet<V>,
366+
uses: LiveVarSet<V>,
367367
}
368368

369369
impl<V: Idx> DefsUses<V> {
@@ -372,7 +372,7 @@ impl<V: Idx> DefsUses<V> {
372372
self.defs.clear();
373373
}
374374

375-
fn apply(&self, bits: &mut LocalSet<V>) -> bool {
375+
fn apply(&self, bits: &mut LiveVarSet<V>) -> bool {
376376
bits.subtract(&self.defs) | bits.union(&self.uses)
377377
}
378378

@@ -418,10 +418,10 @@ where
418418
&mut self,
419419
location: Location,
420420
value: &impl MirVisitable<'tcx>,
421-
bits: &mut LocalSet<V>,
421+
bits: &mut LiveVarSet<V>,
422422
callback: &mut OP,
423423
) where
424-
OP: FnMut(Location, &LocalSet<V>),
424+
OP: FnMut(Location, &LiveVarSet<V>),
425425
{
426426
value.apply(location, self);
427427
self.defs_uses.apply(bits);
@@ -455,8 +455,8 @@ fn block<'tcx, V: Idx>(
455455
mode,
456456
map,
457457
defs_uses: DefsUses {
458-
defs: LocalSet::new_empty(locals),
459-
uses: LocalSet::new_empty(locals),
458+
defs: LiveVarSet::new_empty(locals),
459+
uses: LiveVarSet::new_empty(locals),
460460
},
461461
};
462462

@@ -527,7 +527,7 @@ pub fn write_mir_fn<'a, 'tcx, V: Idx>(
527527
) -> io::Result<()> {
528528
write_mir_intro(tcx, src, mir, w)?;
529529
for block in mir.basic_blocks().indices() {
530-
let print = |w: &mut dyn Write, prefix, result: &IndexVec<BasicBlock, LocalSet<V>>| {
530+
let print = |w: &mut dyn Write, prefix, result: &IndexVec<BasicBlock, LiveVarSet<V>>| {
531531
let live: Vec<String> = result[block].iter()
532532
.map(|v| map.from_live_var(v))
533533
.map(|local| format!("{:?}", local))
@@ -545,43 +545,4 @@ pub fn write_mir_fn<'a, 'tcx, V: Idx>(
545545
Ok(())
546546
}
547547

548-
crate struct NllLivenessMap {
549-
pub from_local: IndexVec<Local, Option<LocalWithRegion>>,
550-
pub to_local: IndexVec<LocalWithRegion, Local>,
551548

552-
}
553-
554-
impl LiveVariableMap for NllLivenessMap {
555-
type LiveVar = LocalWithRegion;
556-
557-
fn from_local(&self, local: Local) -> Option<Self::LiveVar> {
558-
self.from_local[local]
559-
}
560-
561-
fn from_live_var(&self, local: Self::LiveVar) -> Local {
562-
self.to_local[local]
563-
}
564-
565-
fn num_variables(&self) -> usize {
566-
self.to_local.len()
567-
}
568-
}
569-
570-
impl NllLivenessMap {
571-
pub fn compute(mir: &Mir) -> Self {
572-
let mut to_local = IndexVec::default();
573-
let from_local: IndexVec<Local,Option<_>> = mir
574-
.local_decls
575-
.iter_enumerated()
576-
.map(|(local, local_decl)| {
577-
if local_decl.ty.has_free_regions() {
578-
Some(to_local.push(local))
579-
}
580-
else {
581-
None
582-
}
583-
}).collect();
584-
585-
Self { from_local, to_local }
586-
}
587-
}

0 commit comments

Comments
 (0)