2017-07-16 16:26:37 -04:00
|
|
|
// Copyright 2017 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.
|
|
|
|
|
2017-10-09 22:16:57 -04:00
|
|
|
use self::infer::InferenceContext;
|
2017-07-17 22:26:21 -04:00
|
|
|
use rustc::ty::TypeFoldable;
|
2017-08-03 00:16:36 -04:00
|
|
|
use rustc::ty::subst::{Kind, Substs};
|
|
|
|
use rustc::ty::{Ty, TyCtxt, ClosureSubsts, RegionVid, RegionKind};
|
2017-07-17 22:26:21 -04:00
|
|
|
use rustc::mir::{Mir, Location, Rvalue, BasicBlock, Statement, StatementKind};
|
2017-07-30 15:08:07 -04:00
|
|
|
use rustc::mir::visit::{MutVisitor, Lookup};
|
2017-07-16 16:26:37 -04:00
|
|
|
use rustc::mir::transform::{MirPass, MirSource};
|
2017-10-09 22:16:57 -04:00
|
|
|
use rustc::infer::{self as rustc_infer, InferCtxt};
|
2017-10-24 13:32:00 -04:00
|
|
|
use rustc::util::nodemap::{FxHashMap, FxHashSet};
|
2017-09-27 14:29:11 -03:00
|
|
|
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
|
2017-08-03 00:16:36 -04:00
|
|
|
use syntax_pos::DUMMY_SP;
|
|
|
|
use std::collections::HashMap;
|
2017-09-28 00:14:34 -04:00
|
|
|
use std::fmt;
|
2017-10-24 10:42:47 -04:00
|
|
|
use util::liveness;
|
2017-09-28 00:14:34 -04:00
|
|
|
|
|
|
|
use util as mir_util;
|
|
|
|
use self::mir_util::PassWhere;
|
2017-07-16 16:26:37 -04:00
|
|
|
|
2017-10-09 22:16:57 -04:00
|
|
|
mod infer;
|
|
|
|
|
2017-07-16 16:26:37 -04:00
|
|
|
#[allow(dead_code)]
|
2017-07-17 22:26:21 -04:00
|
|
|
struct NLLVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
|
2017-08-04 09:06:55 -04:00
|
|
|
lookup_map: HashMap<RegionVid, Lookup>,
|
2017-09-27 14:29:11 -03:00
|
|
|
regions: IndexVec<RegionIndex, Region>,
|
2017-10-10 19:37:34 -03:00
|
|
|
#[allow(dead_code)]
|
|
|
|
infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
|
2017-07-16 16:26:37 -04:00
|
|
|
}
|
|
|
|
|
2017-07-17 22:26:21 -04:00
|
|
|
impl<'a, 'gcx, 'tcx> NLLVisitor<'a, 'gcx, 'tcx> {
|
2017-10-10 19:37:34 -03:00
|
|
|
pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>) -> Self {
|
2017-07-16 16:26:37 -04:00
|
|
|
NLLVisitor {
|
2017-08-06 22:54:09 -07:00
|
|
|
infcx,
|
2017-08-03 00:16:36 -04:00
|
|
|
lookup_map: HashMap::new(),
|
2017-09-27 14:29:11 -03:00
|
|
|
regions: IndexVec::new(),
|
2017-07-16 16:26:37 -04:00
|
|
|
}
|
|
|
|
}
|
2017-07-17 22:26:21 -04:00
|
|
|
|
2017-10-09 22:16:57 -04:00
|
|
|
pub fn into_results(self) -> (HashMap<RegionVid, Lookup>, IndexVec<RegionIndex, Region>) {
|
|
|
|
(self.lookup_map, self.regions)
|
2017-08-04 09:06:55 -04:00
|
|
|
}
|
|
|
|
|
2017-09-26 22:24:19 -04:00
|
|
|
fn renumber_regions<T>(&mut self, value: &T) -> T where T: TypeFoldable<'tcx> {
|
2017-07-17 22:26:21 -04:00
|
|
|
self.infcx.tcx.fold_regions(value, &mut false, |_region, _depth| {
|
2017-09-26 22:24:19 -04:00
|
|
|
self.regions.push(Region::default());
|
2017-10-09 22:16:57 -04:00
|
|
|
self.infcx.next_region_var(rustc_infer::MiscVariable(DUMMY_SP))
|
2017-07-17 22:26:21 -04:00
|
|
|
})
|
|
|
|
}
|
2017-07-16 16:26:37 -04:00
|
|
|
|
2017-08-03 00:16:36 -04:00
|
|
|
fn store_region(&mut self, region: &RegionKind, lookup: Lookup) {
|
|
|
|
if let RegionKind::ReVar(rid) = *region {
|
|
|
|
self.lookup_map.entry(rid).or_insert(lookup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn store_ty_regions(&mut self, ty: &Ty<'tcx>, lookup: Lookup) {
|
|
|
|
for region in ty.regions() {
|
|
|
|
self.store_region(region, lookup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn store_kind_regions(&mut self, kind: &'tcx Kind, lookup: Lookup) {
|
|
|
|
if let Some(ty) = kind.as_type() {
|
|
|
|
self.store_ty_regions(&ty, lookup);
|
|
|
|
} else if let Some(region) = kind.as_region() {
|
|
|
|
self.store_region(region, lookup);
|
|
|
|
}
|
|
|
|
}
|
2017-07-17 22:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'gcx, 'tcx> MutVisitor<'tcx> for NLLVisitor<'a, 'gcx, 'tcx> {
|
2017-07-30 15:08:07 -04:00
|
|
|
fn visit_ty(&mut self, ty: &mut Ty<'tcx>, lookup: Lookup) {
|
2017-07-17 22:26:21 -04:00
|
|
|
let old_ty = *ty;
|
2017-08-03 00:16:36 -04:00
|
|
|
*ty = self.renumber_regions(&old_ty);
|
|
|
|
self.store_ty_regions(ty, lookup);
|
2017-07-17 22:26:21 -04:00
|
|
|
}
|
|
|
|
|
2017-07-30 15:08:07 -04:00
|
|
|
fn visit_substs(&mut self, substs: &mut &'tcx Substs<'tcx>, location: Location) {
|
2017-08-03 00:16:36 -04:00
|
|
|
*substs = self.renumber_regions(&{*substs});
|
|
|
|
let lookup = Lookup::Loc(location);
|
|
|
|
for kind in *substs {
|
|
|
|
self.store_kind_regions(kind, lookup);
|
|
|
|
}
|
2017-07-17 22:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>, location: Location) {
|
|
|
|
match *rvalue {
|
|
|
|
Rvalue::Ref(ref mut r, _, _) => {
|
|
|
|
let old_r = *r;
|
2017-08-03 00:16:36 -04:00
|
|
|
*r = self.renumber_regions(&old_r);
|
|
|
|
let lookup = Lookup::Loc(location);
|
|
|
|
self.store_region(r, lookup);
|
2017-07-17 22:26:21 -04:00
|
|
|
}
|
|
|
|
Rvalue::Use(..) |
|
|
|
|
Rvalue::Repeat(..) |
|
|
|
|
Rvalue::Len(..) |
|
|
|
|
Rvalue::Cast(..) |
|
|
|
|
Rvalue::BinaryOp(..) |
|
|
|
|
Rvalue::CheckedBinaryOp(..) |
|
|
|
|
Rvalue::UnaryOp(..) |
|
|
|
|
Rvalue::Discriminant(..) |
|
|
|
|
Rvalue::NullaryOp(..) |
|
|
|
|
Rvalue::Aggregate(..) => {
|
|
|
|
// These variants don't contain regions.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.super_rvalue(rvalue, location);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_closure_substs(&mut self,
|
2017-07-30 15:08:07 -04:00
|
|
|
substs: &mut ClosureSubsts<'tcx>,
|
|
|
|
location: Location) {
|
2017-08-03 00:16:36 -04:00
|
|
|
*substs = self.renumber_regions(substs);
|
|
|
|
let lookup = Lookup::Loc(location);
|
|
|
|
for kind in substs.substs {
|
|
|
|
self.store_kind_regions(kind, lookup);
|
|
|
|
}
|
2017-07-17 22:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_statement(&mut self,
|
|
|
|
block: BasicBlock,
|
|
|
|
statement: &mut Statement<'tcx>,
|
|
|
|
location: Location) {
|
|
|
|
if let StatementKind::EndRegion(_) = statement.kind {
|
|
|
|
statement.kind = StatementKind::Nop;
|
|
|
|
}
|
|
|
|
self.super_statement(block, statement, location);
|
|
|
|
}
|
2017-07-16 16:26:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// MIR Pass for non-lexical lifetimes
|
|
|
|
pub struct NLL;
|
|
|
|
|
|
|
|
impl MirPass for NLL {
|
|
|
|
fn run_pass<'a, 'tcx>(&self,
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-09-28 00:14:34 -04:00
|
|
|
source: MirSource,
|
2017-10-24 13:32:00 -04:00
|
|
|
input_mir: &mut Mir<'tcx>) {
|
2017-07-17 22:26:21 -04:00
|
|
|
if !tcx.sess.opts.debugging_opts.nll {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tcx.infer_ctxt().enter(|infcx| {
|
2017-08-03 00:16:36 -04:00
|
|
|
// Clone mir so we can mutate it without disturbing the rest of the compiler
|
2017-10-24 13:32:00 -04:00
|
|
|
let mir = &mut input_mir.clone();
|
2017-10-24 10:42:47 -04:00
|
|
|
|
2017-10-10 19:37:34 -03:00
|
|
|
let mut visitor = NLLVisitor::new(&infcx);
|
2017-10-24 13:32:00 -04:00
|
|
|
visitor.visit_mir(mir);
|
|
|
|
|
|
|
|
let liveness = liveness::liveness_of_locals(mir);
|
|
|
|
|
|
|
|
let liveness_per_location: FxHashMap<_, _> =
|
|
|
|
mir
|
|
|
|
.basic_blocks()
|
|
|
|
.indices()
|
|
|
|
.flat_map(|bb| {
|
|
|
|
let mut results = vec![];
|
|
|
|
liveness.simulate_block(&mir, bb, |location, local_set| {
|
|
|
|
results.push((location, local_set.clone()));
|
|
|
|
});
|
|
|
|
results
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
mir_util::dump_mir(infcx.tcx, None, "nll", &0, source, mir, |pass_where, out| {
|
2017-10-24 10:42:47 -04:00
|
|
|
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)?;
|
|
|
|
}
|
2017-09-28 00:14:34 -04:00
|
|
|
}
|
2017-10-24 10:42:47 -04:00
|
|
|
|
|
|
|
// 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)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-24 13:32:00 -04:00
|
|
|
PassWhere::InCFG(location) => {
|
|
|
|
let local_set = &liveness_per_location[&location];
|
|
|
|
let mut string = String::new();
|
|
|
|
for local in local_set.iter() {
|
|
|
|
string.push_str(&format!(", {:?}", local));
|
|
|
|
}
|
|
|
|
if !string.is_empty() {
|
|
|
|
writeln!(out, " | Live variables here: [{}]", &string[2..])?;
|
|
|
|
} else {
|
|
|
|
writeln!(out, " | Live variables here: []")?;
|
|
|
|
}
|
|
|
|
}
|
2017-10-24 10:42:47 -04:00
|
|
|
|
|
|
|
PassWhere::AfterCFG => { }
|
2017-09-28 00:14:34 -04:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
});
|
2017-10-09 22:16:57 -04:00
|
|
|
let (_lookup_map, regions) = visitor.into_results();
|
2017-10-10 19:18:19 -03:00
|
|
|
let mut inference_context = InferenceContext::new(regions);
|
2017-10-24 13:32:00 -04:00
|
|
|
inference_context.solve(&infcx, mir);
|
2017-07-17 22:26:21 -04:00
|
|
|
})
|
2017-07-16 16:26:37 -04:00
|
|
|
}
|
2017-09-26 22:24:19 -04:00
|
|
|
}
|
|
|
|
|
2017-09-28 00:14:34 -04:00
|
|
|
#[derive(Clone, Default, PartialEq, Eq)]
|
2017-10-10 14:25:39 -03:00
|
|
|
pub struct Region {
|
2017-09-26 22:24:19 -04:00
|
|
|
points: FxHashSet<Location>,
|
|
|
|
}
|
2017-09-27 14:29:11 -03:00
|
|
|
|
2017-09-28 00:14:34 -04:00
|
|
|
impl fmt::Debug for Region {
|
|
|
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
write!(formatter, "{:?}", self.points)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-09 22:16:57 -04:00
|
|
|
impl Region {
|
|
|
|
pub fn add_point(&mut self, point: Location) -> bool {
|
|
|
|
self.points.insert(point)
|
|
|
|
}
|
2017-09-28 00:14:34 -04:00
|
|
|
|
2017-10-09 22:16:57 -04:00
|
|
|
pub fn may_contain(&self, point: Location) -> bool {
|
|
|
|
self.points.contains(&point)
|
|
|
|
}
|
|
|
|
}
|
2017-09-28 00:14:34 -04:00
|
|
|
|
2017-10-03 14:19:56 -03:00
|
|
|
newtype_index!(RegionIndex);
|