]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_ssa/src/mir/analyze.rs
8a22a74f97c34ad23577e3fc971a279c7531ef3f
[rust.git] / compiler / rustc_codegen_ssa / src / mir / analyze.rs
1 //! An analysis to determine which locals require allocas and
2 //! which do not.
3
4 use super::FunctionCx;
5 use crate::traits::*;
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;
14
15 pub fn non_ssa_locals<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
16     fx: &FunctionCx<'a, 'tcx, Bx>,
17 ) -> BitSet<mir::Local> {
18     let mir = fx.mir;
19     let mut analyzer = LocalAnalyzer::new(fx);
20
21     for (bb, data) in mir.basic_blocks().iter_enumerated() {
22         analyzer.visit_basic_block_data(bb, data);
23     }
24
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.
34         } else {
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);
42         }
43     }
44
45     analyzer.non_ssa_locals
46 }
47
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>,
55 }
56
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 {
62             fx,
63             dominators,
64             non_ssa_locals: BitSet::new_empty(fx.mir.local_decls.len()),
65             first_assignment: IndexVec::from_elem(invalid_location, &fx.mir.local_decls),
66         };
67
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();
71         }
72
73         analyzer
74     }
75
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() {
79             Some(location)
80         } else {
81             None
82         }
83     }
84
85     fn not_ssa(&mut self, local: mir::Local) {
86         debug!("marking {:?} as non-SSA", local);
87         self.non_ssa_locals.insert(local);
88     }
89
90     fn assign(&mut self, local: mir::Local, location: Location) {
91         if self.first_assignment(local).is_some() {
92             self.not_ssa(local);
93         } else {
94             self.first_assignment[local] = location;
95         }
96     }
97
98     fn process_place(
99         &mut self,
100         place_ref: &mir::PlaceRef<'tcx>,
101         context: PlaceContext,
102         location: Location,
103     ) {
104         let cx = self.fx.cx;
105
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)
109             } else {
110                 PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection)
111             };
112
113             // Allow uses of projections that are ZSTs or from scalar fields.
114             let is_consume = matches!(
115                 context,
116                 PlaceContext::NonMutatingUse(
117                     NonMutatingUseContext::Copy | NonMutatingUseContext::Move,
118                 )
119             );
120             if is_consume {
121                 let base_ty = place_base.ty(self.fx.mir, cx.tcx());
122                 let base_ty = self.fx.monomorphize(base_ty);
123
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() {
128                     return;
129                 }
130
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;
138                     }
139                 }
140             }
141
142             if let mir::ProjectionElem::Deref = elem {
143                 // Deref projections typically only read the pointer.
144                 base_context = PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy);
145             }
146
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 {
152                 self.visit_local(
153                     &local,
154                     PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
155                     location,
156                 );
157             }
158         } else {
159             self.visit_local(&place_ref.local, context, location);
160         }
161     }
162 }
163
164 impl<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> Visitor<'tcx>
165     for LocalAnalyzer<'mir, 'a, 'tcx, Bx>
166 {
167     fn visit_assign(
168         &mut self,
169         place: &mir::Place<'tcx>,
170         rvalue: &mir::Rvalue<'tcx>,
171         location: Location,
172     ) {
173         debug!("visit_assign(place={:?}, rvalue={:?})", place, rvalue);
174
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) {
179                 self.not_ssa(index);
180             }
181         } else {
182             self.visit_place(place, PlaceContext::MutatingUse(MutatingUseContext::Store), location);
183         }
184
185         self.visit_rvalue(rvalue, location);
186     }
187
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);
191     }
192
193     fn visit_local(&mut self, &local: &mir::Local, context: PlaceContext, location: Location) {
194         match context {
195             PlaceContext::MutatingUse(MutatingUseContext::Call)
196             | PlaceContext::MutatingUse(MutatingUseContext::Yield) => {
197                 self.assign(local, location);
198             }
199
200             PlaceContext::NonUse(_) | PlaceContext::MutatingUse(MutatingUseContext::Retag) => {}
201
202             PlaceContext::NonMutatingUse(
203                 NonMutatingUseContext::Copy | NonMutatingUseContext::Move,
204             ) => {
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)
212                     }
213                     None => {
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);
220
221                         let is_zst = self.fx.cx.layout_of(ty).is_zst();
222                         debug!("is_zst: {}", is_zst);
223                         is_zst
224                     }
225                 };
226                 if !ssa_read {
227                     self.not_ssa(local);
228                 }
229             }
230
231             PlaceContext::MutatingUse(
232                 MutatingUseContext::Store
233                 | MutatingUseContext::AsmOutput
234                 | MutatingUseContext::Borrow
235                 | MutatingUseContext::AddressOf
236                 | MutatingUseContext::Projection,
237             )
238             | PlaceContext::NonMutatingUse(
239                 NonMutatingUseContext::Inspect
240                 | NonMutatingUseContext::SharedBorrow
241                 | NonMutatingUseContext::UniqueBorrow
242                 | NonMutatingUseContext::ShallowBorrow
243                 | NonMutatingUseContext::AddressOf
244                 | NonMutatingUseContext::Projection,
245             ) => {
246                 self.not_ssa(local);
247             }
248
249             PlaceContext::MutatingUse(MutatingUseContext::Drop) => {
250                 let ty = self.fx.mir.local_decls[local].ty;
251                 let ty = self.fx.monomorphize(ty);
252
253                 // Only need the place if we're actually dropping it.
254                 if self.fx.cx.type_needs_drop(ty) {
255                     self.not_ssa(local);
256                 }
257             }
258         }
259     }
260 }
261
262 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
263 pub enum CleanupKind {
264     NotCleanup,
265     Funclet,
266     Internal { funclet: mir::BasicBlock },
267 }
268
269 impl CleanupKind {
270     pub fn funclet_bb(self, for_bb: mir::BasicBlock) -> Option<mir::BasicBlock> {
271         match self {
272             CleanupKind::NotCleanup => None,
273             CleanupKind::Funclet => Some(for_bb),
274             CleanupKind::Internal { funclet } => Some(funclet),
275         }
276     }
277 }
278
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>,
283     ) {
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 {
302                         debug!(
303                             "cleanup_kinds: {:?}/{:?} registering {:?} as funclet",
304                             bb, data, unwind
305                         );
306                         result[unwind] = CleanupKind::Funclet;
307                     }
308                 }
309             }
310         }
311     }
312
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());
315
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);
319                 *s = Some(succ);
320             }
321             Some(s) => {
322                 if s != succ {
323                     span_bug!(
324                         mir.span,
325                         "funclet {:?} has 2 parents - {:?} and {:?}",
326                         funclet,
327                         s,
328                         succ
329                     );
330                 }
331             }
332         };
333
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,
339             };
340
341             debug!(
342                 "cleanup_kinds: {:?}/{:?}/{:?} propagating funclet {:?}",
343                 bb, data, result[bb], funclet
344             );
345
346             for &succ in data.terminator().successors() {
347                 let kind = result[succ];
348                 debug!("cleanup_kinds: propagating {:?} to {:?}/{:?}", funclet, succ, kind);
349                 match kind {
350                     CleanupKind::NotCleanup => {
351                         result[succ] = CleanupKind::Internal { funclet };
352                     }
353                     CleanupKind::Funclet => {
354                         if funclet != succ {
355                             set_successor(funclet, succ);
356                         }
357                     }
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.
362
363                             debug!(
364                                 "promoting {:?} to a funclet and updating {:?}",
365                                 succ, succ_funclet
366                             );
367                             result[succ] = CleanupKind::Funclet;
368                             set_successor(succ_funclet, succ);
369                             set_successor(funclet, succ);
370                         }
371                     }
372                 }
373             }
374         }
375     }
376
377     let mut result = IndexVec::from_elem(CleanupKind::NotCleanup, mir.basic_blocks());
378
379     discover_masters(&mut result, mir);
380     propagate(&mut result, mir);
381     debug!("cleanup_kinds: result={:?}", result);
382     result
383 }