Skip to content

Commit a0da3e9

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 266e2d3 commit a0da3e9

File tree

6 files changed

+37
-56
lines changed

6 files changed

+37
-56
lines changed

src/librustc_data_structures/bit_set.rs

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

168168
/// Get a slice of the underlying words.
@@ -243,13 +243,13 @@ pub trait SubtractFromBitSet<T: Idx> {
243243

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

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

@@ -302,43 +302,26 @@ impl<'a, T: Idx> Iterator for BitIter<'a, T> {
302302
}
303303
}
304304

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

310310
#[inline]
311-
pub fn bitwise<Op: BitwiseOperator>(out_vec: &mut [Word], in_vec: &[Word], op: &Op) -> bool
311+
fn bitwise<Op>(out_vec: &mut [Word], in_vec: &[Word], op: Op) -> bool
312+
where Op: Fn(Word, Word) -> Word
312313
{
313314
assert_eq!(out_vec.len(), in_vec.len());
314315
let mut changed = false;
315316
for (out_elem, in_elem) in out_vec.iter_mut().zip(in_vec.iter()) {
316317
let old_val = *out_elem;
317-
let new_val = op.join(old_val, *in_elem);
318+
let new_val = op(old_val, *in_elem);
318319
*out_elem = new_val;
319320
changed |= old_val != new_val;
320321
}
321322
changed
322323
}
323324

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

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

src/librustc_mir/dataflow/impls/borrowed_locals.rs

Lines changed: 3 additions & 3 deletions
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 13 additions & 13 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 4 additions & 6 deletions
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)