]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_ssa/src/mir/analyze.rs
Remove check for projections in a branch without any
[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;
13 use rustc_middle::ty::layout::HasTyCtxt;
14 use rustc_target::abi::LayoutOf;
15
16 pub fn non_ssa_locals<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
17     fx: &FunctionCx<'a, 'tcx, Bx>,
18 ) -> BitSet<mir::Local> {
19     let mir = fx.mir;
20     let mut analyzer = LocalAnalyzer::new(fx);
21
22     for (bb, data) in mir.basic_blocks().iter_enumerated() {
23         analyzer.visit_basic_block_data(bb, data);
24     }
25
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.
35         } else {
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);
43         }
44     }
45
46     analyzer.non_ssa_locals
47 }
48
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>,
56 }
57
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 {
63             fx,
64             dominators,
65             non_ssa_locals: BitSet::new_empty(fx.mir.local_decls.len()),
66             first_assignment: IndexVec::from_elem(invalid_location, &fx.mir.local_decls),
67         };
68
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();
72         }
73
74         analyzer
75     }
76
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() {
80             Some(location)
81         } else {
82             None
83         }
84     }
85
86     fn not_ssa(&mut self, local: mir::Local) {
87         debug!("marking {:?} as non-SSA", local);
88         self.non_ssa_locals.insert(local);
89     }
90
91     fn assign(&mut self, local: mir::Local, location: Location) {
92         if self.first_assignment(local).is_some() {
93             self.not_ssa(local);
94         } else {
95             self.first_assignment[local] = location;
96         }
97     }
98
99     fn process_place(
100         &mut self,
101         place_ref: &mir::PlaceRef<'tcx>,
102         context: PlaceContext,
103         location: Location,
104     ) {
105         let cx = self.fx.cx;
106
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)
110             } else {
111                 PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection)
112             };
113
114             // Allow uses of projections that are ZSTs or from scalar fields.
115             let is_consume = matches!(
116                 context,
117                 PlaceContext::NonMutatingUse(
118                     NonMutatingUseContext::Copy | NonMutatingUseContext::Move,
119                 )
120             );
121             if is_consume {
122                 let base_ty = place_base.ty(self.fx.mir, cx.tcx());
123                 let base_ty = self.fx.monomorphize(base_ty);
124
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() {
129                     return;
130                 }
131
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;
139                     }
140                 }
141             }
142
143             if let mir::ProjectionElem::Deref = elem {
144                 // Deref projections typically only read the pointer.
145                 base_context = PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy);
146             }
147
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 {
153                 self.visit_local(
154                     &local,
155                     PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
156                     location,
157                 );
158             }
159         } else {
160             self.visit_local(&place_ref.local, context, location);
161         }
162     }
163 }
164
165 impl<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> Visitor<'tcx>
166     for LocalAnalyzer<'mir, 'a, 'tcx, Bx>
167 {
168     fn visit_assign(
169         &mut self,
170         place: &mir::Place<'tcx>,
171         rvalue: &mir::Rvalue<'tcx>,
172         location: Location,
173     ) {
174         debug!("visit_assign(place={:?}, rvalue={:?})", place, rvalue);
175
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) {
180                 self.not_ssa(index);
181             }
182         } else {
183             self.visit_place(place, PlaceContext::MutatingUse(MutatingUseContext::Store), location);
184         }
185
186         self.visit_rvalue(rvalue, location);
187     }
188
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)),
194                     _ => None,
195                 }
196             }
197             _ => None,
198         };
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] {
205                     self.visit_place(
206                         place,
207                         PlaceContext::MutatingUse(MutatingUseContext::Drop),
208                         location,
209                     );
210                 }
211             }
212         }
213
214         self.super_terminator(terminator, location);
215     }
216
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);
220     }
221
222     fn visit_local(&mut self, &local: &mir::Local, context: PlaceContext, location: Location) {
223         match context {
224             PlaceContext::MutatingUse(MutatingUseContext::Call)
225             | PlaceContext::MutatingUse(MutatingUseContext::Yield) => {
226                 self.assign(local, location);
227             }
228
229             PlaceContext::NonUse(_) | PlaceContext::MutatingUse(MutatingUseContext::Retag) => {}
230
231             PlaceContext::NonMutatingUse(
232                 NonMutatingUseContext::Copy | NonMutatingUseContext::Move,
233             ) => {
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)
241                     }
242                     None => {
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);
249
250                         let is_zst = self.fx.cx.layout_of(ty).is_zst();
251                         debug!("is_zst: {}", is_zst);
252                         is_zst
253                     }
254                 };
255                 if !ssa_read {
256                     self.not_ssa(local);
257                 }
258             }
259
260             PlaceContext::MutatingUse(
261                 MutatingUseContext::Store
262                 | MutatingUseContext::AsmOutput
263                 | MutatingUseContext::Borrow
264                 | MutatingUseContext::AddressOf
265                 | MutatingUseContext::Projection,
266             )
267             | PlaceContext::NonMutatingUse(
268                 NonMutatingUseContext::Inspect
269                 | NonMutatingUseContext::SharedBorrow
270                 | NonMutatingUseContext::UniqueBorrow
271                 | NonMutatingUseContext::ShallowBorrow
272                 | NonMutatingUseContext::AddressOf
273                 | NonMutatingUseContext::Projection,
274             ) => {
275                 self.not_ssa(local);
276             }
277
278             PlaceContext::MutatingUse(MutatingUseContext::Drop) => {
279                 let ty = self.fx.mir.local_decls[local].ty;
280                 let ty = self.fx.monomorphize(ty);
281
282                 // Only need the place if we're actually dropping it.
283                 if self.fx.cx.type_needs_drop(ty) {
284                     self.not_ssa(local);
285                 }
286             }
287         }
288     }
289 }
290
291 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
292 pub enum CleanupKind {
293     NotCleanup,
294     Funclet,
295     Internal { funclet: mir::BasicBlock },
296 }
297
298 impl CleanupKind {
299     pub fn funclet_bb(self, for_bb: mir::BasicBlock) -> Option<mir::BasicBlock> {
300         match self {
301             CleanupKind::NotCleanup => None,
302             CleanupKind::Funclet => Some(for_bb),
303             CleanupKind::Internal { funclet } => Some(funclet),
304         }
305     }
306 }
307
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>,
312     ) {
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 {
331                         debug!(
332                             "cleanup_kinds: {:?}/{:?} registering {:?} as funclet",
333                             bb, data, unwind
334                         );
335                         result[unwind] = CleanupKind::Funclet;
336                     }
337                 }
338             }
339         }
340     }
341
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());
344
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);
348                 *s = Some(succ);
349             }
350             Some(s) => {
351                 if s != succ {
352                     span_bug!(
353                         mir.span,
354                         "funclet {:?} has 2 parents - {:?} and {:?}",
355                         funclet,
356                         s,
357                         succ
358                     );
359                 }
360             }
361         };
362
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,
368             };
369
370             debug!(
371                 "cleanup_kinds: {:?}/{:?}/{:?} propagating funclet {:?}",
372                 bb, data, result[bb], funclet
373             );
374
375             for &succ in data.terminator().successors() {
376                 let kind = result[succ];
377                 debug!("cleanup_kinds: propagating {:?} to {:?}/{:?}", funclet, succ, kind);
378                 match kind {
379                     CleanupKind::NotCleanup => {
380                         result[succ] = CleanupKind::Internal { funclet };
381                     }
382                     CleanupKind::Funclet => {
383                         if funclet != succ {
384                             set_successor(funclet, succ);
385                         }
386                     }
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.
391
392                             debug!(
393                                 "promoting {:?} to a funclet and updating {:?}",
394                                 succ, succ_funclet
395                             );
396                             result[succ] = CleanupKind::Funclet;
397                             set_successor(succ_funclet, succ);
398                             set_successor(funclet, succ);
399                         }
400                     }
401                 }
402             }
403         }
404     }
405
406     let mut result = IndexVec::from_elem(CleanupKind::NotCleanup, mir.basic_blocks());
407
408     discover_masters(&mut result, mir);
409     propagate(&mut result, mir);
410     debug!("cleanup_kinds: result={:?}", result);
411     result
412 }