Skip to content

Commit 48064d4

Browse files
committed
Inline and remove some functions.
These are all functions with a single callsite, where having a separate function does nothing to help with readability. These changes make the code a little shorter and easier to read.
1 parent 8235af0 commit 48064d4

10 files changed

+344
-395
lines changed

compiler/rustc_mir_transform/src/add_call_guards.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ pub(super) use self::AddCallGuards::*;
3232

3333
impl<'tcx> crate::MirPass<'tcx> for AddCallGuards {
3434
fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
35-
self.add_call_guards(body);
36-
}
37-
}
38-
39-
impl AddCallGuards {
40-
pub(super) fn add_call_guards(&self, body: &mut Body<'_>) {
4135
let mut pred_count: IndexVec<_, _> =
4236
body.basic_blocks.predecessors().iter().map(|ps| ps.len()).collect();
4337
pred_count[START_BLOCK] += 1;

compiler/rustc_mir_transform/src/add_moves_for_packed_drops.rs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,35 +40,34 @@ pub(super) struct AddMovesForPackedDrops;
4040
impl<'tcx> crate::MirPass<'tcx> for AddMovesForPackedDrops {
4141
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
4242
debug!("add_moves_for_packed_drops({:?} @ {:?})", body.source, body.span);
43-
add_moves_for_packed_drops(tcx, body);
44-
}
45-
}
46-
47-
fn add_moves_for_packed_drops<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
48-
let patch = add_moves_for_packed_drops_patch(tcx, body);
49-
patch.apply(body);
50-
}
5143

52-
fn add_moves_for_packed_drops_patch<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) -> MirPatch<'tcx> {
53-
let def_id = body.source.def_id();
54-
let mut patch = MirPatch::new(body);
55-
let param_env = tcx.param_env(def_id);
44+
let def_id = body.source.def_id();
45+
let mut patch = MirPatch::new(body);
46+
let param_env = tcx.param_env(def_id);
5647

57-
for (bb, data) in body.basic_blocks.iter_enumerated() {
58-
let loc = Location { block: bb, statement_index: data.statements.len() };
59-
let terminator = data.terminator();
48+
for (bb, data) in body.basic_blocks.iter_enumerated() {
49+
let loc = Location { block: bb, statement_index: data.statements.len() };
50+
let terminator = data.terminator();
6051

61-
match terminator.kind {
62-
TerminatorKind::Drop { place, .. }
63-
if util::is_disaligned(tcx, body, param_env, place) =>
64-
{
65-
add_move_for_packed_drop(tcx, body, &mut patch, terminator, loc, data.is_cleanup);
52+
match terminator.kind {
53+
TerminatorKind::Drop { place, .. }
54+
if util::is_disaligned(tcx, body, param_env, place) =>
55+
{
56+
add_move_for_packed_drop(
57+
tcx,
58+
body,
59+
&mut patch,
60+
terminator,
61+
loc,
62+
data.is_cleanup,
63+
);
64+
}
65+
_ => {}
6666
}
67-
_ => {}
6867
}
69-
}
7068

71-
patch
69+
patch.apply(body);
70+
}
7271
}
7372

7473
fn add_move_for_packed_drop<'tcx>(

compiler/rustc_mir_transform/src/add_subtyping_projections.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,14 @@ impl<'a, 'tcx> MutVisitor<'tcx> for SubTypeChecker<'a, 'tcx> {
5151
// // gets transformed to
5252
// let temp: rval_ty = rval;
5353
// let place: place_ty = temp as place_ty;
54-
fn subtype_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
55-
let patch = MirPatch::new(body);
56-
let mut checker = SubTypeChecker { tcx, patcher: patch, local_decls: &body.local_decls };
57-
58-
for (bb, data) in body.basic_blocks.as_mut_preserves_cfg().iter_enumerated_mut() {
59-
checker.visit_basic_block_data(bb, data);
60-
}
61-
checker.patcher.apply(body);
62-
}
63-
6454
impl<'tcx> crate::MirPass<'tcx> for Subtyper {
6555
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
66-
subtype_finder(tcx, body);
56+
let patch = MirPatch::new(body);
57+
let mut checker = SubTypeChecker { tcx, patcher: patch, local_decls: &body.local_decls };
58+
59+
for (bb, data) in body.basic_blocks.as_mut_preserves_cfg().iter_enumerated_mut() {
60+
checker.visit_basic_block_data(bb, data);
61+
}
62+
checker.patcher.apply(body);
6763
}
6864
}

compiler/rustc_mir_transform/src/copy_prop.rs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,34 @@ impl<'tcx> crate::MirPass<'tcx> for CopyProp {
2727
#[instrument(level = "trace", skip(self, tcx, body))]
2828
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
2929
debug!(def_id = ?body.source.def_id());
30-
propagate_ssa(tcx, body);
31-
}
32-
}
3330

34-
fn propagate_ssa<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
35-
let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
36-
let ssa = SsaLocals::new(tcx, body, param_env);
31+
let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
32+
let ssa = SsaLocals::new(tcx, body, param_env);
3733

38-
let fully_moved = fully_moved_locals(&ssa, body);
39-
debug!(?fully_moved);
34+
let fully_moved = fully_moved_locals(&ssa, body);
35+
debug!(?fully_moved);
4036

41-
let mut storage_to_remove = BitSet::new_empty(fully_moved.domain_size());
42-
for (local, &head) in ssa.copy_classes().iter_enumerated() {
43-
if local != head {
44-
storage_to_remove.insert(head);
37+
let mut storage_to_remove = BitSet::new_empty(fully_moved.domain_size());
38+
for (local, &head) in ssa.copy_classes().iter_enumerated() {
39+
if local != head {
40+
storage_to_remove.insert(head);
41+
}
4542
}
46-
}
4743

48-
let any_replacement = ssa.copy_classes().iter_enumerated().any(|(l, &h)| l != h);
44+
let any_replacement = ssa.copy_classes().iter_enumerated().any(|(l, &h)| l != h);
4945

50-
Replacer {
51-
tcx,
52-
copy_classes: ssa.copy_classes(),
53-
fully_moved,
54-
borrowed_locals: ssa.borrowed_locals(),
55-
storage_to_remove,
56-
}
57-
.visit_body_preserves_cfg(body);
46+
Replacer {
47+
tcx,
48+
copy_classes: ssa.copy_classes(),
49+
fully_moved,
50+
borrowed_locals: ssa.borrowed_locals(),
51+
storage_to_remove,
52+
}
53+
.visit_body_preserves_cfg(body);
5854

59-
if any_replacement {
60-
crate::simplify::remove_unused_definitions(body);
55+
if any_replacement {
56+
crate::simplify::remove_unused_definitions(body);
57+
}
6158
}
6259
}
6360

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -119,53 +119,50 @@ impl<'tcx> crate::MirPass<'tcx> for GVN {
119119
#[instrument(level = "trace", skip(self, tcx, body))]
120120
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
121121
debug!(def_id = ?body.source.def_id());
122-
propagate_ssa(tcx, body);
123-
}
124-
}
125122

126-
fn propagate_ssa<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
127-
let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
128-
let ssa = SsaLocals::new(tcx, body, param_env);
129-
// Clone dominators as we need them while mutating the body.
130-
let dominators = body.basic_blocks.dominators().clone();
131-
132-
let mut state = VnState::new(tcx, body, param_env, &ssa, &dominators, &body.local_decls);
133-
ssa.for_each_assignment_mut(
134-
body.basic_blocks.as_mut_preserves_cfg(),
135-
|local, value, location| {
136-
let value = match value {
137-
// We do not know anything of this assigned value.
138-
AssignedValue::Arg | AssignedValue::Terminator => None,
139-
// Try to get some insight.
140-
AssignedValue::Rvalue(rvalue) => {
141-
let value = state.simplify_rvalue(rvalue, location);
142-
// FIXME(#112651) `rvalue` may have a subtype to `local`. We can only mark
143-
// `local` as reusable if we have an exact type match.
144-
if state.local_decls[local].ty != rvalue.ty(state.local_decls, tcx) {
145-
return;
123+
let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
124+
let ssa = SsaLocals::new(tcx, body, param_env);
125+
// Clone dominators as we need them while mutating the body.
126+
let dominators = body.basic_blocks.dominators().clone();
127+
128+
let mut state = VnState::new(tcx, body, param_env, &ssa, &dominators, &body.local_decls);
129+
ssa.for_each_assignment_mut(
130+
body.basic_blocks.as_mut_preserves_cfg(),
131+
|local, value, location| {
132+
let value = match value {
133+
// We do not know anything of this assigned value.
134+
AssignedValue::Arg | AssignedValue::Terminator => None,
135+
// Try to get some insight.
136+
AssignedValue::Rvalue(rvalue) => {
137+
let value = state.simplify_rvalue(rvalue, location);
138+
// FIXME(#112651) `rvalue` may have a subtype to `local`. We can only mark
139+
// `local` as reusable if we have an exact type match.
140+
if state.local_decls[local].ty != rvalue.ty(state.local_decls, tcx) {
141+
return;
142+
}
143+
value
146144
}
147-
value
148-
}
149-
};
150-
// `next_opaque` is `Some`, so `new_opaque` must return `Some`.
151-
let value = value.or_else(|| state.new_opaque()).unwrap();
152-
state.assign(local, value);
153-
},
154-
);
155-
156-
// Stop creating opaques during replacement as it is useless.
157-
state.next_opaque = None;
158-
159-
let reverse_postorder = body.basic_blocks.reverse_postorder().to_vec();
160-
for bb in reverse_postorder {
161-
let data = &mut body.basic_blocks.as_mut_preserves_cfg()[bb];
162-
state.visit_basic_block_data(bb, data);
163-
}
145+
};
146+
// `next_opaque` is `Some`, so `new_opaque` must return `Some`.
147+
let value = value.or_else(|| state.new_opaque()).unwrap();
148+
state.assign(local, value);
149+
},
150+
);
151+
152+
// Stop creating opaques during replacement as it is useless.
153+
state.next_opaque = None;
164154

165-
// For each local that is reused (`y` above), we remove its storage statements do avoid any
166-
// difficulty. Those locals are SSA, so should be easy to optimize by LLVM without storage
167-
// statements.
168-
StorageRemover { tcx, reused_locals: state.reused_locals }.visit_body_preserves_cfg(body);
155+
let reverse_postorder = body.basic_blocks.reverse_postorder().to_vec();
156+
for bb in reverse_postorder {
157+
let data = &mut body.basic_blocks.as_mut_preserves_cfg()[bb];
158+
state.visit_basic_block_data(bb, data);
159+
}
160+
161+
// For each local that is reused (`y` above), we remove its storage statements do avoid any
162+
// difficulty. Those locals are SSA, so should be easy to optimize by LLVM without storage
163+
// statements.
164+
StorageRemover { tcx, reused_locals: state.reused_locals }.visit_body_preserves_cfg(body);
165+
}
169166
}
170167

171168
newtype_index! {

compiler/rustc_mir_transform/src/instsimplify.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,13 @@ pub(super) enum InstSimplify {
1818
AfterSimplifyCfg,
1919
}
2020

21-
impl InstSimplify {
21+
impl<'tcx> crate::MirPass<'tcx> for InstSimplify {
2222
fn name(&self) -> &'static str {
2323
match self {
2424
InstSimplify::BeforeInline => "InstSimplify-before-inline",
2525
InstSimplify::AfterSimplifyCfg => "InstSimplify-after-simplifycfg",
2626
}
2727
}
28-
}
29-
30-
impl<'tcx> crate::MirPass<'tcx> for InstSimplify {
31-
fn name(&self) -> &'static str {
32-
self.name()
33-
}
3428

3529
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
3630
sess.mir_opt_level() > 0

0 commit comments

Comments
 (0)