Skip to content

Commit d4add5d

Browse files
committed
Refactoring: pull bitvector initialization out from other parts of dataflow.
This is meant to ease development of multi-stage dataflow analyses where the output from one analysis is used to initialize the state for the next; in such a context, you cannot start with `bottom_value` for all the bits.
1 parent 171c2ae commit d4add5d

File tree

4 files changed

+18
-21
lines changed

4 files changed

+18
-21
lines changed

src/librustc_mir/dataflow/impls/borrows.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_data_structures::bitslice::{BitwiseOperator};
2222
use rustc_data_structures::indexed_set::{IdxSet};
2323
use rustc_data_structures::indexed_vec::{IndexVec};
2424

25-
use dataflow::{BitDenotation, BlockSets, DataflowOperator};
25+
use dataflow::{BitDenotation, BlockSets, InitialFlow};
2626
pub use dataflow::indexes::BorrowIndex;
2727
use borrow_check::nll::region_infer::RegionInferenceContext;
2828
use borrow_check::nll::ToRegionVid;
@@ -339,7 +339,7 @@ impl<'a, 'gcx, 'tcx> BitwiseOperator for Borrows<'a, 'gcx, 'tcx> {
339339
}
340340
}
341341

342-
impl<'a, 'gcx, 'tcx> DataflowOperator for Borrows<'a, 'gcx, 'tcx> {
342+
impl<'a, 'gcx, 'tcx> InitialFlow for Borrows<'a, 'gcx, 'tcx> {
343343
#[inline]
344344
fn bottom_value() -> bool {
345345
false // bottom = no Rvalue::Refs are active by default

src/librustc_mir/dataflow/impls/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use util::elaborate_drops::DropFlagState;
2323

2424
use super::move_paths::{HasMoveData, MoveData, MoveOutIndex, MovePathIndex, InitIndex};
2525
use super::move_paths::{LookupResult, InitKind};
26-
use super::{BitDenotation, BlockSets, DataflowOperator};
26+
use super::{BitDenotation, BlockSets, InitialFlow};
2727

2828
use super::drop_flag_effects_for_function_entry;
2929
use super::drop_flag_effects_for_location;
@@ -702,35 +702,35 @@ impl<'a, 'gcx, 'tcx> BitwiseOperator for EverInitializedLvals<'a, 'gcx, 'tcx> {
702702
// propagating, or you start at all-ones and then use Intersect as
703703
// your merge when propagating.
704704

705-
impl<'a, 'gcx, 'tcx> DataflowOperator for MaybeInitializedLvals<'a, 'gcx, 'tcx> {
705+
impl<'a, 'gcx, 'tcx> InitialFlow for MaybeInitializedLvals<'a, 'gcx, 'tcx> {
706706
#[inline]
707707
fn bottom_value() -> bool {
708708
false // bottom = uninitialized
709709
}
710710
}
711711

712-
impl<'a, 'gcx, 'tcx> DataflowOperator for MaybeUninitializedLvals<'a, 'gcx, 'tcx> {
712+
impl<'a, 'gcx, 'tcx> InitialFlow for MaybeUninitializedLvals<'a, 'gcx, 'tcx> {
713713
#[inline]
714714
fn bottom_value() -> bool {
715715
false // bottom = initialized (start_block_effect counters this at outset)
716716
}
717717
}
718718

719-
impl<'a, 'gcx, 'tcx> DataflowOperator for DefinitelyInitializedLvals<'a, 'gcx, 'tcx> {
719+
impl<'a, 'gcx, 'tcx> InitialFlow for DefinitelyInitializedLvals<'a, 'gcx, 'tcx> {
720720
#[inline]
721721
fn bottom_value() -> bool {
722722
true // bottom = initialized (start_block_effect counters this at outset)
723723
}
724724
}
725725

726-
impl<'a, 'gcx, 'tcx> DataflowOperator for MovingOutStatements<'a, 'gcx, 'tcx> {
726+
impl<'a, 'gcx, 'tcx> InitialFlow for MovingOutStatements<'a, 'gcx, 'tcx> {
727727
#[inline]
728728
fn bottom_value() -> bool {
729729
false // bottom = no loans in scope by default
730730
}
731731
}
732732

733-
impl<'a, 'gcx, 'tcx> DataflowOperator for EverInitializedLvals<'a, 'gcx, 'tcx> {
733+
impl<'a, 'gcx, 'tcx> InitialFlow for EverInitializedLvals<'a, 'gcx, 'tcx> {
734734
#[inline]
735735
fn bottom_value() -> bool {
736736
false // bottom = no initialized variables by default

src/librustc_mir/dataflow/impls/storage_liveness.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl<'a, 'tcx> BitwiseOperator for MaybeStorageLive<'a, 'tcx> {
7474
}
7575
}
7676

77-
impl<'a, 'tcx> DataflowOperator for MaybeStorageLive<'a, 'tcx> {
77+
impl<'a, 'tcx> InitialFlow for MaybeStorageLive<'a, 'tcx> {
7878
#[inline]
7979
fn bottom_value() -> bool {
8080
false // bottom = dead

src/librustc_mir/dataflow/mod.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub(crate) fn do_dataflow<'a, 'gcx, 'tcx, BD, P>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
127127
bd: BD,
128128
p: P)
129129
-> DataflowResults<BD>
130-
where BD: BitDenotation,
130+
where BD: BitDenotation + InitialFlow,
131131
P: Fn(&BD, BD::Idx) -> DebugFormatted
132132
{
133133
let name_found = |sess: &Session, attrs: &[ast::Attribute], name| -> Option<String> {
@@ -176,7 +176,6 @@ impl<'a, 'tcx: 'a, BD> DataflowAnalysis<'a, 'tcx, BD> where BD: BitDenotation
176176
};
177177
while propcx.changed {
178178
propcx.changed = false;
179-
propcx.reset(&mut temp);
180179
propcx.walk_cfg(&mut temp);
181180
}
182181
}
@@ -212,13 +211,6 @@ impl<'a, 'tcx: 'a, BD> DataflowAnalysis<'a, 'tcx, BD> where BD: BitDenotation
212211

213212
impl<'b, 'a: 'b, 'tcx: 'a, BD> PropagationContext<'b, 'a, 'tcx, BD> where BD: BitDenotation
214213
{
215-
fn reset(&mut self, bits: &mut IdxSet<BD::Idx>) {
216-
let e = if BD::bottom_value() {!0} else {0};
217-
for b in bits.words_mut() {
218-
*b = e;
219-
}
220-
}
221-
222214
fn walk_cfg(&mut self, in_out: &mut IdxSet<BD::Idx>) {
223215
let mir = self.builder.mir;
224216
for (bb_idx, bb_data) in mir.basic_blocks().iter().enumerate() {
@@ -554,12 +546,17 @@ impl<E:Idx> AllSets<E> {
554546
}
555547

556548
/// Parameterization for the precise form of data flow that is used.
557-
pub trait DataflowOperator: BitwiseOperator {
549+
/// `InitialFlow` handles initializing the bitvectors before any
550+
/// code is inspected by the analysis. Analyses that need more nuanced
551+
/// initialization (e.g. they need to consult the results of some other
552+
/// dataflow analysis to set up the initial bitvectors) should not
553+
/// implement this.
554+
pub trait InitialFlow {
558555
/// Specifies the initial value for each bit in the `on_entry` set
559556
fn bottom_value() -> bool;
560557
}
561558

562-
pub trait BitDenotation: DataflowOperator {
559+
pub trait BitDenotation: BitwiseOperator {
563560
/// Specifies what index type is used to access the bitvector.
564561
type Idx: Idx;
565562

@@ -642,7 +639,7 @@ impl<'a, 'gcx, 'tcx: 'a, D> DataflowAnalysis<'a, 'tcx, D> where D: BitDenotation
642639
pub fn new(_tcx: TyCtxt<'a, 'gcx, 'tcx>,
643640
mir: &'a Mir<'tcx>,
644641
dead_unwinds: &'a IdxSet<mir::BasicBlock>,
645-
denotation: D) -> Self {
642+
denotation: D) -> Self where D: InitialFlow {
646643
let bits_per_block = denotation.bits_per_block();
647644
let usize_bits = mem::size_of::<usize>() * 8;
648645
let words_per_block = (bits_per_block + usize_bits - 1) / usize_bits;

0 commit comments

Comments
 (0)