about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2016-12-14 06:26:21 +0000
committerbors <bors@rust-lang.org>2016-12-14 06:26:21 +0000
commit5d3ec6b0a0f6f95412235dc5a6b5d416187a01f3 (patch)
treec6a6b029221302d59bd7088497ccf8565eaf6a04
parentaa7a2e9e61cfb9469c7eb88308fa2e1a087ebdb4 (diff)
parent2b093b702aec27bcc12785a76e38222a04043365 (diff)
downloadrust-5d3ec6b0a0f6f95412235dc5a6b5d416187a01f3.tar.gz
rust-5d3ec6b0a0f6f95412235dc5a6b5d416187a01f3.zip
Auto merge of #38332 - bluss:copy-prop-arguments, r=eddyb
Allow copy-propagation of function arguments

Allow propagating function argument locals in copy propagation.
-rw-r--r--src/librustc_mir/transform/copy_prop.rs10
-rw-r--r--src/test/mir-opt/copy_propagation.rs34
2 files changed, 40 insertions, 4 deletions
diff --git a/src/librustc_mir/transform/copy_prop.rs b/src/librustc_mir/transform/copy_prop.rs
index d16b51adbaf..3cbf8573ba9 100644
--- a/src/librustc_mir/transform/copy_prop.rs
+++ b/src/librustc_mir/transform/copy_prop.rs
@@ -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;
@@ -122,7 +122,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)
@@ -159,7 +159,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 {
@@ -195,7 +195,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
diff --git a/src/test/mir-opt/copy_propagation.rs b/src/test/mir-opt/copy_propagation.rs
new file mode 100644
index 00000000000..26b042d0343
--- /dev/null
+++ b/src/test/mir-opt/copy_propagation.rs
@@ -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