1 //! An analysis to determine which locals require allocas and
4 use rustc_data_structures::bit_set::BitSet;
5 use rustc_data_structures::graph::dominators::Dominators;
6 use rustc_data_structures::indexed_vec::{Idx, IndexVec};
7 use rustc::mir::{self, Location, TerminatorKind};
8 use rustc::mir::visit::{Visitor, PlaceContext, MutatingUseContext, NonMutatingUseContext};
9 use rustc::mir::traversal;
11 use rustc::ty::layout::{LayoutOf, HasTyCtxt};
12 use super::FunctionCx;
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 analyzer.visit_body(mir);
23 for (index, ty) in mir.local_decls.iter().map(|l| l.ty).enumerate() {
24 let ty = fx.monomorphize(&ty);
25 debug!("local {} has type {:?}", index, ty);
26 let layout = fx.cx.layout_of(ty);
27 if fx.cx.is_backend_immediate(layout) {
28 // These sorts of types are immediates that we can store
29 // in an Value without an alloca.
30 } else if fx.cx.is_backend_scalar_pair(layout) {
31 // We allow pairs and uses of any of their 2 fields.
33 // These sorts of types require an alloca. Note that
34 // is_llvm_immediate() may *still* be true, particularly
35 // for newtypes, but we currently force some types
36 // (e.g., structs) into an alloca unconditionally, just so
37 // that we don't have to deal with having two pathways
38 // (gep vs extractvalue etc).
39 analyzer.not_ssa(mir::Local::new(index));
43 analyzer.non_ssa_locals
46 struct LocalAnalyzer<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> {
47 fx: &'mir FunctionCx<'a, 'tcx, Bx>,
48 dominators: Dominators<mir::BasicBlock>,
49 non_ssa_locals: BitSet<mir::Local>,
50 // The location of the first visited direct assignment to each
51 // local, or an invalid location (out of bounds `block` index).
52 first_assignment: IndexVec<mir::Local, Location>,
55 impl<Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'tcx, Bx> {
56 fn new(fx: &'mir FunctionCx<'a, 'tcx, Bx>) -> Self {
57 let invalid_location =
58 mir::BasicBlock::new(fx.mir.basic_blocks().len()).start_location();
59 let mut analyzer = LocalAnalyzer {
61 dominators: fx.mir.dominators(),
62 non_ssa_locals: BitSet::new_empty(fx.mir.local_decls.len()),
63 first_assignment: IndexVec::from_elem(invalid_location, &fx.mir.local_decls)
66 // Arguments get assigned to by means of the function being called
67 for arg in fx.mir.args_iter() {
68 analyzer.first_assignment[arg] = mir::START_BLOCK.start_location();
74 fn first_assignment(&self, local: mir::Local) -> Option<Location> {
75 let location = self.first_assignment[local];
76 if location.block.index() < self.fx.mir.basic_blocks().len() {
83 fn not_ssa(&mut self, local: mir::Local) {
84 debug!("marking {:?} as non-SSA", local);
85 self.non_ssa_locals.insert(local);
88 fn assign(&mut self, local: mir::Local, location: Location) {
89 if self.first_assignment(local).is_some() {
92 self.first_assignment[local] = location;
97 impl<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> Visitor<'tcx>
98 for LocalAnalyzer<'mir, 'a, 'tcx, Bx>
100 fn visit_assign(&mut self,
101 place: &mir::Place<'tcx>,
102 rvalue: &mir::Rvalue<'tcx>,
103 location: Location) {
104 debug!("visit_assign(place={:?}, rvalue={:?})", place, rvalue);
106 if let mir::Place::Base(mir::PlaceBase::Local(index)) = *place {
107 self.assign(index, location);
108 if !self.fx.rvalue_creates_operand(rvalue) {
114 PlaceContext::MutatingUse(MutatingUseContext::Store),
119 self.visit_rvalue(rvalue, location);
122 fn visit_terminator_kind(&mut self,
123 kind: &mir::TerminatorKind<'tcx>,
124 location: Location) {
125 let check = match *kind {
126 mir::TerminatorKind::Call {
127 func: mir::Operand::Constant(ref c),
129 } => match c.ty.sty {
130 ty::FnDef(did, _) => Some((did, args)),
135 if let Some((def_id, args)) = check {
136 if Some(def_id) == self.fx.cx.tcx().lang_items().box_free_fn() {
137 // box_free(x) shares with `drop x` the property that it
138 // is not guaranteed to be statically dominated by the
139 // definition of x, so x must always be in an alloca.
140 if let mir::Operand::Move(ref place) = args[0] {
143 PlaceContext::MutatingUse(MutatingUseContext::Drop),
150 self.super_terminator_kind(kind, location);
153 fn visit_place(&mut self,
154 place: &mir::Place<'tcx>,
155 context: PlaceContext,
156 location: Location) {
157 debug!("visit_place(place={:?}, context={:?})", place, context);
160 if let mir::Place::Projection(ref proj) = *place {
161 // Allow uses of projections that are ZSTs or from scalar fields.
162 let is_consume = match context {
163 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy) |
164 PlaceContext::NonMutatingUse(NonMutatingUseContext::Move) => true,
168 let base_ty = proj.base.ty(self.fx.mir, cx.tcx());
169 let base_ty = self.fx.monomorphize(&base_ty);
171 // ZSTs don't require any actual memory access.
172 let elem_ty = base_ty
173 .projection_ty(cx.tcx(), &proj.elem)
175 let elem_ty = self.fx.monomorphize(&elem_ty);
176 if cx.layout_of(elem_ty).is_zst() {
180 if let mir::ProjectionElem::Field(..) = proj.elem {
181 let layout = cx.layout_of(base_ty.ty);
182 if cx.is_backend_immediate(layout) || cx.is_backend_scalar_pair(layout) {
183 // Recurse with the same context, instead of `Projection`,
184 // potentially stopping at non-operand projections,
185 // which would trigger `not_ssa` on locals.
186 self.visit_place(&proj.base, context, location);
192 // A deref projection only reads the pointer, never needs the place.
193 if let mir::ProjectionElem::Deref = proj.elem {
194 return self.visit_place(
196 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
202 self.super_place(place, context, location);
205 fn visit_local(&mut self,
207 context: PlaceContext,
208 location: Location) {
210 PlaceContext::MutatingUse(MutatingUseContext::Call) => {
211 self.assign(local, location);
214 PlaceContext::NonUse(_) |
215 PlaceContext::MutatingUse(MutatingUseContext::Retag) => {}
217 PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy) |
218 PlaceContext::NonMutatingUse(NonMutatingUseContext::Move) => {
219 // Reads from uninitialized variables (e.g., in dead code, after
220 // optimizations) require locals to be in (uninitialized) memory.
221 // N.B., there can be uninitialized reads of a local visited after
222 // an assignment to that local, if they happen on disjoint paths.
223 let ssa_read = match self.first_assignment(local) {
224 Some(assignment_location) => {
225 assignment_location.dominates(location, &self.dominators)
234 PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect) |
235 PlaceContext::MutatingUse(MutatingUseContext::Store) |
236 PlaceContext::MutatingUse(MutatingUseContext::AsmOutput) |
237 PlaceContext::MutatingUse(MutatingUseContext::Borrow) |
238 PlaceContext::MutatingUse(MutatingUseContext::Projection) |
239 PlaceContext::NonMutatingUse(NonMutatingUseContext::SharedBorrow) |
240 PlaceContext::NonMutatingUse(NonMutatingUseContext::UniqueBorrow) |
241 PlaceContext::NonMutatingUse(NonMutatingUseContext::ShallowBorrow) |
242 PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection) => {
246 PlaceContext::MutatingUse(MutatingUseContext::Drop) => {
247 let ty = self.fx.mir.local_decls[local].ty;
248 let ty = self.fx.monomorphize(&ty);
250 // Only need the place if we're actually dropping it.
251 if self.fx.cx.type_needs_drop(ty) {
259 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
260 pub enum CleanupKind {
263 Internal { funclet: mir::BasicBlock }
267 pub fn funclet_bb(self, for_bb: mir::BasicBlock) -> Option<mir::BasicBlock> {
269 CleanupKind::NotCleanup => None,
270 CleanupKind::Funclet => Some(for_bb),
271 CleanupKind::Internal { funclet } => Some(funclet),
276 pub fn cleanup_kinds<'tcx>(mir: &mir::Body<'tcx>) -> IndexVec<mir::BasicBlock, CleanupKind> {
277 fn discover_masters<'tcx>(result: &mut IndexVec<mir::BasicBlock, CleanupKind>,
278 mir: &mir::Body<'tcx>) {
279 for (bb, data) in mir.basic_blocks().iter_enumerated() {
280 match data.terminator().kind {
281 TerminatorKind::Goto { .. } |
282 TerminatorKind::Resume |
283 TerminatorKind::Abort |
284 TerminatorKind::Return |
285 TerminatorKind::GeneratorDrop |
286 TerminatorKind::Unreachable |
287 TerminatorKind::SwitchInt { .. } |
288 TerminatorKind::Yield { .. } |
289 TerminatorKind::FalseEdges { .. } |
290 TerminatorKind::FalseUnwind { .. } => {
293 TerminatorKind::Call { cleanup: unwind, .. } |
294 TerminatorKind::Assert { cleanup: unwind, .. } |
295 TerminatorKind::DropAndReplace { unwind, .. } |
296 TerminatorKind::Drop { unwind, .. } => {
297 if let Some(unwind) = unwind {
298 debug!("cleanup_kinds: {:?}/{:?} registering {:?} as funclet",
300 result[unwind] = CleanupKind::Funclet;
307 fn propagate<'tcx>(result: &mut IndexVec<mir::BasicBlock, CleanupKind>,
308 mir: &mir::Body<'tcx>) {
309 let mut funclet_succs = IndexVec::from_elem(None, mir.basic_blocks());
311 let mut set_successor = |funclet: mir::BasicBlock, succ| {
312 match funclet_succs[funclet] {
313 ref mut s @ None => {
314 debug!("set_successor: updating successor of {:?} to {:?}",
318 Some(s) => if s != succ {
319 span_bug!(mir.span, "funclet {:?} has 2 parents - {:?} and {:?}",
325 for (bb, data) in traversal::reverse_postorder(mir) {
326 let funclet = match result[bb] {
327 CleanupKind::NotCleanup => continue,
328 CleanupKind::Funclet => bb,
329 CleanupKind::Internal { funclet } => funclet,
332 debug!("cleanup_kinds: {:?}/{:?}/{:?} propagating funclet {:?}",
333 bb, data, result[bb], funclet);
335 for &succ in data.terminator().successors() {
336 let kind = result[succ];
337 debug!("cleanup_kinds: propagating {:?} to {:?}/{:?}",
338 funclet, succ, kind);
340 CleanupKind::NotCleanup => {
341 result[succ] = CleanupKind::Internal { funclet };
343 CleanupKind::Funclet => {
345 set_successor(funclet, succ);
348 CleanupKind::Internal { funclet: succ_funclet } => {
349 if funclet != succ_funclet {
350 // `succ` has 2 different funclet going into it, so it must
351 // be a funclet by itself.
353 debug!("promoting {:?} to a funclet and updating {:?}", succ,
355 result[succ] = CleanupKind::Funclet;
356 set_successor(succ_funclet, succ);
357 set_successor(funclet, succ);
365 let mut result = IndexVec::from_elem(CleanupKind::NotCleanup, mir.basic_blocks());
367 discover_masters(&mut result, mir);
368 propagate(&mut result, mir);
369 debug!("cleanup_kinds: result={:?}", result);