Skip to content

Commit 33a5928

Browse files
committed
auto merge of #9656 : thestinger/rust/immediate, r=alexcrichton
fn foo() -> (u32, u8, u8, u8, u8) { (4, 5, 6, 7, 8) } Before: ; Function Attrs: nounwind uwtable define void @_ZN3foo18hbb616262f874f8daf4v0.0E({ i32, i8, i8, i8, i8 }* noalias nocapture sret, { i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #0 { "function top level": %2 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 0 store i32 4, i32* %2, align 4 %3 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 1 store i8 5, i8* %3, align 4 %4 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 2 store i8 6, i8* %4, align 1 %5 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 3 store i8 7, i8* %5, align 2 %6 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 4 store i8 8, i8* %6, align 1 ret void } After: ; Function Attrs: nounwind readnone uwtable define { i32, i8, i8, i8, i8 } @_ZN3foo18hbb616262f874f8daf4v0.0E({ i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #0 { "function top level": ret { i32, i8, i8, i8, i8 } { i32 4, i8 5, i8 6, i8 7, i8 8 } }
2 parents 1d6c011 + 5e4ae4f commit 33a5928

File tree

8 files changed

+118
-75
lines changed

8 files changed

+118
-75
lines changed

src/librustc/middle/trans/base.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ pub fn decl_rust_fn(ccx: &mut CrateContext, inputs: &[ty::t], output: ty::t,
236236
_ => ()
237237
}
238238

239-
let uses_outptr = type_of::return_uses_outptr(ccx.tcx, output);
239+
let uses_outptr = type_of::return_uses_outptr(ccx, output);
240240
let offset = if uses_outptr { 2 } else { 1 };
241241

242242
for (i, &arg_ty) in inputs.iter().enumerate() {
@@ -1117,13 +1117,13 @@ pub fn do_spill_noroot(cx: @mut Block, v: ValueRef) -> ValueRef {
11171117

11181118
pub fn spill_if_immediate(cx: @mut Block, v: ValueRef, t: ty::t) -> ValueRef {
11191119
let _icx = push_ctxt("spill_if_immediate");
1120-
if type_is_immediate(cx.tcx(), t) { return do_spill(cx, v, t); }
1120+
if type_is_immediate(cx.ccx(), t) { return do_spill(cx, v, t); }
11211121
return v;
11221122
}
11231123

11241124
pub fn load_if_immediate(cx: @mut Block, v: ValueRef, t: ty::t) -> ValueRef {
11251125
let _icx = push_ctxt("load_if_immediate");
1126-
if type_is_immediate(cx.tcx(), t) { return Load(cx, v); }
1126+
if type_is_immediate(cx.ccx(), t) { return Load(cx, v); }
11271127
return v;
11281128
}
11291129

@@ -1656,7 +1656,7 @@ pub fn mk_return_basic_block(llfn: ValueRef) -> BasicBlockRef {
16561656
// slot where the return value of the function must go.
16571657
pub fn make_return_pointer(fcx: @mut FunctionContext, output_type: ty::t) -> ValueRef {
16581658
unsafe {
1659-
if type_of::return_uses_outptr(fcx.ccx.tcx, output_type) {
1659+
if type_of::return_uses_outptr(fcx.ccx, output_type) {
16601660
llvm::LLVMGetParam(fcx.llfn, 0)
16611661
} else {
16621662
let lloutputtype = type_of::type_of(fcx.ccx, output_type);
@@ -1696,7 +1696,7 @@ pub fn new_fn_ctxt_w_id(ccx: @mut CrateContext,
16961696
ty::subst_tps(ccx.tcx, substs.tys, substs.self_ty, output_type)
16971697
}
16981698
};
1699-
let uses_outptr = type_of::return_uses_outptr(ccx.tcx, substd_output_type);
1699+
let uses_outptr = type_of::return_uses_outptr(ccx, substd_output_type);
17001700
let debug_context = debuginfo::create_function_debug_context(ccx, id, param_substs, llfndecl);
17011701

17021702
let fcx = @mut FunctionContext {
@@ -1808,7 +1808,7 @@ pub fn copy_args_to_allocas(fcx: @mut FunctionContext,
18081808
match fcx.llself {
18091809
Some(slf) => {
18101810
let self_val = if slf.is_copy
1811-
&& datum::appropriate_mode(bcx.tcx(), slf.t).is_by_value() {
1811+
&& datum::appropriate_mode(bcx.ccx(), slf.t).is_by_value() {
18121812
let tmp = BitCast(bcx, slf.v, type_of(bcx.ccx(), slf.t));
18131813
let alloc = alloc_ty(bcx, slf.t, "__self");
18141814
Store(bcx, tmp, alloc);
@@ -1838,7 +1838,7 @@ pub fn copy_args_to_allocas(fcx: @mut FunctionContext,
18381838
// This alloca should be optimized away by LLVM's mem-to-reg pass in
18391839
// the event it's not truly needed.
18401840
// only by value if immediate:
1841-
let llarg = if datum::appropriate_mode(bcx.tcx(), arg_ty).is_by_value() {
1841+
let llarg = if datum::appropriate_mode(bcx.ccx(), arg_ty).is_by_value() {
18421842
let alloc = alloc_ty(bcx, arg_ty, "__arg");
18431843
Store(bcx, raw_llarg, alloc);
18441844
alloc

src/librustc/middle/trans/callee.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ pub fn trans_call_inner(in_cx: @mut Block,
655655
// not care about the result, just make a stack slot.
656656
let opt_llretslot = match dest {
657657
None => {
658-
assert!(!type_of::return_uses_outptr(in_cx.tcx(), ret_ty));
658+
assert!(!type_of::return_uses_outptr(in_cx.ccx(), ret_ty));
659659
None
660660
}
661661
Some(expr::SaveIn(dst)) => Some(dst),
@@ -685,7 +685,7 @@ pub fn trans_call_inner(in_cx: @mut Block,
685685

686686
// Push the out-pointer if we use an out-pointer for this
687687
// return type, otherwise push "undef".
688-
if type_of::return_uses_outptr(in_cx.tcx(), ret_ty) {
688+
if type_of::return_uses_outptr(in_cx.ccx(), ret_ty) {
689689
llargs.push(opt_llretslot.unwrap());
690690
}
691691

@@ -711,7 +711,7 @@ pub fn trans_call_inner(in_cx: @mut Block,
711711
// any attributes with ABI implications directly to the call instruction. Right now, the
712712
// only attribute we need to worry about is `sret`.
713713
let mut attrs = ~[];
714-
if type_of::return_uses_outptr(in_cx.tcx(), ret_ty) {
714+
if type_of::return_uses_outptr(in_cx.ccx(), ret_ty) {
715715
attrs.push((1, StructRetAttribute));
716716
}
717717

@@ -734,7 +734,7 @@ pub fn trans_call_inner(in_cx: @mut Block,
734734
// the return value, copy it into llretslot.
735735
match opt_llretslot {
736736
Some(llretslot) => {
737-
if !type_of::return_uses_outptr(bcx.tcx(), ret_ty) &&
737+
if !type_of::return_uses_outptr(bcx.ccx(), ret_ty) &&
738738
!ty::type_is_voidish(ret_ty)
739739
{
740740
Store(bcx, llret, llretslot);
@@ -758,7 +758,7 @@ pub fn trans_call_inner(in_cx: @mut Block,
758758
// drop the temporary slot we made.
759759
match dest {
760760
None => {
761-
assert!(!type_of::return_uses_outptr(bcx.tcx(), ret_ty));
761+
assert!(!type_of::return_uses_outptr(bcx.ccx(), ret_ty));
762762
}
763763
Some(expr::Ignore) => {
764764
// drop the value if it is not being saved.
@@ -871,7 +871,7 @@ pub fn trans_arg_expr(bcx: @mut Block,
871871
DontAutorefArg => {
872872
let need_scratch = ty::type_needs_drop(bcx.tcx(), arg_datum.ty) ||
873873
(bcx.expr_is_lval(arg_expr) &&
874-
arg_datum.appropriate_mode(bcx.tcx()).is_by_ref());
874+
arg_datum.appropriate_mode(bcx.ccx()).is_by_ref());
875875

876876
let arg_datum = if need_scratch {
877877
let scratch = scratch_datum(bcx, arg_datum.ty, "__self", false);

src/librustc/middle/trans/common.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,37 @@ use syntax::{ast, ast_map};
4646

4747
pub use middle::trans::context::CrateContext;
4848

49-
fn type_is_newtype_immediate(cx: ty::ctxt, ty: ty::t) -> bool {
49+
fn type_is_newtype_immediate(ccx: &mut CrateContext, ty: ty::t) -> bool {
5050
match ty::get(ty).sty {
5151
ty::ty_struct(def_id, ref substs) => {
52-
let fields = ty::struct_fields(cx, def_id, substs);
52+
let fields = ty::struct_fields(ccx.tcx, def_id, substs);
5353
fields.len() == 1 &&
5454
fields[0].ident.name == token::special_idents::unnamed_field.name &&
55-
type_is_immediate(cx, fields[0].mt.ty)
55+
type_is_immediate(ccx, fields[0].mt.ty)
5656
}
5757
_ => false
5858
}
5959
}
6060

61-
pub fn type_is_immediate(cx: ty::ctxt, ty: ty::t) -> bool {
62-
ty::type_is_scalar(ty) || ty::type_is_boxed(ty) ||
61+
pub fn type_is_immediate(ccx: &mut CrateContext, ty: ty::t) -> bool {
62+
use middle::trans::machine::llsize_of_alloc;
63+
use middle::trans::type_of::sizing_type_of;
64+
let tcx = ccx.tcx;
65+
let simple = ty::type_is_scalar(ty) || ty::type_is_boxed(ty) ||
6366
ty::type_is_unique(ty) || ty::type_is_region_ptr(ty) ||
64-
type_is_newtype_immediate(cx, ty) ||
65-
ty::type_is_simd(cx, ty)
67+
type_is_newtype_immediate(ccx, ty) ||
68+
ty::type_is_simd(tcx, ty);
69+
if simple {
70+
return true;
71+
}
72+
match ty::get(ty).sty {
73+
// FIXME: #9651: small `ty_struct` and `ty_enum` should also be immediate
74+
ty::ty_tup(*) => {
75+
let llty = sizing_type_of(ccx, ty);
76+
llsize_of_alloc(ccx, llty) <= llsize_of_alloc(ccx, ccx.int_type)
77+
}
78+
_ => false
79+
}
6680
}
6781

6882
pub fn gensym_name(name: &str) -> (Ident, path_elt) {

src/librustc/middle/trans/datum.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ pub fn scratch_datum(bcx: @mut Block, ty: ty::t, name: &str, zero: bool) -> Datu
188188
Datum { val: scratch, ty: ty, mode: ByRef(RevokeClean) }
189189
}
190190

191-
pub fn appropriate_mode(tcx: ty::ctxt, ty: ty::t) -> DatumMode {
191+
pub fn appropriate_mode(ccx: &mut CrateContext, ty: ty::t) -> DatumMode {
192192
/*!
193193
* Indicates the "appropriate" mode for this value,
194194
* which is either by ref or by value, depending
@@ -197,7 +197,7 @@ pub fn appropriate_mode(tcx: ty::ctxt, ty: ty::t) -> DatumMode {
197197

198198
if ty::type_is_voidish(ty) {
199199
ByValue
200-
} else if type_is_immediate(tcx, ty) {
200+
} else if type_is_immediate(ccx, ty) {
201201
ByValue
202202
} else {
203203
ByRef(RevokeClean)
@@ -505,18 +505,18 @@ impl Datum {
505505
}
506506
}
507507

508-
pub fn appropriate_mode(&self, tcx: ty::ctxt) -> DatumMode {
508+
pub fn appropriate_mode(&self, ccx: &mut CrateContext) -> DatumMode {
509509
/*! See the `appropriate_mode()` function */
510510

511-
appropriate_mode(tcx, self.ty)
511+
appropriate_mode(ccx, self.ty)
512512
}
513513

514514
pub fn to_appropriate_llval(&self, bcx: @mut Block) -> ValueRef {
515515
/*!
516516
*
517517
* Yields an llvalue with the `appropriate_mode()`. */
518518

519-
match self.appropriate_mode(bcx.tcx()) {
519+
match self.appropriate_mode(bcx.ccx()) {
520520
ByValue => self.to_value_llval(bcx),
521521
ByRef(_) => self.to_ref_llval(bcx)
522522
}
@@ -527,7 +527,7 @@ impl Datum {
527527
*
528528
* Yields a datum with the `appropriate_mode()`. */
529529

530-
match self.appropriate_mode(bcx.tcx()) {
530+
match self.appropriate_mode(bcx.ccx()) {
531531
ByValue => self.to_value_datum(bcx),
532532
ByRef(_) => self.to_ref_datum(bcx)
533533
}
@@ -667,7 +667,7 @@ impl Datum {
667667
ByValue => {
668668
// Actually, this case cannot happen right
669669
// now, because enums are never immediate.
670-
assert!(type_is_immediate(bcx.tcx(), ty));
670+
assert!(type_is_immediate(bcx.ccx(), ty));
671671
(Some(Datum {ty: ty, ..*self}), bcx)
672672
}
673673
};
@@ -699,7 +699,7 @@ impl Datum {
699699
)
700700
}
701701
ByValue => {
702-
assert!(type_is_immediate(bcx.tcx(), ty));
702+
assert!(type_is_immediate(bcx.ccx(), ty));
703703
(
704704
Some(Datum {
705705
val: ExtractValue(bcx, self.val, 0),

src/librustc/middle/trans/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ pub fn trans_to_datum(bcx: @mut Block, expr: &ast::Expr) -> DatumBlock {
290290
debug2!("add_env(closure_ty={})", closure_ty.repr(tcx));
291291
let scratch = scratch_datum(bcx, closure_ty, "__adjust", false);
292292
let llfn = GEPi(bcx, scratch.val, [0u, abi::fn_field_code]);
293-
assert_eq!(datum.appropriate_mode(tcx), ByValue);
293+
assert_eq!(datum.appropriate_mode(bcx.ccx()), ByValue);
294294
Store(bcx, datum.to_appropriate_llval(bcx), llfn);
295295
let llenv = GEPi(bcx, scratch.val, [0u, abi::fn_field_box]);
296296
Store(bcx, base::null_env_ptr(bcx.ccx()), llenv);

src/librustc/middle/trans/foreign.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ pub fn trans_rust_fn_with_foreign_abi(ccx: @mut CrateContext,
492492

493493
// Push Rust return pointer, using null if it will be unused.
494494
let rust_uses_outptr =
495-
type_of::return_uses_outptr(tcx, tys.fn_sig.output);
495+
type_of::return_uses_outptr(ccx, tys.fn_sig.output);
496496
let return_alloca: Option<ValueRef>;
497497
let llrust_ret_ty = tys.llsig.llret_ty;
498498
let llrust_retptr_ty = llrust_ret_ty.ptr_to();
@@ -702,7 +702,7 @@ fn foreign_signature(ccx: &mut CrateContext, fn_sig: &ty::FnSig)
702702
LlvmSignature {
703703
llarg_tys: llarg_tys,
704704
llret_ty: llret_ty,
705-
sret: type_of::return_uses_outptr(ccx.tcx, fn_sig.output),
705+
sret: type_of::return_uses_outptr(ccx, fn_sig.output),
706706
}
707707
}
708708

0 commit comments

Comments
 (0)