|
| 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 | + |
| 11 | +//! A MIR walk gathering a union-crfind of assigned locals, for the purpose of locating the ones |
| 12 | +//! escaping into the output. |
| 13 | +
|
| 14 | +use rustc::mir::visit::Visitor; |
| 15 | +use rustc::mir::*; |
| 16 | + |
| 17 | +use rustc_data_structures::indexed_vec::Idx; |
| 18 | +use rustc_data_structures::unify as ut; |
| 19 | + |
| 20 | +crate struct EscapingLocals { |
| 21 | + unification_table: ut::UnificationTable<ut::InPlace<AssignedLocal>>, |
| 22 | +} |
| 23 | + |
| 24 | +impl EscapingLocals { |
| 25 | + crate fn compute(mir: &Mir<'_>) -> Self { |
| 26 | + let mut visitor = GatherAssignedLocalsVisitor::new(); |
| 27 | + visitor.visit_mir(mir); |
| 28 | + |
| 29 | + EscapingLocals { unification_table: visitor.unification_table } |
| 30 | + } |
| 31 | + |
| 32 | + /// True if `local` is known to escape into static |
| 33 | + /// memory. |
| 34 | + crate fn escapes_into_return(&mut self, local: Local) -> bool { |
| 35 | + let return_place = AssignedLocal::from(RETURN_PLACE); |
| 36 | + let other_place = AssignedLocal::from(local); |
| 37 | + self.unification_table.unioned(return_place, other_place) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/// The MIR visitor gathering the union-find of the locals used in |
| 42 | +/// assignments. |
| 43 | +struct GatherAssignedLocalsVisitor { |
| 44 | + unification_table: ut::UnificationTable<ut::InPlace<AssignedLocal>>, |
| 45 | +} |
| 46 | + |
| 47 | +#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] |
| 48 | +struct AssignedLocal(u32); |
| 49 | + |
| 50 | +impl ut::UnifyKey for AssignedLocal { |
| 51 | + type Value = (); |
| 52 | + |
| 53 | + fn index(&self) -> u32 { |
| 54 | + self.0 |
| 55 | + } |
| 56 | + |
| 57 | + fn from_index(i: u32) -> AssignedLocal { |
| 58 | + AssignedLocal(i) |
| 59 | + } |
| 60 | + |
| 61 | + fn tag() -> &'static str { |
| 62 | + "AssignedLocal" |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl From<Local> for AssignedLocal { |
| 67 | + fn from(item: Local) -> Self { |
| 68 | + // newtype_indexes use usize but are u32s. |
| 69 | + assert!(item.index() < ::std::u32::MAX as usize); |
| 70 | + AssignedLocal(item.index() as u32) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +impl GatherAssignedLocalsVisitor { |
| 75 | + fn new() -> Self { |
| 76 | + Self { |
| 77 | + unification_table: ut::UnificationTable::new(), |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + fn union_locals_if_needed(&mut self, lvalue: Option<Local>, rvalue: Option<Local>) { |
| 82 | + if let Some(lvalue) = lvalue { |
| 83 | + if let Some(rvalue) = rvalue { |
| 84 | + if lvalue != rvalue { |
| 85 | + self.unification_table |
| 86 | + .union(AssignedLocal::from(lvalue), AssignedLocal::from(rvalue)); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// Returns the potential `Local` associated to this `Place` or `PlaceProjection` |
| 94 | +fn find_local_in_place(place: &Place) -> Option<Local> { |
| 95 | + match place { |
| 96 | + Place::Local(local) => Some(*local), |
| 97 | + |
| 98 | + // If you do e.g. `x = a.f` then only *part* of the type of |
| 99 | + // `a` escapes into `x` (the part contained in `f`); if `a`'s |
| 100 | + // type has regions that don't appear in `f`, those might not |
| 101 | + // escape. |
| 102 | + Place::Projection(..) => None, |
| 103 | + |
| 104 | + Place::Static { .. } | Place::Promoted { .. } => None, |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// Returns the potential `Local` in this `Operand`. |
| 109 | +fn find_local_in_operand(op: &Operand) -> Option<Local> { |
| 110 | + // Conservatively check a subset of `Operand`s we know our |
| 111 | + // benchmarks track, for example `html5ever`. |
| 112 | + match op { |
| 113 | + Operand::Copy(place) | Operand::Move(place) => find_local_in_place(place), |
| 114 | + Operand::Constant(_) => None, |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +impl<'tcx> Visitor<'tcx> for GatherAssignedLocalsVisitor { |
| 119 | + fn visit_mir(&mut self, mir: &Mir<'tcx>) { |
| 120 | + // We need as many union-find keys as there are locals |
| 121 | + for _ in 0..mir.local_decls.len() { |
| 122 | + self.unification_table.new_key(()); |
| 123 | + } |
| 124 | + |
| 125 | + self.super_mir(mir); |
| 126 | + } |
| 127 | + |
| 128 | + fn visit_assign( |
| 129 | + &mut self, |
| 130 | + block: BasicBlock, |
| 131 | + place: &Place<'tcx>, |
| 132 | + rvalue: &Rvalue<'tcx>, |
| 133 | + location: Location, |
| 134 | + ) { |
| 135 | + let local = find_local_in_place(place); |
| 136 | + |
| 137 | + // Conservatively check a subset of `Rvalue`s we know our |
| 138 | + // benchmarks track, for example `html5ever`. |
| 139 | + match rvalue { |
| 140 | + Rvalue::Use(op) => self.union_locals_if_needed(local, find_local_in_operand(op)), |
| 141 | + Rvalue::Ref(_, _, place) => { |
| 142 | + self.union_locals_if_needed(local, find_local_in_place(place)) |
| 143 | + } |
| 144 | + |
| 145 | + Rvalue::Cast(kind, op, _) => match kind { |
| 146 | + CastKind::Unsize => self.union_locals_if_needed(local, find_local_in_operand(op)), |
| 147 | + _ => (), |
| 148 | + }, |
| 149 | + |
| 150 | + Rvalue::Aggregate(_, ops) => { |
| 151 | + for rvalue in ops.iter().map(find_local_in_operand) { |
| 152 | + self.union_locals_if_needed(local, rvalue); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + _ => (), |
| 157 | + }; |
| 158 | + |
| 159 | + self.super_assign(block, place, rvalue, location); |
| 160 | + } |
| 161 | +} |
0 commit comments