1 //! Propagate `Qualif`s between locals and query the results.
3 //! This contains the dataflow analysis used to track `Qualif`s on complex control-flow graphs.
5 use rustc::mir::visit::Visitor;
6 use rustc::mir::{self, BasicBlock, Local, Location};
7 use rustc_index::bit_set::BitSet;
9 use std::marker::PhantomData;
11 use super::{Item, Qualif};
12 use crate::dataflow::{self as old_dataflow, generic as dataflow};
14 /// A `Visitor` that propagates qualifs between locals. This defines the transfer function of
15 /// `FlowSensitiveAnalysis`.
17 /// This transfer does nothing when encountering an indirect assignment. Consumers should rely on
18 /// the `MaybeMutBorrowedLocals` dataflow pass to see if a `Local` may have become qualified via
19 /// an indirect assignment or function call.
20 struct TransferFunction<'a, 'mir, 'tcx, Q> {
21 item: &'a Item<'mir, 'tcx>,
22 qualifs_per_local: &'a mut BitSet<Local>,
24 _qualif: PhantomData<Q>,
27 impl<Q> TransferFunction<'a, 'mir, 'tcx, Q>
31 fn new(item: &'a Item<'mir, 'tcx>, qualifs_per_local: &'a mut BitSet<Local>) -> Self {
32 TransferFunction { item, qualifs_per_local, _qualif: PhantomData }
35 fn initialize_state(&mut self) {
36 self.qualifs_per_local.clear();
38 for arg in self.item.body.args_iter() {
39 let arg_ty = self.item.body.local_decls[arg].ty;
40 if Q::in_any_value_of_ty(self.item, arg_ty) {
41 self.qualifs_per_local.insert(arg);
46 fn assign_qualif_direct(&mut self, place: &mir::Place<'tcx>, value: bool) {
47 debug_assert!(!place.is_indirect());
49 match (value, place.as_ref()) {
50 (true, mir::PlaceRef { local, .. }) => {
51 self.qualifs_per_local.insert(local);
54 // For now, we do not clear the qualif if a local is overwritten in full by
55 // an unqualified rvalue (e.g. `y = 5`). This is to be consistent
56 // with aggregates where we overwrite all fields with assignments, which would not
58 (false, mir::PlaceRef { local: _, projection: &[] }) => {
59 // self.qualifs_per_local.remove(*local);
66 fn apply_call_return_effect(
69 func: &mir::Operand<'tcx>,
70 args: &[mir::Operand<'tcx>],
71 return_place: &mir::Place<'tcx>,
73 let return_ty = return_place.ty(*self.item.body, self.item.tcx).ty;
74 let qualif = Q::in_call(
76 &mut |l| self.qualifs_per_local.contains(l),
81 if !return_place.is_indirect() {
82 self.assign_qualif_direct(return_place, qualif);
87 impl<Q> Visitor<'tcx> for TransferFunction<'_, '_, 'tcx, Q>
91 fn visit_operand(&mut self, operand: &mir::Operand<'tcx>, location: Location) {
92 self.super_operand(operand, location);
94 if !Q::IS_CLEARED_ON_MOVE {
98 // If a local with no projections is moved from (e.g. `x` in `y = x`), record that
99 // it no longer needs to be dropped.
100 if let mir::Operand::Move(place) = operand {
101 if let Some(local) = place.as_local() {
102 self.qualifs_per_local.remove(local);
109 place: &mir::Place<'tcx>,
110 rvalue: &mir::Rvalue<'tcx>,
113 let qualif = Q::in_rvalue(self.item, &mut |l| self.qualifs_per_local.contains(l), rvalue);
114 if !place.is_indirect() {
115 self.assign_qualif_direct(place, qualif);
118 // We need to assign qualifs to the left-hand side before visiting `rvalue` since
119 // qualifs can be cleared on move.
120 self.super_assign(place, rvalue, location);
123 fn visit_terminator_kind(&mut self, kind: &mir::TerminatorKind<'tcx>, location: Location) {
124 // The effect of assignment to the return place in `TerminatorKind::Call` is not applied
125 // here; that occurs in `apply_call_return_effect`.
127 if let mir::TerminatorKind::DropAndReplace { value, location: dest, .. } = kind {
129 Q::in_operand(self.item, &mut |l| self.qualifs_per_local.contains(l), value);
130 if !dest.is_indirect() {
131 self.assign_qualif_direct(dest, qualif);
135 // We need to assign qualifs to the dropped location before visiting the operand that
136 // replaces it since qualifs can be cleared on move.
137 self.super_terminator_kind(kind, location);
141 /// The dataflow analysis used to propagate qualifs on arbitrary CFGs.
142 pub(super) struct FlowSensitiveAnalysis<'a, 'mir, 'tcx, Q> {
143 item: &'a Item<'mir, 'tcx>,
144 _qualif: PhantomData<Q>,
147 impl<'a, 'mir, 'tcx, Q> FlowSensitiveAnalysis<'a, 'mir, 'tcx, Q>
151 pub(super) fn new(_: Q, item: &'a Item<'mir, 'tcx>) -> Self {
152 FlowSensitiveAnalysis { item, _qualif: PhantomData }
155 fn transfer_function(
157 state: &'a mut BitSet<Local>,
158 ) -> TransferFunction<'a, 'mir, 'tcx, Q> {
159 TransferFunction::<Q>::new(self.item, state)
163 impl<Q> old_dataflow::BottomValue for FlowSensitiveAnalysis<'_, '_, '_, Q> {
164 const BOTTOM_VALUE: bool = false;
167 impl<Q> dataflow::AnalysisDomain<'tcx> for FlowSensitiveAnalysis<'_, '_, 'tcx, Q>
173 const NAME: &'static str = Q::ANALYSIS_NAME;
175 fn bits_per_block(&self, body: &mir::Body<'tcx>) -> usize {
176 body.local_decls.len()
179 fn initialize_start_block(&self, _body: &mir::Body<'tcx>, state: &mut BitSet<Self::Idx>) {
180 self.transfer_function(state).initialize_state();
184 impl<Q> dataflow::Analysis<'tcx> for FlowSensitiveAnalysis<'_, '_, 'tcx, Q>
188 fn apply_statement_effect(
190 state: &mut BitSet<Self::Idx>,
191 statement: &mir::Statement<'tcx>,
194 self.transfer_function(state).visit_statement(statement, location);
197 fn apply_terminator_effect(
199 state: &mut BitSet<Self::Idx>,
200 terminator: &mir::Terminator<'tcx>,
203 self.transfer_function(state).visit_terminator(terminator, location);
206 fn apply_call_return_effect(
208 state: &mut BitSet<Self::Idx>,
210 func: &mir::Operand<'tcx>,
211 args: &[mir::Operand<'tcx>],
212 return_place: &mir::Place<'tcx>,
214 self.transfer_function(state).apply_call_return_effect(block, func, args, return_place)