Skip to content

Commit 0913498

Browse files
committed
optimize un_derefer
1 parent b4c3a2a commit 0913498

File tree

7 files changed

+26
-27
lines changed

7 files changed

+26
-27
lines changed

compiler/rustc_borrowck/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ fn do_mir_borrowck<'a, 'tcx>(
212212

213213
let (move_data, move_errors): (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>) =
214214
match MoveData::gather_moves(&body, tcx, param_env) {
215-
Ok(move_data) => (move_data, Vec::new()),
215+
Ok((_, move_data)) => (move_data, Vec::new()),
216216
Err((move_data, move_errors)) => (move_data, move_errors),
217217
};
218218
let promoted_errors = promoted

compiler/rustc_mir_dataflow/src/move_paths/builder.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::un_derefer::UnDerefer;
2+
use rustc_data_structures::stable_map::FxHashMap;
23
use rustc_index::vec::IndexVec;
34
use rustc_middle::mir::tcx::RvalueInitializationState;
45
use rustc_middle::mir::*;
@@ -209,7 +210,10 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
209210
impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
210211
fn finalize(
211212
self,
212-
) -> Result<MoveData<'tcx>, (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>)> {
213+
) -> Result<
214+
(FxHashMap<rustc_middle::mir::Local, rustc_middle::mir::Place<'tcx>>, MoveData<'tcx>),
215+
(MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>),
216+
> {
213217
debug!("{}", {
214218
debug!("moves for {:?}:", self.body.span);
215219
for (j, mo) in self.data.moves.iter_enumerated() {
@@ -222,15 +226,22 @@ impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
222226
"done dumping moves"
223227
});
224228

225-
if !self.errors.is_empty() { Err((self.data, self.errors)) } else { Ok(self.data) }
229+
if !self.errors.is_empty() {
230+
Err((self.data, self.errors))
231+
} else {
232+
Ok((self.un_derefer.derefer_sidetable.clone(), self.data))
233+
}
226234
}
227235
}
228236

229237
pub(super) fn gather_moves<'tcx>(
230238
body: &Body<'tcx>,
231239
tcx: TyCtxt<'tcx>,
232240
param_env: ty::ParamEnv<'tcx>,
233-
) -> Result<MoveData<'tcx>, (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>)> {
241+
) -> Result<
242+
(FxHashMap<rustc_middle::mir::Local, rustc_middle::mir::Place<'tcx>>, MoveData<'tcx>),
243+
(MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>),
244+
> {
234245
let mut builder = MoveDataBuilder::new(body, tcx, param_env);
235246

236247
builder.gather_args();

compiler/rustc_mir_dataflow/src/move_paths/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,10 @@ impl<'tcx> MoveData<'tcx> {
386386
body: &Body<'tcx>,
387387
tcx: TyCtxt<'tcx>,
388388
param_env: ParamEnv<'tcx>,
389-
) -> Result<Self, (Self, Vec<(Place<'tcx>, MoveError<'tcx>)>)> {
389+
) -> Result<
390+
(FxHashMap<rustc_middle::mir::Local, rustc_middle::mir::Place<'tcx>>, MoveData<'tcx>),
391+
(MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>),
392+
> {
390393
builder::gather_moves(body, tcx, param_env)
391394
}
392395

compiler/rustc_mir_dataflow/src/rustc_peek.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ impl<'tcx> MirPass<'tcx> for SanityCheck {
3030
}
3131

3232
let param_env = tcx.param_env(def_id);
33-
let move_data = MoveData::gather_moves(body, tcx, param_env).unwrap();
34-
let mdpe = MoveDataParamEnv { move_data, param_env };
33+
let (_, move_data) = MoveData::gather_moves(body, tcx, param_env).unwrap();
34+
let mdpe = MoveDataParamEnv { move_data: move_data, param_env };
3535

3636
if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_maybe_init).is_some() {
3737
let flow_inits = MaybeInitializedPlaces::new(tcx, body, &mdpe)

compiler/rustc_mir_dataflow/src/un_derefer.rs

+1-15
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub struct UnDerefer<'tcx> {
99
}
1010

1111
impl<'tcx> UnDerefer<'tcx> {
12+
#[inline]
1213
pub fn derefer(&self, place: PlaceRef<'tcx>, body: &Body<'tcx>) -> Option<Place<'tcx>> {
1314
let reffed = self.derefer_sidetable.get(&place.local)?;
1415

@@ -18,19 +19,4 @@ impl<'tcx> UnDerefer<'tcx> {
1819
}
1920
Some(new_place)
2021
}
21-
22-
pub fn ref_finder(&mut self, body: &Body<'tcx>) {
23-
for (_bb, data) in body.basic_blocks().iter_enumerated() {
24-
for stmt in data.statements.iter() {
25-
match stmt.kind {
26-
StatementKind::Assign(box (place, Rvalue::CopyForDeref(reffed))) => {
27-
if body.local_decls[place.local].is_deref_temp() {
28-
self.derefer_sidetable.insert(place.local, reffed);
29-
}
30-
}
31-
_ => (),
32-
}
33-
}
34-
}
35-
}
3622
}

compiler/rustc_mir_transform/src/elaborate_drops.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,19 @@ impl<'tcx> MirPass<'tcx> for ElaborateDrops {
2828
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
2929
debug!("elaborate_drops({:?} @ {:?})", body.source, body.span);
3030

31-
let mut un_derefer = UnDerefer { tcx: tcx, derefer_sidetable: Default::default() };
32-
un_derefer.ref_finder(body);
3331
let def_id = body.source.def_id();
3432
let param_env = tcx.param_env_reveal_all_normalized(def_id);
35-
let move_data = match MoveData::gather_moves(body, tcx, param_env) {
33+
let (side_table, move_data) = match MoveData::gather_moves(body, tcx, param_env) {
3634
Ok(move_data) => move_data,
3735
Err((move_data, _)) => {
3836
tcx.sess.delay_span_bug(
3937
body.span,
4038
"No `move_errors` should be allowed in MIR borrowck",
4139
);
42-
move_data
40+
(Default::default(), move_data)
4341
}
4442
};
43+
let un_derefer = UnDerefer { tcx: tcx, derefer_sidetable: side_table };
4544
let elaborate_patch = {
4645
let body = &*body;
4746
let env = MoveDataParamEnv { move_data, param_env };

compiler/rustc_mir_transform/src/remove_uninit_drops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub struct RemoveUninitDrops;
2121
impl<'tcx> MirPass<'tcx> for RemoveUninitDrops {
2222
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
2323
let param_env = tcx.param_env(body.source.def_id());
24-
let Ok(move_data) = MoveData::gather_moves(body, tcx, param_env) else {
24+
let Ok((_,move_data)) = MoveData::gather_moves(body, tcx, param_env) else {
2525
// We could continue if there are move errors, but there's not much point since our
2626
// init data isn't complete.
2727
return;

0 commit comments

Comments
 (0)