|
| 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 | +} |
0 commit comments