|
13 | 13 |
|
14 | 14 | use rustc_data_structures::bitvec::BitVector;
|
15 | 15 | use rustc::mir::repr as mir;
|
| 16 | +use rustc::mir::repr::TerminatorKind; |
16 | 17 | use rustc::mir::visit::{Visitor, LvalueContext};
|
| 18 | +use rustc_mir::traversal; |
17 | 19 | use common::{self, Block, BlockAndBuilder};
|
18 | 20 | use super::rvalue;
|
19 | 21 |
|
@@ -134,3 +136,104 @@ impl<'mir, 'bcx, 'tcx> Visitor<'tcx> for TempAnalyzer<'mir, 'bcx, 'tcx> {
|
134 | 136 | self.super_lvalue(lvalue, context);
|
135 | 137 | }
|
136 | 138 | }
|
| 139 | + |
| 140 | +#[derive(Copy, Clone, Debug, PartialEq, Eq)] |
| 141 | +pub enum CleanupKind { |
| 142 | + NotCleanup, |
| 143 | + Funclet, |
| 144 | + Internal { funclet: mir::BasicBlock } |
| 145 | +} |
| 146 | + |
| 147 | +pub fn cleanup_kinds<'bcx,'tcx>(_bcx: Block<'bcx,'tcx>, |
| 148 | + mir: &mir::Mir<'tcx>) |
| 149 | + -> Vec<CleanupKind> |
| 150 | +{ |
| 151 | + fn discover_masters<'tcx>(result: &mut [CleanupKind], mir: &mir::Mir<'tcx>) { |
| 152 | + for bb in mir.all_basic_blocks() { |
| 153 | + let data = mir.basic_block_data(bb); |
| 154 | + match data.terminator().kind { |
| 155 | + TerminatorKind::Goto { .. } | |
| 156 | + TerminatorKind::Resume | |
| 157 | + TerminatorKind::Return | |
| 158 | + TerminatorKind::If { .. } | |
| 159 | + TerminatorKind::Switch { .. } | |
| 160 | + TerminatorKind::SwitchInt { .. } => { |
| 161 | + /* nothing to do */ |
| 162 | + } |
| 163 | + TerminatorKind::Call { cleanup: unwind, .. } | |
| 164 | + TerminatorKind::DropAndReplace { unwind, .. } | |
| 165 | + TerminatorKind::Drop { unwind, .. } => { |
| 166 | + if let Some(unwind) = unwind { |
| 167 | + debug!("cleanup_kinds: {:?}/{:?} registering {:?} as funclet", |
| 168 | + bb, data, unwind); |
| 169 | + result[unwind.index()] = CleanupKind::Funclet; |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + fn propagate<'tcx>(result: &mut [CleanupKind], mir: &mir::Mir<'tcx>) { |
| 177 | + let mut funclet_succs : Vec<_> = |
| 178 | + mir.all_basic_blocks().iter().map(|_| None).collect(); |
| 179 | + |
| 180 | + let mut set_successor = |funclet: mir::BasicBlock, succ| { |
| 181 | + match funclet_succs[funclet.index()] { |
| 182 | + ref mut s @ None => { |
| 183 | + debug!("set_successor: updating successor of {:?} to {:?}", |
| 184 | + funclet, succ); |
| 185 | + *s = Some(succ); |
| 186 | + }, |
| 187 | + Some(s) => if s != succ { |
| 188 | + span_bug!(mir.span, "funclet {:?} has 2 parents - {:?} and {:?}", |
| 189 | + funclet, s, succ); |
| 190 | + } |
| 191 | + } |
| 192 | + }; |
| 193 | + |
| 194 | + for (bb, data) in traversal::reverse_postorder(mir) { |
| 195 | + let funclet = match result[bb.index()] { |
| 196 | + CleanupKind::NotCleanup => continue, |
| 197 | + CleanupKind::Funclet => bb, |
| 198 | + CleanupKind::Internal { funclet } => funclet, |
| 199 | + }; |
| 200 | + |
| 201 | + debug!("cleanup_kinds: {:?}/{:?}/{:?} propagating funclet {:?}", |
| 202 | + bb, data, result[bb.index()], funclet); |
| 203 | + |
| 204 | + for &succ in data.terminator().successors().iter() { |
| 205 | + let kind = result[succ.index()]; |
| 206 | + debug!("cleanup_kinds: propagating {:?} to {:?}/{:?}", |
| 207 | + funclet, succ, kind); |
| 208 | + match kind { |
| 209 | + CleanupKind::NotCleanup => { |
| 210 | + result[succ.index()] = CleanupKind::Internal { funclet: funclet }; |
| 211 | + } |
| 212 | + CleanupKind::Funclet => { |
| 213 | + set_successor(funclet, succ); |
| 214 | + } |
| 215 | + CleanupKind::Internal { funclet: succ_funclet } => { |
| 216 | + if funclet != succ_funclet { |
| 217 | + // `succ` has 2 different funclet going into it, so it must |
| 218 | + // be a funclet by itself. |
| 219 | + |
| 220 | + debug!("promoting {:?} to a funclet and updating {:?}", succ, |
| 221 | + succ_funclet); |
| 222 | + result[succ.index()] = CleanupKind::Funclet; |
| 223 | + set_successor(succ_funclet, succ); |
| 224 | + set_successor(funclet, succ); |
| 225 | + } |
| 226 | + } |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + let mut result : Vec<_> = |
| 233 | + mir.all_basic_blocks().iter().map(|_| CleanupKind::NotCleanup).collect(); |
| 234 | + |
| 235 | + discover_masters(&mut result, mir); |
| 236 | + propagate(&mut result, mir); |
| 237 | + debug!("cleanup_kinds: result={:?}", result); |
| 238 | + result |
| 239 | +} |
0 commit comments