Skip to content

Commit b19b8ea

Browse files
Update dataflow analyses to use new interface
1 parent 3233fb1 commit b19b8ea

File tree

9 files changed

+106
-143
lines changed

9 files changed

+106
-143
lines changed

compiler/rustc_mir/src/dataflow/impls/borrowed_locals.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,15 @@ impl<K> AnalysisDomain<'tcx> for MaybeBorrowedLocals<K>
8282
where
8383
K: BorrowAnalysisKind<'tcx>,
8484
{
85-
type Idx = Local;
86-
85+
type Domain = BitSet<Local>;
8786
const NAME: &'static str = K::ANALYSIS_NAME;
8887

89-
fn bits_per_block(&self, body: &mir::Body<'tcx>) -> usize {
90-
body.local_decls().len()
88+
fn bottom_value(&self, body: &mir::Body<'tcx>) -> Self::Domain {
89+
// bottom = unborrowed
90+
BitSet::new_empty(body.local_decls().len())
9191
}
9292

93-
fn initialize_start_block(&self, _: &mir::Body<'tcx>, _: &mut BitSet<Self::Idx>) {
93+
fn initialize_start_block(&self, _: &mir::Body<'tcx>, _: &mut Self::Domain) {
9494
// No locals are aliased on function entry
9595
}
9696
}
@@ -99,6 +99,8 @@ impl<K> GenKillAnalysis<'tcx> for MaybeBorrowedLocals<K>
9999
where
100100
K: BorrowAnalysisKind<'tcx>,
101101
{
102+
type Idx = Local;
103+
102104
fn statement_effect(
103105
&self,
104106
trans: &mut impl GenKill<Self::Idx>,
@@ -128,11 +130,6 @@ where
128130
}
129131
}
130132

131-
impl<K> BottomValue for MaybeBorrowedLocals<K> {
132-
// bottom = unborrowed
133-
const BOTTOM_VALUE: bool = false;
134-
}
135-
136133
/// A `Visitor` that defines the transfer function for `MaybeBorrowedLocals`.
137134
struct TransferFunction<'a, T, K> {
138135
trans: &'a mut T,

compiler/rustc_mir/src/dataflow/impls/borrows.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use rustc_index::bit_set::BitSet;
88
use crate::borrow_check::{
99
places_conflict, BorrowSet, PlaceConflictBias, PlaceExt, RegionInferenceContext, ToRegionVid,
1010
};
11-
use crate::dataflow::BottomValue;
12-
use crate::dataflow::{self, GenKill};
11+
use crate::dataflow::{self, fmt::DebugWithContext, GenKill};
1312

13+
use std::fmt;
1414
use std::rc::Rc;
1515

1616
rustc_index::newtype_index! {
@@ -227,25 +227,24 @@ impl<'a, 'tcx> Borrows<'a, 'tcx> {
227227
}
228228

229229
impl<'tcx> dataflow::AnalysisDomain<'tcx> for Borrows<'_, 'tcx> {
230-
type Idx = BorrowIndex;
230+
type Domain = BitSet<BorrowIndex>;
231231

232232
const NAME: &'static str = "borrows";
233233

234-
fn bits_per_block(&self, _: &mir::Body<'tcx>) -> usize {
235-
self.borrow_set.len() * 2
234+
fn bottom_value(&self, _: &mir::Body<'tcx>) -> Self::Domain {
235+
// bottom = nothing is reserved or activated yet;
236+
BitSet::new_empty(self.borrow_set.len() * 2)
236237
}
237238

238-
fn initialize_start_block(&self, _: &mir::Body<'tcx>, _: &mut BitSet<Self::Idx>) {
239+
fn initialize_start_block(&self, _: &mir::Body<'tcx>, _: &mut Self::Domain) {
239240
// no borrows of code region_scopes have been taken prior to
240241
// function execution, so this method has no effect.
241242
}
242-
243-
fn pretty_print_idx(&self, w: &mut impl std::io::Write, idx: Self::Idx) -> std::io::Result<()> {
244-
write!(w, "{:?}", self.location(idx))
245-
}
246243
}
247244

248245
impl<'tcx> dataflow::GenKillAnalysis<'tcx> for Borrows<'_, 'tcx> {
246+
type Idx = BorrowIndex;
247+
249248
fn before_statement_effect(
250249
&self,
251250
trans: &mut impl GenKill<Self::Idx>,
@@ -344,7 +343,8 @@ impl<'tcx> dataflow::GenKillAnalysis<'tcx> for Borrows<'_, 'tcx> {
344343
}
345344
}
346345

347-
impl<'a, 'tcx> BottomValue for Borrows<'a, 'tcx> {
348-
/// bottom = nothing is reserved or activated yet;
349-
const BOTTOM_VALUE: bool = false;
346+
impl DebugWithContext<Borrows<'_, '_>> for BorrowIndex {
347+
fn fmt_with(&self, ctxt: &Borrows<'_, '_>, f: &mut fmt::Formatter<'_>) -> fmt::Result {
348+
write!(f, "{:?}", ctxt.location(*self))
349+
}
350350
}

compiler/rustc_mir/src/dataflow/impls/init_locals.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,25 @@
22
//!
33
//! A local will be maybe initialized if *any* projections of that local might be initialized.
44
5-
use crate::dataflow::{self, BottomValue, GenKill};
5+
use crate::dataflow::{self, GenKill};
66

77
use rustc_index::bit_set::BitSet;
88
use rustc_middle::mir::visit::{PlaceContext, Visitor};
99
use rustc_middle::mir::{self, BasicBlock, Local, Location};
1010

1111
pub struct MaybeInitializedLocals;
1212

13-
impl BottomValue for MaybeInitializedLocals {
14-
/// bottom = uninit
15-
const BOTTOM_VALUE: bool = false;
16-
}
17-
1813
impl dataflow::AnalysisDomain<'tcx> for MaybeInitializedLocals {
19-
type Idx = Local;
14+
type Domain = BitSet<Local>;
2015

2116
const NAME: &'static str = "maybe_init_locals";
2217

23-
fn bits_per_block(&self, body: &mir::Body<'tcx>) -> usize {
24-
body.local_decls.len()
18+
fn bottom_value(&self, body: &mir::Body<'tcx>) -> Self::Domain {
19+
// bottom = uninit
20+
BitSet::new_empty(body.local_decls.len())
2521
}
2622

27-
fn initialize_start_block(&self, body: &mir::Body<'tcx>, entry_set: &mut BitSet<Self::Idx>) {
23+
fn initialize_start_block(&self, body: &mir::Body<'tcx>, entry_set: &mut Self::Domain) {
2824
// Function arguments are initialized to begin with.
2925
for arg in body.args_iter() {
3026
entry_set.insert(arg);
@@ -33,6 +29,8 @@ impl dataflow::AnalysisDomain<'tcx> for MaybeInitializedLocals {
3329
}
3430

3531
impl dataflow::GenKillAnalysis<'tcx> for MaybeInitializedLocals {
32+
type Idx = Local;
33+
3634
fn statement_effect(
3735
&self,
3836
trans: &mut impl GenKill<Self::Idx>,

compiler/rustc_mir/src/dataflow/impls/liveness.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_index::bit_set::BitSet;
22
use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor};
33
use rustc_middle::mir::{self, Local, Location};
44

5-
use crate::dataflow::{AnalysisDomain, Backward, BottomValue, GenKill, GenKillAnalysis};
5+
use crate::dataflow::{AnalysisDomain, Backward, GenKill, GenKillAnalysis};
66

77
/// A [live-variable dataflow analysis][liveness].
88
///
@@ -22,27 +22,25 @@ impl MaybeLiveLocals {
2222
}
2323
}
2424

25-
impl BottomValue for MaybeLiveLocals {
26-
// bottom = not live
27-
const BOTTOM_VALUE: bool = false;
28-
}
29-
3025
impl AnalysisDomain<'tcx> for MaybeLiveLocals {
31-
type Idx = Local;
26+
type Domain = BitSet<Local>;
3227
type Direction = Backward;
3328

3429
const NAME: &'static str = "liveness";
3530

36-
fn bits_per_block(&self, body: &mir::Body<'tcx>) -> usize {
37-
body.local_decls.len()
31+
fn bottom_value(&self, body: &mir::Body<'tcx>) -> Self::Domain {
32+
// bottom = not live
33+
BitSet::new_empty(body.local_decls.len())
3834
}
3935

40-
fn initialize_start_block(&self, _: &mir::Body<'tcx>, _: &mut BitSet<Self::Idx>) {
36+
fn initialize_start_block(&self, _: &mir::Body<'tcx>, _: &mut Self::Domain) {
4137
// No variables are live until we observe a use
4238
}
4339
}
4440

4541
impl GenKillAnalysis<'tcx> for MaybeLiveLocals {
42+
type Idx = Local;
43+
4644
fn statement_effect(
4745
&self,
4846
trans: &mut impl GenKill<Self::Idx>,

compiler/rustc_mir/src/dataflow/impls/mod.rs

+32-53
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use super::MoveDataParamEnv;
1313
use crate::util::elaborate_drops::DropFlagState;
1414

1515
use super::move_paths::{HasMoveData, InitIndex, InitKind, MoveData, MovePathIndex};
16-
use super::{AnalysisDomain, BottomValue, GenKill, GenKillAnalysis};
16+
use super::{lattice, AnalysisDomain, GenKill, GenKillAnalysis};
1717

1818
use super::drop_flag_effects_for_function_entry;
1919
use super::drop_flag_effects_for_location;
@@ -290,27 +290,25 @@ impl<'a, 'tcx> DefinitelyInitializedPlaces<'a, 'tcx> {
290290
}
291291

292292
impl<'tcx> AnalysisDomain<'tcx> for MaybeInitializedPlaces<'_, 'tcx> {
293-
type Idx = MovePathIndex;
294-
293+
type Domain = BitSet<MovePathIndex>;
295294
const NAME: &'static str = "maybe_init";
296295

297-
fn bits_per_block(&self, _: &mir::Body<'tcx>) -> usize {
298-
self.move_data().move_paths.len()
296+
fn bottom_value(&self, _: &mir::Body<'tcx>) -> Self::Domain {
297+
// bottom = uninitialized
298+
BitSet::new_empty(self.move_data().move_paths.len())
299299
}
300300

301-
fn initialize_start_block(&self, _: &mir::Body<'tcx>, state: &mut BitSet<Self::Idx>) {
301+
fn initialize_start_block(&self, _: &mir::Body<'tcx>, state: &mut Self::Domain) {
302302
drop_flag_effects_for_function_entry(self.tcx, self.body, self.mdpe, |path, s| {
303303
assert!(s == DropFlagState::Present);
304304
state.insert(path);
305305
});
306306
}
307-
308-
fn pretty_print_idx(&self, w: &mut impl std::io::Write, mpi: Self::Idx) -> std::io::Result<()> {
309-
write!(w, "{}", self.move_data().move_paths[mpi])
310-
}
311307
}
312308

313309
impl<'tcx> GenKillAnalysis<'tcx> for MaybeInitializedPlaces<'_, 'tcx> {
310+
type Idx = MovePathIndex;
311+
314312
fn statement_effect(
315313
&self,
316314
trans: &mut impl GenKill<Self::Idx>,
@@ -376,32 +374,30 @@ impl<'tcx> GenKillAnalysis<'tcx> for MaybeInitializedPlaces<'_, 'tcx> {
376374
}
377375

378376
impl<'tcx> AnalysisDomain<'tcx> for MaybeUninitializedPlaces<'_, 'tcx> {
379-
type Idx = MovePathIndex;
377+
type Domain = BitSet<MovePathIndex>;
380378

381379
const NAME: &'static str = "maybe_uninit";
382380

383-
fn bits_per_block(&self, _: &mir::Body<'tcx>) -> usize {
384-
self.move_data().move_paths.len()
381+
fn bottom_value(&self, _: &mir::Body<'tcx>) -> Self::Domain {
382+
// bottom = initialized (start_block_effect counters this at outset)
383+
BitSet::new_empty(self.move_data().move_paths.len())
385384
}
386385

387386
// sets on_entry bits for Arg places
388-
fn initialize_start_block(&self, body: &mir::Body<'tcx>, state: &mut BitSet<Self::Idx>) {
387+
fn initialize_start_block(&self, _: &mir::Body<'tcx>, state: &mut Self::Domain) {
389388
// set all bits to 1 (uninit) before gathering counterevidence
390-
assert!(self.bits_per_block(body) == state.domain_size());
391389
state.insert_all();
392390

393391
drop_flag_effects_for_function_entry(self.tcx, self.body, self.mdpe, |path, s| {
394392
assert!(s == DropFlagState::Present);
395393
state.remove(path);
396394
});
397395
}
398-
399-
fn pretty_print_idx(&self, w: &mut impl std::io::Write, mpi: Self::Idx) -> std::io::Result<()> {
400-
write!(w, "{}", self.move_data().move_paths[mpi])
401-
}
402396
}
403397

404398
impl<'tcx> GenKillAnalysis<'tcx> for MaybeUninitializedPlaces<'_, 'tcx> {
399+
type Idx = MovePathIndex;
400+
405401
fn statement_effect(
406402
&self,
407403
trans: &mut impl GenKill<Self::Idx>,
@@ -471,30 +467,30 @@ impl<'tcx> GenKillAnalysis<'tcx> for MaybeUninitializedPlaces<'_, 'tcx> {
471467
}
472468

473469
impl<'a, 'tcx> AnalysisDomain<'tcx> for DefinitelyInitializedPlaces<'a, 'tcx> {
474-
type Idx = MovePathIndex;
470+
/// Use set intersection as the join operator.
471+
type Domain = lattice::Dual<BitSet<MovePathIndex>>;
475472

476473
const NAME: &'static str = "definite_init";
477474

478-
fn bits_per_block(&self, _: &mir::Body<'tcx>) -> usize {
479-
self.move_data().move_paths.len()
475+
fn bottom_value(&self, _: &mir::Body<'tcx>) -> Self::Domain {
476+
// bottom = initialized (start_block_effect counters this at outset)
477+
lattice::Dual(BitSet::new_filled(self.move_data().move_paths.len()))
480478
}
481479

482480
// sets on_entry bits for Arg places
483-
fn initialize_start_block(&self, _: &mir::Body<'tcx>, state: &mut BitSet<Self::Idx>) {
484-
state.clear();
481+
fn initialize_start_block(&self, _: &mir::Body<'tcx>, state: &mut Self::Domain) {
482+
state.0.clear();
485483

486484
drop_flag_effects_for_function_entry(self.tcx, self.body, self.mdpe, |path, s| {
487485
assert!(s == DropFlagState::Present);
488-
state.insert(path);
486+
state.0.insert(path);
489487
});
490488
}
491-
492-
fn pretty_print_idx(&self, w: &mut impl std::io::Write, mpi: Self::Idx) -> std::io::Result<()> {
493-
write!(w, "{}", self.move_data().move_paths[mpi])
494-
}
495489
}
496490

497491
impl<'tcx> GenKillAnalysis<'tcx> for DefinitelyInitializedPlaces<'_, 'tcx> {
492+
type Idx = MovePathIndex;
493+
498494
fn statement_effect(
499495
&self,
500496
trans: &mut impl GenKill<Self::Idx>,
@@ -540,22 +536,25 @@ impl<'tcx> GenKillAnalysis<'tcx> for DefinitelyInitializedPlaces<'_, 'tcx> {
540536
}
541537

542538
impl<'tcx> AnalysisDomain<'tcx> for EverInitializedPlaces<'_, 'tcx> {
543-
type Idx = InitIndex;
539+
type Domain = BitSet<InitIndex>;
544540

545541
const NAME: &'static str = "ever_init";
546542

547-
fn bits_per_block(&self, _: &mir::Body<'tcx>) -> usize {
548-
self.move_data().inits.len()
543+
fn bottom_value(&self, _: &mir::Body<'tcx>) -> Self::Domain {
544+
// bottom = no initialized variables by default
545+
BitSet::new_empty(self.move_data().inits.len())
549546
}
550547

551-
fn initialize_start_block(&self, body: &mir::Body<'tcx>, state: &mut BitSet<Self::Idx>) {
548+
fn initialize_start_block(&self, body: &mir::Body<'tcx>, state: &mut Self::Domain) {
552549
for arg_init in 0..body.arg_count {
553550
state.insert(InitIndex::new(arg_init));
554551
}
555552
}
556553
}
557554

558555
impl<'tcx> GenKillAnalysis<'tcx> for EverInitializedPlaces<'_, 'tcx> {
556+
type Idx = InitIndex;
557+
559558
fn statement_effect(
560559
&self,
561560
trans: &mut impl GenKill<Self::Idx>,
@@ -625,23 +624,3 @@ impl<'tcx> GenKillAnalysis<'tcx> for EverInitializedPlaces<'_, 'tcx> {
625624
}
626625
}
627626
}
628-
629-
impl<'a, 'tcx> BottomValue for MaybeInitializedPlaces<'a, 'tcx> {
630-
/// bottom = uninitialized
631-
const BOTTOM_VALUE: bool = false;
632-
}
633-
634-
impl<'a, 'tcx> BottomValue for MaybeUninitializedPlaces<'a, 'tcx> {
635-
/// bottom = initialized (start_block_effect counters this at outset)
636-
const BOTTOM_VALUE: bool = false;
637-
}
638-
639-
impl<'a, 'tcx> BottomValue for DefinitelyInitializedPlaces<'a, 'tcx> {
640-
/// bottom = initialized (start_block_effect counters this at outset)
641-
const BOTTOM_VALUE: bool = true;
642-
}
643-
644-
impl<'a, 'tcx> BottomValue for EverInitializedPlaces<'a, 'tcx> {
645-
/// bottom = no initialized variables by default
646-
const BOTTOM_VALUE: bool = false;
647-
}

0 commit comments

Comments
 (0)