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};
11 use rustc::session::config::DebugInfo;
13 use rustc::ty::layout::{HasTyCtxt, LayoutOf};
14 use rustc_data_structures::graph::dominators::Dominators;
15 use rustc_index::bit_set::BitSet;
16 use rustc_index::vec::{Idx, IndexVec};
17 use rustc_span::DUMMY_SP;
19 pub fn non_ssa_locals<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
20 fx: &FunctionCx<'a, 'tcx, Bx>,
21 ) -> BitSet<mir::Local> {
23 let mut analyzer = LocalAnalyzer::new(fx);
25 analyzer.visit_body(mir);
27 for (local, decl) in mir.local_decls.iter_enumerated() {
28 // FIXME(eddyb): We should figure out how to use llvm.dbg.value instead
29 // of putting everything in allocas just so we can use llvm.dbg.declare.
30 if fx.cx.sess().opts.debuginfo == DebugInfo::Full {
31 if fx.mir.local_kind(local) == mir::LocalKind::Arg {
32 analyzer.not_ssa(local);
37 let ty = fx.monomorphize(&decl.ty);
38 debug!("local {:?} has type `{}`", local, ty);
39 let layout = fx.cx.spanned_layout_of(ty, decl.source_info.span);
40 if fx.cx.is_backend_immediate(layout) {
41 // These sorts of types are immediates that we can store
42 // in an Value without an alloca.
43 } else if fx.cx.is_backend_scalar_pair(layout) {
44 // We allow pairs and uses of any of their 2 fields.
46 // These sorts of types require an alloca. Note that
47 // is_llvm_immediate() may *still* be true, particularly
48 // for newtypes, but we currently force some types
49 // (e.g., structs) into an alloca unconditionally, just so
50 // that we don't have to deal with having two pathways
51 // (gep vs extractvalue etc).
52 analyzer.not_ssa(local);
56 analyzer.non_ssa_locals
59 struct LocalAnalyzer<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> {
60 fx: &'mir FunctionCx<'a, 'tcx, Bx>,
61 dominators: Dominators<mir::BasicBlock>,
62 non_ssa_locals: BitSet<mir::Local>,
63 // The location of the first visited direct assignment to each
64 // local, or an invalid location (out of bounds `block` index).
65 first_assignment: IndexVec<mir::Local, Location>,
68 impl<Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'tcx, Bx> {
69 fn new(fx: &'mir FunctionCx<'a, 'tcx, Bx>) -> Self {
70 let invalid_location = mir::BasicBlock::new(fx.mir.basic_blocks().len()).start_location();
71 let dominators = fx.mir.dominators();
72 let mut analyzer = LocalAnalyzer {
75 non_ssa_locals: BitSet::new_empty(fx.mir.local_decls.len()),
76 first_assignment: IndexVec::from_elem(invalid_location, &fx.mir.local_decls),
79 // Arguments get assigned to by means of the function being called
80 for arg in fx.mir.args_iter() {
81 analyzer.first_assignment[arg] = mir::START_BLOCK.start_location();
87 fn first_assignment(&self, local: mir::Local) -> Option<Location> {
88 let location = self.first_assignment[local];
89 if location.block.index() < self.fx.mir.basic_blocks().len() {
96 fn not_ssa(&mut self, local: mir::Local) {
97 debug!("marking {:?} as non-SSA", local);
98 self.non_ssa_locals.insert(local);
101 fn assign(&mut self, local: mir::Local, location: Location) {
102 if self.first_assignment(local).is_some() {
105 self.first_assignment[local] = location;
111 place_ref: &mir::PlaceRef<'_, 'tcx>,
112 context: PlaceContext,
117 if let [proj_base @ .., elem] = place_ref.projection {
118 let mut base_context = if context.is_mutating_use() {
119 PlaceContext::MutatingUse(MutatingUseContext::Projection)
121 PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection)
124 // Allow uses of projections that are ZSTs or from scalar fields.
125 let is_consume = match context {
126 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy)
127 | PlaceContext::NonMutatingUse(NonMutatingUseContext::Move) => true,
132 mir::Place::ty_from(place_ref.base, proj_base, *self.fx.mir, cx.tcx());
133 let base_ty = self.fx.monomorphize(&base_ty);
135 // ZSTs don't require any actual memory access.
136 let elem_ty = base_ty.projection_ty(cx.tcx(), elem).ty;
137 let elem_ty = self.fx.monomorphize(&elem_ty);
138 let span = if let mir::PlaceBase::Local(index) = place_ref.base {
139 self.fx.mir.local_decls[*index].source_info.span
143 if cx.spanned_layout_of(elem_ty, span).is_zst() {
147 if let mir::ProjectionElem::Field(..) = elem {
148 let layout = cx.spanned_layout_of(base_ty.ty, span);
149 if cx.is_backend_immediate(layout) || cx.is_backend_scalar_pair(layout) {
150 // Recurse with the same context, instead of `Projection`,
151 // potentially stopping at non-operand projections,
152 // which would trigger `not_ssa` on locals.
153 base_context = context;
158 if let mir::ProjectionElem::Deref = elem {
159 // Deref projections typically only read the pointer.
160 // (the exception being `VarDebugInfo` contexts, handled below)
161 base_context = PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy);
163 // Indirect debuginfo requires going through memory, that only
164 // the debugger accesses, following our emitted DWARF pointer ops.
166 // FIXME(eddyb) Investigate the possibility of relaxing this, but
167 // note that `llvm.dbg.declare` *must* be used for indirect places,
168 // even if we start using `llvm.dbg.value` for all other cases,
169 // as we don't necessarily know when the value changes, but only
170 // where it lives in memory.
172 // It's possible `llvm.dbg.declare` could support starting from
173 // a pointer that doesn't point to an `alloca`, but this would
174 // only be useful if we know the pointer being `Deref`'d comes
175 // from an immutable place, and if `llvm.dbg.declare` calls
176 // must be at the very start of the function, then only function
177 // arguments could contain such pointers.
178 if context == PlaceContext::NonUse(NonUseContext::VarDebugInfo) {
179 // We use `NonUseContext::VarDebugInfo` for the base,
180 // which might not force the base local to memory,
181 // so we have to do it manually.
182 if let mir::PlaceBase::Local(local) = place_ref.base {
183 self.visit_local(&local, context, location);
188 // `NonUseContext::VarDebugInfo` needs to flow all the
189 // way down to the base local (see `visit_local`).
190 if context == PlaceContext::NonUse(NonUseContext::VarDebugInfo) {
191 base_context = context;
195 &mir::PlaceRef { base: place_ref.base, projection: proj_base },
199 // HACK(eddyb) this emulates the old `visit_projection_elem`, this
200 // entire `visit_place`-like `process_place` method should be rewritten,
201 // now that we have moved to the "slice of projections" representation.
202 if let mir::ProjectionElem::Index(local) = elem {
205 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
210 // FIXME this is super_place code, is repeated here to avoid cloning place or changing
212 let mut context = context;
214 if !place_ref.projection.is_empty() {
215 context = if context.is_mutating_use() {
216 PlaceContext::MutatingUse(MutatingUseContext::Projection)
218 PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection)
222 self.visit_place_base(place_ref.base, context, location);
223 self.visit_projection(place_ref.base, place_ref.projection, context, location);
228 impl<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> Visitor<'tcx>
229 for LocalAnalyzer<'mir, 'a, 'tcx, Bx>
233 place: &mir::Place<'tcx>,
234 rvalue: &mir::Rvalue<'tcx>,
237 debug!("visit_assign(place={:?}, rvalue={:?})", place, rvalue);
239 if let Some(index) = place.as_local() {
240 self.assign(index, location);
241 let decl_span = self.fx.mir.local_decls[index].source_info.span;
242 if !self.fx.rvalue_creates_operand(rvalue, decl_span) {
246 self.visit_place(place, PlaceContext::MutatingUse(MutatingUseContext::Store), location);
249 self.visit_rvalue(rvalue, location);
252 fn visit_terminator_kind(&mut self, kind: &mir::TerminatorKind<'tcx>, location: Location) {
253 let check = match *kind {
254 mir::TerminatorKind::Call { func: mir::Operand::Constant(ref c), ref args, .. } => {
255 match c.literal.ty.kind {
256 ty::FnDef(did, _) => Some((did, args)),
262 if let Some((def_id, args)) = check {
263 if Some(def_id) == self.fx.cx.tcx().lang_items().box_free_fn() {
264 // box_free(x) shares with `drop x` the property that it
265 // is not guaranteed to be statically dominated by the
266 // definition of x, so x must always be in an alloca.
267 if let mir::Operand::Move(ref place) = args[0] {
270 PlaceContext::MutatingUse(MutatingUseContext::Drop),
277 self.super_terminator_kind(kind, location);
280 fn visit_place(&mut self, place: &mir::Place<'tcx>, context: PlaceContext, location: Location) {
281 debug!("visit_place(place={:?}, context={:?})", place, context);
282 self.process_place(&place.as_ref(), context, location);
285 fn visit_local(&mut self, &local: &mir::Local, context: PlaceContext, location: Location) {
287 PlaceContext::MutatingUse(MutatingUseContext::Call) => {
288 self.assign(local, location);
291 PlaceContext::NonUse(NonUseContext::VarDebugInfo) => {
292 // We need to keep locals in `alloca`s for debuginfo.
293 // FIXME(eddyb): We should figure out how to use `llvm.dbg.value` instead
294 // of putting everything in allocas just so we can use `llvm.dbg.declare`.
295 if self.fx.cx.sess().opts.debuginfo == DebugInfo::Full {
300 PlaceContext::NonUse(_) | PlaceContext::MutatingUse(MutatingUseContext::Retag) => {}
302 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy)
303 | PlaceContext::NonMutatingUse(NonMutatingUseContext::Move) => {
304 // Reads from uninitialized variables (e.g., in dead code, after
305 // optimizations) require locals to be in (uninitialized) memory.
306 // N.B., there can be uninitialized reads of a local visited after
307 // an assignment to that local, if they happen on disjoint paths.
308 let ssa_read = match self.first_assignment(local) {
309 Some(assignment_location) => {
310 assignment_location.dominates(location, &self.dominators)
319 PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect)
320 | PlaceContext::MutatingUse(MutatingUseContext::Store)
321 | PlaceContext::MutatingUse(MutatingUseContext::AsmOutput)
322 | PlaceContext::MutatingUse(MutatingUseContext::Borrow)
323 | PlaceContext::MutatingUse(MutatingUseContext::AddressOf)
324 | PlaceContext::MutatingUse(MutatingUseContext::Projection)
325 | PlaceContext::NonMutatingUse(NonMutatingUseContext::SharedBorrow)
326 | PlaceContext::NonMutatingUse(NonMutatingUseContext::UniqueBorrow)
327 | PlaceContext::NonMutatingUse(NonMutatingUseContext::ShallowBorrow)
328 | PlaceContext::NonMutatingUse(NonMutatingUseContext::AddressOf)
329 | PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection) => {
333 PlaceContext::MutatingUse(MutatingUseContext::Drop) => {
334 let ty = self.fx.mir.local_decls[local].ty;
335 let ty = self.fx.monomorphize(&ty);
337 // Only need the place if we're actually dropping it.
338 if self.fx.cx.type_needs_drop(ty) {
346 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
347 pub enum CleanupKind {
350 Internal { funclet: mir::BasicBlock },
354 pub fn funclet_bb(self, for_bb: mir::BasicBlock) -> Option<mir::BasicBlock> {
356 CleanupKind::NotCleanup => None,
357 CleanupKind::Funclet => Some(for_bb),
358 CleanupKind::Internal { funclet } => Some(funclet),
363 pub fn cleanup_kinds(mir: &mir::Body<'_>) -> IndexVec<mir::BasicBlock, CleanupKind> {
364 fn discover_masters<'tcx>(
365 result: &mut IndexVec<mir::BasicBlock, CleanupKind>,
366 mir: &mir::Body<'tcx>,
368 for (bb, data) in mir.basic_blocks().iter_enumerated() {
369 match data.terminator().kind {
370 TerminatorKind::Goto { .. }
371 | TerminatorKind::Resume
372 | TerminatorKind::Abort
373 | TerminatorKind::Return
374 | TerminatorKind::GeneratorDrop
375 | TerminatorKind::Unreachable
376 | TerminatorKind::SwitchInt { .. }
377 | TerminatorKind::Yield { .. }
378 | TerminatorKind::FalseEdges { .. }
379 | TerminatorKind::FalseUnwind { .. } => { /* nothing to do */ }
380 TerminatorKind::Call { cleanup: unwind, .. }
381 | TerminatorKind::Assert { cleanup: unwind, .. }
382 | TerminatorKind::DropAndReplace { unwind, .. }
383 | TerminatorKind::Drop { unwind, .. } => {
384 if let Some(unwind) = unwind {
386 "cleanup_kinds: {:?}/{:?} registering {:?} as funclet",
389 result[unwind] = CleanupKind::Funclet;
396 fn propagate<'tcx>(result: &mut IndexVec<mir::BasicBlock, CleanupKind>, mir: &mir::Body<'tcx>) {
397 let mut funclet_succs = IndexVec::from_elem(None, mir.basic_blocks());
399 let mut set_successor = |funclet: mir::BasicBlock, succ| match funclet_succs[funclet] {
400 ref mut s @ None => {
401 debug!("set_successor: updating successor of {:?} to {:?}", funclet, succ);
408 "funclet {:?} has 2 parents - {:?} and {:?}",
417 for (bb, data) in traversal::reverse_postorder(mir) {
418 let funclet = match result[bb] {
419 CleanupKind::NotCleanup => continue,
420 CleanupKind::Funclet => bb,
421 CleanupKind::Internal { funclet } => funclet,
425 "cleanup_kinds: {:?}/{:?}/{:?} propagating funclet {:?}",
426 bb, data, result[bb], funclet
429 for &succ in data.terminator().successors() {
430 let kind = result[succ];
431 debug!("cleanup_kinds: propagating {:?} to {:?}/{:?}", funclet, succ, kind);
433 CleanupKind::NotCleanup => {
434 result[succ] = CleanupKind::Internal { funclet };
436 CleanupKind::Funclet => {
438 set_successor(funclet, succ);
441 CleanupKind::Internal { funclet: succ_funclet } => {
442 if funclet != succ_funclet {
443 // `succ` has 2 different funclet going into it, so it must
444 // be a funclet by itself.
447 "promoting {:?} to a funclet and updating {:?}",
450 result[succ] = CleanupKind::Funclet;
451 set_successor(succ_funclet, succ);
452 set_successor(funclet, succ);
460 let mut result = IndexVec::from_elem(CleanupKind::NotCleanup, mir.basic_blocks());
462 discover_masters(&mut result, mir);
463 propagate(&mut result, mir);
464 debug!("cleanup_kinds: result={:?}", result);