Skip to content

Commit 626b298

Browse files
committed
Introduce UnionIntoIdxSet and SubtractFromIdxSet traits.
They let `union()`, `union_sparse()` and `union_hybrid()` be merged. Likewise for subtract()`, `subtract_sparse()` and `subtract_hybrid()`.
1 parent 180052d commit 626b298

File tree

4 files changed

+78
-48
lines changed

4 files changed

+78
-48
lines changed

src/librustc_data_structures/indexed_set.rs

+68-38
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ use bitslice::{bitwise, Union, Subtract, Intersect};
1919
use indexed_vec::Idx;
2020
use rustc_serialize;
2121

22+
/// This is implemented by all the index sets so that IdxSet::union() can be
23+
/// passed any type of index set.
24+
pub trait UnionIntoIdxSet<T: Idx> {
25+
// Performs `other = other | self`.
26+
fn union_into(&self, other: &mut IdxSet<T>) -> bool;
27+
}
28+
29+
/// This is implemented by all the index sets so that IdxSet::subtract() can be
30+
/// passed any type of index set.
31+
pub trait SubtractFromIdxSet<T: Idx> {
32+
// Performs `other = other - self`.
33+
fn subtract_from(&self, other: &mut IdxSet<T>) -> bool;
34+
}
35+
2236
/// Represents a set of some element type E, where each E is identified by some
2337
/// unique index type `T`.
2438
///
@@ -164,48 +178,14 @@ impl<T: Idx> IdxSet<T> {
164178

165179
/// Set `self = self | other` and return true if `self` changed
166180
/// (i.e., if new bits were added).
167-
pub fn union(&mut self, other: &IdxSet<T>) -> bool {
168-
bitwise(self.words_mut(), other.words(), &Union)
169-
}
170-
171-
/// Like `union()`, but takes a `SparseIdxSet` argument.
172-
fn union_sparse(&mut self, other: &SparseIdxSet<T>) -> bool {
173-
let mut changed = false;
174-
for elem in other.iter() {
175-
changed |= self.add(&elem);
176-
}
177-
changed
178-
}
179-
180-
/// Like `union()`, but takes a `HybridIdxSet` argument.
181-
pub fn union_hybrid(&mut self, other: &HybridIdxSet<T>) -> bool {
182-
match other {
183-
HybridIdxSet::Sparse(sparse, _) => self.union_sparse(sparse),
184-
HybridIdxSet::Dense(dense, _) => self.union(dense),
185-
}
181+
pub fn union(&mut self, other: &impl UnionIntoIdxSet<T>) -> bool {
182+
other.union_into(self)
186183
}
187184

188185
/// Set `self = self - other` and return true if `self` changed.
189186
/// (i.e., if any bits were removed).
190-
pub fn subtract(&mut self, other: &IdxSet<T>) -> bool {
191-
bitwise(self.words_mut(), other.words(), &Subtract)
192-
}
193-
194-
/// Like `subtract()`, but takes a `SparseIdxSet` argument.
195-
fn subtract_sparse(&mut self, other: &SparseIdxSet<T>) -> bool {
196-
let mut changed = false;
197-
for elem in other.iter() {
198-
changed |= self.remove(&elem);
199-
}
200-
changed
201-
}
202-
203-
/// Like `subtract()`, but takes a `HybridIdxSet` argument.
204-
pub fn subtract_hybrid(&mut self, other: &HybridIdxSet<T>) -> bool {
205-
match other {
206-
HybridIdxSet::Sparse(sparse, _) => self.subtract_sparse(sparse),
207-
HybridIdxSet::Dense(dense, _) => self.subtract(dense),
208-
}
187+
pub fn subtract(&mut self, other: &impl SubtractFromIdxSet<T>) -> bool {
188+
other.subtract_from(self)
209189
}
210190

211191
/// Set `self = self & other` and return true if `self` changed.
@@ -223,6 +203,18 @@ impl<T: Idx> IdxSet<T> {
223203
}
224204
}
225205

206+
impl<T: Idx> UnionIntoIdxSet<T> for IdxSet<T> {
207+
fn union_into(&self, other: &mut IdxSet<T>) -> bool {
208+
bitwise(other.words_mut(), self.words(), &Union)
209+
}
210+
}
211+
212+
impl<T: Idx> SubtractFromIdxSet<T> for IdxSet<T> {
213+
fn subtract_from(&self, other: &mut IdxSet<T>) -> bool {
214+
bitwise(other.words_mut(), self.words(), &Subtract)
215+
}
216+
}
217+
226218
pub struct Iter<'a, T: Idx> {
227219
cur: Option<(Word, usize)>,
228220
iter: iter::Enumerate<slice::Iter<'a, Word>>,
@@ -308,6 +300,26 @@ impl<T: Idx> SparseIdxSet<T> {
308300
}
309301
}
310302

303+
impl<T: Idx> UnionIntoIdxSet<T> for SparseIdxSet<T> {
304+
fn union_into(&self, other: &mut IdxSet<T>) -> bool {
305+
let mut changed = false;
306+
for elem in self.iter() {
307+
changed |= other.add(&elem);
308+
}
309+
changed
310+
}
311+
}
312+
313+
impl<T: Idx> SubtractFromIdxSet<T> for SparseIdxSet<T> {
314+
fn subtract_from(&self, other: &mut IdxSet<T>) -> bool {
315+
let mut changed = false;
316+
for elem in self.iter() {
317+
changed |= other.remove(&elem);
318+
}
319+
changed
320+
}
321+
}
322+
311323
pub struct SparseIter<'a, T: Idx> {
312324
iter: slice::Iter<'a, T>,
313325
}
@@ -411,6 +423,24 @@ impl<T: Idx> HybridIdxSet<T> {
411423
}
412424
}
413425

426+
impl<T: Idx> UnionIntoIdxSet<T> for HybridIdxSet<T> {
427+
fn union_into(&self, other: &mut IdxSet<T>) -> bool {
428+
match self {
429+
HybridIdxSet::Sparse(sparse, _) => sparse.union_into(other),
430+
HybridIdxSet::Dense(dense, _) => dense.union_into(other),
431+
}
432+
}
433+
}
434+
435+
impl<T: Idx> SubtractFromIdxSet<T> for HybridIdxSet<T> {
436+
fn subtract_from(&self, other: &mut IdxSet<T>) -> bool {
437+
match self {
438+
HybridIdxSet::Sparse(sparse, _) => sparse.subtract_from(other),
439+
HybridIdxSet::Dense(dense, _) => dense.subtract_from(other),
440+
}
441+
}
442+
}
443+
414444
pub enum HybridIter<'a, T: Idx> {
415445
Sparse(SparseIter<'a, T>),
416446
Dense(Iter<'a, T>),

src/librustc_mir/dataflow/at_location.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ where
129129
F: FnOnce(Iter<BD::Idx>),
130130
{
131131
let mut curr_state = self.curr_state.clone();
132-
curr_state.union_hybrid(&self.stmt_gen);
133-
curr_state.subtract_hybrid(&self.stmt_kill);
132+
curr_state.union(&self.stmt_gen);
133+
curr_state.subtract(&self.stmt_kill);
134134
f(curr_state.iter());
135135
}
136136
}
@@ -193,8 +193,8 @@ impl<BD> FlowsAtLocation for FlowAtLocation<BD>
193193
}
194194

195195
fn apply_local_effect(&mut self, _loc: Location) {
196-
self.curr_state.union_hybrid(&self.stmt_gen);
197-
self.curr_state.subtract_hybrid(&self.stmt_kill);
196+
self.curr_state.union(&self.stmt_gen);
197+
self.curr_state.subtract(&self.stmt_kill);
198198
}
199199
}
200200

src/librustc_mir/dataflow/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ impl<'b, 'a: 'b, 'tcx: 'a, BD> PropagationContext<'b, 'a, 'tcx, BD> where BD: Bi
241241
let sets = self.builder.flow_state.sets.for_block(bb.index());
242242
debug_assert!(in_out.words().len() == sets.on_entry.words().len());
243243
in_out.overwrite(sets.on_entry);
244-
in_out.union_hybrid(sets.gen_set);
245-
in_out.subtract_hybrid(sets.kill_set);
244+
in_out.union(sets.gen_set);
245+
in_out.subtract(sets.kill_set);
246246
}
247247
self.builder.propagate_bits_into_graph_successors_of(
248248
in_out, (bb, bb_data), &mut dirty_queue);
@@ -534,8 +534,8 @@ impl<'a, E:Idx> BlockSets<'a, E> {
534534
}
535535

536536
fn apply_local_effect(&mut self) {
537-
self.on_entry.union_hybrid(&self.gen_set);
538-
self.on_entry.subtract_hybrid(&self.kill_set);
537+
self.on_entry.union(self.gen_set);
538+
self.on_entry.subtract(self.kill_set);
539539
}
540540
}
541541

src/librustc_mir/transform/rustc_peek.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ fn each_block<'a, 'tcx, O>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
209209
&mut sets, Location { block: bb, statement_index: j });
210210
results.0.operator.statement_effect(
211211
&mut sets, Location { block: bb, statement_index: j });
212-
sets.on_entry.union_hybrid(sets.gen_set);
213-
sets.on_entry.subtract_hybrid(sets.kill_set);
212+
sets.on_entry.union(sets.gen_set);
213+
sets.on_entry.subtract(sets.kill_set);
214214
}
215215

216216
results.0.operator.before_terminator_effect(

0 commit comments

Comments
 (0)