Skip to content

Commit 3d68f5f

Browse files
committed
Improved BodyCache body impl so it only returns a sharable ref, add new body_mut method, fix visit macros, simplify usage in codegen_ssa analyzer
1 parent 30b1d9e commit 3d68f5f

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

src/librustc/mir/cache.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl<'a, 'tcx> BodyCache<&'a Body<'tcx>> {
132132
}
133133

134134
#[inline]
135-
pub fn body(&self) -> &Body<'tcx> {
135+
pub fn body(&self) -> &'a Body<'tcx> {
136136
self.body
137137
}
138138

@@ -149,7 +149,8 @@ impl<'a, 'tcx> BodyCache<&'a Body<'tcx>> {
149149

150150
impl<'a, 'tcx> Deref for BodyCache<&'a Body<'tcx>> {
151151
type Target = Body<'tcx>;
152-
fn deref(&self) -> &Body<'tcx> {
152+
153+
fn deref(&self) -> &Self::Target {
153154
self.body
154155
}
155156
}
@@ -209,7 +210,12 @@ impl<'a, 'b, 'tcx> graph::GraphSuccessors<'b> for BodyCache<&'a Body<'tcx>> {
209210

210211
impl<'a, 'tcx> BodyCache<&'a mut Body<'tcx>> {
211212
#[inline]
212-
pub fn body(&mut self) -> &mut Body<'tcx> {
213+
pub fn body(&self) -> &Body<'tcx> {
214+
self.body
215+
}
216+
217+
#[inline]
218+
pub fn body_mut(&mut self) -> &mut Body<'tcx> {
213219
self.body
214220
}
215221

@@ -227,7 +233,7 @@ impl<'a, 'tcx> BodyCache<&'a mut Body<'tcx>> {
227233
impl<'a, 'tcx> Deref for BodyCache<&'a mut Body<'tcx>> {
228234
type Target = Body<'tcx>;
229235

230-
fn deref(&self) -> &Body<'tcx> {
236+
fn deref(&self) -> &Self::Target {
231237
self.body
232238
}
233239
}

src/librustc/mir/visit.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,12 @@ macro_rules! make_mir_visitor {
247247
&mut self,
248248
body_cache: & $($mutability)? BodyCache<&'_ $($mutability)? Body<'tcx>>
249249
) {
250+
macro_rules! body {
251+
(mut) => (body_cache.body_mut());
252+
() => (body_cache.body());
253+
}
250254
let span = body_cache.body().span;
251-
if let Some(yield_ty) = &$($mutability)? body_cache.body().yield_ty {
255+
if let Some(yield_ty) = &$($mutability)? body!($($mutability)?).yield_ty {
252256
self.visit_ty(yield_ty, TyContext::YieldTy(SourceInfo {
253257
span,
254258
scope: OUTERMOST_SOURCE_SCOPE,
@@ -266,7 +270,7 @@ macro_rules! make_mir_visitor {
266270
self.visit_basic_block_data(bb, data);
267271
}
268272

269-
let body = body_cache.body();
273+
let body = body!($($mutability)?);
270274
for scope in &$($mutability)? body.source_scopes {
271275
self.visit_source_scope_data(scope);
272276
}

src/librustc_codegen_ssa/mir/analyze.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use rustc_index::bit_set::BitSet;
55
use rustc_data_structures::graph::dominators::Dominators;
66
use rustc_index::vec::{Idx, IndexVec};
7-
use rustc::mir::{self, BasicBlock, Body, BodyCache, Location, TerminatorKind};
7+
use rustc::mir::{self, Body, BodyCache, Location, TerminatorKind};
88
use rustc::mir::visit::{
99
Visitor, PlaceContext, MutatingUseContext, NonMutatingUseContext, NonUseContext,
1010
};
@@ -20,8 +20,7 @@ pub fn non_ssa_locals<'b, 'a: 'b, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
2020
fx: &mut FunctionCx<'a, 'tcx, Bx>,
2121
mir: &'b mut BodyCache<&'a Body<'tcx>>,
2222
) -> BitSet<mir::Local> {
23-
let dominators = mir.dominators();
24-
let mut analyzer = LocalAnalyzer::new(fx, mir, dominators);
23+
let mut analyzer = LocalAnalyzer::new(fx, mir);
2524

2625
analyzer.visit_body(mir);
2726

@@ -68,13 +67,15 @@ struct LocalAnalyzer<'mir, 'a, 'b, 'tcx, Bx: BuilderMethods<'a, 'tcx>> {
6867
first_assignment: IndexVec<mir::Local, Location>,
6968
}
7069

71-
impl<'mir, 'a, 'b, 'tcx, Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'b, 'tcx, Bx> {
72-
fn new(fx: &'mir FunctionCx<'a, 'tcx, Bx>, mir: &'b Body<'tcx>, dominators: Dominators<BasicBlock>) -> Self {
70+
impl<'mir, 'a, 'b, 'c, 'tcx, Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'b, 'tcx, Bx> {
71+
fn new(fx: &'mir FunctionCx<'a, 'tcx, Bx>, mir: &'c mut BodyCache<&'b Body<'tcx>>) -> Self {
7372
let invalid_location =
7473
mir::BasicBlock::new(mir.basic_blocks().len()).start_location();
74+
let dominators = mir.dominators();
75+
let body = mir.body();
7576
let mut analyzer = LocalAnalyzer {
7677
fx,
77-
mir,
78+
mir: body,
7879
dominators,
7980
non_ssa_locals: BitSet::new_empty(mir.local_decls.len()),
8081
first_assignment: IndexVec::from_elem(invalid_location, &mir.local_decls)

0 commit comments

Comments
 (0)