Skip to content

Commit b6bbc0f

Browse files
committed
Codegen goto return block as return terminator
1 parent ea1a999 commit b6bbc0f

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/base.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ pub(crate) fn trans_fn<'clif, 'tcx, B: Backend + 'static>(
169169
crate::optimize::optimize_function(tcx, instance, context, &cold_blocks, &mut clif_comments);
170170
});
171171

172+
// If the return block is not reachable, then the SSA builder may have inserted a `iconst.i128`
173+
// instruction, which doesn't have an encoding.
174+
context.compute_cfg();
175+
context.compute_domtree();
176+
context.eliminate_unreachable_code(cx.module.isa()).unwrap();
177+
172178
// Define function
173179
let module = &mut cx.module;
174180
tcx.sess.time(
@@ -265,6 +271,23 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, impl Backend>) {
265271

266272
match &bb_data.terminator().kind {
267273
TerminatorKind::Goto { target } => {
274+
if let TerminatorKind::Return = fx.mir[*target].terminator().kind {
275+
let mut can_immediately_return = true;
276+
for stmt in &fx.mir[*target].statements {
277+
if let StatementKind::StorageDead(_) = stmt.kind {
278+
} else {
279+
// FIXME Can sometimes happen, see rust-lang/rust#70531
280+
can_immediately_return = false;
281+
break;
282+
}
283+
}
284+
285+
if can_immediately_return {
286+
crate::abi::codegen_return(fx);
287+
continue;
288+
}
289+
}
290+
268291
let block = fx.get_block(*target);
269292
fx.bcx.ins().jump(block, &[]);
270293
}

0 commit comments

Comments
 (0)