Skip to content

Commit

Permalink
Use SmallVec in SwitchTargets
Browse files Browse the repository at this point in the history
This allows building common SwitchTargets (eg. for `if`s) without
allocation.
  • Loading branch information
jonas-schievink committed Oct 10, 2020
1 parent 432535d commit 9a47f74
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 15 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
mut bx: Bx,
discr: &mir::Operand<'tcx>,
switch_ty: Ty<'tcx>,
targets: &SwitchTargets<'tcx>,
targets: &SwitchTargets,
) {
let discr = self.codegen_operand(&mut bx, &discr);
// `switch_ty` is redundant, sanity-check that.
Expand Down
19 changes: 10 additions & 9 deletions compiler/rustc_middle/src/mir/terminator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::mir::interpret::Scalar;
use crate::ty::{self, Ty, TyCtxt};
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
use smallvec::{smallvec, SmallVec};

use super::{
AssertMessage, BasicBlock, InlineAsmOperand, Operand, Place, SourceInfo, Successors,
Expand All @@ -17,10 +18,10 @@ use std::slice;
pub use super::query::*;

#[derive(Debug, Clone, TyEncodable, TyDecodable, HashStable, PartialEq)]
pub struct SwitchTargets<'tcx> {
pub struct SwitchTargets {
/// Possible values. The locations to branch to in each case
/// are found in the corresponding indices from the `targets` vector.
values: Cow<'tcx, [u128]>,
values: SmallVec<[u128; 1]>,

/// Possible branch sites. The last element of this vector is used
/// for the otherwise branch, so targets.len() == values.len() + 1
Expand All @@ -34,24 +35,24 @@ pub struct SwitchTargets<'tcx> {
//
// However we’ve decided to keep this as-is until we figure a case
// where some other approach seems to be strictly better than other.
targets: Vec<BasicBlock>,
targets: SmallVec<[BasicBlock; 2]>,
}

impl<'tcx> SwitchTargets<'tcx> {
impl SwitchTargets {
/// Creates switch targets from an iterator of values and target blocks.
///
/// The iterator may be empty, in which case the `SwitchInt` instruction is equivalent to
/// `goto otherwise;`.
pub fn new(targets: impl Iterator<Item = (u128, BasicBlock)>, otherwise: BasicBlock) -> Self {
let (values, mut targets): (Vec<_>, Vec<_>) = targets.unzip();
let (values, mut targets): (SmallVec<_>, SmallVec<_>) = targets.unzip();
targets.push(otherwise);
Self { values: values.into(), targets }
}

/// Builds a switch targets definition that jumps to `then` if the tested value equals `value`,
/// and to `else_` if not.
pub fn static_if(value: &'static [u128; 1], then: BasicBlock, else_: BasicBlock) -> Self {
Self { values: Cow::Borrowed(value), targets: vec![then, else_] }
pub fn static_if(value: u128, then: BasicBlock, else_: BasicBlock) -> Self {
Self { values: smallvec![value], targets: smallvec![then, else_] }
}

/// Returns the fallback target that is jumped to when none of the values match the operand.
Expand Down Expand Up @@ -113,7 +114,7 @@ pub enum TerminatorKind<'tcx> {
/// FIXME: remove this redundant information. Currently, it is relied on by pretty-printing.
switch_ty: Ty<'tcx>,

targets: SwitchTargets<'tcx>,
targets: SwitchTargets,
},

/// Indicates that the landing pad is finished and unwinding should
Expand Down Expand Up @@ -295,7 +296,7 @@ impl<'tcx> TerminatorKind<'tcx> {
TerminatorKind::SwitchInt {
discr: cond,
switch_ty: tcx.types.bool,
targets: SwitchTargets::static_if(&[0], f, t),
targets: SwitchTargets::static_if(0, f, t),
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir/src/dataflow/framework/direction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ impl Direction for Forward {

struct SwitchIntEdgeEffectApplier<'a, D, F> {
exit_state: &'a mut D,
targets: &'a SwitchTargets<'a>,
targets: &'a SwitchTargets,
propagate: F,

effects_applied: bool,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,5 @@ struct OptimizationInfo<'tcx> {
/// Either Eq or Ne
op: BinOp,
/// Current targets used in the switch
targets: SwitchTargets<'tcx>,
targets: SwitchTargets,
}
4 changes: 1 addition & 3 deletions compiler/rustc_mir/src/util/elaborate_drops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,8 +760,6 @@ where
let elem_size = Place::from(self.new_temp(tcx.types.usize));
let len = Place::from(self.new_temp(tcx.types.usize));

static USIZE_SWITCH_ZERO: &[u128; 1] = &[0];

let base_block = BasicBlockData {
statements: vec![
self.assign(elem_size, Rvalue::NullaryOp(NullOp::SizeOf, ety)),
Expand All @@ -774,7 +772,7 @@ where
discr: move_(elem_size),
switch_ty: tcx.types.usize,
targets: SwitchTargets::static_if(
USIZE_SWITCH_ZERO,
0,
self.drop_loop_pair(ety, false, len),
self.drop_loop_pair(ety, true, len),
),
Expand Down

0 comments on commit 9a47f74

Please sign in to comment.