Move default blocks into jump tables (#5756)

Move the default block off of the br_table instrution, and into the JumpTable that it references.
This commit is contained in:
Trevor Elliott
2023-02-10 08:53:30 -08:00
committed by GitHub
parent 49613be393
commit d99783fc91
21 changed files with 118 additions and 175 deletions

View File

@@ -295,19 +295,11 @@ impl FunctionStencil {
}
}
InstructionData::BranchTable {
table,
destination: default_dest,
..
} => {
self.dfg.jump_tables[*table].iter_mut().for_each(|entry| {
InstructionData::BranchTable { table, .. } => {
for entry in self.dfg.jump_tables[*table].all_branches_mut() {
if *entry == old_dest {
*entry = new_dest;
}
});
if *default_dest == old_dest {
*default_dest = new_dest;
}
}

View File

@@ -14,6 +14,12 @@ use serde::{Deserialize, Serialize};
/// Contents of a jump table.
///
/// All jump tables use 0-based indexing and are densely populated.
///
/// The default block for the jump table is stored as the last element of the underlying vector,
/// and is not included in the length of the jump table. It can be accessed through the
/// `default_block` and `default_block_mut` functions. All blocks may be iterated using the
/// `all_branches` and `all_branches_mut` functions, which will both iterate over the default block
/// last.
#[derive(Clone, PartialEq, Hash)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub struct JumpTableData {
@@ -22,72 +28,71 @@ pub struct JumpTableData {
}
impl JumpTableData {
/// Create a new empty jump table.
pub fn new() -> Self {
Self { table: Vec::new() }
}
/// Create a new empty jump table with the specified capacity.
pub fn with_capacity(capacity: usize) -> Self {
Self {
table: Vec::with_capacity(capacity),
}
}
/// Create a new jump table with the provided blocks
pub fn with_blocks(table: Vec<Block>) -> Self {
pub fn new(def: Block, mut table: Vec<Block>) -> Self {
table.push(def);
Self { table }
}
/// Get the number of table entries.
pub fn len(&self) -> usize {
self.table.len()
/// Fetch the default block for this jump table.
pub fn default_block(&self) -> Block {
*self.table.last().unwrap()
}
/// Append a table entry.
pub fn push_entry(&mut self, dest: Block) {
self.table.push(dest)
/// Mutable access to the default block of this jump table.
pub fn default_block_mut(&mut self) -> &mut Block {
self.table.last_mut().unwrap()
}
/// Checks if any of the entries branch to `block`.
pub fn branches_to(&self, block: Block) -> bool {
self.table.iter().any(|target_block| *target_block == block)
}
/// Access the whole table as a slice.
pub fn as_slice(&self) -> &[Block] {
/// The jump table and default block as a single slice. The default block will always be last.
pub fn all_branches(&self) -> &[Block] {
self.table.as_slice()
}
/// Access the whole table as a mutable slice.
pub fn as_mut_slice(&mut self) -> &mut [Block] {
/// The jump table and default block as a single mutable slice. The default block will always
/// be last.
pub fn all_branches_mut(&mut self) -> &mut [Block] {
self.table.as_mut_slice()
}
/// Returns an iterator over the table.
/// Access the jump table as a slice. This excludes the default block.
pub fn as_slice(&self) -> &[Block] {
let last = self.table.len() - 1;
&self.table.as_slice()[0..last]
}
/// Access the jump table as a mutable slice. This excludes the default block.
pub fn as_mut_slice(&mut self) -> &mut [Block] {
let last = self.table.len() - 1;
&mut self.table.as_mut_slice()[0..last]
}
/// Returns an iterator to the jump table, excluding the default block.
#[deprecated(since = "7.0.0", note = "please use `.as_slice()` instead")]
pub fn iter(&self) -> Iter<Block> {
self.table.iter()
self.as_slice().iter()
}
/// Returns an iterator that allows modifying each value.
/// Returns an iterator that allows modifying each value, excluding the default block.
#[deprecated(since = "7.0.0", note = "please use `.as_mut_slice()` instead")]
pub fn iter_mut(&mut self) -> IterMut<Block> {
self.table.iter_mut()
self.as_mut_slice().iter_mut()
}
/// Clears all entries in this jump table.
/// Clears all entries in this jump table, except for the default block.
pub fn clear(&mut self) {
self.table.clear();
self.table.drain(0..self.table.len() - 1);
}
}
impl Display for JumpTableData {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
write!(fmt, "[")?;
match self.table.first() {
None => (),
Some(first) => write!(fmt, "{}", first)?,
}
for block in self.table.iter().skip(1) {
write!(fmt, ", {}", block)?;
write!(fmt, "{}, [", self.default_block())?;
if let Some((first, rest)) = self.as_slice().split_first() {
write!(fmt, "{}", first)?;
for block in rest {
write!(fmt, ", {}", block)?;
}
}
write!(fmt, "]")
}
@@ -102,31 +107,33 @@ mod tests {
#[test]
fn empty() {
let jt = JumpTableData::new();
let def = Block::new(0);
let jt = JumpTableData::new(def, vec![]);
assert_eq!(jt.all_branches().get(0), Some(&def));
assert_eq!(jt.as_slice().get(0), None);
assert_eq!(jt.as_slice().get(10), None);
assert_eq!(jt.to_string(), "[]");
assert_eq!(jt.to_string(), "block0, []");
let v = jt.as_slice();
assert_eq!(v, []);
assert_eq!(jt.all_branches(), [def]);
assert_eq!(jt.as_slice(), []);
}
#[test]
fn insert() {
let def = Block::new(0);
let e1 = Block::new(1);
let e2 = Block::new(2);
let mut jt = JumpTableData::new();
let jt = JumpTableData::new(def, vec![e1, e2, e1]);
jt.push_entry(e1);
jt.push_entry(e2);
jt.push_entry(e1);
assert_eq!(jt.default_block(), def);
assert_eq!(jt.to_string(), "block0, [block1, block2, block1]");
assert_eq!(jt.to_string(), "[block1, block2, block1]");
let v = jt.as_slice();
assert_eq!(v, [e1, e2, e1]);
assert_eq!(jt.all_branches(), [e1, e2, e1, def]);
assert_eq!(jt.as_slice(), [e1, e2, e1]);
}
}