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- #[ orderable]
94- #[ debug_format = "PointIndex({})" ]
95- pub struct PointIndex { }
96- }
97-
9813rustc_index:: newtype_index! {
9914 /// A single integer representing a `ty::Placeholder`.
10015 #[ debug_format = "PlaceholderIndex({})" ]
@@ -117,73 +32,6 @@ pub(crate) enum RegionElement {
11732 PlaceholderRegion ( ty:: PlaceholderRegion ) ,
11833}
11934
120- /// When we initially compute liveness, we use an interval matrix storing
121- /// liveness ranges for each region-vid.
122- pub ( crate ) struct LivenessValues < N : Idx > {
123- elements : Rc < RegionValueElements > ,
124- points : SparseIntervalMatrix < N , PointIndex > ,
125- }
126-
127- impl < N : Idx > LivenessValues < N > {
128- /// Creates a new set of "region values" that tracks causal information.
129- /// Each of the regions in num_region_variables will be initialized with an
130- /// empty set of points and no causal information.
131- pub ( crate ) fn new ( elements : Rc < RegionValueElements > ) -> Self {
132- Self { points : SparseIntervalMatrix :: new ( elements. num_points ) , elements }
133- }
134-
135- /// Iterate through each region that has a value in this set.
136- pub ( crate ) fn rows ( & self ) -> impl Iterator < Item = N > {
137- self . points . rows ( )
138- }
139-
140- /// Adds the given element to the value for the given region. Returns whether
141- /// the element is newly added (i.e., was not already present).
142- pub ( crate ) fn add_element ( & mut self , row : N , location : Location ) -> bool {
143- debug ! ( "LivenessValues::add(r={:?}, location={:?})" , row, location) ;
144- let index = self . elements . point_from_location ( location) ;
145- self . points . insert ( row, index)
146- }
147-
148- /// Adds all the elements in the given bit array into the given
149- /// region. Returns whether any of them are newly added.
150- pub ( crate ) fn add_elements ( & mut self , row : N , locations : & IntervalSet < PointIndex > ) -> bool {
151- debug ! ( "LivenessValues::add_elements(row={:?}, locations={:?})" , row, locations) ;
152- self . points . union_row ( row, locations)
153- }
154-
155- /// Adds all the control-flow points to the values for `r`.
156- pub ( crate ) fn add_all_points ( & mut self , row : N ) {
157- self . points . insert_all_into_row ( row) ;
158- }
159-
160- /// Returns `true` if the region `r` contains the given element.
161- pub ( crate ) fn contains ( & self , row : N , location : Location ) -> bool {
162- let index = self . elements . point_from_location ( location) ;
163- self . points . row ( row) . is_some_and ( |r| r. contains ( index) )
164- }
165-
166- /// Returns an iterator of all the elements contained by the region `r`
167- pub ( crate ) fn get_elements ( & self , row : N ) -> impl Iterator < Item = Location > + ' _ {
168- self . points
169- . row ( row)
170- . into_iter ( )
171- . flat_map ( |set| set. iter ( ) )
172- . take_while ( move |& p| self . elements . point_in_range ( p) )
173- . map ( move |p| self . elements . to_location ( p) )
174- }
175-
176- /// Returns a "pretty" string value of the region. Meant for debugging.
177- pub ( crate ) fn region_value_str ( & self , r : N ) -> String {
178- region_value_str ( self . get_elements ( r) . map ( RegionElement :: Location ) )
179- }
180-
181- #[ inline]
182- pub ( crate ) fn point_from_location ( & self , location : Location ) -> PointIndex {
183- self . elements . point_from_location ( location)
184- }
185- }
186-
18735/// Maps from `ty::PlaceholderRegion` values that are used in the rest of
18836/// rustc to the internal `PlaceholderIndex` values that are used in
18937/// NLL.
@@ -235,7 +83,7 @@ impl PlaceholderIndices {
23583/// it would also contain various points from within the function.
23684#[ derive( Clone ) ]
23785pub ( crate ) struct RegionValues < N : Idx > {
238- elements : Rc < RegionValueElements > ,
86+ elements : Rc < DenseLocationMap > ,
23987 placeholder_indices : Rc < PlaceholderIndices > ,
24088 points : SparseIntervalMatrix < N , PointIndex > ,
24189 free_regions : SparseBitMatrix < N , RegionVid > ,
@@ -250,14 +98,14 @@ impl<N: Idx> RegionValues<N> {
25098 /// Each of the regions in num_region_variables will be initialized with an
25199 /// empty set of points and no causal information.
252100 pub ( crate ) fn new (
253- elements : & Rc < RegionValueElements > ,
101+ elements : & Rc < DenseLocationMap > ,
254102 num_universal_regions : usize ,
255103 placeholder_indices : & Rc < PlaceholderIndices > ,
256104 ) -> Self {
257105 let num_placeholders = placeholder_indices. len ( ) ;
258106 Self {
259107 elements : elements. clone ( ) ,
260- points : SparseIntervalMatrix :: new ( elements. num_points ) ,
108+ points : SparseIntervalMatrix :: new ( elements. num_points ( ) ) ,
261109 placeholder_indices : placeholder_indices. clone ( ) ,
262110 free_regions : SparseBitMatrix :: new ( num_universal_regions) ,
263111 placeholders : SparseBitMatrix :: new ( num_placeholders) ,
@@ -309,7 +157,7 @@ impl<N: Idx> RegionValues<N> {
309157 /// elements for the region `from` from `values` and add them to
310158 /// the region `to` in `self`.
311159 pub ( crate ) fn merge_liveness < M : Idx > ( & mut self , to : N , from : M , values : & LivenessValues < M > ) {
312- if let Some ( set) = values. points . row ( from) {
160+ if let Some ( set) = values. get_intervals ( from) {
313161 self . points . union_row ( to, set) ;
314162 }
315163 }
@@ -422,7 +270,7 @@ impl ToElementIndex for ty::PlaceholderRegion {
422270}
423271
424272pub ( crate ) fn location_set_str (
425- elements : & RegionValueElements ,
273+ elements : & DenseLocationMap ,
426274 points : impl IntoIterator < Item = PointIndex > ,
427275) -> String {
428276 region_value_str (
0 commit comments