Skip to content

Commit 2b093b7

Browse files
committed
mir: Allow copy-propagation of function arguments
1 parent fd5dc05 commit 2b093b7

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

src/librustc_mir/transform/copy_prop.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
//! future.
3131
3232
use def_use::DefUseAnalysis;
33-
use rustc::mir::{Constant, Local, Location, Lvalue, Mir, Operand, Rvalue, StatementKind};
33+
use rustc::mir::{Constant, Local, LocalKind, Location, Lvalue, Mir, Operand, Rvalue, StatementKind};
3434
use rustc::mir::transform::{MirPass, MirSource, Pass};
3535
use rustc::mir::visit::MutVisitor;
3636
use rustc::ty::TyCtxt;
@@ -123,7 +123,7 @@ impl<'tcx> MirPass<'tcx> for CopyPropagation {
123123
local == dest_local => {
124124
let maybe_action = match *operand {
125125
Operand::Consume(ref src_lvalue) => {
126-
Action::local_copy(&def_use_analysis, src_lvalue)
126+
Action::local_copy(&mir, &def_use_analysis, src_lvalue)
127127
}
128128
Operand::Constant(ref src_constant) => {
129129
Action::constant(src_constant)
@@ -160,7 +160,7 @@ enum Action<'tcx> {
160160
}
161161

162162
impl<'tcx> Action<'tcx> {
163-
fn local_copy(def_use_analysis: &DefUseAnalysis, src_lvalue: &Lvalue<'tcx>)
163+
fn local_copy(mir: &Mir<'tcx>, def_use_analysis: &DefUseAnalysis, src_lvalue: &Lvalue<'tcx>)
164164
-> Option<Action<'tcx>> {
165165
// The source must be a local.
166166
let src_local = if let Lvalue::Local(local) = *src_lvalue {
@@ -196,7 +196,9 @@ impl<'tcx> Action<'tcx> {
196196
// SRC = X;
197197
// USE(SRC);
198198
let src_def_count = src_use_info.def_count_not_including_drop();
199-
if src_def_count != 1 {
199+
// allow function arguments to be propagated
200+
if src_def_count > 1 ||
201+
(src_def_count == 0 && mir.local_kind(src_local) != LocalKind::Arg) {
200202
debug!(" Can't copy-propagate local: {} defs of src",
201203
src_use_info.def_count_not_including_drop());
202204
return None

src/test/mir-opt/copy_propagation.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn test(x: u32) -> u32 {
12+
let y = x;
13+
y
14+
}
15+
16+
fn main() { }
17+
18+
// END RUST SOURCE
19+
// START rustc.node4.CopyPropagation.before.mir
20+
// bb0: {
21+
// _2 = _1;
22+
// _4 = _2;
23+
// _3 = _4;
24+
// _5 = _3;
25+
// _0 = _5;
26+
// return;
27+
// }
28+
// END rustc.node4.CopyPropagation.before.mir
29+
// START rustc.node4.CopyPropagation.after.mir
30+
// bb0: {
31+
// _0 = _1;
32+
// return;
33+
// }
34+
// END rustc.node4.CopyPropagation.after.mir

0 commit comments

Comments
 (0)