1 //! An analysis to determine which locals require allocas and
6 use rustc::mir::traversal;
7 use rustc::mir::visit::{
8 MutatingUseContext, NonMutatingUseContext, NonUseContext, PlaceContext, Visitor,
10 use rustc::mir::{self, Location, TerminatorKind};
12 use rustc::ty::layout::{HasTyCtxt, LayoutOf};
13 use rustc_data_structures::graph::dominators::Dominators;
14 use rustc_index::bit_set::BitSet;
15 use rustc_index::vec::{Idx, IndexVec};
17 pub fn non_ssa_locals<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
18 fx: &FunctionCx<'a, 'tcx, Bx>,
19 ) -> BitSet<mir::Local> {
21 let mut analyzer = LocalAnalyzer::new(fx);
23 analyzer.visit_body(mir);
25 for (local, decl) in mir.local_decls.iter_enumerated() {
26 let ty = fx.monomorphize(&decl.ty);
27 debug!("local {:?} has type `{}`", local, ty);
28 let layout = fx.cx.spanned_layout_of(ty, decl.source_info.span);
29 if fx.cx.is_backend_immediate(layout) {
30 // These sorts of types are immediates that we can store
31 // in an Value without an alloca.
32 } else if fx.cx.is_backend_scalar_pair(layout) {
33 // We allow pairs and uses of any of their 2 fields.
35 // These sorts of types require an alloca. Note that
36 // is_llvm_immediate() may *still* be true, particularly
37 // for newtypes, but we currently force some types
38 // (e.g., structs) into an alloca unconditionally, just so
39 // that we don't have to deal with having two pathways
40 // (gep vs extractvalue etc).
41 analyzer.not_ssa(local);
45 analyzer.non_ssa_locals
48 struct LocalAnalyzer<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> {
49 fx: &'mir FunctionCx<'a, 'tcx, Bx>,
50 dominators: Dominators<mir::BasicBlock>,
51 non_ssa_locals: BitSet<mir::Local>,
52 // The location of the first visited direct assignment to each
53 // local, or an invalid location (out of bounds `block` index).
54 first_assignment: IndexVec<mir::Local, Location>,
57 impl<Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'tcx, Bx> {
58 fn new(fx: &'mir FunctionCx<'a, 'tcx, Bx>) -> Self {
59 let invalid_location = mir::BasicBlock::new(fx.mir.basic_blocks().len()).start_location();
60 let dominators = fx.mir.dominators();
61 let mut analyzer = LocalAnalyzer {
64 non_ssa_locals: BitSet::new_empty(fx.mir.local_decls.len()),
65 first_assignment: IndexVec::from_elem(invalid_location, &fx.mir.local_decls),
68 // Arguments get assigned to by means of the function being called
69 for arg in fx.mir.args_iter() {
70 analyzer.first_assignment[arg] = mir::START_BLOCK.start_location();
76 fn first_assignment(&self, local: mir::Local) -> Option<Location> {
77 let location = self.first_assignment[local];
78 if location.block.index() < self.fx.mir.basic_blocks().len() {
85 fn not_ssa(&mut self, local: mir::Local) {
86 debug!("marking {:?} as non-SSA", local);
87 self.non_ssa_locals.insert(local);
90 fn assign(&mut self, local: mir::Local, location: Location) {
91 if self.first_assignment(local).is_some() {
94 self.first_assignment[local] = location;
100 place_ref: &mir::PlaceRef<'_, 'tcx>,
101 context: PlaceContext,
106 if let [proj_base @ .., elem] = place_ref.projection {
107 let mut base_context = if context.is_mutating_use() {
108 PlaceContext::MutatingUse(MutatingUseContext::Projection)
110 PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection)
113 // Allow uses of projections that are ZSTs or from scalar fields.
114 let is_consume = match context {
115 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy)
116 | PlaceContext::NonMutatingUse(NonMutatingUseContext::Move) => true,
121 mir::Place::ty_from(place_ref.local, proj_base, *self.fx.mir, cx.tcx());
122 let base_ty = self.fx.monomorphize(&base_ty);
124 // ZSTs don't require any actual memory access.
125 let elem_ty = base_ty.projection_ty(cx.tcx(), elem).ty;
126 let elem_ty = self.fx.monomorphize(&elem_ty);
127 let span = self.fx.mir.local_decls[place_ref.local].source_info.span;
128 if cx.spanned_layout_of(elem_ty, span).is_zst() {
132 if let mir::ProjectionElem::Field(..) = elem {
133 let layout = cx.spanned_layout_of(base_ty.ty, span);
134 if cx.is_backend_immediate(layout) || cx.is_backend_scalar_pair(layout) {
135 // Recurse with the same context, instead of `Projection`,
136 // potentially stopping at non-operand projections,
137 // which would trigger `not_ssa` on locals.
138 base_context = context;
143 if let mir::ProjectionElem::Deref = elem {
144 // Deref projections typically only read the pointer.
145 // (the exception being `VarDebugInfo` contexts, handled below)
146 base_context = PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy);
148 // Indirect debuginfo requires going through memory, that only
149 // the debugger accesses, following our emitted DWARF pointer ops.
151 // FIXME(eddyb) Investigate the possibility of relaxing this, but
152 // note that `llvm.dbg.declare` *must* be used for indirect places,
153 // even if we start using `llvm.dbg.value` for all other cases,
154 // as we don't necessarily know when the value changes, but only
155 // where it lives in memory.
157 // It's possible `llvm.dbg.declare` could support starting from
158 // a pointer that doesn't point to an `alloca`, but this would
159 // only be useful if we know the pointer being `Deref`'d comes
160 // from an immutable place, and if `llvm.dbg.declare` calls
161 // must be at the very start of the function, then only function
162 // arguments could contain such pointers.
163 if context == PlaceContext::NonUse(NonUseContext::VarDebugInfo) {
164 // We use `NonUseContext::VarDebugInfo` for the base,
165 // which might not force the base local to memory,
166 // so we have to do it manually.
167 self.visit_local(&place_ref.local, context, location);
171 // `NonUseContext::VarDebugInfo` needs to flow all the
172 // way down to the base local (see `visit_local`).
173 if context == PlaceContext::NonUse(NonUseContext::VarDebugInfo) {
174 base_context = context;
178 &mir::PlaceRef { local: place_ref.local, projection: proj_base },
182 // HACK(eddyb) this emulates the old `visit_projection_elem`, this
183 // entire `visit_place`-like `process_place` method should be rewritten,
184 // now that we have moved to the "slice of projections" representation.
185 if let mir::ProjectionElem::Index(local) = elem {
188 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
193 // FIXME this is super_place code, is repeated here to avoid cloning place or changing
195 let mut context = context;
197 if !place_ref.projection.is_empty() {
198 context = if context.is_mutating_use() {
199 PlaceContext::MutatingUse(MutatingUseContext::Projection)
201 PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection)
205 self.visit_place_base(&place_ref.local, context, location);
206 self.visit_projection(&place_ref.local, place_ref.projection, context, location);
211 impl<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> Visitor<'tcx>
212 for LocalAnalyzer<'mir, 'a, 'tcx, Bx>
216 place: &mir::Place<'tcx>,
217 rvalue: &mir::Rvalue<'tcx>,
220 debug!("visit_assign(place={:?}, rvalue={:?})", place, rvalue);
222 if let Some(index) = place.as_local() {
223 self.assign(index, location);
224 let decl_span = self.fx.mir.local_decls[index].source_info.span;
225 if !self.fx.rvalue_creates_operand(rvalue, decl_span) {
229 self.visit_place(place, PlaceContext::MutatingUse(MutatingUseContext::Store), location);
232 self.visit_rvalue(rvalue, location);
235 fn visit_terminator_kind(&mut self, kind: &mir::TerminatorKind<'tcx>, location: Location) {
236 let check = match *kind {
237 mir::TerminatorKind::Call { func: mir::Operand::Constant(ref c), ref args, .. } => {
238 match c.literal.ty.kind {
239 ty::FnDef(did, _) => Some((did, args)),
245 if let Some((def_id, args)) = check {
246 if Some(def_id) == self.fx.cx.tcx().lang_items().box_free_fn() {
247 // box_free(x) shares with `drop x` the property that it
248 // is not guaranteed to be statically dominated by the
249 // definition of x, so x must always be in an alloca.
250 if let mir::Operand::Move(ref place) = args[0] {
253 PlaceContext::MutatingUse(MutatingUseContext::Drop),
260 self.super_terminator_kind(kind, location);
263 fn visit_place(&mut self, place: &mir::Place<'tcx>, context: PlaceContext, location: Location) {
264 debug!("visit_place(place={:?}, context={:?})", place, context);
265 self.process_place(&place.as_ref(), context, location);
268 fn visit_local(&mut self, &local: &mir::Local, context: PlaceContext, location: Location) {
270 PlaceContext::MutatingUse(MutatingUseContext::Call) => {
271 self.assign(local, location);
274 PlaceContext::NonUse(_) | PlaceContext::MutatingUse(MutatingUseContext::Retag) => {}
276 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy)
277 | PlaceContext::NonMutatingUse(NonMutatingUseContext::Move) => {
278 // Reads from uninitialized variables (e.g., in dead code, after
279 // optimizations) require locals to be in (uninitialized) memory.
280 // N.B., there can be uninitialized reads of a local visited after
281 // an assignment to that local, if they happen on disjoint paths.
282 let ssa_read = match self.first_assignment(local) {
283 Some(assignment_location) => {
284 assignment_location.dominates(location, &self.dominators)
293 PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect)
294 | PlaceContext::MutatingUse(MutatingUseContext::Store)
295 | PlaceContext::MutatingUse(MutatingUseContext::AsmOutput)
296 | PlaceContext::MutatingUse(MutatingUseContext::Borrow)
297 | PlaceContext::MutatingUse(MutatingUseContext::AddressOf)
298 | PlaceContext::MutatingUse(MutatingUseContext::Projection)
299 | PlaceContext::NonMutatingUse(NonMutatingUseContext::SharedBorrow)
300 | PlaceContext::NonMutatingUse(NonMutatingUseContext::UniqueBorrow)
301 | PlaceContext::NonMutatingUse(NonMutatingUseContext::ShallowBorrow)
302 | PlaceContext::NonMutatingUse(NonMutatingUseContext::AddressOf)
303 | PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection) => {
307 PlaceContext::MutatingUse(MutatingUseContext::Drop) => {
308 let ty = self.fx.mir.local_decls[local].ty;
309 let ty = self.fx.monomorphize(&ty);
311 // Only need the place if we're actually dropping it.
312 if self.fx.cx.type_needs_drop(ty) {
320 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
321 pub enum CleanupKind {
324 Internal { funclet: mir::BasicBlock },
328 pub fn funclet_bb(self, for_bb: mir::BasicBlock) -> Option<mir::BasicBlock> {
330 CleanupKind::NotCleanup => None,
331 CleanupKind::Funclet => Some(for_bb),
332 CleanupKind::Internal { funclet } => Some(funclet),
337 pub fn cleanup_kinds(mir: &mir::Body<'_>) -> IndexVec<mir::BasicBlock, CleanupKind> {
338 fn discover_masters<'tcx>(
339 result: &mut IndexVec<mir::BasicBlock, CleanupKind>,
340 mir: &mir::Body<'tcx>,
342 for (bb, data) in mir.basic_blocks().iter_enumerated() {
343 match data.terminator().kind {
344 TerminatorKind::Goto { .. }
345 | TerminatorKind::Resume
346 | TerminatorKind::Abort
347 | TerminatorKind::Return
348 | TerminatorKind::GeneratorDrop
349 | TerminatorKind::Unreachable
350 | TerminatorKind::SwitchInt { .. }
351 | TerminatorKind::Yield { .. }
352 | TerminatorKind::FalseEdges { .. }
353 | TerminatorKind::FalseUnwind { .. } => { /* nothing to do */ }
354 TerminatorKind::Call { cleanup: unwind, .. }
355 | TerminatorKind::Assert { cleanup: unwind, .. }
356 | TerminatorKind::DropAndReplace { unwind, .. }
357 | TerminatorKind::Drop { unwind, .. } => {
358 if let Some(unwind) = unwind {
360 "cleanup_kinds: {:?}/{:?} registering {:?} as funclet",
363 result[unwind] = CleanupKind::Funclet;
370 fn propagate<'tcx>(result: &mut IndexVec<mir::BasicBlock, CleanupKind>, mir: &mir::Body<'tcx>) {
371 let mut funclet_succs = IndexVec::from_elem(None, mir.basic_blocks());
373 let mut set_successor = |funclet: mir::BasicBlock, succ| match funclet_succs[funclet] {
374 ref mut s @ None => {
375 debug!("set_successor: updating successor of {:?} to {:?}", funclet, succ);
382 "funclet {:?} has 2 parents - {:?} and {:?}",
391 for (bb, data) in traversal::reverse_postorder(mir) {
392 let funclet = match result[bb] {
393 CleanupKind::NotCleanup => continue,
394 CleanupKind::Funclet => bb,
395 CleanupKind::Internal { funclet } => funclet,
399 "cleanup_kinds: {:?}/{:?}/{:?} propagating funclet {:?}",
400 bb, data, result[bb], funclet
403 for &succ in data.terminator().successors() {
404 let kind = result[succ];
405 debug!("cleanup_kinds: propagating {:?} to {:?}/{:?}", funclet, succ, kind);
407 CleanupKind::NotCleanup => {
408 result[succ] = CleanupKind::Internal { funclet };
410 CleanupKind::Funclet => {
412 set_successor(funclet, succ);
415 CleanupKind::Internal { funclet: succ_funclet } => {
416 if funclet != succ_funclet {
417 // `succ` has 2 different funclet going into it, so it must
418 // be a funclet by itself.
421 "promoting {:?} to a funclet and updating {:?}",
424 result[succ] = CleanupKind::Funclet;
425 set_successor(succ_funclet, succ);
426 set_successor(funclet, succ);
434 let mut result = IndexVec::from_elem(CleanupKind::NotCleanup, mir.basic_blocks());
436 discover_masters(&mut result, mir);
437 propagate(&mut result, mir);
438 debug!("cleanup_kinds: result={:?}", result);