2019-10-20 23:48:31 -04:00
|
|
|
//! A pass that eliminates branches on uninhabited enum variants.
|
|
|
|
|
2021-01-01 01:53:25 +01:00
|
|
|
use crate::MirPass;
|
2022-07-08 18:06:18 +02:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::{
|
2021-12-11 21:32:08 -05:00
|
|
|
BasicBlockData, Body, Local, Operand, Rvalue, StatementKind, SwitchTargets, Terminator,
|
|
|
|
TerminatorKind,
|
2019-10-20 23:48:31 -04:00
|
|
|
};
|
2020-03-31 18:16:47 +02:00
|
|
|
use rustc_middle::ty::layout::TyAndLayout;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty::{Ty, TyCtxt};
|
2020-03-31 18:16:47 +02:00
|
|
|
use rustc_target::abi::{Abi, Variants};
|
2019-10-20 23:48:31 -04:00
|
|
|
|
|
|
|
pub struct UninhabitedEnumBranching;
|
|
|
|
|
|
|
|
fn get_discriminant_local(terminator: &TerminatorKind<'_>) -> Option<Local> {
|
|
|
|
if let TerminatorKind::SwitchInt { discr: Operand::Move(p), .. } = terminator {
|
|
|
|
p.as_local()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If the basic block terminates by switching on a discriminant, this returns the `Ty` the
|
|
|
|
/// discriminant is read from. Otherwise, returns None.
|
|
|
|
fn get_switched_on_type<'tcx>(
|
|
|
|
block_data: &BasicBlockData<'tcx>,
|
2021-05-23 16:12:01 +02:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-10-20 23:48:31 -04:00
|
|
|
body: &Body<'tcx>,
|
|
|
|
) -> Option<Ty<'tcx>> {
|
|
|
|
let terminator = block_data.terminator();
|
|
|
|
|
|
|
|
// Only bother checking blocks which terminate by switching on a local.
|
2023-10-09 05:22:31 +02:00
|
|
|
if let Some(local) = get_discriminant_local(&terminator.kind)
|
|
|
|
&& let [.., stmt_before_term] = &block_data.statements[..]
|
|
|
|
&& let StatementKind::Assign(box (l, Rvalue::Discriminant(place))) = stmt_before_term.kind
|
|
|
|
&& l.as_local() == Some(local)
|
|
|
|
&& let ty = place.ty(body, tcx).ty
|
|
|
|
&& ty.is_enum()
|
|
|
|
{
|
|
|
|
Some(ty)
|
|
|
|
} else {
|
|
|
|
None
|
2019-10-20 23:48:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn variant_discriminants<'tcx>(
|
2020-03-04 14:50:21 +00:00
|
|
|
layout: &TyAndLayout<'tcx>,
|
2019-10-20 23:48:31 -04:00
|
|
|
ty: Ty<'tcx>,
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-10-05 23:58:46 +02:00
|
|
|
) -> FxHashSet<u128> {
|
2020-03-04 14:13:00 +00:00
|
|
|
match &layout.variants {
|
2020-10-05 23:58:46 +02:00
|
|
|
Variants::Single { index } => {
|
|
|
|
let mut res = FxHashSet::default();
|
2021-10-11 00:00:00 +00:00
|
|
|
res.insert(
|
|
|
|
ty.discriminant_for_variant(tcx, *index)
|
|
|
|
.map_or(index.as_u32() as u128, |discr| discr.val),
|
|
|
|
);
|
2020-10-05 23:58:46 +02:00
|
|
|
res
|
|
|
|
}
|
2019-10-20 23:48:31 -04:00
|
|
|
Variants::Multiple { variants, .. } => variants
|
|
|
|
.iter_enumerated()
|
|
|
|
.filter_map(|(idx, layout)| {
|
2022-11-07 00:36:11 +03:30
|
|
|
(layout.abi != Abi::Uninhabited)
|
2019-12-06 13:09:03 +00:00
|
|
|
.then(|| ty.discriminant_for_variant(tcx, idx).unwrap().val)
|
2019-10-20 23:48:31 -04:00
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:32:08 -05:00
|
|
|
/// Ensures that the `otherwise` branch leads to an unreachable bb, returning `None` if so and a new
|
|
|
|
/// bb to use as the new target if not.
|
|
|
|
fn ensure_otherwise_unreachable<'tcx>(
|
|
|
|
body: &Body<'tcx>,
|
|
|
|
targets: &SwitchTargets,
|
|
|
|
) -> Option<BasicBlockData<'tcx>> {
|
|
|
|
let otherwise = targets.otherwise();
|
2022-07-05 00:00:00 +00:00
|
|
|
let bb = &body.basic_blocks[otherwise];
|
2021-12-11 21:32:08 -05:00
|
|
|
if bb.terminator().kind == TerminatorKind::Unreachable
|
|
|
|
&& bb.statements.iter().all(|s| matches!(&s.kind, StatementKind::StorageDead(_)))
|
|
|
|
{
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut new_block = BasicBlockData::new(Some(Terminator {
|
|
|
|
source_info: bb.terminator().source_info,
|
|
|
|
kind: TerminatorKind::Unreachable,
|
|
|
|
}));
|
|
|
|
new_block.is_cleanup = bb.is_cleanup;
|
|
|
|
Some(new_block)
|
|
|
|
}
|
|
|
|
|
2019-10-20 23:48:31 -04:00
|
|
|
impl<'tcx> MirPass<'tcx> for UninhabitedEnumBranching {
|
2021-12-02 09:17:32 -08:00
|
|
|
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
|
|
|
|
sess.mir_opt_level() > 0
|
|
|
|
}
|
|
|
|
|
2020-10-04 11:01:38 -07:00
|
|
|
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
|
|
|
trace!("UninhabitedEnumBranching starting for {:?}", body.source);
|
2019-10-20 23:48:31 -04:00
|
|
|
|
2022-07-05 00:00:00 +00:00
|
|
|
for bb in body.basic_blocks.indices() {
|
2019-10-20 23:48:31 -04:00
|
|
|
trace!("processing block {:?}", bb);
|
|
|
|
|
2022-07-05 00:00:00 +00:00
|
|
|
let Some(discriminant_ty) = get_switched_on_type(&body.basic_blocks[bb], tcx, body)
|
|
|
|
else {
|
2021-10-16 03:45:14 +02:00
|
|
|
continue;
|
|
|
|
};
|
2019-10-20 23:48:31 -04:00
|
|
|
|
2023-04-15 08:34:05 +00:00
|
|
|
let layout = tcx.layout_of(
|
|
|
|
tcx.param_env_reveal_all_normalized(body.source.def_id()).and(discriminant_ty),
|
|
|
|
);
|
2019-10-20 23:48:31 -04:00
|
|
|
|
|
|
|
let allowed_variants = if let Ok(layout) = layout {
|
|
|
|
variant_discriminants(&layout, discriminant_ty, tcx)
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
trace!("allowed_variants = {:?}", allowed_variants);
|
|
|
|
|
2020-10-10 17:36:04 +02:00
|
|
|
if let TerminatorKind::SwitchInt { targets, .. } =
|
2019-10-20 23:48:31 -04:00
|
|
|
&mut body.basic_blocks_mut()[bb].terminator_mut().kind
|
|
|
|
{
|
2021-12-11 21:32:08 -05:00
|
|
|
let mut new_targets = SwitchTargets::new(
|
2020-10-10 17:36:04 +02:00
|
|
|
targets.iter().filter(|(val, _)| allowed_variants.contains(val)),
|
|
|
|
targets.otherwise(),
|
|
|
|
);
|
|
|
|
|
2021-12-11 21:32:08 -05:00
|
|
|
if new_targets.iter().count() == allowed_variants.len() {
|
|
|
|
if let Some(updated) = ensure_otherwise_unreachable(body, &new_targets) {
|
|
|
|
let new_otherwise = body.basic_blocks_mut().push(updated);
|
|
|
|
*new_targets.all_targets_mut().last_mut().unwrap() = new_otherwise;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let TerminatorKind::SwitchInt { targets, .. } =
|
|
|
|
&mut body.basic_blocks_mut()[bb].terminator_mut().kind
|
|
|
|
{
|
|
|
|
*targets = new_targets;
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
}
|
2019-10-20 23:48:31 -04:00
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|