Skip to content

Commit a51e3e4

Browse files
committed
trans_arg_expr: Omit extra copies for rvalues
rvalues aren't going to be used anywhere but as the argument, so there's no point in copying them. LLVM used to eliminate the copy later, but why bother emitting it in the first place?
1 parent 2765811 commit a51e3e4

File tree

1 file changed

+24
-51
lines changed

1 file changed

+24
-51
lines changed

src/librustc/middle/trans/callee.rs

+24-51
Original file line numberDiff line numberDiff line change
@@ -834,60 +834,33 @@ pub fn trans_arg_expr(bcx: @mut Block,
834834
val = arg_datum.to_ref_llval(bcx);
835835
}
836836
DontAutorefArg => {
837-
match self_mode {
837+
let need_scratch = ty::type_needs_drop(bcx.tcx(), arg_datum.ty) ||
838+
(bcx.expr_is_lval(arg_expr) &&
839+
arg_datum.appropriate_mode(bcx.tcx()).is_by_ref());
840+
841+
let arg_datum = if need_scratch {
842+
let scratch = scratch_datum(bcx, arg_datum.ty, "__self", false);
843+
arg_datum.store_to_datum(bcx, INIT, scratch);
844+
845+
// Technically, ownership of val passes to the callee.
846+
// However, we must cleanup should we fail before the
847+
// callee is actually invoked.
848+
scratch.add_clean(bcx);
849+
temp_cleanups.push(scratch.val);
850+
851+
scratch
852+
} else {
853+
arg_datum
854+
};
855+
856+
val = match self_mode {
838857
ty::ByRef => {
839-
// This assertion should really be valid, but because
840-
// the explicit self code currently passes by-ref, it
841-
// does not hold.
842-
//
843-
//assert !bcx.ccx().maps.moves_map.contains_key(
844-
// &arg_expr.id);
845-
debug!("by ref arg with type %s, storing to scratch",
846-
bcx.ty_to_str(arg_datum.ty));
847-
let scratch = scratch_datum(bcx, arg_datum.ty,
848-
"__self", false);
849-
850-
arg_datum.store_to_datum(bcx,
851-
INIT,
852-
scratch);
853-
854-
// Technically, ownership of val passes to the callee.
855-
// However, we must cleanup should we fail before the
856-
// callee is actually invoked.
857-
scratch.add_clean(bcx);
858-
temp_cleanups.push(scratch.val);
859-
860-
val = scratch.to_ref_llval(bcx);
858+
debug!("by ref arg with type %s", bcx.ty_to_str(arg_datum.ty));
859+
arg_datum.to_ref_llval(bcx)
861860
}
862861
ty::ByCopy => {
863-
if ty::type_needs_drop(bcx.tcx(), arg_datum.ty) ||
864-
arg_datum.appropriate_mode(bcx.tcx()).is_by_ref() {
865-
debug!("by copy arg with type %s, storing to scratch",
866-
bcx.ty_to_str(arg_datum.ty));
867-
let scratch = scratch_datum(bcx, arg_datum.ty,
868-
"__arg", false);
869-
870-
arg_datum.store_to_datum(bcx,
871-
INIT,
872-
scratch);
873-
874-
// Technically, ownership of val passes to the callee.
875-
// However, we must cleanup should we fail before the
876-
// callee is actually invoked.
877-
scratch.add_clean(bcx);
878-
temp_cleanups.push(scratch.val);
879-
880-
match scratch.appropriate_mode(bcx.tcx()) {
881-
ByValue => val = Load(bcx, scratch.val),
882-
ByRef(_) => val = scratch.val,
883-
}
884-
} else {
885-
debug!("by copy arg with type %s", bcx.ty_to_str(arg_datum.ty));
886-
match arg_datum.mode {
887-
ByRef(_) => val = Load(bcx, arg_datum.val),
888-
ByValue => val = arg_datum.val,
889-
}
890-
}
862+
debug!("by copy arg with type %s", bcx.ty_to_str(arg_datum.ty));
863+
arg_datum.to_appropriate_llval(bcx)
891864
}
892865
}
893866
}

0 commit comments

Comments
 (0)