Skip to content

Commit c42a645

Browse files
committed
execute liveness, write a simple test
1 parent 6713736 commit c42a645

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

src/librustc_mir/transform/nll/mod.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use rustc_data_structures::indexed_vec::{IndexVec, Idx};
2121
use syntax_pos::DUMMY_SP;
2222
use std::collections::HashMap;
2323
use std::fmt;
24+
use util::liveness;
2425

2526
use util as mir_util;
2627
use self::mir_util::PassWhere;
@@ -151,13 +152,34 @@ impl MirPass for NLL {
151152
tcx.infer_ctxt().enter(|infcx| {
152153
// Clone mir so we can mutate it without disturbing the rest of the compiler
153154
let mut renumbered_mir = mir.clone();
155+
154156
let mut visitor = NLLVisitor::new(&infcx);
155157
visitor.visit_mir(&mut renumbered_mir);
158+
159+
let liveness = liveness::liveness_of_locals(&renumbered_mir);
160+
156161
mir_util::dump_mir(tcx, None, "nll", &0, source, mir, |pass_where, out| {
157-
if let PassWhere::BeforeCFG = pass_where {
158-
for (index, value) in visitor.regions.iter_enumerated() {
159-
writeln!(out, "// R{:03}: {:?}", index.0, value)?;
162+
match pass_where {
163+
// Before the CFG, dump out the values for each region variable.
164+
PassWhere::BeforeCFG => {
165+
for (index, value) in visitor.regions.iter_enumerated() {
166+
writeln!(out, "| R{:03}: {:?}", index.0, value)?;
167+
}
160168
}
169+
170+
// Before each basic block, dump out the values
171+
// that are live on entry to the basic block.
172+
PassWhere::BeforeBlock(bb) => {
173+
let local_set = &liveness.ins[bb];
174+
writeln!(out, " | Variables live on entry to the block {:?}:", bb)?;
175+
for local in local_set.iter() {
176+
writeln!(out, " | - {:?}", local)?;
177+
}
178+
}
179+
180+
PassWhere::InCFG(_) => { }
181+
182+
PassWhere::AfterCFG => { }
161183
}
162184
Ok(())
163185
});
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2012-2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags:-Znll
12+
13+
fn cond() -> bool { false }
14+
15+
fn make_live(x: usize) { }
16+
17+
fn make_dead() { }
18+
19+
fn main() {
20+
let x = 5;
21+
22+
if cond() {
23+
make_live(x);
24+
} else {
25+
// x should be dead on entry to this block
26+
make_dead();
27+
}
28+
}
29+
30+
// END RUST SOURCE
31+
// START rustc.node18.nll.0.mir
32+
// | Variables live on entry to the block bb2:
33+
// | - _1
34+
// bb2: {
35+
// StorageLive(_4);
36+
// _4 = _1;
37+
// _3 = const make_live(_4) -> bb4;
38+
// }
39+
// END rustc.node18.nll.0.mir
40+
// START rustc.node18.nll.0.mir
41+
// | Variables live on entry to the block bb3:
42+
// bb3: {
43+
// _5 = const make_dead() -> bb5;
44+
// }
45+
// END rustc.node18.nll.0.mir
46+
47+

0 commit comments

Comments
 (0)