1
Fork 0

Auto merge of #119459 - cjgillot:inline-mir-utils, r=compiler-errors

Inline a few utility functions around MIR

Most of them are small enough to benefit from inlining.
This commit is contained in:
bors 2024-01-06 04:01:09 +00:00
commit d62f05b842
4 changed files with 24 additions and 0 deletions

View file

@ -394,6 +394,7 @@ impl<Node: Idx> Dominators<Node> {
/// # Panics /// # Panics
/// ///
/// Panics if `b` is unreachable. /// Panics if `b` is unreachable.
#[inline]
pub fn dominates(&self, a: Node, b: Node) -> bool { pub fn dominates(&self, a: Node, b: Node) -> bool {
match &self.kind { match &self.kind {
Kind::Path => a.index() <= b.index(), Kind::Path => a.index() <= b.index(),

View file

@ -1576,6 +1576,7 @@ impl Location {
/// ///
/// Note that if this location represents a terminator, then the /// Note that if this location represents a terminator, then the
/// resulting location would be out of bounds and invalid. /// resulting location would be out of bounds and invalid.
#[inline]
pub fn successor_within_block(&self) -> Location { pub fn successor_within_block(&self) -> Location {
Location { block: self.block, statement_index: self.statement_index + 1 } Location { block: self.block, statement_index: self.statement_index + 1 }
} }
@ -1612,6 +1613,7 @@ impl Location {
false false
} }
#[inline]
pub fn dominates(&self, other: Location, dominators: &Dominators<BasicBlock>) -> bool { pub fn dominates(&self, other: Location, dominators: &Dominators<BasicBlock>) -> bool {
if self.block == other.block { if self.block == other.block {
self.statement_index <= other.statement_index self.statement_index <= other.statement_index
@ -1631,6 +1633,7 @@ pub enum DefLocation {
} }
impl DefLocation { impl DefLocation {
#[inline]
pub fn dominates(self, location: Location, dominators: &Dominators<BasicBlock>) -> bool { pub fn dominates(self, location: Location, dominators: &Dominators<BasicBlock>) -> bool {
match self { match self {
DefLocation::Argument => true, DefLocation::Argument => true,

View file

@ -26,6 +26,7 @@ impl SwitchTargets {
} }
/// Inverse of `SwitchTargets::static_if`. /// Inverse of `SwitchTargets::static_if`.
#[inline]
pub fn as_static_if(&self) -> Option<(u128, BasicBlock, BasicBlock)> { pub fn as_static_if(&self) -> Option<(u128, BasicBlock, BasicBlock)> {
if let &[value] = &self.values[..] if let &[value] = &self.values[..]
&& let &[then, else_] = &self.targets[..] && let &[then, else_] = &self.targets[..]
@ -37,6 +38,7 @@ impl SwitchTargets {
} }
/// Returns the fallback target that is jumped to when none of the values match the operand. /// Returns the fallback target that is jumped to when none of the values match the operand.
#[inline]
pub fn otherwise(&self) -> BasicBlock { pub fn otherwise(&self) -> BasicBlock {
*self.targets.last().unwrap() *self.targets.last().unwrap()
} }
@ -47,15 +49,18 @@ impl SwitchTargets {
/// including the `otherwise` fallback target. /// including the `otherwise` fallback target.
/// ///
/// Note that this may yield 0 elements. Only the `otherwise` branch is mandatory. /// Note that this may yield 0 elements. Only the `otherwise` branch is mandatory.
#[inline]
pub fn iter(&self) -> SwitchTargetsIter<'_> { pub fn iter(&self) -> SwitchTargetsIter<'_> {
SwitchTargetsIter { inner: iter::zip(&self.values, &self.targets) } SwitchTargetsIter { inner: iter::zip(&self.values, &self.targets) }
} }
/// Returns a slice with all possible jump targets (including the fallback target). /// Returns a slice with all possible jump targets (including the fallback target).
#[inline]
pub fn all_targets(&self) -> &[BasicBlock] { pub fn all_targets(&self) -> &[BasicBlock] {
&self.targets &self.targets
} }
#[inline]
pub fn all_targets_mut(&mut self) -> &mut [BasicBlock] { pub fn all_targets_mut(&mut self) -> &mut [BasicBlock] {
&mut self.targets &mut self.targets
} }
@ -63,6 +68,7 @@ impl SwitchTargets {
/// Finds the `BasicBlock` to which this `SwitchInt` will branch given the /// Finds the `BasicBlock` to which this `SwitchInt` will branch given the
/// specific value. This cannot fail, as it'll return the `otherwise` /// specific value. This cannot fail, as it'll return the `otherwise`
/// branch if there's not a specific match for the value. /// branch if there's not a specific match for the value.
#[inline]
pub fn target_for_value(&self, value: u128) -> BasicBlock { pub fn target_for_value(&self, value: u128) -> BasicBlock {
self.iter().find_map(|(v, t)| (v == value).then_some(t)).unwrap_or_else(|| self.otherwise()) self.iter().find_map(|(v, t)| (v == value).then_some(t)).unwrap_or_else(|| self.otherwise())
} }
@ -75,10 +81,12 @@ pub struct SwitchTargetsIter<'a> {
impl<'a> Iterator for SwitchTargetsIter<'a> { impl<'a> Iterator for SwitchTargetsIter<'a> {
type Item = (u128, BasicBlock); type Item = (u128, BasicBlock);
#[inline]
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|(val, bb)| (*val, *bb)) self.inner.next().map(|(val, bb)| (*val, *bb))
} }
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) { fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint() self.inner.size_hint()
} }
@ -330,28 +338,34 @@ pub type SuccessorsMut<'a> =
iter::Chain<std::option::IntoIter<&'a mut BasicBlock>, slice::IterMut<'a, BasicBlock>>; iter::Chain<std::option::IntoIter<&'a mut BasicBlock>, slice::IterMut<'a, BasicBlock>>;
impl<'tcx> Terminator<'tcx> { impl<'tcx> Terminator<'tcx> {
#[inline]
pub fn successors(&self) -> Successors<'_> { pub fn successors(&self) -> Successors<'_> {
self.kind.successors() self.kind.successors()
} }
#[inline]
pub fn successors_mut(&mut self) -> SuccessorsMut<'_> { pub fn successors_mut(&mut self) -> SuccessorsMut<'_> {
self.kind.successors_mut() self.kind.successors_mut()
} }
#[inline]
pub fn unwind(&self) -> Option<&UnwindAction> { pub fn unwind(&self) -> Option<&UnwindAction> {
self.kind.unwind() self.kind.unwind()
} }
#[inline]
pub fn unwind_mut(&mut self) -> Option<&mut UnwindAction> { pub fn unwind_mut(&mut self) -> Option<&mut UnwindAction> {
self.kind.unwind_mut() self.kind.unwind_mut()
} }
} }
impl<'tcx> TerminatorKind<'tcx> { impl<'tcx> TerminatorKind<'tcx> {
#[inline]
pub fn if_(cond: Operand<'tcx>, t: BasicBlock, f: BasicBlock) -> TerminatorKind<'tcx> { pub fn if_(cond: Operand<'tcx>, t: BasicBlock, f: BasicBlock) -> TerminatorKind<'tcx> {
TerminatorKind::SwitchInt { discr: cond, targets: SwitchTargets::static_if(0, f, t) } TerminatorKind::SwitchInt { discr: cond, targets: SwitchTargets::static_if(0, f, t) }
} }
#[inline]
pub fn successors(&self) -> Successors<'_> { pub fn successors(&self) -> Successors<'_> {
use self::TerminatorKind::*; use self::TerminatorKind::*;
match *self { match *self {
@ -392,6 +406,7 @@ impl<'tcx> TerminatorKind<'tcx> {
} }
} }
#[inline]
pub fn successors_mut(&mut self) -> SuccessorsMut<'_> { pub fn successors_mut(&mut self) -> SuccessorsMut<'_> {
use self::TerminatorKind::*; use self::TerminatorKind::*;
match *self { match *self {
@ -430,6 +445,7 @@ impl<'tcx> TerminatorKind<'tcx> {
} }
} }
#[inline]
pub fn unwind(&self) -> Option<&UnwindAction> { pub fn unwind(&self) -> Option<&UnwindAction> {
match *self { match *self {
TerminatorKind::Goto { .. } TerminatorKind::Goto { .. }
@ -449,6 +465,7 @@ impl<'tcx> TerminatorKind<'tcx> {
} }
} }
#[inline]
pub fn unwind_mut(&mut self) -> Option<&mut UnwindAction> { pub fn unwind_mut(&mut self) -> Option<&mut UnwindAction> {
match *self { match *self {
TerminatorKind::Goto { .. } TerminatorKind::Goto { .. }
@ -468,6 +485,7 @@ impl<'tcx> TerminatorKind<'tcx> {
} }
} }
#[inline]
pub fn as_switch(&self) -> Option<(&Operand<'tcx>, &SwitchTargets)> { pub fn as_switch(&self) -> Option<(&Operand<'tcx>, &SwitchTargets)> {
match self { match self {
TerminatorKind::SwitchInt { discr, targets } => Some((discr, targets)), TerminatorKind::SwitchInt { discr, targets } => Some((discr, targets)),
@ -475,6 +493,7 @@ impl<'tcx> TerminatorKind<'tcx> {
} }
} }
#[inline]
pub fn as_goto(&self) -> Option<BasicBlock> { pub fn as_goto(&self) -> Option<BasicBlock> {
match self { match self {
TerminatorKind::Goto { target } => Some(*target), TerminatorKind::Goto { target } => Some(*target),

View file

@ -94,6 +94,7 @@ impl SsaLocals {
self.direct_uses[local] self.direct_uses[local]
} }
#[inline]
pub fn assignment_dominates( pub fn assignment_dominates(
&self, &self,
dominators: &Dominators<BasicBlock>, dominators: &Dominators<BasicBlock>,