From 9b8b8c6aebcc581bf2e74d9a0d3bf65a0ebb2742 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Tue, 4 Jul 2017 15:12:11 +0200 Subject: [PATCH] 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`. --- src/librustc_data_structures/indexed_set.rs | 34 ++++++++++++++ src/librustc_mir/dataflow/graphviz.rs | 50 --------------------- src/librustc_mir/dataflow/mod.rs | 22 +++++++++ 3 files changed, 56 insertions(+), 50 deletions(-) diff --git a/src/librustc_data_structures/indexed_set.rs b/src/librustc_data_structures/indexed_set.rs index 572ce98d3ae..4189089e20d 100644 --- a/src/librustc_data_structures/indexed_set.rs +++ b/src/librustc_data_structures/indexed_set.rs @@ -153,4 +153,38 @@ impl IdxSet { pub fn subtract(&mut self, other: &IdxSet) -> bool { bitwise(self.words_mut(), other.words(), &Subtract) } + + /// Calls `f` on each index value held in this set, up to the + /// bound `max_bits` on the size of universe of indexes. + pub fn each_bit(&self, max_bits: usize, f: F) where F: FnMut(T) { + each_bit(self, max_bits, f) + } +} + +fn each_bit(words: &IdxSet, max_bits: usize, mut f: F) where F: FnMut(T) { + let usize_bits: usize = mem::size_of::() * 8; + + for (word_index, &word) in words.words().iter().enumerate() { + if word != 0 { + let base_index = word_index * usize_bits; + for offset in 0..usize_bits { + let bit = 1 << offset; + if (word & bit) != 0 { + // NB: we round up the total number of bits + // that we store in any given bit set so that + // it is an even multiple of usize::BITS. This + // means that there may be some stray bits at + // the end that do not correspond to any + // actual value; that's why we first check + // that we are in range of bits_per_block. + let bit_index = base_index + offset as usize; + if bit_index >= max_bits { + return; + } else { + f(Idx::new(bit_index)); + } + } + } + } + } } diff --git a/src/librustc_mir/dataflow/graphviz.rs b/src/librustc_mir/dataflow/graphviz.rs index e6d77aa2686..7ff4fbcf199 100644 --- a/src/librustc_mir/dataflow/graphviz.rs +++ b/src/librustc_mir/dataflow/graphviz.rs @@ -13,7 +13,6 @@ use syntax::ast::NodeId; use rustc::mir::{BasicBlock, Mir}; use rustc_data_structures::bitslice::bits_to_string; -use rustc_data_structures::indexed_set::{IdxSet}; use rustc_data_structures::indexed_vec::Idx; use dot; @@ -24,7 +23,6 @@ use std::fs::File; use std::io; use std::io::prelude::*; use std::marker::PhantomData; -use std::mem; use std::path::Path; use util; @@ -32,54 +30,6 @@ use util; use super::{BitDenotation, DataflowState}; use super::DataflowBuilder; -impl DataflowState { - fn each_bit(&self, words: &IdxSet, mut f: F) - where F: FnMut(O::Idx) { - //! Helper for iterating over the bits in a bitvector. - - let bits_per_block = self.operator.bits_per_block(); - let usize_bits: usize = mem::size_of::() * 8; - - for (word_index, &word) in words.words().iter().enumerate() { - if word != 0 { - let base_index = word_index * usize_bits; - for offset in 0..usize_bits { - let bit = 1 << offset; - if (word & bit) != 0 { - // NB: we round up the total number of bits - // that we store in any given bit set so that - // it is an even multiple of usize::BITS. This - // means that there may be some stray bits at - // the end that do not correspond to any - // actual value; that's why we first check - // that we are in range of bits_per_block. - let bit_index = base_index + offset as usize; - if bit_index >= bits_per_block { - return; - } else { - f(O::Idx::new(bit_index)); - } - } - } - } - } - } - - pub fn interpret_set<'c, P>(&self, - o: &'c O, - words: &IdxSet, - render_idx: &P) - -> Vec<&'c Debug> - where P: Fn(&O, O::Idx) -> &Debug - { - let mut v = Vec::new(); - self.each_bit(words, |i| { - v.push(render_idx(o, i)); - }); - v - } -} - pub trait MirWithFlowState<'tcx> { type BD: BitDenotation; fn node_id(&self) -> NodeId; diff --git a/src/librustc_mir/dataflow/mod.rs b/src/librustc_mir/dataflow/mod.rs index 4445fea1254..08b8d332fa1 100644 --- a/src/librustc_mir/dataflow/mod.rs +++ b/src/librustc_mir/dataflow/mod.rs @@ -293,6 +293,28 @@ pub struct DataflowState pub(crate) operator: O, } +impl DataflowState { + pub fn each_bit(&self, words: &IdxSet, f: F) where F: FnMut(O::Idx) + { + let bits_per_block = self.operator.bits_per_block(); + words.each_bit(bits_per_block, f) + } + + pub fn interpret_set<'c, P>(&self, + o: &'c O, + words: &IdxSet, + render_idx: &P) + -> Vec<&'c Debug> + where P: Fn(&O, O::Idx) -> &Debug + { + let mut v = Vec::new(); + self.each_bit(words, |i| { + v.push(render_idx(o, i)); + }); + v + } +} + #[derive(Debug)] pub struct AllSets { /// Analysis bitwidth for each block.