Skip to content

Commit e699063

Browse files
committed
[const-prop] Also propagate variables which have been propagated into
1 parent 9184c91 commit e699063

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/librustc_mir/transform/const_prop.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,28 @@ impl<'a, 'mir, 'tcx> ConstPropagator<'a, 'mir, 'tcx> {
596596
fn should_const_prop(&self) -> bool {
597597
self.tcx.sess.opts.debugging_opts.mir_opt_level >= 2
598598
}
599+
600+
fn should_const_prop_local(&self, local: Local, rval: &Rvalue<'tcx>) -> bool {
601+
trace!("should_const_prop(local={:?}, rval={:?})", local, rval);
602+
if self.can_const_prop[local] {
603+
return true;
604+
}
605+
606+
// if we should not actually perform const propagation, then we're done
607+
if !self.should_const_prop() {
608+
return false;
609+
}
610+
611+
// if `rval` is a read of a local that we already propagated into,
612+
// then we can also propagate it
613+
if let Rvalue::Use(Operand::Move(Place::Projection(proj))) = rval {
614+
if let Place::Base(PlaceBase::Local(l)) = &proj.base {
615+
return self.places[*l].is_some()
616+
}
617+
}
618+
619+
false
620+
}
599621
}
600622

601623
fn type_size_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
@@ -692,7 +714,7 @@ impl<'b, 'a, 'tcx> MutVisitor<'tcx> for ConstPropagator<'b, 'a, 'tcx> {
692714
if let Some(value) = self.const_prop(rval, place_layout, statement.source_info) {
693715
if let Place::Base(PlaceBase::Local(local)) = *place {
694716
trace!("checking whether {:?} can be stored to {:?}", value, local);
695-
if self.can_const_prop[local] {
717+
if self.should_const_prop_local(local, rval) {
696718
trace!("storing {:?} to {:?}", value, local);
697719
assert!(self.places[local].is_none());
698720
self.places[local] = Some(value);

src/test/codegen/optimize-attr-1.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
// CHECK-LABEL: define i32 @nothing
1010
// CHECK-SAME: [[NOTHING_ATTRS:#[0-9]+]]
11-
// NO-OPT: ret i32 %1
11+
// NO-OPT: ret i32 4
1212
// SIZE-OPT: ret i32 4
1313
// SPEEC-OPT: ret i32 4
1414
#[no_mangle]
@@ -18,7 +18,7 @@ pub fn nothing() -> i32 {
1818

1919
// CHECK-LABEL: define i32 @size
2020
// CHECK-SAME: [[SIZE_ATTRS:#[0-9]+]]
21-
// NO-OPT: ret i32 %1
21+
// NO-OPT: ret i32 6
2222
// SIZE-OPT: ret i32 6
2323
// SPEED-OPT: ret i32 6
2424
#[optimize(size)]
@@ -31,7 +31,7 @@ pub fn size() -> i32 {
3131
// NO-OPT-SAME: [[NOTHING_ATTRS]]
3232
// SPEED-OPT-SAME: [[NOTHING_ATTRS]]
3333
// SIZE-OPT-SAME: [[SPEED_ATTRS:#[0-9]+]]
34-
// NO-OPT: ret i32 %1
34+
// NO-OPT: ret i32 8
3535
// SIZE-OPT: ret i32 8
3636
// SPEED-OPT: ret i32 8
3737
#[optimize(speed)]

0 commit comments

Comments
 (0)