2
2
#![ deny( rustc:: diagnostic_outside_of_impl) ]
3
3
use rustc_data_structures:: fx:: FxIndexSet ;
4
4
use rustc_index:: bit_set:: SparseBitMatrix ;
5
- use rustc_index:: interval:: IntervalSet ;
6
5
use rustc_index:: interval:: SparseIntervalMatrix ;
7
6
use rustc_index:: Idx ;
8
- use rustc_index:: IndexVec ;
9
- use rustc_middle:: mir:: { BasicBlock , Body , Location } ;
7
+ use rustc_middle:: mir:: { BasicBlock , Location } ;
10
8
use rustc_middle:: ty:: { self , RegionVid } ;
9
+ use rustc_mir_dataflow:: points:: { DenseLocationMap , LivenessValues , PointIndex } ;
11
10
use std:: fmt:: Debug ;
12
11
use std:: rc:: Rc ;
13
12
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
-
98
13
rustc_index:: newtype_index! {
99
14
/// A single integer representing a `ty::Placeholder`.
100
15
#[ debug_format = "PlaceholderIndex({})" ]
@@ -117,73 +32,6 @@ pub(crate) enum RegionElement {
117
32
PlaceholderRegion ( ty:: PlaceholderRegion ) ,
118
33
}
119
34
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
-
187
35
/// Maps from `ty::PlaceholderRegion` values that are used in the rest of
188
36
/// rustc to the internal `PlaceholderIndex` values that are used in
189
37
/// NLL.
@@ -235,7 +83,7 @@ impl PlaceholderIndices {
235
83
/// it would also contain various points from within the function.
236
84
#[ derive( Clone ) ]
237
85
pub ( crate ) struct RegionValues < N : Idx > {
238
- elements : Rc < RegionValueElements > ,
86
+ elements : Rc < DenseLocationMap > ,
239
87
placeholder_indices : Rc < PlaceholderIndices > ,
240
88
points : SparseIntervalMatrix < N , PointIndex > ,
241
89
free_regions : SparseBitMatrix < N , RegionVid > ,
@@ -250,14 +98,14 @@ impl<N: Idx> RegionValues<N> {
250
98
/// Each of the regions in num_region_variables will be initialized with an
251
99
/// empty set of points and no causal information.
252
100
pub ( crate ) fn new (
253
- elements : & Rc < RegionValueElements > ,
101
+ elements : & Rc < DenseLocationMap > ,
254
102
num_universal_regions : usize ,
255
103
placeholder_indices : & Rc < PlaceholderIndices > ,
256
104
) -> Self {
257
105
let num_placeholders = placeholder_indices. len ( ) ;
258
106
Self {
259
107
elements : elements. clone ( ) ,
260
- points : SparseIntervalMatrix :: new ( elements. num_points ) ,
108
+ points : SparseIntervalMatrix :: new ( elements. num_points ( ) ) ,
261
109
placeholder_indices : placeholder_indices. clone ( ) ,
262
110
free_regions : SparseBitMatrix :: new ( num_universal_regions) ,
263
111
placeholders : SparseBitMatrix :: new ( num_placeholders) ,
@@ -309,7 +157,7 @@ impl<N: Idx> RegionValues<N> {
309
157
/// elements for the region `from` from `values` and add them to
310
158
/// the region `to` in `self`.
311
159
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) {
313
161
self . points . union_row ( to, set) ;
314
162
}
315
163
}
@@ -422,7 +270,7 @@ impl ToElementIndex for ty::PlaceholderRegion {
422
270
}
423
271
424
272
pub ( crate ) fn location_set_str (
425
- elements : & RegionValueElements ,
273
+ elements : & DenseLocationMap ,
426
274
points : impl IntoIterator < Item = PointIndex > ,
427
275
) -> String {
428
276
region_value_str (
0 commit comments