2019-10-20 23:48:31 -04:00
|
|
|
//! A pass that eliminates branches on uninhabited enum variants.
|
|
|
|
|
2020-10-04 11:01:38 -07:00
|
|
|
use crate::transform::MirPass;
|
2020-10-05 23:58:46 +02:00
|
|
|
use rustc_data_structures::stable_set::FxHashSet;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::{
|
2020-10-10 17:36:04 +02:00
|
|
|
BasicBlock, BasicBlockData, Body, Local, Operand, Rvalue, StatementKind, SwitchTargets,
|
|
|
|
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.
|
|
|
|
if let Some(local) = get_discriminant_local(&terminator.kind) {
|
2020-02-28 14:20:33 +01:00
|
|
|
let stmt_before_term = (!block_data.statements.is_empty())
|
2019-12-06 13:09:03 +00:00
|
|
|
.then(|| &block_data.statements[block_data.statements.len() - 1].kind);
|
2019-10-20 23:48:31 -04:00
|
|
|
|
|
|
|
if let Some(StatementKind::Assign(box (l, Rvalue::Discriminant(place)))) = stmt_before_term
|
|
|
|
{
|
|
|
|
if l.as_local() == Some(local) {
|
2021-05-23 16:12:01 +02:00
|
|
|
let ty = place.ty(body, tcx).ty;
|
|
|
|
if ty.is_enum() {
|
|
|
|
return Some(ty);
|
2019-10-20 23:48:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
res.insert(index.as_u32() as u128);
|
|
|
|
res
|
|
|
|
}
|
2019-10-20 23:48:31 -04:00
|
|
|
Variants::Multiple { variants, .. } => variants
|
|
|
|
.iter_enumerated()
|
|
|
|
.filter_map(|(idx, layout)| {
|
|
|
|
(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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> MirPass<'tcx> for UninhabitedEnumBranching {
|
2020-10-04 11:01:38 -07:00
|
|
|
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
|
|
|
if body.source.promoted.is_some() {
|
2019-10-20 23:48:31 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-04 11:01:38 -07:00
|
|
|
trace!("UninhabitedEnumBranching starting for {:?}", body.source);
|
2019-10-20 23:48:31 -04:00
|
|
|
|
|
|
|
let basic_block_count = body.basic_blocks().len();
|
|
|
|
|
|
|
|
for bb in 0..basic_block_count {
|
|
|
|
let bb = BasicBlock::from_usize(bb);
|
|
|
|
trace!("processing block {:?}", bb);
|
|
|
|
|
|
|
|
let discriminant_ty =
|
2021-05-23 16:12:01 +02:00
|
|
|
if let Some(ty) = get_switched_on_type(&body.basic_blocks()[bb], tcx, body) {
|
2019-10-20 23:48:31 -04:00
|
|
|
ty
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
2020-10-04 11:01:38 -07:00
|
|
|
let layout = tcx.layout_of(tcx.param_env(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
|
|
|
|
{
|
2020-10-10 17:36:04 +02:00
|
|
|
let new_targets = SwitchTargets::new(
|
|
|
|
targets.iter().filter(|(val, _)| allowed_variants.contains(val)),
|
|
|
|
targets.otherwise(),
|
|
|
|
);
|
|
|
|
|
|
|
|
*targets = new_targets;
|
2019-10-20 23:48:31 -04:00
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|