1 //! An analysis to determine which locals require allocas and
6 use rustc_data_structures::graph::dominators::Dominators;
7 use rustc_index::bit_set::BitSet;
8 use rustc_index::vec::{Idx, IndexVec};
9 use rustc_middle::mir::traversal;
10 use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor};
11 use rustc_middle::mir::{self, Location, TerminatorKind};
13 use rustc_middle::ty::layout::HasTyCtxt;
14 use rustc_target::abi::LayoutOf;
16 pub fn non_ssa_locals<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
17 fx: &FunctionCx<'a, 'tcx, Bx>,
18 ) -> BitSet<mir::Local> {
20 let mut analyzer = LocalAnalyzer::new(fx);
22 for (bb, data) in mir.basic_blocks().iter_enumerated() {
23 analyzer.visit_basic_block_data(bb, data);
26 for (local, decl) in mir.local_decls.iter_enumerated() {
27 let ty = fx.monomorphize(decl.ty);
28 debug!("local {:?} has type `{}`", local, ty);
29 let layout = fx.cx.spanned_layout_of(ty, decl.source_info.span);
30 if fx.cx.is_backend_immediate(layout) {
31 // These sorts of types are immediates that we can store
32 // in an Value without an alloca.
33 } else if fx.cx.is_backend_scalar_pair(layout) {
34 // We allow pairs and uses of any of their 2 fields.
36 // These sorts of types require an alloca. Note that
37 // is_llvm_immediate() may *still* be true, particularly
38 // for newtypes, but we currently force some types
39 // (e.g., structs) into an alloca unconditionally, just so
40 // that we don't have to deal with having two pathways
41 // (gep vs extractvalue etc).
42 analyzer.not_ssa(local);
46 analyzer.non_ssa_locals
49 struct LocalAnalyzer<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> {
50 fx: &'mir FunctionCx<'a, 'tcx, Bx>,
51 dominators: Dominators<mir::BasicBlock>,
52 non_ssa_locals: BitSet<mir::Local>,
53 // The location of the first visited direct assignment to each
54 // local, or an invalid location (out of bounds `block` index).
55 first_assignment: IndexVec<mir::Local, Location>,
58 impl<Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'tcx, Bx> {
59 fn new(fx: &'mir FunctionCx<'a, 'tcx, Bx>) -> Self {
60 let invalid_location = mir::BasicBlock::new(fx.mir.basic_blocks().len()).start_location();
61 let dominators = fx.mir.dominators();
62 let mut analyzer = LocalAnalyzer {
65 non_ssa_locals: BitSet::new_empty(fx.mir.local_decls.len()),
66 first_assignment: IndexVec::from_elem(invalid_location, &fx.mir.local_decls),
69 // Arguments get assigned to by means of the function being called
70 for arg in fx.mir.args_iter() {
71 analyzer.first_assignment[arg] = mir::START_BLOCK.start_location();
77 fn first_assignment(&self, local: mir::Local) -> Option<Location> {
78 let location = self.first_assignment[local];
79 if location.block.index() < self.fx.mir.basic_blocks().len() {
86 fn not_ssa(&mut self, local: mir::Local) {
87 debug!("marking {:?} as non-SSA", local);
88 self.non_ssa_locals.insert(local);
91 fn assign(&mut self, local: mir::Local, location: Location) {
92 if self.first_assignment(local).is_some() {
95 self.first_assignment[local] = location;
101 place_ref: &mir::PlaceRef<'tcx>,
102 context: PlaceContext,
107 if let Some((place_base, elem)) = place_ref.last_projection() {
108 let mut base_context = if context.is_mutating_use() {
109 PlaceContext::MutatingUse(MutatingUseContext::Projection)
111 PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection)
114 // Allow uses of projections that are ZSTs or from scalar fields.
115 let is_consume = matches!(
117 PlaceContext::NonMutatingUse(
118 NonMutatingUseContext::Copy | NonMutatingUseContext::Move,
122 let base_ty = place_base.ty(self.fx.mir, cx.tcx());
123 let base_ty = self.fx.monomorphize(base_ty);
125 // ZSTs don't require any actual memory access.
126 let elem_ty = base_ty.projection_ty(cx.tcx(), 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 base_context = PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy);
148 self.process_place(&place_base, base_context, location);
149 // HACK(eddyb) this emulates the old `visit_projection_elem`, this
150 // entire `visit_place`-like `process_place` method should be rewritten,
151 // now that we have moved to the "slice of projections" representation.
152 if let mir::ProjectionElem::Index(local) = elem {
155 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
160 self.visit_local(&place_ref.local, context, location);
165 impl<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> Visitor<'tcx>
166 for LocalAnalyzer<'mir, 'a, 'tcx, Bx>
170 place: &mir::Place<'tcx>,
171 rvalue: &mir::Rvalue<'tcx>,
174 debug!("visit_assign(place={:?}, rvalue={:?})", place, rvalue);
176 if let Some(index) = place.as_local() {
177 self.assign(index, location);
178 let decl_span = self.fx.mir.local_decls[index].source_info.span;
179 if !self.fx.rvalue_creates_operand(rvalue, decl_span) {
183 self.visit_place(place, PlaceContext::MutatingUse(MutatingUseContext::Store), location);
186 self.visit_rvalue(rvalue, location);
189 fn visit_terminator(&mut self, terminator: &mir::Terminator<'tcx>, location: Location) {
190 let check = match terminator.kind {
191 mir::TerminatorKind::Call { func: mir::Operand::Constant(ref c), ref args, .. } => {
192 match *c.ty().kind() {
193 ty::FnDef(did, _) => Some((did, args)),
199 if let Some((def_id, args)) = check {
200 if Some(def_id) == self.fx.cx.tcx().lang_items().box_free_fn() {
201 // box_free(x) shares with `drop x` the property that it
202 // is not guaranteed to be statically dominated by the
203 // definition of x, so x must always be in an alloca.
204 if let mir::Operand::Move(ref place) = args[0] {
207 PlaceContext::MutatingUse(MutatingUseContext::Drop),
214 self.super_terminator(terminator, location);
217 fn visit_place(&mut self, place: &mir::Place<'tcx>, context: PlaceContext, location: Location) {
218 debug!("visit_place(place={:?}, context={:?})", place, context);
219 self.process_place(&place.as_ref(), context, location);
222 fn visit_local(&mut self, &local: &mir::Local, context: PlaceContext, location: Location) {
224 PlaceContext::MutatingUse(MutatingUseContext::Call)
225 | PlaceContext::MutatingUse(MutatingUseContext::Yield) => {
226 self.assign(local, location);
229 PlaceContext::NonUse(_) | PlaceContext::MutatingUse(MutatingUseContext::Retag) => {}
231 PlaceContext::NonMutatingUse(
232 NonMutatingUseContext::Copy | NonMutatingUseContext::Move,
234 // Reads from uninitialized variables (e.g., in dead code, after
235 // optimizations) require locals to be in (uninitialized) memory.
236 // N.B., there can be uninitialized reads of a local visited after
237 // an assignment to that local, if they happen on disjoint paths.
238 let ssa_read = match self.first_assignment(local) {
239 Some(assignment_location) => {
240 assignment_location.dominates(location, &self.dominators)
243 debug!("No first assignment found for {:?}", local);
244 // We have not seen any assignment to the local yet,
245 // but before marking not_ssa, check if it is a ZST,
246 // in which case we don't need to initialize the local.
247 let ty = self.fx.mir.local_decls[local].ty;
248 let ty = self.fx.monomorphize(ty);
250 let is_zst = self.fx.cx.layout_of(ty).is_zst();
251 debug!("is_zst: {}", is_zst);
260 PlaceContext::MutatingUse(
261 MutatingUseContext::Store
262 | MutatingUseContext::AsmOutput
263 | MutatingUseContext::Borrow
264 | MutatingUseContext::AddressOf
265 | MutatingUseContext::Projection,
267 | PlaceContext::NonMutatingUse(
268 NonMutatingUseContext::Inspect
269 | NonMutatingUseContext::SharedBorrow
270 | NonMutatingUseContext::UniqueBorrow
271 | NonMutatingUseContext::ShallowBorrow
272 | NonMutatingUseContext::AddressOf
273 | NonMutatingUseContext::Projection,
278 PlaceContext::MutatingUse(MutatingUseContext::Drop) => {
279 let ty = self.fx.mir.local_decls[local].ty;
280 let ty = self.fx.monomorphize(ty);
282 // Only need the place if we're actually dropping it.
283 if self.fx.cx.type_needs_drop(ty) {
291 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
292 pub enum CleanupKind {
295 Internal { funclet: mir::BasicBlock },
299 pub fn funclet_bb(self, for_bb: mir::BasicBlock) -> Option<mir::BasicBlock> {
301 CleanupKind::NotCleanup => None,
302 CleanupKind::Funclet => Some(for_bb),
303 CleanupKind::Internal { funclet } => Some(funclet),
308 pub fn cleanup_kinds(mir: &mir::Body<'_>) -> IndexVec<mir::BasicBlock, CleanupKind> {
309 fn discover_masters<'tcx>(
310 result: &mut IndexVec<mir::BasicBlock, CleanupKind>,
311 mir: &mir::Body<'tcx>,
313 for (bb, data) in mir.basic_blocks().iter_enumerated() {
314 match data.terminator().kind {
315 TerminatorKind::Goto { .. }
316 | TerminatorKind::Resume
317 | TerminatorKind::Abort
318 | TerminatorKind::Return
319 | TerminatorKind::GeneratorDrop
320 | TerminatorKind::Unreachable
321 | TerminatorKind::SwitchInt { .. }
322 | TerminatorKind::Yield { .. }
323 | TerminatorKind::FalseEdge { .. }
324 | TerminatorKind::FalseUnwind { .. }
325 | TerminatorKind::InlineAsm { .. } => { /* nothing to do */ }
326 TerminatorKind::Call { cleanup: unwind, .. }
327 | TerminatorKind::Assert { cleanup: unwind, .. }
328 | TerminatorKind::DropAndReplace { unwind, .. }
329 | TerminatorKind::Drop { unwind, .. } => {
330 if let Some(unwind) = unwind {
332 "cleanup_kinds: {:?}/{:?} registering {:?} as funclet",
335 result[unwind] = CleanupKind::Funclet;
342 fn propagate<'tcx>(result: &mut IndexVec<mir::BasicBlock, CleanupKind>, mir: &mir::Body<'tcx>) {
343 let mut funclet_succs = IndexVec::from_elem(None, mir.basic_blocks());
345 let mut set_successor = |funclet: mir::BasicBlock, succ| match funclet_succs[funclet] {
346 ref mut s @ None => {
347 debug!("set_successor: updating successor of {:?} to {:?}", funclet, succ);
354 "funclet {:?} has 2 parents - {:?} and {:?}",
363 for (bb, data) in traversal::reverse_postorder(mir) {
364 let funclet = match result[bb] {
365 CleanupKind::NotCleanup => continue,
366 CleanupKind::Funclet => bb,
367 CleanupKind::Internal { funclet } => funclet,
371 "cleanup_kinds: {:?}/{:?}/{:?} propagating funclet {:?}",
372 bb, data, result[bb], funclet
375 for &succ in data.terminator().successors() {
376 let kind = result[succ];
377 debug!("cleanup_kinds: propagating {:?} to {:?}/{:?}", funclet, succ, kind);
379 CleanupKind::NotCleanup => {
380 result[succ] = CleanupKind::Internal { funclet };
382 CleanupKind::Funclet => {
384 set_successor(funclet, succ);
387 CleanupKind::Internal { funclet: succ_funclet } => {
388 if funclet != succ_funclet {
389 // `succ` has 2 different funclet going into it, so it must
390 // be a funclet by itself.
393 "promoting {:?} to a funclet and updating {:?}",
396 result[succ] = CleanupKind::Funclet;
397 set_successor(succ_funclet, succ);
398 set_successor(funclet, succ);
406 let mut result = IndexVec::from_elem(CleanupKind::NotCleanup, mir.basic_blocks());
408 discover_masters(&mut result, mir);
409 propagate(&mut result, mir);
410 debug!("cleanup_kinds: result={:?}", result);