@@ -117,65 +117,68 @@ pub(crate) enum RegionElement {
117
117
PlaceholderRegion ( ty:: PlaceholderRegion ) ,
118
118
}
119
119
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 > {
120
+ /// Records the CFG locations where each region is live. When we initially compute liveness, we use
121
+ /// an interval matrix storing liveness ranges for each region-vid.
122
+ pub ( crate ) struct LivenessValues {
123
123
elements : Rc < RegionValueElements > ,
124
- points : SparseIntervalMatrix < N , PointIndex > ,
124
+ points : SparseIntervalMatrix < RegionVid , PointIndex > ,
125
125
}
126
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.
127
+ impl LivenessValues {
128
+ /// Create an empty map of regions to locations where they're live.
131
129
pub ( crate ) fn new ( elements : Rc < RegionValueElements > ) -> Self {
132
130
Self { points : SparseIntervalMatrix :: new ( elements. num_points ) , elements }
133
131
}
134
132
135
133
/// Iterate through each region that has a value in this set.
136
- pub ( crate ) fn rows ( & self ) -> impl Iterator < Item = N > {
134
+ pub ( crate ) fn regions ( & self ) -> impl Iterator < Item = RegionVid > {
137
135
self . points . rows ( )
138
136
}
139
137
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)
138
+ /// Records `region` as being live at the given `location`.
139
+ pub ( crate ) fn add_location ( & mut self , region : RegionVid , location : Location ) {
140
+ debug ! ( "LivenessValues::add_location(region={:?}, location={:?})" , region, location) ;
141
+ let point = self . elements . point_from_location ( location) ;
142
+ self . points . insert ( region, point) ;
146
143
}
147
144
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)
145
+ /// Records `region` as being live at all the given `points`.
146
+ pub ( crate ) fn add_points ( & mut self , region : RegionVid , points : & IntervalSet < PointIndex > ) {
147
+ debug ! ( "LivenessValues::add_points(region={:?}, points={:?})" , region, points) ;
148
+ self . points . union_row ( region, points) ;
153
149
}
154
150
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 ) ;
151
+ /// Records `region` as being live at all the control-flow points.
152
+ pub ( crate ) fn add_all_points ( & mut self , region : RegionVid ) {
153
+ self . points . insert_all_into_row ( region ) ;
158
154
}
159
155
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) )
156
+ /// Returns whether `region` is marked live at the given `location`.
157
+ pub ( crate ) fn is_live_at ( & self , region : RegionVid , location : Location ) -> bool {
158
+ let point = self . elements . point_from_location ( location) ;
159
+ self . points . row ( region) . is_some_and ( |r| r. contains ( point) )
160
+ }
161
+
162
+ /// Returns whether `region` is marked live at any location.
163
+ pub ( crate ) fn is_live_anywhere ( & self , region : RegionVid ) -> bool {
164
+ self . live_points ( region) . next ( ) . is_some ( )
164
165
}
165
166
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 > + ' _ {
167
+ /// Returns an iterator of all the points where ` region` is live.
168
+ fn live_points ( & self , region : RegionVid ) -> impl Iterator < Item = PointIndex > + ' _ {
168
169
self . points
169
- . row ( row )
170
+ . row ( region )
170
171
. into_iter ( )
171
172
. 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) )
173
+ . take_while ( |& p| self . elements . point_in_range ( p) )
174
174
}
175
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 ) )
176
+ /// For debugging purposes, returns a pretty-printed string of the points where the `region` is
177
+ /// live.
178
+ pub ( crate ) fn pretty_print_live_points ( & self , region : RegionVid ) -> String {
179
+ pretty_print_region_elements (
180
+ self . live_points ( region) . map ( |p| RegionElement :: Location ( self . elements . to_location ( p) ) ) ,
181
+ )
179
182
}
180
183
181
184
#[ inline]
@@ -308,7 +311,7 @@ impl<N: Idx> RegionValues<N> {
308
311
/// `self[to] |= values[from]`, essentially: that is, take all the
309
312
/// elements for the region `from` from `values` and add them to
310
313
/// the region `to` in `self`.
311
- pub ( crate ) fn merge_liveness < M : Idx > ( & mut self , to : N , from : M , values : & LivenessValues < M > ) {
314
+ pub ( crate ) fn merge_liveness ( & mut self , to : N , from : RegionVid , values : & LivenessValues ) {
312
315
if let Some ( set) = values. points . row ( from) {
313
316
self . points . union_row ( to, set) ;
314
317
}
@@ -377,7 +380,7 @@ impl<N: Idx> RegionValues<N> {
377
380
378
381
/// Returns a "pretty" string value of the region. Meant for debugging.
379
382
pub ( crate ) fn region_value_str ( & self , r : N ) -> String {
380
- region_value_str ( self . elements_contained_in ( r) )
383
+ pretty_print_region_elements ( self . elements_contained_in ( r) )
381
384
}
382
385
}
383
386
@@ -421,11 +424,12 @@ impl ToElementIndex for ty::PlaceholderRegion {
421
424
}
422
425
}
423
426
424
- pub ( crate ) fn location_set_str (
427
+ /// For debugging purposes, returns a pretty-printed string of the given points.
428
+ pub ( crate ) fn pretty_print_points (
425
429
elements : & RegionValueElements ,
426
430
points : impl IntoIterator < Item = PointIndex > ,
427
431
) -> String {
428
- region_value_str (
432
+ pretty_print_region_elements (
429
433
points
430
434
. into_iter ( )
431
435
. take_while ( |& p| elements. point_in_range ( p) )
@@ -434,7 +438,8 @@ pub(crate) fn location_set_str(
434
438
)
435
439
}
436
440
437
- fn region_value_str ( elements : impl IntoIterator < Item = RegionElement > ) -> String {
441
+ /// For debugging purposes, returns a pretty-printed string of the given region elements.
442
+ fn pretty_print_region_elements ( elements : impl IntoIterator < Item = RegionElement > ) -> String {
438
443
let mut result = String :: new ( ) ;
439
444
result. push ( '{' ) ;
440
445
0 commit comments