Skip to content

Commit 4e12b1f

Browse files
committed
Move ever_borrowed_locals to utilities
1 parent 1195bea commit 4e12b1f

File tree

3 files changed

+68
-63
lines changed

3 files changed

+68
-63
lines changed

compiler/rustc_mir/src/transform/dest_prop.rs

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ use crate::dataflow::impls::{MaybeInitializedLocals, MaybeLiveLocals};
100100
use crate::dataflow::Analysis;
101101
use crate::{
102102
transform::MirPass,
103-
util::{dump_mir, PassWhere},
103+
util::{dump_mir, ever_borrowed_locals, PassWhere},
104104
};
105105
use itertools::Itertools;
106106
use rustc_data_structures::unify::{InPlaceUnificationTable, UnifyKey};
@@ -946,68 +946,6 @@ fn is_local_required(local: Local, body: &Body<'_>) -> bool {
946946
}
947947
}
948948

949-
/// Walks MIR to find all locals that have their address taken anywhere.
950-
fn ever_borrowed_locals(body: &Body<'_>) -> BitSet<Local> {
951-
let mut visitor = BorrowCollector { locals: BitSet::new_empty(body.local_decls.len()) };
952-
visitor.visit_body(body);
953-
visitor.locals
954-
}
955-
956-
struct BorrowCollector {
957-
locals: BitSet<Local>,
958-
}
959-
960-
impl<'tcx> Visitor<'tcx> for BorrowCollector {
961-
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
962-
self.super_rvalue(rvalue, location);
963-
964-
match rvalue {
965-
Rvalue::AddressOf(_, borrowed_place) | Rvalue::Ref(_, _, borrowed_place) => {
966-
if !borrowed_place.is_indirect() {
967-
self.locals.insert(borrowed_place.local);
968-
}
969-
}
970-
971-
Rvalue::Cast(..)
972-
| Rvalue::Use(..)
973-
| Rvalue::Repeat(..)
974-
| Rvalue::Len(..)
975-
| Rvalue::BinaryOp(..)
976-
| Rvalue::CheckedBinaryOp(..)
977-
| Rvalue::NullaryOp(..)
978-
| Rvalue::UnaryOp(..)
979-
| Rvalue::Discriminant(..)
980-
| Rvalue::Aggregate(..)
981-
| Rvalue::ThreadLocalRef(..) => {}
982-
}
983-
}
984-
985-
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
986-
self.super_terminator(terminator, location);
987-
988-
match terminator.kind {
989-
TerminatorKind::Drop { place: dropped_place, .. }
990-
| TerminatorKind::DropAndReplace { place: dropped_place, .. } => {
991-
self.locals.insert(dropped_place.local);
992-
}
993-
994-
TerminatorKind::Abort
995-
| TerminatorKind::Assert { .. }
996-
| TerminatorKind::Call { .. }
997-
| TerminatorKind::FalseEdge { .. }
998-
| TerminatorKind::FalseUnwind { .. }
999-
| TerminatorKind::GeneratorDrop
1000-
| TerminatorKind::Goto { .. }
1001-
| TerminatorKind::Resume
1002-
| TerminatorKind::Return
1003-
| TerminatorKind::SwitchInt { .. }
1004-
| TerminatorKind::Unreachable
1005-
| TerminatorKind::Yield { .. }
1006-
| TerminatorKind::InlineAsm { .. } => {}
1007-
}
1008-
}
1009-
}
1010-
1011949
/// `PlaceElem::Index` only stores a `Local`, so we can't replace that with a full `Place`.
1012950
///
1013951
/// Collect locals used as indices so we don't generate candidates that are impossible to apply
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use rustc_index::bit_set::BitSet;
2+
use rustc_middle::mir::visit::Visitor;
3+
use rustc_middle::mir::*;
4+
5+
/// Walks MIR to find all locals that have their address taken anywhere.
6+
pub fn ever_borrowed_locals(body: &Body<'_>) -> BitSet<Local> {
7+
let mut visitor = BorrowCollector { locals: BitSet::new_empty(body.local_decls.len()) };
8+
for (block, data) in body.basic_blocks().iter_enumerated() {
9+
visitor.visit_basic_block_data(block, data);
10+
}
11+
visitor.locals
12+
}
13+
14+
struct BorrowCollector {
15+
locals: BitSet<Local>,
16+
}
17+
18+
impl<'tcx> Visitor<'tcx> for BorrowCollector {
19+
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, _location: Location) {
20+
match rvalue {
21+
Rvalue::AddressOf(_, borrowed_place) | Rvalue::Ref(_, _, borrowed_place) => {
22+
if !borrowed_place.is_indirect() {
23+
self.locals.insert(borrowed_place.local);
24+
}
25+
}
26+
27+
Rvalue::Cast(..)
28+
| Rvalue::Use(..)
29+
| Rvalue::Repeat(..)
30+
| Rvalue::Len(..)
31+
| Rvalue::BinaryOp(..)
32+
| Rvalue::CheckedBinaryOp(..)
33+
| Rvalue::NullaryOp(..)
34+
| Rvalue::UnaryOp(..)
35+
| Rvalue::Discriminant(..)
36+
| Rvalue::Aggregate(..)
37+
| Rvalue::ThreadLocalRef(..) => {}
38+
}
39+
}
40+
41+
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
42+
self.super_terminator(terminator, location);
43+
44+
match terminator.kind {
45+
TerminatorKind::Drop { place: dropped_place, .. }
46+
| TerminatorKind::DropAndReplace { place: dropped_place, .. } => {
47+
self.locals.insert(dropped_place.local);
48+
}
49+
50+
TerminatorKind::Abort
51+
| TerminatorKind::Assert { .. }
52+
| TerminatorKind::Call { .. }
53+
| TerminatorKind::FalseEdge { .. }
54+
| TerminatorKind::FalseUnwind { .. }
55+
| TerminatorKind::GeneratorDrop
56+
| TerminatorKind::Goto { .. }
57+
| TerminatorKind::Resume
58+
| TerminatorKind::Return
59+
| TerminatorKind::SwitchInt { .. }
60+
| TerminatorKind::Unreachable
61+
| TerminatorKind::Yield { .. }
62+
| TerminatorKind::InlineAsm { .. } => {}
63+
}
64+
}
65+
}

compiler/rustc_mir/src/util/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub mod patch;
55
pub mod storage;
66

77
mod alignment;
8+
mod borrowed;
89
pub mod collect_writes;
910
mod find_self_call;
1011
mod generic_graph;
@@ -15,6 +16,7 @@ pub(crate) mod spanview;
1516

1617
pub use self::aggregate::expand_aggregate;
1718
pub use self::alignment::is_disaligned;
19+
pub use self::borrowed::ever_borrowed_locals;
1820
pub use self::find_self_call::find_self_call;
1921
pub use self::generic_graph::graphviz_safe_def_name;
2022
pub use self::graphviz::write_mir_graphviz;

0 commit comments

Comments
 (0)