Skip to content

Commit 438aeb8

Browse files
committed
Avoid cloning Place in codegen_place
1 parent e11adb1 commit 438aeb8

File tree

6 files changed

+51
-40
lines changed

6 files changed

+51
-40
lines changed

src/librustc/mir/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,6 +1954,15 @@ impl From<Local> for PlaceBase<'_> {
19541954
}
19551955
}
19561956

1957+
impl<'a, 'tcx> PlaceRef<'a, 'tcx> {
1958+
pub fn iterate<R>(
1959+
&self,
1960+
op: impl FnOnce(&PlaceBase<'tcx>, ProjectionsIter<'_, 'tcx>) -> R,
1961+
) -> R {
1962+
Place::iterate_over(self.base, self.projection, op)
1963+
}
1964+
}
1965+
19571966
/// A linked list of projections running up the stack; begins with the
19581967
/// innermost projection and extends to the outermost (e.g., `a.b.c`
19591968
/// would have the place `b` with a "next" pointer to `b.c`).

src/librustc_codegen_ssa/mir/block.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
253253

254254
PassMode::Direct(_) | PassMode::Pair(..) => {
255255
let op =
256-
self.codegen_consume(&mut bx, &mir::Place::RETURN_PLACE);
256+
self.codegen_consume(&mut bx, &mir::Place::RETURN_PLACE.as_place_ref());
257257
if let Ref(llval, _, align) = op.val {
258258
bx.load(llval, align)
259259
} else {
@@ -314,7 +314,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
314314
return
315315
}
316316

317-
let place = self.codegen_place(&mut bx, location);
317+
let place = self.codegen_place(&mut bx, &location.as_place_ref());
318318
let (args1, args2);
319319
let mut args = if let Some(llextra) = place.llextra {
320320
args2 = [place.llval, llextra];
@@ -1135,7 +1135,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
11351135
}
11361136
}
11371137
} else {
1138-
self.codegen_place(bx, dest)
1138+
self.codegen_place(bx, &mir::PlaceRef {
1139+
base: &dest.base,
1140+
projection: &dest.projection,
1141+
})
11391142
};
11401143
if fn_ret.is_indirect() {
11411144
if dest.align < dest.layout.align.abi {
@@ -1168,7 +1171,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
11681171
LocalRef::Place(place) => self.codegen_transmute_into(bx, src, place),
11691172
LocalRef::UnsizedPlace(_) => bug!("transmute must not involve unsized locals"),
11701173
LocalRef::Operand(None) => {
1171-
let dst_layout = bx.layout_of(self.monomorphized_place_ty(dst));
1174+
let dst_layout = bx.layout_of(self.monomorphized_place_ty(&dst.as_place_ref()));
11721175
assert!(!dst_layout.ty.has_erasable_regions());
11731176
let place = PlaceRef::alloca(bx, dst_layout, "transmute_temp");
11741177
place.storage_live(bx);
@@ -1183,7 +1186,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
11831186
}
11841187
}
11851188
} else {
1186-
let dst = self.codegen_place(bx, dst);
1189+
let dst = self.codegen_place(bx, &dst.as_place_ref());
11871190
self.codegen_transmute_into(bx, src, dst);
11881191
}
11891192
}

src/librustc_codegen_ssa/mir/operand.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
380380
fn maybe_codegen_consume_direct(
381381
&mut self,
382382
bx: &mut Bx,
383-
place: &mir::Place<'tcx>
383+
place_ref: &mir::PlaceRef<'_, 'tcx>
384384
) -> Option<OperandRef<'tcx, Bx::Value>> {
385-
debug!("maybe_codegen_consume_direct(place={:?})", place);
385+
debug!("maybe_codegen_consume_direct(place_ref={:?})", place_ref);
386386

387-
place.iterate(|place_base, place_projection| {
387+
place_ref.iterate(|place_base, place_projection| {
388388
if let mir::PlaceBase::Local(index) = place_base {
389389
match self.locals[*index] {
390390
LocalRef::Operand(Some(mut o)) => {
@@ -413,7 +413,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
413413
Some(o)
414414
}
415415
LocalRef::Operand(None) => {
416-
bug!("use of {:?} before def", place);
416+
bug!("use of {:?} before def", place_ref);
417417
}
418418
LocalRef::Place(..) | LocalRef::UnsizedPlace(..) => {
419419
// watch out for locals that do not have an
@@ -430,25 +430,25 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
430430
pub fn codegen_consume(
431431
&mut self,
432432
bx: &mut Bx,
433-
place: &mir::Place<'tcx>
433+
place_ref: &mir::PlaceRef<'_, 'tcx>
434434
) -> OperandRef<'tcx, Bx::Value> {
435-
debug!("codegen_consume(place={:?})", place);
435+
debug!("codegen_consume(place_ref={:?})", place_ref);
436436

437-
let ty = self.monomorphized_place_ty(place);
437+
let ty = self.monomorphized_place_ty(place_ref);
438438
let layout = bx.cx().layout_of(ty);
439439

440440
// ZSTs don't require any actual memory access.
441441
if layout.is_zst() {
442442
return OperandRef::new_zst(bx, layout);
443443
}
444444

445-
if let Some(o) = self.maybe_codegen_consume_direct(bx, place) {
445+
if let Some(o) = self.maybe_codegen_consume_direct(bx, place_ref) {
446446
return o;
447447
}
448448

449449
// for most places, to consume them we just load them
450450
// out from their home
451-
let place = self.codegen_place(bx, place);
451+
let place = self.codegen_place(bx, place_ref);
452452
bx.load_operand(place)
453453
}
454454

@@ -462,7 +462,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
462462
match *operand {
463463
mir::Operand::Copy(ref place) |
464464
mir::Operand::Move(ref place) => {
465-
self.codegen_consume(bx, place)
465+
self.codegen_consume(bx, &place.as_place_ref())
466466
}
467467

468468
mir::Operand::Constant(ref constant) => {

src/librustc_codegen_ssa/mir/place.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -428,15 +428,14 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
428428
pub fn codegen_place(
429429
&mut self,
430430
bx: &mut Bx,
431-
place: &mir::Place<'tcx>
431+
place_ref: &mir::PlaceRef<'_, 'tcx>
432432
) -> PlaceRef<'tcx, Bx::Value> {
433-
debug!("codegen_place(place={:?})", place);
434-
433+
debug!("codegen_place(place_ref={:?})", place_ref);
435434
let cx = self.cx;
436435
let tcx = self.cx.tcx();
437436

438-
let result = match place {
439-
mir::Place {
437+
let result = match &place_ref {
438+
mir::PlaceRef {
440439
base: mir::PlaceBase::Local(index),
441440
projection: None,
442441
} => {
@@ -448,11 +447,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
448447
return bx.load_operand(place).deref(cx);
449448
}
450449
LocalRef::Operand(..) => {
451-
bug!("using operand local {:?} as place", place);
450+
bug!("using operand local {:?} as place", place_ref);
452451
}
453452
}
454453
}
455-
mir::Place {
454+
mir::PlaceRef {
456455
base: mir::PlaceBase::Static(box mir::Static {
457456
ty,
458457
kind: mir::StaticKind::Promoted(promoted),
@@ -485,7 +484,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
485484
}
486485
}
487486
}
488-
mir::Place {
487+
mir::PlaceRef {
489488
base: mir::PlaceBase::Static(box mir::Static {
490489
ty,
491490
kind: mir::StaticKind::Static(def_id),
@@ -498,27 +497,27 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
498497
let static_ = bx.get_static(*def_id);
499498
PlaceRef::new_thin_place(bx, static_, layout, layout.align.abi)
500499
},
501-
mir::Place {
500+
mir::PlaceRef {
502501
base,
503502
projection: Some(box mir::Projection {
504503
base: proj_base,
505504
elem: mir::ProjectionElem::Deref,
506505
}),
507506
} => {
508507
// Load the pointer from its location.
509-
self.codegen_consume(bx, &mir::Place {
510-
base: base.clone(),
511-
projection: proj_base.clone(),
508+
self.codegen_consume(bx, &mir::PlaceRef {
509+
base,
510+
projection: proj_base,
512511
}).deref(bx.cx())
513512
}
514-
mir::Place {
513+
mir::PlaceRef {
515514
base,
516515
projection: Some(projection),
517516
} => {
518517
// FIXME turn this recursion into iteration
519-
let cg_base = self.codegen_place(bx, &mir::Place {
520-
base: base.clone(),
521-
projection: projection.base.clone(),
518+
let cg_base = self.codegen_place(bx, &mir::PlaceRef {
519+
base,
520+
projection: &projection.base,
522521
});
523522

524523
match projection.elem {
@@ -573,13 +572,13 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
573572
}
574573
}
575574
};
576-
debug!("codegen_place(place={:?}) => {:?}", place, result);
575+
debug!("codegen_place(place={:?}) => {:?}", place_ref, result);
577576
result
578577
}
579578

580-
pub fn monomorphized_place_ty(&self, place: &mir::Place<'tcx>) -> Ty<'tcx> {
579+
pub fn monomorphized_place_ty(&self, place_ref: &mir::PlaceRef<'_, 'tcx>) -> Ty<'tcx> {
581580
let tcx = self.cx.tcx();
582-
let place_ty = place.ty(self.mir, tcx);
581+
let place_ty = mir::Place::ty_from(place_ref.base, place_ref.projection, self.mir, tcx);
583582
self.monomorphize(&place_ty.ty)
584583
}
585584
}

src/librustc_codegen_ssa/mir/rvalue.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
355355
}
356356

357357
mir::Rvalue::Ref(_, bk, ref place) => {
358-
let cg_place = self.codegen_place(&mut bx, place);
358+
let cg_place = self.codegen_place(&mut bx, &place.as_place_ref());
359359

360360
let ty = cg_place.layout.ty;
361361

@@ -446,7 +446,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
446446

447447
mir::Rvalue::Discriminant(ref place) => {
448448
let discr_ty = rvalue.ty(&*self.mir, bx.tcx());
449-
let discr = self.codegen_place(&mut bx, place)
449+
let discr = self.codegen_place(&mut bx, &place.as_place_ref())
450450
.codegen_get_discr(&mut bx, discr_ty);
451451
(bx, OperandRef {
452452
val: OperandValue::Immediate(discr),
@@ -527,7 +527,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
527527
}
528528
}
529529
// use common size calculation for non zero-sized types
530-
let cg_value = self.codegen_place(bx, place);
530+
let cg_value = self.codegen_place(bx, &place.as_place_ref());
531531
return cg_value.len(bx.cx());
532532
}
533533

src/librustc_codegen_ssa/mir/statement.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
4646
}
4747
}
4848
} else {
49-
let cg_dest = self.codegen_place(&mut bx, place);
49+
let cg_dest = self.codegen_place(&mut bx, &place.as_place_ref());
5050
self.codegen_rvalue(bx, cg_dest, rvalue)
5151
}
5252
}
5353
mir::StatementKind::SetDiscriminant{ref place, variant_index} => {
54-
self.codegen_place(&mut bx, place)
54+
self.codegen_place(&mut bx, &place.as_place_ref())
5555
.codegen_set_discr(&mut bx, variant_index);
5656
bx
5757
}
@@ -73,7 +73,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
7373
}
7474
mir::StatementKind::InlineAsm(ref asm) => {
7575
let outputs = asm.outputs.iter().map(|output| {
76-
self.codegen_place(&mut bx, output)
76+
self.codegen_place(&mut bx, &output.as_place_ref())
7777
}).collect();
7878

7979
let input_vals = asm.inputs.iter()

0 commit comments

Comments
 (0)