Skip to content

Commit 7023402

Browse files
committed
Remove references from some structs.
In all cases the struct can own the relevant thing instead of having a reference to it. This makes the code simpler, and in some cases removes a struct lifetime.
1 parent d1c55a3 commit 7023402

File tree

7 files changed

+56
-64
lines changed

7 files changed

+56
-64
lines changed

compiler/rustc_mir_transform/src/dest_prop.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ impl<'tcx> crate::MirPass<'tcx> for DestinationPropagation {
242242
}
243243
round_count += 1;
244244

245-
apply_merges(body, tcx, &merges, &merged_locals);
245+
apply_merges(body, tcx, merges, merged_locals);
246246
}
247247

248248
trace!(round_count);
@@ -281,20 +281,20 @@ struct Candidates {
281281
fn apply_merges<'tcx>(
282282
body: &mut Body<'tcx>,
283283
tcx: TyCtxt<'tcx>,
284-
merges: &FxIndexMap<Local, Local>,
285-
merged_locals: &BitSet<Local>,
284+
merges: FxIndexMap<Local, Local>,
285+
merged_locals: BitSet<Local>,
286286
) {
287287
let mut merger = Merger { tcx, merges, merged_locals };
288288
merger.visit_body_preserves_cfg(body);
289289
}
290290

291-
struct Merger<'a, 'tcx> {
291+
struct Merger<'tcx> {
292292
tcx: TyCtxt<'tcx>,
293-
merges: &'a FxIndexMap<Local, Local>,
294-
merged_locals: &'a BitSet<Local>,
293+
merges: FxIndexMap<Local, Local>,
294+
merged_locals: BitSet<Local>,
295295
}
296296

297-
impl<'a, 'tcx> MutVisitor<'tcx> for Merger<'a, 'tcx> {
297+
impl<'tcx> MutVisitor<'tcx> for Merger<'tcx> {
298298
fn tcx(&self) -> TyCtxt<'tcx> {
299299
self.tcx
300300
}

compiler/rustc_mir_transform/src/gvn.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl<'tcx> crate::MirPass<'tcx> for GVN {
125125
// Clone dominators because we need them while mutating the body.
126126
let dominators = body.basic_blocks.dominators().clone();
127127

128-
let mut state = VnState::new(tcx, body, param_env, &ssa, &dominators, &body.local_decls);
128+
let mut state = VnState::new(tcx, body, param_env, &ssa, dominators, &body.local_decls);
129129
ssa.for_each_assignment_mut(
130130
body.basic_blocks.as_mut_preserves_cfg(),
131131
|local, value, location| {
@@ -258,7 +258,7 @@ struct VnState<'body, 'tcx> {
258258
/// Cache the value of the `unsized_locals` features, to avoid fetching it repeatedly in a loop.
259259
feature_unsized_locals: bool,
260260
ssa: &'body SsaLocals,
261-
dominators: &'body Dominators<BasicBlock>,
261+
dominators: Dominators<BasicBlock>,
262262
reused_locals: BitSet<Local>,
263263
}
264264

@@ -268,7 +268,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
268268
body: &Body<'tcx>,
269269
param_env: ty::ParamEnv<'tcx>,
270270
ssa: &'body SsaLocals,
271-
dominators: &'body Dominators<BasicBlock>,
271+
dominators: Dominators<BasicBlock>,
272272
local_decls: &'body LocalDecls<'tcx>,
273273
) -> Self {
274274
// Compute a rough estimate of the number of values in the body from the number of
@@ -1490,7 +1490,7 @@ impl<'tcx> VnState<'_, 'tcx> {
14901490
let other = self.rev_locals.get(index)?;
14911491
other
14921492
.iter()
1493-
.find(|&&other| self.ssa.assignment_dominates(self.dominators, other, loc))
1493+
.find(|&&other| self.ssa.assignment_dominates(&self.dominators, other, loc))
14941494
.copied()
14951495
}
14961496
}

compiler/rustc_mir_transform/src/jump_threading.rs

+20-22
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,16 @@ impl<'tcx> crate::MirPass<'tcx> for JumpThreading {
7878
}
7979

8080
let param_env = tcx.param_env_reveal_all_normalized(def_id);
81-
let map = Map::new(tcx, body, Some(MAX_PLACES));
82-
let loop_headers = loop_headers(body);
8381

84-
let arena = DroplessArena::default();
82+
let arena = &DroplessArena::default();
8583
let mut finder = TOFinder {
8684
tcx,
8785
param_env,
8886
ecx: InterpCx::new(tcx, DUMMY_SP, param_env, DummyMachine),
8987
body,
90-
arena: &arena,
91-
map: &map,
92-
loop_headers: &loop_headers,
88+
arena,
89+
map: Map::new(tcx, body, Some(MAX_PLACES)),
90+
loop_headers: loop_headers(body),
9391
opportunities: Vec::new(),
9492
};
9593

@@ -105,7 +103,7 @@ impl<'tcx> crate::MirPass<'tcx> for JumpThreading {
105103

106104
// Verify that we do not thread through a loop header.
107105
for to in opportunities.iter() {
108-
assert!(to.chain.iter().all(|&block| !loop_headers.contains(block)));
106+
assert!(to.chain.iter().all(|&block| !finder.loop_headers.contains(block)));
109107
}
110108
OpportunitySet::new(body, opportunities).apply(body);
111109
}
@@ -124,8 +122,8 @@ struct TOFinder<'tcx, 'a> {
124122
param_env: ty::ParamEnv<'tcx>,
125123
ecx: InterpCx<'tcx, DummyMachine>,
126124
body: &'a Body<'tcx>,
127-
map: &'a Map<'tcx>,
128-
loop_headers: &'a BitSet<BasicBlock>,
125+
map: Map<'tcx>,
126+
loop_headers: BitSet<BasicBlock>,
129127
/// We use an arena to avoid cloning the slices when cloning `state`.
130128
arena: &'a DroplessArena,
131129
opportunities: Vec<ThreadingOpportunity>,
@@ -223,7 +221,7 @@ impl<'tcx, 'a> TOFinder<'tcx, 'a> {
223221
}))
224222
};
225223
let conds = ConditionSet(conds);
226-
state.insert_value_idx(discr, conds, self.map);
224+
state.insert_value_idx(discr, conds, &self.map);
227225

228226
self.find_opportunity(bb, state, cost, 0);
229227
}
@@ -264,7 +262,7 @@ impl<'tcx, 'a> TOFinder<'tcx, 'a> {
264262
// _1 = 5 // Whatever happens here, it won't change the result of a `SwitchInt`.
265263
// _1 = 6
266264
if let Some((lhs, tail)) = self.mutated_statement(stmt) {
267-
state.flood_with_tail_elem(lhs.as_ref(), tail, self.map, ConditionSet::BOTTOM);
265+
state.flood_with_tail_elem(lhs.as_ref(), tail, &self.map, ConditionSet::BOTTOM);
268266
}
269267
}
270268

@@ -370,7 +368,7 @@ impl<'tcx, 'a> TOFinder<'tcx, 'a> {
370368
self.opportunities.push(ThreadingOpportunity { chain: vec![bb], target: c.target })
371369
};
372370

373-
if let Some(conditions) = state.try_get_idx(lhs, self.map)
371+
if let Some(conditions) = state.try_get_idx(lhs, &self.map)
374372
&& let Immediate::Scalar(Scalar::Int(int)) = *rhs
375373
{
376374
conditions.iter_matches(int).for_each(register_opportunity);
@@ -406,7 +404,7 @@ impl<'tcx, 'a> TOFinder<'tcx, 'a> {
406404
}
407405
},
408406
&mut |place, op| {
409-
if let Some(conditions) = state.try_get_idx(place, self.map)
407+
if let Some(conditions) = state.try_get_idx(place, &self.map)
410408
&& let Ok(imm) = self.ecx.read_immediate_raw(op)
411409
&& let Some(imm) = imm.right()
412410
&& let Immediate::Scalar(Scalar::Int(int)) = *imm
@@ -441,7 +439,7 @@ impl<'tcx, 'a> TOFinder<'tcx, 'a> {
441439
// Transfer the conditions on the copied rhs.
442440
Operand::Move(rhs) | Operand::Copy(rhs) => {
443441
let Some(rhs) = self.map.find(rhs.as_ref()) else { return };
444-
state.insert_place_idx(rhs, lhs, self.map);
442+
state.insert_place_idx(rhs, lhs, &self.map);
445443
}
446444
}
447445
}
@@ -461,7 +459,7 @@ impl<'tcx, 'a> TOFinder<'tcx, 'a> {
461459
Rvalue::CopyForDeref(rhs) => self.process_operand(bb, lhs, &Operand::Copy(*rhs), state),
462460
Rvalue::Discriminant(rhs) => {
463461
let Some(rhs) = self.map.find_discr(rhs.as_ref()) else { return };
464-
state.insert_place_idx(rhs, lhs, self.map);
462+
state.insert_place_idx(rhs, lhs, &self.map);
465463
}
466464
// If we expect `lhs ?= A`, we have an opportunity if we assume `constant == A`.
467465
Rvalue::Aggregate(box ref kind, ref operands) => {
@@ -492,10 +490,10 @@ impl<'tcx, 'a> TOFinder<'tcx, 'a> {
492490
}
493491
// Transfer the conditions on the copy rhs, after inversing polarity.
494492
Rvalue::UnaryOp(UnOp::Not, Operand::Move(place) | Operand::Copy(place)) => {
495-
let Some(conditions) = state.try_get_idx(lhs, self.map) else { return };
493+
let Some(conditions) = state.try_get_idx(lhs, &self.map) else { return };
496494
let Some(place) = self.map.find(place.as_ref()) else { return };
497495
let conds = conditions.map(self.arena, Condition::inv);
498-
state.insert_value_idx(place, conds, self.map);
496+
state.insert_value_idx(place, conds, &self.map);
499497
}
500498
// We expect `lhs ?= A`. We found `lhs = Eq(rhs, B)`.
501499
// Create a condition on `rhs ?= B`.
@@ -504,7 +502,7 @@ impl<'tcx, 'a> TOFinder<'tcx, 'a> {
504502
box (Operand::Move(place) | Operand::Copy(place), Operand::Constant(value))
505503
| box (Operand::Constant(value), Operand::Move(place) | Operand::Copy(place)),
506504
) => {
507-
let Some(conditions) = state.try_get_idx(lhs, self.map) else { return };
505+
let Some(conditions) = state.try_get_idx(lhs, &self.map) else { return };
508506
let Some(place) = self.map.find(place.as_ref()) else { return };
509507
let equals = match op {
510508
BinOp::Eq => ScalarInt::TRUE,
@@ -528,7 +526,7 @@ impl<'tcx, 'a> TOFinder<'tcx, 'a> {
528526
polarity: if c.matches(equals) { Polarity::Eq } else { Polarity::Ne },
529527
..c
530528
});
531-
state.insert_value_idx(place, conds, self.map);
529+
state.insert_value_idx(place, conds, &self.map);
532530
}
533531

534532
_ => {}
@@ -583,7 +581,7 @@ impl<'tcx, 'a> TOFinder<'tcx, 'a> {
583581
StatementKind::Intrinsic(box NonDivergingIntrinsic::Assume(
584582
Operand::Copy(place) | Operand::Move(place),
585583
)) => {
586-
let Some(conditions) = state.try_get(place.as_ref(), self.map) else { return };
584+
let Some(conditions) = state.try_get(place.as_ref(), &self.map) else { return };
587585
conditions.iter_matches(ScalarInt::TRUE).for_each(register_opportunity);
588586
}
589587
StatementKind::Assign(box (lhs_place, rhs)) => {
@@ -631,7 +629,7 @@ impl<'tcx, 'a> TOFinder<'tcx, 'a> {
631629
// We can recurse through this terminator.
632630
let mut state = state();
633631
if let Some(place_to_flood) = place_to_flood {
634-
state.flood_with(place_to_flood.as_ref(), self.map, ConditionSet::BOTTOM);
632+
state.flood_with(place_to_flood.as_ref(), &self.map, ConditionSet::BOTTOM);
635633
}
636634
self.find_opportunity(bb, state, cost.clone(), depth + 1);
637635
}
@@ -650,7 +648,7 @@ impl<'tcx, 'a> TOFinder<'tcx, 'a> {
650648
let Some(discr) = discr.place() else { return };
651649
let discr_ty = discr.ty(self.body, self.tcx).ty;
652650
let Ok(discr_layout) = self.ecx.layout_of(discr_ty) else { return };
653-
let Some(conditions) = state.try_get(discr.as_ref(), self.map) else { return };
651+
let Some(conditions) = state.try_get(discr.as_ref(), &self.map) else { return };
654652

655653
if let Some((value, _)) = targets.iter().find(|&(_, target)| target == target_bb) {
656654
let Some(value) = ScalarInt::try_from_uint(value, discr_layout.size) else { return };

compiler/rustc_mir_transform/src/lib.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -223,24 +223,26 @@ fn is_mir_available(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
223223
/// MIR associated with them.
224224
fn mir_keys(tcx: TyCtxt<'_>, (): ()) -> FxIndexSet<LocalDefId> {
225225
// All body-owners have MIR associated with them.
226-
let mut set: FxIndexSet<_> = tcx.hir().body_owners().collect();
226+
let set: FxIndexSet<_> = tcx.hir().body_owners().collect();
227227

228228
// Additionally, tuple struct/variant constructors have MIR, but
229229
// they don't have a BodyId, so we need to build them separately.
230-
struct GatherCtors<'a> {
231-
set: &'a mut FxIndexSet<LocalDefId>,
230+
struct GatherCtors {
231+
set: FxIndexSet<LocalDefId>,
232232
}
233-
impl<'tcx> Visitor<'tcx> for GatherCtors<'_> {
233+
impl<'tcx> Visitor<'tcx> for GatherCtors {
234234
fn visit_variant_data(&mut self, v: &'tcx hir::VariantData<'tcx>) {
235235
if let hir::VariantData::Tuple(_, _, def_id) = *v {
236236
self.set.insert(def_id);
237237
}
238238
intravisit::walk_struct_def(self, v)
239239
}
240240
}
241-
tcx.hir().visit_all_item_likes_in_crate(&mut GatherCtors { set: &mut set });
242241

243-
set
242+
let mut gather_ctors = GatherCtors { set };
243+
tcx.hir().visit_all_item_likes_in_crate(&mut gather_ctors);
244+
245+
gather_ctors.set
244246
}
245247

246248
fn mir_const_qualif(tcx: TyCtxt<'_>, def: LocalDefId) -> ConstQualifs {

compiler/rustc_mir_transform/src/mentioned_items.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub(super) struct MentionedItems;
1010
struct MentionedItemsVisitor<'a, 'tcx> {
1111
tcx: TyCtxt<'tcx>,
1212
body: &'a mir::Body<'tcx>,
13-
mentioned_items: &'a mut Vec<Spanned<MentionedItem<'tcx>>>,
13+
mentioned_items: Vec<Spanned<MentionedItem<'tcx>>>,
1414
}
1515

1616
impl<'tcx> crate::MirPass<'tcx> for MentionedItems {
@@ -23,9 +23,9 @@ impl<'tcx> crate::MirPass<'tcx> for MentionedItems {
2323
}
2424

2525
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut mir::Body<'tcx>) {
26-
let mut mentioned_items = Vec::new();
27-
MentionedItemsVisitor { tcx, body, mentioned_items: &mut mentioned_items }.visit_body(body);
28-
body.set_mentioned_items(mentioned_items);
26+
let mut visitor = MentionedItemsVisitor { tcx, body, mentioned_items: Vec::new() };
27+
visitor.visit_body(body);
28+
body.set_mentioned_items(visitor.mentioned_items);
2929
}
3030
}
3131

compiler/rustc_mir_transform/src/ref_prop.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,8 @@ fn compute_replacement<'tcx>(
253253

254254
debug!(?targets);
255255

256-
let mut finder = ReplacementFinder {
257-
targets: &mut targets,
258-
can_perform_opt,
259-
allowed_replacements: FxHashSet::default(),
260-
};
256+
let mut finder =
257+
ReplacementFinder { targets, can_perform_opt, allowed_replacements: FxHashSet::default() };
261258
let reachable_blocks = traversal::reachable_as_bitset(body);
262259
for (bb, bbdata) in body.basic_blocks.iter_enumerated() {
263260
// Only visit reachable blocks as we rely on dataflow.
@@ -269,19 +266,19 @@ fn compute_replacement<'tcx>(
269266
let allowed_replacements = finder.allowed_replacements;
270267
return Replacer {
271268
tcx,
272-
targets,
269+
targets: finder.targets,
273270
storage_to_remove,
274271
allowed_replacements,
275272
any_replacement: false,
276273
};
277274

278-
struct ReplacementFinder<'a, 'tcx, F> {
279-
targets: &'a mut IndexVec<Local, Value<'tcx>>,
275+
struct ReplacementFinder<'tcx, F> {
276+
targets: IndexVec<Local, Value<'tcx>>,
280277
can_perform_opt: F,
281278
allowed_replacements: FxHashSet<(Local, Location)>,
282279
}
283280

284-
impl<'tcx, F> Visitor<'tcx> for ReplacementFinder<'_, 'tcx, F>
281+
impl<'tcx, F> Visitor<'tcx> for ReplacementFinder<'tcx, F>
285282
where
286283
F: FnMut(Place<'tcx>, Location) -> bool,
287284
{

compiler/rustc_mir_transform/src/required_consts.rs

+7-12
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
use rustc_middle::mir::visit::Visitor;
22
use rustc_middle::mir::{traversal, Body, ConstOperand, Location};
33

4-
pub(super) struct RequiredConstsVisitor<'a, 'tcx> {
5-
required_consts: &'a mut Vec<ConstOperand<'tcx>>,
4+
pub(super) struct RequiredConstsVisitor<'tcx> {
5+
required_consts: Vec<ConstOperand<'tcx>>,
66
}
77

8-
impl<'a, 'tcx> RequiredConstsVisitor<'a, 'tcx> {
9-
fn new(required_consts: &'a mut Vec<ConstOperand<'tcx>>) -> Self {
10-
RequiredConstsVisitor { required_consts }
11-
}
12-
8+
impl<'tcx> RequiredConstsVisitor<'tcx> {
139
pub(super) fn compute_required_consts(body: &mut Body<'tcx>) {
14-
let mut required_consts = Vec::new();
15-
let mut required_consts_visitor = RequiredConstsVisitor::new(&mut required_consts);
10+
let mut visitor = RequiredConstsVisitor { required_consts: Vec::new() };
1611
for (bb, bb_data) in traversal::reverse_postorder(&body) {
17-
required_consts_visitor.visit_basic_block_data(bb, bb_data);
12+
visitor.visit_basic_block_data(bb, bb_data);
1813
}
19-
body.set_required_consts(required_consts);
14+
body.set_required_consts(visitor.required_consts);
2015
}
2116
}
2217

23-
impl<'tcx> Visitor<'tcx> for RequiredConstsVisitor<'_, 'tcx> {
18+
impl<'tcx> Visitor<'tcx> for RequiredConstsVisitor<'tcx> {
2419
fn visit_const_operand(&mut self, constant: &ConstOperand<'tcx>, _: Location) {
2520
if constant.const_.is_required_const() {
2621
self.required_consts.push(*constant);

0 commit comments

Comments
 (0)