Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline a few utility functions around MIR #119459

Merged
merged 3 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_data_structures/src/graph/dominators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ impl<Node: Idx> Dominators<Node> {
/// # Panics
///
/// Panics if `b` is unreachable.
#[inline]
pub fn dominates(&self, a: Node, b: Node) -> bool {
match &self.kind {
Kind::Path => a.index() <= b.index(),
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_middle/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1568,6 +1568,7 @@ impl Location {
///
/// Note that if this location represents a terminator, then the
/// resulting location would be out of bounds and invalid.
#[inline]
pub fn successor_within_block(&self) -> Location {
Location { block: self.block, statement_index: self.statement_index + 1 }
}
Expand Down Expand Up @@ -1604,6 +1605,7 @@ impl Location {
false
}

#[inline]
pub fn dominates(&self, other: Location, dominators: &Dominators<BasicBlock>) -> bool {
if self.block == other.block {
self.statement_index <= other.statement_index
Expand All @@ -1623,6 +1625,7 @@ pub enum DefLocation {
}

impl DefLocation {
#[inline]
pub fn dominates(self, location: Location, dominators: &Dominators<BasicBlock>) -> bool {
match self {
DefLocation::Argument => true,
Expand Down
19 changes: 19 additions & 0 deletions compiler/rustc_middle/src/mir/terminator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ impl SwitchTargets {
}

/// Inverse of `SwitchTargets::static_if`.
#[inline]
pub fn as_static_if(&self) -> Option<(u128, BasicBlock, BasicBlock)> {
if let &[value] = &self.values[..]
&& let &[then, else_] = &self.targets[..]
Expand All @@ -37,6 +38,7 @@ impl SwitchTargets {
}

/// Returns the fallback target that is jumped to when none of the values match the operand.
#[inline]
pub fn otherwise(&self) -> BasicBlock {
*self.targets.last().unwrap()
}
Expand All @@ -47,22 +49,26 @@ impl SwitchTargets {
/// including the `otherwise` fallback target.
///
/// Note that this may yield 0 elements. Only the `otherwise` branch is mandatory.
#[inline]
pub fn iter(&self) -> SwitchTargetsIter<'_> {
SwitchTargetsIter { inner: iter::zip(&self.values, &self.targets) }
}

/// Returns a slice with all possible jump targets (including the fallback target).
#[inline]
pub fn all_targets(&self) -> &[BasicBlock] {
&self.targets
}

#[inline]
pub fn all_targets_mut(&mut self) -> &mut [BasicBlock] {
&mut self.targets
}

/// Finds the `BasicBlock` to which this `SwitchInt` will branch given the
/// specific value. This cannot fail, as it'll return the `otherwise`
/// branch if there's not a specific match for the value.
#[inline]
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())
}
Expand All @@ -75,10 +81,12 @@ pub struct SwitchTargetsIter<'a> {
impl<'a> Iterator for SwitchTargetsIter<'a> {
type Item = (u128, BasicBlock);

#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|(val, bb)| (*val, *bb))
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
Expand Down Expand Up @@ -330,28 +338,34 @@ pub type SuccessorsMut<'a> =
iter::Chain<std::option::IntoIter<&'a mut BasicBlock>, slice::IterMut<'a, BasicBlock>>;

impl<'tcx> Terminator<'tcx> {
#[inline]
pub fn successors(&self) -> Successors<'_> {
self.kind.successors()
}

#[inline]
pub fn successors_mut(&mut self) -> SuccessorsMut<'_> {
self.kind.successors_mut()
}

#[inline]
pub fn unwind(&self) -> Option<&UnwindAction> {
self.kind.unwind()
}

#[inline]
pub fn unwind_mut(&mut self) -> Option<&mut UnwindAction> {
self.kind.unwind_mut()
}
}

impl<'tcx> TerminatorKind<'tcx> {
#[inline]
pub fn if_(cond: Operand<'tcx>, t: BasicBlock, f: BasicBlock) -> TerminatorKind<'tcx> {
TerminatorKind::SwitchInt { discr: cond, targets: SwitchTargets::static_if(0, f, t) }
}

#[inline]
pub fn successors(&self) -> Successors<'_> {
use self::TerminatorKind::*;
match *self {
Expand Down Expand Up @@ -392,6 +406,7 @@ impl<'tcx> TerminatorKind<'tcx> {
}
}

#[inline]
pub fn successors_mut(&mut self) -> SuccessorsMut<'_> {
use self::TerminatorKind::*;
match *self {
Expand Down Expand Up @@ -430,6 +445,7 @@ impl<'tcx> TerminatorKind<'tcx> {
}
}

#[inline]
pub fn unwind(&self) -> Option<&UnwindAction> {
match *self {
TerminatorKind::Goto { .. }
Expand All @@ -449,6 +465,7 @@ impl<'tcx> TerminatorKind<'tcx> {
}
}

#[inline]
pub fn unwind_mut(&mut self) -> Option<&mut UnwindAction> {
match *self {
TerminatorKind::Goto { .. }
Expand All @@ -468,13 +485,15 @@ impl<'tcx> TerminatorKind<'tcx> {
}
}

#[inline]
pub fn as_switch(&self) -> Option<(&Operand<'tcx>, &SwitchTargets)> {
match self {
TerminatorKind::SwitchInt { discr, targets } => Some((discr, targets)),
_ => None,
}
}

#[inline]
pub fn as_goto(&self) -> Option<BasicBlock> {
match self {
TerminatorKind::Goto { target } => Some(*target),
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_mir_transform/src/ssa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ impl SsaLocals {
self.direct_uses[local]
}

#[inline]
pub fn assignment_dominates(
&self,
dominators: &Dominators<BasicBlock>,
Expand Down
Loading