Skip to content

Commit 899c7ad

Browse files
committed
avoid unnecessary copies in liveness computation
1 parent 7523c73 commit 899c7ad

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

src/librustc_mir/util/liveness.rs

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -171,40 +171,33 @@ pub fn liveness_of_locals<'tcx>(mir: &Mir<'tcx>) -> LivenessResult {
171171
block(b, locals)
172172
}).collect();
173173

174-
let copy = |from: &IndexVec<BasicBlock, LocalSet>, to: &mut IndexVec<BasicBlock, LocalSet>| {
175-
for (i, set) in to.iter_enumerated_mut() {
176-
set.clone_from(&from[i]);
177-
}
178-
};
179-
180174
let mut ins: IndexVec<_, _> = mir.basic_blocks()
181175
.indices()
182-
.map(|_| LocalSet::new_empty(locals)).collect();
176+
.map(|_| LocalSet::new_empty(locals))
177+
.collect();
183178
let mut outs = ins.clone();
184179

185-
let mut ins_ = ins.clone();
186-
let mut outs_ = outs.clone();
187-
188-
loop {
189-
copy(&ins, &mut ins_);
190-
copy(&outs, &mut outs_);
180+
let mut changed = true;
181+
let mut bits = LocalSet::new_empty(locals);
182+
while changed {
183+
changed = false;
191184

192185
for b in mir.basic_blocks().indices().rev() {
193-
// out = ∪ {ins of successors}
194-
outs[b].clear();
186+
// outs[b] = ∪ {ins of successors}
187+
bits.clear();
195188
for &successor in mir.basic_blocks()[b].terminator().successors().into_iter() {
196-
outs[b].union(&ins[successor]);
189+
bits.union(&ins[successor]);
197190
}
191+
outs[b].clone_from(&bits);
198192

199-
// in = use ∪ (out - def)
200-
ins[b].clone_from(&outs[b]);
193+
// bits = use ∪ (bits - def)
194+
def_use[b].apply(&mut bits);
201195

202-
// FIXME use the return value to detect if we have changed things
203-
def_use[b].apply(&mut ins[b]);
204-
}
205-
206-
if ins_ == ins && outs_ == outs {
207-
break;
196+
// update bits on entry and flag if they have changed
197+
if ins[b] != bits {
198+
ins[b].clone_from(&bits);
199+
changed = true;
200+
}
208201
}
209202
}
210203

0 commit comments

Comments
 (0)