2020-01-10 13:15:16 -06:00
|
|
|
//! A pass that propagates the unreachable terminator of a block to its predecessors
|
|
|
|
//! when all of their successors are unreachable. This is achieved through a
|
|
|
|
//! post-order traversal of the blocks.
|
|
|
|
|
2020-12-31 18:53:25 -06:00
|
|
|
use crate::simplify;
|
|
|
|
use crate::MirPass;
|
2020-03-29 10:19:48 -05:00
|
|
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
2020-03-29 09:41:09 -05:00
|
|
|
use rustc_middle::mir::*;
|
|
|
|
use rustc_middle::ty::TyCtxt;
|
2020-01-10 13:15:16 -06:00
|
|
|
|
|
|
|
pub struct UnreachablePropagation;
|
|
|
|
|
|
|
|
impl MirPass<'_> for UnreachablePropagation {
|
2021-12-02 11:17:32 -06:00
|
|
|
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
|
2022-07-26 08:15:07 -05:00
|
|
|
// Enable only under -Zmir-opt-level=2 as this can make programs less debuggable.
|
|
|
|
sess.mir_opt_level() >= 2
|
2021-12-02 11:17:32 -06:00
|
|
|
}
|
2020-01-10 13:15:16 -06:00
|
|
|
|
2021-12-02 11:17:32 -06:00
|
|
|
fn run_pass<'tcx>(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
2020-01-10 13:15:16 -06:00
|
|
|
let mut unreachable_blocks = FxHashSet::default();
|
|
|
|
let mut replacements = FxHashMap::default();
|
|
|
|
|
|
|
|
for (bb, bb_data) in traversal::postorder(body) {
|
|
|
|
let terminator = bb_data.terminator();
|
2022-01-11 18:00:00 -06:00
|
|
|
if terminator.kind == TerminatorKind::Unreachable {
|
2020-01-10 13:15:16 -06:00
|
|
|
unreachable_blocks.insert(bb);
|
|
|
|
} else {
|
|
|
|
let is_unreachable = |succ: BasicBlock| unreachable_blocks.contains(&succ);
|
|
|
|
let terminator_kind_opt = remove_successors(&terminator.kind, is_unreachable);
|
|
|
|
|
|
|
|
if let Some(terminator_kind) = terminator_kind_opt {
|
2022-01-11 18:00:00 -06:00
|
|
|
if terminator_kind == TerminatorKind::Unreachable {
|
2020-01-10 13:15:16 -06:00
|
|
|
unreachable_blocks.insert(bb);
|
|
|
|
}
|
|
|
|
replacements.insert(bb, terminator_kind);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-26 08:11:12 -05:00
|
|
|
// We do want do keep some unreachable blocks, but make them empty.
|
|
|
|
for bb in unreachable_blocks {
|
|
|
|
if !tcx.consider_optimizing(|| {
|
|
|
|
format!("UnreachablePropagation {:?} ", body.source.def_id())
|
|
|
|
}) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
body.basic_blocks_mut()[bb].statements.clear();
|
|
|
|
}
|
|
|
|
|
2020-01-10 13:15:16 -06:00
|
|
|
let replaced = !replacements.is_empty();
|
2022-07-26 08:11:12 -05:00
|
|
|
|
2020-01-10 13:15:16 -06:00
|
|
|
for (bb, terminator_kind) in replacements {
|
2020-11-18 18:16:23 -06:00
|
|
|
if !tcx.consider_optimizing(|| {
|
|
|
|
format!("UnreachablePropagation {:?} ", body.source.def_id())
|
|
|
|
}) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-01-10 13:15:16 -06:00
|
|
|
body.basic_blocks_mut()[bb].terminator_mut().kind = terminator_kind;
|
|
|
|
}
|
|
|
|
|
|
|
|
if replaced {
|
2021-05-01 16:56:48 -05:00
|
|
|
simplify::remove_dead_blocks(tcx, body);
|
2020-01-10 13:15:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 02:48:37 -06:00
|
|
|
fn remove_successors<'tcx, F>(
|
2020-01-10 13:15:16 -06:00
|
|
|
terminator_kind: &TerminatorKind<'tcx>,
|
2022-07-26 08:11:12 -05:00
|
|
|
is_unreachable: F,
|
2020-01-10 13:15:16 -06:00
|
|
|
) -> Option<TerminatorKind<'tcx>>
|
|
|
|
where
|
|
|
|
F: Fn(BasicBlock) -> bool,
|
|
|
|
{
|
2022-07-26 08:11:12 -05:00
|
|
|
let terminator = match terminator_kind {
|
|
|
|
// This will unconditionally run into an unreachable and is therefore unreachable as well.
|
|
|
|
TerminatorKind::Goto { target } if is_unreachable(*target) => TerminatorKind::Unreachable,
|
|
|
|
TerminatorKind::SwitchInt { targets, discr, switch_ty } => {
|
2020-10-10 10:36:04 -05:00
|
|
|
let otherwise = targets.otherwise();
|
|
|
|
|
2022-07-26 08:11:12 -05:00
|
|
|
// If all targets are unreachable, we can be unreachable as well.
|
|
|
|
if targets.all_targets().iter().all(|bb| is_unreachable(*bb)) {
|
|
|
|
TerminatorKind::Unreachable
|
|
|
|
} else if is_unreachable(otherwise) {
|
|
|
|
// If there are multiple targets, don't delete unreachable branches (like an unreachable otherwise)
|
|
|
|
// unless otherwise is unrachable, in which case deleting a normal branch causes it to be merged with
|
|
|
|
// the otherwise, keeping its unreachable.
|
|
|
|
// This looses information about reachability causing worse codegen.
|
|
|
|
// For example (see src/test/codegen/match-optimizes-away.rs)
|
|
|
|
//
|
|
|
|
// pub enum Two { A, B }
|
|
|
|
// pub fn identity(x: Two) -> Two {
|
|
|
|
// match x {
|
|
|
|
// Two::A => Two::A,
|
|
|
|
// Two::B => Two::B,
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// This generates a `switchInt() -> [0: 0, 1: 1, otherwise: unreachable]`, which allows us or LLVM to
|
|
|
|
// turn it into just `x` later. Without the unreachable, such a transformation would be illegal.
|
|
|
|
// If the otherwise branch is unreachable, we can delete all other unreacahble targets, as they will
|
|
|
|
// still point to the unreachable and therefore not lose reachability information.
|
|
|
|
let reachable_iter = targets.iter().filter(|(_, bb)| !is_unreachable(*bb));
|
2020-01-10 13:15:16 -06:00
|
|
|
|
2022-07-26 08:11:12 -05:00
|
|
|
let new_targets = SwitchTargets::new(reachable_iter, otherwise);
|
2020-01-10 13:15:16 -06:00
|
|
|
|
2022-07-26 08:11:12 -05:00
|
|
|
// No unreachable branches were removed.
|
|
|
|
if new_targets.all_targets().len() == targets.all_targets().len() {
|
|
|
|
return None;
|
|
|
|
}
|
2020-01-10 13:15:16 -06:00
|
|
|
|
2020-08-13 16:59:47 -05:00
|
|
|
TerminatorKind::SwitchInt {
|
2020-01-10 13:15:16 -06:00
|
|
|
discr: discr.clone(),
|
2022-07-26 08:11:12 -05:00
|
|
|
switch_ty: *switch_ty,
|
|
|
|
targets: new_targets,
|
2020-08-13 16:59:47 -05:00
|
|
|
}
|
2020-01-10 13:15:16 -06:00
|
|
|
} else {
|
2022-07-26 08:11:12 -05:00
|
|
|
// If the otherwise branch is reachable, we don't want to delete any unreachable branches.
|
2020-08-13 16:59:47 -05:00
|
|
|
return None;
|
2020-01-10 13:15:16 -06:00
|
|
|
}
|
|
|
|
}
|
2020-08-13 16:59:47 -05:00
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
Some(terminator)
|
2020-01-10 13:15:16 -06:00
|
|
|
}
|