|
| 1 | +// Copyright 2016 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 | +use std::mem; |
| 11 | + |
| 12 | +use rustc_back::slice; |
| 13 | +use rustc::mir::repr::*; |
| 14 | +use rustc::mir::mir_map::MirMap; |
| 15 | + |
| 16 | +use traversal; |
| 17 | + |
| 18 | +/** |
| 19 | + * Breaks critical edges in the MIR. |
| 20 | + * |
| 21 | + * Critical edges are edges that are neither the only edge leaving a |
| 22 | + * block, nor the only edge entering one. |
| 23 | + * |
| 24 | + * When you want something to happen "along" an edge, you can either |
| 25 | + * do at the end of the predecessor block, or at the start of the |
| 26 | + * successor block. Critical edges have to be broken in order to prevent |
| 27 | + * "edge actions" from affecting other edges. |
| 28 | + * |
| 29 | + * This function will break those edges by inserting new blocks along them. |
| 30 | + * |
| 31 | + * A special case is Drop and Call terminators with unwind/cleanup successors, |
| 32 | + * They use `invoke` in LLVM, which terminates a block, meaning that code cannot |
| 33 | + * be inserted after them, so even if an edge is the only edge leaving a block |
| 34 | + * like that, we still insert blocks if the edge is one of many entering the |
| 35 | + * target. |
| 36 | + * |
| 37 | + * NOTE: Simplify CFG will happily undo most of the work this pass does. |
| 38 | + * |
| 39 | + */ |
| 40 | +pub fn break_critical_edges<'tcx>(mir_map: &mut MirMap<'tcx>) { |
| 41 | + for (_, mir) in &mut mir_map.map { |
| 42 | + break_critical_edges_fn(mir); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/* |
| 47 | + * Predecessor map for tracking the predecessors of a block |
| 48 | + */ |
| 49 | +struct PredMap { |
| 50 | + preds: Vec<BlockPredecessors> |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Most blocks only have one predecessor, so we can cut down on |
| 55 | + * some allocation by not using Vec until we have more than one. |
| 56 | + */ |
| 57 | +#[derive(Clone)] |
| 58 | +enum BlockPredecessors { |
| 59 | + None, |
| 60 | + One(BasicBlock), |
| 61 | + Some(Vec<BasicBlock>) |
| 62 | +} |
| 63 | + |
| 64 | +impl PredMap { |
| 65 | + pub fn new(n: usize) -> PredMap { |
| 66 | + let preds = vec![BlockPredecessors::None; n]; |
| 67 | + |
| 68 | + PredMap { |
| 69 | + preds: preds |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + fn ensure_len(&mut self, bb: BasicBlock) { |
| 74 | + let idx = bb.index(); |
| 75 | + while self.preds.len() <= idx { |
| 76 | + self.preds.push(BlockPredecessors::None); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + pub fn add_pred(&mut self, target: BasicBlock, pred: BasicBlock) { |
| 81 | + self.ensure_len(target); |
| 82 | + |
| 83 | + let preds = mem::replace(&mut self.preds[target.index()], BlockPredecessors::None); |
| 84 | + match preds { |
| 85 | + BlockPredecessors::None => { |
| 86 | + self.preds[target.index()] = BlockPredecessors::One(pred); |
| 87 | + } |
| 88 | + BlockPredecessors::One(bb) => { |
| 89 | + self.preds[target.index()] = BlockPredecessors::Some(vec![bb, pred]); |
| 90 | + } |
| 91 | + BlockPredecessors::Some(mut preds) => { |
| 92 | + preds.push(pred); |
| 93 | + self.preds[target.index()] = BlockPredecessors::Some(preds); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + pub fn remove_pred(&mut self, target: BasicBlock, pred: BasicBlock) { |
| 99 | + self.ensure_len(target); |
| 100 | + |
| 101 | + let preds = mem::replace(&mut self.preds[target.index()], BlockPredecessors::None); |
| 102 | + match preds { |
| 103 | + BlockPredecessors::None => {} |
| 104 | + BlockPredecessors::One(bb) if bb == pred => {} |
| 105 | + |
| 106 | + BlockPredecessors::One(bb) => { |
| 107 | + self.preds[target.index()] = BlockPredecessors::One(bb); |
| 108 | + } |
| 109 | + |
| 110 | + BlockPredecessors::Some(mut preds) => { |
| 111 | + preds.retain(|&bb| bb != pred); |
| 112 | + self.preds[target.index()] = BlockPredecessors::Some(preds); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + pub fn get_preds(&self, bb: BasicBlock) -> &[BasicBlock] { |
| 118 | + match self.preds[bb.index()] { |
| 119 | + BlockPredecessors::None => &[], |
| 120 | + BlockPredecessors::One(ref bb) => slice::ref_slice(bb), |
| 121 | + BlockPredecessors::Some(ref bbs) => &bbs[..] |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | + |
| 127 | +fn break_critical_edges_fn(mir: &mut Mir) { |
| 128 | + let mut pred_map = PredMap::new(mir.basic_blocks.len()); |
| 129 | + |
| 130 | + // Build the precedecessor map for the MIR |
| 131 | + for (pred, data) in traversal::preorder(mir) { |
| 132 | + if let Some(ref term) = data.terminator { |
| 133 | + for &tgt in term.successors().iter() { |
| 134 | + pred_map.add_pred(tgt, pred); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + // We need a place to store the new blocks generated |
| 140 | + let mut new_blocks = Vec::new(); |
| 141 | + |
| 142 | + let bbs = mir.all_basic_blocks(); |
| 143 | + let cur_len = mir.basic_blocks.len(); |
| 144 | + |
| 145 | + for &bb in &bbs { |
| 146 | + let data = mir.basic_block_data_mut(bb); |
| 147 | + |
| 148 | + if let Some(ref mut term) = data.terminator { |
| 149 | + let is_invoke = term_is_invoke(term); |
| 150 | + let succs = term.successors_mut(); |
| 151 | + if succs.len() > 1 || (succs.len() > 0 && is_invoke) { |
| 152 | + for tgt in succs { |
| 153 | + let num_preds = pred_map.get_preds(*tgt).len(); |
| 154 | + if num_preds > 1 { |
| 155 | + // It's a critical edge, break it |
| 156 | + let goto = Terminator::Goto { target: *tgt }; |
| 157 | + let data = BasicBlockData::new(Some(goto)); |
| 158 | + // Get the index it will be when inserted into the MIR |
| 159 | + let idx = cur_len + new_blocks.len(); |
| 160 | + new_blocks.push(data); |
| 161 | + *tgt = BasicBlock::new(idx); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + debug!("Broke {} N edges", new_blocks.len()); |
| 169 | + |
| 170 | + mir.basic_blocks.extend_from_slice(&new_blocks); |
| 171 | +} |
| 172 | + |
| 173 | +// Returns true if the terminator would use an invoke in LLVM. |
| 174 | +fn term_is_invoke(term: &Terminator) -> bool { |
| 175 | + match *term { |
| 176 | + Terminator::Call { cleanup: Some(_), .. } | |
| 177 | + Terminator::Drop { unwind: Some(_), .. } => true, |
| 178 | + _ => false |
| 179 | + } |
| 180 | +} |
0 commit comments