Skip to content

Commit 9b8b8c6

Browse files
committed
Move DataFlowState::{each_bit,interpret_set} method definitions to parent module.
Refactored `each_bit`, which traverses a `IdxSet`, so that the bulk of its implementation lives in `rustc_data_structures`.
1 parent 3ef1afc commit 9b8b8c6

File tree

3 files changed

+56
-50
lines changed

3 files changed

+56
-50
lines changed

src/librustc_data_structures/indexed_set.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,38 @@ impl<T: Idx> IdxSet<T> {
153153
pub fn subtract(&mut self, other: &IdxSet<T>) -> bool {
154154
bitwise(self.words_mut(), other.words(), &Subtract)
155155
}
156+
157+
/// Calls `f` on each index value held in this set, up to the
158+
/// bound `max_bits` on the size of universe of indexes.
159+
pub fn each_bit<F>(&self, max_bits: usize, f: F) where F: FnMut(T) {
160+
each_bit(self, max_bits, f)
161+
}
162+
}
163+
164+
fn each_bit<T: Idx, F>(words: &IdxSet<T>, max_bits: usize, mut f: F) where F: FnMut(T) {
165+
let usize_bits: usize = mem::size_of::<usize>() * 8;
166+
167+
for (word_index, &word) in words.words().iter().enumerate() {
168+
if word != 0 {
169+
let base_index = word_index * usize_bits;
170+
for offset in 0..usize_bits {
171+
let bit = 1 << offset;
172+
if (word & bit) != 0 {
173+
// NB: we round up the total number of bits
174+
// that we store in any given bit set so that
175+
// it is an even multiple of usize::BITS. This
176+
// means that there may be some stray bits at
177+
// the end that do not correspond to any
178+
// actual value; that's why we first check
179+
// that we are in range of bits_per_block.
180+
let bit_index = base_index + offset as usize;
181+
if bit_index >= max_bits {
182+
return;
183+
} else {
184+
f(Idx::new(bit_index));
185+
}
186+
}
187+
}
188+
}
189+
}
156190
}

src/librustc_mir/dataflow/graphviz.rs

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use syntax::ast::NodeId;
1414
use rustc::mir::{BasicBlock, Mir};
1515
use rustc_data_structures::bitslice::bits_to_string;
16-
use rustc_data_structures::indexed_set::{IdxSet};
1716
use rustc_data_structures::indexed_vec::Idx;
1817

1918
use dot;
@@ -24,62 +23,13 @@ use std::fs::File;
2423
use std::io;
2524
use std::io::prelude::*;
2625
use std::marker::PhantomData;
27-
use std::mem;
2826
use std::path::Path;
2927

3028
use util;
3129

3230
use super::{BitDenotation, DataflowState};
3331
use super::DataflowBuilder;
3432

35-
impl<O: BitDenotation> DataflowState<O> {
36-
fn each_bit<F>(&self, words: &IdxSet<O::Idx>, mut f: F)
37-
where F: FnMut(O::Idx) {
38-
//! Helper for iterating over the bits in a bitvector.
39-
40-
let bits_per_block = self.operator.bits_per_block();
41-
let usize_bits: usize = mem::size_of::<usize>() * 8;
42-
43-
for (word_index, &word) in words.words().iter().enumerate() {
44-
if word != 0 {
45-
let base_index = word_index * usize_bits;
46-
for offset in 0..usize_bits {
47-
let bit = 1 << offset;
48-
if (word & bit) != 0 {
49-
// NB: we round up the total number of bits
50-
// that we store in any given bit set so that
51-
// it is an even multiple of usize::BITS. This
52-
// means that there may be some stray bits at
53-
// the end that do not correspond to any
54-
// actual value; that's why we first check
55-
// that we are in range of bits_per_block.
56-
let bit_index = base_index + offset as usize;
57-
if bit_index >= bits_per_block {
58-
return;
59-
} else {
60-
f(O::Idx::new(bit_index));
61-
}
62-
}
63-
}
64-
}
65-
}
66-
}
67-
68-
pub fn interpret_set<'c, P>(&self,
69-
o: &'c O,
70-
words: &IdxSet<O::Idx>,
71-
render_idx: &P)
72-
-> Vec<&'c Debug>
73-
where P: Fn(&O, O::Idx) -> &Debug
74-
{
75-
let mut v = Vec::new();
76-
self.each_bit(words, |i| {
77-
v.push(render_idx(o, i));
78-
});
79-
v
80-
}
81-
}
82-
8333
pub trait MirWithFlowState<'tcx> {
8434
type BD: BitDenotation;
8535
fn node_id(&self) -> NodeId;

src/librustc_mir/dataflow/mod.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,28 @@ pub struct DataflowState<O: BitDenotation>
293293
pub(crate) operator: O,
294294
}
295295

296+
impl<O: BitDenotation> DataflowState<O> {
297+
pub fn each_bit<F>(&self, words: &IdxSet<O::Idx>, f: F) where F: FnMut(O::Idx)
298+
{
299+
let bits_per_block = self.operator.bits_per_block();
300+
words.each_bit(bits_per_block, f)
301+
}
302+
303+
pub fn interpret_set<'c, P>(&self,
304+
o: &'c O,
305+
words: &IdxSet<O::Idx>,
306+
render_idx: &P)
307+
-> Vec<&'c Debug>
308+
where P: Fn(&O, O::Idx) -> &Debug
309+
{
310+
let mut v = Vec::new();
311+
self.each_bit(words, |i| {
312+
v.push(render_idx(o, i));
313+
});
314+
v
315+
}
316+
}
317+
296318
#[derive(Debug)]
297319
pub struct AllSets<E: Idx> {
298320
/// Analysis bitwidth for each block.

0 commit comments

Comments
 (0)