@@ -11,6 +11,10 @@ use rustc_middle::mir::*;
11
11
use rustc_middle:: ty:: TyCtxt ;
12
12
use rustc_session:: Session ;
13
13
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).
14
18
pub struct ReorderBasicBlocks ;
15
19
16
20
impl < ' tcx > MirPass < ' tcx > for ReorderBasicBlocks {
@@ -33,6 +37,12 @@ impl<'tcx> MirPass<'tcx> for ReorderBasicBlocks {
33
37
}
34
38
}
35
39
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`].)
36
46
pub struct ReorderLocals ;
37
47
38
48
impl < ' tcx > MirPass < ' tcx > for ReorderLocals {
@@ -45,8 +55,8 @@ impl<'tcx> MirPass<'tcx> for ReorderLocals {
45
55
LocalFinder { map : IndexVec :: new ( ) , seen : BitSet :: new_empty ( body. local_decls . len ( ) ) } ;
46
56
47
57
// 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 ) ;
50
60
}
51
61
52
62
for ( bb, bbd) in body. basic_blocks . iter_enumerated ( ) {
@@ -64,7 +74,11 @@ impl<'tcx> MirPass<'tcx> for ReorderLocals {
64
74
}
65
75
66
76
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
+
68
82
updater. visit_body_preserves_cfg ( body) ;
69
83
70
84
permute ( & mut body. local_decls , & updater. map ) ;
@@ -112,6 +126,8 @@ impl LocalFinder {
112
126
113
127
impl < ' tcx > Visitor < ' tcx > for LocalFinder {
114
128
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.
115
131
if context. is_use ( ) {
116
132
self . track ( l) ;
117
133
}
0 commit comments