// option. This file may not be copied, modified, or distributed
// except according to those terms.
-use rustc_data_structures::bitvec::BitVector;
+use rustc_data_structures::bitvec::BitArray;
use super::*;
#[derive(Clone)]
pub struct Preorder<'a, 'tcx: 'a> {
mir: &'a Mir<'tcx>,
- visited: BitVector<BasicBlock>,
+ visited: BitArray<BasicBlock>,
worklist: Vec<BasicBlock>,
}
Preorder {
mir,
- visited: BitVector::new(mir.basic_blocks().len()),
+ visited: BitArray::new(mir.basic_blocks().len()),
worklist,
}
}
/// A Postorder traversal of this graph is `D B C A` or `D C B A`
pub struct Postorder<'a, 'tcx: 'a> {
mir: &'a Mir<'tcx>,
- visited: BitVector<BasicBlock>,
+ visited: BitArray<BasicBlock>,
visit_stack: Vec<(BasicBlock, Successors<'a>)>
}
pub fn new(mir: &'a Mir<'tcx>, root: BasicBlock) -> Postorder<'a, 'tcx> {
let mut po = Postorder {
mir,
- visited: BitVector::new(mir.basic_blocks().len()),
+ visited: BitArray::new(mir.basic_blocks().len()),
visit_stack: Vec::new()
};
use mir::interpret::{GlobalId};
use rustc_data_structures::sync::Lock;
-use rustc_data_structures::bitvec::BitVector;
+use rustc_data_structures::bitvec::BitArray;
use std::iter;
use std::cmp;
use std::fmt;
} else {
return Err(Unimplemented);
};
- let mut ty_params = BitVector::new(substs_a.types().count());
+ let mut ty_params = BitArray::new(substs_a.types().count());
let mut found = false;
for ty in field.walk() {
if let ty::TyParam(p) = ty.sty {
use syntax_pos::Pos;
-use rustc_data_structures::bitvec::BitVector;
+use rustc_data_structures::bitvec::BitArray;
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
use syntax_pos::BytePos;
};
// Find all the scopes with variables defined in them.
- let mut has_variables = BitVector::new(mir.source_scopes.len());
+ let mut has_variables = BitArray::new(mir.source_scopes.len());
for var in mir.vars_iter() {
let decl = &mir.local_decls[var];
has_variables.insert(decl.visibility_scope);
fn make_mir_scope(cx: &CodegenCx<'ll, '_>,
mir: &Mir,
- has_variables: &BitVector<SourceScope>,
+ has_variables: &BitArray<SourceScope>,
debug_context: &FunctionDebugContextData<'ll>,
scope: SourceScope,
scopes: &mut IndexVec<SourceScope, MirDebugScope<'ll>>) {
//! An analysis to determine which locals require allocas and
//! which do not.
-use rustc_data_structures::bitvec::BitVector;
+use rustc_data_structures::bitvec::BitArray;
use rustc_data_structures::graph::dominators::Dominators;
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
use rustc::mir::{self, Location, TerminatorKind};
use type_of::LayoutLlvmExt;
use super::FunctionCx;
-pub fn non_ssa_locals(fx: &FunctionCx<'a, 'll, 'tcx>) -> BitVector<mir::Local> {
+pub fn non_ssa_locals(fx: &FunctionCx<'a, 'll, 'tcx>) -> BitArray<mir::Local> {
let mir = fx.mir;
let mut analyzer = LocalAnalyzer::new(fx);
struct LocalAnalyzer<'mir, 'a: 'mir, 'll: 'a, 'tcx: 'll> {
fx: &'mir FunctionCx<'a, 'll, 'tcx>,
dominators: Dominators<mir::BasicBlock>,
- non_ssa_locals: BitVector<mir::Local>,
+ non_ssa_locals: BitArray<mir::Local>,
// The location of the first visited direct assignment to each
// local, or an invalid location (out of bounds `block` index).
first_assignment: IndexVec<mir::Local, Location>
let mut analyzer = LocalAnalyzer {
fx,
dominators: fx.mir.dominators(),
- non_ssa_locals: BitVector::new(fx.mir.local_decls.len()),
+ non_ssa_locals: BitArray::new(fx.mir.local_decls.len()),
first_assignment: IndexVec::from_elem(invalid_location, &fx.mir.local_decls)
};
use std::iter;
-use rustc_data_structures::bitvec::BitVector;
+use rustc_data_structures::bitvec::BitArray;
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
pub use self::constant::codegen_static_initializer;
debuginfo::start_emitting_source_locations(&fx.debug_context);
let rpo = traversal::reverse_postorder(&mir);
- let mut visited = BitVector::new(mir.basic_blocks().len());
+ let mut visited = BitArray::new(mir.basic_blocks().len());
// Codegen the body of each block using reverse postorder
for (bb, _) in rpo {
bx: &Builder<'a, 'll, 'tcx>,
fx: &FunctionCx<'a, 'll, 'tcx>,
scopes: &IndexVec<mir::SourceScope, debuginfo::MirDebugScope<'ll>>,
- memory_locals: &BitVector<mir::Local>,
+ memory_locals: &BitArray<mir::Local>,
) -> Vec<LocalRef<'ll, 'tcx>> {
let mir = fx.mir;
let tcx = bx.tcx();
// except according to those terms.
use indexed_vec::{Idx, IndexVec};
-use std::iter::FromIterator;
use std::marker::PhantomData;
type Word = u128;
const WORD_BITS: usize = 128;
-/// A very simple BitVector type.
+/// A very simple BitArray type.
+///
+/// It does not support resizing after creation; use `BitVector` for that.
#[derive(Clone, Debug, PartialEq)]
-pub struct BitVector<C: Idx> {
+pub struct BitArray<C: Idx> {
data: Vec<Word>,
marker: PhantomData<C>,
}
+#[derive(Clone, Debug, PartialEq)]
+pub struct BitVector<C: Idx> {
+ data: BitArray<C>,
+}
+
impl<C: Idx> BitVector<C> {
+ pub fn grow(&mut self, num_bits: C) {
+ self.data.grow(num_bits)
+ }
+
+ pub fn new() -> BitVector<C> {
+ BitVector {
+ data: BitArray::new(0),
+ }
+ }
+
+ pub fn with_capacity(bits: usize) -> BitVector<C> {
+ BitVector {
+ data: BitArray::new(bits),
+ }
+ }
+
+ /// Returns true if the bit has changed.
+ #[inline]
+ pub fn insert(&mut self, bit: C) -> bool {
+ self.grow(bit);
+ self.data.insert(bit)
+ }
+
#[inline]
- pub fn new(num_bits: usize) -> BitVector<C> {
+ pub fn contains(&self, bit: C) -> bool {
+ let (word, mask) = word_mask(bit);
+ if let Some(word) = self.data.data.get(word) {
+ (word & mask) != 0
+ } else {
+ false
+ }
+ }
+}
+
+impl<C: Idx> BitArray<C> {
+ // Do not make this method public, instead switch your use case to BitVector.
+ #[inline]
+ fn grow(&mut self, num_bits: C) {
let num_words = words(num_bits);
- BitVector {
+ if self.data.len() <= num_words {
+ self.data.resize(num_words + 1, 0)
+ }
+ }
+
+ #[inline]
+ pub fn new(num_bits: usize) -> BitArray<C> {
+ let num_words = words(num_bits);
+ BitArray {
data: vec![0; num_words],
marker: PhantomData,
}
///
/// The two vectors must have the same length.
#[inline]
- pub fn contains_all(&self, other: &BitVector<C>) -> bool {
+ pub fn contains_all(&self, other: &BitArray<C>) -> bool {
assert_eq!(self.data.len(), other.data.len());
self.data.iter().zip(&other.data).all(|(a, b)| (a & b) == *b)
}
}
#[inline]
- pub fn merge(&mut self, all: &BitVector<C>) -> bool {
+ pub fn merge(&mut self, all: &BitArray<C>) -> bool {
assert!(self.data.len() == all.data.len());
let mut changed = false;
for (i, j) in self.data.iter_mut().zip(&all.data) {
changed
}
- #[inline]
- pub fn grow(&mut self, num_bits: C) {
- let num_words = words(num_bits);
- if self.data.len() < num_words {
- self.data.resize(num_words, 0)
- }
- }
-
/// Iterates over indexes of set bits in a sorted order
#[inline]
- pub fn iter<'a>(&'a self) -> BitVectorIter<'a, C> {
- BitVectorIter {
+ pub fn iter<'a>(&'a self) -> BitIter<'a, C> {
+ BitIter {
iter: self.data.iter(),
current: 0,
idx: 0,
}
}
-pub struct BitVectorIter<'a, C: Idx> {
+pub struct BitIter<'a, C: Idx> {
iter: ::std::slice::Iter<'a, Word>,
current: Word,
idx: usize,
marker: PhantomData<C>
}
-impl<'a, C: Idx> Iterator for BitVectorIter<'a, C> {
+impl<'a, C: Idx> Iterator for BitIter<'a, C> {
type Item = C;
fn next(&mut self) -> Option<C> {
while self.current == 0 {
}
}
-impl<C: Idx> FromIterator<bool> for BitVector<C> {
- fn from_iter<I>(iter: I) -> BitVector<C>
- where
- I: IntoIterator<Item = bool>,
- {
- let iter = iter.into_iter();
- let (len, _) = iter.size_hint();
- // Make the minimum length for the bitvector WORD_BITS bits since that's
- // the smallest non-zero size anyway.
- let len = if len < WORD_BITS { WORD_BITS } else { len };
- let mut bv = BitVector::new(len);
- for (idx, val) in iter.enumerate() {
- if idx > len {
- bv.grow(C::new(idx));
- }
- if val {
- bv.insert(C::new(idx));
- }
- }
-
- bv
- }
-}
-
/// A "bit matrix" is basically a matrix of booleans represented as
/// one gigantic bitvector. In other words, it is as if you have
/// `rows` bitvectors, each of length `columns`.
/// Iterates through all the columns set to true in a given row of
/// the matrix.
- pub fn iter<'a>(&'a self, row: R) -> BitVectorIter<'a, C> {
+ pub fn iter<'a>(&'a self, row: R) -> BitIter<'a, C> {
let (start, end) = self.range(row);
- BitVectorIter {
+ BitIter {
iter: self.vector[start..end].iter(),
current: 0,
idx: 0,
C: Idx,
{
columns: usize,
- vector: IndexVec<R, BitVector<C>>,
+ vector: IndexVec<R, BitArray<C>>,
}
impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
fn ensure_row(&mut self, row: R) {
let columns = self.columns;
self.vector
- .ensure_contains_elem(row, || BitVector::new(columns));
+ .ensure_contains_elem(row, || BitArray::new(columns));
}
/// Sets the cell at `(row, column)` to true. Put another way, insert
}
/// Merge a row, `from`, into the `into` row.
- pub fn merge_into(&mut self, into: R, from: &BitVector<C>) -> bool {
+ pub fn merge_into(&mut self, into: R, from: &BitArray<C>) -> bool {
self.ensure_row(into);
self.vector[into].merge(from)
}
}
/// Iterates through each row and the accompanying bit set.
- pub fn iter_enumerated<'a>(&'a self) -> impl Iterator<Item = (R, &'a BitVector<C>)> + 'a {
+ pub fn iter_enumerated<'a>(&'a self) -> impl Iterator<Item = (R, &'a BitArray<C>)> + 'a {
self.vector.iter_enumerated()
}
- pub fn row(&self, row: R) -> Option<&BitVector<C>> {
+ pub fn row(&self, row: R) -> Option<&BitArray<C>> {
self.vector.get(row)
}
}
#[test]
fn bitvec_iter_works() {
- let mut bitvec: BitVector<usize> = BitVector::new(100);
+ let mut bitvec: BitArray<usize> = BitArray::new(100);
bitvec.insert(1);
bitvec.insert(10);
bitvec.insert(19);
#[test]
fn bitvec_iter_works_2() {
- let mut bitvec: BitVector<usize> = BitVector::new(319);
+ let mut bitvec: BitArray<usize> = BitArray::new(319);
bitvec.insert(0);
bitvec.insert(127);
bitvec.insert(191);
#[test]
fn union_two_vecs() {
- let mut vec1: BitVector<usize> = BitVector::new(65);
- let mut vec2: BitVector<usize> = BitVector::new(65);
+ let mut vec1: BitArray<usize> = BitArray::new(65);
+ let mut vec2: BitArray<usize> = BitArray::new(65);
assert!(vec1.insert(3));
assert!(!vec1.insert(3));
assert!(vec2.insert(5));
#[test]
fn grow() {
- let mut vec1: BitVector<usize> = BitVector::new(65);
+ let mut vec1: BitVector<usize> = BitVector::with_capacity(65);
for index in 0..65 {
assert!(vec1.insert(index));
assert!(!vec1.insert(index));
//! the field `next_edge`). Each of those fields is an array that should
//! be indexed by the direction (see the type `Direction`).
-use bitvec::BitVector;
+use bitvec::BitArray;
use std::fmt::Debug;
use std::usize;
use snapshot_vec::{SnapshotVec, SnapshotVecDelegate};
direction: Direction,
entry_node: NodeIndex,
) -> Vec<NodeIndex> {
- let mut visited = BitVector::new(self.len_nodes());
+ let mut visited = BitArray::new(self.len_nodes());
let mut stack = vec![];
let mut result = Vec::with_capacity(self.len_nodes());
let mut push_node = |stack: &mut Vec<_>, node: NodeIndex| {
{
graph: &'g Graph<N, E>,
stack: Vec<NodeIndex>,
- visited: BitVector<usize>,
+ visited: BitArray<usize>,
direction: Direction,
}
start_node: NodeIndex,
direction: Direction,
) -> Self {
- let mut visited = BitVector::new(graph.len_nodes());
+ let mut visited = BitArray::new(graph.len_nodes());
visited.insert(start_node.node_id());
DepthFirstTraversal {
graph,
use build::ForGuard::{self, OutsideGuard, RefWithinGuard, ValWithinGuard};
use build::scope::{CachedBlock, DropKind};
use rustc_data_structures::fx::FxHashMap;
-use rustc_data_structures::bitvec::BitVector;
+use rustc_data_structures::bitvec::BitArray;
use rustc::ty::{self, Ty};
use rustc::mir::*;
use rustc::hir;
// test the branches of enum
Switch {
adt_def: &'tcx ty::AdtDef,
- variants: BitVector<usize>,
+ variants: BitArray<usize>,
},
// test the branches of enum
use build::matches::{Candidate, MatchPair, Test, TestKind};
use hair::*;
use rustc_data_structures::fx::FxHashMap;
-use rustc_data_structures::bitvec::BitVector;
+use rustc_data_structures::bitvec::BitArray;
use rustc::ty::{self, Ty};
use rustc::ty::util::IntTypeExt;
use rustc::mir::*;
span: match_pair.pattern.span,
kind: TestKind::Switch {
adt_def: adt_def.clone(),
- variants: BitVector::new(adt_def.variants.len()),
+ variants: BitArray::new(adt_def.variants.len()),
},
}
}
pub fn add_variants_to_switch<'pat>(&mut self,
test_place: &Place<'tcx>,
candidate: &Candidate<'pat, 'tcx>,
- variants: &mut BitVector<usize>)
+ variants: &mut BitArray<usize>)
-> bool
{
let match_pair = match candidate.match_pairs.iter().find(|mp| mp.place == *test_place) {
InliningMap {
index: FxHashMap(),
targets: Vec::new(),
- inlines: BitVector::new(1024),
+ inlines: BitVector::with_capacity(1024),
}
}
use rustc::hir::CodegenFnAttrFlags;
use rustc::hir::def_id::DefId;
-use rustc_data_structures::bitvec::BitVector;
+use rustc_data_structures::bitvec::BitArray;
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
use rustc::mir::*;
// Traverse the MIR manually so we can account for the effects of
// inlining on the CFG.
let mut work_list = vec![START_BLOCK];
- let mut visited = BitVector::new(callee_mir.basic_blocks().len());
+ let mut visited = BitArray::new(callee_mir.basic_blocks().len());
while let Some(bb) = work_list.pop() {
if !visited.insert(bb.index()) { continue; }
let blk = &callee_mir.basic_blocks()[bb];
//! The Qualif flags below can be used to also provide better
//! diagnostics as to why a constant rvalue wasn't promoted.
-use rustc_data_structures::bitvec::BitVector;
+use rustc_data_structures::bitvec::BitArray;
use rustc_data_structures::indexed_set::IdxSetBuf;
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
use rustc_data_structures::fx::FxHashSet;
param_env: ty::ParamEnv<'tcx>,
local_qualif: IndexVec<Local, Option<Qualif>>,
qualif: Qualif,
- const_fn_arg_vars: BitVector<Local>,
+ const_fn_arg_vars: BitArray<Local>,
temp_promotion_state: IndexVec<Local, TempState>,
promotion_candidates: Vec<Candidate>
}
param_env,
local_qualif,
qualif: Qualif::empty(),
- const_fn_arg_vars: BitVector::new(mir.local_decls.len()),
+ const_fn_arg_vars: BitArray::new(mir.local_decls.len()),
temp_promotion_state: temps,
promotion_candidates: vec![]
}
let mir = self.mir;
- let mut seen_blocks = BitVector::new(mir.basic_blocks().len());
+ let mut seen_blocks = BitArray::new(mir.basic_blocks().len());
let mut bb = START_BLOCK;
loop {
seen_blocks.insert(bb.index());
use rustc::ty::TyCtxt;
use rustc::mir::*;
-use rustc_data_structures::bitvec::BitVector;
+use rustc_data_structures::bitvec::BitArray;
use transform::{MirPass, MirSource};
use util::patch::MirPatch;
&self,
bb: BasicBlock,
mir: &Mir,
- nop_landing_pads: &BitVector<BasicBlock>,
+ nop_landing_pads: &BitArray<BasicBlock>,
) -> bool {
for stmt in &mir[bb].statements {
match stmt.kind {
let mut jumps_folded = 0;
let mut landing_pads_removed = 0;
- let mut nop_landing_pads = BitVector::new(mir.basic_blocks().len());
+ let mut nop_landing_pads = BitArray::new(mir.basic_blocks().len());
// This is a post-order traversal, so that if A post-dominates B
// then A will be visited before B.
//! naively generate still contains the `_a = ()` write in the unreachable block "after" the
//! return.
-use rustc_data_structures::bitvec::BitVector;
+use rustc_data_structures::bitvec::BitArray;
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
use rustc::ty::TyCtxt;
use rustc::mir::*;
}
pub fn remove_dead_blocks(mir: &mut Mir) {
- let mut seen = BitVector::new(mir.basic_blocks().len());
+ let mut seen = BitArray::new(mir.basic_blocks().len());
for (bb, _) in traversal::preorder(mir) {
seen.insert(bb.index());
}
tcx: TyCtxt<'a, 'tcx, 'tcx>,
_: MirSource,
mir: &mut Mir<'tcx>) {
- let mut marker = DeclMarker { locals: BitVector::new(mir.local_decls.len()) };
+ let mut marker = DeclMarker { locals: BitArray::new(mir.local_decls.len()) };
marker.visit_mir(mir);
// Return pointer and arguments are always live
marker.locals.insert(RETURN_PLACE);
/// Construct the mapping while swapping out unused stuff out from the `vec`.
fn make_local_map<'tcx, V>(
vec: &mut IndexVec<Local, V>,
- mask: BitVector<Local>,
+ mask: BitArray<Local>,
) -> IndexVec<Local, Option<Local>> {
let mut map: IndexVec<Local, Option<Local>> = IndexVec::from_elem(None, &*vec);
let mut used = Local::new(0);
}
struct DeclMarker {
- pub locals: BitVector<Local>,
+ pub locals: BitArray<Local>,
}
impl<'tcx> Visitor<'tcx> for DeclMarker {
use print::pprust;
use ptr::P;
use rustc_data_structures::indexed_vec;
+use rustc_data_structures::indexed_vec::Idx;
use symbol::{Symbol, keywords};
use tokenstream::{ThinTokenStream, TokenStream};
Inner,
}
-#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
+#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, PartialOrd, Ord, Copy)]
pub struct AttrId(pub usize);
+impl Idx for AttrId {
+ fn new(idx: usize) -> Self {
+ AttrId(idx)
+ }
+ fn index(self) -> usize {
+ self.0
+ }
+}
+
/// Meta-data associated with an item
/// Doc-comments are promoted to attributes that have is_sugared_doc = true
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub fn mark_used(attr: &Attribute) {
debug!("Marking {:?} as used.", attr);
- let AttrId(id) = attr.id;
GLOBALS.with(|globals| {
- let mut slot = globals.used_attrs.lock();
- let idx = (id / 64) as usize;
- let shift = id % 64;
- if slot.len() <= idx {
- slot.resize(idx + 1, 0);
- }
- slot[idx] |= 1 << shift;
+ globals.used_attrs.lock().insert(attr.id);
});
}
pub fn is_used(attr: &Attribute) -> bool {
- let AttrId(id) = attr.id;
GLOBALS.with(|globals| {
- let slot = globals.used_attrs.lock();
- let idx = (id / 64) as usize;
- let shift = id % 64;
- slot.get(idx).map(|bits| bits & (1 << shift) != 0)
- .unwrap_or(false)
+ globals.used_attrs.lock().contains(attr.id)
})
}
pub fn mark_known(attr: &Attribute) {
debug!("Marking {:?} as known.", attr);
- let AttrId(id) = attr.id;
GLOBALS.with(|globals| {
- let mut slot = globals.known_attrs.lock();
- let idx = (id / 64) as usize;
- let shift = id % 64;
- if slot.len() <= idx {
- slot.resize(idx + 1, 0);
- }
- slot[idx] |= 1 << shift;
+ globals.known_attrs.lock().insert(attr.id);
});
}
pub fn is_known(attr: &Attribute) -> bool {
- let AttrId(id) = attr.id;
GLOBALS.with(|globals| {
- let slot = globals.known_attrs.lock();
- let idx = (id / 64) as usize;
- let shift = id % 64;
- slot.get(idx).map(|bits| bits & (1 << shift) != 0)
- .unwrap_or(false)
+ globals.known_attrs.lock().contains(attr.id)
})
}
extern crate serialize as rustc_serialize; // used by deriving
use rustc_data_structures::sync::Lock;
+use rustc_data_structures::bitvec::BitVector;
+use ast::AttrId;
// A variant of 'try!' that panics on an Err. This is used as a crutch on the
// way towards a non-panic!-prone parser. It should be used for fatal parsing
}
pub struct Globals {
- used_attrs: Lock<Vec<u64>>,
- known_attrs: Lock<Vec<u64>>,
+ used_attrs: Lock<BitVector<AttrId>>,
+ known_attrs: Lock<BitVector<AttrId>>,
syntax_pos_globals: syntax_pos::Globals,
}
impl Globals {
fn new() -> Globals {
Globals {
- used_attrs: Lock::new(Vec::new()),
- known_attrs: Lock::new(Vec::new()),
+ // We have no idea how many attributes their will be, so just
+ // initiate the vectors with 0 bits. We'll grow them as necessary.
+ used_attrs: Lock::new(BitVector::new()),
+ known_attrs: Lock::new(BitVector::new()),
syntax_pos_globals: syntax_pos::Globals::new(),
}
}