2016-06-08 21:10:15 +00:00
|
|
|
//! A pass that simplifies branches when their condition is known.
|
|
|
|
|
2020-10-04 18:01:38 +00:00
|
|
|
use crate::transform::MirPass;
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::mir::*;
|
|
|
|
use rustc_middle::ty::TyCtxt;
|
2016-06-08 21:10:15 +00:00
|
|
|
|
2017-04-25 22:23:33 +00:00
|
|
|
use std::borrow::Cow;
|
2016-06-08 21:10:15 +00:00
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
pub struct SimplifyBranches {
|
|
|
|
label: String,
|
|
|
|
}
|
2016-06-08 21:10:15 +00:00
|
|
|
|
2017-04-25 22:23:33 +00:00
|
|
|
impl SimplifyBranches {
|
|
|
|
pub fn new(label: &str) -> Self {
|
|
|
|
SimplifyBranches { label: format!("SimplifyBranches-{}", label) }
|
2016-06-08 21:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-04 20:20:00 +00:00
|
|
|
impl<'tcx> MirPass<'tcx> for SimplifyBranches {
|
2019-06-21 16:12:39 +00:00
|
|
|
fn name(&self) -> Cow<'_, str> {
|
2017-04-25 22:23:33 +00:00
|
|
|
Cow::Borrowed(&self.label)
|
|
|
|
}
|
|
|
|
|
2020-10-04 18:01:38 +00:00
|
|
|
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
|
|
|
let param_env = tcx.param_env(body.source.def_id());
|
2019-11-06 17:00:46 +00:00
|
|
|
for block in body.basic_blocks_mut() {
|
2019-10-04 04:55:28 +00:00
|
|
|
let terminator = block.terminator_mut();
|
2016-06-08 21:10:15 +00:00
|
|
|
terminator.kind = match terminator.kind {
|
2018-07-21 23:01:07 +00:00
|
|
|
TerminatorKind::SwitchInt {
|
2019-12-22 22:42:04 +00:00
|
|
|
discr: Operand::Constant(ref c),
|
|
|
|
switch_ty,
|
|
|
|
ref targets,
|
|
|
|
..
|
2018-07-21 23:01:07 +00:00
|
|
|
} => {
|
2019-03-31 16:35:39 +00:00
|
|
|
let constant = c.literal.try_eval_bits(tcx, param_env, switch_ty);
|
2018-12-13 10:15:18 +00:00
|
|
|
if let Some(constant) = constant {
|
2020-10-10 15:36:04 +00:00
|
|
|
let otherwise = targets.otherwise();
|
|
|
|
let mut ret = TerminatorKind::Goto { target: otherwise };
|
|
|
|
for (v, t) in targets.iter() {
|
2018-12-13 10:15:18 +00:00
|
|
|
if v == constant {
|
2020-10-10 15:36:04 +00:00
|
|
|
ret = TerminatorKind::Goto { target: t };
|
2018-12-13 10:15:18 +00:00
|
|
|
break;
|
2017-02-02 06:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-13 10:15:18 +00:00
|
|
|
ret
|
2017-02-02 06:41:01 +00:00
|
|
|
} else {
|
2019-12-22 22:42:04 +00:00
|
|
|
continue;
|
2017-02-02 06:41:01 +00:00
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
2018-07-21 23:01:07 +00:00
|
|
|
TerminatorKind::Assert {
|
|
|
|
target, cond: Operand::Constant(ref c), expected, ..
|
2019-12-22 22:42:04 +00:00
|
|
|
} if (c.literal.try_eval_bool(tcx, param_env) == Some(true)) == expected => {
|
|
|
|
TerminatorKind::Goto { target }
|
|
|
|
}
|
2020-06-02 07:15:24 +00:00
|
|
|
TerminatorKind::FalseEdge { real_target, .. } => {
|
2017-10-13 13:36:15 +00:00
|
|
|
TerminatorKind::Goto { target: real_target }
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
2018-01-25 06:45:45 +00:00
|
|
|
TerminatorKind::FalseUnwind { real_target, .. } => {
|
|
|
|
TerminatorKind::Goto { target: real_target }
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
|
|
|
_ => continue,
|
2016-06-08 21:10:15 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|