@@ -179,27 +179,6 @@ impl LivenessResult {
179
179
block,
180
180
statement_index,
181
181
} ;
182
- let terminator_defs_uses = self . defs_uses ( mir, terminator_location, & data. terminator ) ;
183
- terminator_defs_uses. apply ( & mut bits) ;
184
- callback ( terminator_location, & bits) ;
185
-
186
- // Compute liveness before each statement (in rev order) and invoke callback.
187
- for statement in data. statements . iter ( ) . rev ( ) {
188
- statement_index -= 1 ;
189
- let statement_location = Location {
190
- block,
191
- statement_index,
192
- } ;
193
- let statement_defs_uses = self . defs_uses ( mir, statement_location, statement) ;
194
- statement_defs_uses. apply ( & mut bits) ;
195
- callback ( statement_location, & bits) ;
196
- }
197
- }
198
-
199
- fn defs_uses < ' tcx , V > ( & self , mir : & Mir < ' tcx > , location : Location , thing : & V ) -> DefsUses
200
- where
201
- V : MirVisitable < ' tcx > ,
202
- {
203
182
let locals = mir. local_decls . len ( ) ;
204
183
let mut visitor = DefsUsesVisitor {
205
184
mode : self . mode ,
@@ -208,12 +187,22 @@ impl LivenessResult {
208
187
uses : LocalSet :: new_empty ( locals) ,
209
188
} ,
210
189
} ;
211
-
212
190
// Visit the various parts of the basic block in reverse. If we go
213
191
// forward, the logic in `add_def` and `add_use` would be wrong.
214
- thing. apply ( location, & mut visitor) ;
192
+ visitor. update_bits_and_do_callback ( terminator_location, & data. terminator , & mut bits,
193
+ & mut callback) ;
215
194
216
- visitor. defs_uses
195
+ // Compute liveness before each statement (in rev order) and invoke callback.
196
+ for statement in data. statements . iter ( ) . rev ( ) {
197
+ statement_index -= 1 ;
198
+ let statement_location = Location {
199
+ block,
200
+ statement_index,
201
+ } ;
202
+ visitor. defs_uses . clear ( ) ;
203
+ visitor. update_bits_and_do_callback ( statement_location, statement, & mut bits,
204
+ & mut callback) ;
205
+ }
217
206
}
218
207
}
219
208
@@ -304,6 +293,11 @@ struct DefsUses {
304
293
}
305
294
306
295
impl DefsUses {
296
+ fn clear ( & mut self ) {
297
+ self . uses . clear ( ) ;
298
+ self . defs . clear ( ) ;
299
+ }
300
+
307
301
fn apply ( & self , bits : & mut LocalSet ) -> bool {
308
302
bits. subtract ( & self . defs ) | bits. union ( & self . uses )
309
303
}
@@ -338,6 +332,22 @@ impl DefsUses {
338
332
}
339
333
}
340
334
335
+ impl DefsUsesVisitor {
336
+ /// Update `bits` with the effects of `value` and call `callback`. We
337
+ /// should always visit in reverse order. This method assumes that we have
338
+ /// not visited anything before; if you have, clear `bits` first.
339
+ fn update_bits_and_do_callback < ' tcx , OP > ( & mut self , location : Location ,
340
+ value : & impl MirVisitable < ' tcx > , bits : & mut LocalSet ,
341
+ callback : & mut OP )
342
+ where
343
+ OP : FnMut ( Location , & LocalSet ) ,
344
+ {
345
+ value. apply ( location, self ) ;
346
+ self . defs_uses . apply ( bits) ;
347
+ callback ( location, bits) ;
348
+ }
349
+ }
350
+
341
351
impl < ' tcx > Visitor < ' tcx > for DefsUsesVisitor {
342
352
fn visit_local ( & mut self , & local: & Local , context : PlaceContext < ' tcx > , _: Location ) {
343
353
match categorize ( context, self . mode ) {
0 commit comments