Move the default block to the front of the underlying jump table storage (#5770)

The new api on JumpTableData makese it easy to keep the default label first, and that shrinks the diff in #5731 a bit.
This commit is contained in:
Trevor Elliott
2023-02-13 12:50:29 -08:00
committed by GitHub
parent 6cddc923f3
commit 19f337e29b
8 changed files with 26 additions and 28 deletions

View File

@@ -15,11 +15,10 @@ use serde::{Deserialize, Serialize};
///
/// 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.
/// The default block for the jump table is stored as the first element of the underlying vector.
/// 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 first.
#[derive(Clone, PartialEq, Hash)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub struct JumpTableData {
@@ -29,42 +28,41 @@ pub struct JumpTableData {
impl JumpTableData {
/// Create a new jump table with the provided blocks
pub fn new(def: Block, mut table: Vec<Block>) -> Self {
table.push(def);
Self { table }
pub fn new(def: Block, table: &[Block]) -> Self {
Self {
table: std::iter::once(def).chain(table.iter().copied()).collect(),
}
}
/// Fetch the default block for this jump table.
pub fn default_block(&self) -> Block {
*self.table.last().unwrap()
*self.table.first().unwrap()
}
/// Mutable access to the default block of this jump table.
pub fn default_block_mut(&mut self) -> &mut Block {
self.table.last_mut().unwrap()
self.table.first_mut().unwrap()
}
/// The jump table and default block as a single slice. The default block will always be last.
/// The jump table and default block as a single slice. The default block will always be first.
pub fn all_branches(&self) -> &[Block] {
self.table.as_slice()
}
/// The jump table and default block as a single mutable slice. The default block will always
/// be last.
/// be first.
pub fn all_branches_mut(&mut self) -> &mut [Block] {
self.table.as_mut_slice()
}
/// 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]
&self.table.as_slice()[1..]
}
/// 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]
&mut self.table.as_mut_slice()[1..]
}
/// Returns an iterator to the jump table, excluding the default block.
@@ -81,7 +79,7 @@ impl JumpTableData {
/// Clears all entries in this jump table, except for the default block.
pub fn clear(&mut self) {
self.table.drain(0..self.table.len() - 1);
self.table.drain(1..);
}
}
@@ -109,7 +107,7 @@ mod tests {
fn empty() {
let def = Block::new(0);
let jt = JumpTableData::new(def, vec![]);
let jt = JumpTableData::new(def, &[]);
assert_eq!(jt.all_branches().get(0), Some(&def));
@@ -128,12 +126,12 @@ mod tests {
let e1 = Block::new(1);
let e2 = Block::new(2);
let jt = JumpTableData::new(def, vec![e1, e2, e1]);
let jt = JumpTableData::new(def, &[e1, e2, e1]);
assert_eq!(jt.default_block(), def);
assert_eq!(jt.to_string(), "block0, [block1, block2, block1]");
assert_eq!(jt.all_branches(), [e1, e2, e1, def]);
assert_eq!(jt.all_branches(), [def, e1, e2, e1]);
assert_eq!(jt.as_slice(), [e1, e2, e1]);
}
}

View File

@@ -363,7 +363,7 @@ mod test {
pos.insert_block(bb0);
let jt = pos
.func
.create_jump_table(JumpTableData::new(bb3, vec![bb1, bb2]));
.create_jump_table(JumpTableData::new(bb3, &[bb1, bb2]));
pos.ins().br_table(arg0, jt);
pos.insert_block(bb1);

View File

@@ -410,7 +410,7 @@ mod test {
pos.insert_block(bb0);
let jt = pos
.func
.create_jump_table(JumpTableData::new(bb3, vec![bb1, bb2]));
.create_jump_table(JumpTableData::new(bb3, &[bb1, bb2]));
pos.ins().br_table(arg0, jt);
pos.insert_block(bb1);