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

7 files changed

Lines changed: 56 additions & 64 deletions

File tree

‎compiler/rustc_mir_transform/src/dest_prop.rs‎

Lines changed: 7 additions & 7 deletions
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
fnapply_merges<'tcx>(
282282
body:&mutBody<'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
letmut merger = Merger{ tcx, merges, merged_locals };
288288
merger.visit_body_preserves_cfg(body);
289289
}
290290

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

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

‎compiler/rustc_mir_transform/src/gvn.rs‎

Lines changed: 4 additions & 4 deletions
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-
letmut state = VnState::new(tcx, body, param_env,&ssa,&dominators,&body.local_decls);
128+
letmut 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:&'bodySsaLocals,
261-
dominators:&'bodyDominators<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:&'bodySsaLocals,
271-
dominators:&'bodyDominators<BasicBlock>,
271+
dominators:Dominators<BasicBlock>,
272272
local_decls:&'bodyLocalDecls<'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‎

Lines changed: 20 additions & 22 deletions
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
letmut 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:&'aBody<'tcx>,
127-
map:&'aMap<'tcx>,
128-
loop_headers:&'aBitSet<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:&'aDroplessArena,
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
ifletSome((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-
ifletSome(conditions) = state.try_get_idx(lhs,self.map)
371+
ifletSome(conditions) = state.try_get_idx(lhs,&self.map)
374372
&& letImmediate::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-
ifletSome(conditions) = state.try_get_idx(place,self.map)
407+
ifletSome(conditions) = state.try_get_idx(place,&self.map)
410408
&& letOk(imm) = self.ecx.read_immediate_raw(op)
411409
&& letSome(imm) = imm.right()
412410
&& letImmediate::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
letSome(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
letSome(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-
letSome(conditions) = state.try_get_idx(lhs,self.map)else{return};
493+
letSome(conditions) = state.try_get_idx(lhs,&self.map)else{return};
496494
letSome(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-
letSome(conditions) = state.try_get_idx(lhs,self.map)else{return};
505+
letSome(conditions) = state.try_get_idx(lhs,&self.map)else{return};
508506
letSome(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-
letSome(conditions) = state.try_get(place.as_ref(),self.map)else{return};
584+
letSome(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
letmut state = state();
633631
ifletSome(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
letSome(discr) = discr.place()else{return};
651649
let discr_ty = discr.ty(self.body,self.tcx).ty;
652650
letOk(discr_layout) = self.ecx.layout_of(discr_ty)else{return};
653-
letSome(conditions) = state.try_get(discr.as_ref(),self.map)else{return};
651+
letSome(conditions) = state.try_get(discr.as_ref(),&self.map)else{return};
654652

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

‎compiler/rustc_mir_transform/src/lib.rs‎

Lines changed: 8 additions & 6 deletions
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
fnmir_keys(tcx:TyCtxt<'_>,():()) -> FxIndexSet<LocalDefId>{
225225
// All body-owners have MIR associated with them.
226-
letmutset: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-
structGatherCtors<'a>{
231-
set:&'amutFxIndexSet<LocalDefId>,
230+
structGatherCtors{
231+
set:FxIndexSet<LocalDefId>,
232232
}
233-
impl<'tcx>Visitor<'tcx>forGatherCtors<'_>{
233+
impl<'tcx>Visitor<'tcx>forGatherCtors{
234234
fnvisit_variant_data(&mutself,v:&'tcx hir::VariantData<'tcx>){
235235
iflet 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(&mutGatherCtors{set:&mut set });
242241

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

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

‎compiler/rustc_mir_transform/src/mentioned_items.rs‎

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

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

2525
fnrun_pass(&self,tcx:TyCtxt<'tcx>,body:&mut mir::Body<'tcx>){
26-
letmutmentioned_items = Vec::new();
27-
MentionedItemsVisitor{ tcx, body,mentioned_items:&mut mentioned_items }.visit_body(body);
28-
body.set_mentioned_items(mentioned_items);
26+
letmutvisitor = 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‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,8 @@ fn compute_replacement<'tcx>(
253253

254254
debug!(?targets);
255255

256-
letmut finder = ReplacementFinder{
257-
targets:&mut targets,
258-
can_perform_opt,
259-
allowed_replacements:FxHashSet::default(),
260-
};
256+
letmut 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
returnReplacer{
271268
tcx,
272-
targets,
269+
targets: finder.targets,
273270
storage_to_remove,
274271
allowed_replacements,
275272
any_replacement:false,
276273
};
277274

278-
structReplacementFinder<'a,'tcx,F>{
279-
targets:&'amutIndexVec<Local,Value<'tcx>>,
275+
structReplacementFinder<'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>forReplacementFinder<'_,'tcx,F>
281+
impl<'tcx,F>Visitor<'tcx>forReplacementFinder<'tcx,F>
285282
where
286283
F:FnMut(Place<'tcx>,Location) -> bool,
287284
{

‎compiler/rustc_mir_transform/src/required_consts.rs‎

Lines changed: 7 additions & 12 deletions
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)structRequiredConstsVisitor<'a,'tcx>{
5-
required_consts:&'amutVec<ConstOperand<'tcx>>,
4+
pub(super)structRequiredConstsVisitor<'tcx>{
5+
required_consts:Vec<ConstOperand<'tcx>>,
66
}
77

8-
impl<'a,'tcx>RequiredConstsVisitor<'a,'tcx>{
9-
fnnew(required_consts:&'amutVec<ConstOperand<'tcx>>) -> Self{
10-
RequiredConstsVisitor{ required_consts }
11-
}
12-
8+
impl<'tcx>RequiredConstsVisitor<'tcx>{
139
pub(super)fncompute_required_consts(body:&mutBody<'tcx>){
14-
letmut required_consts = Vec::new();
15-
letmut required_consts_visitor = RequiredConstsVisitor::new(&mut required_consts);
10+
letmut 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>forRequiredConstsVisitor<'_,'tcx>{
18+
impl<'tcx>Visitor<'tcx>forRequiredConstsVisitor<'tcx>{
2419
fnvisit_const_operand(&mutself,constant:&ConstOperand<'tcx>, _:Location){
2520
if constant.const_.is_required_const(){
2621
self.required_consts.push(*constant);

0 commit comments

Comments
 (0)