]>
Commit | Line | Data |
---|---|---|
f9f354fc XL |
1 | use rustc_index::bit_set::BitSet; |
2 | use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor}; | |
3 | use rustc_middle::mir::{self, Local, Location}; | |
4 | ||
5 | use crate::dataflow::{AnalysisDomain, Backward, BottomValue, GenKill, GenKillAnalysis}; | |
6 | ||
7 | /// A [live-variable dataflow analysis][liveness]. | |
8 | /// | |
9 | /// This analysis considers references as being used only at the point of the | |
10 | /// borrow. In other words, this analysis does not track uses because of references that already | |
11 | /// exist. See [this `mir-datalow` test][flow-test] for an example. You almost never want to use | |
12 | /// this analysis without also looking at the results of [`MaybeBorrowedLocals`]. | |
13 | /// | |
14 | /// [`MaybeBorrowedLocals`]: ../struct.MaybeBorrowedLocals.html | |
15 | /// [flow-test]: https://github.com/rust-lang/rust/blob/a08c47310c7d49cbdc5d7afb38408ba519967ecd/src/test/ui/mir-dataflow/liveness-ptr.rs | |
16 | /// [liveness]: https://en.wikipedia.org/wiki/Live_variable_analysis | |
17 | pub struct MaybeLiveLocals; | |
18 | ||
19 | impl MaybeLiveLocals { | |
20 | fn transfer_function<T>(&self, trans: &'a mut T) -> TransferFunction<'a, T> { | |
21 | TransferFunction(trans) | |
22 | } | |
23 | } | |
24 | ||
25 | impl BottomValue for MaybeLiveLocals { | |
26 | // bottom = not live | |
27 | const BOTTOM_VALUE: bool = false; | |
28 | } | |
29 | ||
30 | impl AnalysisDomain<'tcx> for MaybeLiveLocals { | |
31 | type Idx = Local; | |
32 | type Direction = Backward; | |
33 | ||
34 | const NAME: &'static str = "liveness"; | |
35 | ||
36 | fn bits_per_block(&self, body: &mir::Body<'tcx>) -> usize { | |
37 | body.local_decls.len() | |
38 | } | |
39 | ||
40 | fn initialize_start_block(&self, _: &mir::Body<'tcx>, _: &mut BitSet<Self::Idx>) { | |
41 | // No variables are live until we observe a use | |
42 | } | |
43 | } | |
44 | ||
45 | impl GenKillAnalysis<'tcx> for MaybeLiveLocals { | |
46 | fn statement_effect( | |
47 | &self, | |
48 | trans: &mut impl GenKill<Self::Idx>, | |
49 | statement: &mir::Statement<'tcx>, | |
50 | location: Location, | |
51 | ) { | |
52 | self.transfer_function(trans).visit_statement(statement, location); | |
53 | } | |
54 | ||
55 | fn terminator_effect( | |
56 | &self, | |
57 | trans: &mut impl GenKill<Self::Idx>, | |
58 | terminator: &mir::Terminator<'tcx>, | |
59 | location: Location, | |
60 | ) { | |
61 | self.transfer_function(trans).visit_terminator(terminator, location); | |
62 | } | |
63 | ||
64 | fn call_return_effect( | |
65 | &self, | |
66 | trans: &mut impl GenKill<Self::Idx>, | |
67 | _block: mir::BasicBlock, | |
68 | _func: &mir::Operand<'tcx>, | |
69 | _args: &[mir::Operand<'tcx>], | |
70 | dest_place: mir::Place<'tcx>, | |
71 | ) { | |
72 | if let Some(local) = dest_place.as_local() { | |
73 | trans.kill(local); | |
74 | } | |
75 | } | |
76 | ||
77 | fn yield_resume_effect( | |
78 | &self, | |
79 | trans: &mut impl GenKill<Self::Idx>, | |
80 | _resume_block: mir::BasicBlock, | |
81 | resume_place: mir::Place<'tcx>, | |
82 | ) { | |
83 | if let Some(local) = resume_place.as_local() { | |
84 | trans.kill(local); | |
85 | } | |
86 | } | |
87 | } | |
88 | ||
89 | struct TransferFunction<'a, T>(&'a mut T); | |
90 | ||
91 | impl<'tcx, T> Visitor<'tcx> for TransferFunction<'_, T> | |
92 | where | |
93 | T: GenKill<Local>, | |
94 | { | |
f035d41b XL |
95 | fn visit_place(&mut self, place: &mir::Place<'tcx>, context: PlaceContext, location: Location) { |
96 | let mir::Place { projection, local } = *place; | |
97 | ||
98 | // We purposefully do not call `super_place` here to avoid calling `visit_local` for this | |
99 | // place with one of the `Projection` variants of `PlaceContext`. | |
100 | self.visit_projection(local, projection, context, location); | |
101 | ||
102 | match DefUse::for_place(context) { | |
103 | // Treat derefs as a use of the base local. `*p = 4` is not a def of `p` but a use. | |
104 | Some(_) if place.is_indirect() => self.0.gen(local), | |
105 | ||
106 | Some(DefUse::Def) if projection.is_empty() => self.0.kill(local), | |
107 | Some(DefUse::Use) => self.0.gen(local), | |
108 | _ => {} | |
109 | } | |
110 | } | |
111 | ||
f9f354fc | 112 | fn visit_local(&mut self, &local: &Local, context: PlaceContext, _: Location) { |
f035d41b XL |
113 | // Because we do not call `super_place` above, `visit_local` is only called for locals that |
114 | // do not appear as part of a `Place` in the MIR. This handles cases like the implicit use | |
115 | // of the return place in a `Return` terminator or the index in an `Index` projection. | |
f9f354fc XL |
116 | match DefUse::for_place(context) { |
117 | Some(DefUse::Def) => self.0.kill(local), | |
118 | Some(DefUse::Use) => self.0.gen(local), | |
119 | _ => {} | |
120 | } | |
121 | } | |
122 | } | |
123 | ||
124 | #[derive(Eq, PartialEq, Clone)] | |
125 | enum DefUse { | |
126 | Def, | |
127 | Use, | |
128 | } | |
129 | ||
130 | impl DefUse { | |
131 | fn for_place(context: PlaceContext) -> Option<DefUse> { | |
132 | match context { | |
133 | PlaceContext::NonUse(_) => None, | |
134 | ||
135 | PlaceContext::MutatingUse(MutatingUseContext::Store) => Some(DefUse::Def), | |
136 | ||
137 | // `MutatingUseContext::Call` and `MutatingUseContext::Yield` indicate that this is the | |
138 | // destination place for a `Call` return or `Yield` resume respectively. Since this is | |
139 | // only a `Def` when the function returns succesfully, we handle this case separately | |
140 | // in `call_return_effect` above. | |
141 | PlaceContext::MutatingUse(MutatingUseContext::Call | MutatingUseContext::Yield) => None, | |
142 | ||
143 | // All other contexts are uses... | |
144 | PlaceContext::MutatingUse( | |
145 | MutatingUseContext::AddressOf | |
146 | | MutatingUseContext::AsmOutput | |
147 | | MutatingUseContext::Borrow | |
148 | | MutatingUseContext::Drop | |
f9f354fc XL |
149 | | MutatingUseContext::Retag, |
150 | ) | |
151 | | PlaceContext::NonMutatingUse( | |
152 | NonMutatingUseContext::AddressOf | |
153 | | NonMutatingUseContext::Copy | |
154 | | NonMutatingUseContext::Inspect | |
155 | | NonMutatingUseContext::Move | |
f9f354fc XL |
156 | | NonMutatingUseContext::ShallowBorrow |
157 | | NonMutatingUseContext::SharedBorrow | |
158 | | NonMutatingUseContext::UniqueBorrow, | |
159 | ) => Some(DefUse::Use), | |
f035d41b XL |
160 | |
161 | PlaceContext::MutatingUse(MutatingUseContext::Projection) | |
162 | | PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection) => { | |
163 | unreachable!("A projection could be a def or a use and must be handled separately") | |
164 | } | |
f9f354fc XL |
165 | } |
166 | } | |
167 | } |