1 use rustc_data_structures::fx::FxHashSet;
2 use rustc_middle::mir::{Body, Local, ReadOnlyBodyAndCache};
3 use rustc_middle::ty::{RegionVid, TyCtxt};
6 use crate::dataflow::move_paths::MoveData;
7 use crate::dataflow::MaybeInitializedPlaces;
8 use crate::dataflow::ResultsCursor;
10 use crate::borrow_check::{
11 constraints::OutlivesConstraintSet,
12 facts::{AllFacts, AllFactsExt},
13 location::LocationTable,
15 region_infer::values::RegionValueElements,
16 universal_regions::UniversalRegions,
19 use super::TypeChecker;
25 /// Combines liveness analysis with initialization analysis to
26 /// determine which variables are live at which points, both due to
27 /// ordinary uses and drops. Returns a set of (ty, location) pairs
28 /// that indicate which types must be live at which point in the CFG.
29 /// This vector is consumed by `constraint_generation`.
31 /// N.B., this computation requires normalization; therefore, it must be
33 pub(super) fn generate<'mir, 'tcx>(
34 typeck: &mut TypeChecker<'_, 'tcx>,
35 body: ReadOnlyBodyAndCache<'_, 'tcx>,
36 elements: &Rc<RegionValueElements>,
37 flow_inits: &mut ResultsCursor<'mir, 'tcx, MaybeInitializedPlaces<'mir, 'tcx>>,
38 move_data: &MoveData<'tcx>,
39 location_table: &LocationTable,
41 debug!("liveness::generate");
43 let free_regions = regions_that_outlive_free_regions(
44 typeck.infcx.num_region_vars(),
45 &typeck.borrowck_context.universal_regions,
46 &typeck.borrowck_context.constraints.outlives_constraints,
48 let live_locals = compute_live_locals(typeck.tcx(), &free_regions, &body);
49 let facts_enabled = AllFacts::enabled(typeck.tcx());
51 let polonius_drop_used = if facts_enabled {
52 let mut drop_used = Vec::new();
53 polonius::populate_access_facts(typeck, body, location_table, move_data, &mut drop_used);
59 if !live_locals.is_empty() || facts_enabled {
72 // The purpose of `compute_live_locals` is to define the subset of `Local`
73 // variables for which we need to do a liveness computation. We only need
74 // to compute whether a variable `X` is live if that variable contains
75 // some region `R` in its type where `R` is not known to outlive a free
76 // region (i.e., where `R` may be valid for just a subset of the fn body).
77 fn compute_live_locals(
79 free_regions: &FxHashSet<RegionVid>,
82 let live_locals: Vec<Local> = body
85 .filter_map(|(local, local_decl)| {
86 if tcx.all_free_regions_meet(&local_decl.ty, |r| {
87 free_regions.contains(&r.to_region_vid())
96 debug!("{} total variables", body.local_decls.len());
97 debug!("{} variables need liveness", live_locals.len());
98 debug!("{} regions outlive free regions", free_regions.len());
103 /// Computes all regions that are (currently) known to outlive free
104 /// regions. For these regions, we do not need to compute
105 /// liveness, since the outlives constraints will ensure that they
106 /// are live over the whole fn body anyhow.
107 fn regions_that_outlive_free_regions(
108 num_region_vars: usize,
109 universal_regions: &UniversalRegions<'tcx>,
110 constraint_set: &OutlivesConstraintSet,
111 ) -> FxHashSet<RegionVid> {
112 // Build a graph of the outlives constraints thus far. This is
113 // a reverse graph, so for each constraint `R1: R2` we have an
114 // edge `R2 -> R1`. Therefore, if we find all regions
115 // reachable from each free region, we will have all the
116 // regions that are forced to outlive some free region.
117 let rev_constraint_graph = constraint_set.reverse_graph(num_region_vars);
118 let fr_static = universal_regions.fr_static;
119 let rev_region_graph = rev_constraint_graph.region_graph(constraint_set, fr_static);
121 // Stack for the depth-first search. Start out with all the free regions.
122 let mut stack: Vec<_> = universal_regions.universal_regions().collect();
124 // Set of all free regions, plus anything that outlives them. Initially
125 // just contains the free regions.
126 let mut outlives_free_region: FxHashSet<_> = stack.iter().cloned().collect();
128 // Do the DFS -- for each thing in the stack, find all things
129 // that outlive it and add them to the set. If they are not,
130 // push them onto the stack for later.
131 while let Some(sub_region) = stack.pop() {
134 .outgoing_regions(sub_region)
135 .filter(|&r| outlives_free_region.insert(r)),
139 // Return the final set of things we visited.