Skip to content

Commit 98ec48d

Browse files
committed
Codegen the return block last
1 parent 00335ab commit 98ec48d

File tree

1 file changed

+10
-2
lines changed
  • compiler/rustc_codegen_ssa/src/mir

1 file changed

+10
-2
lines changed

compiler/rustc_codegen_ssa/src/mir/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
260260
// Apply debuginfo to the newly allocated locals.
261261
fx.debug_introduce_locals(&mut start_bx);
262262

263+
// If we have multiple return terminators, create a special return block. If this block is
264+
// present, return terminator codegen will be a jump to it instead of a return.
263265
if mir
264266
.basic_blocks
265267
.iter()
@@ -268,15 +270,21 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
268270
> 1
269271
{
270272
let llbb = Bx::append_block(fx.cx, fx.llfn, "return");
271-
let mut bx = Bx::build(fx.cx, llbb);
272-
fx.codegen_return_terminator(&mut bx);
273273
fx.return_block = Some(llbb);
274274
}
275275

276276
// Codegen the body of each block using reverse postorder
277277
for (bb, _) in traversal::reverse_postorder(&mir) {
278278
fx.codegen_block(bb);
279279
}
280+
281+
// Actually codegen the return block. We need to delay until here because our return place
282+
// might not be generated yet, and will only be generated by handling codegen for the
283+
// assignment to it.
284+
if let Some(llbb) = fx.return_block {
285+
let mut bx = Bx::build(fx.cx, llbb);
286+
fx.codegen_return_terminator(&mut bx);
287+
}
280288
}
281289

282290
/// Produces, for each argument, a `Value` pointing at the

0 commit comments

Comments
 (0)