2015-11-10 14:38:36 -06:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2016-06-06 14:58:28 -05:00
|
|
|
//! A pass that removes various redundancies in the CFG. It should be
|
|
|
|
//! called after every significant CFG modification to tidy things
|
|
|
|
//! up.
|
|
|
|
//!
|
|
|
|
//! This pass must also be run before any analysis passes because it removes
|
|
|
|
//! dead blocks, and some of these can be ill-typed.
|
|
|
|
//!
|
|
|
|
//! The cause of that is that typeck lets most blocks whose end is not
|
|
|
|
//! reachable have an arbitrary return type, rather than having the
|
|
|
|
//! usual () return type (as a note, typeck's notion of reachability
|
|
|
|
//! is in fact slightly weaker than MIR CFG reachability - see #31617).
|
|
|
|
//!
|
|
|
|
//! A standard example of the situation is:
|
|
|
|
//! ```rust
|
|
|
|
//! fn example() {
|
|
|
|
//! let _a: char = { return; };
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Here the block (`{ return; }`) has the return type `char`,
|
|
|
|
//! rather than `()`, but the MIR we naively generate still contains
|
|
|
|
//! the `_a = ()` write in the unreachable block "after" the return.
|
|
|
|
|
|
|
|
|
2015-11-14 16:52:17 -06:00
|
|
|
use rustc_data_structures::bitvec::BitVector;
|
2016-06-07 09:28:36 -05:00
|
|
|
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
|
2016-03-30 06:43:36 -05:00
|
|
|
use rustc::middle::const_val::ConstVal;
|
2016-03-22 10:30:57 -05:00
|
|
|
use rustc::ty::TyCtxt;
|
2015-11-19 09:37:34 -06:00
|
|
|
use rustc::mir::repr::*;
|
2016-05-02 16:26:41 -05:00
|
|
|
use rustc::mir::transform::{MirPass, MirSource, Pass};
|
2016-06-05 11:38:22 -05:00
|
|
|
use rustc::mir::traversal;
|
2016-06-08 13:03:06 -05:00
|
|
|
|
|
|
|
use std::fmt;
|
2015-11-14 16:52:17 -06:00
|
|
|
use std::mem;
|
2016-02-26 10:05:50 -06:00
|
|
|
|
2016-06-06 14:58:28 -05:00
|
|
|
pub struct SimplifyCfg<'a> { label: &'a str }
|
2015-11-10 14:38:36 -06:00
|
|
|
|
2016-06-06 14:58:28 -05:00
|
|
|
impl<'a> SimplifyCfg<'a> {
|
|
|
|
pub fn new(label: &'a str) -> Self {
|
|
|
|
SimplifyCfg { label: label }
|
2015-11-10 14:38:36 -06:00
|
|
|
}
|
2015-11-14 16:52:17 -06:00
|
|
|
}
|
|
|
|
|
2016-06-06 14:58:28 -05:00
|
|
|
impl<'l, 'tcx> MirPass<'tcx> for SimplifyCfg<'l> {
|
2016-06-08 13:03:06 -05:00
|
|
|
fn run_pass<'a>(&mut self, _tcx: TyCtxt<'a, 'tcx, 'tcx>, _src: MirSource, mir: &mut Mir<'tcx>) {
|
2015-11-14 16:52:17 -06:00
|
|
|
simplify_branches(mir);
|
2016-06-06 14:58:28 -05:00
|
|
|
remove_dead_blocks(mir);
|
2015-11-14 16:52:17 -06:00
|
|
|
merge_consecutive_blocks(mir);
|
2016-06-06 14:58:28 -05:00
|
|
|
remove_dead_blocks(mir);
|
2015-11-14 16:52:17 -06:00
|
|
|
|
|
|
|
// FIXME: Should probably be moved into some kind of pass manager
|
2016-06-07 13:20:50 -05:00
|
|
|
mir.basic_blocks_mut().raw.shrink_to_fit();
|
2015-11-14 16:52:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-08 13:03:06 -05:00
|
|
|
impl<'l> Pass for SimplifyCfg<'l> {
|
|
|
|
fn name(&self) -> &str { "simplify-cfg" }
|
|
|
|
fn disambiguator<'a>(&'a self) -> Option<Box<fmt::Display+'a>> {
|
|
|
|
Some(Box::new(self.label))
|
|
|
|
}
|
|
|
|
}
|
2015-11-14 16:52:17 -06:00
|
|
|
|
|
|
|
fn merge_consecutive_blocks(mir: &mut Mir) {
|
2016-06-07 14:02:08 -05:00
|
|
|
let mut pred_count: IndexVec<_, _> =
|
|
|
|
mir.predecessors().iter().map(|ps| ps.len()).collect();
|
2015-11-14 16:52:17 -06:00
|
|
|
|
|
|
|
loop {
|
|
|
|
let mut changed = false;
|
2016-06-07 13:20:50 -05:00
|
|
|
let mut seen = BitVector::new(mir.basic_blocks().len());
|
2015-11-14 16:52:17 -06:00
|
|
|
let mut worklist = vec![START_BLOCK];
|
|
|
|
while let Some(bb) = worklist.pop() {
|
|
|
|
// Temporarily take ownership of the terminator we're modifying to keep borrowck happy
|
2016-06-07 13:20:50 -05:00
|
|
|
let mut terminator = mir[bb].terminator.take().expect("invalid terminator state");
|
2015-11-14 16:52:17 -06:00
|
|
|
|
|
|
|
// See if we can merge the target block into this one
|
|
|
|
loop {
|
|
|
|
let mut inner_change = false;
|
2015-11-10 14:38:36 -06:00
|
|
|
|
2015-11-14 16:52:17 -06:00
|
|
|
if let TerminatorKind::Goto { target } = terminator.kind {
|
|
|
|
// Don't bother trying to merge a block into itself
|
|
|
|
if target == bb {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-06-07 13:20:50 -05:00
|
|
|
let num_insts = mir[target].statements.len();
|
|
|
|
match mir[target].terminator().kind {
|
2015-11-14 16:52:17 -06:00
|
|
|
TerminatorKind::Goto { target: new_target } if num_insts == 0 => {
|
|
|
|
inner_change = true;
|
|
|
|
terminator.kind = TerminatorKind::Goto { target: new_target };
|
2016-06-07 09:28:36 -05:00
|
|
|
pred_count[target] -= 1;
|
|
|
|
pred_count[new_target] += 1;
|
2015-11-14 16:52:17 -06:00
|
|
|
}
|
2016-06-07 09:28:36 -05:00
|
|
|
_ if pred_count[target] == 1 => {
|
2015-11-14 16:52:17 -06:00
|
|
|
inner_change = true;
|
|
|
|
let mut stmts = Vec::new();
|
|
|
|
{
|
2016-06-07 13:20:50 -05:00
|
|
|
let target_data = &mut mir[target];
|
2015-11-14 16:52:17 -06:00
|
|
|
mem::swap(&mut stmts, &mut target_data.statements);
|
|
|
|
mem::swap(&mut terminator, target_data.terminator_mut());
|
2016-03-22 15:19:44 -05:00
|
|
|
}
|
2015-11-14 16:52:17 -06:00
|
|
|
|
2016-06-07 13:20:50 -05:00
|
|
|
mir[bb].statements.append(&mut stmts);
|
2015-11-10 14:38:36 -06:00
|
|
|
}
|
2015-11-14 16:52:17 -06:00
|
|
|
_ => {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
for target in terminator.successors_mut() {
|
|
|
|
let new_target = match final_target(mir, *target) {
|
|
|
|
Some(new_target) => new_target,
|
2016-06-07 13:20:50 -05:00
|
|
|
None if mir[bb].statements.is_empty() => bb,
|
2015-11-14 16:52:17 -06:00
|
|
|
None => continue
|
|
|
|
};
|
|
|
|
if *target != new_target {
|
|
|
|
inner_change = true;
|
2016-06-07 09:28:36 -05:00
|
|
|
pred_count[*target] -= 1;
|
|
|
|
pred_count[new_target] += 1;
|
2015-11-14 16:52:17 -06:00
|
|
|
*target = new_target;
|
2015-11-10 14:38:36 -06:00
|
|
|
}
|
2015-11-14 16:52:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
changed |= inner_change;
|
|
|
|
if !inner_change {
|
|
|
|
break;
|
2015-11-10 14:38:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-07 13:20:50 -05:00
|
|
|
mir[bb].terminator = Some(terminator);
|
2015-11-14 16:52:17 -06:00
|
|
|
|
2016-06-07 13:20:50 -05:00
|
|
|
for succ in mir[bb].terminator().successors().iter() {
|
2015-11-14 16:52:17 -06:00
|
|
|
if seen.insert(succ.index()) {
|
|
|
|
worklist.push(*succ);
|
|
|
|
}
|
|
|
|
}
|
2015-11-10 14:38:36 -06:00
|
|
|
}
|
|
|
|
|
2015-11-14 16:52:17 -06:00
|
|
|
if !changed {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the target at the end of the jump chain, return None if there is a loop
|
|
|
|
fn final_target(mir: &Mir, mut target: BasicBlock) -> Option<BasicBlock> {
|
|
|
|
// Keep track of already seen blocks to detect loops
|
|
|
|
let mut seen: Vec<BasicBlock> = Vec::with_capacity(8);
|
|
|
|
|
2016-06-07 13:20:50 -05:00
|
|
|
while mir[target].statements.is_empty() {
|
2015-11-14 16:52:17 -06:00
|
|
|
// NB -- terminator may have been swapped with `None` in
|
|
|
|
// merge_consecutive_blocks, in which case we have a cycle and just want
|
|
|
|
// to stop
|
2016-06-07 13:20:50 -05:00
|
|
|
match mir[target].terminator {
|
2015-11-14 16:52:17 -06:00
|
|
|
Some(Terminator { kind: TerminatorKind::Goto { target: next }, .. }) => {
|
|
|
|
if seen.contains(&next) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
seen.push(next);
|
|
|
|
target = next;
|
2015-11-10 14:38:36 -06:00
|
|
|
}
|
2015-11-14 16:52:17 -06:00
|
|
|
_ => break
|
2015-11-10 14:38:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-14 16:52:17 -06:00
|
|
|
Some(target)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn simplify_branches(mir: &mut Mir) {
|
|
|
|
loop {
|
2015-11-10 14:38:36 -06:00
|
|
|
let mut changed = false;
|
|
|
|
|
2016-06-07 13:20:50 -05:00
|
|
|
for (_, basic_block) in mir.basic_blocks_mut().iter_enumerated_mut() {
|
2015-12-18 16:44:32 -06:00
|
|
|
let mut terminator = basic_block.terminator_mut();
|
2016-03-10 08:55:15 -06:00
|
|
|
terminator.kind = match terminator.kind {
|
|
|
|
TerminatorKind::If { ref targets, .. } if targets.0 == targets.1 => {
|
2015-11-10 14:38:36 -06:00
|
|
|
changed = true;
|
2016-03-10 08:55:15 -06:00
|
|
|
TerminatorKind::Goto { target: targets.0 }
|
2015-11-10 14:38:36 -06:00
|
|
|
}
|
2016-01-31 11:17:15 -06:00
|
|
|
|
2016-03-10 08:55:15 -06:00
|
|
|
TerminatorKind::If { ref targets, cond: Operand::Constant(Constant {
|
2015-11-10 14:38:36 -06:00
|
|
|
literal: Literal::Value {
|
|
|
|
value: ConstVal::Bool(cond)
|
|
|
|
}, ..
|
|
|
|
}) } => {
|
|
|
|
changed = true;
|
2015-12-10 14:46:40 -06:00
|
|
|
if cond {
|
2016-03-10 08:55:15 -06:00
|
|
|
TerminatorKind::Goto { target: targets.0 }
|
2015-12-10 14:46:40 -06:00
|
|
|
} else {
|
2016-03-10 08:55:15 -06:00
|
|
|
TerminatorKind::Goto { target: targets.1 }
|
2015-12-10 14:46:40 -06:00
|
|
|
}
|
2015-11-10 14:38:36 -06:00
|
|
|
}
|
2016-01-31 11:17:15 -06:00
|
|
|
|
2016-05-25 00:39:32 -05:00
|
|
|
TerminatorKind::Assert { target, cond: Operand::Constant(Constant {
|
|
|
|
literal: Literal::Value {
|
|
|
|
value: ConstVal::Bool(cond)
|
|
|
|
}, ..
|
|
|
|
}), expected, .. } if cond == expected => {
|
|
|
|
changed = true;
|
|
|
|
TerminatorKind::Goto { target: target }
|
|
|
|
}
|
|
|
|
|
2016-03-10 08:55:15 -06:00
|
|
|
TerminatorKind::SwitchInt { ref targets, .. } if targets.len() == 1 => {
|
2016-05-25 00:39:32 -05:00
|
|
|
changed = true;
|
2016-03-10 08:55:15 -06:00
|
|
|
TerminatorKind::Goto { target: targets[0] }
|
2015-11-10 14:38:36 -06:00
|
|
|
}
|
2015-12-18 16:44:32 -06:00
|
|
|
_ => continue
|
2015-11-10 14:38:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-14 16:52:17 -06:00
|
|
|
if !changed {
|
|
|
|
break;
|
2015-11-10 14:38:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-06 14:58:28 -05:00
|
|
|
|
|
|
|
fn remove_dead_blocks(mir: &mut Mir) {
|
2016-06-07 13:20:50 -05:00
|
|
|
let mut seen = BitVector::new(mir.basic_blocks().len());
|
2016-06-06 14:58:28 -05:00
|
|
|
for (bb, _) in traversal::preorder(mir) {
|
|
|
|
seen.insert(bb.index());
|
|
|
|
}
|
|
|
|
|
2016-06-07 13:20:50 -05:00
|
|
|
let basic_blocks = mir.basic_blocks_mut();
|
2016-06-06 14:58:28 -05:00
|
|
|
|
2016-06-07 13:20:50 -05:00
|
|
|
let num_blocks = basic_blocks.len();
|
2016-06-07 09:28:36 -05:00
|
|
|
let mut replacements : Vec<_> = (0..num_blocks).map(BasicBlock::new).collect();
|
2016-06-06 14:58:28 -05:00
|
|
|
let mut used_blocks = 0;
|
|
|
|
for alive_index in seen.iter() {
|
|
|
|
replacements[alive_index] = BasicBlock::new(used_blocks);
|
|
|
|
if alive_index != used_blocks {
|
|
|
|
// Swap the next alive block data with the current available slot. Since alive_index is
|
|
|
|
// non-decreasing this is a valid operation.
|
2016-06-07 13:20:50 -05:00
|
|
|
basic_blocks.raw.swap(alive_index, used_blocks);
|
2016-06-06 14:58:28 -05:00
|
|
|
}
|
|
|
|
used_blocks += 1;
|
|
|
|
}
|
2016-06-07 13:20:50 -05:00
|
|
|
basic_blocks.raw.truncate(used_blocks);
|
2016-06-06 14:58:28 -05:00
|
|
|
|
2016-06-07 13:20:50 -05:00
|
|
|
for block in basic_blocks {
|
|
|
|
for target in block.terminator_mut().successors_mut() {
|
2016-06-06 14:58:28 -05:00
|
|
|
*target = replacements[target.index()];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|