Skip to content

Commit 4153a2e

Browse files
committed
Auto merge of #97833 - tmiasko:borrowed-locals, r=nagisa
Remove duplicated implementations of borrowed locals analysis
2 parents 53305f1 + 777bf84 commit 4153a2e

File tree

4 files changed

+30
-140
lines changed

4 files changed

+30
-140
lines changed

compiler/rustc_mir_dataflow/src/impls/borrowed_locals.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,7 @@ where
8585
self.super_rvalue(rvalue, location);
8686

8787
match rvalue {
88-
mir::Rvalue::AddressOf(_mt, borrowed_place) => {
89-
if !borrowed_place.is_indirect() {
90-
self.trans.gen(borrowed_place.local);
91-
}
92-
}
93-
94-
mir::Rvalue::Ref(_, _kind, borrowed_place) => {
88+
mir::Rvalue::AddressOf(_, borrowed_place) | mir::Rvalue::Ref(_, _, borrowed_place) => {
9589
if !borrowed_place.is_indirect() {
9690
self.trans.gen(borrowed_place.local);
9791
}
@@ -145,3 +139,23 @@ where
145139
}
146140
}
147141
}
142+
143+
/// The set of locals that are borrowed at some point in the MIR body.
144+
pub fn borrowed_locals(body: &Body<'_>) -> BitSet<Local> {
145+
struct Borrowed(BitSet<Local>);
146+
147+
impl GenKill<Local> for Borrowed {
148+
#[inline]
149+
fn gen(&mut self, elem: Local) {
150+
self.0.gen(elem)
151+
}
152+
#[inline]
153+
fn kill(&mut self, _: Local) {
154+
// Ignore borrow invalidation.
155+
}
156+
}
157+
158+
let mut borrowed = Borrowed(BitSet::new_empty(body.local_decls.len()));
159+
TransferFunction { trans: &mut borrowed }.visit_body(body);
160+
borrowed.0
161+
}

compiler/rustc_mir_dataflow/src/impls/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ mod init_locals;
2323
mod liveness;
2424
mod storage_liveness;
2525

26+
pub use self::borrowed_locals::borrowed_locals;
2627
pub use self::borrowed_locals::MaybeBorrowedLocals;
2728
pub use self::init_locals::MaybeInitializedLocals;
2829
pub use self::liveness::MaybeLiveLocals;

compiler/rustc_mir_transform/src/dead_store_elimination.rs

+6-68
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,15 @@
1313
//!
1414
1515
use rustc_index::bit_set::BitSet;
16-
use rustc_middle::{
17-
mir::{visit::Visitor, *},
18-
ty::TyCtxt,
19-
};
20-
use rustc_mir_dataflow::{impls::MaybeTransitiveLiveLocals, Analysis};
16+
use rustc_middle::mir::*;
17+
use rustc_middle::ty::TyCtxt;
18+
use rustc_mir_dataflow::impls::{borrowed_locals, MaybeTransitiveLiveLocals};
19+
use rustc_mir_dataflow::Analysis;
2120

2221
/// Performs the optimization on the body
2322
///
2423
/// The `borrowed` set must be a `BitSet` of all the locals that are ever borrowed in this body. It
25-
/// can be generated via the [`get_borrowed_locals`] function.
24+
/// can be generated via the [`borrowed_locals`] function.
2625
pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, borrowed: &BitSet<Local>) {
2726
let mut live = MaybeTransitiveLiveLocals::new(borrowed)
2827
.into_engine(tcx, body)
@@ -73,67 +72,6 @@ pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, borrowed: &BitS
7372
}
7473
}
7574

76-
pub fn get_borrowed_locals(body: &Body<'_>) -> BitSet<Local> {
77-
let mut b = BorrowedLocals(BitSet::new_empty(body.local_decls.len()));
78-
b.visit_body(body);
79-
b.0
80-
}
81-
82-
struct BorrowedLocals(BitSet<Local>);
83-
84-
impl<'tcx> Visitor<'tcx> for BorrowedLocals {
85-
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, loc: Location) {
86-
self.super_rvalue(rvalue, loc);
87-
match rvalue {
88-
Rvalue::AddressOf(_, borrowed_place) | Rvalue::Ref(_, _, borrowed_place) => {
89-
if !borrowed_place.is_indirect() {
90-
self.0.insert(borrowed_place.local);
91-
}
92-
}
93-
94-
Rvalue::Cast(..)
95-
| Rvalue::ShallowInitBox(..)
96-
| Rvalue::Use(..)
97-
| Rvalue::Repeat(..)
98-
| Rvalue::Len(..)
99-
| Rvalue::BinaryOp(..)
100-
| Rvalue::CheckedBinaryOp(..)
101-
| Rvalue::NullaryOp(..)
102-
| Rvalue::UnaryOp(..)
103-
| Rvalue::Discriminant(..)
104-
| Rvalue::Aggregate(..)
105-
| Rvalue::ThreadLocalRef(..) => {}
106-
}
107-
}
108-
109-
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
110-
self.super_terminator(terminator, location);
111-
112-
match terminator.kind {
113-
TerminatorKind::Drop { place: dropped_place, .. } => {
114-
if !dropped_place.is_indirect() {
115-
self.0.insert(dropped_place.local);
116-
}
117-
}
118-
119-
TerminatorKind::Abort
120-
| TerminatorKind::DropAndReplace { .. }
121-
| TerminatorKind::Assert { .. }
122-
| TerminatorKind::Call { .. }
123-
| TerminatorKind::FalseEdge { .. }
124-
| TerminatorKind::FalseUnwind { .. }
125-
| TerminatorKind::GeneratorDrop
126-
| TerminatorKind::Goto { .. }
127-
| TerminatorKind::Resume
128-
| TerminatorKind::Return
129-
| TerminatorKind::SwitchInt { .. }
130-
| TerminatorKind::Unreachable
131-
| TerminatorKind::Yield { .. }
132-
| TerminatorKind::InlineAsm { .. } => {}
133-
}
134-
}
135-
}
136-
13775
pub struct DeadStoreElimination;
13876

13977
impl<'tcx> MirPass<'tcx> for DeadStoreElimination {
@@ -142,7 +80,7 @@ impl<'tcx> MirPass<'tcx> for DeadStoreElimination {
14280
}
14381

14482
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
145-
let borrowed = get_borrowed_locals(body);
83+
let borrowed = borrowed_locals(body);
14684
eliminate(tcx, body, &borrowed);
14785
}
14886
}

compiler/rustc_mir_transform/src/dest_prop.rs

+2-65
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ use rustc_middle::mir::{
104104
Rvalue, Statement, StatementKind, Terminator, TerminatorKind,
105105
};
106106
use rustc_middle::ty::TyCtxt;
107-
use rustc_mir_dataflow::impls::{MaybeInitializedLocals, MaybeLiveLocals};
107+
use rustc_mir_dataflow::impls::{borrowed_locals, MaybeInitializedLocals, MaybeLiveLocals};
108108
use rustc_mir_dataflow::Analysis;
109109

110110
// Empirical measurements have resulted in some observations:
@@ -805,7 +805,7 @@ fn find_candidates<'tcx>(body: &Body<'tcx>) -> Vec<CandidateAssignment<'tcx>> {
805805
let mut visitor = FindAssignments {
806806
body,
807807
candidates: Vec::new(),
808-
ever_borrowed_locals: ever_borrowed_locals(body),
808+
ever_borrowed_locals: borrowed_locals(body),
809809
locals_used_as_array_index: locals_used_as_array_index(body),
810810
};
811811
visitor.visit_body(body);
@@ -886,69 +886,6 @@ fn is_local_required(local: Local, body: &Body<'_>) -> bool {
886886
}
887887
}
888888

889-
/// Walks MIR to find all locals that have their address taken anywhere.
890-
fn ever_borrowed_locals(body: &Body<'_>) -> BitSet<Local> {
891-
let mut visitor = BorrowCollector { locals: BitSet::new_empty(body.local_decls.len()) };
892-
visitor.visit_body(body);
893-
visitor.locals
894-
}
895-
896-
struct BorrowCollector {
897-
locals: BitSet<Local>,
898-
}
899-
900-
impl<'tcx> Visitor<'tcx> for BorrowCollector {
901-
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
902-
self.super_rvalue(rvalue, location);
903-
904-
match rvalue {
905-
Rvalue::AddressOf(_, borrowed_place) | Rvalue::Ref(_, _, borrowed_place) => {
906-
if !borrowed_place.is_indirect() {
907-
self.locals.insert(borrowed_place.local);
908-
}
909-
}
910-
911-
Rvalue::Cast(..)
912-
| Rvalue::ShallowInitBox(..)
913-
| Rvalue::Use(..)
914-
| Rvalue::Repeat(..)
915-
| Rvalue::Len(..)
916-
| Rvalue::BinaryOp(..)
917-
| Rvalue::CheckedBinaryOp(..)
918-
| Rvalue::NullaryOp(..)
919-
| Rvalue::UnaryOp(..)
920-
| Rvalue::Discriminant(..)
921-
| Rvalue::Aggregate(..)
922-
| Rvalue::ThreadLocalRef(..) => {}
923-
}
924-
}
925-
926-
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
927-
self.super_terminator(terminator, location);
928-
929-
match terminator.kind {
930-
TerminatorKind::Drop { place: dropped_place, .. }
931-
| TerminatorKind::DropAndReplace { place: dropped_place, .. } => {
932-
self.locals.insert(dropped_place.local);
933-
}
934-
935-
TerminatorKind::Abort
936-
| TerminatorKind::Assert { .. }
937-
| TerminatorKind::Call { .. }
938-
| TerminatorKind::FalseEdge { .. }
939-
| TerminatorKind::FalseUnwind { .. }
940-
| TerminatorKind::GeneratorDrop
941-
| TerminatorKind::Goto { .. }
942-
| TerminatorKind::Resume
943-
| TerminatorKind::Return
944-
| TerminatorKind::SwitchInt { .. }
945-
| TerminatorKind::Unreachable
946-
| TerminatorKind::Yield { .. }
947-
| TerminatorKind::InlineAsm { .. } => {}
948-
}
949-
}
950-
}
951-
952889
/// `PlaceElem::Index` only stores a `Local`, so we can't replace that with a full `Place`.
953890
///
954891
/// Collect locals used as indices so we don't generate candidates that are impossible to apply

0 commit comments

Comments
 (0)