]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_ssa/mir/analyze.rs
Promote `Ref`s to constants instead of static
[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::session::config::DebugInfo;
12 use rustc::ty;
13 use rustc::ty::layout::{HasTyCtxt, LayoutOf};
14 use rustc_data_structures::graph::dominators::Dominators;
15 use rustc_index::bit_set::BitSet;
16 use rustc_index::vec::{Idx, IndexVec};
17 use rustc_span::DUMMY_SP;
18
19 pub fn non_ssa_locals<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
20     fx: &FunctionCx<'a, 'tcx, Bx>,
21 ) -> BitSet<mir::Local> {
22     let mir = fx.mir;
23     let mut analyzer = LocalAnalyzer::new(fx);
24
25     analyzer.visit_body(mir);
26
27     for (local, decl) in mir.local_decls.iter_enumerated() {
28         // FIXME(eddyb): We should figure out how to use llvm.dbg.value instead
29         // of putting everything in allocas just so we can use llvm.dbg.declare.
30         if fx.cx.sess().opts.debuginfo == DebugInfo::Full {
31             if fx.mir.local_kind(local) == mir::LocalKind::Arg {
32                 analyzer.not_ssa(local);
33                 continue;
34             }
35         }
36
37         let ty = fx.monomorphize(&decl.ty);
38         debug!("local {:?} has type `{}`", local, ty);
39         let layout = fx.cx.spanned_layout_of(ty, decl.source_info.span);
40         if fx.cx.is_backend_immediate(layout) {
41             // These sorts of types are immediates that we can store
42             // in an Value without an alloca.
43         } else if fx.cx.is_backend_scalar_pair(layout) {
44             // We allow pairs and uses of any of their 2 fields.
45         } else {
46             // These sorts of types require an alloca. Note that
47             // is_llvm_immediate() may *still* be true, particularly
48             // for newtypes, but we currently force some types
49             // (e.g., structs) into an alloca unconditionally, just so
50             // that we don't have to deal with having two pathways
51             // (gep vs extractvalue etc).
52             analyzer.not_ssa(local);
53         }
54     }
55
56     analyzer.non_ssa_locals
57 }
58
59 struct LocalAnalyzer<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> {
60     fx: &'mir FunctionCx<'a, 'tcx, Bx>,
61     dominators: Dominators<mir::BasicBlock>,
62     non_ssa_locals: BitSet<mir::Local>,
63     // The location of the first visited direct assignment to each
64     // local, or an invalid location (out of bounds `block` index).
65     first_assignment: IndexVec<mir::Local, Location>,
66 }
67
68 impl<Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'tcx, Bx> {
69     fn new(fx: &'mir FunctionCx<'a, 'tcx, Bx>) -> Self {
70         let invalid_location = mir::BasicBlock::new(fx.mir.basic_blocks().len()).start_location();
71         let dominators = fx.mir.dominators();
72         let mut analyzer = LocalAnalyzer {
73             fx,
74             dominators,
75             non_ssa_locals: BitSet::new_empty(fx.mir.local_decls.len()),
76             first_assignment: IndexVec::from_elem(invalid_location, &fx.mir.local_decls),
77         };
78
79         // Arguments get assigned to by means of the function being called
80         for arg in fx.mir.args_iter() {
81             analyzer.first_assignment[arg] = mir::START_BLOCK.start_location();
82         }
83
84         analyzer
85     }
86
87     fn first_assignment(&self, local: mir::Local) -> Option<Location> {
88         let location = self.first_assignment[local];
89         if location.block.index() < self.fx.mir.basic_blocks().len() {
90             Some(location)
91         } else {
92             None
93         }
94     }
95
96     fn not_ssa(&mut self, local: mir::Local) {
97         debug!("marking {:?} as non-SSA", local);
98         self.non_ssa_locals.insert(local);
99     }
100
101     fn assign(&mut self, local: mir::Local, location: Location) {
102         if self.first_assignment(local).is_some() {
103             self.not_ssa(local);
104         } else {
105             self.first_assignment[local] = location;
106         }
107     }
108
109     fn process_place(
110         &mut self,
111         place_ref: &mir::PlaceRef<'_, 'tcx>,
112         context: PlaceContext,
113         location: Location,
114     ) {
115         let cx = self.fx.cx;
116
117         if let [proj_base @ .., elem] = place_ref.projection {
118             let mut base_context = if context.is_mutating_use() {
119                 PlaceContext::MutatingUse(MutatingUseContext::Projection)
120             } else {
121                 PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection)
122             };
123
124             // Allow uses of projections that are ZSTs or from scalar fields.
125             let is_consume = match context {
126                 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy)
127                 | PlaceContext::NonMutatingUse(NonMutatingUseContext::Move) => true,
128                 _ => false,
129             };
130             if is_consume {
131                 let base_ty =
132                     mir::Place::ty_from(place_ref.base, proj_base, *self.fx.mir, cx.tcx());
133                 let base_ty = self.fx.monomorphize(&base_ty);
134
135                 // ZSTs don't require any actual memory access.
136                 let elem_ty = base_ty.projection_ty(cx.tcx(), elem).ty;
137                 let elem_ty = self.fx.monomorphize(&elem_ty);
138                 let span = if let mir::PlaceBase::Local(index) = place_ref.base {
139                     self.fx.mir.local_decls[*index].source_info.span
140                 } else {
141                     DUMMY_SP
142                 };
143                 if cx.spanned_layout_of(elem_ty, span).is_zst() {
144                     return;
145                 }
146
147                 if let mir::ProjectionElem::Field(..) = elem {
148                     let layout = cx.spanned_layout_of(base_ty.ty, span);
149                     if cx.is_backend_immediate(layout) || cx.is_backend_scalar_pair(layout) {
150                         // Recurse with the same context, instead of `Projection`,
151                         // potentially stopping at non-operand projections,
152                         // which would trigger `not_ssa` on locals.
153                         base_context = context;
154                     }
155                 }
156             }
157
158             if let mir::ProjectionElem::Deref = elem {
159                 // Deref projections typically only read the pointer.
160                 // (the exception being `VarDebugInfo` contexts, handled below)
161                 base_context = PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy);
162
163                 // Indirect debuginfo requires going through memory, that only
164                 // the debugger accesses, following our emitted DWARF pointer ops.
165                 //
166                 // FIXME(eddyb) Investigate the possibility of relaxing this, but
167                 // note that `llvm.dbg.declare` *must* be used for indirect places,
168                 // even if we start using `llvm.dbg.value` for all other cases,
169                 // as we don't necessarily know when the value changes, but only
170                 // where it lives in memory.
171                 //
172                 // It's possible `llvm.dbg.declare` could support starting from
173                 // a pointer that doesn't point to an `alloca`, but this would
174                 // only be useful if we know the pointer being `Deref`'d comes
175                 // from an immutable place, and if `llvm.dbg.declare` calls
176                 // must be at the very start of the function, then only function
177                 // arguments could contain such pointers.
178                 if context == PlaceContext::NonUse(NonUseContext::VarDebugInfo) {
179                     // We use `NonUseContext::VarDebugInfo` for the base,
180                     // which might not force the base local to memory,
181                     // so we have to do it manually.
182                     if let mir::PlaceBase::Local(local) = place_ref.base {
183                         self.visit_local(&local, context, location);
184                     }
185                 }
186             }
187
188             // `NonUseContext::VarDebugInfo` needs to flow all the
189             // way down to the base local (see `visit_local`).
190             if context == PlaceContext::NonUse(NonUseContext::VarDebugInfo) {
191                 base_context = context;
192             }
193
194             self.process_place(
195                 &mir::PlaceRef { base: place_ref.base, projection: proj_base },
196                 base_context,
197                 location,
198             );
199             // HACK(eddyb) this emulates the old `visit_projection_elem`, this
200             // entire `visit_place`-like `process_place` method should be rewritten,
201             // now that we have moved to the "slice of projections" representation.
202             if let mir::ProjectionElem::Index(local) = elem {
203                 self.visit_local(
204                     local,
205                     PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
206                     location,
207                 );
208             }
209         } else {
210             // FIXME this is super_place code, is repeated here to avoid cloning place or changing
211             // visit_place API
212             let mut context = context;
213
214             if !place_ref.projection.is_empty() {
215                 context = if context.is_mutating_use() {
216                     PlaceContext::MutatingUse(MutatingUseContext::Projection)
217                 } else {
218                     PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection)
219                 };
220             }
221
222             self.visit_place_base(place_ref.base, context, location);
223             self.visit_projection(place_ref.base, place_ref.projection, context, location);
224         }
225     }
226 }
227
228 impl<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> Visitor<'tcx>
229     for LocalAnalyzer<'mir, 'a, 'tcx, Bx>
230 {
231     fn visit_assign(
232         &mut self,
233         place: &mir::Place<'tcx>,
234         rvalue: &mir::Rvalue<'tcx>,
235         location: Location,
236     ) {
237         debug!("visit_assign(place={:?}, rvalue={:?})", place, rvalue);
238
239         if let Some(index) = place.as_local() {
240             self.assign(index, location);
241             let decl_span = self.fx.mir.local_decls[index].source_info.span;
242             if !self.fx.rvalue_creates_operand(rvalue, decl_span) {
243                 self.not_ssa(index);
244             }
245         } else {
246             self.visit_place(place, PlaceContext::MutatingUse(MutatingUseContext::Store), location);
247         }
248
249         self.visit_rvalue(rvalue, location);
250     }
251
252     fn visit_terminator_kind(&mut self, kind: &mir::TerminatorKind<'tcx>, location: Location) {
253         let check = match *kind {
254             mir::TerminatorKind::Call { func: mir::Operand::Constant(ref c), ref args, .. } => {
255                 match c.literal.ty.kind {
256                     ty::FnDef(did, _) => Some((did, args)),
257                     _ => None,
258                 }
259             }
260             _ => None,
261         };
262         if let Some((def_id, args)) = check {
263             if Some(def_id) == self.fx.cx.tcx().lang_items().box_free_fn() {
264                 // box_free(x) shares with `drop x` the property that it
265                 // is not guaranteed to be statically dominated by the
266                 // definition of x, so x must always be in an alloca.
267                 if let mir::Operand::Move(ref place) = args[0] {
268                     self.visit_place(
269                         place,
270                         PlaceContext::MutatingUse(MutatingUseContext::Drop),
271                         location,
272                     );
273                 }
274             }
275         }
276
277         self.super_terminator_kind(kind, location);
278     }
279
280     fn visit_place(&mut self, place: &mir::Place<'tcx>, context: PlaceContext, location: Location) {
281         debug!("visit_place(place={:?}, context={:?})", place, context);
282         self.process_place(&place.as_ref(), context, location);
283     }
284
285     fn visit_local(&mut self, &local: &mir::Local, context: PlaceContext, location: Location) {
286         match context {
287             PlaceContext::MutatingUse(MutatingUseContext::Call) => {
288                 self.assign(local, location);
289             }
290
291             PlaceContext::NonUse(NonUseContext::VarDebugInfo) => {
292                 // We need to keep locals in `alloca`s for debuginfo.
293                 // FIXME(eddyb): We should figure out how to use `llvm.dbg.value` instead
294                 // of putting everything in allocas just so we can use `llvm.dbg.declare`.
295                 if self.fx.cx.sess().opts.debuginfo == DebugInfo::Full {
296                     self.not_ssa(local);
297                 }
298             }
299
300             PlaceContext::NonUse(_) | PlaceContext::MutatingUse(MutatingUseContext::Retag) => {}
301
302             PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy)
303             | PlaceContext::NonMutatingUse(NonMutatingUseContext::Move) => {
304                 // Reads from uninitialized variables (e.g., in dead code, after
305                 // optimizations) require locals to be in (uninitialized) memory.
306                 // N.B., there can be uninitialized reads of a local visited after
307                 // an assignment to that local, if they happen on disjoint paths.
308                 let ssa_read = match self.first_assignment(local) {
309                     Some(assignment_location) => {
310                         assignment_location.dominates(location, &self.dominators)
311                     }
312                     None => false,
313                 };
314                 if !ssa_read {
315                     self.not_ssa(local);
316                 }
317             }
318
319             PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect)
320             | PlaceContext::MutatingUse(MutatingUseContext::Store)
321             | PlaceContext::MutatingUse(MutatingUseContext::AsmOutput)
322             | PlaceContext::MutatingUse(MutatingUseContext::Borrow)
323             | PlaceContext::MutatingUse(MutatingUseContext::AddressOf)
324             | PlaceContext::MutatingUse(MutatingUseContext::Projection)
325             | PlaceContext::NonMutatingUse(NonMutatingUseContext::SharedBorrow)
326             | PlaceContext::NonMutatingUse(NonMutatingUseContext::UniqueBorrow)
327             | PlaceContext::NonMutatingUse(NonMutatingUseContext::ShallowBorrow)
328             | PlaceContext::NonMutatingUse(NonMutatingUseContext::AddressOf)
329             | PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection) => {
330                 self.not_ssa(local);
331             }
332
333             PlaceContext::MutatingUse(MutatingUseContext::Drop) => {
334                 let ty = self.fx.mir.local_decls[local].ty;
335                 let ty = self.fx.monomorphize(&ty);
336
337                 // Only need the place if we're actually dropping it.
338                 if self.fx.cx.type_needs_drop(ty) {
339                     self.not_ssa(local);
340                 }
341             }
342         }
343     }
344 }
345
346 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
347 pub enum CleanupKind {
348     NotCleanup,
349     Funclet,
350     Internal { funclet: mir::BasicBlock },
351 }
352
353 impl CleanupKind {
354     pub fn funclet_bb(self, for_bb: mir::BasicBlock) -> Option<mir::BasicBlock> {
355         match self {
356             CleanupKind::NotCleanup => None,
357             CleanupKind::Funclet => Some(for_bb),
358             CleanupKind::Internal { funclet } => Some(funclet),
359         }
360     }
361 }
362
363 pub fn cleanup_kinds(mir: &mir::Body<'_>) -> IndexVec<mir::BasicBlock, CleanupKind> {
364     fn discover_masters<'tcx>(
365         result: &mut IndexVec<mir::BasicBlock, CleanupKind>,
366         mir: &mir::Body<'tcx>,
367     ) {
368         for (bb, data) in mir.basic_blocks().iter_enumerated() {
369             match data.terminator().kind {
370                 TerminatorKind::Goto { .. }
371                 | TerminatorKind::Resume
372                 | TerminatorKind::Abort
373                 | TerminatorKind::Return
374                 | TerminatorKind::GeneratorDrop
375                 | TerminatorKind::Unreachable
376                 | TerminatorKind::SwitchInt { .. }
377                 | TerminatorKind::Yield { .. }
378                 | TerminatorKind::FalseEdges { .. }
379                 | TerminatorKind::FalseUnwind { .. } => { /* nothing to do */ }
380                 TerminatorKind::Call { cleanup: unwind, .. }
381                 | TerminatorKind::Assert { cleanup: unwind, .. }
382                 | TerminatorKind::DropAndReplace { unwind, .. }
383                 | TerminatorKind::Drop { unwind, .. } => {
384                     if let Some(unwind) = unwind {
385                         debug!(
386                             "cleanup_kinds: {:?}/{:?} registering {:?} as funclet",
387                             bb, data, unwind
388                         );
389                         result[unwind] = CleanupKind::Funclet;
390                     }
391                 }
392             }
393         }
394     }
395
396     fn propagate<'tcx>(result: &mut IndexVec<mir::BasicBlock, CleanupKind>, mir: &mir::Body<'tcx>) {
397         let mut funclet_succs = IndexVec::from_elem(None, mir.basic_blocks());
398
399         let mut set_successor = |funclet: mir::BasicBlock, succ| match funclet_succs[funclet] {
400             ref mut s @ None => {
401                 debug!("set_successor: updating successor of {:?} to {:?}", funclet, succ);
402                 *s = Some(succ);
403             }
404             Some(s) => {
405                 if s != succ {
406                     span_bug!(
407                         mir.span,
408                         "funclet {:?} has 2 parents - {:?} and {:?}",
409                         funclet,
410                         s,
411                         succ
412                     );
413                 }
414             }
415         };
416
417         for (bb, data) in traversal::reverse_postorder(mir) {
418             let funclet = match result[bb] {
419                 CleanupKind::NotCleanup => continue,
420                 CleanupKind::Funclet => bb,
421                 CleanupKind::Internal { funclet } => funclet,
422             };
423
424             debug!(
425                 "cleanup_kinds: {:?}/{:?}/{:?} propagating funclet {:?}",
426                 bb, data, result[bb], funclet
427             );
428
429             for &succ in data.terminator().successors() {
430                 let kind = result[succ];
431                 debug!("cleanup_kinds: propagating {:?} to {:?}/{:?}", funclet, succ, kind);
432                 match kind {
433                     CleanupKind::NotCleanup => {
434                         result[succ] = CleanupKind::Internal { funclet };
435                     }
436                     CleanupKind::Funclet => {
437                         if funclet != succ {
438                             set_successor(funclet, succ);
439                         }
440                     }
441                     CleanupKind::Internal { funclet: succ_funclet } => {
442                         if funclet != succ_funclet {
443                             // `succ` has 2 different funclet going into it, so it must
444                             // be a funclet by itself.
445
446                             debug!(
447                                 "promoting {:?} to a funclet and updating {:?}",
448                                 succ, succ_funclet
449                             );
450                             result[succ] = CleanupKind::Funclet;
451                             set_successor(succ_funclet, succ);
452                             set_successor(funclet, succ);
453                         }
454                     }
455                 }
456             }
457         }
458     }
459
460     let mut result = IndexVec::from_elem(CleanupKind::NotCleanup, mir.basic_blocks());
461
462     discover_masters(&mut result, mir);
463     propagate(&mut result, mir);
464     debug!("cleanup_kinds: result={:?}", result);
465     result
466 }