rustc_mir_dataflow/impls/
liveness.rs1use rustc_index::bit_set::DenseBitSet;
2use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor};
3use rustc_middle::mir::{
4 self, CallReturnPlaces, Local, Location, Place, StatementKind, TerminatorEdges,
5};
6
7use crate::{Analysis, Backward, GenKill};
8
9pub struct MaybeLiveLocals;
27
28impl<'tcx> Analysis<'tcx> for MaybeLiveLocals {
29 type Domain = DenseBitSet<Local>;
30 type Direction = Backward;
31
32 const NAME: &'static str = "liveness";
33
34 fn bottom_value(&self, body: &mir::Body<'tcx>) -> Self::Domain {
35 DenseBitSet::new_empty(body.local_decls.len())
37 }
38
39 fn initialize_start_block(&self, _: &mir::Body<'tcx>, _: &mut Self::Domain) {
40 }
42
43 fn apply_primary_statement_effect(
44 &mut self,
45 state: &mut Self::Domain,
46 statement: &mir::Statement<'tcx>,
47 location: Location,
48 ) {
49 TransferFunction(state).visit_statement(statement, location);
50 }
51
52 fn apply_primary_terminator_effect<'mir>(
53 &mut self,
54 state: &mut Self::Domain,
55 terminator: &'mir mir::Terminator<'tcx>,
56 location: Location,
57 ) -> TerminatorEdges<'mir, 'tcx> {
58 TransferFunction(state).visit_terminator(terminator, location);
59 terminator.edges()
60 }
61
62 fn apply_call_return_effect(
63 &mut self,
64 state: &mut Self::Domain,
65 _block: mir::BasicBlock,
66 return_places: CallReturnPlaces<'_, 'tcx>,
67 ) {
68 if let CallReturnPlaces::Yield(resume_place) = return_places {
69 YieldResumeEffect(state).visit_place(
70 &resume_place,
71 PlaceContext::MutatingUse(MutatingUseContext::Yield),
72 Location::START,
73 )
74 } else {
75 return_places.for_each(|place| {
76 if let Some(local) = place.as_local() {
77 state.kill(local);
78 }
79 });
80 }
81 }
82}
83
84pub struct TransferFunction<'a>(pub &'a mut DenseBitSet<Local>);
85
86impl<'tcx> Visitor<'tcx> for TransferFunction<'_> {
87 fn visit_place(&mut self, place: &mir::Place<'tcx>, context: PlaceContext, location: Location) {
88 if let PlaceContext::MutatingUse(MutatingUseContext::Yield) = context {
89 return;
92 }
93
94 match DefUse::for_place(*place, context) {
95 DefUse::Def => {
96 if let PlaceContext::MutatingUse(
97 MutatingUseContext::Call | MutatingUseContext::AsmOutput,
98 ) = context
99 {
100 } else {
105 self.0.kill(place.local);
106 }
107 }
108 DefUse::Use => self.0.gen_(place.local),
109 DefUse::PartialWrite | DefUse::NonUse => {}
110 }
111
112 self.visit_projection(place.as_ref(), context, location);
113 }
114
115 fn visit_local(&mut self, local: Local, context: PlaceContext, _: Location) {
116 DefUse::apply(self.0, local.into(), context);
117 }
118}
119
120struct YieldResumeEffect<'a>(&'a mut DenseBitSet<Local>);
121
122impl<'tcx> Visitor<'tcx> for YieldResumeEffect<'_> {
123 fn visit_place(&mut self, place: &mir::Place<'tcx>, context: PlaceContext, location: Location) {
124 DefUse::apply(self.0, *place, context);
125 self.visit_projection(place.as_ref(), context, location);
126 }
127
128 fn visit_local(&mut self, local: Local, context: PlaceContext, _: Location) {
129 DefUse::apply(self.0, local.into(), context);
130 }
131}
132
133#[derive(Eq, PartialEq, Clone)]
134pub enum DefUse {
135 Def,
137 Use,
139 PartialWrite,
141 NonUse,
143}
144
145impl DefUse {
146 fn apply(state: &mut DenseBitSet<Local>, place: Place<'_>, context: PlaceContext) {
147 match DefUse::for_place(place, context) {
148 DefUse::Def => state.kill(place.local),
149 DefUse::Use => state.gen_(place.local),
150 DefUse::PartialWrite | DefUse::NonUse => {}
151 }
152 }
153
154 pub fn for_place(place: Place<'_>, context: PlaceContext) -> DefUse {
155 match context {
156 PlaceContext::NonUse(_) => DefUse::NonUse,
157
158 PlaceContext::MutatingUse(
159 MutatingUseContext::Call
160 | MutatingUseContext::Yield
161 | MutatingUseContext::AsmOutput
162 | MutatingUseContext::Store
163 | MutatingUseContext::Deinit,
164 ) => {
165 if place.is_indirect() {
167 DefUse::Use
168 } else if place.projection.is_empty() {
169 DefUse::Def
170 } else {
171 DefUse::PartialWrite
172 }
173 }
174
175 PlaceContext::MutatingUse(MutatingUseContext::SetDiscriminant) => {
178 if place.is_indirect() { DefUse::Use } else { DefUse::PartialWrite }
179 }
180
181 PlaceContext::MutatingUse(
183 MutatingUseContext::RawBorrow
184 | MutatingUseContext::Borrow
185 | MutatingUseContext::Drop
186 | MutatingUseContext::Retag,
187 )
188 | PlaceContext::NonMutatingUse(
189 NonMutatingUseContext::RawBorrow
190 | NonMutatingUseContext::Copy
191 | NonMutatingUseContext::Inspect
192 | NonMutatingUseContext::Move
193 | NonMutatingUseContext::PlaceMention
194 | NonMutatingUseContext::FakeBorrow
195 | NonMutatingUseContext::SharedBorrow,
196 ) => DefUse::Use,
197
198 PlaceContext::MutatingUse(MutatingUseContext::Projection)
199 | PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection) => {
200 unreachable!("A projection could be a def or a use and must be handled separately")
201 }
202 }
203 }
204}
205
206pub struct MaybeTransitiveLiveLocals<'a> {
212 always_live: &'a DenseBitSet<Local>,
213 debuginfo_locals: &'a DenseBitSet<Local>,
214}
215
216impl<'a> MaybeTransitiveLiveLocals<'a> {
217 pub fn new(
222 always_live: &'a DenseBitSet<Local>,
223 debuginfo_locals: &'a DenseBitSet<Local>,
224 ) -> Self {
225 MaybeTransitiveLiveLocals { always_live, debuginfo_locals }
226 }
227
228 pub fn can_be_removed_if_dead<'tcx>(
229 stmt_kind: &StatementKind<'tcx>,
230 always_live: &DenseBitSet<Local>,
231 debuginfo_locals: &'a DenseBitSet<Local>,
232 ) -> Option<Place<'tcx>> {
233 let destination = match stmt_kind {
235 StatementKind::Assign(box (place, rvalue)) => (rvalue.is_safe_to_remove()
236 && (!debuginfo_locals.contains(place.local)
239 || (place.as_local().is_some() && stmt_kind.as_debuginfo().is_some())))
240 .then_some(*place),
241 StatementKind::SetDiscriminant { place, .. } | StatementKind::Deinit(place) => {
242 (!debuginfo_locals.contains(place.local)).then_some(**place)
243 }
244 StatementKind::FakeRead(_)
245 | StatementKind::StorageLive(_)
246 | StatementKind::StorageDead(_)
247 | StatementKind::Retag(..)
248 | StatementKind::AscribeUserType(..)
249 | StatementKind::PlaceMention(..)
250 | StatementKind::Coverage(..)
251 | StatementKind::Intrinsic(..)
252 | StatementKind::ConstEvalCounter
253 | StatementKind::BackwardIncompatibleDropHint { .. }
254 | StatementKind::Nop => None,
255 };
256 if let Some(destination) = destination
257 && !destination.is_indirect()
258 && !always_live.contains(destination.local)
259 {
260 return Some(destination);
261 }
262 None
263 }
264}
265
266impl<'a, 'tcx> Analysis<'tcx> for MaybeTransitiveLiveLocals<'a> {
267 type Domain = DenseBitSet<Local>;
268 type Direction = Backward;
269
270 const NAME: &'static str = "transitive liveness";
271
272 fn bottom_value(&self, body: &mir::Body<'tcx>) -> Self::Domain {
273 DenseBitSet::new_empty(body.local_decls.len())
275 }
276
277 fn initialize_start_block(&self, _: &mir::Body<'tcx>, _: &mut Self::Domain) {
278 }
280
281 fn apply_primary_statement_effect(
282 &mut self,
283 state: &mut Self::Domain,
284 statement: &mir::Statement<'tcx>,
285 location: Location,
286 ) {
287 if let Some(destination) =
288 Self::can_be_removed_if_dead(&statement.kind, &self.always_live, &self.debuginfo_locals)
289 && !state.contains(destination.local)
290 {
291 return;
293 }
294 TransferFunction(state).visit_statement(statement, location);
295 }
296
297 fn apply_primary_terminator_effect<'mir>(
298 &mut self,
299 state: &mut Self::Domain,
300 terminator: &'mir mir::Terminator<'tcx>,
301 location: Location,
302 ) -> TerminatorEdges<'mir, 'tcx> {
303 TransferFunction(state).visit_terminator(terminator, location);
304 terminator.edges()
305 }
306
307 fn apply_call_return_effect(
308 &mut self,
309 state: &mut Self::Domain,
310 _block: mir::BasicBlock,
311 return_places: CallReturnPlaces<'_, 'tcx>,
312 ) {
313 if let CallReturnPlaces::Yield(resume_place) = return_places {
314 YieldResumeEffect(state).visit_place(
315 &resume_place,
316 PlaceContext::MutatingUse(MutatingUseContext::Yield),
317 Location::START,
318 )
319 } else {
320 return_places.for_each(|place| {
321 if let Some(local) = place.as_local() {
322 state.remove(local);
323 }
324 });
325 }
326 }
327}