Skip to content

Commit eded1aa

Browse files
committed
Auto merge of #51870 - nnethercote:reuse-DefsUsesVisitor, r=nikomatsakis
Reuse the `DefsUsesVisitor` in `simulate_block()`. This avoids a bunch of allocations for the bitsets within it, speeding up a number of NLL benchmarks, the best by 1%. r? @nikomatsakis
2 parents a22bcd8 + b0c7812 commit eded1aa

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

src/librustc_mir/util/liveness.rs

+34-24
Original file line numberDiff line numberDiff line change
@@ -179,27 +179,6 @@ impl LivenessResult {
179179
block,
180180
statement_index,
181181
};
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-
{
203182
let locals = mir.local_decls.len();
204183
let mut visitor = DefsUsesVisitor {
205184
mode: self.mode,
@@ -208,12 +187,22 @@ impl LivenessResult {
208187
uses: LocalSet::new_empty(locals),
209188
},
210189
};
211-
212190
// Visit the various parts of the basic block in reverse. If we go
213191
// 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);
215194

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+
}
217206
}
218207
}
219208

@@ -304,6 +293,11 @@ struct DefsUses {
304293
}
305294

306295
impl DefsUses {
296+
fn clear(&mut self) {
297+
self.uses.clear();
298+
self.defs.clear();
299+
}
300+
307301
fn apply(&self, bits: &mut LocalSet) -> bool {
308302
bits.subtract(&self.defs) | bits.union(&self.uses)
309303
}
@@ -338,6 +332,22 @@ impl DefsUses {
338332
}
339333
}
340334

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+
341351
impl<'tcx> Visitor<'tcx> for DefsUsesVisitor {
342352
fn visit_local(&mut self, &local: &Local, context: PlaceContext<'tcx>, _: Location) {
343353
match categorize(context, self.mode) {

0 commit comments

Comments
 (0)