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};
12 use rustc_middle::ty::layout::HasTyCtxt;
13 use rustc_target::abi::LayoutOf;
15 pub fn non_ssa_locals<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
16 fx: &FunctionCx<'a, 'tcx, Bx>,
17 ) -> BitSet<mir::Local> {
19 let mut analyzer = LocalAnalyzer::new(fx);
21 for (bb, data) in mir.basic_blocks().iter_enumerated() {
22 analyzer.visit_basic_block_data(bb, data);
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 Some((place_base, elem)) = place_ref.last_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 = matches!(
116 PlaceContext::NonMutatingUse(
117 NonMutatingUseContext::Copy | NonMutatingUseContext::Move,
121 let base_ty = place_base.ty(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(), self.fx.monomorphize(elem)).ty;
126 let span = self.fx.mir.local_decls[place_ref.local].source_info.span;
127 if cx.spanned_layout_of(elem_ty, span).is_zst() {
131 if let mir::ProjectionElem::Field(..) = elem {
132 let layout = cx.spanned_layout_of(base_ty.ty, span);
133 if cx.is_backend_immediate(layout) || cx.is_backend_scalar_pair(layout) {
134 // Recurse with the same context, instead of `Projection`,
135 // potentially stopping at non-operand projections,
136 // which would trigger `not_ssa` on locals.
137 base_context = context;
142 if let mir::ProjectionElem::Deref = elem {
143 // Deref projections typically only read the pointer.
144 base_context = PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy);
147 self.process_place(&place_base, base_context, location);
148 // HACK(eddyb) this emulates the old `visit_projection_elem`, this
149 // entire `visit_place`-like `process_place` method should be rewritten,
150 // now that we have moved to the "slice of projections" representation.
151 if let mir::ProjectionElem::Index(local) = elem {
154 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
159 self.visit_local(&place_ref.local, context, location);
164 impl<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> Visitor<'tcx>
165 for LocalAnalyzer<'mir, 'a, 'tcx, Bx>
169 place: &mir::Place<'tcx>,
170 rvalue: &mir::Rvalue<'tcx>,
173 debug!("visit_assign(place={:?}, rvalue={:?})", place, rvalue);
175 if let Some(index) = place.as_local() {
176 self.assign(index, location);
177 let decl_span = self.fx.mir.local_decls[index].source_info.span;
178 if !self.fx.rvalue_creates_operand(rvalue, decl_span) {
182 self.visit_place(place, PlaceContext::MutatingUse(MutatingUseContext::Store), location);
185 self.visit_rvalue(rvalue, location);
188 fn visit_place(&mut self, place: &mir::Place<'tcx>, context: PlaceContext, location: Location) {
189 debug!("visit_place(place={:?}, context={:?})", place, context);
190 self.process_place(&place.as_ref(), context, location);
193 fn visit_local(&mut self, &local: &mir::Local, context: PlaceContext, location: Location) {
195 PlaceContext::MutatingUse(MutatingUseContext::Call)
196 | PlaceContext::MutatingUse(MutatingUseContext::Yield) => {
197 self.assign(local, location);
200 PlaceContext::NonUse(_) | PlaceContext::MutatingUse(MutatingUseContext::Retag) => {}
202 PlaceContext::NonMutatingUse(
203 NonMutatingUseContext::Copy | NonMutatingUseContext::Move,
205 // Reads from uninitialized variables (e.g., in dead code, after
206 // optimizations) require locals to be in (uninitialized) memory.
207 // N.B., there can be uninitialized reads of a local visited after
208 // an assignment to that local, if they happen on disjoint paths.
209 let ssa_read = match self.first_assignment(local) {
210 Some(assignment_location) => {
211 assignment_location.dominates(location, &self.dominators)
214 debug!("No first assignment found for {:?}", local);
215 // We have not seen any assignment to the local yet,
216 // but before marking not_ssa, check if it is a ZST,
217 // in which case we don't need to initialize the local.
218 let ty = self.fx.mir.local_decls[local].ty;
219 let ty = self.fx.monomorphize(ty);
221 let is_zst = self.fx.cx.layout_of(ty).is_zst();
222 debug!("is_zst: {}", is_zst);
231 PlaceContext::MutatingUse(
232 MutatingUseContext::Store
233 | MutatingUseContext::AsmOutput
234 | MutatingUseContext::Borrow
235 | MutatingUseContext::AddressOf
236 | MutatingUseContext::Projection,
238 | PlaceContext::NonMutatingUse(
239 NonMutatingUseContext::Inspect
240 | NonMutatingUseContext::SharedBorrow
241 | NonMutatingUseContext::UniqueBorrow
242 | NonMutatingUseContext::ShallowBorrow
243 | NonMutatingUseContext::AddressOf
244 | NonMutatingUseContext::Projection,
249 PlaceContext::MutatingUse(MutatingUseContext::Drop) => {
250 let ty = self.fx.mir.local_decls[local].ty;
251 let ty = self.fx.monomorphize(ty);
253 // Only need the place if we're actually dropping it.
254 if self.fx.cx.type_needs_drop(ty) {
262 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
263 pub enum CleanupKind {
266 Internal { funclet: mir::BasicBlock },
270 pub fn funclet_bb(self, for_bb: mir::BasicBlock) -> Option<mir::BasicBlock> {
272 CleanupKind::NotCleanup => None,
273 CleanupKind::Funclet => Some(for_bb),
274 CleanupKind::Internal { funclet } => Some(funclet),
279 pub fn cleanup_kinds(mir: &mir::Body<'_>) -> IndexVec<mir::BasicBlock, CleanupKind> {
280 fn discover_masters<'tcx>(
281 result: &mut IndexVec<mir::BasicBlock, CleanupKind>,
282 mir: &mir::Body<'tcx>,
284 for (bb, data) in mir.basic_blocks().iter_enumerated() {
285 match data.terminator().kind {
286 TerminatorKind::Goto { .. }
287 | TerminatorKind::Resume
288 | TerminatorKind::Abort
289 | TerminatorKind::Return
290 | TerminatorKind::GeneratorDrop
291 | TerminatorKind::Unreachable
292 | TerminatorKind::SwitchInt { .. }
293 | TerminatorKind::Yield { .. }
294 | TerminatorKind::FalseEdge { .. }
295 | TerminatorKind::FalseUnwind { .. }
296 | TerminatorKind::InlineAsm { .. } => { /* nothing to do */ }
297 TerminatorKind::Call { cleanup: unwind, .. }
298 | TerminatorKind::Assert { cleanup: unwind, .. }
299 | TerminatorKind::DropAndReplace { unwind, .. }
300 | TerminatorKind::Drop { unwind, .. } => {
301 if let Some(unwind) = unwind {
303 "cleanup_kinds: {:?}/{:?} registering {:?} as funclet",
306 result[unwind] = CleanupKind::Funclet;
313 fn propagate<'tcx>(result: &mut IndexVec<mir::BasicBlock, CleanupKind>, mir: &mir::Body<'tcx>) {
314 let mut funclet_succs = IndexVec::from_elem(None, mir.basic_blocks());
316 let mut set_successor = |funclet: mir::BasicBlock, succ| match funclet_succs[funclet] {
317 ref mut s @ None => {
318 debug!("set_successor: updating successor of {:?} to {:?}", funclet, succ);
325 "funclet {:?} has 2 parents - {:?} and {:?}",
334 for (bb, data) in traversal::reverse_postorder(mir) {
335 let funclet = match result[bb] {
336 CleanupKind::NotCleanup => continue,
337 CleanupKind::Funclet => bb,
338 CleanupKind::Internal { funclet } => funclet,
342 "cleanup_kinds: {:?}/{:?}/{:?} propagating funclet {:?}",
343 bb, data, result[bb], funclet
346 for &succ in data.terminator().successors() {
347 let kind = result[succ];
348 debug!("cleanup_kinds: propagating {:?} to {:?}/{:?}", funclet, succ, kind);
350 CleanupKind::NotCleanup => {
351 result[succ] = CleanupKind::Internal { funclet };
353 CleanupKind::Funclet => {
355 set_successor(funclet, succ);
358 CleanupKind::Internal { funclet: succ_funclet } => {
359 if funclet != succ_funclet {
360 // `succ` has 2 different funclet going into it, so it must
361 // be a funclet by itself.
364 "promoting {:?} to a funclet and updating {:?}",
367 result[succ] = CleanupKind::Funclet;
368 set_successor(succ_funclet, succ);
369 set_successor(funclet, succ);
377 let mut result = IndexVec::from_elem(CleanupKind::NotCleanup, mir.basic_blocks());
379 discover_masters(&mut result, mir);
380 propagate(&mut result, mir);
381 debug!("cleanup_kinds: result={:?}", result);