Skip to content

Commit 098ea17

Browse files
committed
Auto merge of #29757 - dotdash:mir_simplify_cfg, r=nikomatsakis
For now, this pass does some easy transformations, like eliminating empty blocks that just jump to another block, some trivial conversion of If terminators into Gotos and removal of dead blocks. r? @nikomatsakis
2 parents 8c9c951 + a4e5c0f commit 098ea17

File tree

6 files changed

+223
-1
lines changed

6 files changed

+223
-1
lines changed

src/librustc_mir/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub mod mir_map;
3434
mod hair;
3535
pub mod repr;
3636
mod graphviz;
37+
pub mod transform;
3738
pub mod tcx;
3839
pub mod visit;
3940

src/librustc_mir/mir_map.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ extern crate rustc_front;
2222

2323
use build;
2424
use dot;
25+
use transform::*;
2526
use repr::Mir;
2627
use hair::cx::Cx;
2728
use std::fs::File;
@@ -147,7 +148,9 @@ impl<'a, 'm, 'tcx> visit::Visitor<'tcx> for InnerDump<'a,'m,'tcx> {
147148
let infcx = infer::new_infer_ctxt(self.tcx, &self.tcx.tables, Some(param_env), true);
148149

149150
match build_mir(Cx::new(&infcx), implicit_arg_tys, id, span, decl, body) {
150-
Ok(mir) => {
151+
Ok(mut mir) => {
152+
simplify_cfg::SimplifyCfg::new().run_on_mir(&mut mir);
153+
151154
let meta_item_list = self.attr
152155
.iter()
153156
.flat_map(|a| a.meta_item_list())

src/librustc_mir/repr.rs

+14
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,20 @@ impl<'tcx> Terminator<'tcx> {
307307
Call { data: _, targets: ref b } => b,
308308
}
309309
}
310+
311+
pub fn successors_mut(&mut self) -> &mut [BasicBlock] {
312+
use self::Terminator::*;
313+
match *self {
314+
Goto { target: ref mut b } => slice::mut_ref_slice(b),
315+
Panic { target: ref mut b } => slice::mut_ref_slice(b),
316+
If { cond: _, targets: ref mut b } => b,
317+
Switch { targets: ref mut b, .. } => b,
318+
SwitchInt { targets: ref mut b, .. } => b,
319+
Diverge => &mut [],
320+
Return => &mut [],
321+
Call { data: _, targets: ref mut b } => b,
322+
}
323+
}
310324
}
311325

312326
#[derive(Debug)]

src/librustc_mir/transform/mod.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub mod simplify_cfg;
12+
mod util;
13+
14+
use repr::Mir;
15+
16+
pub trait MirPass {
17+
fn run_on_mir(&mut self, mir: &mut Mir);
18+
}
+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use repr::*;
12+
use rustc::middle::const_eval::ConstVal;
13+
use std::mem;
14+
use transform::util;
15+
use transform::MirPass;
16+
17+
pub struct SimplifyCfg;
18+
19+
impl SimplifyCfg {
20+
pub fn new() -> SimplifyCfg {
21+
SimplifyCfg
22+
}
23+
24+
fn remove_dead_blocks(&self, mir: &mut Mir) {
25+
let mut seen = vec![false; mir.basic_blocks.len()];
26+
27+
// These blocks are always required.
28+
seen[START_BLOCK.index()] = true;
29+
seen[END_BLOCK.index()] = true;
30+
seen[DIVERGE_BLOCK.index()] = true;
31+
32+
let mut worklist = vec![START_BLOCK];
33+
while let Some(bb) = worklist.pop() {
34+
for succ in mir.basic_block_data(bb).terminator.successors() {
35+
if !seen[succ.index()] {
36+
seen[succ.index()] = true;
37+
worklist.push(*succ);
38+
}
39+
}
40+
}
41+
42+
util::retain_basic_blocks(mir, &seen);
43+
}
44+
45+
fn remove_goto_chains(&self, mir: &mut Mir) -> bool {
46+
47+
// Find the target at the end of the jump chain, return None if there is a loop
48+
fn final_target(mir: &Mir, mut target: BasicBlock) -> Option<BasicBlock> {
49+
// Keep track of already seen blocks to detect loops
50+
let mut seen: Vec<BasicBlock> = Vec::with_capacity(8);
51+
52+
while mir.basic_block_data(target).statements.is_empty() {
53+
match mir.basic_block_data(target).terminator {
54+
Terminator::Goto { target: next } => {
55+
if seen.contains(&next) {
56+
return None;
57+
}
58+
seen.push(next);
59+
target = next;
60+
}
61+
_ => break
62+
}
63+
}
64+
65+
Some(target)
66+
}
67+
68+
let mut changed = false;
69+
for bb in mir.all_basic_blocks() {
70+
// Temporarily swap out the terminator we're modifying to keep borrowck happy
71+
let mut terminator = Terminator::Diverge;
72+
mem::swap(&mut terminator, &mut mir.basic_block_data_mut(bb).terminator);
73+
74+
for target in terminator.successors_mut() {
75+
let new_target = match final_target(mir, *target) {
76+
Some(new_target) => new_target,
77+
None if mir.basic_block_data(bb).statements.is_empty() => bb,
78+
None => continue
79+
};
80+
changed |= *target != new_target;
81+
*target = new_target;
82+
}
83+
84+
mir.basic_block_data_mut(bb).terminator = terminator;
85+
}
86+
87+
changed
88+
}
89+
90+
fn simplify_branches(&self, mir: &mut Mir) -> bool {
91+
let mut changed = false;
92+
93+
for bb in mir.all_basic_blocks() {
94+
// Temporarily swap out the terminator we're modifying to keep borrowck happy
95+
let mut terminator = Terminator::Diverge;
96+
mem::swap(&mut terminator, &mut mir.basic_block_data_mut(bb).terminator);
97+
98+
mir.basic_block_data_mut(bb).terminator = match terminator {
99+
Terminator::If { ref targets, .. } if targets[0] == targets[1] => {
100+
changed = true;
101+
Terminator::Goto { target: targets[0] }
102+
}
103+
Terminator::If { ref targets, cond: Operand::Constant(Constant {
104+
literal: Literal::Value {
105+
value: ConstVal::Bool(cond)
106+
}, ..
107+
}) } => {
108+
changed = true;
109+
let target_idx = if cond { 0 } else { 1 };
110+
Terminator::Goto { target: targets[target_idx] }
111+
}
112+
Terminator::SwitchInt { ref targets, .. } if targets.len() == 1 => {
113+
Terminator::Goto { target: targets[0] }
114+
}
115+
_ => terminator
116+
}
117+
}
118+
119+
changed
120+
}
121+
}
122+
123+
impl MirPass for SimplifyCfg {
124+
fn run_on_mir(&mut self, mir: &mut Mir) {
125+
let mut changed = true;
126+
while changed {
127+
changed = self.simplify_branches(mir);
128+
changed |= self.remove_goto_chains(mir);
129+
self.remove_dead_blocks(mir);
130+
}
131+
132+
// FIXME: Should probably be moved into some kind of pass manager
133+
mir.basic_blocks.shrink_to_fit();
134+
}
135+
}

src/librustc_mir/transform/util.rs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use repr::*;
12+
13+
/// Update basic block ids in all terminators using the given replacements,
14+
/// useful e.g. after removal of several basic blocks to update all terminators
15+
/// in a single pass
16+
pub fn update_basic_block_ids(mir: &mut Mir, replacements: &[BasicBlock]) {
17+
for bb in mir.all_basic_blocks() {
18+
for target in mir.basic_block_data_mut(bb).terminator.successors_mut() {
19+
*target = replacements[target.index()];
20+
}
21+
}
22+
}
23+
24+
/// Mass removal of basic blocks to keep the ID-remapping cheap.
25+
pub fn retain_basic_blocks(mir: &mut Mir, keep: &[bool]) {
26+
let num_blocks = mir.basic_blocks.len();
27+
28+
// Check that we have a usage flag for every block
29+
assert_eq!(num_blocks, keep.len());
30+
31+
let first_dead = match keep.iter().position(|&k| !k) {
32+
None => return,
33+
Some(first_dead) => first_dead,
34+
};
35+
36+
// `replacements` maps the old block ids to the new ones
37+
let mut replacements: Vec<_> = (0..num_blocks).map(BasicBlock::new).collect();
38+
39+
let mut dead = 0;
40+
for i in first_dead..num_blocks {
41+
if keep[i] {
42+
replacements[i] = BasicBlock::new(i - dead);
43+
mir.basic_blocks.swap(i, i - dead);
44+
} else {
45+
dead += 1;
46+
}
47+
}
48+
mir.basic_blocks.truncate(num_blocks - dead);
49+
50+
update_basic_block_ids(mir, &replacements);
51+
}

0 commit comments

Comments
 (0)