22#![ deny( rustc:: diagnostic_outside_of_impl) ]
33use rustc_data_structures:: fx:: FxIndexSet ;
44use rustc_index:: bit_set:: SparseBitMatrix ;
5- use rustc_index:: interval:: IntervalSet ;
65use rustc_index:: interval:: SparseIntervalMatrix ;
76use rustc_index:: Idx ;
8- use rustc_index:: IndexVec ;
9- use rustc_middle:: mir:: { BasicBlock , Body , Location } ;
7+ use rustc_middle:: mir:: { BasicBlock , Location } ;
108use rustc_middle:: ty:: { self , RegionVid } ;
9+ use rustc_mir_dataflow:: points:: { DenseLocationMap , LivenessValues , PointIndex } ;
1110use std:: fmt:: Debug ;
1211use std:: rc:: Rc ;
1312
14- /// Maps between a `Location` and a `PointIndex` (and vice versa).
15- pub ( crate ) struct RegionValueElements {
16- /// For each basic block, how many points are contained within?
17- statements_before_block : IndexVec < BasicBlock , usize > ,
18-
19- /// Map backward from each point to the basic block that it
20- /// belongs to.
21- basic_blocks : IndexVec < PointIndex , BasicBlock > ,
22-
23- num_points : usize ,
24- }
25-
26- impl RegionValueElements {
27- pub ( crate ) fn new ( body : & Body < ' _ > ) -> Self {
28- let mut num_points = 0 ;
29- let statements_before_block: IndexVec < BasicBlock , usize > = body
30- . basic_blocks
31- . iter ( )
32- . map ( |block_data| {
33- let v = num_points;
34- num_points += block_data. statements . len ( ) + 1 ;
35- v
36- } )
37- . collect ( ) ;
38- debug ! ( "RegionValueElements: statements_before_block={:#?}" , statements_before_block) ;
39- debug ! ( "RegionValueElements: num_points={:#?}" , num_points) ;
40-
41- let mut basic_blocks = IndexVec :: with_capacity ( num_points) ;
42- for ( bb, bb_data) in body. basic_blocks . iter_enumerated ( ) {
43- basic_blocks. extend ( ( 0 ..=bb_data. statements . len ( ) ) . map ( |_| bb) ) ;
44- }
45-
46- Self { statements_before_block, basic_blocks, num_points }
47- }
48-
49- /// Total number of point indices
50- pub ( crate ) fn num_points ( & self ) -> usize {
51- self . num_points
52- }
53-
54- /// Converts a `Location` into a `PointIndex`. O(1).
55- pub ( crate ) fn point_from_location ( & self , location : Location ) -> PointIndex {
56- let Location { block, statement_index } = location;
57- let start_index = self . statements_before_block [ block] ;
58- PointIndex :: new ( start_index + statement_index)
59- }
60-
61- /// Converts a `Location` into a `PointIndex`. O(1).
62- pub ( crate ) fn entry_point ( & self , block : BasicBlock ) -> PointIndex {
63- let start_index = self . statements_before_block [ block] ;
64- PointIndex :: new ( start_index)
65- }
66-
67- /// Return the PointIndex for the block start of this index.
68- pub ( crate ) fn to_block_start ( & self , index : PointIndex ) -> PointIndex {
69- PointIndex :: new ( self . statements_before_block [ self . basic_blocks [ index] ] )
70- }
71-
72- /// Converts a `PointIndex` back to a location. O(1).
73- pub ( crate ) fn to_location ( & self , index : PointIndex ) -> Location {
74- assert ! ( index. index( ) < self . num_points) ;
75- let block = self . basic_blocks [ index] ;
76- let start_index = self . statements_before_block [ block] ;
77- let statement_index = index. index ( ) - start_index;
78- Location { block, statement_index }
79- }
80-
81- /// Sometimes we get point-indices back from bitsets that may be
82- /// out of range (because they round up to the nearest 2^N number
83- /// of bits). Use this function to filter such points out if you
84- /// like.
85- pub ( crate ) fn point_in_range ( & self , index : PointIndex ) -> bool {
86- index. index ( ) < self . num_points
87- }
88- }
89-
90- rustc_index:: newtype_index! {
91- /// A single integer representing a `Location` in the MIR control-flow
92- /// graph. Constructed efficiently from `RegionValueElements`.
93- #[ debug_format = "PointIndex({})" ]
94- pub struct PointIndex { }
95- }
96-
9713rustc_index:: newtype_index! {
9814 /// A single integer representing a `ty::Placeholder`.
9915 #[ debug_format = "PlaceholderIndex({})" ]
@@ -116,73 +32,6 @@ pub(crate) enum RegionElement {
11632 PlaceholderRegion ( ty:: PlaceholderRegion ) ,
11733}
11834
119- /// When we initially compute liveness, we use an interval matrix storing
120- /// liveness ranges for each region-vid.
121- pub ( crate ) struct LivenessValues < N : Idx > {
122- elements : Rc < RegionValueElements > ,
123- points : SparseIntervalMatrix < N , PointIndex > ,
124- }
125-
126- impl < N : Idx > LivenessValues < N > {
127- /// Creates a new set of "region values" that tracks causal information.
128- /// Each of the regions in num_region_variables will be initialized with an
129- /// empty set of points and no causal information.
130- pub ( crate ) fn new ( elements : Rc < RegionValueElements > ) -> Self {
131- Self { points : SparseIntervalMatrix :: new ( elements. num_points ) , elements }
132- }
133-
134- /// Iterate through each region that has a value in this set.
135- pub ( crate ) fn rows ( & self ) -> impl Iterator < Item = N > {
136- self . points . rows ( )
137- }
138-
139- /// Adds the given element to the value for the given region. Returns whether
140- /// the element is newly added (i.e., was not already present).
141- pub ( crate ) fn add_element ( & mut self , row : N , location : Location ) -> bool {
142- debug ! ( "LivenessValues::add(r={:?}, location={:?})" , row, location) ;
143- let index = self . elements . point_from_location ( location) ;
144- self . points . insert ( row, index)
145- }
146-
147- /// Adds all the elements in the given bit array into the given
148- /// region. Returns whether any of them are newly added.
149- pub ( crate ) fn add_elements ( & mut self , row : N , locations : & IntervalSet < PointIndex > ) -> bool {
150- debug ! ( "LivenessValues::add_elements(row={:?}, locations={:?})" , row, locations) ;
151- self . points . union_row ( row, locations)
152- }
153-
154- /// Adds all the control-flow points to the values for `r`.
155- pub ( crate ) fn add_all_points ( & mut self , row : N ) {
156- self . points . insert_all_into_row ( row) ;
157- }
158-
159- /// Returns `true` if the region `r` contains the given element.
160- pub ( crate ) fn contains ( & self , row : N , location : Location ) -> bool {
161- let index = self . elements . point_from_location ( location) ;
162- self . points . row ( row) . is_some_and ( |r| r. contains ( index) )
163- }
164-
165- /// Returns an iterator of all the elements contained by the region `r`
166- pub ( crate ) fn get_elements ( & self , row : N ) -> impl Iterator < Item = Location > + ' _ {
167- self . points
168- . row ( row)
169- . into_iter ( )
170- . flat_map ( |set| set. iter ( ) )
171- . take_while ( move |& p| self . elements . point_in_range ( p) )
172- . map ( move |p| self . elements . to_location ( p) )
173- }
174-
175- /// Returns a "pretty" string value of the region. Meant for debugging.
176- pub ( crate ) fn region_value_str ( & self , r : N ) -> String {
177- region_value_str ( self . get_elements ( r) . map ( RegionElement :: Location ) )
178- }
179-
180- #[ inline]
181- pub ( crate ) fn point_from_location ( & self , location : Location ) -> PointIndex {
182- self . elements . point_from_location ( location)
183- }
184- }
185-
18635/// Maps from `ty::PlaceholderRegion` values that are used in the rest of
18736/// rustc to the internal `PlaceholderIndex` values that are used in
18837/// NLL.
@@ -234,7 +83,7 @@ impl PlaceholderIndices {
23483/// it would also contain various points from within the function.
23584#[ derive( Clone ) ]
23685pub ( crate ) struct RegionValues < N : Idx > {
237- elements : Rc < RegionValueElements > ,
86+ elements : Rc < DenseLocationMap > ,
23887 placeholder_indices : Rc < PlaceholderIndices > ,
23988 points : SparseIntervalMatrix < N , PointIndex > ,
24089 free_regions : SparseBitMatrix < N , RegionVid > ,
@@ -249,14 +98,14 @@ impl<N: Idx> RegionValues<N> {
24998 /// Each of the regions in num_region_variables will be initialized with an
25099 /// empty set of points and no causal information.
251100 pub ( crate ) fn new (
252- elements : & Rc < RegionValueElements > ,
101+ elements : & Rc < DenseLocationMap > ,
253102 num_universal_regions : usize ,
254103 placeholder_indices : & Rc < PlaceholderIndices > ,
255104 ) -> Self {
256105 let num_placeholders = placeholder_indices. len ( ) ;
257106 Self {
258107 elements : elements. clone ( ) ,
259- points : SparseIntervalMatrix :: new ( elements. num_points ) ,
108+ points : SparseIntervalMatrix :: new ( elements. num_points ( ) ) ,
260109 placeholder_indices : placeholder_indices. clone ( ) ,
261110 free_regions : SparseBitMatrix :: new ( num_universal_regions) ,
262111 placeholders : SparseBitMatrix :: new ( num_placeholders) ,
@@ -308,7 +157,7 @@ impl<N: Idx> RegionValues<N> {
308157 /// elements for the region `from` from `values` and add them to
309158 /// the region `to` in `self`.
310159 pub ( crate ) fn merge_liveness < M : Idx > ( & mut self , to : N , from : M , values : & LivenessValues < M > ) {
311- if let Some ( set) = values. points . row ( from) {
160+ if let Some ( set) = values. get_intervals ( from) {
312161 self . points . union_row ( to, set) ;
313162 }
314163 }
@@ -421,7 +270,7 @@ impl ToElementIndex for ty::PlaceholderRegion {
421270}
422271
423272pub ( crate ) fn location_set_str (
424- elements : & RegionValueElements ,
273+ elements : & DenseLocationMap ,
425274 points : impl IntoIterator < Item = PointIndex > ,
426275) -> String {
427276 region_value_str (
0 commit comments