]> git.lizzy.rs Git - rust.git/commitdiff
execute liveness, write a simple test
authorNiko Matsakis <niko@alum.mit.edu>
Tue, 24 Oct 2017 14:42:47 +0000 (10:42 -0400)
committerNiko Matsakis <niko@alum.mit.edu>
Tue, 31 Oct 2017 16:41:37 +0000 (12:41 -0400)
src/librustc_mir/transform/nll/mod.rs
src/test/mir-opt/nll/liveness-interblock.rs [new file with mode: 0644]

index 805e9c976e4f0ed6774b1ebfb1e14620b7977bab..ebc241e44ffdd823b11d1418fcd26a3fcc19ee48 100644 (file)
@@ -21,6 +21,7 @@
 use syntax_pos::DUMMY_SP;
 use std::collections::HashMap;
 use std::fmt;
+use util::liveness;
 
 use util as mir_util;
 use self::mir_util::PassWhere;
@@ -151,13 +152,34 @@ fn run_pass<'a, 'tcx>(&self,
         tcx.infer_ctxt().enter(|infcx| {
             // Clone mir so we can mutate it without disturbing the rest of the compiler
             let mut renumbered_mir = mir.clone();
+
             let mut visitor = NLLVisitor::new(&infcx);
             visitor.visit_mir(&mut renumbered_mir);
+
+            let liveness = liveness::liveness_of_locals(&renumbered_mir);
+
             mir_util::dump_mir(tcx, None, "nll", &0, source, mir, |pass_where, out| {
-                if let PassWhere::BeforeCFG = pass_where {
-                    for (index, value) in visitor.regions.iter_enumerated() {
-                        writeln!(out, "// R{:03}: {:?}", index.0, value)?;
+                match pass_where {
+                    // Before the CFG, dump out the values for each region variable.
+                    PassWhere::BeforeCFG => {
+                        for (index, value) in visitor.regions.iter_enumerated() {
+                            writeln!(out, "| R{:03}: {:?}", index.0, value)?;
+                        }
                     }
+
+                    // Before each basic block, dump out the values
+                    // that are live on entry to the basic block.
+                    PassWhere::BeforeBlock(bb) => {
+                        let local_set = &liveness.ins[bb];
+                        writeln!(out, "    | Variables live on entry to the block {:?}:", bb)?;
+                        for local in local_set.iter() {
+                            writeln!(out, "    | - {:?}", local)?;
+                        }
+                    }
+
+                    PassWhere::InCFG(_) => { }
+
+                    PassWhere::AfterCFG => { }
                 }
                 Ok(())
             });
diff --git a/src/test/mir-opt/nll/liveness-interblock.rs b/src/test/mir-opt/nll/liveness-interblock.rs
new file mode 100644 (file)
index 0000000..e3c7ab3
--- /dev/null
@@ -0,0 +1,47 @@
+// Copyright 2012-2016 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// compile-flags:-Znll
+
+fn cond() -> bool { false }
+
+fn make_live(x: usize) { }
+
+fn make_dead() { }
+
+fn main() {
+    let x = 5;
+
+    if cond() {
+        make_live(x);
+    } else {
+        // x should be dead on entry to this block
+        make_dead();
+    }
+}
+
+// END RUST SOURCE
+// START rustc.node18.nll.0.mir
+//     | Variables live on entry to the block bb2:
+//     | - _1
+//     bb2: {
+//         StorageLive(_4);
+//         _4 = _1;
+//         _3 = const make_live(_4) -> bb4;
+//     }
+// END rustc.node18.nll.0.mir
+// START rustc.node18.nll.0.mir
+//     | Variables live on entry to the block bb3:
+//     bb3: {
+//         _5 = const make_dead() -> bb5;
+//     }
+// END rustc.node18.nll.0.mir
+
+