Skip to content

Commit 67283ea

Browse files
committed
Omit unused implicit argument if return type is immediate.
1 parent dbc5758 commit 67283ea

File tree

11 files changed

+132
-111
lines changed

11 files changed

+132
-111
lines changed

src/librustc/middle/trans/base.rs

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,7 +1593,7 @@ pub fn new_fn_ctxt_w_id(ccx: @CrateContext,
15931593
impl_id: Option<ast::def_id>,
15941594
param_substs: Option<@param_substs>,
15951595
sp: Option<span>)
1596-
-> fn_ctxt {
1596+
-> (fn_ctxt, bool) {
15971597
for param_substs.each |p| { p.validate(); }
15981598

15991599
debug!("new_fn_ctxt_w_id(path=%s, id=%?, impl_id=%?, \
@@ -1611,19 +1611,21 @@ pub fn new_fn_ctxt_w_id(ccx: @CrateContext,
16111611
ty::subst_tps(ccx.tcx, substs.tys, substs.self_ty, output_type)
16121612
}
16131613
};
1614-
let is_immediate = ty::type_is_immediate(substd_output_type);
1614+
let imm = ty::type_is_immediate(substd_output_type);
16151615

16161616
let fcx = @mut fn_ctxt_ {
16171617
llfn: llfndecl,
1618-
llenv: unsafe { llvm::LLVMGetParam(llfndecl, 1u as c_uint) },
1618+
llenv: unsafe {
1619+
llvm::LLVMGetParam(llfndecl, arg_env(imm) as c_uint)
1620+
},
16191621
llretptr: None,
16201622
llstaticallocas: llbbs.sa,
16211623
llloadenv: None,
16221624
llreturn: llbbs.rt,
16231625
llself: None,
16241626
personality: None,
16251627
loop_ret: None,
1626-
has_immediate_return_value: is_immediate,
1628+
has_immediate_return_value: imm,
16271629
llargs: @mut HashMap::new(),
16281630
lllocals: @mut HashMap::new(),
16291631
llupvars: @mut HashMap::new(),
@@ -1636,15 +1638,15 @@ pub fn new_fn_ctxt_w_id(ccx: @CrateContext,
16361638
};
16371639

16381640
fcx.llretptr = Some(make_return_pointer(fcx, substd_output_type));
1639-
fcx
1641+
(fcx, imm)
16401642
}
16411643

16421644
pub fn new_fn_ctxt(ccx: @CrateContext,
16431645
path: path,
16441646
llfndecl: ValueRef,
16451647
output_type: ty::t,
16461648
sp: Option<span>)
1647-
-> fn_ctxt {
1649+
-> (fn_ctxt, bool) {
16481650
new_fn_ctxt_w_id(ccx, path, llfndecl, -1, output_type, None, None, sp)
16491651
}
16501652

@@ -1664,7 +1666,8 @@ pub fn new_fn_ctxt(ccx: @CrateContext,
16641666
// field of the fn_ctxt with
16651667
pub fn create_llargs_for_fn_args(cx: fn_ctxt,
16661668
self_arg: self_arg,
1667-
args: &[ast::arg])
1669+
args: &[ast::arg],
1670+
ret_imm: bool)
16681671
-> ~[ValueRef] {
16691672
let _icx = cx.insn_ctxt("create_llargs_for_fn_args");
16701673

@@ -1690,7 +1693,7 @@ pub fn create_llargs_for_fn_args(cx: fn_ctxt,
16901693
// llvm::LLVMGetParam for each argument.
16911694
vec::from_fn(args.len(), |i| {
16921695
unsafe {
1693-
let arg_n = first_real_arg + i;
1696+
let arg_n = arg_pos(ret_imm, i);
16941697
let arg = &args[i];
16951698
let llarg = llvm::LLVMGetParam(cx.llfn, arg_n as c_uint);
16961699

@@ -1829,15 +1832,15 @@ pub fn trans_closure(ccx: @CrateContext,
18291832
param_substs.repr(ccx.tcx));
18301833

18311834
// Set up arguments to the function.
1832-
let fcx = new_fn_ctxt_w_id(ccx,
1833-
path,
1834-
llfndecl,
1835-
id,
1836-
output_type,
1837-
impl_id,
1838-
param_substs,
1839-
Some(body.span));
1840-
let raw_llargs = create_llargs_for_fn_args(fcx, self_arg, decl.inputs);
1835+
let (fcx, imm) = new_fn_ctxt_w_id(ccx,
1836+
path,
1837+
llfndecl,
1838+
id,
1839+
output_type,
1840+
impl_id,
1841+
param_substs,
1842+
Some(body.span));
1843+
let raw_llargs = create_llargs_for_fn_args(fcx, self_arg, decl.inputs, imm);
18411844

18421845
// Set the fixed stack segment flag if necessary.
18431846
if attr::attrs_contains_name(attributes, "fixed_stack_segment") {
@@ -1962,16 +1965,16 @@ pub fn trans_enum_variant(ccx: @CrateContext,
19621965
ty_param_substs,
19631966
None,
19641967
ty::node_id_to_type(ccx.tcx, enum_id));
1965-
let fcx = new_fn_ctxt_w_id(ccx,
1966-
~[],
1967-
llfndecl,
1968-
variant.node.id,
1969-
enum_ty,
1970-
None,
1971-
param_substs,
1972-
None);
1973-
1974-
let raw_llargs = create_llargs_for_fn_args(fcx, no_self, fn_args);
1968+
let (fcx, imm) = new_fn_ctxt_w_id(ccx,
1969+
~[],
1970+
llfndecl,
1971+
variant.node.id,
1972+
enum_ty,
1973+
None,
1974+
param_substs,
1975+
None);
1976+
1977+
let raw_llargs = create_llargs_for_fn_args(fcx, no_self, fn_args, imm);
19751978
let bcx = top_scope_block(fcx, None), lltop = bcx.llbb;
19761979
let arg_tys = ty::ty_fn_args(node_id_type(bcx, variant.node.id));
19771980
let bcx = copy_args_to_allocas(fcx, bcx, fn_args, raw_llargs, arg_tys);
@@ -2041,16 +2044,16 @@ pub fn trans_tuple_struct(ccx: @CrateContext,
20412044
ty_to_str(ccx.tcx, ctor_ty)))
20422045
};
20432046
2044-
let fcx = new_fn_ctxt_w_id(ccx,
2045-
~[],
2046-
llfndecl,
2047-
ctor_id,
2048-
tup_ty,
2049-
None,
2050-
param_substs,
2051-
None);
2047+
let (fcx, imm) = new_fn_ctxt_w_id(ccx,
2048+
~[],
2049+
llfndecl,
2050+
ctor_id,
2051+
tup_ty,
2052+
None,
2053+
param_substs,
2054+
None);
20522055
2053-
let raw_llargs = create_llargs_for_fn_args(fcx, no_self, fn_args);
2056+
let raw_llargs = create_llargs_for_fn_args(fcx, no_self, fn_args, imm);
20542057
20552058
let bcx = top_scope_block(fcx, None);
20562059
let lltop = bcx.llbb;
@@ -2293,19 +2296,21 @@ pub fn create_entry_wrapper(ccx: @CrateContext,
22932296
22942297
fn create_main(ccx: @CrateContext, main_llfn: ValueRef) -> ValueRef {
22952298
let nt = ty::mk_nil();
2299+
22962300
let llfty = type_of_fn(ccx, [], nt);
22972301
let llfdecl = decl_fn(ccx.llmod, "_rust_main",
22982302
lib::llvm::CCallConv, llfty);
22992303
2300-
let fcx = new_fn_ctxt(ccx, ~[], llfdecl, nt, None);
2304+
let (fcx, _) = new_fn_ctxt(ccx, ~[], llfdecl, nt, None);
23012305
23022306
let bcx = top_scope_block(fcx, None);
23032307
let lltop = bcx.llbb;
23042308
23052309
// Call main.
2306-
let lloutputarg = C_null(T_ptr(T_i8()));
2307-
let llenvarg = unsafe { llvm::LLVMGetParam(llfdecl, 1 as c_uint) };
2308-
let args = ~[lloutputarg, llenvarg];
2310+
let llenvarg = unsafe {
2311+
llvm::LLVMGetParam(llfdecl, arg_env(true) as c_uint)
2312+
};
2313+
let args = ~[llenvarg];
23092314
let llresult = Call(bcx, main_llfn, args);
23102315
Store(bcx, llresult, fcx.llretptr.get());
23112316
@@ -2347,8 +2352,6 @@ pub fn create_entry_wrapper(ccx: @CrateContext,
23472352
trans_external_path(ccx, start_def_id, start_fn_type);
23482353
}
23492354
2350-
let retptr = llvm::LLVMBuildAlloca(bld, T_i8(), noname());
2351-
23522355
let crate_map = ccx.crate_map;
23532356
let opaque_crate_map = llvm::LLVMBuildPointerCast(bld,
23542357
crate_map,
@@ -2371,7 +2374,6 @@ pub fn create_entry_wrapper(ccx: @CrateContext,
23712374
bld, rust_main, T_ptr(T_i8()), noname());
23722375
23732376
~[
2374-
retptr,
23752377
C_null(T_opaque_box_ptr(ccx)),
23762378
opaque_rust_main,
23772379
llvm::LLVMGetParam(llfn, 0),
@@ -2384,7 +2386,6 @@ pub fn create_entry_wrapper(ccx: @CrateContext,
23842386
debug!("using user-defined start fn");
23852387
let args = {
23862388
~[
2387-
retptr,
23882389
C_null(T_opaque_box_ptr(ccx)),
23892390
llvm::LLVMGetParam(llfn, 0 as c_uint),
23902391
llvm::LLVMGetParam(llfn, 1 as c_uint),

src/librustc/middle/trans/cabi.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ pub impl FnType {
132132
bcx: block,
133133
ret_ty: TypeRef,
134134
llwrapfn: ValueRef,
135-
llargbundle: ValueRef) {
135+
llargbundle: ValueRef,
136+
ret_imm: bool) {
136137
let mut atys = /*bad*/copy self.arg_tys;
137138
let mut attrs = /*bad*/copy self.attrs;
138139
let mut j = 0u;

src/librustc/middle/trans/callee.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -510,11 +510,7 @@ pub fn trans_call_inner(in_cx: block,
510510

511511
let mut llargs = ~[];
512512

513-
if ty::type_is_immediate(ret_ty) {
514-
unsafe {
515-
llargs.push(llvm::LLVMGetUndef(T_ptr(T_i8())));
516-
}
517-
} else {
513+
if !ty::type_is_immediate(ret_ty) {
518514
llargs.push(llretslot);
519515
}
520516

src/librustc/middle/trans/common.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -660,8 +660,26 @@ pub fn mk_block(llbb: BasicBlockRef, parent: Option<block>, kind: block_kind,
660660
@mut block_(llbb, parent, kind, is_lpad, node_info, fcx)
661661
}
662662

663-
// First two args are retptr, env
664-
pub static first_real_arg: uint = 2u;
663+
pub fn arg_pos(ret_imm: bool, arg: uint) -> uint {
664+
if ret_imm {
665+
arg + 1u
666+
} else {
667+
arg + 2u
668+
}
669+
}
670+
671+
pub fn arg_out(ret_imm: bool) -> uint {
672+
assert!(ret_imm);
673+
0u
674+
}
675+
676+
pub fn arg_env(ret_imm: bool) -> uint {
677+
if !ret_imm {
678+
1u
679+
} else {
680+
0u
681+
}
682+
}
665683

666684
pub struct Result {
667685
bcx: block,
@@ -962,8 +980,7 @@ pub fn T_tydesc(targ_cfg: @session::config) -> TypeRef {
962980
let tydescpp = T_ptr(T_ptr(tydesc));
963981
let pvoid = T_ptr(T_i8());
964982
let glue_fn_ty =
965-
T_ptr(T_fn([T_ptr(T_nil()), T_ptr(T_nil()), tydescpp,
966-
pvoid], T_void()));
983+
T_ptr(T_fn([T_ptr(T_nil()), tydescpp, pvoid], T_void()));
967984

968985
let int_type = T_int(targ_cfg);
969986
let elems =

0 commit comments

Comments
 (0)