Skip to content

Commit d1180af

Browse files
committed
Generate block containing return lazily instead
1 parent 1356572 commit d1180af

File tree

4 files changed

+33
-28
lines changed

4 files changed

+33
-28
lines changed

src/librustc/mir/repr.rs

-7
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,6 @@ impl BasicBlock {
213213
BasicBlock(index as u32)
214214
}
215215

216-
/// Returns a BasicBlock with index 1. This is actual end block (containing
217-
/// the Return terminator) only during the building of MIR and should not be
218-
/// used outside that.
219-
pub const fn end_block() -> BasicBlock {
220-
BasicBlock(1)
221-
}
222-
223216
/// Extract the index.
224217
pub fn index(self) -> usize {
225218
self.0 as usize

src/librustc_mir/build/expr/into.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,8 @@ impl<'a,'tcx> Builder<'a,'tcx> {
262262
}
263263
};
264264
let extent = this.extent_of_return_scope();
265-
this.exit_scope(expr_span, extent, block, BasicBlock::end_block());
265+
let return_block = this.return_block();
266+
this.exit_scope(expr_span, extent, block, return_block);
266267
this.cfg.start_new_block().unit()
267268
}
268269
ExprKind::Call { ty, fun, args } => {

src/librustc_mir/build/mod.rs

+31-19
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,35 @@ pub struct Builder<'a, 'tcx: 'a> {
2424

2525
fn_span: Span,
2626

27-
// the current set of scopes, updated as we traverse;
28-
// see the `scope` module for more details
27+
/// the current set of scopes, updated as we traverse;
28+
/// see the `scope` module for more details
2929
scopes: Vec<scope::Scope<'tcx>>,
3030

31-
// for each scope, a span of blocks that defines it;
32-
// we track these for use in region and borrow checking,
33-
// but these are liable to get out of date once optimization
34-
// begins. They are also hopefully temporary, and will be
35-
// no longer needed when we adopt graph-based regions.
31+
/// for each scope, a span of blocks that defines it;
32+
/// we track these for use in region and borrow checking,
33+
/// but these are liable to get out of date once optimization
34+
/// begins. They are also hopefully temporary, and will be
35+
/// no longer needed when we adopt graph-based regions.
3636
scope_auxiliary: ScopeAuxiliaryVec,
3737

38-
// the current set of loops; see the `scope` module for more
39-
// details
38+
/// the current set of loops; see the `scope` module for more
39+
/// details
4040
loop_scopes: Vec<scope::LoopScope>,
4141

42-
// the vector of all scopes that we have created thus far;
43-
// we track this for debuginfo later
42+
/// the vector of all scopes that we have created thus far;
43+
/// we track this for debuginfo later
4444
scope_datas: Vec<ScopeData>,
4545

4646
var_decls: Vec<VarDecl<'tcx>>,
4747
var_indices: FnvHashMap<ast::NodeId, u32>,
4848
temp_decls: Vec<TempDecl<'tcx>>,
4949
unit_temp: Option<Lvalue<'tcx>>,
5050

51-
// cached block with a RESUME terminator; we create this at the
52-
// first panic
51+
/// cached block with the RESUME terminator; this is created
52+
/// when first set of cleanups are built.
5353
cached_resume_block: Option<BasicBlock>,
54+
/// cached block with the RETURN terminator
55+
cached_return_block: Option<BasicBlock>,
5456
}
5557

5658
struct CFG<'tcx> {
@@ -180,12 +182,10 @@ pub fn construct<'a,'tcx>(hir: Cx<'a,'tcx>,
180182
var_indices: FnvHashMap(),
181183
unit_temp: None,
182184
cached_resume_block: None,
185+
cached_return_block: None
183186
};
184187

185188
assert_eq!(builder.cfg.start_new_block(), START_BLOCK);
186-
let end_block = builder.cfg.start_new_block();
187-
assert_eq!(end_block, BasicBlock::end_block());
188-
189189

190190
let mut arg_decls = None; // assigned to `Some` in closures below
191191
let call_site_extent =
@@ -205,11 +205,12 @@ pub fn construct<'a,'tcx>(hir: Cx<'a,'tcx>,
205205
block.unit()
206206
}));
207207

208+
let return_block = builder.return_block();
208209
builder.cfg.terminate(block, call_site_scope_id, span,
209-
TerminatorKind::Goto { target: end_block });
210-
builder.cfg.terminate(end_block, call_site_scope_id, span,
210+
TerminatorKind::Goto { target: return_block });
211+
builder.cfg.terminate(return_block, call_site_scope_id, span,
211212
TerminatorKind::Return);
212-
end_block.unit()
213+
return_block.unit()
213214
});
214215

215216
assert!(
@@ -290,6 +291,17 @@ impl<'a,'tcx> Builder<'a,'tcx> {
290291
}
291292
}
292293
}
294+
295+
fn return_block(&mut self) -> BasicBlock {
296+
match self.cached_return_block {
297+
Some(rb) => rb,
298+
None => {
299+
let rb = self.cfg.start_new_block();
300+
self.cached_return_block = Some(rb);
301+
rb
302+
}
303+
}
304+
}
293305
}
294306

295307
///////////////////////////////////////////////////////////////////////////

src/librustc_mir/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ Rust MIR: a lowered representation of Rust. Also: an experiment!
2121
#![unstable(feature = "rustc_private", issue = "27812")]
2222

2323
#![feature(box_patterns)]
24-
#![feature(const_fn)]
2524
#![feature(rustc_private)]
2625
#![feature(staged_api)]
2726
#![feature(question_mark)]

0 commit comments

Comments
 (0)