Skip to content

Commit 6ba8a42

Browse files
committed
Eliminate BitwiseOperator.
`BitwiseOperator` is an unnecessarily low-level thing. This commit replaces it with `BitSetOperator`, which works on `BitSet`s instead of words. Within `bit_set.rs`, the commit eliminates `Intersect`, `Union`, and `Subtract` by instead passing a function to `bitwise()`.
1 parent b59273f commit 6ba8a42

File tree

6 files changed

+37
-56
lines changed

6 files changed

+37
-56
lines changed

src/librustc_data_structures/bit_set.rs

+9-26
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl<T: Idx> BitSet<T> {
160160
/// Set `self = self & other` and return true if `self` changed.
161161
/// (i.e., if any bits were removed).
162162
pub fn intersect(&mut self, other: &BitSet<T>) -> bool {
163-
bitwise(self.words_mut(), other.words(), &Intersect)
163+
bitwise(self.words_mut(), other.words(), |a, b| { a & b })
164164
}
165165

166166
/// Get a slice of the underlying words.
@@ -241,13 +241,13 @@ pub trait SubtractFromBitSet<T: Idx> {
241241

242242
impl<T: Idx> UnionIntoBitSet<T> for BitSet<T> {
243243
fn union_into(&self, other: &mut BitSet<T>) -> bool {
244-
bitwise(other.words_mut(), self.words(), &Union)
244+
bitwise(other.words_mut(), self.words(), |a, b| { a | b })
245245
}
246246
}
247247

248248
impl<T: Idx> SubtractFromBitSet<T> for BitSet<T> {
249249
fn subtract_from(&self, other: &mut BitSet<T>) -> bool {
250-
bitwise(other.words_mut(), self.words(), &Subtract)
250+
bitwise(other.words_mut(), self.words(), |a, b| { a & !b })
251251
}
252252
}
253253

@@ -300,43 +300,26 @@ impl<'a, T: Idx> Iterator for BitIter<'a, T> {
300300
}
301301
}
302302

303-
pub trait BitwiseOperator {
304-
/// Applies some bit-operation pointwise to each of the bits in the two inputs.
305-
fn join(&self, pred1: Word, pred2: Word) -> Word;
303+
pub trait BitSetOperator {
304+
/// Combine one bitset into another.
305+
fn join<T: Idx>(&self, inout_set: &mut BitSet<T>, in_set: &BitSet<T>) -> bool;
306306
}
307307

308308
#[inline]
309-
pub fn bitwise<Op: BitwiseOperator>(out_vec: &mut [Word], in_vec: &[Word], op: &Op) -> bool
309+
fn bitwise<Op>(out_vec: &mut [Word], in_vec: &[Word], op: Op) -> bool
310+
where Op: Fn(Word, Word) -> Word
310311
{
311312
assert_eq!(out_vec.len(), in_vec.len());
312313
let mut changed = false;
313314
for (out_elem, in_elem) in out_vec.iter_mut().zip(in_vec.iter()) {
314315
let old_val = *out_elem;
315-
let new_val = op.join(old_val, *in_elem);
316+
let new_val = op(old_val, *in_elem);
316317
*out_elem = new_val;
317318
changed |= old_val != new_val;
318319
}
319320
changed
320321
}
321322

322-
pub struct Intersect;
323-
impl BitwiseOperator for Intersect {
324-
#[inline]
325-
fn join(&self, a: Word, b: Word) -> Word { a & b }
326-
}
327-
328-
pub struct Union;
329-
impl BitwiseOperator for Union {
330-
#[inline]
331-
fn join(&self, a: Word, b: Word) -> Word { a | b }
332-
}
333-
334-
pub struct Subtract;
335-
impl BitwiseOperator for Subtract {
336-
#[inline]
337-
fn join(&self, a: Word, b: Word) -> Word { a & !b }
338-
}
339-
340323
const SPARSE_MAX: usize = 8;
341324

342325
/// A fixed-size bitset type with a sparse representation and a maximum of

src/librustc_mir/dataflow/impls/borrowed_locals.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ impl<'a, 'tcx> BitDenotation for HaveBeenBorrowedLocals<'a, 'tcx> {
8080
}
8181
}
8282

83-
impl<'a, 'tcx> BitwiseOperator for HaveBeenBorrowedLocals<'a, 'tcx> {
83+
impl<'a, 'tcx> BitSetOperator for HaveBeenBorrowedLocals<'a, 'tcx> {
8484
#[inline]
85-
fn join(&self, pred1: Word, pred2: Word) -> Word {
86-
pred1 | pred2 // "maybe" means we union effects of both preds
85+
fn join<T: Idx>(&self, inout_set: &mut BitSet<T>, in_set: &BitSet<T>) -> bool {
86+
inout_set.union(in_set) // "maybe" means we union effects of both preds
8787
}
8888
}
8989

src/librustc_mir/dataflow/impls/borrows.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ use rustc::ty::TyCtxt;
2020
use rustc::ty::{RegionKind, RegionVid};
2121
use rustc::ty::RegionKind::ReScope;
2222

23-
use rustc_data_structures::bit_set::{BitSet, BitwiseOperator, Word};
23+
use rustc_data_structures::bit_set::{BitSet, BitSetOperator};
2424
use rustc_data_structures::fx::FxHashMap;
25-
use rustc_data_structures::indexed_vec::IndexVec;
25+
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
2626
use rustc_data_structures::sync::Lrc;
2727

2828
use dataflow::{BitDenotation, BlockSets, InitialFlow};
@@ -410,10 +410,10 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
410410
}
411411
}
412412

413-
impl<'a, 'gcx, 'tcx> BitwiseOperator for Borrows<'a, 'gcx, 'tcx> {
413+
impl<'a, 'gcx, 'tcx> BitSetOperator for Borrows<'a, 'gcx, 'tcx> {
414414
#[inline]
415-
fn join(&self, pred1: Word, pred2: Word) -> Word {
416-
pred1 | pred2 // union effects of preds when computing reservations
415+
fn join<T: Idx>(&self, inout_set: &mut BitSet<T>, in_set: &BitSet<T>) -> bool {
416+
inout_set.union(in_set) // "maybe" means we union effects of both preds
417417
}
418418
}
419419

src/librustc_mir/dataflow/impls/mod.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
1515
use rustc::ty::TyCtxt;
1616
use rustc::mir::{self, Mir, Location};
17-
use rustc_data_structures::bit_set::{BitSet, BitwiseOperator, Word};
17+
use rustc_data_structures::bit_set::{BitSet, BitSetOperator};
1818
use rustc_data_structures::indexed_vec::Idx;
1919

2020
use super::MoveDataParamEnv;
@@ -549,31 +549,31 @@ impl<'a, 'gcx, 'tcx> BitDenotation for EverInitializedPlaces<'a, 'gcx, 'tcx> {
549549
}
550550
}
551551

552-
impl<'a, 'gcx, 'tcx> BitwiseOperator for MaybeInitializedPlaces<'a, 'gcx, 'tcx> {
552+
impl<'a, 'gcx, 'tcx> BitSetOperator for MaybeInitializedPlaces<'a, 'gcx, 'tcx> {
553553
#[inline]
554-
fn join(&self, pred1: Word, pred2: Word) -> Word {
555-
pred1 | pred2 // "maybe" means we union effects of both preds
554+
fn join<T: Idx>(&self, inout_set: &mut BitSet<T>, in_set: &BitSet<T>) -> bool {
555+
inout_set.union(in_set) // "maybe" means we union effects of both preds
556556
}
557557
}
558558

559-
impl<'a, 'gcx, 'tcx> BitwiseOperator for MaybeUninitializedPlaces<'a, 'gcx, 'tcx> {
559+
impl<'a, 'gcx, 'tcx> BitSetOperator for MaybeUninitializedPlaces<'a, 'gcx, 'tcx> {
560560
#[inline]
561-
fn join(&self, pred1: Word, pred2: Word) -> Word {
562-
pred1 | pred2 // "maybe" means we union effects of both preds
561+
fn join<T: Idx>(&self, inout_set: &mut BitSet<T>, in_set: &BitSet<T>) -> bool {
562+
inout_set.union(in_set) // "maybe" means we union effects of both preds
563563
}
564564
}
565565

566-
impl<'a, 'gcx, 'tcx> BitwiseOperator for DefinitelyInitializedPlaces<'a, 'gcx, 'tcx> {
566+
impl<'a, 'gcx, 'tcx> BitSetOperator for DefinitelyInitializedPlaces<'a, 'gcx, 'tcx> {
567567
#[inline]
568-
fn join(&self, pred1: Word, pred2: Word) -> Word {
569-
pred1 & pred2 // "definitely" means we intersect effects of both preds
568+
fn join<T: Idx>(&self, inout_set: &mut BitSet<T>, in_set: &BitSet<T>) -> bool {
569+
inout_set.intersect(in_set) // "definitely" means we intersect effects of both preds
570570
}
571571
}
572572

573-
impl<'a, 'gcx, 'tcx> BitwiseOperator for EverInitializedPlaces<'a, 'gcx, 'tcx> {
573+
impl<'a, 'gcx, 'tcx> BitSetOperator for EverInitializedPlaces<'a, 'gcx, 'tcx> {
574574
#[inline]
575-
fn join(&self, pred1: Word, pred2: Word) -> Word {
576-
pred1 | pred2 // inits from both preds are in scope
575+
fn join<T: Idx>(&self, inout_set: &mut BitSet<T>, in_set: &BitSet<T>) -> bool {
576+
inout_set.union(in_set) // inits from both preds are in scope
577577
}
578578
}
579579

src/librustc_mir/dataflow/impls/storage_liveness.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ impl<'a, 'tcx> BitDenotation for MaybeStorageLive<'a, 'tcx> {
6767
}
6868
}
6969

70-
impl<'a, 'tcx> BitwiseOperator for MaybeStorageLive<'a, 'tcx> {
70+
impl<'a, 'tcx> BitSetOperator for MaybeStorageLive<'a, 'tcx> {
7171
#[inline]
72-
fn join(&self, pred1: Word, pred2: Word) -> Word {
73-
pred1 | pred2 // "maybe" means we union effects of both preds
72+
fn join<T: Idx>(&self, inout_set: &mut BitSet<T>, in_set: &BitSet<T>) -> bool {
73+
inout_set.union(in_set) // "maybe" means we union effects of both preds
7474
}
7575
}
7676

src/librustc_mir/dataflow/mod.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use syntax::ast::{self, MetaItem};
1212

13-
use rustc_data_structures::bit_set::{bitwise, BitwiseOperator, BitSet, HybridBitSet};
13+
use rustc_data_structures::bit_set::{BitSet, BitSetOperator, HybridBitSet};
1414
use rustc_data_structures::indexed_vec::Idx;
1515
use rustc_data_structures::work_queue::WorkQueue;
1616

@@ -561,7 +561,7 @@ pub trait InitialFlow {
561561
fn bottom_value() -> bool;
562562
}
563563

564-
pub trait BitDenotation: BitwiseOperator {
564+
pub trait BitDenotation: BitSetOperator {
565565
/// Specifies what index type is used to access the bitvector.
566566
type Idx: Idx;
567567

@@ -830,10 +830,8 @@ impl<'a, 'tcx: 'a, D> DataflowAnalysis<'a, 'tcx, D> where D: BitDenotation
830830
in_out: &BitSet<D::Idx>,
831831
bb: mir::BasicBlock,
832832
dirty_queue: &mut WorkQueue<mir::BasicBlock>) {
833-
let entry_set = self.flow_state.sets.for_block(bb.index()).on_entry;
834-
let set_changed = bitwise(entry_set.words_mut(),
835-
in_out.words(),
836-
&self.flow_state.operator);
833+
let entry_set = &mut self.flow_state.sets.for_block(bb.index()).on_entry;
834+
let set_changed = self.flow_state.operator.join(entry_set, &in_out);
837835
if set_changed {
838836
dirty_queue.insert(bb);
839837
}

0 commit comments

Comments
 (0)