Skip to content

Allow copy-propagation of function arguments #38332

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

Merged
merged 1 commit into from
Dec 14, 2016
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
10 changes: 6 additions & 4 deletions src/librustc_mir/transform/copy_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
//! future.

use def_use::DefUseAnalysis;
use rustc::mir::{Constant, Local, Location, Lvalue, Mir, Operand, Rvalue, StatementKind};
use rustc::mir::{Constant, Local, LocalKind, Location, Lvalue, Mir, Operand, Rvalue, StatementKind};
use rustc::mir::transform::{MirPass, MirSource, Pass};
use rustc::mir::visit::MutVisitor;
use rustc::ty::TyCtxt;
Expand Down Expand Up @@ -123,7 +123,7 @@ impl<'tcx> MirPass<'tcx> for CopyPropagation {
local == dest_local => {
let maybe_action = match *operand {
Operand::Consume(ref src_lvalue) => {
Action::local_copy(&def_use_analysis, src_lvalue)
Action::local_copy(&mir, &def_use_analysis, src_lvalue)
}
Operand::Constant(ref src_constant) => {
Action::constant(src_constant)
Expand Down Expand Up @@ -160,7 +160,7 @@ enum Action<'tcx> {
}

impl<'tcx> Action<'tcx> {
fn local_copy(def_use_analysis: &DefUseAnalysis, src_lvalue: &Lvalue<'tcx>)
fn local_copy(mir: &Mir<'tcx>, def_use_analysis: &DefUseAnalysis, src_lvalue: &Lvalue<'tcx>)
-> Option<Action<'tcx>> {
// The source must be a local.
let src_local = if let Lvalue::Local(local) = *src_lvalue {
Expand Down Expand Up @@ -196,7 +196,9 @@ impl<'tcx> Action<'tcx> {
// SRC = X;
// USE(SRC);
let src_def_count = src_use_info.def_count_not_including_drop();
if src_def_count != 1 {
// allow function arguments to be propagated
if src_def_count > 1 ||
(src_def_count == 0 && mir.local_kind(src_local) != LocalKind::Arg) {
debug!(" Can't copy-propagate local: {} defs of src",
src_use_info.def_count_not_including_drop());
return None
Expand Down
34 changes: 34 additions & 0 deletions src/test/mir-opt/copy_propagation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn test(x: u32) -> u32 {
let y = x;
y
}

fn main() { }

// END RUST SOURCE
// START rustc.node4.CopyPropagation.before.mir
// bb0: {
// _2 = _1;
// _4 = _2;
// _3 = _4;
// _5 = _3;
// _0 = _5;
// return;
// }
// END rustc.node4.CopyPropagation.before.mir
// START rustc.node4.CopyPropagation.after.mir
// bb0: {
// _0 = _1;
// return;
// }
// END rustc.node4.CopyPropagation.after.mir