@@ -51,8 +51,8 @@ impl SimplifyCfg {
51
51
let mut worklist = vec ! [ START_BLOCK ] ;
52
52
while let Some ( bb) = worklist. pop ( ) {
53
53
// Temporarily swap out the terminator we're modifying to keep borrowck happy
54
- let mut terminator = Terminator :: Diverge ;
55
- mem :: swap ( & mut terminator , & mut mir . basic_block_data_mut ( bb ) . terminator ) ;
54
+ let mut terminator = mem :: replace ( & mut mir . basic_block_data_mut ( bb ) . terminator ,
55
+ Terminator :: Diverge ) ;
56
56
57
57
// Shortcut chains of empty blocks that just jump from one to the next
58
58
for target in terminator. successors_mut ( ) {
@@ -71,27 +71,30 @@ impl SimplifyCfg {
71
71
}
72
72
73
73
// See if we can merge the target block into this one
74
- match terminator {
75
- Terminator :: Goto { target } if target. index ( ) > DIVERGE_BLOCK . index ( ) &&
76
- predecessor_map. num_predecessors ( target) == 1 => {
77
- changed = true ;
78
- let mut other_data = BasicBlockData {
79
- statements : Vec :: new ( ) ,
80
- terminator : Terminator :: Goto { target : target}
81
- } ;
82
- mem:: swap ( & mut other_data, mir. basic_block_data_mut ( target) ) ;
83
-
84
- // target used to have 1 predecessor (bb), and still has only one (itself)
85
- // All the successors of target have had target replaced by bb in their
86
- // list of predecessors, keeping the number the same.
87
-
88
- let data = mir. basic_block_data_mut ( bb) ;
89
- data. statements . append ( & mut other_data. statements ) ;
90
- mem:: swap ( & mut data. terminator , & mut other_data. terminator ) ;
74
+ while let Terminator :: Goto { target } = terminator {
75
+ if target. index ( ) <= DIVERGE_BLOCK . index ( ) || predecessor_map. num_predecessors ( target) > 1 {
76
+ break ;
91
77
}
92
- _ => mir. basic_block_data_mut ( bb) . terminator = terminator
78
+
79
+ changed = true ;
80
+
81
+ let mut other_data = mem:: replace ( mir. basic_block_data_mut ( target) , BasicBlockData {
82
+ statements : Vec :: new ( ) ,
83
+ terminator : Terminator :: Goto { target : target }
84
+ } ) ;
85
+
86
+ // target used to have 1 predecessor (bb), and still has only one (itself)
87
+ // All the successors of target have had target replaced by bb in their
88
+ // list of predecessors, keeping the number the same.
89
+
90
+ let data = mir. basic_block_data_mut ( bb) ;
91
+ data. statements . append ( & mut other_data. statements ) ;
92
+ terminator = other_data. terminator ;
93
93
}
94
94
95
+ // Restore the terminator we swapped out for Diverge
96
+ mir. basic_block_data_mut ( bb) . terminator = terminator;
97
+
95
98
for succ in mir. basic_block_data ( bb) . terminator . successors ( ) {
96
99
if !seen[ succ. index ( ) ] {
97
100
seen[ succ. index ( ) ] = true ;
0 commit comments