From dae349371fd94ac165417d3a1adaa1df5d107a4a Mon Sep 17 00:00:00 2001 From: Jakob Stoklund Olesen Date: Fri, 29 Jul 2016 15:59:09 -0700 Subject: [PATCH] Add EntityMap::with_capacity. Create a secondary entity map with space reserved for a known range of entity references. Add dfg.num_ebbs() and dfg.num_insts() methods to provide capacities. --- cranelift/src/libcretonne/entity_map.rs | 15 +++++++++++++++ cranelift/src/libcretonne/ir/dfg.rs | 16 ++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/cranelift/src/libcretonne/entity_map.rs b/cranelift/src/libcretonne/entity_map.rs index 9ce39bec19..a0f4f3fa6b 100644 --- a/cranelift/src/libcretonne/entity_map.rs +++ b/cranelift/src/libcretonne/entity_map.rs @@ -102,6 +102,21 @@ impl EntityMap where K: EntityRef, V: Clone + Default { + /// Create a new secondary `EntityMap` that is prepared to hold `n` elements. + /// + /// Use this when the length of the primary map is known: + /// ``` + /// let secondary_map = EntityMap::with_capacity(primary_map.len()); + /// ``` + pub fn with_capacity(n: usize) -> Self { + let mut map = EntityMap { + elems: Vec::with_capacity(n), + unused: PhantomData, + }; + map.elems.resize(n, V::default()); + map + } + /// Ensure that `k` is a valid key but adding default entries if necesssary. /// /// Return a mutable reference to the corresponding entry. diff --git a/cranelift/src/libcretonne/ir/dfg.rs b/cranelift/src/libcretonne/ir/dfg.rs index d377813544..aba45354d7 100644 --- a/cranelift/src/libcretonne/ir/dfg.rs +++ b/cranelift/src/libcretonne/ir/dfg.rs @@ -42,6 +42,22 @@ impl DataFlowGraph { extended_values: Vec::new(), } } + + /// Get the total number of instructions created in this function, whether they are currently + /// inserted in the layout or not. + /// + /// This is intended for use with `EntityMap::with_capacity`. + pub fn num_insts(&self) -> usize { + self.insts.len() + } + + /// Get the total number of extended basic blocks created in this function, whether they are + /// currently inserted in the layout or not. + /// + /// This is intended for use with `EntityMap::with_capacity`. + pub fn num_ebbs(&self) -> usize { + self.ebbs.len() + } } /// Handling values.