1 //! Return value handling
5 use rustc_middle::ty::layout::FnAbiExt;
6 use rustc_target::abi::call::{ArgAbi, FnAbi, PassMode};
7 use smallvec::{smallvec, SmallVec};
9 /// Can the given type be returned into an ssa var or does it need to be returned on the stack.
10 pub(crate) fn can_return_to_ssa_var<'tcx>(
11 fx: &FunctionCx<'_, '_, 'tcx>,
12 func: &mir::Operand<'tcx>,
13 args: &[mir::Operand<'tcx>],
15 let fn_ty = fx.monomorphize(func.ty(fx.mir, fx.tcx));
17 fx.tcx.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), fn_ty.fn_sig(fx.tcx));
19 // Handle special calls like instrinsics and empty drop glue.
20 let instance = if let ty::FnDef(def_id, substs) = *fn_ty.kind() {
21 let instance = ty::Instance::resolve(fx.tcx, ty::ParamEnv::reveal_all(), def_id, substs)
24 .polymorphize(fx.tcx);
27 InstanceDef::Intrinsic(_) | InstanceDef::DropGlue(_, _) => {
36 let extra_args = &args[fn_sig.inputs().len()..];
37 let extra_args = extra_args
39 .map(|op_arg| fx.monomorphize(op_arg.ty(fx.mir, fx.tcx)))
41 let fn_abi = if let Some(instance) = instance {
42 FnAbi::of_instance(&RevealAllLayoutCx(fx.tcx), instance, &extra_args)
44 FnAbi::of_fn_ptr(&RevealAllLayoutCx(fx.tcx), fn_ty.fn_sig(fx.tcx), &extra_args)
46 match fn_abi.ret.mode {
47 PassMode::Ignore | PassMode::Direct(_) | PassMode::Pair(_, _) => true,
48 // FIXME Make it possible to return Cast and Indirect to an ssa var.
49 PassMode::Cast(_) | PassMode::Indirect { .. } => false,
53 /// Return a place where the return value of the current function can be written to. If necessary
54 /// this adds an extra parameter pointing to where the return value needs to be stored.
55 pub(super) fn codegen_return_param<'tcx>(
56 fx: &mut FunctionCx<'_, '_, 'tcx>,
57 ssa_analyzed: &rustc_index::vec::IndexVec<Local, crate::analyze::SsaKind>,
58 block_params_iter: &mut impl Iterator<Item = Value>,
60 let (ret_place, ret_param): (_, SmallVec<[_; 2]>) = match fx.fn_abi.as_ref().unwrap().ret.mode {
61 PassMode::Ignore => (CPlace::no_place(fx.fn_abi.as_ref().unwrap().ret.layout), smallvec![]),
62 PassMode::Direct(_) | PassMode::Pair(_, _) | PassMode::Cast(_) => {
63 let is_ssa = ssa_analyzed[RETURN_PLACE] == crate::analyze::SsaKind::Ssa;
65 super::make_local_place(
68 fx.fn_abi.as_ref().unwrap().ret.layout,
74 PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => {
75 let ret_param = block_params_iter.next().unwrap();
76 assert_eq!(fx.bcx.func.dfg.value_type(ret_param), pointer_ty(fx.tcx));
78 CPlace::for_ptr(Pointer::new(ret_param), fx.fn_abi.as_ref().unwrap().ret.layout),
82 PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => {
83 unreachable!("unsized return value")
87 #[cfg(not(debug_assertions))]
90 #[cfg(debug_assertions)]
91 crate::abi::comments::add_arg_comment(
97 fx.fn_abi.as_ref().unwrap().ret.mode,
98 fx.fn_abi.as_ref().unwrap().ret.layout,
104 /// Invokes the closure with if necessary a value representing the return pointer. When the closure
105 /// returns the call return value(s) if any are written to the correct place.
106 pub(super) fn codegen_with_call_return_arg<'tcx, T>(
107 fx: &mut FunctionCx<'_, '_, 'tcx>,
108 ret_arg_abi: &ArgAbi<'tcx, Ty<'tcx>>,
109 ret_place: Option<CPlace<'tcx>>,
110 f: impl FnOnce(&mut FunctionCx<'_, '_, 'tcx>, Option<Value>) -> (Inst, T),
112 let return_ptr = match ret_arg_abi.mode {
113 PassMode::Ignore => None,
114 PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => match ret_place {
115 Some(ret_place) => Some(ret_place.to_ptr().get_addr(fx)),
116 None => Some(fx.bcx.ins().iconst(fx.pointer_type, 43)), // FIXME allocate temp stack slot
118 PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => {
119 unreachable!("unsized return value")
121 PassMode::Direct(_) | PassMode::Pair(_, _) | PassMode::Cast(_) => None,
124 let (call_inst, meta) = f(fx, return_ptr);
126 match ret_arg_abi.mode {
127 PassMode::Ignore => {}
128 PassMode::Direct(_) => {
129 if let Some(ret_place) = ret_place {
130 let ret_val = fx.bcx.inst_results(call_inst)[0];
131 ret_place.write_cvalue(fx, CValue::by_val(ret_val, ret_arg_abi.layout));
134 PassMode::Pair(_, _) => {
135 if let Some(ret_place) = ret_place {
136 let ret_val_a = fx.bcx.inst_results(call_inst)[0];
137 let ret_val_b = fx.bcx.inst_results(call_inst)[1];
138 ret_place.write_cvalue(
140 CValue::by_val_pair(ret_val_a, ret_val_b, ret_arg_abi.layout),
144 PassMode::Cast(cast) => {
145 if let Some(ret_place) = ret_place {
148 .inst_results(call_inst)
151 .collect::<SmallVec<[Value; 2]>>();
153 super::pass_mode::from_casted_value(fx, &results, ret_place.layout(), cast);
154 ret_place.write_cvalue(fx, result);
157 PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => {}
158 PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => {
159 unreachable!("unsized return value")
166 /// Codegen a return instruction with the right return value(s) if any.
167 pub(crate) fn codegen_return(fx: &mut FunctionCx<'_, '_, '_>) {
168 match fx.fn_abi.as_ref().unwrap().ret.mode {
169 PassMode::Ignore | PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => {
170 fx.bcx.ins().return_(&[]);
172 PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => {
173 unreachable!("unsized return value")
175 PassMode::Direct(_) => {
176 let place = fx.get_local_place(RETURN_PLACE);
177 let ret_val = place.to_cvalue(fx).load_scalar(fx);
178 fx.bcx.ins().return_(&[ret_val]);
180 PassMode::Pair(_, _) => {
181 let place = fx.get_local_place(RETURN_PLACE);
182 let (ret_val_a, ret_val_b) = place.to_cvalue(fx).load_scalar_pair(fx);
183 fx.bcx.ins().return_(&[ret_val_a, ret_val_b]);
185 PassMode::Cast(cast) => {
186 let place = fx.get_local_place(RETURN_PLACE);
187 let ret_val = place.to_cvalue(fx);
188 let ret_vals = super::pass_mode::to_casted_value(fx, ret_val, cast);
189 fx.bcx.ins().return_(&ret_vals);