Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mir-opt] Turn on the ConstProp pass by default #66074

Merged
merged 1 commit into from
Nov 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions src/librustc_mir/transform/const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,8 +641,21 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
}
}

fn should_const_prop(&self) -> bool {
self.tcx.sess.opts.debugging_opts.mir_opt_level >= 2
fn should_const_prop(&mut self, op: OpTy<'tcx>) -> bool {
if self.tcx.sess.opts.debugging_opts.mir_opt_level >= 2 {
return true;
} else if self.tcx.sess.opts.debugging_opts.mir_opt_level == 0 {
return false;
}

match *op {
interpret::Operand::Immediate(Immediate::Scalar(ScalarMaybeUndef::Scalar(s))) =>
s.is_bits(),
interpret::Operand::Immediate(Immediate::ScalarPair(ScalarMaybeUndef::Scalar(l),
ScalarMaybeUndef::Scalar(r))) =>
l.is_bits() && r.is_bits(),
_ => false
}
wesleywiser marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -742,15 +755,15 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
if self.can_const_prop[local] {
trace!("propagated into {:?}", local);

if self.should_const_prop() {
let value =
self.get_const(local).expect("local was dead/uninitialized");
trace!("replacing {:?} with {:?}", rval, value);
self.replace_with_const(
rval,
value,
statement.source_info,
);
if let Some(value) = self.get_const(local) {
if self.should_const_prop(value) {
trace!("replacing {:?} with {:?}", rval, value);
self.replace_with_const(
rval,
value,
statement.source_info,
);
}
}
} else {
trace!("can't propagate into {:?}", local);
Expand Down Expand Up @@ -852,7 +865,7 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
&msg,
);
} else {
if self.should_const_prop() {
if self.should_const_prop(value) {
if let ScalarMaybeUndef::Scalar(scalar) = value_const {
*cond = self.operand_from_scalar(
scalar,
Expand All @@ -865,8 +878,8 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
}
},
TerminatorKind::SwitchInt { ref mut discr, switch_ty, .. } => {
if self.should_const_prop() {
if let Some(value) = self.eval_operand(&discr, source_info) {
if let Some(value) = self.eval_operand(&discr, source_info) {
if self.should_const_prop(value) {
if let ScalarMaybeUndef::Scalar(scalar) =
self.ecx.read_scalar(value).unwrap() {
*discr = self.operand_from_scalar(scalar, switch_ty, source_info.span);
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.0
// 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.0
// 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.0
// NO-OPT: ret i32 8
// SIZE-OPT: ret i32 8
// SPEED-OPT: ret i32 8
#[optimize(speed)]
Expand Down
10 changes: 5 additions & 5 deletions src/test/incremental/hashes/while_let_loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn change_loop_condition() {
}

#[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built, optimized_mir")]
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built")]
#[rustc_clean(cfg="cfail3")]
pub fn change_loop_condition() {
let mut _x = 0;
Expand All @@ -70,7 +70,7 @@ pub fn add_break() {
}

#[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built, optimized_mir, typeck_tables_of")]
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built, typeck_tables_of")]
#[rustc_clean(cfg="cfail3")]
pub fn add_break() {
let mut _x = 0;
Expand Down Expand Up @@ -141,7 +141,7 @@ pub fn change_break_label() {
}

#[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built, optimized_mir")]
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built")]
#[rustc_clean(cfg="cfail3")]
pub fn change_break_label() {
let mut _x = 0;
Expand Down Expand Up @@ -191,7 +191,7 @@ pub fn change_continue_label() {
}

#[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built, optimized_mir")]
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built")]
#[rustc_clean(cfg="cfail3")]
pub fn change_continue_label() {
let mut _x = 0;
Expand All @@ -216,7 +216,7 @@ pub fn change_continue_to_break() {
}

#[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built, optimized_mir")]
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built")]
#[rustc_clean(cfg="cfail3")]
pub fn change_continue_to_break() {
let mut _x = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/test/incremental/hashes/while_loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn change_loop_condition() {
}

#[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built, optimized_mir")]
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built")]
#[rustc_clean(cfg="cfail3")]
pub fn change_loop_condition() {
let mut _x = 0;
Expand Down Expand Up @@ -191,7 +191,7 @@ pub fn change_continue_label() {
}

#[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built, optimized_mir")]
#[rustc_clean(cfg="cfail2", except="HirBody, mir_built")]
#[rustc_clean(cfg="cfail3")]
pub fn change_continue_label() {
let mut _x = 0;
Expand Down