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-11-10 19:20:35 +02:00
|
|
|
use rustc::hir::def_id::DefId;
|
2017-10-30 05:14:40 -04:00
|
|
|
use rustc::mir::Mir;
|
2017-10-24 14:20:47 -04:00
|
|
|
use rustc::infer::InferCtxt;
|
2017-11-06 04:15:38 -05:00
|
|
|
use rustc::ty::{self, RegionKind, RegionVid};
|
2017-10-24 14:20:47 -04:00
|
|
|
use rustc::util::nodemap::FxHashMap;
|
|
|
|
use std::collections::BTreeSet;
|
2017-11-10 19:20:35 +02:00
|
|
|
use transform::MirSource;
|
2017-10-25 10:59:50 -04:00
|
|
|
use util::liveness::{self, LivenessMode, LivenessResult, LocalSet};
|
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-24 14:20:47 -04:00
|
|
|
mod constraint_generation;
|
2017-10-30 04:51:10 -04:00
|
|
|
mod free_regions;
|
2017-10-25 18:04:57 -04:00
|
|
|
mod subtype;
|
2017-10-09 22:16:57 -04:00
|
|
|
|
2017-10-26 09:48:20 -04:00
|
|
|
pub(crate) mod region_infer;
|
2017-10-24 14:20:47 -04:00
|
|
|
use self::region_infer::RegionInferenceContext;
|
2017-07-16 16:26:37 -04:00
|
|
|
|
2017-10-24 14:20:47 -04:00
|
|
|
mod renumber;
|
2017-07-17 22:26:21 -04:00
|
|
|
|
2017-10-25 12:09:01 -04:00
|
|
|
/// Computes the (non-lexical) regions from the input MIR.
|
|
|
|
///
|
|
|
|
/// This may result in errors being reported.
|
|
|
|
pub fn compute_regions<'a, 'gcx, 'tcx>(
|
|
|
|
infcx: &InferCtxt<'a, 'gcx, 'tcx>,
|
2017-11-10 19:20:35 +02:00
|
|
|
def_id: DefId,
|
2017-10-26 09:48:20 -04:00
|
|
|
mir: &mut Mir<'tcx>,
|
2017-10-30 04:51:10 -04:00
|
|
|
) -> RegionInferenceContext<'tcx> {
|
|
|
|
// Compute named region information.
|
2017-11-10 19:20:35 +02:00
|
|
|
let free_regions = &free_regions::free_regions(infcx, def_id);
|
2017-10-30 04:51:10 -04:00
|
|
|
|
2017-10-25 12:09:01 -04:00
|
|
|
// Replace all regions with fresh inference variables.
|
2017-11-06 04:33:15 -05:00
|
|
|
renumber::renumber_mir(infcx, free_regions, mir);
|
2017-10-25 12:09:01 -04:00
|
|
|
|
|
|
|
// Compute what is live where.
|
|
|
|
let liveness = &LivenessResults {
|
|
|
|
regular: liveness::liveness_of_locals(
|
|
|
|
&mir,
|
|
|
|
LivenessMode {
|
|
|
|
include_regular_use: true,
|
|
|
|
include_drops: false,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
|
|
|
|
drop: liveness::liveness_of_locals(
|
|
|
|
&mir,
|
|
|
|
LivenessMode {
|
|
|
|
include_regular_use: false,
|
|
|
|
include_drops: true,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create the region inference context, generate the constraints,
|
|
|
|
// and then solve them.
|
2017-11-06 05:21:48 -05:00
|
|
|
let var_origins = infcx.take_region_var_origins();
|
|
|
|
let mut regioncx = RegionInferenceContext::new(var_origins, free_regions, mir);
|
2017-11-10 19:20:35 +02:00
|
|
|
let param_env = infcx.tcx.param_env(def_id);
|
|
|
|
constraint_generation::generate_constraints(infcx, &mut regioncx, &mir, param_env, liveness);
|
2017-10-30 04:51:10 -04:00
|
|
|
regioncx.solve(infcx, &mir);
|
2017-10-25 12:09:01 -04:00
|
|
|
|
|
|
|
// Dump MIR results into a file, if that is enabled. This let us
|
|
|
|
// write unit-tests.
|
2017-11-10 19:20:35 +02:00
|
|
|
dump_mir_results(infcx, liveness, MirSource::item(def_id), &mir, ®ioncx);
|
2017-10-25 12:09:01 -04:00
|
|
|
|
2017-10-26 09:48:20 -04:00
|
|
|
regioncx
|
2017-10-25 12:09:01 -04:00
|
|
|
}
|
|
|
|
|
2017-10-24 16:20:47 -04:00
|
|
|
struct LivenessResults {
|
|
|
|
regular: LivenessResult,
|
|
|
|
drop: LivenessResult,
|
|
|
|
}
|
|
|
|
|
2017-10-24 14:20:47 -04:00
|
|
|
fn dump_mir_results<'a, 'gcx, 'tcx>(
|
|
|
|
infcx: &InferCtxt<'a, 'gcx, 'tcx>,
|
2017-10-24 16:20:47 -04:00
|
|
|
liveness: &LivenessResults,
|
2017-10-24 14:20:47 -04:00
|
|
|
source: MirSource,
|
2017-10-26 09:48:20 -04:00
|
|
|
mir: &Mir<'tcx>,
|
|
|
|
regioncx: &RegionInferenceContext,
|
2017-10-24 14:20:47 -04:00
|
|
|
) {
|
|
|
|
if !mir_util::dump_enabled(infcx.tcx, "nll", source) {
|
|
|
|
return;
|
2017-07-17 22:26:21 -04:00
|
|
|
}
|
|
|
|
|
2017-10-24 16:20:47 -04:00
|
|
|
let regular_liveness_per_location: FxHashMap<_, _> = mir.basic_blocks()
|
|
|
|
.indices()
|
|
|
|
.flat_map(|bb| {
|
|
|
|
let mut results = vec![];
|
2017-10-25 10:59:50 -04:00
|
|
|
liveness
|
|
|
|
.regular
|
|
|
|
.simulate_block(&mir, bb, |location, local_set| {
|
|
|
|
results.push((location, local_set.clone()));
|
|
|
|
});
|
2017-10-24 16:20:47 -04:00
|
|
|
results
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let drop_liveness_per_location: FxHashMap<_, _> = mir.basic_blocks()
|
2017-10-24 14:20:47 -04:00
|
|
|
.indices()
|
|
|
|
.flat_map(|bb| {
|
|
|
|
let mut results = vec![];
|
2017-10-25 10:59:50 -04:00
|
|
|
liveness
|
|
|
|
.drop
|
|
|
|
.simulate_block(&mir, bb, |location, local_set| {
|
|
|
|
results.push((location, local_set.clone()));
|
|
|
|
});
|
2017-10-24 14:20:47 -04:00
|
|
|
results
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
mir_util::dump_mir(infcx.tcx, None, "nll", &0, source, mir, |pass_where, out| {
|
|
|
|
match pass_where {
|
|
|
|
// Before the CFG, dump out the values for each region variable.
|
|
|
|
PassWhere::BeforeCFG => for region in regioncx.regions() {
|
2017-10-25 12:09:01 -04:00
|
|
|
writeln!(
|
|
|
|
out,
|
|
|
|
"| {:?}: {:?}",
|
|
|
|
region,
|
|
|
|
regioncx.region_value(region)
|
|
|
|
)?;
|
2017-10-24 14:20:47 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Before each basic block, dump out the values
|
|
|
|
// that are live on entry to the basic block.
|
|
|
|
PassWhere::BeforeBlock(bb) => {
|
2017-10-25 10:59:50 -04:00
|
|
|
let s = live_variable_set(&liveness.regular.ins[bb], &liveness.drop.ins[bb]);
|
|
|
|
writeln!(out, " | Live variables on entry to {:?}: {}", bb, s)?;
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
2017-07-16 16:26:37 -04:00
|
|
|
|
2017-10-24 14:20:47 -04:00
|
|
|
PassWhere::InCFG(location) => {
|
2017-10-25 10:59:50 -04:00
|
|
|
let s = live_variable_set(
|
|
|
|
®ular_liveness_per_location[&location],
|
|
|
|
&drop_liveness_per_location[&location],
|
|
|
|
);
|
2017-10-25 12:09:01 -04:00
|
|
|
writeln!(out, " | Live variables at {:?}: {}", location, s)?;
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
2017-07-16 16:26:37 -04:00
|
|
|
|
2017-10-24 14:20:47 -04:00
|
|
|
PassWhere::AfterCFG => {}
|
2017-07-17 22:26:21 -04:00
|
|
|
}
|
2017-10-24 14:20:47 -04:00
|
|
|
Ok(())
|
|
|
|
});
|
2017-09-26 22:24:19 -04:00
|
|
|
}
|
|
|
|
|
2017-10-24 14:20:47 -04:00
|
|
|
/// Right now, we piggy back on the `ReVar` to store our NLL inference
|
2017-11-06 04:15:38 -05:00
|
|
|
/// regions. These are indexed with `RegionVid`. This method will
|
|
|
|
/// assert that the region is a `ReVar` and extract its interal index.
|
|
|
|
/// This is reasonable because in our MIR we replace all free regions
|
|
|
|
/// with inference variables.
|
|
|
|
pub trait ToRegionVid {
|
|
|
|
fn to_region_vid(&self) -> RegionVid;
|
2017-10-24 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2017-11-06 04:15:38 -05:00
|
|
|
impl ToRegionVid for RegionKind {
|
|
|
|
fn to_region_vid(&self) -> RegionVid {
|
2017-10-24 14:20:47 -04:00
|
|
|
if let &ty::ReVar(vid) = self {
|
2017-11-06 04:15:38 -05:00
|
|
|
vid
|
2017-10-24 14:20:47 -04:00
|
|
|
} else {
|
|
|
|
bug!("region is not an ReVar: {:?}", self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-25 10:59:50 -04:00
|
|
|
|
|
|
|
fn live_variable_set(regular: &LocalSet, drops: &LocalSet) -> String {
|
|
|
|
// sort and deduplicate:
|
|
|
|
let all_locals: BTreeSet<_> = regular.iter().chain(drops.iter()).collect();
|
|
|
|
|
|
|
|
// construct a string with each local, including `(drop)` if it is
|
|
|
|
// only dropped, versus a regular use.
|
|
|
|
let mut string = String::new();
|
|
|
|
for local in all_locals {
|
|
|
|
string.push_str(&format!("{:?}", local));
|
|
|
|
|
|
|
|
if !regular.contains(&local) {
|
|
|
|
assert!(drops.contains(&local));
|
|
|
|
string.push_str(" (drop)");
|
|
|
|
}
|
|
|
|
|
|
|
|
string.push_str(", ");
|
|
|
|
}
|
|
|
|
|
2017-10-25 12:09:01 -04:00
|
|
|
let len = if string.is_empty() {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
string.len() - 2
|
|
|
|
};
|
2017-10-25 10:59:50 -04:00
|
|
|
|
|
|
|
format!("[{}]", &string[..len])
|
|
|
|
}
|