]>
Commit | Line | Data |
---|---|---|
3157f602 | 1 | //! An analysis to determine which locals require allocas and |
92a42be0 SL |
2 | //! which do not. |
3 | ||
dfeec247 XL |
4 | use super::FunctionCx; |
5 | use crate::traits::*; | |
dfeec247 XL |
6 | use rustc_data_structures::graph::dominators::Dominators; |
7 | use rustc_index::bit_set::BitSet; | |
136023e0 | 8 | use rustc_index::vec::IndexVec; |
ba9703b0 | 9 | use rustc_middle::mir::traversal; |
17df50a5 | 10 | use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor}; |
ba9703b0 | 11 | use rustc_middle::mir::{self, Location, TerminatorKind}; |
c295e0f8 | 12 | use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf}; |
92a42be0 | 13 | |
dc9dc135 XL |
14 | pub fn non_ssa_locals<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>( |
15 | fx: &FunctionCx<'a, 'tcx, Bx>, | |
a1dfa0c6 | 16 | ) -> BitSet<mir::Local> { |
2c00a5a8 | 17 | let mir = fx.mir; |
136023e0 XL |
18 | let dominators = mir.dominators(); |
19 | let locals = mir | |
20 | .local_decls | |
21 | .iter() | |
22 | .map(|decl| { | |
23 | let ty = fx.monomorphize(decl.ty); | |
24 | let layout = fx.cx.spanned_layout_of(ty, decl.source_info.span); | |
25 | if layout.is_zst() { | |
26 | LocalKind::ZST | |
27 | } else if fx.cx.is_backend_immediate(layout) || fx.cx.is_backend_scalar_pair(layout) { | |
28 | LocalKind::Unused | |
29 | } else { | |
30 | LocalKind::Memory | |
31 | } | |
32 | }) | |
33 | .collect(); | |
34 | ||
35 | let mut analyzer = LocalAnalyzer { fx, dominators, locals }; | |
36 | ||
37 | // Arguments get assigned to by means of the function being called | |
38 | for arg in mir.args_iter() { | |
39 | analyzer.assign(arg, mir::START_BLOCK.start_location()); | |
40 | } | |
92a42be0 | 41 | |
17df50a5 | 42 | // If there exists a local definition that dominates all uses of that local, |
04454e1e | 43 | // the definition should be visited first. Traverse blocks in an order that |
17df50a5 | 44 | // is a topological sort of dominance partial order. |
04454e1e | 45 | for (bb, data) in traversal::reverse_postorder(&mir) { |
17df50a5 XL |
46 | analyzer.visit_basic_block_data(bb, data); |
47 | } | |
92a42be0 | 48 | |
136023e0 XL |
49 | let mut non_ssa_locals = BitSet::new_empty(analyzer.locals.len()); |
50 | for (local, kind) in analyzer.locals.iter_enumerated() { | |
51 | if matches!(kind, LocalKind::Memory) { | |
52 | non_ssa_locals.insert(local); | |
92a42be0 SL |
53 | } |
54 | } | |
55 | ||
136023e0 XL |
56 | non_ssa_locals |
57 | } | |
58 | ||
59 | #[derive(Copy, Clone, PartialEq, Eq)] | |
60 | enum LocalKind { | |
61 | ZST, | |
62 | /// A local that requires an alloca. | |
63 | Memory, | |
64 | /// A scalar or a scalar pair local that is neither defined nor used. | |
65 | Unused, | |
66 | /// A scalar or a scalar pair local with a single definition that dominates all uses. | |
67 | SSA(mir::Location), | |
92a42be0 SL |
68 | } |
69 | ||
dc9dc135 | 70 | struct LocalAnalyzer<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> { |
a1dfa0c6 | 71 | fx: &'mir FunctionCx<'a, 'tcx, Bx>, |
83c7162d | 72 | dominators: Dominators<mir::BasicBlock>, |
136023e0 | 73 | locals: IndexVec<mir::Local, LocalKind>, |
92a42be0 SL |
74 | } |
75 | ||
a2a8927a | 76 | impl<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'tcx, Bx> { |
83c7162d | 77 | fn assign(&mut self, local: mir::Local, location: Location) { |
136023e0 XL |
78 | let kind = &mut self.locals[local]; |
79 | match *kind { | |
80 | LocalKind::ZST => {} | |
81 | LocalKind::Memory => {} | |
82 | LocalKind::Unused => { | |
83 | *kind = LocalKind::SSA(location); | |
84 | } | |
85 | LocalKind::SSA(_) => { | |
86 | *kind = LocalKind::Memory; | |
87 | } | |
7453a54e SL |
88 | } |
89 | } | |
416331ca XL |
90 | |
91 | fn process_place( | |
92 | &mut self, | |
74b04a01 | 93 | place_ref: &mir::PlaceRef<'tcx>, |
416331ca XL |
94 | context: PlaceContext, |
95 | location: Location, | |
96 | ) { | |
97 | let cx = self.fx.cx; | |
98 | ||
5869c6ff | 99 | if let Some((place_base, elem)) = place_ref.last_projection() { |
60c5eb7d XL |
100 | let mut base_context = if context.is_mutating_use() { |
101 | PlaceContext::MutatingUse(MutatingUseContext::Projection) | |
102 | } else { | |
103 | PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection) | |
104 | }; | |
105 | ||
416331ca | 106 | // Allow uses of projections that are ZSTs or from scalar fields. |
5869c6ff XL |
107 | let is_consume = matches!( |
108 | context, | |
ba9703b0 XL |
109 | PlaceContext::NonMutatingUse( |
110 | NonMutatingUseContext::Copy | NonMutatingUseContext::Move, | |
5869c6ff XL |
111 | ) |
112 | ); | |
416331ca | 113 | if is_consume { |
5869c6ff | 114 | let base_ty = place_base.ty(self.fx.mir, cx.tcx()); |
fc512014 | 115 | let base_ty = self.fx.monomorphize(base_ty); |
416331ca XL |
116 | |
117 | // ZSTs don't require any actual memory access. | |
fc512014 | 118 | let elem_ty = base_ty.projection_ty(cx.tcx(), self.fx.monomorphize(elem)).ty; |
74b04a01 | 119 | let span = self.fx.mir.local_decls[place_ref.local].source_info.span; |
416331ca XL |
120 | if cx.spanned_layout_of(elem_ty, span).is_zst() { |
121 | return; | |
122 | } | |
123 | ||
e1599b0c | 124 | if let mir::ProjectionElem::Field(..) = elem { |
416331ca XL |
125 | let layout = cx.spanned_layout_of(base_ty.ty, span); |
126 | if cx.is_backend_immediate(layout) || cx.is_backend_scalar_pair(layout) { | |
127 | // Recurse with the same context, instead of `Projection`, | |
128 | // potentially stopping at non-operand projections, | |
129 | // which would trigger `not_ssa` on locals. | |
60c5eb7d | 130 | base_context = context; |
416331ca XL |
131 | } |
132 | } | |
133 | } | |
134 | ||
e1599b0c | 135 | if let mir::ProjectionElem::Deref = elem { |
60c5eb7d | 136 | // Deref projections typically only read the pointer. |
60c5eb7d | 137 | base_context = PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy); |
60c5eb7d XL |
138 | } |
139 | ||
5869c6ff | 140 | self.process_place(&place_base, base_context, location); |
60c5eb7d XL |
141 | // HACK(eddyb) this emulates the old `visit_projection_elem`, this |
142 | // entire `visit_place`-like `process_place` method should be rewritten, | |
143 | // now that we have moved to the "slice of projections" representation. | |
144 | if let mir::ProjectionElem::Index(local) = elem { | |
145 | self.visit_local( | |
f9f354fc | 146 | &local, |
416331ca | 147 | PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy), |
dfeec247 | 148 | location, |
416331ca | 149 | ); |
416331ca | 150 | } |
60c5eb7d | 151 | } else { |
f9f354fc | 152 | self.visit_local(&place_ref.local, context, location); |
416331ca | 153 | } |
416331ca | 154 | } |
92a42be0 SL |
155 | } |
156 | ||
dc9dc135 XL |
157 | impl<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> Visitor<'tcx> |
158 | for LocalAnalyzer<'mir, 'a, 'tcx, Bx> | |
159 | { | |
dfeec247 XL |
160 | fn visit_assign( |
161 | &mut self, | |
162 | place: &mir::Place<'tcx>, | |
163 | rvalue: &mir::Rvalue<'tcx>, | |
164 | location: Location, | |
165 | ) { | |
48663c56 | 166 | debug!("visit_assign(place={:?}, rvalue={:?})", place, rvalue); |
92a42be0 | 167 | |
136023e0 XL |
168 | if let Some(local) = place.as_local() { |
169 | self.assign(local, location); | |
170 | if self.locals[local] != LocalKind::Memory { | |
171 | let decl_span = self.fx.mir.local_decls[local].source_info.span; | |
172 | if !self.fx.rvalue_creates_operand(rvalue, decl_span) { | |
173 | self.locals[local] = LocalKind::Memory; | |
174 | } | |
92a42be0 | 175 | } |
3157f602 | 176 | } else { |
dfeec247 | 177 | self.visit_place(place, PlaceContext::MutatingUse(MutatingUseContext::Store), location); |
92a42be0 SL |
178 | } |
179 | ||
9e0c209e | 180 | self.visit_rvalue(rvalue, location); |
92a42be0 SL |
181 | } |
182 | ||
dfeec247 | 183 | fn visit_place(&mut self, place: &mir::Place<'tcx>, context: PlaceContext, location: Location) { |
ff7c6d11 | 184 | debug!("visit_place(place={:?}, context={:?})", place, context); |
416331ca | 185 | self.process_place(&place.as_ref(), context, location); |
ea8adc8c | 186 | } |
5bcae85e | 187 | |
dfeec247 | 188 | fn visit_local(&mut self, &local: &mir::Local, context: PlaceContext, location: Location) { |
ea8adc8c | 189 | match context { |
f9f354fc XL |
190 | PlaceContext::MutatingUse(MutatingUseContext::Call) |
191 | | PlaceContext::MutatingUse(MutatingUseContext::Yield) => { | |
83c7162d | 192 | self.assign(local, location); |
ea8adc8c | 193 | } |
5bcae85e | 194 | |
dfeec247 | 195 | PlaceContext::NonUse(_) | PlaceContext::MutatingUse(MutatingUseContext::Retag) => {} |
83c7162d | 196 | |
ba9703b0 XL |
197 | PlaceContext::NonMutatingUse( |
198 | NonMutatingUseContext::Copy | NonMutatingUseContext::Move, | |
136023e0 XL |
199 | ) => match &mut self.locals[local] { |
200 | LocalKind::ZST => {} | |
201 | LocalKind::Memory => {} | |
202 | LocalKind::SSA(def) if def.dominates(location, &self.dominators) => {} | |
0731742a | 203 | // Reads from uninitialized variables (e.g., in dead code, after |
83c7162d | 204 | // optimizations) require locals to be in (uninitialized) memory. |
0731742a | 205 | // N.B., there can be uninitialized reads of a local visited after |
83c7162d | 206 | // an assignment to that local, if they happen on disjoint paths. |
136023e0 XL |
207 | kind @ (LocalKind::Unused | LocalKind::SSA(_)) => { |
208 | *kind = LocalKind::Memory; | |
83c7162d | 209 | } |
136023e0 | 210 | }, |
ff7c6d11 | 211 | |
ba9703b0 XL |
212 | PlaceContext::MutatingUse( |
213 | MutatingUseContext::Store | |
04454e1e FG |
214 | | MutatingUseContext::Deinit |
215 | | MutatingUseContext::SetDiscriminant | |
ba9703b0 XL |
216 | | MutatingUseContext::AsmOutput |
217 | | MutatingUseContext::Borrow | |
218 | | MutatingUseContext::AddressOf | |
219 | | MutatingUseContext::Projection, | |
220 | ) | |
221 | | PlaceContext::NonMutatingUse( | |
222 | NonMutatingUseContext::Inspect | |
223 | | NonMutatingUseContext::SharedBorrow | |
224 | | NonMutatingUseContext::UniqueBorrow | |
225 | | NonMutatingUseContext::ShallowBorrow | |
226 | | NonMutatingUseContext::AddressOf | |
227 | | NonMutatingUseContext::Projection, | |
228 | ) => { | |
136023e0 | 229 | self.locals[local] = LocalKind::Memory; |
92a42be0 | 230 | } |
3157f602 | 231 | |
13cf67c4 | 232 | PlaceContext::MutatingUse(MutatingUseContext::Drop) => { |
136023e0 XL |
233 | let kind = &mut self.locals[local]; |
234 | if *kind != LocalKind::Memory { | |
235 | let ty = self.fx.mir.local_decls[local].ty; | |
236 | let ty = self.fx.monomorphize(ty); | |
237 | if self.fx.cx.type_needs_drop(ty) { | |
238 | // Only need the place if we're actually dropping it. | |
239 | *kind = LocalKind::Memory; | |
240 | } | |
ea8adc8c | 241 | } |
92a42be0 SL |
242 | } |
243 | } | |
92a42be0 SL |
244 | } |
245 | } | |
3157f602 XL |
246 | |
247 | #[derive(Copy, Clone, Debug, PartialEq, Eq)] | |
248 | pub enum CleanupKind { | |
249 | NotCleanup, | |
250 | Funclet, | |
dfeec247 | 251 | Internal { funclet: mir::BasicBlock }, |
3157f602 XL |
252 | } |
253 | ||
7cac9316 XL |
254 | impl CleanupKind { |
255 | pub fn funclet_bb(self, for_bb: mir::BasicBlock) -> Option<mir::BasicBlock> { | |
256 | match self { | |
257 | CleanupKind::NotCleanup => None, | |
258 | CleanupKind::Funclet => Some(for_bb), | |
259 | CleanupKind::Internal { funclet } => Some(funclet), | |
260 | } | |
261 | } | |
262 | } | |
263 | ||
416331ca | 264 | pub fn cleanup_kinds(mir: &mir::Body<'_>) -> IndexVec<mir::BasicBlock, CleanupKind> { |
dfeec247 XL |
265 | fn discover_masters<'tcx>( |
266 | result: &mut IndexVec<mir::BasicBlock, CleanupKind>, | |
267 | mir: &mir::Body<'tcx>, | |
268 | ) { | |
3157f602 XL |
269 | for (bb, data) in mir.basic_blocks().iter_enumerated() { |
270 | match data.terminator().kind { | |
dfeec247 XL |
271 | TerminatorKind::Goto { .. } |
272 | | TerminatorKind::Resume | |
273 | | TerminatorKind::Abort | |
274 | | TerminatorKind::Return | |
275 | | TerminatorKind::GeneratorDrop | |
276 | | TerminatorKind::Unreachable | |
277 | | TerminatorKind::SwitchInt { .. } | |
278 | | TerminatorKind::Yield { .. } | |
f035d41b | 279 | | TerminatorKind::FalseEdge { .. } |
a2a8927a | 280 | | TerminatorKind::FalseUnwind { .. } => { /* nothing to do */ } |
dfeec247 | 281 | TerminatorKind::Call { cleanup: unwind, .. } |
a2a8927a | 282 | | TerminatorKind::InlineAsm { cleanup: unwind, .. } |
dfeec247 XL |
283 | | TerminatorKind::Assert { cleanup: unwind, .. } |
284 | | TerminatorKind::DropAndReplace { unwind, .. } | |
285 | | TerminatorKind::Drop { unwind, .. } => { | |
3157f602 | 286 | if let Some(unwind) = unwind { |
dfeec247 XL |
287 | debug!( |
288 | "cleanup_kinds: {:?}/{:?} registering {:?} as funclet", | |
289 | bb, data, unwind | |
290 | ); | |
3157f602 XL |
291 | result[unwind] = CleanupKind::Funclet; |
292 | } | |
293 | } | |
294 | } | |
295 | } | |
296 | } | |
297 | ||
dfeec247 | 298 | fn propagate<'tcx>(result: &mut IndexVec<mir::BasicBlock, CleanupKind>, mir: &mir::Body<'tcx>) { |
3157f602 XL |
299 | let mut funclet_succs = IndexVec::from_elem(None, mir.basic_blocks()); |
300 | ||
dfeec247 XL |
301 | let mut set_successor = |funclet: mir::BasicBlock, succ| match funclet_succs[funclet] { |
302 | ref mut s @ None => { | |
303 | debug!("set_successor: updating successor of {:?} to {:?}", funclet, succ); | |
304 | *s = Some(succ); | |
305 | } | |
306 | Some(s) => { | |
307 | if s != succ { | |
308 | span_bug!( | |
309 | mir.span, | |
310 | "funclet {:?} has 2 parents - {:?} and {:?}", | |
311 | funclet, | |
312 | s, | |
313 | succ | |
314 | ); | |
3157f602 XL |
315 | } |
316 | } | |
317 | }; | |
318 | ||
319 | for (bb, data) in traversal::reverse_postorder(mir) { | |
320 | let funclet = match result[bb] { | |
321 | CleanupKind::NotCleanup => continue, | |
322 | CleanupKind::Funclet => bb, | |
323 | CleanupKind::Internal { funclet } => funclet, | |
324 | }; | |
325 | ||
dfeec247 XL |
326 | debug!( |
327 | "cleanup_kinds: {:?}/{:?}/{:?} propagating funclet {:?}", | |
328 | bb, data, result[bb], funclet | |
329 | ); | |
3157f602 | 330 | |
83c7162d | 331 | for &succ in data.terminator().successors() { |
3157f602 | 332 | let kind = result[succ]; |
dfeec247 | 333 | debug!("cleanup_kinds: propagating {:?} to {:?}/{:?}", funclet, succ, kind); |
3157f602 XL |
334 | match kind { |
335 | CleanupKind::NotCleanup => { | |
a1dfa0c6 | 336 | result[succ] = CleanupKind::Internal { funclet }; |
3157f602 XL |
337 | } |
338 | CleanupKind::Funclet => { | |
7cac9316 XL |
339 | if funclet != succ { |
340 | set_successor(funclet, succ); | |
341 | } | |
3157f602 XL |
342 | } |
343 | CleanupKind::Internal { funclet: succ_funclet } => { | |
344 | if funclet != succ_funclet { | |
345 | // `succ` has 2 different funclet going into it, so it must | |
346 | // be a funclet by itself. | |
347 | ||
dfeec247 XL |
348 | debug!( |
349 | "promoting {:?} to a funclet and updating {:?}", | |
350 | succ, succ_funclet | |
351 | ); | |
3157f602 XL |
352 | result[succ] = CleanupKind::Funclet; |
353 | set_successor(succ_funclet, succ); | |
354 | set_successor(funclet, succ); | |
355 | } | |
356 | } | |
357 | } | |
358 | } | |
359 | } | |
360 | } | |
361 | ||
362 | let mut result = IndexVec::from_elem(CleanupKind::NotCleanup, mir.basic_blocks()); | |
363 | ||
364 | discover_masters(&mut result, mir); | |
365 | propagate(&mut result, mir); | |
366 | debug!("cleanup_kinds: result={:?}", result); | |
367 | result | |
368 | } |