Skip to content

Commit ae5f722

Browse files
committed
nll: remove IdentityMap and LiveVariableMap
With `NllLivenessMap` and `LiveVar` removed, the `IdentityMap` (remaining structure implementing the `LiveVariableMap` trait) loses its meaning. Specialize the `LiveVarSet` to a `BitSet<Local>` removing the `V` and related parameters. The `LiveVarSet<V>` was only being used as `LiveVarSet<Local>` so this commit doesn't bring any change to the logic, it just removes an unused parameter (that without `LiveVar` now, it couldn't have been specialized to anything but `Local`).
1 parent d6ede91 commit ae5f722

File tree

2 files changed

+41
-97
lines changed

2 files changed

+41
-97
lines changed

src/librustc_mir/transform/generator.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ use crate::transform::no_landing_pads::no_landing_pads;
6868
use crate::dataflow::{do_dataflow, DebugFormatted, state_for_location};
6969
use crate::dataflow::{MaybeStorageLive, HaveBeenBorrowedLocals};
7070
use crate::util::dump_mir;
71-
use crate::util::liveness::{self, IdentityMap};
71+
use crate::util::liveness;
7272

7373
pub struct StateTransform;
7474

@@ -148,7 +148,7 @@ struct SuspensionPoint {
148148
state: u32,
149149
resume: BasicBlock,
150150
drop: Option<BasicBlock>,
151-
storage_liveness: liveness::LiveVarSet<Local>,
151+
storage_liveness: liveness::LiveVarSet,
152152
}
153153

154154
struct TransformVisitor<'a, 'tcx: 'a> {
@@ -165,7 +165,7 @@ struct TransformVisitor<'a, 'tcx: 'a> {
165165

166166
// A map from a suspension point in a block to the locals which have live storage at that point
167167
// FIXME(eddyb) This should use `IndexVec<BasicBlock, Option<_>>`.
168-
storage_liveness: FxHashMap<BasicBlock, liveness::LiveVarSet<Local>>,
168+
storage_liveness: FxHashMap<BasicBlock, liveness::LiveVarSet>,
169169

170170
// A list of suspension points, generated during the transform
171171
suspension_points: Vec<SuspensionPoint>,
@@ -358,7 +358,7 @@ fn replace_result_variable<'tcx>(
358358
new_ret_local
359359
}
360360

361-
struct StorageIgnored(liveness::LiveVarSet<Local>);
361+
struct StorageIgnored(liveness::LiveVarSet);
362362

363363
impl<'tcx> Visitor<'tcx> for StorageIgnored {
364364
fn visit_statement(&mut self,
@@ -379,8 +379,8 @@ fn locals_live_across_suspend_points(
379379
source: MirSource<'tcx>,
380380
movable: bool,
381381
) -> (
382-
liveness::LiveVarSet<Local>,
383-
FxHashMap<BasicBlock, liveness::LiveVarSet<Local>>,
382+
liveness::LiveVarSet,
383+
FxHashMap<BasicBlock, liveness::LiveVarSet>,
384384
) {
385385
let dead_unwinds = BitSet::new_empty(mir.basic_blocks().len());
386386
let node_id = tcx.hir().as_local_node_id(source.def_id()).unwrap();
@@ -414,14 +414,12 @@ fn locals_live_across_suspend_points(
414414
let mut set = liveness::LiveVarSet::new_empty(mir.local_decls.len());
415415
let mut liveness = liveness::liveness_of_locals(
416416
mir,
417-
&IdentityMap::new(mir),
418417
);
419418
liveness::dump_mir(
420419
tcx,
421420
"generator_liveness",
422421
source,
423422
mir,
424-
&IdentityMap::new(mir),
425423
&liveness,
426424
);
427425

@@ -491,7 +489,7 @@ fn compute_layout<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
491489
mir: &mut Mir<'tcx>)
492490
-> (FxHashMap<Local, (Ty<'tcx>, usize)>,
493491
GeneratorLayout<'tcx>,
494-
FxHashMap<BasicBlock, liveness::LiveVarSet<Local>>)
492+
FxHashMap<BasicBlock, liveness::LiveVarSet>)
495493
{
496494
// Use a liveness analysis to compute locals which are live across a suspension point
497495
let (live_locals, storage_liveness) = locals_live_across_suspend_points(tcx,

src/librustc_mir/util/liveness.rs

+34-88
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use std::path::{Path, PathBuf};
3939
use crate::transform::MirSource;
4040
use crate::util::pretty::{dump_enabled, write_basic_block, write_mir_intro};
4141

42-
pub type LiveVarSet<V> = BitSet<V>;
42+
pub type LiveVarSet = BitSet<Local>;
4343

4444
/// This gives the result of the liveness analysis at the boundary of
4545
/// basic blocks.
@@ -48,66 +48,27 @@ pub type LiveVarSet<V> = BitSet<V>;
4848
/// liveness for. This is often `Local`, in which case we computed
4949
/// liveness for all variables -- but it can also be some other type,
5050
/// which indicates a subset of the variables within the graph.
51-
pub struct LivenessResult<V: Idx> {
51+
pub struct LivenessResult {
5252
/// Live variables on exit to each basic block. This is equal to
5353
/// the union of the `ins` for each successor.
54-
pub outs: IndexVec<BasicBlock, LiveVarSet<V>>,
55-
}
56-
57-
/// Defines the mapping to/from the MIR local variables (`Local`) to
58-
/// the "live variable indices" we are using in a particular
59-
/// computation.
60-
pub trait LiveVariableMap {
61-
type LiveVar;
62-
63-
fn from_local(&self, local: Local) -> Option<Self::LiveVar>;
64-
fn from_live_var(&self, local: Self::LiveVar) -> Local;
65-
fn num_variables(&self) -> usize;
66-
}
67-
68-
#[derive(Debug)]
69-
pub struct IdentityMap<'a, 'tcx: 'a> {
70-
mir: &'a Mir<'tcx>,
71-
}
72-
73-
impl<'a, 'tcx> IdentityMap<'a, 'tcx> {
74-
pub fn new(mir: &'a Mir<'tcx>) -> Self {
75-
Self { mir }
76-
}
77-
}
78-
79-
impl<'a, 'tcx> LiveVariableMap for IdentityMap<'a, 'tcx> {
80-
type LiveVar = Local;
81-
82-
fn from_local(&self, local: Local) -> Option<Self::LiveVar> {
83-
Some(local)
84-
}
85-
86-
fn from_live_var(&self, local: Self::LiveVar) -> Local {
87-
local
88-
}
89-
90-
fn num_variables(&self) -> usize {
91-
self.mir.local_decls.len()
92-
}
54+
pub outs: IndexVec<BasicBlock, LiveVarSet>,
9355
}
9456

9557
/// Computes which local variables are live within the given function
9658
/// `mir`. The liveness mode `mode` determines what sorts of uses are
9759
/// considered to make a variable live (e.g., do drops count?).
98-
pub fn liveness_of_locals<'tcx, V: Idx>(
60+
pub fn liveness_of_locals<'tcx>(
9961
mir: &Mir<'tcx>,
100-
map: &impl LiveVariableMap<LiveVar = V>,
101-
) -> LivenessResult<V> {
102-
let num_live_vars = map.num_variables();
62+
) -> LivenessResult {
63+
let num_live_vars = mir.local_decls.len();
10364

104-
let def_use: IndexVec<_, DefsUses<V>> = mir
65+
let def_use: IndexVec<_, DefsUses> = mir
10566
.basic_blocks()
10667
.iter()
107-
.map(|b| block(map, b, num_live_vars))
68+
.map(|b| block(b, num_live_vars))
10869
.collect();
10970

110-
let mut outs: IndexVec<_, LiveVarSet<V>> = mir
71+
let mut outs: IndexVec<_, LiveVarSet> = mir
11172
.basic_blocks()
11273
.indices()
11374
.map(|_| LiveVarSet::new_empty(num_live_vars))
@@ -211,27 +172,23 @@ pub fn categorize<'tcx>(context: PlaceContext<'tcx>) -> Option<DefUse> {
211172
}
212173
}
213174

214-
struct DefsUsesVisitor<'lv, V, M>
215-
where
216-
V: Idx,
217-
M: LiveVariableMap<LiveVar = V> + 'lv,
175+
struct DefsUsesVisitor
218176
{
219-
map: &'lv M,
220-
defs_uses: DefsUses<V>,
177+
defs_uses: DefsUses,
221178
}
222179

223180
#[derive(Eq, PartialEq, Clone)]
224-
struct DefsUses<V: Idx> {
225-
defs: LiveVarSet<V>,
226-
uses: LiveVarSet<V>,
181+
struct DefsUses {
182+
defs: LiveVarSet,
183+
uses: LiveVarSet,
227184
}
228185

229-
impl<V: Idx> DefsUses<V> {
230-
fn apply(&self, bits: &mut LiveVarSet<V>) -> bool {
186+
impl DefsUses {
187+
fn apply(&self, bits: &mut LiveVarSet) -> bool {
231188
bits.subtract(&self.defs) | bits.union(&self.uses)
232189
}
233190

234-
fn add_def(&mut self, index: V) {
191+
fn add_def(&mut self, index: Local) {
235192
// If it was used already in the block, remove that use
236193
// now that we found a definition.
237194
//
@@ -245,7 +202,7 @@ impl<V: Idx> DefsUses<V> {
245202
self.defs.insert(index);
246203
}
247204

248-
fn add_use(&mut self, index: V) {
205+
fn add_use(&mut self, index: Local) {
249206
// Inverse of above.
250207
//
251208
// Example:
@@ -261,29 +218,22 @@ impl<V: Idx> DefsUses<V> {
261218
}
262219
}
263220

264-
impl<'tcx, 'lv, V, M> Visitor<'tcx> for DefsUsesVisitor<'lv, V, M>
265-
where
266-
V: Idx,
267-
M: LiveVariableMap<LiveVar = V>,
221+
impl<'tcx> Visitor<'tcx> for DefsUsesVisitor
268222
{
269223
fn visit_local(&mut self, &local: &Local, context: PlaceContext<'tcx>, _: Location) {
270-
if let Some(v_index) = self.map.from_local(local) {
271-
match categorize(context) {
272-
Some(DefUse::Def) => self.defs_uses.add_def(v_index),
273-
Some(DefUse::Use) | Some(DefUse::Drop) => self.defs_uses.add_use(v_index),
274-
_ => (),
275-
}
224+
match categorize(context) {
225+
Some(DefUse::Def) => self.defs_uses.add_def(local),
226+
Some(DefUse::Use) | Some(DefUse::Drop) => self.defs_uses.add_use(local),
227+
_ => (),
276228
}
277229
}
278230
}
279231

280-
fn block<'tcx, V: Idx>(
281-
map: &impl LiveVariableMap<LiveVar = V>,
232+
fn block<'tcx>(
282233
b: &BasicBlockData<'tcx>,
283234
locals: usize,
284-
) -> DefsUses<V> {
235+
) -> DefsUses {
285236
let mut visitor = DefsUsesVisitor {
286-
map,
287237
defs_uses: DefsUses {
288238
defs: LiveVarSet::new_empty(locals),
289239
uses: LiveVarSet::new_empty(locals),
@@ -305,13 +255,12 @@ fn block<'tcx, V: Idx>(
305255
visitor.defs_uses
306256
}
307257

308-
pub fn dump_mir<'a, 'tcx, V: Idx>(
258+
pub fn dump_mir<'a, 'tcx>(
309259
tcx: TyCtxt<'a, 'tcx, 'tcx>,
310260
pass_name: &str,
311261
source: MirSource<'tcx>,
312262
mir: &Mir<'tcx>,
313-
map: &impl LiveVariableMap<LiveVar = V>,
314-
result: &LivenessResult<V>,
263+
result: &LivenessResult,
315264
) {
316265
if !dump_enabled(tcx, pass_name, source) {
317266
return;
@@ -320,17 +269,16 @@ pub fn dump_mir<'a, 'tcx, V: Idx>(
320269
// see notes on #41697 below
321270
tcx.item_path_str(source.def_id())
322271
});
323-
dump_matched_mir_node(tcx, pass_name, &node_path, source, mir, map, result);
272+
dump_matched_mir_node(tcx, pass_name, &node_path, source, mir, result);
324273
}
325274

326-
fn dump_matched_mir_node<'a, 'tcx, V: Idx>(
275+
fn dump_matched_mir_node<'a, 'tcx>(
327276
tcx: TyCtxt<'a, 'tcx, 'tcx>,
328277
pass_name: &str,
329278
node_path: &str,
330279
source: MirSource<'tcx>,
331280
mir: &Mir<'tcx>,
332-
map: &dyn LiveVariableMap<LiveVar = V>,
333-
result: &LivenessResult<V>,
281+
result: &LivenessResult,
334282
) {
335283
let mut file_path = PathBuf::new();
336284
file_path.push(Path::new(&tcx.sess.opts.debugging_opts.dump_mir_dir));
@@ -342,25 +290,23 @@ fn dump_matched_mir_node<'a, 'tcx, V: Idx>(
342290
writeln!(file, "// source = {:?}", source)?;
343291
writeln!(file, "// pass_name = {}", pass_name)?;
344292
writeln!(file, "")?;
345-
write_mir_fn(tcx, source, mir, map, &mut file, result)?;
293+
write_mir_fn(tcx, source, mir, &mut file, result)?;
346294
Ok(())
347295
});
348296
}
349297

350-
pub fn write_mir_fn<'a, 'tcx, V: Idx>(
298+
pub fn write_mir_fn<'a, 'tcx>(
351299
tcx: TyCtxt<'a, 'tcx, 'tcx>,
352300
src: MirSource<'tcx>,
353301
mir: &Mir<'tcx>,
354-
map: &dyn LiveVariableMap<LiveVar = V>,
355302
w: &mut dyn Write,
356-
result: &LivenessResult<V>,
303+
result: &LivenessResult,
357304
) -> io::Result<()> {
358305
write_mir_intro(tcx, src, mir, w)?;
359306
for block in mir.basic_blocks().indices() {
360-
let print = |w: &mut dyn Write, prefix, result: &IndexVec<BasicBlock, LiveVarSet<V>>| {
307+
let print = |w: &mut dyn Write, prefix, result: &IndexVec<BasicBlock, LiveVarSet>| {
361308
let live: Vec<String> = result[block]
362309
.iter()
363-
.map(|v| map.from_live_var(v))
364310
.map(|local| format!("{:?}", local))
365311
.collect();
366312
writeln!(w, "{} {{{}}}", prefix, live.join(", "))

0 commit comments

Comments
 (0)