Skip to content

Commit

Permalink
[const-prop] Also propagate variables which have been propagated into
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleywiser committed May 25, 2019
1 parent 9184c91 commit e699063
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
24 changes: 23 additions & 1 deletion src/librustc_mir/transform/const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,28 @@ impl<'a, 'mir, 'tcx> ConstPropagator<'a, 'mir, 'tcx> {
fn should_const_prop(&self) -> bool {
self.tcx.sess.opts.debugging_opts.mir_opt_level >= 2
}

fn should_const_prop_local(&self, local: Local, rval: &Rvalue<'tcx>) -> bool {
trace!("should_const_prop(local={:?}, rval={:?})", local, rval);
if self.can_const_prop[local] {
return true;
}

// if we should not actually perform const propagation, then we're done
if !self.should_const_prop() {
return false;
}

// if `rval` is a read of a local that we already propagated into,
// then we can also propagate it
if let Rvalue::Use(Operand::Move(Place::Projection(proj))) = rval {
if let Place::Base(PlaceBase::Local(l)) = &proj.base {
return self.places[*l].is_some()
}
}

false
}
}

fn type_size_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
Expand Down Expand Up @@ -692,7 +714,7 @@ impl<'b, 'a, 'tcx> MutVisitor<'tcx> for ConstPropagator<'b, 'a, 'tcx> {
if let Some(value) = self.const_prop(rval, place_layout, statement.source_info) {
if let Place::Base(PlaceBase::Local(local)) = *place {
trace!("checking whether {:?} can be stored to {:?}", value, local);
if self.can_const_prop[local] {
if self.should_const_prop_local(local, rval) {
trace!("storing {:?} to {:?}", value, local);
assert!(self.places[local].is_none());
self.places[local] = Some(value);
Expand Down
6 changes: 3 additions & 3 deletions src/test/codegen/optimize-attr-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

// CHECK-LABEL: define i32 @nothing
// CHECK-SAME: [[NOTHING_ATTRS:#[0-9]+]]
// NO-OPT: ret i32 %1
// NO-OPT: ret i32 4
// SIZE-OPT: ret i32 4
// SPEEC-OPT: ret i32 4
#[no_mangle]
Expand All @@ -18,7 +18,7 @@ pub fn nothing() -> i32 {

// CHECK-LABEL: define i32 @size
// CHECK-SAME: [[SIZE_ATTRS:#[0-9]+]]
// NO-OPT: ret i32 %1
// NO-OPT: ret i32 6
// SIZE-OPT: ret i32 6
// SPEED-OPT: ret i32 6
#[optimize(size)]
Expand All @@ -31,7 +31,7 @@ pub fn size() -> i32 {
// NO-OPT-SAME: [[NOTHING_ATTRS]]
// SPEED-OPT-SAME: [[NOTHING_ATTRS]]
// SIZE-OPT-SAME: [[SPEED_ATTRS:#[0-9]+]]
// NO-OPT: ret i32 %1
// NO-OPT: ret i32 8
// SIZE-OPT: ret i32 8
// SPEED-OPT: ret i32 8
#[optimize(speed)]
Expand Down

0 comments on commit e699063

Please sign in to comment.