]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_ssa/mir/analyze.rs
Update const_forget.rs
[rust.git] / src / librustc_codegen_ssa / 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::mir::traversal;
7 use rustc::mir::visit::{
8     MutatingUseContext, NonMutatingUseContext, NonUseContext, PlaceContext, Visitor,
9 };
10 use rustc::mir::{self, Location, TerminatorKind};
11 use rustc::ty;
12 use rustc::ty::layout::{HasTyCtxt, LayoutOf};
13 use rustc_data_structures::graph::dominators::Dominators;
14 use rustc_index::bit_set::BitSet;
15 use rustc_index::vec::{Idx, IndexVec};
16
17 pub fn non_ssa_locals<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
18     fx: &FunctionCx<'a, 'tcx, Bx>,
19 ) -> BitSet<mir::Local> {
20     let mir = fx.mir;
21     let mut analyzer = LocalAnalyzer::new(fx);
22
23     analyzer.visit_body(mir);
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 [proj_base @ .., elem] = place_ref.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 = match context {
115                 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy)
116                 | PlaceContext::NonMutatingUse(NonMutatingUseContext::Move) => true,
117                 _ => false,
118             };
119             if is_consume {
120                 let base_ty =
121                     mir::Place::ty_from(place_ref.local, proj_base, *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(), elem).ty;
126                 let elem_ty = 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                 // (the exception being `VarDebugInfo` contexts, handled below)
146                 base_context = PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy);
147
148                 // Indirect debuginfo requires going through memory, that only
149                 // the debugger accesses, following our emitted DWARF pointer ops.
150                 //
151                 // FIXME(eddyb) Investigate the possibility of relaxing this, but
152                 // note that `llvm.dbg.declare` *must* be used for indirect places,
153                 // even if we start using `llvm.dbg.value` for all other cases,
154                 // as we don't necessarily know when the value changes, but only
155                 // where it lives in memory.
156                 //
157                 // It's possible `llvm.dbg.declare` could support starting from
158                 // a pointer that doesn't point to an `alloca`, but this would
159                 // only be useful if we know the pointer being `Deref`'d comes
160                 // from an immutable place, and if `llvm.dbg.declare` calls
161                 // must be at the very start of the function, then only function
162                 // arguments could contain such pointers.
163                 if context == PlaceContext::NonUse(NonUseContext::VarDebugInfo) {
164                     // We use `NonUseContext::VarDebugInfo` for the base,
165                     // which might not force the base local to memory,
166                     // so we have to do it manually.
167                     self.visit_local(&place_ref.local, context, location);
168                 }
169             }
170
171             // `NonUseContext::VarDebugInfo` needs to flow all the
172             // way down to the base local (see `visit_local`).
173             if context == PlaceContext::NonUse(NonUseContext::VarDebugInfo) {
174                 base_context = context;
175             }
176
177             self.process_place(
178                 &mir::PlaceRef { local: place_ref.local, projection: proj_base },
179                 base_context,
180                 location,
181             );
182             // HACK(eddyb) this emulates the old `visit_projection_elem`, this
183             // entire `visit_place`-like `process_place` method should be rewritten,
184             // now that we have moved to the "slice of projections" representation.
185             if let mir::ProjectionElem::Index(local) = elem {
186                 self.visit_local(
187                     local,
188                     PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
189                     location,
190                 );
191             }
192         } else {
193             // FIXME this is super_place code, is repeated here to avoid cloning place or changing
194             // visit_place API
195             let mut context = context;
196
197             if !place_ref.projection.is_empty() {
198                 context = if context.is_mutating_use() {
199                     PlaceContext::MutatingUse(MutatingUseContext::Projection)
200                 } else {
201                     PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection)
202                 };
203             }
204
205             self.visit_place_base(&place_ref.local, context, location);
206             self.visit_projection(&place_ref.local, place_ref.projection, context, location);
207         }
208     }
209 }
210
211 impl<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> Visitor<'tcx>
212     for LocalAnalyzer<'mir, 'a, 'tcx, Bx>
213 {
214     fn visit_assign(
215         &mut self,
216         place: &mir::Place<'tcx>,
217         rvalue: &mir::Rvalue<'tcx>,
218         location: Location,
219     ) {
220         debug!("visit_assign(place={:?}, rvalue={:?})", place, rvalue);
221
222         if let Some(index) = place.as_local() {
223             self.assign(index, location);
224             let decl_span = self.fx.mir.local_decls[index].source_info.span;
225             if !self.fx.rvalue_creates_operand(rvalue, decl_span) {
226                 self.not_ssa(index);
227             }
228         } else {
229             self.visit_place(place, PlaceContext::MutatingUse(MutatingUseContext::Store), location);
230         }
231
232         self.visit_rvalue(rvalue, location);
233     }
234
235     fn visit_terminator_kind(&mut self, kind: &mir::TerminatorKind<'tcx>, location: Location) {
236         let check = match *kind {
237             mir::TerminatorKind::Call { func: mir::Operand::Constant(ref c), ref args, .. } => {
238                 match c.literal.ty.kind {
239                     ty::FnDef(did, _) => Some((did, args)),
240                     _ => None,
241                 }
242             }
243             _ => None,
244         };
245         if let Some((def_id, args)) = check {
246             if Some(def_id) == self.fx.cx.tcx().lang_items().box_free_fn() {
247                 // box_free(x) shares with `drop x` the property that it
248                 // is not guaranteed to be statically dominated by the
249                 // definition of x, so x must always be in an alloca.
250                 if let mir::Operand::Move(ref place) = args[0] {
251                     self.visit_place(
252                         place,
253                         PlaceContext::MutatingUse(MutatingUseContext::Drop),
254                         location,
255                     );
256                 }
257             }
258         }
259
260         self.super_terminator_kind(kind, location);
261     }
262
263     fn visit_place(&mut self, place: &mir::Place<'tcx>, context: PlaceContext, location: Location) {
264         debug!("visit_place(place={:?}, context={:?})", place, context);
265         self.process_place(&place.as_ref(), context, location);
266     }
267
268     fn visit_local(&mut self, &local: &mir::Local, context: PlaceContext, location: Location) {
269         match context {
270             PlaceContext::MutatingUse(MutatingUseContext::Call) => {
271                 self.assign(local, location);
272             }
273
274             PlaceContext::NonUse(_) | PlaceContext::MutatingUse(MutatingUseContext::Retag) => {}
275
276             PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy)
277             | PlaceContext::NonMutatingUse(NonMutatingUseContext::Move) => {
278                 // Reads from uninitialized variables (e.g., in dead code, after
279                 // optimizations) require locals to be in (uninitialized) memory.
280                 // N.B., there can be uninitialized reads of a local visited after
281                 // an assignment to that local, if they happen on disjoint paths.
282                 let ssa_read = match self.first_assignment(local) {
283                     Some(assignment_location) => {
284                         assignment_location.dominates(location, &self.dominators)
285                     }
286                     None => false,
287                 };
288                 if !ssa_read {
289                     self.not_ssa(local);
290                 }
291             }
292
293             PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect)
294             | PlaceContext::MutatingUse(MutatingUseContext::Store)
295             | PlaceContext::MutatingUse(MutatingUseContext::AsmOutput)
296             | PlaceContext::MutatingUse(MutatingUseContext::Borrow)
297             | PlaceContext::MutatingUse(MutatingUseContext::AddressOf)
298             | PlaceContext::MutatingUse(MutatingUseContext::Projection)
299             | PlaceContext::NonMutatingUse(NonMutatingUseContext::SharedBorrow)
300             | PlaceContext::NonMutatingUse(NonMutatingUseContext::UniqueBorrow)
301             | PlaceContext::NonMutatingUse(NonMutatingUseContext::ShallowBorrow)
302             | PlaceContext::NonMutatingUse(NonMutatingUseContext::AddressOf)
303             | PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection) => {
304                 self.not_ssa(local);
305             }
306
307             PlaceContext::MutatingUse(MutatingUseContext::Drop) => {
308                 let ty = self.fx.mir.local_decls[local].ty;
309                 let ty = self.fx.monomorphize(&ty);
310
311                 // Only need the place if we're actually dropping it.
312                 if self.fx.cx.type_needs_drop(ty) {
313                     self.not_ssa(local);
314                 }
315             }
316         }
317     }
318 }
319
320 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
321 pub enum CleanupKind {
322     NotCleanup,
323     Funclet,
324     Internal { funclet: mir::BasicBlock },
325 }
326
327 impl CleanupKind {
328     pub fn funclet_bb(self, for_bb: mir::BasicBlock) -> Option<mir::BasicBlock> {
329         match self {
330             CleanupKind::NotCleanup => None,
331             CleanupKind::Funclet => Some(for_bb),
332             CleanupKind::Internal { funclet } => Some(funclet),
333         }
334     }
335 }
336
337 pub fn cleanup_kinds(mir: &mir::Body<'_>) -> IndexVec<mir::BasicBlock, CleanupKind> {
338     fn discover_masters<'tcx>(
339         result: &mut IndexVec<mir::BasicBlock, CleanupKind>,
340         mir: &mir::Body<'tcx>,
341     ) {
342         for (bb, data) in mir.basic_blocks().iter_enumerated() {
343             match data.terminator().kind {
344                 TerminatorKind::Goto { .. }
345                 | TerminatorKind::Resume
346                 | TerminatorKind::Abort
347                 | TerminatorKind::Return
348                 | TerminatorKind::GeneratorDrop
349                 | TerminatorKind::Unreachable
350                 | TerminatorKind::SwitchInt { .. }
351                 | TerminatorKind::Yield { .. }
352                 | TerminatorKind::FalseEdges { .. }
353                 | TerminatorKind::FalseUnwind { .. } => { /* nothing to do */ }
354                 TerminatorKind::Call { cleanup: unwind, .. }
355                 | TerminatorKind::Assert { cleanup: unwind, .. }
356                 | TerminatorKind::DropAndReplace { unwind, .. }
357                 | TerminatorKind::Drop { unwind, .. } => {
358                     if let Some(unwind) = unwind {
359                         debug!(
360                             "cleanup_kinds: {:?}/{:?} registering {:?} as funclet",
361                             bb, data, unwind
362                         );
363                         result[unwind] = CleanupKind::Funclet;
364                     }
365                 }
366             }
367         }
368     }
369
370     fn propagate<'tcx>(result: &mut IndexVec<mir::BasicBlock, CleanupKind>, mir: &mir::Body<'tcx>) {
371         let mut funclet_succs = IndexVec::from_elem(None, mir.basic_blocks());
372
373         let mut set_successor = |funclet: mir::BasicBlock, succ| match funclet_succs[funclet] {
374             ref mut s @ None => {
375                 debug!("set_successor: updating successor of {:?} to {:?}", funclet, succ);
376                 *s = Some(succ);
377             }
378             Some(s) => {
379                 if s != succ {
380                     span_bug!(
381                         mir.span,
382                         "funclet {:?} has 2 parents - {:?} and {:?}",
383                         funclet,
384                         s,
385                         succ
386                     );
387                 }
388             }
389         };
390
391         for (bb, data) in traversal::reverse_postorder(mir) {
392             let funclet = match result[bb] {
393                 CleanupKind::NotCleanup => continue,
394                 CleanupKind::Funclet => bb,
395                 CleanupKind::Internal { funclet } => funclet,
396             };
397
398             debug!(
399                 "cleanup_kinds: {:?}/{:?}/{:?} propagating funclet {:?}",
400                 bb, data, result[bb], funclet
401             );
402
403             for &succ in data.terminator().successors() {
404                 let kind = result[succ];
405                 debug!("cleanup_kinds: propagating {:?} to {:?}/{:?}", funclet, succ, kind);
406                 match kind {
407                     CleanupKind::NotCleanup => {
408                         result[succ] = CleanupKind::Internal { funclet };
409                     }
410                     CleanupKind::Funclet => {
411                         if funclet != succ {
412                             set_successor(funclet, succ);
413                         }
414                     }
415                     CleanupKind::Internal { funclet: succ_funclet } => {
416                         if funclet != succ_funclet {
417                             // `succ` has 2 different funclet going into it, so it must
418                             // be a funclet by itself.
419
420                             debug!(
421                                 "promoting {:?} to a funclet and updating {:?}",
422                                 succ, succ_funclet
423                             );
424                             result[succ] = CleanupKind::Funclet;
425                             set_successor(succ_funclet, succ);
426                             set_successor(funclet, succ);
427                         }
428                     }
429                 }
430             }
431         }
432     }
433
434     let mut result = IndexVec::from_elem(CleanupKind::NotCleanup, mir.basic_blocks());
435
436     discover_masters(&mut result, mir);
437     propagate(&mut result, mir);
438     debug!("cleanup_kinds: result={:?}", result);
439     result
440 }