1
Fork 0

Replace (Body, DefId) with Body where possible

A `Body` now contains its `MirSource`, which in turn contains the
`DefId` of the item associated with the `Body`.
This commit is contained in:
Dylan MacKenzie 2020-10-04 15:22:23 -07:00
parent 4ccf5f731b
commit e72e43c730
25 changed files with 159 additions and 232 deletions

View file

@ -81,7 +81,6 @@ where
{
tcx: TyCtxt<'tcx>,
body: &'a mir::Body<'tcx>,
def_id: DefId,
dead_unwinds: Option<&'a BitSet<BasicBlock>>,
entry_sets: IndexVec<BasicBlock, A::Domain>,
pass_name: Option<&'static str>,
@ -103,18 +102,13 @@ where
T: Idx,
{
/// Creates a new `Engine` to solve a gen-kill dataflow problem.
pub fn new_gen_kill(
tcx: TyCtxt<'tcx>,
body: &'a mir::Body<'tcx>,
def_id: DefId,
analysis: A,
) -> Self {
pub fn new_gen_kill(tcx: TyCtxt<'tcx>, body: &'a mir::Body<'tcx>, analysis: A) -> Self {
// If there are no back-edges in the control-flow graph, we only ever need to apply the
// transfer function for each block exactly once (assuming that we process blocks in RPO).
//
// In this case, there's no need to compute the block transfer functions ahead of time.
if !body.is_cfg_cyclic() {
return Self::new(tcx, body, def_id, analysis, None);
return Self::new(tcx, body, analysis, None);
}
// Otherwise, compute and store the cumulative transfer function for each block.
@ -131,7 +125,7 @@ where
trans_for_block[bb].apply(state.borrow_mut());
});
Self::new(tcx, body, def_id, analysis, Some(apply_trans as Box<_>))
Self::new(tcx, body, analysis, Some(apply_trans as Box<_>))
}
}
@ -145,19 +139,13 @@ where
///
/// Gen-kill problems should use `new_gen_kill`, which will coalesce transfer functions for
/// better performance.
pub fn new_generic(
tcx: TyCtxt<'tcx>,
body: &'a mir::Body<'tcx>,
def_id: DefId,
analysis: A,
) -> Self {
Self::new(tcx, body, def_id, analysis, None)
pub fn new_generic(tcx: TyCtxt<'tcx>, body: &'a mir::Body<'tcx>, analysis: A) -> Self {
Self::new(tcx, body, analysis, None)
}
fn new(
tcx: TyCtxt<'tcx>,
body: &'a mir::Body<'tcx>,
def_id: DefId,
analysis: A,
apply_trans_for_block: Option<Box<dyn Fn(BasicBlock, &mut A::Domain)>>,
) -> Self {
@ -173,7 +161,6 @@ where
analysis,
tcx,
body,
def_id,
dead_unwinds: None,
pass_name: None,
entry_sets,
@ -209,7 +196,6 @@ where
analysis,
body,
dead_unwinds,
def_id,
mut entry_sets,
tcx,
apply_trans_for_block,
@ -261,7 +247,7 @@ where
let results = Results { analysis, entry_sets };
let res = write_graphviz_results(tcx, def_id, &body, &results, pass_name);
let res = write_graphviz_results(tcx, &body, &results, pass_name);
if let Err(e) = res {
warn!("Failed to write graphviz dataflow results: {}", e);
}
@ -276,7 +262,6 @@ where
/// `rustc_mir` attributes.
fn write_graphviz_results<A>(
tcx: TyCtxt<'tcx>,
def_id: DefId,
body: &mir::Body<'tcx>,
results: &Results<'tcx, A>,
pass_name: Option<&'static str>,
@ -285,6 +270,7 @@ where
A: Analysis<'tcx>,
A::Domain: DebugWithContext<A>,
{
let def_id = body.source.def_id();
let attrs = match RustcMirAttrs::parse(tcx, def_id) {
Ok(attrs) => attrs,
@ -323,7 +309,7 @@ where
debug!("printing dataflow results for {:?} to {}", def_id, path.display());
let mut buf = Vec::new();
let graphviz = graphviz::Formatter::new(body, def_id, results, style);
let graphviz = graphviz::Formatter::new(body, results, style);
let mut render_opts =
vec![dot::RenderOption::Fontname(tcx.sess.opts.debugging_opts.graphviz_font.clone())];
if tcx.sess.opts.debugging_opts.graphviz_dark_mode {

View file

@ -6,7 +6,6 @@ use std::{io, ops, str};
use regex::Regex;
use rustc_graphviz as dot;
use rustc_hir::def_id::DefId;
use rustc_middle::mir::{self, BasicBlock, Body, Location};
use super::fmt::{DebugDiffWithAdapter, DebugWithAdapter, DebugWithContext};
@ -33,7 +32,6 @@ where
A: Analysis<'tcx>,
{
body: &'a Body<'tcx>,
def_id: DefId,
results: &'a Results<'tcx, A>,
style: OutputStyle,
}
@ -42,13 +40,8 @@ impl<A> Formatter<'a, 'tcx, A>
where
A: Analysis<'tcx>,
{
pub fn new(
body: &'a Body<'tcx>,
def_id: DefId,
results: &'a Results<'tcx, A>,
style: OutputStyle,
) -> Self {
Formatter { body, def_id, results, style }
pub fn new(body: &'a Body<'tcx>, results: &'a Results<'tcx, A>, style: OutputStyle) -> Self {
Formatter { body, results, style }
}
}
@ -77,7 +70,7 @@ where
type Edge = CfgEdge;
fn graph_id(&self) -> dot::Id<'_> {
let name = graphviz_safe_def_name(self.def_id);
let name = graphviz_safe_def_name(self.body.source.def_id());
dot::Id::new(format!("graph_for_def_id_{}", name)).unwrap()
}

View file

@ -13,9 +13,9 @@
//! ```ignore(cross-crate-imports)
//! use rustc_mir::dataflow::Analysis; // Makes `into_engine` available.
//!
//! fn do_my_analysis(tcx: TyCtxt<'tcx>, body: &mir::Body<'tcx>, did: DefId) {
//! fn do_my_analysis(tcx: TyCtxt<'tcx>, body: &mir::Body<'tcx>) {
//! let analysis = MyAnalysis::new()
//! .into_engine(tcx, body, did)
//! .into_engine(tcx, body)
//! .iterate_to_fixpoint()
//! .into_results_cursor(body);
//!
@ -33,7 +33,6 @@
use std::borrow::BorrowMut;
use std::cmp::Ordering;
use rustc_hir::def_id::DefId;
use rustc_index::bit_set::{BitSet, HybridBitSet};
use rustc_index::vec::Idx;
use rustc_middle::mir::{self, BasicBlock, Location};
@ -218,16 +217,11 @@ pub trait Analysis<'tcx>: AnalysisDomain<'tcx> {
/// .iterate_to_fixpoint()
/// .into_results_cursor(body);
/// ```
fn into_engine(
self,
tcx: TyCtxt<'tcx>,
body: &'mir mir::Body<'tcx>,
def_id: DefId,
) -> Engine<'mir, 'tcx, Self>
fn into_engine(self, tcx: TyCtxt<'tcx>, body: &'mir mir::Body<'tcx>) -> Engine<'mir, 'tcx, Self>
where
Self: Sized,
{
Engine::new_generic(tcx, body, def_id, self)
Engine::new_generic(tcx, body, self)
}
}
@ -381,16 +375,11 @@ where
/* Extension methods */
fn into_engine(
self,
tcx: TyCtxt<'tcx>,
body: &'mir mir::Body<'tcx>,
def_id: DefId,
) -> Engine<'mir, 'tcx, Self>
fn into_engine(self, tcx: TyCtxt<'tcx>, body: &'mir mir::Body<'tcx>) -> Engine<'mir, 'tcx, Self>
where
Self: Sized,
{
Engine::new_gen_kill(tcx, body, def_id, self)
Engine::new_gen_kill(tcx, body, self)
}
}