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
- #[ debug_format = "PointIndex({})" ]
94
- pub struct PointIndex { }
95
- }
96
-
97
13
rustc_index:: newtype_index! {
98
14
/// A single integer representing a `ty::Placeholder`.
99
15
#[ debug_format = "PlaceholderIndex({})" ]
@@ -116,68 +32,6 @@ pub(crate) enum RegionElement {
116
32
PlaceholderRegion ( ty:: PlaceholderRegion ) ,
117
33
}
118
34
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
-
181
35
/// Maps from `ty::PlaceholderRegion` values that are used in the rest of
182
36
/// rustc to the internal `PlaceholderIndex` values that are used in
183
37
/// NLL.
@@ -229,7 +83,7 @@ impl PlaceholderIndices {
229
83
/// it would also contain various points from within the function.
230
84
#[ derive( Clone ) ]
231
85
pub ( crate ) struct RegionValues < N : Idx > {
232
- elements : Rc < RegionValueElements > ,
86
+ elements : Rc < DenseLocationMap > ,
233
87
placeholder_indices : Rc < PlaceholderIndices > ,
234
88
points : SparseIntervalMatrix < N , PointIndex > ,
235
89
free_regions : SparseBitMatrix < N , RegionVid > ,
@@ -244,14 +98,14 @@ impl<N: Idx> RegionValues<N> {
244
98
/// Each of the regions in num_region_variables will be initialized with an
245
99
/// empty set of points and no causal information.
246
100
pub ( crate ) fn new (
247
- elements : & Rc < RegionValueElements > ,
101
+ elements : & Rc < DenseLocationMap > ,
248
102
num_universal_regions : usize ,
249
103
placeholder_indices : & Rc < PlaceholderIndices > ,
250
104
) -> Self {
251
105
let num_placeholders = placeholder_indices. len ( ) ;
252
106
Self {
253
107
elements : elements. clone ( ) ,
254
- points : SparseIntervalMatrix :: new ( elements. num_points ) ,
108
+ points : SparseIntervalMatrix :: new ( elements. num_points ( ) ) ,
255
109
placeholder_indices : placeholder_indices. clone ( ) ,
256
110
free_regions : SparseBitMatrix :: new ( num_universal_regions) ,
257
111
placeholders : SparseBitMatrix :: new ( num_placeholders) ,
@@ -303,7 +157,7 @@ impl<N: Idx> RegionValues<N> {
303
157
/// elements for the region `from` from `values` and add them to
304
158
/// the region `to` in `self`.
305
159
pub ( crate ) fn merge_liveness < M : Idx > ( & mut self , to : N , from : M , values : & LivenessValues < M > ) {
306
- if let Some ( set) = values. points . row ( from) {
160
+ if let Some ( set) = values. get_intervals ( from) {
307
161
self . points . union_row ( to, set) ;
308
162
}
309
163
}
@@ -416,7 +270,7 @@ impl ToElementIndex for ty::PlaceholderRegion {
416
270
}
417
271
418
272
pub ( crate ) fn location_set_str (
419
- elements : & RegionValueElements ,
273
+ elements : & DenseLocationMap ,
420
274
points : impl IntoIterator < Item = PointIndex > ,
421
275
) -> String {
422
276
region_value_str (
0 commit comments