@@ -11,6 +11,10 @@ use rustc_middle::mir::*;
1111use rustc_middle:: ty:: TyCtxt ;
1212use rustc_session:: Session ;
1313
14+ /// Rearranges the basic blocks into a *reverse post-order*.
15+ ///
16+ /// Thus after this pass, all the successors of a block are later than it in the
17+ /// `IndexVec`, unless that successor is a back-edge (such as from a loop).
1418pub struct ReorderBasicBlocks ;
1519
1620impl < ' tcx > MirPass < ' tcx > for ReorderBasicBlocks {
@@ -33,6 +37,12 @@ impl<'tcx> MirPass<'tcx> for ReorderBasicBlocks {
3337 }
3438}
3539
40+ /// Rearranges the locals into *use* order.
41+ ///
42+ /// Thus after this pass, a local with a smaller [`Location`] where it was first
43+ /// assigned or referenced will have a smaller number.
44+ ///
45+ /// (Does not reorder arguments nor the [`RETURN_PLACE`].)
3646pub struct ReorderLocals ;
3747
3848impl < ' tcx > MirPass < ' tcx > for ReorderLocals {
@@ -45,8 +55,8 @@ impl<'tcx> MirPass<'tcx> for ReorderLocals {
4555 LocalFinder { map : IndexVec :: new ( ) , seen : BitSet :: new_empty ( body. local_decls . len ( ) ) } ;
4656
4757 // We can't reorder the return place or the arguments
48- for i in 0 ..=body. arg_count {
49- finder. track ( Local :: from_usize ( i ) ) ;
58+ for local in ( 0 ..=body. arg_count ) . map ( Local :: from_usize ) {
59+ finder. track ( local ) ;
5060 }
5161
5262 for ( bb, bbd) in body. basic_blocks . iter_enumerated ( ) {
@@ -64,7 +74,11 @@ impl<'tcx> MirPass<'tcx> for ReorderLocals {
6474 }
6575
6676 let mut updater = LocalUpdater { map : finder. map . invert_bijective_mapping ( ) , tcx } ;
67- debug_assert_eq ! ( updater. map[ RETURN_PLACE ] , RETURN_PLACE ) ;
77+
78+ for local in ( 0 ..=body. arg_count ) . map ( Local :: from_usize) {
79+ debug_assert_eq ! ( updater. map[ local] , local) ;
80+ }
81+
6882 updater. visit_body_preserves_cfg ( body) ;
6983
7084 permute ( & mut body. local_decls , & updater. map ) ;
@@ -112,6 +126,8 @@ impl LocalFinder {
112126
113127impl < ' tcx > Visitor < ' tcx > for LocalFinder {
114128 fn visit_local ( & mut self , l : Local , context : PlaceContext , _location : Location ) {
129+ // Exclude non-uses to keep `StorageLive` from controlling where we put
130+ // a `Local`, since it might not actually be assigned until much later.
115131 if context. is_use ( ) {
116132 self . track ( l) ;
117133 }
0 commit comments