Skip to content

Commit 9c60161

Browse files
committed
Simplify the cleanup_post_borrowck passes
1 parent bf446c8 commit 9c60161

File tree

3 files changed

+25
-90
lines changed

3 files changed

+25
-90
lines changed

src/librustc_mir/transform/cleanup_post_borrowck.rs

+18-79
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,51 @@
1-
//! This module provides two passes:
1+
//! This module provides a pass to replacing the following statements with
2+
//! [`Nop`]s
23
//!
3-
//! - [`CleanAscribeUserType`], that replaces all [`AscribeUserType`]
4-
//! statements with [`Nop`].
5-
//! - [`CleanFakeReadsAndBorrows`], that replaces all [`FakeRead`] statements
6-
//! and borrows that are read by [`ForMatchGuard`] fake reads with [`Nop`].
4+
//! - [`AscribeUserType`]
5+
//! - [`FakeRead`]
6+
//! - [`Assign`] statements with a [`Shallow`] borrow
77
//!
88
//! The `CleanFakeReadsAndBorrows` "pass" is actually implemented as two
99
//! traversals (aka visits) of the input MIR. The first traversal,
1010
//! [`DeleteAndRecordFakeReads`], deletes the fake reads and finds the
1111
//! temporaries read by [`ForMatchGuard`] reads, and [`DeleteFakeBorrows`]
1212
//! deletes the initialization of those temporaries.
1313
//!
14-
//! [`CleanAscribeUserType`]: cleanup_post_borrowck::CleanAscribeUserType
15-
//! [`CleanFakeReadsAndBorrows`]: cleanup_post_borrowck::CleanFakeReadsAndBorrows
16-
//! [`DeleteAndRecordFakeReads`]: cleanup_post_borrowck::DeleteAndRecordFakeReads
17-
//! [`DeleteFakeBorrows`]: cleanup_post_borrowck::DeleteFakeBorrows
1814
//! [`AscribeUserType`]: rustc::mir::StatementKind::AscribeUserType
19-
//! [`Nop`]: rustc::mir::StatementKind::Nop
15+
//! [`Shallow`]: rustc::mir::BorrowKind::Shallow
2016
//! [`FakeRead`]: rustc::mir::StatementKind::FakeRead
21-
//! [`ForMatchGuard`]: rustc::mir::FakeReadCause::ForMatchGuard
22-
23-
use rustc_data_structures::fx::FxHashSet;
17+
//! [`Nop`]: rustc::mir::StatementKind::Nop
2418
25-
use rustc::mir::{BasicBlock, FakeReadCause, Local, Location, Mir, Place};
19+
use rustc::mir::{BasicBlock, BorrowKind, Rvalue, Location, Mir};
2620
use rustc::mir::{Statement, StatementKind};
2721
use rustc::mir::visit::MutVisitor;
2822
use rustc::ty::TyCtxt;
2923
use crate::transform::{MirPass, MirSource};
3024

31-
pub struct CleanAscribeUserType;
25+
pub struct CleanupNonCodegenStatements;
3226

33-
pub struct DeleteAscribeUserType;
27+
pub struct DeleteNonCodegenStatements;
3428

35-
impl MirPass for CleanAscribeUserType {
29+
impl MirPass for CleanupNonCodegenStatements {
3630
fn run_pass<'a, 'tcx>(&self,
3731
_tcx: TyCtxt<'a, 'tcx, 'tcx>,
3832
_source: MirSource<'tcx>,
3933
mir: &mut Mir<'tcx>) {
40-
let mut delete = DeleteAscribeUserType;
34+
let mut delete = DeleteNonCodegenStatements;
4135
delete.visit_mir(mir);
4236
}
4337
}
4438

45-
impl<'tcx> MutVisitor<'tcx> for DeleteAscribeUserType {
46-
fn visit_statement(&mut self,
47-
block: BasicBlock,
48-
statement: &mut Statement<'tcx>,
49-
location: Location) {
50-
if let StatementKind::AscribeUserType(..) = statement.kind {
51-
statement.make_nop();
52-
}
53-
self.super_statement(block, statement, location);
54-
}
55-
}
56-
57-
pub struct CleanFakeReadsAndBorrows;
58-
59-
#[derive(Default)]
60-
pub struct DeleteAndRecordFakeReads {
61-
fake_borrow_temporaries: FxHashSet<Local>,
62-
}
63-
64-
pub struct DeleteFakeBorrows {
65-
fake_borrow_temporaries: FxHashSet<Local>,
66-
}
67-
68-
// Removes any FakeReads from the MIR
69-
impl MirPass for CleanFakeReadsAndBorrows {
70-
fn run_pass<'a, 'tcx>(&self,
71-
_tcx: TyCtxt<'a, 'tcx, 'tcx>,
72-
_source: MirSource<'tcx>,
73-
mir: &mut Mir<'tcx>) {
74-
let mut delete_reads = DeleteAndRecordFakeReads::default();
75-
delete_reads.visit_mir(mir);
76-
let mut delete_borrows = DeleteFakeBorrows {
77-
fake_borrow_temporaries: delete_reads.fake_borrow_temporaries,
78-
};
79-
delete_borrows.visit_mir(mir);
80-
}
81-
}
82-
83-
impl<'tcx> MutVisitor<'tcx> for DeleteAndRecordFakeReads {
84-
fn visit_statement(&mut self,
85-
block: BasicBlock,
86-
statement: &mut Statement<'tcx>,
87-
location: Location) {
88-
if let StatementKind::FakeRead(cause, ref place) = statement.kind {
89-
if let FakeReadCause::ForMatchGuard = cause {
90-
match *place {
91-
Place::Local(local) => self.fake_borrow_temporaries.insert(local),
92-
_ => bug!("Fake match guard read of non-local: {:?}", place),
93-
};
94-
}
95-
statement.make_nop();
96-
}
97-
self.super_statement(block, statement, location);
98-
}
99-
}
100-
101-
impl<'tcx> MutVisitor<'tcx> for DeleteFakeBorrows {
39+
impl<'tcx> MutVisitor<'tcx> for DeleteNonCodegenStatements {
10240
fn visit_statement(&mut self,
10341
block: BasicBlock,
10442
statement: &mut Statement<'tcx>,
10543
location: Location) {
106-
if let StatementKind::Assign(Place::Local(local), _) = statement.kind {
107-
if self.fake_borrow_temporaries.contains(&local) {
108-
statement.make_nop();
109-
}
44+
match statement.kind {
45+
StatementKind::AscribeUserType(..)
46+
| StatementKind::Assign(_, box Rvalue::Ref(_, BorrowKind::Shallow, _))
47+
| StatementKind::FakeRead(..) => statement.make_nop(),
48+
_ => (),
11049
}
11150
self.super_statement(block, statement, location);
11251
}

src/librustc_mir/transform/mod.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -241,15 +241,11 @@ fn optimized_mir<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx
241241

242242
let mut mir = tcx.mir_validated(def_id).steal();
243243
run_passes(tcx, &mut mir, InstanceDef::Item(def_id), MirPhase::Optimized, &[
244-
// Remove all things not needed by analysis
244+
// Remove all things only needed by analysis
245245
&no_landing_pads::NoLandingPads,
246246
&simplify_branches::SimplifyBranches::new("initial"),
247247
&remove_noop_landing_pads::RemoveNoopLandingPads,
248-
// Remove all `AscribeUserType` statements.
249-
&cleanup_post_borrowck::CleanAscribeUserType,
250-
// Remove all `FakeRead` statements and the borrows that are only
251-
// used for checking matches
252-
&cleanup_post_borrowck::CleanFakeReadsAndBorrows,
248+
&cleanup_post_borrowck::CleanupNonCodegenStatements,
253249

254250
&simplify::SimplifyCfg::new("early-opt"),
255251

src/test/mir-opt/remove_fake_borrows.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn main() {
1717

1818
// END RUST SOURCE
1919

20-
// START rustc.match_guard.CleanFakeReadsAndBorrows.before.mir
20+
// START rustc.match_guard.CleanupNonCodegenStatements.before.mir
2121
// bb0: {
2222
// FakeRead(ForMatchedPlace, _1);
2323
// _3 = discriminant(_1);
@@ -66,9 +66,9 @@ fn main() {
6666
// bb10: {
6767
// resume;
6868
// }
69-
// END rustc.match_guard.CleanFakeReadsAndBorrows.before.mir
69+
// END rustc.match_guard.CleanupNonCodegenStatements.before.mir
7070

71-
// START rustc.match_guard.CleanFakeReadsAndBorrows.after.mir
71+
// START rustc.match_guard.CleanupNonCodegenStatements.after.mir
7272
// bb0: {
7373
// nop;
7474
// _3 = discriminant(_1);
@@ -116,5 +116,5 @@ fn main() {
116116
// }
117117
// bb10: {
118118
// resume;
119-
// }
120-
// END rustc.match_guard.CleanFakeReadsAndBorrows.after.mir
119+
// }
120+
// END rustc.match_guard.CleanupNonCodegenStatements.after.mir

0 commit comments

Comments
 (0)