Skip to content

Commit 5011ae4

Browse files
committed
Refactor call terminator to always hold a destination place
1 parent d9025ce commit 5011ae4

File tree

6 files changed

+66
-71
lines changed

6 files changed

+66
-71
lines changed

src/abi/mod.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,14 @@ pub(crate) fn codegen_terminator_call<'tcx>(
312312
source_info: mir::SourceInfo,
313313
func: &Operand<'tcx>,
314314
args: &[Operand<'tcx>],
315-
mir_dest: Option<(Place<'tcx>, BasicBlock)>,
315+
destination: Place<'tcx>,
316+
target: Option<BasicBlock>,
316317
) {
317318
let fn_ty = fx.monomorphize(func.ty(fx.mir, fx.tcx));
318319
let fn_sig =
319320
fx.tcx.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), fn_ty.fn_sig(fx.tcx));
320321

321-
let destination = mir_dest.map(|(place, bb)| (codegen_place(fx, place), bb));
322+
let ret_place = codegen_place(fx, destination);
322323

323324
// Handle special calls like instrinsics and empty drop glue.
324325
let instance = if let ty::FnDef(def_id, substs) = *fn_ty.kind() {
@@ -333,7 +334,8 @@ pub(crate) fn codegen_terminator_call<'tcx>(
333334
&fx.tcx.symbol_name(instance).name,
334335
substs,
335336
args,
336-
destination,
337+
ret_place,
338+
target,
337339
);
338340
return;
339341
}
@@ -344,14 +346,15 @@ pub(crate) fn codegen_terminator_call<'tcx>(
344346
fx,
345347
instance,
346348
args,
347-
destination,
349+
ret_place,
350+
target,
348351
source_info,
349352
);
350353
return;
351354
}
352355
InstanceDef::DropGlue(_, None) => {
353356
// empty drop glue - a nop.
354-
let (_, dest) = destination.expect("Non terminating drop_in_place_real???");
357+
let dest = target.expect("Non terminating drop_in_place_real???");
355358
let ret_block = fx.get_block(dest);
356359
fx.bcx.ins().jump(ret_block, &[]);
357360
return;
@@ -377,7 +380,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
377380
.unwrap_or(false);
378381
if is_cold {
379382
fx.bcx.set_cold_block(fx.bcx.current_block().unwrap());
380-
if let Some((_place, destination_block)) = destination {
383+
if let Some(destination_block) = target {
381384
fx.bcx.set_cold_block(fx.get_block(destination_block));
382385
}
383386
}
@@ -459,7 +462,6 @@ pub(crate) fn codegen_terminator_call<'tcx>(
459462
}
460463
};
461464

462-
let ret_place = destination.map(|(place, _)| place);
463465
self::returning::codegen_with_call_return_arg(fx, &fn_abi.ret, ret_place, |fx, return_ptr| {
464466
let call_args = return_ptr
465467
.into_iter()
@@ -511,7 +513,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
511513
call_inst
512514
});
513515

514-
if let Some((_, dest)) = destination {
516+
if let Some(dest) = target {
515517
let ret_block = fx.get_block(dest);
516518
fx.bcx.ins().jump(ret_block, &[]);
517519
} else {

src/abi/returning.rs

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,22 @@ pub(super) fn codegen_return_param<'tcx>(
5656
pub(super) fn codegen_with_call_return_arg<'tcx>(
5757
fx: &mut FunctionCx<'_, '_, 'tcx>,
5858
ret_arg_abi: &ArgAbi<'tcx, Ty<'tcx>>,
59-
ret_place: Option<CPlace<'tcx>>,
59+
ret_place: CPlace<'tcx>,
6060
f: impl FnOnce(&mut FunctionCx<'_, '_, 'tcx>, Option<Value>) -> Inst,
6161
) {
6262
let (ret_temp_place, return_ptr) = match ret_arg_abi.mode {
6363
PassMode::Ignore => (None, None),
64-
PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => match ret_place {
65-
Some(ret_place) if matches!(ret_place.inner(), CPlaceInner::Addr(_, None)) => {
64+
PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => {
65+
if matches!(ret_place.inner(), CPlaceInner::Addr(_, None)) {
6666
// This is an optimization to prevent unnecessary copies of the return value when
6767
// the return place is already a memory place as opposed to a register.
6868
// This match arm can be safely removed.
6969
(None, Some(ret_place.to_ptr().get_addr(fx)))
70-
}
71-
_ => {
70+
} else {
7271
let place = CPlace::new_stack_slot(fx, ret_arg_abi.layout);
7372
(Some(place), Some(place.to_ptr().get_addr(fx)))
7473
}
75-
},
74+
}
7675
PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => {
7776
unreachable!("unsized return value")
7877
}
@@ -84,39 +83,25 @@ pub(super) fn codegen_with_call_return_arg<'tcx>(
8483
match ret_arg_abi.mode {
8584
PassMode::Ignore => {}
8685
PassMode::Direct(_) => {
87-
if let Some(ret_place) = ret_place {
88-
let ret_val = fx.bcx.inst_results(call_inst)[0];
89-
ret_place.write_cvalue(fx, CValue::by_val(ret_val, ret_arg_abi.layout));
90-
}
86+
let ret_val = fx.bcx.inst_results(call_inst)[0];
87+
ret_place.write_cvalue(fx, CValue::by_val(ret_val, ret_arg_abi.layout));
9188
}
9289
PassMode::Pair(_, _) => {
93-
if let Some(ret_place) = ret_place {
94-
let ret_val_a = fx.bcx.inst_results(call_inst)[0];
95-
let ret_val_b = fx.bcx.inst_results(call_inst)[1];
96-
ret_place.write_cvalue(
97-
fx,
98-
CValue::by_val_pair(ret_val_a, ret_val_b, ret_arg_abi.layout),
99-
);
100-
}
90+
let ret_val_a = fx.bcx.inst_results(call_inst)[0];
91+
let ret_val_b = fx.bcx.inst_results(call_inst)[1];
92+
ret_place
93+
.write_cvalue(fx, CValue::by_val_pair(ret_val_a, ret_val_b, ret_arg_abi.layout));
10194
}
10295
PassMode::Cast(cast) => {
103-
if let Some(ret_place) = ret_place {
104-
let results = fx
105-
.bcx
106-
.inst_results(call_inst)
107-
.iter()
108-
.copied()
109-
.collect::<SmallVec<[Value; 2]>>();
110-
let result =
111-
super::pass_mode::from_casted_value(fx, &results, ret_place.layout(), cast);
112-
ret_place.write_cvalue(fx, result);
113-
}
96+
let results =
97+
fx.bcx.inst_results(call_inst).iter().copied().collect::<SmallVec<[Value; 2]>>();
98+
let result =
99+
super::pass_mode::from_casted_value(fx, &results, ret_place.layout(), cast);
100+
ret_place.write_cvalue(fx, result);
114101
}
115102
PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => {
116-
if let (Some(ret_place), Some(ret_temp_place)) = (ret_place, ret_temp_place) {
117-
// Both ret_place and ret_temp_place must be Some. If ret_place is None, this is
118-
// a non-returning call. If ret_temp_place is None, it is not necessary to copy the
119-
// return value.
103+
if let Some(ret_temp_place) = ret_temp_place {
104+
// If ret_temp_place is None, it is not necessary to copy the return value.
120105
let ret_temp_value = ret_temp_place.to_cvalue(fx);
121106
ret_place.write_cvalue(fx, ret_temp_value);
122107
}

src/base.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, '_>) {
393393
func,
394394
args,
395395
destination,
396+
target,
396397
fn_span,
397398
cleanup: _,
398399
from_hir_call: _,
@@ -404,6 +405,7 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, '_>) {
404405
func,
405406
args,
406407
*destination,
408+
*target,
407409
)
408410
});
409411
}

src/constant.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,8 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
542542
| TerminatorKind::FalseEdge { .. }
543543
| TerminatorKind::FalseUnwind { .. } => unreachable!(),
544544
TerminatorKind::InlineAsm { .. } => return None,
545-
TerminatorKind::Call { destination: Some((call_place, _)), .. }
546-
if call_place == place =>
545+
TerminatorKind::Call { destination, target: Some(_), .. }
546+
if destination == place =>
547547
{
548548
return None;
549549
}

src/intrinsics/llvm.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ pub(crate) fn codegen_llvm_intrinsic_call<'tcx>(
1010
intrinsic: &str,
1111
_substs: SubstsRef<'tcx>,
1212
args: &[mir::Operand<'tcx>],
13-
destination: Option<(CPlace<'tcx>, BasicBlock)>,
13+
ret: CPlace<'tcx>,
14+
target: Option<BasicBlock>,
1415
) {
15-
let ret = destination.unwrap().0;
16-
1716
intrinsic_match! {
1817
fx, intrinsic, args,
1918
_ => {
@@ -126,7 +125,7 @@ pub(crate) fn codegen_llvm_intrinsic_call<'tcx>(
126125
};
127126
}
128127

129-
let dest = destination.expect("all llvm intrinsics used by stdlib should return").1;
128+
let dest = target.expect("all llvm intrinsics used by stdlib should return");
130129
let ret_block = fx.get_block(dest);
131130
fx.bcx.ins().jump(ret_block, &[]);
132131
}

src/intrinsics/mod.rs

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -217,35 +217,42 @@ pub(crate) fn codegen_intrinsic_call<'tcx>(
217217
fx: &mut FunctionCx<'_, '_, 'tcx>,
218218
instance: Instance<'tcx>,
219219
args: &[mir::Operand<'tcx>],
220-
destination: Option<(CPlace<'tcx>, BasicBlock)>,
220+
destination: CPlace<'tcx>,
221+
target: Option<BasicBlock>,
221222
source_info: mir::SourceInfo,
222223
) {
223224
let intrinsic = fx.tcx.item_name(instance.def_id());
224225
let substs = instance.substs;
225226

226-
let ret = match destination {
227-
Some((place, _)) => place,
228-
None => {
229-
// Insert non returning intrinsics here
230-
match intrinsic {
231-
sym::abort => {
232-
fx.bcx.ins().trap(TrapCode::User(0));
233-
}
234-
sym::transmute => {
235-
crate::base::codegen_panic(fx, "Transmuting to uninhabited type.", source_info);
236-
}
237-
_ => unimplemented!("unsupported instrinsic {}", intrinsic),
227+
let target = if let Some(target) = target {
228+
target
229+
} else {
230+
// Insert non returning intrinsics here
231+
match intrinsic {
232+
sym::abort => {
233+
fx.bcx.ins().trap(TrapCode::User(0));
238234
}
239-
return;
235+
sym::transmute => {
236+
crate::base::codegen_panic(fx, "Transmuting to uninhabited type.", source_info);
237+
}
238+
_ => unimplemented!("unsupported instrinsic {}", intrinsic),
240239
}
240+
return;
241241
};
242242

243243
if intrinsic.as_str().starts_with("simd_") {
244-
self::simd::codegen_simd_intrinsic_call(fx, intrinsic, substs, args, ret, source_info.span);
245-
let ret_block = fx.get_block(destination.expect("SIMD intrinsics don't diverge").1);
244+
self::simd::codegen_simd_intrinsic_call(
245+
fx,
246+
intrinsic,
247+
substs,
248+
args,
249+
destination,
250+
source_info.span,
251+
);
252+
let ret_block = fx.get_block(target);
246253
fx.bcx.ins().jump(ret_block, &[]);
247-
} else if codegen_float_intrinsic_call(fx, intrinsic, args, ret) {
248-
let ret_block = fx.get_block(destination.expect("Float intrinsics don't diverge").1);
254+
} else if codegen_float_intrinsic_call(fx, intrinsic, args, destination) {
255+
let ret_block = fx.get_block(target);
249256
fx.bcx.ins().jump(ret_block, &[]);
250257
} else {
251258
codegen_regular_intrinsic_call(
@@ -254,9 +261,9 @@ pub(crate) fn codegen_intrinsic_call<'tcx>(
254261
intrinsic,
255262
substs,
256263
args,
257-
ret,
258-
source_info,
259264
destination,
265+
Some(target),
266+
source_info,
260267
);
261268
}
262269
}
@@ -339,8 +346,8 @@ fn codegen_regular_intrinsic_call<'tcx>(
339346
substs: SubstsRef<'tcx>,
340347
args: &[mir::Operand<'tcx>],
341348
ret: CPlace<'tcx>,
349+
destination: Option<BasicBlock>,
342350
source_info: mir::SourceInfo,
343-
destination: Option<(CPlace<'tcx>, BasicBlock)>,
344351
) {
345352
let usize_layout = fx.layout_of(fx.tcx.types.usize);
346353

@@ -761,7 +768,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
761768
if fx.tcx.is_compiler_builtins(LOCAL_CRATE) {
762769
// special case for compiler-builtins to avoid having to patch it
763770
crate::trap::trap_unimplemented(fx, "128bit atomics not yet supported");
764-
let ret_block = fx.get_block(destination.unwrap().1);
771+
let ret_block = fx.get_block(destination.unwrap());
765772
fx.bcx.ins().jump(ret_block, &[]);
766773
return;
767774
} else {
@@ -789,7 +796,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
789796
if fx.tcx.is_compiler_builtins(LOCAL_CRATE) {
790797
// special case for compiler-builtins to avoid having to patch it
791798
crate::trap::trap_unimplemented(fx, "128bit atomics not yet supported");
792-
let ret_block = fx.get_block(destination.unwrap().1);
799+
let ret_block = fx.get_block(destination.unwrap());
793800
fx.bcx.ins().jump(ret_block, &[]);
794801
return;
795802
} else {
@@ -1130,6 +1137,6 @@ fn codegen_regular_intrinsic_call<'tcx>(
11301137
};
11311138
}
11321139

1133-
let ret_block = fx.get_block(destination.unwrap().1);
1140+
let ret_block = fx.get_block(destination.unwrap());
11341141
fx.bcx.ins().jump(ret_block, &[]);
11351142
}

0 commit comments

Comments
 (0)